page-with
Version:
A library for usage example-driven in-browser testing of your own libraries.
28 lines (27 loc) • 1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.staticFromMemory = void 0;
const path = require("path");
const createLogger_1 = require("../internal/createLogger");
const log = (0, createLogger_1.createLogger)('staticFromMemory');
function staticFromMemory(ifs) {
return (req, res) => {
const filePath = path.join('dist', req.url);
log('reading file "%s"...', filePath);
if (!ifs.existsSync(filePath)) {
log('asset "%s" not found in memory', filePath);
return res.status(404).end();
}
const stream = ifs.createReadStream(filePath, 'utf8');
stream.pipe(res);
stream.on('error', (error) => {
log('error while reading "%s" from memory', filePath);
console.error(error);
});
stream.on('end', () => {
log('successfully read the file!', filePath);
res.end();
});
};
}
exports.staticFromMemory = staticFromMemory;