wrangler 0.0.0-adae8e8 → 0.0.0-b0fcc7d
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 +51 -0
- package/bin/wrangler.js +2 -2
- package/package.json +34 -24
- package/src/__tests__/clipboardy-mock.js +4 -0
- package/src/__tests__/index.test.ts +83 -52
- package/src/__tests__/mock-cfetch.js +46 -0
- package/src/api/form_data.ts +35 -19
- package/src/api/preview.ts +28 -23
- package/src/api/worker.ts +24 -3
- package/src/{index_node.ts → cli.ts} +1 -1
- package/src/config.ts +29 -1
- package/src/dev.tsx +96 -30
- package/src/dialogs.tsx +85 -0
- package/src/index.tsx +256 -108
- package/src/pages.tsx +5 -1
- package/src/publish.ts +186 -111
- package/src/sites.tsx +1 -1
- package/src/tail.tsx +1 -1
- package/src/user.tsx +19 -0
- package/wrangler-dist/{index_node.js → cli.js} +30743 -26202
- package/wrangler-dist/{index_node.js.map → cli.js.map} +3 -3
- package/src/secret.tsx +0 -40
- package/src/util/fetch.ts +0 -74
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
## 🤠 wrangler
|
|
2
|
+
|
|
3
|
+
`wrangler` is a command line tool for building [Cloudflare Workers](https://workers.cloudflare.com/).
|
|
4
|
+
|
|
5
|
+
[(Read the full stack week launch blog post.)](https://blog.cloudflare.com/wrangler-v2-beta/)
|
|
6
|
+
|
|
7
|
+
**DISCLAIMER**: This is a work in progress, and is NOT recommended for use in production. We are opening this preview for feedback from the community, and to openly share our [roadmap](https://github.com/cloudflare/wrangler2/issues/12) for the future. As such, expect APIs and documentation to change before the end of the preview.
|
|
8
|
+
|
|
9
|
+
Further, we will NOT do a general release until we are both feature complete, and have a full backward compatibility and incremental migration plan in place. For more details, follow the [parent roadmap issue](https://github.com/cloudflare/wrangler2/issues/12).
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# Make a javascript file
|
|
15
|
+
$ echo "export default { fetch() { return new Response('hello world') } }" > index.js
|
|
16
|
+
# try it out
|
|
17
|
+
$ npx wrangler@beta dev index.js
|
|
18
|
+
# and then publish it
|
|
19
|
+
$ npx wrangler@beta publish index.js --name my-worker
|
|
20
|
+
# visit https://my-worker.<your workers subdomain>.workers.dev
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Installation:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
$ npm install wrangler@beta
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Commands
|
|
30
|
+
|
|
31
|
+
### `wrangler init [name]`
|
|
32
|
+
|
|
33
|
+
Creates a `wrangler.toml` configuration file. For more details on the configuration keys and values, refer to the [documentation](https://developers.cloudflare.com/workers/cli-wrangler/configuration).
|
|
34
|
+
|
|
35
|
+
### `wrangler dev [script]`
|
|
36
|
+
|
|
37
|
+
Start a local development server, with live reloading and devtools.
|
|
38
|
+
|
|
39
|
+
### `wrangler publish [script] --name [name]`
|
|
40
|
+
|
|
41
|
+
Publish the given script to the worldwide Cloudflare network.
|
|
42
|
+
|
|
43
|
+
For more commands and options, refer to the [documentation](https://developers.cloudflare.com/workers/cli-wrangler/commands).
|
|
44
|
+
|
|
45
|
+
### `wrangler pages dev [directory] [-- command]`
|
|
46
|
+
|
|
47
|
+
Either serves a static build asset directory, or proxies itself in front of a command.
|
|
48
|
+
|
|
49
|
+
Builds and runs functions from a `./functions` directory or uses a `_worker.js` file inside the static build asset directory.
|
|
50
|
+
|
|
51
|
+
For more commands and options, refer to the [documentation](https://developers.cloudflare.com/pages/platform/functions#develop-and-preview-locally) or run `wrangler pages dev --help`.
|
package/bin/wrangler.js
CHANGED
|
@@ -10,7 +10,7 @@ async function main() {
|
|
|
10
10
|
// Note Volta and nvm are also recommended in the official docs:
|
|
11
11
|
// https://developers.cloudflare.com/workers/get-started/guide#2-install-the-workers-cli
|
|
12
12
|
console.error(
|
|
13
|
-
`Wrangler requires at least Node.js ${MIN_NODE_VERSION}.
|
|
13
|
+
`Wrangler requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}.
|
|
14
14
|
You should use the latest Node.js version if possible, as Cloudflare Workers use a very up-to-date version of V8.
|
|
15
15
|
Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.`
|
|
16
16
|
);
|
|
@@ -24,7 +24,7 @@ Consider using a Node.js version manager such as https://volta.sh/ or https://gi
|
|
|
24
24
|
"--no-warnings",
|
|
25
25
|
"--experimental-vm-modules",
|
|
26
26
|
...process.execArgv,
|
|
27
|
-
join(__dirname, "../wrangler-dist/
|
|
27
|
+
join(__dirname, "../wrangler-dist/cli.js"),
|
|
28
28
|
...process.argv.slice(2),
|
|
29
29
|
],
|
|
30
30
|
{ stdio: "inherit" }
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrangler",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-b0fcc7d",
|
|
4
4
|
"author": "wrangler@cloudflare.com",
|
|
5
5
|
"description": "Command-line interface for all things Cloudflare Workers",
|
|
6
|
-
"bin":
|
|
6
|
+
"bin": {
|
|
7
|
+
"wrangler": "./bin/wrangler.js",
|
|
8
|
+
"wrangler2": "./bin/wrangler.js"
|
|
9
|
+
},
|
|
7
10
|
"license": "MIT OR Apache-2.0",
|
|
8
11
|
"bugs": {
|
|
9
12
|
"url": "https://github.com/cloudflare/wrangler/issues"
|
|
@@ -33,25 +36,28 @@
|
|
|
33
36
|
"cli"
|
|
34
37
|
],
|
|
35
38
|
"dependencies": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
+
"@cloudflare/pages-functions-compiler": "0.3.8",
|
|
40
|
+
"esbuild": "0.14.1",
|
|
41
|
+
"miniflare": "2.0.0-rc.3",
|
|
42
|
+
"semiver": "^1.1.0",
|
|
43
|
+
"serve": "^13.0.2",
|
|
44
|
+
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
|
45
|
+
"@esbuild-plugins/node-modules-polyfill": "^0.1.2"
|
|
39
46
|
},
|
|
40
47
|
"optionalDependencies": {
|
|
41
48
|
"fsevents": "~2.3.2"
|
|
42
49
|
},
|
|
43
50
|
"devDependencies": {
|
|
44
|
-
"@cloudflare/pages-functions-compiler": "^0.3.3",
|
|
45
51
|
"@iarna/toml": "^2.2.5",
|
|
46
52
|
"@types/cloudflare": "^2.7.6",
|
|
47
53
|
"@types/express": "^4.17.13",
|
|
48
|
-
"@types/react": "^17.0.
|
|
54
|
+
"@types/react": "^17.0.37",
|
|
49
55
|
"@types/signal-exit": "^3.0.1",
|
|
50
|
-
"@types/ws": "^8.2.
|
|
51
|
-
"@types/yargs": "^17.0.
|
|
56
|
+
"@types/ws": "^8.2.1",
|
|
57
|
+
"@types/yargs": "^17.0.7",
|
|
52
58
|
"clipboardy": "^3.0.0",
|
|
53
59
|
"command-exists": "^1.2.9",
|
|
54
|
-
"execa": "^
|
|
60
|
+
"execa": "^6.0.0",
|
|
55
61
|
"express": "^4.17.1",
|
|
56
62
|
"finalhandler": "^1.1.2",
|
|
57
63
|
"find-up": "^6.2.0",
|
|
@@ -59,18 +65,18 @@
|
|
|
59
65
|
"http-proxy": "^1.18.1",
|
|
60
66
|
"http-proxy-middleware": "^2.0.1",
|
|
61
67
|
"ink": "^3.2.0",
|
|
62
|
-
"ink-select-input": "^4.2.
|
|
68
|
+
"ink-select-input": "^4.2.1",
|
|
63
69
|
"ink-table": "^3.0.0",
|
|
64
|
-
"ink-text-input": "^4.0.
|
|
65
|
-
"node-fetch": "
|
|
70
|
+
"ink-text-input": "^4.0.2",
|
|
71
|
+
"node-fetch": "^3.1.0",
|
|
66
72
|
"open": "^8.4.0",
|
|
73
|
+
"path-to-regexp": "^6.2.0",
|
|
67
74
|
"react": "^17.0.2",
|
|
68
|
-
"serve": "^13.0.2",
|
|
69
75
|
"serve-static": "^1.14.1",
|
|
70
|
-
"signal-exit": "^3.0.
|
|
76
|
+
"signal-exit": "^3.0.6",
|
|
71
77
|
"tmp-promise": "^3.0.3",
|
|
72
|
-
"ws": "^8.
|
|
73
|
-
"yargs": "^17.
|
|
78
|
+
"ws": "^8.3.0",
|
|
79
|
+
"yargs": "^17.3.0"
|
|
74
80
|
},
|
|
75
81
|
"files": [
|
|
76
82
|
"src",
|
|
@@ -86,20 +92,24 @@
|
|
|
86
92
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
87
93
|
"build": "npm run clean && npm run bundle",
|
|
88
94
|
"start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
89
|
-
"test": "
|
|
95
|
+
"test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true"
|
|
90
96
|
},
|
|
91
97
|
"engines": {
|
|
92
|
-
"node": ">=16.
|
|
98
|
+
"node": ">=16.7.0"
|
|
93
99
|
},
|
|
94
100
|
"jest": {
|
|
101
|
+
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
|
|
102
|
+
"transformIgnorePatterns": [
|
|
103
|
+
"node_modules/(?!node-fetch|fetch-blob|find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|data-uri-to-buffer|formdata-polyfill|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)"
|
|
104
|
+
],
|
|
105
|
+
"moduleNameMapper": {
|
|
106
|
+
"clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
|
|
107
|
+
},
|
|
95
108
|
"transform": {
|
|
96
|
-
"^.+\\.
|
|
109
|
+
"^.+\\.c?(t|j)sx?$": [
|
|
97
110
|
"esbuild-jest",
|
|
98
111
|
{
|
|
99
|
-
"sourcemap": true
|
|
100
|
-
"loaders": {
|
|
101
|
-
".spec.ts": "tsx"
|
|
102
|
-
}
|
|
112
|
+
"sourcemap": true
|
|
103
113
|
}
|
|
104
114
|
]
|
|
105
115
|
}
|
|
@@ -1,19 +1,53 @@
|
|
|
1
|
-
import execa from "execa";
|
|
2
1
|
import * as fs from "node:fs";
|
|
3
2
|
import * as fsp from "node:fs/promises";
|
|
4
3
|
import * as path from "node:path";
|
|
5
4
|
import * as TOML from "@iarna/toml";
|
|
5
|
+
import { main } from "../index";
|
|
6
|
+
// @ts-expect-error we're mocking cfetch, so of course setMock isn't a thing
|
|
7
|
+
import { setMock, unsetAllMocks } from "../cfetch";
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
return
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
jest.mock("../cfetch", () => {
|
|
10
|
+
return jest.requireActual("./mock-cfetch");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
async function w(cmd: void | string, options?: { tap: boolean }) {
|
|
14
|
+
const tapped = options?.tap ? tap() : undefined;
|
|
15
|
+
await main([...(cmd ? cmd.split(" ") : [])]);
|
|
16
|
+
tapped?.off();
|
|
17
|
+
return { stdout: tapped?.out, stderr: tapped?.err };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function tap() {
|
|
21
|
+
const oldLog = console.log;
|
|
22
|
+
const oldError = console.error;
|
|
23
|
+
|
|
24
|
+
const toReturn = {
|
|
25
|
+
off: () => {
|
|
26
|
+
console.log = oldLog;
|
|
27
|
+
console.error = oldError;
|
|
28
|
+
},
|
|
29
|
+
out: "",
|
|
30
|
+
err: "",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
console.log = (...args) => {
|
|
34
|
+
toReturn.out += args.join("");
|
|
35
|
+
oldLog.apply(console, args);
|
|
36
|
+
// console.trace(...args); // use this if you want to find the true source of your console.log
|
|
37
|
+
};
|
|
38
|
+
console.error = (...args) => {
|
|
39
|
+
toReturn.err += args.join("");
|
|
40
|
+
oldError.apply(console, args);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return toReturn;
|
|
12
44
|
}
|
|
13
45
|
|
|
14
46
|
describe("wrangler", () => {
|
|
15
47
|
it("should run", async () => {
|
|
16
|
-
|
|
48
|
+
const { stdout } = await w(undefined, { tap: true });
|
|
49
|
+
|
|
50
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
17
51
|
"wrangler
|
|
18
52
|
|
|
19
53
|
Commands:
|
|
@@ -28,12 +62,12 @@ describe("wrangler", () => {
|
|
|
28
62
|
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
29
63
|
|
|
30
64
|
Flags:
|
|
31
|
-
--config Path to .toml configuration file [string]
|
|
32
|
-
--help Show help [boolean]
|
|
33
|
-
--version Show version number [boolean]
|
|
65
|
+
-c, --config Path to .toml configuration file [string]
|
|
66
|
+
-h, --help Show help [boolean]
|
|
67
|
+
-v, --version Show version number [boolean]
|
|
34
68
|
|
|
35
69
|
Options:
|
|
36
|
-
--local Run on my machine [boolean] [default: false]"
|
|
70
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
37
71
|
`);
|
|
38
72
|
});
|
|
39
73
|
|
|
@@ -52,32 +86,46 @@ describe("wrangler", () => {
|
|
|
52
86
|
it("should create a wrangler.toml", async () => {
|
|
53
87
|
await w("init");
|
|
54
88
|
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
55
|
-
expect(parsed.name).toBe("init");
|
|
56
89
|
expect(typeof parsed.compatibility_date).toBe("string");
|
|
57
90
|
});
|
|
58
91
|
|
|
59
92
|
it("should error when wrangler.toml already exists", async () => {
|
|
60
93
|
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
61
|
-
const { stderr } = await w("init");
|
|
94
|
+
const { stderr } = await w("init", { tap: true });
|
|
62
95
|
expect(stderr.endsWith("wrangler.toml already exists.")).toBe(true);
|
|
63
96
|
});
|
|
64
|
-
|
|
65
|
-
it("should create a named config", async () => {
|
|
66
|
-
await w("init xyz");
|
|
67
|
-
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
68
|
-
expect(parsed.name).toBe("xyz");
|
|
69
|
-
});
|
|
70
97
|
});
|
|
71
98
|
|
|
72
99
|
describe("kv:namespace", () => {
|
|
100
|
+
afterAll(() => {
|
|
101
|
+
unsetAllMocks();
|
|
102
|
+
});
|
|
103
|
+
let KVNamespaces: { title: string; id: string }[] = [];
|
|
73
104
|
it("can create a namespace", async () => {
|
|
74
|
-
|
|
75
|
-
|
|
105
|
+
setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
|
|
106
|
+
expect(init.method === "POST");
|
|
107
|
+
const body = JSON.parse(init.body);
|
|
108
|
+
expect(body.title).toBe("worker-UnitTestNamespace");
|
|
109
|
+
KVNamespaces.push({ title: body.title, id: "some-namespace-id" });
|
|
110
|
+
return { id: "some-namespace-id" };
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
await w("kv:namespace create UnitTestNamespace");
|
|
114
|
+
expect(
|
|
115
|
+
KVNamespaces.find((ns) => ns.title === `worker-UnitTestNamespace`)
|
|
116
|
+
).toBeTruthy();
|
|
76
117
|
});
|
|
77
118
|
|
|
78
119
|
let createdNamespace: { id: string; title: string };
|
|
79
120
|
it("can list namespaces", async () => {
|
|
80
|
-
|
|
121
|
+
setMock(
|
|
122
|
+
"/accounts/:accountId/storage/kv/namespaces\\?:qs",
|
|
123
|
+
(uri, init) => {
|
|
124
|
+
expect(init).toBe(undefined);
|
|
125
|
+
return KVNamespaces;
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
const { stdout } = await w("kv:namespace list", { tap: true });
|
|
81
129
|
const namespaces = JSON.parse(stdout);
|
|
82
130
|
createdNamespace = namespaces.find(
|
|
83
131
|
(ns) => ns.title === "worker-UnitTestNamespace"
|
|
@@ -86,37 +134,20 @@ describe("wrangler", () => {
|
|
|
86
134
|
});
|
|
87
135
|
|
|
88
136
|
it("can delete a namespace", async () => {
|
|
89
|
-
const
|
|
90
|
-
|
|
137
|
+
const namespaceIdToDelete = createdNamespace.id;
|
|
138
|
+
setMock(
|
|
139
|
+
"/accounts/:accountId/storage/kv/namespaces/:namespaceId",
|
|
140
|
+
(uri, init) => {
|
|
141
|
+
expect(init.method).toBe("DELETE");
|
|
142
|
+
KVNamespaces = KVNamespaces.filter(
|
|
143
|
+
(ns) => ns.id !== namespaceIdToDelete
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
await w(`kv:namespace delete --namespace-id ${namespaceIdToDelete}`);
|
|
148
|
+
expect(KVNamespaces.find((ns) => ns.id === namespaceIdToDelete)).toBe(
|
|
149
|
+
undefined
|
|
91
150
|
);
|
|
92
|
-
console.log(stdout);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
describe("kv:key", () => {
|
|
96
|
-
let namespaceId: string;
|
|
97
|
-
beforeAll(async () => {
|
|
98
|
-
await w('kv:namespace create "UnitTestNamespace"');
|
|
99
|
-
namespaceId = JSON.parse((await w("kv:namespace list")).stdout).find(
|
|
100
|
-
(ns) => ns.title === "worker-UnitTestNamespace"
|
|
101
|
-
).id;
|
|
102
|
-
});
|
|
103
|
-
afterAll(async () => {
|
|
104
|
-
await w(`kv:namespace delete --namespace-id ${namespaceId}`);
|
|
105
151
|
});
|
|
106
|
-
it("stub", () => {});
|
|
107
|
-
// it("can add a key", () => {});
|
|
108
|
-
// it("can read a key", () => {});
|
|
109
|
-
// it("can list keys", () => {});
|
|
110
|
-
// it("can delete a key", () => {});
|
|
111
152
|
});
|
|
112
|
-
|
|
113
|
-
// describe("login", () => {
|
|
114
|
-
// it("should show possible scopes", async () => {
|
|
115
|
-
// const { stdout } = await w("login --scopes-list");
|
|
116
|
-
// // UGH jest doesn't like wide text huh
|
|
117
|
-
// expect(stdout).toMatchInlineSnapshot();
|
|
118
|
-
// });
|
|
119
|
-
// });
|
|
120
|
-
|
|
121
|
-
// TODO: setup a "api server" to mock requests
|
|
122
153
|
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// This file mocks ../cfetch.ts
|
|
2
|
+
// so we can insert whatever responses we want from it
|
|
3
|
+
|
|
4
|
+
const pathToRegexp = require("path-to-regexp");
|
|
5
|
+
// TODO: add jsdoc style types here
|
|
6
|
+
|
|
7
|
+
// type MockHandler = (resource: string, init?: RequestInit) => any; // TODO: use a generic here
|
|
8
|
+
|
|
9
|
+
let mocks = [];
|
|
10
|
+
|
|
11
|
+
function mockCfetch(resource, init) {
|
|
12
|
+
for (const { regexp, handler } of mocks) {
|
|
13
|
+
if (regexp.test(resource)) {
|
|
14
|
+
return handler(resource, init); // should we have some kind of fallthrough system? we'll see.
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`no mocks found for ${resource}`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function setMock(resource, handler) {
|
|
21
|
+
const mock = {
|
|
22
|
+
resource,
|
|
23
|
+
handler,
|
|
24
|
+
regexp: pathToRegexp(resource),
|
|
25
|
+
};
|
|
26
|
+
mocks.push(mock);
|
|
27
|
+
return () => {
|
|
28
|
+
mocks = mocks.filter((x) => x !== mock);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function unsetAllMocks() {
|
|
33
|
+
mocks = [];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const CF_API_BASE_URL =
|
|
37
|
+
process.env.CF_API_BASE_URL || "https://api.cloudflare.com/client/v4";
|
|
38
|
+
|
|
39
|
+
Object.assign(module.exports, {
|
|
40
|
+
__esModule: true,
|
|
41
|
+
default: mockCfetch,
|
|
42
|
+
mockCfetch,
|
|
43
|
+
setMock,
|
|
44
|
+
unsetAllMocks,
|
|
45
|
+
CF_API_BASE_URL,
|
|
46
|
+
});
|
package/src/api/form_data.ts
CHANGED
|
@@ -3,8 +3,6 @@ import type {
|
|
|
3
3
|
CfModuleType,
|
|
4
4
|
CfVariable,
|
|
5
5
|
CfModule,
|
|
6
|
-
CfKvNamespace,
|
|
7
|
-
CfCryptoKey,
|
|
8
6
|
} from "./worker.js";
|
|
9
7
|
import { FormData, Blob } from "formdata-node";
|
|
10
8
|
|
|
@@ -27,12 +25,26 @@ function toBinding(
|
|
|
27
25
|
return { name, type: "plain_text", text: variable };
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
if ("namespaceId" in variable) {
|
|
29
|
+
return {
|
|
30
|
+
name,
|
|
31
|
+
type: "kv_namespace",
|
|
32
|
+
namespace_id: variable.namespaceId,
|
|
33
|
+
};
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
if ("class_name" in variable) {
|
|
37
|
+
return {
|
|
38
|
+
name,
|
|
39
|
+
type: "durable_object_namespace",
|
|
40
|
+
class_name: variable.class_name,
|
|
41
|
+
...(variable.script_name && {
|
|
42
|
+
script_name: variable.script_name,
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { format, algorithm, usages, data } = variable;
|
|
36
48
|
if (format) {
|
|
37
49
|
let key_base64;
|
|
38
50
|
let key_jwk;
|
|
@@ -84,7 +96,15 @@ function toModule(module: CfModule, entryType?: CfModuleType): Blob {
|
|
|
84
96
|
*/
|
|
85
97
|
export function toFormData(worker: CfWorkerInit): FormData {
|
|
86
98
|
const formData = new FormData();
|
|
87
|
-
const {
|
|
99
|
+
const {
|
|
100
|
+
main,
|
|
101
|
+
modules,
|
|
102
|
+
variables,
|
|
103
|
+
migrations,
|
|
104
|
+
usage_model,
|
|
105
|
+
compatibility_date,
|
|
106
|
+
compatibility_flags,
|
|
107
|
+
} = worker;
|
|
88
108
|
const { name, type: mainType } = main;
|
|
89
109
|
|
|
90
110
|
const bindings = [];
|
|
@@ -93,23 +113,19 @@ export function toFormData(worker: CfWorkerInit): FormData {
|
|
|
93
113
|
bindings.push(binding);
|
|
94
114
|
}
|
|
95
115
|
|
|
96
|
-
|
|
116
|
+
// TODO: this object should be typed
|
|
97
117
|
const metadata = {
|
|
98
|
-
main_module:
|
|
99
|
-
body_part: singleton ? name : undefined,
|
|
118
|
+
...(mainType !== "commonjs" ? { main_module: name } : { body_part: name }),
|
|
100
119
|
bindings,
|
|
120
|
+
...(compatibility_date && { compatibility_date }),
|
|
121
|
+
...(compatibility_flags && { compatibility_flags }),
|
|
122
|
+
...(usage_model && { usage_model }),
|
|
123
|
+
...(migrations && { migrations }),
|
|
101
124
|
};
|
|
102
|
-
|
|
103
|
-
// @ts-expect-error - we should type metadata
|
|
104
|
-
metadata.compatibility_date = compatibility_date;
|
|
105
|
-
}
|
|
106
|
-
if (usage_model) {
|
|
107
|
-
// @ts-expect-error - we should type metadata
|
|
108
|
-
metadata.usage_model = usage_model;
|
|
109
|
-
}
|
|
125
|
+
|
|
110
126
|
formData.set("metadata", JSON.stringify(metadata));
|
|
111
127
|
|
|
112
|
-
if (
|
|
128
|
+
if (mainType === "commonjs" && modules && modules.length > 0) {
|
|
113
129
|
throw new TypeError(
|
|
114
130
|
"More than one module can only be specified when type = 'esm'"
|
|
115
131
|
);
|
package/src/api/preview.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import cfetch from "../cfetch";
|
|
2
|
-
import { fetchJson } from "../util/fetch";
|
|
3
2
|
import { toFormData } from "./form_data";
|
|
4
3
|
import type { CfAccount, CfWorkerInit } from "./worker";
|
|
4
|
+
import fetch from "node-fetch";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* A preview mode.
|
|
@@ -9,7 +9,7 @@ import type { CfAccount, CfWorkerInit } from "./worker";
|
|
|
9
9
|
* * If true, then using a `workers.dev` subdomain.
|
|
10
10
|
* * Otherwise, a list of routes under a single zone.
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* A preview token.
|
|
@@ -60,17 +60,17 @@ async function sessionToken(account: CfAccount): Promise<CfPreviewToken> {
|
|
|
60
60
|
? `/zones/${zoneId}/workers/edge-preview`
|
|
61
61
|
: `/accounts/${accountId}/workers/subdomain/edge-preview`;
|
|
62
62
|
|
|
63
|
-
const { exchange_url
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const { host } = new URL(
|
|
63
|
+
const { exchange_url } = await cfetch<{ 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
68
|
const query = `cf_workers_preview_token=${token}`;
|
|
69
69
|
|
|
70
70
|
return {
|
|
71
71
|
value: token,
|
|
72
72
|
host,
|
|
73
|
-
inspectorUrl: new URL(`${
|
|
73
|
+
inspectorUrl: new URL(`${inspector_websocket}?${query}`),
|
|
74
74
|
prewarmUrl: new URL(
|
|
75
75
|
`https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
|
|
76
76
|
),
|
|
@@ -96,27 +96,32 @@ export async function previewToken(
|
|
|
96
96
|
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(account);
|
|
97
97
|
|
|
98
98
|
const { accountId, zoneId } = account;
|
|
99
|
-
const scriptId = zoneId ? randomId() : host.split(".")[0];
|
|
99
|
+
const scriptId = zoneId ? randomId() : worker.name || host.split(".")[0];
|
|
100
100
|
const url = `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
|
|
101
101
|
|
|
102
|
-
const mode = zoneId
|
|
102
|
+
const mode: CfPreviewMode = zoneId
|
|
103
|
+
? { routes: ["*/*"] }
|
|
104
|
+
: { workers_dev: true };
|
|
105
|
+
|
|
103
106
|
const formData = toFormData(worker);
|
|
104
107
|
formData.set("wrangler-session-config", JSON.stringify(mode));
|
|
105
108
|
|
|
106
|
-
const { preview_token
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
);
|
|
109
|
+
const { preview_token } = await cfetch<{ 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
|
+
|
|
117
118
|
return {
|
|
118
|
-
value:
|
|
119
|
-
|
|
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,
|
|
120
125
|
inspectorUrl,
|
|
121
126
|
prewarmUrl,
|
|
122
127
|
};
|
package/src/api/worker.ts
CHANGED
|
@@ -72,6 +72,21 @@ export interface CfKvNamespace {
|
|
|
72
72
|
namespaceId: string;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
export interface CfDurableObject {
|
|
76
|
+
class_name: string;
|
|
77
|
+
script_name?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface CfDOMigrations {
|
|
81
|
+
old_tag?: string;
|
|
82
|
+
new_tag: string;
|
|
83
|
+
steps: {
|
|
84
|
+
new_classes?: string[];
|
|
85
|
+
renamed_classes?: string[];
|
|
86
|
+
deleted_classes?: string[];
|
|
87
|
+
}[];
|
|
88
|
+
}
|
|
89
|
+
|
|
75
90
|
/**
|
|
76
91
|
* A `WebCrypto` key.
|
|
77
92
|
*
|
|
@@ -99,12 +114,16 @@ export interface CfCryptoKey {
|
|
|
99
114
|
/**
|
|
100
115
|
* A variable (aka. environment variable).
|
|
101
116
|
*/
|
|
102
|
-
export type CfVariable = string | CfKvNamespace | CfCryptoKey;
|
|
117
|
+
export type CfVariable = string | CfKvNamespace | CfCryptoKey | CfDurableObject;
|
|
103
118
|
|
|
104
119
|
/**
|
|
105
120
|
* Options for creating a `CfWorker`.
|
|
106
121
|
*/
|
|
107
122
|
export interface CfWorkerInit {
|
|
123
|
+
/**
|
|
124
|
+
* The name of the worker.
|
|
125
|
+
*/
|
|
126
|
+
name: string | void;
|
|
108
127
|
/**
|
|
109
128
|
* The entrypoint module.
|
|
110
129
|
*/
|
|
@@ -117,8 +136,10 @@ export interface CfWorkerInit {
|
|
|
117
136
|
* The map of names to variables. (aka. environment variables)
|
|
118
137
|
*/
|
|
119
138
|
variables?: { [name: string]: CfVariable };
|
|
120
|
-
|
|
121
|
-
|
|
139
|
+
migrations: void | CfDOMigrations;
|
|
140
|
+
compatibility_date: string | void;
|
|
141
|
+
compatibility_flags: void | string[];
|
|
142
|
+
usage_model: void | "bundled" | "unbound";
|
|
122
143
|
}
|
|
123
144
|
|
|
124
145
|
/**
|