wrangler 0.0.0-e6733a3 → 0.0.0-e6ada079
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.
Potentially problematic release.
This version of wrangler might be problematic. Click here for more details.
- package/README.md +47 -16
- package/bin/wrangler.js +94 -31
- package/config-schema.json +3100 -0
- package/kv-asset-handler.js +1 -0
- package/package.json +154 -82
- 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-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +15 -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 +334 -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 +240 -0
- package/wrangler-dist/ProxyWorker.js.map +6 -0
- package/wrangler-dist/cli.d.ts +26391 -0
- package/wrangler-dist/cli.js +204293 -116652
- 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/pages/functions/buildWorker.ts +0 -62
- package/pages/functions/filepath-routing.test.ts +0 -39
- package/pages/functions/filepath-routing.ts +0 -221
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -158
- package/pages/functions/template-worker.ts +0 -144
- package/src/__tests__/clipboardy-mock.js +0 -4
- package/src/__tests__/dev.test.tsx +0 -66
- package/src/__tests__/index.test.ts +0 -287
- package/src/__tests__/jest.setup.ts +0 -22
- package/src/__tests__/kv.test.ts +0 -1098
- package/src/__tests__/mock-cfetch.ts +0 -171
- package/src/__tests__/mock-dialogs.ts +0 -65
- package/src/__tests__/run-in-tmp.ts +0 -19
- package/src/__tests__/run-wrangler.ts +0 -32
- 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 -102
- package/src/cfetch/internal.ts +0 -69
- package/src/cli.ts +0 -9
- package/src/config.ts +0 -487
- package/src/dev.tsx +0 -771
- package/src/dialogs.tsx +0 -77
- package/src/index.tsx +0 -1974
- package/src/inspect.ts +0 -524
- package/src/kv.tsx +0 -267
- package/src/module-collection.ts +0 -64
- package/src/pages.tsx +0 -1031
- package/src/proxy.ts +0 -294
- package/src/publish.ts +0 -358
- package/src/sites.tsx +0 -114
- package/src/tail.tsx +0 -73
- package/src/user.tsx +0 -1025
- 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/proxy.ts
DELETED
@@ -1,294 +0,0 @@
|
|
1
|
-
import { connect } from "node:http2";
|
2
|
-
import type { ServerHttp2Stream } from "node:http2";
|
3
|
-
import { createServer } from "node:http";
|
4
|
-
import type {
|
5
|
-
IncomingHttpHeaders,
|
6
|
-
RequestListener,
|
7
|
-
IncomingMessage,
|
8
|
-
ServerResponse,
|
9
|
-
Server,
|
10
|
-
} from "node:http";
|
11
|
-
import WebSocket from "faye-websocket";
|
12
|
-
import serveStatic from "serve-static";
|
13
|
-
import type { CfPreviewToken } from "./api/preview";
|
14
|
-
import { useEffect, useRef } from "react";
|
15
|
-
|
16
|
-
/**
|
17
|
-
* `usePreviewServer` is a React hook that creates a local development
|
18
|
-
* server that can be used to develop a Worker.
|
19
|
-
*
|
20
|
-
* When we run `wrangler dev`, we start by uploading the compiled worker
|
21
|
-
* to the preview service, which responds with a preview token.
|
22
|
-
* (see `useWorker`/`createWorker` for details.)
|
23
|
-
* We can then use that token to connect to the preview server for a
|
24
|
-
* great local development experience. Further, as we change the worker,
|
25
|
-
* we can update the preview token transparently without having to restart
|
26
|
-
* the development server.
|
27
|
-
*/
|
28
|
-
|
29
|
-
/** Rewrite request headers to add the preview token. */
|
30
|
-
function addCfPreviewTokenHeader(
|
31
|
-
headers: IncomingHttpHeaders,
|
32
|
-
previewTokenValue: string
|
33
|
-
) {
|
34
|
-
headers["cf-workers-preview-token"] = previewTokenValue;
|
35
|
-
}
|
36
|
-
|
37
|
-
/**
|
38
|
-
* Rewrite references in request headers
|
39
|
-
* from the preview host to the local host.
|
40
|
-
*/
|
41
|
-
function rewriteRemoteHostToLocalHostInHeaders(
|
42
|
-
headers: IncomingHttpHeaders,
|
43
|
-
remoteHost: string,
|
44
|
-
localPort: number
|
45
|
-
) {
|
46
|
-
for (const [name, value] of Object.entries(headers)) {
|
47
|
-
// Rewrite the remote host to the local host.
|
48
|
-
if (typeof value === "string" && value.includes(remoteHost)) {
|
49
|
-
headers[name] = value
|
50
|
-
.replaceAll(`https://${remoteHost}`, `http://localhost:${localPort}`)
|
51
|
-
.replaceAll(remoteHost, `localhost:${localPort}`);
|
52
|
-
}
|
53
|
-
}
|
54
|
-
}
|
55
|
-
|
56
|
-
export function usePreviewServer({
|
57
|
-
previewToken,
|
58
|
-
publicRoot,
|
59
|
-
port,
|
60
|
-
}: {
|
61
|
-
previewToken: CfPreviewToken | undefined;
|
62
|
-
publicRoot: undefined | string;
|
63
|
-
port: number;
|
64
|
-
}) {
|
65
|
-
/** Creates an HTTP/1 proxy that sends requests over HTTP/2. */
|
66
|
-
const proxyServer = useRef<Server>();
|
67
|
-
if (!proxyServer.current) {
|
68
|
-
proxyServer.current = createServer()
|
69
|
-
.on("request", function (req, res) {
|
70
|
-
// log all requests
|
71
|
-
console.log(
|
72
|
-
new Date().toLocaleTimeString(),
|
73
|
-
req.method,
|
74
|
-
req.url,
|
75
|
-
res.statusCode
|
76
|
-
);
|
77
|
-
})
|
78
|
-
.on("upgrade", (req) => {
|
79
|
-
// log all websocket connections
|
80
|
-
console.log(
|
81
|
-
new Date().toLocaleTimeString(),
|
82
|
-
req.method,
|
83
|
-
req.url,
|
84
|
-
101,
|
85
|
-
"(WebSocket)"
|
86
|
-
);
|
87
|
-
})
|
88
|
-
.on("error", (err) => {
|
89
|
-
// log all connection errors
|
90
|
-
console.error(new Date().toLocaleTimeString(), err);
|
91
|
-
});
|
92
|
-
}
|
93
|
-
|
94
|
-
/**
|
95
|
-
* When we're not connected / getting a fresh token on changes,
|
96
|
-
* we'd like to buffer streams/requests until we're connected.
|
97
|
-
* Once connected, we can flush the buffered streams/requests.
|
98
|
-
* streamBufferRef is used to buffer http/2 streams, while
|
99
|
-
* requestResponseBufferRef is used to buffer http/1 requests.
|
100
|
-
*/
|
101
|
-
const streamBufferRef = useRef<
|
102
|
-
{ stream: ServerHttp2Stream; headers: IncomingHttpHeaders }[]
|
103
|
-
>([]);
|
104
|
-
const requestResponseBufferRef = useRef<
|
105
|
-
{ request: IncomingMessage; response: ServerResponse }[]
|
106
|
-
>([]);
|
107
|
-
|
108
|
-
useEffect(() => {
|
109
|
-
const proxy = proxyServer.current;
|
110
|
-
|
111
|
-
// If we don't have a token, that means either we're just starting up,
|
112
|
-
// or we're refreshing the token.
|
113
|
-
if (!previewToken) {
|
114
|
-
const cleanupListeners: (() => void)[] = [];
|
115
|
-
const bufferStream = (
|
116
|
-
stream: ServerHttp2Stream,
|
117
|
-
headers: IncomingHttpHeaders
|
118
|
-
) => {
|
119
|
-
// store the stream in a buffer so we can replay it later
|
120
|
-
streamBufferRef.current.push({ stream, headers });
|
121
|
-
};
|
122
|
-
proxy.on("stream", bufferStream);
|
123
|
-
cleanupListeners.push(() => proxy.off("stream", bufferStream));
|
124
|
-
|
125
|
-
const bufferRequestResponse = (
|
126
|
-
request: IncomingMessage,
|
127
|
-
response: ServerResponse
|
128
|
-
) => {
|
129
|
-
// store the request and response in a buffer so we can replay it later
|
130
|
-
requestResponseBufferRef.current.push({ request, response });
|
131
|
-
};
|
132
|
-
|
133
|
-
proxy.on("request", bufferRequestResponse);
|
134
|
-
cleanupListeners.push(() => proxy.off("request", bufferRequestResponse));
|
135
|
-
return () => {
|
136
|
-
cleanupListeners.forEach((cleanup) => cleanup());
|
137
|
-
};
|
138
|
-
}
|
139
|
-
|
140
|
-
// We have a token. Let's proxy requests to the preview end point.
|
141
|
-
const cleanupListeners: (() => void)[] = [];
|
142
|
-
|
143
|
-
const assetPath = typeof publicRoot === "string" ? publicRoot : null;
|
144
|
-
|
145
|
-
// create a ClientHttp2Session
|
146
|
-
const remote = connect(`https://${previewToken.host}`);
|
147
|
-
cleanupListeners.push(() => remote.destroy());
|
148
|
-
|
149
|
-
/** HTTP/2 -> HTTP/2 */
|
150
|
-
function handleStream(
|
151
|
-
stream: ServerHttp2Stream,
|
152
|
-
headers: IncomingHttpHeaders
|
153
|
-
) {
|
154
|
-
addCfPreviewTokenHeader(headers, previewToken.value);
|
155
|
-
headers[":authority"] = previewToken.host;
|
156
|
-
const request = stream.pipe(remote.request(headers));
|
157
|
-
request.on("response", (responseHeaders: IncomingHttpHeaders) => {
|
158
|
-
rewriteRemoteHostToLocalHostInHeaders(
|
159
|
-
responseHeaders,
|
160
|
-
previewToken.host,
|
161
|
-
port
|
162
|
-
);
|
163
|
-
stream.respond(responseHeaders);
|
164
|
-
request.pipe(stream, { end: true });
|
165
|
-
});
|
166
|
-
}
|
167
|
-
proxy.on("stream", handleStream);
|
168
|
-
cleanupListeners.push(() => proxy.off("stream", handleStream));
|
169
|
-
|
170
|
-
// flush and replay buffered streams
|
171
|
-
streamBufferRef.current.forEach((buffer) =>
|
172
|
-
handleStream(buffer.stream, buffer.headers)
|
173
|
-
);
|
174
|
-
streamBufferRef.current = [];
|
175
|
-
|
176
|
-
/** HTTP/1 -> HTTP/2 */
|
177
|
-
const handleRequest: RequestListener = (
|
178
|
-
message: IncomingMessage,
|
179
|
-
response: ServerResponse
|
180
|
-
) => {
|
181
|
-
const { httpVersionMajor, headers, method, url } = message;
|
182
|
-
if (httpVersionMajor >= 2) {
|
183
|
-
return; // Already handled by the "stream" event.
|
184
|
-
}
|
185
|
-
addCfPreviewTokenHeader(headers, previewToken.value);
|
186
|
-
headers[":method"] = method;
|
187
|
-
headers[":path"] = url;
|
188
|
-
headers[":authority"] = previewToken.host;
|
189
|
-
headers[":scheme"] = "https";
|
190
|
-
for (const name of Object.keys(headers)) {
|
191
|
-
if (HTTP1_HEADERS.has(name.toLowerCase())) {
|
192
|
-
delete headers[name];
|
193
|
-
}
|
194
|
-
}
|
195
|
-
const request = message.pipe(remote.request(headers));
|
196
|
-
request.on("response", (responseHeaders) => {
|
197
|
-
const status = responseHeaders[":status"];
|
198
|
-
rewriteRemoteHostToLocalHostInHeaders(
|
199
|
-
responseHeaders,
|
200
|
-
previewToken.host,
|
201
|
-
port
|
202
|
-
);
|
203
|
-
for (const name of Object.keys(responseHeaders)) {
|
204
|
-
if (name.startsWith(":")) {
|
205
|
-
delete responseHeaders[name];
|
206
|
-
}
|
207
|
-
}
|
208
|
-
response.writeHead(status, responseHeaders);
|
209
|
-
request.pipe(response, { end: true });
|
210
|
-
});
|
211
|
-
};
|
212
|
-
|
213
|
-
// If an asset path is defined, check the file system
|
214
|
-
// for a file first and serve if it exists.
|
215
|
-
const actualHandleRequest = assetPath
|
216
|
-
? createHandleAssetsRequest(assetPath, handleRequest)
|
217
|
-
: handleRequest;
|
218
|
-
|
219
|
-
proxy.on("request", actualHandleRequest);
|
220
|
-
cleanupListeners.push(() => proxy.off("request", actualHandleRequest));
|
221
|
-
|
222
|
-
// flush and replay buffered requests
|
223
|
-
requestResponseBufferRef.current.forEach(({ request, response }) =>
|
224
|
-
actualHandleRequest(request, response)
|
225
|
-
);
|
226
|
-
requestResponseBufferRef.current = [];
|
227
|
-
|
228
|
-
/** HTTP/1 -> WebSocket (over HTTP/1) */
|
229
|
-
const handleUpgrade = (
|
230
|
-
message: IncomingMessage,
|
231
|
-
socket: WebSocket,
|
232
|
-
body: Buffer
|
233
|
-
) => {
|
234
|
-
const { headers, url } = message;
|
235
|
-
addCfPreviewTokenHeader(headers, previewToken.value);
|
236
|
-
headers["host"] = previewToken.host;
|
237
|
-
const localWebsocket = new WebSocket(message, socket, body);
|
238
|
-
// TODO(soon): Custom WebSocket protocol is not working?
|
239
|
-
const remoteWebsocketClient = new WebSocket.Client(
|
240
|
-
`wss://${previewToken.host}${url}`,
|
241
|
-
[],
|
242
|
-
{ headers }
|
243
|
-
);
|
244
|
-
localWebsocket.pipe(remoteWebsocketClient).pipe(localWebsocket);
|
245
|
-
// We close down websockets whenever we refresh the token.
|
246
|
-
cleanupListeners.push(() => {
|
247
|
-
localWebsocket.destroy();
|
248
|
-
remoteWebsocketClient.destroy();
|
249
|
-
});
|
250
|
-
};
|
251
|
-
proxy.on("upgrade", handleUpgrade);
|
252
|
-
cleanupListeners.push(() => proxy.off("upgrade", handleUpgrade));
|
253
|
-
|
254
|
-
return () => {
|
255
|
-
cleanupListeners.forEach((d) => d());
|
256
|
-
};
|
257
|
-
}, [previewToken, publicRoot, port]);
|
258
|
-
|
259
|
-
// Start/stop the server whenever the
|
260
|
-
// containing component is mounted/unmounted.
|
261
|
-
useEffect(() => {
|
262
|
-
proxyServer.current.listen(port);
|
263
|
-
console.log(`⬣ Listening at http://localhost:${port}`);
|
264
|
-
|
265
|
-
return () => {
|
266
|
-
proxyServer.current.close();
|
267
|
-
};
|
268
|
-
}, [port]);
|
269
|
-
}
|
270
|
-
|
271
|
-
function createHandleAssetsRequest(
|
272
|
-
assetPath: string,
|
273
|
-
handleRequest: RequestListener
|
274
|
-
) {
|
275
|
-
const handleAsset = serveStatic(assetPath, {
|
276
|
-
cacheControl: false,
|
277
|
-
});
|
278
|
-
return (request: IncomingMessage, response: ServerResponse) => {
|
279
|
-
handleAsset(request, response, () => {
|
280
|
-
handleRequest(request, response);
|
281
|
-
});
|
282
|
-
};
|
283
|
-
}
|
284
|
-
|
285
|
-
/** A Set of headers we want to remove from HTTP/1 requests. */
|
286
|
-
const HTTP1_HEADERS = new Set([
|
287
|
-
"host",
|
288
|
-
"connection",
|
289
|
-
"upgrade",
|
290
|
-
"keep-alive",
|
291
|
-
"proxy-connection",
|
292
|
-
"transfer-encoding",
|
293
|
-
"http2-settings",
|
294
|
-
]);
|
package/src/publish.ts
DELETED
@@ -1,358 +0,0 @@
|
|
1
|
-
import assert from "node:assert";
|
2
|
-
import path from "node:path";
|
3
|
-
import { readFile } from "node:fs/promises";
|
4
|
-
import esbuild from "esbuild";
|
5
|
-
import { execa } from "execa";
|
6
|
-
import tmp from "tmp-promise";
|
7
|
-
import type { CfWorkerInit } from "./api/worker";
|
8
|
-
import { toFormData } from "./api/form_data";
|
9
|
-
import { fetchResult } from "./cfetch";
|
10
|
-
import type { Config } from "./config";
|
11
|
-
import makeModuleCollector from "./module-collection";
|
12
|
-
import { syncAssets } from "./sites";
|
13
|
-
|
14
|
-
type CfScriptFormat = undefined | "modules" | "service-worker";
|
15
|
-
|
16
|
-
type Props = {
|
17
|
-
config: Config;
|
18
|
-
format?: CfScriptFormat;
|
19
|
-
script?: string;
|
20
|
-
name?: string;
|
21
|
-
env?: string;
|
22
|
-
compatibilityDate?: string;
|
23
|
-
compatibilityFlags?: string[];
|
24
|
-
public?: string;
|
25
|
-
site?: string;
|
26
|
-
triggers?: (string | number)[];
|
27
|
-
routes?: (string | number)[];
|
28
|
-
legacyEnv?: boolean;
|
29
|
-
jsxFactory: undefined | string;
|
30
|
-
jsxFragment: undefined | string;
|
31
|
-
};
|
32
|
-
|
33
|
-
function sleep(ms: number) {
|
34
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
35
|
-
}
|
36
|
-
|
37
|
-
export default async function publish(props: Props): Promise<void> {
|
38
|
-
if (props.public && props.format === "service-worker") {
|
39
|
-
// TODO: check config too
|
40
|
-
throw new Error(
|
41
|
-
"You cannot use the service worker format with a public directory."
|
42
|
-
);
|
43
|
-
}
|
44
|
-
// TODO: warn if git/hg has uncommitted changes
|
45
|
-
const { config } = props;
|
46
|
-
const {
|
47
|
-
account_id: accountId,
|
48
|
-
build,
|
49
|
-
// @ts-expect-error hidden
|
50
|
-
__path__,
|
51
|
-
} = config;
|
52
|
-
|
53
|
-
const envRootObj = props.env ? config.env[props.env] || {} : config;
|
54
|
-
|
55
|
-
assert(
|
56
|
-
envRootObj.compatibility_date || props["compatibility-date"],
|
57
|
-
"A compatibility_date is required when publishing. Add one to your wrangler.toml file, or pass it in your terminal as --compatibility_date. See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
|
58
|
-
);
|
59
|
-
|
60
|
-
const triggers = props.triggers || config.triggers?.crons;
|
61
|
-
const routes = props.routes || config.routes;
|
62
|
-
|
63
|
-
const jsxFactory = props.jsxFactory || config.jsx_factory;
|
64
|
-
const jsxFragment = props.jsxFragment || config.jsx_fragment;
|
65
|
-
|
66
|
-
assert(config.account_id, "missing account id");
|
67
|
-
|
68
|
-
let scriptName = props.name || config.name;
|
69
|
-
assert(
|
70
|
-
scriptName,
|
71
|
-
'You need to provide a name when publishing a worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`'
|
72
|
-
);
|
73
|
-
|
74
|
-
let file: string;
|
75
|
-
if (props.script) {
|
76
|
-
file = props.script;
|
77
|
-
} else {
|
78
|
-
assert(build?.upload?.main, "missing main file");
|
79
|
-
file = path.join(path.dirname(__path__), build.upload.main);
|
80
|
-
}
|
81
|
-
|
82
|
-
if (props.legacyEnv) {
|
83
|
-
scriptName += props.env ? `-${props.env}` : "";
|
84
|
-
}
|
85
|
-
const envName = props.env ?? "production";
|
86
|
-
|
87
|
-
const destination = await tmp.dir({ unsafeCleanup: true });
|
88
|
-
|
89
|
-
if (props.config.build?.command) {
|
90
|
-
// TODO: add a deprecation message here?
|
91
|
-
console.log("running:", props.config.build.command);
|
92
|
-
const buildCommandPieces = props.config.build.command.split(" ");
|
93
|
-
await execa(buildCommandPieces[0], buildCommandPieces.slice(1), {
|
94
|
-
stdout: "inherit",
|
95
|
-
stderr: "inherit",
|
96
|
-
...(props.config.build?.cwd && { cwd: props.config.build.cwd }),
|
97
|
-
});
|
98
|
-
}
|
99
|
-
|
100
|
-
const moduleCollector = makeModuleCollector();
|
101
|
-
const result = await esbuild.build({
|
102
|
-
...(props.public
|
103
|
-
? {
|
104
|
-
stdin: {
|
105
|
-
contents: (
|
106
|
-
await readFile(
|
107
|
-
path.join(__dirname, "../static-asset-facade.js"),
|
108
|
-
"utf8"
|
109
|
-
)
|
110
|
-
).replace("__ENTRY_POINT__", path.join(process.cwd(), file)),
|
111
|
-
sourcefile: "static-asset-facade.js",
|
112
|
-
resolveDir: path.dirname(file),
|
113
|
-
},
|
114
|
-
}
|
115
|
-
: { entryPoints: [file] }),
|
116
|
-
bundle: true,
|
117
|
-
nodePaths: props.public ? [path.join(__dirname, "../vendor")] : undefined,
|
118
|
-
outdir: destination.path,
|
119
|
-
external: ["__STATIC_CONTENT_MANIFEST"],
|
120
|
-
format: "esm",
|
121
|
-
sourcemap: true,
|
122
|
-
metafile: true,
|
123
|
-
conditions: ["worker", "browser"],
|
124
|
-
loader: {
|
125
|
-
".js": "jsx",
|
126
|
-
},
|
127
|
-
plugins: [moduleCollector.plugin],
|
128
|
-
...(jsxFactory && { jsxFactory }),
|
129
|
-
...(jsxFragment && { jsxFragment }),
|
130
|
-
});
|
131
|
-
|
132
|
-
const chunks = Object.entries(result.metafile.outputs).find(
|
133
|
-
([_path, { entryPoint }]) =>
|
134
|
-
entryPoint ===
|
135
|
-
(props.public
|
136
|
-
? path.join(path.dirname(file), "static-asset-facade.js")
|
137
|
-
: Object.keys(result.metafile.inputs)[0])
|
138
|
-
);
|
139
|
-
|
140
|
-
const { format } = props;
|
141
|
-
const bundle = {
|
142
|
-
type: chunks[1].exports.length > 0 ? "esm" : "commonjs",
|
143
|
-
exports: chunks[1].exports,
|
144
|
-
};
|
145
|
-
|
146
|
-
// TODO: instead of bundling the facade with the worker, we should just bundle the worker and expose it as a module.
|
147
|
-
// That way we'll be able to accurately tell if this is a service worker or not.
|
148
|
-
|
149
|
-
if (format === "modules" && bundle.type === "commonjs") {
|
150
|
-
console.error("⎔ Cannot use modules with a commonjs bundle.");
|
151
|
-
// TODO: a much better error message here, with what to do next
|
152
|
-
return;
|
153
|
-
}
|
154
|
-
if (format === "service-worker" && bundle.type !== "esm") {
|
155
|
-
console.error("⎔ Cannot use service-worker with a esm bundle.");
|
156
|
-
// TODO: a much better error message here, with what to do next
|
157
|
-
return;
|
158
|
-
}
|
159
|
-
|
160
|
-
const content = await readFile(chunks[0], { encoding: "utf-8" });
|
161
|
-
await destination.cleanup();
|
162
|
-
|
163
|
-
// if config.migrations
|
164
|
-
// get current migration tag
|
165
|
-
let migrations;
|
166
|
-
if ("migrations" in config) {
|
167
|
-
const scripts = await fetchResult<{ id: string; migration_tag: string }[]>(
|
168
|
-
`/accounts/${accountId}/workers/scripts`
|
169
|
-
);
|
170
|
-
const script = scripts.find(({ id }) => id === scriptName);
|
171
|
-
if (script?.migration_tag) {
|
172
|
-
// was already published once
|
173
|
-
const foundIndex = config.migrations.findIndex(
|
174
|
-
(migration) => migration.tag === script.migration_tag
|
175
|
-
);
|
176
|
-
if (foundIndex === -1) {
|
177
|
-
console.warn(
|
178
|
-
`The published script ${scriptName} has a migration tag "${script.migration_tag}, which was not found in wrangler.toml. You may have already deleted it. Applying all available migrations to the script...`
|
179
|
-
);
|
180
|
-
migrations = {
|
181
|
-
old_tag: script.migration_tag,
|
182
|
-
new_tag: config.migrations[config.migrations.length - 1].tag,
|
183
|
-
steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
|
184
|
-
};
|
185
|
-
} else {
|
186
|
-
migrations = {
|
187
|
-
old_tag: script.migration_tag,
|
188
|
-
new_tag: config.migrations[config.migrations.length - 1].tag,
|
189
|
-
steps: config.migrations
|
190
|
-
.slice(foundIndex + 1)
|
191
|
-
.map(({ tag: _tag, ...rest }) => rest),
|
192
|
-
};
|
193
|
-
}
|
194
|
-
} else {
|
195
|
-
migrations = {
|
196
|
-
new_tag: config.migrations[config.migrations.length - 1].tag,
|
197
|
-
steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
|
198
|
-
};
|
199
|
-
}
|
200
|
-
}
|
201
|
-
|
202
|
-
const assets =
|
203
|
-
props.public || props.site || props.config.site?.bucket // TODO: allow both
|
204
|
-
? await syncAssets(
|
205
|
-
accountId,
|
206
|
-
scriptName,
|
207
|
-
props.public || props.site || props.config.site?.bucket,
|
208
|
-
false
|
209
|
-
)
|
210
|
-
: { manifest: undefined, namespace: undefined };
|
211
|
-
|
212
|
-
const bindings: CfWorkerInit["bindings"] = {
|
213
|
-
kv_namespaces: envRootObj.kv_namespaces?.concat(
|
214
|
-
assets.namespace
|
215
|
-
? { binding: "__STATIC_CONTENT", id: assets.namespace }
|
216
|
-
: []
|
217
|
-
),
|
218
|
-
vars: envRootObj.vars,
|
219
|
-
durable_objects: envRootObj.durable_objects,
|
220
|
-
services: envRootObj.experimental_services,
|
221
|
-
};
|
222
|
-
|
223
|
-
const worker: CfWorkerInit = {
|
224
|
-
name: scriptName,
|
225
|
-
main: {
|
226
|
-
name: path.basename(chunks[0]),
|
227
|
-
content: content,
|
228
|
-
type: bundle.type === "esm" ? "esm" : "commonjs",
|
229
|
-
},
|
230
|
-
bindings,
|
231
|
-
...(migrations && { migrations }),
|
232
|
-
modules: moduleCollector.modules.concat(
|
233
|
-
assets.manifest
|
234
|
-
? {
|
235
|
-
name: "__STATIC_CONTENT_MANIFEST",
|
236
|
-
content: JSON.stringify(assets.manifest),
|
237
|
-
type: "text",
|
238
|
-
}
|
239
|
-
: []
|
240
|
-
),
|
241
|
-
compatibility_date: config.compatibility_date,
|
242
|
-
compatibility_flags: config.compatibility_flags,
|
243
|
-
usage_model: config.usage_model,
|
244
|
-
};
|
245
|
-
|
246
|
-
const start = Date.now();
|
247
|
-
function formatTime(duration: number) {
|
248
|
-
return `(${(duration / 1000).toFixed(2)} sec)`;
|
249
|
-
}
|
250
|
-
|
251
|
-
const notProd = !props.legacyEnv && props.env;
|
252
|
-
const workerName = notProd ? `${scriptName} (${envName})` : scriptName;
|
253
|
-
const workerUrl = notProd
|
254
|
-
? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
|
255
|
-
: `/accounts/${accountId}/workers/scripts/${scriptName}`;
|
256
|
-
|
257
|
-
// Upload the script so it has time to propagate.
|
258
|
-
const { available_on_subdomain } = await fetchResult(
|
259
|
-
`${workerUrl}?available_on_subdomain=true`,
|
260
|
-
{
|
261
|
-
method: "PUT",
|
262
|
-
// @ts-expect-error: TODO: fix this type error!
|
263
|
-
body: toFormData(worker),
|
264
|
-
}
|
265
|
-
);
|
266
|
-
|
267
|
-
const uploadMs = Date.now() - start;
|
268
|
-
console.log("Uploaded", workerName, formatTime(uploadMs));
|
269
|
-
const deployments: Promise<string[]>[] = [];
|
270
|
-
|
271
|
-
const userSubdomain = (
|
272
|
-
await fetchResult<{ subdomain: string }>(
|
273
|
-
`/accounts/${accountId}/workers/subdomain`
|
274
|
-
)
|
275
|
-
).subdomain;
|
276
|
-
|
277
|
-
const scriptURL =
|
278
|
-
props.legacyEnv || !props.env
|
279
|
-
? `${scriptName}.${userSubdomain}.workers.dev`
|
280
|
-
: `${envName}.${scriptName}.${userSubdomain}.workers.dev`;
|
281
|
-
|
282
|
-
// Enable the `workers.dev` subdomain.
|
283
|
-
// TODO: Make this configurable.
|
284
|
-
if (!available_on_subdomain) {
|
285
|
-
deployments.push(
|
286
|
-
fetchResult(`${workerUrl}/subdomain`, {
|
287
|
-
method: "POST",
|
288
|
-
body: JSON.stringify({ enabled: true }),
|
289
|
-
headers: {
|
290
|
-
"Content-Type": "application/json",
|
291
|
-
},
|
292
|
-
})
|
293
|
-
.then(() => [scriptURL])
|
294
|
-
// Add a delay when the subdomain is first created.
|
295
|
-
// This is to prevent an issue where a negative cache-hit
|
296
|
-
// causes the subdomain to be unavailable for 30 seconds.
|
297
|
-
// This is a temporary measure until we fix this on the edge.
|
298
|
-
.then(async (url) => {
|
299
|
-
await sleep(3000);
|
300
|
-
return url;
|
301
|
-
})
|
302
|
-
);
|
303
|
-
} else {
|
304
|
-
deployments.push(Promise.resolve([scriptURL]));
|
305
|
-
}
|
306
|
-
|
307
|
-
// Update routing table for the script.
|
308
|
-
if (routes && routes.length) {
|
309
|
-
deployments.push(
|
310
|
-
fetchResult(`${workerUrl}/routes`, {
|
311
|
-
// TODO: PATCH will not delete previous routes on this script,
|
312
|
-
// whereas PUT will. We need to decide on the default behaviour
|
313
|
-
// and how to configure it.
|
314
|
-
method: "PUT",
|
315
|
-
body: JSON.stringify(routes.map((pattern) => ({ pattern }))),
|
316
|
-
headers: {
|
317
|
-
"Content-Type": "application/json",
|
318
|
-
},
|
319
|
-
}).then(() => {
|
320
|
-
if (routes.length > 10) {
|
321
|
-
return routes
|
322
|
-
.slice(0, 9)
|
323
|
-
.map(String)
|
324
|
-
.concat([`...and ${routes.length - 10} more routes`]);
|
325
|
-
}
|
326
|
-
return routes.map(String);
|
327
|
-
})
|
328
|
-
);
|
329
|
-
}
|
330
|
-
|
331
|
-
// Configure any schedules for the script.
|
332
|
-
// TODO: rename this to `schedules`?
|
333
|
-
if (triggers && triggers.length) {
|
334
|
-
deployments.push(
|
335
|
-
fetchResult(`${workerUrl}/schedules`, {
|
336
|
-
// TODO: Unlike routes, this endpoint does not support PATCH.
|
337
|
-
// So technically, this will override any previous schedules.
|
338
|
-
// We should change the endpoint to support PATCH.
|
339
|
-
method: "PUT",
|
340
|
-
body: JSON.stringify(triggers.map((cron) => ({ cron }))),
|
341
|
-
headers: {
|
342
|
-
"Content-Type": "application/json",
|
343
|
-
},
|
344
|
-
}).then(() => triggers.map(String))
|
345
|
-
);
|
346
|
-
}
|
347
|
-
|
348
|
-
if (!deployments.length) {
|
349
|
-
return;
|
350
|
-
}
|
351
|
-
|
352
|
-
const targets = await Promise.all(deployments);
|
353
|
-
const deployMs = Date.now() - start - uploadMs;
|
354
|
-
console.log("Deployed", workerName, formatTime(deployMs));
|
355
|
-
for (const target of targets.flat()) {
|
356
|
-
console.log(" ", target);
|
357
|
-
}
|
358
|
-
}
|