101arrowz / fflate

High performance (de)compression in an 8kB package
https://101arrowz.github.io/fflate
MIT License
2.27k stars 79 forks source link

Unexpected EOF during test with nock + fetch #136

Closed LiterallyDad closed 2 years ago

LiterallyDad commented 2 years ago

The problem Lib works until testing with nock + fetch.

test code

let result: Example[] =
  [
    {
      test: '8475835',
      test2: '1248241',
    }
  ];

const exampleJson = JSON.stringify(result);
let compressedExample: Uint8Array = 
fflate.compressSync(fflate.strToU8(exampleJson));

export const mockApi = () => {
  nock('http://localhost')
      .get('/fake_path')
      .reply(200, compressedExample);
}

Api.ts


const response = await fetch(url, {
    credentials: 'omit'
  });
const responseBuffer = await response.arrayBuffer();
const response_array = new Uint8Array(responseBuffer);
const decompressed = fflate.decompressSync(response_array) // explodes here with 'Unexpected EOF'

important

this works totally fine in all circumstances except for when the data comes from the mocked fetch's Response.arrayBuffer()

even the code above, if I decompress immediately after compressing, no problem.

Error

Error: unexpected EOF
        at inflt ([worker eval]:204:21)
        at inflateSync ([worker eval]:257:12)
        at onmessage ([worker eval]:258:221)
        at MessagePort.<anonymous> ([worker eval]:258:341)
        at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:562:20)
        at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)
        at MessagePort.callbackTrampoline (node:internal/async_hooks:130:17) {
      code: 0
101arrowz commented 2 years ago

I'm quite confident that this is an issue in usage of Nock rather than fflate. Try logging the length of the data you're receiving from the mocked endpoint and see if it is what you expect.

101arrowz commented 2 years ago

I'm going to assume this was an issue with usage and close, but feel free to reopen if this really is a bug with fflate.