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

generic-filehandle

v3.1.2

Published

uniform interface for accessing binary data from local files, remote HTTP resources, and browser Blob data

Downloads

10,126

Readme

generic-filehandle

NPM version Coverage Status Build Status

Provides a uniform interface for accessing binary data from local files, remote HTTP resources, and Blob data in the browser. Implements a subset of the Node.js v10 promise-based FileHandle API.

Usage

import { LocalFile, RemoteFile, BlobFile } from 'generic-filehandle'

// operate on a local file path
const local = new LocalFile('/some/file/path/file.txt')

// operate on a remote file path
const remote = new RemoteFile('http://somesite.com/file.txt')

// operate on blob objects
const blobfile = new BlobFile(new Blob([some_existing_buffer], { type: 'text/plain' }))

// read slice of file, works on remote files with range request, pre-allocate buffer
const buf = Buffer.alloc(10)
const { bytesRead } = await remote.read(buf, 0, 10, 10)
console.log(buf.toString())

// readFile, returns buffer
const buf = remote.readFile()

Important: under node.js, you should supply a fetch function to the RemoteFile constructor

import { RemoteFile } from 'generic-filehandle'
import fetch from 'node-fetch'
const remote = new RemoteFile('http://somesite.com/file.txt', { fetch })

API

async read(buf:Buffer, offset: number=0, length: number, position: number=0, opts?: Options): Promise<{bytesRead:number,buffer:Buffer}>

  • buf - a pre-allocated buffer that can contain length bytes
  • offset - an offset into the buffer to write into
  • length - a length of data to read
  • position - the byte offset in the file to read from
  • opts - optional Options object

Returns a Promise for the number of bytes read, and the data will be copied into the Buffer provided in the arguments.

async readFile(opts?: Options): Promise<Buffer | string>

Returns a Promise for a buffer or string containing the contents of the whole file.

async stat() : Promise<{size: number}>

Returns a Promise for an object containing as much information about the file as is available. At minimum, the size of the file will be present.

async close() : Promise

Closes the filehandle.

Options

The Options object for the constructor, read and readFile can contain abort signal to customize behavior. All entries are optional.

  • signal <AbortSignal> - an AbortSignal that is passed to remote file fetch() API or other file readers
  • headers <Object <string, string> >- extra HTTP headers to pass to remote file fetch() API
  • overrides <Object> - extra parameters to pass to the remote file fetch() API
  • fetch <Function> - a custom fetch callback, otherwise defaults to the environment (initialized in constructor)
  • encoding <string> - if specified, then this function returns a string. Otherwise it returns a buffer. Currently only utf8 encoding is supported.

The Options object for readFile can also contain an entry encoding. The default is no encoding, in which case the file contents are returned as a buffer. Currently, the only available encoding is utf8, and specifying that will cause the file contents to be returned as a string. For compatibility with the Node API, the readFile method will accept the string "utf8" instead of an Options object.

References

This library implements a subset of the Node.js v10 promise-based FileHandle API.