wrangler 0.0.0-ece06ea → 0.0.0-ecef68635
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 +50 -15
- package/bin/wrangler.js +94 -31
- package/config-schema.json +3074 -0
- package/kv-asset-handler.js +1 -0
- package/package.json +155 -75
- 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-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 +26463 -0
- package/wrangler-dist/cli.js +206335 -125492
- package/wrangler-dist/wasm-sync.wasm +0 -0
- 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/src/__tests__/clipboardy-mock.js +0 -4
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/index.test.ts +0 -153
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/form_data.ts +0 -158
- package/src/api/inspect.ts +0 -441
- package/src/api/preview.ts +0 -123
- package/src/api/worker.ts +0 -161
- package/src/cfetch.ts +0 -72
- package/src/cli.ts +0 -10
- package/src/config.ts +0 -124
- package/src/dev.tsx +0 -736
- package/src/dialogs.tsx +0 -85
- package/src/index.tsx +0 -1845
- package/src/kv.tsx +0 -211
- package/src/pages.tsx +0 -344
- package/src/publish.ts +0 -354
- package/src/sites.tsx +0 -115
- package/src/tail.tsx +0 -71
- package/src/user.tsx +0 -1029
- package/src/util/fetch.ts +0 -74
- 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
package/src/kv.tsx
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import type { Config } from "./config";
|
|
2
|
-
import cfetch from "./cfetch";
|
|
3
|
-
import qs from "node:querystring";
|
|
4
|
-
|
|
5
|
-
type KvArgs = {
|
|
6
|
-
binding?: string;
|
|
7
|
-
"namespace-id"?: string;
|
|
8
|
-
env?: string;
|
|
9
|
-
preview?: boolean;
|
|
10
|
-
config?: Config;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export async function listNamespaces(accountId: string) {
|
|
14
|
-
let page = 1,
|
|
15
|
-
done = false,
|
|
16
|
-
results = [];
|
|
17
|
-
while (!(done || results.length % 100 !== 0)) {
|
|
18
|
-
const json = await cfetch<
|
|
19
|
-
{ id: string; title: string; supports_url_encoding: boolean }[]
|
|
20
|
-
>(
|
|
21
|
-
`/accounts/${accountId}/storage/kv/namespaces?per_page=100&order=title&direction=asc&page=${page}`
|
|
22
|
-
);
|
|
23
|
-
page++;
|
|
24
|
-
results = [...results, ...json];
|
|
25
|
-
if (json.length === 0) {
|
|
26
|
-
done = true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return results;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export async function listNamespaceKeys(
|
|
33
|
-
accountId: string,
|
|
34
|
-
namespaceId: string,
|
|
35
|
-
prefix?: string,
|
|
36
|
-
limit?: number
|
|
37
|
-
) {
|
|
38
|
-
// TODO: this doesn't appear to do pagination
|
|
39
|
-
return await cfetch<
|
|
40
|
-
{ name: string; expiration: number; metadata: { [key: string]: unknown } }[]
|
|
41
|
-
>(
|
|
42
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys?${qs.stringify(
|
|
43
|
-
{ prefix, limit }
|
|
44
|
-
)}`
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function putKeyValue(
|
|
49
|
-
accountId: string,
|
|
50
|
-
namespaceId: string,
|
|
51
|
-
key: string,
|
|
52
|
-
value: string,
|
|
53
|
-
args?: { expiration?: number; expiration_ttl?: number }
|
|
54
|
-
) {
|
|
55
|
-
return await cfetch(
|
|
56
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}?${
|
|
57
|
-
args
|
|
58
|
-
? qs.stringify({
|
|
59
|
-
expiration: args.expiration,
|
|
60
|
-
expiration_ttl: args.expiration_ttl,
|
|
61
|
-
})
|
|
62
|
-
: ""
|
|
63
|
-
}`,
|
|
64
|
-
{ method: "PUT", body: value }
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export async function putBulkKeyValue(
|
|
69
|
-
accountId: string,
|
|
70
|
-
namespaceId: string,
|
|
71
|
-
keyvalueStr: string
|
|
72
|
-
) {
|
|
73
|
-
return await cfetch(
|
|
74
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
75
|
-
{
|
|
76
|
-
method: "PUT",
|
|
77
|
-
body: keyvalueStr,
|
|
78
|
-
headers: { "Content-Type": "application/json" },
|
|
79
|
-
}
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export async function deleteBulkKeyValue(
|
|
84
|
-
accountId: string,
|
|
85
|
-
namespaceId: string,
|
|
86
|
-
keyStr: string
|
|
87
|
-
) {
|
|
88
|
-
return await cfetch(
|
|
89
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
90
|
-
{
|
|
91
|
-
method: "DELETE",
|
|
92
|
-
body: keyStr,
|
|
93
|
-
headers: { "Content-Type": "application/json" },
|
|
94
|
-
}
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function getNamespaceId({
|
|
99
|
-
preview,
|
|
100
|
-
binding,
|
|
101
|
-
config,
|
|
102
|
-
"namespace-id": namespaceId,
|
|
103
|
-
env,
|
|
104
|
-
}: KvArgs): string {
|
|
105
|
-
// nice
|
|
106
|
-
if (namespaceId) {
|
|
107
|
-
return namespaceId;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// begin pre-flight checks
|
|
111
|
-
|
|
112
|
-
// `--binding` is only valid if there's a wrangler configuration file.
|
|
113
|
-
if (binding && !config) {
|
|
114
|
-
throw new Error("--binding specified, but no config file was found.");
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// there's no config. abort here
|
|
118
|
-
if (!config) {
|
|
119
|
-
throw new Error(
|
|
120
|
-
"Failed to find a config file.\n" +
|
|
121
|
-
"Either use --namespace to upload directly or create a configuration file with a binding."
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// they want to use an environment, actually
|
|
126
|
-
if (env) {
|
|
127
|
-
if (!config.env || !config.env[env]) {
|
|
128
|
-
throw new Error(
|
|
129
|
-
`Failed to find environment "${env}" in configuration file!`
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// TODO: either a bespoke arg type for this function to avoid undefineds or a EnvOrConfig type
|
|
134
|
-
return getNamespaceId({
|
|
135
|
-
binding,
|
|
136
|
-
"namespace-id": namespaceId,
|
|
137
|
-
env: undefined,
|
|
138
|
-
preview,
|
|
139
|
-
config: {
|
|
140
|
-
env: undefined,
|
|
141
|
-
build: undefined,
|
|
142
|
-
name: undefined,
|
|
143
|
-
account_id: undefined,
|
|
144
|
-
...config.env[env],
|
|
145
|
-
},
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// there's no KV namespaces
|
|
150
|
-
if (!config.kv_namespaces || config.kv_namespaces.length === 0) {
|
|
151
|
-
throw new Error(
|
|
152
|
-
"No KV Namespace to upload to! Either use --namespace to upload directly or add a KV namespace to your wrangler config file."
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const namespace = config.kv_namespaces.find(
|
|
157
|
-
(namespace) => namespace.binding === binding
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
// we couldn't find a namespace with that binding
|
|
161
|
-
if (!namespace) {
|
|
162
|
-
throw new Error(`No KV Namespaces found with binding ${binding}!`);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// end pre-flight checks
|
|
166
|
-
|
|
167
|
-
// we're in preview mode, `--preview true` or `--preivew` was passed
|
|
168
|
-
if (preview && namespace.preview_id) {
|
|
169
|
-
namespaceId = namespace.preview_id;
|
|
170
|
-
} else {
|
|
171
|
-
throw new Error(
|
|
172
|
-
`No preview ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// either `--preview false`, or preview wasn't passed
|
|
177
|
-
// TODO: should we care? or should we just treat false and undefined the same
|
|
178
|
-
const previewIsDefined = typeof preview !== "undefined";
|
|
179
|
-
|
|
180
|
-
// --preview false was passed
|
|
181
|
-
if (previewIsDefined && namespace.id) {
|
|
182
|
-
namespaceId = namespace.id;
|
|
183
|
-
} else {
|
|
184
|
-
throw new Error(
|
|
185
|
-
`No namespace ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// `--preview` wasn't passed
|
|
190
|
-
const bindingHasOnlyOneId =
|
|
191
|
-
(namespace.id && !namespace.preview_id) ||
|
|
192
|
-
(!namespace.id && namespace.preview_id);
|
|
193
|
-
if (bindingHasOnlyOneId) {
|
|
194
|
-
namespaceId = namespace.id || namespace.preview_id;
|
|
195
|
-
} else {
|
|
196
|
-
throw new Error(
|
|
197
|
-
`${binding} has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.`
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// shouldn't happen. we should be able to prove this with strong typing.
|
|
202
|
-
// TODO: when we add strongly typed commands, rewrite these checks so they're exhaustive
|
|
203
|
-
if (!namespaceId) {
|
|
204
|
-
throw Error(
|
|
205
|
-
"Something went wrong trying to determine which namespace to upload to.\n" +
|
|
206
|
-
"Please create a github issue with the command you just ran along with your wrangler configuration."
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return namespaceId;
|
|
211
|
-
}
|
package/src/pages.tsx
DELETED
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
import type { BuilderCallback } from "yargs";
|
|
2
|
-
import { join } from "path";
|
|
3
|
-
import { tmpdir } from "os";
|
|
4
|
-
import { existsSync } from "fs";
|
|
5
|
-
import type { ChildProcess } from "child_process";
|
|
6
|
-
import { execSync, spawn } from "child_process";
|
|
7
|
-
import express from "express";
|
|
8
|
-
import type { MiniflareOptions } from "miniflare";
|
|
9
|
-
import type { RequestInfo, RequestInit } from "@miniflare/core";
|
|
10
|
-
import httpProxyMiddleware from "http-proxy-middleware";
|
|
11
|
-
|
|
12
|
-
type Exit = (message?: string) => undefined;
|
|
13
|
-
|
|
14
|
-
const isWindows = () => process.platform === "win32";
|
|
15
|
-
|
|
16
|
-
const SECONDS_TO_WAIT_FOR_PROXY = 5;
|
|
17
|
-
|
|
18
|
-
const sleep = async (ms: number) =>
|
|
19
|
-
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
20
|
-
|
|
21
|
-
const getPids = (pid: number) => {
|
|
22
|
-
const pids: number[] = [pid];
|
|
23
|
-
let command: string, regExp: RegExp;
|
|
24
|
-
|
|
25
|
-
if (isWindows()) {
|
|
26
|
-
command = `wmic process where (ParentProcessId=${pid}) get ProcessId`;
|
|
27
|
-
regExp = new RegExp(/(\d+)/);
|
|
28
|
-
} else {
|
|
29
|
-
command = `pgrep -P ${pid}`;
|
|
30
|
-
regExp = new RegExp(/(\d+)/);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
const newPids = (
|
|
35
|
-
execSync(command)
|
|
36
|
-
.toString()
|
|
37
|
-
.split("\n")
|
|
38
|
-
.map((line) => line.match(regExp))
|
|
39
|
-
.filter((line) => line !== null) as RegExpExecArray[]
|
|
40
|
-
).map((match) => parseInt(match[1]));
|
|
41
|
-
|
|
42
|
-
pids.push(...newPids.map(getPids).flat());
|
|
43
|
-
} catch {}
|
|
44
|
-
|
|
45
|
-
return pids;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const getPort = (pid: number) => {
|
|
49
|
-
let command: string, regExp: RegExp;
|
|
50
|
-
|
|
51
|
-
if (isWindows()) {
|
|
52
|
-
command = "\\windows\\system32\\netstat.exe -nao";
|
|
53
|
-
regExp = new RegExp(`TCP\\s+.*:(\\d+)\\s+.*:\\d+\\s+LISTENING\\s+${pid}`);
|
|
54
|
-
} else {
|
|
55
|
-
command = "lsof -nPi";
|
|
56
|
-
regExp = new RegExp(`${pid}\\s+.*TCP\\s+.*:(\\d+)\\s+\\(LISTEN\\)`);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
const matches = execSync(command)
|
|
61
|
-
.toString()
|
|
62
|
-
.split("\n")
|
|
63
|
-
.map((line) => line.match(regExp))
|
|
64
|
-
.filter((line) => line !== null) as RegExpExecArray[];
|
|
65
|
-
|
|
66
|
-
const match = matches[0];
|
|
67
|
-
if (match) return parseInt(match[1]);
|
|
68
|
-
} catch (thrown) {
|
|
69
|
-
console.error(
|
|
70
|
-
`Error scanning for ports of process with PID ${pid}: ${thrown}`
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const spawnProxyProcess = async ({
|
|
76
|
-
directory,
|
|
77
|
-
port,
|
|
78
|
-
remaining,
|
|
79
|
-
}: {
|
|
80
|
-
directory?: string;
|
|
81
|
-
port?: number;
|
|
82
|
-
remaining: (string | number)[];
|
|
83
|
-
}) => {
|
|
84
|
-
let proxy: ChildProcess;
|
|
85
|
-
|
|
86
|
-
const exit: Exit = (message) => {
|
|
87
|
-
if (message) console.error(message);
|
|
88
|
-
if (proxy) proxy.kill();
|
|
89
|
-
return undefined;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
if (directory !== undefined) {
|
|
93
|
-
console.log(`Serving ${directory}...`);
|
|
94
|
-
const args = ["serve", directory];
|
|
95
|
-
if (port) args.push("-p", port.toString());
|
|
96
|
-
proxy = spawn("npx", args, { shell: isWindows() });
|
|
97
|
-
} else {
|
|
98
|
-
const command = remaining;
|
|
99
|
-
if (command.length === 0)
|
|
100
|
-
return exit(
|
|
101
|
-
"Must specify a directory of static assets to serve or a command to run."
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
console.log(`Running ${command.join(" ")}...`);
|
|
105
|
-
proxy = spawn(
|
|
106
|
-
command[0].toString(),
|
|
107
|
-
command.slice(1).map((value) => value.toString()),
|
|
108
|
-
{ shell: isWindows() }
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
proxy.stdout.on("data", (data) => {
|
|
113
|
-
console.log(`[proxy]: ${data}`);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
proxy.stderr.on("data", (data) => {
|
|
117
|
-
console.error(`[proxy]: ${data}`);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
proxy.on("close", (code) => {
|
|
121
|
-
console.error(`Proxy exited with status ${code}.`);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// Wait for proxy process to start...
|
|
125
|
-
while (!proxy.pid) {}
|
|
126
|
-
|
|
127
|
-
if (port === undefined) {
|
|
128
|
-
console.log(
|
|
129
|
-
`Sleeping ${SECONDS_TO_WAIT_FOR_PROXY} seconds to allow proxy process to start before attempting to automatically determine port...`
|
|
130
|
-
);
|
|
131
|
-
console.log("To skip, specify the proxy port with --proxy.");
|
|
132
|
-
await sleep(SECONDS_TO_WAIT_FOR_PROXY * 1000);
|
|
133
|
-
|
|
134
|
-
port = getPids(proxy.pid)
|
|
135
|
-
.map(getPort)
|
|
136
|
-
.filter((port) => port !== undefined)[0];
|
|
137
|
-
|
|
138
|
-
if (port === undefined) {
|
|
139
|
-
return exit(
|
|
140
|
-
"Could not automatically determin proxy port. Please specify the proxy port with --proxy."
|
|
141
|
-
);
|
|
142
|
-
} else {
|
|
143
|
-
console.log(`Automatically determined the proxy port to be ${port}.`);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return { proxy, port, exit };
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
151
|
-
return yargs.command(
|
|
152
|
-
"dev [directory] [-- command]",
|
|
153
|
-
"🧑💻 Develop your full-stack Pages application locally",
|
|
154
|
-
(yargs) => {
|
|
155
|
-
return yargs
|
|
156
|
-
.positional("directory", {
|
|
157
|
-
type: "string",
|
|
158
|
-
demandOption: undefined,
|
|
159
|
-
description: "The directory of static assets to serve",
|
|
160
|
-
})
|
|
161
|
-
.positional("command", {
|
|
162
|
-
type: "string",
|
|
163
|
-
demandOption: undefined,
|
|
164
|
-
description: "The proxy command to run",
|
|
165
|
-
})
|
|
166
|
-
.options({
|
|
167
|
-
local: {
|
|
168
|
-
type: "boolean",
|
|
169
|
-
default: true,
|
|
170
|
-
description: "Run on my machine",
|
|
171
|
-
},
|
|
172
|
-
port: {
|
|
173
|
-
type: "number",
|
|
174
|
-
default: 8788,
|
|
175
|
-
description: "The port to listen on (serve from)",
|
|
176
|
-
},
|
|
177
|
-
proxy: {
|
|
178
|
-
type: "number",
|
|
179
|
-
description:
|
|
180
|
-
"The port to proxy (where the static assets are served)",
|
|
181
|
-
},
|
|
182
|
-
"script-path": {
|
|
183
|
-
type: "string",
|
|
184
|
-
default: "_worker.js",
|
|
185
|
-
description:
|
|
186
|
-
"The location of the single Worker script if not using functions",
|
|
187
|
-
},
|
|
188
|
-
binding: {
|
|
189
|
-
type: "array",
|
|
190
|
-
description: "Bind variable/secret (KEY=VALUE)",
|
|
191
|
-
alias: "b",
|
|
192
|
-
},
|
|
193
|
-
kv: {
|
|
194
|
-
type: "array",
|
|
195
|
-
description: "KV namespace to bind",
|
|
196
|
-
alias: "k",
|
|
197
|
-
},
|
|
198
|
-
do: {
|
|
199
|
-
type: "array",
|
|
200
|
-
description: "Durable Object to bind (NAME=CLASS)",
|
|
201
|
-
alias: "o",
|
|
202
|
-
},
|
|
203
|
-
// TODO: Miniflare user options
|
|
204
|
-
});
|
|
205
|
-
},
|
|
206
|
-
async ({
|
|
207
|
-
local,
|
|
208
|
-
directory,
|
|
209
|
-
port,
|
|
210
|
-
proxy: requestedProxyPort,
|
|
211
|
-
"script-path": singleWorkerScriptPath,
|
|
212
|
-
binding: bindings = [],
|
|
213
|
-
kv: kvs = [],
|
|
214
|
-
do: durableObjects = [],
|
|
215
|
-
"--": remaining = [],
|
|
216
|
-
}) => {
|
|
217
|
-
if (!local) {
|
|
218
|
-
console.error("Only local mode is supported at the moment.");
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const functionsDirectory = "./functions";
|
|
223
|
-
const usingFunctions = existsSync(functionsDirectory);
|
|
224
|
-
|
|
225
|
-
const proxy = await spawnProxyProcess({
|
|
226
|
-
directory,
|
|
227
|
-
port: requestedProxyPort,
|
|
228
|
-
remaining: remaining as (string | number)[],
|
|
229
|
-
});
|
|
230
|
-
if (proxy === undefined) return undefined;
|
|
231
|
-
|
|
232
|
-
const { port: proxyPort, exit } = proxy;
|
|
233
|
-
|
|
234
|
-
let miniflareArgs: MiniflareOptions = {};
|
|
235
|
-
|
|
236
|
-
if (usingFunctions) {
|
|
237
|
-
const scriptPath = join(tmpdir(), "./functionsWorker.js");
|
|
238
|
-
miniflareArgs = {
|
|
239
|
-
scriptPath,
|
|
240
|
-
buildWatchPaths: [functionsDirectory],
|
|
241
|
-
buildCommand: `npx @cloudflare/pages-functions-compiler build ${functionsDirectory} --outfile ${scriptPath}`,
|
|
242
|
-
};
|
|
243
|
-
} else {
|
|
244
|
-
const scriptPath =
|
|
245
|
-
directory !== undefined
|
|
246
|
-
? join(directory, singleWorkerScriptPath)
|
|
247
|
-
: singleWorkerScriptPath;
|
|
248
|
-
|
|
249
|
-
if (!existsSync(scriptPath)) {
|
|
250
|
-
return exit(
|
|
251
|
-
`No Worker script found at ${scriptPath}. Please either create a functions directory or create a single Worker at ${scriptPath}.`
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
miniflareArgs = {
|
|
256
|
-
scriptPath,
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const { Miniflare } = await import("miniflare");
|
|
261
|
-
const { Request, Response, fetch } = await import("@miniflare/core");
|
|
262
|
-
const miniflare = new Miniflare({
|
|
263
|
-
watch: true,
|
|
264
|
-
modules: true,
|
|
265
|
-
|
|
266
|
-
kvNamespaces: kvs.map((kv) => kv.toString()),
|
|
267
|
-
|
|
268
|
-
durableObjects: Object.fromEntries(
|
|
269
|
-
durableObjects.map((durableObject) =>
|
|
270
|
-
durableObject.toString().split("=")
|
|
271
|
-
)
|
|
272
|
-
),
|
|
273
|
-
|
|
274
|
-
bindings: {
|
|
275
|
-
// User bindings
|
|
276
|
-
...Object.fromEntries(
|
|
277
|
-
bindings.map((binding) => binding.toString().split("="))
|
|
278
|
-
),
|
|
279
|
-
|
|
280
|
-
// env.ASSETS.fetch
|
|
281
|
-
ASSETS: {
|
|
282
|
-
fetch: async (
|
|
283
|
-
input: RequestInfo,
|
|
284
|
-
init?: RequestInit | undefined
|
|
285
|
-
) => {
|
|
286
|
-
try {
|
|
287
|
-
let request = new Request(input, init);
|
|
288
|
-
const url = new URL(request.url);
|
|
289
|
-
url.host = `127.0.0.1:${proxyPort}`;
|
|
290
|
-
request = new Request(url.toString(), request);
|
|
291
|
-
return await fetch(request);
|
|
292
|
-
} catch (thrown) {
|
|
293
|
-
console.error(`Could not proxy request: ${thrown}`);
|
|
294
|
-
|
|
295
|
-
// TODO: Pretty error page
|
|
296
|
-
return new Response(
|
|
297
|
-
`[wrangler] Could not proxy request: ${thrown}`,
|
|
298
|
-
{ status: 502 }
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
},
|
|
302
|
-
},
|
|
303
|
-
},
|
|
304
|
-
|
|
305
|
-
kvPersist: true,
|
|
306
|
-
durableObjectsPersist: true,
|
|
307
|
-
cachePersist: true,
|
|
308
|
-
|
|
309
|
-
...miniflareArgs,
|
|
310
|
-
});
|
|
311
|
-
const miniflareServer = await miniflare.createServer();
|
|
312
|
-
|
|
313
|
-
miniflareServer.listen(0, () => {
|
|
314
|
-
const address = miniflareServer.address();
|
|
315
|
-
if (typeof address === "string")
|
|
316
|
-
return exit(
|
|
317
|
-
"Could not determine Miniflare's port. Please report this issue to the Wrangler team."
|
|
318
|
-
);
|
|
319
|
-
|
|
320
|
-
const miniflarePort = address.port;
|
|
321
|
-
|
|
322
|
-
const app = express();
|
|
323
|
-
|
|
324
|
-
app.use(
|
|
325
|
-
"/",
|
|
326
|
-
httpProxyMiddleware.createProxyMiddleware({
|
|
327
|
-
target: `http://127.0.0.1:${miniflarePort}`,
|
|
328
|
-
})
|
|
329
|
-
);
|
|
330
|
-
|
|
331
|
-
app.use(
|
|
332
|
-
"/",
|
|
333
|
-
httpProxyMiddleware.createProxyMiddleware({
|
|
334
|
-
target: `http://127.0.0.1:${proxyPort}`,
|
|
335
|
-
})
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
app.listen(port, () => {
|
|
339
|
-
console.log(`Serving at http://127.0.0.1:${port}/`);
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
);
|
|
344
|
-
};
|