| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,252 @@ |
| +import { IncrementalCache } from '../../../packages/next/src/server/lib/incremental-cache' |
| + |
| +function createCache() { |
| + return new IncrementalCache({ |
| + dev: false, |
| + requestHeaders: {}, |
| + getPrerenderManifest: () => |
| + ({ |
| + version: 4, |
| + routes: {}, |
| + dynamicRoutes: {}, |
| + notFoundRoutes: [], |
| + preview: { |
| + previewModeId: 'id', |
| + previewModeSigningKey: 'key', |
| + previewModeEncryptionKey: 'key', |
| + }, |
| + }) as any, |
| + }) |
| +} |
| + |
| +describe('IncrementalCache.generateCacheKey', () => { |
| + const cache = createCache() |
| + const url = 'https://example.com/api' |
| + |
| + it('distinguishes binary bodies that UTF-8 decoding would collapse', async () => { |
| + // 0xff and 0xfe both decode to U+FFFD as UTF-8; the key must tell them |
| + // apart by their raw bytes. |
| + const a = await cache.generateCacheKey(url, { |
| + body: new Uint8Array([0xff]), |
| + }) |
| + const b = await cache.generateCacheKey(url, { |
| + body: new Uint8Array([0xfe]), |
| + }) |
| + expect(a).not.toBe(b) |
| + }) |
| + |
| + it('distinguishes a string body from its UTF-8 encoded bytes', async () => { |
| + // A string and the bytes it encodes to are different request shapes. |
| + const a = await cache.generateCacheKey(url, { body: 'hello' }) |
| + const b = await cache.generateCacheKey(url, { |
| + body: new TextEncoder().encode('hello'), |
| + }) |
| + expect(a).not.toBe(b) |
| + }) |
| + |
| + it('uses the selected bytes of arbitrary ArrayBuffer views', async () => { |
| + const backing = new Uint8Array([9, 1, 2, 3, 9]) |
| + const dataView = new DataView(backing.buffer, 1, 3) |
| + const typedView = new Uint8Array([1, 2, 3]) |
| + |
| + const a = await cache.generateCacheKey(url, { body: dataView }) |
| + const b = await cache.generateCacheKey(url, { body: typedView }) |
| + expect(a).toBe(b) |
| + }) |
| + |
| + it('distinguishes URLSearchParams, FormData, and string bodies', async () => { |
| + const form = new FormData() |
| + form.append('x', 'a') |
| + |
| + const formKey = await cache.generateCacheKey(url, { body: form }) |
| + const paramsKey = await cache.generateCacheKey(url, { |
| + body: new URLSearchParams([['x', 'a']]), |
| + }) |
| + const stringKey = await cache.generateCacheKey(url, { body: 'x=a' }) |
| + |
| + expect(new Set([formKey, paramsKey, stringKey]).size).toBe(3) |
| + }) |
| + |
| + it('distinguishes a Blob from its raw content bytes', async () => { |
| + const bytes = new Uint8Array([1, 2, 3]) |
| + const blobKey = await cache.generateCacheKey(url, { |
| + body: new Blob([bytes]), |
| + }) |
| + const bytesKey = await cache.generateCacheKey(url, { body: bytes }) |
| + |
| + expect(blobKey).not.toBe(bytesKey) |
| + }) |
| + |
| + it('is deterministic for identical bodies', async () => { |
| + const a = await cache.generateCacheKey(url, { |
| + body: new Uint8Array([1, 2, 3]), |
| + }) |
| + const b = await cache.generateCacheKey(url, { |
| + body: new Uint8Array([1, 2, 3]), |
| + }) |
| + expect(a).toBe(b) |
| + }) |
| + |
| + it('does not collide FormData multi-values with a comma-joined string', async () => { |
| + // The two values ['a', 'b'] must not hash the same as the single 'a,b'. |
| + const multi = new FormData() |
| + multi.append('x', 'a') |
| + multi.append('x', 'b') |
| + |
| + const joined = new FormData() |
| + joined.append('x', 'a,b') |
| + |
| + const a = await cache.generateCacheKey(url, { body: multi }) |
| + const b = await cache.generateCacheKey(url, { body: joined }) |
| + expect(a).not.toBe(b) |
| + }) |
| + |
| + it('distinguishes blobs that differ only by content type', async () => { |
| + const bytes = new Uint8Array([1, 2, 3]) |
| + const a = await cache.generateCacheKey(url, { |
| + body: new Blob([bytes], { type: 'text/plain' }), |
| + }) |
| + const b = await cache.generateCacheKey(url, { |
| + body: new Blob([bytes], { type: 'application/json' }), |
| + }) |
| + expect(a).not.toBe(b) |
| + }) |
| + |
| + it('distinguishes ArrayBuffers', async () => { |
| + const a = await cache.generateCacheKey(url, { |
| + body: new Uint8Array([1, 2, 3, 4]).buffer, |
| + }) |
| + const b = await cache.generateCacheKey(url, { |
| + body: new Uint8Array([1, 2, 3]).buffer, |
| + }) |
| + expect(a).not.toBe(b) |
| + }) |
| + |
| + it('produces the same key for identical bytes in different typed arrays', async () => { |
| + const uint8View = new Uint8Array([1, 2, 3, 4]) |
| + const uint16View = new Uint16Array( |
| + uint8View.buffer, |
| + uint8View.byteOffset, |
| + uint8View.byteLength / 2 |
| + ) |
| + const a = await cache.generateCacheKey(url, { |
| + body: uint8View, |
| + }) |
| + const b = await cache.generateCacheKey(url, { |
| + body: uint16View, |
| + }) |
| + expect(a).toBe(b) |
| + }) |
| + |
| + it('does not collide FormData with differently interleaved values', async () => { |
| + const a = new FormData() |
| + a.append('x', 'a') |
| + a.append('x', 'b') |
| + a.append('y', 'a') |
| + |
| + const b = new FormData() |
| + b.append('x', 'a') |
| + b.append('y', 'a') |
| + b.append('x', 'b') |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| + |
| + it('does not collide FormData whose value forges quoted entry delimiters', async () => { |
| + // User values may resemble framing text and must remain data. |
| + const a = new FormData() |
| + a.append('x', 'a"key:"y"str:"b') |
| + |
| + const b = new FormData() |
| + b.append('x', 'a') |
| + b.append('y', 'b') |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| + |
| + it('does not collide FormData whose value spans an entry boundary', async () => { |
| + // One value may contain text resembling a complete following entry. |
| + const a = new FormData() |
| + a.append('x', 'akey:ystr:b') |
| + |
| + const b = new FormData() |
| + b.append('x', 'a') |
| + b.append('y', 'b') |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| + |
| + it('does not collide FormData files that differ only by name/type split', async () => { |
| + // File name and content type are separate fetch-relevant metadata. |
| + const bytes = new Uint8Array([1, 2, 3]) |
| + |
| + const a = new FormData() |
| + a.append('f', new Blob([bytes], { type: 'c' }), 'ab') |
| + |
| + const b = new FormData() |
| + b.append('f', new Blob([bytes], { type: 'bc' }), 'a') |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| + |
| + it('does not collide a file whose content forges a following entry', async () => { |
| + // File bytes may resemble framing for a following field. |
| + const enc = new TextEncoder() |
| + |
| + const a = new FormData() |
| + a.append('f', new Blob([enc.encode('AAAkey:1kstr:1v')], { type: '' }), 'n') |
| + |
| + const b = new FormData() |
| + b.append('f', new Blob([enc.encode('AAA')], { type: '' }), 'n') |
| + b.append('k', 'v') |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| + |
| + it('does not collide blobs that differ only by type/content split', async () => { |
| + // Blob content types and bytes must be framed as separate values. |
| + const enc = new TextEncoder() |
| + |
| + const a = new Blob([enc.encode('y')], { type: 'bytes:x' }) |
| + const b = new Blob([enc.encode('xbytes:y')], { type: '' }) |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| + |
| + it('does not collide a FormData value whose length digits absorb a forged entry', async () => { |
| + // Length digits adjacent to user data must not make framing ambiguous. |
| + const a = new FormData() |
| + a.append('x', 'key:1astr:0') |
| + |
| + const b = new FormData() |
| + b.append('x', '1') |
| + b.append('a', '') |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| + |
| + it('does not collide blobs whose type-length digits absorb the content', async () => { |
| + // Type lengths and content beginning with digits remain distinct. |
| + const enc = new TextEncoder() |
| + const a = new Blob([new Uint8Array(0)], { type: 'bytes:aaaaa' }) |
| + const b = new Blob([enc.encode('aaaaabytes:')], { type: '1' }) |
| + |
| + const keyA = await cache.generateCacheKey(url, { body: a }) |
| + const keyB = await cache.generateCacheKey(url, { body: b }) |
| + expect(keyA).not.toBe(keyB) |
| + }) |
| +}) |
|
|