uniform interface for accessing binary data from local files, remote HTTP resources, and browser Blob data
npm install generic-filehandleNOTE: please see https://github.com/GMOD/generic-filehandle2 for new work, with modified API and no Buffer polyfill needed



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.
``js
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
`js`
import { RemoteFile } from 'generic-filehandle'
import fetch from 'node-fetch'
const remote = new RemoteFile('http://somesite.com/file.txt', { fetch })
- 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.
Returns a Promise for a buffer or string containing the contents of the whole file.
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.
Closes the filehandle.
The Options object for the constructor, read and readFile can contain abort signal
to customize behavior. All entries are optional.
- signal - an AbortSignal that is passed to remote file fetch() API or other file readers
- headers
The Options object for readFile can also contain an entry encoding. Theutf8
default is no encoding, in which case the file contents are returned as a
buffer. Currently, the only available encoding is , andreadFile` method will accept the string "utf8" instead of an Options object.
specifying that will cause the file contents to be returned as a string. For compatibility with the Node API, the
This library implements a subset of the Node.js v10 promise-based FileHandle API.