endojs / endo

Endo is a distributed secure JavaScript sandbox, based on SES
Apache License 2.0
804 stars 71 forks source link

@endo/zip conflicts with browser Blob usage #1811

Closed dckc closed 11 months ago

dckc commented 11 months ago

Describe the bug

I recently prototyped a browser-based bundle explorer tool:

I tried to use @endo/zip but the endo ZipReader() constructor depends on the BufferReader implementation:

  constructor(data, options = {}) {
    const { name = '<unknown>' } = options;
    const reader = new BufferReader(data);

I don't see a way to slot in a BlobReader.

I suppose I could have used a Buffer shim for the browser, but I had the data in a Blob. It turned out to be easier to use zip-loader.

cc @kriskowal

mhofman commented 11 months ago

BufferReader takes an ArrayBuffer which can be obtained from a Blob. I'm not sure where node Buffer comes in.

mhofman commented 11 months ago

Btw, the type looks wrong on ZipReader, it likely takes an ArrayBuffer, not an UInt8Array...

Edit: according to the spec, the typed array constructors takes either, so I guess we're fine. The documented types however are inconsistent.

kriskowal commented 11 months ago

Interoperating with other zip implementations is a win I’ll take. But, @endo/zip uses Uint8Array as the coin of its realm.

These steps should be sufficient to bridge:

const buffer = await blob.arrayBuffer();
const bytes = new Uint8Array(buffer);
const zip = new ZipReader(bytes);
mhofman commented 11 months ago

step 2 seem to only be necessary to satisfy types.

dckc commented 11 months ago

... I'm not sure where node Buffer comes in.

oops! Looks like I was just reading too fast.