@dxos/plugin-script 0.7.5-main.9d26e3a → 0.7.5-main.b19bfc8
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/lib/browser/{AutomationPanel-RFQAQ7NF.mjs → AutomationPanel-6HFRNRWV.mjs} +3 -3
- package/dist/lib/browser/ScriptContainer-U3BFT76B.mjs +713 -0
- package/dist/lib/browser/ScriptContainer-U3BFT76B.mjs.map +7 -0
- package/dist/lib/browser/artifact-definition-NAWYXFF5.mjs +243 -0
- package/dist/lib/browser/artifact-definition-NAWYXFF5.mjs.map +7 -0
- package/dist/lib/browser/{chunk-K7KY6YMG.mjs → chunk-5SXOHNAK.mjs} +4 -4
- package/dist/lib/browser/chunk-5SXOHNAK.mjs.map +7 -0
- package/dist/lib/browser/chunk-6MOQ4BP3.mjs +15 -0
- package/dist/lib/browser/chunk-6MOQ4BP3.mjs.map +7 -0
- package/dist/lib/browser/chunk-FVVCAWNP.mjs +21 -0
- package/dist/lib/browser/chunk-FVVCAWNP.mjs.map +7 -0
- package/dist/lib/browser/chunk-HJNS3BBK.mjs +236 -0
- package/dist/lib/browser/chunk-HJNS3BBK.mjs.map +7 -0
- package/dist/lib/browser/chunk-N46LE7FS.mjs +40 -0
- package/dist/lib/browser/chunk-N46LE7FS.mjs.map +7 -0
- package/dist/lib/browser/chunk-PZPYF7M4.mjs +69 -0
- package/dist/lib/browser/chunk-PZPYF7M4.mjs.map +7 -0
- package/dist/lib/browser/compiler-YY2XSKHQ.mjs +82 -0
- package/dist/lib/browser/compiler-YY2XSKHQ.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +129 -288
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/intent-resolver-D6FPY5ZA.mjs +45 -0
- package/dist/lib/browser/intent-resolver-D6FPY5ZA.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/react-surface-DIXYM2HA.mjs +65 -0
- package/dist/lib/browser/react-surface-DIXYM2HA.mjs.map +7 -0
- package/dist/lib/browser/settings-SZCDKHWO.mjs +24 -0
- package/dist/lib/browser/settings-SZCDKHWO.mjs.map +7 -0
- package/dist/lib/browser/types.mjs +6 -4
- package/dist/lib/node/{AutomationPanel-5CJEF4MI.cjs → AutomationPanel-RBQ4DODN.cjs} +8 -8
- package/dist/lib/node/ScriptContainer-PSLB34RX.cjs +719 -0
- package/dist/lib/node/ScriptContainer-PSLB34RX.cjs.map +7 -0
- package/dist/lib/node/artifact-definition-7AANUS3X.cjs +262 -0
- package/dist/lib/node/artifact-definition-7AANUS3X.cjs.map +7 -0
- package/dist/lib/node/chunk-2UNNYU2C.cjs +264 -0
- package/dist/lib/node/chunk-2UNNYU2C.cjs.map +7 -0
- package/dist/lib/node/{chunk-6LYX73ZT.cjs → chunk-5UH3F7TA.cjs} +11 -8
- package/dist/lib/node/chunk-5UH3F7TA.cjs.map +7 -0
- package/dist/lib/node/{meta.cjs → chunk-ALY3N6PW.cjs} +12 -8
- package/dist/lib/node/chunk-ALY3N6PW.cjs.map +7 -0
- package/dist/lib/node/{chunk-PK3OQP5L.cjs → chunk-DJGLGRQV.cjs} +26 -11
- package/dist/lib/node/chunk-DJGLGRQV.cjs.map +7 -0
- package/dist/lib/node/chunk-ITAJ3WRW.cjs +77 -0
- package/dist/lib/node/chunk-ITAJ3WRW.cjs.map +7 -0
- package/dist/lib/node/{chunk-4INUJLXL.cjs → chunk-K3ZZWVXS.cjs} +7 -7
- package/dist/lib/node/chunk-K3ZZWVXS.cjs.map +7 -0
- package/dist/lib/node/compiler-UIBLKJKG.cjs +104 -0
- package/dist/lib/node/compiler-UIBLKJKG.cjs.map +7 -0
- package/dist/lib/node/index.cjs +122 -277
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/intent-resolver-NYJFPD5B.cjs +59 -0
- package/dist/lib/node/intent-resolver-NYJFPD5B.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/react-surface-JOMWTFI2.cjs +84 -0
- package/dist/lib/node/react-surface-JOMWTFI2.cjs.map +7 -0
- package/dist/lib/node/settings-SZFSMSKM.cjs +38 -0
- package/dist/lib/node/settings-SZFSMSKM.cjs.map +7 -0
- package/dist/lib/node/types.cjs +6 -4
- package/dist/lib/node/types.cjs.map +2 -2
- package/dist/lib/node-esm/{AutomationPanel-7Y6REBNJ.mjs → AutomationPanel-WTVVVMRH.mjs} +3 -3
- package/dist/lib/node-esm/ScriptContainer-E4J6XXE6.mjs +714 -0
- package/dist/lib/node-esm/ScriptContainer-E4J6XXE6.mjs.map +7 -0
- package/dist/lib/node-esm/artifact-definition-KBASPVMM.mjs +245 -0
- package/dist/lib/node-esm/artifact-definition-KBASPVMM.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-OFYGBUNI.mjs → chunk-2AFXX34U.mjs} +4 -4
- package/dist/lib/node-esm/chunk-2AFXX34U.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-4OCRARHP.mjs +237 -0
- package/dist/lib/node-esm/chunk-4OCRARHP.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-CP5LXTK5.mjs +16 -0
- package/dist/lib/node-esm/chunk-CP5LXTK5.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-SLTZMY34.mjs → chunk-P2V2Y5LV.mjs} +7 -4
- package/dist/lib/node-esm/chunk-P2V2Y5LV.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-UNQFIERE.mjs +41 -0
- package/dist/lib/node-esm/chunk-UNQFIERE.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-YIWRJ25J.mjs +71 -0
- package/dist/lib/node-esm/chunk-YIWRJ25J.mjs.map +7 -0
- package/dist/lib/node-esm/compiler-XTXFGZJM.mjs +83 -0
- package/dist/lib/node-esm/compiler-XTXFGZJM.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +129 -288
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/intent-resolver-KLBHSMW4.mjs +46 -0
- package/dist/lib/node-esm/intent-resolver-KLBHSMW4.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/react-surface-OLH7N3DK.mjs +66 -0
- package/dist/lib/node-esm/react-surface-OLH7N3DK.mjs.map +7 -0
- package/dist/lib/node-esm/settings-GR4O2WHZ.mjs +25 -0
- package/dist/lib/node-esm/settings-GR4O2WHZ.mjs.map +7 -0
- package/dist/lib/node-esm/types.mjs +6 -4
- package/dist/types/src/ScriptPlugin.d.ts +1 -3
- package/dist/types/src/ScriptPlugin.d.ts.map +1 -1
- package/dist/types/src/capabilities/artifact-definition.d.ts +11 -0
- package/dist/types/src/capabilities/artifact-definition.d.ts.map +1 -0
- package/dist/types/src/capabilities/capabilities.d.ts +5 -0
- package/dist/types/src/capabilities/capabilities.d.ts.map +1 -0
- package/dist/types/src/capabilities/compiler.d.ts +4 -0
- package/dist/types/src/capabilities/compiler.d.ts.map +1 -0
- package/dist/types/src/capabilities/index.d.ts +7 -0
- package/dist/types/src/capabilities/index.d.ts.map +1 -0
- package/dist/types/src/capabilities/intent-resolver.d.ts +4 -0
- package/dist/types/src/capabilities/intent-resolver.d.ts.map +1 -0
- package/dist/types/src/capabilities/react-surface.d.ts +4 -0
- package/dist/types/src/capabilities/react-surface.d.ts.map +1 -0
- package/dist/types/src/capabilities/settings.d.ts +4 -0
- package/dist/types/src/capabilities/settings.d.ts.map +1 -0
- package/dist/types/src/components/AutomationPanel/AutomationPanel.d.ts +1 -2
- package/dist/types/src/components/AutomationPanel/AutomationPanel.d.ts.map +1 -1
- package/dist/types/src/components/DebugPanel/DebugPanel.d.ts +1 -2
- package/dist/types/src/components/DebugPanel/DebugPanel.d.ts.map +1 -1
- package/dist/types/src/components/FrameContainer/FrameContainer.d.ts +3 -4
- package/dist/types/src/components/FrameContainer/FrameContainer.d.ts.map +1 -1
- package/dist/types/src/components/ScriptContainer.d.ts +10 -3
- package/dist/types/src/components/ScriptContainer.d.ts.map +1 -1
- package/dist/types/src/components/ScriptSettings/ScriptSettings.d.ts +1 -2
- package/dist/types/src/components/ScriptSettings/ScriptSettings.d.ts.map +1 -1
- package/dist/types/src/components/ScriptSettingsPanel.d.ts +1 -2
- package/dist/types/src/components/ScriptSettingsPanel.d.ts.map +1 -1
- package/dist/types/src/components/ScriptToolbar/ScriptToolbar.d.ts +10 -0
- package/dist/types/src/components/ScriptToolbar/ScriptToolbar.d.ts.map +1 -0
- package/dist/types/src/components/{Toolbar/Toolbar.stories.d.ts → ScriptToolbar/ScriptToolbar.stories.d.ts} +1 -1
- package/dist/types/src/components/ScriptToolbar/ScriptToolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/ScriptToolbar/index.d.ts +2 -0
- package/dist/types/src/components/ScriptToolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/TypescriptEditor/TypescriptEditor.d.ts +4 -3
- package/dist/types/src/components/TypescriptEditor/TypescriptEditor.d.ts.map +1 -1
- package/dist/types/src/components/TypescriptEditor/TypescriptEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +2 -2
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/events.d.ts +4 -0
- package/dist/types/src/events.d.ts.map +1 -0
- package/dist/types/src/hooks/deploy.d.ts +48 -0
- package/dist/types/src/hooks/deploy.d.ts.map +1 -0
- package/dist/types/src/hooks/format.d.ts +25 -0
- package/dist/types/src/hooks/format.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +7 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/template.d.ts +31 -0
- package/dist/types/src/hooks/template.d.ts.map +1 -0
- package/dist/types/src/hooks/useToolbarAction.d.ts +14 -0
- package/dist/types/src/hooks/useToolbarAction.d.ts.map +1 -0
- package/dist/types/src/hooks/useToolbarState.d.ts +10 -0
- package/dist/types/src/hooks/useToolbarState.d.ts.map +1 -0
- package/dist/types/src/hooks/view.d.ts +33 -0
- package/dist/types/src/hooks/view.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts +2 -2
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/src/templates/chess.d.ts +1 -4
- package/dist/types/src/templates/chess.d.ts.map +1 -1
- package/dist/types/src/templates/discord.d.ts +3 -0
- package/dist/types/src/templates/discord.d.ts.map +1 -0
- package/dist/types/src/templates/echo.d.ts.map +1 -1
- package/dist/types/src/templates/email.d.ts +2 -1
- package/dist/types/src/templates/email.d.ts.map +1 -1
- package/dist/types/src/templates/forex-effect.d.ts +3 -0
- package/dist/types/src/templates/forex-effect.d.ts.map +1 -0
- package/dist/types/src/templates/forex.d.ts +1 -4
- package/dist/types/src/templates/forex.d.ts.map +1 -1
- package/dist/types/src/{templates.d.ts → templates/index.d.ts} +1 -1
- package/dist/types/src/templates/index.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +97 -0
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +14 -6
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +45 -46
- package/src/ScriptPlugin.tsx +70 -90
- package/src/capabilities/artifact-definition.ts +219 -0
- package/src/capabilities/capabilities.ts +12 -0
- package/src/capabilities/compiler.ts +33 -0
- package/src/capabilities/index.ts +13 -0
- package/src/capabilities/intent-resolver.ts +46 -0
- package/src/capabilities/react-surface.tsx +54 -0
- package/src/capabilities/settings.ts +14 -0
- package/src/compiler/compiler.browser.test.ts +1 -1
- package/src/components/DebugPanel/DebugPanel.tsx +2 -2
- package/src/components/FrameContainer/FrameContainer.tsx +5 -4
- package/src/components/ScriptContainer.tsx +69 -9
- package/src/components/ScriptSettings/ScriptSettings.tsx +28 -4
- package/src/components/ScriptSettingsPanel.tsx +139 -51
- package/src/components/{Toolbar/Toolbar.stories.tsx → ScriptToolbar/ScriptToolbar.stories.tsx} +2 -2
- package/src/components/ScriptToolbar/ScriptToolbar.tsx +53 -0
- package/src/{bundler → components/ScriptToolbar}/index.ts +1 -1
- package/src/components/TypescriptEditor/TypescriptEditor.stories.tsx +2 -0
- package/src/components/TypescriptEditor/TypescriptEditor.tsx +75 -10
- package/src/events.ts +11 -0
- package/src/hooks/deploy.ts +169 -0
- package/src/hooks/format.ts +47 -0
- package/src/hooks/index.ts +11 -0
- package/src/hooks/template.ts +64 -0
- package/src/hooks/useToolbarAction.ts +54 -0
- package/src/hooks/useToolbarState.ts +16 -0
- package/src/hooks/view.ts +52 -0
- package/src/index.ts +2 -7
- package/src/meta.ts +4 -2
- package/src/templates/chat.ts +4 -4
- package/src/templates/chess.ts +42 -35
- package/src/templates/discord.ts +103 -0
- package/src/templates/echo.ts +5 -1
- package/src/templates/email.ts +26 -23
- package/src/templates/forex-effect.ts +41 -0
- package/src/templates/forex.ts +23 -16
- package/src/{templates.ts → templates/index.ts} +20 -5
- package/src/translations.ts +23 -0
- package/src/types.ts +19 -18
- package/dist/lib/browser/ScriptContainer-GDVBWFI7.mjs +0 -415
- package/dist/lib/browser/ScriptContainer-GDVBWFI7.mjs.map +0 -7
- package/dist/lib/browser/chunk-4UTRJLQ3.mjs +0 -286
- package/dist/lib/browser/chunk-4UTRJLQ3.mjs.map +0 -7
- package/dist/lib/browser/chunk-C7VBULMK.mjs +0 -18
- package/dist/lib/browser/chunk-C7VBULMK.mjs.map +0 -7
- package/dist/lib/browser/chunk-K7KY6YMG.mjs.map +0 -7
- package/dist/lib/browser/chunk-RQA3EB5K.mjs +0 -26
- package/dist/lib/browser/chunk-RQA3EB5K.mjs.map +0 -7
- package/dist/lib/browser/meta.mjs +0 -9
- package/dist/lib/browser/meta.mjs.map +0 -7
- package/dist/lib/node/ScriptContainer-YWXXNLG6.cjs +0 -426
- package/dist/lib/node/ScriptContainer-YWXXNLG6.cjs.map +0 -7
- package/dist/lib/node/chunk-4INUJLXL.cjs.map +0 -7
- package/dist/lib/node/chunk-6LYX73ZT.cjs.map +0 -7
- package/dist/lib/node/chunk-PK3OQP5L.cjs.map +0 -7
- package/dist/lib/node/chunk-WPP65EYS.cjs +0 -298
- package/dist/lib/node/chunk-WPP65EYS.cjs.map +0 -7
- package/dist/lib/node/meta.cjs.map +0 -7
- package/dist/lib/node-esm/ScriptContainer-BG2PHKEH.mjs +0 -416
- package/dist/lib/node-esm/ScriptContainer-BG2PHKEH.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-4SRPREUK.mjs +0 -288
- package/dist/lib/node-esm/chunk-4SRPREUK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-H6BBLQ5Q.mjs +0 -27
- package/dist/lib/node-esm/chunk-H6BBLQ5Q.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-OFYGBUNI.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-SLTZMY34.mjs.map +0 -7
- package/dist/lib/node-esm/meta.mjs +0 -10
- package/dist/lib/node-esm/meta.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -39
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts +0 -12
- package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts.map +0 -1
- package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts +0 -6
- package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts.map +0 -1
- package/dist/types/src/components/ScriptBlock/index.d.ts +0 -2
- package/dist/types/src/components/ScriptBlock/index.d.ts.map +0 -1
- package/dist/types/src/components/ScriptEditor.d.ts +0 -9
- package/dist/types/src/components/ScriptEditor.d.ts.map +0 -1
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +0 -23
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +0 -1
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +0 -1
- package/dist/types/src/components/Toolbar/index.d.ts +0 -2
- package/dist/types/src/components/Toolbar/index.d.ts.map +0 -1
- package/dist/types/src/templates.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -54
- package/src/bundler/bundler.ts +0 -250
- package/src/components/ScriptBlock/ScriptBlock.stories.tsx +0 -55
- package/src/components/ScriptBlock/ScriptBlock.tsx +0 -102
- package/src/components/ScriptBlock/index.ts +0 -5
- package/src/components/ScriptEditor.tsx +0 -193
- package/src/components/Toolbar/Toolbar.tsx +0 -177
- package/src/components/Toolbar/index.ts +0 -5
- /package/dist/lib/browser/{AutomationPanel-RFQAQ7NF.mjs.map → AutomationPanel-6HFRNRWV.mjs.map} +0 -0
- /package/dist/lib/node/{AutomationPanel-5CJEF4MI.cjs.map → AutomationPanel-RBQ4DODN.cjs.map} +0 -0
- /package/dist/lib/node-esm/{AutomationPanel-7Y6REBNJ.mjs.map → AutomationPanel-WTVVVMRH.mjs.map} +0 -0
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2022 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
import wasmUrl from 'esbuild-wasm/esbuild.wasm?url';
|
|
7
|
-
import { beforeAll, describe, expect, test } from 'vitest';
|
|
8
|
-
|
|
9
|
-
import { isNode } from '@dxos/util';
|
|
10
|
-
|
|
11
|
-
import { Bundler, initializeBundler } from './bundler';
|
|
12
|
-
|
|
13
|
-
describe('Bundler', () => {
|
|
14
|
-
beforeAll(async () => {
|
|
15
|
-
if (!isNode()) {
|
|
16
|
-
await initializeBundler({ wasmUrl });
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('Basic', async () => {
|
|
21
|
-
const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
|
|
22
|
-
const result = await bundler.bundle('const x = 100'); // TODO(burdon): Test import.
|
|
23
|
-
expect(result.bundle).to.exist;
|
|
24
|
-
expect(result.error).to.not.exist;
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('Import', async () => {
|
|
28
|
-
const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
|
|
29
|
-
const result = await bundler.bundle(`
|
|
30
|
-
import { Filter } from './runtime.js';
|
|
31
|
-
|
|
32
|
-
const query = Filter.typename('dxos.org/type/Example');
|
|
33
|
-
`);
|
|
34
|
-
expect(result.bundle).to.exist;
|
|
35
|
-
expect(result.error).to.not.exist;
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('HTTPS Import', async () => {
|
|
39
|
-
const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
|
|
40
|
-
const result = await bundler.bundle(`
|
|
41
|
-
import { Octokit } from 'https://esm.sh/octokit';
|
|
42
|
-
const octokit = new Octokit();
|
|
43
|
-
`);
|
|
44
|
-
expect(result.bundle).to.exist;
|
|
45
|
-
expect(result.error).to.not.exist;
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test('Error', async () => {
|
|
49
|
-
const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
|
|
50
|
-
const result = await bundler.bundle("import missing from './module'; missing();");
|
|
51
|
-
expect(result.bundle).to.not.exist;
|
|
52
|
-
expect(result.error).to.exist;
|
|
53
|
-
});
|
|
54
|
-
});
|
package/src/bundler/bundler.ts
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type BuildOptions } from 'esbuild';
|
|
6
|
-
import { build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';
|
|
7
|
-
|
|
8
|
-
import { subtleCrypto } from '@dxos/crypto';
|
|
9
|
-
import { invariant } from '@dxos/invariant';
|
|
10
|
-
import { log } from '@dxos/log';
|
|
11
|
-
|
|
12
|
-
export type Import = {
|
|
13
|
-
moduleUrl: string;
|
|
14
|
-
defaultImport: boolean;
|
|
15
|
-
namedImports: string[];
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type BundlerResult = {
|
|
19
|
-
timestamp: number;
|
|
20
|
-
sourceHash: Buffer;
|
|
21
|
-
imports?: Import[];
|
|
22
|
-
bundle?: string;
|
|
23
|
-
error?: any;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type BundlerOptions = {
|
|
27
|
-
platform: BuildOptions['platform'];
|
|
28
|
-
sandboxedModules: string[];
|
|
29
|
-
remoteModules: Record<string, string>;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
let initialized: Promise<void>;
|
|
33
|
-
export const initializeBundler = async (options: { wasmUrl: string }) => {
|
|
34
|
-
await (initialized ??= initialize({
|
|
35
|
-
wasmURL: options.wasmUrl,
|
|
36
|
-
}));
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* ESBuild bundler.
|
|
41
|
-
*/
|
|
42
|
-
export class Bundler {
|
|
43
|
-
constructor(private readonly _options: BundlerOptions) {}
|
|
44
|
-
|
|
45
|
-
async bundle(source: string): Promise<BundlerResult> {
|
|
46
|
-
const { sandboxedModules: providedModules, ...options } = this._options;
|
|
47
|
-
|
|
48
|
-
const createResult = async (result?: Partial<BundlerResult>) => {
|
|
49
|
-
return {
|
|
50
|
-
timestamp: Date.now(),
|
|
51
|
-
sourceHash: Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))),
|
|
52
|
-
...result,
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
if (this._options.platform === 'browser') {
|
|
57
|
-
invariant(initialized, 'Compiler not initialized.');
|
|
58
|
-
await initialized;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const imports = analyzeSourceFileImports(source);
|
|
62
|
-
|
|
63
|
-
// https://esbuild.github.io/api/#build
|
|
64
|
-
try {
|
|
65
|
-
const result = await build({
|
|
66
|
-
platform: options.platform,
|
|
67
|
-
conditions: ['workerd', 'browser'],
|
|
68
|
-
metafile: true,
|
|
69
|
-
write: false,
|
|
70
|
-
entryPoints: ['memory:main.tsx'],
|
|
71
|
-
bundle: true,
|
|
72
|
-
format: 'esm',
|
|
73
|
-
plugins: [
|
|
74
|
-
{
|
|
75
|
-
name: 'memory',
|
|
76
|
-
setup: (build) => {
|
|
77
|
-
build.onResolve({ filter: /^\.\/runtime\.js$/ }, ({ path }) => {
|
|
78
|
-
return { path, external: true };
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
build.onResolve({ filter: /^dxos:functions$/ }, ({ path }) => {
|
|
82
|
-
return { path: './runtime.js', external: true };
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
build.onResolve({ filter: /^memory:/ }, ({ path }) => {
|
|
86
|
-
return { path: path.split(':')[1], namespace: 'memory' };
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
build.onLoad({ filter: /.*/, namespace: 'memory' }, ({ path }) => {
|
|
90
|
-
if (path === 'main.tsx') {
|
|
91
|
-
return {
|
|
92
|
-
contents: source,
|
|
93
|
-
loader: 'tsx',
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
for (const module of providedModules) {
|
|
99
|
-
build.onResolve({ filter: new RegExp(`^${module}$`) }, ({ path }) => {
|
|
100
|
-
return { path, namespace: 'injected-module' };
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
build.onLoad({ filter: /.*/, namespace: 'injected-module' }, ({ path }) => {
|
|
105
|
-
const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];
|
|
106
|
-
return {
|
|
107
|
-
contents: `
|
|
108
|
-
const { ${namedImports.join(',')} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];
|
|
109
|
-
export { ${namedImports.join(',')} };
|
|
110
|
-
export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;
|
|
111
|
-
`,
|
|
112
|
-
loader: 'tsx',
|
|
113
|
-
};
|
|
114
|
-
});
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
httpPlugin,
|
|
118
|
-
],
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
log('compile complete', result.metafile);
|
|
122
|
-
|
|
123
|
-
return await createResult({
|
|
124
|
-
imports: this.analyzeImports(result),
|
|
125
|
-
bundle: result.outputFiles![0].text,
|
|
126
|
-
});
|
|
127
|
-
} catch (err) {
|
|
128
|
-
return await createResult({ error: err });
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.
|
|
133
|
-
analyzeImports(result: BuildResult): Import[] {
|
|
134
|
-
invariant(result.outputFiles);
|
|
135
|
-
|
|
136
|
-
// TODO(dmaretskyi): Support import aliases and wildcard imports.
|
|
137
|
-
const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
|
|
138
|
-
return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {
|
|
139
|
-
const namedImports: string[] = [];
|
|
140
|
-
|
|
141
|
-
const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
|
|
142
|
-
if (parsedImport?.[2]) {
|
|
143
|
-
NAMED_IMPORTS_REGEX.lastIndex = 0;
|
|
144
|
-
const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);
|
|
145
|
-
if (namedImportsMatch) {
|
|
146
|
-
namedImportsMatch[1].split(',').forEach((importName) => {
|
|
147
|
-
namedImports.push(importName.trim());
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return {
|
|
153
|
-
moduleUrl: entry.path,
|
|
154
|
-
defaultImport: !!parsedImport?.[1],
|
|
155
|
-
namedImports,
|
|
156
|
-
};
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
analyzeSourceFileImports(code: string) {
|
|
161
|
-
// TODO(dmaretskyi): Support import aliases and wildcard imports.
|
|
162
|
-
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
163
|
-
return parsedImports.map((capture) => {
|
|
164
|
-
return {
|
|
165
|
-
defaultImportName: capture[1],
|
|
166
|
-
namedImports: capture[2]?.split(',').map((importName) => importName.trim()),
|
|
167
|
-
wildcardImportName: capture[3],
|
|
168
|
-
moduleIdentifier: capture[4],
|
|
169
|
-
quotes: capture[5],
|
|
170
|
-
};
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// https://regex101.com/r/FEN5ks/1
|
|
176
|
-
// https://stackoverflow.com/a/73265022
|
|
177
|
-
// $1 = default import name (can be non-existent)
|
|
178
|
-
// $2 = destructured exports (can be non-existent)
|
|
179
|
-
// $3 = wildcard import name (can be non-existent)
|
|
180
|
-
// $4 = module identifier
|
|
181
|
-
// $5 = quotes used (either ' or ")
|
|
182
|
-
const IMPORT_REGEX =
|
|
183
|
-
/import(?:(?:(?:[ \n\t]+([^ *\n\t{},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*{(?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t{}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/gm;
|
|
184
|
-
|
|
185
|
-
const NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
|
|
186
|
-
|
|
187
|
-
const allMatches = (regex: RegExp, str: string) => {
|
|
188
|
-
regex.lastIndex = 0;
|
|
189
|
-
|
|
190
|
-
let match;
|
|
191
|
-
const matches = [];
|
|
192
|
-
while ((match = regex.exec(str))) {
|
|
193
|
-
matches.push(match);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return matches;
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
type ParsedImport = {
|
|
200
|
-
defaultImportName?: string;
|
|
201
|
-
namedImports: string[];
|
|
202
|
-
wildcardImportName?: string;
|
|
203
|
-
moduleIdentifier: string;
|
|
204
|
-
quotes: string;
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
const analyzeSourceFileImports = (code: string): ParsedImport[] => {
|
|
208
|
-
// TODO(dmaretskyi): Support import aliases and wildcard imports.
|
|
209
|
-
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
210
|
-
return parsedImports.map((capture) => {
|
|
211
|
-
return {
|
|
212
|
-
defaultImportName: capture[1],
|
|
213
|
-
namedImports: capture[2]
|
|
214
|
-
?.trim()
|
|
215
|
-
.slice(1, -1)
|
|
216
|
-
.split(',')
|
|
217
|
-
.map((importName) => importName.trim()),
|
|
218
|
-
wildcardImportName: capture[3],
|
|
219
|
-
moduleIdentifier: capture[4],
|
|
220
|
-
quotes: capture[5],
|
|
221
|
-
};
|
|
222
|
-
});
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
const httpPlugin: Plugin = {
|
|
226
|
-
name: 'http',
|
|
227
|
-
setup: (build) => {
|
|
228
|
-
// Intercept import paths starting with "http:" and "https:" so esbuild doesn't attempt to map them to a file system location.
|
|
229
|
-
// Tag them with the "http-url" namespace to associate them with this plugin.
|
|
230
|
-
build.onResolve({ filter: /^https?:\/\// }, (args) => ({
|
|
231
|
-
path: args.path,
|
|
232
|
-
namespace: 'http-url',
|
|
233
|
-
}));
|
|
234
|
-
|
|
235
|
-
// We also want to intercept all import paths inside downloaded files and resolve them against the original URL.
|
|
236
|
-
// All of these files will be in the "http-url" namespace.
|
|
237
|
-
// Make sure to keep the newly resolved URL in the "http-url" namespace so imports inside it will also be resolved as URLs recursively.
|
|
238
|
-
build.onResolve({ filter: /.*/, namespace: 'http-url' }, (args) => ({
|
|
239
|
-
path: new URL(args.path, args.importer).toString(),
|
|
240
|
-
namespace: 'http-url',
|
|
241
|
-
}));
|
|
242
|
-
|
|
243
|
-
// When a URL is loaded, we want to actually download the content from the internet.
|
|
244
|
-
// This has just enough logic to be able to handle the example import from unpkg.com but in reality this would probably need to be more complex.
|
|
245
|
-
build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {
|
|
246
|
-
const response = await fetch(args.path);
|
|
247
|
-
return { contents: await response.text(), loader: 'jsx' };
|
|
248
|
-
});
|
|
249
|
-
},
|
|
250
|
-
};
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxos-theme';
|
|
6
|
-
|
|
7
|
-
import { type Meta } from '@storybook/react';
|
|
8
|
-
import React, { useMemo } from 'react';
|
|
9
|
-
|
|
10
|
-
import { ScriptType } from '@dxos/functions';
|
|
11
|
-
import { TextType } from '@dxos/plugin-markdown/types';
|
|
12
|
-
import { create, createObject, makeRef } from '@dxos/react-client/echo';
|
|
13
|
-
import { withClientProvider } from '@dxos/react-client/testing';
|
|
14
|
-
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
15
|
-
|
|
16
|
-
import { ScriptBlock } from './ScriptBlock';
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
import mainUrl from '../FrameContainer/frame?url';
|
|
19
|
-
|
|
20
|
-
const typename = 'example.com/type/contact';
|
|
21
|
-
|
|
22
|
-
const code = [
|
|
23
|
-
"import { Filter, useQuery, useSpaces} from '@dxos/react-client/echo';",
|
|
24
|
-
"import { Chart, Explorer, Globe } from '@dxos/plugin-explorer';",
|
|
25
|
-
'',
|
|
26
|
-
'export default () => {',
|
|
27
|
-
' const spaces = useSpaces();',
|
|
28
|
-
' const space = spaces[1]',
|
|
29
|
-
` const objects = useQuery(space, Filter.typename('${typename}'));`,
|
|
30
|
-
' return <Chart items={objects} accessor={object => ({ x: object.lat, y: object.lng })} />',
|
|
31
|
-
'}',
|
|
32
|
-
].join('\n');
|
|
33
|
-
|
|
34
|
-
const DefaultStory = () => {
|
|
35
|
-
// TODO(burdon): Review what's the right way to create automerge-backed objects.
|
|
36
|
-
const object = useMemo(
|
|
37
|
-
() => createObject(create(ScriptType, { source: makeRef(create(TextType, { content: code })) })),
|
|
38
|
-
[],
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
// TODO(dmaretskyi): Not sure how to provide `containerUrl` here since the html now lives in composer-app.
|
|
42
|
-
// TODO(burdon): Normalize html/frame.tsx with composer-app to test locally.
|
|
43
|
-
return <ScriptBlock script={object} containerUrl={mainUrl} />;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export const Default = {};
|
|
47
|
-
|
|
48
|
-
const meta: Meta = {
|
|
49
|
-
title: 'plugins/plugin-script/ScriptBlock',
|
|
50
|
-
component: ScriptBlock,
|
|
51
|
-
render: DefaultStory,
|
|
52
|
-
decorators: [withClientProvider({ createSpace: true }), withTheme, withLayout({ fullscreen: true, tooltips: true })],
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export default meta;
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
import wasmUrl from 'esbuild-wasm/esbuild.wasm?url';
|
|
7
|
-
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
8
|
-
|
|
9
|
-
import { type ScriptType } from '@dxos/functions';
|
|
10
|
-
import { createDocAccessor, DocAccessor } from '@dxos/react-client/echo';
|
|
11
|
-
import { Toolbar, Button, Icon } from '@dxos/react-ui';
|
|
12
|
-
|
|
13
|
-
import { Bundler, type BundlerResult, initializeBundler } from '../../bundler';
|
|
14
|
-
import { FrameContainer } from '../FrameContainer';
|
|
15
|
-
import { ScriptEditor } from '../ScriptEditor';
|
|
16
|
-
|
|
17
|
-
// Keep in sync with packages/apps/composer-app/script-frame/main.tsx .
|
|
18
|
-
const PROVIDED_MODULES = [
|
|
19
|
-
'react',
|
|
20
|
-
'react-dom/client',
|
|
21
|
-
'@dxos/client',
|
|
22
|
-
'@dxos/react-client',
|
|
23
|
-
'@dxos/react-client/echo',
|
|
24
|
-
'@dxos/plugin-explorer',
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
export type ScriptBlockProps = {
|
|
28
|
-
script: ScriptType;
|
|
29
|
-
hideSelector?: boolean;
|
|
30
|
-
|
|
31
|
-
// Url to the page used to host the script in the iframe.
|
|
32
|
-
containerUrl: string;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated
|
|
37
|
-
*/
|
|
38
|
-
// TODO(burdon): Cache compiled results in context.
|
|
39
|
-
export const ScriptBlock = ({ script, hideSelector, containerUrl }: ScriptBlockProps) => {
|
|
40
|
-
const source = useMemo(() => script.source && createDocAccessor(script.source.target!, ['content']), [script.source]);
|
|
41
|
-
const [result, setResult] = useState<BundlerResult>();
|
|
42
|
-
const bundler = useMemo(
|
|
43
|
-
() => new Bundler({ platform: 'browser', sandboxedModules: PROVIDED_MODULES, remoteModules: {} }),
|
|
44
|
-
[],
|
|
45
|
-
);
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
// TODO(burdon): Create useCompiler hook (with initialization).
|
|
48
|
-
void initializeBundler({ wasmUrl });
|
|
49
|
-
}, []);
|
|
50
|
-
useEffect(() => {
|
|
51
|
-
// TODO(burdon): Throttle and listen for update.
|
|
52
|
-
const t = setTimeout(async () => {
|
|
53
|
-
if (!source) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const result = await bundler.bundle(DocAccessor.getValue(source));
|
|
58
|
-
setResult(result);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
return () => clearTimeout(t);
|
|
62
|
-
}, [source]);
|
|
63
|
-
|
|
64
|
-
const handleExec = useCallback(
|
|
65
|
-
async (auto = true) => {
|
|
66
|
-
if (!source) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const result = await bundler.bundle(DocAccessor.getValue(source));
|
|
70
|
-
setResult(result);
|
|
71
|
-
},
|
|
72
|
-
[source],
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
return (
|
|
76
|
-
<div className='flex flex-col grow overflow-hidden'>
|
|
77
|
-
{!hideSelector && (
|
|
78
|
-
<Toolbar.Root>
|
|
79
|
-
<div className='grow' />
|
|
80
|
-
{result?.bundle && !result?.error && (
|
|
81
|
-
<div title={String(result.error)}>
|
|
82
|
-
<Icon icon='ph--check--regular' size={5} classNames='text-green-500' />
|
|
83
|
-
</div>
|
|
84
|
-
)}
|
|
85
|
-
{result?.error && (
|
|
86
|
-
<div title={String(result.error)}>
|
|
87
|
-
<Icon icon='ph--warning--regular' size={5} classNames='text-orange-500' />
|
|
88
|
-
</div>
|
|
89
|
-
)}
|
|
90
|
-
<Button variant='ghost' onClick={() => handleExec()}>
|
|
91
|
-
<Icon icon='ph--play--regular' size={5} />
|
|
92
|
-
</Button>
|
|
93
|
-
</Toolbar.Root>
|
|
94
|
-
)}
|
|
95
|
-
|
|
96
|
-
<div className='flex'>
|
|
97
|
-
<ScriptEditor script={script} />
|
|
98
|
-
{result && <FrameContainer key={script.id} result={result} containerUrl={containerUrl} />}
|
|
99
|
-
</div>
|
|
100
|
-
</div>
|
|
101
|
-
);
|
|
102
|
-
};
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { format } from 'prettier';
|
|
6
|
-
import prettierPluginEstree from 'prettier/plugins/estree';
|
|
7
|
-
import prettierPluginTypescript from 'prettier/plugins/typescript';
|
|
8
|
-
import React, { useCallback, useMemo, useState } from 'react';
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
FUNCTIONS_PRESET_META_KEY,
|
|
12
|
-
FunctionType,
|
|
13
|
-
type ScriptType,
|
|
14
|
-
getInvocationUrl,
|
|
15
|
-
getUserFunctionUrlInMetadata,
|
|
16
|
-
setUserFunctionUrlInMetadata,
|
|
17
|
-
uploadWorkerFunction,
|
|
18
|
-
incrementSemverPatch,
|
|
19
|
-
} from '@dxos/functions';
|
|
20
|
-
import { log } from '@dxos/log';
|
|
21
|
-
import { useClient } from '@dxos/react-client';
|
|
22
|
-
import { create, createDocAccessor, Filter, getMeta, getSpace, makeRef, useQuery } from '@dxos/react-client/echo';
|
|
23
|
-
import { useIdentity } from '@dxos/react-client/halo';
|
|
24
|
-
import { useTranslation, type ThemedClassName } from '@dxos/react-ui';
|
|
25
|
-
import { createDataExtensions, listener } from '@dxos/react-ui-editor';
|
|
26
|
-
import { mx } from '@dxos/react-ui-theme';
|
|
27
|
-
|
|
28
|
-
import { DebugPanel } from './DebugPanel';
|
|
29
|
-
import { type TemplateSelectProps, Toolbar, type ViewType } from './Toolbar';
|
|
30
|
-
import { TypescriptEditor, type TypescriptEditorProps } from './TypescriptEditor';
|
|
31
|
-
import { Bundler } from '../bundler';
|
|
32
|
-
import { SCRIPT_PLUGIN } from '../meta';
|
|
33
|
-
import { templates } from '../templates';
|
|
34
|
-
|
|
35
|
-
export type ScriptEditorProps = ThemedClassName<{
|
|
36
|
-
script: ScriptType;
|
|
37
|
-
}> &
|
|
38
|
-
Pick<TypescriptEditorProps, 'env'>;
|
|
39
|
-
|
|
40
|
-
export const ScriptEditor = ({ classNames, script, env }: ScriptEditorProps) => {
|
|
41
|
-
const { t } = useTranslation(SCRIPT_PLUGIN);
|
|
42
|
-
const client = useClient();
|
|
43
|
-
const identity = useIdentity();
|
|
44
|
-
const space = getSpace(script);
|
|
45
|
-
const [fn] = useQuery(space, Filter.schema(FunctionType, { source: script }));
|
|
46
|
-
|
|
47
|
-
const extensions = useMemo(
|
|
48
|
-
() => [
|
|
49
|
-
listener({
|
|
50
|
-
onChange: (text) => {
|
|
51
|
-
if (script.source.target && script.source.target.content !== text) {
|
|
52
|
-
script.changed = true;
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
}),
|
|
56
|
-
createDataExtensions({
|
|
57
|
-
id: script.id,
|
|
58
|
-
text: script.source.target && createDocAccessor(script.source.target, ['content']),
|
|
59
|
-
space,
|
|
60
|
-
identity,
|
|
61
|
-
}),
|
|
62
|
-
],
|
|
63
|
-
[script, script.source.target, space, identity],
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
const [view, setView] = useState<ViewType>('editor');
|
|
67
|
-
const existingFunctionUrl = fn && getUserFunctionUrlInMetadata(getMeta(fn));
|
|
68
|
-
const [error, setError] = useState<string>();
|
|
69
|
-
|
|
70
|
-
const handleFormat = useCallback(async () => {
|
|
71
|
-
if (!script.source) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
script.source.target!.content = await format(script.source.target!.content, {
|
|
77
|
-
parser: 'typescript',
|
|
78
|
-
plugins: [prettierPluginEstree, prettierPluginTypescript],
|
|
79
|
-
semi: true,
|
|
80
|
-
singleQuote: true,
|
|
81
|
-
});
|
|
82
|
-
} catch (err: any) {
|
|
83
|
-
// TODO(wittjosiah): Show error in UI.
|
|
84
|
-
log.catch(err);
|
|
85
|
-
}
|
|
86
|
-
}, [script.source]);
|
|
87
|
-
|
|
88
|
-
const handleTemplateChange: TemplateSelectProps['onTemplateSelect'] = (id) => {
|
|
89
|
-
const template = templates.find((template) => template.id === id);
|
|
90
|
-
if (template) {
|
|
91
|
-
script.name = template.name;
|
|
92
|
-
script.source!.target!.content = template.source;
|
|
93
|
-
const metaKeys = getMeta(script).keys;
|
|
94
|
-
const oldPresetIndex = metaKeys.findIndex((key) => key.source === FUNCTIONS_PRESET_META_KEY);
|
|
95
|
-
if (oldPresetIndex >= 0) {
|
|
96
|
-
metaKeys.splice(oldPresetIndex, 1);
|
|
97
|
-
}
|
|
98
|
-
if (template.presetId) {
|
|
99
|
-
metaKeys.push({ source: FUNCTIONS_PRESET_META_KEY, id: template.presetId });
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const handleDeploy = useCallback(async () => {
|
|
105
|
-
if (!script.source || !identity || !space) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
setError(undefined);
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
const existingFunctionId = existingFunctionUrl?.split('/').at(-1);
|
|
113
|
-
|
|
114
|
-
const bundler = new Bundler({ platform: 'browser', sandboxedModules: [], remoteModules: {} });
|
|
115
|
-
const buildResult = await bundler.bundle(script.source.target!.content);
|
|
116
|
-
if (buildResult.error || !buildResult.bundle) {
|
|
117
|
-
throw buildResult.error;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const { functionId, version, meta } = await uploadWorkerFunction({
|
|
121
|
-
client,
|
|
122
|
-
spaceId: space.id,
|
|
123
|
-
version: fn ? incrementSemverPatch(fn.version) : '0.0.1',
|
|
124
|
-
functionId: existingFunctionId,
|
|
125
|
-
source: buildResult.bundle,
|
|
126
|
-
});
|
|
127
|
-
if (functionId === undefined || version === undefined) {
|
|
128
|
-
throw new Error(`Upload didn't return expected data: functionId=${functionId}, version=${version}`);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
log.info('function uploaded', { functionId, version });
|
|
132
|
-
if (fn) {
|
|
133
|
-
fn.version = version;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const deployedFunction =
|
|
137
|
-
fn ?? space.db.add(create(FunctionType, { name: functionId, version, source: makeRef(script) }));
|
|
138
|
-
|
|
139
|
-
script.changed = false;
|
|
140
|
-
|
|
141
|
-
if (meta.inputSchema) {
|
|
142
|
-
deployedFunction.inputSchema = meta.inputSchema;
|
|
143
|
-
} else {
|
|
144
|
-
log.verbose('no input schema in function metadata', { functionId });
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
setUserFunctionUrlInMetadata(getMeta(deployedFunction), `/${space.id}/${functionId}`);
|
|
148
|
-
|
|
149
|
-
setView('split');
|
|
150
|
-
} catch (err: any) {
|
|
151
|
-
log.catch(err);
|
|
152
|
-
setError(t('upload failed label'));
|
|
153
|
-
}
|
|
154
|
-
}, [fn, existingFunctionUrl, script, script.name, script.source, space?.id ?? null]);
|
|
155
|
-
|
|
156
|
-
const functionUrl = useMemo(() => {
|
|
157
|
-
if (!existingFunctionUrl) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return getInvocationUrl(existingFunctionUrl, client.config.values.runtime?.services?.edge?.url ?? '', {
|
|
162
|
-
spaceId: space?.id,
|
|
163
|
-
});
|
|
164
|
-
}, [existingFunctionUrl, space]);
|
|
165
|
-
|
|
166
|
-
return (
|
|
167
|
-
<div role='none' className={mx('flex flex-col w-full overflow-hidden divide-y divide-separator', classNames)}>
|
|
168
|
-
<Toolbar
|
|
169
|
-
deployed={Boolean(existingFunctionUrl) && !script.changed}
|
|
170
|
-
functionUrl={functionUrl}
|
|
171
|
-
error={error}
|
|
172
|
-
view={view}
|
|
173
|
-
templates={templates}
|
|
174
|
-
onDeploy={handleDeploy}
|
|
175
|
-
onFormat={handleFormat}
|
|
176
|
-
onViewChange={setView}
|
|
177
|
-
onTemplateSelect={handleTemplateChange}
|
|
178
|
-
/>
|
|
179
|
-
|
|
180
|
-
{view !== 'preview' && (
|
|
181
|
-
<TypescriptEditor
|
|
182
|
-
id={script.id}
|
|
183
|
-
env={env}
|
|
184
|
-
initialValue={script.source?.target?.content}
|
|
185
|
-
extensions={extensions}
|
|
186
|
-
className='flex is-full bs-full overflow-hidden ch-focus-ring-inset-over-all'
|
|
187
|
-
/>
|
|
188
|
-
)}
|
|
189
|
-
|
|
190
|
-
{view !== 'editor' && <DebugPanel functionUrl={functionUrl} />}
|
|
191
|
-
</div>
|
|
192
|
-
);
|
|
193
|
-
};
|