From acd95251a15eaa5667b1cc6317abe4602894ea84 Mon Sep 17 00:00:00 2001 From: Alex Rattray Date: Fri, 9 Aug 2019 14:13:36 -0700 Subject: [PATCH] Refactor requestDataProcessor for File out into its own file --- ...MultipartDataGenerator.js => multipart.js} | 49 +++++++++++++++- lib/resources/Files.js | 57 +------------------ 2 files changed, 48 insertions(+), 58 deletions(-) rename lib/{MultipartDataGenerator.js => multipart.js} (51%) diff --git a/lib/MultipartDataGenerator.js b/lib/multipart.js similarity index 51% rename from lib/MultipartDataGenerator.js rename to lib/multipart.js index 9c819bd7a5..f4d429b1aa 100644 --- a/lib/MultipartDataGenerator.js +++ b/lib/multipart.js @@ -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(); @@ -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; diff --git a/lib/resources/Files.js b/lib/resources/Files.js index 990de87750..eea7ffa509 100644 --- a/lib/resources/Files.js +++ b/lib/resources/Files.js @@ -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',