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: modifies the data type detection logic for json-seq #1682

Merged
merged 1 commit into from
Jun 1, 2022
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
24 changes: 17 additions & 7 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,15 +1066,14 @@ Request.prototype._end = function () {
const form = formidable();
parser = form.parse.bind(form);
buffer = true;
} else if (isImageOrVideo(mime)) {
} else if (isBinary(mime)) {
parser = exports.parse.image;
buffer = true; // For backwards-compatibility buffering default is ad-hoc MIME-dependent
} else if (exports.parse[mime]) {
parser = exports.parse[mime];
} else if (type === 'text') {
parser = exports.parse.text;
buffer = buffer !== false;

// everyone wants their own white-labeled json
} else if (isJSON(mime)) {
parser = exports.parse['application/json'];
Expand Down Expand Up @@ -1345,11 +1344,22 @@ function isText(mime) {
return type === 'text' || subtype === 'x-www-form-urlencoded';
}

function isImageOrVideo(mime) {
let type = mime.split('/')[0];
if (type) type = type.toLowerCase().trim();

return type === 'image' || type === 'video';
// This is not a catchall, but a start. It might be useful
// in the long run to have file that includes all binary
// content types from https://www.iana.org/assignments/media-types/media-types.xhtml
function isBinary(mime) {
let [ registry, name ] = mime.split('/');
if (registry) registry = registry.toLowerCase().trim();
if (name) name = name.toLowerCase().trim();
return [
'audio',
'font',
'image',
'video'
].includes(registry) || [
'gz',
'gzip',
].includes(name);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/node/parsers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
exports['application/x-www-form-urlencoded'] = require('./urlencoded');
exports['application/json'] = require('./json');
exports.text = require('./text');
exports['application/json-seq'] = exports.text;

const binary = require('./image');

Expand Down
1 change: 1 addition & 0 deletions test/node/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('exports', () => {
'application/x-www-form-urlencoded',
'application/json',
'text',
'application/json-seq',
'application/octet-stream',
'application/pdf',
'image'
Expand Down
Binary file added test/node/fixtures/test.aac
Binary file not shown.
18 changes: 18 additions & 0 deletions test/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const fs = require('fs');

const assert = require('assert');
const getSetup = require('./support/setup');

const request = require('./support/client');
const binData = fs.readFileSync(`${__dirname}/node/fixtures/test.aac`);

describe('request', function () {
let setup;
Expand Down Expand Up @@ -630,6 +633,21 @@ describe('request', function () {
});
});

it('GET binary data', (next) => {
request
.get(`${uri}/binary-data`)
.buffer()
.end((error, res) => {
try {
assert.ifError(error);
assert.deepEqual(res.body, binData);
next();
} catch (err) {
next(err);
}
});
});

it('GET x-www-form-urlencoded', (next) => {
request.get(`${uri}/foo`).end((error, res) => {
try {
Expand Down
7 changes: 7 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');
let http = require('http');
const multer = require('multer');
const bodyParser = require('body-parser');
Expand Down Expand Up @@ -502,6 +503,12 @@ app.get('/image-as-octets', (request, res) => {
serveImageWithType(res, 'application/octet-stream');
});

app.get('/binary-data', (request, res) => {
const binData = fs.readFileSync(`${__dirname}/../node/fixtures/test.aac`);
res.writeHead(200, { 'Content-type': 'audio/aac' });
res.end(binData, 'binary')
});

app.get('/chunked-json', (request, res) => {
res.set('content-type', 'application/json');
res.set('Transfer-Encoding', 'chunked');
Expand Down