-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
234 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './mutex' | ||
export * from './stream' | ||
export * from './async-abort-controller' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Transform, TransformCallback } from 'stream' | ||
|
||
interface ByteCounterStreamOptions { | ||
maxHistory?: number | ||
onMaxHistory?: (history: Date[]) => void | ||
rewriteHistoryOnMax?: boolean | ||
} | ||
|
||
export const createByteCounterStream = (options: ByteCounterStreamOptions) => { | ||
const { maxHistory = 100 } = options | ||
|
||
let bytes = 0 | ||
let history: Date[] = [] | ||
|
||
const transformStream = new Transform({ | ||
transform(chunk: Buffer, encoding: string, callback: TransformCallback) { | ||
bytes += chunk.length | ||
history.push(new Date()) | ||
|
||
if (history.length === maxHistory) { | ||
if (options.rewriteHistoryOnMax) { | ||
options.onMaxHistory?.(history) | ||
history = [] | ||
} | ||
} | ||
|
||
callback(null, chunk) | ||
}, | ||
}) | ||
|
||
return { | ||
transformStream, | ||
get bytes() { | ||
return bytes | ||
}, | ||
get history() { | ||
return history | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.