UNPKG

http2-express-autopush

Version:
98 lines (97 loc) 3.74 kB
"use strict"; ///<reference types='express'/> var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path = __importStar(require("path")); const fs = __importStar(require("fs")); const serve_static_1 = __importDefault(require("serve-static")); const h2Auto = __importStar(require("h2-auto-push")); const cookie = __importStar(require("cookie")); function isHttp2Request(req) { return !!req.stream; } function isHttp2Response(res) { return !!res.stream; } function isStatic(url, root) { try { const filePath = path.join(root, url); const stat = fs.statSync(filePath); return stat.isFile(); } catch (err) { return false; } } const CACHE_COOKIE_KEY = '__ap_cache__'; function serveAutoPush(root, staticOptions, cacheConfig) { if (!root) { throw Error('root path required'); } if (typeof root !== 'string') { throw Error('root path must be a string'); } const rootDir = path.resolve(root); let ap; if (cacheConfig) { ap = new h2Auto.AutoPush(root, cacheConfig); } else { ap = new h2Auto.AutoPush(root); } return [ async function (req, res, next) { if (isHttp2Request(req) && isHttp2Response(res)) { try { const reqPath = req.url; const reqStream = req.stream; const cookies = cookie.parse(req.headers['cookie'] || ''); const cacheKey = cookies[CACHE_COOKIE_KEY]; const { newCacheCookie, pushFn } = await ap.preprocessRequest(reqPath, reqStream, cacheKey); res.setHeader('set-cookie', cookie.serialize(CACHE_COOKIE_KEY, newCacheCookie, { path: '/' })); reqStream.on('pushError', err => { console.error('Error while pushing', err); }); const resStream = res.stream; if (isStatic(req.url, rootDir)) { ap.recordRequestPath(resStream.session, reqPath, true); } else { ap.recordRequestPath(resStream.session, reqPath, false); } pushFn().then(noop, noop); } catch (err) { console.error('Error while autopush', err); } } next(); }, serve_static_1.default(rootDir, staticOptions) ]; } exports.default = serveAutoPush; function noop() { } module.exports = serveAutoPush; module.exports.default = serveAutoPush;