|
| 1 | +/* eslint-env node, browser, jasmine */ |
| 2 | +/* global DecompressionStream */ |
| 3 | +import pako from 'pako' |
| 4 | + |
| 5 | +import { browserInflate } from '../src/utils/browserInflate.js' |
| 6 | + |
| 7 | +import { corruptZlibCases } from './__helpers__/corruptZlib.js' |
| 8 | + |
| 9 | +const describeNative = |
| 10 | + typeof DecompressionStream === 'function' && |
| 11 | + typeof Blob === 'function' && |
| 12 | + typeof Blob.prototype.stream === 'function' && |
| 13 | + typeof Response === 'function' |
| 14 | + ? describe |
| 15 | + : xdescribe |
| 16 | + |
| 17 | +describeNative('browserInflate', () => { |
| 18 | + it('inflates valid binary data', async () => { |
| 19 | + const bytes = Uint8Array.from([0, 1, 127, 128, 255]) |
| 20 | + expect(await browserInflate(pako.deflate(bytes))).toEqual(bytes) |
| 21 | + }) |
| 22 | + |
| 23 | + it('inflates a valid empty stream', async () => { |
| 24 | + const bytes = new Uint8Array(0) |
| 25 | + expect(await browserInflate(pako.deflate(bytes))).toEqual(bytes) |
| 26 | + }) |
| 27 | + |
| 28 | + for (const [name, bytes] of corruptZlibCases) { |
| 29 | + it(`reports ${name} compressed data with a nonempty error`, async () => { |
| 30 | + let error = null |
| 31 | + try { |
| 32 | + await browserInflate(Uint8Array.from(bytes)) |
| 33 | + } catch (err) { |
| 34 | + error = err |
| 35 | + } |
| 36 | + // Native stream errors can originate outside Jest's VM realm. |
| 37 | + expect(Object.prototype.toString.call(error)).toBe('[object Error]') |
| 38 | + expect(error.message.length).toBeGreaterThan(0) |
| 39 | + expect(error.message).not.toMatch(/internal error|file an issue/) |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + for (const original of [ |
| 44 | + new TypeError(''), |
| 45 | + new TypeError('native diagnostic'), |
| 46 | + ]) { |
| 47 | + it(`handles an asynchronous TypeError with message "${original.message}"`, async () => { |
| 48 | + const arrayBuffer = Response.prototype.arrayBuffer |
| 49 | + Response.prototype.arrayBuffer = async function () { |
| 50 | + await arrayBuffer.call(this) |
| 51 | + throw original |
| 52 | + } |
| 53 | + let error = null |
| 54 | + try { |
| 55 | + await browserInflate(pako.deflate(Uint8Array.from([1, 2, 3]))) |
| 56 | + } catch (err) { |
| 57 | + error = err |
| 58 | + } finally { |
| 59 | + Response.prototype.arrayBuffer = arrayBuffer |
| 60 | + } |
| 61 | + if (original.message) { |
| 62 | + expect(error).toBe(original) |
| 63 | + } else { |
| 64 | + expect(error.message).toBe( |
| 65 | + 'Invalid compressed buffer: decompression failed' |
| 66 | + ) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +}) |
0 commit comments