getAssetFromKV.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const ava_1 = require("ava");
  4. const mocks_1 = require("../mocks");
  5. (0, mocks_1.mockGlobalScope)();
  6. const index_1 = require("../index");
  7. (0, ava_1.default)('getAssetFromKV return correct val from KV and default caching', async (t) => {
  8. (0, mocks_1.mockRequestScope)();
  9. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/key1.txt'));
  10. const res = await (0, index_1.getAssetFromKV)(event);
  11. if (res) {
  12. t.is(res.headers.get('cache-control'), null);
  13. t.is(res.headers.get('cf-cache-status'), 'MISS');
  14. t.is(await res.text(), 'val1');
  15. t.true(res.headers.get('content-type').includes('text'));
  16. }
  17. else {
  18. t.fail('Response was undefined');
  19. }
  20. });
  21. (0, ava_1.default)('getAssetFromKV evaluated the file matching the extensionless path first /client/ -> client', async (t) => {
  22. (0, mocks_1.mockRequestScope)();
  23. const event = (0, mocks_1.getEvent)(new Request(`https://foo.com/client/`));
  24. const res = await (0, index_1.getAssetFromKV)(event);
  25. t.is(await res.text(), 'important file');
  26. t.true(res.headers.get('content-type').includes('text'));
  27. });
  28. (0, ava_1.default)('getAssetFromKV evaluated the file matching the extensionless path first /client -> client', async (t) => {
  29. (0, mocks_1.mockRequestScope)();
  30. const event = (0, mocks_1.getEvent)(new Request(`https://foo.com/client`));
  31. const res = await (0, index_1.getAssetFromKV)(event);
  32. t.is(await res.text(), 'important file');
  33. t.true(res.headers.get('content-type').includes('text'));
  34. });
  35. (0, ava_1.default)('getAssetFromKV if not in asset manifest still returns nohash.txt', async (t) => {
  36. (0, mocks_1.mockRequestScope)();
  37. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/nohash.txt'));
  38. const res = await (0, index_1.getAssetFromKV)(event);
  39. if (res) {
  40. t.is(await res.text(), 'no hash but still got some result');
  41. t.true(res.headers.get('content-type').includes('text'));
  42. }
  43. else {
  44. t.fail('Response was undefined');
  45. }
  46. });
  47. (0, ava_1.default)('getAssetFromKV if no asset manifest /client -> client fails', async (t) => {
  48. (0, mocks_1.mockRequestScope)();
  49. const event = (0, mocks_1.getEvent)(new Request(`https://foo.com/client`));
  50. const error = await t.throwsAsync((0, index_1.getAssetFromKV)(event, { ASSET_MANIFEST: {} }));
  51. t.is(error.status, 404);
  52. });
  53. (0, ava_1.default)('getAssetFromKV if sub/ -> sub/index.html served', async (t) => {
  54. (0, mocks_1.mockRequestScope)();
  55. const event = (0, mocks_1.getEvent)(new Request(`https://foo.com/sub`));
  56. const res = await (0, index_1.getAssetFromKV)(event);
  57. if (res) {
  58. t.is(await res.text(), 'picturedis');
  59. }
  60. else {
  61. t.fail('Response was undefined');
  62. }
  63. });
  64. (0, ava_1.default)('getAssetFromKV gets index.html by default for / requests', async (t) => {
  65. (0, mocks_1.mockRequestScope)();
  66. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/'));
  67. const res = await (0, index_1.getAssetFromKV)(event);
  68. if (res) {
  69. t.is(await res.text(), 'index.html');
  70. t.true(res.headers.get('content-type').includes('html'));
  71. }
  72. else {
  73. t.fail('Response was undefined');
  74. }
  75. });
  76. (0, ava_1.default)('getAssetFromKV non ASCII path support', async (t) => {
  77. (0, mocks_1.mockRequestScope)();
  78. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/测试.html'));
  79. const res = await (0, index_1.getAssetFromKV)(event);
  80. if (res) {
  81. t.is(await res.text(), 'My filename is non-ascii');
  82. }
  83. else {
  84. t.fail('Response was undefined');
  85. }
  86. });
  87. (0, ava_1.default)('getAssetFromKV supports browser percent encoded URLs', async (t) => {
  88. (0, mocks_1.mockRequestScope)();
  89. const event = (0, mocks_1.getEvent)(new Request('https://example.com/%not-really-percent-encoded.html'));
  90. const res = await (0, index_1.getAssetFromKV)(event);
  91. if (res) {
  92. t.is(await res.text(), 'browser percent encoded');
  93. }
  94. else {
  95. t.fail('Response was undefined');
  96. }
  97. });
  98. (0, ava_1.default)('getAssetFromKV supports user percent encoded URLs', async (t) => {
  99. (0, mocks_1.mockRequestScope)();
  100. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/%2F.html'));
  101. const res = await (0, index_1.getAssetFromKV)(event);
  102. if (res) {
  103. t.is(await res.text(), 'user percent encoded');
  104. }
  105. else {
  106. t.fail('Response was undefined');
  107. }
  108. });
  109. (0, ava_1.default)('getAssetFromKV only decode URL when necessary', async (t) => {
  110. (0, mocks_1.mockRequestScope)();
  111. const event1 = (0, mocks_1.getEvent)(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD.html'));
  112. const event2 = (0, mocks_1.getEvent)(new Request('https://blah.com/你好.html'));
  113. const res1 = await (0, index_1.getAssetFromKV)(event1);
  114. const res2 = await (0, index_1.getAssetFromKV)(event2);
  115. if (res1 && res2) {
  116. t.is(await res1.text(), 'Im important');
  117. t.is(await res2.text(), 'Im important');
  118. }
  119. else {
  120. t.fail('Response was undefined');
  121. }
  122. });
  123. (0, ava_1.default)('getAssetFromKV Support for user decode url path', async (t) => {
  124. (0, mocks_1.mockRequestScope)();
  125. const event1 = (0, mocks_1.getEvent)(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD/'));
  126. const event2 = (0, mocks_1.getEvent)(new Request('https://blah.com/你好/'));
  127. const res1 = await (0, index_1.getAssetFromKV)(event1);
  128. const res2 = await (0, index_1.getAssetFromKV)(event2);
  129. if (res1 && res2) {
  130. t.is(await res1.text(), 'My path is non-ascii');
  131. t.is(await res2.text(), 'My path is non-ascii');
  132. }
  133. else {
  134. t.fail('Response was undefined');
  135. }
  136. });
  137. (0, ava_1.default)('getAssetFromKV custom key modifier', async (t) => {
  138. (0, mocks_1.mockRequestScope)();
  139. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/docs/sub/blah.png'));
  140. const customRequestMapper = (request) => {
  141. let defaultModifiedRequest = (0, index_1.mapRequestToAsset)(request);
  142. let url = new URL(defaultModifiedRequest.url);
  143. url.pathname = url.pathname.replace('/docs', '');
  144. return new Request(url.toString(), request);
  145. };
  146. const res = await (0, index_1.getAssetFromKV)(event, { mapRequestToAsset: customRequestMapper });
  147. if (res) {
  148. t.is(await res.text(), 'picturedis');
  149. }
  150. else {
  151. t.fail('Response was undefined');
  152. }
  153. });
  154. (0, ava_1.default)('getAssetFromKV request override with existing manifest file', async (t) => {
  155. // see https://github.com/cloudflare/kv-asset-handler/pull/159 for more info
  156. (0, mocks_1.mockRequestScope)();
  157. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/image.png')); // real file in manifest
  158. const customRequestMapper = (request) => {
  159. let defaultModifiedRequest = (0, index_1.mapRequestToAsset)(request);
  160. let url = new URL(defaultModifiedRequest.url);
  161. url.pathname = '/image.webp'; // other different file in manifest
  162. return new Request(url.toString(), request);
  163. };
  164. const res = await (0, index_1.getAssetFromKV)(event, { mapRequestToAsset: customRequestMapper });
  165. if (res) {
  166. t.is(await res.text(), 'imagewebp');
  167. }
  168. else {
  169. t.fail('Response was undefined');
  170. }
  171. });
  172. (0, ava_1.default)('getAssetFromKV when setting browser caching', async (t) => {
  173. (0, mocks_1.mockRequestScope)();
  174. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/'));
  175. const res = await (0, index_1.getAssetFromKV)(event, { cacheControl: { browserTTL: 22 } });
  176. if (res) {
  177. t.is(res.headers.get('cache-control'), 'max-age=22');
  178. }
  179. else {
  180. t.fail('Response was undefined');
  181. }
  182. });
  183. (0, ava_1.default)('getAssetFromKV when setting custom cache setting', async (t) => {
  184. (0, mocks_1.mockRequestScope)();
  185. const event1 = (0, mocks_1.getEvent)(new Request('https://blah.com/'));
  186. const event2 = (0, mocks_1.getEvent)(new Request('https://blah.com/key1.png?blah=34'));
  187. const cacheOnlyPngs = (req) => {
  188. if (new URL(req.url).pathname.endsWith('.png'))
  189. return {
  190. browserTTL: 720,
  191. edgeTTL: 720,
  192. };
  193. else
  194. return {
  195. bypassCache: true,
  196. };
  197. };
  198. const res1 = await (0, index_1.getAssetFromKV)(event1, { cacheControl: cacheOnlyPngs });
  199. const res2 = await (0, index_1.getAssetFromKV)(event2, { cacheControl: cacheOnlyPngs });
  200. if (res1 && res2) {
  201. t.is(res1.headers.get('cache-control'), null);
  202. t.true(res2.headers.get('content-type').includes('png'));
  203. t.is(res2.headers.get('cache-control'), 'max-age=720');
  204. t.is(res2.headers.get('cf-cache-status'), 'MISS');
  205. }
  206. else {
  207. t.fail('Response was undefined');
  208. }
  209. });
  210. (0, ava_1.default)('getAssetFromKV caches on two sequential requests', async (t) => {
  211. (0, mocks_1.mockRequestScope)();
  212. const resourceKey = 'cache.html';
  213. const resourceVersion = JSON.parse((0, mocks_1.mockManifest)())[resourceKey];
  214. const event1 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`));
  215. const event2 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`, {
  216. headers: {
  217. 'if-none-match': `"${resourceVersion}"`,
  218. },
  219. }));
  220. const res1 = await (0, index_1.getAssetFromKV)(event1, { cacheControl: { edgeTTL: 720, browserTTL: 720 } });
  221. await (0, mocks_1.sleep)(1);
  222. const res2 = await (0, index_1.getAssetFromKV)(event2);
  223. if (res1 && res2) {
  224. t.is(res1.headers.get('cf-cache-status'), 'MISS');
  225. t.is(res1.headers.get('cache-control'), 'max-age=720');
  226. t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
  227. }
  228. else {
  229. t.fail('Response was undefined');
  230. }
  231. });
  232. (0, ava_1.default)('getAssetFromKV does not store max-age on two sequential requests', async (t) => {
  233. (0, mocks_1.mockRequestScope)();
  234. const resourceKey = 'cache.html';
  235. const resourceVersion = JSON.parse((0, mocks_1.mockManifest)())[resourceKey];
  236. const event1 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`));
  237. const event2 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`, {
  238. headers: {
  239. 'if-none-match': `"${resourceVersion}"`,
  240. },
  241. }));
  242. const res1 = await (0, index_1.getAssetFromKV)(event1, { cacheControl: { edgeTTL: 720 } });
  243. await (0, mocks_1.sleep)(100);
  244. const res2 = await (0, index_1.getAssetFromKV)(event2);
  245. if (res1 && res2) {
  246. t.is(res1.headers.get('cf-cache-status'), 'MISS');
  247. t.is(res1.headers.get('cache-control'), null);
  248. t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
  249. t.is(res2.headers.get('cache-control'), null);
  250. }
  251. else {
  252. t.fail('Response was undefined');
  253. }
  254. });
  255. (0, ava_1.default)('getAssetFromKV does not cache on Cloudflare when bypass cache set', async (t) => {
  256. (0, mocks_1.mockRequestScope)();
  257. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/'));
  258. const res = await (0, index_1.getAssetFromKV)(event, { cacheControl: { bypassCache: true } });
  259. if (res) {
  260. t.is(res.headers.get('cache-control'), null);
  261. t.is(res.headers.get('cf-cache-status'), null);
  262. }
  263. else {
  264. t.fail('Response was undefined');
  265. }
  266. });
  267. (0, ava_1.default)('getAssetFromKV with no trailing slash on root', async (t) => {
  268. (0, mocks_1.mockRequestScope)();
  269. const event = (0, mocks_1.getEvent)(new Request('https://blah.com'));
  270. const res = await (0, index_1.getAssetFromKV)(event);
  271. if (res) {
  272. t.is(await res.text(), 'index.html');
  273. }
  274. else {
  275. t.fail('Response was undefined');
  276. }
  277. });
  278. (0, ava_1.default)('getAssetFromKV with no trailing slash on a subdirectory', async (t) => {
  279. (0, mocks_1.mockRequestScope)();
  280. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/sub/blah.png'));
  281. const res = await (0, index_1.getAssetFromKV)(event);
  282. if (res) {
  283. t.is(await res.text(), 'picturedis');
  284. }
  285. else {
  286. t.fail('Response was undefined');
  287. }
  288. });
  289. (0, ava_1.default)('getAssetFromKV no result throws an error', async (t) => {
  290. (0, mocks_1.mockRequestScope)();
  291. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/random'));
  292. const error = await t.throwsAsync((0, index_1.getAssetFromKV)(event));
  293. t.is(error.status, 404);
  294. });
  295. (0, ava_1.default)('getAssetFromKV TTls set to null should not cache on browser or edge', async (t) => {
  296. (0, mocks_1.mockRequestScope)();
  297. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/'));
  298. const res1 = await (0, index_1.getAssetFromKV)(event, { cacheControl: { browserTTL: null, edgeTTL: null } });
  299. await (0, mocks_1.sleep)(100);
  300. const res2 = await (0, index_1.getAssetFromKV)(event, { cacheControl: { browserTTL: null, edgeTTL: null } });
  301. if (res1 && res2) {
  302. t.is(res1.headers.get('cf-cache-status'), null);
  303. t.is(res1.headers.get('cache-control'), null);
  304. t.is(res2.headers.get('cf-cache-status'), null);
  305. t.is(res2.headers.get('cache-control'), null);
  306. }
  307. else {
  308. t.fail('Response was undefined');
  309. }
  310. });
  311. (0, ava_1.default)('getAssetFromKV passing in a custom NAMESPACE serves correct asset', async (t) => {
  312. (0, mocks_1.mockRequestScope)();
  313. let CUSTOM_NAMESPACE = (0, mocks_1.mockKV)({
  314. 'key1.123HASHBROWN.txt': 'val1',
  315. });
  316. Object.assign(global, { CUSTOM_NAMESPACE });
  317. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/'));
  318. const res = await (0, index_1.getAssetFromKV)(event);
  319. if (res) {
  320. t.is(await res.text(), 'index.html');
  321. t.true(res.headers.get('content-type').includes('html'));
  322. }
  323. else {
  324. t.fail('Response was undefined');
  325. }
  326. });
  327. (0, ava_1.default)('getAssetFromKV when custom namespace without the asset should fail', async (t) => {
  328. (0, mocks_1.mockRequestScope)();
  329. let CUSTOM_NAMESPACE = (0, mocks_1.mockKV)({
  330. 'key5.123HASHBROWN.txt': 'customvalu',
  331. });
  332. const event = (0, mocks_1.getEvent)(new Request('https://blah.com'));
  333. const error = await t.throwsAsync((0, index_1.getAssetFromKV)(event, { ASSET_NAMESPACE: CUSTOM_NAMESPACE }));
  334. t.is(error.status, 404);
  335. });
  336. (0, ava_1.default)('getAssetFromKV when namespace not bound fails', async (t) => {
  337. (0, mocks_1.mockRequestScope)();
  338. var MY_CUSTOM_NAMESPACE = undefined;
  339. Object.assign(global, { MY_CUSTOM_NAMESPACE });
  340. const event = (0, mocks_1.getEvent)(new Request('https://blah.com/'));
  341. const error = await t.throwsAsync((0, index_1.getAssetFromKV)(event, { ASSET_NAMESPACE: MY_CUSTOM_NAMESPACE }));
  342. t.is(error.status, 500);
  343. });
  344. (0, ava_1.default)('getAssetFromKV when if-none-match === active resource version, should revalidate', async (t) => {
  345. (0, mocks_1.mockRequestScope)();
  346. const resourceKey = 'key1.png';
  347. const resourceVersion = JSON.parse((0, mocks_1.mockManifest)())[resourceKey];
  348. const event1 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`));
  349. const event2 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`, {
  350. headers: {
  351. 'if-none-match': `W/"${resourceVersion}"`,
  352. },
  353. }));
  354. const res1 = await (0, index_1.getAssetFromKV)(event1, { cacheControl: { edgeTTL: 720 } });
  355. await (0, mocks_1.sleep)(100);
  356. const res2 = await (0, index_1.getAssetFromKV)(event2);
  357. if (res1 && res2) {
  358. t.is(res1.headers.get('cf-cache-status'), 'MISS');
  359. t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
  360. }
  361. else {
  362. t.fail('Response was undefined');
  363. }
  364. });
  365. (0, ava_1.default)('getAssetFromKV when if-none-match equals etag of stale resource then should bypass cache', async (t) => {
  366. (0, mocks_1.mockRequestScope)();
  367. const resourceKey = 'key1.png';
  368. const resourceVersion = JSON.parse((0, mocks_1.mockManifest)())[resourceKey];
  369. const req1 = new Request(`https://blah.com/${resourceKey}`, {
  370. headers: {
  371. 'if-none-match': `"${resourceVersion}"`,
  372. },
  373. });
  374. const req2 = new Request(`https://blah.com/${resourceKey}`, {
  375. headers: {
  376. 'if-none-match': `"${resourceVersion}-another-version"`,
  377. },
  378. });
  379. const event = (0, mocks_1.getEvent)(req1);
  380. const event2 = (0, mocks_1.getEvent)(req2);
  381. const res1 = await (0, index_1.getAssetFromKV)(event, { cacheControl: { edgeTTL: 720 } });
  382. const res2 = await (0, index_1.getAssetFromKV)(event);
  383. const res3 = await (0, index_1.getAssetFromKV)(event2);
  384. if (res1 && res2 && res3) {
  385. t.is(res1.headers.get('cf-cache-status'), 'MISS');
  386. t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`);
  387. t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
  388. t.not(res3.headers.get('etag'), req2.headers.get('if-none-match'));
  389. t.is(res3.headers.get('cf-cache-status'), 'MISS');
  390. }
  391. else {
  392. t.fail('Response was undefined');
  393. }
  394. });
  395. (0, ava_1.default)('getAssetFromKV when resource in cache, etag should be weakened before returned to eyeball', async (t) => {
  396. (0, mocks_1.mockRequestScope)();
  397. const resourceKey = 'key1.png';
  398. const resourceVersion = JSON.parse((0, mocks_1.mockManifest)())[resourceKey];
  399. const req1 = new Request(`https://blah.com/${resourceKey}`, {
  400. headers: {
  401. 'if-none-match': `"${resourceVersion}"`,
  402. },
  403. });
  404. const event = (0, mocks_1.getEvent)(req1);
  405. const res1 = await (0, index_1.getAssetFromKV)(event, { cacheControl: { edgeTTL: 720 } });
  406. const res2 = await (0, index_1.getAssetFromKV)(event);
  407. if (res1 && res2) {
  408. t.is(res1.headers.get('cf-cache-status'), 'MISS');
  409. t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`);
  410. }
  411. else {
  412. t.fail('Response was undefined');
  413. }
  414. });
  415. (0, ava_1.default)('getAssetFromKV if-none-match not sent but resource in cache, should return cache hit 200 OK', async (t) => {
  416. const resourceKey = 'cache.html';
  417. const event = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`));
  418. const res1 = await (0, index_1.getAssetFromKV)(event, { cacheControl: { edgeTTL: 720 } });
  419. await (0, mocks_1.sleep)(1);
  420. const res2 = await (0, index_1.getAssetFromKV)(event);
  421. if (res1 && res2) {
  422. t.is(res1.headers.get('cf-cache-status'), 'MISS');
  423. t.is(res1.headers.get('cache-control'), null);
  424. t.is(res2.status, 200);
  425. t.is(res2.headers.get('cf-cache-status'), 'HIT');
  426. }
  427. else {
  428. t.fail('Response was undefined');
  429. }
  430. });
  431. (0, ava_1.default)('getAssetFromKV if range request submitted and resource in cache, request fulfilled', async (t) => {
  432. const resourceKey = 'cache.html';
  433. const event1 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`));
  434. const event2 = (0, mocks_1.getEvent)(new Request(`https://blah.com/${resourceKey}`, { headers: { range: 'bytes=0-10' } }));
  435. const res1 = (0, index_1.getAssetFromKV)(event1, { cacheControl: { edgeTTL: 720 } });
  436. await res1;
  437. await (0, mocks_1.sleep)(2);
  438. const res2 = await (0, index_1.getAssetFromKV)(event2);
  439. if (res2.headers.has('content-range')) {
  440. t.is(res2.status, 206);
  441. }
  442. else {
  443. t.fail('Response was undefined');
  444. }
  445. });
  446. ava_1.default.todo('getAssetFromKV when body not empty, should invoke .cancel()');