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

Refactor requestDataProcessor for File out into its own file #674

Merged
merged 1 commit into from
Aug 9, 2019
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
49 changes: 46 additions & 3 deletions lib/MultipartDataGenerator.js → lib/multipart.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

const utils = require('./utils');
const {StripeError} = require('./Error');

class StreamProcessingError extends StripeError {}

// Method for formatting HTTP body for the multipart/form-data specification
// Mostly taken from Fermata.js
// https://github.com/natevw/fermata/blob/5d9732a33d776ce925013a265935facd1626cc88/fermata.js#L315-L343
function multipartDataGenerator(method, data, headers) {
const multipartDataGenerator = (method, data, headers) => {
const segno = (
Math.round(Math.random() * 1e16) + Math.round(Math.random() * 1e16)
).toString();
Expand Down Expand Up @@ -46,6 +49,46 @@ function multipartDataGenerator(method, data, headers) {
push(`--${segno}--`);

return buffer;
}
};

const streamProcessor = (method, data, headers, callback) => {
const bufferArray = [];
data.file.data
.on('data', (line) => {
bufferArray.push(line);
})
.once('end', () => {
const bufferData = Object.assign({}, data);
bufferData.file.data = Buffer.concat(bufferArray);
const buffer = multipartDataGenerator(method, bufferData, headers);
callback(null, buffer);
})
.on('error', (err) => {
callback(
new StreamProcessingError({
message:
'An error occurred while attempting to process the file for upload.',
detail: err,
}),
null
);
});
};

const multipartRequestDataProcessor = (method, data, headers, callback) => {
data = data || {};

if (method !== 'POST') {
return callback(null, utils.stringifyRequestData(data));
}

const isStream = utils.checkForStream(data);
if (isStream) {
return streamProcessor(method, data, headers, callback);
}

const buffer = multipartDataGenerator(method, data, headers);
return callback(null, buffer);
};

module.exports = multipartDataGenerator;
module.exports.multipartRequestDataProcessor = multipartRequestDataProcessor;
57 changes: 2 additions & 55 deletions lib/resources/Files.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,15 @@
'use strict';

const utils = require('../utils');
const multipartDataGenerator = require('../MultipartDataGenerator');
const {StripeError} = require('../Error');
const {multipartRequestDataProcessor} = require('../multipart');
const StripeResource = require('../StripeResource');
const stripeMethod = StripeResource.method;

class StreamProcessingError extends StripeError {}

module.exports = StripeResource.extend({
path: 'files',

includeBasic: ['list', 'retrieve'],

requestDataProcessor(method, data, headers, callback) {
data = data || {};

if (method === 'POST') {
return getProcessorForSourceType(data);
} else {
return callback(null, utils.stringifyRequestData(data));
}

function getProcessorForSourceType(data) {
const isStream = utils.checkForStream(data);
if (isStream) {
return streamProcessor(multipartDataGenerator);
} else {
const buffer = multipartDataGenerator(method, data, headers);
return callback(null, buffer);
}
}

function streamProcessor(fn) {
const bufferArray = [];
data.file.data
.on('data', (line) => {
bufferArray.push(line);
})
.once('end', () => {
const bufferData = Object.assign({}, data);
bufferData.file.data = Buffer.concat(bufferArray);
const buffer = fn(method, bufferData, headers);
callback(null, buffer);
})
.on('error', (err) => {
const errorHandler = streamError(callback);
errorHandler(err);
});
}

function streamError(callback) {
return (error) => {
callback(
new StreamProcessingError({
message:
'An error occurred while attempting to process the file for upload.',
detail: error,
}),
null
);
};
}
},
requestDataProcessor: multipartRequestDataProcessor,

create: stripeMethod({
method: 'POST',
Expand Down