@hpcc-js/observablehq-compiler 1.1.2 → 1.1.3

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.esm.js CHANGED
@@ -6229,15 +6229,15 @@ function parseCell$1(input, {tag, raw, globals, ...options} = {}) {
6229
6229
  if (tag != null && input) {
6230
6230
  cell = TemplateCellParser.parse(input, options);
6231
6231
  const parsedTag = CellTagParser.parse(tag, options);
6232
- parseReferences(parsedTag, tag, globals);
6233
- parseFeatures(parsedTag, tag);
6232
+ parseReferences$1(parsedTag, tag, globals);
6233
+ parseFeatures$1(parsedTag, tag);
6234
6234
  cell.tag = parsedTag;
6235
6235
  cell.raw = !!raw;
6236
6236
  } else {
6237
6237
  cell = CellParser.parse(input, options);
6238
6238
  }
6239
- parseReferences(cell, input, globals);
6240
- parseFeatures(cell, input);
6239
+ parseReferences$1(cell, input, globals);
6240
+ parseFeatures$1(cell, input);
6241
6241
  return cell;
6242
6242
  }
6243
6243
 
@@ -6549,7 +6549,7 @@ class CellTagParser extends Parser {
6549
6549
  // Find references.
6550
6550
  // Check for illegal references to arguments.
6551
6551
  // Check for illegal assignments to global references.
6552
- function parseReferences(cell, input, globals = defaultGlobals) {
6552
+ function parseReferences$1(cell, input, globals = defaultGlobals) {
6553
6553
  if (!cell.body) {
6554
6554
  cell.references = [];
6555
6555
  } else if (cell.body.type === "ImportDeclaration") {
@@ -6576,7 +6576,7 @@ function parseReferences(cell, input, globals = defaultGlobals) {
6576
6576
  // Find features: file attachments, secrets, database clients.
6577
6577
  // Check for illegal references to arguments.
6578
6578
  // Check for illegal assignments to global references.
6579
- function parseFeatures(cell, input) {
6579
+ function parseFeatures$1(cell, input) {
6580
6580
  if (cell.body && cell.body.type !== "ImportDeclaration") {
6581
6581
  try {
6582
6582
  cell.fileAttachments = findFeatures(cell, "FileAttachment");
@@ -6599,7 +6599,6 @@ function parseFeatures(cell, input) {
6599
6599
  }
6600
6600
  return cell;
6601
6601
  }
6602
-
6603
6602
  class ModuleParser extends CellParser {
6604
6603
  parseTopLevel(node) {
6605
6604
  if (!node.cells)
@@ -6618,10 +6617,12 @@ class ModuleParser extends CellParser {
6618
6617
  return this.finishNode(node, "Program");
6619
6618
  }
6620
6619
  }
6621
- // @ts-ignore
6622
- function parseModule(input, { globals } = {}) {
6620
+ function splitModule(input) {
6623
6621
  // @ts-ignore
6624
- return ModuleParser.parse(input).cells.map(cell => input.substring(cell.start, cell.end));
6622
+ return ModuleParser.parse(input, { ecmaVersion: "latest" }).cells.map(cell => input.substring(cell.start, cell.end));
6623
+ }
6624
+ function parseCell$2(input) {
6625
+ return parseCell$1(input);
6625
6626
  }
6626
6627
 
6627
6628
  const FuncTypes = {
@@ -6660,10 +6661,20 @@ function encodeBacktick(str) {
6660
6661
  .split("`").join("\\`");
6661
6662
  }
6662
6663
  function createParsedOJS(ojs, offset, inlineMD) {
6664
+ let cell;
6665
+ let error;
6666
+ try {
6667
+ cell = parseCell$2(ojs);
6668
+ }
6669
+ catch (e) {
6670
+ error = e;
6671
+ }
6663
6672
  return {
6664
6673
  ojs,
6665
6674
  offset,
6666
- inlineMD
6675
+ inlineMD,
6676
+ cell,
6677
+ error
6667
6678
  };
6668
6679
  }
6669
6680
  function parseOmd(_) {
@@ -6694,7 +6705,7 @@ function parseOmd(_) {
6694
6705
  return retVal;
6695
6706
  }
6696
6707
  function ojs2notebook(ojs) {
6697
- const cells = parseModule(ojs);
6708
+ const cells = splitModule(ojs);
6698
6709
  return {
6699
6710
  files: [],
6700
6711
  nodes: cells.map((cell, idx) => {
@@ -6719,7 +6730,7 @@ function omd2notebook(omd) {
6719
6730
  })
6720
6731
  };
6721
6732
  }
6722
- function fetchEx(url) {
6733
+ function fetchEx(url, proxyPrefix = "https://observable-cors.glitch.me/", proxyPostfix = "") {
6723
6734
  return fetch(url)
6724
6735
  .then(response => {
6725
6736
  if (response.ok)
@@ -6727,7 +6738,7 @@ function fetchEx(url) {
6727
6738
  throw new Error("CORS?");
6728
6739
  }).catch(e => {
6729
6740
  const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img);
6730
- url = "https://observable-cors.glitch.me/" + url;
6741
+ url = `${proxyPrefix}${url}${proxyPostfix}`;
6731
6742
  return fetch(url, {
6732
6743
  headers: {
6733
6744
  origin: matches[0],
@@ -6858,7 +6869,7 @@ function parseVariableExpression(cellStr, cellAst, refs, bodyStr) {
6858
6869
  }
6859
6870
  function parseCell(cellStr) {
6860
6871
  var _a, _b;
6861
- const cellAst = parseCell$1(cellStr);
6872
+ const cellAst = parseCell$2(cellStr);
6862
6873
  if (((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) == "ImportDeclaration") {
6863
6874
  return parseImportExpression(cellAst);
6864
6875
  }
@@ -6944,8 +6955,10 @@ export default function define(runtime, observer) {
6944
6955
  }
6945
6956
  }
6946
6957
 
6958
+ const isRelativePath = (path) => path[0] === ".";
6959
+ const fullUrl = (path, basePath) => isRelativePath(path) ? join(basePath, path) : path;
6947
6960
  async function importFile(relativePath, baseUrl) {
6948
- const path = join(baseUrl, relativePath);
6961
+ const path = fullUrl(relativePath, baseUrl);
6949
6962
  const content = await fetchEx(path).then(r => r.text());
6950
6963
  let notebook;
6951
6964
  if (endsWith(relativePath, ".ojsnb")) {
@@ -7038,7 +7051,7 @@ function createImportVariable(name, alias) {
7038
7051
  return retVal;
7039
7052
  }
7040
7053
  async function createModule(parsed, text, baseUrl) {
7041
- const otherModule = [".", "/"].indexOf(parsed.src[0]) === 0 ?
7054
+ const otherModule = isRelativePath(parsed.src) ?
7042
7055
  await importFile(parsed.src, baseUrl) :
7043
7056
  await importCompiledNotebook(parsed.src);
7044
7057
  const importVariables = [];
@@ -7082,31 +7095,36 @@ async function createCell(node, baseUrl) {
7082
7095
  const variables = [];
7083
7096
  try {
7084
7097
  const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
7085
- const parsed = parseCell(text);
7086
- switch (parsed.type) {
7087
- case "import":
7088
- modules.push(await createModule(parsed, text, baseUrl));
7089
- break;
7090
- case "viewof":
7091
- variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7092
- variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7093
- break;
7094
- case "mutable":
7095
- variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
7096
- variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7097
- variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7098
- break;
7099
- case "variable":
7100
- variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
7101
- break;
7098
+ const parsedModule = splitModule(text);
7099
+ for (const text of parsedModule) {
7100
+ const parsed = parseCell(text);
7101
+ switch (parsed.type) {
7102
+ case "import":
7103
+ modules.push(await createModule(parsed, text, baseUrl));
7104
+ break;
7105
+ case "viewof":
7106
+ variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7107
+ variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7108
+ break;
7109
+ case "mutable":
7110
+ variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
7111
+ variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7112
+ variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7113
+ break;
7114
+ case "variable":
7115
+ variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
7116
+ break;
7117
+ }
7102
7118
  }
7103
7119
  }
7104
7120
  catch (e) {
7121
+ variables.push(createVariable(true, undefined, [], e.message));
7105
7122
  }
7106
7123
  const retVal = (runtime, main, inspector) => {
7107
7124
  modules.forEach(imp => imp(runtime, main, inspector));
7108
7125
  variables.forEach(v => v(main, inspector));
7109
7126
  };
7127
+ retVal.id = "" + node.id;
7110
7128
  retVal.modules = modules;
7111
7129
  retVal.variables = variables;
7112
7130
  retVal.dispose = () => {
@@ -7119,20 +7137,22 @@ async function createCell(node, baseUrl) {
7119
7137
  };
7120
7138
  return retVal;
7121
7139
  }
7122
- function createFile(file) {
7140
+ function createFile(file, baseUrl) {
7123
7141
  function toString() { return globalThis.url; }
7124
- return [file.name, { url: new URL(file.url), mimeType: file.mime_type, toString }];
7142
+ return [file.name, { url: new URL(fullUrl(file.url, baseUrl)), mimeType: file.mime_type, toString }];
7125
7143
  }
7126
7144
  async function compile(notebook, baseUrl = ".") {
7127
- const files = notebook.files.map(f => createFile(f));
7145
+ const files = notebook.files.map(f => createFile(f, baseUrl));
7128
7146
  const fileAttachments = new Map(files);
7129
- let cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
7147
+ const _cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
7148
+ const cells = new Map(_cells.map(c => [c.id, c]));
7130
7149
  const retVal = (runtime, inspector) => {
7131
7150
  const main = runtime.module();
7132
7151
  main.builtin("FileAttachment", runtime.fileAttachments(name => {
7133
7152
  var _a;
7134
- return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(name), mimeType: null };
7153
+ return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(fullUrl(name, baseUrl)), mimeType: null };
7135
7154
  }));
7155
+ main.builtin("fetchEx", fetchEx);
7136
7156
  cells.forEach(cell => {
7137
7157
  cell(runtime, main, inspector);
7138
7158
  });
@@ -7140,18 +7160,22 @@ async function compile(notebook, baseUrl = ".") {
7140
7160
  };
7141
7161
  retVal.fileAttachments = fileAttachments;
7142
7162
  retVal.cells = cells;
7143
- retVal.appendCell = async (n, baseUrl = ".") => {
7163
+ retVal.appendCell = async (n, baseUrl) => {
7144
7164
  const cell = await createCell(n, baseUrl);
7145
- cells.push(cell);
7165
+ retVal.disposeCell(cell.id);
7166
+ cells.set(cell.id, cell);
7146
7167
  return cell;
7147
7168
  };
7148
- retVal.disposeCell = async (cell) => {
7149
- cells = cells.filter(c => c !== cell);
7150
- cell.dispose();
7169
+ retVal.disposeCell = async (id) => {
7170
+ const cell = cells.get(id);
7171
+ if (cell) {
7172
+ cells.delete(id);
7173
+ cell.dispose();
7174
+ }
7151
7175
  };
7152
7176
  retVal.dispose = () => {
7153
7177
  cells.forEach(cell => cell.dispose());
7154
- cells = [];
7178
+ cells.clear();
7155
7179
  };
7156
7180
  retVal.write = (w) => {
7157
7181
  w.files(notebook.files);
@@ -7164,5 +7188,5 @@ async function compile(notebook, baseUrl = ".") {
7164
7188
  return retVal;
7165
7189
  }
7166
7190
 
7167
- export { Writer, compile, download, ojs2notebook, omd2notebook };
7191
+ export { compile, download, ojs2notebook, omd2notebook, parseOmd };
7168
7192
  //# sourceMappingURL=index.esm.js.map