wrangler 0.0.13 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +20 -11
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +112 -28
  5. package/pages/functions/filepath-routing.ts +44 -51
  6. package/pages/functions/routes.ts +11 -18
  7. package/pages/functions/template-worker.ts +3 -9
  8. package/src/__tests__/dev.test.tsx +42 -5
  9. package/src/__tests__/guess-worker-format.test.ts +66 -0
  10. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  11. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  12. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  13. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  14. package/src/__tests__/helpers/mock-bin.ts +36 -0
  15. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +43 -9
  16. package/src/__tests__/helpers/mock-console.ts +62 -0
  17. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  18. package/src/__tests__/helpers/mock-kv.ts +40 -0
  19. package/src/__tests__/helpers/mock-user.ts +27 -0
  20. package/src/__tests__/helpers/mock-web-socket.ts +37 -0
  21. package/src/__tests__/{run-in-tmp.ts → helpers/run-in-tmp.ts} +1 -1
  22. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  23. package/src/__tests__/helpers/write-wrangler-toml.ts +20 -0
  24. package/src/__tests__/index.test.ts +418 -71
  25. package/src/__tests__/jest.setup.ts +30 -2
  26. package/src/__tests__/kv.test.ts +147 -252
  27. package/src/__tests__/logout.test.ts +50 -0
  28. package/src/__tests__/package-manager.test.ts +206 -0
  29. package/src/__tests__/publish.test.ts +1136 -291
  30. package/src/__tests__/r2.test.ts +206 -0
  31. package/src/__tests__/secret.test.ts +210 -0
  32. package/src/__tests__/sentry.test.ts +146 -0
  33. package/src/__tests__/tail.test.ts +246 -0
  34. package/src/__tests__/whoami.test.tsx +6 -47
  35. package/src/api/form_data.ts +75 -25
  36. package/src/api/preview.ts +2 -2
  37. package/src/api/worker.ts +34 -15
  38. package/src/bundle.ts +127 -0
  39. package/src/cfetch/index.ts +7 -15
  40. package/src/cfetch/internal.ts +41 -6
  41. package/src/cli.ts +10 -0
  42. package/src/config.ts +125 -95
  43. package/src/dev.tsx +300 -193
  44. package/src/dialogs.tsx +2 -2
  45. package/src/guess-worker-format.ts +68 -0
  46. package/src/index.tsx +578 -192
  47. package/src/inspect.ts +29 -10
  48. package/src/kv.tsx +23 -17
  49. package/src/module-collection.ts +32 -12
  50. package/src/open-in-browser.ts +13 -0
  51. package/src/package-manager.ts +120 -0
  52. package/src/pages.tsx +28 -23
  53. package/src/paths.ts +26 -0
  54. package/src/proxy.ts +88 -14
  55. package/src/publish.ts +260 -297
  56. package/src/r2.ts +50 -0
  57. package/src/reporting.ts +115 -0
  58. package/src/sites.tsx +28 -27
  59. package/src/tail.tsx +178 -9
  60. package/src/user.tsx +58 -44
  61. package/templates/new-worker.js +15 -0
  62. package/templates/new-worker.ts +15 -0
  63. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  64. package/wrangler-dist/cli.js +124315 -104677
  65. package/wrangler-dist/cli.js.map +3 -3
  66. package/src/__tests__/mock-console.ts +0 -34
  67. package/src/__tests__/run-wrangler.ts +0 -8
@@ -0,0 +1,66 @@
1
+ import { writeFile } from "fs/promises";
2
+ import path from "path";
3
+ import guessWorkerFormat from "../guess-worker-format";
4
+ import { runInTempDir } from "./helpers/run-in-tmp";
5
+
6
+ describe("guess worker format", () => {
7
+ runInTempDir();
8
+ it('should detect a "modules" worker', async () => {
9
+ await writeFile("./index.ts", "export default {};");
10
+ // Note that this isn't actually a valid worker, because it's missing
11
+ // a fetch handler. Regardless, our heuristic is simply to check for exports.
12
+ const guess = await guessWorkerFormat(
13
+ {
14
+ file: path.join(process.cwd(), "./index.ts"),
15
+ directory: process.cwd(),
16
+ },
17
+ undefined
18
+ );
19
+ expect(guess).toBe("modules");
20
+ });
21
+
22
+ it('should detect a "service-worker" worker', async () => {
23
+ await writeFile("./index.ts", "");
24
+ // Note that this isn't actually a valid worker, because it's missing
25
+ // a fetch listener. Regardless, our heuristic is simply to check for
26
+ // the lack of exports.
27
+ const guess = await guessWorkerFormat(
28
+ {
29
+ file: path.join(process.cwd(), "./index.ts"),
30
+ directory: process.cwd(),
31
+ },
32
+ undefined
33
+ );
34
+ expect(guess).toBe("service-worker");
35
+ });
36
+
37
+ it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
38
+ await writeFile("./index.ts", "export default {};");
39
+ await expect(
40
+ guessWorkerFormat(
41
+ {
42
+ file: path.join(process.cwd(), "./index.ts"),
43
+ directory: process.cwd(),
44
+ },
45
+ "service-worker"
46
+ )
47
+ ).rejects.toThrow(
48
+ "You configured this worker to be a 'service-worker', but the file you are trying to build appears to have ES module exports. Please pass `--format modules`, or simply remove the configuration."
49
+ );
50
+ });
51
+
52
+ it("should throw an error when the hint doesn't match the guess (service-worker - modules)", async () => {
53
+ await writeFile("./index.ts", "");
54
+ await expect(
55
+ guessWorkerFormat(
56
+ {
57
+ file: path.join(process.cwd(), "./index.ts"),
58
+ directory: process.cwd(),
59
+ },
60
+ "modules"
61
+ )
62
+ ).rejects.toThrow(
63
+ "You configured this worker to be 'modules', but the file you are trying to build doesn't export a handler. Please pass `--format service-worker`, or simply remove the configuration."
64
+ );
65
+ });
66
+ });
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The typings file available at `@types/cmd-shim` are out of date.
3
+ */
4
+ module "cmd-shim" {
5
+ /**
6
+ *
7
+ * Create a cmd shim at `to` for the command line program at `from`.
8
+ *
9
+ */
10
+ export default function cmdShim(from: string, to: string): Promise<void>;
11
+ }
@@ -0,0 +1,6 @@
1
+ module "faye-websocket" {
2
+ /**
3
+ * Standards-compliant WebSocket client and server.
4
+ */
5
+ export default WebSocket;
6
+ }
@@ -0,0 +1,30 @@
1
+ const ORIGINAL_CF_API_TOKEN = process.env.CF_API_TOKEN;
2
+ const ORIGINAL_CF_ACCOUNT_ID = process.env.CF_ACCOUNT_ID;
3
+
4
+ /**
5
+ * Mock the API token so that we don't need to read it from user configuration files.
6
+ */
7
+ export function mockApiToken({
8
+ apiToken = "some-api-token",
9
+ }: { apiToken?: string } = {}) {
10
+ beforeEach(() => {
11
+ process.env.CF_API_TOKEN = apiToken;
12
+ });
13
+ afterEach(() => {
14
+ process.env.CF_API_TOKEN = ORIGINAL_CF_API_TOKEN;
15
+ });
16
+ }
17
+
18
+ /**
19
+ * Mock the current account ID so that we don't need to read it from configuration files.
20
+ */
21
+ export function mockAccountId({
22
+ accountId = "some-account-id",
23
+ }: { accountId?: string } = {}) {
24
+ beforeEach(() => {
25
+ process.env.CF_ACCOUNT_ID = accountId;
26
+ });
27
+ afterEach(() => {
28
+ process.env.CF_ACCOUNT_ID = ORIGINAL_CF_ACCOUNT_ID;
29
+ });
30
+ }
@@ -0,0 +1,36 @@
1
+ import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
+ import { basename, resolve } from "node:path";
3
+ import cmdShim from "cmd-shim";
4
+
5
+ const nodeShebang = "#!/usr/bin/env node";
6
+
7
+ /**
8
+ * Create a binary file in a temp directory and make it available on the PATH.
9
+ */
10
+ export async function mockBinary(
11
+ binaryName: string,
12
+ code: string
13
+ ): Promise<() => void> {
14
+ // Ensure there is a directory to put the mock binary in.
15
+ const tmpDir = resolve(mkdtempSync(".mock-binary-"));
16
+
17
+ // Use a fake extension on Windows because we will create a cmd-shim to run the binary.
18
+ const extension = process.platform === "win32" ? ".x-mock-bin" : "";
19
+ const filePath = resolve(tmpDir, `${binaryName}${extension}`);
20
+ writeFileSync(filePath, nodeShebang + "\n" + code);
21
+ chmodSync(filePath, 0o777);
22
+
23
+ if (process.platform === "win32") {
24
+ await cmdShim(filePath, basename(filePath, ".x-mock-bin"));
25
+ }
26
+
27
+ // Update PATH using the appropriate separator for the platform.
28
+ const oldPath = process.env.PATH;
29
+ const sep = process.platform === "win32" ? ";" : ":";
30
+ process.env.PATH = tmpDir + sep + oldPath;
31
+
32
+ return function unMock() {
33
+ rmSync(tmpDir, { recursive: true });
34
+ process.env.PATH = process.env.PATH?.replace(tmpDir + sep, "");
35
+ };
36
+ }
@@ -1,8 +1,8 @@
1
- import type { RequestInit } from "node-fetch";
2
- import { URLSearchParams } from "node:url";
1
+ import { URL, URLSearchParams } from "node:url";
3
2
  import { pathToRegexp } from "path-to-regexp";
4
- import { CF_API_BASE_URL } from "../cfetch";
5
- import type { FetchResult } from "../cfetch";
3
+ import { CF_API_BASE_URL } from "../../cfetch";
4
+ import type { FetchResult } from "../../cfetch";
5
+ import type { RequestInit } from "undici";
6
6
 
7
7
  /**
8
8
  * The signature of the function that will handle a mock request.
@@ -140,23 +140,23 @@ export function setMockResponse<ResponseType>(
140
140
  /**
141
141
  * A helper to make it easier to create `FetchResult` objects in tests.
142
142
  */
143
- export function createFetchResult<ResponseType>(
144
- result: ResponseType,
143
+ export async function createFetchResult<ResponseType>(
144
+ result: ResponseType | Promise<ResponseType>,
145
145
  success = true,
146
146
  errors = [],
147
147
  messages = [],
148
148
  result_info?: unknown
149
- ): FetchResult<ResponseType> {
149
+ ): Promise<FetchResult<ResponseType>> {
150
150
  return result_info
151
151
  ? {
152
- result,
152
+ result: await result,
153
153
  success,
154
154
  errors,
155
155
  messages,
156
156
  result_info,
157
157
  }
158
158
  : {
159
- result,
159
+ result: await result,
160
160
  success,
161
161
  errors,
162
162
  messages,
@@ -171,3 +171,37 @@ export function createFetchResult<ResponseType>(
171
171
  export function unsetAllMocks() {
172
172
  mocks.length = 0;
173
173
  }
174
+
175
+ /**
176
+ * We special-case fetching the request for `kv:key get`, because it's
177
+ * the only cloudflare API endpoint that returns a plain string as the
178
+ * value, and not as the "standard" FetchResult-style json. Hence, we also
179
+ * special-case mocking it here.
180
+ */
181
+
182
+ const kvGetMocks = new Map<string, string>();
183
+
184
+ export function mockFetchKVGetValue(
185
+ accountId: string,
186
+ namespaceId: string,
187
+ key: string
188
+ ) {
189
+ const mapKey = `${accountId}/${namespaceId}/${key}`;
190
+ if (kvGetMocks.has(mapKey)) {
191
+ return kvGetMocks.get(mapKey);
192
+ }
193
+ throw new Error(`no mock value found for \`kv:key get\` - ${mapKey}`);
194
+ }
195
+
196
+ export function setMockFetchKVGetValue(
197
+ accountId: string,
198
+ namespaceId: string,
199
+ key: string,
200
+ value: string
201
+ ) {
202
+ kvGetMocks.set(`${accountId}/${namespaceId}/${key}`, value);
203
+ }
204
+
205
+ export function unsetMockFetchKVGetValues() {
206
+ kvGetMocks.clear();
207
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * We use this module to mock console methods, and optionally
3
+ * assert on the values they're called with in our tests.
4
+ */
5
+
6
+ let logSpy: jest.SpyInstance,
7
+ errorSpy: jest.SpyInstance,
8
+ warnSpy: jest.SpyInstance;
9
+
10
+ const std = {
11
+ get out() {
12
+ return stripTrailingWhitespace(
13
+ normalizeSlashes(stripTimings(logSpy.mock.calls.flat(2).join("\n")))
14
+ );
15
+ },
16
+ get err() {
17
+ return stripTrailingWhitespace(
18
+ normalizeSlashes(stripTimings(errorSpy.mock.calls.flat(2).join("\n")))
19
+ );
20
+ },
21
+ get warn() {
22
+ return stripTrailingWhitespace(
23
+ normalizeSlashes(stripTimings(warnSpy.mock.calls.flat(2).join("\n")))
24
+ );
25
+ },
26
+ };
27
+
28
+ export function mockConsoleMethods() {
29
+ beforeEach(() => {
30
+ logSpy = jest.spyOn(console, "log").mockImplementation();
31
+ errorSpy = jest.spyOn(console, "error").mockImplementation();
32
+ warnSpy = jest.spyOn(console, "warn").mockImplementation();
33
+ });
34
+ afterEach(() => {
35
+ logSpy.mockRestore();
36
+ errorSpy.mockRestore();
37
+ warnSpy.mockRestore();
38
+ });
39
+ return std;
40
+ }
41
+
42
+ /**
43
+ * Ensure slashes in the `str` are OS file-system agnostic.
44
+ *
45
+ * Use this in snapshot tests to be resilient to file-system differences.
46
+ */
47
+ export function normalizeSlashes(str: string): string {
48
+ return str.replace(/\\/g, "/");
49
+ }
50
+
51
+ /**
52
+ * Strip "timing data" out of the `stdout` string, since this is not always deterministic.
53
+ *
54
+ * Use this in snapshot tests to be resilient to slight changes in timing of processing.
55
+ */
56
+ export function stripTimings(stdout: string): string {
57
+ return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
58
+ }
59
+
60
+ export function stripTrailingWhitespace(str: string): string {
61
+ return str.replace(/[^\S\n]+\n/g, "\n");
62
+ }
@@ -1,4 +1,4 @@
1
- import { confirm, prompt } from "../dialogs";
1
+ import { confirm, prompt } from "../../dialogs";
2
2
 
3
3
  /**
4
4
  * The expected values for a confirmation request.
@@ -0,0 +1,40 @@
1
+ import { createFetchResult, setMockRawResponse } from "./mock-cfetch";
2
+
3
+ export function mockKeyListRequest(
4
+ expectedNamespaceId: string,
5
+ expectedKeys: string[],
6
+ keysPerRequest = 1000,
7
+ blankCursorValue: "" | undefined | null = undefined
8
+ ) {
9
+ const requests = { count: 0 };
10
+ // See https://api.cloudflare.com/#workers-kv-namespace-list-a-namespace-s-keys
11
+ const expectedKeyObjects = expectedKeys.map((name) => ({
12
+ name,
13
+ expiration: 123456789,
14
+ metadata: {},
15
+ }));
16
+ setMockRawResponse(
17
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/keys",
18
+ "GET",
19
+ ([_url, accountId, namespaceId], _init, query) => {
20
+ requests.count++;
21
+ expect(accountId).toEqual("some-account-id");
22
+ expect(namespaceId).toEqual(expectedNamespaceId);
23
+ if (expectedKeyObjects.length <= keysPerRequest) {
24
+ return createFetchResult(expectedKeyObjects);
25
+ } else {
26
+ const start = parseInt(query.get("cursor") ?? "0") || 0;
27
+ const end = start + keysPerRequest;
28
+ const cursor = end < expectedKeyObjects.length ? end : blankCursorValue;
29
+ return createFetchResult(
30
+ expectedKeyObjects.slice(start, end),
31
+ true,
32
+ [],
33
+ [],
34
+ { cursor }
35
+ );
36
+ }
37
+ }
38
+ );
39
+ return requests;
40
+ }
@@ -0,0 +1,27 @@
1
+ import { mkdirSync, writeFileSync } from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+
5
+ export function writeUserConfig(
6
+ oauth_token?: string,
7
+ refresh_token?: string,
8
+ expiration_time?: string
9
+ ) {
10
+ const lines: string[] = [];
11
+ if (oauth_token) {
12
+ lines.push(`oauth_token = "${oauth_token}"`);
13
+ }
14
+ if (refresh_token) {
15
+ lines.push(`refresh_token = "${refresh_token}"`);
16
+ }
17
+ if (expiration_time) {
18
+ lines.push(`expiration_time = "${expiration_time}"`);
19
+ }
20
+ const configPath = path.join(os.homedir(), ".wrangler/config");
21
+ mkdirSync(configPath, { recursive: true });
22
+ writeFileSync(
23
+ path.join(configPath, "default.toml"),
24
+ lines.join("\n"),
25
+ "utf-8"
26
+ );
27
+ }
@@ -0,0 +1,37 @@
1
+ import { default as StockWS } from "jest-websocket-mock";
2
+ import { WebSocket } from "mock-socket";
3
+
4
+ /**
5
+ * A version of the mock WebSocket that supports the methods that we use.
6
+ */
7
+ export class MockWebSocket extends WebSocket {
8
+ on(event: string | symbol, listener: (...args: unknown[]) => void): this {
9
+ switch (event) {
10
+ case "message":
11
+ this.onmessage = ({ data }) => {
12
+ listener(data);
13
+ };
14
+ break;
15
+ case "open":
16
+ this.onopen = listener;
17
+ break;
18
+ case "close":
19
+ this.onclose = listener;
20
+ break;
21
+ default:
22
+ throw new Error("Unknown event type: " + event.toString());
23
+ }
24
+ return this;
25
+ }
26
+
27
+ terminate() {
28
+ this.close();
29
+ }
30
+ }
31
+
32
+ export class WS extends StockWS {
33
+ async nextMessageJson() {
34
+ const message = await this.nextMessage;
35
+ return JSON.parse(message as string);
36
+ }
37
+ }
@@ -1,6 +1,6 @@
1
+ import * as fs from "node:fs";
1
2
  import os from "node:os";
2
3
  import * as path from "node:path";
3
- import * as fs from "fs";
4
4
 
5
5
  const originalCwd = process.cwd();
6
6
 
@@ -0,0 +1,16 @@
1
+ import { main } from "../../index";
2
+ import { normalizeSlashes, stripTimings } from "./mock-console";
3
+
4
+ /**
5
+ * A helper to 'run' wrangler commands for tests.
6
+ */
7
+ export async function runWrangler(cmd?: string) {
8
+ try {
9
+ await main(cmd?.split(" ") ?? []);
10
+ } catch (err) {
11
+ if (err instanceof Error) {
12
+ err.message = normalizeSlashes(stripTimings(err.message));
13
+ }
14
+ throw err;
15
+ }
16
+ }
@@ -0,0 +1,20 @@
1
+ import * as fs from "fs";
2
+ import TOML from "@iarna/toml";
3
+ import type { Config } from "../../config";
4
+
5
+ /** Write a mock wrangler.toml file to disk. */
6
+ export default function writeWranglerToml(config: Omit<Config, "env"> = {}) {
7
+ // We Omit `env` from config because TOML.stringify() appears to
8
+ // have a weird type signature that appears to fail. We'll revisit this
9
+ // when we write tests for publishing environments
10
+ fs.writeFileSync(
11
+ "./wrangler.toml",
12
+ TOML.stringify({
13
+ compatibility_date: "2022-01-12",
14
+ name: "test-name",
15
+ ...(config as TOML.JsonMap),
16
+ }),
17
+
18
+ "utf-8"
19
+ );
20
+ }