@vizhub/runtime 2.0.0 → 3.0.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.
package/dist/index.js CHANGED
@@ -1,614 +1,213 @@
1
- import { g as j, p as I, a as O, b as F, s as z, t as D, d as T, c as V, e as $ } from "./slugCache-DqUp8B5A.js";
2
- import { f as oe, j as ie, i as ce, h as ae, k as le } from "./slugCache-DqUp8B5A.js";
3
- import { isVizId as W, getFileText as L, generateVizId as A, fileCollectionToVizFiles as P } from "@vizhub/viz-utils";
4
- const U = (e) => async (t) => {
5
- const s = j();
6
- return new Promise((i) => {
7
- const r = (o) => {
8
- o.data.type === "invalidateVizCacheResponse" && o.data.requestId === s && (e.removeEventListener(
1
+ import { g as D } from "./build-B2oXi2Mv.js";
2
+ import { b as x, c as G, e as _, a as A, f as F, d as J, s as T, v as Y } from "./build-B2oXi2Mv.js";
3
+ const S = (n) => async (t) => {
4
+ const i = D();
5
+ return new Promise((r) => {
6
+ const d = (l) => {
7
+ l.data.type === "invalidateVizCacheResponse" && l.data.requestId === i && (n.removeEventListener(
9
8
  "message",
10
- r
11
- ), i());
9
+ d
10
+ ), r());
12
11
  };
13
- e.addEventListener(
12
+ n.addEventListener(
14
13
  "message",
15
- r
16
- ), e.postMessage({
14
+ d
15
+ ), n.postMessage({
17
16
  type: "invalidateVizCacheRequest",
18
17
  changedVizIds: t,
19
- requestId: s
18
+ requestId: i
20
19
  });
21
20
  });
22
- }, q = ({
23
- worker: e,
21
+ }, V = ({
22
+ worker: n,
24
23
  setBuildErrorMessage: t
25
24
  }) => ({
26
- files: s,
27
- enableSourcemap: i,
28
- vizId: r
25
+ files: i,
26
+ enableSourcemap: r,
27
+ vizId: d
29
28
  }) => {
30
- const o = j();
29
+ const l = D();
31
30
  return new Promise(
32
- (c) => {
33
- const a = (m) => {
31
+ (s) => {
32
+ const c = (m) => {
34
33
  const p = m.data;
35
- if (p.type === "buildResponse" && p.requestId === o) {
36
- e.removeEventListener(
34
+ if (p.type === "buildResponse" && p.requestId === l) {
35
+ n.removeEventListener(
37
36
  "message",
38
- a
37
+ c
39
38
  );
40
- const w = p.buildResult, v = p.error;
41
- v ? t == null || t(v) : t == null || t(null), c(w);
39
+ const E = p.buildResult, f = p.error;
40
+ f ? t == null || t(f) : t == null || t(null), s(E);
42
41
  }
43
42
  };
44
- e.addEventListener("message", a);
45
- const u = {
43
+ n.addEventListener("message", c);
44
+ const h = {
46
45
  type: "buildRequest",
47
- files: s,
48
- enableSourcemap: i,
49
- requestId: o,
50
- vizId: r
46
+ files: i,
47
+ enableSourcemap: r,
48
+ requestId: l,
49
+ vizId: d
51
50
  };
52
- e.postMessage(u);
51
+ n.postMessage(h);
53
52
  }
54
53
  );
55
- }, S = "IDLE", x = "ENQUEUED", b = "PENDING_CLEAN", C = "PENDING_DIRTY", B = ({
56
- iframe: e,
54
+ }, I = "IDLE", y = "ENQUEUED", L = "PENDING_CLEAN", g = "PENDING_DIRTY", z = ({
55
+ iframe: n,
57
56
  worker: t,
58
- setBuildErrorMessage: s,
59
- getLatestContent: i,
60
- resolveSlugKey: r,
61
- writeFile: o
57
+ setBuildErrorMessage: i,
58
+ getLatestContent: r,
59
+ resolveSlugKey: d,
60
+ writeFile: l
62
61
  }) => {
63
- let c = S, a = null;
64
- const u = q({
62
+ let s = I, c = null;
63
+ const h = V({
65
64
  worker: t,
66
- setBuildErrorMessage: s
65
+ setBuildErrorMessage: i
67
66
  }), m = ({
68
- data: l
67
+ data: o
69
68
  }) => {
70
- if (l.type === "contentRequest" && i) {
71
- const { vizId: n } = l;
72
- i(n).then((d) => {
69
+ if (o.type === "contentRequest" && r) {
70
+ const { vizId: e } = o;
71
+ r(e).then((a) => {
73
72
  t.postMessage({
74
73
  type: "contentResponse",
75
- vizId: n,
76
- content: d
74
+ vizId: e,
75
+ content: a
77
76
  });
78
77
  });
79
- } else if (l.type === "resolveSlugRequest") {
80
- const { slugKey: n, requestId: d } = l;
81
- if (r)
82
- r(n).then(
83
- (f) => {
84
- const h = {
78
+ } else if (o.type === "resolveSlugRequest") {
79
+ const { slugKey: e, requestId: a } = o;
80
+ if (d)
81
+ d(e).then(
82
+ (u) => {
83
+ const v = {
85
84
  type: "resolveSlugResponse",
86
- vizId: f,
87
- requestId: d
85
+ vizId: u,
86
+ requestId: a
88
87
  };
89
- t.postMessage(h);
88
+ t.postMessage(v);
90
89
  }
91
90
  );
92
91
  else {
93
- const f = {
92
+ const u = {
94
93
  type: "resolveSlugResponse",
95
94
  vizId: null,
96
- requestId: d
95
+ requestId: a
97
96
  };
98
- t.postMessage(f);
97
+ t.postMessage(u);
99
98
  }
100
99
  }
101
100
  };
102
101
  t.addEventListener("message", m);
103
- const p = (l) => {
104
- const n = l.data;
105
- (n.type === "runDone" || n.type === "runError") && n.type === "runError" && s && s(n.error.message), n.type === "writeFile" && o && n.fileName && n.content && o(n.fileName, n.content);
102
+ const p = (o) => {
103
+ const e = o.data;
104
+ (e.type === "runDone" || e.type === "runError") && e.type === "runError" && i && i(e.error.message), e.type === "writeFile" && l && e.fileName && e.content && l(e.fileName, e.content);
106
105
  };
107
106
  window.addEventListener("message", p);
108
- const w = () => {
107
+ const E = () => {
109
108
  t.removeEventListener("message", m), window.removeEventListener("message", p);
110
- }, v = async ({
111
- files: l,
112
- enableHotReloading: n = !1,
113
- enableSourcemap: d = !1,
114
- vizId: f
109
+ }, f = async ({
110
+ files: o,
111
+ enableHotReloading: e = !1,
112
+ enableSourcemap: a = !1,
113
+ vizId: u
115
114
  }) => {
116
- c = b;
117
- const h = await u({
118
- files: l,
119
- enableSourcemap: d,
120
- vizId: f
115
+ s = L;
116
+ const v = await h({
117
+ files: o,
118
+ enableSourcemap: a,
119
+ vizId: u
121
120
  });
122
- if (h) {
123
- const { html: E, js: y, css: R } = h;
124
- if (console.clear(), n) {
125
- if (R) {
126
- const N = {
121
+ if (v) {
122
+ const { html: C, js: N, css: w } = v;
123
+ if (console.clear(), e) {
124
+ if (w) {
125
+ const R = {
127
126
  type: "runCSS",
128
- css: R
127
+ css: w
129
128
  };
130
- if (!e.contentWindow)
129
+ if (!n.contentWindow)
131
130
  throw new Error(
132
131
  "iframe.contentWindow is null - this should never happen"
133
132
  );
134
- e.contentWindow.postMessage(
135
- N,
133
+ n.contentWindow.postMessage(
134
+ R,
136
135
  window.location.origin
137
136
  );
138
137
  }
139
- if (y) {
140
- const N = {
138
+ if (N) {
139
+ const R = {
141
140
  type: "runJS",
142
- js: y
141
+ js: N
143
142
  };
144
- if (!e.contentWindow)
143
+ if (!n.contentWindow)
145
144
  throw new Error(
146
145
  "iframe.contentWindow is null - this should never happen"
147
146
  );
148
- e.contentWindow.postMessage(
149
- N,
147
+ n.contentWindow.postMessage(
148
+ R,
150
149
  window.location.origin
151
150
  );
152
151
  }
153
152
  } else
154
- e.srcdoc = E || "";
153
+ n.srcdoc = C || "";
155
154
  }
156
- c === C ? (requestAnimationFrame(() => {
157
- if (!a)
155
+ s === g ? (requestAnimationFrame(() => {
156
+ if (!c)
158
157
  throw new Error(
159
158
  "latestFiles is not defined and state is PENDING_DIRTY - this should never happen"
160
159
  );
161
- v({
162
- files: a,
163
- enableHotReloading: n,
164
- enableSourcemap: d,
165
- vizId: f
160
+ f({
161
+ files: c,
162
+ enableHotReloading: e,
163
+ enableSourcemap: a,
164
+ vizId: u
166
165
  });
167
- }), c = x) : c = S;
168
- }, g = ({
169
- files: l,
170
- enableHotReloading: n = !1,
171
- enableSourcemap: d = !1,
172
- vizId: f = void 0
166
+ }), s = y) : s = I;
167
+ }, q = ({
168
+ files: o,
169
+ enableHotReloading: e = !1,
170
+ enableSourcemap: a = !1,
171
+ vizId: u = void 0
173
172
  }) => {
174
- if (a = null, c === S)
175
- c = x, v({
176
- files: l,
177
- enableHotReloading: n,
178
- enableSourcemap: d,
179
- vizId: f
173
+ if (c = null, s === I)
174
+ s = y, f({
175
+ files: o,
176
+ enableHotReloading: e,
177
+ enableSourcemap: a,
178
+ vizId: u
180
179
  });
181
- else if (c === b)
182
- a = l, c = C;
183
- else if (c === C)
184
- a = l;
185
- else if (c === x)
186
- a = l;
180
+ else if (s === L)
181
+ c = o, s = g;
182
+ else if (s === g)
183
+ c = o;
184
+ else if (s === y)
185
+ c = o;
187
186
  else
188
- throw new Error(`Unexpected state: ${c}`);
187
+ throw new Error(`Unexpected state: ${s}`);
189
188
  };
190
189
  return {
191
- invalidateVizCache: U(t),
192
- run: g,
193
- cleanup: w
190
+ invalidateVizCache: S(t),
191
+ run: q,
192
+ cleanup: E
194
193
  };
195
- }, J = `var EOL = {},
196
- EOF = {},
197
- QUOTE = 34,
198
- NEWLINE = 10,
199
- RETURN = 13;
200
-
201
- function objectConverter(columns) {
202
- return new Function(
203
- 'd',
204
- 'return {' +
205
- columns
206
- .map(function (name, i) {
207
- return (
208
- JSON.stringify(name) + ': d[' + i + '] || ""'
209
- );
210
- })
211
- .join(',') +
212
- '}',
213
- );
214
- }
215
-
216
- function customConverter(columns, f) {
217
- var object = objectConverter(columns);
218
- return function (row, i) {
219
- return f(object(row), i, columns);
220
- };
221
- }
222
-
223
- function dsv(delimiter) {
224
- var DELIMITER = delimiter.charCodeAt(0);
225
-
226
- function parse(text, f) {
227
- var convert,
228
- columns,
229
- rows = parseRows(text, function (row, i) {
230
- if (convert) return convert(row, i - 1);
231
- (columns = row),
232
- (convert = f
233
- ? customConverter(row, f)
234
- : objectConverter(row));
235
- });
236
- rows.columns = columns || [];
237
- return rows;
238
- }
239
-
240
- function parseRows(text, f) {
241
- var rows = [], // output rows
242
- N = text.length,
243
- I = 0, // current character index
244
- n = 0, // current line number
245
- t, // current token
246
- eof = N <= 0, // current token followed by EOF?
247
- eol = false; // current token followed by EOL?
248
-
249
- // Strip the trailing newline.
250
- if (text.charCodeAt(N - 1) === NEWLINE) --N;
251
- if (text.charCodeAt(N - 1) === RETURN) --N;
252
-
253
- function token() {
254
- if (eof) return EOF;
255
- if (eol) return (eol = false), EOL;
256
-
257
- // Unescape quotes.
258
- var i,
259
- j = I,
260
- c;
261
- if (text.charCodeAt(j) === QUOTE) {
262
- while (
263
- (I++ < N && text.charCodeAt(I) !== QUOTE) ||
264
- text.charCodeAt(++I) === QUOTE
265
- );
266
- if ((i = I) >= N) eof = true;
267
- else if ((c = text.charCodeAt(I++)) === NEWLINE)
268
- eol = true;
269
- else if (c === RETURN) {
270
- eol = true;
271
- if (text.charCodeAt(I) === NEWLINE) ++I;
272
- }
273
- return text.slice(j + 1, i - 1).replace(/""/g, '"');
274
- }
275
-
276
- // Find next delimiter or newline.
277
- while (I < N) {
278
- if ((c = text.charCodeAt((i = I++))) === NEWLINE)
279
- eol = true;
280
- else if (c === RETURN) {
281
- eol = true;
282
- if (text.charCodeAt(I) === NEWLINE) ++I;
283
- } else if (c !== DELIMITER) continue;
284
- return text.slice(j, i);
285
- }
286
-
287
- // Return last token before EOF.
288
- return (eof = true), text.slice(j, N);
289
- }
290
-
291
- while ((t = token()) !== EOF) {
292
- var row = [];
293
- while (t !== EOL && t !== EOF)
294
- row.push(t), (t = token());
295
- if (f && (row = f(row, n++)) == null) continue;
296
- rows.push(row);
297
- }
298
-
299
- return rows;
300
- }
301
-
302
- return {
303
- parse: parse,
304
- };
305
- }
306
-
307
- var csv = dsv(',');
308
-
309
- var csvParse = csv.parse;
310
-
311
- var tsv = dsv(' ');
312
-
313
- var tsvParse = tsv.parse;
314
-
315
- export { csvParse, tsvParse };`, k = (e) => (
316
- // str.replace(/`/g, '\\`');
317
- e.replace(/\\/g, "\\\\").replace(/`/g, "\\`")
318
- ), _ = () => ({
319
- name: "transformDSV",
320
- // `id` here is of the form
321
- // `{vizId}/{fileName}`
322
- transform: async (e, t) => {
323
- const { fileName: s } = I(t), i = s.endsWith(".csv"), r = s.endsWith(".tsv");
324
- if (i || r)
325
- return {
326
- code: `
327
- ${J}
328
- const data = ${i ? "csvParse" : "tsvParse"}(\`${k(e)}\`);
329
- export default data;
330
- `,
331
- map: { mappings: "" }
332
- };
333
- }
334
- }), G = (e) => {
335
- const t = /^@([a-zA-Z0-9_-]+)\/([a-zA-Z0-9_-]+)$/, s = e.match(t);
336
- return s ? {
337
- userName: s[1],
338
- idOrSlug: s[2]
339
- } : null;
340
- }, Q = ({
341
- vizId: e,
342
- slugCache: t
343
- }) => ({
344
- name: "vizResolve",
345
- resolveId: async (s, i) => {
346
- if (s.startsWith("./") && !(i != null && i.startsWith("https://"))) {
347
- let o = s.substring(2);
348
- if (!o.endsWith(".js") && !o.endsWith(".css") && !o.endsWith(".csv") && !o.endsWith(".svelte") && (o += ".js"), i) {
349
- const {
350
- vizId: c,
351
- fileName: a
352
- } = I(i), u = a.split("/").slice(0, -1).join("/"), m = u ? `${u}/${o}` : o;
353
- return `${c}/${m}`;
354
- }
355
- return e + "/" + o;
356
- }
357
- const r = G(s);
358
- if (r) {
359
- let o;
360
- if (W(r.idOrSlug))
361
- o = r.idOrSlug;
362
- else {
363
- if (!t)
364
- throw new Error(
365
- "slugCache is required to import by slug in v3 runtime"
366
- );
367
- o = await t.get(
368
- `${r.userName}/${r.idOrSlug}`
369
- );
370
- }
371
- return o + "/index.js";
372
- }
373
- }
374
- }), M = ({
375
- vizCache: e,
376
- trackCSSImport: t,
377
- vizId: s,
378
- files: i
379
- }) => ({
380
- name: "vizLoad",
381
- // `id` here is of the form
382
- // `{vizId}/{fileName}`
383
- load: async (r) => {
384
- const o = I(r), c = o.vizId, a = o.fileName;
385
- if (a.endsWith(".css"))
386
- return t(r), "";
387
- let u = null;
388
- if (c === s && i)
389
- u = i[a] || null;
390
- else {
391
- const m = await e.get(c);
392
- u = L(m, a);
393
- }
394
- if (u === null)
395
- throw new Error(
396
- `Imported file "${a}" not found.`
397
- );
398
- return u;
399
- }
400
- }), Y = async ({
401
- files: e,
402
- rollup: t,
403
- enableSourcemap: s = !0,
404
- vizCache: i,
405
- vizId: r,
406
- slugCache: o,
407
- getSvelteCompiler: c
408
- }) => {
409
- const a = /* @__PURE__ */ new Set(), u = (n) => {
410
- a.add(n);
411
- };
412
- if (!e["index.js"])
413
- throw new Error("Missing index.js");
414
- const p = {
415
- input: "./index.js",
416
- plugins: [
417
- ...r ? [Q({ vizId: r, slugCache: o })] : [],
418
- _(),
419
- z(),
420
- D({ getSvelteCompiler: c }),
421
- ...i ? [
422
- M({
423
- vizCache: i,
424
- trackCSSImport: u,
425
- vizId: r,
426
- files: e
427
- })
428
- ] : []
429
- ],
430
- onwarn(n, d) {
431
- n.code !== "UNRESOLVED_IMPORT" && d(n);
432
- }
433
- }, w = {
434
- format: "umd",
435
- name: "Viz",
436
- sourcemap: !!s,
437
- compact: !0
438
- }, v = O(e);
439
- if (v) {
440
- const n = F(v);
441
- n && (p.external = Object.keys(n), w.globals = n);
442
- }
443
- const g = await t(p), { output: l } = await g.generate(w);
444
- return {
445
- src: l[0].code,
446
- cssFiles: Array.from(a)
447
- };
448
- };
449
- function Z() {
450
- return Math.random().toString().slice(2, 7);
451
- }
452
- const K = ({
453
- cdn: e,
454
- src: t,
455
- styles: s
456
- }) => {
457
- const r = `viz-container-${Z()}`;
458
- return `<!DOCTYPE html>
459
- <html>
460
- <head>
461
- <meta charset="utf-8">${e}${s}
462
- <style>
463
- body {
464
- margin: 0;
465
- overflow: hidden;
466
- }
467
- #${r} {
468
- height: 100vh;
469
- }
470
- </style>
471
- </head>
472
- <body>
473
- <div id="${r}"></div>
474
- <script id="injected-script">${t}<\/script>
475
- <script>
476
- (() => {
477
- let cleanup;
478
- const render = () => {
479
- const container = document.getElementById('${r}');
480
- typeof cleanup === 'function' && cleanup();
481
- cleanup = Viz.main(container, { state: window.state, setState, writeFile });
482
- };
483
- const setState = (next) => {
484
- window.state = next(window.state);
485
- render();
486
- };
487
- const writeFile = (fileName, content) => {
488
- parent.postMessage({ type: 'writeFile', fileName, content }, "*");
489
- };
490
- const run = () => {
491
- try {
492
- setState((state) => state || {});
493
- } catch (error) {
494
- console.error(error);
495
- parent.postMessage({ type: 'runError', error }, "*");
496
- }
497
- }
498
- run();
499
- const runJS = (src) => {
500
- document.getElementById('injected-script')?.remove();
501
- const script = document.createElement('script');
502
- script.textContent = src;
503
- script.id = 'injected-script';
504
- document.body.appendChild(script);
505
- run();
506
- };
507
- const runCSS = (css) => {
508
- let style = document.getElementById('injected-style');
509
- if (!style) {
510
- style = document.createElement('style');
511
- style.type = 'text/css';
512
- style.id = 'injected-style';
513
- document.head.appendChild(style);
514
- }
515
- style.textContent = css;
516
- };
517
- onmessage = (message) => {
518
- switch (message.data.type) {
519
- case 'runJS':
520
- runJS(message.data.js);
521
- parent.postMessage({ type: 'runDone' }, "*");
522
- break;
523
- case 'runCSS':
524
- runCSS(message.data.css);
525
- break;
526
- case 'ping':
527
- parent.postMessage({ type: 'pong' }, "*");
528
- break;
529
- default:
530
- break;
531
- }
532
- }
533
- })();
534
- <\/script>
535
- </body>
536
- </html>`;
537
- }, ee = async ({
538
- files: e,
539
- rollup: t,
540
- enableSourcemap: s = !0,
541
- vizCache: i,
542
- vizId: r,
543
- slugCache: o,
544
- getSvelteCompiler: c
545
- }) => {
546
- const { src: a, cssFiles: u } = await Y({
547
- files: e,
548
- rollup: t,
549
- enableSourcemap: s,
550
- vizCache: i,
551
- vizId: r,
552
- slugCache: o,
553
- getSvelteCompiler: c
554
- });
555
- let m = [];
556
- if (u.length > 0)
557
- for (let l = 0; l < u.length; l++) {
558
- const n = u[l], d = I(n), f = d.vizId, h = d.fileName;
559
- let E = null;
560
- if (f === r && e)
561
- E = e[h] || null;
562
- else {
563
- const y = await i.get(f);
564
- E = L(y, h);
565
- }
566
- E && m.push(E);
567
- }
568
- const p = m.join(`
569
- `), w = `
570
- <style id="injected-style">${p}</style>`;
571
- let v = "";
572
- const g = Object.entries(
573
- T(e)
574
- );
575
- if (g.length > 0) {
576
- const l = V(e);
577
- v = g.map(([n, d], f) => {
578
- const h = $(
579
- { name: n, version: d },
580
- l
581
- );
582
- return `${f > 0 ? " " : `
583
- `}<script src="${h}"><\/script>`;
584
- }).join("");
585
- }
586
- return {
587
- html: K({ cdn: v, src: a, styles: w }),
588
- css: p,
589
- js: a
590
- };
591
- }, te = ({
592
- rawMessage: e,
194
+ }, P = ({
195
+ rawMessage: n,
593
196
  vizId: t
594
197
  }) => {
595
- const s = new RegExp(t, "g");
596
- return e == null ? void 0 : e.replace(s, ".");
597
- }, ne = (e, t = "Sample Content for Exporting", s = A()) => ({
598
- id: s,
599
- files: P(e),
600
- title: t
601
- });
198
+ const i = new RegExp(t, "g");
199
+ return n == null ? void 0 : n.replace(i, ".");
200
+ };
602
201
  export {
603
- oe as build,
604
- te as cleanRollupErrorMessage,
605
- Y as computeBundleJSV3,
606
- B as createRuntime,
607
- ie as createSlugCache,
608
- ce as createVizCache,
609
- ne as createVizContent,
610
- ae as determineRuntimeVersion,
611
- le as svelteCompilerUrl,
612
- ee as v3Build
202
+ x as build,
203
+ P as cleanRollupErrorMessage,
204
+ G as computeBundleJSV3,
205
+ z as createRuntime,
206
+ _ as createSlugCache,
207
+ A as createVizCache,
208
+ F as createVizContent,
209
+ J as determineRuntimeVersion,
210
+ T as svelteCompilerUrl,
211
+ Y as v3Build
613
212
  };
614
213
  //# sourceMappingURL=index.js.map