Skip to content

Commit a811139

Browse files
Refactor requestDataProcessor for File out into its own file (stripe#674)
1 parent cc03e59 commit a811139

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed

lib/MultipartDataGenerator.js lib/multipart.js

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict';
22

33
const utils = require('./utils');
4+
const {StripeError} = require('./Error');
5+
6+
class StreamProcessingError extends StripeError {}
47

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

4851
return buffer;
49-
}
52+
};
53+
54+
const streamProcessor = (method, data, headers, callback) => {
55+
const bufferArray = [];
56+
data.file.data
57+
.on('data', (line) => {
58+
bufferArray.push(line);
59+
})
60+
.once('end', () => {
61+
const bufferData = Object.assign({}, data);
62+
bufferData.file.data = Buffer.concat(bufferArray);
63+
const buffer = multipartDataGenerator(method, bufferData, headers);
64+
callback(null, buffer);
65+
})
66+
.on('error', (err) => {
67+
callback(
68+
new StreamProcessingError({
69+
message:
70+
'An error occurred while attempting to process the file for upload.',
71+
detail: err,
72+
}),
73+
null
74+
);
75+
});
76+
};
77+
78+
const multipartRequestDataProcessor = (method, data, headers, callback) => {
79+
data = data || {};
80+
81+
if (method !== 'POST') {
82+
return callback(null, utils.stringifyRequestData(data));
83+
}
84+
85+
const isStream = utils.checkForStream(data);
86+
if (isStream) {
87+
return streamProcessor(method, data, headers, callback);
88+
}
89+
90+
const buffer = multipartDataGenerator(method, data, headers);
91+
return callback(null, buffer);
92+
};
5093

51-
module.exports = multipartDataGenerator;
94+
module.exports.multipartRequestDataProcessor = multipartRequestDataProcessor;

lib/resources/Files.js

+2-55
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,15 @@
11
'use strict';
22

3-
const utils = require('../utils');
4-
const multipartDataGenerator = require('../MultipartDataGenerator');
5-
const {StripeError} = require('../Error');
3+
const {multipartRequestDataProcessor} = require('../multipart');
64
const StripeResource = require('../StripeResource');
75
const stripeMethod = StripeResource.method;
86

9-
class StreamProcessingError extends StripeError {}
10-
117
module.exports = StripeResource.extend({
128
path: 'files',
139

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

16-
requestDataProcessor(method, data, headers, callback) {
17-
data = data || {};
18-
19-
if (method === 'POST') {
20-
return getProcessorForSourceType(data);
21-
} else {
22-
return callback(null, utils.stringifyRequestData(data));
23-
}
24-
25-
function getProcessorForSourceType(data) {
26-
const isStream = utils.checkForStream(data);
27-
if (isStream) {
28-
return streamProcessor(multipartDataGenerator);
29-
} else {
30-
const buffer = multipartDataGenerator(method, data, headers);
31-
return callback(null, buffer);
32-
}
33-
}
34-
35-
function streamProcessor(fn) {
36-
const bufferArray = [];
37-
data.file.data
38-
.on('data', (line) => {
39-
bufferArray.push(line);
40-
})
41-
.once('end', () => {
42-
const bufferData = Object.assign({}, data);
43-
bufferData.file.data = Buffer.concat(bufferArray);
44-
const buffer = fn(method, bufferData, headers);
45-
callback(null, buffer);
46-
})
47-
.on('error', (err) => {
48-
const errorHandler = streamError(callback);
49-
errorHandler(err);
50-
});
51-
}
52-
53-
function streamError(callback) {
54-
return (error) => {
55-
callback(
56-
new StreamProcessingError({
57-
message:
58-
'An error occurred while attempting to process the file for upload.',
59-
detail: error,
60-
}),
61-
null
62-
);
63-
};
64-
}
65-
},
12+
requestDataProcessor: multipartRequestDataProcessor,
6613

6714
create: stripeMethod({
6815
method: 'POST',

0 commit comments

Comments
 (0)