wrangler 0.0.0-e9ab55a → 0.0.0-e9e9872

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 (201) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +33 -14
  3. package/bin/wrangler.js +126 -25
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +4894 -0
  6. package/package.json +159 -125
  7. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  8. package/src/__tests__/config-cache.test.ts +42 -0
  9. package/src/__tests__/configuration.test.ts +3946 -0
  10. package/src/__tests__/dev.test.tsx +1155 -63
  11. package/src/__tests__/guess-worker-format.test.ts +85 -0
  12. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  13. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  14. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  15. package/src/__tests__/helpers/mock-bin.ts +36 -0
  16. package/src/__tests__/helpers/mock-cfetch.ts +211 -0
  17. package/src/__tests__/helpers/mock-console.ts +97 -0
  18. package/src/__tests__/helpers/mock-dialogs.ts +153 -0
  19. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  20. package/src/__tests__/helpers/mock-istty.ts +74 -0
  21. package/src/__tests__/helpers/mock-kv.ts +33 -0
  22. package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
  23. package/src/__tests__/helpers/mock-process.ts +39 -0
  24. package/src/__tests__/helpers/mock-stdin.ts +108 -0
  25. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  26. package/src/__tests__/helpers/run-in-tmp.ts +38 -0
  27. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  28. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  29. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  30. package/src/__tests__/https-options.test.ts +163 -0
  31. package/src/__tests__/index.test.ts +259 -284
  32. package/src/__tests__/init.test.ts +1992 -0
  33. package/src/__tests__/jest.setup.ts +108 -10
  34. package/src/__tests__/kv.test.ts +1433 -909
  35. package/src/__tests__/logger.test.ts +141 -0
  36. package/src/__tests__/package-manager.test.ts +353 -0
  37. package/src/__tests__/pages.test.ts +1254 -0
  38. package/src/__tests__/parse.test.ts +330 -0
  39. package/src/__tests__/publish.test.ts +6586 -93
  40. package/src/__tests__/pubsub.test.ts +367 -0
  41. package/src/__tests__/r2.test.ts +233 -0
  42. package/src/__tests__/route.test.ts +45 -0
  43. package/src/__tests__/secret.test.ts +492 -0
  44. package/src/__tests__/tail.test.ts +698 -0
  45. package/src/__tests__/user.test.ts +143 -0
  46. package/src/__tests__/whoami.test.tsx +230 -0
  47. package/src/__tests__/worker-namespace.test.ts +327 -0
  48. package/src/abort.d.ts +3 -0
  49. package/src/api/dev.ts +52 -0
  50. package/src/api/index.ts +1 -0
  51. package/src/bundle-reporter.tsx +29 -0
  52. package/src/bundle.ts +212 -0
  53. package/src/cfetch/index.ts +88 -69
  54. package/src/cfetch/internal.ts +124 -44
  55. package/src/cli.ts +24 -6
  56. package/src/config/README.md +107 -0
  57. package/src/config/config.ts +243 -0
  58. package/src/config/diagnostics.ts +80 -0
  59. package/src/config/environment.ts +453 -0
  60. package/src/config/index.ts +238 -0
  61. package/src/config/validation-helpers.ts +584 -0
  62. package/src/config/validation.ts +1745 -0
  63. package/src/config-cache.ts +83 -0
  64. package/src/create-worker-preview.ts +255 -0
  65. package/src/create-worker-upload-form.ts +263 -0
  66. package/src/dev/dev-vars.ts +36 -0
  67. package/src/dev/dev.tsx +417 -0
  68. package/src/dev/local.tsx +354 -0
  69. package/src/dev/remote.tsx +382 -0
  70. package/src/dev/use-esbuild.ts +150 -0
  71. package/src/dev.tsx +527 -773
  72. package/src/dialogs.tsx +117 -59
  73. package/src/durable.ts +102 -0
  74. package/src/entry.ts +328 -0
  75. package/src/environment-variables.ts +35 -0
  76. package/src/errors.ts +11 -0
  77. package/src/faye-websocket.d.ts +6 -0
  78. package/src/generate.ts +33 -0
  79. package/src/git-client.ts +42 -0
  80. package/src/global-wrangler-config-path.ts +26 -0
  81. package/src/https-options.ts +127 -0
  82. package/src/index.tsx +1869 -1901
  83. package/src/init.ts +549 -0
  84. package/src/inspect.ts +615 -496
  85. package/src/intl-polyfill.d.ts +139 -0
  86. package/src/is-interactive.ts +12 -0
  87. package/src/kv.ts +430 -0
  88. package/src/logger.ts +73 -0
  89. package/src/miniflare-cli/README.md +30 -0
  90. package/src/miniflare-cli/enum-keys.ts +17 -0
  91. package/src/miniflare-cli/index.ts +53 -0
  92. package/src/miniflare-cli/request-context.ts +40 -0
  93. package/src/module-collection.ts +247 -53
  94. package/src/open-in-browser.ts +17 -0
  95. package/src/package-manager.ts +219 -0
  96. package/src/pages/build.tsx +202 -0
  97. package/src/pages/constants.ts +7 -0
  98. package/src/pages/deployments.tsx +101 -0
  99. package/src/pages/dev.tsx +964 -0
  100. package/src/pages/functions/buildPlugin.ts +105 -0
  101. package/src/pages/functions/buildWorker.ts +151 -0
  102. package/src/pages/functions/filepath-routing.test.ts +234 -0
  103. package/src/pages/functions/filepath-routing.ts +189 -0
  104. package/src/pages/functions/identifiers.ts +78 -0
  105. package/src/pages/functions/routes.ts +151 -0
  106. package/src/pages/index.tsx +84 -0
  107. package/src/pages/projects.tsx +157 -0
  108. package/src/pages/publish.tsx +335 -0
  109. package/src/pages/types.ts +40 -0
  110. package/src/pages/upload.tsx +396 -0
  111. package/src/pages/utils.ts +12 -0
  112. package/src/parse.ts +286 -0
  113. package/src/paths.ts +26 -0
  114. package/src/preview.ts +31 -0
  115. package/src/proxy.ts +428 -254
  116. package/src/publish.ts +764 -366
  117. package/src/pubsub/index.ts +286 -0
  118. package/src/pubsub/pubsub-commands.tsx +577 -0
  119. package/src/r2.ts +50 -0
  120. package/src/selfsigned.d.ts +29 -0
  121. package/src/sites.tsx +357 -103
  122. package/src/tail/filters.ts +279 -0
  123. package/src/tail/index.ts +299 -0
  124. package/src/tail/printing.ts +67 -0
  125. package/src/update-check.ts +19 -0
  126. package/src/user/choose-account.tsx +60 -0
  127. package/src/user/env-vars.ts +46 -0
  128. package/src/user/generate-auth-url.ts +33 -0
  129. package/src/user/generate-random-state.ts +16 -0
  130. package/src/user/index.ts +3 -0
  131. package/src/user/user.tsx +1170 -0
  132. package/src/whoami.tsx +90 -0
  133. package/src/worker-namespace.ts +190 -0
  134. package/src/worker.ts +191 -0
  135. package/src/zones.ts +76 -0
  136. package/templates/checked-fetch.js +17 -0
  137. package/templates/gitignore +171 -0
  138. package/templates/new-worker-scheduled.js +17 -0
  139. package/templates/new-worker-scheduled.ts +32 -0
  140. package/templates/new-worker.js +15 -0
  141. package/templates/new-worker.ts +30 -0
  142. package/templates/no-op-worker.js +10 -0
  143. package/templates/pages-template-plugin.ts +155 -0
  144. package/templates/pages-template-worker.ts +161 -0
  145. package/templates/static-asset-facade.js +49 -0
  146. package/templates/tsconfig.json +105 -0
  147. package/wrangler-dist/cli.d.ts +298 -0
  148. package/wrangler-dist/cli.js +107673 -103262
  149. package/pages/functions/buildWorker.ts +0 -62
  150. package/pages/functions/filepath-routing.test.ts +0 -39
  151. package/pages/functions/filepath-routing.ts +0 -228
  152. package/pages/functions/identifiers.ts +0 -78
  153. package/pages/functions/routes.ts +0 -158
  154. package/pages/functions/template-worker.ts +0 -136
  155. package/src/__tests__/mock-cfetch.ts +0 -171
  156. package/src/__tests__/mock-dialogs.ts +0 -65
  157. package/src/__tests__/run-in-tmp.ts +0 -19
  158. package/src/__tests__/run-wrangler.ts +0 -32
  159. package/src/api/form_data.ts +0 -131
  160. package/src/api/preview.ts +0 -128
  161. package/src/api/worker.ts +0 -155
  162. package/src/config.ts +0 -646
  163. package/src/kv.tsx +0 -276
  164. package/src/pages.tsx +0 -1040
  165. package/src/tail.tsx +0 -73
  166. package/src/user.tsx +0 -1027
  167. package/static-asset-facade.js +0 -47
  168. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  169. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  170. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  171. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  172. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  173. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  174. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  175. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  176. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  177. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  178. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  179. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  180. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  181. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  182. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  183. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  184. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  185. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  186. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  187. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  188. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  189. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  190. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  191. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  192. package/vendor/wrangler-mime/LICENSE +0 -21
  193. package/vendor/wrangler-mime/Mime.js +0 -97
  194. package/vendor/wrangler-mime/README.md +0 -187
  195. package/vendor/wrangler-mime/cli.js +0 -46
  196. package/vendor/wrangler-mime/index.js +0 -4
  197. package/vendor/wrangler-mime/lite.js +0 -4
  198. package/vendor/wrangler-mime/package.json +0 -52
  199. package/vendor/wrangler-mime/types/other.js +0 -1
  200. package/vendor/wrangler-mime/types/standard.js +0 -1
  201. package/wrangler-dist/cli.js.map +0 -7
@@ -1,171 +0,0 @@
1
- import type { RequestInit } from "node-fetch";
2
- import { URLSearchParams } from "node:url";
3
- import { pathToRegexp } from "path-to-regexp";
4
- import { CF_API_BASE_URL } from "../cfetch";
5
- import type { FetchResult } from "../cfetch";
6
-
7
- /**
8
- * The signature of the function that will handle a mock request.
9
- */
10
- export type MockHandler<ResponseType> = (
11
- uri: RegExpExecArray,
12
- init: RequestInit,
13
- queryParams: URLSearchParams
14
- ) => ResponseType;
15
-
16
- type RemoveMockFn = () => void;
17
-
18
- interface MockFetch<ResponseType> {
19
- regexp: RegExp;
20
- method: string | undefined;
21
- handler: MockHandler<ResponseType>;
22
- }
23
- const mocks: MockFetch<unknown>[] = [];
24
-
25
- /**
26
- * The mock implementation of `cfApi.fetch()`.
27
- *
28
- * This function will attempt to match the given request to one of the mock handlers configured by calls to `setMock`.
29
- *
30
- * Once found the handler will be used to generate a mock response.
31
- */
32
- export async function mockFetchInternal(
33
- resource: string,
34
- init: RequestInit = {},
35
- queryParams: URLSearchParams = new URLSearchParams()
36
- ) {
37
- for (const { regexp, method, handler } of mocks) {
38
- const resourcePath = new URL(resource, CF_API_BASE_URL).pathname;
39
- const uri = regexp.exec(resourcePath);
40
- // Do the resource path and (if specified) the HTTP method match?
41
- if (uri !== null && (!method || method === init.method)) {
42
- // The `resource` regular expression will extract the labelled groups from the URL.
43
- // These are passed through to the `handler` call, to allow it to do additional checks or behaviour.
44
- return handler(uri, init, queryParams); // TODO: should we have some kind of fallthrough system? we'll see.
45
- }
46
- }
47
- throw new Error(`no mocks found for ${init.method}: ${resource}`);
48
- }
49
-
50
- /**
51
- * Specify an expected resource path that is to be handled, resulting in a raw JSON response.
52
- *
53
- * @param resource The path of the resource to be matched.
54
- * This can include wildcards whose value will be passed to the `handler`.
55
- * @param handler The function that will generate the mock response for this request.
56
- */
57
- export function setMockRawResponse<ResponseType>(
58
- resource: string,
59
- handler: MockHandler<ResponseType>
60
- ): RemoveMockFn;
61
- /**
62
- * Specify an expected resource path that is to be handled, resulting in a raw JSON response.
63
- *
64
- * @param resource The path of the resource to be matched.
65
- * This can include wildcards whose value will be passed to the `handler`.
66
- * @param method The HTTP method (e.g. GET, POST, etc) that the request must have to match this mock handler.
67
- * @param handler The function that will generate the mock response for this request.
68
- */
69
- export function setMockRawResponse<ResponseType>(
70
- resource: string,
71
- method: string,
72
- handler: MockHandler<ResponseType>
73
- ): RemoveMockFn;
74
- /**
75
- * Specify an expected resource path that is to be handled, resulting in a raw JSON response.
76
- */
77
- export function setMockRawResponse<ResponseType>(
78
- resource: string,
79
- ...args: [string, MockHandler<ResponseType>] | [MockHandler<ResponseType>]
80
- ): RemoveMockFn {
81
- const handler = args.pop() as MockHandler<ResponseType>;
82
- const method = args.pop() as string;
83
- const mock = {
84
- resource,
85
- method,
86
- handler,
87
- regexp: pathToRegexp(resource),
88
- };
89
- mocks.push(mock);
90
- return () => {
91
- const mockIndex = mocks.indexOf(mock);
92
- if (mockIndex !== -1) {
93
- mocks.splice(mockIndex, 1);
94
- }
95
- };
96
- }
97
-
98
- /**
99
- * Specify an expected resource path that is to be handled, resulting in a `FetchRequest`.
100
- *
101
- * The mock `handler` should return the `result`, which will then be wrapped in a `FetchRequest` object.
102
- *
103
- * @param resource The path of the resource to be matched.
104
- * This can include wildcards whose value will be passed to the `handler`.
105
- * @param handler The function that will generate the mock response for this request.
106
- */
107
- export function setMockResponse<ResponseType>(
108
- resource: string,
109
- handler: MockHandler<ResponseType>
110
- ): RemoveMockFn;
111
- /**
112
- * Specify an expected resource path that is to be handled, resulting in a FetchRequest..
113
- *
114
- * @param resource The path of the resource to be matched.
115
- * This can include wildcards whose value will be passed to the `handler`.
116
- * @param method The HTTP method (e.g. GET, POST, etc) that the request must have to match this mock handler.
117
- * @param handler The function that will generate the mock response for this request.
118
- */
119
- export function setMockResponse<ResponseType>(
120
- resource: string,
121
- method: string,
122
- handler: MockHandler<ResponseType>
123
- ): RemoveMockFn;
124
- /**
125
- * Specify an expected resource path that is to be handled, resulting in a FetchRequest.
126
- */
127
- export function setMockResponse<ResponseType>(
128
- resource: string,
129
- ...args: [string, MockHandler<ResponseType>] | [MockHandler<ResponseType>]
130
- ): RemoveMockFn {
131
- const handler = args.pop() as MockHandler<ResponseType>;
132
- const method = args.pop() as string;
133
- return setMockRawResponse(resource, method, (...handlerArgs) =>
134
- createFetchResult(handler(...handlerArgs))
135
- );
136
- }
137
-
138
- /**
139
- * A helper to make it easier to create `FetchResult` objects in tests.
140
- */
141
- export function createFetchResult<ResponseType>(
142
- result: ResponseType,
143
- success = true,
144
- errors = [],
145
- messages = [],
146
- result_info?: unknown
147
- ): FetchResult<ResponseType> {
148
- return result_info
149
- ? {
150
- result,
151
- success,
152
- errors,
153
- messages,
154
- result_info,
155
- }
156
- : {
157
- result,
158
- success,
159
- errors,
160
- messages,
161
- };
162
- }
163
-
164
- /**
165
- * Remove all the configured mock handlers.
166
- *
167
- * This should be called in an `afterEach()` block to ensure that mock handlers do not leak between tests.
168
- */
169
- export function unsetAllMocks() {
170
- mocks.length = 0;
171
- }
@@ -1,65 +0,0 @@
1
- import { confirm, prompt } from "../dialogs";
2
-
3
- /**
4
- * The expected values for a confirmation request.
5
- */
6
- export interface ConfirmExpectation {
7
- /** The text expected to be seen in the confirmation dialog. */
8
- text: string;
9
- /** The mock response send back from the confirmation dialog. */
10
- result: boolean;
11
- }
12
-
13
- /**
14
- * Mock the implementation of `confirm()` that will respond with configured results
15
- * for configured confirmation text messages.
16
- *
17
- * If there is a call to `confirm()` that does not match any of the expectations
18
- * then an error is thrown.
19
- */
20
- export function mockConfirm(...expectations: ConfirmExpectation[]) {
21
- (confirm as jest.Mock).mockImplementation((text: string) => {
22
- for (const { text: expectedText, result } of expectations) {
23
- if (text === expectedText) {
24
- return Promise.resolve(result);
25
- }
26
- }
27
- throw new Error(`Unexpected confirmation message: ${text}`);
28
- });
29
- }
30
-
31
- /**
32
- * The expected values for a prompt request.
33
- */
34
- export interface PromptExpectation {
35
- /** The text expected to be seen in the prompt dialog. */
36
- text: string;
37
- /** The type of the prompt. */
38
- type: "text" | "password";
39
- /** The mock response send back from the prompt dialog. */
40
- result: string;
41
- }
42
-
43
- /**
44
- * Mock the implementation of `prompt()` that will respond with configured results
45
- * for configured prompt text messages.
46
- *
47
- * If there is a call to `prompt()` that does not match any of the expectations
48
- * then an error is thrown.
49
- */
50
- export function mockPrompt(...expectations: PromptExpectation[]) {
51
- (prompt as jest.Mock).mockImplementation(
52
- (text: string, type: "text" | "password") => {
53
- for (const {
54
- text: expectedText,
55
- type: expectedType,
56
- result,
57
- } of expectations) {
58
- if (text === expectedText && type == expectedType) {
59
- return Promise.resolve(result);
60
- }
61
- }
62
- throw new Error(`Unexpected confirmation message: ${text}`);
63
- }
64
- );
65
- }
@@ -1,19 +0,0 @@
1
- import * as os from "node:os";
2
- import * as path from "node:path";
3
- import * as fs from "fs";
4
-
5
- const originalCwd = process.cwd();
6
-
7
- export function runInTempDir() {
8
- let tmpDir: string;
9
-
10
- beforeEach(() => {
11
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "wrangler-tests"));
12
- process.chdir(tmpDir);
13
- });
14
-
15
- afterEach(() => {
16
- process.chdir(originalCwd);
17
- fs.rmSync(tmpDir, { recursive: true });
18
- });
19
- }
@@ -1,32 +0,0 @@
1
- import { main } from "../index";
2
-
3
- /**
4
- * Run the wrangler CLI tool in tests, capturing the logging output.
5
- */
6
- export async function runWrangler(cmd?: string) {
7
- const logSpy = jest.spyOn(console, "log").mockImplementation();
8
- const errorSpy = jest.spyOn(console, "error").mockImplementation();
9
- const warnSpy = jest.spyOn(console, "warn").mockImplementation();
10
- try {
11
- let error: unknown = undefined;
12
- try {
13
- await main(cmd?.split(" ") ?? []);
14
- } catch (e) {
15
- error = e;
16
- }
17
- return {
18
- error,
19
- stdout: captureCalls(logSpy),
20
- stderr: captureCalls(errorSpy),
21
- warnings: captureCalls(warnSpy),
22
- };
23
- } finally {
24
- logSpy.mockRestore();
25
- errorSpy.mockRestore();
26
- warnSpy.mockRestore();
27
- }
28
- }
29
-
30
- function captureCalls(spy: jest.SpyInstance): string {
31
- return spy.mock.calls.flat(2).join("\n");
32
- }
@@ -1,131 +0,0 @@
1
- import type {
2
- CfWorkerInit,
3
- CfModuleType,
4
- CfModule,
5
- CfDurableObjectMigrations,
6
- } from "./worker.js";
7
- import { FormData, Blob } from "formdata-node";
8
-
9
- export function toMimeType(type: CfModuleType): string {
10
- switch (type) {
11
- case "esm":
12
- return "application/javascript+module";
13
- case "commonjs":
14
- return "application/javascript";
15
- case "compiled-wasm":
16
- return "application/wasm";
17
- case "buffer":
18
- return "application/octet-stream";
19
- case "text":
20
- return "text/plain";
21
- default:
22
- throw new TypeError("Unsupported module: " + type);
23
- }
24
- }
25
-
26
- function toModule(module: CfModule, entryType: CfModuleType): Blob {
27
- const { type: moduleType, content } = module;
28
- const type = toMimeType(moduleType ?? entryType);
29
-
30
- return new Blob([content], { type });
31
- }
32
-
33
- interface WorkerMetadata {
34
- compatibility_date?: string;
35
- compatibility_flags?: string[];
36
- usage_model?: "bundled" | "unbound";
37
- migrations?: CfDurableObjectMigrations;
38
- bindings: (
39
- | { type: "kv_namespace"; name: string; namespace_id: string }
40
- | { type: "plain_text"; name: string; text: string }
41
- | {
42
- type: "durable_object_namespace";
43
- name: string;
44
- class_name: string;
45
- script_name?: string;
46
- }
47
- | {
48
- type: "service";
49
- name: string;
50
- service: string;
51
- environment: string;
52
- }
53
- )[];
54
- }
55
-
56
- /**
57
- * Creates a `FormData` upload from a `CfWorkerInit`.
58
- */
59
- export function toFormData(worker: CfWorkerInit): FormData {
60
- const formData = new FormData();
61
- const {
62
- main,
63
- modules,
64
- bindings,
65
- migrations,
66
- usage_model,
67
- compatibility_date,
68
- compatibility_flags,
69
- } = worker;
70
-
71
- const metadataBindings: WorkerMetadata["bindings"] = [];
72
-
73
- bindings.kv_namespaces?.forEach(({ id, binding }) => {
74
- metadataBindings.push({
75
- name: binding,
76
- type: "kv_namespace",
77
- namespace_id: id,
78
- });
79
- });
80
-
81
- bindings.durable_objects?.bindings.forEach(
82
- ({ name, class_name, script_name }) => {
83
- metadataBindings.push({
84
- name,
85
- type: "durable_object_namespace",
86
- class_name: class_name,
87
- ...(script_name && { script_name }),
88
- });
89
- }
90
- );
91
-
92
- Object.entries(bindings.vars || {})?.forEach(([key, value]) => {
93
- metadataBindings.push({ name: key, type: "plain_text", text: value });
94
- });
95
-
96
- bindings.services?.forEach(({ name, service, environment }) => {
97
- metadataBindings.push({
98
- name,
99
- type: "service",
100
- service,
101
- environment,
102
- });
103
- });
104
-
105
- const metadata: WorkerMetadata = {
106
- ...(main.type !== "commonjs"
107
- ? { main_module: main.name }
108
- : { body_part: main.name }),
109
- bindings: metadataBindings,
110
- ...(compatibility_date && { compatibility_date }),
111
- ...(compatibility_flags && { compatibility_flags }),
112
- ...(usage_model && { usage_model }),
113
- ...(migrations && { migrations }),
114
- };
115
-
116
- formData.set("metadata", JSON.stringify(metadata));
117
-
118
- if (main.type === "commonjs" && modules && modules.length > 0) {
119
- throw new TypeError(
120
- "More than one module can only be specified when type = 'esm'"
121
- );
122
- }
123
-
124
- for (const module of [main].concat(modules || [])) {
125
- const { name } = module;
126
- const blob = toModule(module, main.type ?? "esm");
127
- formData.set(name, blob, name);
128
- }
129
-
130
- return formData;
131
- }
@@ -1,128 +0,0 @@
1
- import fetch from "node-fetch";
2
- import { fetchResult } from "../cfetch";
3
- import { toFormData } from "./form_data";
4
- import type { CfAccount, CfWorkerInit } from "./worker";
5
-
6
- /**
7
- * A preview mode.
8
- *
9
- * * If true, then using a `workers.dev` subdomain.
10
- * * Otherwise, a list of routes under a single zone.
11
- */
12
- type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
13
-
14
- /**
15
- * A preview token.
16
- */
17
- export interface CfPreviewToken {
18
- /**
19
- * The header value required to trigger a preview.
20
- *
21
- * @example
22
- * const headers = { 'cf-workers-preview-token': value }
23
- * const response = await fetch('https://' + host, { headers })
24
- */
25
- value: string;
26
- /**
27
- * The host where the preview is available.
28
- */
29
- host: string;
30
- /**
31
- * A websocket url to a DevTools inspector.
32
- *
33
- * Workers does not have a fully-featured implementation
34
- * of the Chrome DevTools protocol, but supports the following:
35
- * * `console.log()` output.
36
- * * `Error` stack traces.
37
- * * `fetch()` events.
38
- *
39
- * There is no support for breakpoints, but we want to implement
40
- * this eventually.
41
- *
42
- * @link https://chromedevtools.github.io/devtools-protocol/
43
- */
44
- inspectorUrl: URL;
45
- /**
46
- * A url to prewarm the preview session.
47
- *
48
- * @example
49
- * fetch(prewarmUrl, { method: 'POST' })
50
- */
51
- prewarmUrl: URL;
52
- }
53
-
54
- /**
55
- * Generates a preview session token.
56
- */
57
- async function sessionToken(account: CfAccount): Promise<CfPreviewToken> {
58
- const { accountId, zoneId } = account;
59
- const initUrl = zoneId
60
- ? `/zones/${zoneId}/workers/edge-preview`
61
- : `/accounts/${accountId}/workers/subdomain/edge-preview`;
62
-
63
- const { exchange_url } = await fetchResult<{ exchange_url: string }>(initUrl);
64
- const { inspector_websocket, token } = (await (
65
- await fetch(exchange_url)
66
- ).json()) as { inspector_websocket: string; token: string };
67
- const { host } = new URL(inspector_websocket);
68
- const query = `cf_workers_preview_token=${token}`;
69
-
70
- return {
71
- value: token,
72
- host,
73
- inspectorUrl: new URL(`${inspector_websocket}?${query}`),
74
- prewarmUrl: new URL(
75
- `https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
76
- ),
77
- };
78
- }
79
-
80
- // Credit: https://stackoverflow.com/a/2117523
81
- function randomId(): string {
82
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
83
- const r = (Math.random() * 16) | 0,
84
- v = c == "x" ? r : (r & 0x3) | 0x8;
85
- return v.toString(16);
86
- });
87
- }
88
-
89
- /**
90
- * Creates a preview token.
91
- */
92
- export async function previewToken(
93
- account: CfAccount,
94
- worker: CfWorkerInit
95
- ): Promise<CfPreviewToken> {
96
- const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(account);
97
-
98
- const { accountId, zoneId } = account;
99
- const scriptId = zoneId ? randomId() : worker.name || host.split(".")[0];
100
- const url = `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
101
-
102
- const mode: CfPreviewMode = zoneId
103
- ? { routes: ["*/*"] }
104
- : { workers_dev: true };
105
-
106
- const formData = toFormData(worker);
107
- formData.set("wrangler-session-config", JSON.stringify(mode));
108
-
109
- const { preview_token } = await fetchResult<{ preview_token: string }>(url, {
110
- method: "POST",
111
- // @ts-expect-error TODO: fix this
112
- body: formData,
113
- headers: {
114
- "cf-preview-upload-config-token": value,
115
- },
116
- });
117
-
118
- return {
119
- value: preview_token,
120
- // TODO: verify this works with zoned workers
121
- host:
122
- worker.name && !zoneId
123
- ? `${worker.name}.${host.split(".").slice(1).join(".")}`
124
- : host,
125
- inspectorUrl,
126
- prewarmUrl,
127
- };
128
- }
package/src/api/worker.ts DELETED
@@ -1,155 +0,0 @@
1
- import type { CfPreviewToken } from "./preview";
2
- import { previewToken } from "./preview";
3
- import fetch from "node-fetch";
4
-
5
- /**
6
- * A Cloudflare account.
7
- */
8
- export interface CfAccount {
9
- /**
10
- * An API token.
11
- *
12
- * @link https://api.cloudflare.com/#user-api-tokens-properties
13
- */
14
- apiToken: string;
15
- /**
16
- * An account ID.
17
- */
18
- accountId: string;
19
- /**
20
- * A zone ID, only required if not using `workers.dev`.
21
- */
22
- zoneId?: string;
23
- }
24
-
25
- /**
26
- * A module type.
27
- */
28
- export type CfModuleType =
29
- | "esm"
30
- | "commonjs"
31
- | "compiled-wasm"
32
- | "text"
33
- | "buffer";
34
-
35
- /**
36
- * An imported module.
37
- */
38
- export interface CfModule {
39
- /**
40
- * The module name.
41
- *
42
- * @example
43
- * './src/index.js'
44
- */
45
- name: string;
46
- /**
47
- * The module content, usually JavaScript or WASM code.
48
- *
49
- * @example
50
- * export default {
51
- * async fetch(request) {
52
- * return new Response('Ok')
53
- * }
54
- * }
55
- */
56
- content: string | BufferSource;
57
- /**
58
- * The module type.
59
- *
60
- * If absent, will default to the main module's type.
61
- */
62
- type?: CfModuleType;
63
- }
64
-
65
- /**
66
- * A map of variable names to values.
67
- */
68
- interface CfVars {
69
- [key: string]: string;
70
- }
71
-
72
- /**
73
- * A KV namespace.
74
- */
75
- interface CfKvNamespace {
76
- binding: string;
77
- id: string;
78
- }
79
-
80
- /**
81
- * A Durable Object.
82
- */
83
- interface CfDurableObject {
84
- name: string;
85
- class_name: string;
86
- script_name?: string;
87
- }
88
-
89
- /**
90
- * A Service.
91
- */
92
- interface CfService {
93
- name: string;
94
- service: string;
95
- environment: string;
96
- }
97
-
98
- export interface CfDurableObjectMigrations {
99
- old_tag?: string;
100
- new_tag: string;
101
- steps: {
102
- new_classes?: string[];
103
- renamed_classes?: string[];
104
- deleted_classes?: string[];
105
- }[];
106
- }
107
-
108
- /**
109
- * Options for creating a `CfWorker`.
110
- */
111
- export interface CfWorkerInit {
112
- /**
113
- * The name of the worker.
114
- */
115
- name: string | undefined;
116
- /**
117
- * The entrypoint module.
118
- */
119
- main: CfModule;
120
- /**
121
- * The list of additional modules.
122
- */
123
- modules: undefined | CfModule[];
124
- /**
125
- * All the bindings
126
- */
127
- bindings: {
128
- kv_namespaces?: CfKvNamespace[];
129
- durable_objects?: { bindings: CfDurableObject[] };
130
- vars?: CfVars;
131
- services?: CfService[];
132
- };
133
- migrations: undefined | CfDurableObjectMigrations;
134
- compatibility_date: string | undefined;
135
- compatibility_flags: undefined | string[];
136
- usage_model: undefined | "bundled" | "unbound";
137
- }
138
-
139
- /**
140
- * A stub to create a Cloudflare Worker preview.
141
- *
142
- * @example
143
- * const {value, host} = await createWorker(init, acct);
144
- */
145
- export async function createWorker(
146
- init: CfWorkerInit,
147
- account: CfAccount
148
- ): Promise<CfPreviewToken> {
149
- const token = await previewToken(account, init);
150
- const response = await fetch(token.prewarmUrl.href, { method: "POST" });
151
- if (!response.ok) {
152
- // console.error("worker failed to prewarm: ", response.statusText);
153
- }
154
- return token;
155
- }