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

Node renderer fixes #510

Merged
merged 5 commits into from May 29, 2017
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Fall back to old Buffer API

  • Loading branch information
mwiencek committed May 16, 2017
commit 43a385dad829b53737dd4f541411c828f3eda4a7
@@ -0,0 +1,37 @@
// This file is part of MusicBrainz, the open internet music database.
// Copyright (C) 2017 MetaBrainz Foundation
// Licensed under the GPL version 2, or (at your option) any later version:
// http://www.gnu.org/licenses/gpl-2.0.txt


// Buffer.from apparently exists in some Nodes <= 4.5.0, but is broken,
// throwing "this is not a typed array" if you pass it a string.
//
// Buffer.from doesn't exist at all in versions <= 0.12.
//
// Buffer.allocUnsafe is not known to be broken in any versions, but doesn't
// exist before v5.10 according to the documentation.

try {
Buffer.from('');
exports.bufferFrom = Buffer.from;
} catch (e) {
exports.bufferFrom = function (string) {
if (typeof string !== 'string') {
throw new TypeError('expected a string');
}
return new Buffer(string);
};
}

try {
Buffer.allocUnsafe(1);
exports.allocBuffer = Buffer.allocUnsafe;
} catch (e) {
exports.allocBuffer = function (size) {
if (typeof size !== 'number') {
throw new TypeError('expected a number');
}
return new Buffer(size);
};
}
@@ -7,6 +7,7 @@ const net = require('net');
const Raven = require('raven');

const DBDefs = require('../static/scripts/common/DBDefs');
const {allocBuffer} = require('./buffer');
const {badRequest, getResponse} = require('./response');
const {clearRequireCache} = require('./utils');

@@ -27,7 +28,7 @@ const connectionListener = Raven.wrap(function (socket) {
function receiveData(data) {
if (!recvBuffer) {
expectedBytes = data.readUInt32LE(0);
recvBuffer = Buffer.allocUnsafe(expectedBytes);
recvBuffer = allocBuffer(expectedBytes);
data = data.slice(4);
}

@@ -81,7 +82,7 @@ const connectionListener = Raven.wrap(function (socket) {
});

function writeResponse(socket, body) {
const lengthBuffer = Buffer.allocUnsafe(4);
const lengthBuffer = allocBuffer(4);
lengthBuffer.writeUInt32LE(Buffer.byteLength(body), 0);
socket.write(lengthBuffer);
socket.write(body);
@@ -12,14 +12,15 @@ const ReactDOMServer = require('react-dom/server');
const DBDefs = require('../static/scripts/common/DBDefs');
const i18n = require('../static/scripts/common/i18n');
const getCookie = require('../static/scripts/common/utility/getCookie');
const {bufferFrom} = require('./buffer');
const gettext = require('./gettext');

function pathFromRoot(fpath) {
return path.resolve(__dirname, '../../', fpath);
}

function badRequest(err) {
return Buffer.from(JSON.stringify({
return bufferFrom(JSON.stringify({
body: err.stack,
content_type: 'text/plain',
status: 400,
@@ -74,7 +75,7 @@ function getResponse(requestBody, context) {
return badRequest(err);
}

return Buffer.from(JSON.stringify({
return bufferFrom(JSON.stringify({
body: response,
content_type: 'text/html',
status,
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.