wrangler 0.0.0-e3cab74 → 0.0.0-e452a35
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 +27 -14
- package/bin/wrangler.js +49 -5
- package/miniflare-dist/index.mjs +4886 -0
- package/package.json +42 -28
- package/pages/functions/buildPlugin.ts +105 -0
- package/pages/functions/buildWorker.ts +93 -4
- package/pages/functions/filepath-routing.test.ts +136 -25
- package/pages/functions/filepath-routing.ts +14 -3
- package/pages/functions/routes.ts +6 -1
- package/pages/functions/template-plugin.ts +147 -0
- package/pages/functions/template-worker.ts +21 -8
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +1514 -367
- package/src/__tests__/dev.test.tsx +870 -84
- package/src/__tests__/guess-worker-format.test.ts +37 -18
- package/src/__tests__/helpers/mock-account-id.ts +22 -4
- package/src/__tests__/helpers/mock-cfetch.ts +8 -4
- package/src/__tests__/helpers/mock-console.ts +35 -10
- package/src/__tests__/helpers/mock-dialogs.ts +26 -1
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +27 -0
- package/src/__tests__/helpers/mock-kv.ts +9 -16
- package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
- package/src/__tests__/helpers/mock-stdin.ts +103 -0
- package/src/__tests__/helpers/run-in-tmp.ts +10 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/https-options.test.ts +130 -0
- package/src/__tests__/index.test.ts +137 -747
- package/src/__tests__/init.test.ts +1557 -0
- package/src/__tests__/jest.setup.ts +28 -8
- package/src/__tests__/kv.test.ts +598 -258
- package/src/__tests__/logger.test.ts +115 -0
- package/src/__tests__/pages.test.ts +657 -0
- package/src/__tests__/parse.test.ts +8 -0
- package/src/__tests__/publish.test.ts +4003 -941
- package/src/__tests__/r2.test.ts +69 -34
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +228 -73
- package/src/__tests__/tail.test.ts +146 -20
- package/src/__tests__/user.test.ts +131 -0
- package/src/__tests__/whoami.test.tsx +26 -7
- package/src/abort.d.ts +3 -0
- package/src/bundle.ts +74 -7
- package/src/cfetch/index.ts +21 -4
- package/src/cfetch/internal.ts +20 -14
- package/src/cli.ts +5 -11
- package/src/config/README.md +7 -7
- package/src/config/config.ts +69 -100
- package/src/config/environment.ts +119 -4
- package/src/config/index.ts +177 -4
- package/src/config/validation-helpers.ts +228 -23
- package/src/config/validation.ts +687 -174
- package/src/config-cache.ts +45 -0
- package/src/{api/preview.ts → create-worker-preview.ts} +74 -36
- package/src/{api/form_data.ts → create-worker-upload-form.ts} +55 -18
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +422 -0
- package/src/dev/local.tsx +318 -0
- package/src/dev/remote.tsx +227 -0
- package/src/dev/use-esbuild.ts +118 -0
- package/src/dialogs.tsx +16 -6
- package/src/durable.ts +102 -0
- package/src/entry.ts +279 -14
- package/src/environment-variables.ts +3 -1
- package/src/errors.ts +11 -0
- package/src/https-options.ts +123 -0
- package/src/index.tsx +1268 -806
- package/src/inspect.ts +64 -11
- package/src/intl-polyfill.d.ts +139 -0
- package/src/{kv.tsx → kv.ts} +195 -62
- package/src/logger.ts +66 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +41 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +94 -6
- package/src/open-in-browser.ts +9 -3
- package/src/package-manager.ts +9 -8
- package/src/pages.tsx +1057 -75
- package/src/parse.ts +24 -5
- package/src/proxy.ts +143 -54
- package/src/publish.ts +444 -229
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +98 -44
- package/src/tail/index.ts +2 -2
- package/src/tail/printing.ts +13 -8
- package/src/update-check.ts +19 -0
- package/src/user.tsx +263 -109
- package/src/whoami.tsx +2 -1
- package/src/{api/worker.ts → worker.ts} +24 -30
- package/templates/gitignore +171 -0
- package/templates/new-worker.ts +16 -1
- package/templates/static-asset-facade.js +1 -1
- package/wrangler-dist/cli.js +55345 -69246
- package/src/__tests__/helpers/mock-user.ts +0 -27
- package/src/__tests__/logout.test.ts +0 -50
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/dev.tsx +0 -961
- package/src/guess-worker-format.ts +0 -68
- package/src/reporting.ts +0 -115
- package/wrangler-dist/cli.js.map +0 -7
|
@@ -3,6 +3,7 @@ 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 { useMockIsTTY } from "./helpers/mock-istty";
|
|
6
7
|
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
7
8
|
import { runWrangler } from "./helpers/run-wrangler";
|
|
8
9
|
import type { TailEventMessage, RequestEvent, ScheduledEvent } from "../tail";
|
|
@@ -134,7 +135,7 @@ describe("tail", () => {
|
|
|
134
135
|
|
|
135
136
|
it("sends header filters without a query", async () => {
|
|
136
137
|
const api = mockWebsocketAPIs();
|
|
137
|
-
await runWrangler("tail test-worker --header X-CUSTOM-HEADER
|
|
138
|
+
await runWrangler("tail test-worker --header X-CUSTOM-HEADER");
|
|
138
139
|
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
139
140
|
{ header: { key: "X-CUSTOM-HEADER" } },
|
|
140
141
|
]);
|
|
@@ -142,9 +143,7 @@ describe("tail", () => {
|
|
|
142
143
|
|
|
143
144
|
it("sends header filters with a query", async () => {
|
|
144
145
|
const api = mockWebsocketAPIs();
|
|
145
|
-
await runWrangler(
|
|
146
|
-
"tail test-worker --header X-CUSTOM-HEADER:some-value "
|
|
147
|
-
);
|
|
146
|
+
await runWrangler("tail test-worker --header X-CUSTOM-HEADER:some-value");
|
|
148
147
|
await expect(api.nextMessageJson()).resolves.toHaveProperty("filters", [
|
|
149
148
|
{ header: { key: "X-CUSTOM-HEADER", query: "some-value" } },
|
|
150
149
|
]);
|
|
@@ -218,6 +217,8 @@ describe("tail", () => {
|
|
|
218
217
|
});
|
|
219
218
|
|
|
220
219
|
describe("printing", () => {
|
|
220
|
+
const { setIsTTY } = useMockIsTTY();
|
|
221
|
+
|
|
221
222
|
it("logs request messages in JSON format", async () => {
|
|
222
223
|
const api = mockWebsocketAPIs();
|
|
223
224
|
await runWrangler("tail test-worker --format json");
|
|
@@ -262,7 +263,7 @@ describe("tail", () => {
|
|
|
262
263
|
"[mock expiration date]"
|
|
263
264
|
)
|
|
264
265
|
).toMatchInlineSnapshot(`
|
|
265
|
-
"
|
|
266
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
266
267
|
Connected to test-worker, waiting for logs...
|
|
267
268
|
GET https://example.org/ - Ok @ [mock event timestamp]"
|
|
268
269
|
`);
|
|
@@ -288,11 +289,131 @@ describe("tail", () => {
|
|
|
288
289
|
"[mock expiration date]"
|
|
289
290
|
)
|
|
290
291
|
).toMatchInlineSnapshot(`
|
|
291
|
-
"
|
|
292
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
292
293
|
Connected to test-worker, waiting for logs...
|
|
293
294
|
\\"* * * * *\\" @ [mock timestamp string] - Ok"
|
|
294
295
|
`);
|
|
295
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
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
318
|
+
Connected to test-worker, waiting for logs...
|
|
319
|
+
[missing request] - Ok @ [mock timestamp string]"
|
|
320
|
+
`);
|
|
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
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
345
|
+
Connected to test-worker, waiting for logs...
|
|
346
|
+
GET https://example.org/ - Ok @ [mock event timestamp]"
|
|
347
|
+
`);
|
|
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
|
+
"Successfully created tail, expires at [mock expiration date]
|
|
401
|
+
Connected to test-worker, waiting for logs...
|
|
402
|
+
GET https://example.org/ - Ok @ [mock event timestamp]
|
|
403
|
+
(log) some string
|
|
404
|
+
(log) { complex: 'object' }
|
|
405
|
+
(error) 1234"
|
|
406
|
+
`);
|
|
407
|
+
expect(std.err).toMatchInlineSnapshot(`
|
|
408
|
+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m Error: some error[0m
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m Error: { complex: 'error' }[0m
|
|
412
|
+
|
|
413
|
+
"
|
|
414
|
+
`);
|
|
415
|
+
expect(std.warn).toMatchInlineSnapshot(`""`);
|
|
416
|
+
});
|
|
296
417
|
});
|
|
297
418
|
});
|
|
298
419
|
|
|
@@ -318,7 +439,8 @@ function serialize(message: TailEventMessage): WebSocket.RawData {
|
|
|
318
439
|
// This isn't a problem outside of testing since deserialization
|
|
319
440
|
// works just fine and wrangler never _sends_ any event messages,
|
|
320
441
|
// it only receives them.
|
|
321
|
-
const request = (message.event as RequestEvent)
|
|
442
|
+
const request = ((message.event as RequestEvent | undefined | null) || {})
|
|
443
|
+
.request;
|
|
322
444
|
const stringified = JSON.stringify(message, (key, value) => {
|
|
323
445
|
if (key !== "request") {
|
|
324
446
|
return value;
|
|
@@ -326,9 +448,9 @@ function serialize(message: TailEventMessage): WebSocket.RawData {
|
|
|
326
448
|
|
|
327
449
|
return {
|
|
328
450
|
...request,
|
|
329
|
-
url: request
|
|
330
|
-
headers: request
|
|
331
|
-
method: request
|
|
451
|
+
url: request?.url,
|
|
452
|
+
headers: request?.headers,
|
|
453
|
+
method: request?.method,
|
|
332
454
|
};
|
|
333
455
|
});
|
|
334
456
|
|
|
@@ -343,9 +465,9 @@ function serialize(message: TailEventMessage): WebSocket.RawData {
|
|
|
343
465
|
* @returns whether event is a ScheduledEvent (true) or a RequestEvent
|
|
344
466
|
*/
|
|
345
467
|
function isScheduled(
|
|
346
|
-
event: ScheduledEvent | RequestEvent
|
|
468
|
+
event: ScheduledEvent | RequestEvent | undefined | null
|
|
347
469
|
): event is ScheduledEvent {
|
|
348
|
-
return "cron" in event;
|
|
470
|
+
return Boolean(event && "cron" in event);
|
|
349
471
|
}
|
|
350
472
|
|
|
351
473
|
/**
|
|
@@ -511,15 +633,19 @@ function mockWebsocketAPIs(env?: string, legacyEnv = false): MockAPI {
|
|
|
511
633
|
* @param opts Any specific parts of the message to use instead of defaults
|
|
512
634
|
* @returns a `TailEventMessage` that wrangler can process and display
|
|
513
635
|
*/
|
|
514
|
-
function generateMockEventMessage(
|
|
515
|
-
|
|
516
|
-
|
|
636
|
+
function generateMockEventMessage({
|
|
637
|
+
outcome = "ok",
|
|
638
|
+
exceptions = [],
|
|
639
|
+
logs = [],
|
|
640
|
+
eventTimestamp = mockEventTimestamp,
|
|
641
|
+
event = generateMockRequestEvent(),
|
|
642
|
+
}: Partial<TailEventMessage>): TailEventMessage {
|
|
517
643
|
return {
|
|
518
|
-
outcome
|
|
519
|
-
exceptions
|
|
520
|
-
logs
|
|
521
|
-
eventTimestamp
|
|
522
|
-
event
|
|
644
|
+
outcome,
|
|
645
|
+
exceptions,
|
|
646
|
+
logs,
|
|
647
|
+
eventTimestamp,
|
|
648
|
+
event,
|
|
523
649
|
};
|
|
524
650
|
}
|
|
525
651
|
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import fetchMock from "jest-fetch-mock";
|
|
5
|
+
import {
|
|
6
|
+
readAuthConfigFile,
|
|
7
|
+
requireAuth,
|
|
8
|
+
USER_AUTH_CONFIG_FILE,
|
|
9
|
+
writeAuthConfigFile,
|
|
10
|
+
} from "../user";
|
|
11
|
+
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
12
|
+
import { useMockIsTTY } from "./helpers/mock-istty";
|
|
13
|
+
import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
|
|
14
|
+
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
15
|
+
import { runWrangler } from "./helpers/run-wrangler";
|
|
16
|
+
import type { Config } from "../config";
|
|
17
|
+
import type { UserAuthConfig } from "../user";
|
|
18
|
+
|
|
19
|
+
describe("User", () => {
|
|
20
|
+
runInTempDir({ homedir: "./home" });
|
|
21
|
+
const std = mockConsoleMethods();
|
|
22
|
+
const {
|
|
23
|
+
mockOAuthServerCallback,
|
|
24
|
+
mockGrantAccessToken,
|
|
25
|
+
mockGrantAuthorization,
|
|
26
|
+
mockRevokeAuthorization,
|
|
27
|
+
mockExchangeRefreshTokenForAccessToken,
|
|
28
|
+
} = mockOAuthFlow();
|
|
29
|
+
|
|
30
|
+
const { setIsTTY } = useMockIsTTY();
|
|
31
|
+
|
|
32
|
+
describe("login", () => {
|
|
33
|
+
it("should login a user when `wrangler login` is run", async () => {
|
|
34
|
+
mockOAuthServerCallback();
|
|
35
|
+
const accessTokenRequest = mockGrantAccessToken({ respondWith: "ok" });
|
|
36
|
+
mockGrantAuthorization({ respondWith: "success" });
|
|
37
|
+
|
|
38
|
+
await runWrangler("login");
|
|
39
|
+
|
|
40
|
+
expect(accessTokenRequest.actual.url).toEqual(
|
|
41
|
+
accessTokenRequest.expected.url
|
|
42
|
+
);
|
|
43
|
+
expect(accessTokenRequest.actual.method).toEqual(
|
|
44
|
+
accessTokenRequest.expected.method
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
expect(std.out).toMatchInlineSnapshot(`
|
|
48
|
+
"Attempting to login via OAuth...
|
|
49
|
+
Successfully logged in."
|
|
50
|
+
`);
|
|
51
|
+
|
|
52
|
+
expect(readAuthConfigFile()).toEqual<UserAuthConfig>({
|
|
53
|
+
api_token: undefined,
|
|
54
|
+
oauth_token: "test-access-token",
|
|
55
|
+
refresh_token: "test-refresh-token",
|
|
56
|
+
expiration_time: expect.any(String),
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("logout", () => {
|
|
62
|
+
it("should exit with a message stating the user is not logged in", async () => {
|
|
63
|
+
await runWrangler("logout");
|
|
64
|
+
expect(std.out).toMatchInlineSnapshot(`"Not logged in, exiting..."`);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("should logout user that has been properly logged in", async () => {
|
|
68
|
+
writeAuthConfigFile({
|
|
69
|
+
oauth_token: "some-oauth-tok",
|
|
70
|
+
refresh_token: "some-refresh-tok",
|
|
71
|
+
});
|
|
72
|
+
const outcome = mockRevokeAuthorization();
|
|
73
|
+
|
|
74
|
+
await runWrangler("logout");
|
|
75
|
+
|
|
76
|
+
expect(outcome.actual.url).toEqual(
|
|
77
|
+
"https://dash.cloudflare.com/oauth2/revoke"
|
|
78
|
+
);
|
|
79
|
+
expect(outcome.actual.method).toEqual("POST");
|
|
80
|
+
|
|
81
|
+
expect(std.out).toMatchInlineSnapshot(`"Successfully logged out."`);
|
|
82
|
+
|
|
83
|
+
// Make sure that we made the request to logout.
|
|
84
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
85
|
+
|
|
86
|
+
// Make sure that logout removed the config file containing the auth tokens.
|
|
87
|
+
const config = path.join(os.homedir(), USER_AUTH_CONFIG_FILE);
|
|
88
|
+
expect(fs.existsSync(config)).toBeFalsy();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// TODO: Improve OAuth mocking to handle `/token` endpoints from different calls
|
|
93
|
+
it("should handle errors for failed token refresh", async () => {
|
|
94
|
+
setIsTTY(false);
|
|
95
|
+
mockOAuthServerCallback();
|
|
96
|
+
writeAuthConfigFile({
|
|
97
|
+
oauth_token: "hunter2",
|
|
98
|
+
refresh_token: "Order 66",
|
|
99
|
+
});
|
|
100
|
+
mockGrantAuthorization({ respondWith: "success" });
|
|
101
|
+
|
|
102
|
+
mockExchangeRefreshTokenForAccessToken({
|
|
103
|
+
respondWith: "badResponse",
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Handles the requireAuth error throw from failed login that is unhandled due to directly calling it here
|
|
107
|
+
await expect(
|
|
108
|
+
requireAuth({} as Config)
|
|
109
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
110
|
+
`"Did not login, quitting..."`
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should confirm no error message when refresh is successful", async () => {
|
|
115
|
+
setIsTTY(false);
|
|
116
|
+
mockOAuthServerCallback();
|
|
117
|
+
writeAuthConfigFile({
|
|
118
|
+
oauth_token: "hunter2",
|
|
119
|
+
refresh_token: "Order 66",
|
|
120
|
+
});
|
|
121
|
+
mockGrantAuthorization({ respondWith: "success" });
|
|
122
|
+
|
|
123
|
+
mockExchangeRefreshTokenForAccessToken({
|
|
124
|
+
respondWith: "refreshSuccess",
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Handles the requireAuth error throw from failed login that is unhandled due to directly calling it here
|
|
128
|
+
await expect(requireAuth({} as Config)).rejects.toThrowError();
|
|
129
|
+
expect(std.err).toContain("");
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -1,30 +1,36 @@
|
|
|
1
1
|
import { render } from "ink-testing-library";
|
|
2
2
|
import React from "react";
|
|
3
|
-
import {
|
|
3
|
+
import { writeAuthConfigFile } from "../user";
|
|
4
4
|
import { getUserInfo, WhoAmI } from "../whoami";
|
|
5
5
|
import { setMockResponse } from "./helpers/mock-cfetch";
|
|
6
|
-
import {
|
|
6
|
+
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
7
|
+
import { useMockIsTTY } from "./helpers/mock-istty";
|
|
7
8
|
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
8
9
|
import type { UserInfo } from "../whoami";
|
|
9
10
|
|
|
10
11
|
describe("getUserInfo()", () => {
|
|
11
12
|
runInTempDir({ homedir: "./home" });
|
|
13
|
+
const std = mockConsoleMethods();
|
|
14
|
+
const { setIsTTY } = useMockIsTTY();
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
setIsTTY(true);
|
|
18
|
+
});
|
|
12
19
|
|
|
13
20
|
it("should return undefined if there is no config file", async () => {
|
|
14
|
-
await initialise();
|
|
15
21
|
const userInfo = await getUserInfo();
|
|
16
22
|
expect(userInfo).toBeUndefined();
|
|
17
23
|
});
|
|
18
24
|
|
|
19
25
|
it("should return undefined if there is an empty config file", async () => {
|
|
20
|
-
|
|
21
|
-
await initialise();
|
|
26
|
+
writeAuthConfigFile({});
|
|
22
27
|
const userInfo = await getUserInfo();
|
|
23
28
|
expect(userInfo).toBeUndefined();
|
|
24
29
|
});
|
|
25
30
|
|
|
26
31
|
it("should return the user's email and accounts if authenticated via config token", async () => {
|
|
27
|
-
|
|
32
|
+
writeAuthConfigFile({ oauth_token: "some-oauth-token" });
|
|
33
|
+
|
|
28
34
|
setMockResponse("/user", () => {
|
|
29
35
|
return { email: "user@example.com" };
|
|
30
36
|
});
|
|
@@ -36,7 +42,6 @@ describe("getUserInfo()", () => {
|
|
|
36
42
|
];
|
|
37
43
|
});
|
|
38
44
|
|
|
39
|
-
await initialise();
|
|
40
45
|
const userInfo = await getUserInfo();
|
|
41
46
|
|
|
42
47
|
expect(userInfo).toEqual({
|
|
@@ -50,6 +55,20 @@ describe("getUserInfo()", () => {
|
|
|
50
55
|
],
|
|
51
56
|
});
|
|
52
57
|
});
|
|
58
|
+
|
|
59
|
+
it("should display a warning message if the config file contains a legacy api_token field", async () => {
|
|
60
|
+
writeAuthConfigFile({ api_token: "API_TOKEN" });
|
|
61
|
+
await getUserInfo();
|
|
62
|
+
expect(std.warn).toMatchInlineSnapshot(`
|
|
63
|
+
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mIt looks like you have used Wrangler 1's \`config\` command to login with an API token.[0m
|
|
64
|
+
|
|
65
|
+
This is no longer supported in the current version of Wrangler.
|
|
66
|
+
If you wish to authenticate via an API token then please set the \`CLOUDFLARE_API_TOKEN\`
|
|
67
|
+
environment variable.
|
|
68
|
+
|
|
69
|
+
"
|
|
70
|
+
`);
|
|
71
|
+
});
|
|
53
72
|
});
|
|
54
73
|
|
|
55
74
|
describe("WhoAmI component", () => {
|
package/src/abort.d.ts
ADDED
package/src/bundle.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import assert from "node:assert";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
|
+
import { builtinModules } from "node:module";
|
|
3
4
|
import * as path from "node:path";
|
|
5
|
+
import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
|
|
6
|
+
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
|
|
4
7
|
import * as esbuild from "esbuild";
|
|
5
|
-
import
|
|
6
|
-
import type { CfModule, CfScriptFormat } from "./api/worker";
|
|
8
|
+
import createModuleCollector from "./module-collection";
|
|
7
9
|
import type { Config } from "./config";
|
|
8
10
|
import type { Entry } from "./entry";
|
|
11
|
+
import type { CfModule } from "./worker";
|
|
9
12
|
|
|
10
13
|
type BundleResult = {
|
|
11
14
|
modules: CfModule[];
|
|
@@ -14,6 +17,33 @@ type BundleResult = {
|
|
|
14
17
|
stop: (() => void) | undefined;
|
|
15
18
|
};
|
|
16
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Searches for any uses of node's builtin modules, and throws an error if it
|
|
22
|
+
* finds anything. This plugin is only used when nodeCompat is not enabled.
|
|
23
|
+
* Supports both regular node builtins, and the new "node:<MODULE>" format.
|
|
24
|
+
*/
|
|
25
|
+
const checkForNodeBuiltinsPlugin = {
|
|
26
|
+
name: "checkForNodeBuiltins",
|
|
27
|
+
setup(build: esbuild.PluginBuild) {
|
|
28
|
+
build.onResolve(
|
|
29
|
+
{
|
|
30
|
+
filter: new RegExp(
|
|
31
|
+
"^(" +
|
|
32
|
+
builtinModules.join("|") +
|
|
33
|
+
"|" +
|
|
34
|
+
builtinModules.map((module) => "node:" + module).join("|") +
|
|
35
|
+
")$"
|
|
36
|
+
),
|
|
37
|
+
},
|
|
38
|
+
() => {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`Detected a Node builtin module import while Node compatibility is disabled.\nAdd node_compat = true to your wrangler.toml file to enable Node compatibility.`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
17
47
|
/**
|
|
18
48
|
* Generate a bundle for the worker identified by the arguments passed in.
|
|
19
49
|
*/
|
|
@@ -24,38 +54,75 @@ export async function bundleWorker(
|
|
|
24
54
|
serveAssetsFromWorker: boolean;
|
|
25
55
|
jsxFactory: string | undefined;
|
|
26
56
|
jsxFragment: string | undefined;
|
|
27
|
-
format: CfScriptFormat;
|
|
28
57
|
rules: Config["rules"];
|
|
29
58
|
watch?: esbuild.WatchMode;
|
|
59
|
+
tsconfig: string | undefined;
|
|
60
|
+
minify: boolean | undefined;
|
|
61
|
+
nodeCompat: boolean | undefined;
|
|
30
62
|
}
|
|
31
63
|
): Promise<BundleResult> {
|
|
32
64
|
const {
|
|
33
65
|
serveAssetsFromWorker,
|
|
34
66
|
jsxFactory,
|
|
35
67
|
jsxFragment,
|
|
36
|
-
format,
|
|
37
68
|
rules,
|
|
38
69
|
watch,
|
|
70
|
+
tsconfig,
|
|
71
|
+
minify,
|
|
72
|
+
nodeCompat,
|
|
39
73
|
} = options;
|
|
40
|
-
const
|
|
74
|
+
const entryDirectory = path.dirname(entry.file);
|
|
75
|
+
const moduleCollector = createModuleCollector({
|
|
76
|
+
wrangler1xlegacyModuleReferences: {
|
|
77
|
+
rootDirectory: entryDirectory,
|
|
78
|
+
fileNames: new Set(
|
|
79
|
+
fs
|
|
80
|
+
.readdirSync(entryDirectory, { withFileTypes: true })
|
|
81
|
+
.filter(
|
|
82
|
+
(dirEntry) =>
|
|
83
|
+
dirEntry.isFile() && dirEntry.name !== path.basename(entry.file)
|
|
84
|
+
)
|
|
85
|
+
.map((dirEnt) => dirEnt.name)
|
|
86
|
+
),
|
|
87
|
+
},
|
|
88
|
+
format: entry.format,
|
|
89
|
+
rules,
|
|
90
|
+
});
|
|
91
|
+
|
|
41
92
|
const result = await esbuild.build({
|
|
42
93
|
...getEntryPoint(entry.file, serveAssetsFromWorker),
|
|
43
94
|
bundle: true,
|
|
44
95
|
absWorkingDir: entry.directory,
|
|
45
96
|
outdir: destination,
|
|
46
97
|
external: ["__STATIC_CONTENT_MANIFEST"],
|
|
47
|
-
format: "esm",
|
|
98
|
+
format: entry.format === "modules" ? "esm" : "iife",
|
|
99
|
+
target: "es2020",
|
|
48
100
|
sourcemap: true,
|
|
101
|
+
minify,
|
|
49
102
|
metafile: true,
|
|
50
103
|
conditions: ["worker", "browser"],
|
|
104
|
+
...(process.env.NODE_ENV && {
|
|
105
|
+
define: {
|
|
106
|
+
"process.env.NODE_ENV": `"${process.env.NODE_ENV}"`,
|
|
107
|
+
...(nodeCompat ? { global: "globalThis" } : {}),
|
|
108
|
+
},
|
|
109
|
+
}),
|
|
51
110
|
loader: {
|
|
52
111
|
".js": "jsx",
|
|
53
112
|
".mjs": "jsx",
|
|
54
113
|
".cjs": "jsx",
|
|
55
114
|
},
|
|
56
|
-
plugins: [
|
|
115
|
+
plugins: [
|
|
116
|
+
moduleCollector.plugin,
|
|
117
|
+
...(nodeCompat
|
|
118
|
+
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
|
|
119
|
+
: // we use checkForNodeBuiltinsPlugin to throw a nicer error
|
|
120
|
+
// if we find node builtins when nodeCompat isn't turned on
|
|
121
|
+
[checkForNodeBuiltinsPlugin]),
|
|
122
|
+
],
|
|
57
123
|
...(jsxFactory && { jsxFactory }),
|
|
58
124
|
...(jsxFragment && { jsxFragment }),
|
|
125
|
+
...(tsconfig && { tsconfig }),
|
|
59
126
|
watch,
|
|
60
127
|
});
|
|
61
128
|
|
package/src/cfetch/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { getCloudflareAPIBaseURL as getCloudflareApiBaseUrl } from "./internal";
|
|
|
10
10
|
export interface FetchError {
|
|
11
11
|
code: number;
|
|
12
12
|
message: string;
|
|
13
|
+
error_chain?: FetchError[];
|
|
13
14
|
}
|
|
14
15
|
export interface FetchResult<ResponseType = unknown> {
|
|
15
16
|
success: boolean;
|
|
@@ -27,12 +28,14 @@ export { fetchKVGetValue } from "./internal";
|
|
|
27
28
|
export async function fetchResult<ResponseType>(
|
|
28
29
|
resource: string,
|
|
29
30
|
init: RequestInit = {},
|
|
30
|
-
queryParams?: URLSearchParams
|
|
31
|
+
queryParams?: URLSearchParams,
|
|
32
|
+
abortSignal?: AbortSignal
|
|
31
33
|
): Promise<ResponseType> {
|
|
32
34
|
const json = await fetchInternal<FetchResult<ResponseType>>(
|
|
33
35
|
resource,
|
|
34
36
|
init,
|
|
35
|
-
queryParams
|
|
37
|
+
queryParams,
|
|
38
|
+
abortSignal
|
|
36
39
|
);
|
|
37
40
|
if (json.success) {
|
|
38
41
|
return json.result;
|
|
@@ -83,9 +86,9 @@ function throwFetchError(
|
|
|
83
86
|
response: FetchResult<unknown>
|
|
84
87
|
): never {
|
|
85
88
|
const error = new ParseError({
|
|
86
|
-
text:
|
|
89
|
+
text: `A request to the Cloudflare API (${resource}) failed.`,
|
|
87
90
|
notes: response.errors.map((err) => ({
|
|
88
|
-
text: err
|
|
91
|
+
text: renderError(err),
|
|
89
92
|
})),
|
|
90
93
|
});
|
|
91
94
|
// add the first error code directly to this error
|
|
@@ -102,3 +105,17 @@ function hasCursor(result_info: unknown): result_info is { cursor: string } {
|
|
|
102
105
|
const cursor = (result_info as { cursor: string } | undefined)?.cursor;
|
|
103
106
|
return cursor !== undefined && cursor !== null && cursor !== "";
|
|
104
107
|
}
|
|
108
|
+
|
|
109
|
+
function renderError(err: FetchError, level = 0): string {
|
|
110
|
+
const chainedMessages =
|
|
111
|
+
err.error_chain
|
|
112
|
+
?.map(
|
|
113
|
+
(chainedError) =>
|
|
114
|
+
`\n${" ".repeat(level)}- ${renderError(chainedError, level + 1)}`
|
|
115
|
+
)
|
|
116
|
+
.join("\n") ?? "";
|
|
117
|
+
return (
|
|
118
|
+
(err.code ? `${err.message} [code: ${err.code}]` : err.message) +
|
|
119
|
+
chainedMessages
|
|
120
|
+
);
|
|
121
|
+
}
|