Skip to content

Commit

Permalink
feat: Add request option to skip PassThrough (#863)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Minh Nguyen Cong <mcong@box.com>
  • Loading branch information
rlyonbox and congminh1254 authored Nov 3, 2023
1 parent 5f49464 commit 726db45
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
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

0 comments on commit 726db45

Please sign in to comment.