wrangler 0.0.0-e61907d → 0.0.0-ece06ea
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 +36 -2
- package/import_meta_url.js +3 -0
- package/package.json +49 -16
- package/src/__tests__/clipboardy-mock.js +4 -0
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/index.test.ts +153 -0
- package/src/__tests__/mock-cfetch.js +46 -0
- package/src/api/form_data.ts +158 -0
- package/src/api/inspect.ts +441 -0
- package/src/api/preview.ts +123 -0
- package/src/api/worker.ts +161 -0
- package/src/cfetch.ts +72 -0
- package/src/cli.ts +10 -0
- package/src/config.ts +124 -0
- package/src/dev.tsx +736 -0
- package/src/dialogs.tsx +85 -0
- package/src/index.tsx +1845 -0
- package/src/kv.tsx +211 -0
- package/src/pages.tsx +344 -0
- package/src/publish.ts +354 -0
- package/src/sites.tsx +115 -0
- package/src/tail.tsx +71 -0
- package/src/user.tsx +1029 -0
- package/src/util/fetch.ts +74 -0
- package/wrangler-dist/{index_node.js → cli.js} +55431 -20739
- package/wrangler-dist/cli.js.map +7 -0
- package/wrangler-dist/index_node.js.map +0 -7
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
|
@@ -1,2 +1,36 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
require("
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { spawn } = require("child_process");
|
|
3
|
+
const { join } = require("path");
|
|
4
|
+
const semiver = require("semiver");
|
|
5
|
+
|
|
6
|
+
const MIN_NODE_VERSION = "16.7.0";
|
|
7
|
+
|
|
8
|
+
async function main() {
|
|
9
|
+
if (semiver(process.versions.node, MIN_NODE_VERSION) < 0) {
|
|
10
|
+
// Note Volta and nvm are also recommended in the official docs:
|
|
11
|
+
// https://developers.cloudflare.com/workers/get-started/guide#2-install-the-workers-cli
|
|
12
|
+
console.error(
|
|
13
|
+
`Wrangler requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}.
|
|
14
|
+
You should use the latest Node.js version if possible, as Cloudflare Workers use a very up-to-date version of V8.
|
|
15
|
+
Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.`
|
|
16
|
+
);
|
|
17
|
+
process.exitCode = 1;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
spawn(
|
|
22
|
+
process.execPath,
|
|
23
|
+
[
|
|
24
|
+
"--no-warnings",
|
|
25
|
+
"--experimental-vm-modules",
|
|
26
|
+
...process.execArgv,
|
|
27
|
+
join(__dirname, "../wrangler-dist/cli.js"),
|
|
28
|
+
...process.argv.slice(2),
|
|
29
|
+
],
|
|
30
|
+
{ stdio: "inherit" }
|
|
31
|
+
).on("exit", (code) =>
|
|
32
|
+
process.exit(code === undefined || code === null ? 0 : code)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
void main();
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrangler",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-ece06ea",
|
|
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,9 +36,13 @@
|
|
|
33
36
|
"cli"
|
|
34
37
|
],
|
|
35
38
|
"dependencies": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
+
"@cloudflare/pages-functions-compiler": "0.3.7",
|
|
40
|
+
"esbuild": "0.14.1",
|
|
41
|
+
"miniflare": "2.0.0-rc.2",
|
|
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"
|
|
@@ -43,42 +50,68 @@
|
|
|
43
50
|
"devDependencies": {
|
|
44
51
|
"@iarna/toml": "^2.2.5",
|
|
45
52
|
"@types/cloudflare": "^2.7.6",
|
|
46
|
-
"@types/
|
|
53
|
+
"@types/express": "^4.17.13",
|
|
54
|
+
"@types/react": "^17.0.37",
|
|
47
55
|
"@types/signal-exit": "^3.0.1",
|
|
48
|
-
"@types/ws": "^8.2.
|
|
49
|
-
"@types/yargs": "^17.0.
|
|
56
|
+
"@types/ws": "^8.2.1",
|
|
57
|
+
"@types/yargs": "^17.0.7",
|
|
50
58
|
"clipboardy": "^3.0.0",
|
|
51
|
-
"cloudflare": "^2.9.1",
|
|
52
59
|
"command-exists": "^1.2.9",
|
|
60
|
+
"execa": "^6.0.0",
|
|
61
|
+
"express": "^4.17.1",
|
|
53
62
|
"finalhandler": "^1.1.2",
|
|
54
63
|
"find-up": "^6.2.0",
|
|
55
64
|
"formdata-node": "^4.3.1",
|
|
56
65
|
"http-proxy": "^1.18.1",
|
|
66
|
+
"http-proxy-middleware": "^2.0.1",
|
|
57
67
|
"ink": "^3.2.0",
|
|
68
|
+
"ink-select-input": "^4.2.1",
|
|
58
69
|
"ink-table": "^3.0.0",
|
|
59
|
-
"ink-text-input": "^4.0.
|
|
60
|
-
"node-fetch": "
|
|
70
|
+
"ink-text-input": "^4.0.2",
|
|
71
|
+
"node-fetch": "^3.1.0",
|
|
61
72
|
"open": "^8.4.0",
|
|
73
|
+
"path-to-regexp": "^6.2.0",
|
|
62
74
|
"react": "^17.0.2",
|
|
63
75
|
"serve-static": "^1.14.1",
|
|
64
|
-
"signal-exit": "^3.0.
|
|
76
|
+
"signal-exit": "^3.0.6",
|
|
65
77
|
"tmp-promise": "^3.0.3",
|
|
66
|
-
"ws": "^8.
|
|
78
|
+
"ws": "^8.3.0",
|
|
79
|
+
"yargs": "^17.3.0"
|
|
67
80
|
},
|
|
68
81
|
"files": [
|
|
82
|
+
"src",
|
|
69
83
|
"bin",
|
|
70
84
|
"miniflare-config-stubs",
|
|
71
85
|
"wrangler-dist",
|
|
72
86
|
"static-asset-facade.js",
|
|
73
|
-
"vendor"
|
|
87
|
+
"vendor",
|
|
88
|
+
"import_meta_url.js"
|
|
74
89
|
],
|
|
75
90
|
"scripts": {
|
|
76
91
|
"clean": "rm -rf wrangler-dist",
|
|
77
92
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
78
93
|
"build": "npm run clean && npm run bundle",
|
|
79
|
-
"start": "npm run bundle && ./bin/wrangler.js"
|
|
94
|
+
"start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
95
|
+
"test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true"
|
|
80
96
|
},
|
|
81
97
|
"engines": {
|
|
82
|
-
"node": ">=16.
|
|
98
|
+
"node": ">=16.7.0"
|
|
99
|
+
},
|
|
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
|
+
},
|
|
108
|
+
"transform": {
|
|
109
|
+
"^.+\\.c?(t|j)sx?$": [
|
|
110
|
+
"esbuild-jest",
|
|
111
|
+
{
|
|
112
|
+
"sourcemap": true
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
}
|
|
83
116
|
}
|
|
84
117
|
}
|
|
File without changes
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as fsp from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
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";
|
|
8
|
+
|
|
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;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("wrangler", () => {
|
|
47
|
+
it("should run", async () => {
|
|
48
|
+
const { stdout } = await w(undefined, { tap: true });
|
|
49
|
+
|
|
50
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
51
|
+
"wrangler
|
|
52
|
+
|
|
53
|
+
Commands:
|
|
54
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
55
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
56
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
57
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
58
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
59
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
60
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
61
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
62
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
63
|
+
|
|
64
|
+
Flags:
|
|
65
|
+
--config Path to .toml configuration file [string]
|
|
66
|
+
--help Show help [boolean]
|
|
67
|
+
--version Show version number [boolean]
|
|
68
|
+
|
|
69
|
+
Options:
|
|
70
|
+
--local Run on my machine [boolean] [default: false]"
|
|
71
|
+
`);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("init", () => {
|
|
75
|
+
const ogcwd = process.cwd();
|
|
76
|
+
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
process.chdir(path.join(__dirname, "fixtures", "init"));
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
afterEach(async () => {
|
|
82
|
+
await fsp.rm("./wrangler.toml");
|
|
83
|
+
process.chdir(ogcwd);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should create a wrangler.toml", async () => {
|
|
87
|
+
await w("init");
|
|
88
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
89
|
+
expect(typeof parsed.compatibility_date).toBe("string");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("should error when wrangler.toml already exists", async () => {
|
|
93
|
+
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
94
|
+
const { stderr } = await w("init", { tap: true });
|
|
95
|
+
expect(stderr.endsWith("wrangler.toml already exists.")).toBe(true);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("kv:namespace", () => {
|
|
100
|
+
afterAll(() => {
|
|
101
|
+
unsetAllMocks();
|
|
102
|
+
});
|
|
103
|
+
let KVNamespaces: { title: string; id: string }[] = [];
|
|
104
|
+
it("can create a namespace", async () => {
|
|
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();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
let createdNamespace: { id: string; title: string };
|
|
120
|
+
it("can list namespaces", async () => {
|
|
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 });
|
|
129
|
+
const namespaces = JSON.parse(stdout);
|
|
130
|
+
createdNamespace = namespaces.find(
|
|
131
|
+
(ns) => ns.title === "worker-UnitTestNamespace"
|
|
132
|
+
);
|
|
133
|
+
expect(createdNamespace.title).toBe("worker-UnitTestNamespace");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("can delete a namespace", async () => {
|
|
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
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
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
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CfWorkerInit,
|
|
3
|
+
CfModuleType,
|
|
4
|
+
CfVariable,
|
|
5
|
+
CfModule,
|
|
6
|
+
} from "./worker.js";
|
|
7
|
+
import { FormData, Blob } from "formdata-node";
|
|
8
|
+
|
|
9
|
+
// Credit: https://stackoverflow.com/a/9458996
|
|
10
|
+
function toBase64(source: BufferSource): string {
|
|
11
|
+
let result = "";
|
|
12
|
+
const buffer = source instanceof ArrayBuffer ? source : source.buffer;
|
|
13
|
+
const bytes = new Uint8Array(buffer);
|
|
14
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
15
|
+
result += String.fromCharCode(bytes[i]);
|
|
16
|
+
}
|
|
17
|
+
return btoa(result);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function toBinding(
|
|
21
|
+
name: string,
|
|
22
|
+
variable: CfVariable
|
|
23
|
+
): Record<string, unknown> {
|
|
24
|
+
if (typeof variable === "string") {
|
|
25
|
+
return { name, type: "plain_text", text: variable };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if ("namespaceId" in variable) {
|
|
29
|
+
return {
|
|
30
|
+
name,
|
|
31
|
+
type: "kv_namespace",
|
|
32
|
+
namespace_id: variable.namespaceId,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
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;
|
|
48
|
+
if (format) {
|
|
49
|
+
let key_base64;
|
|
50
|
+
let key_jwk;
|
|
51
|
+
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
52
|
+
key_base64 = toBase64(data);
|
|
53
|
+
} else {
|
|
54
|
+
key_jwk = data;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
name,
|
|
58
|
+
type: "secret_key",
|
|
59
|
+
format,
|
|
60
|
+
algorithm,
|
|
61
|
+
usages,
|
|
62
|
+
key_base64,
|
|
63
|
+
key_jwk,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw new TypeError("Unsupported variable: " + variable);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function toMimeType(type: CfModuleType): string {
|
|
71
|
+
switch (type) {
|
|
72
|
+
case "esm":
|
|
73
|
+
return "application/javascript+module";
|
|
74
|
+
case "commonjs":
|
|
75
|
+
return "application/javascript";
|
|
76
|
+
case "compiled-wasm":
|
|
77
|
+
return "application/wasm";
|
|
78
|
+
case "buffer":
|
|
79
|
+
return "application/octet-stream";
|
|
80
|
+
case "text":
|
|
81
|
+
return "text/plain";
|
|
82
|
+
default:
|
|
83
|
+
throw new TypeError("Unsupported module: " + type);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function toModule(module: CfModule, entryType?: CfModuleType): Blob {
|
|
88
|
+
const { type: moduleType, content } = module;
|
|
89
|
+
const type = toMimeType(moduleType ?? entryType);
|
|
90
|
+
|
|
91
|
+
return new Blob([content], { type });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a `FormData` upload from a `CfWorkerInit`.
|
|
96
|
+
*/
|
|
97
|
+
export function toFormData(worker: CfWorkerInit): FormData {
|
|
98
|
+
const formData = new FormData();
|
|
99
|
+
const {
|
|
100
|
+
main,
|
|
101
|
+
modules,
|
|
102
|
+
variables,
|
|
103
|
+
migrations,
|
|
104
|
+
usage_model,
|
|
105
|
+
compatibility_date,
|
|
106
|
+
compatibility_flags,
|
|
107
|
+
} = worker;
|
|
108
|
+
const { name, type: mainType } = main;
|
|
109
|
+
|
|
110
|
+
const bindings = [];
|
|
111
|
+
for (const [name, variable] of Object.entries(variables ?? {})) {
|
|
112
|
+
const binding = toBinding(name, variable);
|
|
113
|
+
bindings.push(binding);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const metadata =
|
|
117
|
+
mainType !== "commonjs"
|
|
118
|
+
? {
|
|
119
|
+
main_module: name,
|
|
120
|
+
bindings,
|
|
121
|
+
}
|
|
122
|
+
: {
|
|
123
|
+
body_part: name,
|
|
124
|
+
bindings,
|
|
125
|
+
};
|
|
126
|
+
if (compatibility_date) {
|
|
127
|
+
// @ts-expect-error - we should type metadata
|
|
128
|
+
metadata.compatibility_date = compatibility_date;
|
|
129
|
+
}
|
|
130
|
+
if (compatibility_flags) {
|
|
131
|
+
// @ts-expect-error - we should type metadata
|
|
132
|
+
metadata.compatibility_flags = compatibility_flags;
|
|
133
|
+
}
|
|
134
|
+
if (usage_model) {
|
|
135
|
+
// @ts-expect-error - we should type metadata
|
|
136
|
+
metadata.usage_model = usage_model;
|
|
137
|
+
}
|
|
138
|
+
if (migrations) {
|
|
139
|
+
// @ts-expect-error - we should type metadata
|
|
140
|
+
metadata.migrations = migrations;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
formData.set("metadata", JSON.stringify(metadata));
|
|
144
|
+
|
|
145
|
+
if (mainType === "commonjs" && modules && modules.length > 0) {
|
|
146
|
+
throw new TypeError(
|
|
147
|
+
"More than one module can only be specified when type = 'esm'"
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
for (const module of [main].concat(modules || [])) {
|
|
152
|
+
const { name } = module;
|
|
153
|
+
const blob = toModule(module, mainType ?? "esm");
|
|
154
|
+
formData.set(name, blob, name);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return formData;
|
|
158
|
+
}
|