Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Ensure proper handling of multi-byte characters in streams #280

Merged
merged 3 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function createMiddleware({resources}) {
// and LibraryBuilder
if ((!charset || charset === "UTF-8") && rReplaceVersion.test(resourcePath)) {
if (resource._project) {
stream.setEncoding("utf8");
stream = stream.pipe(replaceStream("${version}", resource._project.version));
} else {
log.verbose("Project missing from resource %s", pathname);
Expand Down
141 changes: 141 additions & 0 deletions test/lib/server/middleware/serveResources.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const test = require("ava");
const sinon = require("sinon");
const {Readable, Writable} = require("stream");
const resourceFactory = require("@ui5/fs").resourceFactory;
const serveResourcesMiddleware = require("../../../../lib/middleware/serveResources");
const writeResource = function(writer, path, size, stringContent, project) {
Expand Down Expand Up @@ -157,3 +158,143 @@ fame=stra\\u00dfe`);
});
});
});

test.serial.cb("Check if version replacement is done", (t) => {
const input = "foo ${version} bar";
const expected = "foo 1.0.0 bar";

const resource = {
getPath: sinon.stub().returns("/foo.js"),
getStatInfo: sinon.stub().returns({
ino: 0,
ctime: new Date(),
mtime: new Date(),
size: 1024 * 1024,
isDirectory: function() {
return false;
}
}),
getStream: () => {
const stream = new Readable();
stream.push(Buffer.from(input));
stream.push(null);
return stream;
},
_project: {
version: "1.0.0"
}
};

const resources = {
all: {
byPath: sinon.stub()
}
};
const middleware = serveResourcesMiddleware({
resources
});

resources.all.byPath.withArgs("/foo.js").resolves(resource);

const req = {
url: "/foo.js",
headers: {}
};

const res = new Writable();
const buffers = [];
res.setHeader = sinon.stub();
res.getHeader = sinon.stub();
res._write = function(chunk, encoding, callback) {
buffers.push(chunk);
callback();
};
res.end = function() {
t.is(Buffer.concat(buffers).toString(), expected);
t.end();
},

middleware(req, res, function(err) {
if (err) {
t.fail("Unexpected error passed to next function: " + err);
} else {
t.fail("Unexpected call of next function");
}
t.end();
});
});

// Skip test in Node v8 as unicode handling of streams seems to be broken
test.serial[
process.version.startsWith("v8.") ? "skip" : "cb"
]("Check if utf8 characters are correctly processed in version replacement", (t) => {
const utf8string = "Κυ";
const expected = utf8string;

const resource = {
getPath: sinon.stub().returns("/foo.js"),
getStatInfo: sinon.stub().returns({
ino: 0,
ctime: new Date(),
mtime: new Date(),
size: 1024 * 1024,
isDirectory: function() {
return false;
}
}),
getStream: () => {
const stream = new Readable();
const utf8stringAsBuffer = Buffer.from(utf8string, "utf8");
// Pushing each byte separately makes content unreadable
// if stream encoding is not set to utf8
// This might happen when reading large files with utf8 characters
stream.push(Buffer.from([utf8stringAsBuffer[0]]));
stream.push(Buffer.from([utf8stringAsBuffer[1]]));
stream.push(Buffer.from([utf8stringAsBuffer[2]]));
stream.push(Buffer.from([utf8stringAsBuffer[3]]));
stream.push(null);
return stream;
},
_project: {
version: "1.0.0"
}
};

const resources = {
all: {
byPath: sinon.stub()
}
};
const middleware = serveResourcesMiddleware({
resources
});

resources.all.byPath.withArgs("/foo.js").resolves(resource);

const req = {
url: "/foo.js",
headers: {}
};

const res = new Writable();
const buffers = [];
res.setHeader = sinon.stub();
res.getHeader = sinon.stub();
res._write = function(chunk, encoding, callback) {
buffers.push(chunk);
callback();
};
res.end = function() {
t.is(Buffer.concat(buffers).toString(), expected);
t.end();
},

middleware(req, res, function(err) {
if (err) {
t.fail("Unexpected error passed to next function: " + err);
} else {
t.fail("Unexpected call of next function");
}
t.end();
});
});