@dxos/functions 0.8.1 → 0.8.2-main.2f9c567
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 +0 -3
- package/dist/lib/browser/bundler/index.mjs.map +1 -1
- package/dist/lib/browser/edge/index.mjs +63 -7
- package/dist/lib/browser/edge/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +368 -98
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/bundler/index.cjs +0 -1
- package/dist/lib/node/bundler/index.cjs.map +1 -1
- package/dist/lib/node/edge/index.cjs +65 -5
- package/dist/lib/node/edge/index.cjs.map +4 -4
- package/dist/lib/node/index.cjs +383 -93
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/bundler/index.mjs +0 -1
- package/dist/lib/node-esm/bundler/index.mjs.map +1 -1
- package/dist/lib/node-esm/edge/index.mjs +64 -6
- package/dist/lib/node-esm/edge/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +368 -96
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/edge/index.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +15 -63
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +57 -0
- package/dist/types/src/schema.d.ts.map +1 -0
- package/dist/types/src/trace.d.ts +148 -0
- package/dist/types/src/trace.d.ts.map +1 -0
- package/dist/types/src/{types/types.d.ts → types.d.ts} +191 -49
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/{types/url.d.ts → url.d.ts} +6 -0
- package/dist/types/src/url.d.ts.map +1 -0
- package/package.json +20 -35
- package/src/edge/index.ts +4 -0
- package/src/handler.ts +17 -120
- package/src/index.ts +4 -5
- package/src/schema.ts +53 -0
- package/src/{types/trace.ts → trace.ts} +33 -31
- package/src/translations.ts +1 -1
- package/src/types.ts +214 -0
- package/src/{types/url.ts → url.ts} +5 -0
- package/dist/lib/browser/chunk-HI7YZO2K.mjs +0 -482
- package/dist/lib/browser/chunk-HI7YZO2K.mjs.map +0 -7
- package/dist/lib/browser/chunk-LT4LR4VU.mjs +0 -72
- package/dist/lib/browser/chunk-LT4LR4VU.mjs.map +0 -7
- package/dist/lib/browser/chunk-RVSG6WTL.mjs +0 -358
- package/dist/lib/browser/chunk-RVSG6WTL.mjs.map +0 -7
- package/dist/lib/browser/chunk-XRCXIG74.mjs +0 -12
- package/dist/lib/browser/chunk-XRCXIG74.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -670
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/browser/types/index.mjs +0 -49
- package/dist/lib/browser/types/index.mjs.map +0 -7
- package/dist/lib/node/chunk-DSUGRAAL.cjs +0 -392
- package/dist/lib/node/chunk-DSUGRAAL.cjs.map +0 -7
- package/dist/lib/node/chunk-JEQ2X3Z6.cjs +0 -34
- package/dist/lib/node/chunk-JEQ2X3Z6.cjs.map +0 -7
- package/dist/lib/node/chunk-NXZNXVT3.cjs +0 -94
- package/dist/lib/node/chunk-NXZNXVT3.cjs.map +0 -7
- package/dist/lib/node/chunk-RXMCVAMJ.cjs +0 -496
- package/dist/lib/node/chunk-RXMCVAMJ.cjs.map +0 -7
- package/dist/lib/node/testing/index.cjs +0 -687
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node/types/index.cjs +0 -70
- package/dist/lib/node/types/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs +0 -12
- package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HBD2FZXO.mjs +0 -358
- package/dist/lib/node-esm/chunk-HBD2FZXO.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-O2SXVYU5.mjs +0 -72
- package/dist/lib/node-esm/chunk-O2SXVYU5.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-SQSJO5HI.mjs +0 -482
- package/dist/lib/node-esm/chunk-SQSJO5HI.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -670
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/types/index.mjs +0 -49
- package/dist/lib/node-esm/types/index.mjs.map +0 -7
- package/dist/types/src/browser/index.d.ts +0 -2
- package/dist/types/src/browser/index.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.d.ts +0 -25
- package/dist/types/src/function/function-registry.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.test.d.ts +0 -2
- package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
- package/dist/types/src/function/index.d.ts +0 -2
- package/dist/types/src/function/index.d.ts.map +0 -1
- package/dist/types/src/runtime/dev-server.d.ts +0 -52
- package/dist/types/src/runtime/dev-server.d.ts.map +0 -1
- package/dist/types/src/runtime/dev-server.test.d.ts +0 -2
- package/dist/types/src/runtime/dev-server.test.d.ts.map +0 -1
- package/dist/types/src/runtime/index.d.ts +0 -3
- package/dist/types/src/runtime/index.d.ts.map +0 -1
- package/dist/types/src/runtime/scheduler.d.ts +0 -34
- package/dist/types/src/runtime/scheduler.d.ts.map +0 -1
- package/dist/types/src/runtime/scheduler.test.d.ts +0 -2
- package/dist/types/src/runtime/scheduler.test.d.ts.map +0 -1
- package/dist/types/src/testing/functions-integration.test.d.ts +0 -2
- package/dist/types/src/testing/functions-integration.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -5
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/manifest.d.ts +0 -3
- package/dist/types/src/testing/manifest.d.ts.map +0 -1
- package/dist/types/src/testing/plugin-init.d.ts +0 -6
- package/dist/types/src/testing/plugin-init.d.ts.map +0 -1
- package/dist/types/src/testing/setup.d.ts +0 -15
- package/dist/types/src/testing/setup.d.ts.map +0 -1
- package/dist/types/src/testing/test/handler.d.ts +0 -4
- package/dist/types/src/testing/test/handler.d.ts.map +0 -1
- package/dist/types/src/testing/test/index.d.ts +0 -3
- package/dist/types/src/testing/test/index.d.ts.map +0 -1
- package/dist/types/src/testing/types.d.ts +0 -10
- package/dist/types/src/testing/types.d.ts.map +0 -1
- package/dist/types/src/testing/util.d.ts +0 -5
- package/dist/types/src/testing/util.d.ts.map +0 -1
- package/dist/types/src/trigger/index.d.ts +0 -3
- package/dist/types/src/trigger/index.d.ts.map +0 -1
- package/dist/types/src/trigger/trigger-registry.d.ts +0 -38
- package/dist/types/src/trigger/trigger-registry.d.ts.map +0 -1
- package/dist/types/src/trigger/trigger-registry.test.d.ts +0 -2
- package/dist/types/src/trigger/trigger-registry.test.d.ts.map +0 -1
- package/dist/types/src/trigger/type/index.d.ts +0 -3
- package/dist/types/src/trigger/type/index.d.ts.map +0 -1
- package/dist/types/src/trigger/type/subscription-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +0 -1
- package/dist/types/src/trigger/type/timer-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/timer-trigger.d.ts.map +0 -1
- package/dist/types/src/trigger/type/webhook-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +0 -1
- package/dist/types/src/types/index.d.ts +0 -5
- package/dist/types/src/types/index.d.ts.map +0 -1
- package/dist/types/src/types/schema.d.ts +0 -53
- package/dist/types/src/types/schema.d.ts.map +0 -1
- package/dist/types/src/types/trace.d.ts +0 -146
- package/dist/types/src/types/trace.d.ts.map +0 -1
- package/dist/types/src/types/types.d.ts.map +0 -1
- package/dist/types/src/types/url.d.ts.map +0 -1
- package/dist/types/tools/schema.d.ts +0 -2
- package/dist/types/tools/schema.d.ts.map +0 -1
- package/schema/functions.json +0 -211
- package/src/browser/index.ts +0 -5
- package/src/function/function-registry.test.ts +0 -118
- package/src/function/function-registry.ts +0 -104
- package/src/function/index.ts +0 -5
- package/src/runtime/dev-server.test.ts +0 -79
- package/src/runtime/dev-server.ts +0 -240
- package/src/runtime/index.ts +0 -6
- package/src/runtime/scheduler.test.ts +0 -152
- package/src/runtime/scheduler.ts +0 -170
- package/src/testing/functions-integration.test.ts +0 -65
- package/src/testing/index.ts +0 -8
- package/src/testing/manifest.ts +0 -15
- package/src/testing/plugin-init.ts +0 -20
- package/src/testing/setup.ts +0 -109
- package/src/testing/test/handler.ts +0 -15
- package/src/testing/test/index.ts +0 -7
- package/src/testing/types.ts +0 -9
- package/src/testing/util.ts +0 -26
- package/src/trigger/index.ts +0 -6
- package/src/trigger/trigger-registry.test.ts +0 -278
- package/src/trigger/trigger-registry.ts +0 -218
- package/src/trigger/type/index.ts +0 -7
- package/src/trigger/type/subscription-trigger.ts +0 -84
- package/src/trigger/type/timer-trigger.ts +0 -48
- package/src/trigger/type/webhook-trigger.ts +0 -48
- package/src/types/index.ts +0 -8
- package/src/types/schema.ts +0 -46
- package/src/types/types.ts +0 -163
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/bundler/bundler.ts"],
|
|
4
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' };\n });\n },\n};\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";AAKA,SAASA,OAAOC,kBAAiD;AAEjE,SAASC,oBAAoB;AAC7B,SAASC,iBAAiB;AAC1B,SAASC,WAAW;;AAoCpB,IAAIC;AACG,IAAMC,oBAAoB,OAAOC,YAAAA;AACtC,SAAOF,gBAAgBJ,WAAW;IAChCO,SAASD,QAAQE;EACnB,CAAA;AACF;AAKO,IAAMC,UAAN,MAAMA;EACXC,YAA6BC,UAA0B;SAA1BA,WAAAA;EAA2B;EAExD,MAAMC,OAAO,EAAEC,MAAMC,OAAM,GAA0C;AACnE,UAAM,EAAEC,kBAAkBC,iBAAiB,GAAGV,QAAAA,IAAY,KAAKK;AAE/D,UAAMM,eAAe,OAAOC,WAAAA;AAC1B,aAAO;QACLC,WAAWC,KAAKC,IAAG;QACnBC,YAAYR,SAASS,OAAOC,KAAK,MAAMvB,aAAawB,OAAO,WAAWF,OAAOC,KAAKV,MAAAA,CAAAA,CAAAA,IAAYY;QAC9F,GAAGR;MACL;IACF;AAEA,QAAI,KAAKP,SAASgB,aAAa,WAAW;AACxCzB,gBAAUE,aAAa,6BAAA;;;;;;;;;AACvB,YAAMA;IACR;AAEA,UAAMwB,UAAUd,SAASe,yBAAyBf,MAAAA,IAAU,CAAA;AAG5D,QAAI;AACF,YAAMI,SAAS,MAAMnB,MAAM;QACzB4B,UAAUrB,QAAQqB;QAClBG,YAAY;UAAC;UAAW;;QACxBC,UAAU;QACVC,OAAO;QACPC,aAAa;UAACpB,QAAQ;;QACtBD,QAAQ;QACRsB,QAAQ;QACRC,SAAS;UACP;YACEC,MAAM;YACNC,OAAO,CAACtC,WAAAA;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AAM4E,KAAKE,QAAQ,EAAEC,SAAQ;MAChDlD,WAAW;IACb,EAAA;AAIA3C,IAAAA,OAAM4C,OAAO;MAAEJ,QAAQ;MAAMG,WAAW;IAAW,GAAG,OAAO+C,SAAAA;AAC3D,YAAMI,WAAW,MAAMC,MAAML,KAAK5E,IAAI;AACtC,aAAO;QAAE+B,UAAU,MAAMiD,SAASnC,KAAI;QAAIb,QAAQ;MAAM;IAC1D,CAAA;EACF;AACF;",
|
|
6
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
|
}
|
|
@@ -1,10 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
// packages/core/functions/src/edge/functions.ts
|
|
2
|
+
import { createEdgeIdentity } from "@dxos/client/edge";
|
|
3
|
+
import { EdgeHttpClient } from "@dxos/edge-client";
|
|
4
|
+
import { invariant } from "@dxos/invariant";
|
|
5
|
+
import { log } from "@dxos/log";
|
|
6
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/edge/functions.ts";
|
|
7
|
+
var uploadWorkerFunction = async ({ client, spaceId, version, source, name, functionId }) => {
|
|
8
|
+
const edgeUrl = client.config.values.runtime?.services?.edge?.url;
|
|
9
|
+
invariant(edgeUrl, "Edge is not configured.", {
|
|
10
|
+
F: __dxlog_file,
|
|
11
|
+
L: 33,
|
|
12
|
+
S: void 0,
|
|
13
|
+
A: [
|
|
14
|
+
"edgeUrl",
|
|
15
|
+
"'Edge is not configured.'"
|
|
16
|
+
]
|
|
17
|
+
});
|
|
18
|
+
const edgeClient = new EdgeHttpClient(edgeUrl);
|
|
19
|
+
const edgeIdentity = createEdgeIdentity(client);
|
|
20
|
+
edgeClient.setIdentity(edgeIdentity);
|
|
21
|
+
const response = await edgeClient.uploadFunction({
|
|
22
|
+
spaceId,
|
|
23
|
+
functionId
|
|
24
|
+
}, {
|
|
25
|
+
name,
|
|
26
|
+
version,
|
|
27
|
+
script: source
|
|
28
|
+
});
|
|
29
|
+
log.info("Uploaded", {
|
|
30
|
+
identityKey: edgeIdentity.identityKey,
|
|
31
|
+
functionId,
|
|
32
|
+
name,
|
|
33
|
+
source: source.length,
|
|
34
|
+
response
|
|
35
|
+
}, {
|
|
36
|
+
F: __dxlog_file,
|
|
37
|
+
L: 40,
|
|
38
|
+
S: void 0,
|
|
39
|
+
C: (f, a) => f(...a)
|
|
40
|
+
});
|
|
41
|
+
return response;
|
|
42
|
+
};
|
|
43
|
+
var incrementSemverPatch = (version) => {
|
|
44
|
+
const [major, minor, patch] = version.split(".");
|
|
45
|
+
const patchNum = Number(patch);
|
|
46
|
+
invariant(!Number.isNaN(patchNum), `Unexpected function version format: ${version}`, {
|
|
47
|
+
F: __dxlog_file,
|
|
48
|
+
L: 54,
|
|
49
|
+
S: void 0,
|
|
50
|
+
A: [
|
|
51
|
+
"!Number.isNaN(patchNum)",
|
|
52
|
+
"`Unexpected function version format: ${version}`"
|
|
53
|
+
]
|
|
54
|
+
});
|
|
55
|
+
return [
|
|
56
|
+
major,
|
|
57
|
+
minor,
|
|
58
|
+
String(patchNum + 1)
|
|
59
|
+
].join(".");
|
|
60
|
+
};
|
|
61
|
+
var publicKeyToDid = (key) => {
|
|
62
|
+
return `did:key:${key.toHex()}`;
|
|
63
|
+
};
|
|
8
64
|
export {
|
|
9
65
|
incrementSemverPatch,
|
|
10
66
|
publicKeyToDid,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": [],
|
|
4
|
-
"sourcesContent": [],
|
|
5
|
-
"mappings": "",
|
|
6
|
-
"names": []
|
|
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 { createEdgeIdentity } from '@dxos/client/edge';\nimport { EdgeHttpClient } 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\nexport type UploadWorkerArgs = {\n client: Client;\n spaceId: SpaceId;\n source: string;\n version: string;\n name?: string;\n functionId?: string;\n};\n\nexport const uploadWorkerFunction = async ({\n client,\n spaceId,\n version,\n source,\n name,\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 // TODO(burdon): Edge service log.\n log.info('Uploaded', {\n identityKey: edgeIdentity.identityKey,\n functionId,\n name,\n source: source.length,\n response,\n });\n\n return response;\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: ${version}`);\n return [major, minor, String(patchNum + 1)].join('.');\n};\n\n// TODO(burdon): Factor out.\nexport const publicKeyToDid = (key: PublicKey): DID => {\n return `did:key:${key.toHex()}`;\n};\n"],
|
|
5
|
+
"mappings": ";AAOA,SAASA,0BAA0B;AACnC,SAASC,sBAAsB;AAC/B,SAASC,iBAAiB;AAE1B,SAASC,WAAW;;AAYb,IAAMC,uBAAuB,OAAO,EACzCC,QACAC,SACAC,SACAC,QACAC,MACAC,WAAU,MACO;AACjB,QAAMC,UAAUN,OAAOO,OAAOC,OAAOC,SAASC,UAAUC,MAAMC;AAC9Df,YAAUS,SAAS,2BAAA;;;;;;;;;AACnB,QAAMO,aAAa,IAAIjB,eAAeU,OAAAA;AACtC,QAAMQ,eAAenB,mBAAmBK,MAAAA;AACxCa,aAAWE,YAAYD,YAAAA;AACvB,QAAME,WAAW,MAAMH,WAAWI,eAAe;IAAEhB;IAASI;EAAW,GAAG;IAAED;IAAMF;IAASgB,QAAQf;EAAO,CAAA;AAG1GL,MAAIqB,KAAK,YAAY;IACnBC,aAAaN,aAAaM;IAC1Bf;IACAD;IACAD,QAAQA,OAAOkB;IACfL;EACF,GAAA;;;;;;AAEA,SAAOA;AACT;AAEO,IAAMM,uBAAuB,CAACpB,YAAAA;AACnC,QAAM,CAACqB,OAAOC,OAAOC,KAAAA,IAASvB,QAAQwB,MAAM,GAAA;AAC5C,QAAMC,WAAWC,OAAOH,KAAAA;AACxB5B,YAAU,CAAC+B,OAAOC,MAAMF,QAAAA,GAAW,uCAAuCzB,OAAAA,IAAS;;;;;;;;;AACnF,SAAO;IAACqB;IAAOC;IAAOM,OAAOH,WAAW,CAAA;IAAII,KAAK,GAAA;AACnD;AAGO,IAAMC,iBAAiB,CAACC,QAAAA;AAC7B,SAAO,WAAWA,IAAIC,MAAK,CAAA;AAC7B;",
|
|
6
|
+
"names": ["createEdgeIdentity", "EdgeHttpClient", "invariant", "log", "uploadWorkerFunction", "client", "spaceId", "version", "source", "name", "functionId", "edgeUrl", "config", "values", "runtime", "services", "edge", "url", "edgeClient", "edgeIdentity", "setIdentity", "response", "uploadFunction", "script", "info", "identityKey", "length", "incrementSemverPatch", "major", "minor", "patch", "split", "patchNum", "Number", "isNaN", "String", "join", "publicKeyToDid", "key", "toHex"]
|
|
7
7
|
}
|
|
@@ -1,46 +1,5 @@
|
|
|
1
|
-
import "@dxos/node-std/globals";
|
|
2
|
-
import {
|
|
3
|
-
incrementSemverPatch,
|
|
4
|
-
publicKeyToDid,
|
|
5
|
-
uploadWorkerFunction
|
|
6
|
-
} from "./chunk-LT4LR4VU.mjs";
|
|
7
|
-
import {
|
|
8
|
-
FunctionRegistry,
|
|
9
|
-
TriggerRegistry,
|
|
10
|
-
createSubscriptionTrigger,
|
|
11
|
-
createTimerTrigger
|
|
12
|
-
} from "./chunk-HI7YZO2K.mjs";
|
|
13
|
-
import {
|
|
14
|
-
FUNCTIONS_PRESET_META_KEY,
|
|
15
|
-
FUNCTION_TYPES,
|
|
16
|
-
FunctionDef,
|
|
17
|
-
FunctionManifestSchema,
|
|
18
|
-
FunctionTrigger,
|
|
19
|
-
FunctionTriggerSchema,
|
|
20
|
-
FunctionType,
|
|
21
|
-
InvocationOutcome,
|
|
22
|
-
InvocationTraceEndEvent,
|
|
23
|
-
InvocationTraceEventType,
|
|
24
|
-
InvocationTraceStartEvent,
|
|
25
|
-
ScriptType,
|
|
26
|
-
TraceEvent,
|
|
27
|
-
TraceEventException,
|
|
28
|
-
TraceEventLog,
|
|
29
|
-
TriggerKind,
|
|
30
|
-
TriggerSchema,
|
|
31
|
-
createInvocationSpans,
|
|
32
|
-
getInvocationUrl,
|
|
33
|
-
getUserFunctionUrlInMetadata,
|
|
34
|
-
setUserFunctionUrlInMetadata
|
|
35
|
-
} from "./chunk-RVSG6WTL.mjs";
|
|
36
|
-
import "./chunk-XRCXIG74.mjs";
|
|
37
|
-
|
|
38
1
|
// packages/core/functions/src/handler.ts
|
|
39
2
|
import { Schema as S } from "effect";
|
|
40
|
-
import { PublicKey } from "@dxos/client";
|
|
41
|
-
import { log } from "@dxos/log";
|
|
42
|
-
import { isNonNullable } from "@dxos/util";
|
|
43
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
|
|
44
3
|
var defineFunction = (params) => {
|
|
45
4
|
if (!S.isSchema(params.inputSchema)) {
|
|
46
5
|
throw new Error("Input schema must be a valid schema");
|
|
@@ -55,71 +14,384 @@ var defineFunction = (params) => {
|
|
|
55
14
|
handler: params.handler
|
|
56
15
|
};
|
|
57
16
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
17
|
+
|
|
18
|
+
// packages/core/functions/src/schema.ts
|
|
19
|
+
import { Schema } from "effect";
|
|
20
|
+
import { EchoObject, JsonSchemaType, LabelAnnotationId, Ref, TypedObject } from "@dxos/echo-schema";
|
|
21
|
+
import { DataType } from "@dxos/schema";
|
|
22
|
+
var ScriptType = Schema.Struct({
|
|
23
|
+
name: Schema.optional(Schema.String),
|
|
24
|
+
description: Schema.optional(Schema.String),
|
|
25
|
+
// TODO(burdon): Change to hash of deployed content.
|
|
26
|
+
// Whether source has changed since last deploy.
|
|
27
|
+
changed: Schema.optional(Schema.Boolean),
|
|
28
|
+
source: Ref(DataType.Text)
|
|
29
|
+
}).annotations({
|
|
30
|
+
[LabelAnnotationId]: "name"
|
|
31
|
+
}).pipe(EchoObject({
|
|
32
|
+
typename: "dxos.org/type/Script",
|
|
33
|
+
version: "0.1.0"
|
|
34
|
+
}));
|
|
35
|
+
var FunctionType = class extends TypedObject({
|
|
36
|
+
typename: "dxos.org/type/Function",
|
|
37
|
+
version: "0.1.0"
|
|
38
|
+
})({
|
|
39
|
+
// TODO(burdon): Rename to id/uri?
|
|
40
|
+
name: Schema.NonEmptyString,
|
|
41
|
+
version: Schema.String,
|
|
42
|
+
description: Schema.optional(Schema.String),
|
|
43
|
+
// Reference to a source script if it exists within ECHO.
|
|
44
|
+
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
45
|
+
source: Schema.optional(Ref(ScriptType)),
|
|
46
|
+
inputSchema: Schema.optional(JsonSchemaType),
|
|
47
|
+
outputSchema: Schema.optional(JsonSchemaType),
|
|
48
|
+
// Local binding to a function name.
|
|
49
|
+
binding: Schema.optional(Schema.String)
|
|
50
|
+
}) {
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// packages/core/functions/src/trace.ts
|
|
54
|
+
import { Schema as Schema2 } from "effect";
|
|
55
|
+
import { EchoObject as EchoObject2, Expando as Expando2, ObjectId, Ref as Ref3 } from "@dxos/echo-schema";
|
|
56
|
+
import { log } from "@dxos/log";
|
|
57
|
+
|
|
58
|
+
// packages/core/functions/src/types.ts
|
|
59
|
+
import { Schema as S2, SchemaAST } from "effect";
|
|
60
|
+
import { Expando, OptionsAnnotationId, TypedObject as TypedObject2, DXN, Ref as Ref2, RawObject } from "@dxos/echo-schema";
|
|
61
|
+
var TriggerKind;
|
|
62
|
+
(function(TriggerKind2) {
|
|
63
|
+
TriggerKind2["Timer"] = "timer";
|
|
64
|
+
TriggerKind2["Webhook"] = "webhook";
|
|
65
|
+
TriggerKind2["Subscription"] = "subscription";
|
|
66
|
+
TriggerKind2["Email"] = "email";
|
|
67
|
+
TriggerKind2["Queue"] = "queue";
|
|
68
|
+
})(TriggerKind || (TriggerKind = {}));
|
|
69
|
+
var typeLiteralAnnotations = {
|
|
70
|
+
[SchemaAST.TitleAnnotationId]: "Type"
|
|
71
|
+
};
|
|
72
|
+
var TimerTriggerSchema = S2.Struct({
|
|
73
|
+
type: S2.Literal("timer").annotations(typeLiteralAnnotations),
|
|
74
|
+
cron: S2.String.annotations({
|
|
75
|
+
[SchemaAST.TitleAnnotationId]: "Cron",
|
|
76
|
+
[SchemaAST.ExamplesAnnotationId]: [
|
|
77
|
+
"0 0 * * *"
|
|
78
|
+
]
|
|
79
|
+
})
|
|
80
|
+
}).pipe(S2.mutable);
|
|
81
|
+
var EmailTriggerSchema = S2.Struct({
|
|
82
|
+
type: S2.Literal("email").annotations(typeLiteralAnnotations)
|
|
83
|
+
}).pipe(S2.mutable);
|
|
84
|
+
var QueueTriggerSchema = S2.Struct({
|
|
85
|
+
type: S2.Literal("queue").annotations(typeLiteralAnnotations),
|
|
86
|
+
queue: DXN
|
|
87
|
+
}).pipe(S2.mutable);
|
|
88
|
+
var WebhookTriggerSchema = S2.Struct({
|
|
89
|
+
type: S2.Literal("webhook").annotations(typeLiteralAnnotations),
|
|
90
|
+
method: S2.optional(S2.String.annotations({
|
|
91
|
+
[SchemaAST.TitleAnnotationId]: "Method",
|
|
92
|
+
[OptionsAnnotationId]: [
|
|
93
|
+
"GET",
|
|
94
|
+
"POST"
|
|
95
|
+
]
|
|
96
|
+
})),
|
|
97
|
+
port: S2.optional(S2.Number.annotations({
|
|
98
|
+
[SchemaAST.TitleAnnotationId]: "Port"
|
|
99
|
+
}))
|
|
100
|
+
}).pipe(S2.mutable);
|
|
101
|
+
var QuerySchema = S2.Struct({
|
|
102
|
+
type: S2.optional(S2.String.annotations({
|
|
103
|
+
[SchemaAST.TitleAnnotationId]: "Type"
|
|
104
|
+
})),
|
|
105
|
+
props: S2.optional(S2.Record({
|
|
106
|
+
key: S2.String,
|
|
107
|
+
value: S2.Any
|
|
108
|
+
}))
|
|
109
|
+
}).annotations({
|
|
110
|
+
[SchemaAST.TitleAnnotationId]: "Query"
|
|
111
|
+
});
|
|
112
|
+
var SubscriptionTriggerSchema = S2.Struct({
|
|
113
|
+
type: S2.Literal("subscription").annotations(typeLiteralAnnotations),
|
|
114
|
+
// TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
|
|
115
|
+
filter: QuerySchema,
|
|
116
|
+
options: S2.optional(S2.Struct({
|
|
117
|
+
// Watch changes to object (not just creation).
|
|
118
|
+
deep: S2.optional(S2.Boolean.annotations({
|
|
119
|
+
[SchemaAST.TitleAnnotationId]: "Nested"
|
|
120
|
+
})),
|
|
121
|
+
// Debounce changes (delay in ms).
|
|
122
|
+
delay: S2.optional(S2.Number.annotations({
|
|
123
|
+
[SchemaAST.TitleAnnotationId]: "Delay"
|
|
124
|
+
}))
|
|
125
|
+
}).annotations({
|
|
126
|
+
[SchemaAST.TitleAnnotationId]: "Options"
|
|
127
|
+
}))
|
|
128
|
+
}).pipe(S2.mutable);
|
|
129
|
+
var TriggerSchema = S2.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
|
|
130
|
+
[SchemaAST.TitleAnnotationId]: "Trigger"
|
|
131
|
+
});
|
|
132
|
+
var EmailTriggerOutput = S2.mutable(S2.Struct({
|
|
133
|
+
from: S2.String,
|
|
134
|
+
to: S2.String,
|
|
135
|
+
subject: S2.String,
|
|
136
|
+
created: S2.String,
|
|
137
|
+
body: S2.String
|
|
138
|
+
}));
|
|
139
|
+
var WebhookTriggerOutput = S2.mutable(S2.Struct({
|
|
140
|
+
url: S2.String,
|
|
141
|
+
method: S2.Literal("GET", "POST"),
|
|
142
|
+
headers: S2.Record({
|
|
143
|
+
key: S2.String,
|
|
144
|
+
value: S2.String
|
|
145
|
+
}),
|
|
146
|
+
bodyText: S2.String
|
|
147
|
+
}));
|
|
148
|
+
var QueueTriggerOutput = S2.mutable(S2.Struct({
|
|
149
|
+
queue: DXN,
|
|
150
|
+
item: S2.Any,
|
|
151
|
+
cursor: S2.String
|
|
152
|
+
}));
|
|
153
|
+
var SubscriptionTriggerOutput = S2.mutable(S2.Struct({
|
|
154
|
+
type: S2.String,
|
|
155
|
+
changedObjectId: S2.String
|
|
156
|
+
}));
|
|
157
|
+
var TimerTriggerOutput = S2.mutable(S2.Struct({
|
|
158
|
+
tick: S2.Number
|
|
159
|
+
}));
|
|
160
|
+
var FunctionTriggerSchema = S2.Struct({
|
|
161
|
+
/**
|
|
162
|
+
* Function or workflow to invoke.
|
|
163
|
+
*/
|
|
164
|
+
// TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
|
|
165
|
+
function: S2.optional(Ref2(Expando).annotations({
|
|
166
|
+
[SchemaAST.TitleAnnotationId]: "Function"
|
|
167
|
+
})),
|
|
168
|
+
/**
|
|
169
|
+
* Only used for workflows.
|
|
170
|
+
* Specifies the input node in the circuit.
|
|
171
|
+
* @deprecated Remove and enforce a single input node in all compute graphs.
|
|
172
|
+
*/
|
|
173
|
+
inputNodeId: S2.optional(S2.String.annotations({
|
|
174
|
+
[SchemaAST.TitleAnnotationId]: "Input Node ID"
|
|
175
|
+
})),
|
|
176
|
+
enabled: S2.optional(S2.Boolean.annotations({
|
|
177
|
+
[SchemaAST.TitleAnnotationId]: "Enabled"
|
|
178
|
+
})),
|
|
179
|
+
spec: S2.optional(TriggerSchema),
|
|
180
|
+
/**
|
|
181
|
+
* Passed as the input data to the function.
|
|
182
|
+
* Must match the function's input schema.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* {
|
|
186
|
+
* item: '{{$.trigger.event}}',
|
|
187
|
+
* instructions: 'Summarize and perform entity-extraction'
|
|
188
|
+
* mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
|
|
189
|
+
* }
|
|
190
|
+
*/
|
|
191
|
+
input: S2.optional(S2.mutable(S2.Record({
|
|
192
|
+
key: S2.String,
|
|
193
|
+
value: S2.Any
|
|
194
|
+
})))
|
|
195
|
+
});
|
|
196
|
+
var FunctionTrigger = class extends TypedObject2({
|
|
197
|
+
typename: "dxos.org/type/FunctionTrigger",
|
|
198
|
+
version: "0.1.0"
|
|
199
|
+
})(FunctionTriggerSchema.fields) {
|
|
200
|
+
};
|
|
201
|
+
var FunctionManifestSchema = S2.Struct({
|
|
202
|
+
functions: S2.optional(S2.mutable(S2.Array(RawObject(FunctionType)))),
|
|
203
|
+
triggers: S2.optional(S2.mutable(S2.Array(RawObject(FunctionTrigger))))
|
|
204
|
+
});
|
|
205
|
+
var FUNCTION_TYPES = [
|
|
206
|
+
FunctionType,
|
|
207
|
+
FunctionTrigger
|
|
208
|
+
];
|
|
209
|
+
|
|
210
|
+
// packages/core/functions/src/trace.ts
|
|
211
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/trace.ts";
|
|
212
|
+
var InvocationOutcome;
|
|
213
|
+
(function(InvocationOutcome2) {
|
|
214
|
+
InvocationOutcome2["SUCCESS"] = "success";
|
|
215
|
+
InvocationOutcome2["FAILURE"] = "failure";
|
|
216
|
+
InvocationOutcome2["PENDING"] = "pending";
|
|
217
|
+
})(InvocationOutcome || (InvocationOutcome = {}));
|
|
218
|
+
var InvocationTraceEventType;
|
|
219
|
+
(function(InvocationTraceEventType2) {
|
|
220
|
+
InvocationTraceEventType2["START"] = "start";
|
|
221
|
+
InvocationTraceEventType2["END"] = "end";
|
|
222
|
+
})(InvocationTraceEventType || (InvocationTraceEventType = {}));
|
|
223
|
+
var TraceEventException = Schema2.Struct({
|
|
224
|
+
timestampMs: Schema2.Number,
|
|
225
|
+
message: Schema2.String,
|
|
226
|
+
name: Schema2.String,
|
|
227
|
+
stack: Schema2.optional(Schema2.String)
|
|
228
|
+
});
|
|
229
|
+
var InvocationTraceStartEvent = Schema2.Struct({
|
|
230
|
+
/**
|
|
231
|
+
* Queue message id.
|
|
232
|
+
*/
|
|
233
|
+
id: ObjectId,
|
|
234
|
+
type: Schema2.Literal("start"),
|
|
235
|
+
/**
|
|
236
|
+
* Invocation id, the same for invocation start and end events.
|
|
237
|
+
*/
|
|
238
|
+
invocationId: ObjectId,
|
|
239
|
+
/**
|
|
240
|
+
* Event generation time.
|
|
241
|
+
*/
|
|
242
|
+
timestampMs: Schema2.Number,
|
|
243
|
+
/**
|
|
244
|
+
* Data passed to function / workflow as an argument.
|
|
245
|
+
*/
|
|
246
|
+
// TODO(burdon): Input schema?
|
|
247
|
+
input: Schema2.Object,
|
|
248
|
+
/**
|
|
249
|
+
* Queue DXN for function/workflow invocation events.
|
|
250
|
+
*/
|
|
251
|
+
// TODO(burdon): Need reference type for queue. vs. string?
|
|
252
|
+
invocationTraceQueue: Ref3(Expando2),
|
|
253
|
+
/**
|
|
254
|
+
* DXN of the invoked function/workflow.
|
|
255
|
+
*/
|
|
256
|
+
invocationTarget: Ref3(Expando2),
|
|
257
|
+
/**
|
|
258
|
+
* Present for automatic invocations.
|
|
259
|
+
*/
|
|
260
|
+
trigger: Schema2.optional(Ref3(FunctionTrigger))
|
|
261
|
+
}).pipe(EchoObject2({
|
|
262
|
+
typename: "dxos.org/type/InvocationTraceStart",
|
|
263
|
+
version: "0.1.0"
|
|
264
|
+
}));
|
|
265
|
+
var InvocationTraceEndEvent = Schema2.Struct({
|
|
266
|
+
/**
|
|
267
|
+
* Trace event id.
|
|
268
|
+
*/
|
|
269
|
+
id: ObjectId,
|
|
270
|
+
type: Schema2.Literal("end"),
|
|
271
|
+
/**
|
|
272
|
+
* Invocation id, will be the same for invocation start and end.
|
|
273
|
+
*/
|
|
274
|
+
invocationId: ObjectId,
|
|
275
|
+
/**
|
|
276
|
+
* Event generation time.
|
|
277
|
+
*/
|
|
278
|
+
// TODO(burdon): Remove ms suffix.
|
|
279
|
+
timestampMs: Schema2.Number,
|
|
280
|
+
outcome: Schema2.Enums(InvocationOutcome),
|
|
281
|
+
exception: Schema2.optional(TraceEventException)
|
|
282
|
+
}).pipe(EchoObject2({
|
|
283
|
+
typename: "dxos.org/type/InvocationTraceEnd",
|
|
284
|
+
version: "0.1.0"
|
|
285
|
+
}));
|
|
286
|
+
var TraceEventLog = Schema2.Struct({
|
|
287
|
+
timestampMs: Schema2.Number,
|
|
288
|
+
level: Schema2.String,
|
|
289
|
+
message: Schema2.String,
|
|
290
|
+
context: Schema2.optional(Schema2.Object)
|
|
291
|
+
});
|
|
292
|
+
var TraceEvent = Schema2.Struct({
|
|
293
|
+
id: ObjectId,
|
|
294
|
+
// TODO(burdon): Need enum/numeric result (not string).
|
|
295
|
+
outcome: Schema2.String,
|
|
296
|
+
truncated: Schema2.Boolean,
|
|
297
|
+
/**
|
|
298
|
+
* Time when the event was persisted.
|
|
299
|
+
*/
|
|
300
|
+
ingestionTimestampMs: Schema2.Number,
|
|
301
|
+
logs: Schema2.Array(TraceEventLog),
|
|
302
|
+
exceptions: Schema2.Array(TraceEventException)
|
|
303
|
+
}).pipe(EchoObject2({
|
|
304
|
+
typename: "dxos.org/type/TraceEvent",
|
|
305
|
+
version: "0.1.0"
|
|
306
|
+
}));
|
|
307
|
+
var createInvocationSpans = (items) => {
|
|
308
|
+
if (!items) {
|
|
309
|
+
return [];
|
|
310
|
+
}
|
|
311
|
+
const eventsByInvocationId = /* @__PURE__ */ new Map();
|
|
312
|
+
for (const event of items) {
|
|
313
|
+
if (!("invocationId" in event)) {
|
|
314
|
+
continue;
|
|
70
315
|
}
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
316
|
+
const invocationId = event.invocationId;
|
|
317
|
+
const entry = eventsByInvocationId.get(invocationId) || {
|
|
318
|
+
start: void 0,
|
|
319
|
+
end: void 0
|
|
320
|
+
};
|
|
321
|
+
if (event.type === "start") {
|
|
322
|
+
entry.start = event;
|
|
323
|
+
} else if (event.type === "end") {
|
|
324
|
+
entry.end = event;
|
|
325
|
+
}
|
|
326
|
+
eventsByInvocationId.set(invocationId, entry);
|
|
327
|
+
}
|
|
328
|
+
const now = Date.now();
|
|
329
|
+
const result = [];
|
|
330
|
+
for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
|
|
331
|
+
if (!start) {
|
|
332
|
+
log.warn("found end event without matching start", {
|
|
333
|
+
invocationId
|
|
86
334
|
}, {
|
|
87
335
|
F: __dxlog_file,
|
|
88
|
-
L:
|
|
336
|
+
L: 160,
|
|
89
337
|
S: void 0,
|
|
90
338
|
C: (f, a) => f(...a)
|
|
91
339
|
});
|
|
340
|
+
continue;
|
|
92
341
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
342
|
+
const isInProgress = end === void 0;
|
|
343
|
+
result.push({
|
|
344
|
+
id: invocationId,
|
|
345
|
+
timestampMs: start.timestampMs,
|
|
346
|
+
durationMs: isInProgress ? now - start.timestampMs : end.timestampMs - start.timestampMs,
|
|
347
|
+
outcome: end?.outcome ?? "pending",
|
|
348
|
+
exception: end?.exception,
|
|
349
|
+
input: start.input,
|
|
350
|
+
invocationTraceQueue: start.invocationTraceQueue,
|
|
351
|
+
invocationTarget: start.invocationTarget,
|
|
352
|
+
trigger: start.trigger
|
|
104
353
|
});
|
|
105
|
-
}
|
|
354
|
+
}
|
|
355
|
+
return result;
|
|
106
356
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
357
|
+
|
|
358
|
+
// packages/core/functions/src/url.ts
|
|
359
|
+
var FUNCTIONS_META_KEY = "dxos.org/service/function";
|
|
360
|
+
var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
|
|
361
|
+
var isSecure = (protocol) => {
|
|
362
|
+
return protocol === "https:" || protocol === "wss:";
|
|
363
|
+
};
|
|
364
|
+
var getUserFunctionUrlInMetadata = (meta) => {
|
|
365
|
+
return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
|
|
366
|
+
};
|
|
367
|
+
var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
|
|
368
|
+
const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
|
|
369
|
+
if (key) {
|
|
370
|
+
if (key.id !== functionUrl) {
|
|
371
|
+
throw new Error("Metadata mismatch");
|
|
114
372
|
}
|
|
373
|
+
} else {
|
|
374
|
+
meta.keys.push({
|
|
375
|
+
source: FUNCTIONS_META_KEY,
|
|
376
|
+
id: functionUrl
|
|
377
|
+
});
|
|
115
378
|
}
|
|
116
379
|
};
|
|
380
|
+
var makeFunctionUrl = (spaceId, fn) => `/${spaceId}/${fn.functionId}`;
|
|
381
|
+
var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
382
|
+
const baseUrl = new URL("functions/", edgeUrl);
|
|
383
|
+
const relativeUrl = functionUrl.replace(/^\//, "");
|
|
384
|
+
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
385
|
+
options.spaceId && url.searchParams.set("spaceId", options.spaceId);
|
|
386
|
+
options.subjectId && url.searchParams.set("subjectId", options.subjectId);
|
|
387
|
+
url.protocol = isSecure(url.protocol) ? "https" : "http";
|
|
388
|
+
return url.toString();
|
|
389
|
+
};
|
|
117
390
|
export {
|
|
391
|
+
EmailTriggerOutput,
|
|
118
392
|
FUNCTIONS_PRESET_META_KEY,
|
|
119
393
|
FUNCTION_TYPES,
|
|
120
|
-
FunctionDef,
|
|
121
394
|
FunctionManifestSchema,
|
|
122
|
-
FunctionRegistry,
|
|
123
395
|
FunctionTrigger,
|
|
124
396
|
FunctionTriggerSchema,
|
|
125
397
|
FunctionType,
|
|
@@ -127,23 +399,21 @@ export {
|
|
|
127
399
|
InvocationTraceEndEvent,
|
|
128
400
|
InvocationTraceEventType,
|
|
129
401
|
InvocationTraceStartEvent,
|
|
402
|
+
QueueTriggerOutput,
|
|
130
403
|
ScriptType,
|
|
404
|
+
SubscriptionTriggerOutput,
|
|
405
|
+
TimerTriggerOutput,
|
|
131
406
|
TraceEvent,
|
|
132
407
|
TraceEventException,
|
|
133
408
|
TraceEventLog,
|
|
134
409
|
TriggerKind,
|
|
135
|
-
TriggerRegistry,
|
|
136
410
|
TriggerSchema,
|
|
411
|
+
WebhookTriggerOutput,
|
|
137
412
|
createInvocationSpans,
|
|
138
|
-
createSubscriptionTrigger,
|
|
139
|
-
createTimerTrigger,
|
|
140
413
|
defineFunction,
|
|
141
414
|
getInvocationUrl,
|
|
142
415
|
getUserFunctionUrlInMetadata,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
setUserFunctionUrlInMetadata,
|
|
146
|
-
subscriptionHandler,
|
|
147
|
-
uploadWorkerFunction
|
|
416
|
+
makeFunctionUrl,
|
|
417
|
+
setUserFunctionUrlInMetadata
|
|
148
418
|
};
|
|
149
419
|
//# sourceMappingURL=index.mjs.map
|