wrangler 2.0.12 → 2.0.16
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/README.md +7 -1
- package/bin/wrangler.js +111 -57
- package/miniflare-dist/index.mjs +9 -2
- package/package.json +156 -154
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +30 -24
- package/src/__tests__/configuration.test.ts +3935 -3476
- package/src/__tests__/dev.test.tsx +1128 -979
- package/src/__tests__/guess-worker-format.test.ts +68 -68
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/faye-websocket.d.ts +4 -4
- package/src/__tests__/helpers/mock-account-id.ts +24 -24
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-cfetch.ts +92 -92
- package/src/__tests__/helpers/mock-console.ts +49 -39
- package/src/__tests__/helpers/mock-dialogs.ts +94 -71
- package/src/__tests__/helpers/mock-http-server.ts +30 -30
- package/src/__tests__/helpers/mock-istty.ts +65 -18
- package/src/__tests__/helpers/mock-kv.ts +26 -26
- package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
- package/src/__tests__/helpers/mock-process.ts +39 -0
- package/src/__tests__/helpers/mock-stdin.ts +82 -77
- package/src/__tests__/helpers/mock-web-socket.ts +21 -21
- package/src/__tests__/helpers/run-in-tmp.ts +27 -27
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/write-worker-source.ts +16 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +104 -104
- package/src/__tests__/index.test.ts +239 -234
- package/src/__tests__/init.test.ts +1605 -1250
- package/src/__tests__/jest.setup.ts +63 -33
- package/src/__tests__/kv.test.ts +1128 -1011
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/package-manager.test.ts +303 -303
- package/src/__tests__/pages.test.ts +1152 -652
- package/src/__tests__/parse.test.ts +252 -252
- package/src/__tests__/publish.test.ts +6371 -5622
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +133 -133
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +382 -377
- package/src/__tests__/tail.test.ts +530 -530
- package/src/__tests__/user.test.ts +123 -111
- package/src/__tests__/whoami.test.tsx +198 -117
- package/src/__tests__/worker-namespace.test.ts +327 -0
- package/src/abort.d.ts +1 -1
- package/src/api/dev.ts +49 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +157 -149
- package/src/cfetch/index.ts +80 -80
- package/src/cfetch/internal.ts +90 -83
- package/src/cli.ts +21 -7
- package/src/config/config.ts +204 -195
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +390 -357
- package/src/config/index.ts +206 -193
- package/src/config/validation-helpers.ts +366 -366
- package/src/config/validation.ts +1573 -1376
- package/src/config-cache.ts +79 -41
- package/src/create-worker-preview.ts +206 -136
- package/src/create-worker-upload-form.ts +247 -238
- package/src/dev/dev-vars.ts +13 -13
- package/src/dev/dev.tsx +329 -307
- package/src/dev/local.tsx +304 -275
- package/src/dev/remote.tsx +366 -224
- package/src/dev/use-esbuild.ts +126 -91
- package/src/dev.tsx +538 -0
- package/src/dialogs.tsx +97 -97
- package/src/durable.ts +87 -87
- package/src/entry.ts +234 -228
- package/src/environment-variables.ts +23 -23
- package/src/errors.ts +6 -6
- package/src/generate.ts +33 -0
- package/src/git-client.ts +42 -0
- package/src/https-options.ts +79 -79
- package/src/index.tsx +1775 -2763
- package/src/init.ts +549 -0
- package/src/inspect.ts +593 -593
- package/src/intl-polyfill.d.ts +123 -123
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +277 -277
- package/src/logger.ts +46 -39
- package/src/miniflare-cli/enum-keys.ts +8 -8
- package/src/miniflare-cli/index.ts +42 -31
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/module-collection.ts +212 -212
- package/src/open-in-browser.ts +4 -6
- package/src/package-manager.ts +123 -123
- package/src/pages/build.tsx +202 -0
- package/src/pages/constants.ts +7 -0
- package/src/pages/deployments.tsx +101 -0
- package/src/pages/dev.tsx +964 -0
- package/src/pages/functions/buildPlugin.ts +105 -0
- package/src/pages/functions/buildWorker.ts +151 -0
- package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/index.tsx +84 -0
- package/src/pages/projects.tsx +157 -0
- package/src/pages/publish.tsx +335 -0
- package/src/pages/types.ts +40 -0
- package/src/pages/upload.tsx +384 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +202 -138
- package/src/paths.ts +6 -6
- package/src/preview.ts +31 -0
- package/src/proxy.ts +400 -402
- package/src/publish.ts +667 -621
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +577 -0
- package/src/r2.ts +19 -19
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +271 -225
- package/src/tail/filters.ts +108 -108
- package/src/tail/index.ts +217 -217
- package/src/tail/printing.ts +45 -45
- package/src/update-check.ts +11 -11
- package/src/user/choose-account.tsx +60 -0
- package/src/user/env-vars.ts +46 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +3 -0
- package/src/user/user.tsx +1161 -0
- package/src/whoami.tsx +61 -42
- package/src/worker-namespace.ts +190 -0
- package/src/worker.ts +110 -100
- package/src/zones.ts +39 -36
- package/templates/checked-fetch.js +17 -0
- package/templates/new-worker-scheduled.js +3 -3
- package/templates/new-worker-scheduled.ts +15 -15
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +15 -15
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-template-plugin.ts +155 -0
- package/templates/pages-template-worker.ts +161 -0
- package/templates/static-asset-facade.js +31 -31
- package/templates/tsconfig.json +95 -95
- package/wrangler-dist/cli.js +55383 -54138
- package/pages/functions/buildPlugin.ts +0 -105
- package/pages/functions/buildWorker.ts +0 -151
- package/pages/functions/filepath-routing.ts +0 -189
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -156
- package/pages/functions/template-plugin.ts +0 -147
- package/pages/functions/template-worker.ts +0 -143
- package/src/pages.tsx +0 -2093
- package/src/user.tsx +0 -1214
|
@@ -10,393 +10,393 @@ import type { TailEventMessage, RequestEvent, ScheduledEvent } from "../tail";
|
|
|
10
10
|
import type WebSocket from "ws";
|
|
11
11
|
|
|
12
12
|
describe("tail", () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
13
|
+
runInTempDir();
|
|
14
|
+
mockAccountId();
|
|
15
|
+
mockApiToken();
|
|
16
|
+
|
|
17
|
+
const std = mockConsoleMethods();
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
mockWebSockets.forEach((ws) => ws.close());
|
|
21
|
+
mockWebSockets.splice(0);
|
|
22
|
+
unsetAllMocks();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Interaction with the tailing API, including tail creation,
|
|
27
|
+
* deletion, and connection.
|
|
28
|
+
*/
|
|
29
|
+
describe("API interaction", () => {
|
|
30
|
+
it("creates and then delete tails", async () => {
|
|
31
|
+
const api = mockWebsocketAPIs();
|
|
32
|
+
expect(api.requests.creation.count).toStrictEqual(0);
|
|
33
|
+
|
|
34
|
+
await runWrangler("tail test-worker");
|
|
35
|
+
|
|
36
|
+
await expect(api.ws.connected).resolves.toBeTruthy();
|
|
37
|
+
expect(api.requests.creation.count).toStrictEqual(1);
|
|
38
|
+
expect(api.requests.deletion.count).toStrictEqual(0);
|
|
39
|
+
|
|
40
|
+
api.ws.close();
|
|
41
|
+
expect(api.requests.deletion.count).toStrictEqual(1);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("creates and then delete tails: legacy envs", async () => {
|
|
45
|
+
const api = mockWebsocketAPIs("some-env", true);
|
|
46
|
+
expect(api.requests.creation.count).toStrictEqual(0);
|
|
47
|
+
|
|
48
|
+
await runWrangler("tail test-worker --env some-env --legacy-env true");
|
|
49
|
+
|
|
50
|
+
await expect(api.ws.connected).resolves.toBeTruthy();
|
|
51
|
+
expect(api.requests.creation.count).toStrictEqual(1);
|
|
52
|
+
expect(api.requests.deletion.count).toStrictEqual(0);
|
|
53
|
+
|
|
54
|
+
api.ws.close();
|
|
55
|
+
expect(api.requests.deletion.count).toStrictEqual(1);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("creates and then delete tails: service envs", async () => {
|
|
59
|
+
const api = mockWebsocketAPIs("some-env");
|
|
60
|
+
expect(api.requests.creation.count).toStrictEqual(0);
|
|
61
|
+
|
|
62
|
+
await runWrangler("tail test-worker --env some-env --legacy-env false");
|
|
63
|
+
|
|
64
|
+
await expect(api.ws.connected).resolves.toBeTruthy();
|
|
65
|
+
expect(api.requests.creation.count).toStrictEqual(1);
|
|
66
|
+
expect(api.requests.deletion.count).toStrictEqual(0);
|
|
67
|
+
|
|
68
|
+
api.ws.close();
|
|
69
|
+
expect(api.requests.deletion.count).toStrictEqual(1);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("errors when the websocket closes unexpectedly", async () => {
|
|
73
|
+
const api = mockWebsocketAPIs();
|
|
74
|
+
api.ws.close();
|
|
75
|
+
|
|
76
|
+
await expect(runWrangler("tail test-worker")).rejects.toThrow();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("activates debug mode when the cli arg is passed in", async () => {
|
|
80
|
+
const api = mockWebsocketAPIs();
|
|
81
|
+
await runWrangler("tail test-worker --debug");
|
|
82
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty(
|
|
83
|
+
"debug",
|
|
84
|
+
true
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("filtering", () => {
|
|
90
|
+
it("sends sampling rate filters", async () => {
|
|
91
|
+
const api = mockWebsocketAPIs();
|
|
92
|
+
const tooHigh = runWrangler("tail test-worker --sampling-rate 10");
|
|
93
|
+
await expect(tooHigh).rejects.toThrow();
|
|
94
|
+
|
|
95
|
+
const tooLow = runWrangler("tail test-worker --sampling-rate -5");
|
|
96
|
+
await expect(tooLow).rejects.toThrow();
|
|
97
|
+
|
|
98
|
+
await runWrangler("tail test-worker --sampling-rate 0.25");
|
|
99
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
100
|
+
{ sampling_rate: 0.25 },
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("sends single status filters", async () => {
|
|
105
|
+
const api = mockWebsocketAPIs();
|
|
106
|
+
await runWrangler("tail test-worker --status error");
|
|
107
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
108
|
+
{ outcome: ["exception", "exceededCpu", "unknown"] },
|
|
109
|
+
]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("sends multiple status filters", async () => {
|
|
113
|
+
const api = mockWebsocketAPIs();
|
|
114
|
+
await runWrangler("tail test-worker --status error --status canceled");
|
|
115
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
116
|
+
{ outcome: ["exception", "exceededCpu", "unknown", "canceled"] },
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("sends single HTTP method filters", async () => {
|
|
121
|
+
const api = mockWebsocketAPIs();
|
|
122
|
+
await runWrangler("tail test-worker --method POST");
|
|
123
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
124
|
+
{ method: ["POST"] },
|
|
125
|
+
]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("sends multiple HTTP method filters", async () => {
|
|
129
|
+
const api = mockWebsocketAPIs();
|
|
130
|
+
await runWrangler("tail test-worker --method POST --method GET");
|
|
131
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
132
|
+
{ method: ["POST", "GET"] },
|
|
133
|
+
]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("sends header filters without a query", async () => {
|
|
137
|
+
const api = mockWebsocketAPIs();
|
|
138
|
+
await runWrangler("tail test-worker --header X-CUSTOM-HEADER");
|
|
139
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
140
|
+
{ header: { key: "X-CUSTOM-HEADER" } },
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("sends header filters with a query", async () => {
|
|
145
|
+
const api = mockWebsocketAPIs();
|
|
146
|
+
await runWrangler("tail test-worker --header X-CUSTOM-HEADER:some-value");
|
|
147
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
148
|
+
{ header: { key: "X-CUSTOM-HEADER", query: "some-value" } },
|
|
149
|
+
]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("sends single IP filters", async () => {
|
|
153
|
+
const api = mockWebsocketAPIs();
|
|
154
|
+
const fakeIp = "192.0.2.1";
|
|
155
|
+
|
|
156
|
+
await runWrangler(`tail test-worker --ip ${fakeIp}`);
|
|
157
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
158
|
+
{ client_ip: [fakeIp] },
|
|
159
|
+
]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("sends multiple IP filters", async () => {
|
|
163
|
+
const api = mockWebsocketAPIs();
|
|
164
|
+
const fakeIp = "192.0.2.1";
|
|
165
|
+
|
|
166
|
+
await runWrangler(`tail test-worker --ip ${fakeIp} --ip self`);
|
|
167
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
168
|
+
{ client_ip: [fakeIp, "self"] },
|
|
169
|
+
]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("sends search filters", async () => {
|
|
173
|
+
const api = mockWebsocketAPIs();
|
|
174
|
+
const search = "filterMe";
|
|
175
|
+
|
|
176
|
+
await runWrangler(`tail test-worker --search ${search}`);
|
|
177
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
178
|
+
{ query: search },
|
|
179
|
+
]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("sends everything but the kitchen sink", async () => {
|
|
183
|
+
const api = mockWebsocketAPIs();
|
|
184
|
+
const sampling_rate = 0.69;
|
|
185
|
+
const status = ["ok", "error"];
|
|
186
|
+
const method = ["GET", "POST", "PUT"];
|
|
187
|
+
const header = "X-HELLO:world";
|
|
188
|
+
const client_ip = ["192.0.2.1", "self"];
|
|
189
|
+
const query = "onlyTheseMessagesPlease";
|
|
190
|
+
|
|
191
|
+
const cliFilters =
|
|
192
|
+
`--sampling-rate ${sampling_rate} ` +
|
|
193
|
+
status.map((s) => `--status ${s} `).join("") +
|
|
194
|
+
method.map((m) => `--method ${m} `).join("") +
|
|
195
|
+
`--header ${header} ` +
|
|
196
|
+
client_ip.map((c) => `--ip ${c} `).join("") +
|
|
197
|
+
`--search ${query} ` +
|
|
198
|
+
`--debug`;
|
|
199
|
+
|
|
200
|
+
const expectedWebsocketMessage = {
|
|
201
|
+
filters: [
|
|
202
|
+
{ sampling_rate },
|
|
203
|
+
{ outcome: ["ok", "exception", "exceededCpu", "unknown"] },
|
|
204
|
+
{ method },
|
|
205
|
+
{ header: { key: "X-HELLO", query: "world" } },
|
|
206
|
+
{ client_ip },
|
|
207
|
+
{ query },
|
|
208
|
+
],
|
|
209
|
+
debug: true,
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
await runWrangler(`tail test-worker ${cliFilters}`);
|
|
213
|
+
await expect(api.nextMessageJson()).resolves.toEqual(
|
|
214
|
+
expectedWebsocketMessage
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe("printing", () => {
|
|
220
|
+
const { setIsTTY } = useMockIsTTY();
|
|
221
|
+
|
|
222
|
+
it("logs request messages in JSON format", async () => {
|
|
223
|
+
const api = mockWebsocketAPIs();
|
|
224
|
+
await runWrangler("tail test-worker --format json");
|
|
225
|
+
|
|
226
|
+
const event = generateMockRequestEvent();
|
|
227
|
+
const message = generateMockEventMessage({ event });
|
|
228
|
+
const serializedMessage = serialize(message);
|
|
229
|
+
|
|
230
|
+
api.ws.send(serializedMessage);
|
|
231
|
+
expect(std.out).toMatch(deserializeToJson(serializedMessage));
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("logs scheduled messages in JSON format", async () => {
|
|
235
|
+
const api = mockWebsocketAPIs();
|
|
236
|
+
await runWrangler("tail test-worker --format json");
|
|
237
|
+
|
|
238
|
+
const event = generateMockScheduledEvent();
|
|
239
|
+
const message = generateMockEventMessage({ event });
|
|
240
|
+
const serializedMessage = serialize(message);
|
|
241
|
+
|
|
242
|
+
api.ws.send(serializedMessage);
|
|
243
|
+
expect(std.out).toMatch(deserializeToJson(serializedMessage));
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("logs request messages in pretty format", async () => {
|
|
247
|
+
const api = mockWebsocketAPIs();
|
|
248
|
+
await runWrangler("tail test-worker --format pretty");
|
|
249
|
+
|
|
250
|
+
const event = generateMockRequestEvent();
|
|
251
|
+
const message = generateMockEventMessage({ event });
|
|
252
|
+
const serializedMessage = serialize(message);
|
|
253
|
+
|
|
254
|
+
api.ws.send(serializedMessage);
|
|
255
|
+
expect(
|
|
256
|
+
std.out
|
|
257
|
+
.replace(
|
|
258
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
259
|
+
"[mock event timestamp]"
|
|
260
|
+
)
|
|
261
|
+
.replace(
|
|
262
|
+
mockTailExpiration.toLocaleString(),
|
|
263
|
+
"[mock expiration date]"
|
|
264
|
+
)
|
|
265
|
+
).toMatchInlineSnapshot(`
|
|
266
266
|
"Successfully created tail, expires at [mock expiration date]
|
|
267
267
|
Connected to test-worker, waiting for logs...
|
|
268
268
|
GET https://example.org/ - Ok @ [mock event timestamp]"
|
|
269
269
|
`);
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("logs scheduled messages in pretty format", async () => {
|
|
273
|
+
const api = mockWebsocketAPIs();
|
|
274
|
+
await runWrangler("tail test-worker --format pretty");
|
|
275
|
+
|
|
276
|
+
const event = generateMockScheduledEvent();
|
|
277
|
+
const message = generateMockEventMessage({ event });
|
|
278
|
+
const serializedMessage = serialize(message);
|
|
279
|
+
|
|
280
|
+
api.ws.send(serializedMessage);
|
|
281
|
+
expect(
|
|
282
|
+
std.out
|
|
283
|
+
.replace(
|
|
284
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
285
|
+
"[mock timestamp string]"
|
|
286
|
+
)
|
|
287
|
+
.replace(
|
|
288
|
+
mockTailExpiration.toLocaleString(),
|
|
289
|
+
"[mock expiration date]"
|
|
290
|
+
)
|
|
291
|
+
).toMatchInlineSnapshot(`
|
|
292
292
|
"Successfully created tail, expires at [mock expiration date]
|
|
293
293
|
Connected to test-worker, waiting for logs...
|
|
294
294
|
\\"* * * * *\\" @ [mock timestamp string] - Ok"
|
|
295
295
|
`);
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("should not crash when the tail message has a void event", async () => {
|
|
299
|
+
const api = mockWebsocketAPIs();
|
|
300
|
+
await runWrangler("tail test-worker --format pretty");
|
|
301
|
+
|
|
302
|
+
const message = generateMockEventMessage({ event: null });
|
|
303
|
+
const serializedMessage = serialize(message);
|
|
304
|
+
|
|
305
|
+
api.ws.send(serializedMessage);
|
|
306
|
+
expect(
|
|
307
|
+
std.out
|
|
308
|
+
.replace(
|
|
309
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
310
|
+
"[mock timestamp string]"
|
|
311
|
+
)
|
|
312
|
+
.replace(
|
|
313
|
+
mockTailExpiration.toLocaleString(),
|
|
314
|
+
"[mock expiration date]"
|
|
315
|
+
)
|
|
316
|
+
).toMatchInlineSnapshot(`
|
|
317
317
|
"Successfully created tail, expires at [mock expiration date]
|
|
318
318
|
Connected to test-worker, waiting for logs...
|
|
319
319
|
[missing request] - Ok @ [mock timestamp string]"
|
|
320
320
|
`);
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("defaults to logging in pretty format when the output is a TTY", async () => {
|
|
324
|
+
setIsTTY(true);
|
|
325
|
+
const api = mockWebsocketAPIs();
|
|
326
|
+
await runWrangler("tail test-worker");
|
|
327
|
+
|
|
328
|
+
const event = generateMockRequestEvent();
|
|
329
|
+
const message = generateMockEventMessage({ event });
|
|
330
|
+
const serializedMessage = serialize(message);
|
|
331
|
+
|
|
332
|
+
api.ws.send(serializedMessage);
|
|
333
|
+
expect(
|
|
334
|
+
std.out
|
|
335
|
+
.replace(
|
|
336
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
337
|
+
"[mock event timestamp]"
|
|
338
|
+
)
|
|
339
|
+
.replace(
|
|
340
|
+
mockTailExpiration.toLocaleString(),
|
|
341
|
+
"[mock expiration date]"
|
|
342
|
+
)
|
|
343
|
+
).toMatchInlineSnapshot(`
|
|
344
344
|
"Successfully created tail, expires at [mock expiration date]
|
|
345
345
|
Connected to test-worker, waiting for logs...
|
|
346
346
|
GET https://example.org/ - Ok @ [mock event timestamp]"
|
|
347
347
|
`);
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("defaults to logging in json format when the output is not a TTY", async () => {
|
|
351
|
+
setIsTTY(false);
|
|
352
|
+
|
|
353
|
+
const api = mockWebsocketAPIs();
|
|
354
|
+
await runWrangler("tail test-worker");
|
|
355
|
+
|
|
356
|
+
const event = generateMockRequestEvent();
|
|
357
|
+
const message = generateMockEventMessage({ event });
|
|
358
|
+
const serializedMessage = serialize(message);
|
|
359
|
+
|
|
360
|
+
api.ws.send(serializedMessage);
|
|
361
|
+
expect(std.out).toMatch(deserializeToJson(serializedMessage));
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("logs console messages and exceptions", async () => {
|
|
365
|
+
setIsTTY(true);
|
|
366
|
+
const api = mockWebsocketAPIs();
|
|
367
|
+
await runWrangler("tail test-worker");
|
|
368
|
+
|
|
369
|
+
const event = generateMockRequestEvent();
|
|
370
|
+
const message = generateMockEventMessage({
|
|
371
|
+
event,
|
|
372
|
+
logs: [
|
|
373
|
+
{ message: ["some string"], level: "log", timestamp: 1234561 },
|
|
374
|
+
{
|
|
375
|
+
message: [{ complex: "object" }],
|
|
376
|
+
level: "log",
|
|
377
|
+
timestamp: 1234562,
|
|
378
|
+
},
|
|
379
|
+
{ message: [1234], level: "error", timestamp: 1234563 },
|
|
380
|
+
],
|
|
381
|
+
exceptions: [
|
|
382
|
+
{ name: "Error", message: "some error", timestamp: 1234564 },
|
|
383
|
+
{ name: "Error", message: { complex: "error" }, timestamp: 1234564 },
|
|
384
|
+
],
|
|
385
|
+
});
|
|
386
|
+
const serializedMessage = serialize(message);
|
|
387
|
+
|
|
388
|
+
api.ws.send(serializedMessage);
|
|
389
|
+
expect(
|
|
390
|
+
std.out
|
|
391
|
+
.replace(
|
|
392
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
393
|
+
"[mock event timestamp]"
|
|
394
|
+
)
|
|
395
|
+
.replace(
|
|
396
|
+
mockTailExpiration.toLocaleString(),
|
|
397
|
+
"[mock expiration date]"
|
|
398
|
+
)
|
|
399
|
+
).toMatchInlineSnapshot(`
|
|
400
400
|
"Successfully created tail, expires at [mock expiration date]
|
|
401
401
|
Connected to test-worker, waiting for logs...
|
|
402
402
|
GET https://example.org/ - Ok @ [mock event timestamp]
|
|
@@ -404,7 +404,7 @@ describe("tail", () => {
|
|
|
404
404
|
(log) { complex: 'object' }
|
|
405
405
|
(error) 1234"
|
|
406
406
|
`);
|
|
407
|
-
|
|
407
|
+
expect(std.err).toMatchInlineSnapshot(`
|
|
408
408
|
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m Error: some error[0m
|
|
409
409
|
|
|
410
410
|
|
|
@@ -412,9 +412,9 @@ describe("tail", () => {
|
|
|
412
412
|
|
|
413
413
|
"
|
|
414
414
|
`);
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
415
|
+
expect(std.warn).toMatchInlineSnapshot(`""`);
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
418
|
});
|
|
419
419
|
|
|
420
420
|
/* helpers */
|
|
@@ -429,33 +429,33 @@ describe("tail", () => {
|
|
|
429
429
|
* @returns the same type we expect when deserializing in wrangler
|
|
430
430
|
*/
|
|
431
431
|
function serialize(message: TailEventMessage): WebSocket.RawData {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
432
|
+
if (isScheduled(message.event)) {
|
|
433
|
+
// `ScheduledEvent`s work just fine
|
|
434
|
+
const stringified = JSON.stringify(message);
|
|
435
|
+
return Buffer.from(stringified, "utf-8");
|
|
436
|
+
} else {
|
|
437
|
+
// Since the "properties" of an `undici.Request` are actually getters,
|
|
438
|
+
// which don't serialize properly, we need to hydrate them manually.
|
|
439
|
+
// This isn't a problem outside of testing since deserialization
|
|
440
|
+
// works just fine and wrangler never _sends_ any event messages,
|
|
441
|
+
// it only receives them.
|
|
442
|
+
const request = ((message.event as RequestEvent | undefined | null) || {})
|
|
443
|
+
.request;
|
|
444
|
+
const stringified = JSON.stringify(message, (key, value) => {
|
|
445
|
+
if (key !== "request") {
|
|
446
|
+
return value;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return {
|
|
450
|
+
...request,
|
|
451
|
+
url: request?.url,
|
|
452
|
+
headers: request?.headers,
|
|
453
|
+
method: request?.method,
|
|
454
|
+
};
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
return Buffer.from(stringified, "utf-8");
|
|
458
|
+
}
|
|
459
459
|
}
|
|
460
460
|
|
|
461
461
|
/**
|
|
@@ -465,9 +465,9 @@ function serialize(message: TailEventMessage): WebSocket.RawData {
|
|
|
465
465
|
* @returns whether event is a ScheduledEvent (true) or a RequestEvent
|
|
466
466
|
*/
|
|
467
467
|
function isScheduled(
|
|
468
|
-
|
|
468
|
+
event: ScheduledEvent | RequestEvent | undefined | null
|
|
469
469
|
): event is ScheduledEvent {
|
|
470
|
-
|
|
470
|
+
return Boolean(event && "cron" in event);
|
|
471
471
|
}
|
|
472
472
|
|
|
473
473
|
/**
|
|
@@ -479,7 +479,7 @@ function isScheduled(
|
|
|
479
479
|
* @returns a string ready to be printed to the terminal or compared against
|
|
480
480
|
*/
|
|
481
481
|
function deserializeToJson(message: WebSocket.RawData): string {
|
|
482
|
-
|
|
482
|
+
return JSON.stringify(JSON.parse(message.toString()), null, 2);
|
|
483
483
|
}
|
|
484
484
|
|
|
485
485
|
/**
|
|
@@ -487,12 +487,12 @@ function deserializeToJson(message: WebSocket.RawData): string {
|
|
|
487
487
|
* when running `wrangler tail`
|
|
488
488
|
*/
|
|
489
489
|
type MockAPI = {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
490
|
+
requests: {
|
|
491
|
+
creation: RequestCounter;
|
|
492
|
+
deletion: RequestCounter;
|
|
493
|
+
};
|
|
494
|
+
ws: MockWebSocket;
|
|
495
|
+
nextMessageJson(): Promise<unknown>;
|
|
496
496
|
};
|
|
497
497
|
|
|
498
498
|
/**
|
|
@@ -501,7 +501,7 @@ type MockAPI = {
|
|
|
501
501
|
* the correct API calls without actually sending any web traffic
|
|
502
502
|
*/
|
|
503
503
|
type RequestCounter = {
|
|
504
|
-
|
|
504
|
+
count: number;
|
|
505
505
|
};
|
|
506
506
|
|
|
507
507
|
/**
|
|
@@ -511,34 +511,34 @@ type RequestCounter = {
|
|
|
511
511
|
* @returns a `RequestCounter` for counting how many times the API is hit
|
|
512
512
|
*/
|
|
513
513
|
function mockCreateTailRequest(
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
514
|
+
websocketURL: string,
|
|
515
|
+
env?: string,
|
|
516
|
+
legacyEnv = false
|
|
517
517
|
): RequestCounter {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
518
|
+
const requests = { count: 0 };
|
|
519
|
+
const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
|
|
520
|
+
const environment = env && !legacyEnv ? "/environments/:envName" : "";
|
|
521
|
+
setMockResponse(
|
|
522
|
+
`/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/tails`,
|
|
523
|
+
"POST",
|
|
524
|
+
([_url, accountId, scriptName, envName]) => {
|
|
525
|
+
requests.count++;
|
|
526
|
+
expect(accountId).toEqual("some-account-id");
|
|
527
|
+
expect(scriptName).toEqual(
|
|
528
|
+
legacyEnv && env ? `test-worker-${env}` : "test-worker"
|
|
529
|
+
);
|
|
530
|
+
if (!legacyEnv) {
|
|
531
|
+
expect(envName).toEqual(env);
|
|
532
|
+
}
|
|
533
|
+
return {
|
|
534
|
+
id: "tail-id",
|
|
535
|
+
url: websocketURL,
|
|
536
|
+
expires_at: mockTailExpiration,
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
);
|
|
540
|
+
|
|
541
|
+
return requests;
|
|
542
542
|
}
|
|
543
543
|
|
|
544
544
|
/**
|
|
@@ -557,37 +557,37 @@ const mockEventTimestamp = 1645454470467;
|
|
|
557
557
|
* @returns a `RequestCounter` for counting how many times the API is hit
|
|
558
558
|
*/
|
|
559
559
|
function mockDeleteTailRequest(
|
|
560
|
-
|
|
561
|
-
|
|
560
|
+
env?: string,
|
|
561
|
+
legacyEnv = false
|
|
562
562
|
): RequestCounter {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
563
|
+
const requests = { count: 0 };
|
|
564
|
+
const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
|
|
565
|
+
const environment = env && !legacyEnv ? "/environments/:envName" : "";
|
|
566
|
+
setMockResponse(
|
|
567
|
+
// "/accounts/:accountId/workers/scripts/:worker/tails",
|
|
568
|
+
`/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/tails/:tailId`,
|
|
569
|
+
"DELETE",
|
|
570
|
+
([_url, accountId, scriptName, envNameOrTailId, tailId]) => {
|
|
571
|
+
requests.count++;
|
|
572
|
+
expect(accountId).toEqual("some-account-id");
|
|
573
|
+
expect(scriptName).toEqual(
|
|
574
|
+
legacyEnv && env ? `test-worker-${env}` : "test-worker"
|
|
575
|
+
);
|
|
576
|
+
if (!legacyEnv) {
|
|
577
|
+
if (env) {
|
|
578
|
+
expect(envNameOrTailId).toEqual(env);
|
|
579
|
+
expect(tailId).toEqual("tail-id");
|
|
580
|
+
} else {
|
|
581
|
+
expect(envNameOrTailId).toEqual("tail-id");
|
|
582
|
+
}
|
|
583
|
+
} else {
|
|
584
|
+
expect(envNameOrTailId).toEqual("tail-id");
|
|
585
|
+
}
|
|
586
|
+
return null;
|
|
587
|
+
}
|
|
588
|
+
);
|
|
589
|
+
|
|
590
|
+
return requests;
|
|
591
591
|
}
|
|
592
592
|
|
|
593
593
|
const mockWebSockets: MockWebSocket[] = [];
|
|
@@ -600,30 +600,30 @@ const mockWebSockets: MockWebSocket[] = [];
|
|
|
600
600
|
* @returns a mocked-out version of the API
|
|
601
601
|
*/
|
|
602
602
|
function mockWebsocketAPIs(env?: string, legacyEnv = false): MockAPI {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
603
|
+
const websocketURL = "ws://localhost:1234";
|
|
604
|
+
const api: MockAPI = {
|
|
605
|
+
requests: {
|
|
606
|
+
deletion: { count: 0 },
|
|
607
|
+
creation: { count: 0 },
|
|
608
|
+
},
|
|
609
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
610
|
+
ws: null!, // will be set in the `beforeEach()` below.
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Parse the next message received by the mock websocket as JSON
|
|
614
|
+
* @returns JSON.parse of the next message received by the websocket
|
|
615
|
+
*/
|
|
616
|
+
async nextMessageJson() {
|
|
617
|
+
const message = await api.ws.nextMessage;
|
|
618
|
+
return JSON.parse(message as string);
|
|
619
|
+
},
|
|
620
|
+
};
|
|
621
|
+
api.requests.creation = mockCreateTailRequest(websocketURL, env, legacyEnv);
|
|
622
|
+
api.requests.deletion = mockDeleteTailRequest(env, legacyEnv);
|
|
623
|
+
api.ws = new MockWebSocket(websocketURL);
|
|
624
|
+
mockWebSockets.push(api.ws);
|
|
625
|
+
|
|
626
|
+
return api;
|
|
627
627
|
}
|
|
628
628
|
|
|
629
629
|
/**
|
|
@@ -634,19 +634,19 @@ function mockWebsocketAPIs(env?: string, legacyEnv = false): MockAPI {
|
|
|
634
634
|
* @returns a `TailEventMessage` that wrangler can process and display
|
|
635
635
|
*/
|
|
636
636
|
function generateMockEventMessage({
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
637
|
+
outcome = "ok",
|
|
638
|
+
exceptions = [],
|
|
639
|
+
logs = [],
|
|
640
|
+
eventTimestamp = mockEventTimestamp,
|
|
641
|
+
event = generateMockRequestEvent(),
|
|
642
642
|
}: Partial<TailEventMessage>): TailEventMessage {
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
643
|
+
return {
|
|
644
|
+
outcome,
|
|
645
|
+
exceptions,
|
|
646
|
+
logs,
|
|
647
|
+
eventTimestamp,
|
|
648
|
+
event,
|
|
649
|
+
};
|
|
650
650
|
}
|
|
651
651
|
|
|
652
652
|
/**
|
|
@@ -657,34 +657,34 @@ function generateMockEventMessage({
|
|
|
657
657
|
* @returns a `RequestEvent` that can be used within an `EventMessage`
|
|
658
658
|
*/
|
|
659
659
|
function generateMockRequestEvent(
|
|
660
|
-
|
|
660
|
+
opts?: Partial<RequestEvent["request"]>
|
|
661
661
|
): RequestEvent {
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
662
|
+
return {
|
|
663
|
+
request: Object.assign(
|
|
664
|
+
new Request(opts?.url || "https://example.org/", {
|
|
665
|
+
method: opts?.method || "GET",
|
|
666
|
+
headers:
|
|
667
|
+
opts?.headers || new Headers({ "X-EXAMPLE-HEADER": "some_value" }),
|
|
668
|
+
}),
|
|
669
|
+
{
|
|
670
|
+
cf: opts?.cf || {
|
|
671
|
+
tlsCipher: "AEAD-ENCRYPT-O-MATIC-SHA",
|
|
672
|
+
tlsVersion: "TLSv2.0",
|
|
673
|
+
asn: 42069,
|
|
674
|
+
colo: "ATL",
|
|
675
|
+
httpProtocol: "HTTP/4",
|
|
676
|
+
asOrganization: "Cloudflare",
|
|
677
|
+
},
|
|
678
|
+
}
|
|
679
|
+
),
|
|
680
|
+
};
|
|
681
681
|
}
|
|
682
682
|
|
|
683
683
|
function generateMockScheduledEvent(
|
|
684
|
-
|
|
684
|
+
opts?: Partial<ScheduledEvent>
|
|
685
685
|
): ScheduledEvent {
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
686
|
+
return {
|
|
687
|
+
cron: opts?.cron || "* * * * *",
|
|
688
|
+
scheduledTime: opts?.scheduledTime || mockEventTimestamp,
|
|
689
|
+
};
|
|
690
690
|
}
|