Skip to content

Commit

Permalink
feat: support custom highWaterMark
Browse files Browse the repository at this point in the history
closes #435
  • Loading branch information
fengmk2 committed May 21, 2023
1 parent 39219e6 commit a8d81e0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ console.log('status: %s, body size: %d, headers: %j', res.status, data.length, r
- ***compressed*** Boolean - Accept `gzip, br` response content and auto decode it, default is `false`.
- ***timing*** Boolean - Enable timing or not, default is `false`.
- ***socketPath*** String | null - request a unix socket service, default is `null`.
- ***highWaterMark*** Number - default is `67108864`, 64 KiB.

#### Options: `options.data`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@types/busboy": "^1.5.0",
"@types/default-user-agent": "^1.0.0",
"@types/mime-types": "^2.1.1",
"@types/node": "^18.11.18",
"@types/node": "^20.2.1",
"@types/pump": "^1.1.1",
"@types/selfsigned": "^2.0.1",
"@types/tar-stream": "^2.2.2",
Expand Down
4 changes: 3 additions & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,12 @@ export class HttpClient extends EventEmitter {
opaque: internalOpaque,
dispatcher: args.dispatcher ?? this.#dispatcher,
};
if (typeof args.highWaterMark === 'number') {
requestOptions.highWaterMark = args.highWaterMark;
}
if (typeof args.reset === 'boolean') {
requestOptions.reset = args.reset;
}

if (args.followRedirect === false) {
requestOptions.maxRedirections = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ export type RequestOptions = {
* request dispatcher, default is getGlobalDispatcher()
*/
dispatcher?: Dispatcher;

/**
* unix domain socket file path
*/
socketPath?: string | null;
/** Whether the request should stablish a keep-alive or not. Default `undefined` */
reset?: boolean;
/** Default: `64 KiB` */
highWaterMark?: number;
};
28 changes: 28 additions & 0 deletions test/options.streaming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,32 @@ describe('options.streaming.test.ts', () => {
const bytes = await readableToBytes(response.res);
assert.equal(bytes.length, 1024102400);
});

it('should save big streaming response with highWaterMark', async () => {
let start = Date.now();
const size = 1024 * 1024 * 100;
const response1 = await urllib.request(`${_url}mock-bytes?size=${size}`, {
streaming: true,
// highWaterMark: 64 * 1024,
});
assert.equal(response1.status, 200);
assert(isReadable(response1.res as any));
const bytes1 = await readableToBytes(response1.res);
assert.equal(bytes1.length, size);
const use1 = Date.now() - start;
console.log('highWaterMark 64KB use %dms', use1);

start = Date.now();
const response2 = await urllib.request(`${_url}mock-bytes?size=${size}`, {
streaming: true,
highWaterMark: 128 * 1024,
});
assert.equal(response2.status, 200);
assert(isReadable(response2.res as any));
const bytes2 = await readableToBytes(response2.res);
assert.equal(bytes2.length, size);
const use2 = Date.now() - start;
console.log('highWaterMark 128KB use %dms', use2);
assert(use2 < use1);
});
});

0 comments on commit a8d81e0

Please sign in to comment.