wrangler 0.0.0-e5d3690 → 0.0.0-e5ebdb143
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/README.md +44 -25
- package/bin/wrangler.js +94 -31
- package/config-schema.json +3324 -0
- package/kv-asset-handler.js +1 -0
- package/package.json +146 -91
- package/templates/__tests__/pages-dev-util.test.ts +128 -0
- package/templates/__tests__/tsconfig-sanity.ts +12 -0
- package/templates/__tests__/tsconfig.json +8 -0
- package/templates/checked-fetch.js +30 -0
- package/templates/facade.d.ts +19 -0
- package/templates/gitignore +170 -0
- package/templates/init-tests/test-jest-new-worker.js +23 -0
- package/templates/init-tests/test-vitest-new-worker.js +24 -0
- package/templates/init-tests/test-vitest-new-worker.ts +25 -0
- package/templates/middleware/common.ts +67 -0
- package/templates/middleware/loader-modules.ts +134 -0
- package/templates/middleware/loader-sw.ts +229 -0
- package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
- package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
- package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
- package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
- package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +29 -0
- package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
- package/templates/middleware/middleware-serve-static-assets.ts +56 -0
- package/templates/modules-watch-stub.js +4 -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 +33 -0
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-dev-pipeline.ts +32 -0
- package/templates/pages-dev-util.ts +55 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +190 -0
- package/templates/pages-template-worker.ts +198 -0
- package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
- package/templates/startDevWorker/ProxyWorker.ts +336 -0
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +22 -0
- package/templates/tsconfig.json +8 -0
- package/wrangler-dist/InspectorProxyWorker.js +464 -0
- package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
- package/wrangler-dist/ProxyWorker.js +241 -0
- package/wrangler-dist/ProxyWorker.js.map +6 -0
- package/wrangler-dist/cli.d.ts +26702 -0
- package/wrangler-dist/cli.js +164834 -117766
- package/import_meta_url.js +0 -3
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +0 -1
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/pages/functions/buildWorker.ts +0 -62
- package/pages/functions/filepath-routing.test.ts +0 -39
- package/pages/functions/filepath-routing.ts +0 -228
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -158
- package/pages/functions/template-worker.ts +0 -136
- package/src/__tests__/clipboardy-mock.js +0 -4
- package/src/__tests__/dev.test.tsx +0 -67
- package/src/__tests__/index.test.ts +0 -289
- package/src/__tests__/jest.setup.ts +0 -22
- package/src/__tests__/kv.test.ts +0 -1199
- package/src/__tests__/mock-cfetch.ts +0 -173
- package/src/__tests__/mock-dialogs.ts +0 -65
- package/src/__tests__/publish.test.ts +0 -672
- package/src/__tests__/run-in-tmp.ts +0 -19
- package/src/__tests__/run-wrangler.ts +0 -32
- package/src/__tests__/whoami.test.tsx +0 -130
- package/src/api/form_data.ts +0 -131
- package/src/api/preview.ts +0 -128
- package/src/api/worker.ts +0 -155
- package/src/cfetch/index.ts +0 -110
- package/src/cfetch/internal.ts +0 -71
- package/src/cli.ts +0 -9
- package/src/config.ts +0 -646
- package/src/dev.tsx +0 -779
- package/src/dialogs.tsx +0 -77
- package/src/index.tsx +0 -1975
- package/src/inspect.ts +0 -543
- package/src/kv.tsx +0 -276
- package/src/module-collection.ts +0 -64
- package/src/pages.tsx +0 -1040
- package/src/proxy.ts +0 -302
- package/src/publish.ts +0 -382
- package/src/sites.tsx +0 -243
- package/src/tail.tsx +0 -73
- package/src/user.tsx +0 -1019
- package/src/whoami.tsx +0 -69
- package/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
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { main } from "../index";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Run the wrangler CLI tool in tests, capturing the logging output.
|
|
5
|
-
*/
|
|
6
|
-
export async function runWrangler(cmd?: string) {
|
|
7
|
-
const logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
8
|
-
const errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
9
|
-
const warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
10
|
-
try {
|
|
11
|
-
let error: unknown = undefined;
|
|
12
|
-
try {
|
|
13
|
-
await main(cmd?.split(" ") ?? []);
|
|
14
|
-
} catch (e) {
|
|
15
|
-
error = e;
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
error,
|
|
19
|
-
stdout: captureCalls(logSpy),
|
|
20
|
-
stderr: captureCalls(errorSpy),
|
|
21
|
-
warnings: captureCalls(warnSpy),
|
|
22
|
-
};
|
|
23
|
-
} finally {
|
|
24
|
-
logSpy.mockRestore();
|
|
25
|
-
errorSpy.mockRestore();
|
|
26
|
-
warnSpy.mockRestore();
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function captureCalls(spy: jest.SpyInstance): string {
|
|
31
|
-
return spy.mock.calls.flat(2).join("\n");
|
|
32
|
-
}
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { render } from "ink-testing-library";
|
|
5
|
-
import type { UserInfo } from "../whoami";
|
|
6
|
-
import { getUserInfo, WhoAmI } from "../whoami";
|
|
7
|
-
import { runInTempDir } from "./run-in-tmp";
|
|
8
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
9
|
-
import { setMockResponse } from "./mock-cfetch";
|
|
10
|
-
import { initialise } from "../user";
|
|
11
|
-
|
|
12
|
-
const ORIGINAL_CF_API_TOKEN = process.env.CF_API_TOKEN;
|
|
13
|
-
const ORIGINAL_CF_ACCOUNT_ID = process.env.CF_ACCOUNT_ID;
|
|
14
|
-
|
|
15
|
-
describe("getUserInfo()", () => {
|
|
16
|
-
runInTempDir();
|
|
17
|
-
|
|
18
|
-
beforeEach(() => {
|
|
19
|
-
// Clear the environment variables, so we can control them in the tests
|
|
20
|
-
delete process.env.CF_API_TOKEN;
|
|
21
|
-
delete process.env.CF_ACCOUNT_ID;
|
|
22
|
-
// Override where the home directory is so that we can specify a user config
|
|
23
|
-
mkdirSync("./home");
|
|
24
|
-
jest.spyOn(os, "homedir").mockReturnValue("./home");
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
afterEach(() => {
|
|
28
|
-
// Reset any changes to the environment variables
|
|
29
|
-
process.env.CF_API_TOKEN = ORIGINAL_CF_API_TOKEN;
|
|
30
|
-
process.env.CF_ACCOUNT_ID = ORIGINAL_CF_ACCOUNT_ID;
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("should return undefined if there is no config file", async () => {
|
|
34
|
-
await initialise();
|
|
35
|
-
const userInfo = await getUserInfo();
|
|
36
|
-
expect(userInfo).toBeUndefined();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("should return undefined if there is an empty config file", async () => {
|
|
40
|
-
writeUserConfig();
|
|
41
|
-
await initialise();
|
|
42
|
-
const userInfo = await getUserInfo();
|
|
43
|
-
expect(userInfo).toBeUndefined();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("should return the user's email and accounts if authenticated via config token", async () => {
|
|
47
|
-
writeUserConfig("some-oauth-token");
|
|
48
|
-
setMockResponse("/user", () => {
|
|
49
|
-
return { email: "user@example.com" };
|
|
50
|
-
});
|
|
51
|
-
setMockResponse("/accounts", () => {
|
|
52
|
-
return [
|
|
53
|
-
{ name: "Account One", id: "account-1" },
|
|
54
|
-
{ name: "Account Two", id: "account-2" },
|
|
55
|
-
{ name: "Account Three", id: "account-3" },
|
|
56
|
-
];
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
await initialise();
|
|
60
|
-
const userInfo = await getUserInfo();
|
|
61
|
-
|
|
62
|
-
expect(userInfo).toEqual({
|
|
63
|
-
authType: "OAuth",
|
|
64
|
-
apiToken: "some-oauth-token",
|
|
65
|
-
email: "user@example.com",
|
|
66
|
-
accounts: [
|
|
67
|
-
{ name: "Account One", id: "account-1" },
|
|
68
|
-
{ name: "Account Two", id: "account-2" },
|
|
69
|
-
{ name: "Account Three", id: "account-3" },
|
|
70
|
-
],
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
describe("WhoAmI component", () => {
|
|
76
|
-
it("should return undefined if there is no user", async () => {
|
|
77
|
-
const { lastFrame } = render(<WhoAmI user={undefined}></WhoAmI>);
|
|
78
|
-
|
|
79
|
-
expect(lastFrame()).toMatchInlineSnapshot(
|
|
80
|
-
`"You are not authenticated. Please run \`wrangler login\`."`
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("should display the user's email and accounts", async () => {
|
|
85
|
-
const user: UserInfo = {
|
|
86
|
-
authType: "OAuth",
|
|
87
|
-
apiToken: "some-oauth-token",
|
|
88
|
-
email: "user@example.com",
|
|
89
|
-
accounts: [
|
|
90
|
-
{ name: "Account One", id: "account-1" },
|
|
91
|
-
{ name: "Account Two", id: "account-2" },
|
|
92
|
-
{ name: "Account Three", id: "account-3" },
|
|
93
|
-
],
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const { lastFrame } = render(<WhoAmI user={user}></WhoAmI>);
|
|
97
|
-
|
|
98
|
-
expect(lastFrame()).toContain(
|
|
99
|
-
"You are logged in with an OAuth Token, associated with the email 'user@example.com'!"
|
|
100
|
-
);
|
|
101
|
-
expect(lastFrame()).toMatch(/Account Name .+ Account ID/);
|
|
102
|
-
expect(lastFrame()).toMatch(/Account One .+ account-1/);
|
|
103
|
-
expect(lastFrame()).toMatch(/Account Two .+ account-2/);
|
|
104
|
-
expect(lastFrame()).toMatch(/Account Three .+ account-3/);
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
function writeUserConfig(
|
|
109
|
-
oauth_token?: string,
|
|
110
|
-
refresh_token?: string,
|
|
111
|
-
expiration_time?: string
|
|
112
|
-
) {
|
|
113
|
-
const lines: string[] = [];
|
|
114
|
-
if (oauth_token) {
|
|
115
|
-
lines.push(`oauth_token = "${oauth_token}"`);
|
|
116
|
-
}
|
|
117
|
-
if (refresh_token) {
|
|
118
|
-
lines.push(`refresh_token = "${refresh_token}"`);
|
|
119
|
-
}
|
|
120
|
-
if (expiration_time) {
|
|
121
|
-
lines.push(`expiration_time = "${expiration_time}"`);
|
|
122
|
-
}
|
|
123
|
-
const configPath = path.join(os.homedir(), ".wrangler/config");
|
|
124
|
-
mkdirSync(configPath, { recursive: true });
|
|
125
|
-
writeFileSync(
|
|
126
|
-
path.join(configPath, "default.toml"),
|
|
127
|
-
lines.join("\n"),
|
|
128
|
-
"utf-8"
|
|
129
|
-
);
|
|
130
|
-
}
|
package/src/api/form_data.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CfWorkerInit,
|
|
3
|
-
CfModuleType,
|
|
4
|
-
CfModule,
|
|
5
|
-
CfDurableObjectMigrations,
|
|
6
|
-
} from "./worker.js";
|
|
7
|
-
import { FormData, Blob } from "formdata-node";
|
|
8
|
-
|
|
9
|
-
export function toMimeType(type: CfModuleType): string {
|
|
10
|
-
switch (type) {
|
|
11
|
-
case "esm":
|
|
12
|
-
return "application/javascript+module";
|
|
13
|
-
case "commonjs":
|
|
14
|
-
return "application/javascript";
|
|
15
|
-
case "compiled-wasm":
|
|
16
|
-
return "application/wasm";
|
|
17
|
-
case "buffer":
|
|
18
|
-
return "application/octet-stream";
|
|
19
|
-
case "text":
|
|
20
|
-
return "text/plain";
|
|
21
|
-
default:
|
|
22
|
-
throw new TypeError("Unsupported module: " + type);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function toModule(module: CfModule, entryType: CfModuleType): Blob {
|
|
27
|
-
const { type: moduleType, content } = module;
|
|
28
|
-
const type = toMimeType(moduleType ?? entryType);
|
|
29
|
-
|
|
30
|
-
return new Blob([content], { type });
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface WorkerMetadata {
|
|
34
|
-
compatibility_date?: string;
|
|
35
|
-
compatibility_flags?: string[];
|
|
36
|
-
usage_model?: "bundled" | "unbound";
|
|
37
|
-
migrations?: CfDurableObjectMigrations;
|
|
38
|
-
bindings: (
|
|
39
|
-
| { type: "kv_namespace"; name: string; namespace_id: string }
|
|
40
|
-
| { type: "plain_text"; name: string; text: string }
|
|
41
|
-
| {
|
|
42
|
-
type: "durable_object_namespace";
|
|
43
|
-
name: string;
|
|
44
|
-
class_name: string;
|
|
45
|
-
script_name?: string;
|
|
46
|
-
}
|
|
47
|
-
| {
|
|
48
|
-
type: "service";
|
|
49
|
-
name: string;
|
|
50
|
-
service: string;
|
|
51
|
-
environment: string;
|
|
52
|
-
}
|
|
53
|
-
)[];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Creates a `FormData` upload from a `CfWorkerInit`.
|
|
58
|
-
*/
|
|
59
|
-
export function toFormData(worker: CfWorkerInit): FormData {
|
|
60
|
-
const formData = new FormData();
|
|
61
|
-
const {
|
|
62
|
-
main,
|
|
63
|
-
modules,
|
|
64
|
-
bindings,
|
|
65
|
-
migrations,
|
|
66
|
-
usage_model,
|
|
67
|
-
compatibility_date,
|
|
68
|
-
compatibility_flags,
|
|
69
|
-
} = worker;
|
|
70
|
-
|
|
71
|
-
const metadataBindings: WorkerMetadata["bindings"] = [];
|
|
72
|
-
|
|
73
|
-
bindings.kv_namespaces?.forEach(({ id, binding }) => {
|
|
74
|
-
metadataBindings.push({
|
|
75
|
-
name: binding,
|
|
76
|
-
type: "kv_namespace",
|
|
77
|
-
namespace_id: id,
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
bindings.durable_objects?.bindings.forEach(
|
|
82
|
-
({ name, class_name, script_name }) => {
|
|
83
|
-
metadataBindings.push({
|
|
84
|
-
name,
|
|
85
|
-
type: "durable_object_namespace",
|
|
86
|
-
class_name: class_name,
|
|
87
|
-
...(script_name && { script_name }),
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
Object.entries(bindings.vars || {})?.forEach(([key, value]) => {
|
|
93
|
-
metadataBindings.push({ name: key, type: "plain_text", text: value });
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
bindings.services?.forEach(({ name, service, environment }) => {
|
|
97
|
-
metadataBindings.push({
|
|
98
|
-
name,
|
|
99
|
-
type: "service",
|
|
100
|
-
service,
|
|
101
|
-
environment,
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
const metadata: WorkerMetadata = {
|
|
106
|
-
...(main.type !== "commonjs"
|
|
107
|
-
? { main_module: main.name }
|
|
108
|
-
: { body_part: main.name }),
|
|
109
|
-
bindings: metadataBindings,
|
|
110
|
-
...(compatibility_date && { compatibility_date }),
|
|
111
|
-
...(compatibility_flags && { compatibility_flags }),
|
|
112
|
-
...(usage_model && { usage_model }),
|
|
113
|
-
...(migrations && { migrations }),
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
formData.set("metadata", JSON.stringify(metadata));
|
|
117
|
-
|
|
118
|
-
if (main.type === "commonjs" && modules && modules.length > 0) {
|
|
119
|
-
throw new TypeError(
|
|
120
|
-
"More than one module can only be specified when type = 'esm'"
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
for (const module of [main].concat(modules || [])) {
|
|
125
|
-
const { name } = module;
|
|
126
|
-
const blob = toModule(module, main.type ?? "esm");
|
|
127
|
-
formData.set(name, blob, name);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return formData;
|
|
131
|
-
}
|
package/src/api/preview.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import fetch from "node-fetch";
|
|
2
|
-
import { fetchResult } from "../cfetch";
|
|
3
|
-
import { toFormData } from "./form_data";
|
|
4
|
-
import type { CfAccount, CfWorkerInit } from "./worker";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A preview mode.
|
|
8
|
-
*
|
|
9
|
-
* * If true, then using a `workers.dev` subdomain.
|
|
10
|
-
* * Otherwise, a list of routes under a single zone.
|
|
11
|
-
*/
|
|
12
|
-
type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A preview token.
|
|
16
|
-
*/
|
|
17
|
-
export interface CfPreviewToken {
|
|
18
|
-
/**
|
|
19
|
-
* The header value required to trigger a preview.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* const headers = { 'cf-workers-preview-token': value }
|
|
23
|
-
* const response = await fetch('https://' + host, { headers })
|
|
24
|
-
*/
|
|
25
|
-
value: string;
|
|
26
|
-
/**
|
|
27
|
-
* The host where the preview is available.
|
|
28
|
-
*/
|
|
29
|
-
host: string;
|
|
30
|
-
/**
|
|
31
|
-
* A websocket url to a DevTools inspector.
|
|
32
|
-
*
|
|
33
|
-
* Workers does not have a fully-featured implementation
|
|
34
|
-
* of the Chrome DevTools protocol, but supports the following:
|
|
35
|
-
* * `console.log()` output.
|
|
36
|
-
* * `Error` stack traces.
|
|
37
|
-
* * `fetch()` events.
|
|
38
|
-
*
|
|
39
|
-
* There is no support for breakpoints, but we want to implement
|
|
40
|
-
* this eventually.
|
|
41
|
-
*
|
|
42
|
-
* @link https://chromedevtools.github.io/devtools-protocol/
|
|
43
|
-
*/
|
|
44
|
-
inspectorUrl: URL;
|
|
45
|
-
/**
|
|
46
|
-
* A url to prewarm the preview session.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* fetch(prewarmUrl, { method: 'POST' })
|
|
50
|
-
*/
|
|
51
|
-
prewarmUrl: URL;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Generates a preview session token.
|
|
56
|
-
*/
|
|
57
|
-
async function sessionToken(account: CfAccount): Promise<CfPreviewToken> {
|
|
58
|
-
const { accountId, zoneId } = account;
|
|
59
|
-
const initUrl = zoneId
|
|
60
|
-
? `/zones/${zoneId}/workers/edge-preview`
|
|
61
|
-
: `/accounts/${accountId}/workers/subdomain/edge-preview`;
|
|
62
|
-
|
|
63
|
-
const { exchange_url } = await fetchResult<{ exchange_url: string }>(initUrl);
|
|
64
|
-
const { inspector_websocket, token } = (await (
|
|
65
|
-
await fetch(exchange_url)
|
|
66
|
-
).json()) as { inspector_websocket: string; token: string };
|
|
67
|
-
const { host } = new URL(inspector_websocket);
|
|
68
|
-
const query = `cf_workers_preview_token=${token}`;
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
value: token,
|
|
72
|
-
host,
|
|
73
|
-
inspectorUrl: new URL(`${inspector_websocket}?${query}`),
|
|
74
|
-
prewarmUrl: new URL(
|
|
75
|
-
`https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
|
|
76
|
-
),
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Credit: https://stackoverflow.com/a/2117523
|
|
81
|
-
function randomId(): string {
|
|
82
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
83
|
-
const r = (Math.random() * 16) | 0,
|
|
84
|
-
v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
85
|
-
return v.toString(16);
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Creates a preview token.
|
|
91
|
-
*/
|
|
92
|
-
export async function previewToken(
|
|
93
|
-
account: CfAccount,
|
|
94
|
-
worker: CfWorkerInit
|
|
95
|
-
): Promise<CfPreviewToken> {
|
|
96
|
-
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(account);
|
|
97
|
-
|
|
98
|
-
const { accountId, zoneId } = account;
|
|
99
|
-
const scriptId = zoneId ? randomId() : worker.name || host.split(".")[0];
|
|
100
|
-
const url = `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
|
|
101
|
-
|
|
102
|
-
const mode: CfPreviewMode = zoneId
|
|
103
|
-
? { routes: ["*/*"] }
|
|
104
|
-
: { workers_dev: true };
|
|
105
|
-
|
|
106
|
-
const formData = toFormData(worker);
|
|
107
|
-
formData.set("wrangler-session-config", JSON.stringify(mode));
|
|
108
|
-
|
|
109
|
-
const { preview_token } = await fetchResult<{ preview_token: string }>(url, {
|
|
110
|
-
method: "POST",
|
|
111
|
-
// @ts-expect-error TODO: fix this
|
|
112
|
-
body: formData,
|
|
113
|
-
headers: {
|
|
114
|
-
"cf-preview-upload-config-token": value,
|
|
115
|
-
},
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
return {
|
|
119
|
-
value: preview_token,
|
|
120
|
-
// TODO: verify this works with zoned workers
|
|
121
|
-
host:
|
|
122
|
-
worker.name && !zoneId
|
|
123
|
-
? `${worker.name}.${host.split(".").slice(1).join(".")}`
|
|
124
|
-
: host,
|
|
125
|
-
inspectorUrl,
|
|
126
|
-
prewarmUrl,
|
|
127
|
-
};
|
|
128
|
-
}
|
package/src/api/worker.ts
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import type { CfPreviewToken } from "./preview";
|
|
2
|
-
import { previewToken } from "./preview";
|
|
3
|
-
import fetch from "node-fetch";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A Cloudflare account.
|
|
7
|
-
*/
|
|
8
|
-
export interface CfAccount {
|
|
9
|
-
/**
|
|
10
|
-
* An API token.
|
|
11
|
-
*
|
|
12
|
-
* @link https://api.cloudflare.com/#user-api-tokens-properties
|
|
13
|
-
*/
|
|
14
|
-
apiToken: string;
|
|
15
|
-
/**
|
|
16
|
-
* An account ID.
|
|
17
|
-
*/
|
|
18
|
-
accountId: string;
|
|
19
|
-
/**
|
|
20
|
-
* A zone ID, only required if not using `workers.dev`.
|
|
21
|
-
*/
|
|
22
|
-
zoneId?: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* A module type.
|
|
27
|
-
*/
|
|
28
|
-
export type CfModuleType =
|
|
29
|
-
| "esm"
|
|
30
|
-
| "commonjs"
|
|
31
|
-
| "compiled-wasm"
|
|
32
|
-
| "text"
|
|
33
|
-
| "buffer";
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* An imported module.
|
|
37
|
-
*/
|
|
38
|
-
export interface CfModule {
|
|
39
|
-
/**
|
|
40
|
-
* The module name.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* './src/index.js'
|
|
44
|
-
*/
|
|
45
|
-
name: string;
|
|
46
|
-
/**
|
|
47
|
-
* The module content, usually JavaScript or WASM code.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* export default {
|
|
51
|
-
* async fetch(request) {
|
|
52
|
-
* return new Response('Ok')
|
|
53
|
-
* }
|
|
54
|
-
* }
|
|
55
|
-
*/
|
|
56
|
-
content: string | BufferSource;
|
|
57
|
-
/**
|
|
58
|
-
* The module type.
|
|
59
|
-
*
|
|
60
|
-
* If absent, will default to the main module's type.
|
|
61
|
-
*/
|
|
62
|
-
type?: CfModuleType;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* A map of variable names to values.
|
|
67
|
-
*/
|
|
68
|
-
interface CfVars {
|
|
69
|
-
[key: string]: string;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* A KV namespace.
|
|
74
|
-
*/
|
|
75
|
-
interface CfKvNamespace {
|
|
76
|
-
binding: string;
|
|
77
|
-
id: string;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* A Durable Object.
|
|
82
|
-
*/
|
|
83
|
-
interface CfDurableObject {
|
|
84
|
-
name: string;
|
|
85
|
-
class_name: string;
|
|
86
|
-
script_name?: string;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* A Service.
|
|
91
|
-
*/
|
|
92
|
-
interface CfService {
|
|
93
|
-
name: string;
|
|
94
|
-
service: string;
|
|
95
|
-
environment: string;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export interface CfDurableObjectMigrations {
|
|
99
|
-
old_tag?: string;
|
|
100
|
-
new_tag: string;
|
|
101
|
-
steps: {
|
|
102
|
-
new_classes?: string[];
|
|
103
|
-
renamed_classes?: string[];
|
|
104
|
-
deleted_classes?: string[];
|
|
105
|
-
}[];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Options for creating a `CfWorker`.
|
|
110
|
-
*/
|
|
111
|
-
export interface CfWorkerInit {
|
|
112
|
-
/**
|
|
113
|
-
* The name of the worker.
|
|
114
|
-
*/
|
|
115
|
-
name: string | undefined;
|
|
116
|
-
/**
|
|
117
|
-
* The entrypoint module.
|
|
118
|
-
*/
|
|
119
|
-
main: CfModule;
|
|
120
|
-
/**
|
|
121
|
-
* The list of additional modules.
|
|
122
|
-
*/
|
|
123
|
-
modules: undefined | CfModule[];
|
|
124
|
-
/**
|
|
125
|
-
* All the bindings
|
|
126
|
-
*/
|
|
127
|
-
bindings: {
|
|
128
|
-
kv_namespaces?: CfKvNamespace[];
|
|
129
|
-
durable_objects?: { bindings: CfDurableObject[] };
|
|
130
|
-
vars?: CfVars;
|
|
131
|
-
services?: CfService[];
|
|
132
|
-
};
|
|
133
|
-
migrations: undefined | CfDurableObjectMigrations;
|
|
134
|
-
compatibility_date: string | undefined;
|
|
135
|
-
compatibility_flags: undefined | string[];
|
|
136
|
-
usage_model: undefined | "bundled" | "unbound";
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* A stub to create a Cloudflare Worker preview.
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* const {value, host} = await createWorker(init, acct);
|
|
144
|
-
*/
|
|
145
|
-
export async function createWorker(
|
|
146
|
-
init: CfWorkerInit,
|
|
147
|
-
account: CfAccount
|
|
148
|
-
): Promise<CfPreviewToken> {
|
|
149
|
-
const token = await previewToken(account, init);
|
|
150
|
-
const response = await fetch(token.prewarmUrl.href, { method: "POST" });
|
|
151
|
-
if (!response.ok) {
|
|
152
|
-
// console.error("worker failed to prewarm: ", response.statusText);
|
|
153
|
-
}
|
|
154
|
-
return token;
|
|
155
|
-
}
|
package/src/cfetch/index.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import type { RequestInit } from "node-fetch";
|
|
2
|
-
import { URLSearchParams } from "url";
|
|
3
|
-
import { fetchInternal } from "./internal";
|
|
4
|
-
|
|
5
|
-
// Check out https://api.cloudflare.com/ for API docs.
|
|
6
|
-
|
|
7
|
-
export { CF_API_BASE_URL } from "./internal";
|
|
8
|
-
|
|
9
|
-
export interface FetchError {
|
|
10
|
-
code: number;
|
|
11
|
-
message: string;
|
|
12
|
-
}
|
|
13
|
-
export interface FetchResult<ResponseType = unknown> {
|
|
14
|
-
success: boolean;
|
|
15
|
-
result: ResponseType;
|
|
16
|
-
errors: FetchError[];
|
|
17
|
-
messages: string[];
|
|
18
|
-
result_info?: unknown;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Make a fetch request for a raw JSON value.
|
|
23
|
-
*/
|
|
24
|
-
export async function fetchRaw<ResponseType>(
|
|
25
|
-
resource: string,
|
|
26
|
-
init: RequestInit = {},
|
|
27
|
-
queryParams?: URLSearchParams
|
|
28
|
-
): Promise<ResponseType> {
|
|
29
|
-
return fetchInternal<ResponseType>(resource, init, queryParams);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Make a fetch request, and extract the `result` from the JSON response.
|
|
34
|
-
*/
|
|
35
|
-
export async function fetchResult<ResponseType>(
|
|
36
|
-
resource: string,
|
|
37
|
-
init: RequestInit = {},
|
|
38
|
-
queryParams?: URLSearchParams
|
|
39
|
-
): Promise<ResponseType> {
|
|
40
|
-
const json = await fetchInternal<FetchResult<ResponseType>>(
|
|
41
|
-
resource,
|
|
42
|
-
init,
|
|
43
|
-
queryParams
|
|
44
|
-
);
|
|
45
|
-
if (json.success) {
|
|
46
|
-
return json.result;
|
|
47
|
-
} else {
|
|
48
|
-
throwFetchError(resource, json);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Make a fetch request for a list of values,
|
|
54
|
-
* extracting the `result` from the JSON response,
|
|
55
|
-
* and repeating the request if the results are paginated.
|
|
56
|
-
*/
|
|
57
|
-
export async function fetchListResult<ResponseType>(
|
|
58
|
-
resource: string,
|
|
59
|
-
init: RequestInit = {},
|
|
60
|
-
queryParams?: URLSearchParams
|
|
61
|
-
): Promise<ResponseType[]> {
|
|
62
|
-
const results: ResponseType[] = [];
|
|
63
|
-
let getMoreResults = true;
|
|
64
|
-
let cursor: string | undefined;
|
|
65
|
-
while (getMoreResults) {
|
|
66
|
-
if (cursor) {
|
|
67
|
-
queryParams = new URLSearchParams(queryParams);
|
|
68
|
-
queryParams.set("cursor", cursor);
|
|
69
|
-
}
|
|
70
|
-
const json = await fetchInternal<FetchResult<ResponseType[]>>(
|
|
71
|
-
resource,
|
|
72
|
-
init,
|
|
73
|
-
queryParams
|
|
74
|
-
);
|
|
75
|
-
if (json.success) {
|
|
76
|
-
results.push(...json.result);
|
|
77
|
-
if (hasCursor(json.result_info)) {
|
|
78
|
-
cursor = json.result_info?.cursor;
|
|
79
|
-
} else {
|
|
80
|
-
getMoreResults = false;
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
throwFetchError(resource, json);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return results;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function throwFetchError(
|
|
90
|
-
resource: string,
|
|
91
|
-
response: FetchResult<unknown>
|
|
92
|
-
): never {
|
|
93
|
-
response.messages.forEach((message) => console.warn(message));
|
|
94
|
-
const errors = response.errors
|
|
95
|
-
.map((error) => `${error.code}: ${error.message}`)
|
|
96
|
-
.join("\n");
|
|
97
|
-
const error = new Error(`Failed to fetch ${resource} - \n${errors}`);
|
|
98
|
-
// add the first error code directly to this error
|
|
99
|
-
// so consumers can use it for specific behaviour
|
|
100
|
-
const code = response.errors[0]?.code;
|
|
101
|
-
if (code) {
|
|
102
|
-
//@ts-expect-error non-standard property on Error
|
|
103
|
-
error.code = code;
|
|
104
|
-
}
|
|
105
|
-
throw error;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function hasCursor(result_info: unknown): result_info is { cursor: string } {
|
|
109
|
-
return (result_info as { cursor: string } | undefined)?.cursor !== undefined;
|
|
110
|
-
}
|