@sdboyer/thema-playground 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <meta name="description" content="Write a thema lineage, then bind it, validate and translate data with it, and generate code from it, in the browser." />
7
+ <title>thema playground</title>
8
+ <script type="module" crossorigin src="./assets/index-DKo6KyhX.js"></script>
9
+ <link rel="stylesheet" crossorigin href="./assets/index-D9DeBLCS.css">
10
+ </head>
11
+ <body>
12
+ <div id="app"></div>
13
+ </body>
14
+ </html>
@@ -0,0 +1,69 @@
1
+ // thema-fs.js: the filesystem thema.wasm runs with in a browser, where there
2
+ // is none. Load it before Go's wasm_exec.js, which installs its own stub only
3
+ // when globalThis.fs is undefined.
4
+ //
5
+ // Every file thema reads is in a loader overlay, but CUE's loader also looks
6
+ // for optional files outside it (cue.mod/local-module.cue) and lists
7
+ // directories, and it accepts only "does not exist" as an answer. wasm_exec's
8
+ // stub answers every call with ENOSYS, and declares its open flags as -1,
9
+ // which Go reads as "directories cannot be opened at all"; either fails the
10
+ // load. Here, lookups say that nothing exists, the flags are real, and
11
+ // writes go to the console as wasm_exec's stub sends them.
12
+ (() => {
13
+ if (globalThis.fs) {
14
+ return;
15
+ }
16
+ const err = (code) => {
17
+ const e = new Error(code);
18
+ e.code = code;
19
+ return e;
20
+ };
21
+ const enoent = (...args) => args[args.length - 1](err("ENOENT"));
22
+ const enosys = (...args) => args[args.length - 1](err("ENOSYS"));
23
+ const decoder = new TextDecoder("utf-8");
24
+ let outputBuf = "";
25
+ globalThis.fs = {
26
+ constants: { O_WRONLY: 1, O_RDWR: 2, O_CREAT: 64, O_TRUNC: 512, O_APPEND: 1024, O_EXCL: 128, O_DIRECTORY: 65536 },
27
+ writeSync(fd, buf) {
28
+ outputBuf += decoder.decode(buf);
29
+ const nl = outputBuf.lastIndexOf("\n");
30
+ if (nl != -1) {
31
+ console.log(outputBuf.substring(0, nl));
32
+ outputBuf = outputBuf.substring(nl + 1);
33
+ }
34
+ return buf.length;
35
+ },
36
+ write(fd, buf, offset, length, position, callback) {
37
+ if (offset !== 0 || length !== buf.length || position !== null) {
38
+ callback(err("ENOSYS"));
39
+ return;
40
+ }
41
+ callback(null, this.writeSync(fd, buf));
42
+ },
43
+ open: enoent,
44
+ stat: enoent,
45
+ lstat: enoent,
46
+ readdir: enoent,
47
+ readlink: enoent,
48
+ fsync(fd, callback) {
49
+ callback(null);
50
+ },
51
+ chmod: enosys,
52
+ chown: enosys,
53
+ close: enosys,
54
+ fchmod: enosys,
55
+ fchown: enosys,
56
+ fstat: enosys,
57
+ ftruncate: enosys,
58
+ lchown: enosys,
59
+ link: enosys,
60
+ mkdir: enosys,
61
+ read: enosys,
62
+ rename: enosys,
63
+ rmdir: enosys,
64
+ symlink: enosys,
65
+ truncate: enosys,
66
+ unlink: enosys,
67
+ utimes: enosys,
68
+ };
69
+ })();
@@ -0,0 +1,64 @@
1
+ // Runs thema.wasm off the page's thread. A classic worker, not bundled: it
2
+ // loads thema's own scripts beside it, thema-fs.js before wasm_exec.js as
3
+ // thema requires, and they and thema.wasm are whatever thema build was put
4
+ // next to it: this tree's, by go generate ./cmd/thema/playground, or the one
5
+ // thema serve playground was built with.
6
+ //
7
+ // Messages to the page:
8
+ // {type: "progress", loaded} bytes of thema.wasm received so far
9
+ // {type: "ready", init} thema's init response, parsed
10
+ // {type: "failed", error} thema could not start
11
+ // {id, response} the response to call id, as JSON text
12
+ // Messages from the page: {id, op, request}, request as JSON text.
13
+ importScripts("thema-fs.js", "wasm_exec.js");
14
+
15
+ const ready = new Promise((resolve) => {
16
+ self.onThemaReady = resolve;
17
+ });
18
+
19
+ async function fetchWasm() {
20
+ const resp = await fetch("thema.wasm");
21
+ if (!resp.ok) {
22
+ throw new Error(`fetching thema.wasm: ${resp.status} ${resp.statusText}`);
23
+ }
24
+ const reader = resp.body.getReader();
25
+ const chunks = [];
26
+ let loaded = 0;
27
+ for (;;) {
28
+ const { done, value } = await reader.read();
29
+ if (done) break;
30
+ chunks.push(value);
31
+ loaded += value.length;
32
+ self.postMessage({ type: "progress", loaded });
33
+ }
34
+ const bytes = new Uint8Array(loaded);
35
+ let at = 0;
36
+ for (const c of chunks) {
37
+ bytes.set(c, at);
38
+ at += c.length;
39
+ }
40
+ return bytes;
41
+ }
42
+
43
+ async function start() {
44
+ const go = new Go();
45
+ const { instance } = await WebAssembly.instantiate(await fetchWasm(), go.importObject);
46
+ go.run(instance);
47
+ await ready;
48
+ self.postMessage({ type: "ready", init: JSON.parse(self.themaInit()) });
49
+ }
50
+
51
+ const started = start().catch((err) => {
52
+ self.postMessage({ type: "failed", error: String(err && err.message ? err.message : err) });
53
+ throw err;
54
+ });
55
+
56
+ self.onmessage = async (e) => {
57
+ const { id, op, request } = e.data;
58
+ try {
59
+ await started;
60
+ self.postMessage({ id, response: self.themaCall(op, request) });
61
+ } catch (err) {
62
+ self.postMessage({ id, error: String(err) });
63
+ }
64
+ };
@@ -0,0 +1,4 @@
1
+ [diffend] Oversized file quarantined before diffing.
2
+ name: package/dist/thema.wasm
3
+ size: 42738491 bytes
4
+ sha256: 7a5cb8a8937b1f9ceb4ddd92ba6fe07e295647cc2140346106649499572455c7
Binary file