wrangler 0.0.0-e043479 → 0.0.0-e0850ad1
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/Cloudflare_CA.pem +18 -0
- package/README.md +47 -16
- package/bin/wrangler.js +118 -32
- package/kv-asset-handler.js +1 -0
- package/package.json +148 -78
- package/templates/__tests__/pages-dev-util.test.ts +128 -0
- package/templates/__tests__/tsconfig-sanity.ts +12 -0
- package/templates/__tests__/tsconfig.json +8 -0
- package/templates/checked-fetch.js +30 -0
- package/templates/facade.d.ts +17 -0
- package/templates/gitignore +172 -0
- package/templates/init-tests/test-jest-new-worker.js +23 -0
- package/templates/init-tests/test-vitest-new-worker.js +24 -0
- package/templates/init-tests/test-vitest-new-worker.ts +25 -0
- package/templates/middleware/common.ts +67 -0
- package/templates/middleware/loader-modules.ts +137 -0
- package/templates/middleware/loader-sw.ts +228 -0
- package/templates/middleware/middleware-d1-beta.d.ts +3 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +33 -0
- package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
- package/templates/middleware/middleware-multiworker-dev.ts +59 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +15 -0
- package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
- package/templates/middleware/middleware-serve-static-assets.ts +56 -0
- package/templates/new-worker-scheduled.js +17 -0
- package/templates/new-worker-scheduled.ts +32 -0
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +22 -4
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-dev-pipeline.ts +31 -0
- package/templates/pages-dev-util.ts +55 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +190 -0
- package/templates/pages-template-worker.ts +198 -0
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +105 -0
- package/templates/tsconfig.json +6 -103
- package/wrangler-dist/cli.d.ts +1743 -0
- package/wrangler-dist/cli.js +142279 -130711
- package/wrangler-dist/wasm-sync.wasm +0 -0
- package/import_meta_url.js +0 -3
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +0 -1
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/pages/functions/buildWorker.ts +0 -62
- package/pages/functions/filepath-routing.test.ts +0 -123
- package/pages/functions/filepath-routing.ts +0 -178
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -151
- package/pages/functions/template-worker.ts +0 -130
- package/src/__tests__/configuration.test.ts +0 -2186
- package/src/__tests__/dev.test.tsx +0 -247
- package/src/__tests__/guess-worker-format.test.ts +0 -66
- package/src/__tests__/helpers/clipboardy-mock.js +0 -4
- package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/__tests__/helpers/mock-account-id.ts +0 -44
- package/src/__tests__/helpers/mock-bin.ts +0 -36
- package/src/__tests__/helpers/mock-cfetch.ts +0 -207
- package/src/__tests__/helpers/mock-console.ts +0 -70
- package/src/__tests__/helpers/mock-dialogs.ts +0 -65
- package/src/__tests__/helpers/mock-kv.ts +0 -40
- package/src/__tests__/helpers/mock-stdin.ts +0 -103
- package/src/__tests__/helpers/mock-user.ts +0 -27
- package/src/__tests__/helpers/mock-web-socket.ts +0 -29
- package/src/__tests__/helpers/run-in-tmp.ts +0 -29
- package/src/__tests__/helpers/run-wrangler.ts +0 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
- package/src/__tests__/index.test.ts +0 -866
- package/src/__tests__/jest.setup.ts +0 -69
- package/src/__tests__/kv.test.ts +0 -1408
- package/src/__tests__/logout.test.ts +0 -50
- package/src/__tests__/package-manager.test.ts +0 -206
- package/src/__tests__/parse.test.ts +0 -322
- package/src/__tests__/publish.test.ts +0 -2139
- package/src/__tests__/r2.test.ts +0 -198
- package/src/__tests__/secret.test.ts +0 -425
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/__tests__/tail.test.ts +0 -564
- package/src/__tests__/whoami.test.tsx +0 -86
- package/src/api/form_data.ts +0 -217
- package/src/api/preview.ts +0 -147
- package/src/api/worker.ts +0 -186
- package/src/bundle.ts +0 -133
- package/src/cfetch/index.ts +0 -104
- package/src/cfetch/internal.ts +0 -133
- package/src/cli.ts +0 -19
- package/src/config/README.md +0 -107
- package/src/config/config.ts +0 -270
- package/src/config/diagnostics.ts +0 -80
- package/src/config/environment.ts +0 -305
- package/src/config/index.ts +0 -52
- package/src/config/validation-helpers.ts +0 -370
- package/src/config/validation.ts +0 -1019
- package/src/dev.tsx +0 -960
- package/src/dialogs.tsx +0 -77
- package/src/entry.ts +0 -57
- package/src/environment-variables.ts +0 -33
- package/src/guess-worker-format.ts +0 -68
- package/src/index.tsx +0 -2302
- package/src/inspect.ts +0 -609
- package/src/kv.tsx +0 -339
- package/src/module-collection.ts +0 -170
- package/src/open-in-browser.ts +0 -13
- package/src/package-manager.ts +0 -126
- package/src/pages.tsx +0 -1045
- package/src/parse.ts +0 -183
- package/src/paths.ts +0 -26
- package/src/proxy.ts +0 -376
- package/src/publish.ts +0 -363
- package/src/r2.ts +0 -50
- package/src/reporting.ts +0 -115
- package/src/sites.tsx +0 -238
- package/src/tail/filters.ts +0 -279
- package/src/tail/index.ts +0 -299
- package/src/tail/printing.ts +0 -62
- package/src/user.tsx +0 -1063
- package/src/whoami.tsx +0 -69
- package/templates/static-asset-facade.js +0 -47
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
- package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
- package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
- package/vendor/wrangler-mime/CHANGELOG.md +0 -289
- package/vendor/wrangler-mime/LICENSE +0 -21
- package/vendor/wrangler-mime/Mime.js +0 -97
- package/vendor/wrangler-mime/README.md +0 -187
- package/vendor/wrangler-mime/cli.js +0 -46
- package/vendor/wrangler-mime/index.js +0 -4
- package/vendor/wrangler-mime/lite.js +0 -4
- package/vendor/wrangler-mime/package.json +0 -52
- package/vendor/wrangler-mime/types/other.js +0 -1
- package/vendor/wrangler-mime/types/standard.js +0 -1
- package/wrangler-dist/cli.js.map +0 -7
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import patchConsole from "patch-console";
|
|
3
|
-
import Dev from "../dev";
|
|
4
|
-
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
|
|
5
|
-
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
|
|
6
|
-
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
7
|
-
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
8
|
-
import { runWrangler } from "./helpers/run-wrangler";
|
|
9
|
-
import writeWranglerToml from "./helpers/write-wrangler-toml";
|
|
10
|
-
|
|
11
|
-
describe("Dev component", () => {
|
|
12
|
-
beforeEach(() => {});
|
|
13
|
-
mockAccountId();
|
|
14
|
-
mockApiToken();
|
|
15
|
-
runInTempDir();
|
|
16
|
-
const std = mockConsoleMethods();
|
|
17
|
-
afterEach(() => {
|
|
18
|
-
(Dev as jest.Mock).mockClear();
|
|
19
|
-
patchConsole(() => {});
|
|
20
|
-
unsetAllMocks();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
describe("entry-points", () => {
|
|
24
|
-
it("should error if there is no entry-point specified", async () => {
|
|
25
|
-
writeWranglerToml();
|
|
26
|
-
|
|
27
|
-
await expect(
|
|
28
|
-
runWrangler("dev")
|
|
29
|
-
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
30
|
-
`"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field."`
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
34
|
-
expect(std.err).toMatchInlineSnapshot(`
|
|
35
|
-
"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.
|
|
36
|
-
|
|
37
|
-
[32m%s[0m If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
|
|
38
|
-
`);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
describe("host", () => {
|
|
43
|
-
it("should resolve a host to its zone", async () => {
|
|
44
|
-
writeWranglerToml({
|
|
45
|
-
main: "index.js",
|
|
46
|
-
});
|
|
47
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
48
|
-
mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
|
|
49
|
-
await runWrangler("dev --host some-host.com");
|
|
50
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
|
|
51
|
-
host: "some-host.com",
|
|
52
|
-
id: "some-zone-id",
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("should read wrangler.toml's dev.host", async () => {
|
|
57
|
-
writeWranglerToml({
|
|
58
|
-
main: "index.js",
|
|
59
|
-
dev: {
|
|
60
|
-
host: "some-host.com",
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
64
|
-
mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
|
|
65
|
-
await runWrangler("dev");
|
|
66
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
|
|
67
|
-
"some-host.com"
|
|
68
|
-
);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("should read --route", async () => {
|
|
72
|
-
writeWranglerToml({
|
|
73
|
-
main: "index.js",
|
|
74
|
-
});
|
|
75
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
76
|
-
mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
|
|
77
|
-
await runWrangler("dev --route http://some-host.com/some/path/*");
|
|
78
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
|
|
79
|
-
"some-host.com"
|
|
80
|
-
);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it("should read wrangler.toml's routes", async () => {
|
|
84
|
-
writeWranglerToml({
|
|
85
|
-
main: "index.js",
|
|
86
|
-
routes: [
|
|
87
|
-
"http://some-host.com/some/path/*",
|
|
88
|
-
"http://some-other-host.com/path/*",
|
|
89
|
-
],
|
|
90
|
-
});
|
|
91
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
92
|
-
mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
|
|
93
|
-
await runWrangler("dev");
|
|
94
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
|
|
95
|
-
"some-host.com"
|
|
96
|
-
);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("should read wrangler.toml's environment specific routes", async () => {
|
|
100
|
-
writeWranglerToml({
|
|
101
|
-
main: "index.js",
|
|
102
|
-
routes: [
|
|
103
|
-
"http://a-host.com/some/path/*",
|
|
104
|
-
"http://another-host.com/path/*",
|
|
105
|
-
],
|
|
106
|
-
env: {
|
|
107
|
-
staging: {
|
|
108
|
-
routes: [
|
|
109
|
-
"http://some-host.com/some/path/*",
|
|
110
|
-
"http://some-other-host.com/path/*",
|
|
111
|
-
],
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
116
|
-
mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
|
|
117
|
-
await runWrangler("dev --env staging");
|
|
118
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
|
|
119
|
-
"some-host.com"
|
|
120
|
-
);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
|
|
124
|
-
writeWranglerToml({
|
|
125
|
-
main: "index.js",
|
|
126
|
-
});
|
|
127
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
128
|
-
mockGetZones("111.222.333.some-host.com", []);
|
|
129
|
-
mockGetZones("222.333.some-host.com", []);
|
|
130
|
-
mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
|
|
131
|
-
await runWrangler("dev --host 111.222.333.some-host.com");
|
|
132
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
|
|
133
|
-
host: "111.222.333.some-host.com",
|
|
134
|
-
id: "some-zone-id",
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("should, in order, use args.host/config.dev.host/args.routes/config.route/config.routes", async () => {
|
|
139
|
-
// This test might seem like it's testing implmentation details, but let's be specific and consider it a spec
|
|
140
|
-
|
|
141
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
142
|
-
|
|
143
|
-
// config.routes
|
|
144
|
-
mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
|
|
145
|
-
writeWranglerToml({
|
|
146
|
-
main: "index.js",
|
|
147
|
-
routes: ["http://5.some-host.com/some/path/*"],
|
|
148
|
-
});
|
|
149
|
-
await runWrangler("dev");
|
|
150
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
|
|
151
|
-
host: "5.some-host.com",
|
|
152
|
-
id: "some-zone-id-5",
|
|
153
|
-
});
|
|
154
|
-
(Dev as jest.Mock).mockClear();
|
|
155
|
-
|
|
156
|
-
// config.route
|
|
157
|
-
mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
|
|
158
|
-
writeWranglerToml({
|
|
159
|
-
main: "index.js",
|
|
160
|
-
route: "https://4.some-host.com/some/path/*",
|
|
161
|
-
routes: ["http://5.some-host.com/some/path/*"],
|
|
162
|
-
});
|
|
163
|
-
await runWrangler("dev");
|
|
164
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
|
|
165
|
-
host: "4.some-host.com",
|
|
166
|
-
id: "some-zone-id-4",
|
|
167
|
-
});
|
|
168
|
-
(Dev as jest.Mock).mockClear();
|
|
169
|
-
|
|
170
|
-
// --routes
|
|
171
|
-
mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
|
|
172
|
-
writeWranglerToml({
|
|
173
|
-
main: "index.js",
|
|
174
|
-
route: "https://4.some-host.com/some/path/*",
|
|
175
|
-
routes: ["http://5.some-host.com/some/path/*"],
|
|
176
|
-
});
|
|
177
|
-
await runWrangler("dev --routes http://3.some-host.com/some/path/*");
|
|
178
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
|
|
179
|
-
host: "3.some-host.com",
|
|
180
|
-
id: "some-zone-id-3",
|
|
181
|
-
});
|
|
182
|
-
(Dev as jest.Mock).mockClear();
|
|
183
|
-
|
|
184
|
-
// config.dev.host
|
|
185
|
-
mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
|
|
186
|
-
writeWranglerToml({
|
|
187
|
-
main: "index.js",
|
|
188
|
-
dev: {
|
|
189
|
-
host: `2.some-host.com`,
|
|
190
|
-
},
|
|
191
|
-
route: "4.some-host.com/some/path/*",
|
|
192
|
-
routes: ["5.some-host.com/some/path/*"],
|
|
193
|
-
});
|
|
194
|
-
await runWrangler("dev --routes http://3.some-host.com/some/path/*");
|
|
195
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
|
|
196
|
-
host: "2.some-host.com",
|
|
197
|
-
id: "some-zone-id-2",
|
|
198
|
-
});
|
|
199
|
-
(Dev as jest.Mock).mockClear();
|
|
200
|
-
|
|
201
|
-
// --host
|
|
202
|
-
mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
|
|
203
|
-
writeWranglerToml({
|
|
204
|
-
main: "index.js",
|
|
205
|
-
dev: {
|
|
206
|
-
host: `2.some-host.com`,
|
|
207
|
-
},
|
|
208
|
-
route: "4.some-host.com/some/path/*",
|
|
209
|
-
routes: ["5.some-host.com/some/path/*"],
|
|
210
|
-
});
|
|
211
|
-
await runWrangler(
|
|
212
|
-
"dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
|
|
213
|
-
);
|
|
214
|
-
expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
|
|
215
|
-
host: "1.some-host.com",
|
|
216
|
-
id: "some-zone-id-1",
|
|
217
|
-
});
|
|
218
|
-
(Dev as jest.Mock).mockClear();
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it("should error if a host can't resolve to a zone", async () => {
|
|
222
|
-
writeWranglerToml({
|
|
223
|
-
main: "index.js",
|
|
224
|
-
});
|
|
225
|
-
fs.writeFileSync("index.js", `export default {};`);
|
|
226
|
-
mockGetZones("some-host.com", []);
|
|
227
|
-
await expect(
|
|
228
|
-
runWrangler("dev --host some-host.com")
|
|
229
|
-
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
230
|
-
`"Could not find zone for some-host.com"`
|
|
231
|
-
);
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
function mockGetZones(domain: string, zones: { id: string }[] = []) {
|
|
237
|
-
const removeMock = setMockResponse(
|
|
238
|
-
"/zones",
|
|
239
|
-
"GET",
|
|
240
|
-
(_urlPieces, _init, queryParams) => {
|
|
241
|
-
expect([...queryParams.entries()]).toEqual([["name", domain]]);
|
|
242
|
-
// Because the API URL `/zones` is the same for each request, we can get into a situation where earlier mocks get triggered for later requests. So, we simply clear the mock on every trigger.
|
|
243
|
-
removeMock();
|
|
244
|
-
return zones;
|
|
245
|
-
}
|
|
246
|
-
);
|
|
247
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { writeFile } from "fs/promises";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import guessWorkerFormat from "../guess-worker-format";
|
|
4
|
-
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
5
|
-
|
|
6
|
-
describe("guess worker format", () => {
|
|
7
|
-
runInTempDir();
|
|
8
|
-
it('should detect a "modules" worker', async () => {
|
|
9
|
-
await writeFile("./index.ts", "export default {};");
|
|
10
|
-
// Note that this isn't actually a valid worker, because it's missing
|
|
11
|
-
// a fetch handler. Regardless, our heuristic is simply to check for exports.
|
|
12
|
-
const guess = await guessWorkerFormat(
|
|
13
|
-
{
|
|
14
|
-
file: path.join(process.cwd(), "./index.ts"),
|
|
15
|
-
directory: process.cwd(),
|
|
16
|
-
},
|
|
17
|
-
undefined
|
|
18
|
-
);
|
|
19
|
-
expect(guess).toBe("modules");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should detect a "service-worker" worker', async () => {
|
|
23
|
-
await writeFile("./index.ts", "");
|
|
24
|
-
// Note that this isn't actually a valid worker, because it's missing
|
|
25
|
-
// a fetch listener. Regardless, our heuristic is simply to check for
|
|
26
|
-
// the lack of exports.
|
|
27
|
-
const guess = await guessWorkerFormat(
|
|
28
|
-
{
|
|
29
|
-
file: path.join(process.cwd(), "./index.ts"),
|
|
30
|
-
directory: process.cwd(),
|
|
31
|
-
},
|
|
32
|
-
undefined
|
|
33
|
-
);
|
|
34
|
-
expect(guess).toBe("service-worker");
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
|
|
38
|
-
await writeFile("./index.ts", "export default {};");
|
|
39
|
-
await expect(
|
|
40
|
-
guessWorkerFormat(
|
|
41
|
-
{
|
|
42
|
-
file: path.join(process.cwd(), "./index.ts"),
|
|
43
|
-
directory: process.cwd(),
|
|
44
|
-
},
|
|
45
|
-
"service-worker"
|
|
46
|
-
)
|
|
47
|
-
).rejects.toThrow(
|
|
48
|
-
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have ES module exports. Please pass `--format modules`, or simply remove the configuration."
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("should throw an error when the hint doesn't match the guess (service-worker - modules)", async () => {
|
|
53
|
-
await writeFile("./index.ts", "");
|
|
54
|
-
await expect(
|
|
55
|
-
guessWorkerFormat(
|
|
56
|
-
{
|
|
57
|
-
file: path.join(process.cwd(), "./index.ts"),
|
|
58
|
-
directory: process.cwd(),
|
|
59
|
-
},
|
|
60
|
-
"modules"
|
|
61
|
-
)
|
|
62
|
-
).rejects.toThrow(
|
|
63
|
-
"You configured this worker to be 'modules', but the file you are trying to build doesn't export a handler. Please pass `--format service-worker`, or simply remove the configuration."
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The typings file available at `@types/cmd-shim` are out of date.
|
|
3
|
-
*/
|
|
4
|
-
module "cmd-shim" {
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
* Create a cmd shim at `to` for the command line program at `from`.
|
|
8
|
-
*
|
|
9
|
-
*/
|
|
10
|
-
export default function cmdShim(from: string, to: string): Promise<void>;
|
|
11
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
const ORIGINAL_CLOUDFLARE_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
|
|
2
|
-
const ORIGINAL_CLOUDFLARE_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Mock the API token so that we don't need to read it from user configuration files.
|
|
6
|
-
*
|
|
7
|
-
* Note that you can remove any API token from the environment by setting the value to `null`.
|
|
8
|
-
* This is useful if a higher `describe()` block has already called `mockApiToken()`.
|
|
9
|
-
*/
|
|
10
|
-
export function mockApiToken({
|
|
11
|
-
apiToken = "some-api-token",
|
|
12
|
-
}: { apiToken?: string | null } = {}) {
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
if (apiToken === null) {
|
|
15
|
-
delete process.env.CLOUDFLARE_API_TOKEN;
|
|
16
|
-
} else {
|
|
17
|
-
process.env.CLOUDFLARE_API_TOKEN = apiToken;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
afterEach(() => {
|
|
21
|
-
process.env.CLOUDFLARE_API_TOKEN = ORIGINAL_CLOUDFLARE_API_TOKEN;
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Mock the current account ID so that we don't need to read it from configuration files.
|
|
27
|
-
*
|
|
28
|
-
* Note that you can remove any account ID from the environment by setting the value to `null`.
|
|
29
|
-
* This is useful if a higher `describe()` block has already called `mockAccountId()`.
|
|
30
|
-
*/
|
|
31
|
-
export function mockAccountId({
|
|
32
|
-
accountId = "some-account-id",
|
|
33
|
-
}: { accountId?: string | null } = {}) {
|
|
34
|
-
beforeEach(() => {
|
|
35
|
-
if (accountId === null) {
|
|
36
|
-
delete process.env.CLOUDFLARE_ACCOUNT_ID;
|
|
37
|
-
} else {
|
|
38
|
-
process.env.CLOUDFLARE_ACCOUNT_ID = accountId;
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
afterEach(() => {
|
|
42
|
-
process.env.CLOUDFLARE_ACCOUNT_ID = ORIGINAL_CLOUDFLARE_ACCOUNT_ID;
|
|
43
|
-
});
|
|
44
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { basename, resolve } from "node:path";
|
|
3
|
-
import cmdShim from "cmd-shim";
|
|
4
|
-
|
|
5
|
-
const nodeShebang = "#!/usr/bin/env node";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Create a binary file in a temp directory and make it available on the PATH.
|
|
9
|
-
*/
|
|
10
|
-
export async function mockBinary(
|
|
11
|
-
binaryName: string,
|
|
12
|
-
code: string
|
|
13
|
-
): Promise<() => void> {
|
|
14
|
-
// Ensure there is a directory to put the mock binary in.
|
|
15
|
-
const tmpDir = resolve(mkdtempSync(".mock-binary-"));
|
|
16
|
-
|
|
17
|
-
// Use a fake extension on Windows because we will create a cmd-shim to run the binary.
|
|
18
|
-
const extension = process.platform === "win32" ? ".x-mock-bin" : "";
|
|
19
|
-
const filePath = resolve(tmpDir, `${binaryName}${extension}`);
|
|
20
|
-
writeFileSync(filePath, nodeShebang + "\n" + code);
|
|
21
|
-
chmodSync(filePath, 0o777);
|
|
22
|
-
|
|
23
|
-
if (process.platform === "win32") {
|
|
24
|
-
await cmdShim(filePath, basename(filePath, ".x-mock-bin"));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Update PATH using the appropriate separator for the platform.
|
|
28
|
-
const oldPath = process.env.PATH;
|
|
29
|
-
const sep = process.platform === "win32" ? ";" : ":";
|
|
30
|
-
process.env.PATH = tmpDir + sep + oldPath;
|
|
31
|
-
|
|
32
|
-
return function unMock() {
|
|
33
|
-
rmSync(tmpDir, { recursive: true });
|
|
34
|
-
process.env.PATH = process.env.PATH?.replace(tmpDir + sep, "");
|
|
35
|
-
};
|
|
36
|
-
}
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import { URL, URLSearchParams } from "node:url";
|
|
2
|
-
import { pathToRegexp } from "path-to-regexp";
|
|
3
|
-
import { getCloudflareApiBaseUrl } from "../../cfetch";
|
|
4
|
-
import type { FetchResult } from "../../cfetch";
|
|
5
|
-
import type { RequestInit } from "undici";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The signature of the function that will handle a mock request.
|
|
9
|
-
*/
|
|
10
|
-
export type MockHandler<ResponseType> = (
|
|
11
|
-
uri: RegExpExecArray,
|
|
12
|
-
init: RequestInit,
|
|
13
|
-
queryParams: URLSearchParams
|
|
14
|
-
) => ResponseType | Promise<ResponseType>;
|
|
15
|
-
|
|
16
|
-
type RemoveMockFn = () => void;
|
|
17
|
-
|
|
18
|
-
interface MockFetch<ResponseType> {
|
|
19
|
-
regexp: RegExp;
|
|
20
|
-
method: string | undefined;
|
|
21
|
-
handler: MockHandler<ResponseType>;
|
|
22
|
-
}
|
|
23
|
-
const mocks: MockFetch<unknown>[] = [];
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The mock implementation of `cfApi.fetch()`.
|
|
27
|
-
*
|
|
28
|
-
* This function will attempt to match the given request to one of the mock handlers configured by calls to `setMock`.
|
|
29
|
-
*
|
|
30
|
-
* Once found the handler will be used to generate a mock response.
|
|
31
|
-
*/
|
|
32
|
-
export async function mockFetchInternal(
|
|
33
|
-
resource: string,
|
|
34
|
-
init: RequestInit = {},
|
|
35
|
-
queryParams: URLSearchParams = new URLSearchParams()
|
|
36
|
-
) {
|
|
37
|
-
for (const { regexp, method, handler } of mocks) {
|
|
38
|
-
const resourcePath = new URL(resource, getCloudflareApiBaseUrl()).pathname;
|
|
39
|
-
const uri = regexp.exec(resourcePath);
|
|
40
|
-
// Do the resource path and (if specified) the HTTP method match?
|
|
41
|
-
if (uri !== null && (!method || method === (init.method ?? "GET"))) {
|
|
42
|
-
// The `resource` regular expression will extract the labelled groups from the URL.
|
|
43
|
-
// These are passed through to the `handler` call, to allow it to do additional checks or behaviour.
|
|
44
|
-
return await handler(uri, init, queryParams); // TODO: should we have some kind of fallthrough system? we'll see.
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
throw new Error(
|
|
48
|
-
`no mocks found for ${init.method ?? "any HTTP"} request to ${resource}`
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Specify an expected resource path that is to be handled, resulting in a raw JSON response.
|
|
54
|
-
*
|
|
55
|
-
* @param resource The path of the resource to be matched.
|
|
56
|
-
* This can include wildcards whose value will be passed to the `handler`.
|
|
57
|
-
* @param handler The function that will generate the mock response for this request.
|
|
58
|
-
*/
|
|
59
|
-
export function setMockRawResponse<ResponseType>(
|
|
60
|
-
resource: string,
|
|
61
|
-
handler: MockHandler<ResponseType>
|
|
62
|
-
): RemoveMockFn;
|
|
63
|
-
/**
|
|
64
|
-
* Specify an expected resource path that is to be handled, resulting in a raw JSON response.
|
|
65
|
-
*
|
|
66
|
-
* @param resource The path of the resource to be matched.
|
|
67
|
-
* This can include wildcards whose value will be passed to the `handler`.
|
|
68
|
-
* @param method The HTTP method (e.g. GET, POST, etc) that the request must have to match this mock handler.
|
|
69
|
-
* @param handler The function that will generate the mock response for this request.
|
|
70
|
-
*/
|
|
71
|
-
export function setMockRawResponse<ResponseType>(
|
|
72
|
-
resource: string,
|
|
73
|
-
method: string,
|
|
74
|
-
handler: MockHandler<ResponseType>
|
|
75
|
-
): RemoveMockFn;
|
|
76
|
-
/**
|
|
77
|
-
* Specify an expected resource path that is to be handled, resulting in a raw JSON response.
|
|
78
|
-
*/
|
|
79
|
-
export function setMockRawResponse<ResponseType>(
|
|
80
|
-
resource: string,
|
|
81
|
-
...args: [string, MockHandler<ResponseType>] | [MockHandler<ResponseType>]
|
|
82
|
-
): RemoveMockFn {
|
|
83
|
-
const handler = args.pop() as MockHandler<ResponseType>;
|
|
84
|
-
const method = args.pop() as string;
|
|
85
|
-
const mock = {
|
|
86
|
-
resource,
|
|
87
|
-
method,
|
|
88
|
-
handler,
|
|
89
|
-
regexp: pathToRegexp(resource),
|
|
90
|
-
};
|
|
91
|
-
mocks.push(mock);
|
|
92
|
-
return () => {
|
|
93
|
-
const mockIndex = mocks.indexOf(mock);
|
|
94
|
-
if (mockIndex !== -1) {
|
|
95
|
-
mocks.splice(mockIndex, 1);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Specify an expected resource path that is to be handled, resulting in a `FetchRequest`.
|
|
102
|
-
*
|
|
103
|
-
* The mock `handler` should return the `result`, which will then be wrapped in a `FetchRequest` object.
|
|
104
|
-
*
|
|
105
|
-
* @param resource The path of the resource to be matched.
|
|
106
|
-
* This can include wildcards whose value will be passed to the `handler`.
|
|
107
|
-
* @param handler The function that will generate the mock response for this request.
|
|
108
|
-
*/
|
|
109
|
-
export function setMockResponse<ResponseType>(
|
|
110
|
-
resource: string,
|
|
111
|
-
handler: MockHandler<ResponseType>
|
|
112
|
-
): RemoveMockFn;
|
|
113
|
-
/**
|
|
114
|
-
* Specify an expected resource path that is to be handled, resulting in a FetchRequest..
|
|
115
|
-
*
|
|
116
|
-
* @param resource The path of the resource to be matched.
|
|
117
|
-
* This can include wildcards whose value will be passed to the `handler`.
|
|
118
|
-
* @param method The HTTP method (e.g. GET, POST, etc) that the request must have to match this mock handler.
|
|
119
|
-
* @param handler The function that will generate the mock response for this request.
|
|
120
|
-
*/
|
|
121
|
-
export function setMockResponse<ResponseType>(
|
|
122
|
-
resource: string,
|
|
123
|
-
method: string,
|
|
124
|
-
handler: MockHandler<ResponseType>
|
|
125
|
-
): RemoveMockFn;
|
|
126
|
-
/**
|
|
127
|
-
* Specify an expected resource path that is to be handled, resulting in a FetchRequest.
|
|
128
|
-
*/
|
|
129
|
-
export function setMockResponse<ResponseType>(
|
|
130
|
-
resource: string,
|
|
131
|
-
...args: [string, MockHandler<ResponseType>] | [MockHandler<ResponseType>]
|
|
132
|
-
): RemoveMockFn {
|
|
133
|
-
const handler = args.pop() as MockHandler<ResponseType>;
|
|
134
|
-
const method = args.pop() as string;
|
|
135
|
-
return setMockRawResponse(resource, method, (...handlerArgs) =>
|
|
136
|
-
createFetchResult(handler(...handlerArgs))
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* A helper to make it easier to create `FetchResult` objects in tests.
|
|
142
|
-
*/
|
|
143
|
-
export async function createFetchResult<ResponseType>(
|
|
144
|
-
result: ResponseType | Promise<ResponseType>,
|
|
145
|
-
success = true,
|
|
146
|
-
errors = [],
|
|
147
|
-
messages = [],
|
|
148
|
-
result_info?: unknown
|
|
149
|
-
): Promise<FetchResult<ResponseType>> {
|
|
150
|
-
return result_info
|
|
151
|
-
? {
|
|
152
|
-
result: await result,
|
|
153
|
-
success,
|
|
154
|
-
errors,
|
|
155
|
-
messages,
|
|
156
|
-
result_info,
|
|
157
|
-
}
|
|
158
|
-
: {
|
|
159
|
-
result: await result,
|
|
160
|
-
success,
|
|
161
|
-
errors,
|
|
162
|
-
messages,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Remove all the configured mock handlers.
|
|
168
|
-
*
|
|
169
|
-
* This should be called in an `afterEach()` block to ensure that mock handlers do not leak between tests.
|
|
170
|
-
*/
|
|
171
|
-
export function unsetAllMocks() {
|
|
172
|
-
mocks.length = 0;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* We special-case fetching the request for `kv:key get`, because it's
|
|
177
|
-
* the only cloudflare API endpoint that returns a plain string as the
|
|
178
|
-
* value, and not as the "standard" FetchResult-style json. Hence, we also
|
|
179
|
-
* special-case mocking it here.
|
|
180
|
-
*/
|
|
181
|
-
|
|
182
|
-
const kvGetMocks = new Map<string, string>();
|
|
183
|
-
|
|
184
|
-
export function mockFetchKVGetValue(
|
|
185
|
-
accountId: string,
|
|
186
|
-
namespaceId: string,
|
|
187
|
-
key: string
|
|
188
|
-
) {
|
|
189
|
-
const mapKey = `${accountId}/${namespaceId}/${key}`;
|
|
190
|
-
if (kvGetMocks.has(mapKey)) {
|
|
191
|
-
return kvGetMocks.get(mapKey);
|
|
192
|
-
}
|
|
193
|
-
throw new Error(`no mock value found for \`kv:key get\` - ${mapKey}`);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
export function setMockFetchKVGetValue(
|
|
197
|
-
accountId: string,
|
|
198
|
-
namespaceId: string,
|
|
199
|
-
key: string,
|
|
200
|
-
value: string
|
|
201
|
-
) {
|
|
202
|
-
kvGetMocks.set(`${accountId}/${namespaceId}/${key}`, value);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export function unsetMockFetchKVGetValues() {
|
|
206
|
-
kvGetMocks.clear();
|
|
207
|
-
}
|