inital #2
This commit is contained in:
43
src/polyfill.ts
Normal file
43
src/polyfill.ts
Normal 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,
|
||||
};
|
||||
Reference in New Issue
Block a user