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.
Files changed (108) hide show
  1. package/README.md +50 -15
  2. package/bin/wrangler.js +94 -31
  3. package/config-schema.json +3074 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +155 -75
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +19 -0
  11. package/templates/gitignore +170 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  21. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  22. package/templates/middleware/middleware-pretty-error.ts +40 -0
  23. package/templates/middleware/middleware-scheduled.ts +29 -0
  24. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  25. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  26. package/templates/modules-watch-stub.js +4 -0
  27. package/templates/new-worker-scheduled.js +17 -0
  28. package/templates/new-worker-scheduled.ts +32 -0
  29. package/templates/new-worker.js +15 -0
  30. package/templates/new-worker.ts +33 -0
  31. package/templates/no-op-worker.js +10 -0
  32. package/templates/pages-dev-pipeline.ts +32 -0
  33. package/templates/pages-dev-util.ts +55 -0
  34. package/templates/pages-shim.ts +9 -0
  35. package/templates/pages-template-plugin.ts +190 -0
  36. package/templates/pages-template-worker.ts +198 -0
  37. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  38. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  39. package/templates/tsconfig-sanity.ts +11 -0
  40. package/templates/tsconfig.init.json +22 -0
  41. package/templates/tsconfig.json +8 -0
  42. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  43. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  44. package/wrangler-dist/ProxyWorker.js +241 -0
  45. package/wrangler-dist/ProxyWorker.js.map +6 -0
  46. package/wrangler-dist/cli.d.ts +26463 -0
  47. package/wrangler-dist/cli.js +206335 -125492
  48. package/wrangler-dist/wasm-sync.wasm +0 -0
  49. package/import_meta_url.js +0 -3
  50. package/miniflare-config-stubs/.env.empty +0 -0
  51. package/miniflare-config-stubs/package.empty.json +0 -1
  52. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  53. package/src/__tests__/clipboardy-mock.js +0 -4
  54. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  55. package/src/__tests__/index.test.ts +0 -153
  56. package/src/__tests__/mock-cfetch.js +0 -46
  57. package/src/api/form_data.ts +0 -158
  58. package/src/api/inspect.ts +0 -441
  59. package/src/api/preview.ts +0 -123
  60. package/src/api/worker.ts +0 -161
  61. package/src/cfetch.ts +0 -72
  62. package/src/cli.ts +0 -10
  63. package/src/config.ts +0 -124
  64. package/src/dev.tsx +0 -736
  65. package/src/dialogs.tsx +0 -85
  66. package/src/index.tsx +0 -1845
  67. package/src/kv.tsx +0 -211
  68. package/src/pages.tsx +0 -344
  69. package/src/publish.ts +0 -354
  70. package/src/sites.tsx +0 -115
  71. package/src/tail.tsx +0 -71
  72. package/src/user.tsx +0 -1029
  73. package/src/util/fetch.ts +0 -74
  74. package/static-asset-facade.js +0 -47
  75. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  76. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  77. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  78. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  79. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  80. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  81. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  82. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  83. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  84. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  85. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  86. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  87. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  88. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  89. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  90. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  91. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  92. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  93. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  94. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  95. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  96. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  97. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  98. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  99. package/vendor/wrangler-mime/LICENSE +0 -21
  100. package/vendor/wrangler-mime/Mime.js +0 -97
  101. package/vendor/wrangler-mime/README.md +0 -187
  102. package/vendor/wrangler-mime/cli.js +0 -46
  103. package/vendor/wrangler-mime/index.js +0 -4
  104. package/vendor/wrangler-mime/lite.js +0 -4
  105. package/vendor/wrangler-mime/package.json +0 -52
  106. package/vendor/wrangler-mime/types/other.js +0 -1
  107. package/vendor/wrangler-mime/types/standard.js +0 -1
  108. package/wrangler-dist/cli.js.map +0 -7
package/src/publish.ts DELETED
@@ -1,354 +0,0 @@
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 NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
12
- import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
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
- polyfillNode: void | boolean;
30
- };
31
-
32
- function sleep(ms: number) {
33
- return new Promise((resolve) => setTimeout(resolve, ms));
34
- }
35
-
36
- export default async function publish(props: Props): Promise<void> {
37
- if (props.public && props.format === "service-worker") {
38
- // TODO: check config too
39
- throw new Error(
40
- "You cannot use the service worker format with a public directory."
41
- );
42
- }
43
- // TODO: warn if git/hg has uncommitted changes
44
- const { config } = props;
45
- const {
46
- account_id: accountId,
47
- build,
48
- // @ts-expect-error hidden
49
- __path__,
50
- } = config;
51
-
52
- const triggers = props.triggers || config.triggers?.crons;
53
- const routes = props.routes || config.routes;
54
-
55
- const jsxFactory = props.jsxFactory || config.jsx_factory;
56
- const jsxFragment = props.jsxFragment || config.jsx_fragment;
57
-
58
- assert(config.account_id, "missing account id");
59
-
60
- let scriptName = props.name || config.name;
61
- assert(
62
- scriptName,
63
- '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>"`'
64
- );
65
-
66
- let file: string;
67
- if (props.script) {
68
- file = props.script;
69
- } else {
70
- assert(build?.upload?.main, "missing main file");
71
- file = path.join(path.dirname(__path__), build.upload.main);
72
- }
73
-
74
- if (props.legacyEnv) {
75
- scriptName += props.env ? `-${props.env}` : "";
76
- }
77
- const envName = props.env ?? "production";
78
-
79
- const destination = await tmp.dir({ unsafeCleanup: true });
80
-
81
- const result = await esbuild.build({
82
- ...(props.public
83
- ? {
84
- stdin: {
85
- contents: (
86
- await readFile(
87
- path.join(__dirname, "../static-asset-facade.js"),
88
- "utf8"
89
- )
90
- ).replace("__ENTRY_POINT__", path.join(process.cwd(), file)),
91
- sourcefile: "static-asset-facade.js",
92
- resolveDir: path.dirname(file),
93
- },
94
- }
95
- : { entryPoints: [file] }),
96
- bundle: true,
97
- define: {
98
- ...((props.polyfillNode ?? config.polyfill_node) && {
99
- global: "globalThis",
100
- }),
101
- },
102
- nodePaths: props.public ? [path.join(__dirname, "../vendor")] : undefined,
103
- outdir: destination.path,
104
- external: ["__STATIC_CONTENT_MANIFEST"],
105
- format: "esm",
106
- plugins:
107
- props.polyfillNode ?? config.polyfill_node
108
- ? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
109
- : undefined,
110
- sourcemap: true,
111
- metafile: true,
112
- loader: {
113
- ".js": "jsx",
114
- },
115
- ...(jsxFactory && { jsxFactory }),
116
- ...(jsxFragment && { jsxFragment }),
117
- });
118
-
119
- const chunks = Object.entries(result.metafile.outputs).find(
120
- ([_path, { entryPoint }]) =>
121
- entryPoint ===
122
- (props.public
123
- ? path.join(path.dirname(file), "static-asset-facade.js")
124
- : file)
125
- );
126
-
127
- const { format } = props;
128
- const bundle = {
129
- type: chunks[1].exports.length > 0 ? "esm" : "commonjs",
130
- exports: chunks[1].exports,
131
- };
132
-
133
- // TODO: instead of bundling the facade with the worker, we should just bundle the worker and expose it as a module.
134
- // That way we'll be able to accurately tell if this is a service worker or not.
135
-
136
- if (format === "modules" && bundle.type === "commonjs") {
137
- console.error("⎔ Cannot use modules with a commonjs bundle.");
138
- // TODO: a much better error message here, with what to do next
139
- return;
140
- }
141
- if (format === "service-worker" && bundle.type !== "esm") {
142
- console.error("⎔ Cannot use service-worker with a esm bundle.");
143
- // TODO: a much better error message here, with what to do next
144
- return;
145
- }
146
-
147
- const content = await readFile(chunks[0], { encoding: "utf-8" });
148
- destination.cleanup();
149
-
150
- // if config.migrations
151
- // get current migration tag
152
- let migrations;
153
- if ("migrations" in config) {
154
- const scripts = await cfetch<{ id: string; migration_tag: string }[]>(
155
- `/accounts/${accountId}/workers/scripts`
156
- );
157
- const script = scripts.find((script) => script.id === scriptName);
158
- if (script?.migration_tag) {
159
- // was already published once
160
- const foundIndex = config.migrations.findIndex(
161
- (migration) => migration.tag === script.migration_tag
162
- );
163
- if (foundIndex === -1) {
164
- console.warn(
165
- `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...`
166
- );
167
- migrations = {
168
- old_tag: script.migration_tag,
169
- new_tag: config.migrations[config.migrations.length - 1].tag,
170
- steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
171
- };
172
- } else {
173
- migrations = {
174
- old_tag: script.migration_tag,
175
- new_tag: config.migrations[config.migrations.length - 1].tag,
176
- steps: config.migrations
177
- .slice(foundIndex + 1)
178
- .map(({ tag: _tag, ...rest }) => rest),
179
- };
180
- }
181
- } else {
182
- migrations = {
183
- new_tag: config.migrations[config.migrations.length - 1].tag,
184
- steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
185
- };
186
- }
187
- }
188
-
189
- const assets =
190
- props.public || props.site || props.config.site?.bucket // TODO: allow both
191
- ? await syncAssets(
192
- accountId,
193
- scriptName,
194
- props.public || props.site || props.config.site?.bucket,
195
- false
196
- )
197
- : { manifest: undefined, namespace: undefined };
198
-
199
- const envRootObj = props.env ? config.env[props.env] || {} : config;
200
-
201
- const worker: CfWorkerInit = {
202
- name: scriptName,
203
- main: {
204
- name: path.basename(chunks[0]),
205
- content: content,
206
- type: bundle.type === "esm" ? "esm" : "commonjs",
207
- },
208
- variables: {
209
- ...(envRootObj?.vars || {}),
210
- ...(envRootObj?.kv_namespaces || []).reduce(
211
- (obj, { binding, preview_id: _preview_id, id }) => {
212
- return { ...obj, [binding]: { namespaceId: id } };
213
- },
214
- {}
215
- ),
216
- ...(envRootObj?.durable_objects?.bindings || []).reduce(
217
- (obj, { name, class_name, script_name }) => {
218
- return {
219
- ...obj,
220
- [name]: { class_name, ...(script_name && { script_name }) },
221
- };
222
- },
223
- {}
224
- ),
225
- ...(assets.namespace
226
- ? { __STATIC_CONTENT: { namespaceId: assets.namespace } }
227
- : {}),
228
- },
229
- ...(migrations && { migrations }),
230
- modules: assets.manifest
231
- ? [].concat({
232
- name: "__STATIC_CONTENT_MANIFEST",
233
- content: JSON.stringify(assets.manifest),
234
- type: "text",
235
- })
236
- : [],
237
- compatibility_date: config.compatibility_date,
238
- compatibility_flags: config.compatibility_flags,
239
- usage_model: config.usage_model,
240
- };
241
-
242
- const start = Date.now();
243
- function formatTime(duration: number) {
244
- return `(${(duration / 1000).toFixed(2)} sec)`;
245
- }
246
-
247
- const notProd = !props.legacyEnv && props.env;
248
- const workerName = notProd ? `${scriptName} (${envName})` : scriptName;
249
- const workerUrl = notProd
250
- ? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
251
- : `/accounts/${accountId}/workers/scripts/${scriptName}`;
252
-
253
- // Upload the script so it has time to propogate.
254
- const { available_on_subdomain } = await cfetch(
255
- `${workerUrl}?available_on_subdomain=true`,
256
- {
257
- method: "PUT",
258
- // @ts-expect-error: TODO: fix this type error!
259
- body: toFormData(worker),
260
- }
261
- );
262
-
263
- const uploadMs = Date.now() - start;
264
- console.log("Uploaded", workerName, formatTime(uploadMs));
265
- const deployments: Promise<string[]>[] = [];
266
-
267
- const userSubdomain = (
268
- await cfetch<{ subdomain: string }>(
269
- `/accounts/${accountId}/workers/subdomain`
270
- )
271
- ).subdomain;
272
-
273
- const scriptURL =
274
- props.legacyEnv || !props.env
275
- ? `${scriptName}.${userSubdomain}.workers.dev`
276
- : `${envName}.${scriptName}.${userSubdomain}.workers.dev`;
277
-
278
- // Enable the `workers.dev` subdomain.
279
- // TODO: Make this configurable.
280
- if (!available_on_subdomain) {
281
- deployments.push(
282
- cfetch(`${workerUrl}/subdomain`, {
283
- method: "POST",
284
- body: JSON.stringify({ enabled: true }),
285
- headers: {
286
- "Content-Type": "application/json",
287
- },
288
- })
289
- .then(() => [scriptURL])
290
- // Add a delay when the subdomain is first created.
291
- // This is to prevent an issue where a negative cache-hit
292
- // causes the subdomain to be unavailable for 30 seconds.
293
- // This is a temporary measure until we fix this on the edge.
294
- .then((url) => {
295
- sleep(3000);
296
- return url;
297
- })
298
- );
299
- } else {
300
- deployments.push(Promise.resolve([scriptURL]));
301
- }
302
-
303
- // Update routing table for the script.
304
- if (routes && routes.length) {
305
- deployments.push(
306
- cfetch(`${workerUrl}/routes`, {
307
- // TODO: PATCH will not delete previous routes on this script,
308
- // whereas PUT will. We need to decide on the default behaviour
309
- // and how to configure it.
310
- method: "PUT",
311
- body: JSON.stringify(routes.map((pattern) => ({ pattern }))),
312
- headers: {
313
- "Content-Type": "application/json",
314
- },
315
- }).then(() => {
316
- if (routes.length > 10) {
317
- return routes
318
- .slice(0, 9)
319
- .map(String)
320
- .concat([`...and ${routes.length - 10} more routes`]);
321
- }
322
- return routes.map(String);
323
- })
324
- );
325
- }
326
-
327
- // Configure any schedules for the script.
328
- // TODO: rename this to `schedules`?
329
- if (triggers && triggers.length) {
330
- deployments.push(
331
- cfetch(`${workerUrl}/schedules`, {
332
- // TODO: Unlike routes, this endpoint does not support PATCH.
333
- // So technically, this will override any previous schedules.
334
- // We should change the endpoint to support PATCH.
335
- method: "PUT",
336
- body: JSON.stringify(triggers.map((cron) => ({ cron }))),
337
- headers: {
338
- "Content-Type": "application/json",
339
- },
340
- }).then(() => triggers.map(String))
341
- );
342
- }
343
-
344
- if (!deployments.length) {
345
- return;
346
- }
347
-
348
- const targets = await Promise.all(deployments);
349
- const deployMs = Date.now() - start - uploadMs;
350
- console.log("Deployed", workerName, formatTime(deployMs));
351
- for (const target of targets.flat()) {
352
- console.log(" ", target);
353
- }
354
- }
package/src/sites.tsx DELETED
@@ -1,115 +0,0 @@
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 DELETED
@@ -1,71 +0,0 @@
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
- };