{"id":15601595,"url":"https://github.com/julianrubisch/express-asset-file-cache-middleware","last_synced_at":"2025-04-28T10:42:03.602Z","repository":{"id":36473186,"uuid":"226557698","full_name":"julianrubisch/express-asset-file-cache-middleware","owner":"julianrubisch","description":"An express.js middleware to locally cache assets (images, videos, audio, etc.) for faster access and proxying, for use in e.g. Electron apps","archived":false,"fork":false,"pushed_at":"2023-03-03T11:02:06.000Z","size":114,"stargazers_count":13,"open_issues_count":6,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-09T15:49:49.443Z","etag":null,"topics":["electron-apps","express","express-middleware","expressjs","javascript","node","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/julianrubisch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-12-07T18:22:50.000Z","updated_at":"2023-10-24T20:18:18.000Z","dependencies_parsed_at":"2024-12-12T14:31:58.181Z","dependency_job_id":"53560c13-e67b-49d6-b171-eae1f807b915","html_url":"https://github.com/julianrubisch/express-asset-file-cache-middleware","commit_stats":{"total_commits":35,"total_committers":3,"mean_commits":"11.666666666666666","dds":0.3142857142857143,"last_synced_commit":"74bce3c1df1aea1b1fc7498b2187691ce5bcc7b4"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julianrubisch%2Fexpress-asset-file-cache-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julianrubisch%2Fexpress-asset-file-cache-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julianrubisch%2Fexpress-asset-file-cache-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julianrubisch%2Fexpress-asset-file-cache-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/julianrubisch","download_url":"https://codeload.github.com/julianrubisch/express-asset-file-cache-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233504803,"owners_count":18686209,"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":["electron-apps","express","express-middleware","expressjs","javascript","node","nodejs"],"created_at":"2024-10-03T02:23:07.528Z","updated_at":"2025-01-11T16:24:49.638Z","avatar_url":"https://github.com/julianrubisch.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-asset-file-cache-middleware\n\u003c!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --\u003e\n[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)\n\u003c!-- ALL-CONTRIBUTORS-BADGE:END --\u003e\n\n![Build Status](https://github.com/julianrubisch/express-asset-file-cache-middleware/workflows/Node%20CI/badge.svg)\n\nA modest express.js middleware to locally cache assets (images, videos, audio, etc.) for faster access and proxying, for use in e.g. Electron apps.\n\n## TL;DR\n\nFor offline use of dynamic assets, e.g. in your Electron app or local express server.\n\n## Usage\n\n```javascript\nconst express = require(\"express\");\nconst fileCacheMiddleware = require(\"express-asset-file-cache-middleware\");\n\nconst app = express();\n\napp.get(\n  \"/assets/:asset_id\",\n  async (req, res, next) =\u003e {    \n    res.locals.fetchUrl = `https://cdn.example.org/path/to/actual/asset/${req.params.asset_id}`;\n\n    res.locals.cacheKey = `${someExpirableUniqueKey}`;\n    next();\n  },\n  fileCacheMiddleware({ cacheDir: \"/tmp\", maxSize: 10 * 1024 * 1024 * 1024 }),\n  (req, res) =\u003e {\n    res.set({\n      \"Content-Type\": res.locals.contentType,\n      \"Content-Length\": res.locals.contentLength\n    });\n    res.end(res.locals.buffer, \"binary\");\n  }\n);\n\napp.listen(3000);\n```\n\nIt works by fetching your asset in between two callbacks on e.g. a route, by attaching a `fetchUrl` onto `res.locals`. When the asset isn't cached on disk already, it will write it into a directory specified by the option `cacheDir`. If it finds a file that's alread there, it will use that.\n\nThe asset's `contentType` and `contentLength` are stored base64 encoded in the filename, thus no offline database is necessary\n\nNote that setting `cacheKey` and `cacheDir` isn't strictly necessary, it will fall back to `res.local.fetchUrl` and `path.join(process.cwd(), \"/tmp\")`, respectively.\n\n## LRU Eviction\n\nTo avoid cluttering your device, an LRU (least recently used) cache eviction strategy is in place. Per default, when your cache dir grows over 1 GB of size, the least recently used (accessed) files will be evicted (deleted), until enough disk space is available again. You can change the cache dir size by specifying `options.maxSize` (in bytes) when creating the middleware.\n\n\n## Install\n\n    $ npm install express-asset-file-cache-middleware\n    \nor\n\n    $ yarn add express-asset-file-cache-middleware\n    \n## API\n\n### Input\n\n#### `res.locals.fetchUrl` (required)\n\nThe URL of the asset to cache.\n\n#### `res.locals.cacheKey` (optional)\n\nA unique, expireable cache key. If your asset contains a checksum/digest, you're already done, because it falls back to `res.locals.fetchUrl`.\n\n### Output\n\nTo further process the response, the following entries of `res.locals` are set:\n\n#### `res.locals.buffer`\n\nThe cached asset as a binary buffer. Most likely, you will end the request chain with\n\n```javascript\nres.end(res.locals.buffer, \"binary\");\n```\n\n#### `res.locals.contentType` and `res.locals.contentLength`\n\nIf you're serving your assets in the response, you'll need to set\n\n```javascript\nres.set({\n  \"Content-Type\": res.locals.contentType,\n  \"Content-Length\": res.locals.contentLength\n});\n```    \n    \n## Options\n\nYou can pass the following options to the middleware:\n\n### `cacheDir` (optional)\n\nThe root directory where the file cache will be located. Falls back to `path.join(process.cwd(), \"/tmp\")`.\n\n### `logger` (optional)\n\nA logger to use for debugging, e.g. Winston, console, etc.\n\n### `maxSize` (optional)\nThe maximum size of the cache directory, from which LRU eviction is applied. Defaults to 1 GB (1024 * 1024 * 1024).\n\n\n## Tests\n\nRun the test suite:\n\n```bash\n# install dependencies\n$ npm install\n\n# unit tests\n$ npm test\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2019 Julian Rubisch\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"http://www.julianrubisch.at\"\u003e\u003cimg src=\"https://avatars0.githubusercontent.com/u/4352208?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJulian Rubisch\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/julianrubisch/express-asset-file-cache-middleware/commits?author=julianrubisch\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/memalloc\"\u003e\u003cimg src=\"https://avatars2.githubusercontent.com/u/7209927?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003ememalloc\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/julianrubisch/express-asset-file-cache-middleware/pulls?q=is%3Apr+reviewed-by%3Amemalloc\" title=\"Reviewed Pull Requests\"\u003e👀\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-enable --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulianrubisch%2Fexpress-asset-file-cache-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjulianrubisch%2Fexpress-asset-file-cache-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulianrubisch%2Fexpress-asset-file-cache-middleware/lists"}