wrangler 0.0.0-ece06ea → 0.0.0-ef13735
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/bin/wrangler.js +2 -2
- package/package.json +35 -21
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +66 -0
- package/pages/functions/filepath-routing.ts +219 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +151 -0
- package/pages/functions/template-worker.ts +130 -0
- package/src/__tests__/dev.test.tsx +73 -0
- package/src/__tests__/guess-worker-format.test.ts +51 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/mock-account-id.ts +30 -0
- package/src/__tests__/helpers/mock-bin.ts +33 -0
- package/src/__tests__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +56 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +40 -0
- package/src/__tests__/helpers/mock-user.ts +27 -0
- package/src/__tests__/helpers/run-in-tmp.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +14 -0
- package/src/__tests__/index.test.ts +715 -119
- package/src/__tests__/jest.setup.ts +37 -0
- package/src/__tests__/kv.test.ts +1139 -0
- package/src/__tests__/logout.test.ts +50 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/publish.test.ts +1381 -0
- package/src/__tests__/secret.test.ts +210 -0
- package/src/__tests__/whoami.test.tsx +86 -0
- package/src/api/form_data.ts +93 -100
- package/src/api/preview.ts +29 -24
- package/src/api/worker.ts +55 -47
- package/src/cfetch/index.ts +102 -0
- package/src/cfetch/internal.ts +101 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +641 -112
- package/src/dev.tsx +476 -355
- package/src/dialogs.tsx +6 -14
- package/src/guess-worker-format.ts +66 -0
- package/src/index.tsx +828 -496
- package/src/inspect.ts +596 -0
- package/src/kv.tsx +118 -45
- package/src/module-collection.ts +63 -0
- package/src/open-in-browser.ts +13 -0
- package/src/package-manager.ts +120 -0
- package/src/pages.tsx +915 -215
- package/src/paths.ts +26 -0
- package/src/proxy.ts +370 -0
- package/src/publish.ts +349 -290
- package/src/sites.tsx +184 -55
- package/src/tail.tsx +5 -3
- package/src/user.tsx +81 -91
- package/src/whoami.tsx +69 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
- package/wrangler-dist/cli.js +119231 -130682
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/inspect.ts +0 -441
- package/src/cfetch.ts +0 -72
- package/src/util/fetch.ts +0 -74
|
@@ -0,0 +1,56 @@
|
|
|
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 normalizeSlashes(stripTimings(logSpy.mock.calls.flat(2).join("\n")));
|
|
13
|
+
},
|
|
14
|
+
get err() {
|
|
15
|
+
return normalizeSlashes(
|
|
16
|
+
stripTimings(errorSpy.mock.calls.flat(2).join("\n"))
|
|
17
|
+
);
|
|
18
|
+
},
|
|
19
|
+
get warn() {
|
|
20
|
+
return normalizeSlashes(
|
|
21
|
+
stripTimings(warnSpy.mock.calls.flat(2).join("\n"))
|
|
22
|
+
);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function mockConsoleMethods() {
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
29
|
+
errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
30
|
+
warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
31
|
+
});
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
logSpy.mockRestore();
|
|
34
|
+
errorSpy.mockRestore();
|
|
35
|
+
warnSpy.mockRestore();
|
|
36
|
+
});
|
|
37
|
+
return std;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Ensure slashes in the `str` are OS file-system agnostic.
|
|
42
|
+
*
|
|
43
|
+
* Use this in snapshot tests to be resilient to file-system differences.
|
|
44
|
+
*/
|
|
45
|
+
export function normalizeSlashes(str: string): string {
|
|
46
|
+
return str.replace(/\\/g, "/");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Strip "timing data" out of the `stdout` string, since this is not always deterministic.
|
|
51
|
+
*
|
|
52
|
+
* Use this in snapshot tests to be resilient to slight changes in timing of processing.
|
|
53
|
+
*/
|
|
54
|
+
export function stripTimings(stdout: string): string {
|
|
55
|
+
return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
|
|
56
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { confirm, prompt } from "../../dialogs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The expected values for a confirmation request.
|
|
5
|
+
*/
|
|
6
|
+
export interface ConfirmExpectation {
|
|
7
|
+
/** The text expected to be seen in the confirmation dialog. */
|
|
8
|
+
text: string;
|
|
9
|
+
/** The mock response send back from the confirmation dialog. */
|
|
10
|
+
result: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Mock the implementation of `confirm()` that will respond with configured results
|
|
15
|
+
* for configured confirmation text messages.
|
|
16
|
+
*
|
|
17
|
+
* If there is a call to `confirm()` that does not match any of the expectations
|
|
18
|
+
* then an error is thrown.
|
|
19
|
+
*/
|
|
20
|
+
export function mockConfirm(...expectations: ConfirmExpectation[]) {
|
|
21
|
+
(confirm as jest.Mock).mockImplementation((text: string) => {
|
|
22
|
+
for (const { text: expectedText, result } of expectations) {
|
|
23
|
+
if (text === expectedText) {
|
|
24
|
+
return Promise.resolve(result);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`Unexpected confirmation message: ${text}`);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The expected values for a prompt request.
|
|
33
|
+
*/
|
|
34
|
+
export interface PromptExpectation {
|
|
35
|
+
/** The text expected to be seen in the prompt dialog. */
|
|
36
|
+
text: string;
|
|
37
|
+
/** The type of the prompt. */
|
|
38
|
+
type: "text" | "password";
|
|
39
|
+
/** The mock response send back from the prompt dialog. */
|
|
40
|
+
result: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Mock the implementation of `prompt()` that will respond with configured results
|
|
45
|
+
* for configured prompt text messages.
|
|
46
|
+
*
|
|
47
|
+
* If there is a call to `prompt()` that does not match any of the expectations
|
|
48
|
+
* then an error is thrown.
|
|
49
|
+
*/
|
|
50
|
+
export function mockPrompt(...expectations: PromptExpectation[]) {
|
|
51
|
+
(prompt as jest.Mock).mockImplementation(
|
|
52
|
+
(text: string, type: "text" | "password") => {
|
|
53
|
+
for (const {
|
|
54
|
+
text: expectedText,
|
|
55
|
+
type: expectedType,
|
|
56
|
+
result,
|
|
57
|
+
} of expectations) {
|
|
58
|
+
if (text === expectedText && type == expectedType) {
|
|
59
|
+
return Promise.resolve(result);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`Unexpected confirmation message: ${text}`);
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -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,29 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
|
|
5
|
+
const originalCwd = process.cwd();
|
|
6
|
+
|
|
7
|
+
export function runInTempDir({ homedir }: { homedir?: string } = {}) {
|
|
8
|
+
let tmpDir: string;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "wrangler-tests"));
|
|
12
|
+
process.chdir(tmpDir);
|
|
13
|
+
if (homedir !== undefined) {
|
|
14
|
+
// Override where the home directory is so that we can write our own user config,
|
|
15
|
+
// without destroying the real thing.
|
|
16
|
+
fs.mkdirSync(homedir);
|
|
17
|
+
// Note it is very important that we use the "default" value from "node:os" (e.g. `import os from "node:os";`)
|
|
18
|
+
// rather than an alias to the module (e.g. `import * as os from "node:os";`).
|
|
19
|
+
// This is because the module gets transpiled so that the "method" `homedir()` gets converted to a
|
|
20
|
+
// getter that is not configurable (and so cannot be spied upon).
|
|
21
|
+
jest.spyOn(os, "homedir").mockReturnValue(homedir);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
process.chdir(originalCwd);
|
|
27
|
+
fs.rmSync(tmpDir, { recursive: true });
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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 (e) {
|
|
11
|
+
e.message = normalizeSlashes(stripTimings(e.message));
|
|
12
|
+
throw e;
|
|
13
|
+
}
|
|
14
|
+
}
|