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

@chunkd/fs

v11.2.0

Published

File system abstraction layer

Downloads

608

Readme

@chunkd/fs

File system abstraction layer

Makes it easier to work with local file system or remote async applications with filesystem like interfaces

  • File - Read/Write the local file system
  • Memory - memory as a mock file system
  • Http -HTTP(s) requests to a remote web server
  • AWS - AWS S3 as a remote file system

Usage

Basic

Read a text file

import { fsa } from '@chunkd/fs';

const bytes = await fsa.read(fsa.toUrl('../foo.txt'));

List a folder

for await (const url of fsa.list(fsa.toUrl('/'))) {
  const bytes = await fsa.read(url)
}

Multiple file systems

Register multiple file systems

import { fsa, FsMemory } from '@chunkd/fs';

fsa.register('memory://', new FsMemory());

await fsa.write(fsa.toUrl('memory://foo.txt'), 'Hello World');

// Stream the file from memory into the local file system
await fsa.write(fsa.toUrl('/foo.txt'), fsa.readStream(fsa.toUrl(()'memory://foo.txt')));

Remote file systems

Remote file systems can also be used

import { fsa, FsHttp } from '@chunkd/fs';
import { FsAwsS3 } from '@chunkd/fs-aws';

fsa.register('https://', new FsHttp());
fsa.register('s3://', new FsAwsS3());

// Stream a file from https://example.com into AWS S3
await fsa.write(fsa.toUrl('s3://example/foo.txt'), fsa.readStream(fsa.toUrl('https://example.com/foo.txt')));

Multiple S3 Credentials

Multiple s3 clients can be registered with different credentials and then be used to stream files between AWS accounts or roles

import {fsa} from '@chunkd/fs'
import {FsAwsS3} from '@chunkd/fs-aws';

fsa.register('s3://bucket-a/', new FsAwsS3(new S3Client({ credentials: bucketA })));
fsa.register('s3://bucket-b/', new FsAwsS3(new S3Client({ credentials: bucketB })));

fsa.write(fsa.toUrl('s3://bucket-a/foo.txt'), fsa.readStream(fsa.toUrl('s3://bucket-b/foo.txt')));

Errors

All file system errors should be wrapped into a FsError

try {
  await fsa.read('s3://foo/bar.txt')
} catch(e) {
  if(FsError.is(e)) {
    e.status // Http Status code eg 403
    e.cause // original error
    e.action // `read`
    e.url // `s3://foo/bar.txt`
  }
}

# Interface

All file systems implement then following interface interface

```typescript
export interface FileSystem {
  /** Read a file into a buffer */
  read(location: URL): Promise<Buffer>;
  /** Create a read stream */
  readStream(location: URL): Readable;
  /** Write a file from either a buffer or stream */
  write(location: URL, buffer: Buffer | Readable | string, opts?: Partial<WriteOptions>): Promise<void>;
  /** list all files in path */
  list(location: URL, opt?: ListOptions): AsyncGenerator<URL>;
  /** list all files with file info in path */
  details(location: URL, opt?: ListOptions): AsyncGenerator<FileInfo>;
  /** Get information about the path  */
  head(location: URL): Promise<FileInfo | null>;
  /** Create a chunk source to read chunks out of */
  source(location: URL): Source;
  /** Delete a file from the location */
  delete(location: URL): Promise<void>;
}