wrangler 0.0.2 → 0.0.6

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.
Files changed (69) hide show
  1. package/README.md +51 -55
  2. package/bin/wrangler.js +36 -0
  3. package/import_meta_url.js +3 -0
  4. package/miniflare-config-stubs/.env.empty +0 -0
  5. package/miniflare-config-stubs/package.empty.json +1 -0
  6. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  7. package/package.json +111 -9
  8. package/src/__tests__/clipboardy-mock.js +4 -0
  9. package/src/__tests__/index.test.ts +391 -0
  10. package/src/__tests__/jest.setup.ts +17 -0
  11. package/src/__tests__/mock-cfetch.js +42 -0
  12. package/src/__tests__/mock-dialogs.ts +65 -0
  13. package/src/api/form_data.ts +141 -0
  14. package/src/api/inspect.ts +430 -0
  15. package/src/api/preview.ts +128 -0
  16. package/src/api/worker.ts +161 -0
  17. package/src/cfetch.ts +72 -0
  18. package/src/cli.ts +10 -0
  19. package/src/config.ts +122 -0
  20. package/src/dev.tsx +867 -0
  21. package/src/dialogs.tsx +77 -0
  22. package/src/index.tsx +1875 -0
  23. package/src/kv.tsx +211 -0
  24. package/src/module-collection.ts +64 -0
  25. package/src/pages.tsx +818 -0
  26. package/src/proxy.ts +104 -0
  27. package/src/publish.ts +358 -0
  28. package/src/sites.tsx +115 -0
  29. package/src/tail.tsx +71 -0
  30. package/src/user.tsx +1029 -0
  31. package/static-asset-facade.js +47 -0
  32. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +332 -0
  33. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +176 -0
  34. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +25 -0
  35. package/vendor/@cloudflare/kv-asset-handler/README.md +245 -0
  36. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +32 -0
  37. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +354 -0
  38. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +13 -0
  39. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +148 -0
  40. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +1 -0
  41. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +436 -0
  42. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +1 -0
  43. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +40 -0
  44. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +1 -0
  45. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +42 -0
  46. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +26 -0
  47. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +31 -0
  48. package/vendor/@cloudflare/kv-asset-handler/package.json +52 -0
  49. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +296 -0
  50. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +136 -0
  51. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +464 -0
  52. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +33 -0
  53. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +42 -0
  54. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +39 -0
  55. package/vendor/wrangler-mime/CHANGELOG.md +289 -0
  56. package/vendor/wrangler-mime/LICENSE +21 -0
  57. package/vendor/wrangler-mime/Mime.js +97 -0
  58. package/vendor/wrangler-mime/README.md +187 -0
  59. package/vendor/wrangler-mime/cli.js +46 -0
  60. package/vendor/wrangler-mime/index.js +4 -0
  61. package/vendor/wrangler-mime/lite.js +4 -0
  62. package/vendor/wrangler-mime/package.json +52 -0
  63. package/vendor/wrangler-mime/types/other.js +1 -0
  64. package/vendor/wrangler-mime/types/standard.js +1 -0
  65. package/wrangler-dist/cli.js +125758 -0
  66. package/wrangler-dist/cli.js.map +7 -0
  67. package/.npmignore +0 -15
  68. package/index.js +0 -250
  69. package/tests/is.spec.js +0 -1155
package/src/proxy.ts ADDED
@@ -0,0 +1,104 @@
1
+ import { connect } from "node:http2";
2
+ import { createServer } from "node:http";
3
+ import type {
4
+ Server,
5
+ IncomingHttpHeaders,
6
+ OutgoingHttpHeaders,
7
+ RequestListener,
8
+ } from "node:http";
9
+ import WebSocket from "faye-websocket";
10
+ import serveStatic from "serve-static";
11
+
12
+ export interface HttpProxyInit {
13
+ host: string;
14
+ assetPath?: string | null;
15
+ onRequest?: (headers: IncomingHttpHeaders) => void;
16
+ onResponse?: (headers: OutgoingHttpHeaders) => void;
17
+ }
18
+
19
+ /**
20
+ * Creates a HTTP/1 proxy that sends requests over HTTP/2.
21
+ */
22
+ export function createHttpProxy(init: HttpProxyInit): Server {
23
+ const { host, assetPath, onRequest = () => {}, onResponse = () => {} } = init;
24
+ const remote = connect(`https://${host}`);
25
+ const local = createServer();
26
+ // HTTP/2 -> HTTP/2
27
+ local.on("stream", (stream, headers: IncomingHttpHeaders) => {
28
+ onRequest(headers);
29
+ headers[":authority"] = host;
30
+ const request = stream.pipe(remote.request(headers));
31
+ request.on("response", (headers: OutgoingHttpHeaders) => {
32
+ onResponse(headers);
33
+ stream.respond(headers);
34
+ request.pipe(stream, { end: true });
35
+ });
36
+ });
37
+ // HTTP/1 -> HTTP/2
38
+ const handleRequest: RequestListener = (message, response) => {
39
+ const { httpVersionMajor, headers, method, url } = message;
40
+ if (httpVersionMajor >= 2) {
41
+ return; // Already handled by the "stream" event.
42
+ }
43
+ onRequest(headers);
44
+ headers[":method"] = method;
45
+ headers[":path"] = url;
46
+ headers[":authority"] = host;
47
+ headers[":scheme"] = "https";
48
+ for (const name of Object.keys(headers)) {
49
+ if (HTTP1_HEADERS.has(name.toLowerCase())) {
50
+ delete headers[name];
51
+ }
52
+ }
53
+ const request = message.pipe(remote.request(headers));
54
+ request.on("response", (headers) => {
55
+ const status = headers[":status"];
56
+ onResponse(headers);
57
+ for (const name of Object.keys(headers)) {
58
+ if (name.startsWith(":")) {
59
+ delete headers[name];
60
+ }
61
+ }
62
+ response.writeHead(status, headers);
63
+ request.pipe(response, { end: true });
64
+ });
65
+ };
66
+ // If an asset path is defined, check the file system
67
+ // for a file first and serve if it exists.
68
+ if (assetPath) {
69
+ const handleAsset = serveStatic(assetPath, {
70
+ cacheControl: false,
71
+ });
72
+ local.on("request", (request, response) => {
73
+ handleAsset(request, response, () => {
74
+ handleRequest(request, response);
75
+ });
76
+ });
77
+ } else {
78
+ local.on("request", handleRequest);
79
+ }
80
+ // HTTP/1 -> WebSocket (over HTTP/1)
81
+ local.on("upgrade", (message, socket, body) => {
82
+ const { headers, url } = message;
83
+ onRequest(headers);
84
+ headers["host"] = host;
85
+ const local = new WebSocket(message, socket, body);
86
+ // TODO(soon): Custom WebSocket protocol is not working?
87
+ const remote = new WebSocket.Client(`wss://${host}${url}`, [], { headers });
88
+ local.pipe(remote).pipe(local);
89
+ });
90
+ remote.on("close", () => {
91
+ local.close();
92
+ });
93
+ return local;
94
+ }
95
+
96
+ const HTTP1_HEADERS = new Set([
97
+ "host",
98
+ "connection",
99
+ "upgrade",
100
+ "keep-alive",
101
+ "proxy-connection",
102
+ "transfer-encoding",
103
+ "http2-settings",
104
+ ]);
package/src/publish.ts ADDED
@@ -0,0 +1,358 @@
1
+ import type { CfWorkerInit } from "./api/worker";
2
+ import { toFormData } from "./api/form_data";
3
+ import esbuild from "esbuild";
4
+ import tmp from "tmp-promise";
5
+ import type { Config } from "./config";
6
+ import path from "path";
7
+ import { readFile } from "fs/promises";
8
+ import cfetch from "./cfetch";
9
+ import assert from "node:assert";
10
+ import { syncAssets } from "./sites";
11
+ import makeModuleCollector from "./module-collection";
12
+ import { execa } from "execa";
13
+
14
+ type CfScriptFormat = void | "modules" | "service-worker";
15
+
16
+ type Props = {
17
+ config: Config;
18
+ format?: CfScriptFormat;
19
+ script?: string;
20
+ name?: string;
21
+ env?: string;
22
+ public?: string;
23
+ site?: string;
24
+ triggers?: (string | number)[];
25
+ routes?: (string | number)[];
26
+ legacyEnv?: boolean;
27
+ jsxFactory: void | string;
28
+ jsxFragment: void | string;
29
+ };
30
+
31
+ function sleep(ms: number) {
32
+ return new Promise((resolve) => setTimeout(resolve, ms));
33
+ }
34
+
35
+ export default async function publish(props: Props): Promise<void> {
36
+ if (props.public && props.format === "service-worker") {
37
+ // TODO: check config too
38
+ throw new Error(
39
+ "You cannot use the service worker format with a public directory."
40
+ );
41
+ }
42
+ // TODO: warn if git/hg has uncommitted changes
43
+ const { config } = props;
44
+ const {
45
+ account_id: accountId,
46
+ build,
47
+ // @ts-expect-error hidden
48
+ __path__,
49
+ } = config;
50
+
51
+ const triggers = props.triggers || config.triggers?.crons;
52
+ const routes = props.routes || config.routes;
53
+
54
+ const jsxFactory = props.jsxFactory || config.jsx_factory;
55
+ const jsxFragment = props.jsxFragment || config.jsx_fragment;
56
+
57
+ assert(config.account_id, "missing account id");
58
+
59
+ let scriptName = props.name || config.name;
60
+ assert(
61
+ scriptName,
62
+ '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>"`'
63
+ );
64
+
65
+ let file: string;
66
+ if (props.script) {
67
+ file = props.script;
68
+ } else {
69
+ assert(build?.upload?.main, "missing main file");
70
+ file = path.join(path.dirname(__path__), build.upload.main);
71
+ }
72
+
73
+ if (props.legacyEnv) {
74
+ scriptName += props.env ? `-${props.env}` : "";
75
+ }
76
+ const envName = props.env ?? "production";
77
+
78
+ const destination = await tmp.dir({ unsafeCleanup: true });
79
+
80
+ if (props.config.build?.command) {
81
+ // TODO: add a deprecation message here?
82
+ console.log("running:", props.config.build.command);
83
+ const buildCommandPieces = props.config.build.command.split(" ");
84
+ await execa(buildCommandPieces[0], buildCommandPieces.slice(1), {
85
+ stdout: "inherit",
86
+ stderr: "inherit",
87
+ ...(props.config.build?.cwd && { cwd: props.config.build.cwd }),
88
+ });
89
+ }
90
+
91
+ const moduleCollector = makeModuleCollector();
92
+ const result = await esbuild.build({
93
+ ...(props.public
94
+ ? {
95
+ stdin: {
96
+ contents: (
97
+ await readFile(
98
+ path.join(__dirname, "../static-asset-facade.js"),
99
+ "utf8"
100
+ )
101
+ ).replace("__ENTRY_POINT__", path.join(process.cwd(), file)),
102
+ sourcefile: "static-asset-facade.js",
103
+ resolveDir: path.dirname(file),
104
+ },
105
+ }
106
+ : { entryPoints: [file] }),
107
+ bundle: true,
108
+ nodePaths: props.public ? [path.join(__dirname, "../vendor")] : undefined,
109
+ outdir: destination.path,
110
+ external: ["__STATIC_CONTENT_MANIFEST"],
111
+ format: "esm",
112
+ sourcemap: true,
113
+ metafile: true,
114
+ conditions: ["worker", "browser"],
115
+ loader: {
116
+ ".js": "jsx",
117
+ },
118
+ plugins: [moduleCollector.plugin],
119
+ ...(jsxFactory && { jsxFactory }),
120
+ ...(jsxFragment && { jsxFragment }),
121
+ });
122
+
123
+ const chunks = Object.entries(result.metafile.outputs).find(
124
+ ([_path, { entryPoint }]) =>
125
+ entryPoint ===
126
+ (props.public
127
+ ? path.join(path.dirname(file), "static-asset-facade.js")
128
+ : file)
129
+ );
130
+
131
+ const { format } = props;
132
+ const bundle = {
133
+ type: chunks[1].exports.length > 0 ? "esm" : "commonjs",
134
+ exports: chunks[1].exports,
135
+ };
136
+
137
+ // TODO: instead of bundling the facade with the worker, we should just bundle the worker and expose it as a module.
138
+ // That way we'll be able to accurately tell if this is a service worker or not.
139
+
140
+ if (format === "modules" && bundle.type === "commonjs") {
141
+ console.error("⎔ Cannot use modules with a commonjs bundle.");
142
+ // TODO: a much better error message here, with what to do next
143
+ return;
144
+ }
145
+ if (format === "service-worker" && bundle.type !== "esm") {
146
+ console.error("⎔ Cannot use service-worker with a esm bundle.");
147
+ // TODO: a much better error message here, with what to do next
148
+ return;
149
+ }
150
+
151
+ const content = await readFile(chunks[0], { encoding: "utf-8" });
152
+ await destination.cleanup();
153
+
154
+ // if config.migrations
155
+ // get current migration tag
156
+ let migrations;
157
+ if ("migrations" in config) {
158
+ const scripts = await cfetch<{ id: string; migration_tag: string }[]>(
159
+ `/accounts/${accountId}/workers/scripts`
160
+ );
161
+ const script = scripts.find((script) => script.id === scriptName);
162
+ if (script?.migration_tag) {
163
+ // was already published once
164
+ const foundIndex = config.migrations.findIndex(
165
+ (migration) => migration.tag === script.migration_tag
166
+ );
167
+ if (foundIndex === -1) {
168
+ console.warn(
169
+ `The published script ${scriptName} has a migration tag "${script.migration_tag}, which was not found in wrangler.toml. You may have already delated it. Applying all available migrations to the script...`
170
+ );
171
+ migrations = {
172
+ old_tag: script.migration_tag,
173
+ new_tag: config.migrations[config.migrations.length - 1].tag,
174
+ steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
175
+ };
176
+ } else {
177
+ migrations = {
178
+ old_tag: script.migration_tag,
179
+ new_tag: config.migrations[config.migrations.length - 1].tag,
180
+ steps: config.migrations
181
+ .slice(foundIndex + 1)
182
+ .map(({ tag: _tag, ...rest }) => rest),
183
+ };
184
+ }
185
+ } else {
186
+ migrations = {
187
+ new_tag: config.migrations[config.migrations.length - 1].tag,
188
+ steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
189
+ };
190
+ }
191
+ }
192
+
193
+ const assets =
194
+ props.public || props.site || props.config.site?.bucket // TODO: allow both
195
+ ? await syncAssets(
196
+ accountId,
197
+ scriptName,
198
+ props.public || props.site || props.config.site?.bucket,
199
+ false
200
+ )
201
+ : { manifest: undefined, namespace: undefined };
202
+
203
+ const envRootObj = props.env ? config.env[props.env] || {} : config;
204
+
205
+ const worker: CfWorkerInit = {
206
+ name: scriptName,
207
+ main: {
208
+ name: path.basename(chunks[0]),
209
+ content: content,
210
+ type: bundle.type === "esm" ? "esm" : "commonjs",
211
+ },
212
+ variables: {
213
+ ...(envRootObj?.vars || {}),
214
+ ...(envRootObj?.kv_namespaces || []).reduce(
215
+ (obj, { binding, preview_id: _preview_id, id }) => {
216
+ return { ...obj, [binding]: { namespaceId: id } };
217
+ },
218
+ {}
219
+ ),
220
+ ...(envRootObj?.durable_objects?.bindings || []).reduce(
221
+ (obj, { name, class_name, script_name }) => {
222
+ return {
223
+ ...obj,
224
+ [name]: { class_name, ...(script_name && { script_name }) },
225
+ };
226
+ },
227
+ {}
228
+ ),
229
+ ...(assets.namespace
230
+ ? { __STATIC_CONTENT: { namespaceId: assets.namespace } }
231
+ : {}),
232
+ },
233
+ ...(migrations && { migrations }),
234
+ modules: assets.manifest
235
+ ? moduleCollector.modules.concat({
236
+ name: "__STATIC_CONTENT_MANIFEST",
237
+ content: JSON.stringify(assets.manifest),
238
+ type: "text",
239
+ })
240
+ : moduleCollector.modules,
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 propogate.
258
+ const { available_on_subdomain } = await cfetch(
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 cfetch<{ 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
+ cfetch(`${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
+ cfetch(`${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
+ cfetch(`${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
+ }
package/src/sites.tsx ADDED
@@ -0,0 +1,115 @@
1
+ import { readdir, readFile } from "node:fs/promises";
2
+ import { createReadStream } from "node:fs";
3
+ import cfetch from "./cfetch";
4
+ import { listNamespaceKeys, listNamespaces, putBulkKeyValue } from "./kv";
5
+
6
+ import * as path from "path";
7
+ import crypto from "node:crypto";
8
+
9
+ async function* getFilesInFolder(dirPath: string): AsyncIterable<string> {
10
+ const files = await readdir(dirPath, { withFileTypes: true });
11
+ for (const file of files) {
12
+ if (file.isDirectory()) {
13
+ yield* await getFilesInFolder(path.join(dirPath, file.name));
14
+ } else {
15
+ yield path.join(dirPath, file.name);
16
+ }
17
+ }
18
+ }
19
+
20
+ async function hashFileContent(filePath: string): Promise<string> {
21
+ return new Promise((resolve, reject) => {
22
+ const hash = crypto.createHash("sha1");
23
+ const rs = createReadStream(filePath);
24
+ rs.on("error", reject);
25
+ rs.on("data", (chunk) => hash.update(chunk));
26
+ rs.on("end", () => resolve(hash.digest("hex")));
27
+ });
28
+ }
29
+
30
+ async function hashFile(filePath: string): Promise<{
31
+ filePath: string;
32
+ hash: string;
33
+ }> {
34
+ const extName = path.extname(filePath);
35
+ const baseName = path.basename(filePath, extName);
36
+ const hash = await hashFileContent(filePath);
37
+ return {
38
+ filePath: `${baseName}.${hash}${extName || ""}`,
39
+ hash,
40
+ };
41
+ }
42
+
43
+ async function createKVNamespaceIfNotAlreadyExisting(
44
+ title: string,
45
+ accountId: string
46
+ ) {
47
+ // check if it already exists
48
+ // TODO: this is super inefficient, should be made better
49
+ const namespaces = await listNamespaces(accountId);
50
+ const found = namespaces.find((x) => x.title === title);
51
+ if (found) {
52
+ return { created: false, id: found.id };
53
+ }
54
+
55
+ // else we make the namespace
56
+ // TODO: use an export from ./kv
57
+ const json = await cfetch<{ id: string }>(
58
+ `/accounts/${accountId}/storage/kv/namespaces`,
59
+ {
60
+ method: "POST",
61
+ headers: {
62
+ "Content-Type": "application/json",
63
+ },
64
+ body: JSON.stringify({ title }),
65
+ }
66
+ );
67
+
68
+ return {
69
+ created: true,
70
+ id: json.id,
71
+ };
72
+ }
73
+
74
+ export async function syncAssets(
75
+ accountId: string,
76
+ scriptName: string,
77
+ dirPath: string,
78
+ preview: boolean,
79
+ _env?: string
80
+ ) {
81
+ const title = `__${scriptName}_sites_assets${preview ? "_preview" : ""}`;
82
+ const { id: namespace } = await createKVNamespaceIfNotAlreadyExisting(
83
+ title,
84
+ accountId
85
+ );
86
+
87
+ // let's get all the keys in this namespace
88
+ const keys = new Set(
89
+ (await listNamespaceKeys(accountId, namespace)).map((x) => x.name)
90
+ );
91
+
92
+ const manifest = {};
93
+ const upload = [];
94
+ // TODO: this can be more efficient by parallelising
95
+ for await (const file of getFilesInFolder(dirPath)) {
96
+ // TODO: "exclude:" config
97
+ const { filePath } = await hashFile(file);
98
+ // now put each of the files into kv
99
+ if (!keys.has(filePath)) {
100
+ console.log(`uploading ${file}...`);
101
+ const content = await readFile(file, "base64");
102
+ if (content.length > 25 * 1024 * 1024) {
103
+ throw new Error(`File ${file} is too big, it should be under 25 mb.`);
104
+ }
105
+ upload.push({
106
+ key: filePath,
107
+ value: content,
108
+ base64: true,
109
+ });
110
+ }
111
+ manifest[path.relative(dirPath, file)] = filePath;
112
+ }
113
+ await putBulkKeyValue(accountId, namespace, JSON.stringify(upload));
114
+ return { manifest, namespace };
115
+ }
package/src/tail.tsx ADDED
@@ -0,0 +1,71 @@
1
+ import WebSocket from "ws";
2
+ import cfetch from "./cfetch";
3
+ import { version as packageVersion } from "../package.json";
4
+
5
+ export type TailApiResponse = {
6
+ id: string;
7
+ url: string;
8
+ expires_at: Date;
9
+ };
10
+
11
+ function makeCreateTailUrl(accountId: string, workerName: string): string {
12
+ return `/accounts/${accountId}/workers/scripts/${workerName}/tails`;
13
+ }
14
+
15
+ function makeDeleteTailUrl(
16
+ accountId: string,
17
+ workerName: string,
18
+ tailId: string
19
+ ): string {
20
+ return `/accounts/${accountId}/workers/scripts/${workerName}/tails/${tailId}`;
21
+ }
22
+
23
+ /// Creates a tail, but doesn't connect to it.
24
+ async function createTailButDontConnect(
25
+ accountId: string,
26
+ workerName: string
27
+ ): Promise<TailApiResponse> {
28
+ const createTailUrl = makeCreateTailUrl(accountId, workerName);
29
+ /// https://api.cloudflare.com/#worker-tail-logs-start-tail
30
+ return await cfetch<TailApiResponse>(createTailUrl, { method: "POST" });
31
+ }
32
+
33
+ export async function createTail(
34
+ accountId: string,
35
+ workerName: string,
36
+ _filters: Filters
37
+ ): Promise<{
38
+ tail: WebSocket;
39
+ expiration: Date;
40
+ deleteTail: () => Promise<void>;
41
+ }> {
42
+ const {
43
+ id: tailId,
44
+ url: websocketUrl,
45
+ expires_at: expiration,
46
+ } = await createTailButDontConnect(accountId, workerName);
47
+ const deleteUrl = makeDeleteTailUrl(accountId, workerName, tailId);
48
+
49
+ // deletes the tail
50
+ async function deleteTail() {
51
+ await cfetch(deleteUrl, { method: "DELETE" });
52
+ }
53
+
54
+ const tail = new WebSocket(websocketUrl, "trace-v1", {
55
+ headers: {
56
+ "Sec-WebSocket-Protocol": "trace-v1", // needs to be `trace-v1` to be accepted
57
+ "User-Agent": `wrangler-js/${packageVersion}`,
58
+ },
59
+ });
60
+
61
+ // TODO: send filters as well
62
+ return { tail, expiration, deleteTail };
63
+ }
64
+
65
+ export type Filters = {
66
+ status?: "ok" | "error" | "canceled";
67
+ header?: string;
68
+ method?: string;
69
+ "sampling-rate"?: number;
70
+ search?: string;
71
+ };