Skip to content

Commit

Permalink
separate static client scripts from config
Browse files Browse the repository at this point in the history
also only process html and add some todos
and comment updates and minor code simplification
  • Loading branch information
nfriedly committed Mar 26, 2021
1 parent b0d8cc4 commit 3907fbd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
35 changes: 16 additions & 19 deletions lib/client-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,47 @@ 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 = `
<script>var unblocker=${JSON.stringify({ prefix })}</script>
<script src="${clientScriptPathWeb}"></script>`;

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 = `<script src="${clientScriptPath}"></script>`;

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(/(<head[^>]*>)/i, "$1\n" + CLIENT_SCRIPT_INIT + "\n");
.replace(/(<head[^>]*>)/i, "$1" + INJECTION_SNIPET + "\n");
this.push(updated, "utf8");
next();
},
});
}

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());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -108,9 +117,9 @@
};
}

initWebsockets(config);
initXMLHttpRequest(config);
initCreateElement(config);
initWebsockets(unblocker);
initXMLHttpRequest(unblocker);
initCreateElement(unblocker);

console.log("unblocker client scripts initialized");

Expand Down
7 changes: 3 additions & 4 deletions lib/unblocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -284,7 +284,6 @@ function Unblocker(config) {
}

// websocket support
// todo: dedupe
const proxyWebsocket = Unblocker.websockets(config);

handleRequest.onUpgrade = function onUpgrade(
Expand Down
2 changes: 1 addition & 1 deletion test/client/fix-url-spec.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 3907fbd

Please sign in to comment.