@dxos/functions 0.8.4-main.84f28bd → 0.8.4-main.8baae0fced
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/LICENSE +102 -5
- package/README.md +5 -7
- package/dist/lib/neutral/index.mjs +602 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/index.d.ts +3 -6
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
- package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
- package/dist/types/src/protocol/index.d.ts +2 -0
- package/dist/types/src/protocol/index.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +24 -0
- package/dist/types/src/protocol/protocol.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.test.d.ts +2 -0
- package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
- package/dist/types/src/sdk.d.ts +10 -0
- package/dist/types/src/sdk.d.ts.map +1 -0
- package/dist/types/src/services/credentials.d.ts +24 -27
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +18 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +3 -8
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +1 -17
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +4 -14
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/types/url.d.ts +13 -0
- package/dist/types/src/types/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +29 -69
- package/src/index.ts +3 -8
- package/src/protocol/functions-ai-http-client.test.ts +105 -0
- package/src/protocol/functions-ai-http-client.ts +141 -0
- package/src/{executor → protocol}/index.ts +1 -1
- package/src/protocol/protocol.test.ts +58 -0
- package/src/protocol/protocol.ts +455 -0
- package/src/sdk.ts +31 -0
- package/src/services/credentials.ts +95 -35
- package/src/services/function-invocation-service.ts +36 -0
- package/src/services/index.ts +3 -8
- package/src/services/queues.ts +1 -38
- package/src/services/tracing.ts +4 -23
- package/src/{testing → types}/index.ts +1 -1
- package/src/types/url.ts +32 -0
- package/dist/lib/browser/bundler/index.mjs +0 -247
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
- package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -69
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -492
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs +0 -79
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -249
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -71
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -493
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -80
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -50
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/dist/types/src/edge/functions.d.ts +0 -16
- package/dist/types/src/edge/functions.d.ts.map +0 -1
- package/dist/types/src/edge/index.d.ts +0 -2
- package/dist/types/src/edge/index.d.ts.map +0 -1
- package/dist/types/src/executor/executor.d.ts +0 -8
- package/dist/types/src/executor/executor.d.ts.map +0 -1
- package/dist/types/src/executor/index.d.ts +0 -2
- package/dist/types/src/executor/index.d.ts.map +0 -1
- package/dist/types/src/handler.d.ts +0 -61
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -38
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/ai.d.ts +0 -12
- package/dist/types/src/services/ai.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -15
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/event-logger.d.ts +0 -37
- package/dist/types/src/services/event-logger.d.ts.map +0 -1
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/dist/types/src/services/service-container.d.ts +0 -46
- package/dist/types/src/services/service-container.d.ts.map +0 -1
- package/dist/types/src/services/tool-resolver.d.ts +0 -14
- package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -2
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -5
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -71
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -124
- package/dist/types/src/trace.d.ts.map +0 -1
- package/dist/types/src/translations.d.ts +0 -12
- package/dist/types/src/translations.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -411
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/url.d.ts +0 -17
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -59
- package/src/bundler/bundler.ts +0 -292
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -64
- package/src/edge/index.ts +0 -9
- package/src/executor/executor.ts +0 -54
- package/src/handler.ts +0 -113
- package/src/schema.ts +0 -57
- package/src/services/ai.ts +0 -32
- package/src/services/database.ts +0 -50
- package/src/services/event-logger.ts +0 -87
- package/src/services/function-call-service.ts +0 -64
- package/src/services/service-container.ts +0 -127
- package/src/services/tool-resolver.ts +0 -31
- package/src/testing/logger.ts +0 -16
- package/src/testing/services.ts +0 -174
- package/src/trace.ts +0 -180
- package/src/translations.ts +0 -20
- package/src/types.ts +0 -211
- package/src/url.ts +0 -52
package/src/services/queues.ts
CHANGED
|
@@ -2,41 +2,4 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
|
|
8
|
-
|
|
9
|
-
export class QueueService extends Context.Tag('QueueService')<
|
|
10
|
-
QueueService,
|
|
11
|
-
{
|
|
12
|
-
/**
|
|
13
|
-
* API to access the queues.
|
|
14
|
-
*/
|
|
15
|
-
readonly queues: QueueAPI;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The queue that is used to store the context of the current research.
|
|
19
|
-
*/
|
|
20
|
-
// TODO(dmaretskyi): Is this really part of the queue service?
|
|
21
|
-
readonly contextQueue: Queue | undefined;
|
|
22
|
-
}
|
|
23
|
-
>() {
|
|
24
|
-
static notAvailable = Layer.succeed(QueueService, {
|
|
25
|
-
queues: {
|
|
26
|
-
get(dxn) {
|
|
27
|
-
throw new Error('Queues not available');
|
|
28
|
-
},
|
|
29
|
-
create() {
|
|
30
|
-
throw new Error('Queues not available');
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
contextQueue: undefined,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
static make = (queues: QueueFactory, contextQueue: Queue | undefined): Context.Tag.Service<QueueService> => {
|
|
37
|
-
return {
|
|
38
|
-
queues,
|
|
39
|
-
contextQueue,
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
}
|
|
5
|
+
export { QueueService, feedServiceFromQueueServiceLayer } from '@dxos/echo-db';
|
package/src/services/tracing.ts
CHANGED
|
@@ -2,26 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export class TracingService extends Context.Tag('TracingService')<
|
|
10
|
-
TracingService,
|
|
11
|
-
{
|
|
12
|
-
/**
|
|
13
|
-
* Write an event to the tracing queue.
|
|
14
|
-
* @param event - The event to write. Must be an a typed object.
|
|
15
|
-
*/
|
|
16
|
-
write(event: AnyEchoObject): void;
|
|
17
|
-
}
|
|
18
|
-
>() {
|
|
19
|
-
static noop: Context.Tag.Service<TracingService> = { write: () => {} };
|
|
20
|
-
|
|
21
|
-
static console: Context.Tag.Service<TracingService> = {
|
|
22
|
-
write: (event) => {
|
|
23
|
-
// eslint-disable-next-line no-console
|
|
24
|
-
console.log(event);
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
}
|
|
5
|
+
/**
|
|
6
|
+
* Goes into {@link Message['properties']}
|
|
7
|
+
*/
|
|
8
|
+
export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
|
package/src/types/url.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Obj } from '@dxos/echo';
|
|
6
|
+
|
|
7
|
+
// TODO: use URL scheme for source?
|
|
8
|
+
export const FUNCTIONS_META_KEY = 'org.dxos.service.function';
|
|
9
|
+
|
|
10
|
+
export const FUNCTIONS_PRESET_META_KEY = 'org.dxos.service.function-preset';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* NOTE: functionId is backend ID, not ECHO object id.
|
|
14
|
+
*/
|
|
15
|
+
export const getUserFunctionIdInMetadata = (meta: Obj.ReadonlyMeta) => {
|
|
16
|
+
return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* NOTE: functionId is backend ID, not ECHO object id.
|
|
21
|
+
* Must be called inside Obj.update() since it mutates the meta.
|
|
22
|
+
*/
|
|
23
|
+
export const setUserFunctionIdInMetadata = (meta: Obj.Meta, functionId: string) => {
|
|
24
|
+
const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);
|
|
25
|
+
if (key) {
|
|
26
|
+
if (key.id !== functionId) {
|
|
27
|
+
throw new Error('Metadata mismatch');
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionId });
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
// src/bundler/bundler.ts
|
|
2
|
-
import { FetchHttpClient, HttpClient } from "@effect/platform";
|
|
3
|
-
import { Duration, Effect, pipe, Schedule } from "effect";
|
|
4
|
-
import { build, initialize } from "esbuild-wasm";
|
|
5
|
-
import { subtleCrypto } from "@dxos/crypto";
|
|
6
|
-
import { runAndForwardErrors } from "@dxos/effect";
|
|
7
|
-
import { invariant } from "@dxos/invariant";
|
|
8
|
-
import { log } from "@dxos/log";
|
|
9
|
-
var __dxlog_file = "/__w/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: 73,
|
|
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: 137,
|
|
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: 150,
|
|
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 MAX_RETRIES = 5;
|
|
208
|
-
var INITIAL_DELAY = 1e3;
|
|
209
|
-
var httpPlugin = {
|
|
210
|
-
name: "http",
|
|
211
|
-
setup: (build2) => {
|
|
212
|
-
build2.onResolve({
|
|
213
|
-
filter: /^https?:\/\//
|
|
214
|
-
}, (args) => ({
|
|
215
|
-
path: args.path,
|
|
216
|
-
namespace: "http-url"
|
|
217
|
-
}));
|
|
218
|
-
build2.onResolve({
|
|
219
|
-
filter: /.*/,
|
|
220
|
-
namespace: "http-url"
|
|
221
|
-
}, (args) => ({
|
|
222
|
-
path: new URL(args.path, args.importer).toString(),
|
|
223
|
-
namespace: "http-url"
|
|
224
|
-
}));
|
|
225
|
-
build2.onLoad({
|
|
226
|
-
filter: /.*/,
|
|
227
|
-
namespace: "http-url"
|
|
228
|
-
}, async (args) => {
|
|
229
|
-
return Effect.gen(function* () {
|
|
230
|
-
const response = yield* HttpClient.get(args.path);
|
|
231
|
-
if (response.status !== 200) {
|
|
232
|
-
throw new Error(`failed to fetch: ${response.status}`);
|
|
233
|
-
}
|
|
234
|
-
const text = yield* response.text;
|
|
235
|
-
return {
|
|
236
|
-
contents: text,
|
|
237
|
-
loader: "jsx"
|
|
238
|
-
};
|
|
239
|
-
}).pipe(Effect.retry(pipe(Schedule.exponential(Duration.millis(INITIAL_DELAY)), Schedule.jittered, Schedule.intersect(Schedule.recurs(MAX_RETRIES - 1)))), Effect.provide(FetchHttpClient.layer), runAndForwardErrors);
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
export {
|
|
244
|
-
Bundler,
|
|
245
|
-
initializeBundler
|
|
246
|
-
};
|
|
247
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/bundler/bundler.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { FetchHttpClient, HttpClient } from '@effect/platform';\nimport { Duration, Effect, pipe, Schedule } from 'effect';\nimport { type BuildOptions, type Loader, build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';\n\nimport { subtleCrypto } from '@dxos/crypto';\nimport { runAndForwardErrors } from '@dxos/effect';\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 defaultImportName: string;\n namedImports: string[];\n wildcardImportName: string;\n moduleIdentifier: string;\n quotes: string;\n }[] {\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 MAX_RETRIES = 5;\nconst INITIAL_DELAY = 1_000;\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 return Effect.gen(function* () {\n const response = yield* HttpClient.get(args.path);\n if (response.status !== 200) {\n throw new Error(`failed to fetch: ${response.status}`);\n }\n\n const text = yield* response.text;\n return { contents: text, loader: 'jsx' as Loader };\n }).pipe(\n Effect.retry(\n pipe(\n Schedule.exponential(Duration.millis(INITIAL_DELAY)),\n Schedule.jittered,\n Schedule.intersect(Schedule.recurs(MAX_RETRIES - 1)),\n ),\n ),\n Effect.provide(FetchHttpClient.layer),\n runAndForwardErrors,\n );\n });\n },\n};\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,iBAAiBC,kBAAkB;AAC5C,SAASC,UAAUC,QAAQC,MAAMC,gBAAgB;AACjD,SAAyCC,OAAOC,kBAAiD;AAEjG,SAASC,oBAAoB;AAC7B,SAASC,2BAA2B;AACpC,SAASC,iBAAiB;AAC1B,SAASC,WAAW;;AAoCpB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AAoBH,SAASE,MAAM,EAAE;QACvD;AAEA,cAAMxC,OAAO,OAAOsC,SAAStC;AAC7B,eAAO;UAAEd,UAAUc;UAAMb,QAAQ;QAAgB;MACnD,CAAA,EAAGhD,KACDD,OAAOwG,MACLvG,KACEC,SAASuG,YAAY1G,SAAS2G,OAAOZ,aAAAA,CAAAA,GACrC5F,SAASyG,UACTzG,SAAS0G,UAAU1G,SAAS2G,OAAOhB,cAAc,CAAA,CAAA,CAAA,CAAA,GAGrD7F,OAAO8G,QAAQjH,gBAAgBkH,KAAK,GACpCzG,mBAAAA;IAEJ,CAAA;EACF;AACF;",
|
|
6
|
-
"names": ["FetchHttpClient", "HttpClient", "Duration", "Effect", "pipe", "Schedule", "build", "initialize", "subtleCrypto", "runAndForwardErrors", "invariant", "log", "initialized", "initializeBundler", "options", "wasmURL", "wasmUrl", "Bundler", "_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", "MAX_RETRIES", "INITIAL_DELAY", "args", "URL", "importer", "toString", "gen", "response", "get", "status", "Error", "retry", "exponential", "millis", "jittered", "intersect", "recurs", "provide", "layer"]
|
|
7
|
-
}
|