This commit is contained in:
Sebastian Seedorf
2020-11-16 10:01:58 +01:00
parent 7fbc95f86a
commit 2629d1e8e9
55 changed files with 4904 additions and 0 deletions

43
src/polyfill.ts Normal file
View File

@@ -0,0 +1,43 @@
import {RequestHandler} from 'express';
import * as polyfillLibrary from 'polyfill-library';
import {PolyfillFeatureList, PolyfillOptions} from 'polyfill-library';
import {DefaultConfig, Logger, WaitForSync} from '.';
import {spawn, Thread, Worker} from 'threads';
import {WorkerFunction} from 'threads/dist/types/worker';
const features = new WaitForSync<PolyfillFeatureList>();
(async () => {
const worker = await spawn<WorkerFunction>(new Worker("./polyfill-worker"));
const feats = await worker() as PolyfillFeatureList;
await Thread.terminate(worker);
return feats;
})()
.then(feats => {
feats["fetch"] = {};
Logger.debug("Polyfill analysed:", Object.keys(feats));
features.setData(feats);
})
.catch(err => features.setError(err));
function getRouter(opts?: Partial<PolyfillOptions>): RequestHandler {
const options: Partial<PolyfillOptions> = {
minify: DefaultConfig.isProduction,
unknown: "polyfill",
...opts
}
return async (req, res) => {
const polyfillBundle = await polyfillLibrary.getPolyfillString({
...options,
uaString: req.header("user-agent"),
features: await features.resolve(),
stream: false,
});
res.setHeader('Content-Type', 'text/javascript');
res.send(polyfillBundle);
};
}
export const Polyfill = {
getRouter: getRouter,
};