wrangler 0.0.0-e1c615f → 0.0.0-e1e2ee5c

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 (212) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +33 -14
  3. package/bin/wrangler.js +159 -27
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +5354 -0
  6. package/package.json +166 -123
  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 +3950 -0
  10. package/src/__tests__/dev.test.tsx +1199 -62
  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 +1459 -886
  35. package/src/__tests__/logger.test.ts +141 -0
  36. package/src/__tests__/metrics.test.ts +445 -0
  37. package/src/__tests__/package-manager.test.ts +353 -0
  38. package/src/__tests__/pages.test.ts +1261 -0
  39. package/src/__tests__/parse.test.ts +330 -0
  40. package/src/__tests__/publish.test.ts +6671 -0
  41. package/src/__tests__/pubsub.test.ts +367 -0
  42. package/src/__tests__/r2.test.ts +233 -0
  43. package/src/__tests__/route.test.ts +45 -0
  44. package/src/__tests__/secret.test.ts +492 -0
  45. package/src/__tests__/tail.test.ts +698 -0
  46. package/src/__tests__/test-old-node-version.js +31 -0
  47. package/src/__tests__/user.test.ts +143 -0
  48. package/src/__tests__/version.test.ts +35 -0
  49. package/src/__tests__/whoami.test.tsx +230 -0
  50. package/src/__tests__/worker-namespace.test.ts +327 -0
  51. package/src/abort.d.ts +3 -0
  52. package/src/api/dev.ts +86 -0
  53. package/src/api/index.ts +1 -0
  54. package/src/bundle-reporter.tsx +29 -0
  55. package/src/bundle.ts +236 -0
  56. package/src/cfetch/index.ts +88 -69
  57. package/src/cfetch/internal.ts +124 -44
  58. package/src/cli.ts +24 -6
  59. package/src/config/README.md +107 -0
  60. package/src/config/config.ts +251 -0
  61. package/src/config/diagnostics.ts +80 -0
  62. package/src/config/environment.ts +453 -0
  63. package/src/config/index.ts +238 -0
  64. package/src/config/validation-helpers.ts +584 -0
  65. package/src/config/validation.ts +1754 -0
  66. package/src/config-cache.ts +84 -0
  67. package/src/create-worker-preview.ts +255 -0
  68. package/src/create-worker-upload-form.ts +263 -0
  69. package/src/dev/dev-vars.ts +36 -0
  70. package/src/dev/dev.tsx +452 -0
  71. package/src/dev/local.tsx +373 -0
  72. package/src/dev/remote.tsx +386 -0
  73. package/src/dev/use-esbuild.ts +150 -0
  74. package/src/dev.tsx +581 -862
  75. package/src/dialogs.tsx +117 -59
  76. package/src/durable.ts +102 -0
  77. package/src/entry.ts +328 -0
  78. package/src/environment-variables.ts +35 -0
  79. package/src/errors.ts +11 -0
  80. package/src/faye-websocket.d.ts +6 -0
  81. package/src/generate.ts +33 -0
  82. package/src/git-client.ts +42 -0
  83. package/src/global-wrangler-config-path.ts +26 -0
  84. package/src/https-options.ts +127 -0
  85. package/src/index.tsx +1987 -1911
  86. package/src/init.ts +549 -0
  87. package/src/inspect.ts +662 -0
  88. package/src/intl-polyfill.d.ts +139 -0
  89. package/src/is-interactive.ts +12 -0
  90. package/src/kv.ts +430 -0
  91. package/src/logger.ts +73 -0
  92. package/src/metrics/index.ts +4 -0
  93. package/src/metrics/is-ci.ts +14 -0
  94. package/src/metrics/metrics-config.ts +239 -0
  95. package/src/metrics/metrics-dispatcher.ts +95 -0
  96. package/src/metrics/send-event.ts +92 -0
  97. package/src/miniflare-cli/README.md +30 -0
  98. package/src/miniflare-cli/assets.ts +543 -0
  99. package/src/miniflare-cli/enum-keys.ts +17 -0
  100. package/src/miniflare-cli/index.ts +85 -0
  101. package/src/miniflare-cli/request-context.ts +40 -0
  102. package/src/module-collection.ts +247 -53
  103. package/src/open-in-browser.ts +17 -0
  104. package/src/package-manager.ts +219 -0
  105. package/src/pages/build.tsx +204 -0
  106. package/src/pages/constants.ts +7 -0
  107. package/src/pages/deployments.tsx +106 -0
  108. package/src/pages/dev.tsx +401 -0
  109. package/src/pages/functions/buildPlugin.ts +105 -0
  110. package/src/pages/functions/buildWorker.ts +151 -0
  111. package/src/pages/functions/filepath-routing.test.ts +234 -0
  112. package/src/pages/functions/filepath-routing.ts +189 -0
  113. package/src/pages/functions/identifiers.ts +78 -0
  114. package/src/pages/functions/routes.ts +151 -0
  115. package/src/pages/index.tsx +84 -0
  116. package/src/pages/projects.tsx +163 -0
  117. package/src/pages/publish.tsx +338 -0
  118. package/src/pages/types.ts +40 -0
  119. package/src/pages/upload.tsx +396 -0
  120. package/src/pages/utils.ts +12 -0
  121. package/src/parse.ts +286 -0
  122. package/src/paths.ts +26 -0
  123. package/src/preview.ts +31 -0
  124. package/src/proxy.ts +464 -91
  125. package/src/publish.ts +780 -337
  126. package/src/pubsub/index.ts +286 -0
  127. package/src/pubsub/pubsub-commands.tsx +623 -0
  128. package/src/r2.ts +50 -0
  129. package/src/selfsigned.d.ts +29 -0
  130. package/src/sites.tsx +357 -99
  131. package/src/tail/filters.ts +279 -0
  132. package/src/tail/index.ts +299 -0
  133. package/src/tail/printing.ts +67 -0
  134. package/src/update-check.ts +19 -0
  135. package/src/user/choose-account.tsx +69 -0
  136. package/src/user/env-vars.ts +46 -0
  137. package/src/user/generate-auth-url.ts +33 -0
  138. package/src/user/generate-random-state.ts +16 -0
  139. package/src/user/index.ts +3 -0
  140. package/src/user/user.tsx +1171 -0
  141. package/src/whoami.tsx +91 -0
  142. package/src/worker-namespace.ts +206 -0
  143. package/src/worker.ts +191 -0
  144. package/src/zones.ts +76 -0
  145. package/templates/checked-fetch.js +17 -0
  146. package/templates/gitignore +171 -0
  147. package/templates/new-worker-scheduled.js +17 -0
  148. package/templates/new-worker-scheduled.ts +32 -0
  149. package/templates/new-worker.js +15 -0
  150. package/templates/new-worker.ts +30 -0
  151. package/templates/no-op-worker.js +10 -0
  152. package/templates/pages-shim.ts +9 -0
  153. package/templates/pages-template-plugin.ts +155 -0
  154. package/templates/pages-template-worker.ts +161 -0
  155. package/templates/static-asset-facade.js +49 -0
  156. package/templates/tsconfig.json +105 -0
  157. package/wrangler-dist/cli.d.ts +327 -0
  158. package/wrangler-dist/cli.js +112594 -112465
  159. package/pages/functions/buildWorker.ts +0 -57
  160. package/pages/functions/filepath-routing.test.ts +0 -39
  161. package/pages/functions/filepath-routing.ts +0 -221
  162. package/pages/functions/identifiers.ts +0 -78
  163. package/pages/functions/routes.ts +0 -158
  164. package/pages/functions/template-worker.ts +0 -137
  165. package/src/__tests__/mock-cfetch.ts +0 -171
  166. package/src/__tests__/mock-dialogs.ts +0 -65
  167. package/src/__tests__/run-in-tmp.ts +0 -19
  168. package/src/__tests__/run-wrangler.ts +0 -32
  169. package/src/api/form_data.ts +0 -131
  170. package/src/api/inspect.ts +0 -430
  171. package/src/api/preview.ts +0 -128
  172. package/src/api/worker.ts +0 -155
  173. package/src/config.ts +0 -131
  174. package/src/kv.tsx +0 -267
  175. package/src/pages.tsx +0 -968
  176. package/src/tail.tsx +0 -73
  177. package/src/user.tsx +0 -1022
  178. package/static-asset-facade.js +0 -47
  179. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  180. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  181. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  182. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  183. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  184. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  185. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  186. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  187. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  188. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  189. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  190. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  191. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  192. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  193. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  194. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  195. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  196. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  197. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  198. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  199. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  200. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  201. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  202. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  203. package/vendor/wrangler-mime/LICENSE +0 -21
  204. package/vendor/wrangler-mime/Mime.js +0 -97
  205. package/vendor/wrangler-mime/README.md +0 -187
  206. package/vendor/wrangler-mime/cli.js +0 -46
  207. package/vendor/wrangler-mime/index.js +0 -4
  208. package/vendor/wrangler-mime/lite.js +0 -4
  209. package/vendor/wrangler-mime/package.json +0 -52
  210. package/vendor/wrangler-mime/types/other.js +0 -1
  211. package/vendor/wrangler-mime/types/standard.js +0 -1
  212. package/wrangler-dist/cli.js.map +0 -7
package/src/config.ts DELETED
@@ -1,131 +0,0 @@
1
- // we're going to manually write both the type definition AND
2
- // the validator for the config, so that we can give better error messages
3
-
4
- type DurableObjectMigration = {
5
- tag: string;
6
- new_classes?: string[];
7
- renamed_classes?: string[];
8
- deleted_classes?: string[];
9
- };
10
-
11
- type Project = "webpack" | "javascript" | "rust";
12
-
13
- type Site = {
14
- // inherited
15
- bucket: string;
16
- "entry-point": string;
17
- include?: string[];
18
- exclude?: string[];
19
- };
20
-
21
- type Dev = {
22
- ip?: string;
23
- port?: number;
24
- local_protocol?: string;
25
- upstream_protocol?: string;
26
- };
27
-
28
- export type Vars = { [key: string]: string };
29
-
30
- type Cron = string; // TODO: we should be able to parse a cron pattern with ts
31
-
32
- type KVNamespace = {
33
- binding: string;
34
- preview_id?: string;
35
- id: string;
36
- };
37
-
38
- type DurableObject = {
39
- name: string;
40
- class_name: string;
41
- script_name?: string;
42
- };
43
-
44
- type Service = {
45
- name: string;
46
- service: string;
47
- environment: string;
48
- };
49
-
50
- type Build = {
51
- command?: string;
52
- cwd?: string;
53
- watch_dir?: string;
54
- } & (
55
- | {
56
- upload?: {
57
- format: "service-worker";
58
- main: string;
59
- };
60
- }
61
- | {
62
- upload?: {
63
- format: "modules";
64
- dir?: string;
65
- main?: string;
66
- rules?: {
67
- type: "ESModule" | "CommonJS" | "Text" | "Data" | "CompiledWasm";
68
- globs: string[]; // can we use typescript for these patterns?
69
- fallthrough?: boolean;
70
- };
71
- };
72
- }
73
- );
74
-
75
- type UsageModel = "bundled" | "unbound";
76
-
77
- type Env = {
78
- name?: string; // inherited
79
- account_id?: string; // inherited
80
- workers_dev?: boolean; // inherited
81
- compatibility_date?: string; // inherited
82
- compatibility_flags?: string[]; // inherited
83
- zone_id?: string; // inherited
84
- routes?: string[]; // inherited
85
- route?: string; // inherited
86
- webpack_config?: string; // inherited
87
- site?: Site;
88
- jsx_factory?: string; // inherited
89
- jsx_fragment?: string; // inherited
90
- // we should use typescript to parse cron patterns
91
- triggers?: { crons: Cron[] }; // inherited
92
- vars?: Vars;
93
- durable_objects?: { bindings: DurableObject[] };
94
- kv_namespaces?: KVNamespace[];
95
- experimental_services?: Service[];
96
- migrations?: DurableObjectMigration[];
97
- usage_model?: UsageModel; // inherited
98
- };
99
-
100
- export type Config = {
101
- name?: string; // inherited
102
- account_id?: string; // inherited
103
- // @deprecated Don't use this
104
- type?: Project; // top level
105
- compatibility_date?: string; // inherited
106
- compatibility_flags?: string[]; // inherited
107
- // -- there's some mutually exclusive logic for this next block,
108
- // but I didn't bother for now
109
- workers_dev?: boolean; // inherited
110
- zone_id?: string; // inherited
111
- routes?: string[]; // inherited
112
- route?: string; // inherited
113
- // -- end mutually exclusive stuff
114
- // @deprecated Don't use this
115
- webpack_config?: string; // inherited
116
- jsx_factory?: string; // inherited
117
- jsx_fragment?: string; // inherited
118
- vars?: Vars;
119
- durable_objects?: { bindings: DurableObject[] };
120
- kv_namespaces?: KVNamespace[];
121
- experimental_services?: Service[];
122
- migrations?: DurableObjectMigration[];
123
- site?: Site; // inherited
124
- // we should use typescript to parse cron patterns
125
- triggers?: { crons: Cron[] }; // inherited
126
- dev?: Dev;
127
- usage_model?: UsageModel; // inherited
128
- // top level
129
- build?: Build;
130
- env?: { [envName: string]: void | Env };
131
- };
package/src/kv.tsx DELETED
@@ -1,267 +0,0 @@
1
- import { URLSearchParams } from "node:url";
2
- import type { Config } from "./config";
3
- import { fetchListResult, fetchResult } from "./cfetch";
4
-
5
- type KvArgs = {
6
- binding?: string;
7
- "namespace-id"?: string;
8
- env?: string;
9
- preview?: boolean;
10
- config?: Config;
11
- };
12
-
13
- /**
14
- * Create a new namespace under the given `accountId` with the given `title`.
15
- *
16
- * @returns the generated id of the created namespace.
17
- */
18
- export async function createNamespace(
19
- accountId: string,
20
- title: string
21
- ): Promise<string> {
22
- const response = await fetchResult<{ id: string }>(
23
- `/accounts/${accountId}/storage/kv/namespaces`,
24
- {
25
- method: "POST",
26
- headers: {
27
- "Content-Type": "application/json",
28
- },
29
- body: JSON.stringify({
30
- title,
31
- }),
32
- }
33
- );
34
-
35
- return response.id;
36
- }
37
-
38
- /**
39
- * The information about a namespace that is returned from `listNamespaces()`.
40
- */
41
- export interface KVNamespaceInfo {
42
- id: string;
43
- title: string;
44
- supports_url_encoding?: boolean;
45
- }
46
-
47
- /**
48
- * Fetch a list of all the namespaces under the given `accountId`.
49
- */
50
- export async function listNamespaces(
51
- accountId: string
52
- ): Promise<KVNamespaceInfo[]> {
53
- const pageSize = 100;
54
- let page = 1;
55
- const results: KVNamespaceInfo[] = [];
56
- while (results.length % pageSize === 0) {
57
- const json = await fetchResult<KVNamespaceInfo[]>(
58
- `/accounts/${accountId}/storage/kv/namespaces`,
59
- {},
60
- new URLSearchParams({
61
- per_page: pageSize.toString(),
62
- order: "title",
63
- direction: "asc",
64
- page: page.toString(),
65
- })
66
- );
67
- page++;
68
- results.push(...json);
69
- if (json.length < pageSize) {
70
- break;
71
- }
72
- }
73
- return results;
74
- }
75
-
76
- export interface NamespaceKeyInfo {
77
- name: string;
78
- expiration?: number;
79
- metadata?: { [key: string]: unknown };
80
- }
81
-
82
- export async function listNamespaceKeys(
83
- accountId: string,
84
- namespaceId: string,
85
- prefix?: string
86
- ) {
87
- return await fetchListResult<NamespaceKeyInfo>(
88
- `/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys`,
89
- {},
90
- new URLSearchParams({ prefix })
91
- );
92
- }
93
-
94
- export async function putKeyValue(
95
- accountId: string,
96
- namespaceId: string,
97
- key: string,
98
- value: string,
99
- args?: { expiration?: number; expiration_ttl?: number }
100
- ) {
101
- return await fetchResult(
102
- `/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`,
103
- { method: "PUT", body: value },
104
- args
105
- ? new URLSearchParams({
106
- expiration: args.expiration?.toString(),
107
- expiration_ttl: args.expiration_ttl?.toString(),
108
- })
109
- : undefined
110
- );
111
- }
112
-
113
- export async function putBulkKeyValue(
114
- accountId: string,
115
- namespaceId: string,
116
- keyvalueStr: string
117
- ) {
118
- return await fetchResult(
119
- `/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
120
- {
121
- method: "PUT",
122
- body: keyvalueStr,
123
- headers: { "Content-Type": "application/json" },
124
- }
125
- );
126
- }
127
-
128
- export async function deleteBulkKeyValue(
129
- accountId: string,
130
- namespaceId: string,
131
- keyStr: string
132
- ) {
133
- return await fetchResult(
134
- `/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
135
- {
136
- method: "DELETE",
137
- body: keyStr,
138
- headers: { "Content-Type": "application/json" },
139
- }
140
- );
141
- }
142
-
143
- export function getNamespaceId({
144
- preview,
145
- binding,
146
- config,
147
- "namespace-id": namespaceId,
148
- env,
149
- }: KvArgs): string {
150
- // nice
151
- if (namespaceId) {
152
- return namespaceId;
153
- }
154
-
155
- // begin pre-flight checks
156
-
157
- // `--binding` is only valid if there's a wrangler configuration file.
158
- if (binding && !config) {
159
- throw new Error("--binding specified, but no config file was found.");
160
- }
161
-
162
- // there's no config. abort here
163
- if (!config) {
164
- throw new Error(
165
- "Failed to find a config file.\n" +
166
- "Either use --namespace-id to upload directly or create a configuration file with a binding."
167
- );
168
- }
169
-
170
- // they want to use an environment, actually
171
- if (env) {
172
- if (!config.env || !config.env[env]) {
173
- throw new Error(
174
- `Failed to find environment "${env}" in configuration file!`
175
- );
176
- }
177
-
178
- // TODO: either a bespoke arg type for this function to avoid `undefined`s or an `EnvOrConfig` type
179
- return getNamespaceId({
180
- binding,
181
- "namespace-id": namespaceId,
182
- env: undefined,
183
- preview,
184
- config: {
185
- env: undefined,
186
- build: undefined,
187
- name: undefined,
188
- account_id: undefined,
189
- ...config.env[env],
190
- },
191
- });
192
- }
193
-
194
- // there's no KV namespaces
195
- if (!config.kv_namespaces || config.kv_namespaces.length === 0) {
196
- throw new Error(
197
- "No KV Namespaces configured! Either use --namespace-id to upload directly or add a KV namespace to your wrangler config file."
198
- );
199
- }
200
-
201
- const namespace = config.kv_namespaces.find((ns) => ns.binding === binding);
202
-
203
- // we couldn't find a namespace with that binding
204
- if (!namespace) {
205
- throw new Error(
206
- `A namespace with binding name "${binding}" was not found in the configured "kv_namespaces".`
207
- );
208
- }
209
-
210
- // end pre-flight checks
211
-
212
- // we're in preview mode, `--preview true` or `--preview` was passed
213
- if (preview && namespace.preview_id) {
214
- namespaceId = namespace.preview_id;
215
- // We don't want to execute code below if preview is set to true, so we just return. Otherwise we will get errors!
216
- return namespaceId;
217
- } else if (preview) {
218
- throw new Error(
219
- `No preview ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
220
- );
221
- }
222
-
223
- // either `--preview false`, or preview wasn't passed
224
- // TODO: should we care? or should we just treat false and undefined the same
225
- const previewIsDefined = typeof preview !== "undefined";
226
-
227
- // --preview false was passed
228
- if (previewIsDefined && namespace.id) {
229
- namespaceId = namespace.id;
230
- // We don't want to execute code below if preview is set to true, so we just return. Otherwise we can get error!
231
- return namespaceId;
232
- } else if (previewIsDefined) {
233
- throw new Error(
234
- `No namespace ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
235
- );
236
- }
237
-
238
- // `--preview` wasn't passed
239
- const bindingHasOnlyOneId =
240
- (namespace.id && !namespace.preview_id) ||
241
- (!namespace.id && namespace.preview_id);
242
- if (bindingHasOnlyOneId) {
243
- namespaceId = namespace.id || namespace.preview_id;
244
- } else {
245
- throw new Error(
246
- `${binding} has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.`
247
- );
248
- }
249
-
250
- // shouldn't happen. we should be able to prove this with strong typing.
251
- // TODO: when we add strongly typed commands, rewrite these checks so they're exhaustive
252
- if (!namespaceId) {
253
- throw Error(
254
- "Something went wrong trying to determine which namespace to upload to.\n" +
255
- "Please create a github issue with the command you just ran along with your wrangler configuration."
256
- );
257
- }
258
-
259
- return namespaceId;
260
- }
261
-
262
- /**
263
- * KV namespace binding names must be valid JS identifiers.
264
- */
265
- export function isValidNamespaceBinding(binding: string): boolean {
266
- return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(binding);
267
- }