wrangler 0.0.0-e64812e → 0.0.0-e665ccc3

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 (373) hide show
  1. package/README.md +24 -4
  2. package/bin/wrangler.js +170 -62
  3. package/kv-asset-handler.js +1 -0
  4. package/miniflare-dist/index.mjs +1643 -83
  5. package/package.json +198 -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.ts +121 -0
  9. package/src/__tests__/api.test.ts +102 -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 +4506 -3363
  13. package/src/__tests__/constellation.test.ts +371 -0
  14. package/src/__tests__/d1/d1.test.ts +82 -0
  15. package/src/__tests__/d1/execute.test.ts +66 -0
  16. package/src/__tests__/d1/migrate.test.ts +257 -0
  17. package/src/__tests__/d1/splitter.test.ts +255 -0
  18. package/src/__tests__/delete.test.ts +272 -0
  19. package/src/__tests__/deployments.test.ts +369 -0
  20. package/src/__tests__/dev.test.tsx +1589 -952
  21. package/src/__tests__/generate.test.ts +237 -0
  22. package/src/__tests__/get-host-from-url.test.ts +16 -0
  23. package/src/__tests__/guess-worker-format.test.ts +103 -68
  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 +64 -39
  30. package/src/__tests__/helpers/mock-dialogs.ts +88 -79
  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 +30 -30
  34. package/src/__tests__/helpers/mock-istty.ts +65 -18
  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 +241 -233
  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 +160 -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 +52 -0
  52. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  53. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  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 +11 -0
  60. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  61. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  62. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  63. package/src/__tests__/https-options.test.ts +142 -109
  64. package/src/__tests__/index.test.ts +262 -236
  65. package/src/__tests__/init.test.ts +3167 -1605
  66. package/src/__tests__/jest.setup.ts +152 -50
  67. package/src/__tests__/kv.test.ts +1789 -1471
  68. package/src/__tests__/logger.test.ts +167 -75
  69. package/src/__tests__/logout.test.ts +47 -0
  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 +589 -0
  74. package/src/__tests__/package-manager.test.ts +303 -303
  75. package/src/__tests__/pages/deployment-list.test.ts +80 -0
  76. package/src/__tests__/pages/functions-build.test.ts +528 -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 +110 -0
  80. package/src/__tests__/pages/project-upload.test.ts +500 -0
  81. package/src/__tests__/pages/publish.test.ts +2864 -0
  82. package/src/__tests__/pages-deployment-tail.test.ts +957 -0
  83. package/src/__tests__/parse.test.ts +358 -252
  84. package/src/__tests__/paths.test.ts +39 -0
  85. package/src/__tests__/publish.test.ts +8736 -5355
  86. package/src/__tests__/pubsub.test.ts +496 -0
  87. package/src/__tests__/queues.test.ts +532 -0
  88. package/src/__tests__/r2.test.ts +369 -228
  89. package/src/__tests__/route.test.ts +18 -18
  90. package/src/__tests__/secret.test.ts +681 -469
  91. package/src/__tests__/tail.test.ts +866 -567
  92. package/src/__tests__/test-old-node-version.js +31 -0
  93. package/src/__tests__/traverse-module-graph.test.ts +220 -0
  94. package/src/__tests__/tsconfig-sanity.ts +12 -0
  95. package/src/__tests__/tsconfig.json +8 -0
  96. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  97. package/src/__tests__/type-generation.test.ts +234 -0
  98. package/src/__tests__/user.test.ts +107 -120
  99. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  100. package/src/__tests__/validate-dev-props.test.ts +56 -0
  101. package/src/__tests__/version.test.ts +35 -0
  102. package/src/__tests__/whoami.test.tsx +165 -98
  103. package/src/__tests__/worker-namespace.test.ts +340 -0
  104. package/src/abort.d.ts +1 -1
  105. package/src/api/dev.ts +321 -0
  106. package/src/api/index.ts +11 -0
  107. package/src/api/mtls-certificate.ts +148 -0
  108. package/src/api/pages/create-worker-bundle-contents.ts +77 -0
  109. package/src/api/pages/index.ts +5 -0
  110. package/src/api/pages/publish.tsx +371 -0
  111. package/src/bundle-reporter.ts +68 -0
  112. package/src/bundle.ts +890 -161
  113. package/src/cfetch/index.ts +120 -83
  114. package/src/cfetch/internal.ts +210 -97
  115. package/src/cli.ts +23 -8
  116. package/src/config/config.ts +244 -196
  117. package/src/config/diagnostics.ts +61 -61
  118. package/src/config/environment.ts +562 -357
  119. package/src/config/index.ts +374 -196
  120. package/src/config/validation-helpers.ts +379 -357
  121. package/src/config/validation.ts +2197 -1367
  122. package/src/config-cache.ts +81 -41
  123. package/src/constellation/createProject.tsx +51 -0
  124. package/src/constellation/deleteProject.ts +51 -0
  125. package/src/constellation/deleteProjectModel.ts +68 -0
  126. package/src/constellation/index.ts +75 -0
  127. package/src/constellation/listCatalog.tsx +35 -0
  128. package/src/constellation/listModel.tsx +41 -0
  129. package/src/constellation/listProject.tsx +28 -0
  130. package/src/constellation/listRuntime.tsx +28 -0
  131. package/src/constellation/options.ts +17 -0
  132. package/src/constellation/types.ts +17 -0
  133. package/src/constellation/uploadModel.tsx +64 -0
  134. package/src/constellation/utils.ts +90 -0
  135. package/src/create-worker-preview.ts +250 -142
  136. package/src/create-worker-upload-form.ts +347 -238
  137. package/src/d1/backups.tsx +219 -0
  138. package/src/d1/constants.ts +2 -0
  139. package/src/d1/create.tsx +70 -0
  140. package/src/d1/delete.ts +53 -0
  141. package/src/d1/execute.tsx +357 -0
  142. package/src/d1/formatTimeAgo.ts +14 -0
  143. package/src/d1/index.ts +100 -0
  144. package/src/d1/list.tsx +62 -0
  145. package/src/d1/migrations/apply.tsx +212 -0
  146. package/src/d1/migrations/create.tsx +79 -0
  147. package/src/d1/migrations/helpers.ts +169 -0
  148. package/src/d1/migrations/index.ts +3 -0
  149. package/src/d1/migrations/list.tsx +95 -0
  150. package/src/d1/migrations/options.ts +23 -0
  151. package/src/d1/options.ts +22 -0
  152. package/src/d1/splitter.ts +161 -0
  153. package/src/d1/types.ts +25 -0
  154. package/src/d1/utils.ts +49 -0
  155. package/src/delete.ts +100 -0
  156. package/src/deployments.ts +368 -0
  157. package/src/deprecated/index.ts +144 -0
  158. package/src/dev/dev-vars.ts +20 -17
  159. package/src/dev/dev.tsx +538 -352
  160. package/src/dev/get-local-persistence-path.ts +31 -0
  161. package/src/dev/local.tsx +932 -298
  162. package/src/dev/remote.tsx +623 -215
  163. package/src/dev/start-server.ts +545 -0
  164. package/src/dev/use-esbuild.ts +194 -97
  165. package/src/dev/validate-dev-props.ts +40 -0
  166. package/src/dev-registry.ts +202 -0
  167. package/src/dev.tsx +934 -0
  168. package/src/dialogs.ts +136 -0
  169. package/src/dispatch-namespace.ts +211 -0
  170. package/src/docs/helpers.ts +50 -0
  171. package/src/docs/index.ts +54 -0
  172. package/src/durable.ts +87 -87
  173. package/src/entry.ts +253 -231
  174. package/src/environment-variables/factory.ts +89 -0
  175. package/src/environment-variables/misc-variables.ts +30 -0
  176. package/src/errors.ts +6 -6
  177. package/src/generate/index.ts +298 -0
  178. package/src/git-client.ts +135 -0
  179. package/src/global-wrangler-config-path.ts +26 -0
  180. package/src/https-options.ts +84 -80
  181. package/src/index.ts +768 -0
  182. package/src/init.ts +1037 -0
  183. package/src/inspect.ts +816 -595
  184. package/src/intl-polyfill.d.ts +123 -123
  185. package/src/is-ci.ts +14 -0
  186. package/src/is-interactive.ts +16 -0
  187. package/src/jest.d.ts +4 -0
  188. package/src/kv/helpers.ts +433 -0
  189. package/src/kv/index.ts +594 -0
  190. package/src/logger.ts +100 -43
  191. package/src/metrics/index.ts +5 -0
  192. package/src/metrics/metrics-config.ts +239 -0
  193. package/src/metrics/metrics-dispatcher.ts +96 -0
  194. package/src/metrics/metrics-usage-headers.ts +24 -0
  195. package/src/metrics/send-event.ts +99 -0
  196. package/src/miniflare-cli/assets.ts +255 -0
  197. package/src/miniflare-cli/index.ts +202 -33
  198. package/src/miniflare-cli/request-context.ts +18 -18
  199. package/src/miniflare-cli/tsconfig.json +9 -0
  200. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  201. package/src/miniflare-cli/types.ts +11 -0
  202. package/src/module-collection.ts +290 -215
  203. package/src/mtls-certificate/cli.ts +155 -0
  204. package/src/open-in-browser.ts +4 -6
  205. package/src/package-manager.ts +173 -128
  206. package/src/pages/build.ts +423 -0
  207. package/src/pages/buildFunctions.ts +140 -0
  208. package/src/pages/constants.ts +18 -0
  209. package/src/pages/deployment-tails.ts +281 -0
  210. package/src/pages/deployments.tsx +84 -0
  211. package/src/pages/dev.ts +734 -0
  212. package/src/pages/errors.ts +67 -0
  213. package/src/pages/functions/buildPlugin.ts +114 -0
  214. package/src/pages/functions/buildWorker.ts +350 -0
  215. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  216. package/src/pages/functions/filepath-routing.ts +189 -0
  217. package/src/pages/functions/identifiers.ts +78 -0
  218. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  219. package/src/pages/functions/routes-consolidation.ts +73 -0
  220. package/src/pages/functions/routes-transformation.test.ts +282 -0
  221. package/src/pages/functions/routes-transformation.ts +115 -0
  222. package/src/pages/functions/routes-validation.test.ts +403 -0
  223. package/src/pages/functions/routes-validation.ts +202 -0
  224. package/src/pages/functions/routes.ts +151 -0
  225. package/src/pages/functions/tsconfig.json +8 -0
  226. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  227. package/src/pages/functions.ts +86 -0
  228. package/src/pages/hash.ts +13 -0
  229. package/src/pages/index.ts +102 -0
  230. package/src/pages/projects.tsx +159 -0
  231. package/src/pages/prompt-select-project.tsx +31 -0
  232. package/src/pages/publish.tsx +267 -0
  233. package/src/pages/types.ts +46 -0
  234. package/src/pages/upload.tsx +469 -0
  235. package/src/pages/utils.ts +23 -0
  236. package/src/parse.ts +232 -126
  237. package/src/paths.ts +53 -8
  238. package/src/proxy.ts +623 -394
  239. package/src/publish/index.ts +274 -0
  240. package/src/publish/publish.ts +1065 -0
  241. package/src/pubsub/index.ts +286 -0
  242. package/src/pubsub/pubsub-commands.ts +623 -0
  243. package/src/queues/cli/commands/consumer/add.ts +71 -0
  244. package/src/queues/cli/commands/consumer/index.ts +19 -0
  245. package/src/queues/cli/commands/consumer/remove.ts +31 -0
  246. package/src/queues/cli/commands/create.ts +25 -0
  247. package/src/queues/cli/commands/delete.ts +26 -0
  248. package/src/queues/cli/commands/index.ts +35 -0
  249. package/src/queues/cli/commands/list.ts +25 -0
  250. package/src/queues/client.ts +136 -0
  251. package/src/queues/utils.ts +18 -0
  252. package/src/r2/constants.ts +4 -0
  253. package/src/r2/helpers.ts +132 -0
  254. package/src/r2/index.ts +289 -0
  255. package/src/routes.ts +140 -0
  256. package/src/secret/index.ts +377 -0
  257. package/src/selfsigned.d.ts +23 -23
  258. package/src/sites.ts +484 -0
  259. package/src/tail/createTail.ts +415 -0
  260. package/src/tail/filters.ts +109 -111
  261. package/src/tail/index.ts +208 -296
  262. package/src/tail/printing.ts +116 -51
  263. package/src/traverse-module-graph.ts +54 -0
  264. package/src/tsconfig-sanity.ts +16 -0
  265. package/src/type-generation.ts +181 -0
  266. package/src/update-check.ts +11 -11
  267. package/src/user/access.ts +68 -0
  268. package/src/user/auth-variables.ts +113 -0
  269. package/src/user/choose-account.tsx +39 -0
  270. package/src/user/generate-auth-url.ts +33 -0
  271. package/src/user/generate-random-state.ts +16 -0
  272. package/src/user/index.ts +2 -0
  273. package/src/user/user.ts +1234 -0
  274. package/src/utils/collectKeyValues.ts +14 -0
  275. package/src/utils/render.ts +93 -0
  276. package/src/whoami.ts +135 -0
  277. package/src/worker.ts +205 -106
  278. package/src/yargs-types.ts +37 -0
  279. package/src/zones.ts +191 -0
  280. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  281. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  282. package/templates/__tests__/tsconfig.json +8 -0
  283. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  284. package/templates/checked-fetch.js +23 -0
  285. package/templates/d1-beta-facade.js +244 -0
  286. package/templates/facade.d.ts +14 -0
  287. package/templates/first-party-worker-module-facade.ts +19 -0
  288. package/templates/format-dev-errors.ts +34 -0
  289. package/templates/gitignore +1 -0
  290. package/templates/init-tests/test-jest-new-worker.js +23 -0
  291. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  292. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  293. package/templates/middleware/common.ts +62 -0
  294. package/templates/middleware/loader-modules.ts +84 -0
  295. package/templates/middleware/loader-sw.ts +233 -0
  296. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  297. package/templates/middleware/middleware-pretty-error.ts +40 -0
  298. package/templates/middleware/middleware-scheduled.ts +15 -0
  299. package/templates/new-worker-scheduled.js +3 -3
  300. package/templates/new-worker-scheduled.ts +15 -15
  301. package/templates/new-worker.js +3 -3
  302. package/templates/new-worker.ts +18 -15
  303. package/templates/no-op-worker.js +10 -0
  304. package/templates/pages-dev-pipeline.ts +31 -0
  305. package/templates/pages-dev-util.ts +52 -0
  306. package/templates/pages-shim.ts +12 -0
  307. package/templates/pages-template-plugin.ts +180 -0
  308. package/templates/pages-template-worker.ts +189 -0
  309. package/templates/serve-static-assets.ts +66 -0
  310. package/templates/service-bindings-module-facade.js +55 -0
  311. package/templates/service-bindings-sw-facade.js +42 -0
  312. package/templates/tsconfig-sanity.ts +11 -0
  313. package/templates/tsconfig.init.json +105 -0
  314. package/templates/tsconfig.json +6 -103
  315. package/templates/tsconfig.tsbuildinfo +1 -0
  316. package/wrangler-dist/cli.d.ts +1652 -0
  317. package/wrangler-dist/cli.js +137978 -110578
  318. package/wrangler-dist/wasm-sync.wasm +0 -0
  319. package/pages/functions/buildPlugin.ts +0 -105
  320. package/pages/functions/buildWorker.ts +0 -151
  321. package/pages/functions/filepath-routing.ts +0 -189
  322. package/pages/functions/identifiers.ts +0 -78
  323. package/pages/functions/routes.ts +0 -156
  324. package/pages/functions/template-plugin.ts +0 -147
  325. package/pages/functions/template-worker.ts +0 -143
  326. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  327. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  328. package/src/__tests__/pages.test.ts +0 -754
  329. package/src/dialogs.tsx +0 -135
  330. package/src/environment-variables.ts +0 -35
  331. package/src/index.tsx +0 -2864
  332. package/src/kv.ts +0 -408
  333. package/src/miniflare-cli/enum-keys.ts +0 -17
  334. package/src/pages.tsx +0 -2038
  335. package/src/publish.ts +0 -583
  336. package/src/r2.ts +0 -50
  337. package/src/sites.tsx +0 -295
  338. package/src/user.tsx +0 -1203
  339. package/src/whoami.tsx +0 -70
  340. package/templates/static-asset-facade.js +0 -47
  341. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  342. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  343. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  344. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  345. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  346. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  347. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  348. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  349. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  350. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  351. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  352. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  353. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  354. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  355. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  356. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  357. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  358. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  359. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  360. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  361. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  362. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  363. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  364. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  365. package/vendor/wrangler-mime/LICENSE +0 -21
  366. package/vendor/wrangler-mime/Mime.js +0 -97
  367. package/vendor/wrangler-mime/README.md +0 -187
  368. package/vendor/wrangler-mime/cli.js +0 -46
  369. package/vendor/wrangler-mime/index.js +0 -4
  370. package/vendor/wrangler-mime/lite.js +0 -4
  371. package/vendor/wrangler-mime/package.json +0 -52
  372. package/vendor/wrangler-mime/types/other.js +0 -1
  373. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -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
- }));