wrangler 0.0.0-e64812e → 0.0.0-e6c9c5c0

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 (338) hide show
  1. package/README.md +23 -3
  2. package/bin/wrangler.js +170 -62
  3. package/kv-asset-handler.js +1 -0
  4. package/miniflare-dist/index.mjs +1499 -21
  5. package/package.json +193 -152
  6. package/src/__tests__/access.test.ts +25 -0
  7. package/src/__tests__/api-dev.test.ts +238 -0
  8. package/src/__tests__/api-devregistry.test.js +64 -0
  9. package/src/__tests__/api.test.ts +80 -0
  10. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  11. package/src/__tests__/config-cache.test.ts +30 -24
  12. package/src/__tests__/configuration.test.ts +4152 -3363
  13. package/src/__tests__/d1/d1.test.ts +127 -0
  14. package/src/__tests__/d1/splitter.test.ts +255 -0
  15. package/src/__tests__/delete.test.ts +266 -0
  16. package/src/__tests__/deployments.test.ts +105 -0
  17. package/src/__tests__/dev.test.tsx +1558 -952
  18. package/src/__tests__/generate.test.ts +238 -0
  19. package/src/__tests__/get-host-from-url.test.ts +16 -0
  20. package/src/__tests__/guess-worker-format.test.ts +68 -68
  21. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  22. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  23. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  24. package/src/__tests__/helpers/mock-bin.ts +20 -20
  25. package/src/__tests__/helpers/mock-console.ts +58 -39
  26. package/src/__tests__/helpers/mock-dialogs.ts +88 -79
  27. package/src/__tests__/helpers/mock-get-zone-from-host.ts +11 -0
  28. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  29. package/src/__tests__/helpers/mock-istty.ts +65 -18
  30. package/src/__tests__/helpers/mock-known-routes.ts +12 -0
  31. package/src/__tests__/helpers/mock-kv.ts +41 -28
  32. package/src/__tests__/helpers/mock-oauth-flow.ts +241 -233
  33. package/src/__tests__/helpers/mock-process.ts +34 -0
  34. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  35. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  36. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  37. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  38. package/src/__tests__/helpers/msw/handlers/deployments.ts +69 -0
  39. package/src/__tests__/helpers/msw/handlers/namespaces.ts +81 -0
  40. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  41. package/src/__tests__/helpers/msw/handlers/r2.ts +60 -0
  42. package/src/__tests__/helpers/msw/handlers/script.ts +56 -0
  43. package/src/__tests__/helpers/msw/handlers/user.ts +52 -0
  44. package/src/__tests__/helpers/msw/handlers/zones.ts +20 -0
  45. package/src/__tests__/helpers/msw/index.ts +50 -0
  46. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  47. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  48. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  49. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  51. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  53. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  54. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  55. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  56. package/src/__tests__/https-options.test.ts +142 -109
  57. package/src/__tests__/index.test.ts +245 -233
  58. package/src/__tests__/init.test.ts +3135 -1605
  59. package/src/__tests__/jest.setup.ts +148 -50
  60. package/src/__tests__/kv.test.ts +1778 -1471
  61. package/src/__tests__/logger.test.ts +100 -74
  62. package/src/__tests__/logout.test.ts +47 -0
  63. package/src/__tests__/metrics.test.ts +493 -0
  64. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  65. package/src/__tests__/middleware.test.ts +750 -0
  66. package/src/__tests__/package-manager.test.ts +303 -303
  67. package/src/__tests__/pages-deployment-tail.test.ts +955 -0
  68. package/src/__tests__/pages.test.ts +2887 -743
  69. package/src/__tests__/parse.test.ts +252 -252
  70. package/src/__tests__/paths.test.ts +39 -0
  71. package/src/__tests__/publish.test.ts +8004 -5355
  72. package/src/__tests__/pubsub.test.ts +490 -0
  73. package/src/__tests__/queues.test.ts +522 -0
  74. package/src/__tests__/r2.test.ts +364 -228
  75. package/src/__tests__/route.test.ts +18 -18
  76. package/src/__tests__/secret.test.ts +681 -469
  77. package/src/__tests__/tail.test.ts +835 -567
  78. package/src/__tests__/test-old-node-version.js +31 -0
  79. package/src/__tests__/tsconfig-sanity.ts +12 -0
  80. package/src/__tests__/tsconfig.json +8 -0
  81. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  82. package/src/__tests__/type-generation.test.ts +233 -0
  83. package/src/__tests__/user.test.ts +107 -120
  84. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  85. package/src/__tests__/validate-dev-props.test.ts +56 -0
  86. package/src/__tests__/version.test.ts +35 -0
  87. package/src/__tests__/whoami.test.tsx +165 -98
  88. package/src/__tests__/worker-namespace.test.ts +334 -0
  89. package/src/abort.d.ts +1 -1
  90. package/src/api/dev.ts +259 -0
  91. package/src/api/index.ts +3 -0
  92. package/src/api/pages/index.ts +5 -0
  93. package/src/api/pages/publish.tsx +321 -0
  94. package/src/bundle-reporter.ts +68 -0
  95. package/src/bundle.ts +838 -163
  96. package/src/cfetch/index.ts +120 -83
  97. package/src/cfetch/internal.ts +210 -97
  98. package/src/cli.ts +23 -8
  99. package/src/config/config.ts +236 -195
  100. package/src/config/diagnostics.ts +61 -61
  101. package/src/config/environment.ts +516 -357
  102. package/src/config/index.ts +326 -193
  103. package/src/config/validation-helpers.ts +379 -357
  104. package/src/config/validation.ts +1979 -1367
  105. package/src/config-cache.ts +81 -41
  106. package/src/create-worker-preview.ts +239 -142
  107. package/src/create-worker-upload-form.ts +309 -238
  108. package/src/d1/backups.tsx +219 -0
  109. package/src/d1/constants.ts +2 -0
  110. package/src/d1/create.tsx +63 -0
  111. package/src/d1/delete.ts +56 -0
  112. package/src/d1/execute.tsx +323 -0
  113. package/src/d1/formatTimeAgo.ts +14 -0
  114. package/src/d1/index.ts +101 -0
  115. package/src/d1/list.tsx +48 -0
  116. package/src/d1/migrations/apply.tsx +197 -0
  117. package/src/d1/migrations/create.tsx +77 -0
  118. package/src/d1/migrations/helpers.ts +147 -0
  119. package/src/d1/migrations/index.ts +3 -0
  120. package/src/d1/migrations/list.tsx +79 -0
  121. package/src/d1/migrations/options.ts +18 -0
  122. package/src/d1/options.ts +22 -0
  123. package/src/d1/splitter.ts +161 -0
  124. package/src/d1/types.ts +24 -0
  125. package/src/d1/utils.ts +48 -0
  126. package/src/delete.ts +103 -0
  127. package/src/deployments.ts +93 -0
  128. package/src/deprecated/index.ts +145 -0
  129. package/src/dev/dev-vars.ts +20 -17
  130. package/src/dev/dev.tsx +547 -352
  131. package/src/dev/get-local-persistence-path.ts +31 -0
  132. package/src/dev/local.tsx +903 -298
  133. package/src/dev/remote.tsx +620 -215
  134. package/src/dev/start-server.ts +538 -0
  135. package/src/dev/use-esbuild.ts +184 -97
  136. package/src/dev/validate-dev-props.ts +31 -0
  137. package/src/dev-registry.ts +201 -0
  138. package/src/dev.tsx +949 -0
  139. package/src/dialogs.ts +136 -0
  140. package/src/dispatch-namespace.ts +207 -0
  141. package/src/docs/index.ts +98 -0
  142. package/src/durable.ts +87 -87
  143. package/src/entry.ts +236 -230
  144. package/src/environment-variables/factory.ts +88 -0
  145. package/src/environment-variables/misc-variables.ts +30 -0
  146. package/src/errors.ts +6 -6
  147. package/src/generate/index.ts +300 -0
  148. package/src/git-client.ts +135 -0
  149. package/src/global-wrangler-config-path.ts +26 -0
  150. package/src/https-options.ts +84 -80
  151. package/src/index.ts +694 -0
  152. package/src/init.ts +1024 -0
  153. package/src/inspect.ts +698 -593
  154. package/src/intl-polyfill.d.ts +123 -123
  155. package/src/is-ci.ts +14 -0
  156. package/src/is-interactive.ts +16 -0
  157. package/src/jest.d.ts +4 -0
  158. package/src/kv/helpers.ts +430 -0
  159. package/src/kv/index.ts +596 -0
  160. package/src/logger.ts +80 -42
  161. package/src/metrics/index.ts +5 -0
  162. package/src/metrics/metrics-config.ts +239 -0
  163. package/src/metrics/metrics-dispatcher.ts +96 -0
  164. package/src/metrics/metrics-usage-headers.ts +24 -0
  165. package/src/metrics/send-event.ts +98 -0
  166. package/src/miniflare-cli/assets.ts +250 -0
  167. package/src/miniflare-cli/index.ts +202 -33
  168. package/src/miniflare-cli/request-context.ts +18 -18
  169. package/src/miniflare-cli/tsconfig.json +9 -0
  170. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  171. package/src/miniflare-cli/types.ts +11 -0
  172. package/src/module-collection.ts +221 -212
  173. package/src/open-in-browser.ts +4 -6
  174. package/src/package-manager.ts +173 -128
  175. package/src/pages/build.ts +150 -0
  176. package/src/pages/buildFunctions.ts +129 -0
  177. package/src/pages/constants.ts +18 -0
  178. package/src/pages/deployment-tails.ts +284 -0
  179. package/src/pages/deployments.tsx +84 -0
  180. package/src/pages/dev.ts +716 -0
  181. package/src/pages/errors.ts +66 -0
  182. package/src/pages/functions/buildPlugin.ts +90 -0
  183. package/src/pages/functions/buildWorker.ts +288 -0
  184. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  185. package/src/pages/functions/filepath-routing.ts +189 -0
  186. package/src/pages/functions/identifiers.ts +78 -0
  187. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  188. package/src/pages/functions/routes-consolidation.ts +73 -0
  189. package/src/pages/functions/routes-transformation.test.ts +282 -0
  190. package/src/pages/functions/routes-transformation.ts +115 -0
  191. package/src/pages/functions/routes-validation.test.ts +403 -0
  192. package/src/pages/functions/routes-validation.ts +202 -0
  193. package/src/pages/functions/routes.ts +151 -0
  194. package/src/pages/functions/tsconfig.json +8 -0
  195. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  196. package/src/pages/functions.ts +82 -0
  197. package/src/pages/hash.ts +13 -0
  198. package/src/pages/index.ts +102 -0
  199. package/src/pages/projects.tsx +167 -0
  200. package/src/pages/prompt-select-project.tsx +31 -0
  201. package/src/pages/publish.tsx +263 -0
  202. package/src/pages/types.ts +41 -0
  203. package/src/pages/upload.tsx +447 -0
  204. package/src/pages/utils.ts +23 -0
  205. package/src/parse.ts +209 -125
  206. package/src/paths.ts +53 -8
  207. package/src/proxy.ts +623 -394
  208. package/src/publish/index.ts +270 -0
  209. package/src/publish/publish.ts +1064 -0
  210. package/src/pubsub/index.ts +286 -0
  211. package/src/pubsub/pubsub-commands.ts +624 -0
  212. package/src/queues/cli/commands/consumer/add.ts +71 -0
  213. package/src/queues/cli/commands/consumer/index.ts +22 -0
  214. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  215. package/src/queues/cli/commands/create.ts +25 -0
  216. package/src/queues/cli/commands/delete.ts +26 -0
  217. package/src/queues/cli/commands/index.ts +36 -0
  218. package/src/queues/cli/commands/list.ts +25 -0
  219. package/src/queues/client.ts +135 -0
  220. package/src/queues/utils.ts +18 -0
  221. package/src/r2/constants.ts +4 -0
  222. package/src/r2/helpers.ts +131 -0
  223. package/src/r2/index.ts +289 -0
  224. package/src/routes.ts +140 -0
  225. package/src/secret/index.ts +379 -0
  226. package/src/selfsigned.d.ts +23 -23
  227. package/src/sites.ts +372 -0
  228. package/src/tail/createTail.ts +406 -0
  229. package/src/tail/filters.ts +108 -110
  230. package/src/tail/index.ts +210 -296
  231. package/src/tail/printing.ts +101 -46
  232. package/src/tsconfig-sanity.ts +16 -0
  233. package/src/type-generation.ts +181 -0
  234. package/src/update-check.ts +11 -11
  235. package/src/user/access.ts +68 -0
  236. package/src/user/auth-variables.ts +113 -0
  237. package/src/user/choose-account.tsx +39 -0
  238. package/src/user/generate-auth-url.ts +33 -0
  239. package/src/user/generate-random-state.ts +16 -0
  240. package/src/user/index.ts +2 -0
  241. package/src/user/user.ts +1232 -0
  242. package/src/utils/collectKeyValues.ts +14 -0
  243. package/src/whoami.ts +135 -0
  244. package/src/worker.ts +177 -106
  245. package/src/yargs-types.ts +24 -0
  246. package/src/zones.ts +191 -0
  247. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  248. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  249. package/templates/__tests__/tsconfig.json +8 -0
  250. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  251. package/templates/checked-fetch.js +15 -0
  252. package/templates/d1-beta-facade.js +241 -0
  253. package/templates/facade.d.ts +14 -0
  254. package/templates/first-party-worker-module-facade.ts +19 -0
  255. package/templates/format-dev-errors.ts +33 -0
  256. package/templates/init-tests/test-jest-new-worker.js +23 -0
  257. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  258. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  259. package/templates/middleware/common.ts +62 -0
  260. package/templates/middleware/loader-modules.ts +84 -0
  261. package/templates/middleware/loader-sw.ts +233 -0
  262. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  263. package/templates/middleware/middleware-pretty-error.ts +40 -0
  264. package/templates/middleware/middleware-scheduled.ts +15 -0
  265. package/templates/new-worker-scheduled.js +3 -3
  266. package/templates/new-worker-scheduled.ts +15 -15
  267. package/templates/new-worker.js +3 -3
  268. package/templates/new-worker.ts +15 -15
  269. package/templates/no-op-worker.js +10 -0
  270. package/templates/pages-dev-pipeline.ts +31 -0
  271. package/templates/pages-dev-util.ts +52 -0
  272. package/templates/pages-shim.ts +12 -0
  273. package/templates/pages-template-plugin.ts +176 -0
  274. package/templates/pages-template-worker.ts +189 -0
  275. package/templates/serve-static-assets.ts +65 -0
  276. package/templates/service-bindings-module-facade.js +54 -0
  277. package/templates/service-bindings-sw-facade.js +42 -0
  278. package/templates/tsconfig-sanity.ts +11 -0
  279. package/templates/tsconfig.init.json +106 -0
  280. package/templates/tsconfig.json +6 -103
  281. package/templates/tsconfig.tsbuildinfo +1 -0
  282. package/wrangler-dist/cli.d.ts +1028 -0
  283. package/wrangler-dist/cli.js +129259 -110922
  284. package/wrangler-dist/wasm-sync.wasm +0 -0
  285. package/pages/functions/buildPlugin.ts +0 -105
  286. package/pages/functions/buildWorker.ts +0 -151
  287. package/pages/functions/filepath-routing.ts +0 -189
  288. package/pages/functions/identifiers.ts +0 -78
  289. package/pages/functions/routes.ts +0 -156
  290. package/pages/functions/template-plugin.ts +0 -147
  291. package/pages/functions/template-worker.ts +0 -143
  292. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  293. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  294. package/src/dialogs.tsx +0 -135
  295. package/src/environment-variables.ts +0 -35
  296. package/src/index.tsx +0 -2864
  297. package/src/kv.ts +0 -408
  298. package/src/miniflare-cli/enum-keys.ts +0 -17
  299. package/src/pages.tsx +0 -2038
  300. package/src/publish.ts +0 -583
  301. package/src/r2.ts +0 -50
  302. package/src/sites.tsx +0 -295
  303. package/src/user.tsx +0 -1203
  304. package/src/whoami.tsx +0 -70
  305. package/templates/static-asset-facade.js +0 -47
  306. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  307. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  308. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  309. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  310. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  311. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  312. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  313. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  314. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  315. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  316. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  317. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  318. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  319. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  320. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  321. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  322. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  323. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  324. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  325. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  326. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  327. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  328. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  329. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  330. package/vendor/wrangler-mime/LICENSE +0 -21
  331. package/vendor/wrangler-mime/Mime.js +0 -97
  332. package/vendor/wrangler-mime/README.md +0 -187
  333. package/vendor/wrangler-mime/cli.js +0 -46
  334. package/vendor/wrangler-mime/index.js +0 -4
  335. package/vendor/wrangler-mime/lite.js +0 -4
  336. package/vendor/wrangler-mime/package.json +0 -52
  337. package/vendor/wrangler-mime/types/other.js +0 -1
  338. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const ava_1 = require("ava");
13
- const mocks_1 = require("../mocks");
14
- const index_1 = require("../index");
15
- function testRequest(path) {
16
- mocks_1.mockGlobal();
17
- let url = new URL('https://example.com');
18
- url.pathname = path;
19
- let request = new Request(url.toString());
20
- return request;
21
- }
22
- ava_1.default('serveSinglePageApp returns root asset path when request path ends in .html', (t) => __awaiter(void 0, void 0, void 0, function* () {
23
- let path = '/foo/thing.html';
24
- let request = testRequest(path);
25
- let expected_request = testRequest('/index.html');
26
- let actual_request = index_1.serveSinglePageApp(request);
27
- t.deepEqual(expected_request, actual_request);
28
- }));
29
- ava_1.default('serveSinglePageApp returns root asset path when request path does not have extension', (t) => __awaiter(void 0, void 0, void 0, function* () {
30
- let path = '/foo/thing';
31
- let request = testRequest(path);
32
- let expected_request = testRequest('/index.html');
33
- let actual_request = index_1.serveSinglePageApp(request);
34
- t.deepEqual(expected_request, actual_request);
35
- }));
36
- ava_1.default('serveSinglePageApp returns requested asset when request path has non-html extension', (t) => __awaiter(void 0, void 0, void 0, function* () {
37
- let path = '/foo/thing.js';
38
- let request = testRequest(path);
39
- let expected_request = request;
40
- let actual_request = index_1.serveSinglePageApp(request);
41
- t.deepEqual(expected_request, actual_request);
42
- }));
@@ -1,26 +0,0 @@
1
- export declare type CacheControl = {
2
- browserTTL: number;
3
- edgeTTL: number;
4
- bypassCache: boolean;
5
- };
6
- export declare type Options = {
7
- cacheControl: ((req: Request) => Partial<CacheControl>) | Partial<CacheControl>;
8
- ASSET_NAMESPACE: any;
9
- ASSET_MANIFEST: Object | string;
10
- mapRequestToAsset?: (req: Request, options?: Partial<Options>) => Request;
11
- defaultMimeType: string;
12
- defaultDocument: string;
13
- };
14
- export declare class KVError extends Error {
15
- constructor(message?: string, status?: number);
16
- status: number;
17
- }
18
- export declare class MethodNotAllowedError extends KVError {
19
- constructor(message?: string, status?: number);
20
- }
21
- export declare class NotFoundError extends KVError {
22
- constructor(message?: string, status?: number);
23
- }
24
- export declare class InternalError extends KVError {
25
- constructor(message?: string, status?: number);
26
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InternalError = exports.NotFoundError = exports.MethodNotAllowedError = exports.KVError = void 0;
4
- class KVError extends Error {
5
- constructor(message, status = 500) {
6
- super(message);
7
- // see: typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html
8
- Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
9
- this.name = KVError.name; // stack traces display correctly now
10
- this.status = status;
11
- }
12
- }
13
- exports.KVError = KVError;
14
- class MethodNotAllowedError extends KVError {
15
- constructor(message = `Not a valid request method`, status = 405) {
16
- super(message, status);
17
- }
18
- }
19
- exports.MethodNotAllowedError = MethodNotAllowedError;
20
- class NotFoundError extends KVError {
21
- constructor(message = `Not Found`, status = 404) {
22
- super(message, status);
23
- }
24
- }
25
- exports.NotFoundError = NotFoundError;
26
- class InternalError extends KVError {
27
- constructor(message = `Internal Error in KV Asset Handler`, status = 500) {
28
- super(message, status);
29
- }
30
- }
31
- exports.InternalError = InternalError;
@@ -1,52 +0,0 @@
1
- {
2
- "name": "@cloudflare/kv-asset-handler",
3
- "version": "0.1.2",
4
- "description": "Routes requests to KV assets",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "scripts": {
8
- "prepack": "npm run build",
9
- "build": "tsc -d",
10
- "format": "prettier --write \"**/*.{js,ts,json,md}\"",
11
- "pretest": "npm run build",
12
- "lint:code": "prettier --check \"**/*.{js,ts,json,md}\"",
13
- "lint:markdown": "markdownlint \"**/*.md\" --ignore node_modules",
14
- "test": "ava dist/test/*.js --verbose"
15
- },
16
- "repository": {
17
- "type": "git",
18
- "url": "git+https://github.com/cloudflare/kv-asset-handler.git"
19
- },
20
- "keywords": [
21
- "kv",
22
- "cloudflare",
23
- "workers",
24
- "wrangler",
25
- "assets"
26
- ],
27
- "files": [
28
- "src",
29
- "dist",
30
- "LICENSE_APACHE",
31
- "LICENSE_MIT"
32
- ],
33
- "author": "wrangler@cloudflare.com",
34
- "license": "MIT OR Apache-2.0",
35
- "bugs": {
36
- "url": "https://github.com/cloudflare/kv-asset-handler/issues"
37
- },
38
- "homepage": "https://github.com/cloudflare/kv-asset-handler#readme",
39
- "dependencies": {
40
- "wrangler-mime": "^2.5.2"
41
- },
42
- "devDependencies": {
43
- "@ava/typescript": "^1.1.1",
44
- "@cloudflare/workers-types": "^2.2.2",
45
- "@types/mime": "^2.0.3",
46
- "@types/node": "^15.3.0",
47
- "ava": "^3.15.0",
48
- "prettier": "^2.3.0",
49
- "service-worker-mock": "^2.0.5",
50
- "typescript": "^4.2.4"
51
- }
52
- }
@@ -1,296 +0,0 @@
1
- import * as mime from 'mime'
2
- import { Options, CacheControl, MethodNotAllowedError, NotFoundError, InternalError } from './types'
3
-
4
- declare global {
5
- var __STATIC_CONTENT: any, __STATIC_CONTENT_MANIFEST: string
6
- }
7
-
8
- const defaultCacheControl: CacheControl = {
9
- browserTTL: null,
10
- edgeTTL: 2 * 60 * 60 * 24, // 2 days
11
- bypassCache: false, // do not bypass Cloudflare's cache
12
- }
13
-
14
- function assignOptions(options?: Partial<Options>): Options {
15
- // Assign any missing options passed in to the default
16
- // options.mapRequestToAsset is handled manually later
17
- return Object.assign(
18
- {
19
- ASSET_NAMESPACE: __STATIC_CONTENT,
20
- ASSET_MANIFEST: __STATIC_CONTENT_MANIFEST,
21
- cacheControl: defaultCacheControl,
22
- defaultMimeType: 'text/plain',
23
- defaultDocument: 'index.html',
24
- },
25
- options,
26
- )
27
- }
28
-
29
- /**
30
- * maps the path of incoming request to the request pathKey to look up
31
- * in bucket and in cache
32
- * e.g. for a path '/' returns '/index.html' which serves
33
- * the content of bucket/index.html
34
- * @param {Request} request incoming request
35
- */
36
- const mapRequestToAsset = (request: Request, options?: Partial<Options>) => {
37
- options = assignOptions(options)
38
-
39
- const parsedUrl = new URL(request.url)
40
- let pathname = parsedUrl.pathname
41
-
42
- if (pathname.endsWith('/')) {
43
- // If path looks like a directory append options.defaultDocument
44
- // e.g. If path is /about/ -> /about/index.html
45
- pathname = pathname.concat(options.defaultDocument)
46
- } else if (!mime.getType(pathname)) {
47
- // If path doesn't look like valid content
48
- // e.g. /about.me -> /about.me/index.html
49
- pathname = pathname.concat('/' + options.defaultDocument)
50
- }
51
-
52
- parsedUrl.pathname = pathname
53
- return new Request(parsedUrl.toString(), request)
54
- }
55
-
56
- /**
57
- * maps the path of incoming request to /index.html if it evaluates to
58
- * any HTML file.
59
- * @param {Request} request incoming request
60
- */
61
- function serveSinglePageApp(request: Request, options?: Partial<Options>): Request {
62
- options = assignOptions(options)
63
-
64
- // First apply the default handler, which already has logic to detect
65
- // paths that should map to HTML files.
66
- request = mapRequestToAsset(request, options)
67
-
68
- const parsedUrl = new URL(request.url)
69
-
70
- // Detect if the default handler decided to map to
71
- // a HTML file in some specific directory.
72
- if (parsedUrl.pathname.endsWith('.html')) {
73
- // If expected HTML file was missing, just return the root index.html (or options.defaultDocument)
74
- return new Request(`${parsedUrl.origin}/${options.defaultDocument}`, request)
75
- } else {
76
- // The default handler decided this is not an HTML page. It's probably
77
- // an image, CSS, or JS file. Leave it as-is.
78
- return request
79
- }
80
- }
81
-
82
- /**
83
- * takes the path of the incoming request, gathers the appropriate content from KV, and returns
84
- * the response
85
- *
86
- * @param {FetchEvent} event the fetch event of the triggered request
87
- * @param {{mapRequestToAsset: (string: Request) => Request, cacheControl: {bypassCache:boolean, edgeTTL: number, browserTTL:number}, ASSET_NAMESPACE: any, ASSET_MANIFEST:any}} [options] configurable options
88
- * @param {CacheControl} [options.cacheControl] determine how to cache on Cloudflare and the browser
89
- * @param {typeof(options.mapRequestToAsset)} [options.mapRequestToAsset] maps the path of incoming request to the request pathKey to look up
90
- * @param {Object | string} [options.ASSET_NAMESPACE] the binding to the namespace that script references
91
- * @param {any} [options.ASSET_MANIFEST] the map of the key to cache and store in KV
92
- * */
93
- const getAssetFromKV = async (event: FetchEvent, options?: Partial<Options>): Promise<Response> => {
94
- options = assignOptions(options)
95
-
96
- const request = event.request
97
- const ASSET_NAMESPACE = options.ASSET_NAMESPACE
98
- const ASSET_MANIFEST =
99
- typeof options.ASSET_MANIFEST === 'string'
100
- ? JSON.parse(options.ASSET_MANIFEST)
101
- : options.ASSET_MANIFEST
102
-
103
- if (typeof ASSET_NAMESPACE === 'undefined') {
104
- throw new InternalError(`there is no KV namespace bound to the script`)
105
- }
106
-
107
- const rawPathKey = new URL(request.url).pathname.replace(/^\/+/, '') // strip any preceding /'s
108
- let pathIsEncoded = false
109
- let requestKey
110
- // if options.mapRequestToAsset is explicitly passed in, always use it and assume user has own intentions
111
- // otherwise handle request as normal, with default mapRequestToAsset below
112
- if (options.mapRequestToAsset) {
113
- requestKey = options.mapRequestToAsset(request)
114
- } else if (ASSET_MANIFEST[rawPathKey]) {
115
- requestKey = request
116
- } else if (ASSET_MANIFEST[decodeURIComponent(rawPathKey)]) {
117
- pathIsEncoded = true
118
- requestKey = request
119
- } else {
120
- const mappedRequest = mapRequestToAsset(request)
121
- const mappedRawPathKey = new URL(mappedRequest.url).pathname.replace(/^\/+/, '')
122
- if (ASSET_MANIFEST[decodeURIComponent(mappedRawPathKey)]) {
123
- pathIsEncoded = true
124
- requestKey = mappedRequest
125
- } else {
126
- // use default mapRequestToAsset
127
- requestKey = mapRequestToAsset(request, options)
128
- }
129
- }
130
-
131
- const SUPPORTED_METHODS = ['GET', 'HEAD']
132
- if (!SUPPORTED_METHODS.includes(requestKey.method)) {
133
- throw new MethodNotAllowedError(`${requestKey.method} is not a valid request method`)
134
- }
135
-
136
- const parsedUrl = new URL(requestKey.url)
137
- const pathname = pathIsEncoded ? decodeURIComponent(parsedUrl.pathname) : parsedUrl.pathname // decode percentage encoded path only when necessary
138
-
139
- // pathKey is the file path to look up in the manifest
140
- let pathKey = pathname.replace(/^\/+/, '') // remove prepended /
141
-
142
- // @ts-ignore
143
- const cache = caches.default
144
- let mimeType = mime.getType(pathKey) || options.defaultMimeType
145
- if (mimeType.startsWith('text') || mimeType === 'application/javascript') {
146
- mimeType += '; charset=utf-8'
147
- }
148
-
149
- let shouldEdgeCache = false // false if storing in KV by raw file path i.e. no hash
150
- // check manifest for map from file path to hash
151
- if (typeof ASSET_MANIFEST !== 'undefined') {
152
- if (ASSET_MANIFEST[pathKey]) {
153
- pathKey = ASSET_MANIFEST[pathKey]
154
- // if path key is in asset manifest, we can assume it contains a content hash and can be cached
155
- shouldEdgeCache = true
156
- }
157
- }
158
-
159
- // TODO this excludes search params from cache, investigate ideal behavior
160
- let cacheKey = new Request(`${parsedUrl.origin}/${pathKey}`, request)
161
-
162
- // if argument passed in for cacheControl is a function then
163
- // evaluate that function. otherwise return the Object passed in
164
- // or default Object
165
- const evalCacheOpts = (() => {
166
- switch (typeof options.cacheControl) {
167
- case 'function':
168
- return options.cacheControl(request)
169
- case 'object':
170
- return options.cacheControl
171
- default:
172
- return defaultCacheControl
173
- }
174
- })()
175
-
176
- // formats the etag depending on the response context. if the entityId
177
- // is invalid, returns an empty string (instead of null) to prevent the
178
- // the potentially disastrous scenario where the value of the Etag resp
179
- // header is "null". Could be modified in future to base64 encode etc
180
- const formatETag = (entityId: any = pathKey, validatorType: string = 'strong') => {
181
- if (!entityId) {
182
- return ''
183
- }
184
- switch (validatorType) {
185
- case 'weak':
186
- if (!entityId.startsWith('W/')) {
187
- return `W/${entityId}`
188
- }
189
- return entityId
190
- case 'strong':
191
- if (entityId.startsWith(`W/"`)) {
192
- entityId = entityId.replace('W/', '')
193
- }
194
- if (!entityId.endsWith(`"`)) {
195
- entityId = `"${entityId}"`
196
- }
197
- return entityId
198
- default:
199
- return ''
200
- }
201
- }
202
-
203
- options.cacheControl = Object.assign({}, defaultCacheControl, evalCacheOpts)
204
-
205
- // override shouldEdgeCache if options say to bypassCache
206
- if (
207
- options.cacheControl.bypassCache ||
208
- options.cacheControl.edgeTTL === null ||
209
- request.method == 'HEAD'
210
- ) {
211
- shouldEdgeCache = false
212
- }
213
- // only set max-age if explicitly passed in a number as an arg
214
- const shouldSetBrowserCache = typeof options.cacheControl.browserTTL === 'number'
215
-
216
- let response = null
217
- if (shouldEdgeCache) {
218
- response = await cache.match(cacheKey)
219
- }
220
-
221
- if (response) {
222
- if (response.status > 300 && response.status < 400) {
223
- if (response.body && 'cancel' in Object.getPrototypeOf(response.body)) {
224
- response.body.cancel()
225
- console.log('Body exists and environment supports readable streams. Body cancelled')
226
- } else {
227
- console.log('Environment doesnt support readable streams')
228
- }
229
- response = new Response(null, response)
230
- } else {
231
- // fixes #165
232
- let opts = {
233
- headers: new Headers(response.headers),
234
- status: 0,
235
- statusText: '',
236
- }
237
-
238
- opts.headers.set('cf-cache-status', 'HIT')
239
-
240
- if (response.status) {
241
- opts.status = response.status
242
- opts.statusText = response.statusText
243
- } else if (opts.headers.has('Content-Range')) {
244
- opts.status = 206
245
- opts.statusText = 'Partial Content'
246
- } else {
247
- opts.status = 200
248
- opts.statusText = 'OK'
249
- }
250
- response = new Response(response.body, opts)
251
- }
252
- } else {
253
- const body = await ASSET_NAMESPACE.get(pathKey, 'arrayBuffer')
254
- if (body === null) {
255
- throw new NotFoundError(`could not find ${pathKey} in your content namespace`)
256
- }
257
- response = new Response(body)
258
-
259
- if (shouldEdgeCache) {
260
- response.headers.set('Accept-Ranges', 'bytes')
261
- response.headers.set('Content-Length', body.length)
262
- // set etag before cache insertion
263
- if (!response.headers.has('etag')) {
264
- response.headers.set('etag', formatETag(pathKey, 'strong'))
265
- }
266
- // determine Cloudflare cache behavior
267
- response.headers.set('Cache-Control', `max-age=${options.cacheControl.edgeTTL}`)
268
- event.waitUntil(cache.put(cacheKey, response.clone()))
269
- response.headers.set('CF-Cache-Status', 'MISS')
270
- }
271
- }
272
- response.headers.set('Content-Type', mimeType)
273
-
274
- if (response.status === 304) {
275
- let etag = formatETag(response.headers.get('etag'), 'strong')
276
- let ifNoneMatch = cacheKey.headers.get('if-none-match')
277
- let proxyCacheStatus = response.headers.get('CF-Cache-Status')
278
- if (etag) {
279
- if (ifNoneMatch && ifNoneMatch === etag && proxyCacheStatus === 'MISS') {
280
- response.headers.set('CF-Cache-Status', 'EXPIRED')
281
- } else {
282
- response.headers.set('CF-Cache-Status', 'REVALIDATED')
283
- }
284
- response.headers.set('etag', formatETag(etag, 'weak'))
285
- }
286
- }
287
- if (shouldSetBrowserCache) {
288
- response.headers.set('Cache-Control', `max-age=${options.cacheControl.browserTTL}`)
289
- } else {
290
- response.headers.delete('Cache-Control')
291
- }
292
- return response
293
- }
294
-
295
- export { getAssetFromKV, mapRequestToAsset, serveSinglePageApp }
296
- export { Options, CacheControl, MethodNotAllowedError, NotFoundError, InternalError }
@@ -1,136 +0,0 @@
1
- const makeServiceWorkerEnv = require('service-worker-mock')
2
-
3
- const HASH = '123HASHBROWN'
4
-
5
- export const getEvent = (request: Request): any => {
6
- const waitUntil = async (callback: any) => {
7
- await callback
8
- }
9
- return {
10
- request,
11
- waitUntil,
12
- }
13
- }
14
- const store: any = {
15
- 'key1.123HASHBROWN.txt': 'val1',
16
- 'key1.123HASHBROWN.png': 'val1',
17
- 'index.123HASHBROWN.html': 'index.html',
18
- 'cache.123HASHBROWN.html': 'cache me if you can',
19
- '测试.123HASHBROWN.html': 'My filename is non-ascii',
20
- '%not-really-percent-encoded.123HASHBROWN.html': 'browser percent encoded',
21
- '%2F.123HASHBROWN.html': 'user percent encoded',
22
- '你好.123HASHBROWN.html': 'I shouldnt be served',
23
- '%E4%BD%A0%E5%A5%BD.123HASHBROWN.html': 'Im important',
24
- 'nohash.txt': 'no hash but still got some result',
25
- 'sub/blah.123HASHBROWN.png': 'picturedis',
26
- 'sub/index.123HASHBROWN.html': 'picturedis',
27
- 'client.123HASHBROWN': 'important file',
28
- 'client.123HASHBROWN/index.html': 'Im here but serve my big bro above',
29
- '你好/index.123HASHBROWN.html': 'My path is non-ascii',
30
- }
31
- export const mockKV = (store: any) => {
32
- return {
33
- get: (path: string) => store[path] || null,
34
- }
35
- }
36
-
37
- export const mockManifest = () => {
38
- return JSON.stringify({
39
- 'key1.txt': `key1.${HASH}.txt`,
40
- 'key1.png': `key1.${HASH}.png`,
41
- 'cache.html': `cache.${HASH}.html`,
42
- '测试.html': `测试.${HASH}.html`,
43
- '你好.html': `你好.${HASH}.html`,
44
- '%not-really-percent-encoded.html': `%not-really-percent-encoded.${HASH}.html`,
45
- '%2F.html': `%2F.${HASH}.html`,
46
- '%E4%BD%A0%E5%A5%BD.html': `%E4%BD%A0%E5%A5%BD.${HASH}.html`,
47
- 'index.html': `index.${HASH}.html`,
48
- 'sub/blah.png': `sub/blah.${HASH}.png`,
49
- 'sub/index.html': `sub/index.${HASH}.html`,
50
- client: `client.${HASH}`,
51
- 'client/index.html': `client.${HASH}`,
52
- '你好/index.html': `你好/index.${HASH}.html`,
53
- })
54
- }
55
-
56
- let cacheStore: any = new Map()
57
- interface CacheKey {
58
- url: object
59
- headers: object
60
- }
61
- export const mockCaches = () => {
62
- return {
63
- default: {
64
- async match(key: any) {
65
- let cacheKey: CacheKey = {
66
- url: key.url,
67
- headers: {},
68
- }
69
- let response
70
- if (key.headers.has('if-none-match')) {
71
- let makeStrongEtag = key.headers.get('if-none-match').replace('W/', '')
72
- Reflect.set(cacheKey.headers, 'etag', makeStrongEtag)
73
- response = cacheStore.get(JSON.stringify(cacheKey))
74
- } else {
75
- // if client doesn't send if-none-match, we need to iterate through these keys
76
- // and just test the URL
77
- const activeCacheKeys: Array<string> = Array.from(cacheStore.keys())
78
- for (const cacheStoreKey of activeCacheKeys) {
79
- if (JSON.parse(cacheStoreKey).url === key.url) {
80
- response = cacheStore.get(cacheStoreKey)
81
- }
82
- }
83
- }
84
- // TODO: write test to accomodate for rare scenarios with where range requests accomodate etags
85
- if (response && !key.headers.has('if-none-match')) {
86
- // this appears overly verbose, but is necessary to document edge cache behavior
87
- // The Range request header triggers the response header Content-Range ...
88
- const range = key.headers.get('range')
89
- if (range) {
90
- response.headers.set(
91
- 'content-range',
92
- `bytes ${range.split('=').pop()}/${response.headers.get('content-length')}`,
93
- )
94
- }
95
- // ... which we are using in this repository to set status 206
96
- if (response.headers.has('content-range')) {
97
- response.status = 206
98
- } else {
99
- response.status = 200
100
- }
101
- let etag = response.headers.get('etag')
102
- if (etag && !etag.includes('W/')) {
103
- response.headers.set('etag', `W/${etag}`)
104
- }
105
- }
106
- return response
107
- },
108
- async put(key: any, val: Response) {
109
- let headers = new Headers(val.headers)
110
- let url = new URL(key.url)
111
- let resWithBody = new Response(val.body, { headers, status: 200 })
112
- let resNoBody = new Response(null, { headers, status: 304 })
113
- let cacheKey: CacheKey = {
114
- url: key.url,
115
- headers: {
116
- etag: `"${url.pathname.replace('/', '')}"`,
117
- },
118
- }
119
- cacheStore.set(JSON.stringify(cacheKey), resNoBody)
120
- cacheKey.headers = {}
121
- cacheStore.set(JSON.stringify(cacheKey), resWithBody)
122
- return
123
- },
124
- },
125
- }
126
- }
127
-
128
- export function mockGlobal() {
129
- Object.assign(global, makeServiceWorkerEnv())
130
- Object.assign(global, { __STATIC_CONTENT_MANIFEST: mockManifest() })
131
- Object.assign(global, { __STATIC_CONTENT: mockKV(store) })
132
- Object.assign(global, { caches: mockCaches() })
133
- }
134
- export const sleep = (milliseconds: number) => {
135
- return new Promise((resolve) => setTimeout(resolve, milliseconds))
136
- }