{"id":15592040,"url":"https://github.com/derhuerst/serve-buffer","last_synced_at":"2025-04-15T05:01:19.926Z","repository":{"id":66219589,"uuid":"255162194","full_name":"derhuerst/serve-buffer","owner":"derhuerst","description":"Serve a blob of binary data via HTTP, with support for range requests, conditional GET \u0026 compression.","archived":false,"fork":false,"pushed_at":"2021-11-17T16:00:38.000Z","size":112,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T05:00:06.667Z","etag":null,"topics":["arraybuffer","buffer","http","http-range-requests","in-memory"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/derhuerst.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-12T20:14:15.000Z","updated_at":"2024-09-14T18:08:08.000Z","dependencies_parsed_at":"2023-06-11T03:30:44.308Z","dependency_job_id":null,"html_url":"https://github.com/derhuerst/serve-buffer","commit_stats":{"total_commits":37,"total_committers":1,"mean_commits":37.0,"dds":0.0,"last_synced_commit":"ccc9cca8ccdecd8158016bb82747ac37eab04222"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fserve-buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fserve-buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fserve-buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fserve-buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derhuerst","download_url":"https://codeload.github.com/derhuerst/serve-buffer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249010212,"owners_count":21197797,"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":["arraybuffer","buffer","http","http-range-requests","in-memory"],"created_at":"2024-10-02T23:53:29.091Z","updated_at":"2025-04-15T05:01:19.872Z","avatar_url":"https://github.com/derhuerst.png","language":"JavaScript","funding_links":["https://github.com/sponsors/derhuerst"],"categories":[],"sub_categories":[],"readme":"# serve-buffer\n\n**Serve a [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer) via HTTP, with [`Range`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range), [conditional `GET`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match) and [GZip](https://developer.mozilla.org/en-US/docs/Glossary/GZip_compression)/[Brotli](https://developer.mozilla.org/en-US/docs/Glossary/brotli_compression) compression support.**\n\n[![npm version](https://img.shields.io/npm/v/serve-buffer.svg)](https://www.npmjs.com/package/serve-buffer)\n[![build status](https://api.travis-ci.org/derhuerst/serve-buffer.svg?branch=master)](https://travis-ci.org/derhuerst/serve-buffer)\n![ISC-licensed](https://img.shields.io/github/license/derhuerst/serve-buffer.svg)\n![minimum Node.js version](https://img.shields.io/node/v/serve-buffer.svg)\n[![support me via GitHub Sponsors](https://img.shields.io/badge/support%20me-donate-fa7664.svg)](https://github.com/sponsors/derhuerst)\n[![chat with me on Twitter](https://img.shields.io/badge/chat%20with%20me-on%20Twitter-1da1f2.svg)](https://twitter.com/derhuerst)\n\n*Note:* If you want to serve *files* with support for `Range`, conditional `GET` and compression, use [`send`](https://github.com/pillarjs/send). If you want to serve an entire directory of files, use [`serve-static`](https://github.com/expressjs/serve-static).\n\nThere is a surprising number of difficult-to-understand corner cases in the HTTP RFCs. I [tried my best](lib/serve-buffer.js) here, so that others don't have to write quick-and-dirty (which in the HTTP realm usually means slightly wrong) implementations. This library supports the following request headers:\n\n- [`Accept-Encoding`](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)\n- [`Range`](https://datatracker.ietf.org/doc/html/rfc7233#section-3.1) \u0026 [`If-Range`](https://datatracker.ietf.org/doc/html/rfc7233#section-3.2)\n- [`If-None-Match`](https://datatracker.ietf.org/doc/html/rfc7232#section-3.2)/[`If-Match`](https://datatracker.ietf.org/doc/html/rfc7232#section-3.1) \u0026 [`If-Modified-Since`](https://datatracker.ietf.org/doc/html/rfc7232#section-3.3)/[`If-Unmodified-Since`](https://datatracker.ietf.org/doc/html/rfc7232#section-3.4)\n\n\n## Installation\n\n```shell\nnpm install serve-buffer\n```\n\n\n## Usage\n\n```js\nconst express = require('express')\nconst serveBuffer = require('serve-buffer')\n\nconst app = express()\n\nlet data = Buffer.from('a lot of data here…', 'utf8')\napp.use('/data', (req, res) =\u003e {\n\tserveBuffer(req, res, data)\n})\n\n// change buffer later\ndata = Buffer.from('entirely different buffer', 'utf8')\n```\n\n### allow caching via `timeModified` \u0026 `etag`\n\n```js\nconst computeEtag = require('etag')\n\nlet data = Buffer.from('a lot of data here…', 'utf8')\nlet timeModified = new Date()\nlet etag = computeEtag(data)\n\napp.use('/data', (req, res) =\u003e {\n\tserveBuffer(req, res, data, {timeModified, etag})\n})\n\n// change buffer later\ndata = Buffer.from('entirely different buffer', 'utf8')\ntimeModified = new Date()\netag = computeEtag(data)\n```\n\n### serve [gzipped](https://en.wikipedia.org/wiki/Gzip) \u0026 [Brotli](https://en.wikipedia.org/wiki/Brotli)-compressed data\n\nServing compressed data reduces the amount of transferred data at the cost of higher CPU load, so it is usually worth it if your data rarely changes, or if you have slowly connected (or a lot of) consumers.\n\nIf `buf` is reasonably small (\u003c=10mb for GZip, \u003c= 512kb for Brotli), `serve-buffer` will compress it by default. If you don't want this, pass `opt.gzip: false` and/or `opt.brotliCompress: false`; Instead, you can also customise the size limits via `opt.gzipMaxSize` \u0026 `opt.brotliCompressMaxSize`.\n\nIf you *never mutate* the buffer(s) that you pass into `serveBuffer`, you can tell it to *cache* each buffer's compressed version as long as the instance exists (using a [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)) by passing `opt.unmutatedBuffers: true`:\n\n```js\nconst data = Buffer.from('a lot of data here…', 'utf8')\nconst timeModified = new Date()\nconst etag = computeEtag(data)\n\napp.use('/data', (req, res) =\u003e {\n\tserveBuffer(req, res, data, {\n\t\ttimeModified,\n\t\tetag,\n\t\t// Only do this if you never mutate `data`!\n\t\tunmutatedBuffers: true,\n\t})\n})\n```\n\n\n## API\n\n```js\nserveBuffer(req, res, buf, opt = {}, cb = () =\u003e {})\n```\n\n`opt` overrides the default config, which looks like this:\n\n```js\n{\n\tcontentType: 'application/octet-stream',\n\ttimeModified: new Date(),\n\tetag: require('etag')(buf),\n\n\tgzip: true, // or `false` or `async (buf) =\u003e ({compressedBuffer, compressedEtag})`\n\tgzipMaxSize: 10 * 1024 * 1024, // 10mb\n\tbrotliCompress: true, // or `false` or `async (buf) =\u003e ({compressedBuffer, compressedEtag})`\n\tbrotliCompressMaxSize: 512 * 1024, // 512kb\n\t// Assume that Buffers passed in as `buf` never get mutated? If `true`, each compressed buffer \u0026 compressed ETag will be cached as long as the buffer instance exists.\n\tunmutatedBuffers: false,\n\n\tcacheControl: true, // send cache-control header?\n\tmaxAge: 0, // for cache-control, in milliseconds\n\timmutable: false, // for cache-control\n\n\t// hook functions for modifying serve-buffer's behavior\n\tbeforeSend: (req, res, body, opt) =\u003e {},\n}\n```\n\n`cb` will be called once the response headers and body (if applicable) have been sent.\n\n\n## Related\n\n- [`send-stream`](https://github.com/nicolashenry/send-stream) – Streaming file server with Range and conditional-GET support from file system or other streaming sources. (Very similar to `serve-buffer`.)\n- [`send`](https://github.com/pillarjs/send) – Streaming static file server with Range and conditional-GET support\n- [`http-file-response`](https://github.com/mafintosh/http-file-response) – Send a file back as a HTTP response with support for range queries etc.\n\n\n## Contributing\n\nIf you have a question or need support using `serve-buffer`, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use [the issues page](https://github.com/derhuerst/serve-buffer/issues).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhuerst%2Fserve-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderhuerst%2Fserve-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhuerst%2Fserve-buffer/lists"}