File
Blob: src/node/internal/streams_state.ts
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // |
| 5 | // Copyright Joyent, Inc. and other Node contributors. |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | // copy of this software and associated documentation files (the |
| 9 | // "Software"), to deal in the Software without restriction, including |
| 10 | // without limitation the rights to use, copy, modify, merge, publish, |
| 11 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 12 | // persons to whom the Software is furnished to do so, subject to the |
| 13 | // following conditions: |
| 14 | // |
| 15 | // The above copyright notice and this permission notice shall be included |
| 16 | // in all copies or substantial portions of the Software. |
| 17 | // |
| 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 21 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 22 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | |
| 26 | import { validateInteger } from 'node-internal:validators'; |
| 27 | import { ERR_INVALID_ARG_VALUE } from 'node-internal:internal_errors'; |
| 28 | |
| 29 | let defaultHighWaterMarkBytes = 64 * 1024; |
| 30 | let defaultHighWaterMarkObjectMode = 16; |
| 31 | |
| 32 | export type HighWaterMarkFromOptions = { highWaterMark?: number }; |
| 33 | |
| 34 | function highWaterMarkFrom( |
| 35 | options: HighWaterMarkFromOptions, |
| 36 | isDuplex: boolean, |
| 37 | duplexKey: string |
| 38 | ): number | null { |
| 39 | return options.highWaterMark != null |
| 40 | ? options.highWaterMark |
| 41 | : isDuplex |
| 42 | ? // @ts-expect-error TS7053 Fix this soon. |
| 43 | (options[duplexKey] as number) |
| 44 | : null; |
| 45 | } |
| 46 | |
| 47 | export function getDefaultHighWaterMark(objectMode?: boolean): number { |
| 48 | return objectMode |
| 49 | ? defaultHighWaterMarkObjectMode |
| 50 | : defaultHighWaterMarkBytes; |
| 51 | } |
| 52 | |
| 53 | export function setDefaultHighWaterMark( |
| 54 | objectMode: boolean, |
| 55 | value: unknown |
| 56 | ): void { |
| 57 | validateInteger(value, 'value', 0); |
| 58 | if (objectMode) { |
| 59 | defaultHighWaterMarkObjectMode = value; |
| 60 | } else { |
| 61 | defaultHighWaterMarkBytes = value; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | export function getHighWaterMark( |
| 66 | state: { objectMode?: boolean }, |
| 67 | options: HighWaterMarkFromOptions, |
| 68 | duplexKey: string, |
| 69 | isDuplex: boolean |
| 70 | ): number { |
| 71 | const hwm = highWaterMarkFrom(options, isDuplex, duplexKey); |
| 72 | if (hwm != null) { |
| 73 | if (!Number.isInteger(hwm) || hwm < 0) { |
| 74 | const name = isDuplex ? `options.${duplexKey}` : 'options.highWaterMark'; |
| 75 | throw new ERR_INVALID_ARG_VALUE(name, hwm); |
| 76 | } |
| 77 | return Math.floor(hwm); |
| 78 | } |
| 79 | |
| 80 | // Default value |
| 81 | return getDefaultHighWaterMark(state.objectMode); |
| 82 | } |