@cloudflare/vitest-pool-workers 0.1.0

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.
Files changed (55) hide show
  1. package/dist/config/d1.d.ts +7 -0
  2. package/dist/config/index.cjs +130 -0
  3. package/dist/config/index.cjs.map +6 -0
  4. package/dist/config/index.d.ts +21 -0
  5. package/dist/pool/config.d.ts +77 -0
  6. package/dist/pool/helpers.d.ts +5 -0
  7. package/dist/pool/index.mjs +1525 -0
  8. package/dist/pool/index.mjs.map +6 -0
  9. package/dist/shared/d1.d.ts +4 -0
  10. package/dist/worker/index.mjs +352 -0
  11. package/dist/worker/index.mjs.map +6 -0
  12. package/dist/worker/lib/cloudflare/empty-internal.cjs +27 -0
  13. package/dist/worker/lib/cloudflare/empty-internal.cjs.map +6 -0
  14. package/dist/worker/lib/cloudflare/mock-agent.cjs +2056 -0
  15. package/dist/worker/lib/cloudflare/mock-agent.cjs.map +6 -0
  16. package/dist/worker/lib/cloudflare/test-internal.mjs +723 -0
  17. package/dist/worker/lib/cloudflare/test-internal.mjs.map +6 -0
  18. package/dist/worker/lib/cloudflare/test-runner.mjs +222 -0
  19. package/dist/worker/lib/cloudflare/test-runner.mjs.map +6 -0
  20. package/dist/worker/lib/cloudflare/test.mjs +30 -0
  21. package/dist/worker/lib/cloudflare/test.mjs.map +6 -0
  22. package/dist/worker/lib/debug.mjs +9 -0
  23. package/dist/worker/lib/debug.mjs.map +6 -0
  24. package/dist/worker/lib/mlly.mjs +48 -0
  25. package/dist/worker/lib/mlly.mjs.map +6 -0
  26. package/dist/worker/lib/node/console.mjs +104 -0
  27. package/dist/worker/lib/node/console.mjs.map +6 -0
  28. package/dist/worker/lib/node/dns.mjs +6 -0
  29. package/dist/worker/lib/node/dns.mjs.map +6 -0
  30. package/dist/worker/lib/node/fs/promises.mjs +6 -0
  31. package/dist/worker/lib/node/fs/promises.mjs.map +6 -0
  32. package/dist/worker/lib/node/fs.mjs +25 -0
  33. package/dist/worker/lib/node/fs.mjs.map +6 -0
  34. package/dist/worker/lib/node/http.cjs +97 -0
  35. package/dist/worker/lib/node/http.cjs.map +6 -0
  36. package/dist/worker/lib/node/module.mjs +15 -0
  37. package/dist/worker/lib/node/module.mjs.map +6 -0
  38. package/dist/worker/lib/node/net.cjs +27 -0
  39. package/dist/worker/lib/node/net.cjs.map +6 -0
  40. package/dist/worker/lib/node/perf_hooks.mjs +6 -0
  41. package/dist/worker/lib/node/perf_hooks.mjs.map +6 -0
  42. package/dist/worker/lib/node/querystring.cjs +44 -0
  43. package/dist/worker/lib/node/querystring.cjs.map +6 -0
  44. package/dist/worker/lib/node/timers.mjs +6 -0
  45. package/dist/worker/lib/node/timers.mjs.map +6 -0
  46. package/dist/worker/lib/node/tty.mjs +8 -0
  47. package/dist/worker/lib/node/tty.mjs.map +6 -0
  48. package/dist/worker/lib/node/url.mjs +75 -0
  49. package/dist/worker/lib/node/url.mjs.map +6 -0
  50. package/dist/worker/lib/node/vm.mjs +17 -0
  51. package/dist/worker/lib/node/vm.mjs.map +6 -0
  52. package/dist/worker/lib/tinypool.mjs +6 -0
  53. package/dist/worker/lib/tinypool.mjs.map +6 -0
  54. package/package.json +56 -0
  55. package/test/cloudflare-test.d.ts +278 -0
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/http.cts"],
4
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACO,IAAM,eAAuC;AAAA,EACnD,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEA,IAAO,eAAQ,CAAC;",
5
+ "names": []
6
+ }
@@ -0,0 +1,15 @@
1
+ // src/worker/lib/node/module.ts
2
+ var builtinModules = [];
3
+ function createRequire(_filename) {
4
+ return () => {
5
+ throw new Error("require() is not yet implemented in Workers");
6
+ };
7
+ }
8
+ var Module = class {
9
+ };
10
+ export {
11
+ Module,
12
+ builtinModules,
13
+ createRequire
14
+ };
15
+ //# sourceMappingURL=module.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/module.ts"],
4
+ "mappings": ";AACO,IAAM,iBAA2B,CAAC;AAGlC,SAAS,cAAc,WAAmB;AAChD,SAAO,MAAM;AACZ,UAAM,IAAI,MAAM,6CAA6C;AAAA,EAC9D;AACD;AAEO,IAAM,SAAN,MAAa;AAAC;",
5
+ "names": []
6
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/worker/lib/node/net.cts
21
+ var net_exports = {};
22
+ __export(net_exports, {
23
+ default: () => net_default
24
+ });
25
+ module.exports = __toCommonJS(net_exports);
26
+ var net_default = {};
27
+ //# sourceMappingURL=net.cjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/net.cts"],
4
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,cAAQ,CAAC;",
5
+ "names": []
6
+ }
@@ -0,0 +1,6 @@
1
+ // src/worker/lib/node/perf_hooks.ts
2
+ var _performance = performance;
3
+ export {
4
+ _performance as performance
5
+ };
6
+ //# sourceMappingURL=perf_hooks.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/perf_hooks.ts"],
4
+ "mappings": ";AAAA,IAAM,eAAe;",
5
+ "names": []
6
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/worker/lib/node/querystring.cts
21
+ var querystring_exports = {};
22
+ __export(querystring_exports, {
23
+ default: () => querystring_default,
24
+ stringify: () => stringify
25
+ });
26
+ module.exports = __toCommonJS(querystring_exports);
27
+ function stringify(object) {
28
+ const params = new URLSearchParams();
29
+ for (const [key, values] of Object.entries(object)) {
30
+ if (Array.isArray(values)) {
31
+ for (const value of values)
32
+ params.append(key, value);
33
+ } else {
34
+ params.append(key, values);
35
+ }
36
+ }
37
+ return params.toString();
38
+ }
39
+ var querystring_default = { stringify };
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ stringify
43
+ });
44
+ //# sourceMappingURL=querystring.cjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/querystring.cts"],
4
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,SAAS,UAAU,QAAmD;AAC5E,QAAM,SAAS,IAAI,gBAAgB;AACnC,aAAW,CAAC,KAAK,MAAM,KAAK,OAAO,QAAQ,MAAM,GAAG;AACnD,QAAI,MAAM,QAAQ,MAAM,GAAG;AAC1B,iBAAW,SAAS;AAAQ,eAAO,OAAO,KAAK,KAAK;AAAA,IACrD,OAAO;AACN,aAAO,OAAO,KAAK,MAAM;AAAA,IAC1B;AAAA,EACD;AACA,SAAO,OAAO,SAAS;AACxB;AAEA,IAAO,sBAAQ,EAAE,UAAU;",
5
+ "names": []
6
+ }
@@ -0,0 +1,6 @@
1
+ // src/worker/lib/node/timers.ts
2
+ var timers_default = {};
3
+ export {
4
+ timers_default as default
5
+ };
6
+ //# sourceMappingURL=timers.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/timers.ts"],
4
+ "mappings": ";AAAA,IAAO,iBAAQ,CAAC;",
5
+ "names": []
6
+ }
@@ -0,0 +1,8 @@
1
+ // src/worker/lib/node/tty.ts
2
+ function isatty() {
3
+ return false;
4
+ }
5
+ export {
6
+ isatty
7
+ };
8
+ //# sourceMappingURL=tty.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/tty.ts"],
4
+ "mappings": ";AAAO,SAAS,SAAS;AACxB,SAAO;AACR;",
5
+ "names": []
6
+ }
@@ -0,0 +1,75 @@
1
+ // src/worker/lib/node/url.ts
2
+ import path from "node:path";
3
+ function fileURLToPath(url) {
4
+ if (typeof url === "string")
5
+ url = new URL(url);
6
+ else if (!(url instanceof URL)) {
7
+ throw new TypeError(`Expected path to be string | URL, got ${url}`);
8
+ }
9
+ if (url.protocol !== "file:") {
10
+ throw new TypeError("Expected protocol to be file:");
11
+ }
12
+ return getPathFromURLPosix(url);
13
+ }
14
+ function getPathFromURLPosix(url) {
15
+ if (url.hostname !== "") {
16
+ throw new TypeError("Expected hostname to be empty");
17
+ }
18
+ const pathname = url.pathname;
19
+ for (let n = 0; n < pathname.length; n++) {
20
+ if (pathname[n] === "%") {
21
+ const third = pathname.codePointAt(n + 2) | 32;
22
+ if (pathname[n + 1] === "2" && third === 102) {
23
+ throw new TypeError(
24
+ "Expected pathname not to include encoded / characters"
25
+ );
26
+ }
27
+ }
28
+ }
29
+ return decodeURIComponent(pathname);
30
+ }
31
+ var CHAR_FORWARD_SLASH = 47;
32
+ var percentRegEx = /%/g;
33
+ var backslashRegEx = /\\/g;
34
+ var newlineRegEx = /\n/g;
35
+ var carriageReturnRegEx = /\r/g;
36
+ var tabRegEx = /\t/g;
37
+ function pathToFileURL(filepath) {
38
+ const outURL = new URL("file://");
39
+ let resolved = path.resolve(filepath);
40
+ const filePathLast = filepath.charCodeAt(filepath.length - 1);
41
+ if (filePathLast === CHAR_FORWARD_SLASH && resolved[resolved.length - 1] !== path.sep) {
42
+ resolved += "/";
43
+ }
44
+ outURL.pathname = encodePathChars(resolved);
45
+ return outURL;
46
+ }
47
+ function encodePathChars(filepath) {
48
+ if (filepath.includes("%")) {
49
+ filepath = filepath.replace(percentRegEx, "%25");
50
+ }
51
+ if (filepath.includes("\\")) {
52
+ filepath = filepath.replace(backslashRegEx, "%5C");
53
+ }
54
+ if (filepath.includes("\n")) {
55
+ filepath = filepath.replace(newlineRegEx, "%0A");
56
+ }
57
+ if (filepath.includes("\r")) {
58
+ filepath = filepath.replace(carriageReturnRegEx, "%0D");
59
+ }
60
+ if (filepath.includes(" ")) {
61
+ filepath = filepath.replace(tabRegEx, "%09");
62
+ }
63
+ return filepath;
64
+ }
65
+ var url_default = {
66
+ fileURLToPath,
67
+ pathToFileURL
68
+ };
69
+ export {
70
+ CHAR_FORWARD_SLASH,
71
+ url_default as default,
72
+ fileURLToPath,
73
+ pathToFileURL
74
+ };
75
+ //# sourceMappingURL=url.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/url.ts"],
4
+ "mappings": ";AAAA,OAAO,UAAU;AAGV,SAAS,cAAc,KAA2B;AAGxD,MAAI,OAAO,QAAQ;AAAU,UAAM,IAAI,IAAI,GAAG;AAAA,WACrC,EAAE,eAAe,MAAM;AAC/B,UAAM,IAAI,UAAU,yCAAyC,KAAK;AAAA,EACnE;AACA,MAAI,IAAI,aAAa,SAAS;AAC7B,UAAM,IAAI,UAAU,+BAA+B;AAAA,EACpD;AACA,SAAO,oBAAoB,GAAG;AAC/B;AACA,SAAS,oBAAoB,KAAU;AACtC,MAAI,IAAI,aAAa,IAAI;AACxB,UAAM,IAAI,UAAU,+BAA+B;AAAA,EACpD;AACA,QAAM,WAAW,IAAI;AACrB,WAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACzC,QAAI,SAAS,CAAC,MAAM,KAAK;AAExB,YAAM,QAAQ,SAAS,YAAY,IAAI,CAAC,IAAK;AAC7C,UAAI,SAAS,IAAI,CAAC,MAAM,OAAO,UAAU,KAAK;AAC7C,cAAM,IAAI;AAAA,UACT;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACA,SAAO,mBAAmB,QAAQ;AACnC;AAEO,IAAM,qBAAqB;AAClC,IAAM,eAAe;AACrB,IAAM,iBAAiB;AACvB,IAAM,eAAe;AACrB,IAAM,sBAAsB;AAC5B,IAAM,WAAW;AAEV,SAAS,cAAc,UAAuB;AAGpD,QAAM,SAAS,IAAI,IAAI,SAAS;AAChC,MAAI,WAAW,KAAK,QAAQ,QAAQ;AAEpC,QAAM,eAAe,SAAS,WAAW,SAAS,SAAS,CAAC;AAC5D,MACC,iBAAiB,sBACjB,SAAS,SAAS,SAAS,CAAC,MAAM,KAAK,KACtC;AACD,gBAAY;AAAA,EACb;AACA,SAAO,WAAW,gBAAgB,QAAQ;AAC1C,SAAO;AACR;AACA,SAAS,gBAAgB,UAA0B;AAClD,MAAI,SAAS,SAAS,GAAG,GAAG;AAC3B,eAAW,SAAS,QAAQ,cAAc,KAAK;AAAA,EAChD;AAEA,MAAI,SAAS,SAAS,IAAI,GAAG;AAC5B,eAAW,SAAS,QAAQ,gBAAgB,KAAK;AAAA,EAClD;AACA,MAAI,SAAS,SAAS,IAAI,GAAG;AAC5B,eAAW,SAAS,QAAQ,cAAc,KAAK;AAAA,EAChD;AACA,MAAI,SAAS,SAAS,IAAI,GAAG;AAC5B,eAAW,SAAS,QAAQ,qBAAqB,KAAK;AAAA,EACvD;AACA,MAAI,SAAS,SAAS,GAAI,GAAG;AAC5B,eAAW,SAAS,QAAQ,UAAU,KAAK;AAAA,EAC5C;AACA,SAAO;AACR;AAEA,IAAO,cAAQ;AAAA,EACd;AAAA,EACA;AACD;",
5
+ "names": []
6
+ }
@@ -0,0 +1,17 @@
1
+ // src/worker/lib/node/vm.ts
2
+ import assert from "node:assert";
3
+ var unsafeEval;
4
+ function _setUnsafeEval(newUnsafeEval) {
5
+ unsafeEval = newUnsafeEval;
6
+ }
7
+ function runInThisContext(code, options) {
8
+ assert(unsafeEval !== void 0);
9
+ return unsafeEval.eval(code, options?.filename);
10
+ }
11
+ var vm_default = { runInThisContext };
12
+ export {
13
+ _setUnsafeEval,
14
+ vm_default as default,
15
+ runInThisContext
16
+ };
17
+ //# sourceMappingURL=vm.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/worker/lib/node/vm.ts"],
4
+ "mappings": ";AAAA,OAAO,YAAY;AAGnB,IAAI;AACG,SAAS,eAAe,eAA2B;AACzD,eAAa;AACd;AAEO,SAAS,iBAAiB,MAAc,SAAgC;AAC9E,SAAO,eAAe,MAAS;AAC/B,SAAO,WAAW,KAAK,MAAM,SAAS,QAAQ;AAC/C;AAEA,IAAO,aAAQ,EAAE,iBAAiB;",
5
+ "names": []
6
+ }
@@ -0,0 +1,6 @@
1
+ // src/worker/lib/tinypool.ts
2
+ var workerId = 0;
3
+ export {
4
+ workerId
5
+ };
6
+ //# sourceMappingURL=tinypool.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/worker/lib/tinypool.ts"],
4
+ "mappings": ";AAAO,IAAM,WAAW;",
5
+ "names": []
6
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@cloudflare/vitest-pool-workers",
3
+ "version": "0.1.0",
4
+ "main": "dist/pool/index.mjs",
5
+ "types": "test/cloudflare-test.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/pool/index.mjs",
9
+ "types": "./test/cloudflare-test.d.ts"
10
+ },
11
+ "./config": {
12
+ "import": "./dist/config/index.cjs",
13
+ "require": "./dist/config/index.cjs",
14
+ "types": "./dist/config/index.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "test/cloudflare-test.d.ts"
20
+ ],
21
+ "devDependencies": {
22
+ "@cloudflare/workers-types": "^4.20231121.0",
23
+ "@types/node": "20.8.3",
24
+ "capnp-ts": "^0.7.0",
25
+ "capnpc-ts": "^0.7.0",
26
+ "undici": "5.28.3",
27
+ "@cloudflare/eslint-config-worker": "1.1.0",
28
+ "@cloudflare/workers-tsconfig": "0.0.0"
29
+ },
30
+ "dependencies": {
31
+ "birpc": "0.2.14",
32
+ "cjs-module-lexer": "^1.2.3",
33
+ "devalue": "^4.3.0",
34
+ "esbuild": "0.17.19",
35
+ "zod": "^3.20.6",
36
+ "miniflare": "3.20240304.1",
37
+ "wrangler": "3.34.0"
38
+ },
39
+ "peerDependencies": {
40
+ "@vitest/runner": "1.3.0",
41
+ "@vitest/snapshot": "1.3.0",
42
+ "vitest": "1.3.0"
43
+ },
44
+ "workers-sdk": {
45
+ "prerelease": true
46
+ },
47
+ "scripts": {
48
+ "build": "node scripts/bundle.mjs && tsc -p tsconfig.emit.json",
49
+ "dev": "node scripts/bundle.mjs watch",
50
+ "check:lint": "eslint .",
51
+ "check:type": "tsc && tsc -p src/worker/tsconfig.json && tsc -p test/tsconfig.json",
52
+ "test": "vitest --config vitest.workers.config.ts --reporter=basic",
53
+ "test:ci": "vitest run --config vitest.workers.config.ts --reporter=basic",
54
+ "capnp:rtti": "capnpc -o ts scripts/rtti/rtti.capnp"
55
+ }
56
+ }
@@ -0,0 +1,278 @@
1
+ declare module "cloudflare:test" {
2
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
3
+ interface ProvidedEnv {}
4
+
5
+ /**
6
+ * 2nd argument passed to modules-format exported handlers. Contains bindings
7
+ * configured in top-level `miniflare` pool options. To configure the type
8
+ * of this value, use an ambient module type:
9
+ *
10
+ * ```ts
11
+ * declare module "cloudflare:test" {
12
+ * interface ProvidedEnv {
13
+ * NAMESPACE: KVNamespace;
14
+ * }
15
+ *
16
+ * // ...or if you have an existing `Env` type...
17
+ * interface ProvidedEnv extends Env {}
18
+ * }
19
+ * ```
20
+ */
21
+ export const env: ProvidedEnv;
22
+
23
+ /**
24
+ * Service binding to the default export defined in the `main` worker. Note
25
+ * this `main` worker runs in the same isolate/context as tests, so any global
26
+ * mocks will apply to it too.
27
+ */
28
+ export const SELF: Fetcher;
29
+
30
+ /**
31
+ * Declarative interface for mocking outbound `fetch()` requests. Deactivated
32
+ * by default and reset before running each test file. Only mocks `fetch()`
33
+ * requests for the current test runner worker. Auxiliary workers should mock
34
+ * `fetch()`es with the Miniflare `fetchMock`/`outboundService` options.
35
+ */
36
+ export const fetchMock: MockAgent;
37
+
38
+ /**
39
+ * Runs `callback` inside the Durable Object pointed-to by `stub`'s context.
40
+ * Conceptually, this temporarily replaces your Durable Object's `fetch()`
41
+ * handler with `callback`, then sends a request to it, returning the result.
42
+ * This can be used to call/spy-on Durable Object instance methods or seed/get
43
+ * persisted data. Note this can only be used with `stub`s pointing to Durable
44
+ * Objects defined in the `main` worker.
45
+ */
46
+ export function runInDurableObject<O extends DurableObject, R>(
47
+ stub: DurableObjectStub,
48
+ callback: (instance: O, state: DurableObjectState) => R | Promise<R>
49
+ ): Promise<R>;
50
+ /**
51
+ * Immediately runs and removes the Durable Object pointed-to by `stub`'s
52
+ * alarm if one is scheduled. Returns `true` if an alarm ran, and `false`
53
+ * otherwise. Note this can only be used with `stub`s pointing to Durable
54
+ * Objects defined in the `main` worker.
55
+ */
56
+ export function runDurableObjectAlarm(
57
+ stub: DurableObjectStub
58
+ ): Promise<boolean /* ran */>;
59
+ /**
60
+ * Gets the IDs of all objects that have been created in the `namespace`.
61
+ * Respects `isolatedStorage` if enabled, i.e. objects created in a different
62
+ * test won't be returned.
63
+ */
64
+ export function listDurableObjectIds(
65
+ namespace: DurableObjectNamespace
66
+ ): Promise<DurableObjectId[]>;
67
+
68
+ /**
69
+ * Creates an instance of `ExecutionContext` for use as the 3rd argument to
70
+ * modules-format exported handlers.
71
+ */
72
+ export function createExecutionContext(): ExecutionContext;
73
+ /**
74
+ * Waits for all `ExecutionContext#waitUntil()`ed `Promise`s to settle. Only
75
+ * accepts instances of `ExecutionContext` returned by
76
+ * `createExecutionContext()`.
77
+ */
78
+ export function waitOnExecutionContext(ctx: ExecutionContext): Promise<void>;
79
+ /**
80
+ * Creates an instance of `ScheduledController` for use as the 1st argument to
81
+ * modules-format `scheduled()` exported handlers.
82
+ */
83
+ export function createScheduledController(
84
+ options?: FetcherScheduledOptions
85
+ ): ScheduledController;
86
+ /**
87
+ * Creates an instance of `MessageBatch` for use as the 1st argument to
88
+ * modules-format `queue()` exported handlers.
89
+ */
90
+ export function createMessageBatch<Body = unknown>(
91
+ queueName: string,
92
+ messages: ServiceBindingQueueMessage<Body>[]
93
+ ): MessageBatch<Body>;
94
+ /**
95
+ * Gets the ack/retry state of messages in the `MessageBatch`, and waits for
96
+ * all `ExecutionContext#waitUntil()`ed `Promise`s to settle. Only accepts
97
+ * instances of `MessageBatch` returned by `createMessageBatch()`, and
98
+ * instances of `ExecutionContext` returned by `createExecutionContext()`.
99
+ */
100
+ export function getQueueResult(
101
+ batch: MessageBatch,
102
+ ctx: ExecutionContext
103
+ ): Promise<FetcherQueueResult>;
104
+
105
+ export interface D1Migration {
106
+ name: string;
107
+ queries: string[];
108
+ }
109
+
110
+ /**
111
+ * Applies all un-applied `migrations` to database `db`, recording migrations
112
+ * state in the `migrationsTableName` table. `migrationsTableName` defaults to
113
+ * `d1_migrations`. Call the `readD1Migrations()` function from the
114
+ * `@cloudflare/vitest-pool-workers/config` package inside Node.js to get the
115
+ * `migrations` array.
116
+ */
117
+ export function applyD1Migrations(
118
+ db: D1Database,
119
+ migrations: D1Migration[],
120
+ migrationsTableName?: string
121
+ ): Promise<void>;
122
+
123
+ // Taken from `undici` (https://github.com/nodejs/undici/tree/main/types) with
124
+ // no dependency on `@types/node` and with unusable functions removed
125
+ //
126
+ // MIT License
127
+ //
128
+ // Copyright (c) Matteo Collina and Undici contributors
129
+ //
130
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
131
+ // of this software and associated documentation files (the "Software"), to deal
132
+ // in the Software without restriction, including without limitation the rights
133
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
134
+ // copies of the Software, and to permit persons to whom the Software is
135
+ // furnished to do so, subject to the following conditions:
136
+ //
137
+ // The above copyright notice and this permission notice shall be included in all
138
+ // copies or substantial portions of the Software.
139
+ //
140
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
141
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
142
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
143
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
144
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
145
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
146
+ // SOFTWARE.
147
+
148
+ type IncomingHttpHeaders = Record<string, string | string[] | undefined>;
149
+ type Buffer = Uint8Array;
150
+
151
+ /** The scope associated with a mock dispatch. */
152
+ abstract class MockScope<TData extends object = object> {
153
+ /** Delay a reply by a set amount of time in ms. */
154
+ delay(waitInMs: number): MockScope<TData>;
155
+ /** Persist the defined mock data for the associated reply. It will return the defined mock data indefinitely. */
156
+ persist(): MockScope<TData>;
157
+ /** Define a reply for a set amount of matching requests. */
158
+ times(repeatTimes: number): MockScope<TData>;
159
+ }
160
+
161
+ /** The interceptor for a Mock. */
162
+ abstract class MockInterceptor {
163
+ /** Mock an undici request with the defined reply. */
164
+ reply<TData extends object = object>(
165
+ replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback<TData>
166
+ ): MockScope<TData>;
167
+ reply<TData extends object = object>(
168
+ statusCode: number,
169
+ data?:
170
+ | TData
171
+ | Buffer
172
+ | string
173
+ | MockInterceptor.MockResponseDataHandler<TData>,
174
+ responseOptions?: MockInterceptor.MockResponseOptions
175
+ ): MockScope<TData>;
176
+ /** Mock an undici request by throwing the defined reply error. */
177
+ replyWithError<TError extends Error = Error>(error: TError): MockScope;
178
+ /** Set default reply headers on the interceptor for subsequent mocked replies. */
179
+ defaultReplyHeaders(headers: IncomingHttpHeaders): MockInterceptor;
180
+ /** Set default reply trailers on the interceptor for subsequent mocked replies. */
181
+ defaultReplyTrailers(trailers: Record<string, string>): MockInterceptor;
182
+ /** Set automatically calculated content-length header on subsequent mocked replies. */
183
+ replyContentLength(): MockInterceptor;
184
+ }
185
+ namespace MockInterceptor {
186
+ /** MockInterceptor options. */
187
+ export interface Options {
188
+ /** Path to intercept on. */
189
+ path: string | RegExp | ((path: string) => boolean);
190
+ /** Method to intercept on. Defaults to GET. */
191
+ method?: string | RegExp | ((method: string) => boolean);
192
+ /** Body to intercept on. */
193
+ body?: string | RegExp | ((body: string) => boolean);
194
+ /** Headers to intercept on. */
195
+ headers?: // eslint-disable-next-line unused-imports/no-unused-vars
196
+ | Record<string, string | RegExp | ((body: string) => boolean)>
197
+ | ((headers: Record<string, string>) => boolean);
198
+ /** Query params to intercept on */
199
+ query?: Record<string, unknown>;
200
+ }
201
+ export interface MockDispatch<
202
+ TData extends object = object,
203
+ TError extends Error = Error
204
+ > extends Options {
205
+ times: number | null;
206
+ persist: boolean;
207
+ consumed: boolean;
208
+ data: MockDispatchData<TData, TError>;
209
+ }
210
+ export interface MockDispatchData<
211
+ TData extends object = object,
212
+ TError extends Error = Error
213
+ > extends MockResponseOptions {
214
+ error: TError | null;
215
+ statusCode?: number;
216
+ data?: TData | string;
217
+ }
218
+ export interface MockResponseOptions {
219
+ headers?: IncomingHttpHeaders;
220
+ trailers?: Record<string, string>;
221
+ }
222
+ export interface MockResponseCallbackOptions {
223
+ path: string;
224
+ origin: string;
225
+ method: string;
226
+ body?: BodyInit;
227
+ headers: Headers | Record<string, string>;
228
+ maxRedirections: number;
229
+ }
230
+ export type MockResponseDataHandler<TData extends object = object> = (
231
+ opts: MockResponseCallbackOptions
232
+ ) => TData | Buffer | string;
233
+ export type MockReplyOptionsCallback<TData extends object = object> = (
234
+ opts: MockResponseCallbackOptions
235
+ ) => {
236
+ statusCode: number;
237
+ data?: TData | Buffer | string;
238
+ responseOptions?: MockResponseOptions;
239
+ };
240
+ }
241
+
242
+ interface Interceptable {
243
+ /** Intercepts any matching requests that use the same origin as this mock client. */
244
+ intercept(options: MockInterceptor.Options): MockInterceptor;
245
+ }
246
+
247
+ interface PendingInterceptor extends MockInterceptor.MockDispatch {
248
+ origin: string;
249
+ }
250
+ interface PendingInterceptorsFormatter {
251
+ format(pendingInterceptors: readonly PendingInterceptor[]): string;
252
+ }
253
+
254
+ /** A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. */
255
+ abstract class MockAgent {
256
+ /** Creates and retrieves mock Dispatcher instances which can then be used to intercept HTTP requests. If the number of connections on the mock agent is set to 1, a MockClient instance is returned. Otherwise a MockPool instance is returned. */
257
+ // eslint-disable-next-line no-shadow
258
+ get(origin: string | RegExp | ((origin: string) => boolean)): Interceptable;
259
+
260
+ /** Disables mocking in MockAgent. */
261
+ deactivate(): void;
262
+ /** Enables mocking in a MockAgent instance. When instantiated, a MockAgent is automatically activated. Therefore, this method is only effective after MockAgent.deactivate has been called. */
263
+ activate(): void;
264
+
265
+ /** Define host matchers so only matching requests that aren't intercepted by the mock dispatchers will be attempted. */
266
+ enableNetConnect(
267
+ // eslint-disable-next-line no-shadow
268
+ host?: string | RegExp | ((host: string) => boolean)
269
+ ): void;
270
+ /** Causes all requests to throw when requests are not matched in a MockAgent intercept. */
271
+ disableNetConnect(): void;
272
+
273
+ pendingInterceptors(): PendingInterceptor[];
274
+ assertNoPendingInterceptors(options?: {
275
+ pendingInterceptorsFormatter?: PendingInterceptorsFormatter;
276
+ }): void;
277
+ }
278
+ }