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

feat: Add request option to skip PassThrough #863

Merged
merged 3 commits into from
Nov 3, 2023
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
11 changes: 11 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ sdk.configure({
});
```

## Disable stream PassThrough for streaming responses

By default the SDK uses `PassThrough` stream to handle streaming responses. It helps to support delayed reading of the response body, but in some cases it can cause memory leaks or not fully works. To disable `PassThrough` stream use the `disableStreamPassThrough` method:

```javascript
sdk = BoxSDKNode.getPreconfiguredInstance(APP_SETTINGS);
sdk.configure({
disableStreamPassThrough: true
});
```

## Configure Base URL

The Base Url is the URL that is used by the SDK to access Box. The default base URL are already defined
Expand Down
6 changes: 5 additions & 1 deletion src/api-request-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ class APIRequestManager {
// available before we can pipe it to the pass-through stream.
// If the stream is undefined, then the request failed and we should
// propagate the error.
if (stream) {
if (
stream &&
requestConfig.disableStreamPassThrough !== true &&
options.disableStreamPassThrough !== true
) {
var passThrough = new PassThrough();
stream.pipe(passThrough);
return passThrough;
Expand Down
1 change: 1 addition & 0 deletions src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var defaults = {
iterators: false,
enterpriseID: undefined,
analyticsClient: null,
disableStreamPassThrough: false,
proxy: {
url: null,
username: null,
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/api-request-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ describe('APIRequestManager', function() {
.withExactArgs()
.returns(expectedResponse);
response = requestManager.makeStreamingRequest({});
assert.instanceOf(response, Stream.PassThrough);
assert.equal(typeof response, typeof expectedResponse);
});

it('should return the Stream directly when `disableStreamPassThrough` is true', function() {
var requestManager = new APIRequestManager(config, eventBusFake),
expectedResponse = new Stream(),
response;

sandbox.stub(apiRequestFake, 'execute');
sandbox.mock(apiRequestFake).expects('getResponseStream')
.withExactArgs()
.returns(expectedResponse);
response = requestManager.makeStreamingRequest({ disableStreamPassThrough: true });
assert.notInstanceOf(response, Stream.PassThrough);
assert.equal(typeof response, typeof expectedResponse);
});
});
Expand Down