@cloudflare/vitest-pool-workers 0.2.2 → 0.2.4
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/package.json +4 -4
- package/dist/config/d1.d.ts +0 -7
- package/dist/config/index.cjs +0 -130
- package/dist/config/index.cjs.map +0 -6
- package/dist/config/index.d.ts +0 -21
- package/dist/pool/config.d.ts +0 -82
- package/dist/pool/helpers.d.ts +0 -5
- package/dist/pool/index.mjs +0 -1529
- package/dist/pool/index.mjs.map +0 -6
- package/dist/shared/d1.d.ts +0 -4
- package/dist/worker/index.mjs +0 -352
- package/dist/worker/index.mjs.map +0 -6
- package/dist/worker/lib/cloudflare/empty-internal.cjs +0 -27
- package/dist/worker/lib/cloudflare/empty-internal.cjs.map +0 -6
- package/dist/worker/lib/cloudflare/mock-agent.cjs +0 -2056
- package/dist/worker/lib/cloudflare/mock-agent.cjs.map +0 -6
- package/dist/worker/lib/cloudflare/test-internal.mjs +0 -739
- package/dist/worker/lib/cloudflare/test-internal.mjs.map +0 -6
- package/dist/worker/lib/cloudflare/test-runner.mjs +0 -222
- package/dist/worker/lib/cloudflare/test-runner.mjs.map +0 -6
- package/dist/worker/lib/cloudflare/test.mjs +0 -30
- package/dist/worker/lib/cloudflare/test.mjs.map +0 -6
- package/dist/worker/lib/debug.mjs +0 -9
- package/dist/worker/lib/debug.mjs.map +0 -6
- package/dist/worker/lib/mlly.mjs +0 -48
- package/dist/worker/lib/mlly.mjs.map +0 -6
- package/dist/worker/lib/node/console.mjs +0 -104
- package/dist/worker/lib/node/console.mjs.map +0 -6
- package/dist/worker/lib/node/dns.mjs +0 -6
- package/dist/worker/lib/node/dns.mjs.map +0 -6
- package/dist/worker/lib/node/fs/promises.mjs +0 -6
- package/dist/worker/lib/node/fs/promises.mjs.map +0 -6
- package/dist/worker/lib/node/fs.mjs +0 -25
- package/dist/worker/lib/node/fs.mjs.map +0 -6
- package/dist/worker/lib/node/http.cjs +0 -97
- package/dist/worker/lib/node/http.cjs.map +0 -6
- package/dist/worker/lib/node/module.mjs +0 -15
- package/dist/worker/lib/node/module.mjs.map +0 -6
- package/dist/worker/lib/node/net.cjs +0 -27
- package/dist/worker/lib/node/net.cjs.map +0 -6
- package/dist/worker/lib/node/perf_hooks.mjs +0 -6
- package/dist/worker/lib/node/perf_hooks.mjs.map +0 -6
- package/dist/worker/lib/node/querystring.cjs +0 -44
- package/dist/worker/lib/node/querystring.cjs.map +0 -6
- package/dist/worker/lib/node/timers.mjs +0 -6
- package/dist/worker/lib/node/timers.mjs.map +0 -6
- package/dist/worker/lib/node/tty.mjs +0 -8
- package/dist/worker/lib/node/tty.mjs.map +0 -6
- package/dist/worker/lib/node/url.mjs +0 -75
- package/dist/worker/lib/node/url.mjs.map +0 -6
- package/dist/worker/lib/node/vm.mjs +0 -17
- package/dist/worker/lib/node/vm.mjs.map +0 -6
- package/dist/worker/lib/tinypool.mjs +0 -6
- package/dist/worker/lib/tinypool.mjs.map +0 -6
|
@@ -1,739 +0,0 @@
|
|
|
1
|
-
// src/worker/d1.ts
|
|
2
|
-
function isD1Database(v) {
|
|
3
|
-
return typeof v === "object" && v !== null && v.constructor.name === "D1Database" && "prepare" in v && typeof v.prepare === "function" && "batch" in v && typeof v.batch === "function" && "exec" in v && typeof v.exec === "function";
|
|
4
|
-
}
|
|
5
|
-
function isD1Migration(v) {
|
|
6
|
-
return typeof v === "object" && v !== null && "name" in v && typeof v.name === "string" && "queries" in v && Array.isArray(v.queries) && v.queries.every((query) => typeof query === "string");
|
|
7
|
-
}
|
|
8
|
-
function isD1Migrations(v) {
|
|
9
|
-
return Array.isArray(v) && v.every(isD1Migration);
|
|
10
|
-
}
|
|
11
|
-
async function applyD1Migrations(db, migrations, migrationsTableName = "d1_migrations") {
|
|
12
|
-
if (!isD1Database(db)) {
|
|
13
|
-
throw new TypeError(
|
|
14
|
-
"Failed to execute 'applyD1Migrations': parameter 1 is not of type 'D1Database'."
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
if (!isD1Migrations(migrations)) {
|
|
18
|
-
throw new TypeError(
|
|
19
|
-
"Failed to execute 'applyD1Migrations': parameter 2 is not of type 'D1Migration[]'."
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
if (typeof migrationsTableName !== "string") {
|
|
23
|
-
throw new TypeError(
|
|
24
|
-
"Failed to execute 'applyD1Migrations': parameter 3 is not of type 'string'."
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
const schema = `CREATE TABLE IF NOT EXISTS ${migrationsTableName} (
|
|
28
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
29
|
-
name TEXT UNIQUE,
|
|
30
|
-
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
|
31
|
-
);`;
|
|
32
|
-
await db.prepare(schema).run();
|
|
33
|
-
const appliedMigrationNamesResult = await db.prepare(`SELECT name FROM ${migrationsTableName};`).all();
|
|
34
|
-
const appliedMigrationNames = appliedMigrationNamesResult.results.map(
|
|
35
|
-
({ name }) => name
|
|
36
|
-
);
|
|
37
|
-
const insertMigrationStmt = db.prepare(
|
|
38
|
-
`INSERT INTO ${migrationsTableName} (name) VALUES (?);`
|
|
39
|
-
);
|
|
40
|
-
for (const migration of migrations) {
|
|
41
|
-
if (appliedMigrationNames.includes(migration.name))
|
|
42
|
-
continue;
|
|
43
|
-
const queries = migration.queries.map((query) => db.prepare(query));
|
|
44
|
-
queries.push(insertMigrationStmt.bind(migration.name));
|
|
45
|
-
await db.batch(queries);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// src/worker/durable-objects.ts
|
|
50
|
-
import assert2 from "node:assert";
|
|
51
|
-
|
|
52
|
-
// src/worker/env.ts
|
|
53
|
-
import assert from "node:assert";
|
|
54
|
-
var env;
|
|
55
|
-
var SELF;
|
|
56
|
-
function stripInternalEnv(internalEnv2) {
|
|
57
|
-
const result = { ...internalEnv2 };
|
|
58
|
-
delete result.__VITEST_POOL_WORKERS_SELF_NAME;
|
|
59
|
-
delete result.__VITEST_POOL_WORKERS_SELF_SERVICE;
|
|
60
|
-
delete result.__VITEST_POOL_WORKERS_LOOPBACK_SERVICE;
|
|
61
|
-
delete result.__VITEST_POOL_WORKERS_RUNNER_OBJECT;
|
|
62
|
-
delete result.__VITEST_POOL_WORKERS_UNSAFE_EVAL;
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
var internalEnv;
|
|
66
|
-
function setEnv(newEnv) {
|
|
67
|
-
internalEnv = newEnv;
|
|
68
|
-
SELF = newEnv.__VITEST_POOL_WORKERS_SELF_SERVICE;
|
|
69
|
-
env = stripInternalEnv(newEnv);
|
|
70
|
-
}
|
|
71
|
-
function getSerializedOptions() {
|
|
72
|
-
assert(typeof __vitest_worker__ === "object", "Expected global Vitest state");
|
|
73
|
-
const options = __vitest_worker__.config?.poolOptions?.workers;
|
|
74
|
-
assert(options !== void 0, "Expected serialised options");
|
|
75
|
-
return options;
|
|
76
|
-
}
|
|
77
|
-
function getResolvedMainPath(forBindingType) {
|
|
78
|
-
const options = getSerializedOptions();
|
|
79
|
-
if (options.main === void 0) {
|
|
80
|
-
throw new Error(
|
|
81
|
-
`Using ${forBindingType} bindings to the current worker requires \`poolOptions.workers.main\` to be set to your worker's entrypoint`
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
return options.main;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/worker/durable-objects.ts
|
|
88
|
-
var CF_KEY_ACTION = "vitestPoolWorkersDurableObjectAction";
|
|
89
|
-
var nextActionId = 0;
|
|
90
|
-
var kUseResponse = Symbol("kUseResponse");
|
|
91
|
-
var actionResults = /* @__PURE__ */ new Map();
|
|
92
|
-
function isDurableObjectNamespace(v) {
|
|
93
|
-
return typeof v === "object" && v !== null && v.constructor.name === "DurableObjectNamespace" && "newUniqueId" in v && typeof v.newUniqueId === "function" && "idFromName" in v && typeof v.idFromName === "function" && "idFromString" in v && typeof v.idFromString === "function" && "get" in v && typeof v.get === "function";
|
|
94
|
-
}
|
|
95
|
-
function isDurableObjectStub(v) {
|
|
96
|
-
return typeof v === "object" && v !== null && (v.constructor.name === "DurableObject" || v.constructor.name === "WorkerRpc") && "fetch" in v && typeof v.fetch === "function" && "id" in v && typeof v.id === "object";
|
|
97
|
-
}
|
|
98
|
-
var sameIsolatedNamespaces;
|
|
99
|
-
function getSameIsolateNamespaces() {
|
|
100
|
-
if (sameIsolatedNamespaces !== void 0)
|
|
101
|
-
return sameIsolatedNamespaces;
|
|
102
|
-
sameIsolatedNamespaces = [];
|
|
103
|
-
const options = getSerializedOptions();
|
|
104
|
-
if (options.durableObjectBindingDesignators === void 0) {
|
|
105
|
-
return sameIsolatedNamespaces;
|
|
106
|
-
}
|
|
107
|
-
for (const [key, designator] of options.durableObjectBindingDesignators) {
|
|
108
|
-
if (designator.scriptName !== void 0)
|
|
109
|
-
continue;
|
|
110
|
-
const namespace = internalEnv[key];
|
|
111
|
-
assert2(
|
|
112
|
-
isDurableObjectNamespace(namespace),
|
|
113
|
-
`Expected ${key} to be a DurableObjectNamespace binding`
|
|
114
|
-
);
|
|
115
|
-
sameIsolatedNamespaces.push(namespace);
|
|
116
|
-
}
|
|
117
|
-
return sameIsolatedNamespaces;
|
|
118
|
-
}
|
|
119
|
-
function assertSameIsolate(stub) {
|
|
120
|
-
const idString = stub.id.toString();
|
|
121
|
-
const namespaces = getSameIsolateNamespaces();
|
|
122
|
-
for (const namespace of namespaces) {
|
|
123
|
-
try {
|
|
124
|
-
namespace.idFromString(idString);
|
|
125
|
-
return;
|
|
126
|
-
} catch {
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
throw new Error(
|
|
130
|
-
"Durable Object test helpers can only be used with stubs pointing to objects defined within the same worker."
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
async function runInStub(stub, callback) {
|
|
134
|
-
const id = nextActionId++;
|
|
135
|
-
actionResults.set(id, callback);
|
|
136
|
-
const response = await stub.fetch("http://x", {
|
|
137
|
-
cf: { [CF_KEY_ACTION]: id }
|
|
138
|
-
});
|
|
139
|
-
assert2(actionResults.has(id), `Expected action result for ${id}`);
|
|
140
|
-
const result = actionResults.get(id);
|
|
141
|
-
actionResults.delete(id);
|
|
142
|
-
if (result === kUseResponse) {
|
|
143
|
-
return response;
|
|
144
|
-
} else if (response.ok) {
|
|
145
|
-
return result;
|
|
146
|
-
} else {
|
|
147
|
-
throw result;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
async function runInDurableObject(stub, callback) {
|
|
151
|
-
if (!isDurableObjectStub(stub)) {
|
|
152
|
-
throw new TypeError(
|
|
153
|
-
"Failed to execute 'runInDurableObject': parameter 1 is not of type 'DurableObjectStub'."
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
if (typeof callback !== "function") {
|
|
157
|
-
throw new TypeError(
|
|
158
|
-
"Failed to execute 'runInDurableObject': parameter 2 is not of type 'function'."
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
assertSameIsolate(stub);
|
|
162
|
-
return runInStub(stub, callback);
|
|
163
|
-
}
|
|
164
|
-
async function runAlarm(instance, state) {
|
|
165
|
-
const alarm = await state.storage.getAlarm();
|
|
166
|
-
if (alarm === null)
|
|
167
|
-
return false;
|
|
168
|
-
await state.storage.deleteAlarm();
|
|
169
|
-
await instance.alarm?.();
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
async function runDurableObjectAlarm(stub) {
|
|
173
|
-
if (!isDurableObjectStub(stub)) {
|
|
174
|
-
throw new TypeError(
|
|
175
|
-
"Failed to execute 'runDurableObjectAlarm': parameter 1 is not of type 'DurableObjectStub'."
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
return runInDurableObject(stub, runAlarm);
|
|
179
|
-
}
|
|
180
|
-
function runInRunnerObject(env2, callback) {
|
|
181
|
-
const stub = env2.__VITEST_POOL_WORKERS_RUNNER_OBJECT.get("singleton");
|
|
182
|
-
return runInStub(stub, callback);
|
|
183
|
-
}
|
|
184
|
-
function importModule(env2, specifier) {
|
|
185
|
-
return runInRunnerObject(env2, (instance) => {
|
|
186
|
-
assert2(
|
|
187
|
-
instance.executor !== void 0,
|
|
188
|
-
"Expected Vitest to start running before importing modules"
|
|
189
|
-
);
|
|
190
|
-
return instance.executor.executeId(specifier);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
async function maybeHandleRunRequest(request, instance, state) {
|
|
194
|
-
const actionId = request.cf?.[CF_KEY_ACTION];
|
|
195
|
-
if (actionId === void 0)
|
|
196
|
-
return;
|
|
197
|
-
assert2(typeof actionId === "number", `Expected numeric ${CF_KEY_ACTION}`);
|
|
198
|
-
try {
|
|
199
|
-
const callback = actionResults.get(actionId);
|
|
200
|
-
assert2(typeof callback === "function", `Expected callback for ${actionId}`);
|
|
201
|
-
const result = await callback(instance, state);
|
|
202
|
-
if (result instanceof Response) {
|
|
203
|
-
actionResults.set(actionId, kUseResponse);
|
|
204
|
-
return result;
|
|
205
|
-
} else {
|
|
206
|
-
actionResults.set(actionId, result);
|
|
207
|
-
}
|
|
208
|
-
return new Response(null, { status: 204 });
|
|
209
|
-
} catch (e) {
|
|
210
|
-
actionResults.set(actionId, e);
|
|
211
|
-
return new Response(null, { status: 500 });
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
var DurableObjectWrapper = class {
|
|
215
|
-
constructor(state, env2, className) {
|
|
216
|
-
this.state = state;
|
|
217
|
-
this.env = env2;
|
|
218
|
-
this.className = className;
|
|
219
|
-
}
|
|
220
|
-
instanceConstructor;
|
|
221
|
-
instance;
|
|
222
|
-
async ensureInstance() {
|
|
223
|
-
const mainPath = getResolvedMainPath("Durable Object");
|
|
224
|
-
const mainModule = await importModule(this.env, mainPath);
|
|
225
|
-
const constructor = mainModule[this.className];
|
|
226
|
-
if (typeof constructor !== "function") {
|
|
227
|
-
throw new Error(
|
|
228
|
-
`${mainPath} does not export a ${this.className} Durable Object`
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
this.instanceConstructor ??= constructor;
|
|
232
|
-
if (this.instanceConstructor !== constructor) {
|
|
233
|
-
await this.state.blockConcurrencyWhile(() => {
|
|
234
|
-
throw new Error(
|
|
235
|
-
`${mainPath} changed, invalidating this Durable Object. Please retry the \`DurableObjectStub#fetch()\` call.`
|
|
236
|
-
);
|
|
237
|
-
});
|
|
238
|
-
assert2.fail("Unreachable");
|
|
239
|
-
}
|
|
240
|
-
if (this.instance === void 0) {
|
|
241
|
-
const userEnv = stripInternalEnv(this.env);
|
|
242
|
-
this.instance = new this.instanceConstructor(this.state, userEnv);
|
|
243
|
-
await this.state.blockConcurrencyWhile(async () => {
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
return this.instance;
|
|
247
|
-
}
|
|
248
|
-
async fetch(request) {
|
|
249
|
-
const instance = await this.ensureInstance();
|
|
250
|
-
const response = await maybeHandleRunRequest(request, instance, this.state);
|
|
251
|
-
if (response !== void 0)
|
|
252
|
-
return response;
|
|
253
|
-
if (instance.fetch === void 0) {
|
|
254
|
-
throw new Error("Handler does not export a fetch() function.");
|
|
255
|
-
}
|
|
256
|
-
return instance.fetch(request);
|
|
257
|
-
}
|
|
258
|
-
async alarm(...args) {
|
|
259
|
-
const instance = await this.ensureInstance();
|
|
260
|
-
return instance.alarm?.(...args);
|
|
261
|
-
}
|
|
262
|
-
async webSocketMessage(...args) {
|
|
263
|
-
const instance = await this.ensureInstance();
|
|
264
|
-
return instance.webSocketMessage?.(...args);
|
|
265
|
-
}
|
|
266
|
-
async webSocketClose(...args) {
|
|
267
|
-
const instance = await this.ensureInstance();
|
|
268
|
-
return instance.webSocketClose?.(...args);
|
|
269
|
-
}
|
|
270
|
-
async webSocketError(...args) {
|
|
271
|
-
const instance = await this.ensureInstance();
|
|
272
|
-
return instance.webSocketError?.(...args);
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
function createDurableObjectWrapper(className) {
|
|
276
|
-
return class extends DurableObjectWrapper {
|
|
277
|
-
constructor(state, env2) {
|
|
278
|
-
super(state, env2, className);
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
async function listDurableObjectIds(namespace) {
|
|
283
|
-
if (!isDurableObjectNamespace(namespace)) {
|
|
284
|
-
throw new TypeError(
|
|
285
|
-
"Failed to execute 'listDurableObjectIds': parameter 1 is not of type 'DurableObjectNamespace'."
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
const boundName = Object.entries(internalEnv).find(
|
|
289
|
-
(entry) => namespace === entry[1]
|
|
290
|
-
)?.[0];
|
|
291
|
-
assert2(boundName !== void 0, "Expected to find bound name for namespace");
|
|
292
|
-
const options = getSerializedOptions();
|
|
293
|
-
const designator = options.durableObjectBindingDesignators?.get(boundName);
|
|
294
|
-
assert2(designator !== void 0, "Expected to find designator for namespace");
|
|
295
|
-
let uniqueKey = designator.unsafeUniqueKey;
|
|
296
|
-
if (uniqueKey === void 0) {
|
|
297
|
-
const scriptName = designator.scriptName ?? internalEnv.__VITEST_POOL_WORKERS_SELF_NAME;
|
|
298
|
-
const className = designator.className;
|
|
299
|
-
uniqueKey = `${scriptName}-${className}`;
|
|
300
|
-
}
|
|
301
|
-
const url = `http://placeholder/durable-objects?unique_key=${encodeURIComponent(
|
|
302
|
-
uniqueKey
|
|
303
|
-
)}`;
|
|
304
|
-
const res = await internalEnv.__VITEST_POOL_WORKERS_LOOPBACK_SERVICE.fetch(
|
|
305
|
-
url
|
|
306
|
-
);
|
|
307
|
-
assert2.strictEqual(res.status, 200);
|
|
308
|
-
const ids = await res.json();
|
|
309
|
-
assert2(Array.isArray(ids));
|
|
310
|
-
return ids.map((id) => {
|
|
311
|
-
assert2(typeof id === "string");
|
|
312
|
-
return namespace.idFromString(id);
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// src/worker/events.ts
|
|
317
|
-
var kConstructFlag = Symbol("kConstructFlag");
|
|
318
|
-
async function waitForWaitUntil(waitUntil) {
|
|
319
|
-
const errors = [];
|
|
320
|
-
while (waitUntil.length > 0) {
|
|
321
|
-
const results = await Promise.allSettled(waitUntil.splice(0));
|
|
322
|
-
for (const result of results) {
|
|
323
|
-
if (result.status === "rejected")
|
|
324
|
-
errors.push(result.reason);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
if (errors.length === 1) {
|
|
328
|
-
throw errors[0];
|
|
329
|
-
} else if (errors.length > 1) {
|
|
330
|
-
throw new AggregateError(errors);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
var globalWaitUntil = [];
|
|
334
|
-
function registerGlobalWaitUntil(promise) {
|
|
335
|
-
globalWaitUntil.push(promise);
|
|
336
|
-
}
|
|
337
|
-
function waitForGlobalWaitUntil() {
|
|
338
|
-
return waitForWaitUntil(globalWaitUntil);
|
|
339
|
-
}
|
|
340
|
-
var kWaitUntil = Symbol("kWaitUntil");
|
|
341
|
-
var ExecutionContext = class {
|
|
342
|
-
// https://github.com/cloudflare/workerd/blob/v1.20231218.0/src/workerd/api/global-scope.h#L168
|
|
343
|
-
[kWaitUntil] = [];
|
|
344
|
-
constructor(flag) {
|
|
345
|
-
if (flag !== kConstructFlag)
|
|
346
|
-
throw new TypeError("Illegal constructor");
|
|
347
|
-
}
|
|
348
|
-
waitUntil(promise) {
|
|
349
|
-
if (!(this instanceof ExecutionContext)) {
|
|
350
|
-
throw new TypeError("Illegal invocation");
|
|
351
|
-
}
|
|
352
|
-
this[kWaitUntil].push(promise);
|
|
353
|
-
registerGlobalWaitUntil(promise);
|
|
354
|
-
}
|
|
355
|
-
passThroughOnException() {
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
function createExecutionContext() {
|
|
359
|
-
return new ExecutionContext(kConstructFlag);
|
|
360
|
-
}
|
|
361
|
-
async function waitOnExecutionContext(ctx) {
|
|
362
|
-
if (!(ctx instanceof ExecutionContext)) {
|
|
363
|
-
throw new TypeError(
|
|
364
|
-
"Failed to execute 'getWaitUntil': parameter 1 is not of type 'ExecutionContext'.\nYou must call 'createExecutionContext()' to get an 'ExecutionContext' instance."
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
return waitForWaitUntil(ctx[kWaitUntil]);
|
|
368
|
-
}
|
|
369
|
-
var ScheduledController = class {
|
|
370
|
-
// https://github.com/cloudflare/workerd/blob/v1.20231218.0/src/workerd/api/scheduled.h#L35
|
|
371
|
-
scheduledTime;
|
|
372
|
-
cron;
|
|
373
|
-
constructor(flag, options) {
|
|
374
|
-
if (flag !== kConstructFlag)
|
|
375
|
-
throw new TypeError("Illegal constructor");
|
|
376
|
-
const scheduledTime = Number(options?.scheduledTime ?? Date.now());
|
|
377
|
-
const cron = String(options?.cron ?? "");
|
|
378
|
-
Object.defineProperties(this, {
|
|
379
|
-
scheduledTime: {
|
|
380
|
-
get() {
|
|
381
|
-
return scheduledTime;
|
|
382
|
-
}
|
|
383
|
-
},
|
|
384
|
-
cron: {
|
|
385
|
-
get() {
|
|
386
|
-
return cron;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
noRetry() {
|
|
392
|
-
if (!(this instanceof ScheduledController)) {
|
|
393
|
-
throw new TypeError("Illegal invocation");
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
function createScheduledController(options) {
|
|
398
|
-
if (options !== void 0 && typeof options !== "object") {
|
|
399
|
-
throw new TypeError(
|
|
400
|
-
"Failed to execute 'createScheduledController': parameter 1 is not of type 'ScheduledOptions'."
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
return new ScheduledController(kConstructFlag, options);
|
|
404
|
-
}
|
|
405
|
-
var kRetry = Symbol("kRetry");
|
|
406
|
-
var kAck = Symbol("kAck");
|
|
407
|
-
var kRetryAll = Symbol("kRetryAll");
|
|
408
|
-
var kAckAll = Symbol("kAckAll");
|
|
409
|
-
var QueueMessage = class {
|
|
410
|
-
// https://github.com/cloudflare/workerd/blob/v1.20231218.0/src/workerd/api/queue.h#L113
|
|
411
|
-
#controller;
|
|
412
|
-
id;
|
|
413
|
-
timestamp;
|
|
414
|
-
body;
|
|
415
|
-
attempts;
|
|
416
|
-
[kRetry] = false;
|
|
417
|
-
[kAck] = false;
|
|
418
|
-
constructor(flag, controller, message) {
|
|
419
|
-
if (flag !== kConstructFlag)
|
|
420
|
-
throw new TypeError("Illegal constructor");
|
|
421
|
-
this.#controller = controller;
|
|
422
|
-
const id = String(message.id);
|
|
423
|
-
let timestamp;
|
|
424
|
-
if (typeof message.timestamp === "number") {
|
|
425
|
-
timestamp = new Date(message.timestamp);
|
|
426
|
-
} else if (message.timestamp instanceof Date) {
|
|
427
|
-
timestamp = new Date(message.timestamp.getTime());
|
|
428
|
-
} else {
|
|
429
|
-
throw new TypeError(
|
|
430
|
-
"Incorrect type for the 'timestamp' field on 'ServiceBindingQueueMessage': the provided value is not of type 'date'."
|
|
431
|
-
);
|
|
432
|
-
}
|
|
433
|
-
let attempts;
|
|
434
|
-
if (typeof message.attempts === "number") {
|
|
435
|
-
attempts = message.attempts;
|
|
436
|
-
} else {
|
|
437
|
-
throw new TypeError(
|
|
438
|
-
"Incorrect type for the 'attempts' field on 'ServiceBindingQueueMessage': the provided value is not of type 'number'."
|
|
439
|
-
);
|
|
440
|
-
}
|
|
441
|
-
if ("serializedBody" in message) {
|
|
442
|
-
throw new TypeError(
|
|
443
|
-
"Cannot use `serializedBody` with `createMessageBatch()`"
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
const body = structuredClone(message.body);
|
|
447
|
-
Object.defineProperties(this, {
|
|
448
|
-
id: {
|
|
449
|
-
get() {
|
|
450
|
-
return id;
|
|
451
|
-
}
|
|
452
|
-
},
|
|
453
|
-
timestamp: {
|
|
454
|
-
get() {
|
|
455
|
-
return timestamp;
|
|
456
|
-
}
|
|
457
|
-
},
|
|
458
|
-
body: {
|
|
459
|
-
get() {
|
|
460
|
-
return body;
|
|
461
|
-
}
|
|
462
|
-
},
|
|
463
|
-
attempts: {
|
|
464
|
-
get() {
|
|
465
|
-
return attempts;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
});
|
|
469
|
-
}
|
|
470
|
-
retry() {
|
|
471
|
-
if (!(this instanceof QueueMessage)) {
|
|
472
|
-
throw new TypeError("Illegal invocation");
|
|
473
|
-
}
|
|
474
|
-
if (this.#controller[kRetryAll])
|
|
475
|
-
return;
|
|
476
|
-
if (this.#controller[kAckAll]) {
|
|
477
|
-
console.warn(
|
|
478
|
-
`Received a call to retry() on message ${this.id} after ackAll() was already called. Calling retry() on a message after calling ackAll() has no effect.`
|
|
479
|
-
);
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
if (this[kAck]) {
|
|
483
|
-
console.warn(
|
|
484
|
-
`Received a call to retry() on message ${this.id} after ack() was already called. Calling retry() on a message after calling ack() has no effect.`
|
|
485
|
-
);
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
this[kRetry] = true;
|
|
489
|
-
}
|
|
490
|
-
ack() {
|
|
491
|
-
if (!(this instanceof QueueMessage)) {
|
|
492
|
-
throw new TypeError("Illegal invocation");
|
|
493
|
-
}
|
|
494
|
-
if (this.#controller[kAckAll])
|
|
495
|
-
return;
|
|
496
|
-
if (this.#controller[kRetryAll]) {
|
|
497
|
-
console.warn(
|
|
498
|
-
`Received a call to ack() on message ${this.id} after retryAll() was already called. Calling ack() on a message after calling retryAll() has no effect.`
|
|
499
|
-
);
|
|
500
|
-
return;
|
|
501
|
-
}
|
|
502
|
-
if (this[kRetry]) {
|
|
503
|
-
console.warn(
|
|
504
|
-
`Received a call to ack() on message ${this.id} after retry() was already called. Calling ack() on a message after calling retry() has no effect.`
|
|
505
|
-
);
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
this[kAck] = true;
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
var QueueController = class {
|
|
512
|
-
// https://github.com/cloudflare/workerd/blob/v1.20231218.0/src/workerd/api/queue.h#L198
|
|
513
|
-
queue;
|
|
514
|
-
messages;
|
|
515
|
-
[kRetryAll] = false;
|
|
516
|
-
[kAckAll] = false;
|
|
517
|
-
constructor(flag, queueOption, messagesOption) {
|
|
518
|
-
if (flag !== kConstructFlag)
|
|
519
|
-
throw new TypeError("Illegal constructor");
|
|
520
|
-
const queue = String(queueOption);
|
|
521
|
-
const messages = messagesOption.map(
|
|
522
|
-
(message) => new QueueMessage(kConstructFlag, this, message)
|
|
523
|
-
);
|
|
524
|
-
Object.defineProperties(this, {
|
|
525
|
-
queue: {
|
|
526
|
-
get() {
|
|
527
|
-
return queue;
|
|
528
|
-
}
|
|
529
|
-
},
|
|
530
|
-
messages: {
|
|
531
|
-
get() {
|
|
532
|
-
return messages;
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
});
|
|
536
|
-
}
|
|
537
|
-
retryAll() {
|
|
538
|
-
if (!(this instanceof QueueController)) {
|
|
539
|
-
throw new TypeError("Illegal invocation");
|
|
540
|
-
}
|
|
541
|
-
if (this[kAckAll]) {
|
|
542
|
-
console.warn(
|
|
543
|
-
"Received a call to retryAll() after ackAll() was already called. Calling retryAll() after calling ackAll() has no effect."
|
|
544
|
-
);
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
this[kRetryAll] = true;
|
|
548
|
-
}
|
|
549
|
-
ackAll() {
|
|
550
|
-
if (!(this instanceof QueueController)) {
|
|
551
|
-
throw new TypeError("Illegal invocation");
|
|
552
|
-
}
|
|
553
|
-
if (this[kRetryAll]) {
|
|
554
|
-
console.warn(
|
|
555
|
-
"Received a call to ackAll() after retryAll() was already called. Calling ackAll() after calling retryAll() has no effect."
|
|
556
|
-
);
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
this[kAckAll] = true;
|
|
560
|
-
}
|
|
561
|
-
};
|
|
562
|
-
function createMessageBatch(queueName, messages) {
|
|
563
|
-
if (arguments.length === 0) {
|
|
564
|
-
throw new TypeError(
|
|
565
|
-
"TypeError: Failed to execute 'createMessageBatch': parameter 1 is not of type 'string'."
|
|
566
|
-
);
|
|
567
|
-
}
|
|
568
|
-
if (!Array.isArray(messages)) {
|
|
569
|
-
throw new TypeError(
|
|
570
|
-
"TypeError: Failed to execute 'createMessageBatch': parameter 2 is not of type 'Array'."
|
|
571
|
-
);
|
|
572
|
-
}
|
|
573
|
-
return new QueueController(kConstructFlag, queueName, messages);
|
|
574
|
-
}
|
|
575
|
-
async function getQueueResult(batch, ctx) {
|
|
576
|
-
if (!(batch instanceof QueueController)) {
|
|
577
|
-
throw new TypeError(
|
|
578
|
-
"Failed to execute 'getQueueResult': parameter 1 is not of type 'MessageBatch'.\nYou must call 'createMessageBatch()' to get a 'MessageBatch' instance."
|
|
579
|
-
);
|
|
580
|
-
}
|
|
581
|
-
if (!(ctx instanceof ExecutionContext)) {
|
|
582
|
-
throw new TypeError(
|
|
583
|
-
"Failed to execute 'getQueueResult': parameter 2 is not of type 'ExecutionContext'.\nYou must call 'createExecutionContext()' to get an 'ExecutionContext' instance."
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
await waitOnExecutionContext(ctx);
|
|
587
|
-
const retryMessages = [];
|
|
588
|
-
const explicitAcks = [];
|
|
589
|
-
for (const message of batch.messages) {
|
|
590
|
-
if (message[kRetry])
|
|
591
|
-
retryMessages.push({ msgId: message.id });
|
|
592
|
-
if (message[kAck])
|
|
593
|
-
explicitAcks.push(message.id);
|
|
594
|
-
}
|
|
595
|
-
return {
|
|
596
|
-
outcome: "ok",
|
|
597
|
-
retryBatch: {
|
|
598
|
-
retry: batch[kRetryAll]
|
|
599
|
-
},
|
|
600
|
-
ackAll: batch[kAckAll],
|
|
601
|
-
retryMessages,
|
|
602
|
-
explicitAcks
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
// src/worker/fetch-mock.ts
|
|
607
|
-
import assert3 from "node:assert";
|
|
608
|
-
import { Buffer } from "node:buffer";
|
|
609
|
-
import { isMockActive, MockAgent, setDispatcher } from "cloudflare:mock-agent";
|
|
610
|
-
var DECODER = new TextDecoder();
|
|
611
|
-
var fetchMock = new MockAgent({ connections: 1 });
|
|
612
|
-
var requests = /* @__PURE__ */ new WeakMap();
|
|
613
|
-
var responses = /* @__PURE__ */ new WeakMap();
|
|
614
|
-
var originalFetch = fetch;
|
|
615
|
-
setDispatcher((opts, handler) => {
|
|
616
|
-
const request = requests.get(opts);
|
|
617
|
-
assert3(request !== void 0, "Expected dispatch to come from fetch()");
|
|
618
|
-
originalFetch.call(globalThis, request.request, { body: request.body }).then((response) => {
|
|
619
|
-
responses.set(opts, response);
|
|
620
|
-
assert3(handler.onComplete !== void 0, "Expected onComplete() handler");
|
|
621
|
-
handler.onComplete?.([]);
|
|
622
|
-
}).catch((error) => {
|
|
623
|
-
assert3(handler.onError !== void 0, "Expected onError() handler");
|
|
624
|
-
handler.onError(error);
|
|
625
|
-
});
|
|
626
|
-
});
|
|
627
|
-
globalThis.fetch = async (input, init) => {
|
|
628
|
-
const isActive = isMockActive(fetchMock);
|
|
629
|
-
if (!isActive)
|
|
630
|
-
return originalFetch.call(globalThis, input, init);
|
|
631
|
-
const request = new Request(input, init);
|
|
632
|
-
const url = new URL(request.url);
|
|
633
|
-
if (request.headers.get("Upgrade") !== null) {
|
|
634
|
-
return originalFetch.call(globalThis, request);
|
|
635
|
-
}
|
|
636
|
-
const requestHeaders = {};
|
|
637
|
-
for (const entry of request.headers) {
|
|
638
|
-
const key = entry[0].toLowerCase();
|
|
639
|
-
const value = entry[1];
|
|
640
|
-
if (key === "set-cookie") {
|
|
641
|
-
(requestHeaders[key] ??= []).push(value);
|
|
642
|
-
} else {
|
|
643
|
-
requestHeaders[key] = value;
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
const bodyArray = request.body === null ? null : new Uint8Array(await request.arrayBuffer());
|
|
647
|
-
const bodyText = bodyArray === null ? "" : DECODER.decode(bodyArray);
|
|
648
|
-
const dispatchOptions = {
|
|
649
|
-
origin: url.origin,
|
|
650
|
-
path: url.pathname,
|
|
651
|
-
method: request.method,
|
|
652
|
-
body: bodyText,
|
|
653
|
-
headers: requestHeaders,
|
|
654
|
-
query: Object.fromEntries(url.searchParams)
|
|
655
|
-
};
|
|
656
|
-
requests.set(dispatchOptions, { request, body: bodyArray });
|
|
657
|
-
let responseStatusCode;
|
|
658
|
-
let responseStatusText;
|
|
659
|
-
let responseHeaders;
|
|
660
|
-
const responseChunks = [];
|
|
661
|
-
let responseResolve;
|
|
662
|
-
let responseReject;
|
|
663
|
-
const responsePromise = new Promise((resolve, reject) => {
|
|
664
|
-
responseResolve = resolve;
|
|
665
|
-
responseReject = reject;
|
|
666
|
-
});
|
|
667
|
-
const dispatchHandlers = {
|
|
668
|
-
onConnect(_abort) {
|
|
669
|
-
},
|
|
670
|
-
// (ignored)
|
|
671
|
-
onError(error) {
|
|
672
|
-
responseReject(error);
|
|
673
|
-
},
|
|
674
|
-
onUpgrade(_statusCode, _headers, _socket) {
|
|
675
|
-
assert3.fail("Unreachable: upgrade requests not supported");
|
|
676
|
-
},
|
|
677
|
-
// `onHeaders` and `onData` will only be called if the response was mocked
|
|
678
|
-
onHeaders(statusCode, headers, _resume, statusText) {
|
|
679
|
-
responseStatusCode = statusCode;
|
|
680
|
-
responseStatusText = statusText;
|
|
681
|
-
if (headers === null)
|
|
682
|
-
return true;
|
|
683
|
-
assert3.strictEqual(headers.length % 2, 0, "Expected key/value array");
|
|
684
|
-
responseHeaders = Array.from({ length: headers.length / 2 }).map(
|
|
685
|
-
(_, i) => [headers[i * 2].toString(), headers[i * 2 + 1].toString()]
|
|
686
|
-
);
|
|
687
|
-
return true;
|
|
688
|
-
},
|
|
689
|
-
onData(chunk) {
|
|
690
|
-
responseChunks.push(chunk);
|
|
691
|
-
return true;
|
|
692
|
-
},
|
|
693
|
-
onComplete(_trailers) {
|
|
694
|
-
const maybeResponse = responses.get(dispatchOptions);
|
|
695
|
-
if (maybeResponse === void 0) {
|
|
696
|
-
const responseBody = Buffer.concat(responseChunks);
|
|
697
|
-
const response = new Response(responseBody, {
|
|
698
|
-
status: responseStatusCode,
|
|
699
|
-
statusText: responseStatusText,
|
|
700
|
-
headers: responseHeaders
|
|
701
|
-
});
|
|
702
|
-
responseResolve(response);
|
|
703
|
-
} else {
|
|
704
|
-
responseResolve(maybeResponse);
|
|
705
|
-
}
|
|
706
|
-
},
|
|
707
|
-
onBodySent(_chunk) {
|
|
708
|
-
}
|
|
709
|
-
// (ignored)
|
|
710
|
-
};
|
|
711
|
-
fetchMock.dispatch(dispatchOptions, dispatchHandlers);
|
|
712
|
-
return responsePromise;
|
|
713
|
-
};
|
|
714
|
-
export {
|
|
715
|
-
SELF,
|
|
716
|
-
applyD1Migrations,
|
|
717
|
-
createDurableObjectWrapper,
|
|
718
|
-
createExecutionContext,
|
|
719
|
-
createMessageBatch,
|
|
720
|
-
createScheduledController,
|
|
721
|
-
env,
|
|
722
|
-
fetchMock,
|
|
723
|
-
getQueueResult,
|
|
724
|
-
getResolvedMainPath,
|
|
725
|
-
getSerializedOptions,
|
|
726
|
-
importModule,
|
|
727
|
-
internalEnv,
|
|
728
|
-
listDurableObjectIds,
|
|
729
|
-
maybeHandleRunRequest,
|
|
730
|
-
registerGlobalWaitUntil,
|
|
731
|
-
runDurableObjectAlarm,
|
|
732
|
-
runInDurableObject,
|
|
733
|
-
runInRunnerObject,
|
|
734
|
-
setEnv,
|
|
735
|
-
stripInternalEnv,
|
|
736
|
-
waitForGlobalWaitUntil,
|
|
737
|
-
waitOnExecutionContext
|
|
738
|
-
};
|
|
739
|
-
//# sourceMappingURL=test-internal.mjs.map
|