diff --git a/lib/client-scripts.js b/lib/client-scripts.js index f3ed1010..c1a8fa06 100644 --- a/lib/client-scripts.js +++ b/lib/client-scripts.js @@ -5,39 +5,39 @@ var path = require("path"); var Transform = require("stream").Transform; var contentTypes = require("./content-types"); -module.exports = function (config) { +module.exports = function ({ prefix }) { const clientDir = "client"; - const clientScriptPath = config.prefix + clientDir + "/unblocker.js"; + const clientScriptPathWeb = prefix + clientDir + "/unblocker-client.js"; + const clientScriptPathFs = path.join( + __dirname, + clientDir, + "unblocker-client.js" + ); - // todo: async - function generateClientScript() { - return fs - .readFileSync(path.join(__dirname, clientDir, "unblocker.template.js")) - .toString() - .replace("PREFIX", config.prefix); - } + const INJECTION_SNIPET = ` + +`; function server(req, res, next) { - if (req.url === clientScriptPath) { - // todo: make this better, at least for NODE_ENV=production + if (req.url === clientScriptPathWeb) { + // todo: etag (always), expires (in production). Maybe use the send module res.writeHead(200, { "content-type": "application/javascript; charset=UTF-8", }); - res.end(generateClientScript()); + fs.createReadStream(clientScriptPathFs).pipe(res); return; } next(); } - const CLIENT_SCRIPT_INIT = ``; - function createStream() { return new Transform({ decodeStrings: false, transform: function (chunk, encoding, next) { + // todo: only inject once (maybe make an "injects into head" helper) var updated = chunk .toString() - .replace(/(]*>)/i, "$1\n" + CLIENT_SCRIPT_INIT + "\n"); + .replace(/(]*>)/i, "$1" + INJECTION_SNIPET + "\n"); this.push(updated, "utf8"); next(); }, @@ -45,10 +45,7 @@ module.exports = function (config) { } function injector(data) { - // todo: catch fetch and XMLHttpRequest and force those through the proxy - // todo: fix postMessage - // config._proxyWebsockets && - if (contentTypes.shouldProcess(config, data)) { + if (contentTypes.html.includes(data.contentType)) { data.stream = data.stream.pipe(createStream()); } } diff --git a/lib/client/unblocker.template.js b/lib/client/unblocker-client.js similarity index 87% rename from lib/client/unblocker.template.js rename to lib/client/unblocker-client.js index 639fc289..d36bbd0d 100644 --- a/lib/client/unblocker.template.js +++ b/lib/client/unblocker-client.js @@ -1,9 +1,18 @@ (function (exports) { "use strict"; + /*global unblocker*/ - console.log("begin unblocker client scripts"); + // todo: + // - fetch + // - history API + // - postMessage + // - open + // - split each part into separate files (?) + // - wrap other JS and provide proxies to fix writes to window.location and document.cookie + // - will require updating contentTypes.html.includes(data.contentType) to include js + // - that, in turn will require decompressing js.... - const config = { prefix: "PREFIX" }; + console.log("begin unblocker client scripts", unblocker); function fixUrl(target, prefix, location) { const currentRemoteHref = @@ -108,9 +117,9 @@ }; } - initWebsockets(config); - initXMLHttpRequest(config); - initCreateElement(config); + initWebsockets(unblocker); + initXMLHttpRequest(unblocker); + initCreateElement(unblocker); console.log("unblocker client scripts initialized"); diff --git a/lib/unblocker.js b/lib/unblocker.js index 8aa5f12b..8116a296 100644 --- a/lib/unblocker.js +++ b/lib/unblocker.js @@ -230,8 +230,8 @@ function Unblocker(config) { url: formatted, uri, rawUrl, - clientRequest: clientRequest, - clientResponse: clientResponse, + clientRequest, + clientResponse, headers: _.cloneDeep(clientRequest.headers), stream: clientRequest, isWebsocket: !!clientSocket, @@ -241,7 +241,7 @@ function Unblocker(config) { return data; } - // todo: make this less confusing + // todo: see if this can be synchronous const clientScriptsServer = config.clientScripts ? clientScripts.server : (req, res, next) => next(); @@ -284,7 +284,6 @@ function Unblocker(config) { } // websocket support - // todo: dedupe const proxyWebsocket = Unblocker.websockets(config); handleRequest.onUpgrade = function onUpgrade( diff --git a/test/client/fix-url-spec.js b/test/client/fix-url-spec.js index f4a04a29..89f73485 100644 --- a/test/client/fix-url-spec.js +++ b/test/client/fix-url-spec.js @@ -1,6 +1,6 @@ "use strict"; const { test } = require("tap"); -const { fixUrl } = require("../../lib/client/unblocker.template.js"); +const { fixUrl } = require("../../lib/client/unblocker-client.js"); const proxy = "http://localhost"; const prefix = "/proxy/"; const target = "http://example.com/page.html?query#hash";