diff --git a/lib/multipart.js b/lib/multipart.js index 27415a83b2..51ce8b2f4c 100644 --- a/lib/multipart.js +++ b/lib/multipart.js @@ -1,7 +1,5 @@ "use strict"; const utils = require("./utils"); -const _Error = require("./Error"); -const { StripeError } = _Error; // 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 diff --git a/lib/platform/WebPlatformFunctions.js b/lib/platform/WebPlatformFunctions.js index 97219f30bd..9b57b1f950 100644 --- a/lib/platform/WebPlatformFunctions.js +++ b/lib/platform/WebPlatformFunctions.js @@ -36,8 +36,7 @@ class WebPlatformFunctions extends PlatformFunctions { } /** @override */ tryBufferData(data) { - if (data.file.data.pipeThrough && - typeof data.file.data.pipeThrough === 'function') { + if (data.file.data instanceof ReadableStream) { throw new Error('Uploading a file as a stream is not supported in non-Node environments. Please open or upvote an issue at github.com/stripe/stripe-node if you use this, detailing your use-case.'); } return Promise.resolve(data); diff --git a/src/multipart.ts b/src/multipart.ts index f76eb97b6f..29abbbb972 100644 --- a/src/multipart.ts +++ b/src/multipart.ts @@ -1,7 +1,5 @@ import utils = require('./utils'); import _Error = require('./Error'); -import PlatformFunctions = require('./platform/PlatformFunctions'); -const {StripeError} = _Error; type MultipartCallbackReturn = any; type MultipartCallback = ( diff --git a/src/platform/WebPlatformFunctions.ts b/src/platform/WebPlatformFunctions.ts index d3955b3d36..221de216a4 100644 --- a/src/platform/WebPlatformFunctions.ts +++ b/src/platform/WebPlatformFunctions.ts @@ -1,6 +1,4 @@ -import EventEmitter = require('events'); import StripeEmitter = require('../StripeEmitter'); -import {emitWarning} from '../utils'; import PlatformFunctions = require('./PlatformFunctions'); /** @@ -44,10 +42,7 @@ class WebPlatformFunctions extends PlatformFunctions { /** @override */ tryBufferData(data: any): Promise { - if ( - data.file.data.pipeThrough && - typeof data.file.data.pipeThrough === 'function' - ) { + if (data.file.data instanceof ReadableStream) { throw new Error( 'Uploading a file as a stream is not supported in non-Node environments. Please open or upvote an issue at github.com/stripe/stripe-node if you use this, detailing your use-case.' ); diff --git a/test/PlatformFunctions.spec.ts b/test/PlatformFunctions.spec.ts index a24606a385..523133b066 100644 --- a/test/PlatformFunctions.spec.ts +++ b/test/PlatformFunctions.spec.ts @@ -2,6 +2,8 @@ require('../testUtils'); +import fs = require('fs'); +import path = require('path'); import NodePlatformFunctions = require('../lib/platform/NodePlatformFunctions'); import PlatformFunctions = require('../lib/platform/PlatformFunctions'); @@ -276,5 +278,76 @@ for (const platform in platforms) { done(); }); }); + + describe('tryBufferData', () => { + if (!isNodeEnvironment) { + // WebPlatformFunctions + it('should throw an error in web environments if streaming data is provided', () => { + if (process.versions.node < '18') { + console.log( + `'ReadableStream' is not available in the global scope for ${process.version}, skipping test.` + ); + return; + } + + const f = new ReadableStream(); + const data = { + file: { + data: f, + name: 'test.pdf', + type: 'application/octet-stream', + }, + }; + + expect(() => { + platformFunctions.tryBufferData(data); + }).to.throw(); + }); + } else { + // NodePlatformFunctions + it('should return data unmodified if not a file stream', () => { + const testFilename = path.join( + __dirname, + 'resources/data/minimal.pdf' + ); + const buf = fs.readFileSync(testFilename); + + // Create Uint8Array from Buffer + const f = new Uint8Array(buf.buffer, buf.byteOffset, buf.length); + + const data = { + file: { + data: f, + name: 'minimal.pdf', + type: 'application/octet-stream', + }, + }; + + expect( + platformFunctions.tryBufferData(data) + ).to.eventually.deep.equal(data); + }); + + it('should load file streams into buffer', () => { + const testFilename = path.join( + __dirname, + 'resources/data/minimal.pdf' + ); + const f = fs.createReadStream(testFilename); + + const data = { + file: { + data: f, + name: 'minimal.pdf', + type: 'application/octet-stream', + }, + }; + + return expect( + platformFunctions.tryBufferData(data).then((d) => d.file.data) + ).to.eventually.have.nested.property('length', 739); + }); + } + }); }); }