npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zip-go

v1.0.1

Published

Small, zero dependent, cross friendly zip tool

Downloads

4

Readme

ZIP-go

zip-go was designed with the goal of saving multiple large files generated by the browsers without holding any of the data in memory by streaming/piping data to a destination. While the zip format isn't technically built for streaming and each file entry needs some pre-header information that tells how large a file is and what the crc checksum is... all of this can be stored in the central directory header also at the end of a zip file...

Install

zip-go is an ESM-only module - you are not able to import it with require. If you are unable to use ESM in your project you can use the async import('zip-go') from CommonJS to load zip-go asynchronously. npm install zip-go

Requirements

It can't as of yet write zip files larger than 4 GiB as it has no zip64 support but it can read those.

Creating a ZIP

import Writer from 'zip-go/lib/write.js'

const s3 = 'https://s3-us-west-2.amazonaws.com/bencmbrook/'
const files = ['NYT.txt', 'water.png', 'Earth.jpg'].values()

// Creates a regular ReadableStream that will pull file like objects
const myReadable = new ReadableStream({
  async pull(controller) {
    const { done, value } = files.next()
    if (done) return controller.close()
    const { body } = await fetch(s3 + value)

    return controller.enqueue({
      name: `/${value}`,
      stream: () => body,
    })
  }
})

const stream = myReadable
  .pipeThrough(new ZipWriter())

const blob = await new Response(stream).blob()

if you would like to work it more manually you can do that as well.

import StreamSaver from 'streamsaver';
import Writer from 'zip-go/lib/write.js';

const { readable, writable } = new Writer();
const writer = writable.getWriter();

// Set up StreamSaver
const fileStream = streamSaver.createWriteStream('archive.zip');

// Add a WebIDL File like object that at least have name and a stream method
// that returns a whatwg ReadableStream
writer.write({
  name: '/cat.txt',
  lastModified: +new Date(123),
  stream: () => new Response('mjau').body
})

writer.write(
  new File(['woof'], 'dog.txt', { lastModified: +new Date(123)})
)

readable.pipeTo(destination)

writer.close()

Reading a zip

This read method only read the central directory (end of the file) to figure out all about each entry. Each Entry returns a WebIDL File like object with added properties that are more zip specific

import read from 'zip-go/lib/reader.js'

for await (const entry of read(blob)) {
  console.log(entry)
  console.log(entry.name)
  console.log(entry.size)
  console.log(entry.type)
  console.log(entry.directory)

  const ab = await entry.arrayBuffer()
  const text = await entry.text()
  const readableStream = entry.stream()

  // returns a real web File Object, if the entry is uncompressed
  // it will just slice the zip with it's start/end position
  const file = await entry.file()
}