@dxos/functions 0.7.5-main.9d2a38b → 0.7.5-main.c41020f
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/bundler/index.mjs +239 -0
- package/dist/lib/browser/bundler/index.mjs.map +7 -0
- package/dist/lib/browser/{chunk-PWSDZCXB.mjs → chunk-6XYG2TNO.mjs} +53 -11
- package/dist/lib/browser/chunk-6XYG2TNO.mjs.map +7 -0
- package/dist/lib/browser/{chunk-IP657FIW.mjs → chunk-WI2RVE6E.mjs} +2 -2
- package/dist/lib/browser/{chunk-B3FGASHJ.mjs → chunk-YJEIETRB.mjs} +6 -3
- package/dist/lib/browser/{chunk-B3FGASHJ.mjs.map → chunk-YJEIETRB.mjs.map} +3 -3
- package/dist/lib/browser/edge/index.mjs +1 -1
- package/dist/lib/browser/index.mjs +32 -8
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +2 -2
- package/dist/lib/browser/types/index.mjs +9 -1
- package/dist/lib/node/bundler/index.cjs +261 -0
- package/dist/lib/node/bundler/index.cjs.map +7 -0
- package/dist/lib/node/{chunk-3HCN2EE6.cjs → chunk-HH4ZO3YY.cjs} +15 -15
- package/dist/lib/node/{chunk-ZDG466QT.cjs → chunk-MZMHE4DC.cjs} +9 -6
- package/dist/lib/node/{chunk-ZDG466QT.cjs.map → chunk-MZMHE4DC.cjs.map} +3 -3
- package/dist/lib/node/{chunk-2YCBLNMR.cjs → chunk-UNB65EWP.cjs} +56 -12
- package/dist/lib/node/chunk-UNB65EWP.cjs.map +7 -0
- package/dist/lib/node/edge/index.cjs +8 -8
- package/dist/lib/node/edge/index.cjs.map +1 -1
- package/dist/lib/node/index.cjs +52 -27
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +7 -7
- package/dist/lib/node/types/index.cjs +18 -10
- package/dist/lib/node/types/index.cjs.map +2 -2
- package/dist/lib/node-esm/bundler/index.mjs +239 -0
- package/dist/lib/node-esm/bundler/index.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-WEI4UCRY.mjs → chunk-A3BWJQVO.mjs} +53 -11
- package/dist/lib/node-esm/chunk-A3BWJQVO.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-EK2U3CEM.mjs → chunk-CM2LOG4A.mjs} +2 -2
- package/dist/lib/node-esm/{chunk-CN74WOTT.mjs → chunk-T2KBXTMR.mjs} +6 -3
- package/dist/lib/node-esm/{chunk-CN74WOTT.mjs.map → chunk-T2KBXTMR.mjs.map} +3 -3
- package/dist/lib/node-esm/edge/index.mjs +1 -1
- package/dist/lib/node-esm/index.mjs +32 -8
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +2 -2
- package/dist/lib/node-esm/types/index.mjs +9 -1
- package/dist/types/src/bundler/bundler.d.ts +51 -0
- package/dist/types/src/bundler/bundler.d.ts.map +1 -0
- package/dist/types/src/bundler/bundler.test.d.ts +2 -0
- package/dist/types/src/bundler/bundler.test.d.ts.map +1 -0
- package/dist/types/src/bundler/index.d.ts +2 -0
- package/dist/types/src/bundler/index.d.ts.map +1 -0
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +30 -4
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/testing/types.d.ts +2 -2
- package/dist/types/src/types/index.d.ts +1 -0
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/src/types/schema.d.ts +20 -16
- package/dist/types/src/types/schema.d.ts.map +1 -1
- package/dist/types/src/types/trace.d.ts +114 -0
- package/dist/types/src/types/trace.d.ts.map +1 -0
- package/dist/types/src/types/types.d.ts +33 -9
- package/dist/types/src/types/types.d.ts.map +1 -1
- package/package.json +33 -19
- package/src/bundler/bundler.test.ts +59 -0
- package/src/bundler/bundler.ts +264 -0
- package/src/bundler/index.ts +5 -0
- package/src/edge/functions.ts +5 -1
- package/src/handler.ts +59 -9
- package/src/trigger/trigger-registry.test.ts +3 -1
- package/src/types/index.ts +1 -0
- package/src/types/schema.ts +3 -1
- package/src/types/trace.ts +49 -0
- package/src/types/types.ts +11 -2
- package/dist/lib/browser/chunk-PWSDZCXB.mjs.map +0 -7
- package/dist/lib/node/chunk-2YCBLNMR.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-WEI4UCRY.mjs.map +0 -7
- /package/dist/lib/browser/{chunk-IP657FIW.mjs.map → chunk-WI2RVE6E.mjs.map} +0 -0
- /package/dist/lib/node/{chunk-3HCN2EE6.cjs.map → chunk-HH4ZO3YY.cjs.map} +0 -0
- /package/dist/lib/node-esm/{chunk-EK2U3CEM.mjs.map → chunk-CM2LOG4A.mjs.map} +0 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import "@dxos/node-std/globals";
|
|
2
|
+
import "../chunk-XRCXIG74.mjs";
|
|
3
|
+
|
|
4
|
+
// packages/core/functions/src/bundler/bundler.ts
|
|
5
|
+
import { build, initialize } from "esbuild-wasm";
|
|
6
|
+
import { subtleCrypto } from "@dxos/crypto";
|
|
7
|
+
import { invariant } from "@dxos/invariant";
|
|
8
|
+
import { log } from "@dxos/log";
|
|
9
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/bundler/bundler.ts";
|
|
10
|
+
var initialized;
|
|
11
|
+
var initializeBundler = async (options) => {
|
|
12
|
+
await (initialized ??= initialize({
|
|
13
|
+
wasmURL: options.wasmUrl
|
|
14
|
+
}));
|
|
15
|
+
};
|
|
16
|
+
var Bundler = class {
|
|
17
|
+
constructor(_options) {
|
|
18
|
+
this._options = _options;
|
|
19
|
+
}
|
|
20
|
+
async bundle({ path, source }) {
|
|
21
|
+
const { sandboxedModules: providedModules, ...options } = this._options;
|
|
22
|
+
const createResult = async (result) => {
|
|
23
|
+
return {
|
|
24
|
+
timestamp: Date.now(),
|
|
25
|
+
sourceHash: source ? Buffer.from(await subtleCrypto.digest("SHA-256", Buffer.from(source))) : void 0,
|
|
26
|
+
...result
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
if (this._options.platform === "browser") {
|
|
30
|
+
invariant(initialized, "Compiler not initialized.", {
|
|
31
|
+
F: __dxlog_file,
|
|
32
|
+
L: 71,
|
|
33
|
+
S: this,
|
|
34
|
+
A: [
|
|
35
|
+
"initialized",
|
|
36
|
+
"'Compiler not initialized.'"
|
|
37
|
+
]
|
|
38
|
+
});
|
|
39
|
+
await initialized;
|
|
40
|
+
}
|
|
41
|
+
const imports = source ? analyzeSourceFileImports(source) : [];
|
|
42
|
+
try {
|
|
43
|
+
const result = await build({
|
|
44
|
+
platform: options.platform,
|
|
45
|
+
conditions: [
|
|
46
|
+
"workerd",
|
|
47
|
+
"browser"
|
|
48
|
+
],
|
|
49
|
+
metafile: true,
|
|
50
|
+
write: false,
|
|
51
|
+
entryPoints: [
|
|
52
|
+
path ?? "memory:main.tsx"
|
|
53
|
+
],
|
|
54
|
+
bundle: true,
|
|
55
|
+
format: "esm",
|
|
56
|
+
plugins: [
|
|
57
|
+
{
|
|
58
|
+
name: "memory",
|
|
59
|
+
setup: (build2) => {
|
|
60
|
+
build2.onResolve({
|
|
61
|
+
filter: /^\.\/runtime\.js$/
|
|
62
|
+
}, ({ path: path2 }) => {
|
|
63
|
+
return {
|
|
64
|
+
path: path2,
|
|
65
|
+
external: true
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
build2.onResolve({
|
|
69
|
+
filter: /^dxos:functions$/
|
|
70
|
+
}, ({ path: path2 }) => {
|
|
71
|
+
return {
|
|
72
|
+
path: "./runtime.js",
|
|
73
|
+
external: true
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
build2.onResolve({
|
|
77
|
+
filter: /^memory:/
|
|
78
|
+
}, ({ path: path2 }) => {
|
|
79
|
+
return {
|
|
80
|
+
path: path2.split(":")[1],
|
|
81
|
+
namespace: "memory"
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
build2.onLoad({
|
|
85
|
+
filter: /.*/,
|
|
86
|
+
namespace: "memory"
|
|
87
|
+
}, ({ path: path2 }) => {
|
|
88
|
+
if (path2 === "main.tsx") {
|
|
89
|
+
return {
|
|
90
|
+
contents: source,
|
|
91
|
+
loader: "tsx"
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
for (const module of providedModules) {
|
|
96
|
+
build2.onResolve({
|
|
97
|
+
filter: new RegExp(`^${module}$`)
|
|
98
|
+
}, ({ path: path2 }) => {
|
|
99
|
+
return {
|
|
100
|
+
path: path2,
|
|
101
|
+
namespace: "injected-module"
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
build2.onLoad({
|
|
106
|
+
filter: /.*/,
|
|
107
|
+
namespace: "injected-module"
|
|
108
|
+
}, ({ path: path2 }) => {
|
|
109
|
+
const namedImports = imports.find((entry) => entry.moduleIdentifier === path2)?.namedImports ?? [];
|
|
110
|
+
return {
|
|
111
|
+
contents: `
|
|
112
|
+
const { ${namedImports.join(",")} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path2)}];
|
|
113
|
+
export { ${namedImports.join(",")} };
|
|
114
|
+
export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path2)}].default;
|
|
115
|
+
`,
|
|
116
|
+
loader: "tsx"
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
httpPlugin
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
log("compile complete", result.metafile, {
|
|
125
|
+
F: __dxlog_file,
|
|
126
|
+
L: 135,
|
|
127
|
+
S: this,
|
|
128
|
+
C: (f, a) => f(...a)
|
|
129
|
+
});
|
|
130
|
+
return await createResult({
|
|
131
|
+
imports: this.analyzeImports(result),
|
|
132
|
+
bundle: result.outputFiles[0].text
|
|
133
|
+
});
|
|
134
|
+
} catch (err) {
|
|
135
|
+
return await createResult({
|
|
136
|
+
error: err
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.
|
|
141
|
+
analyzeImports(result) {
|
|
142
|
+
invariant(result.outputFiles, void 0, {
|
|
143
|
+
F: __dxlog_file,
|
|
144
|
+
L: 148,
|
|
145
|
+
S: this,
|
|
146
|
+
A: [
|
|
147
|
+
"result.outputFiles",
|
|
148
|
+
""
|
|
149
|
+
]
|
|
150
|
+
});
|
|
151
|
+
const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
|
|
152
|
+
return Object.values(result.metafile.outputs)[0].imports.map((entry) => {
|
|
153
|
+
const namedImports = [];
|
|
154
|
+
const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
|
|
155
|
+
if (parsedImport?.[2]) {
|
|
156
|
+
NAMED_IMPORTS_REGEX.lastIndex = 0;
|
|
157
|
+
const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);
|
|
158
|
+
if (namedImportsMatch) {
|
|
159
|
+
namedImportsMatch[1].split(",").forEach((importName) => {
|
|
160
|
+
namedImports.push(importName.trim());
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
moduleUrl: entry.path,
|
|
166
|
+
defaultImport: !!parsedImport?.[1],
|
|
167
|
+
namedImports
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
analyzeSourceFileImports(code) {
|
|
172
|
+
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
173
|
+
return parsedImports.map((capture) => {
|
|
174
|
+
return {
|
|
175
|
+
defaultImportName: capture[1],
|
|
176
|
+
namedImports: capture[2]?.split(",").map((importName) => importName.trim()),
|
|
177
|
+
wildcardImportName: capture[3],
|
|
178
|
+
moduleIdentifier: capture[4],
|
|
179
|
+
quotes: capture[5]
|
|
180
|
+
};
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
var IMPORT_REGEX = /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;
|
|
185
|
+
var NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
|
|
186
|
+
var allMatches = (regex, str) => {
|
|
187
|
+
regex.lastIndex = 0;
|
|
188
|
+
let match;
|
|
189
|
+
const matches = [];
|
|
190
|
+
while (match = regex.exec(str)) {
|
|
191
|
+
matches.push(match);
|
|
192
|
+
}
|
|
193
|
+
return matches;
|
|
194
|
+
};
|
|
195
|
+
var analyzeSourceFileImports = (code) => {
|
|
196
|
+
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
197
|
+
return parsedImports.map((capture) => {
|
|
198
|
+
return {
|
|
199
|
+
defaultImportName: capture[1],
|
|
200
|
+
namedImports: capture[2]?.trim().slice(1, -1).split(",").map((importName) => importName.trim()),
|
|
201
|
+
wildcardImportName: capture[3],
|
|
202
|
+
moduleIdentifier: capture[4],
|
|
203
|
+
quotes: capture[5]
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
var httpPlugin = {
|
|
208
|
+
name: "http",
|
|
209
|
+
setup: (build2) => {
|
|
210
|
+
build2.onResolve({
|
|
211
|
+
filter: /^https?:\/\//
|
|
212
|
+
}, (args) => ({
|
|
213
|
+
path: args.path,
|
|
214
|
+
namespace: "http-url"
|
|
215
|
+
}));
|
|
216
|
+
build2.onResolve({
|
|
217
|
+
filter: /.*/,
|
|
218
|
+
namespace: "http-url"
|
|
219
|
+
}, (args) => ({
|
|
220
|
+
path: new URL(args.path, args.importer).toString(),
|
|
221
|
+
namespace: "http-url"
|
|
222
|
+
}));
|
|
223
|
+
build2.onLoad({
|
|
224
|
+
filter: /.*/,
|
|
225
|
+
namespace: "http-url"
|
|
226
|
+
}, async (args) => {
|
|
227
|
+
const response = await fetch(args.path);
|
|
228
|
+
return {
|
|
229
|
+
contents: await response.text(),
|
|
230
|
+
loader: "jsx"
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
export {
|
|
236
|
+
Bundler,
|
|
237
|
+
initializeBundler
|
|
238
|
+
};
|
|
239
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/bundler/bundler.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type BuildOptions } from 'esbuild';\nimport { build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';\n\nimport { subtleCrypto } from '@dxos/crypto';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nexport type Import = {\n moduleUrl: string;\n defaultImport: boolean;\n namedImports: string[];\n};\n\nexport type BundleOptions = {\n /**\n * Path to the source file on the local file system.\n * If provided, the path will be used instead of the `source` code.\n */\n path?: string;\n\n /**\n * Source code to bundle.\n * Required if `path` is not provided.\n */\n source?: string;\n};\n\nexport type BundleResult = {\n timestamp: number;\n sourceHash?: Buffer;\n imports?: Import[];\n bundle?: string;\n error?: any;\n};\n\nexport type BundlerOptions = {\n platform: BuildOptions['platform'];\n sandboxedModules: string[];\n remoteModules: Record<string, string>;\n};\n\nlet initialized: Promise<void>;\nexport const initializeBundler = async (options: { wasmUrl: string }) => {\n await (initialized ??= initialize({\n wasmURL: options.wasmUrl,\n }));\n};\n\n/**\n * ESBuild bundler.\n */\nexport class Bundler {\n constructor(private readonly _options: BundlerOptions) {}\n\n async bundle({ path, source }: BundleOptions): Promise<BundleResult> {\n const { sandboxedModules: providedModules, ...options } = this._options;\n\n const createResult = async (result?: Partial<BundleResult>) => {\n return {\n timestamp: Date.now(),\n sourceHash: source ? Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))) : undefined,\n ...result,\n };\n };\n\n if (this._options.platform === 'browser') {\n invariant(initialized, 'Compiler not initialized.');\n await initialized;\n }\n\n const imports = source ? analyzeSourceFileImports(source) : [];\n\n // https://esbuild.github.io/api/#build\n try {\n const result = await build({\n platform: options.platform,\n conditions: ['workerd', 'browser'],\n metafile: true,\n write: false,\n entryPoints: [path ?? 'memory:main.tsx'],\n bundle: true,\n format: 'esm',\n plugins: [\n {\n name: 'memory',\n setup: (build) => {\n build.onResolve({ filter: /^\\.\\/runtime\\.js$/ }, ({ path }) => {\n return { path, external: true };\n });\n\n build.onResolve({ filter: /^dxos:functions$/ }, ({ path }) => {\n return { path: './runtime.js', external: true };\n });\n\n build.onResolve({ filter: /^memory:/ }, ({ path }) => {\n return { path: path.split(':')[1], namespace: 'memory' };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'memory' }, ({ path }) => {\n if (path === 'main.tsx') {\n return {\n contents: source,\n loader: 'tsx',\n };\n }\n });\n\n for (const module of providedModules) {\n build.onResolve({ filter: new RegExp(`^${module}$`) }, ({ path }) => {\n return { path, namespace: 'injected-module' };\n });\n }\n\n build.onLoad({ filter: /.*/, namespace: 'injected-module' }, ({ path }) => {\n const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];\n return {\n contents: `\n const { ${namedImports.join(',')} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];\n export { ${namedImports.join(',')} };\n export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;\n `,\n loader: 'tsx',\n };\n });\n },\n },\n httpPlugin,\n ],\n });\n\n log('compile complete', result.metafile);\n\n return await createResult({\n imports: this.analyzeImports(result),\n bundle: result.outputFiles![0].text,\n });\n } catch (err) {\n return await createResult({ error: err });\n }\n }\n\n // TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.\n analyzeImports(result: BuildResult): Import[] {\n invariant(result.outputFiles);\n\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);\n return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {\n const namedImports: string[] = [];\n\n const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);\n if (parsedImport?.[2]) {\n NAMED_IMPORTS_REGEX.lastIndex = 0;\n const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);\n if (namedImportsMatch) {\n namedImportsMatch[1].split(',').forEach((importName) => {\n namedImports.push(importName.trim());\n });\n }\n }\n\n return {\n moduleUrl: entry.path,\n defaultImport: !!parsedImport?.[1],\n namedImports,\n };\n });\n }\n\n analyzeSourceFileImports(code: string) {\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, code);\n return parsedImports.map((capture) => {\n return {\n defaultImportName: capture[1],\n namedImports: capture[2]?.split(',').map((importName) => importName.trim()),\n wildcardImportName: capture[3],\n moduleIdentifier: capture[4],\n quotes: capture[5],\n };\n });\n }\n}\n\n// https://regex101.com/r/FEN5ks/1\n// https://stackoverflow.com/a/73265022\n// $1 = default import name (can be non-existent)\n// $2 = destructured exports (can be non-existent)\n// $3 = wildcard import name (can be non-existent)\n// $4 = module identifier\n// $5 = quotes used (either ' or \")\nconst IMPORT_REGEX =\n /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;\n\nconst NAMED_IMPORTS_REGEX = /[ \\n\\t]*{((?:[ \\n\\t]*[^ \\n\\t\"'{}]+[ \\n\\t]*,?)+)}[ \\n\\t]*/gm;\n\nconst allMatches = (regex: RegExp, str: string) => {\n regex.lastIndex = 0;\n\n let match;\n const matches = [];\n while ((match = regex.exec(str))) {\n matches.push(match);\n }\n\n return matches;\n};\n\ntype ParsedImport = {\n defaultImportName?: string;\n namedImports: string[];\n wildcardImportName?: string;\n moduleIdentifier: string;\n quotes: string;\n};\n\nconst analyzeSourceFileImports = (code: string): ParsedImport[] => {\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, code);\n return parsedImports.map((capture) => {\n return {\n defaultImportName: capture[1],\n namedImports: capture[2]\n ?.trim()\n .slice(1, -1)\n .split(',')\n .map((importName) => importName.trim()),\n wildcardImportName: capture[3],\n moduleIdentifier: capture[4],\n quotes: capture[5],\n };\n });\n};\n\nconst httpPlugin: Plugin = {\n name: 'http',\n setup: (build) => {\n // Intercept import paths starting with \"http:\" and \"https:\" so esbuild doesn't attempt to map them to a file system location.\n // Tag them with the \"http-url\" namespace to associate them with this plugin.\n build.onResolve({ filter: /^https?:\\/\\// }, (args) => ({\n path: args.path,\n namespace: 'http-url',\n }));\n\n // We also want to intercept all import paths inside downloaded files and resolve them against the original URL.\n // All of these files will be in the \"http-url\" namespace.\n // Make sure to keep the newly resolved URL in the \"http-url\" namespace so imports inside it will also be resolved as URLs recursively.\n build.onResolve({ filter: /.*/, namespace: 'http-url' }, (args) => ({\n path: new URL(args.path, args.importer).toString(),\n namespace: 'http-url',\n }));\n\n // When a URL is loaded, we want to actually download the content from the internet.\n // 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.\n build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {\n const response = await fetch(args.path);\n return { contents: await response.text(), loader: 'jsx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AO;QAAE+B,UAAU,MAAMiD,SAASnC,KAAI;QAAIb,QAAQ;MAAM;IAC1D,CAAA;EACF;AACF;",
|
|
6
|
+
"names": ["build", "initialize", "subtleCrypto", "invariant", "log", "initialized", "initializeBundler", "options", "wasmURL", "wasmUrl", "Bundler", "constructor", "_options", "bundle", "path", "source", "sandboxedModules", "providedModules", "createResult", "result", "timestamp", "Date", "now", "sourceHash", "Buffer", "from", "digest", "undefined", "platform", "imports", "analyzeSourceFileImports", "conditions", "metafile", "write", "entryPoints", "format", "plugins", "name", "setup", "onResolve", "filter", "external", "split", "namespace", "onLoad", "contents", "loader", "module", "RegExp", "namedImports", "find", "entry", "moduleIdentifier", "join", "JSON", "stringify", "httpPlugin", "analyzeImports", "outputFiles", "text", "err", "error", "parsedImports", "allMatches", "IMPORT_REGEX", "Object", "values", "outputs", "map", "parsedImport", "capture", "NAMED_IMPORTS_REGEX", "lastIndex", "namedImportsMatch", "exec", "forEach", "importName", "push", "trim", "moduleUrl", "defaultImport", "code", "defaultImportName", "wildcardImportName", "quotes", "regex", "str", "match", "matches", "slice", "args", "URL", "importer", "toString", "response", "fetch"]
|
|
7
|
+
}
|
|
@@ -7,7 +7,6 @@ var ScriptType = class extends TypedObject({
|
|
|
7
7
|
typename: "dxos.org/type/Script",
|
|
8
8
|
version: "0.1.0"
|
|
9
9
|
})({
|
|
10
|
-
// TODO(burdon): Change to URI?
|
|
11
10
|
name: S.optional(S.String),
|
|
12
11
|
description: S.optional(S.String),
|
|
13
12
|
// TODO(burdon): Change to hash of deployed content.
|
|
@@ -23,23 +22,26 @@ var FunctionType = class extends TypedObject({
|
|
|
23
22
|
// TODO(burdon): Rename to id/uri?
|
|
24
23
|
name: S.NonEmptyString,
|
|
25
24
|
version: S.String,
|
|
25
|
+
description: S.optional(S.String),
|
|
26
26
|
// Reference to a source script if it exists within ECHO.
|
|
27
27
|
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
28
28
|
source: S.optional(Ref(ScriptType)),
|
|
29
29
|
inputSchema: S.optional(JsonSchemaType),
|
|
30
|
+
outputSchema: S.optional(JsonSchemaType),
|
|
30
31
|
// Local binding to a function name.
|
|
31
32
|
binding: S.optional(S.String)
|
|
32
33
|
}) {
|
|
33
34
|
};
|
|
34
35
|
|
|
35
36
|
// packages/core/functions/src/types/types.ts
|
|
36
|
-
import { AST, OptionsAnnotationId, RawObject, S as S2, TypedObject as TypedObject2 } from "@dxos/echo-schema";
|
|
37
|
+
import { AST, OptionsAnnotationId, RawObject, S as S2, TypedObject as TypedObject2, DXN } from "@dxos/echo-schema";
|
|
37
38
|
var TriggerKind;
|
|
38
39
|
(function(TriggerKind2) {
|
|
39
40
|
TriggerKind2["Timer"] = "timer";
|
|
40
41
|
TriggerKind2["Webhook"] = "webhook";
|
|
41
42
|
TriggerKind2["Subscription"] = "subscription";
|
|
42
43
|
TriggerKind2["Email"] = "email";
|
|
44
|
+
TriggerKind2["Queue"] = "queue";
|
|
43
45
|
})(TriggerKind || (TriggerKind = {}));
|
|
44
46
|
var typeLiteralAnnotations = {
|
|
45
47
|
[AST.TitleAnnotationId]: "Type"
|
|
@@ -56,6 +58,10 @@ var TimerTriggerSchema = S2.Struct({
|
|
|
56
58
|
var EmailTriggerSchema = S2.Struct({
|
|
57
59
|
type: S2.Literal("email").annotations(typeLiteralAnnotations)
|
|
58
60
|
}).pipe(S2.mutable);
|
|
61
|
+
var QueueTriggerSchema = S2.Struct({
|
|
62
|
+
type: S2.Literal("queue").annotations(typeLiteralAnnotations),
|
|
63
|
+
queue: DXN
|
|
64
|
+
}).pipe(S2.mutable);
|
|
59
65
|
var WebhookTriggerSchema = S2.Struct({
|
|
60
66
|
type: S2.Literal("webhook").annotations(typeLiteralAnnotations),
|
|
61
67
|
method: S2.optional(S2.String.annotations({
|
|
@@ -97,17 +103,12 @@ var SubscriptionTriggerSchema = S2.Struct({
|
|
|
97
103
|
[AST.TitleAnnotationId]: "Options"
|
|
98
104
|
}))
|
|
99
105
|
}).pipe(S2.mutable);
|
|
100
|
-
var TriggerSchema = S2.Union(
|
|
101
|
-
//
|
|
102
|
-
TimerTriggerSchema,
|
|
103
|
-
WebhookTriggerSchema,
|
|
104
|
-
SubscriptionTriggerSchema,
|
|
105
|
-
EmailTriggerSchema
|
|
106
|
-
).annotations({
|
|
106
|
+
var TriggerSchema = S2.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
|
|
107
107
|
[AST.TitleAnnotationId]: "Trigger"
|
|
108
108
|
});
|
|
109
109
|
var FunctionTriggerSchema = S2.Struct({
|
|
110
110
|
// TODO(burdon): What type does this reference.
|
|
111
|
+
// TODO(wittjosiah): This should probably be a Ref?
|
|
111
112
|
function: S2.optional(S2.String.annotations({
|
|
112
113
|
[AST.TitleAnnotationId]: "Function"
|
|
113
114
|
})),
|
|
@@ -147,6 +148,43 @@ var FUNCTION_TYPES = [
|
|
|
147
148
|
FunctionTrigger
|
|
148
149
|
];
|
|
149
150
|
|
|
151
|
+
// packages/core/functions/src/types/trace.ts
|
|
152
|
+
import { EchoObject, Expando, ObjectId, Ref as Ref2, S as S3 } from "@dxos/echo-schema";
|
|
153
|
+
var InvocationTrace = S3.Struct({
|
|
154
|
+
id: ObjectId,
|
|
155
|
+
/**
|
|
156
|
+
* Queue DXN for function/workflow invocation events.
|
|
157
|
+
*/
|
|
158
|
+
invocationTraceQueue: Ref2(Expando),
|
|
159
|
+
/**
|
|
160
|
+
* DXN of the invoked function/workflow.
|
|
161
|
+
*/
|
|
162
|
+
invocationTarget: Ref2(Expando),
|
|
163
|
+
/**
|
|
164
|
+
* Present for automatic invocations.
|
|
165
|
+
*/
|
|
166
|
+
trigger: S3.optional(Ref2(FunctionTrigger))
|
|
167
|
+
}).pipe(EchoObject("dxos.org/type/InvocationTrace", "0.1.0"));
|
|
168
|
+
var TraceEventLog = S3.Struct({
|
|
169
|
+
timestampMs: S3.Number,
|
|
170
|
+
level: S3.String,
|
|
171
|
+
message: S3.String,
|
|
172
|
+
context: S3.optional(S3.Object)
|
|
173
|
+
});
|
|
174
|
+
var TraceEventException = S3.Struct({
|
|
175
|
+
timestampMs: S3.Number,
|
|
176
|
+
message: S3.String,
|
|
177
|
+
name: S3.String,
|
|
178
|
+
stack: S3.optional(S3.String)
|
|
179
|
+
});
|
|
180
|
+
var TraceEvent = S3.Struct({
|
|
181
|
+
id: ObjectId,
|
|
182
|
+
outcome: S3.String,
|
|
183
|
+
truncated: S3.Boolean,
|
|
184
|
+
logs: S3.Array(TraceEventLog),
|
|
185
|
+
exceptions: S3.Array(TraceEventException)
|
|
186
|
+
}).pipe(EchoObject("dxos.org/type/TraceEvent", "0.1.0"));
|
|
187
|
+
|
|
150
188
|
export {
|
|
151
189
|
ScriptType,
|
|
152
190
|
FunctionType,
|
|
@@ -156,6 +194,10 @@ export {
|
|
|
156
194
|
FunctionTrigger,
|
|
157
195
|
FunctionDef,
|
|
158
196
|
FunctionManifestSchema,
|
|
159
|
-
FUNCTION_TYPES
|
|
197
|
+
FUNCTION_TYPES,
|
|
198
|
+
InvocationTrace,
|
|
199
|
+
TraceEventLog,
|
|
200
|
+
TraceEventException,
|
|
201
|
+
TraceEvent
|
|
160
202
|
};
|
|
161
|
-
//# sourceMappingURL=chunk-
|
|
203
|
+
//# sourceMappingURL=chunk-6XYG2TNO.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/types/schema.ts", "../../../src/types/types.ts", "../../../src/types/trace.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { JsonSchemaType, Ref, S, TypedObject } from '@dxos/echo-schema';\nimport { TextType } from '@dxos/schema';\n\n/**\n * Source script.\n */\nexport class ScriptType extends TypedObject({\n typename: 'dxos.org/type/Script',\n version: '0.1.0',\n})({\n name: S.optional(S.String),\n description: S.optional(S.String),\n // TODO(burdon): Change to hash of deployed content.\n // Whether source has changed since last deploy.\n changed: S.optional(S.Boolean),\n source: Ref(TextType),\n}) {}\n\n/**\n * Function deployment.\n */\n// TODO(burdon): Move to core/functions.\nexport class FunctionType extends TypedObject({\n typename: 'dxos.org/type/Function',\n version: '0.1.0',\n})({\n // TODO(burdon): Rename to id/uri?\n name: S.NonEmptyString,\n version: S.String,\n\n description: S.optional(S.String),\n\n // Reference to a source script if it exists within ECHO.\n // TODO(burdon): Don't ref ScriptType directly (core).\n source: S.optional(Ref(ScriptType)),\n\n inputSchema: S.optional(JsonSchemaType),\n outputSchema: S.optional(JsonSchemaType),\n\n // Local binding to a function name.\n binding: S.optional(S.String),\n}) {}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { AST, OptionsAnnotationId, RawObject, S, TypedObject, DXN } from '@dxos/echo-schema';\n\n/**\n * Type discriminator for TriggerType.\n * Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.\n * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions\n */\nexport enum TriggerKind {\n Timer = 'timer',\n Webhook = 'webhook',\n Subscription = 'subscription',\n Email = 'email',\n Queue = 'queue',\n}\n\n// TODO(burdon): Rename prop kind.\nconst typeLiteralAnnotations = { [AST.TitleAnnotationId]: 'Type' };\n\n/**\n * Cron timer.\n */\nconst TimerTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Timer).annotations(typeLiteralAnnotations),\n cron: S.String.annotations({\n [AST.TitleAnnotationId]: 'Cron',\n [AST.ExamplesAnnotationId]: ['0 0 * * *'],\n }),\n}).pipe(S.mutable);\n\nexport type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;\n\nconst EmailTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Email).annotations(typeLiteralAnnotations),\n}).pipe(S.mutable);\n\nexport type EmailTrigger = S.Schema.Type<typeof EmailTriggerSchema>;\n\nconst QueueTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Queue).annotations(typeLiteralAnnotations),\n queue: DXN,\n}).pipe(S.mutable);\n\nexport type QueueTrigger = S.Schema.Type<typeof QueueTriggerSchema>;\n\n/**\n * Webhook.\n */\nconst WebhookTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Webhook).annotations(typeLiteralAnnotations),\n method: S.optional(\n S.String.annotations({\n [AST.TitleAnnotationId]: 'Method',\n [OptionsAnnotationId]: ['GET', 'POST'],\n }),\n ),\n port: S.optional(\n S.Number.annotations({\n [AST.TitleAnnotationId]: 'Port',\n }),\n ),\n}).pipe(S.mutable);\n\nexport type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;\n\n// TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).\nconst QuerySchema = S.Struct({\n type: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Type' })),\n props: S.optional(S.Record({ key: S.String, value: S.Any })),\n}).annotations({ [AST.TitleAnnotationId]: 'Query' });\n\n/**\n * Subscription.\n */\nconst SubscriptionTriggerSchema = S.Struct({\n type: S.Literal(TriggerKind.Subscription).annotations(typeLiteralAnnotations),\n // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.\n filter: QuerySchema,\n options: S.optional(\n S.Struct({\n // Watch changes to object (not just creation).\n deep: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Nested' })),\n // Debounce changes (delay in ms).\n delay: S.optional(S.Number.annotations({ [AST.TitleAnnotationId]: 'Delay' })),\n }).annotations({ [AST.TitleAnnotationId]: 'Options' }),\n ),\n}).pipe(S.mutable);\n\nexport type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;\n\n/**\n * Trigger schema (discriminated union).\n */\nexport const TriggerSchema = S.Union(\n TimerTriggerSchema,\n WebhookTriggerSchema,\n SubscriptionTriggerSchema,\n EmailTriggerSchema,\n QueueTriggerSchema,\n).annotations({\n [AST.TitleAnnotationId]: 'Trigger',\n});\n\nexport type TriggerType = S.Schema.Type<typeof TriggerSchema>;\n\n/**\n * Function trigger.\n */\nexport const FunctionTriggerSchema = S.Struct({\n // TODO(burdon): What type does this reference.\n // TODO(wittjosiah): This should probably be a Ref?\n function: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Function' })),\n\n enabled: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Enabled' })),\n\n // TODO(burdon): Flatten entire schema.\n spec: S.optional(TriggerSchema),\n\n // TODO(burdon): Get meta from function.\n // The `meta` property is merged into the event data passed to the function.\n meta: S.optional(S.mutable(S.Record({ key: S.String, value: S.Any }))),\n});\n\nexport type FunctionTriggerType = S.Schema.Type<typeof FunctionTriggerSchema>;\n\n/**\n * Function trigger.\n */\nexport class FunctionTrigger extends TypedObject({\n typename: 'dxos.org/type/FunctionTrigger',\n version: '0.1.0',\n})(FunctionTriggerSchema.fields) {}\n\n/**\n * Function definition.\n * @deprecated (Use dxos.org/type/Function)\n */\n// TODO(burdon): Reconcile with FunctionType.\nexport class FunctionDef extends TypedObject({\n typename: 'dxos.org/type/FunctionDef',\n version: '0.1.0',\n})({\n uri: S.String,\n description: S.optional(S.String),\n route: S.String,\n handler: S.String,\n}) {}\n\n/**\n * Function manifest file.\n */\nexport const FunctionManifestSchema = S.Struct({\n functions: S.optional(S.mutable(S.Array(RawObject(FunctionDef)))),\n triggers: S.optional(S.mutable(S.Array(RawObject(FunctionTrigger)))),\n});\n\nexport type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;\n\nexport const FUNCTION_TYPES = [FunctionDef, FunctionTrigger];\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { EchoObject, Expando, ObjectId, Ref, S } from '@dxos/echo-schema';\n\nimport { FunctionTrigger } from './types';\n\nexport const InvocationTrace = S.Struct({\n id: ObjectId,\n /**\n * Queue DXN for function/workflow invocation events.\n */\n invocationTraceQueue: Ref(Expando),\n /**\n * DXN of the invoked function/workflow.\n */\n invocationTarget: Ref(Expando),\n /**\n * Present for automatic invocations.\n */\n trigger: S.optional(Ref(FunctionTrigger)),\n}).pipe(EchoObject('dxos.org/type/InvocationTrace', '0.1.0'));\n\nexport type InvocationTraceEvent = S.Schema.Type<typeof InvocationTrace>;\n\nexport const TraceEventLog = S.Struct({\n timestampMs: S.Number,\n level: S.String,\n message: S.String,\n context: S.optional(S.Object),\n});\n\nexport const TraceEventException = S.Struct({\n timestampMs: S.Number,\n message: S.String,\n name: S.String,\n stack: S.optional(S.String),\n});\n\nexport const TraceEvent = S.Struct({\n id: ObjectId,\n outcome: S.String,\n truncated: S.Boolean,\n logs: S.Array(TraceEventLog),\n exceptions: S.Array(TraceEventException),\n}).pipe(EchoObject('dxos.org/type/TraceEvent', '0.1.0'));\n\nexport type TraceEvent = S.Schema.Type<typeof TraceEvent>;\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,gBAAgBC,KAAKC,GAAGC,mBAAmB;AACpD,SAASC,gBAAgB;AAKlB,IAAMC,aAAN,cAAyBC,YAAY;EAC1CC,UAAU;EACVC,SAAS;AACX,CAAA,EAAG;EACDC,MAAMC,EAAEC,SAASD,EAAEE,MAAM;EACzBC,aAAaH,EAAEC,SAASD,EAAEE,MAAM;;;EAGhCE,SAASJ,EAAEC,SAASD,EAAEK,OAAO;EAC7BC,QAAQC,IAAIC,QAAAA;AACd,CAAA,EAAA;AAAI;AAMG,IAAMC,eAAN,cAA2Bb,YAAY;EAC5CC,UAAU;EACVC,SAAS;AACX,CAAA,EAAG;;EAEDC,MAAMC,EAAEU;EACRZ,SAASE,EAAEE;EAEXC,aAAaH,EAAEC,SAASD,EAAEE,MAAM;;;EAIhCI,QAAQN,EAAEC,SAASM,IAAIZ,UAAAA,CAAAA;EAEvBgB,aAAaX,EAAEC,SAASW,cAAAA;EACxBC,cAAcb,EAAEC,SAASW,cAAAA;;EAGzBE,SAASd,EAAEC,SAASD,EAAEE,MAAM;AAC9B,CAAA,EAAA;AAAI;;;ACzCJ,SAASa,KAAKC,qBAAqBC,WAAWC,KAAAA,IAAGC,eAAAA,cAAaC,WAAW;;UAO7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C,IAAIC;EACJsB,SAASzB,GAAEiB;EACXS,WAAW1B,GAAE2B;EACbC,MAAM5B,GAAE6B,MAAMhB,aAAAA;EACdiB,YAAY9B,GAAE6B,MAAMR,mBAAAA;AACtB,CAAA,EAAGV,KAAKC,WAAW,4BAA4B,OAAA,CAAA;",
|
|
6
|
+
"names": ["JsonSchemaType", "Ref", "S", "TypedObject", "TextType", "ScriptType", "TypedObject", "typename", "version", "name", "S", "optional", "String", "description", "changed", "Boolean", "source", "Ref", "TextType", "FunctionType", "NonEmptyString", "inputSchema", "JsonSchemaType", "outputSchema", "binding", "AST", "OptionsAnnotationId", "RawObject", "S", "TypedObject", "DXN", "TriggerKind", "typeLiteralAnnotations", "AST", "TitleAnnotationId", "TimerTriggerSchema", "S", "Struct", "type", "Literal", "annotations", "cron", "String", "ExamplesAnnotationId", "pipe", "mutable", "EmailTriggerSchema", "QueueTriggerSchema", "queue", "DXN", "WebhookTriggerSchema", "method", "optional", "OptionsAnnotationId", "port", "Number", "QuerySchema", "props", "Record", "key", "value", "Any", "SubscriptionTriggerSchema", "filter", "options", "deep", "Boolean", "delay", "TriggerSchema", "Union", "FunctionTriggerSchema", "function", "enabled", "spec", "meta", "FunctionTrigger", "TypedObject", "typename", "version", "fields", "FunctionDef", "uri", "description", "route", "handler", "FunctionManifestSchema", "functions", "Array", "RawObject", "triggers", "FUNCTION_TYPES", "EchoObject", "Expando", "ObjectId", "Ref", "S", "InvocationTrace", "S", "Struct", "id", "ObjectId", "invocationTraceQueue", "Ref", "Expando", "invocationTarget", "trigger", "optional", "FunctionTrigger", "pipe", "EchoObject", "TraceEventLog", "timestampMs", "Number", "level", "String", "message", "context", "Object", "TraceEventException", "name", "stack", "TraceEvent", "outcome", "truncated", "Boolean", "logs", "Array", "exceptions"]
|
|
7
|
+
}
|
|
@@ -2,7 +2,7 @@ import "@dxos/node-std/globals";
|
|
|
2
2
|
import {
|
|
3
3
|
FunctionDef,
|
|
4
4
|
FunctionTrigger
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-6XYG2TNO.mjs";
|
|
6
6
|
|
|
7
7
|
// packages/core/functions/src/function/function-registry.ts
|
|
8
8
|
import { Event } from "@dxos/async";
|
|
@@ -477,4 +477,4 @@ export {
|
|
|
477
477
|
createTimerTrigger,
|
|
478
478
|
TriggerRegistry
|
|
479
479
|
};
|
|
480
|
-
//# sourceMappingURL=chunk-
|
|
480
|
+
//# sourceMappingURL=chunk-WI2RVE6E.mjs.map
|
|
@@ -65,9 +65,12 @@ var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
|
65
65
|
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
66
66
|
options.spaceId && url.searchParams.set("spaceId", options.spaceId);
|
|
67
67
|
options.subjectId && url.searchParams.set("subjectId", options.subjectId);
|
|
68
|
-
url.protocol = "https";
|
|
68
|
+
url.protocol = isSecure(url.protocol) ? "https" : "http";
|
|
69
69
|
return url.toString();
|
|
70
70
|
};
|
|
71
|
+
var isSecure = (protocol) => {
|
|
72
|
+
return protocol === "https:" || protocol === "wss:";
|
|
73
|
+
};
|
|
71
74
|
var createEdgeIdentity = (client) => {
|
|
72
75
|
const identity = client.halo.identity.get();
|
|
73
76
|
const device = client.halo.device;
|
|
@@ -96,7 +99,7 @@ var incrementSemverPatch = (version) => {
|
|
|
96
99
|
const patchNum = Number(patch);
|
|
97
100
|
invariant(!Number.isNaN(patchNum), "Unexpected function version format.", {
|
|
98
101
|
F: __dxlog_file,
|
|
99
|
-
L:
|
|
102
|
+
L: 109,
|
|
100
103
|
S: void 0,
|
|
101
104
|
A: [
|
|
102
105
|
"!Number.isNaN(patchNum)",
|
|
@@ -122,4 +125,4 @@ export {
|
|
|
122
125
|
incrementSemverPatch,
|
|
123
126
|
publicKeyToDid
|
|
124
127
|
};
|
|
125
|
-
//# sourceMappingURL=chunk-
|
|
128
|
+
//# sourceMappingURL=chunk-YJEIETRB.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/edge/functions.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type DID } from 'iso-did/types';\n\nimport { type Client } from '@dxos/client';\nimport { type ObjectMeta } from '@dxos/echo-schema';\nimport { EdgeHttpClient, type EdgeIdentity } from '@dxos/edge-client';\nimport { invariant } from '@dxos/invariant';\nimport type { PublicKey, SpaceId } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type UploadFunctionResponseBody } from '@dxos/protocols';\n\n// TODO: use URL scheme for source?\nconst FUNCTIONS_META_KEY = 'dxos.org/service/function';\n\nexport const FUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';\n\nexport type UploadWorkerArgs = {\n client: Client;\n name?: string;\n source: string;\n version: string;\n functionId?: string;\n spaceId: SpaceId;\n};\n\nexport const uploadWorkerFunction = async ({\n client,\n name,\n version,\n source,\n spaceId,\n functionId,\n}: UploadWorkerArgs): Promise<UploadFunctionResponseBody> => {\n const edgeUrl = client.config.values.runtime?.services?.edge?.url;\n invariant(edgeUrl, 'Edge is not configured.');\n const edgeClient = new EdgeHttpClient(edgeUrl);\n const edgeIdentity = createEdgeIdentity(client);\n edgeClient.setIdentity(edgeIdentity);\n const response = await edgeClient.uploadFunction({ spaceId, functionId }, { name, version, script: source });\n\n log('Uploaded', {\n functionId,\n source,\n name,\n identityKey: edgeIdentity.identityKey,\n response,\n });\n\n return response;\n};\n\nexport const getUserFunctionUrlInMetadata = (meta: ObjectMeta) => {\n return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;\n};\n\nexport const setUserFunctionUrlInMetadata = (meta: ObjectMeta, functionUrl: string) => {\n const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);\n if (key) {\n if (key.id !== functionUrl) {\n throw new Error('Metadata mismatch');\n }\n } else {\n meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionUrl });\n }\n};\n\nexport const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {\n const baseUrl = new URL('functions/', edgeUrl);\n\n // Leading slashes cause the URL to be treated as an absolute path.\n const relativeUrl = functionUrl.replace(/^\\//, '');\n const url = new URL(`./${relativeUrl}`, baseUrl.toString());\n options.spaceId && url.searchParams.set('spaceId', options.spaceId);\n options.subjectId && url.searchParams.set('subjectId', options.subjectId);\n url.protocol = 'https';\n return url.toString();\n};\n\nconst createEdgeIdentity = (client: Client): EdgeIdentity => {\n const identity = client.halo.identity.get();\n const device = client.halo.device;\n if (!identity || !device) {\n throw new Error('Identity not available');\n }\n return {\n identityKey: identity.identityKey.toHex(),\n peerKey: device.deviceKey.toHex(),\n presentCredentials: async ({ challenge }) => {\n const identityService = client.services.services.IdentityService!;\n const authCredential = await identityService.createAuthCredential();\n return identityService.signPresentation({\n presentation: { credentials: [authCredential] },\n nonce: challenge,\n });\n },\n };\n};\n\nexport const incrementSemverPatch = (version: string): string => {\n const [major, minor, patch] = version.split('.');\n const patchNum = Number(patch);\n invariant(!Number.isNaN(patchNum), 'Unexpected function version format.');\n return [major, minor, String(patchNum + 1)].join('.');\n};\n\nexport const publicKeyToDid = (key: PublicKey): DID => {\n return `did:key:${key.toHex()}`;\n};\n\nexport type InvocationOptions = {\n spaceId?: SpaceId;\n subjectId?: string;\n};\n"],
|
|
5
|
-
"mappings": ";;;AAQA,SAASA,sBAAyC;AAClD,SAASC,iBAAiB;AAE1B,SAASC,WAAW;;AAIpB,IAAMC,qBAAqB;AAEpB,IAAMC,4BAA4B;AAWlC,IAAMC,uBAAuB,OAAO,EACzCC,QACAC,MACAC,SACAC,QACAC,SACAC,WAAU,MACO;AACjB,QAAMC,UAAUN,OAAOO,OAAOC,OAAOC,SAASC,UAAUC,MAAMC;AAC9DjB,YAAUW,SAAS,2BAAA;;;;;;;;;AACnB,QAAMO,aAAa,IAAInB,eAAeY,OAAAA;AACtC,QAAMQ,eAAeC,mBAAmBf,MAAAA;AACxCa,aAAWG,YAAYF,YAAAA;AACvB,QAAMG,WAAW,MAAMJ,WAAWK,eAAe;IAAEd;IAASC;EAAW,GAAG;IAAEJ;IAAMC;IAASiB,QAAQhB;EAAO,CAAA;AAE1GP,MAAI,YAAY;IACdS;IACAF;IACAF;IACAmB,aAAaN,aAAaM;IAC1BH;EACF,GAAA;;;;;;AAEA,SAAOA;AACT;AAEO,IAAMI,+BAA+B,CAACC,SAAAA;AAC3C,SAAOA,KAAKC,KAAKC,KAAK,CAACC,QAAQA,IAAItB,WAAWN,kBAAAA,GAAqB6B;AACrE;AAEO,IAAMC,+BAA+B,CAACL,MAAkBM,gBAAAA;AAC7D,QAAMH,MAAMH,KAAKC,KAAKC,KAAK,CAACC,SAAQA,KAAItB,WAAWN,kBAAAA;AACnD,MAAI4B,KAAK;AACP,QAAIA,IAAIC,OAAOE,aAAa;AAC1B,YAAM,IAAIC,MAAM,mBAAA;IAClB;EACF,OAAO;AACLP,SAAKC,KAAKO,KAAK;MAAE3B,QAAQN;MAAoB6B,IAAIE;IAAY,CAAA;EAC/D;AACF;AAEO,IAAMG,mBAAmB,CAACH,aAAqBtB,SAAiB0B,UAA6B,CAAC,MAAC;AACpG,QAAMC,UAAU,IAAIC,IAAI,cAAc5B,OAAAA;AAGtC,QAAM6B,cAAcP,YAAYQ,QAAQ,OAAO,EAAA;AAC/C,QAAMxB,MAAM,IAAIsB,IAAI,KAAKC,WAAAA,IAAeF,QAAQI,SAAQ,CAAA;AACxDL,UAAQ5B,WAAWQ,IAAI0B,aAAaC,IAAI,WAAWP,QAAQ5B,OAAO;AAClE4B,UAAQQ,aAAa5B,IAAI0B,aAAaC,IAAI,aAAaP,QAAQQ,SAAS;AACxE5B,MAAI6B,
|
|
6
|
-
"names": ["EdgeHttpClient", "invariant", "log", "FUNCTIONS_META_KEY", "FUNCTIONS_PRESET_META_KEY", "uploadWorkerFunction", "client", "name", "version", "source", "spaceId", "functionId", "edgeUrl", "config", "values", "runtime", "services", "edge", "url", "edgeClient", "edgeIdentity", "createEdgeIdentity", "setIdentity", "response", "uploadFunction", "script", "identityKey", "getUserFunctionUrlInMetadata", "meta", "keys", "find", "key", "id", "setUserFunctionUrlInMetadata", "functionUrl", "Error", "push", "getInvocationUrl", "options", "baseUrl", "URL", "relativeUrl", "replace", "toString", "searchParams", "set", "subjectId", "protocol", "identity", "halo", "get", "device", "toHex", "peerKey", "deviceKey", "presentCredentials", "challenge", "identityService", "IdentityService", "authCredential", "createAuthCredential", "signPresentation", "presentation", "credentials", "nonce", "incrementSemverPatch", "major", "minor", "patch", "split", "patchNum", "Number", "isNaN", "String", "join", "publicKeyToDid"]
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type DID } from 'iso-did/types';\n\nimport { type Client } from '@dxos/client';\nimport { type ObjectMeta } from '@dxos/echo-schema';\nimport { EdgeHttpClient, type EdgeIdentity } from '@dxos/edge-client';\nimport { invariant } from '@dxos/invariant';\nimport type { PublicKey, SpaceId } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type UploadFunctionResponseBody } from '@dxos/protocols';\n\n// TODO: use URL scheme for source?\nconst FUNCTIONS_META_KEY = 'dxos.org/service/function';\n\nexport const FUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';\n\nexport type UploadWorkerArgs = {\n client: Client;\n name?: string;\n source: string;\n version: string;\n functionId?: string;\n spaceId: SpaceId;\n};\n\nexport const uploadWorkerFunction = async ({\n client,\n name,\n version,\n source,\n spaceId,\n functionId,\n}: UploadWorkerArgs): Promise<UploadFunctionResponseBody> => {\n const edgeUrl = client.config.values.runtime?.services?.edge?.url;\n invariant(edgeUrl, 'Edge is not configured.');\n const edgeClient = new EdgeHttpClient(edgeUrl);\n const edgeIdentity = createEdgeIdentity(client);\n edgeClient.setIdentity(edgeIdentity);\n const response = await edgeClient.uploadFunction({ spaceId, functionId }, { name, version, script: source });\n\n log('Uploaded', {\n functionId,\n source,\n name,\n identityKey: edgeIdentity.identityKey,\n response,\n });\n\n return response;\n};\n\nexport const getUserFunctionUrlInMetadata = (meta: ObjectMeta) => {\n return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;\n};\n\nexport const setUserFunctionUrlInMetadata = (meta: ObjectMeta, functionUrl: string) => {\n const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);\n if (key) {\n if (key.id !== functionUrl) {\n throw new Error('Metadata mismatch');\n }\n } else {\n meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionUrl });\n }\n};\n\nexport const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {\n const baseUrl = new URL('functions/', edgeUrl);\n\n // Leading slashes cause the URL to be treated as an absolute path.\n const relativeUrl = functionUrl.replace(/^\\//, '');\n const url = new URL(`./${relativeUrl}`, baseUrl.toString());\n options.spaceId && url.searchParams.set('spaceId', options.spaceId);\n options.subjectId && url.searchParams.set('subjectId', options.subjectId);\n url.protocol = isSecure(url.protocol) ? 'https' : 'http';\n return url.toString();\n};\n\nconst isSecure = (protocol: string) => {\n return protocol === 'https:' || protocol === 'wss:';\n};\n\nconst createEdgeIdentity = (client: Client): EdgeIdentity => {\n const identity = client.halo.identity.get();\n const device = client.halo.device;\n if (!identity || !device) {\n throw new Error('Identity not available');\n }\n return {\n identityKey: identity.identityKey.toHex(),\n peerKey: device.deviceKey.toHex(),\n presentCredentials: async ({ challenge }) => {\n const identityService = client.services.services.IdentityService!;\n const authCredential = await identityService.createAuthCredential();\n return identityService.signPresentation({\n presentation: { credentials: [authCredential] },\n nonce: challenge,\n });\n },\n };\n};\n\nexport const incrementSemverPatch = (version: string): string => {\n const [major, minor, patch] = version.split('.');\n const patchNum = Number(patch);\n invariant(!Number.isNaN(patchNum), 'Unexpected function version format.');\n return [major, minor, String(patchNum + 1)].join('.');\n};\n\nexport const publicKeyToDid = (key: PublicKey): DID => {\n return `did:key:${key.toHex()}`;\n};\n\nexport type InvocationOptions = {\n spaceId?: SpaceId;\n subjectId?: string;\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AC7C,QAAAA;AAC7B,SAAO,WAAWA,IAAIsB,MAAK,CAAA;AAC7B;",
|
|
6
|
+
"names": ["EdgeHttpClient", "invariant", "log", "FUNCTIONS_META_KEY", "FUNCTIONS_PRESET_META_KEY", "uploadWorkerFunction", "client", "name", "version", "source", "spaceId", "functionId", "edgeUrl", "config", "values", "runtime", "services", "edge", "url", "edgeClient", "edgeIdentity", "createEdgeIdentity", "setIdentity", "response", "uploadFunction", "script", "identityKey", "getUserFunctionUrlInMetadata", "meta", "keys", "find", "key", "id", "setUserFunctionUrlInMetadata", "functionUrl", "Error", "push", "getInvocationUrl", "options", "baseUrl", "URL", "relativeUrl", "replace", "toString", "searchParams", "set", "subjectId", "protocol", "isSecure", "identity", "halo", "get", "device", "toHex", "peerKey", "deviceKey", "presentCredentials", "challenge", "identityService", "IdentityService", "authCredential", "createAuthCredential", "signPresentation", "presentation", "credentials", "nonce", "incrementSemverPatch", "major", "minor", "patch", "split", "patchNum", "Number", "isNaN", "String", "join", "publicKeyToDid"]
|
|
7
7
|
}
|
|
@@ -7,13 +7,13 @@ import {
|
|
|
7
7
|
publicKeyToDid,
|
|
8
8
|
setUserFunctionUrlInMetadata,
|
|
9
9
|
uploadWorkerFunction
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-YJEIETRB.mjs";
|
|
11
11
|
import {
|
|
12
12
|
FunctionRegistry,
|
|
13
13
|
TriggerRegistry,
|
|
14
14
|
createSubscriptionTrigger,
|
|
15
15
|
createTimerTrigger
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-WI2RVE6E.mjs";
|
|
17
17
|
import {
|
|
18
18
|
FUNCTION_TYPES,
|
|
19
19
|
FunctionDef,
|
|
@@ -21,17 +21,36 @@ import {
|
|
|
21
21
|
FunctionTrigger,
|
|
22
22
|
FunctionTriggerSchema,
|
|
23
23
|
FunctionType,
|
|
24
|
+
InvocationTrace,
|
|
24
25
|
ScriptType,
|
|
26
|
+
TraceEvent,
|
|
27
|
+
TraceEventException,
|
|
28
|
+
TraceEventLog,
|
|
25
29
|
TriggerKind,
|
|
26
30
|
TriggerSchema
|
|
27
|
-
} from "./chunk-
|
|
31
|
+
} from "./chunk-6XYG2TNO.mjs";
|
|
28
32
|
import "./chunk-XRCXIG74.mjs";
|
|
29
33
|
|
|
30
34
|
// packages/core/functions/src/handler.ts
|
|
35
|
+
import { Schema as S } from "@effect/schema";
|
|
31
36
|
import { PublicKey } from "@dxos/client";
|
|
32
37
|
import { log } from "@dxos/log";
|
|
33
|
-
import {
|
|
38
|
+
import { isNonNullable } from "@dxos/util";
|
|
34
39
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
|
|
40
|
+
var defineFunction = (params) => {
|
|
41
|
+
if (!S.isSchema(params.inputSchema)) {
|
|
42
|
+
throw new Error("Input schema must be a valid schema");
|
|
43
|
+
}
|
|
44
|
+
if (typeof params.handler !== "function") {
|
|
45
|
+
throw new Error("Handler must be a function");
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
description: params.description,
|
|
49
|
+
inputSchema: params.inputSchema,
|
|
50
|
+
outputSchema: params.outputSchema ?? S.Any,
|
|
51
|
+
handler: params.handler
|
|
52
|
+
};
|
|
53
|
+
};
|
|
35
54
|
var subscriptionHandler = (handler, types) => {
|
|
36
55
|
return async ({ event: { data }, context, response, ...rest }) => {
|
|
37
56
|
const { client } = context;
|
|
@@ -39,20 +58,20 @@ var subscriptionHandler = (handler, types) => {
|
|
|
39
58
|
if (!space) {
|
|
40
59
|
log.error("Invalid space", void 0, {
|
|
41
60
|
F: __dxlog_file,
|
|
42
|
-
L:
|
|
61
|
+
L: 180,
|
|
43
62
|
S: void 0,
|
|
44
63
|
C: (f, a) => f(...a)
|
|
45
64
|
});
|
|
46
65
|
return response.status(500);
|
|
47
66
|
}
|
|
48
67
|
registerTypes(space, types);
|
|
49
|
-
const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(
|
|
68
|
+
const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(isNonNullable) : [];
|
|
50
69
|
if (!!data.spaceKey && !space) {
|
|
51
70
|
log.warn("invalid space", {
|
|
52
71
|
data
|
|
53
72
|
}, {
|
|
54
73
|
F: __dxlog_file,
|
|
55
|
-
L:
|
|
74
|
+
L: 192,
|
|
56
75
|
S: void 0,
|
|
57
76
|
C: (f, a) => f(...a)
|
|
58
77
|
});
|
|
@@ -62,7 +81,7 @@ var subscriptionHandler = (handler, types) => {
|
|
|
62
81
|
objects: objects?.length
|
|
63
82
|
}, {
|
|
64
83
|
F: __dxlog_file,
|
|
65
|
-
L:
|
|
84
|
+
L: 194,
|
|
66
85
|
S: void 0,
|
|
67
86
|
C: (f, a) => f(...a)
|
|
68
87
|
});
|
|
@@ -100,12 +119,17 @@ export {
|
|
|
100
119
|
FunctionTrigger,
|
|
101
120
|
FunctionTriggerSchema,
|
|
102
121
|
FunctionType,
|
|
122
|
+
InvocationTrace,
|
|
103
123
|
ScriptType,
|
|
124
|
+
TraceEvent,
|
|
125
|
+
TraceEventException,
|
|
126
|
+
TraceEventLog,
|
|
104
127
|
TriggerKind,
|
|
105
128
|
TriggerRegistry,
|
|
106
129
|
TriggerSchema,
|
|
107
130
|
createSubscriptionTrigger,
|
|
108
131
|
createTimerTrigger,
|
|
132
|
+
defineFunction,
|
|
109
133
|
getInvocationUrl,
|
|
110
134
|
getUserFunctionUrlInMetadata,
|
|
111
135
|
incrementSemverPatch,
|