|
1 | 1 | # express-asset-file-cache-middleware
|
2 | 2 |
|
3 |
| - |
| 3 | + |
4 | 4 |
|
5 | 5 | A modest express.js middleware to locally cache assets (images, videos, audio, etc.) for faster access and proxying, for use in e.g. Electron apps.
|
6 | 6 |
|
7 | 7 | ## TL;DR
|
| 8 | + |
| 9 | +For offline use of dynamic assets, e.g. in your Electron app or local express server. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```javascript |
| 14 | +const express = require("express"); |
| 15 | +const fileCacheMiddleware = require("express-asset-file-cache-middleware"); |
| 16 | + |
| 17 | +const app = express(); |
| 18 | + |
| 19 | +app.get( |
| 20 | + "/assets/:asset_id", |
| 21 | + async (req, res, next) => { |
| 22 | + res.locals.fetchUrl = `https://cdn.example.org/path/to/actual/asset/${req.params.asset_id}`; |
| 23 | + |
| 24 | + res.locals.cacheKey = `${someExpirableUniqueKey}`; |
| 25 | + next(); |
| 26 | + }, |
| 27 | + fileCacheMiddleware({ cacheDir: "/tmp" }), |
| 28 | + (req, res) => { |
| 29 | + res.set({ |
| 30 | + "Content-Type": res.locals.contentType, |
| 31 | + "Content-Length": res.locals.contentLength |
| 32 | + }); |
| 33 | + res.end(res.locals.buffer, "binary"); |
| 34 | + } |
| 35 | +); |
| 36 | + |
| 37 | +app.listen(3000); |
| 38 | +``` |
| 39 | + |
| 40 | +It 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. |
| 41 | + |
| 42 | +Currently even when the file is present on disk, a `HEAD` request is necessary to determine `contentType` and `contentLength`, otherwise the response will fail (I will potentially mitigate this by optionally using an SQLite database, for _true_ offline use). |
| 43 | + |
| 44 | +Note that setting `cacheKey` and `cacheDir` isn't strictly necessary, it will fall back to `res.local.fetchUrl` and `path.join(process.cwd(), "/tmp")`, respectively. |
| 45 | + |
| 46 | +## Install |
| 47 | + |
| 48 | + $ npm install express-asset-file-cache-middleware |
| 49 | + |
| 50 | +or |
| 51 | + |
| 52 | + $ yarn add express-asset-file-cache-middleware |
| 53 | + |
| 54 | +## API |
| 55 | + |
| 56 | +### Input |
| 57 | + |
| 58 | +#### `res.locals.fetchUrl` (required) |
| 59 | + |
| 60 | +The URL of the asset to cache. |
| 61 | + |
| 62 | +#### `res.locals.cacheKey` (optional) |
| 63 | + |
| 64 | +A unique, expireable cache key. If your asset contains a checksum/digest, you're already done, because it falls back to `res.locals.fetchUrl`. |
| 65 | + |
| 66 | +### Output |
| 67 | + |
| 68 | +To further process the response, the following entries of `res.locals` are set: |
| 69 | + |
| 70 | +#### `res.locals.buffer` |
| 71 | + |
| 72 | +The cached asset as a binary buffer. Most likely, you will end the request chain with |
| 73 | + |
| 74 | +```javascript |
| 75 | +res.end(res.locals.buffer, "binary"); |
| 76 | +``` |
| 77 | + |
| 78 | +#### `res.locals.contentType` and `res.locals.contentLength` |
| 79 | + |
| 80 | +If you're serving your assets in the response, you'll need to set |
| 81 | + |
| 82 | +```javascript |
| 83 | +res.set({ |
| 84 | + "Content-Type": res.locals.contentType, |
| 85 | + "Content-Length": res.locals.contentLength |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +## Options |
| 90 | + |
| 91 | +You can pass the following options to the middleware: |
| 92 | + |
| 93 | +### `cacheDir` (optional) |
| 94 | + |
| 95 | +The root directory where the file cache will be located. Falls back to `path.join(process.cwd(), "/tmp")`. |
| 96 | + |
| 97 | +### `logger` (optional) |
| 98 | + |
| 99 | +A logger to use for debugging, e.g. Winston, console, etc. |
| 100 | + |
| 101 | +## Tests |
| 102 | + |
| 103 | +Run the test suite: |
| 104 | + |
| 105 | +```bash |
| 106 | +# install dependencies |
| 107 | +$ npm install |
| 108 | + |
| 109 | +# unit tests |
| 110 | +$ npm test |
| 111 | +``` |
| 112 | + |
| 113 | +## License |
| 114 | + |
| 115 | +The MIT License (MIT) |
| 116 | + |
| 117 | +Copyright (c) 2019 Julian Rubisch |
0 commit comments