diff --git a/cmd/community/loadapp.go b/cmd/community/loadapp.go index 1714c64d70..b26ad1d437 100644 --- a/cmd/community/loadapp.go +++ b/cmd/community/loadapp.go @@ -43,7 +43,7 @@ func LoadApp(cmd *cobra.Command, args []string) error { }) applet := runtime.Applet{} - err = applet.LoadWithInitializers(script, src, nil, initializers...) + err = applet.LoadWithInitializers("", script, src, nil, initializers...) if err != nil { return fmt.Errorf("failed to load applet: %w", err) } diff --git a/cmd/community/validateicons.go b/cmd/community/validateicons.go index d20bd470c0..3c654f62ad 100644 --- a/cmd/community/validateicons.go +++ b/cmd/community/validateicons.go @@ -47,7 +47,7 @@ func ValidateIcons(cmd *cobra.Command, args []string) error { }) applet := runtime.Applet{} - err = applet.LoadWithInitializers(args[0], src, nil, initializers...) + err = applet.LoadWithInitializers("", args[0], src, nil, initializers...) if err != nil { return fmt.Errorf("failed to load applet: %w", err) } diff --git a/cmd/profile.go b/cmd/profile.go index 1199366847..406b34f458 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -114,7 +114,7 @@ func ProfileApp(script string, config map[string]string) (*pprof_profile.Profile }) applet := runtime.Applet{} - err = applet.LoadWithInitializers(script, src, nil, initializers...) + err = applet.LoadWithInitializers("", script, src, nil, initializers...) if err != nil { return nil, fmt.Errorf("failed to load applet: %w", err) } diff --git a/cmd/render.go b/cmd/render.go index fc391702ac..004facf77e 100644 --- a/cmd/render.go +++ b/cmd/render.go @@ -139,7 +139,7 @@ func render(cmd *cobra.Command, args []string) error { runtime.InitCache(cache) applet := runtime.Applet{} - err = applet.LoadWithInitializers(script, src, nil, initializers...) + err = applet.LoadWithInitializers("", script, src, nil, initializers...) if err != nil { return fmt.Errorf("failed to load applet: %w", err) } diff --git a/docs/gen_widget_imgs.go b/docs/gen_widget_imgs.go index db31f7894f..b3d82f6fba 100644 --- a/docs/gen_widget_imgs.go +++ b/docs/gen_widget_imgs.go @@ -68,7 +68,7 @@ def main(): `, snippet) app := runtime.Applet{} - err = app.Load(name, []byte(src), nil) + err = app.Load(fmt.Sprintf("id-%s", name), name, []byte(src), nil) if err != nil { panic(err) } diff --git a/encode/encode_bench_test.go b/encode/encode_bench_test.go index 626f6dc674..c74c1aac3c 100644 --- a/encode/encode_bench_test.go +++ b/encode/encode_bench_test.go @@ -72,7 +72,7 @@ def main(config): func BenchmarkRunAndRender(b *testing.B) { app := &runtime.Applet{} - err := app.Load("benchmark.star", []byte(BenchmarkDotStar), nil) + err := app.Load("benchid", "benchmark.star", []byte(BenchmarkDotStar), nil) if err != nil { b.Error(err) } diff --git a/encode/encode_test.go b/encode/encode_test.go index c0b9c531d0..0f63c96b37 100644 --- a/encode/encode_test.go +++ b/encode/encode_test.go @@ -145,7 +145,7 @@ def main(): func TestFile(t *testing.T) { app := runtime.Applet{} - err := app.Load("test.star", []byte(TestDotStar), nil) + err := app.Load("testid", "test.star", []byte(TestDotStar), nil) assert.NoError(t, err) roots, err := app.Run(map[string]string{}) @@ -158,7 +158,7 @@ func TestFile(t *testing.T) { func TestHash(t *testing.T) { app := runtime.Applet{} - err := app.Load("test.star", []byte(TestDotStar), nil) + err := app.Load("testid", "test.star", []byte(TestDotStar), nil) require.NoError(t, err) roots, err := app.Run(map[string]string{}) @@ -179,7 +179,7 @@ func TestHash(t *testing.T) { // change the app slightly modifiedSource := strings.Replace(TestDotStar, "foo bar", "bar foo", 1) app2 := runtime.Applet{} - err = app2.Load("test.star", []byte(modifiedSource), nil) + err = app2.Load("testid2", "test.star", []byte(modifiedSource), nil) require.NoError(t, err) roots2, err := app2.Run(map[string]string{}) @@ -238,7 +238,7 @@ def main(): return render.Root(show_full_animation=True, child=render.Box()) ` app := runtime.Applet{} - require.NoError(t, app.Load("test.star", []byte(requestFull), nil)) + require.NoError(t, app.Load("testid", "test.star", []byte(requestFull), nil)) roots, err := app.Run(map[string]string{}) assert.NoError(t, err) assert.True(t, ScreensFromRoots(roots).ShowFullAnimation) @@ -249,7 +249,7 @@ def main(): return render.Root(child=render.Box()) ` app = runtime.Applet{} - require.NoError(t, app.Load("test.star", []byte(dontRequestFull), nil)) + require.NoError(t, app.Load("testid", "test.star", []byte(dontRequestFull), nil)) roots, err = app.Run(map[string]string{}) assert.NoError(t, err) assert.False(t, ScreensFromRoots(roots).ShowFullAnimation) @@ -276,7 +276,7 @@ def main(): `) app := runtime.Applet{} - err := app.Load("test.star", src, nil) + err := app.Load("testid", "test.star", src, nil) assert.NoError(t, err) roots, err := app.Run(map[string]string{}) diff --git a/runtime/applet.go b/runtime/applet.go index a621e2dbc4..f65d6b39ff 100644 --- a/runtime/applet.go +++ b/runtime/applet.go @@ -53,7 +53,8 @@ func init() { type Applet struct { Filename string - Id string + ThreadID string + AppID string Globals starlark.StringDict SecretDecryptionKey *SecretDecryptionKey @@ -69,7 +70,7 @@ type Applet struct { func (a *Applet) thread(initializers ...ThreadInitializer) *starlark.Thread { t := &starlark.Thread{ - Name: a.Id, + Name: a.ThreadID, Load: a.loadModule, Print: func(thread *starlark.Thread, msg string) { fmt.Printf("[%s] %s\n", a.Filename, msg) @@ -92,23 +93,24 @@ func (a *Applet) thread(initializers ...ThreadInitializer) *starlark.Thread { // Loads an applet. The script filename is used as a descriptor only, // and the actual code should be passed in src. Optionally also pass // loader to make additional starlark modules available to the script. -func (a *Applet) Load(filename string, src []byte, loader ModuleLoader) (err error) { - return a.LoadWithInitializers(filename, src, loader) +func (a *Applet) Load(appID string, filename string, src []byte, loader ModuleLoader) (err error) { + return a.LoadWithInitializers(appID, filename, src, loader) } -func (a *Applet) LoadWithInitializers(filename string, src []byte, loader ModuleLoader, initializers ...ThreadInitializer) (err error) { +func (a *Applet) LoadWithInitializers(appID string, filename string, src []byte, loader ModuleLoader, initializers ...ThreadInitializer) (err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("panic while executing %s: %v", a.Filename, r) } }() + a.AppID = appID a.Filename = filename a.loader = loader a.src = src - a.Id = fmt.Sprintf("%s/%x", filename, md5.Sum(src)) + a.ThreadID = fmt.Sprintf("%s/%x", filename, md5.Sum(src)) if a.SecretDecryptionKey != nil { a.decrypter, err = a.SecretDecryptionKey.decrypterForApp(a) diff --git a/runtime/applet_test.go b/runtime/applet_test.go index e48f97e3ab..261a53c4c3 100644 --- a/runtime/applet_test.go +++ b/runtime/applet_test.go @@ -14,14 +14,14 @@ import ( func TestLoadEmptySrc(t *testing.T) { app := &Applet{} - err := app.Load("test.star", []byte{}, nil) + err := app.Load("testid", "test.star", []byte{}, nil) assert.Error(t, err) } func TestLoadMalformed(t *testing.T) { src := "this is not valid starlark" app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.Error(t, err) } @@ -33,7 +33,7 @@ def main(): return render.Root(child=render.Box()) ` app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) // As is this @@ -45,7 +45,7 @@ def main2(): main = main2 ` app = &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) // And this (a lambda is a function) @@ -57,7 +57,7 @@ def main2(): main = lambda: main2() ` app = &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) // But not this, because a string is not a function @@ -69,7 +69,7 @@ def main2(): main = "main2" ` app = &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) assert.Error(t, err) // And not this either, because here main is gone @@ -79,7 +79,7 @@ def main2(): return render.Root(child=render.Box()) ` app = &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) assert.Error(t, err) } @@ -92,7 +92,7 @@ def main(): return [render.Box()] ` app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) assert.Error(t, err) @@ -106,7 +106,7 @@ def main(): ` app = &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) screens, err = app.Run(map[string]string{}) assert.NoError(t, err) @@ -119,7 +119,7 @@ def main(): return [render.Root(child=render.Box()), render.Root(child=render.Text("hi"))] ` app = &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) screens, err = app.Run(map[string]string{}) assert.NoError(t, err) @@ -141,7 +141,7 @@ def main(): return render.Root(child=render.Box()) ` app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) roots, err := app.Run(config) assert.NoError(t, err) @@ -170,7 +170,7 @@ def main(config): return [render.Root(child=render.Box()) for _ in range(int(config["one"]) + int(config["two"]))] ` app = &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) require.NoError(t, err) roots, err = app.Run(config) require.NoError(t, err) @@ -208,7 +208,7 @@ def main(): return render.Root(child=render.Box()) ` app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) roots, err := app.Run(map[string]string{}) assert.NoError(t, err) @@ -230,7 +230,7 @@ def main(): return render.Root(child=render.Box()) ` app = &Applet{} - err = app.Load("test.star", []byte(src), loader) + err = app.Load("testid", "test.star", []byte(src), loader) assert.NoError(t, err) roots, err = app.Run(map[string]string{}) assert.NoError(t, err) @@ -255,7 +255,7 @@ def main(): } app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) _, err = app.Run(map[string]string{}, initializer) assert.NoError(t, err) @@ -275,7 +275,7 @@ def main(): ` app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) _, err = app.Run(map[string]string{}) assert.NoError(t, err) @@ -323,7 +323,7 @@ def main(config): ` app := &Applet{} - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) _, err = app.Run(map[string]string{"ZIP_BYTES": buf.String()}, initializer) assert.NoError(t, err) diff --git a/runtime/cache_test.go b/runtime/cache_test.go index 56daa2600d..f3d039a6b5 100644 --- a/runtime/cache_test.go +++ b/runtime/cache_test.go @@ -33,7 +33,7 @@ def main(): ` InitCache(NewInMemoryCache()) app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) roots, err := app.Run(map[string]string{}) assert.NoError(t, err) @@ -54,7 +54,7 @@ def main(): ` InitCache(NewInMemoryCache()) app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) // first time, i == 1 @@ -72,7 +72,7 @@ def main(): // but run the same code using different filename, and cached // data ends up in a different namespace app = &Applet{} - err = app.Load("test2.star", []byte(src), nil) + err = app.Load("testid", "test2.star", []byte(src), nil) assert.NoError(t, err) roots, _ = app.Run(map[string]string{}) @@ -103,7 +103,7 @@ def main(): ` InitCache(nil) app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) assert.NoError(t, err) @@ -121,7 +121,7 @@ def main(): ` InitCache(nil) app := &Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) assert.Error(t, err) diff --git a/runtime/httpcache_test.go b/runtime/httpcache_test.go index fd1d426e8b..1f1bb76abb 100644 --- a/runtime/httpcache_test.go +++ b/runtime/httpcache_test.go @@ -19,7 +19,7 @@ func TestInitHTTP(t *testing.T) { assert.NoError(t, err) app := &Applet{} - err = app.Load("httpcache.star", b, nil) + err = app.Load("httpid", "httpcache.star", b, nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/runtime/modules/hmac/hmac_test.go b/runtime/modules/hmac/hmac_test.go index 6f79df65b4..c193db6dd4 100644 --- a/runtime/modules/hmac/hmac_test.go +++ b/runtime/modules/hmac/hmac_test.go @@ -26,7 +26,7 @@ def main(): func TestHmac(t *testing.T) { app := &runtime.Applet{} - err := app.Load("hmac_test.star", []byte(hmacSource), nil) + err := app.Load("hmacid", "hmac_test.star", []byte(hmacSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/runtime/modules/humanize/humanize_test.go b/runtime/modules/humanize/humanize_test.go index e9a97dd2fb..e79c1c18f0 100644 --- a/runtime/modules/humanize/humanize_test.go +++ b/runtime/modules/humanize/humanize_test.go @@ -80,7 +80,7 @@ def main(): func TestHumanize(t *testing.T) { app := &runtime.Applet{} - err := app.Load("human.star", []byte(humanSource), nil) + err := app.Load("humanid", "human.star", []byte(humanSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/runtime/modules/qrcode/qrcode_test.go b/runtime/modules/qrcode/qrcode_test.go index 290f38c8bd..cf928d4c18 100644 --- a/runtime/modules/qrcode/qrcode_test.go +++ b/runtime/modules/qrcode/qrcode_test.go @@ -29,7 +29,7 @@ def main(): func TestQRCode(t *testing.T) { app := &runtime.Applet{} - err := app.Load("test.star", []byte(qrCodeSource), nil) + err := app.Load("testid", "test.star", []byte(qrCodeSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/runtime/modules/random/random_test.go b/runtime/modules/random/random_test.go index 08d7fda7fc..6120f59ab8 100644 --- a/runtime/modules/random/random_test.go +++ b/runtime/modules/random/random_test.go @@ -48,7 +48,7 @@ def main(): func TestRandom(t *testing.T) { app := &runtime.Applet{} - err := app.Load("random_test.star", []byte(randomSrc), nil) + err := app.Load("randomid", "random_test.star", []byte(randomSrc), nil) require.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/runtime/modules/sunrise/sunrise_test.go b/runtime/modules/sunrise/sunrise_test.go index 0c4519c2f4..d8ff3e88cd 100644 --- a/runtime/modules/sunrise/sunrise_test.go +++ b/runtime/modules/sunrise/sunrise_test.go @@ -52,7 +52,7 @@ def main(): func TestSunrise(t *testing.T) { app := &runtime.Applet{} - err := app.Load("sun.star", []byte(sunSource), nil) + err := app.Load("sunid", "sun.star", []byte(sunSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/runtime/modules/xpath/xpath_test.go b/runtime/modules/xpath/xpath_test.go index 6a2b45b3e3..54fde86b92 100644 --- a/runtime/modules/xpath/xpath_test.go +++ b/runtime/modules/xpath/xpath_test.go @@ -74,7 +74,7 @@ def main(): return [r.Root(child=r.Text("1337"))] ` app := &runtime.Applet{} - err := app.Load("test.star", []byte(src), nil) + err := app.Load("testid", "test.star", []byte(src), nil) require.NoError(t, err) screens, err := app.Run(map[string]string{}) require.NoError(t, err) diff --git a/runtime/render_test.go b/runtime/render_test.go index f9b571ca49..e7cbe18ffd 100644 --- a/runtime/render_test.go +++ b/runtime/render_test.go @@ -136,7 +136,7 @@ def main(): func TestBigDotStar(t *testing.T) { app := &Applet{} - err := app.Load("big.star", []byte(TestDotStar), nil) + err := app.Load("bigid", "big.star", []byte(TestDotStar), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) assert.NoError(t, err) @@ -159,7 +159,7 @@ def main(): ) app := &Applet{} - err := app.Load(filename, []byte(src), nil) + err := app.Load("boxid", filename, []byte(src), nil) assert.NoError(t, err) b := app.Globals["b"] @@ -195,7 +195,7 @@ def main(): ) app := &Applet{} - err := app.Load(filename, []byte(src), nil) + err := app.Load("textid", filename, []byte(src), nil) assert.NoError(t, err) txt := app.Globals["t"] @@ -240,7 +240,7 @@ def main(): `, base64.StdEncoding.EncodeToString(p.Bytes())) app := &Applet{} - err := app.Load(filename, []byte(src), nil) + err := app.Load("imageid", filename, []byte(src), nil) assert.NoError(t, err) starlarkP := app.Globals["img"] diff --git a/runtime/secret.go b/runtime/secret.go index 865b7fa125..d4e575651a 100644 --- a/runtime/secret.go +++ b/runtime/secret.go @@ -94,7 +94,8 @@ func (sdk *SecretDecryptionKey) decrypterForApp(a *Applet) (decrypter, error) { return nil, errors.Wrap(err, "NewHybridDecrypt") } - context := []byte(strings.TrimSuffix(a.Filename, ".star")) + contextA := []byte(a.AppID) + contextB := []byte(strings.TrimSuffix(a.Filename, ".star")) return func(s starlark.String) (starlark.String, error) { v := regexp.MustCompile(`\s`).ReplaceAllString(s.GoString(), "") @@ -103,9 +104,12 @@ func (sdk *SecretDecryptionKey) decrypterForApp(a *Applet) (decrypter, error) { return "", errors.Wrapf(err, "base64 decoding of secret: %s", s) } - cleartext, err := dec.Decrypt(ciphertext, context) + cleartext, err := dec.Decrypt(ciphertext, contextA) if err != nil { - return "", errors.Wrapf(err, "decrypting secret: %s", s) + cleartext, err = dec.Decrypt(ciphertext, contextB) + if err != nil { + return "", fmt.Errorf("decrypting secret %s: %w", s, err) + } } return starlark.String(cleartext), nil diff --git a/runtime/secret_test.go b/runtime/secret_test.go index 1f4b090b5c..822e6d17d1 100644 --- a/runtime/secret_test.go +++ b/runtime/secret_test.go @@ -46,7 +46,7 @@ func TestSecretDecrypt(t *testing.T) { err = khPub.WriteWithNoSecrets(keyset.NewJSONWriter(pubJSON)) require.NoError(t, err) - // encrypt the secret + // encrypt the secret (method 1) encrypted, err := (&SecretEncryptionKey{ PublicKeysetJSON: pubJSON.Bytes(), }).Encrypt("test", plaintext) @@ -75,12 +75,48 @@ def main(): SecretDecryptionKey: decryptionKey, } - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) require.NoError(t, err) roots, err := app.Run(nil) assert.NoError(t, err) assert.Equal(t, 1, len(roots)) + + // encrypt the secret (method 2) + encrypted, err = (&SecretEncryptionKey{ + PublicKeysetJSON: pubJSON.Bytes(), + }).Encrypt("testid", plaintext) + require.NoError(t, err) + assert.NotEqual(t, encrypted, "") + + src = fmt.Sprintf(` +load("render.star", "render") +load("schema.star", "schema") +load("secret.star", "secret") + +EXPECTED_PLAINTEXT = "%s" +ENCRYPTED = "%s" +DECRYPTED = secret.decrypt(ENCRYPTED) + +def assert_eq(message, actual, expected): + if not expected == actual: + fail(message, "-", "expected", expected, "actual", actual) + +def main(): + assert_eq("secret value", DECRYPTED, EXPECTED_PLAINTEXT) + return render.Root(child=render.Box()) +`, plaintext, encrypted) + + app = &Applet{ + SecretDecryptionKey: decryptionKey, + } + + err = app.Load("testid", "test.star", []byte(src), nil) + require.NoError(t, err) + + roots, err = app.Run(nil) + assert.NoError(t, err) + assert.Equal(t, 1, len(roots)) } func TestSecretDoesntDecryptWithoutKey(t *testing.T) { @@ -132,7 +168,7 @@ def main(): SecretDecryptionKey: nil, } - err = app.Load("test.star", []byte(src), nil) + err = app.Load("testid", "test.star", []byte(src), nil) require.NoError(t, err) roots, err := app.Run(nil) diff --git a/schema/color_test.go b/schema/color_test.go index a29125c080..5cecd4ca32 100644 --- a/schema/color_test.go +++ b/schema/color_test.go @@ -56,7 +56,7 @@ def main(): return [] ` app := &runtime.Applet{} - err := app.Load("colors.star", []byte(src), nil) + err := app.Load("cid", "colors.star", []byte(src), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) @@ -91,7 +91,7 @@ def main(config): return [] ` app := &runtime.Applet{} - err := app.Load("colors.star", []byte(src), nil) + err := app.Load("cid", "colors.star", []byte(src), nil) assert.NoError(t, err) // Well formed input -> success diff --git a/schema/datetime_test.go b/schema/datetime_test.go index f5bda35850..8189f85834 100644 --- a/schema/datetime_test.go +++ b/schema/datetime_test.go @@ -32,7 +32,7 @@ def main(): func TestDateTime(t *testing.T) { app := &runtime.Applet{} - err := app.Load("date_time.star", []byte(dateTimeSource), nil) + err := app.Load("dtid", "date_time.star", []byte(dateTimeSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/dropdown_test.go b/schema/dropdown_test.go index 9fd713bb4a..08426778d4 100644 --- a/schema/dropdown_test.go +++ b/schema/dropdown_test.go @@ -52,7 +52,7 @@ def main(): func TestDropdown(t *testing.T) { app := &runtime.Applet{} - err := app.Load("dropdown.star", []byte(dropdownSource), nil) + err := app.Load("ddid", "dropdown.star", []byte(dropdownSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/generated_test.go b/schema/generated_test.go index d865ba7c1d..03180d91c1 100644 --- a/schema/generated_test.go +++ b/schema/generated_test.go @@ -30,7 +30,7 @@ def main(): func TestGenerated(t *testing.T) { app := &runtime.Applet{} - err := app.Load("generated.star", []byte(generatedSource), nil) + err := app.Load("gid", "generated.star", []byte(generatedSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/handler_test.go b/schema/handler_test.go index 30e283b2c1..07aabc4a8c 100644 --- a/schema/handler_test.go +++ b/schema/handler_test.go @@ -31,7 +31,7 @@ def main(): func TestHandler(t *testing.T) { app := &runtime.Applet{} - err := app.Load("handler.star", []byte(handlerSource), nil) + err := app.Load("hid", "handler.star", []byte(handlerSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) @@ -42,7 +42,7 @@ func TestHandler(t *testing.T) { func TestHandlerBadParams(t *testing.T) { // Handler is a string app := &runtime.Applet{} - err := app.Load("text.star", []byte(` + err := app.Load("tid", "text.star", []byte(` load("schema.star", "schema") def foobar(param): @@ -60,7 +60,7 @@ def main(): // Type is not valid app = &runtime.Applet{} - err = app.Load("text.star", []byte(` + err = app.Load("tid", "text.star", []byte(` load("schema.star", "schema") def foobar(param): diff --git a/schema/location_test.go b/schema/location_test.go index b33bba4cfe..fde878d1d4 100644 --- a/schema/location_test.go +++ b/schema/location_test.go @@ -32,7 +32,7 @@ def main(): func TestLocation(t *testing.T) { app := &runtime.Applet{} - err := app.Load("location.star", []byte(locationSource), nil) + err := app.Load("lid", "location.star", []byte(locationSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/locationbased_test.go b/schema/locationbased_test.go index d76567d01f..399538da31 100644 --- a/schema/locationbased_test.go +++ b/schema/locationbased_test.go @@ -62,7 +62,7 @@ def main(): func TestLocationBased(t *testing.T) { app := &runtime.Applet{} - err := app.Load("location_based.star", []byte(locationBasedSource), nil) + err := app.Load("lbid", "location_based.star", []byte(locationBasedSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/module_test.go b/schema/module_test.go index 9f13a9d5bc..0d4aa8a773 100644 --- a/schema/module_test.go +++ b/schema/module_test.go @@ -42,7 +42,7 @@ def main(): func TestStarlarkSchema(t *testing.T) { app := &runtime.Applet{} - err := app.Load("starlark.star", []byte(schemaSource), nil) + err := app.Load("starlarkid", "starlark.star", []byte(schemaSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) @@ -51,7 +51,7 @@ func TestStarlarkSchema(t *testing.T) { } func TestSchemaModuleLoads(t *testing.T) { app := &runtime.Applet{} - err := app.Load("source.star", []byte(moduleSource), nil) + err := app.Load("sourceid", "source.star", []byte(moduleSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/oauth2_test.go b/schema/oauth2_test.go index fde747454d..4613f4f368 100644 --- a/schema/oauth2_test.go +++ b/schema/oauth2_test.go @@ -48,7 +48,7 @@ def main(): func TestOAuth2(t *testing.T) { app := &runtime.Applet{} - err := app.Load("oauth2.star", []byte(oauth2Source), nil) + err := app.Load("oaid", "oauth2.star", []byte(oauth2Source), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/option_test.go b/schema/option_test.go index ac8752c7f0..aa0216d49a 100644 --- a/schema/option_test.go +++ b/schema/option_test.go @@ -28,7 +28,7 @@ def main(): func TestOption(t *testing.T) { app := &runtime.Applet{} - err := app.Load("option.star", []byte(optionSource), nil) + err := app.Load("oid", "option.star", []byte(optionSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/photoselect_test.go b/schema/photoselect_test.go index 3136c3d775..2d9dd1ae4a 100644 --- a/schema/photoselect_test.go +++ b/schema/photoselect_test.go @@ -32,7 +32,7 @@ def main(): func TestPhotoSelect(t *testing.T) { app := &runtime.Applet{} - err := app.Load("photo_select.star", []byte(photoSelectSource), nil) + err := app.Load("photid", "photo_select.star", []byte(photoSelectSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/schema_test.go b/schema/schema_test.go index 8e72470e4a..20753be38d 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -14,7 +14,7 @@ import ( func loadApp(code string) (*runtime.Applet, error) { app := &runtime.Applet{} - err := app.Load("test.star", []byte(code), nil) + err := app.Load("testid", "test.star", []byte(code), nil) if err != nil { return nil, errors.WithStack(err) } diff --git a/schema/text_test.go b/schema/text_test.go index c788fc9c66..30fa3315ad 100644 --- a/schema/text_test.go +++ b/schema/text_test.go @@ -34,7 +34,7 @@ def main(): func TestText(t *testing.T) { app := &runtime.Applet{} - err := app.Load("text.star", []byte(textSource), nil) + err := app.Load("tid", "text.star", []byte(textSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/toggle_test.go b/schema/toggle_test.go index 0f0db8e8b8..78afb65485 100644 --- a/schema/toggle_test.go +++ b/schema/toggle_test.go @@ -34,7 +34,7 @@ def main(): func TestToggle(t *testing.T) { app := &runtime.Applet{} - err := app.Load("toggle.star", []byte(toggleSource), nil) + err := app.Load("tid", "toggle.star", []byte(toggleSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/schema/typeahead_test.go b/schema/typeahead_test.go index 7e1645b681..fc0af0e6c3 100644 --- a/schema/typeahead_test.go +++ b/schema/typeahead_test.go @@ -47,7 +47,7 @@ def main(): func TestTypeahead(t *testing.T) { app := &runtime.Applet{} - err := app.Load("typeahead.star", []byte(typeaheadSource), nil) + err := app.Load("tahid", "typeahead.star", []byte(typeaheadSource), nil) assert.NoError(t, err) screens, err := app.Run(map[string]string{}) diff --git a/server/loader/loader.go b/server/loader/loader.go index c1b06eae12..2d37cdfd2d 100644 --- a/server/loader/loader.go +++ b/server/loader/loader.go @@ -72,7 +72,7 @@ func NewLoader( runtime.InitCache(cache) if !l.watch { - err := loadScript(&l.applet, l.filename) + err := loadScript(&l.applet, "app-id", l.filename) l.markInitialLoadComplete() if err != nil { return nil, err @@ -157,7 +157,7 @@ func (l *Loader) CallSchemaHandler(ctx context.Context, handlerName, parameter s func (l *Loader) loadApplet(config map[string]string) (string, error) { if l.watch { - err := loadScript(&l.applet, l.filename) + err := loadScript(&l.applet, "app-id", l.filename) l.markInitialLoadComplete() if err != nil { return "", err diff --git a/server/loader/script.go b/server/loader/script.go index f8e4ebfb88..7874910caf 100644 --- a/server/loader/script.go +++ b/server/loader/script.go @@ -9,13 +9,13 @@ import ( "tidbyt.dev/pixlet/runtime" ) -func loadScript(applet *runtime.Applet, filename string) error { +func loadScript(applet *runtime.Applet, appID string, filename string) error { src, err := ioutil.ReadFile(filename) if err != nil { return fmt.Errorf("failed to read file %s: %w", filename, err) } - err = applet.Load(filename, src, nil) + err = applet.Load(appID, filename, src, nil) if err != nil { return fmt.Errorf("failed to load applet: %w", err) } diff --git a/server/loader/script_js.go b/server/loader/script_js.go index 507d183120..bca15e3960 100644 --- a/server/loader/script_js.go +++ b/server/loader/script_js.go @@ -10,7 +10,7 @@ import ( "tidbyt.dev/pixlet/runtime" ) -func loadScript(applet *runtime.Applet, filename string) error { +func loadScript(applet *runtime.Applet, appID string, filename string) error { res, err := http.Get(filename) if err != nil { return fmt.Errorf("failed to fetch file %s: %w", filename, err) @@ -22,7 +22,7 @@ func loadScript(applet *runtime.Applet, filename string) error { return fmt.Errorf("failed to read file %s: %w", filename, err) } - err = applet.Load(filename, src, nil) + err = applet.Load(appID, filename, src, nil) if err != nil { return fmt.Errorf("failed to load applet: %w", err) }