-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caching support #31
Comments
Hey, This isn't supported and likely wouldn't be since there'd be no cross-platform solution ( const fs = require('fs');
const { join } = require('path');
const { send } = require('httpie');
const CACHE = join(__dirname, '...');
function toFilename(method, path) {
return `${method}__${encodeURIComponent(path)}.txt`;
}
async function isFileFresh(file) {
let now = Date.now();
let stats = await fs.stat(file);
return now < +stats.mtime; // custom "mtime" value
}
async function write(file, res) {
const { statusCode, headers, body='' } = res;
const output = { statusCode, headers, body };
// spec-compliance: status range, Cache-Control values, etc
if (!can_cache(statusCode, headers)) return output;
await fs.promises.writeFile(file, JSON.stringify(output));
const secs = get_maxage(headers['cache-control']);
const now = Date.now(), expires = now + secs * 1e3;
// use `mtime` for cache-expiration date
// ~> only auto-set via mknod, write, utimes methods (1)
await fs.promises.utimes(file, now, expires);
return output;
}
async function fetch(method, path, headers={}, body='') {
// checking file system
let file = join(CACHE, toFilename(method, path));
// spec-compliant checks; eg "no-cache" header?
if (can_reuse_cache(headers) && fs.existsSync(file) && await isFileFresh(file)) {
return read_and_parse(file); // read file, sending { status, headers, body }
}
return send(method, path, { headers, body }).then(res => {
return write(file, res);
});
} ^ This is completely untested, haha. All functions with underscores are left unimplemented intentionally – their needs are well-defined via MDN/specs. The If I ever run into this need and have to make something, I'll try to remember to circle back here. But for the most part, I just rely on network cache and the browsers' local caches. Hope that helps~ |
@lukeed thanks for a detailed response! btw as a thought, there could be a separate file for node with caching, that could be used like this: import httpie from 'httpie/caching' Using (just a thought) but I agree it better be a separate module |
I have separate entries already, and they're all focused on being API-compatible with one another. It wouldn't make sense to add something that one supported one platform. Plus, I forgot to mention earlier, but |
Hello, I have a question regarding caching. Is it supported, e.g. saving stuff to files not to make new requests?
Atm I'm using node-fetch-cache but would like to switch to httpie
The text was updated successfully, but these errors were encountered: