@hpcc-js/observablehq-compiler 1.1.0 → 1.1.2

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.
Files changed (49) hide show
  1. package/LICENSE +43 -0
  2. package/README.md +105 -105
  3. package/bin/ojscc.mjs +74 -74
  4. package/dist/index.esm.js +554 -554
  5. package/dist/index.esm.js.map +1 -1
  6. package/dist/index.esm.min.js +4 -0
  7. package/dist/index.esm.min.js.map +1 -0
  8. package/dist/index.js +6700 -6696
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.min.js +4 -0
  11. package/dist/index.min.js.map +1 -0
  12. package/package.json +4 -3
  13. package/src/__package__.ts +3 -3
  14. package/src/__tests__/File Attachments.ts +894 -894
  15. package/src/__tests__/Introduction to Imports.ts +748 -748
  16. package/src/__tests__/Observable TimeChart.ts +771 -771
  17. package/src/__tests__/index.ts +13 -13
  18. package/src/__tests__/node.ts +177 -177
  19. package/src/__tests__/tsconfig.json +20 -20
  20. package/src/compiler.md +234 -234
  21. package/src/compiler.ts +264 -264
  22. package/src/cst.ts +172 -172
  23. package/src/index.css +459 -459
  24. package/src/index.ts +7 -7
  25. package/src/util.md +113 -113
  26. package/src/util.ts +153 -153
  27. package/src/writer.ts +80 -80
  28. package/types/__package__.d.ts +3 -3
  29. package/types/__tests__/File Attachments.d.ts +109 -109
  30. package/types/__tests__/Introduction to Imports.d.ts +119 -119
  31. package/types/__tests__/Observable TimeChart.d.ts +110 -110
  32. package/types/__tests__/index.d.ts +1 -1
  33. package/types/__tests__/node.d.ts +1 -1
  34. package/types/compiler.d.ts +87 -87
  35. package/types/cst.d.ts +41 -41
  36. package/types/index.d.ts +5 -5
  37. package/types/util.d.ts +25 -25
  38. package/types/writer.d.ts +18 -18
  39. package/types-3.4/__package__.d.ts +4 -0
  40. package/types-3.4/__tests__/File Attachments.d.ts +110 -0
  41. package/types-3.4/__tests__/Introduction to Imports.d.ts +120 -0
  42. package/types-3.4/__tests__/Observable TimeChart.d.ts +111 -0
  43. package/types-3.4/__tests__/index.d.ts +2 -0
  44. package/types-3.4/__tests__/node.d.ts +2 -0
  45. package/types-3.4/compiler.d.ts +91 -0
  46. package/types-3.4/cst.d.ts +42 -0
  47. package/types-3.4/index.d.ts +6 -0
  48. package/types-3.4/util.d.ts +26 -0
  49. package/types-3.4/writer.d.ts +19 -0
package/src/compiler.ts CHANGED
@@ -1,264 +1,264 @@
1
- import type { ohq } from "@hpcc-js/observable-shim";
2
-
3
- import { endsWith, join } from "@hpcc-js/util";
4
- import { parseCell, ParsedImportCell } from "./cst";
5
- import { Writer } from "./writer";
6
- import { encodeBacktick, fetchEx, obfuscatedImport, ojs2notebook, omd2notebook } from "./util";
7
-
8
- interface ImportDefine {
9
- (runtime: ohq.Runtime, inspector?: ohq.InspectorFactory): ohq.Module;
10
- dispose: () => void;
11
- write: (w: Writer) => void;
12
- }
13
-
14
- async function importFile(relativePath: string, baseUrl: string) {
15
- const path = join(baseUrl, relativePath);
16
- const content = await fetchEx(path).then(r => r.text());
17
- let notebook: ohq.Notebook;
18
- if (endsWith(relativePath, ".ojsnb")) {
19
- notebook = JSON.parse(content);
20
- } else if (endsWith(relativePath, ".ojs")) {
21
- notebook = ojs2notebook(content);
22
- } else if (endsWith(relativePath, ".omd")) {
23
- notebook = omd2notebook(content);
24
- }
25
- const retVal: ImportDefine = compile(notebook, baseUrl) as any;
26
- retVal.dispose = () => { };
27
- retVal.write = (w: Writer) => {
28
- w.import(path);
29
- };
30
- return retVal;
31
- }
32
-
33
- // @ts-ignore - use precompiled notebook from observable
34
- async function importCompiledNotebook(partial: string) {
35
- const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
36
- let impMod = {
37
- default: function (runtime: ohq.Runtime, inspector?: ohq.InspectorFactory): ohq.Module {
38
- return undefined;
39
- } as any
40
- };
41
- try {
42
- impMod = await obfuscatedImport(url);
43
- } catch (e) {
44
- }
45
- const retVal: ImportDefine = impMod.default;
46
- retVal.dispose = () => { };
47
- retVal.write = (w: Writer) => {
48
- w.import(url);
49
- };
50
- return retVal;
51
- }
52
-
53
- // @ts-ignore - recursive notebook parsing and compiling
54
- async function importNotebook(partial: string) {
55
- const url = `https://api.observablehq.com/document/${partial}`;
56
- const notebook = fetchEx(url)
57
- .then(r => {
58
- return r.json();
59
- }).catch(e => {
60
- console.error(url);
61
- console.error(e);
62
- });
63
- const retVal: ImportDefine = compile(await notebook) as any;
64
- retVal.dispose = () => { };
65
- retVal.write = (w: Writer) => {
66
- w.import(url);
67
- };
68
- return retVal;
69
- }
70
-
71
- function createVariable(inspect: boolean, name?: string, inputs?: string[], definition?: any, inline = false) {
72
-
73
- let i: ohq.Inspector;
74
- let v: ohq.Variable;
75
-
76
- const retVal = (module: ohq.Module, inspector?: ohq.InspectorFactory) => {
77
- i = inspect ? inspector(name) : undefined;
78
- v = module.variable(i);
79
- if (arguments.length > 1) {
80
- try {
81
- v.define(name, inputs, definition);
82
- } catch (e: any) {
83
- console.error(e?.message);
84
- }
85
- }
86
- return v;
87
- };
88
- retVal.dispose = () => {
89
- try {
90
- i?._node?.remove();
91
- } catch (e) {
92
- }
93
- i = undefined;
94
- try {
95
- v?.delete();
96
- } catch (e) {
97
- }
98
- v = undefined;
99
- };
100
- retVal.write = (w: Writer) => {
101
- if (inline) {
102
- w.define({ id: name, inputs, func: definition }, inspect, true);
103
- } else {
104
- const id = w.function({ id: name, func: definition });
105
- w.define({ id: name, inputs, func: definition }, inspect, false, id);
106
- }
107
- };
108
- return retVal;
109
- }
110
- type VariableFunc = ReturnType<typeof createVariable>;
111
-
112
- function createImportVariable(name?: string, alias?: string) {
113
-
114
- let v: ohq.Variable;
115
-
116
- const retVal = (main: ohq.Module, otherModule: ohq.Module) => {
117
- v = main.variable();
118
- v.import(name, alias, otherModule);
119
- };
120
-
121
- retVal.dispose = () => {
122
- v?.delete();
123
- };
124
- return retVal;
125
- }
126
- type ImportVariableFunc = ReturnType<typeof createImportVariable>;
127
-
128
- async function createModule(parsed: ParsedImportCell, text: string, baseUrl: string) {
129
- const otherModule = [".", "/"].indexOf(parsed.src[0]) === 0 ?
130
- await importFile(parsed.src, baseUrl) :
131
- await importCompiledNotebook(parsed.src);
132
-
133
- const importVariables: ImportVariableFunc[] = [];
134
- const variables: VariableFunc[] = [];
135
- parsed.specifiers.forEach(spec => {
136
- const viewof = spec.view ? "viewof " : "";
137
- importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
138
- if (spec.view) {
139
- importVariables.push(createImportVariable(spec.name, spec.alias));
140
- }
141
- });
142
- variables.push(createVariable(true, undefined, ["md"], md => {
143
- return md`\`\`\`JavaScript
144
- ${text}
145
- \`\`\``;
146
- }));
147
-
148
- const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: ohq.InspectorFactory) => {
149
-
150
- let mod = runtime.module(otherModule);
151
- if (parsed.injections.length) {
152
- mod = mod.derive(parsed.injections, main);
153
- }
154
- variables.forEach(v => v(main, inspector));
155
- importVariables.forEach(v => v(main, mod));
156
- return mod;
157
- };
158
- retVal.importVariables = importVariables;
159
- retVal.variables = variables;
160
- retVal.dispose = () => {
161
- importVariables.forEach(v => v.dispose());
162
- variables.forEach(v => v.dispose());
163
- otherModule.dispose();
164
- };
165
- retVal.write = (w: Writer) => {
166
- otherModule.write(w);
167
- w.importDefine(parsed);
168
- };
169
- return retVal;
170
- }
171
- type ModuleFunc = Awaited<ReturnType<typeof createModule>>;
172
-
173
- async function createCell(node: ohq.Node, baseUrl: string) {
174
- const modules: ModuleFunc[] = [];
175
- const variables: VariableFunc[] = [];
176
- try {
177
- const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
178
- const parsed = parseCell(text);
179
- switch (parsed.type) {
180
- case "import":
181
- modules.push(await createModule(parsed, text, baseUrl));
182
- break;
183
- case "viewof":
184
- variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
185
- variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
186
- break;
187
- case "mutable":
188
- variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
189
- variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
190
- variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
191
- break;
192
- case "variable":
193
- variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
194
- break;
195
- }
196
- } catch (e) {
197
- }
198
-
199
- const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: ohq.InspectorFactory) => {
200
- modules.forEach(imp => imp(runtime, main, inspector));
201
- variables.forEach(v => v(main, inspector));
202
- };
203
- retVal.modules = modules;
204
- retVal.variables = variables;
205
- retVal.dispose = () => {
206
- variables.forEach(v => v.dispose());
207
- modules.forEach(mod => mod.dispose());
208
- };
209
- retVal.write = (w: Writer) => {
210
- modules.forEach(imp => imp.write(w));
211
- variables.forEach(v => v.write(w));
212
- };
213
- return retVal;
214
- }
215
- export type CellFunc = Awaited<ReturnType<typeof createCell>>;
216
-
217
- function createFile(file: ohq.File): [string, any] {
218
- function toString() { return globalThis.url; }
219
- return [file.name, { url: new URL(file.url), mimeType: file.mime_type, toString }];
220
- }
221
- export type FileFunc = ReturnType<typeof createFile>;
222
-
223
- export async function compile(notebook: ohq.Notebook, baseUrl: string = ".") {
224
-
225
- const files = notebook.files.map(f => createFile(f));
226
- const fileAttachments = new Map<string, any>(files);
227
- let cells: CellFunc[] = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
228
-
229
- const retVal = (runtime: ohq.Runtime, inspector?: ohq.InspectorFactory): ohq.Module => {
230
- const main = runtime.module();
231
- main.builtin("FileAttachment", runtime.fileAttachments(name => {
232
- return fileAttachments.get(name) ?? { url: new URL(name), mimeType: null };
233
- }));
234
- cells.forEach(cell => {
235
- cell(runtime, main, inspector);
236
- });
237
- return main;
238
- };
239
- retVal.fileAttachments = fileAttachments;
240
- retVal.cells = cells;
241
- retVal.appendCell = async (n: ohq.Node, baseUrl: string = ".") => {
242
- const cell = await createCell(n, baseUrl);
243
- cells.push(cell);
244
- return cell;
245
- };
246
- retVal.disposeCell = async (cell: CellFunc) => {
247
- cells = cells.filter(c => c !== cell);
248
- cell.dispose();
249
- };
250
- retVal.dispose = () => {
251
- cells.forEach(cell => cell.dispose());
252
- cells = [];
253
- };
254
- retVal.write = (w: Writer) => {
255
- w.files(notebook.files);
256
- cells.forEach(cell => cell.write(w));
257
- };
258
- retVal.toString = (w = new Writer()) => {
259
- retVal.write(w);
260
- return w.toString().trim();
261
- };
262
- return retVal;
263
- }
264
- export type compileFunc = Awaited<ReturnType<typeof compile>>;
1
+ import type { ohq } from "@hpcc-js/observable-shim";
2
+
3
+ import { endsWith, join } from "@hpcc-js/util";
4
+ import { parseCell, ParsedImportCell } from "./cst";
5
+ import { Writer } from "./writer";
6
+ import { encodeBacktick, fetchEx, obfuscatedImport, ojs2notebook, omd2notebook } from "./util";
7
+
8
+ interface ImportDefine {
9
+ (runtime: ohq.Runtime, inspector?: ohq.InspectorFactory): ohq.Module;
10
+ dispose: () => void;
11
+ write: (w: Writer) => void;
12
+ }
13
+
14
+ async function importFile(relativePath: string, baseUrl: string) {
15
+ const path = join(baseUrl, relativePath);
16
+ const content = await fetchEx(path).then(r => r.text());
17
+ let notebook: ohq.Notebook;
18
+ if (endsWith(relativePath, ".ojsnb")) {
19
+ notebook = JSON.parse(content);
20
+ } else if (endsWith(relativePath, ".ojs")) {
21
+ notebook = ojs2notebook(content);
22
+ } else if (endsWith(relativePath, ".omd")) {
23
+ notebook = omd2notebook(content);
24
+ }
25
+ const retVal: ImportDefine = compile(notebook, baseUrl) as any;
26
+ retVal.dispose = () => { };
27
+ retVal.write = (w: Writer) => {
28
+ w.import(path);
29
+ };
30
+ return retVal;
31
+ }
32
+
33
+ // @ts-ignore - use precompiled notebook from observable
34
+ async function importCompiledNotebook(partial: string) {
35
+ const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
36
+ let impMod = {
37
+ default: function (runtime: ohq.Runtime, inspector?: ohq.InspectorFactory): ohq.Module {
38
+ return undefined;
39
+ } as any
40
+ };
41
+ try {
42
+ impMod = await obfuscatedImport(url);
43
+ } catch (e) {
44
+ }
45
+ const retVal: ImportDefine = impMod.default;
46
+ retVal.dispose = () => { };
47
+ retVal.write = (w: Writer) => {
48
+ w.import(url);
49
+ };
50
+ return retVal;
51
+ }
52
+
53
+ // @ts-ignore - recursive notebook parsing and compiling
54
+ async function importNotebook(partial: string) {
55
+ const url = `https://api.observablehq.com/document/${partial}`;
56
+ const notebook = fetchEx(url)
57
+ .then(r => {
58
+ return r.json();
59
+ }).catch(e => {
60
+ console.error(url);
61
+ console.error(e);
62
+ });
63
+ const retVal: ImportDefine = compile(await notebook) as any;
64
+ retVal.dispose = () => { };
65
+ retVal.write = (w: Writer) => {
66
+ w.import(url);
67
+ };
68
+ return retVal;
69
+ }
70
+
71
+ function createVariable(inspect: boolean, name?: string, inputs?: string[], definition?: any, inline = false) {
72
+
73
+ let i: ohq.Inspector;
74
+ let v: ohq.Variable;
75
+
76
+ const retVal = (module: ohq.Module, inspector?: ohq.InspectorFactory) => {
77
+ i = inspect ? inspector(name) : undefined;
78
+ v = module.variable(i);
79
+ if (arguments.length > 1) {
80
+ try {
81
+ v.define(name, inputs, definition);
82
+ } catch (e: any) {
83
+ console.error(e?.message);
84
+ }
85
+ }
86
+ return v;
87
+ };
88
+ retVal.dispose = () => {
89
+ try {
90
+ i?._node?.remove();
91
+ } catch (e) {
92
+ }
93
+ i = undefined;
94
+ try {
95
+ v?.delete();
96
+ } catch (e) {
97
+ }
98
+ v = undefined;
99
+ };
100
+ retVal.write = (w: Writer) => {
101
+ if (inline) {
102
+ w.define({ id: name, inputs, func: definition }, inspect, true);
103
+ } else {
104
+ const id = w.function({ id: name, func: definition });
105
+ w.define({ id: name, inputs, func: definition }, inspect, false, id);
106
+ }
107
+ };
108
+ return retVal;
109
+ }
110
+ type VariableFunc = ReturnType<typeof createVariable>;
111
+
112
+ function createImportVariable(name?: string, alias?: string) {
113
+
114
+ let v: ohq.Variable;
115
+
116
+ const retVal = (main: ohq.Module, otherModule: ohq.Module) => {
117
+ v = main.variable();
118
+ v.import(name, alias, otherModule);
119
+ };
120
+
121
+ retVal.dispose = () => {
122
+ v?.delete();
123
+ };
124
+ return retVal;
125
+ }
126
+ type ImportVariableFunc = ReturnType<typeof createImportVariable>;
127
+
128
+ async function createModule(parsed: ParsedImportCell, text: string, baseUrl: string) {
129
+ const otherModule = [".", "/"].indexOf(parsed.src[0]) === 0 ?
130
+ await importFile(parsed.src, baseUrl) :
131
+ await importCompiledNotebook(parsed.src);
132
+
133
+ const importVariables: ImportVariableFunc[] = [];
134
+ const variables: VariableFunc[] = [];
135
+ parsed.specifiers.forEach(spec => {
136
+ const viewof = spec.view ? "viewof " : "";
137
+ importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
138
+ if (spec.view) {
139
+ importVariables.push(createImportVariable(spec.name, spec.alias));
140
+ }
141
+ });
142
+ variables.push(createVariable(true, undefined, ["md"], md => {
143
+ return md`\`\`\`JavaScript
144
+ ${text}
145
+ \`\`\``;
146
+ }));
147
+
148
+ const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: ohq.InspectorFactory) => {
149
+
150
+ let mod = runtime.module(otherModule);
151
+ if (parsed.injections.length) {
152
+ mod = mod.derive(parsed.injections, main);
153
+ }
154
+ variables.forEach(v => v(main, inspector));
155
+ importVariables.forEach(v => v(main, mod));
156
+ return mod;
157
+ };
158
+ retVal.importVariables = importVariables;
159
+ retVal.variables = variables;
160
+ retVal.dispose = () => {
161
+ importVariables.forEach(v => v.dispose());
162
+ variables.forEach(v => v.dispose());
163
+ otherModule.dispose();
164
+ };
165
+ retVal.write = (w: Writer) => {
166
+ otherModule.write(w);
167
+ w.importDefine(parsed);
168
+ };
169
+ return retVal;
170
+ }
171
+ type ModuleFunc = Awaited<ReturnType<typeof createModule>>;
172
+
173
+ async function createCell(node: ohq.Node, baseUrl: string) {
174
+ const modules: ModuleFunc[] = [];
175
+ const variables: VariableFunc[] = [];
176
+ try {
177
+ const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
178
+ const parsed = parseCell(text);
179
+ switch (parsed.type) {
180
+ case "import":
181
+ modules.push(await createModule(parsed, text, baseUrl));
182
+ break;
183
+ case "viewof":
184
+ variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
185
+ variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
186
+ break;
187
+ case "mutable":
188
+ variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
189
+ variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
190
+ variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
191
+ break;
192
+ case "variable":
193
+ variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
194
+ break;
195
+ }
196
+ } catch (e) {
197
+ }
198
+
199
+ const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: ohq.InspectorFactory) => {
200
+ modules.forEach(imp => imp(runtime, main, inspector));
201
+ variables.forEach(v => v(main, inspector));
202
+ };
203
+ retVal.modules = modules;
204
+ retVal.variables = variables;
205
+ retVal.dispose = () => {
206
+ variables.forEach(v => v.dispose());
207
+ modules.forEach(mod => mod.dispose());
208
+ };
209
+ retVal.write = (w: Writer) => {
210
+ modules.forEach(imp => imp.write(w));
211
+ variables.forEach(v => v.write(w));
212
+ };
213
+ return retVal;
214
+ }
215
+ export type CellFunc = Awaited<ReturnType<typeof createCell>>;
216
+
217
+ function createFile(file: ohq.File): [string, any] {
218
+ function toString() { return globalThis.url; }
219
+ return [file.name, { url: new URL(file.url), mimeType: file.mime_type, toString }];
220
+ }
221
+ export type FileFunc = ReturnType<typeof createFile>;
222
+
223
+ export async function compile(notebook: ohq.Notebook, baseUrl: string = ".") {
224
+
225
+ const files = notebook.files.map(f => createFile(f));
226
+ const fileAttachments = new Map<string, any>(files);
227
+ let cells: CellFunc[] = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
228
+
229
+ const retVal = (runtime: ohq.Runtime, inspector?: ohq.InspectorFactory): ohq.Module => {
230
+ const main = runtime.module();
231
+ main.builtin("FileAttachment", runtime.fileAttachments(name => {
232
+ return fileAttachments.get(name) ?? { url: new URL(name), mimeType: null };
233
+ }));
234
+ cells.forEach(cell => {
235
+ cell(runtime, main, inspector);
236
+ });
237
+ return main;
238
+ };
239
+ retVal.fileAttachments = fileAttachments;
240
+ retVal.cells = cells;
241
+ retVal.appendCell = async (n: ohq.Node, baseUrl: string = ".") => {
242
+ const cell = await createCell(n, baseUrl);
243
+ cells.push(cell);
244
+ return cell;
245
+ };
246
+ retVal.disposeCell = async (cell: CellFunc) => {
247
+ cells = cells.filter(c => c !== cell);
248
+ cell.dispose();
249
+ };
250
+ retVal.dispose = () => {
251
+ cells.forEach(cell => cell.dispose());
252
+ cells = [];
253
+ };
254
+ retVal.write = (w: Writer) => {
255
+ w.files(notebook.files);
256
+ cells.forEach(cell => cell.write(w));
257
+ };
258
+ retVal.toString = (w = new Writer()) => {
259
+ retVal.write(w);
260
+ return w.toString().trim();
261
+ };
262
+ return retVal;
263
+ }
264
+ export type compileFunc = Awaited<ReturnType<typeof compile>>;