1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-16 08:28:52 +01:00
pywb/wombat/test/helpers/initServer.js
John Berlin 6437dab1f4 server side rewriting: (#475)
- fixed edge case in jsonP rewriting where no callback name is supplied only ? but body has normal jsonP callback (url = https://geolocation.onetrust.com/cookieconsentpub/v1/geo/countries/EU?callback=?)
  - made the `!self.__WB_pmw` server side inject match the client side one done via wombat
  - added regex's for eval override to JSWombatProxyRules

wombat:
  - added eval override in order to ensure AD network JS does not lockup the browser (cnn.com)
  - added returning a proxied value for window.parent from the window proxy object in order to ensure AD network JS does not lock up browser (cnn.com, boston.com, et. al.)
  - ensured that the read only storageArea property of 'StorageEvents' fired by the custom storage class is present (spec)
  - ensured that userland cannot create new instances of Storage and that localStorage instanceof Storage works with our override (spec)
  - ensured that assignments to SomeElement.[innerHTML|outerHTML], iframe.srcdoc, or style.textContent are more correctly handled in particular doing script.[innerHTML|outerHTML] = <JS>
  - ensured that the test used in the isArgumentsObj function does not throw errors IFF the pages JS has made it so when toString is called (linkedin.com)
  - ensured that Web Worker construction when using the optional options object, the options get supplied to the create worker (spec)
  - ensured that the expected TypeError exception thrown from DomConstructors of overridden interfaces are thrown when their pre-conditions are not met (spec)
  - ensured that wombat worker rewriting skipes data urls which should not be rewritten
  - ensured the bound function returned by the window function is the same on subsequent retrievals fixes #474

  tests:
   - added direct testing of wombat's parts
2019-06-20 19:06:41 -07:00

159 lines
4.9 KiB
JavaScript

const path = require('path');
const fs = require('fs-extra');
const createServer = require('fastify');
const host = '127.0.0.1';
const port = 3030;
const gracefullShutdownTimeout = 50000;
const shutdownOnSignals = ['SIGINT', 'SIGTERM', 'SIGHUP'];
const assetsPath = path.join(__dirname, '..', 'assets');
const httpsSandboxPath = path.join(assetsPath, 'sandbox.html');
const sandboxDirectPath = path.join(assetsPath, 'sandboxDirect.html');
const theyFoundItPath = path.join(assetsPath, 'it.html');
const testPageURL = `http://localhost:${port}/testPage.html`;
const testPageDirectURL = `http://localhost:${port}/testPageDirect.html`;
function promiseResolveReject() {
const prr = { promise: null, resolve: null, reject: null };
prr.promise = new Promise((resolve, reject) => {
let to = setTimeout(
() => reject(new Error('wait for request timed-out')),
15000
);
prr.resolve = () => {
clearTimeout(to);
resolve();
};
prr.reject = reason => {
clearTimeout(to);
reject(reason);
};
});
return prr;
}
/**
* @return {Promise<fastify.FastifyInstance>}
*/
async function initServer() {
const serverOpts = { logger: false };
const requestSubscribers = new Map();
const checkReqSubscribers = (pathName, request, reply) => {
const handler = requestSubscribers.get(pathName);
if (handler) {
handler.resolve(request);
requestSubscribers.delete(pathName);
}
};
const fastify = createServer(serverOpts);
fastify
.get(
'/live/20180803160549wkr_/https://tests.wombat.io/testWorker.js',
async (request, reply) => {
const init = `new WBWombat({'prefix': 'http://localhost:${port}/live/20180803160549', 'prefixMod': 'http://localhost:${port}/live/20180803160549wkr_/', 'originalURL': 'https://tests.wombat.io/testWorker.js'});`;
reply
.code(200)
.type('application/javascript; charset=UTF-8')
.send(
`self.importScripts('/testWorker.js');(function() { self.importScripts('/wombatWorkers.js'); ${init}})();`
);
}
)
.get(
'/live/20180803160549sw_/https://tests.wombat.io/testServiceWorker.js',
(request, reply) => {
reply
.code(200)
.type('application/javascript; charset=UTF-8')
.header(
'Service-Worker-Allowed',
`${address}/live/20180803160549mp_/https://tests.wombat.io/`
)
.send('console.log("hi")');
}
)
.get(
'/live/20180803160549mp_/https://tests.wombat.io/it',
async (request, reply) => {
reply.type('text/html').status(200);
return fs.createReadStream(theyFoundItPath);
}
)
.get(
'/live/20180803160549mp_/https://tests.wombat.io/',
async (request, reply) => {
reply.type('text/html').status(200);
return fs.createReadStream(httpsSandboxPath);
}
)
.get(
'/live/20180803160549mp_/https://tests.direct.wombat.io/',
(request, reply) => {
reply
.type('text/html')
.status(200)
.send(fs.createReadStream(sandboxDirectPath));
}
)
.get(
'/live/20180803160549mp_/https://tests.wombat.io/test',
async (request, reply) => {
reply.type('application/json; charset=utf-8').status(200);
return { headers: request.headers, url: request.raw.originalUrl };
}
)
.decorate('reset', () => {
const error = new Error('Static Server has been reset');
for (const prr of requestSubscribers.values()) {
prr.reject.call(null, error);
}
requestSubscribers.clear();
})
.decorate('stop', () => {
fastify.reset();
return fastify.close();
})
.decorate('testPage', testPageURL)
.decorate('testPageDirect', testPageDirectURL)
.decorate('waitForRequest', route => {
let prr = requestSubscribers.get(route);
if (prr) return prr.promise;
prr = promiseResolveReject();
requestSubscribers.set(route, prr);
return prr.promise;
})
.addHook('onRequest', (request, reply, next) => {
checkReqSubscribers(request.raw.url, request, reply);
// console.log(`${request.raw.method} ${request.raw.url}`);
next();
})
.register(require('fastify-favicon'))
.register(require('fastify-static'), {
root: assetsPath,
etag: false,
lastModified: false
});
shutdownOnSignals.forEach(signal => {
process.once(signal, () => {
setTimeout(() => {
console.error(
`received ${signal} signal, terminate process after timeout of ${gracefullShutdownTimeout}ms`
);
process.exit(1);
}, gracefullShutdownTimeout).unref();
console.log(`received ${signal} signal, triggering close hook`);
fastify.stop().then(() => {
process.exit(0);
});
});
});
const address = await fastify.listen(port, host);
return fastify;
}
module.exports = initServer;