40 lines
2.0 KiB
JavaScript
40 lines
2.0 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Polyfill = void 0;
|
|
const polyfillLibrary = require("polyfill-library");
|
|
const _1 = require(".");
|
|
const threads_1 = require("threads");
|
|
function getRouter(fileToWatch, opts) {
|
|
const features = new _1.WaitForSync();
|
|
(() => __awaiter(this, void 0, void 0, function* () {
|
|
const worker = yield threads_1.spawn(new threads_1.Worker('./polyfill-worker'));
|
|
const feats = yield worker(fileToWatch);
|
|
yield threads_1.Thread.terminate(worker);
|
|
return feats;
|
|
}))()
|
|
.then(feats => {
|
|
feats['fetch'] = {};
|
|
_1.Logger.debug('Polyfill analysed:', Object.keys(feats));
|
|
features.setData(feats);
|
|
})
|
|
.catch(err => features.setError(err));
|
|
const options = Object.assign({ minify: _1.DefaultConfig.isProduction, unknown: 'polyfill' }, opts);
|
|
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
const polyfillBundle = yield polyfillLibrary.getPolyfillString(Object.assign(Object.assign({}, options), { uaString: req.header('user-agent'), features: yield features.resolve(), stream: false }));
|
|
res.setHeader('Content-Type', 'text/javascript');
|
|
res.send(polyfillBundle);
|
|
});
|
|
}
|
|
exports.Polyfill = {
|
|
getRouter: getRouter,
|
|
};
|