Skip to content

Commit

Permalink
Refactor requestDataProcessor for File out into its own file (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex-stripe authored Aug 9, 2019
1 parent 7eacf49 commit 8fbbb01
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 58 deletions.
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

0 comments on commit 8fbbb01

Please sign in to comment.