Skip to content

Commit

Permalink
fix(jsbattle-server): fix serving images
Browse files Browse the repository at this point in the history
  • Loading branch information
jamro committed Nov 27, 2019
1 parent 2b7b330 commit c47f881
Show file tree
Hide file tree
Showing 5 changed files with 8,810 additions and 316 deletions.
1 change: 0 additions & 1 deletion packages/jsbattle-server/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ module.exports = {
"require-atomic-updates": "error",
"require-await": "error",
"require-jsdoc": "off",
"require-unicode-regexp": "error",
"rest-spread-spacing": "error",
"semi": "off",
"semi-spacing": "error",
Expand Down
88 changes: 88 additions & 0 deletions packages/jsbattle-server/app/lib/stringReplaceMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const hijackResponse = require('hijackresponse');
const Transform = require('stream').Transform;
const escapeStringRegexp = require('escape-string-regexp');

const defaultOptions = {
contentTypeFilterRegexp: /^text\/|^application\/json$|^application\/xml$/,
}

module.exports = (replacements, options = defaultOptions ) => (req, res, next) => {
hijackResponse(res, (err, res) => {
const contentType = res.get('content-type');
if (options.contentTypeFilterRegexp.test(contentType)) {
if (err) {
res.unhijack();
return next(err);
}
res.removeHeader('Content-Length');
res
.pipe(stringReplaceStream(replacements))
.pipe(res);
} else {
return res.unhijack();
}
});
next();
};

function stringReplaceStream(replacements) {
const replacers = [];
const replace = (haystack, replacers, replaceBefore) => {
const getBody = (haystack) => haystack.slice(0, replaceBefore);
const tail = haystack.slice(replaceBefore);
replacers.forEach((replacer) => {
if (!replacer.matcher.test(haystack)) {
return;
}
haystack = getBody(haystack).replace(replacer.matcher, replacer.replace) + tail;
});
return [
getBody(haystack),
tail
];
};
let tail = '';
let maxSearchLength = 0;

Object.keys(replacements)
.sort((str1, str2) => str2.length - str1.length)
.forEach((search) => {
maxSearchLength = Math.max(maxSearchLength, search.length);
replacers.push({
matcher: new RegExp(escapeStringRegexp(search), 'gmi'),
replace: replacements[search]
});
});

function transform(buf, enc, cb) {
const replaceBefore = maxSearchLength * 2;
let haystack = tail + buf.toString('utf8');
let body = '';

if (haystack.length + 2 < maxSearchLength * 3) {
tail = haystack;
cb(null, '');
return;
}

[
body,
tail
] = replace(haystack, replacers, replaceBefore);

cb(null, body);
}

function flush(cb) {
if (tail) {
const result = replace(tail, replacers, tail.length);
this.push(result[0]);
}
cb();
}

return new Transform({
transform: transform,
flush: flush
});
}
2 changes: 1 addition & 1 deletion packages/jsbattle-server/app/services/Gateway.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express');
const stringReplace = require('string-replace-middleware');
const stringReplace = require('../lib/stringReplaceMiddleware.js');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
Expand Down
Loading

0 comments on commit c47f881

Please sign in to comment.