wrangler 0.0.0-e151223 → 0.0.0-e273e09
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 +54 -28
- package/pages/functions/buildPlugin.ts +105 -0
- package/pages/functions/buildWorker.ts +93 -4
- package/pages/functions/filepath-routing.test.ts +223 -36
- package/pages/functions/filepath-routing.ts +78 -120
- package/pages/functions/routes.ts +13 -17
- package/pages/functions/template-plugin.ts +147 -0
- package/pages/functions/template-worker.ts +24 -17
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +3487 -0
- package/src/__tests__/dev.test.tsx +977 -65
- package/src/__tests__/guess-worker-format.test.ts +85 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +26 -8
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/helpers/mock-cfetch.ts +48 -10
- package/src/__tests__/helpers/mock-console.ts +39 -8
- package/src/__tests__/helpers/mock-dialogs.ts +67 -2
- 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/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +10 -0
- package/src/__tests__/helpers/run-wrangler.ts +5 -3
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +130 -0
- package/src/__tests__/index.test.ts +160 -309
- package/src/__tests__/init.test.ts +1637 -0
- package/src/__tests__/jest.setup.ts +49 -6
- package/src/__tests__/kv.test.ts +607 -275
- package/src/__tests__/logger.test.ts +115 -0
- package/src/__tests__/package-manager.test.ts +353 -0
- package/src/__tests__/pages.test.ts +754 -0
- package/src/__tests__/parse.test.ts +330 -0
- package/src/__tests__/publish.test.ts +5472 -939
- package/src/__tests__/r2.test.ts +233 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +362 -91
- package/src/__tests__/tail.test.ts +690 -0
- package/src/__tests__/user.test.ts +131 -0
- package/src/__tests__/whoami.test.tsx +60 -7
- package/src/abort.d.ts +3 -0
- package/src/bundle.ts +197 -0
- package/src/cfetch/index.ts +28 -18
- package/src/cfetch/internal.ts +99 -29
- package/src/cli.ts +6 -2
- package/src/config/README.md +107 -0
- package/src/config/config.ts +234 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +420 -0
- package/src/config/index.ts +225 -0
- package/src/config/validation-helpers.ts +584 -0
- package/src/config/validation.ts +1548 -0
- package/src/config-cache.ts +45 -0
- package/src/create-worker-preview.ts +185 -0
- package/src/create-worker-upload-form.ts +254 -0
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +401 -0
- package/src/dev/local.tsx +326 -0
- package/src/dev/remote.tsx +237 -0
- package/src/dev/use-esbuild.ts +118 -0
- package/src/dialogs.tsx +64 -6
- package/src/durable.ts +102 -0
- package/src/entry.ts +322 -0
- package/src/environment-variables.ts +35 -0
- package/src/errors.ts +11 -0
- package/src/https-options.ts +123 -0
- package/src/index.tsx +1768 -982
- package/src/inspect.ts +80 -14
- package/src/intl-polyfill.d.ts +139 -0
- package/src/{kv.tsx → kv.ts} +199 -67
- 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 +226 -31
- package/src/open-in-browser.ts +19 -0
- package/src/package-manager.ts +174 -0
- package/src/pages.tsx +1132 -84
- package/src/parse.ts +203 -0
- package/src/proxy.ts +170 -62
- package/src/publish.ts +617 -306
- package/src/r2.ts +50 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +143 -57
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +67 -0
- package/src/update-check.ts +19 -0
- package/src/user.tsx +341 -146
- package/src/whoami.tsx +5 -3
- package/src/{api/worker.ts → worker.ts} +65 -39
- package/src/zones.ts +73 -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 +15 -0
- package/templates/new-worker.ts +30 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -5
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +77223 -75763
- package/src/__tests__/helpers/mock-user.ts +0 -27
- package/src/__tests__/logout.test.ts +0 -50
- package/src/api/form_data.ts +0 -134
- package/src/api/preview.ts +0 -128
- package/src/config.ts +0 -641
- package/src/dev.tsx +0 -834
- package/src/npm-installer.ts +0 -20
- package/src/tail.tsx +0 -73
- 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
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { writeFile } from "fs/promises";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import guessWorkerFormat from "../entry";
|
|
4
|
+
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
5
|
+
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
6
|
+
|
|
7
|
+
describe("guess worker format", () => {
|
|
8
|
+
runInTempDir();
|
|
9
|
+
const std = mockConsoleMethods();
|
|
10
|
+
it('should detect a "modules" worker', async () => {
|
|
11
|
+
await writeFile("./index.ts", "export default {};");
|
|
12
|
+
// Note that this isn't actually a valid worker, because it's missing
|
|
13
|
+
// a fetch handler. Regardless, our heuristic is simply to check for exports.
|
|
14
|
+
const guess = await guessWorkerFormat(
|
|
15
|
+
path.join(process.cwd(), "./index.ts"),
|
|
16
|
+
process.cwd(),
|
|
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
|
+
path.join(process.cwd(), "./index.ts"),
|
|
29
|
+
process.cwd(),
|
|
30
|
+
undefined
|
|
31
|
+
);
|
|
32
|
+
expect(guess).toBe("service-worker");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
|
|
36
|
+
await writeFile("./index.ts", "export default {};");
|
|
37
|
+
await expect(
|
|
38
|
+
guessWorkerFormat(
|
|
39
|
+
path.join(process.cwd(), "./index.ts"),
|
|
40
|
+
process.cwd(),
|
|
41
|
+
"service-worker"
|
|
42
|
+
)
|
|
43
|
+
).rejects.toThrow(
|
|
44
|
+
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have a `default` export like a module worker. Please pass `--format modules`, or simply remove the configuration."
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should throw an error when the hint doesn't match the guess (service-worker - modules)", async () => {
|
|
49
|
+
await writeFile("./index.ts", "");
|
|
50
|
+
await expect(
|
|
51
|
+
guessWorkerFormat(
|
|
52
|
+
path.join(process.cwd(), "./index.ts"),
|
|
53
|
+
process.cwd(),
|
|
54
|
+
"modules"
|
|
55
|
+
)
|
|
56
|
+
).rejects.toThrow(
|
|
57
|
+
"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."
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should not error if a .js entry point has jsx", async () => {
|
|
62
|
+
await writeFile("./index.js", "console.log(<div/>)");
|
|
63
|
+
const guess = await guessWorkerFormat(
|
|
64
|
+
path.join(process.cwd(), "./index.js"),
|
|
65
|
+
process.cwd(),
|
|
66
|
+
undefined
|
|
67
|
+
);
|
|
68
|
+
expect(guess).toBe("service-worker");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("logs a warning when a worker has exports, but not a default one", async () => {
|
|
72
|
+
await writeFile("./index.ts", "export const foo = 1;");
|
|
73
|
+
const guess = await guessWorkerFormat(
|
|
74
|
+
path.join(process.cwd(), "./index.ts"),
|
|
75
|
+
process.cwd(),
|
|
76
|
+
undefined
|
|
77
|
+
);
|
|
78
|
+
expect(guess).toBe("service-worker");
|
|
79
|
+
expect(std.warn).toMatchInlineSnapshot(`
|
|
80
|
+
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mThe entrypoint index.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using \\"service-worker\\" format...[0m
|
|
81
|
+
|
|
82
|
+
"
|
|
83
|
+
`);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -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
|
+
}
|
|
@@ -1,30 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { reinitialiseAuthTokens } from "../../user";
|
|
2
|
+
|
|
3
|
+
const ORIGINAL_CLOUDFLARE_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
|
|
4
|
+
const ORIGINAL_CLOUDFLARE_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Mock the API token so that we don't need to read it from user configuration files.
|
|
8
|
+
*
|
|
9
|
+
* Note that you can remove any API token from the environment by setting the value to `null`.
|
|
10
|
+
* This is useful if a higher `describe()` block has already called `mockApiToken()`.
|
|
6
11
|
*/
|
|
7
12
|
export function mockApiToken({
|
|
8
13
|
apiToken = "some-api-token",
|
|
9
|
-
}: { apiToken?: string } = {}) {
|
|
14
|
+
}: { apiToken?: string | null } = {}) {
|
|
10
15
|
beforeEach(() => {
|
|
11
|
-
|
|
16
|
+
if (apiToken === null) {
|
|
17
|
+
delete process.env.CLOUDFLARE_API_TOKEN;
|
|
18
|
+
} else {
|
|
19
|
+
process.env.CLOUDFLARE_API_TOKEN = apiToken;
|
|
20
|
+
}
|
|
21
|
+
// Now we have updated the environment, we must reinitialize the user auth state.
|
|
22
|
+
reinitialiseAuthTokens();
|
|
12
23
|
});
|
|
13
24
|
afterEach(() => {
|
|
14
|
-
process.env.
|
|
25
|
+
process.env.CLOUDFLARE_API_TOKEN = ORIGINAL_CLOUDFLARE_API_TOKEN;
|
|
15
26
|
});
|
|
16
27
|
}
|
|
17
28
|
|
|
18
29
|
/**
|
|
19
30
|
* Mock the current account ID so that we don't need to read it from configuration files.
|
|
31
|
+
*
|
|
32
|
+
* Note that you can remove any account ID from the environment by setting the value to `null`.
|
|
33
|
+
* This is useful if a higher `describe()` block has already called `mockAccountId()`.
|
|
20
34
|
*/
|
|
21
35
|
export function mockAccountId({
|
|
22
36
|
accountId = "some-account-id",
|
|
23
|
-
}: { accountId?: string } = {}) {
|
|
37
|
+
}: { accountId?: string | null } = {}) {
|
|
24
38
|
beforeEach(() => {
|
|
25
|
-
|
|
39
|
+
if (accountId === null) {
|
|
40
|
+
delete process.env.CLOUDFLARE_ACCOUNT_ID;
|
|
41
|
+
} else {
|
|
42
|
+
process.env.CLOUDFLARE_ACCOUNT_ID = accountId;
|
|
43
|
+
}
|
|
26
44
|
});
|
|
27
45
|
afterEach(() => {
|
|
28
|
-
process.env.
|
|
46
|
+
process.env.CLOUDFLARE_ACCOUNT_ID = ORIGINAL_CLOUDFLARE_ACCOUNT_ID;
|
|
29
47
|
});
|
|
30
48
|
}
|
|
@@ -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,7 +1,7 @@
|
|
|
1
1
|
import { URL, URLSearchParams } from "node:url";
|
|
2
2
|
import { pathToRegexp } from "path-to-regexp";
|
|
3
|
-
import {
|
|
4
|
-
import type { FetchResult } from "../../cfetch";
|
|
3
|
+
import { getCloudflareApiBaseUrl } from "../../cfetch";
|
|
4
|
+
import type { FetchResult, FetchError } from "../../cfetch";
|
|
5
5
|
import type { RequestInit } from "undici";
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -35,7 +35,7 @@ export async function mockFetchInternal(
|
|
|
35
35
|
queryParams: URLSearchParams = new URLSearchParams()
|
|
36
36
|
) {
|
|
37
37
|
for (const { regexp, method, handler } of mocks) {
|
|
38
|
-
const resourcePath = new URL(resource,
|
|
38
|
+
const resourcePath = new URL(resource, getCloudflareApiBaseUrl()).pathname;
|
|
39
39
|
const uri = regexp.exec(resourcePath);
|
|
40
40
|
// Do the resource path and (if specified) the HTTP method match?
|
|
41
41
|
if (uri !== null && (!method || method === (init.method ?? "GET"))) {
|
|
@@ -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
|
-
errors = [],
|
|
147
|
-
messages = [],
|
|
146
|
+
errors: FetchError[] = [],
|
|
147
|
+
messages: string[] = [],
|
|
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,41 @@ 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
|
+
/**
|
|
185
|
+
* @mocked typeof fetchKVGetValue
|
|
186
|
+
*/
|
|
187
|
+
export function mockFetchKVGetValue(
|
|
188
|
+
accountId: string,
|
|
189
|
+
namespaceId: string,
|
|
190
|
+
key: string
|
|
191
|
+
) {
|
|
192
|
+
const mapKey = `${accountId}/${namespaceId}/${key}`;
|
|
193
|
+
if (kvGetMocks.has(mapKey)) {
|
|
194
|
+
const value = kvGetMocks.get(mapKey);
|
|
195
|
+
if (value !== undefined) return Promise.resolve(value);
|
|
196
|
+
}
|
|
197
|
+
throw new Error(`no mock value found for \`kv:key get\` - ${mapKey}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function setMockFetchKVGetValue(
|
|
201
|
+
accountId: string,
|
|
202
|
+
namespaceId: string,
|
|
203
|
+
key: string,
|
|
204
|
+
value: string
|
|
205
|
+
) {
|
|
206
|
+
kvGetMocks.set(`${accountId}/${namespaceId}/${key}`, value);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function unsetMockFetchKVGetValues() {
|
|
210
|
+
kvGetMocks.clear();
|
|
211
|
+
}
|
|
@@ -1,35 +1,53 @@
|
|
|
1
|
+
import * as util from "node:util";
|
|
2
|
+
import { logger } from "../../logger";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* We use this module to mock console methods, and optionally
|
|
3
6
|
* assert on the values they're called with in our tests.
|
|
4
7
|
*/
|
|
5
8
|
|
|
6
|
-
let
|
|
9
|
+
let debugSpy: jest.SpyInstance,
|
|
10
|
+
logSpy: jest.SpyInstance,
|
|
7
11
|
errorSpy: jest.SpyInstance,
|
|
8
12
|
warnSpy: jest.SpyInstance;
|
|
9
13
|
|
|
10
14
|
const std = {
|
|
15
|
+
get debug() {
|
|
16
|
+
return normalizeOutput(debugSpy);
|
|
17
|
+
},
|
|
11
18
|
get out() {
|
|
12
|
-
return
|
|
19
|
+
return normalizeOutput(logSpy);
|
|
13
20
|
},
|
|
14
21
|
get err() {
|
|
15
|
-
return
|
|
16
|
-
stripTimings(errorSpy.mock.calls.flat(2).join("\n"))
|
|
17
|
-
);
|
|
22
|
+
return normalizeOutput(errorSpy);
|
|
18
23
|
},
|
|
19
24
|
get warn() {
|
|
20
|
-
return
|
|
21
|
-
stripTimings(warnSpy.mock.calls.flat(2).join("\n"))
|
|
22
|
-
);
|
|
25
|
+
return normalizeOutput(warnSpy);
|
|
23
26
|
},
|
|
24
27
|
};
|
|
25
28
|
|
|
29
|
+
function normalizeOutput(spy: jest.SpyInstance): string {
|
|
30
|
+
return normalizeErrorMarkers(
|
|
31
|
+
stripTrailingWhitespace(normalizeSlashes(stripTimings(captureCalls(spy))))
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function captureCalls(spy: jest.SpyInstance): string {
|
|
36
|
+
return spy.mock.calls
|
|
37
|
+
.map((args: unknown[]) => util.format("%s", ...args))
|
|
38
|
+
.join("\n");
|
|
39
|
+
}
|
|
40
|
+
|
|
26
41
|
export function mockConsoleMethods() {
|
|
27
42
|
beforeEach(() => {
|
|
43
|
+
logger.columns = 100;
|
|
44
|
+
debugSpy = jest.spyOn(console, "debug").mockImplementation();
|
|
28
45
|
logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
29
46
|
errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
30
47
|
warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
31
48
|
});
|
|
32
49
|
afterEach(() => {
|
|
50
|
+
debugSpy.mockRestore();
|
|
33
51
|
logSpy.mockRestore();
|
|
34
52
|
errorSpy.mockRestore();
|
|
35
53
|
warnSpy.mockRestore();
|
|
@@ -37,6 +55,15 @@ export function mockConsoleMethods() {
|
|
|
37
55
|
return std;
|
|
38
56
|
}
|
|
39
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Normalize error `X` markers.
|
|
60
|
+
*
|
|
61
|
+
* Windows gets a different character.
|
|
62
|
+
*/
|
|
63
|
+
function normalizeErrorMarkers(str: string): string {
|
|
64
|
+
return str.replaceAll("✘", "X");
|
|
65
|
+
}
|
|
66
|
+
|
|
40
67
|
/**
|
|
41
68
|
* Ensure slashes in the `str` are OS file-system agnostic.
|
|
42
69
|
*
|
|
@@ -54,3 +81,7 @@ export function normalizeSlashes(str: string): string {
|
|
|
54
81
|
export function stripTimings(stdout: string): string {
|
|
55
82
|
return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
|
|
56
83
|
}
|
|
84
|
+
|
|
85
|
+
export function stripTrailingWhitespace(str: string): string {
|
|
86
|
+
return str.replace(/[^\S\n]+\n/g, "\n");
|
|
87
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { confirm, prompt } from "../../dialogs";
|
|
1
|
+
import { confirm, prompt, select } from "../../dialogs";
|
|
2
|
+
import { normalizeSlashes } from "./mock-console";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* The expected values for a confirmation request.
|
|
@@ -20,7 +21,7 @@ export interface ConfirmExpectation {
|
|
|
20
21
|
export function mockConfirm(...expectations: ConfirmExpectation[]) {
|
|
21
22
|
(confirm as jest.Mock).mockImplementation((text: string) => {
|
|
22
23
|
for (const { text: expectedText, result } of expectations) {
|
|
23
|
-
if (text === expectedText) {
|
|
24
|
+
if (normalizeSlashes(text) === normalizeSlashes(expectedText)) {
|
|
24
25
|
return Promise.resolve(result);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
@@ -28,6 +29,18 @@ export function mockConfirm(...expectations: ConfirmExpectation[]) {
|
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
31
|
|
|
32
|
+
export function clearConfirmMocks() {
|
|
33
|
+
(confirm as jest.Mock).mockReset();
|
|
34
|
+
// Because confirm was originally a spy, calling mockReset will simply reset
|
|
35
|
+
// it as a function with no return value (!), so we need to accitionally reset
|
|
36
|
+
// the mock implementation to the one that throws (from jest.setup.js).
|
|
37
|
+
(confirm as jest.Mock).mockImplementation((text: string) => {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Unexpected call to \`confirm("${text}")\`.\nYou should use \`mockConfirm()\` to mock calls to \`confirm()\` with expectations. Search the codebase for \`mockConfirm\` to learn more.`
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
31
44
|
/**
|
|
32
45
|
* The expected values for a prompt request.
|
|
33
46
|
*/
|
|
@@ -63,3 +76,55 @@ export function mockPrompt(...expectations: PromptExpectation[]) {
|
|
|
63
76
|
}
|
|
64
77
|
);
|
|
65
78
|
}
|
|
79
|
+
|
|
80
|
+
export function clearPromptMocks() {
|
|
81
|
+
(prompt as jest.Mock).mockReset();
|
|
82
|
+
// Because prompt was originally a spy, calling mockReset will simply reset
|
|
83
|
+
// it as a function with no return value (!), so we need to accitionally reset
|
|
84
|
+
// the mock implementation to the one that throws (from jest.setup.js).
|
|
85
|
+
(prompt as jest.Mock).mockImplementation((text: string) => {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`Unexpected call to \`prompt(${text}, ...)\`.\nYou should use \`mockPrompt()\` to mock calls to \`prompt()\` with expectations. Search the codebase for \`mockPrompt\` to learn more.`
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The expected values for a select request.
|
|
94
|
+
*/
|
|
95
|
+
export interface SelectExpectation {
|
|
96
|
+
/** The text expected to be seen in the select dialog. */
|
|
97
|
+
text: string;
|
|
98
|
+
/** The mock response send back from the select dialog. */
|
|
99
|
+
result: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Mock the implementation of `select()` that will respond with configured results
|
|
104
|
+
* for configured select text messages.
|
|
105
|
+
*
|
|
106
|
+
* If there is a call to `select()` that does not match any of the expectations
|
|
107
|
+
* then an error is thrown.
|
|
108
|
+
*/
|
|
109
|
+
export function mockSelect(...expectations: SelectExpectation[]) {
|
|
110
|
+
(select as jest.Mock).mockImplementation((text: string) => {
|
|
111
|
+
for (const { text: expectedText, result } of expectations) {
|
|
112
|
+
if (normalizeSlashes(text) === normalizeSlashes(expectedText)) {
|
|
113
|
+
return Promise.resolve(result);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
throw new Error(`Unexpected select message: ${text}`);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function clearSelectMocks() {
|
|
121
|
+
(select as jest.Mock).mockReset();
|
|
122
|
+
// Because select was originally a spy, calling mockReset will simply reset
|
|
123
|
+
// it as a function with no return value (!), so we need to additionally reset
|
|
124
|
+
// the mock implementation to the one that throws (from jest.setup.js).
|
|
125
|
+
(select as jest.Mock).mockImplementation((text: string) => {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Unexpected call to \`select("${text}")\`.\nYou should use \`mockSelect()\` to mock calls to \`select()\` with expectations. Search the codebase for \`mockSelect\` to learn more.`
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import type { Request } from "undici";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Setup a mock HTTP server that can be triggered directly without interacting with any network.
|
|
6
|
+
*
|
|
7
|
+
* @returns a `fetch`-like function that will trigger the mock server to handle the request.
|
|
8
|
+
*/
|
|
9
|
+
export function mockHttpServer() {
|
|
10
|
+
let listener: http.RequestListener;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
jest
|
|
14
|
+
.spyOn(http, "createServer")
|
|
15
|
+
.mockImplementation((...args: unknown[]) => {
|
|
16
|
+
listener = args.pop() as http.RequestListener;
|
|
17
|
+
return {
|
|
18
|
+
listen: jest.fn(),
|
|
19
|
+
close(callback?: (err?: Error) => void) {
|
|
20
|
+
callback?.();
|
|
21
|
+
return this;
|
|
22
|
+
},
|
|
23
|
+
} as unknown as http.Server;
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return async (req: Request) => {
|
|
28
|
+
const resp = new http.ServerResponse(
|
|
29
|
+
// If you squint you can just about see that an `IncomingMessages` is like a `Request`!
|
|
30
|
+
req as unknown as http.IncomingMessage
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// The listener will attache a callback to the response by calling `resp.end(callback)`.
|
|
34
|
+
// We want to capture that so that we can trigger it after the listener has completed its work.
|
|
35
|
+
const endSpy = jest.spyOn(resp, "end");
|
|
36
|
+
|
|
37
|
+
// The `await` here is important to allow the listener to complete its async work before we end the response.
|
|
38
|
+
await listener(req as unknown as http.IncomingMessage, resp);
|
|
39
|
+
|
|
40
|
+
// Now trigger the end callback.
|
|
41
|
+
const endCallback = endSpy.mock.calls[0].pop();
|
|
42
|
+
endCallback?.();
|
|
43
|
+
|
|
44
|
+
return resp;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const ORIGINAL_STDOUT_ISTTY = process.stdout.isTTY;
|
|
2
|
+
const ORIGINAL_STDIN_ISTTY = process.stdin.isTTY;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Mock `process.stdout.isTTY`
|
|
6
|
+
*/
|
|
7
|
+
export function useMockIsTTY() {
|
|
8
|
+
/**
|
|
9
|
+
* Explicitly set `process.stdout.isTTY` to a given value
|
|
10
|
+
*/
|
|
11
|
+
const setIsTTY = (isTTY: boolean) => {
|
|
12
|
+
process.stdout.isTTY = isTTY;
|
|
13
|
+
process.stdin.isTTY = isTTY;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
process.stdout.isTTY = ORIGINAL_STDOUT_ISTTY;
|
|
18
|
+
process.stdin.isTTY = ORIGINAL_STDIN_ISTTY;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
process.stdout.isTTY = ORIGINAL_STDOUT_ISTTY;
|
|
23
|
+
process.stdin.isTTY = ORIGINAL_STDIN_ISTTY;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return { setIsTTY };
|
|
27
|
+
}
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import { createFetchResult, setMockRawResponse } from "./mock-cfetch";
|
|
2
|
+
import type { NamespaceKeyInfo } from "../../kv";
|
|
2
3
|
|
|
3
4
|
export function mockKeyListRequest(
|
|
4
5
|
expectedNamespaceId: string,
|
|
5
|
-
expectedKeys:
|
|
6
|
+
expectedKeys: NamespaceKeyInfo[],
|
|
6
7
|
keysPerRequest = 1000,
|
|
7
8
|
blankCursorValue: "" | undefined | null = undefined
|
|
8
9
|
) {
|
|
9
10
|
const requests = { count: 0 };
|
|
10
11
|
// See https://api.cloudflare.com/#workers-kv-namespace-list-a-namespace-s-keys
|
|
11
|
-
|
|
12
|
-
name,
|
|
13
|
-
expiration: 123456789,
|
|
14
|
-
metadata: {},
|
|
15
|
-
}));
|
|
12
|
+
|
|
16
13
|
setMockRawResponse(
|
|
17
14
|
"/accounts/:accountId/storage/kv/namespaces/:namespaceId/keys",
|
|
18
15
|
"GET",
|
|
@@ -20,19 +17,15 @@ export function mockKeyListRequest(
|
|
|
20
17
|
requests.count++;
|
|
21
18
|
expect(accountId).toEqual("some-account-id");
|
|
22
19
|
expect(namespaceId).toEqual(expectedNamespaceId);
|
|
23
|
-
if (
|
|
24
|
-
return createFetchResult(
|
|
20
|
+
if (expectedKeys.length <= keysPerRequest) {
|
|
21
|
+
return createFetchResult(expectedKeys);
|
|
25
22
|
} else {
|
|
26
23
|
const start = parseInt(query.get("cursor") ?? "0") || 0;
|
|
27
24
|
const end = start + keysPerRequest;
|
|
28
|
-
const cursor = end <
|
|
29
|
-
return createFetchResult(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
[],
|
|
33
|
-
[],
|
|
34
|
-
{ cursor }
|
|
35
|
-
);
|
|
25
|
+
const cursor = end < expectedKeys.length ? end : blankCursorValue;
|
|
26
|
+
return createFetchResult(expectedKeys.slice(start, end), true, [], [], {
|
|
27
|
+
cursor,
|
|
28
|
+
});
|
|
36
29
|
}
|
|
37
30
|
}
|
|
38
31
|
);
|