{"id":18909021,"url":"https://github.com/chdh/buffered-file-reader","last_synced_at":"2026-03-06T21:30:23.545Z","repository":{"id":65149551,"uuid":"584233319","full_name":"chdh/buffered-file-reader","owner":"chdh","description":"A promise-based buffered file reader.","archived":false,"fork":false,"pushed_at":"2023-01-05T01:12:20.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T03:17:45.496Z","etag":null,"topics":["buffered-reader","locate-mode"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chdh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-01T23:44:24.000Z","updated_at":"2023-01-12T02:47:49.000Z","dependencies_parsed_at":"2023-02-03T03:15:54.646Z","dependency_job_id":null,"html_url":"https://github.com/chdh/buffered-file-reader","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chdh%2Fbuffered-file-reader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chdh%2Fbuffered-file-reader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chdh%2Fbuffered-file-reader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chdh%2Fbuffered-file-reader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chdh","download_url":"https://codeload.github.com/chdh/buffered-file-reader/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239896491,"owners_count":19715035,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["buffered-reader","locate-mode"],"created_at":"2024-11-08T09:30:12.811Z","updated_at":"2026-03-06T21:30:23.456Z","avatar_url":"https://github.com/chdh.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# buffered-file-reader\n\nA promise-based buffered reader for sequential byte-wise binary file input.\n\n**NPM package**: [buffered-file-reader](https://www.npmjs.com/package/buffered-file-reader)\u003cbr\u003e\n**Examples of how to use it**: [github.com/chdh/buffered-file-reader/tree/main/examples](https://github.com/chdh/buffered-file-reader/tree/main/examples)\n\n\n## Concept\n\n\"Locate mode\" is used to access data within the internal data buffer.\n\nLocate mode is a very\n[old concept](https://www.google.com/search?q=%22locate+mode%22+ibm)\nwhich was was common in IBM mainframes with PL/I.\nIt's used to avoid copying file data and to allow fast byte-wise inspection of the data stream.\n\nWith the \"view\" functionality of the Node.js\n[Buffer](https://nodejs.org/dist/latest/docs/api/buffer.html)\nand JavaScript\n[TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)\nclasses, it's now possible to use this concept in a modern programming environment.\n\n\n## API\n\n### Reader.open()\n\n```typescript\nstatic Reader.open(fileName: string, options?: OpenOptions) : Promise\u003cReader\u003e\n```\n\nOpens a file and returns a Reader object.\n\n* `fileName`: The name of a file to read from.\n* `options`: Optional options (see below).\n* Return value: A new Reader object.\n\n```typescript\ninterface OpenOptions {\n  bufferSize?:         number; // size of the internal data buffer, default is to use 256 KB\n  preallocatedBuffer?: Buffer; // a pre-allocated buffer to be re-used instead of using bufferSize to allocate a new buffer\n}\n```\n\n### reader.close()\n\n```typescript\nreader.close() : Promise\u003cvoid\u003e\n```\n\nCloses the Reader object and the underlying file handle.\n\n\n### reader.get()\n\n```typescript\nreader.get(length: number) : Promise\u003cBuffer|undefined\u003e\n```\n\nReturns a view into the internal data buffer at the current file position.\n\n* `length`:\n  Specifies the number of bytes requested.\u003cbr\u003e\n  If the end of the file is encountered or if there\n  is not enough space in the internal buffer, a shorter view is returned.\u003cbr\u003e\n  If more data is available in the internal data buffer, which is normally the case, a longer view is returned.\u003cbr\u003e\n  For optimal performance, `length` should be much smaller than the size of the internal data buffer.\n* Return value:\n  A\n  [Buffer](https://nodejs.org/dist/latest/docs/api/buffer.html)\n  object whis is a view into the internal data buffer at the current position.\u003cbr\u003e\n  If the current file position is at the end of the file and no more data is available, `undefined` is returned.\n\nThe current file position is not advanced.\n\n\n### reader.advance()\n\n```typescript\nreader.advance(length: number)\n```\n\nAdvances the current file position.\n\n\n### reader.filePosition\n\n```typescript\nreader.filePosition : number\n\n```\n\nReturns the current file position.\n\n\n## Example\n\n```typescript\nimport {Reader} from \"buffered-file-reader\";\n\nconst reader = await Reader.open(\"test.txt\");\n\nlet buf = await reader.get(4);\nconsole.log(\"First 4 bytes: \", buf.subarray(0, 4));\nconsole.log(\"Available bytes in the buffer view: \", buf.length);\n\nreader.advance(100);\n\nbuf = await reader.get(1);\nconsole.log(\"Byte at position 100: \", buf[0]);\n\nawait reader.close();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchdh%2Fbuffered-file-reader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchdh%2Fbuffered-file-reader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchdh%2Fbuffered-file-reader/lists"}