wrangler 0.0.0-e3a458e → 0.0.0-e3c036d7

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 (361) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -14
  3. package/bin/wrangler.js +181 -29
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +6368 -0
  6. package/package.json +193 -142
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +238 -0
  9. package/src/__tests__/api-devregistry.test.js +64 -0
  10. package/src/__tests__/api.test.ts +80 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +42 -0
  13. package/src/__tests__/configuration.test.ts +4454 -2590
  14. package/src/__tests__/d1/d1.test.ts +82 -0
  15. package/src/__tests__/d1/execute.test.ts +64 -0
  16. package/src/__tests__/d1/migrate.test.ts +155 -0
  17. package/src/__tests__/d1/splitter.test.ts +255 -0
  18. package/src/__tests__/delete.test.ts +266 -0
  19. package/src/__tests__/deployments.test.ts +129 -0
  20. package/src/__tests__/dev.test.tsx +1589 -432
  21. package/src/__tests__/generate.test.ts +238 -0
  22. package/src/__tests__/get-host-from-url.test.ts +16 -0
  23. package/src/__tests__/guess-worker-format.test.ts +113 -51
  24. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  25. package/src/__tests__/helpers/end-event-loop.ts +6 -0
  26. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  27. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  28. package/src/__tests__/helpers/mock-bin.ts +20 -20
  29. package/src/__tests__/helpers/mock-console.ts +68 -32
  30. package/src/__tests__/helpers/mock-dialogs.ts +107 -33
  31. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +25 -0
  32. package/src/__tests__/helpers/mock-get-zone-from-host.ts +11 -0
  33. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  34. package/src/__tests__/helpers/mock-istty.ts +74 -0
  35. package/src/__tests__/helpers/mock-known-routes.ts +12 -0
  36. package/src/__tests__/helpers/mock-kv.ts +41 -28
  37. package/src/__tests__/helpers/mock-oauth-flow.ts +263 -0
  38. package/src/__tests__/helpers/mock-process.ts +34 -0
  39. package/src/__tests__/helpers/mock-set-timeout.ts +16 -0
  40. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  41. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  42. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  43. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  44. package/src/__tests__/helpers/msw/handlers/deployments.ts +93 -0
  45. package/src/__tests__/helpers/msw/handlers/namespaces.ts +81 -0
  46. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  47. package/src/__tests__/helpers/msw/handlers/r2.ts +60 -0
  48. package/src/__tests__/helpers/msw/handlers/script.ts +56 -0
  49. package/src/__tests__/helpers/msw/handlers/user.ts +52 -0
  50. package/src/__tests__/helpers/msw/handlers/zones.ts +20 -0
  51. package/src/__tests__/helpers/msw/index.ts +50 -0
  52. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  53. package/src/__tests__/helpers/run-in-tmp.ts +28 -22
  54. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  55. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +28 -0
  56. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  57. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  58. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  59. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  60. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  61. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  62. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  63. package/src/__tests__/https-options.test.ts +144 -108
  64. package/src/__tests__/index.test.ts +258 -975
  65. package/src/__tests__/init.test.ts +3165 -0
  66. package/src/__tests__/jest.setup.ts +154 -48
  67. package/src/__tests__/kv.test.ts +1789 -1372
  68. package/src/__tests__/logger.test.ts +141 -0
  69. package/src/__tests__/logout.test.ts +36 -41
  70. package/src/__tests__/metrics.test.ts +493 -0
  71. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  72. package/src/__tests__/middleware.test.ts +816 -0
  73. package/src/__tests__/mtls-certificates.test.ts +585 -0
  74. package/src/__tests__/package-manager.test.ts +312 -165
  75. package/src/__tests__/pages/deployment-list.test.ts +78 -0
  76. package/src/__tests__/pages/functions-build.test.ts +453 -0
  77. package/src/__tests__/pages/pages.test.ts +81 -0
  78. package/src/__tests__/pages/project-create.test.ts +63 -0
  79. package/src/__tests__/pages/project-list.test.ts +108 -0
  80. package/src/__tests__/pages/project-upload.test.ts +481 -0
  81. package/src/__tests__/pages/publish.test.ts +2745 -0
  82. package/src/__tests__/pages-deployment-tail.test.ts +955 -0
  83. package/src/__tests__/parse.test.ts +359 -245
  84. package/src/__tests__/paths.test.ts +39 -0
  85. package/src/__tests__/publish.test.ts +8209 -2558
  86. package/src/__tests__/pubsub.test.ts +493 -0
  87. package/src/__tests__/queues.test.ts +529 -0
  88. package/src/__tests__/r2.test.ts +369 -193
  89. package/src/__tests__/route.test.ts +22 -22
  90. package/src/__tests__/secret.test.ts +683 -415
  91. package/src/__tests__/tail.test.ts +838 -444
  92. package/src/__tests__/test-old-node-version.js +31 -0
  93. package/src/__tests__/tsconfig-sanity.ts +12 -0
  94. package/src/__tests__/tsconfig.json +8 -0
  95. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  96. package/src/__tests__/type-generation.test.ts +234 -0
  97. package/src/__tests__/user.test.ts +118 -0
  98. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  99. package/src/__tests__/validate-dev-props.test.ts +56 -0
  100. package/src/__tests__/version.test.ts +35 -0
  101. package/src/__tests__/whoami.test.tsx +166 -94
  102. package/src/__tests__/worker-namespace.test.ts +339 -0
  103. package/src/abort.d.ts +3 -0
  104. package/src/api/dev.ts +327 -0
  105. package/src/api/index.ts +11 -0
  106. package/src/api/mtls-certificate.ts +148 -0
  107. package/src/api/pages/create-worker-bundle-contents.ts +75 -0
  108. package/src/api/pages/index.ts +5 -0
  109. package/src/api/pages/publish.tsx +375 -0
  110. package/src/bundle-reporter.ts +68 -0
  111. package/src/bundle.ts +858 -110
  112. package/src/cfetch/index.ts +123 -69
  113. package/src/cfetch/internal.ts +217 -98
  114. package/src/cli.ts +23 -14
  115. package/src/config/README.md +7 -7
  116. package/src/config/config.ts +232 -229
  117. package/src/config/diagnostics.ts +61 -61
  118. package/src/config/environment.ts +542 -256
  119. package/src/config/index.ts +367 -34
  120. package/src/config/validation-helpers.ts +415 -286
  121. package/src/config/validation.ts +2137 -942
  122. package/src/config-cache.ts +85 -0
  123. package/src/create-worker-preview.ts +251 -124
  124. package/src/create-worker-upload-form.ts +326 -201
  125. package/src/d1/backups.tsx +215 -0
  126. package/src/d1/constants.ts +2 -0
  127. package/src/d1/create.tsx +64 -0
  128. package/src/d1/delete.ts +53 -0
  129. package/src/d1/execute.tsx +354 -0
  130. package/src/d1/formatTimeAgo.ts +14 -0
  131. package/src/d1/index.ts +100 -0
  132. package/src/d1/list.tsx +60 -0
  133. package/src/d1/migrations/apply.tsx +206 -0
  134. package/src/d1/migrations/create.tsx +76 -0
  135. package/src/d1/migrations/helpers.ts +172 -0
  136. package/src/d1/migrations/index.ts +3 -0
  137. package/src/d1/migrations/list.tsx +92 -0
  138. package/src/d1/migrations/options.ts +23 -0
  139. package/src/d1/options.ts +22 -0
  140. package/src/d1/splitter.ts +161 -0
  141. package/src/d1/types.ts +25 -0
  142. package/src/d1/utils.ts +49 -0
  143. package/src/delete.ts +100 -0
  144. package/src/deployments.ts +116 -0
  145. package/src/deprecated/index.ts +144 -0
  146. package/src/dev/dev-vars.ts +39 -0
  147. package/src/dev/dev.tsx +544 -306
  148. package/src/dev/get-local-persistence-path.ts +31 -0
  149. package/src/dev/local.tsx +906 -233
  150. package/src/dev/remote.tsx +621 -192
  151. package/src/dev/start-server.ts +540 -0
  152. package/src/dev/use-esbuild.ts +189 -80
  153. package/src/dev/validate-dev-props.ts +40 -0
  154. package/src/dev-registry.ts +201 -0
  155. package/src/dev.tsx +928 -0
  156. package/src/dialogs.ts +136 -0
  157. package/src/dispatch-namespace.ts +211 -0
  158. package/src/docs/index.ts +97 -0
  159. package/src/durable.ts +102 -0
  160. package/src/entry.ts +277 -109
  161. package/src/environment-variables/factory.ts +88 -0
  162. package/src/environment-variables/misc-variables.ts +30 -0
  163. package/src/errors.ts +11 -0
  164. package/src/generate/index.ts +298 -0
  165. package/src/git-client.ts +135 -0
  166. package/src/global-wrangler-config-path.ts +26 -0
  167. package/src/https-options.ts +85 -80
  168. package/src/index.ts +705 -0
  169. package/src/init.ts +1023 -0
  170. package/src/inspect.ts +722 -557
  171. package/src/intl-polyfill.d.ts +139 -0
  172. package/src/is-ci.ts +14 -0
  173. package/src/is-interactive.ts +16 -0
  174. package/src/jest.d.ts +4 -0
  175. package/src/kv/helpers.ts +430 -0
  176. package/src/kv/index.ts +594 -0
  177. package/src/logger.ts +104 -0
  178. package/src/metrics/index.ts +5 -0
  179. package/src/metrics/metrics-config.ts +239 -0
  180. package/src/metrics/metrics-dispatcher.ts +96 -0
  181. package/src/metrics/metrics-usage-headers.ts +24 -0
  182. package/src/metrics/send-event.ts +98 -0
  183. package/src/miniflare-cli/README.md +30 -0
  184. package/src/miniflare-cli/assets.ts +251 -0
  185. package/src/miniflare-cli/index.ts +210 -0
  186. package/src/miniflare-cli/request-context.ts +40 -0
  187. package/src/miniflare-cli/tsconfig.json +9 -0
  188. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  189. package/src/miniflare-cli/types.ts +11 -0
  190. package/src/module-collection.ts +241 -227
  191. package/src/mtls-certificate/cli.ts +155 -0
  192. package/src/open-in-browser.ts +8 -16
  193. package/src/package-manager.ts +183 -90
  194. package/src/pages/build.ts +252 -0
  195. package/src/pages/buildFunctions.ts +133 -0
  196. package/src/pages/constants.ts +18 -0
  197. package/src/pages/deployment-tails.ts +281 -0
  198. package/src/pages/deployments.tsx +86 -0
  199. package/src/pages/dev.ts +726 -0
  200. package/src/pages/errors.ts +66 -0
  201. package/src/pages/functions/buildPlugin.ts +97 -0
  202. package/src/pages/functions/buildWorker.ts +294 -0
  203. package/src/pages/functions/filepath-routing.test.ts +234 -0
  204. package/src/pages/functions/filepath-routing.ts +189 -0
  205. package/src/pages/functions/identifiers.ts +78 -0
  206. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  207. package/src/pages/functions/routes-consolidation.ts +73 -0
  208. package/src/pages/functions/routes-transformation.test.ts +282 -0
  209. package/src/pages/functions/routes-transformation.ts +115 -0
  210. package/src/pages/functions/routes-validation.test.ts +403 -0
  211. package/src/pages/functions/routes-validation.ts +202 -0
  212. package/src/pages/functions/routes.ts +151 -0
  213. package/src/pages/functions/tsconfig.json +8 -0
  214. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  215. package/src/pages/functions.ts +86 -0
  216. package/src/pages/hash.ts +13 -0
  217. package/src/pages/index.ts +102 -0
  218. package/src/pages/projects.tsx +162 -0
  219. package/src/pages/prompt-select-project.tsx +31 -0
  220. package/src/pages/publish.tsx +276 -0
  221. package/src/pages/types.ts +46 -0
  222. package/src/pages/upload.tsx +449 -0
  223. package/src/pages/utils.ts +23 -0
  224. package/src/parse.ts +247 -122
  225. package/src/paths.ts +53 -8
  226. package/src/proxy.ts +629 -375
  227. package/src/publish/index.ts +274 -0
  228. package/src/publish/publish.ts +1076 -0
  229. package/src/pubsub/index.ts +286 -0
  230. package/src/pubsub/pubsub-commands.ts +623 -0
  231. package/src/queues/cli/commands/consumer/add.ts +65 -0
  232. package/src/queues/cli/commands/consumer/index.ts +19 -0
  233. package/src/queues/cli/commands/consumer/remove.ts +31 -0
  234. package/src/queues/cli/commands/create.ts +25 -0
  235. package/src/queues/cli/commands/delete.ts +26 -0
  236. package/src/queues/cli/commands/index.ts +35 -0
  237. package/src/queues/cli/commands/list.ts +25 -0
  238. package/src/queues/client.ts +135 -0
  239. package/src/queues/utils.ts +18 -0
  240. package/src/r2/constants.ts +4 -0
  241. package/src/r2/helpers.ts +132 -0
  242. package/src/r2/index.ts +289 -0
  243. package/src/routes.ts +140 -0
  244. package/src/secret/index.ts +375 -0
  245. package/src/selfsigned.d.ts +23 -23
  246. package/src/sites.ts +372 -0
  247. package/src/tail/createTail.ts +406 -0
  248. package/src/tail/filters.ts +109 -111
  249. package/src/tail/index.ts +208 -296
  250. package/src/tail/printing.ts +107 -47
  251. package/src/tsconfig-sanity.ts +16 -0
  252. package/src/type-generation.ts +181 -0
  253. package/src/update-check.ts +19 -0
  254. package/src/user/access.ts +68 -0
  255. package/src/user/auth-variables.ts +113 -0
  256. package/src/user/choose-account.tsx +39 -0
  257. package/src/user/generate-auth-url.ts +33 -0
  258. package/src/user/generate-random-state.ts +16 -0
  259. package/src/user/index.ts +2 -0
  260. package/src/user/user.ts +1233 -0
  261. package/src/utils/collectKeyValues.ts +14 -0
  262. package/src/whoami.ts +135 -0
  263. package/src/worker.ts +201 -100
  264. package/src/yargs-types.ts +37 -0
  265. package/src/zones.ts +191 -0
  266. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  267. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  268. package/templates/__tests__/tsconfig.json +8 -0
  269. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  270. package/templates/checked-fetch.js +23 -0
  271. package/templates/d1-beta-facade.js +241 -0
  272. package/templates/facade.d.ts +14 -0
  273. package/templates/first-party-worker-module-facade.ts +19 -0
  274. package/templates/format-dev-errors.ts +34 -0
  275. package/templates/gitignore +172 -0
  276. package/templates/init-tests/test-jest-new-worker.js +23 -0
  277. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  278. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  279. package/templates/middleware/common.ts +62 -0
  280. package/templates/middleware/loader-modules.ts +84 -0
  281. package/templates/middleware/loader-sw.ts +233 -0
  282. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  283. package/templates/middleware/middleware-pretty-error.ts +40 -0
  284. package/templates/middleware/middleware-scheduled.ts +15 -0
  285. package/templates/new-worker-scheduled.js +17 -0
  286. package/templates/new-worker-scheduled.ts +32 -0
  287. package/templates/new-worker.js +3 -3
  288. package/templates/new-worker.ts +21 -3
  289. package/templates/no-op-worker.js +10 -0
  290. package/templates/pages-dev-pipeline.ts +31 -0
  291. package/templates/pages-dev-util.ts +52 -0
  292. package/templates/pages-shim.ts +12 -0
  293. package/templates/pages-template-plugin.ts +180 -0
  294. package/templates/pages-template-worker.ts +189 -0
  295. package/templates/serve-static-assets.ts +66 -0
  296. package/templates/service-bindings-module-facade.js +55 -0
  297. package/templates/service-bindings-sw-facade.js +42 -0
  298. package/templates/tsconfig-sanity.ts +11 -0
  299. package/templates/tsconfig.init.json +106 -0
  300. package/templates/tsconfig.json +6 -103
  301. package/templates/tsconfig.tsbuildinfo +1 -0
  302. package/wrangler-dist/cli.d.ts +1624 -0
  303. package/wrangler-dist/cli.js +139300 -128984
  304. package/wrangler-dist/wasm-sync.wasm +0 -0
  305. package/pages/functions/buildWorker.ts +0 -62
  306. package/pages/functions/filepath-routing.test.ts +0 -223
  307. package/pages/functions/filepath-routing.ts +0 -181
  308. package/pages/functions/identifiers.ts +0 -78
  309. package/pages/functions/routes.ts +0 -151
  310. package/pages/functions/template-worker.ts +0 -130
  311. package/src/__tests__/dev2.test.tsx +0 -154
  312. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  313. package/src/__tests__/helpers/mock-cfetch.ts +0 -207
  314. package/src/__tests__/pages.test.ts +0 -44
  315. package/src/__tests__/sentry.test.ts +0 -146
  316. package/src/dialogs.tsx +0 -77
  317. package/src/environment-variables.ts +0 -33
  318. package/src/index.tsx +0 -2365
  319. package/src/kv.ts +0 -339
  320. package/src/pages.tsx +0 -1045
  321. package/src/publish.ts +0 -354
  322. package/src/r2.ts +0 -50
  323. package/src/reporting.ts +0 -131
  324. package/src/sites.tsx +0 -273
  325. package/src/user.tsx +0 -1118
  326. package/src/whoami.tsx +0 -69
  327. package/templates/static-asset-facade.js +0 -47
  328. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  329. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  330. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  331. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  332. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  333. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  334. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  335. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  336. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  337. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  338. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  339. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  340. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  341. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  342. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  343. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  344. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  345. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  346. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  347. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  348. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  349. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  350. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  351. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  352. package/vendor/wrangler-mime/LICENSE +0 -21
  353. package/vendor/wrangler-mime/Mime.js +0 -97
  354. package/vendor/wrangler-mime/README.md +0 -187
  355. package/vendor/wrangler-mime/cli.js +0 -46
  356. package/vendor/wrangler-mime/index.js +0 -4
  357. package/vendor/wrangler-mime/lite.js +0 -4
  358. package/vendor/wrangler-mime/package.json +0 -52
  359. package/vendor/wrangler-mime/types/other.js +0 -1
  360. package/vendor/wrangler-mime/types/standard.js +0 -1
  361. package/wrangler-dist/cli.js.map +0 -7
@@ -1,148 +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
- exports.sleep = exports.mockGlobal = exports.mockCaches = exports.mockManifest = exports.mockKV = exports.getEvent = void 0;
13
- const makeServiceWorkerEnv = require('service-worker-mock');
14
- const HASH = '123HASHBROWN';
15
- const getEvent = (request) => {
16
- const waitUntil = (callback) => __awaiter(void 0, void 0, void 0, function* () {
17
- yield callback;
18
- });
19
- return {
20
- request,
21
- waitUntil,
22
- };
23
- };
24
- exports.getEvent = getEvent;
25
- const store = {
26
- 'key1.123HASHBROWN.txt': 'val1',
27
- 'key1.123HASHBROWN.png': 'val1',
28
- 'index.123HASHBROWN.html': 'index.html',
29
- 'cache.123HASHBROWN.html': 'cache me if you can',
30
- '测试.123HASHBROWN.html': 'My filename is non-ascii',
31
- '%not-really-percent-encoded.123HASHBROWN.html': 'browser percent encoded',
32
- '%2F.123HASHBROWN.html': 'user percent encoded',
33
- '你好.123HASHBROWN.html': 'I shouldnt be served',
34
- '%E4%BD%A0%E5%A5%BD.123HASHBROWN.html': 'Im important',
35
- 'nohash.txt': 'no hash but still got some result',
36
- 'sub/blah.123HASHBROWN.png': 'picturedis',
37
- 'sub/index.123HASHBROWN.html': 'picturedis',
38
- 'client.123HASHBROWN': 'important file',
39
- 'client.123HASHBROWN/index.html': 'Im here but serve my big bro above',
40
- '你好/index.123HASHBROWN.html': 'My path is non-ascii',
41
- };
42
- const mockKV = (store) => {
43
- return {
44
- get: (path) => store[path] || null,
45
- };
46
- };
47
- exports.mockKV = mockKV;
48
- const mockManifest = () => {
49
- return JSON.stringify({
50
- 'key1.txt': `key1.${HASH}.txt`,
51
- 'key1.png': `key1.${HASH}.png`,
52
- 'cache.html': `cache.${HASH}.html`,
53
- '测试.html': `测试.${HASH}.html`,
54
- '你好.html': `你好.${HASH}.html`,
55
- '%not-really-percent-encoded.html': `%not-really-percent-encoded.${HASH}.html`,
56
- '%2F.html': `%2F.${HASH}.html`,
57
- '%E4%BD%A0%E5%A5%BD.html': `%E4%BD%A0%E5%A5%BD.${HASH}.html`,
58
- 'index.html': `index.${HASH}.html`,
59
- 'sub/blah.png': `sub/blah.${HASH}.png`,
60
- 'sub/index.html': `sub/index.${HASH}.html`,
61
- client: `client.${HASH}`,
62
- 'client/index.html': `client.${HASH}`,
63
- '你好/index.html': `你好/index.${HASH}.html`,
64
- });
65
- };
66
- exports.mockManifest = mockManifest;
67
- let cacheStore = new Map();
68
- const mockCaches = () => {
69
- return {
70
- default: {
71
- match(key) {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- let cacheKey = {
74
- url: key.url,
75
- headers: {},
76
- };
77
- let response;
78
- if (key.headers.has('if-none-match')) {
79
- let makeStrongEtag = key.headers.get('if-none-match').replace('W/', '');
80
- Reflect.set(cacheKey.headers, 'etag', makeStrongEtag);
81
- response = cacheStore.get(JSON.stringify(cacheKey));
82
- }
83
- else {
84
- // if client doesn't send if-none-match, we need to iterate through these keys
85
- // and just test the URL
86
- const activeCacheKeys = Array.from(cacheStore.keys());
87
- for (const cacheStoreKey of activeCacheKeys) {
88
- if (JSON.parse(cacheStoreKey).url === key.url) {
89
- response = cacheStore.get(cacheStoreKey);
90
- }
91
- }
92
- }
93
- // TODO: write test to accomodate for rare scenarios with where range requests accomodate etags
94
- if (response && !key.headers.has('if-none-match')) {
95
- // this appears overly verbose, but is necessary to document edge cache behavior
96
- // The Range request header triggers the response header Content-Range ...
97
- const range = key.headers.get('range');
98
- if (range) {
99
- response.headers.set('content-range', `bytes ${range.split('=').pop()}/${response.headers.get('content-length')}`);
100
- }
101
- // ... which we are using in this repository to set status 206
102
- if (response.headers.has('content-range')) {
103
- response.status = 206;
104
- }
105
- else {
106
- response.status = 200;
107
- }
108
- let etag = response.headers.get('etag');
109
- if (etag && !etag.includes('W/')) {
110
- response.headers.set('etag', `W/${etag}`);
111
- }
112
- }
113
- return response;
114
- });
115
- },
116
- put(key, val) {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- let headers = new Headers(val.headers);
119
- let url = new URL(key.url);
120
- let resWithBody = new Response(val.body, { headers, status: 200 });
121
- let resNoBody = new Response(null, { headers, status: 304 });
122
- let cacheKey = {
123
- url: key.url,
124
- headers: {
125
- etag: `"${url.pathname.replace('/', '')}"`,
126
- },
127
- };
128
- cacheStore.set(JSON.stringify(cacheKey), resNoBody);
129
- cacheKey.headers = {};
130
- cacheStore.set(JSON.stringify(cacheKey), resWithBody);
131
- return;
132
- });
133
- },
134
- },
135
- };
136
- };
137
- exports.mockCaches = mockCaches;
138
- function mockGlobal() {
139
- Object.assign(global, makeServiceWorkerEnv());
140
- Object.assign(global, { __STATIC_CONTENT_MANIFEST: exports.mockManifest() });
141
- Object.assign(global, { __STATIC_CONTENT: exports.mockKV(store) });
142
- Object.assign(global, { caches: exports.mockCaches() });
143
- }
144
- exports.mockGlobal = mockGlobal;
145
- const sleep = (milliseconds) => {
146
- return new Promise((resolve) => setTimeout(resolve, milliseconds));
147
- };
148
- exports.sleep = sleep;
@@ -1,436 +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
- ava_1.default('getAssetFromKV return correct val from KV and default caching', (t) => __awaiter(void 0, void 0, void 0, function* () {
16
- mocks_1.mockGlobal();
17
- const event = mocks_1.getEvent(new Request('https://blah.com/key1.txt'));
18
- const res = yield index_1.getAssetFromKV(event);
19
- if (res) {
20
- t.is(res.headers.get('cache-control'), null);
21
- t.is(res.headers.get('cf-cache-status'), 'MISS');
22
- t.is(yield res.text(), 'val1');
23
- t.true(res.headers.get('content-type').includes('text'));
24
- }
25
- else {
26
- t.fail('Response was undefined');
27
- }
28
- }));
29
- ava_1.default('getAssetFromKV evaluated the file matching the extensionless path first /client/ -> client', (t) => __awaiter(void 0, void 0, void 0, function* () {
30
- mocks_1.mockGlobal();
31
- const event = mocks_1.getEvent(new Request(`https://foo.com/client/`));
32
- const res = yield index_1.getAssetFromKV(event);
33
- t.is(yield res.text(), 'important file');
34
- t.true(res.headers.get('content-type').includes('text'));
35
- }));
36
- ava_1.default('getAssetFromKV evaluated the file matching the extensionless path first /client -> client', (t) => __awaiter(void 0, void 0, void 0, function* () {
37
- mocks_1.mockGlobal();
38
- const event = mocks_1.getEvent(new Request(`https://foo.com/client`));
39
- const res = yield index_1.getAssetFromKV(event);
40
- t.is(yield res.text(), 'important file');
41
- t.true(res.headers.get('content-type').includes('text'));
42
- }));
43
- ava_1.default('getAssetFromKV if not in asset manifest still returns nohash.txt', (t) => __awaiter(void 0, void 0, void 0, function* () {
44
- mocks_1.mockGlobal();
45
- const event = mocks_1.getEvent(new Request('https://blah.com/nohash.txt'));
46
- const res = yield index_1.getAssetFromKV(event);
47
- if (res) {
48
- t.is(yield res.text(), 'no hash but still got some result');
49
- t.true(res.headers.get('content-type').includes('text'));
50
- }
51
- else {
52
- t.fail('Response was undefined');
53
- }
54
- }));
55
- ava_1.default('getAssetFromKV if no asset manifest /client -> client fails', (t) => __awaiter(void 0, void 0, void 0, function* () {
56
- mocks_1.mockGlobal();
57
- const event = mocks_1.getEvent(new Request(`https://foo.com/client`));
58
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event, { ASSET_MANIFEST: {} }));
59
- t.is(error.status, 404);
60
- }));
61
- ava_1.default('getAssetFromKV if sub/ -> sub/index.html served', (t) => __awaiter(void 0, void 0, void 0, function* () {
62
- mocks_1.mockGlobal();
63
- const event = mocks_1.getEvent(new Request(`https://foo.com/sub`));
64
- const res = yield index_1.getAssetFromKV(event);
65
- if (res) {
66
- t.is(yield res.text(), 'picturedis');
67
- }
68
- else {
69
- t.fail('Response was undefined');
70
- }
71
- }));
72
- ava_1.default('getAssetFromKV gets index.html by default for / requests', (t) => __awaiter(void 0, void 0, void 0, function* () {
73
- mocks_1.mockGlobal();
74
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
75
- const res = yield index_1.getAssetFromKV(event);
76
- if (res) {
77
- t.is(yield res.text(), 'index.html');
78
- t.true(res.headers.get('content-type').includes('html'));
79
- }
80
- else {
81
- t.fail('Response was undefined');
82
- }
83
- }));
84
- ava_1.default('getAssetFromKV non ASCII path support', (t) => __awaiter(void 0, void 0, void 0, function* () {
85
- mocks_1.mockGlobal();
86
- const event = mocks_1.getEvent(new Request('https://blah.com/测试.html'));
87
- const res = yield index_1.getAssetFromKV(event);
88
- if (res) {
89
- t.is(yield res.text(), 'My filename is non-ascii');
90
- }
91
- else {
92
- t.fail('Response was undefined');
93
- }
94
- }));
95
- ava_1.default('getAssetFromKV supports browser percent encoded URLs', (t) => __awaiter(void 0, void 0, void 0, function* () {
96
- mocks_1.mockGlobal();
97
- const event = mocks_1.getEvent(new Request('https://example.com/%not-really-percent-encoded.html'));
98
- const res = yield index_1.getAssetFromKV(event);
99
- if (res) {
100
- t.is(yield res.text(), 'browser percent encoded');
101
- }
102
- else {
103
- t.fail('Response was undefined');
104
- }
105
- }));
106
- ava_1.default('getAssetFromKV supports user percent encoded URLs', (t) => __awaiter(void 0, void 0, void 0, function* () {
107
- mocks_1.mockGlobal();
108
- const event = mocks_1.getEvent(new Request('https://blah.com/%2F.html'));
109
- const res = yield index_1.getAssetFromKV(event);
110
- if (res) {
111
- t.is(yield res.text(), 'user percent encoded');
112
- }
113
- else {
114
- t.fail('Response was undefined');
115
- }
116
- }));
117
- ava_1.default('getAssetFromKV only decode URL when necessary', (t) => __awaiter(void 0, void 0, void 0, function* () {
118
- mocks_1.mockGlobal();
119
- const event1 = mocks_1.getEvent(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD.html'));
120
- const event2 = mocks_1.getEvent(new Request('https://blah.com/你好.html'));
121
- const res1 = yield index_1.getAssetFromKV(event1);
122
- const res2 = yield index_1.getAssetFromKV(event2);
123
- if (res1 && res2) {
124
- t.is(yield res1.text(), 'Im important');
125
- t.is(yield res2.text(), 'Im important');
126
- }
127
- else {
128
- t.fail('Response was undefined');
129
- }
130
- }));
131
- ava_1.default('getAssetFromKV Support for user decode url path', (t) => __awaiter(void 0, void 0, void 0, function* () {
132
- mocks_1.mockGlobal();
133
- const event1 = mocks_1.getEvent(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD/'));
134
- const event2 = mocks_1.getEvent(new Request('https://blah.com/你好/'));
135
- const res1 = yield index_1.getAssetFromKV(event1);
136
- const res2 = yield index_1.getAssetFromKV(event2);
137
- if (res1 && res2) {
138
- t.is(yield res1.text(), 'My path is non-ascii');
139
- t.is(yield res2.text(), 'My path is non-ascii');
140
- }
141
- else {
142
- t.fail('Response was undefined');
143
- }
144
- }));
145
- ava_1.default('getAssetFromKV custom key modifier', (t) => __awaiter(void 0, void 0, void 0, function* () {
146
- mocks_1.mockGlobal();
147
- const event = mocks_1.getEvent(new Request('https://blah.com/docs/sub/blah.png'));
148
- const customRequestMapper = (request) => {
149
- let defaultModifiedRequest = index_1.mapRequestToAsset(request);
150
- let url = new URL(defaultModifiedRequest.url);
151
- url.pathname = url.pathname.replace('/docs', '');
152
- return new Request(url.toString(), request);
153
- };
154
- const res = yield index_1.getAssetFromKV(event, { mapRequestToAsset: customRequestMapper });
155
- if (res) {
156
- t.is(yield res.text(), 'picturedis');
157
- }
158
- else {
159
- t.fail('Response was undefined');
160
- }
161
- }));
162
- ava_1.default('getAssetFromKV when setting browser caching', (t) => __awaiter(void 0, void 0, void 0, function* () {
163
- mocks_1.mockGlobal();
164
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
165
- const res = yield index_1.getAssetFromKV(event, { cacheControl: { browserTTL: 22 } });
166
- if (res) {
167
- t.is(res.headers.get('cache-control'), 'max-age=22');
168
- }
169
- else {
170
- t.fail('Response was undefined');
171
- }
172
- }));
173
- ava_1.default('getAssetFromKV when setting custom cache setting', (t) => __awaiter(void 0, void 0, void 0, function* () {
174
- mocks_1.mockGlobal();
175
- const event1 = mocks_1.getEvent(new Request('https://blah.com/'));
176
- const event2 = mocks_1.getEvent(new Request('https://blah.com/key1.png?blah=34'));
177
- const cacheOnlyPngs = (req) => {
178
- if (new URL(req.url).pathname.endsWith('.png'))
179
- return {
180
- browserTTL: 720,
181
- edgeTTL: 720,
182
- };
183
- else
184
- return {
185
- bypassCache: true,
186
- };
187
- };
188
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: cacheOnlyPngs });
189
- const res2 = yield index_1.getAssetFromKV(event2, { cacheControl: cacheOnlyPngs });
190
- if (res1 && res2) {
191
- t.is(res1.headers.get('cache-control'), null);
192
- t.true(res2.headers.get('content-type').includes('png'));
193
- t.is(res2.headers.get('cache-control'), 'max-age=720');
194
- t.is(res2.headers.get('cf-cache-status'), 'MISS');
195
- }
196
- else {
197
- t.fail('Response was undefined');
198
- }
199
- }));
200
- ava_1.default('getAssetFromKV caches on two sequential requests', (t) => __awaiter(void 0, void 0, void 0, function* () {
201
- mocks_1.mockGlobal();
202
- const resourceKey = 'cache.html';
203
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
204
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
205
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, {
206
- headers: {
207
- 'if-none-match': `"${resourceVersion}"`,
208
- },
209
- }));
210
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720, browserTTL: 720 } });
211
- yield mocks_1.sleep(1);
212
- const res2 = yield index_1.getAssetFromKV(event2);
213
- if (res1 && res2) {
214
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
215
- t.is(res1.headers.get('cache-control'), 'max-age=720');
216
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
217
- }
218
- else {
219
- t.fail('Response was undefined');
220
- }
221
- }));
222
- ava_1.default('getAssetFromKV does not store max-age on two sequential requests', (t) => __awaiter(void 0, void 0, void 0, function* () {
223
- mocks_1.mockGlobal();
224
- const resourceKey = 'cache.html';
225
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
226
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
227
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, {
228
- headers: {
229
- 'if-none-match': `"${resourceVersion}"`,
230
- },
231
- }));
232
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } });
233
- yield mocks_1.sleep(100);
234
- const res2 = yield index_1.getAssetFromKV(event2);
235
- if (res1 && res2) {
236
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
237
- t.is(res1.headers.get('cache-control'), null);
238
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
239
- t.is(res2.headers.get('cache-control'), null);
240
- }
241
- else {
242
- t.fail('Response was undefined');
243
- }
244
- }));
245
- ava_1.default('getAssetFromKV does not cache on Cloudflare when bypass cache set', (t) => __awaiter(void 0, void 0, void 0, function* () {
246
- mocks_1.mockGlobal();
247
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
248
- const res = yield index_1.getAssetFromKV(event, { cacheControl: { bypassCache: true } });
249
- if (res) {
250
- t.is(res.headers.get('cache-control'), null);
251
- t.is(res.headers.get('cf-cache-status'), null);
252
- }
253
- else {
254
- t.fail('Response was undefined');
255
- }
256
- }));
257
- ava_1.default('getAssetFromKV with no trailing slash on root', (t) => __awaiter(void 0, void 0, void 0, function* () {
258
- mocks_1.mockGlobal();
259
- const event = mocks_1.getEvent(new Request('https://blah.com'));
260
- const res = yield index_1.getAssetFromKV(event);
261
- if (res) {
262
- t.is(yield res.text(), 'index.html');
263
- }
264
- else {
265
- t.fail('Response was undefined');
266
- }
267
- }));
268
- ava_1.default('getAssetFromKV with no trailing slash on a subdirectory', (t) => __awaiter(void 0, void 0, void 0, function* () {
269
- mocks_1.mockGlobal();
270
- const event = mocks_1.getEvent(new Request('https://blah.com/sub/blah.png'));
271
- const res = yield index_1.getAssetFromKV(event);
272
- if (res) {
273
- t.is(yield res.text(), 'picturedis');
274
- }
275
- else {
276
- t.fail('Response was undefined');
277
- }
278
- }));
279
- ava_1.default('getAssetFromKV no result throws an error', (t) => __awaiter(void 0, void 0, void 0, function* () {
280
- mocks_1.mockGlobal();
281
- const event = mocks_1.getEvent(new Request('https://blah.com/random'));
282
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event));
283
- t.is(error.status, 404);
284
- }));
285
- ava_1.default('getAssetFromKV TTls set to null should not cache on browser or edge', (t) => __awaiter(void 0, void 0, void 0, function* () {
286
- mocks_1.mockGlobal();
287
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
288
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { browserTTL: null, edgeTTL: null } });
289
- yield mocks_1.sleep(100);
290
- const res2 = yield index_1.getAssetFromKV(event, { cacheControl: { browserTTL: null, edgeTTL: null } });
291
- if (res1 && res2) {
292
- t.is(res1.headers.get('cf-cache-status'), null);
293
- t.is(res1.headers.get('cache-control'), null);
294
- t.is(res2.headers.get('cf-cache-status'), null);
295
- t.is(res2.headers.get('cache-control'), null);
296
- }
297
- else {
298
- t.fail('Response was undefined');
299
- }
300
- }));
301
- ava_1.default('getAssetFromKV passing in a custom NAMESPACE serves correct asset', (t) => __awaiter(void 0, void 0, void 0, function* () {
302
- mocks_1.mockGlobal();
303
- let CUSTOM_NAMESPACE = mocks_1.mockKV({
304
- 'key1.123HASHBROWN.txt': 'val1',
305
- });
306
- Object.assign(global, { CUSTOM_NAMESPACE });
307
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
308
- const res = yield index_1.getAssetFromKV(event);
309
- if (res) {
310
- t.is(yield res.text(), 'index.html');
311
- t.true(res.headers.get('content-type').includes('html'));
312
- }
313
- else {
314
- t.fail('Response was undefined');
315
- }
316
- }));
317
- ava_1.default('getAssetFromKV when custom namespace without the asset should fail', (t) => __awaiter(void 0, void 0, void 0, function* () {
318
- mocks_1.mockGlobal();
319
- let CUSTOM_NAMESPACE = mocks_1.mockKV({
320
- 'key5.123HASHBROWN.txt': 'customvalu',
321
- });
322
- const event = mocks_1.getEvent(new Request('https://blah.com'));
323
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event, { ASSET_NAMESPACE: CUSTOM_NAMESPACE }));
324
- t.is(error.status, 404);
325
- }));
326
- ava_1.default('getAssetFromKV when namespace not bound fails', (t) => __awaiter(void 0, void 0, void 0, function* () {
327
- mocks_1.mockGlobal();
328
- var MY_CUSTOM_NAMESPACE = undefined;
329
- Object.assign(global, { MY_CUSTOM_NAMESPACE });
330
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
331
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event, { ASSET_NAMESPACE: MY_CUSTOM_NAMESPACE }));
332
- t.is(error.status, 500);
333
- }));
334
- ava_1.default('getAssetFromKV when if-none-match === active resource version, should revalidate', (t) => __awaiter(void 0, void 0, void 0, function* () {
335
- mocks_1.mockGlobal();
336
- const resourceKey = 'key1.png';
337
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
338
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
339
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, {
340
- headers: {
341
- 'if-none-match': `W/"${resourceVersion}"`,
342
- },
343
- }));
344
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } });
345
- yield mocks_1.sleep(100);
346
- const res2 = yield index_1.getAssetFromKV(event2);
347
- if (res1 && res2) {
348
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
349
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
350
- }
351
- else {
352
- t.fail('Response was undefined');
353
- }
354
- }));
355
- ava_1.default('getAssetFromKV when if-none-match equals etag of stale resource then should bypass cache', (t) => __awaiter(void 0, void 0, void 0, function* () {
356
- mocks_1.mockGlobal();
357
- const resourceKey = 'key1.png';
358
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
359
- const req1 = new Request(`https://blah.com/${resourceKey}`, {
360
- headers: {
361
- 'if-none-match': `"${resourceVersion}"`,
362
- },
363
- });
364
- const req2 = new Request(`https://blah.com/${resourceKey}`, {
365
- headers: {
366
- 'if-none-match': `"${resourceVersion}-another-version"`,
367
- },
368
- });
369
- const event = mocks_1.getEvent(req1);
370
- const event2 = mocks_1.getEvent(req2);
371
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } });
372
- const res2 = yield index_1.getAssetFromKV(event);
373
- const res3 = yield index_1.getAssetFromKV(event2);
374
- if (res1 && res2 && res3) {
375
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
376
- t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`);
377
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
378
- t.not(res3.headers.get('etag'), req2.headers.get('if-none-match'));
379
- t.is(res3.headers.get('cf-cache-status'), 'MISS');
380
- }
381
- else {
382
- t.fail('Response was undefined');
383
- }
384
- }));
385
- ava_1.default('getAssetFromKV when resource in cache, etag should be weakened before returned to eyeball', (t) => __awaiter(void 0, void 0, void 0, function* () {
386
- mocks_1.mockGlobal();
387
- const resourceKey = 'key1.png';
388
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
389
- const req1 = new Request(`https://blah.com/${resourceKey}`, {
390
- headers: {
391
- 'if-none-match': `"${resourceVersion}"`,
392
- },
393
- });
394
- const event = mocks_1.getEvent(req1);
395
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } });
396
- const res2 = yield index_1.getAssetFromKV(event);
397
- if (res1 && res2) {
398
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
399
- t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`);
400
- }
401
- else {
402
- t.fail('Response was undefined');
403
- }
404
- }));
405
- ava_1.default('getAssetFromKV if-none-match not sent but resource in cache, should return cache hit 200 OK', (t) => __awaiter(void 0, void 0, void 0, function* () {
406
- const resourceKey = 'cache.html';
407
- const event = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
408
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } });
409
- yield mocks_1.sleep(1);
410
- const res2 = yield index_1.getAssetFromKV(event);
411
- if (res1 && res2) {
412
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
413
- t.is(res1.headers.get('cache-control'), null);
414
- t.is(res2.status, 200);
415
- t.is(res2.headers.get('cf-cache-status'), 'HIT');
416
- }
417
- else {
418
- t.fail('Response was undefined');
419
- }
420
- }));
421
- ava_1.default('getAssetFromKV if range request submitted and resource in cache, request fulfilled', (t) => __awaiter(void 0, void 0, void 0, function* () {
422
- const resourceKey = 'cache.html';
423
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
424
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, { headers: { range: 'bytes=0-10' } }));
425
- const res1 = index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } });
426
- yield res1;
427
- yield mocks_1.sleep(2);
428
- const res2 = yield index_1.getAssetFromKV(event2);
429
- if (res2.headers.has('content-range')) {
430
- t.is(res2.status, 206);
431
- }
432
- else {
433
- t.fail('Response was undefined');
434
- }
435
- }));
436
- ava_1.default.todo('getAssetFromKV when body not empty, should invoke .cancel()');
@@ -1,40 +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
- ava_1.default('mapRequestToAsset() correctly changes /about -> /about/index.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
16
- mocks_1.mockGlobal();
17
- let path = '/about';
18
- let request = new Request(`https://foo.com${path}`);
19
- let newRequest = index_1.mapRequestToAsset(request);
20
- t.is(newRequest.url, request.url + '/index.html');
21
- }));
22
- ava_1.default('mapRequestToAsset() correctly changes /about/ -> /about/index.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
23
- let path = '/about/';
24
- let request = new Request(`https://foo.com${path}`);
25
- let newRequest = index_1.mapRequestToAsset(request);
26
- t.is(newRequest.url, request.url + 'index.html');
27
- }));
28
- ava_1.default('mapRequestToAsset() correctly changes /about.me/ -> /about.me/index.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
29
- let path = '/about.me/';
30
- let request = new Request(`https://foo.com${path}`);
31
- let newRequest = index_1.mapRequestToAsset(request);
32
- t.is(newRequest.url, request.url + 'index.html');
33
- }));
34
- ava_1.default('mapRequestToAsset() correctly changes /about -> /about/default.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
35
- mocks_1.mockGlobal();
36
- let path = '/about';
37
- let request = new Request(`https://foo.com${path}`);
38
- let newRequest = index_1.mapRequestToAsset(request, { defaultDocument: 'default.html' });
39
- t.is(newRequest.url, request.url + '/default.html');
40
- }));