wrangler 0.0.0-e64812e → 0.0.0-e6c9c5c0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (338) hide show
  1. package/README.md +23 -3
  2. package/bin/wrangler.js +170 -62
  3. package/kv-asset-handler.js +1 -0
  4. package/miniflare-dist/index.mjs +1499 -21
  5. package/package.json +193 -152
  6. package/src/__tests__/access.test.ts +25 -0
  7. package/src/__tests__/api-dev.test.ts +238 -0
  8. package/src/__tests__/api-devregistry.test.js +64 -0
  9. package/src/__tests__/api.test.ts +80 -0
  10. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  11. package/src/__tests__/config-cache.test.ts +30 -24
  12. package/src/__tests__/configuration.test.ts +4152 -3363
  13. package/src/__tests__/d1/d1.test.ts +127 -0
  14. package/src/__tests__/d1/splitter.test.ts +255 -0
  15. package/src/__tests__/delete.test.ts +266 -0
  16. package/src/__tests__/deployments.test.ts +105 -0
  17. package/src/__tests__/dev.test.tsx +1558 -952
  18. package/src/__tests__/generate.test.ts +238 -0
  19. package/src/__tests__/get-host-from-url.test.ts +16 -0
  20. package/src/__tests__/guess-worker-format.test.ts +68 -68
  21. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  22. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  23. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  24. package/src/__tests__/helpers/mock-bin.ts +20 -20
  25. package/src/__tests__/helpers/mock-console.ts +58 -39
  26. package/src/__tests__/helpers/mock-dialogs.ts +88 -79
  27. package/src/__tests__/helpers/mock-get-zone-from-host.ts +11 -0
  28. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  29. package/src/__tests__/helpers/mock-istty.ts +65 -18
  30. package/src/__tests__/helpers/mock-known-routes.ts +12 -0
  31. package/src/__tests__/helpers/mock-kv.ts +41 -28
  32. package/src/__tests__/helpers/mock-oauth-flow.ts +241 -233
  33. package/src/__tests__/helpers/mock-process.ts +34 -0
  34. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  35. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  36. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  37. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  38. package/src/__tests__/helpers/msw/handlers/deployments.ts +69 -0
  39. package/src/__tests__/helpers/msw/handlers/namespaces.ts +81 -0
  40. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  41. package/src/__tests__/helpers/msw/handlers/r2.ts +60 -0
  42. package/src/__tests__/helpers/msw/handlers/script.ts +56 -0
  43. package/src/__tests__/helpers/msw/handlers/user.ts +52 -0
  44. package/src/__tests__/helpers/msw/handlers/zones.ts +20 -0
  45. package/src/__tests__/helpers/msw/index.ts +50 -0
  46. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  47. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  48. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  49. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  51. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  53. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  54. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  55. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  56. package/src/__tests__/https-options.test.ts +142 -109
  57. package/src/__tests__/index.test.ts +245 -233
  58. package/src/__tests__/init.test.ts +3135 -1605
  59. package/src/__tests__/jest.setup.ts +148 -50
  60. package/src/__tests__/kv.test.ts +1778 -1471
  61. package/src/__tests__/logger.test.ts +100 -74
  62. package/src/__tests__/logout.test.ts +47 -0
  63. package/src/__tests__/metrics.test.ts +493 -0
  64. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  65. package/src/__tests__/middleware.test.ts +750 -0
  66. package/src/__tests__/package-manager.test.ts +303 -303
  67. package/src/__tests__/pages-deployment-tail.test.ts +955 -0
  68. package/src/__tests__/pages.test.ts +2887 -743
  69. package/src/__tests__/parse.test.ts +252 -252
  70. package/src/__tests__/paths.test.ts +39 -0
  71. package/src/__tests__/publish.test.ts +8004 -5355
  72. package/src/__tests__/pubsub.test.ts +490 -0
  73. package/src/__tests__/queues.test.ts +522 -0
  74. package/src/__tests__/r2.test.ts +364 -228
  75. package/src/__tests__/route.test.ts +18 -18
  76. package/src/__tests__/secret.test.ts +681 -469
  77. package/src/__tests__/tail.test.ts +835 -567
  78. package/src/__tests__/test-old-node-version.js +31 -0
  79. package/src/__tests__/tsconfig-sanity.ts +12 -0
  80. package/src/__tests__/tsconfig.json +8 -0
  81. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  82. package/src/__tests__/type-generation.test.ts +233 -0
  83. package/src/__tests__/user.test.ts +107 -120
  84. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  85. package/src/__tests__/validate-dev-props.test.ts +56 -0
  86. package/src/__tests__/version.test.ts +35 -0
  87. package/src/__tests__/whoami.test.tsx +165 -98
  88. package/src/__tests__/worker-namespace.test.ts +334 -0
  89. package/src/abort.d.ts +1 -1
  90. package/src/api/dev.ts +259 -0
  91. package/src/api/index.ts +3 -0
  92. package/src/api/pages/index.ts +5 -0
  93. package/src/api/pages/publish.tsx +321 -0
  94. package/src/bundle-reporter.ts +68 -0
  95. package/src/bundle.ts +838 -163
  96. package/src/cfetch/index.ts +120 -83
  97. package/src/cfetch/internal.ts +210 -97
  98. package/src/cli.ts +23 -8
  99. package/src/config/config.ts +236 -195
  100. package/src/config/diagnostics.ts +61 -61
  101. package/src/config/environment.ts +516 -357
  102. package/src/config/index.ts +326 -193
  103. package/src/config/validation-helpers.ts +379 -357
  104. package/src/config/validation.ts +1979 -1367
  105. package/src/config-cache.ts +81 -41
  106. package/src/create-worker-preview.ts +239 -142
  107. package/src/create-worker-upload-form.ts +309 -238
  108. package/src/d1/backups.tsx +219 -0
  109. package/src/d1/constants.ts +2 -0
  110. package/src/d1/create.tsx +63 -0
  111. package/src/d1/delete.ts +56 -0
  112. package/src/d1/execute.tsx +323 -0
  113. package/src/d1/formatTimeAgo.ts +14 -0
  114. package/src/d1/index.ts +101 -0
  115. package/src/d1/list.tsx +48 -0
  116. package/src/d1/migrations/apply.tsx +197 -0
  117. package/src/d1/migrations/create.tsx +77 -0
  118. package/src/d1/migrations/helpers.ts +147 -0
  119. package/src/d1/migrations/index.ts +3 -0
  120. package/src/d1/migrations/list.tsx +79 -0
  121. package/src/d1/migrations/options.ts +18 -0
  122. package/src/d1/options.ts +22 -0
  123. package/src/d1/splitter.ts +161 -0
  124. package/src/d1/types.ts +24 -0
  125. package/src/d1/utils.ts +48 -0
  126. package/src/delete.ts +103 -0
  127. package/src/deployments.ts +93 -0
  128. package/src/deprecated/index.ts +145 -0
  129. package/src/dev/dev-vars.ts +20 -17
  130. package/src/dev/dev.tsx +547 -352
  131. package/src/dev/get-local-persistence-path.ts +31 -0
  132. package/src/dev/local.tsx +903 -298
  133. package/src/dev/remote.tsx +620 -215
  134. package/src/dev/start-server.ts +538 -0
  135. package/src/dev/use-esbuild.ts +184 -97
  136. package/src/dev/validate-dev-props.ts +31 -0
  137. package/src/dev-registry.ts +201 -0
  138. package/src/dev.tsx +949 -0
  139. package/src/dialogs.ts +136 -0
  140. package/src/dispatch-namespace.ts +207 -0
  141. package/src/docs/index.ts +98 -0
  142. package/src/durable.ts +87 -87
  143. package/src/entry.ts +236 -230
  144. package/src/environment-variables/factory.ts +88 -0
  145. package/src/environment-variables/misc-variables.ts +30 -0
  146. package/src/errors.ts +6 -6
  147. package/src/generate/index.ts +300 -0
  148. package/src/git-client.ts +135 -0
  149. package/src/global-wrangler-config-path.ts +26 -0
  150. package/src/https-options.ts +84 -80
  151. package/src/index.ts +694 -0
  152. package/src/init.ts +1024 -0
  153. package/src/inspect.ts +698 -593
  154. package/src/intl-polyfill.d.ts +123 -123
  155. package/src/is-ci.ts +14 -0
  156. package/src/is-interactive.ts +16 -0
  157. package/src/jest.d.ts +4 -0
  158. package/src/kv/helpers.ts +430 -0
  159. package/src/kv/index.ts +596 -0
  160. package/src/logger.ts +80 -42
  161. package/src/metrics/index.ts +5 -0
  162. package/src/metrics/metrics-config.ts +239 -0
  163. package/src/metrics/metrics-dispatcher.ts +96 -0
  164. package/src/metrics/metrics-usage-headers.ts +24 -0
  165. package/src/metrics/send-event.ts +98 -0
  166. package/src/miniflare-cli/assets.ts +250 -0
  167. package/src/miniflare-cli/index.ts +202 -33
  168. package/src/miniflare-cli/request-context.ts +18 -18
  169. package/src/miniflare-cli/tsconfig.json +9 -0
  170. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  171. package/src/miniflare-cli/types.ts +11 -0
  172. package/src/module-collection.ts +221 -212
  173. package/src/open-in-browser.ts +4 -6
  174. package/src/package-manager.ts +173 -128
  175. package/src/pages/build.ts +150 -0
  176. package/src/pages/buildFunctions.ts +129 -0
  177. package/src/pages/constants.ts +18 -0
  178. package/src/pages/deployment-tails.ts +284 -0
  179. package/src/pages/deployments.tsx +84 -0
  180. package/src/pages/dev.ts +716 -0
  181. package/src/pages/errors.ts +66 -0
  182. package/src/pages/functions/buildPlugin.ts +90 -0
  183. package/src/pages/functions/buildWorker.ts +288 -0
  184. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  185. package/src/pages/functions/filepath-routing.ts +189 -0
  186. package/src/pages/functions/identifiers.ts +78 -0
  187. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  188. package/src/pages/functions/routes-consolidation.ts +73 -0
  189. package/src/pages/functions/routes-transformation.test.ts +282 -0
  190. package/src/pages/functions/routes-transformation.ts +115 -0
  191. package/src/pages/functions/routes-validation.test.ts +403 -0
  192. package/src/pages/functions/routes-validation.ts +202 -0
  193. package/src/pages/functions/routes.ts +151 -0
  194. package/src/pages/functions/tsconfig.json +8 -0
  195. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  196. package/src/pages/functions.ts +82 -0
  197. package/src/pages/hash.ts +13 -0
  198. package/src/pages/index.ts +102 -0
  199. package/src/pages/projects.tsx +167 -0
  200. package/src/pages/prompt-select-project.tsx +31 -0
  201. package/src/pages/publish.tsx +263 -0
  202. package/src/pages/types.ts +41 -0
  203. package/src/pages/upload.tsx +447 -0
  204. package/src/pages/utils.ts +23 -0
  205. package/src/parse.ts +209 -125
  206. package/src/paths.ts +53 -8
  207. package/src/proxy.ts +623 -394
  208. package/src/publish/index.ts +270 -0
  209. package/src/publish/publish.ts +1064 -0
  210. package/src/pubsub/index.ts +286 -0
  211. package/src/pubsub/pubsub-commands.ts +624 -0
  212. package/src/queues/cli/commands/consumer/add.ts +71 -0
  213. package/src/queues/cli/commands/consumer/index.ts +22 -0
  214. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  215. package/src/queues/cli/commands/create.ts +25 -0
  216. package/src/queues/cli/commands/delete.ts +26 -0
  217. package/src/queues/cli/commands/index.ts +36 -0
  218. package/src/queues/cli/commands/list.ts +25 -0
  219. package/src/queues/client.ts +135 -0
  220. package/src/queues/utils.ts +18 -0
  221. package/src/r2/constants.ts +4 -0
  222. package/src/r2/helpers.ts +131 -0
  223. package/src/r2/index.ts +289 -0
  224. package/src/routes.ts +140 -0
  225. package/src/secret/index.ts +379 -0
  226. package/src/selfsigned.d.ts +23 -23
  227. package/src/sites.ts +372 -0
  228. package/src/tail/createTail.ts +406 -0
  229. package/src/tail/filters.ts +108 -110
  230. package/src/tail/index.ts +210 -296
  231. package/src/tail/printing.ts +101 -46
  232. package/src/tsconfig-sanity.ts +16 -0
  233. package/src/type-generation.ts +181 -0
  234. package/src/update-check.ts +11 -11
  235. package/src/user/access.ts +68 -0
  236. package/src/user/auth-variables.ts +113 -0
  237. package/src/user/choose-account.tsx +39 -0
  238. package/src/user/generate-auth-url.ts +33 -0
  239. package/src/user/generate-random-state.ts +16 -0
  240. package/src/user/index.ts +2 -0
  241. package/src/user/user.ts +1232 -0
  242. package/src/utils/collectKeyValues.ts +14 -0
  243. package/src/whoami.ts +135 -0
  244. package/src/worker.ts +177 -106
  245. package/src/yargs-types.ts +24 -0
  246. package/src/zones.ts +191 -0
  247. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  248. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  249. package/templates/__tests__/tsconfig.json +8 -0
  250. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  251. package/templates/checked-fetch.js +15 -0
  252. package/templates/d1-beta-facade.js +241 -0
  253. package/templates/facade.d.ts +14 -0
  254. package/templates/first-party-worker-module-facade.ts +19 -0
  255. package/templates/format-dev-errors.ts +33 -0
  256. package/templates/init-tests/test-jest-new-worker.js +23 -0
  257. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  258. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  259. package/templates/middleware/common.ts +62 -0
  260. package/templates/middleware/loader-modules.ts +84 -0
  261. package/templates/middleware/loader-sw.ts +233 -0
  262. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  263. package/templates/middleware/middleware-pretty-error.ts +40 -0
  264. package/templates/middleware/middleware-scheduled.ts +15 -0
  265. package/templates/new-worker-scheduled.js +3 -3
  266. package/templates/new-worker-scheduled.ts +15 -15
  267. package/templates/new-worker.js +3 -3
  268. package/templates/new-worker.ts +15 -15
  269. package/templates/no-op-worker.js +10 -0
  270. package/templates/pages-dev-pipeline.ts +31 -0
  271. package/templates/pages-dev-util.ts +52 -0
  272. package/templates/pages-shim.ts +12 -0
  273. package/templates/pages-template-plugin.ts +176 -0
  274. package/templates/pages-template-worker.ts +189 -0
  275. package/templates/serve-static-assets.ts +65 -0
  276. package/templates/service-bindings-module-facade.js +54 -0
  277. package/templates/service-bindings-sw-facade.js +42 -0
  278. package/templates/tsconfig-sanity.ts +11 -0
  279. package/templates/tsconfig.init.json +106 -0
  280. package/templates/tsconfig.json +6 -103
  281. package/templates/tsconfig.tsbuildinfo +1 -0
  282. package/wrangler-dist/cli.d.ts +1028 -0
  283. package/wrangler-dist/cli.js +129259 -110922
  284. package/wrangler-dist/wasm-sync.wasm +0 -0
  285. package/pages/functions/buildPlugin.ts +0 -105
  286. package/pages/functions/buildWorker.ts +0 -151
  287. package/pages/functions/filepath-routing.ts +0 -189
  288. package/pages/functions/identifiers.ts +0 -78
  289. package/pages/functions/routes.ts +0 -156
  290. package/pages/functions/template-plugin.ts +0 -147
  291. package/pages/functions/template-worker.ts +0 -143
  292. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  293. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  294. package/src/dialogs.tsx +0 -135
  295. package/src/environment-variables.ts +0 -35
  296. package/src/index.tsx +0 -2864
  297. package/src/kv.ts +0 -408
  298. package/src/miniflare-cli/enum-keys.ts +0 -17
  299. package/src/pages.tsx +0 -2038
  300. package/src/publish.ts +0 -583
  301. package/src/r2.ts +0 -50
  302. package/src/sites.tsx +0 -295
  303. package/src/user.tsx +0 -1203
  304. package/src/whoami.tsx +0 -70
  305. package/templates/static-asset-facade.js +0 -47
  306. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  307. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  308. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  309. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  310. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  311. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  312. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  313. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  314. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  315. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  316. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  317. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  318. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  319. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  320. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  321. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  322. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  323. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  324. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  325. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  326. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  327. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  328. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  329. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  330. package/vendor/wrangler-mime/LICENSE +0 -21
  331. package/vendor/wrangler-mime/Mime.js +0 -97
  332. package/vendor/wrangler-mime/README.md +0 -187
  333. package/vendor/wrangler-mime/cli.js +0 -46
  334. package/vendor/wrangler-mime/index.js +0 -4
  335. package/vendor/wrangler-mime/lite.js +0 -4
  336. package/vendor/wrangler-mime/package.json +0 -52
  337. package/vendor/wrangler-mime/types/other.js +0 -1
  338. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,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
- }));