wrangler 0.0.0-e88a54e → 0.0.0-e8e684c7
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 +51 -14
- package/bin/wrangler.js +161 -29
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +5484 -21
- package/package.json +173 -143
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +42 -0
- package/src/__tests__/configuration.test.ts +4016 -2786
- package/src/__tests__/dev.test.tsx +1305 -483
- package/src/__tests__/generate.test.ts +91 -0
- package/src/__tests__/guess-worker-format.test.ts +75 -48
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- 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 +182 -93
- package/src/__tests__/helpers/mock-console.ts +59 -32
- package/src/__tests__/helpers/mock-dialogs.ts +121 -33
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +74 -0
- package/src/__tests__/helpers/mock-known-routes.ts +7 -0
- package/src/__tests__/helpers/mock-kv.ts +26 -26
- package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
- 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 +28 -22
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +144 -108
- package/src/__tests__/index.test.ts +229 -975
- package/src/__tests__/init.test.ts +2170 -0
- package/src/__tests__/jest.setup.ts +122 -34
- package/src/__tests__/kv.test.ts +1376 -1087
- package/src/__tests__/logger.test.ts +141 -0
- package/src/__tests__/metrics.test.ts +450 -0
- package/src/__tests__/package-manager.test.ts +312 -165
- package/src/__tests__/pages.test.ts +1270 -39
- package/src/__tests__/parse.test.ts +253 -245
- package/src/__tests__/publish.test.ts +6878 -2627
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +328 -190
- package/src/__tests__/route.test.ts +22 -22
- package/src/__tests__/secret.test.ts +474 -407
- package/src/__tests__/tail.test.ts +679 -441
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/user.test.ts +155 -0
- package/src/__tests__/validate-dev-props.test.ts +56 -0
- package/src/__tests__/version.test.ts +35 -0
- package/src/__tests__/whoami.test.tsx +276 -87
- package/src/__tests__/worker-namespace.test.ts +329 -0
- package/src/abort.d.ts +3 -0
- package/src/api/dev.ts +93 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +441 -110
- package/src/cfetch/index.ts +83 -66
- package/src/cfetch/internal.ts +251 -92
- package/src/cli.ts +22 -14
- package/src/config/README.md +1 -1
- package/src/config/config.ts +224 -179
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +430 -311
- package/src/config/index.ts +227 -38
- package/src/config/validation-helpers.ts +414 -303
- package/src/config/validation.ts +1745 -973
- package/src/config-cache.ts +85 -0
- package/src/create-worker-preview.ts +209 -118
- package/src/create-worker-upload-form.ts +275 -201
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +488 -307
- package/src/dev/local.tsx +427 -210
- package/src/dev/remote.tsx +407 -188
- package/src/dev/use-esbuild.ts +156 -78
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +160 -0
- package/src/dev.tsx +696 -0
- package/src/dialogs.tsx +115 -57
- package/src/dispatch-namespace.ts +206 -0
- package/src/durable.ts +102 -0
- package/src/entry.ts +267 -147
- package/src/environment-variables.ts +25 -23
- package/src/errors.ts +11 -0
- package/src/faye-websocket.d.ts +6 -0
- package/src/generate.ts +131 -0
- package/src/git-client.ts +42 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +85 -80
- package/src/index.tsx +2191 -2304
- package/src/init.ts +622 -0
- package/src/inspect.ts +687 -557
- package/src/intl-polyfill.d.ts +139 -0
- package/src/is-ci.ts +14 -0
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +353 -242
- package/src/logger.ts +73 -0
- package/src/metrics/index.ts +5 -0
- package/src/metrics/metrics-config.ts +239 -0
- package/src/metrics/metrics-dispatcher.ts +96 -0
- package/src/metrics/metrics-usage-headers.ts +24 -0
- package/src/metrics/send-event.ts +92 -0
- package/src/miniflare-cli/assets.ts +554 -0
- package/src/miniflare-cli/enum-keys.ts +8 -8
- package/src/miniflare-cli/index.ts +199 -29
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +213 -213
- package/src/open-in-browser.ts +8 -16
- package/src/package-manager.ts +183 -90
- package/src/pages/build.tsx +238 -0
- package/src/pages/constants.ts +12 -0
- package/src/pages/deployments.tsx +106 -0
- package/src/pages/dev.tsx +528 -0
- package/src/pages/errors.ts +22 -0
- package/src/pages/functions/buildPlugin.ts +109 -0
- package/src/pages/functions/buildWorker.ts +155 -0
- package/src/pages/functions/filepath-routing.test.ts +234 -0
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes-consolidation.test.ts +250 -0
- package/src/pages/functions/routes-consolidation.ts +73 -0
- package/src/pages/functions/routes-transformation.test.ts +271 -0
- package/src/pages/functions/routes-transformation.ts +122 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/functions.tsx +96 -0
- package/src/pages/index.tsx +94 -0
- package/src/pages/projects.tsx +169 -0
- package/src/pages/publish.tsx +391 -0
- package/src/pages/types.ts +49 -0
- package/src/pages/upload.tsx +413 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +224 -121
- package/src/paths.ts +6 -6
- package/src/preview.ts +31 -0
- package/src/proxy.ts +416 -370
- package/src/publish.ts +796 -321
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +623 -0
- package/src/r2.ts +100 -19
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +281 -189
- package/src/tail/filters.ts +111 -109
- package/src/tail/index.ts +230 -217
- package/src/tail/printing.ts +92 -47
- package/src/update-check.ts +19 -0
- package/src/user/choose-account.tsx +69 -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 +1191 -0
- package/src/whoami.tsx +142 -42
- package/src/worker.ts +135 -94
- package/src/zones.ts +167 -0
- package/templates/checked-fetch.js +17 -0
- package/templates/first-party-worker-module-facade.ts +18 -0
- package/templates/format-dev-errors.ts +32 -0
- package/templates/gitignore +171 -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 +18 -3
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +167 -0
- package/templates/pages-template-worker.ts +172 -0
- package/templates/serve-static-assets.ts +63 -0
- package/templates/service-bindings-module-facade.js +54 -0
- package/templates/service-bindings-sw-facade.js +42 -0
- package/templates/tsconfig.json +95 -95
- package/wrangler-dist/cli.d.ts +338 -0
- package/wrangler-dist/cli.js +124611 -112403
- package/miniflare-dist/index.mjs.map +0 -7
- package/pages/functions/buildWorker.ts +0 -62
- package/pages/functions/filepath-routing.test.ts +0 -223
- package/pages/functions/filepath-routing.ts +0 -181
- 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__/dev2.test.tsx +0 -154
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/__tests__/logout.test.ts +0 -52
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/pages.tsx +0 -1047
- package/src/reporting.ts +0 -131
- package/src/user.tsx +0 -1118
- 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
|
@@ -3,297 +3,519 @@ import { Headers, Request } from "undici";
|
|
|
3
3
|
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
|
|
4
4
|
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
|
|
5
5
|
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
6
|
+
import { mockGetZoneFromHostRequest } from "./helpers/mock-get-zone-from-host";
|
|
7
|
+
import { useMockIsTTY } from "./helpers/mock-istty";
|
|
8
|
+
import { mockCollectKnownRoutesRequest } from "./helpers/mock-known-routes";
|
|
6
9
|
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
7
10
|
import { runWrangler } from "./helpers/run-wrangler";
|
|
8
|
-
import type {
|
|
11
|
+
import type {
|
|
12
|
+
TailEventMessage,
|
|
13
|
+
RequestEvent,
|
|
14
|
+
ScheduledEvent,
|
|
15
|
+
AlarmEvent,
|
|
16
|
+
} from "../tail";
|
|
9
17
|
import type WebSocket from "ws";
|
|
10
18
|
|
|
11
19
|
describe("tail", () => {
|
|
12
|
-
|
|
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
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
20
|
+
runInTempDir();
|
|
21
|
+
mockAccountId();
|
|
22
|
+
mockApiToken();
|
|
23
|
+
|
|
24
|
+
const std = mockConsoleMethods();
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
mockWebSockets.forEach((ws) => ws.close());
|
|
28
|
+
mockWebSockets.splice(0);
|
|
29
|
+
unsetAllMocks();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Interaction with the tailing API, including tail creation,
|
|
34
|
+
* deletion, and connection.
|
|
35
|
+
*/
|
|
36
|
+
describe("API interaction", () => {
|
|
37
|
+
it("should throw an error if name isn't provided", async () => {
|
|
38
|
+
await expect(
|
|
39
|
+
runWrangler("tail")
|
|
40
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
41
|
+
`"Required Worker name missing. Please specify the Worker name in wrangler.toml, or pass it as an argument with \`wrangler tail <worker-name>\`"`
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("creates and then delete tails", async () => {
|
|
46
|
+
const api = mockWebsocketAPIs();
|
|
47
|
+
expect(api.requests.creation.count).toStrictEqual(0);
|
|
48
|
+
|
|
49
|
+
await runWrangler("tail test-worker");
|
|
50
|
+
|
|
51
|
+
await expect(api.ws.connected).resolves.toBeTruthy();
|
|
52
|
+
expect(api.requests.creation.count).toStrictEqual(1);
|
|
53
|
+
expect(api.requests.deletion.count).toStrictEqual(0);
|
|
54
|
+
|
|
55
|
+
api.ws.close();
|
|
56
|
+
expect(api.requests.deletion.count).toStrictEqual(1);
|
|
57
|
+
});
|
|
58
|
+
it("should connect to the worker assigned to a given route", async () => {
|
|
59
|
+
const api = mockWebsocketAPIs();
|
|
60
|
+
expect(api.requests.creation.count).toStrictEqual(0);
|
|
61
|
+
|
|
62
|
+
mockGetZoneFromHostRequest("example.com", "test-zone");
|
|
63
|
+
mockCollectKnownRoutesRequest([
|
|
64
|
+
{
|
|
65
|
+
pattern: "example.com/*",
|
|
66
|
+
script: "test-worker",
|
|
67
|
+
},
|
|
68
|
+
]);
|
|
69
|
+
await runWrangler("tail example.com/*");
|
|
70
|
+
|
|
71
|
+
await expect(api.ws.connected).resolves.toBeTruthy();
|
|
72
|
+
expect(api.requests.creation.count).toStrictEqual(1);
|
|
73
|
+
expect(api.requests.deletion.count).toStrictEqual(0);
|
|
74
|
+
|
|
75
|
+
api.ws.close();
|
|
76
|
+
expect(api.requests.deletion.count).toStrictEqual(1);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should error if a given route is not assigned to the user's zone", async () => {
|
|
80
|
+
mockGetZoneFromHostRequest("example.com", "test-zone");
|
|
81
|
+
mockCollectKnownRoutesRequest([]);
|
|
82
|
+
|
|
83
|
+
await expect(runWrangler("tail example.com/*")).rejects.toThrow();
|
|
84
|
+
});
|
|
85
|
+
it("should error if a given route is not within the user's zone", async () => {
|
|
86
|
+
mockGetZoneFromHostRequest("example.com");
|
|
87
|
+
|
|
88
|
+
await expect(runWrangler("tail example.com/*")).rejects.toThrow();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("creates and then delete tails: legacy envs", async () => {
|
|
92
|
+
const api = mockWebsocketAPIs("some-env", true);
|
|
93
|
+
expect(api.requests.creation.count).toStrictEqual(0);
|
|
94
|
+
|
|
95
|
+
await runWrangler("tail test-worker --env some-env --legacy-env true");
|
|
96
|
+
|
|
97
|
+
await expect(api.ws.connected).resolves.toBeTruthy();
|
|
98
|
+
expect(api.requests.creation.count).toStrictEqual(1);
|
|
99
|
+
expect(api.requests.deletion.count).toStrictEqual(0);
|
|
100
|
+
|
|
101
|
+
api.ws.close();
|
|
102
|
+
expect(api.requests.deletion.count).toStrictEqual(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("creates and then delete tails: service envs", async () => {
|
|
106
|
+
const api = mockWebsocketAPIs("some-env");
|
|
107
|
+
expect(api.requests.creation.count).toStrictEqual(0);
|
|
108
|
+
|
|
109
|
+
await runWrangler("tail test-worker --env some-env --legacy-env false");
|
|
110
|
+
|
|
111
|
+
await expect(api.ws.connected).resolves.toBeTruthy();
|
|
112
|
+
expect(api.requests.creation.count).toStrictEqual(1);
|
|
113
|
+
expect(api.requests.deletion.count).toStrictEqual(0);
|
|
114
|
+
|
|
115
|
+
api.ws.close();
|
|
116
|
+
expect(api.requests.deletion.count).toStrictEqual(1);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("errors when the websocket closes unexpectedly", async () => {
|
|
120
|
+
const api = mockWebsocketAPIs();
|
|
121
|
+
api.ws.close();
|
|
122
|
+
|
|
123
|
+
await expect(runWrangler("tail test-worker")).rejects.toThrow();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("activates debug mode when the cli arg is passed in", async () => {
|
|
127
|
+
const api = mockWebsocketAPIs();
|
|
128
|
+
await runWrangler("tail test-worker --debug");
|
|
129
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty(
|
|
130
|
+
"debug",
|
|
131
|
+
true
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("filtering", () => {
|
|
137
|
+
it("sends sampling rate filters", async () => {
|
|
138
|
+
const api = mockWebsocketAPIs();
|
|
139
|
+
const tooHigh = runWrangler("tail test-worker --sampling-rate 10");
|
|
140
|
+
await expect(tooHigh).rejects.toThrow();
|
|
141
|
+
|
|
142
|
+
const tooLow = runWrangler("tail test-worker --sampling-rate -5");
|
|
143
|
+
await expect(tooLow).rejects.toThrow();
|
|
144
|
+
|
|
145
|
+
await runWrangler("tail test-worker --sampling-rate 0.25");
|
|
146
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
147
|
+
{ sampling_rate: 0.25 },
|
|
148
|
+
]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("sends single status filters", async () => {
|
|
152
|
+
const api = mockWebsocketAPIs();
|
|
153
|
+
await runWrangler("tail test-worker --status error");
|
|
154
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
155
|
+
{ outcome: ["exception", "exceededCpu", "exceededMemory", "unknown"] },
|
|
156
|
+
]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("sends multiple status filters", async () => {
|
|
160
|
+
const api = mockWebsocketAPIs();
|
|
161
|
+
await runWrangler("tail test-worker --status error --status canceled");
|
|
162
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
163
|
+
{
|
|
164
|
+
outcome: [
|
|
165
|
+
"exception",
|
|
166
|
+
"exceededCpu",
|
|
167
|
+
"exceededMemory",
|
|
168
|
+
"unknown",
|
|
169
|
+
"canceled",
|
|
170
|
+
],
|
|
171
|
+
},
|
|
172
|
+
]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("sends single HTTP method filters", async () => {
|
|
176
|
+
const api = mockWebsocketAPIs();
|
|
177
|
+
await runWrangler("tail test-worker --method POST");
|
|
178
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
179
|
+
{ method: ["POST"] },
|
|
180
|
+
]);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("sends multiple HTTP method filters", async () => {
|
|
184
|
+
const api = mockWebsocketAPIs();
|
|
185
|
+
await runWrangler("tail test-worker --method POST --method GET");
|
|
186
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
187
|
+
{ method: ["POST", "GET"] },
|
|
188
|
+
]);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("sends header filters without a query", async () => {
|
|
192
|
+
const api = mockWebsocketAPIs();
|
|
193
|
+
await runWrangler("tail test-worker --header X-CUSTOM-HEADER");
|
|
194
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
195
|
+
{ header: { key: "X-CUSTOM-HEADER" } },
|
|
196
|
+
]);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("sends header filters with a query", async () => {
|
|
200
|
+
const api = mockWebsocketAPIs();
|
|
201
|
+
await runWrangler("tail test-worker --header X-CUSTOM-HEADER:some-value");
|
|
202
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
203
|
+
{ header: { key: "X-CUSTOM-HEADER", query: "some-value" } },
|
|
204
|
+
]);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("sends single IP filters", async () => {
|
|
208
|
+
const api = mockWebsocketAPIs();
|
|
209
|
+
const fakeIp = "192.0.2.1";
|
|
210
|
+
|
|
211
|
+
await runWrangler(`tail test-worker --ip ${fakeIp}`);
|
|
212
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
213
|
+
{ client_ip: [fakeIp] },
|
|
214
|
+
]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("sends multiple IP filters", async () => {
|
|
218
|
+
const api = mockWebsocketAPIs();
|
|
219
|
+
const fakeIp = "192.0.2.1";
|
|
220
|
+
|
|
221
|
+
await runWrangler(`tail test-worker --ip ${fakeIp} --ip self`);
|
|
222
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
223
|
+
{ client_ip: [fakeIp, "self"] },
|
|
224
|
+
]);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("sends search filters", async () => {
|
|
228
|
+
const api = mockWebsocketAPIs();
|
|
229
|
+
const search = "filterMe";
|
|
230
|
+
|
|
231
|
+
await runWrangler(`tail test-worker --search ${search}`);
|
|
232
|
+
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
233
|
+
{ query: search },
|
|
234
|
+
]);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("sends everything but the kitchen sink", async () => {
|
|
238
|
+
const api = mockWebsocketAPIs();
|
|
239
|
+
const sampling_rate = 0.69;
|
|
240
|
+
const status = ["ok", "error"];
|
|
241
|
+
const method = ["GET", "POST", "PUT"];
|
|
242
|
+
const header = "X-HELLO:world";
|
|
243
|
+
const client_ip = ["192.0.2.1", "self"];
|
|
244
|
+
const query = "onlyTheseMessagesPlease";
|
|
245
|
+
|
|
246
|
+
const cliFilters =
|
|
247
|
+
`--sampling-rate ${sampling_rate} ` +
|
|
248
|
+
status.map((s) => `--status ${s} `).join("") +
|
|
249
|
+
method.map((m) => `--method ${m} `).join("") +
|
|
250
|
+
`--header ${header} ` +
|
|
251
|
+
client_ip.map((c) => `--ip ${c} `).join("") +
|
|
252
|
+
`--search ${query} ` +
|
|
253
|
+
`--debug`;
|
|
254
|
+
|
|
255
|
+
const expectedWebsocketMessage = {
|
|
256
|
+
filters: [
|
|
257
|
+
{ sampling_rate },
|
|
258
|
+
{
|
|
259
|
+
outcome: [
|
|
260
|
+
"ok",
|
|
261
|
+
"exception",
|
|
262
|
+
"exceededCpu",
|
|
263
|
+
"exceededMemory",
|
|
264
|
+
"unknown",
|
|
265
|
+
],
|
|
266
|
+
},
|
|
267
|
+
{ method },
|
|
268
|
+
{ header: { key: "X-HELLO", query: "world" } },
|
|
269
|
+
{ client_ip },
|
|
270
|
+
{ query },
|
|
271
|
+
],
|
|
272
|
+
debug: true,
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
await runWrangler(`tail test-worker ${cliFilters}`);
|
|
276
|
+
await expect(api.nextMessageJson()).resolves.toEqual(
|
|
277
|
+
expectedWebsocketMessage
|
|
278
|
+
);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe("printing", () => {
|
|
283
|
+
const { setIsTTY } = useMockIsTTY();
|
|
284
|
+
|
|
285
|
+
it("logs request messages in JSON format", async () => {
|
|
286
|
+
const api = mockWebsocketAPIs();
|
|
287
|
+
await runWrangler("tail test-worker --format json");
|
|
288
|
+
|
|
289
|
+
const event = generateMockRequestEvent();
|
|
290
|
+
const message = generateMockEventMessage({ event });
|
|
291
|
+
const serializedMessage = serialize(message);
|
|
292
|
+
|
|
293
|
+
api.ws.send(serializedMessage);
|
|
294
|
+
expect(std.out).toMatch(deserializeToJson(serializedMessage));
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("logs scheduled messages in JSON format", async () => {
|
|
298
|
+
const api = mockWebsocketAPIs();
|
|
299
|
+
await runWrangler("tail test-worker --format json");
|
|
300
|
+
|
|
301
|
+
const event = generateMockScheduledEvent();
|
|
302
|
+
const message = generateMockEventMessage({ event });
|
|
303
|
+
const serializedMessage = serialize(message);
|
|
304
|
+
|
|
305
|
+
api.ws.send(serializedMessage);
|
|
306
|
+
expect(std.out).toMatch(deserializeToJson(serializedMessage));
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("logs alarm messages in json format", async () => {
|
|
310
|
+
const api = mockWebsocketAPIs();
|
|
311
|
+
await runWrangler("tail test-worker --format json");
|
|
312
|
+
|
|
313
|
+
const event = generateMockAlarmEvent();
|
|
314
|
+
const message = generateMockEventMessage({ event });
|
|
315
|
+
const serializedMessage = serialize(message);
|
|
316
|
+
|
|
317
|
+
api.ws.send(serializedMessage);
|
|
318
|
+
expect(std.out).toMatch(deserializeToJson(serializedMessage));
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("logs request messages in pretty format", async () => {
|
|
322
|
+
const api = mockWebsocketAPIs();
|
|
323
|
+
await runWrangler("tail test-worker --format pretty");
|
|
324
|
+
|
|
325
|
+
const event = generateMockRequestEvent();
|
|
326
|
+
const message = generateMockEventMessage({ event });
|
|
327
|
+
const serializedMessage = serialize(message);
|
|
328
|
+
|
|
329
|
+
api.ws.send(serializedMessage);
|
|
330
|
+
expect(
|
|
331
|
+
std.out
|
|
332
|
+
.replace(
|
|
333
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
334
|
+
"[mock event timestamp]"
|
|
335
|
+
)
|
|
336
|
+
.replace(
|
|
337
|
+
mockTailExpiration.toLocaleString(),
|
|
338
|
+
"[mock expiration date]"
|
|
339
|
+
)
|
|
340
|
+
).toMatchInlineSnapshot(`
|
|
341
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
342
|
+
Connected to test-worker, waiting for logs...
|
|
343
|
+
GET https://example.org/ - Ok @ [mock event timestamp]"
|
|
344
|
+
`);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("logs scheduled messages in pretty format", async () => {
|
|
348
|
+
const api = mockWebsocketAPIs();
|
|
349
|
+
await runWrangler("tail test-worker --format pretty");
|
|
350
|
+
|
|
351
|
+
const event = generateMockScheduledEvent();
|
|
352
|
+
const message = generateMockEventMessage({ event });
|
|
353
|
+
const serializedMessage = serialize(message);
|
|
354
|
+
|
|
355
|
+
api.ws.send(serializedMessage);
|
|
356
|
+
expect(
|
|
357
|
+
std.out
|
|
358
|
+
.replace(
|
|
359
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
360
|
+
"[mock timestamp string]"
|
|
361
|
+
)
|
|
362
|
+
.replace(
|
|
363
|
+
mockTailExpiration.toLocaleString(),
|
|
364
|
+
"[mock expiration date]"
|
|
365
|
+
)
|
|
366
|
+
).toMatchInlineSnapshot(`
|
|
367
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
368
|
+
Connected to test-worker, waiting for logs...
|
|
369
|
+
\\"* * * * *\\" @ [mock timestamp string] - Ok"
|
|
370
|
+
`);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("logs alarm messages in pretty format", async () => {
|
|
374
|
+
const api = mockWebsocketAPIs();
|
|
375
|
+
await runWrangler("tail test-worker --format pretty");
|
|
376
|
+
|
|
377
|
+
const event = generateMockAlarmEvent();
|
|
378
|
+
const message = generateMockEventMessage({ event });
|
|
379
|
+
const serializedMessage = serialize(message);
|
|
380
|
+
|
|
381
|
+
api.ws.send(serializedMessage);
|
|
382
|
+
expect(
|
|
383
|
+
std.out
|
|
384
|
+
.replace(
|
|
385
|
+
new Date(mockEventScheduledTime).toLocaleString(),
|
|
386
|
+
"[mock scheduled time]"
|
|
387
|
+
)
|
|
388
|
+
.replace(
|
|
389
|
+
mockTailExpiration.toLocaleString(),
|
|
390
|
+
"[mock expiration date]"
|
|
391
|
+
)
|
|
392
|
+
).toMatchInlineSnapshot(`
|
|
393
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
394
|
+
Connected to test-worker, waiting for logs...
|
|
395
|
+
Alarm @ [mock scheduled time] - Ok"
|
|
396
|
+
`);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it("should not crash when the tail message has a void event", async () => {
|
|
400
|
+
const api = mockWebsocketAPIs();
|
|
401
|
+
await runWrangler("tail test-worker --format pretty");
|
|
402
|
+
|
|
403
|
+
const message = generateMockEventMessage({ event: null });
|
|
404
|
+
const serializedMessage = serialize(message);
|
|
405
|
+
|
|
406
|
+
api.ws.send(serializedMessage);
|
|
407
|
+
expect(
|
|
408
|
+
std.out
|
|
409
|
+
.replace(
|
|
410
|
+
mockTailExpiration.toLocaleString(),
|
|
411
|
+
"[mock expiration date]"
|
|
412
|
+
)
|
|
413
|
+
.replace(
|
|
414
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
415
|
+
"[mock timestamp string]"
|
|
416
|
+
)
|
|
417
|
+
).toMatchInlineSnapshot(`
|
|
418
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
419
|
+
Connected to test-worker, waiting for logs...
|
|
420
|
+
Unknown Event - Ok @ [mock timestamp string]"
|
|
421
|
+
`);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("defaults to logging in pretty format when the output is a TTY", async () => {
|
|
425
|
+
setIsTTY(true);
|
|
426
|
+
const api = mockWebsocketAPIs();
|
|
427
|
+
await runWrangler("tail test-worker");
|
|
428
|
+
|
|
429
|
+
const event = generateMockRequestEvent();
|
|
430
|
+
const message = generateMockEventMessage({ event });
|
|
431
|
+
const serializedMessage = serialize(message);
|
|
432
|
+
|
|
433
|
+
api.ws.send(serializedMessage);
|
|
434
|
+
expect(
|
|
435
|
+
std.out
|
|
436
|
+
.replace(
|
|
437
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
438
|
+
"[mock event timestamp]"
|
|
439
|
+
)
|
|
440
|
+
.replace(
|
|
441
|
+
mockTailExpiration.toLocaleString(),
|
|
442
|
+
"[mock expiration date]"
|
|
443
|
+
)
|
|
444
|
+
).toMatchInlineSnapshot(`
|
|
445
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
446
|
+
Connected to test-worker, waiting for logs...
|
|
447
|
+
GET https://example.org/ - Ok @ [mock event timestamp]"
|
|
448
|
+
`);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("defaults to logging in json format when the output is not a TTY", async () => {
|
|
452
|
+
setIsTTY(false);
|
|
453
|
+
|
|
454
|
+
const api = mockWebsocketAPIs();
|
|
455
|
+
await runWrangler("tail test-worker");
|
|
456
|
+
|
|
457
|
+
const event = generateMockRequestEvent();
|
|
458
|
+
const message = generateMockEventMessage({ event });
|
|
459
|
+
const serializedMessage = serialize(message);
|
|
460
|
+
|
|
461
|
+
api.ws.send(serializedMessage);
|
|
462
|
+
expect(std.out).toMatch(deserializeToJson(serializedMessage));
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it("logs console messages and exceptions", async () => {
|
|
466
|
+
setIsTTY(true);
|
|
467
|
+
const api = mockWebsocketAPIs();
|
|
468
|
+
await runWrangler("tail test-worker");
|
|
469
|
+
|
|
470
|
+
const event = generateMockRequestEvent();
|
|
471
|
+
const message = generateMockEventMessage({
|
|
472
|
+
event,
|
|
473
|
+
logs: [
|
|
474
|
+
{ message: ["some string"], level: "log", timestamp: 1234561 },
|
|
475
|
+
{
|
|
476
|
+
message: [{ complex: "object" }],
|
|
477
|
+
level: "log",
|
|
478
|
+
timestamp: 1234562,
|
|
479
|
+
},
|
|
480
|
+
{ message: [1234], level: "error", timestamp: 1234563 },
|
|
481
|
+
],
|
|
482
|
+
exceptions: [
|
|
483
|
+
{ name: "Error", message: "some error", timestamp: 1234564 },
|
|
484
|
+
{ name: "Error", message: { complex: "error" }, timestamp: 1234564 },
|
|
485
|
+
],
|
|
486
|
+
});
|
|
487
|
+
const serializedMessage = serialize(message);
|
|
488
|
+
|
|
489
|
+
api.ws.send(serializedMessage);
|
|
490
|
+
expect(
|
|
491
|
+
std.out
|
|
492
|
+
.replace(
|
|
493
|
+
new Date(mockEventTimestamp).toLocaleString(),
|
|
494
|
+
"[mock event timestamp]"
|
|
495
|
+
)
|
|
496
|
+
.replace(
|
|
497
|
+
mockTailExpiration.toLocaleString(),
|
|
498
|
+
"[mock expiration date]"
|
|
499
|
+
)
|
|
500
|
+
).toMatchInlineSnapshot(`
|
|
501
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
502
|
+
Connected to test-worker, waiting for logs...
|
|
503
|
+
GET https://example.org/ - Ok @ [mock event timestamp]
|
|
504
|
+
(log) some string
|
|
505
|
+
(log) { complex: 'object' }
|
|
506
|
+
(error) 1234"
|
|
507
|
+
`);
|
|
508
|
+
expect(std.err).toMatchInlineSnapshot(`
|
|
509
|
+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m Error: some error[0m
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m Error: { complex: 'error' }[0m
|
|
513
|
+
|
|
514
|
+
"
|
|
515
|
+
`);
|
|
516
|
+
expect(std.warn).toMatchInlineSnapshot(`""`);
|
|
517
|
+
});
|
|
518
|
+
});
|
|
297
519
|
});
|
|
298
520
|
|
|
299
521
|
/* helpers */
|
|
@@ -308,44 +530,45 @@ describe("tail", () => {
|
|
|
308
530
|
* @returns the same type we expect when deserializing in wrangler
|
|
309
531
|
*/
|
|
310
532
|
function serialize(message: TailEventMessage): WebSocket.RawData {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
533
|
+
if (!isRequest(message.event)) {
|
|
534
|
+
// `ScheduledEvent`s and `TailEvent`s work just fine
|
|
535
|
+
const stringified = JSON.stringify(message);
|
|
536
|
+
return Buffer.from(stringified, "utf-8");
|
|
537
|
+
} else {
|
|
538
|
+
// Since the "properties" of an `undici.Request` are actually getters,
|
|
539
|
+
// which don't serialize properly, we need to hydrate them manually.
|
|
540
|
+
// This isn't a problem outside of testing since deserialization
|
|
541
|
+
// works just fine and wrangler never _sends_ any event messages,
|
|
542
|
+
// it only receives them.
|
|
543
|
+
const request = ((message.event as RequestEvent | undefined | null) || {})
|
|
544
|
+
.request;
|
|
545
|
+
const stringified = JSON.stringify(message, (key, value) => {
|
|
546
|
+
if (key !== "request") {
|
|
547
|
+
return value;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
return {
|
|
551
|
+
...request,
|
|
552
|
+
url: request?.url,
|
|
553
|
+
headers: request?.headers,
|
|
554
|
+
method: request?.method,
|
|
555
|
+
};
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
return Buffer.from(stringified, "utf-8");
|
|
559
|
+
}
|
|
337
560
|
}
|
|
338
561
|
|
|
339
562
|
/**
|
|
340
563
|
* Small helper to disambiguate the event types possible in a `TailEventMessage`
|
|
341
564
|
*
|
|
342
565
|
* @param event A TailEvent
|
|
343
|
-
* @returns
|
|
566
|
+
* @returns true if `event` is a RequestEvent
|
|
344
567
|
*/
|
|
345
|
-
function
|
|
346
|
-
|
|
347
|
-
): event is
|
|
348
|
-
|
|
568
|
+
function isRequest(
|
|
569
|
+
event: ScheduledEvent | RequestEvent | AlarmEvent | undefined | null
|
|
570
|
+
): event is RequestEvent {
|
|
571
|
+
return Boolean(event && "request" in event);
|
|
349
572
|
}
|
|
350
573
|
|
|
351
574
|
/**
|
|
@@ -357,7 +580,7 @@ function isScheduled(
|
|
|
357
580
|
* @returns a string ready to be printed to the terminal or compared against
|
|
358
581
|
*/
|
|
359
582
|
function deserializeToJson(message: WebSocket.RawData): string {
|
|
360
|
-
|
|
583
|
+
return JSON.stringify(JSON.parse(message.toString()), null, 2);
|
|
361
584
|
}
|
|
362
585
|
|
|
363
586
|
/**
|
|
@@ -365,12 +588,12 @@ function deserializeToJson(message: WebSocket.RawData): string {
|
|
|
365
588
|
* when running `wrangler tail`
|
|
366
589
|
*/
|
|
367
590
|
type MockAPI = {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
591
|
+
requests: {
|
|
592
|
+
creation: RequestCounter;
|
|
593
|
+
deletion: RequestCounter;
|
|
594
|
+
};
|
|
595
|
+
ws: MockWebSocket;
|
|
596
|
+
nextMessageJson(): Promise<unknown>;
|
|
374
597
|
};
|
|
375
598
|
|
|
376
599
|
/**
|
|
@@ -379,7 +602,7 @@ type MockAPI = {
|
|
|
379
602
|
* the correct API calls without actually sending any web traffic
|
|
380
603
|
*/
|
|
381
604
|
type RequestCounter = {
|
|
382
|
-
|
|
605
|
+
count: number;
|
|
383
606
|
};
|
|
384
607
|
|
|
385
608
|
/**
|
|
@@ -389,34 +612,34 @@ type RequestCounter = {
|
|
|
389
612
|
* @returns a `RequestCounter` for counting how many times the API is hit
|
|
390
613
|
*/
|
|
391
614
|
function mockCreateTailRequest(
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
615
|
+
websocketURL: string,
|
|
616
|
+
env?: string,
|
|
617
|
+
legacyEnv = false
|
|
395
618
|
): RequestCounter {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
619
|
+
const requests = { count: 0 };
|
|
620
|
+
const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
|
|
621
|
+
const environment = env && !legacyEnv ? "/environments/:envName" : "";
|
|
622
|
+
setMockResponse(
|
|
623
|
+
`/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/tails`,
|
|
624
|
+
"POST",
|
|
625
|
+
([_url, accountId, scriptName, envName]) => {
|
|
626
|
+
requests.count++;
|
|
627
|
+
expect(accountId).toEqual("some-account-id");
|
|
628
|
+
expect(scriptName).toEqual(
|
|
629
|
+
legacyEnv && env ? `test-worker-${env}` : "test-worker"
|
|
630
|
+
);
|
|
631
|
+
if (!legacyEnv) {
|
|
632
|
+
expect(envName).toEqual(env);
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
id: "tail-id",
|
|
636
|
+
url: websocketURL,
|
|
637
|
+
expires_at: mockTailExpiration,
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
return requests;
|
|
420
643
|
}
|
|
421
644
|
|
|
422
645
|
/**
|
|
@@ -429,43 +652,48 @@ const mockTailExpiration = new Date(3005, 1);
|
|
|
429
652
|
*/
|
|
430
653
|
const mockEventTimestamp = 1645454470467;
|
|
431
654
|
|
|
655
|
+
/**
|
|
656
|
+
* Default value for event time ISO strings
|
|
657
|
+
*/
|
|
658
|
+
const mockEventScheduledTime = new Date(mockEventTimestamp).toISOString();
|
|
659
|
+
|
|
432
660
|
/**
|
|
433
661
|
* Mock out the API hit during Tail deletion
|
|
434
662
|
*
|
|
435
663
|
* @returns a `RequestCounter` for counting how many times the API is hit
|
|
436
664
|
*/
|
|
437
665
|
function mockDeleteTailRequest(
|
|
438
|
-
|
|
439
|
-
|
|
666
|
+
env?: string,
|
|
667
|
+
legacyEnv = false
|
|
440
668
|
): RequestCounter {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
669
|
+
const requests = { count: 0 };
|
|
670
|
+
const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
|
|
671
|
+
const environment = env && !legacyEnv ? "/environments/:envName" : "";
|
|
672
|
+
setMockResponse(
|
|
673
|
+
// "/accounts/:accountId/workers/scripts/:worker/tails",
|
|
674
|
+
`/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/tails/:tailId`,
|
|
675
|
+
"DELETE",
|
|
676
|
+
([_url, accountId, scriptName, envNameOrTailId, tailId]) => {
|
|
677
|
+
requests.count++;
|
|
678
|
+
expect(accountId).toEqual("some-account-id");
|
|
679
|
+
expect(scriptName).toEqual(
|
|
680
|
+
legacyEnv && env ? `test-worker-${env}` : "test-worker"
|
|
681
|
+
);
|
|
682
|
+
if (!legacyEnv) {
|
|
683
|
+
if (env) {
|
|
684
|
+
expect(envNameOrTailId).toEqual(env);
|
|
685
|
+
expect(tailId).toEqual("tail-id");
|
|
686
|
+
} else {
|
|
687
|
+
expect(envNameOrTailId).toEqual("tail-id");
|
|
688
|
+
}
|
|
689
|
+
} else {
|
|
690
|
+
expect(envNameOrTailId).toEqual("tail-id");
|
|
691
|
+
}
|
|
692
|
+
return null;
|
|
693
|
+
}
|
|
694
|
+
);
|
|
695
|
+
|
|
696
|
+
return requests;
|
|
469
697
|
}
|
|
470
698
|
|
|
471
699
|
const mockWebSockets: MockWebSocket[] = [];
|
|
@@ -478,30 +706,30 @@ const mockWebSockets: MockWebSocket[] = [];
|
|
|
478
706
|
* @returns a mocked-out version of the API
|
|
479
707
|
*/
|
|
480
708
|
function mockWebsocketAPIs(env?: string, legacyEnv = false): MockAPI {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
709
|
+
const websocketURL = "ws://localhost:1234";
|
|
710
|
+
const api: MockAPI = {
|
|
711
|
+
requests: {
|
|
712
|
+
deletion: { count: 0 },
|
|
713
|
+
creation: { count: 0 },
|
|
714
|
+
},
|
|
715
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
716
|
+
ws: null!, // will be set in the `beforeEach()` below.
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Parse the next message received by the mock websocket as JSON
|
|
720
|
+
* @returns JSON.parse of the next message received by the websocket
|
|
721
|
+
*/
|
|
722
|
+
async nextMessageJson() {
|
|
723
|
+
const message = await api.ws.nextMessage;
|
|
724
|
+
return JSON.parse(message as string);
|
|
725
|
+
},
|
|
726
|
+
};
|
|
727
|
+
api.requests.creation = mockCreateTailRequest(websocketURL, env, legacyEnv);
|
|
728
|
+
api.requests.deletion = mockDeleteTailRequest(env, legacyEnv);
|
|
729
|
+
api.ws = new MockWebSocket(websocketURL);
|
|
730
|
+
mockWebSockets.push(api.ws);
|
|
731
|
+
|
|
732
|
+
return api;
|
|
505
733
|
}
|
|
506
734
|
|
|
507
735
|
/**
|
|
@@ -511,16 +739,20 @@ function mockWebsocketAPIs(env?: string, legacyEnv = false): MockAPI {
|
|
|
511
739
|
* @param opts Any specific parts of the message to use instead of defaults
|
|
512
740
|
* @returns a `TailEventMessage` that wrangler can process and display
|
|
513
741
|
*/
|
|
514
|
-
function generateMockEventMessage(
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
742
|
+
function generateMockEventMessage({
|
|
743
|
+
outcome = "ok",
|
|
744
|
+
exceptions = [],
|
|
745
|
+
logs = [],
|
|
746
|
+
eventTimestamp = mockEventTimestamp,
|
|
747
|
+
event = generateMockRequestEvent(),
|
|
748
|
+
}: Partial<TailEventMessage>): TailEventMessage {
|
|
749
|
+
return {
|
|
750
|
+
outcome,
|
|
751
|
+
exceptions,
|
|
752
|
+
logs,
|
|
753
|
+
eventTimestamp,
|
|
754
|
+
event,
|
|
755
|
+
};
|
|
524
756
|
}
|
|
525
757
|
|
|
526
758
|
/**
|
|
@@ -531,34 +763,40 @@ function generateMockEventMessage(
|
|
|
531
763
|
* @returns a `RequestEvent` that can be used within an `EventMessage`
|
|
532
764
|
*/
|
|
533
765
|
function generateMockRequestEvent(
|
|
534
|
-
|
|
766
|
+
opts?: Partial<RequestEvent["request"]>
|
|
535
767
|
): RequestEvent {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
768
|
+
return {
|
|
769
|
+
request: Object.assign(
|
|
770
|
+
new Request(opts?.url || "https://example.org/", {
|
|
771
|
+
method: opts?.method || "GET",
|
|
772
|
+
headers:
|
|
773
|
+
opts?.headers || new Headers({ "X-EXAMPLE-HEADER": "some_value" }),
|
|
774
|
+
}),
|
|
775
|
+
{
|
|
776
|
+
cf: opts?.cf || {
|
|
777
|
+
tlsCipher: "AEAD-ENCRYPT-O-MATIC-SHA",
|
|
778
|
+
tlsVersion: "TLSv2.0",
|
|
779
|
+
asn: 42069,
|
|
780
|
+
colo: "ATL",
|
|
781
|
+
httpProtocol: "HTTP/4",
|
|
782
|
+
asOrganization: "Cloudflare",
|
|
783
|
+
},
|
|
784
|
+
}
|
|
785
|
+
),
|
|
786
|
+
};
|
|
555
787
|
}
|
|
556
788
|
|
|
557
789
|
function generateMockScheduledEvent(
|
|
558
|
-
|
|
790
|
+
opts?: Partial<ScheduledEvent>
|
|
559
791
|
): ScheduledEvent {
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
792
|
+
return {
|
|
793
|
+
cron: opts?.cron || "* * * * *",
|
|
794
|
+
scheduledTime: opts?.scheduledTime || mockEventTimestamp,
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function generateMockAlarmEvent(opts?: Partial<AlarmEvent>): AlarmEvent {
|
|
799
|
+
return {
|
|
800
|
+
scheduledTime: opts?.scheduledTime || mockEventScheduledTime,
|
|
801
|
+
};
|
|
564
802
|
}
|