@hpcc-js/observablehq-compiler 1.1.2 → 1.1.4

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.
package/dist/index.js CHANGED
@@ -6233,15 +6233,15 @@
6233
6233
  if (tag != null && input) {
6234
6234
  cell = TemplateCellParser.parse(input, options);
6235
6235
  const parsedTag = CellTagParser.parse(tag, options);
6236
- parseReferences(parsedTag, tag, globals);
6237
- parseFeatures(parsedTag, tag);
6236
+ parseReferences$1(parsedTag, tag, globals);
6237
+ parseFeatures$1(parsedTag, tag);
6238
6238
  cell.tag = parsedTag;
6239
6239
  cell.raw = !!raw;
6240
6240
  } else {
6241
6241
  cell = CellParser.parse(input, options);
6242
6242
  }
6243
- parseReferences(cell, input, globals);
6244
- parseFeatures(cell, input);
6243
+ parseReferences$1(cell, input, globals);
6244
+ parseFeatures$1(cell, input);
6245
6245
  return cell;
6246
6246
  }
6247
6247
 
@@ -6553,7 +6553,7 @@
6553
6553
  // Find references.
6554
6554
  // Check for illegal references to arguments.
6555
6555
  // Check for illegal assignments to global references.
6556
- function parseReferences(cell, input, globals = defaultGlobals) {
6556
+ function parseReferences$1(cell, input, globals = defaultGlobals) {
6557
6557
  if (!cell.body) {
6558
6558
  cell.references = [];
6559
6559
  } else if (cell.body.type === "ImportDeclaration") {
@@ -6580,7 +6580,7 @@
6580
6580
  // Find features: file attachments, secrets, database clients.
6581
6581
  // Check for illegal references to arguments.
6582
6582
  // Check for illegal assignments to global references.
6583
- function parseFeatures(cell, input) {
6583
+ function parseFeatures$1(cell, input) {
6584
6584
  if (cell.body && cell.body.type !== "ImportDeclaration") {
6585
6585
  try {
6586
6586
  cell.fileAttachments = findFeatures(cell, "FileAttachment");
@@ -6603,7 +6603,6 @@
6603
6603
  }
6604
6604
  return cell;
6605
6605
  }
6606
-
6607
6606
  class ModuleParser extends CellParser {
6608
6607
  parseTopLevel(node) {
6609
6608
  if (!node.cells)
@@ -6622,10 +6621,12 @@
6622
6621
  return this.finishNode(node, "Program");
6623
6622
  }
6624
6623
  }
6625
- // @ts-ignore
6626
- function parseModule(input, { globals } = {}) {
6624
+ function splitModule(input) {
6627
6625
  // @ts-ignore
6628
- return ModuleParser.parse(input).cells.map(cell => input.substring(cell.start, cell.end));
6626
+ return ModuleParser.parse(input, { ecmaVersion: "latest" }).cells.map(cell => input.substring(cell.start, cell.end));
6627
+ }
6628
+ function parseCell$2(input) {
6629
+ return parseCell$1(input);
6629
6630
  }
6630
6631
 
6631
6632
  const FuncTypes = {
@@ -6664,10 +6665,20 @@
6664
6665
  .split("`").join("\\`");
6665
6666
  }
6666
6667
  function createParsedOJS(ojs, offset, inlineMD) {
6668
+ let cell;
6669
+ let error;
6670
+ try {
6671
+ cell = parseCell$2(ojs);
6672
+ }
6673
+ catch (e) {
6674
+ error = e;
6675
+ }
6667
6676
  return {
6668
6677
  ojs,
6669
6678
  offset,
6670
- inlineMD
6679
+ inlineMD,
6680
+ cell,
6681
+ error
6671
6682
  };
6672
6683
  }
6673
6684
  function parseOmd(_) {
@@ -6698,7 +6709,7 @@
6698
6709
  return retVal;
6699
6710
  }
6700
6711
  function ojs2notebook(ojs) {
6701
- const cells = parseModule(ojs);
6712
+ const cells = splitModule(ojs);
6702
6713
  return {
6703
6714
  files: [],
6704
6715
  nodes: cells.map((cell, idx) => {
@@ -6723,7 +6734,7 @@
6723
6734
  })
6724
6735
  };
6725
6736
  }
6726
- function fetchEx(url) {
6737
+ function fetchEx(url, proxyPrefix = "https://observable-cors.glitch.me/", proxyPostfix = "") {
6727
6738
  return fetch(url)
6728
6739
  .then(response => {
6729
6740
  if (response.ok)
@@ -6731,7 +6742,7 @@
6731
6742
  throw new Error("CORS?");
6732
6743
  }).catch(e => {
6733
6744
  const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img);
6734
- url = "https://observable-cors.glitch.me/" + url;
6745
+ url = `${proxyPrefix}${url}${proxyPostfix}`;
6735
6746
  return fetch(url, {
6736
6747
  headers: {
6737
6748
  origin: matches[0],
@@ -6862,7 +6873,7 @@
6862
6873
  }
6863
6874
  function parseCell(cellStr) {
6864
6875
  var _a, _b;
6865
- const cellAst = parseCell$1(cellStr);
6876
+ const cellAst = parseCell$2(cellStr);
6866
6877
  if (((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) == "ImportDeclaration") {
6867
6878
  return parseImportExpression(cellAst);
6868
6879
  }
@@ -6948,8 +6959,10 @@ export default function define(runtime, observer) {
6948
6959
  }
6949
6960
  }
6950
6961
 
6962
+ const isRelativePath = (path) => path[0] === ".";
6963
+ const fullUrl = (path, basePath) => isRelativePath(path) ? util.join(basePath, path) : path;
6951
6964
  async function importFile(relativePath, baseUrl) {
6952
- const path = util.join(baseUrl, relativePath);
6965
+ const path = fullUrl(relativePath, baseUrl);
6953
6966
  const content = await fetchEx(path).then(r => r.text());
6954
6967
  let notebook;
6955
6968
  if (util.endsWith(relativePath, ".ojsnb")) {
@@ -7042,7 +7055,7 @@ export default function define(runtime, observer) {
7042
7055
  return retVal;
7043
7056
  }
7044
7057
  async function createModule(parsed, text, baseUrl) {
7045
- const otherModule = [".", "/"].indexOf(parsed.src[0]) === 0 ?
7058
+ const otherModule = isRelativePath(parsed.src) ?
7046
7059
  await importFile(parsed.src, baseUrl) :
7047
7060
  await importCompiledNotebook(parsed.src);
7048
7061
  const importVariables = [];
@@ -7086,31 +7099,36 @@ ${text}
7086
7099
  const variables = [];
7087
7100
  try {
7088
7101
  const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
7089
- const parsed = parseCell(text);
7090
- switch (parsed.type) {
7091
- case "import":
7092
- modules.push(await createModule(parsed, text, baseUrl));
7093
- break;
7094
- case "viewof":
7095
- variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7096
- variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7097
- break;
7098
- case "mutable":
7099
- variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
7100
- variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7101
- variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7102
- break;
7103
- case "variable":
7104
- variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
7105
- break;
7102
+ const parsedModule = splitModule(text);
7103
+ for (const text of parsedModule) {
7104
+ const parsed = parseCell(text);
7105
+ switch (parsed.type) {
7106
+ case "import":
7107
+ modules.push(await createModule(parsed, text, baseUrl));
7108
+ break;
7109
+ case "viewof":
7110
+ variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7111
+ variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7112
+ break;
7113
+ case "mutable":
7114
+ variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
7115
+ variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7116
+ variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7117
+ break;
7118
+ case "variable":
7119
+ variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
7120
+ break;
7121
+ }
7106
7122
  }
7107
7123
  }
7108
7124
  catch (e) {
7125
+ variables.push(createVariable(true, undefined, [], e.message));
7109
7126
  }
7110
7127
  const retVal = (runtime, main, inspector) => {
7111
7128
  modules.forEach(imp => imp(runtime, main, inspector));
7112
7129
  variables.forEach(v => v(main, inspector));
7113
7130
  };
7131
+ retVal.id = "" + node.id;
7114
7132
  retVal.modules = modules;
7115
7133
  retVal.variables = variables;
7116
7134
  retVal.dispose = () => {
@@ -7123,20 +7141,22 @@ ${text}
7123
7141
  };
7124
7142
  return retVal;
7125
7143
  }
7126
- function createFile(file) {
7144
+ function createFile(file, baseUrl) {
7127
7145
  function toString() { return globalThis.url; }
7128
- return [file.name, { url: new URL(file.url), mimeType: file.mime_type, toString }];
7146
+ return [file.name, { url: new URL(fullUrl(file.url, baseUrl)), mimeType: file.mime_type, toString }];
7129
7147
  }
7130
7148
  async function compile(notebook, baseUrl = ".") {
7131
- const files = notebook.files.map(f => createFile(f));
7149
+ const files = notebook.files.map(f => createFile(f, baseUrl));
7132
7150
  const fileAttachments = new Map(files);
7133
- let cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
7151
+ const _cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
7152
+ const cells = new Map(_cells.map(c => [c.id, c]));
7134
7153
  const retVal = (runtime, inspector) => {
7135
7154
  const main = runtime.module();
7136
7155
  main.builtin("FileAttachment", runtime.fileAttachments(name => {
7137
7156
  var _a;
7138
- return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(name), mimeType: null };
7157
+ return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(fullUrl(name, baseUrl)), mimeType: null };
7139
7158
  }));
7159
+ main.builtin("fetchEx", fetchEx);
7140
7160
  cells.forEach(cell => {
7141
7161
  cell(runtime, main, inspector);
7142
7162
  });
@@ -7144,18 +7164,22 @@ ${text}
7144
7164
  };
7145
7165
  retVal.fileAttachments = fileAttachments;
7146
7166
  retVal.cells = cells;
7147
- retVal.appendCell = async (n, baseUrl = ".") => {
7167
+ retVal.appendCell = async (n, baseUrl) => {
7148
7168
  const cell = await createCell(n, baseUrl);
7149
- cells.push(cell);
7169
+ retVal.disposeCell(cell.id);
7170
+ cells.set(cell.id, cell);
7150
7171
  return cell;
7151
7172
  };
7152
- retVal.disposeCell = async (cell) => {
7153
- cells = cells.filter(c => c !== cell);
7154
- cell.dispose();
7173
+ retVal.disposeCell = async (id) => {
7174
+ const cell = cells.get(id);
7175
+ if (cell) {
7176
+ cells.delete(id);
7177
+ cell.dispose();
7178
+ }
7155
7179
  };
7156
7180
  retVal.dispose = () => {
7157
7181
  cells.forEach(cell => cell.dispose());
7158
- cells = [];
7182
+ cells.clear();
7159
7183
  };
7160
7184
  retVal.write = (w) => {
7161
7185
  w.files(notebook.files);
@@ -7168,11 +7192,11 @@ ${text}
7168
7192
  return retVal;
7169
7193
  }
7170
7194
 
7171
- exports.Writer = Writer;
7172
7195
  exports.compile = compile;
7173
7196
  exports.download = download;
7174
7197
  exports.ojs2notebook = ojs2notebook;
7175
7198
  exports.omd2notebook = omd2notebook;
7199
+ exports.parseOmd = parseOmd;
7176
7200
 
7177
7201
  Object.defineProperty(exports, '__esModule', { value: true });
7178
7202