-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* _read method of readable steam learning test * Max Buffer size * retry pipe writableStream * recovery readable stream * readableStream.closed above node@18 [#177] Update minimum NodeJS version to Node@16
- Loading branch information
Showing
6 changed files
with
403 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ jobs: | |
strategy: | ||
matrix: | ||
node_version: | ||
- 14 | ||
- 16 | ||
- 18 | ||
|
||
|
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,122 @@ | ||
/** | ||
* Pinpoint Node.js Agent | ||
* Copyright 2021-present NAVER Corp. | ||
* Apache License v2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const { Readable } = require('node:stream') | ||
const log = require('../utils/logger') | ||
|
||
class BoundedBufferReadableStream { | ||
constructor(constructorOptions) { | ||
this.buffer = [] | ||
this.options = constructorOptions || {} | ||
this.readableStream = this.makeReadableSteam() | ||
this.maxBufferSize = this.options.maxBuffer || 100 | ||
} | ||
|
||
makeReadableSteam() { | ||
const readableStream = new Readable(Object.assign({ | ||
read: () => { | ||
this.readStart() | ||
} | ||
}, this.options)) | ||
|
||
readableStream.on('error', () => { | ||
// https://nodejs.org/api/stream.html#readablepipedestination-options | ||
// `One important caveat is that if the Readable stream emits an error during processing, | ||
// the Writable destination is not closed automatically. | ||
// If an error occurs, it will be necessary to manually close each stream | ||
// in order to prevent memory leaks.` | ||
// for readable steam error memory leak prevention | ||
if (this.writableSteam && typeof this.writableSteam.end === 'function') { | ||
this.writableSteam.end() | ||
} | ||
}) | ||
|
||
readableStream.on('close', () => { | ||
if (!this.writableFactory) { | ||
return | ||
} | ||
|
||
this.readableStream = this.makeReadableSteam() | ||
this._pipe() | ||
}) | ||
|
||
return readableStream | ||
} | ||
|
||
push(data) { | ||
if (this.buffer.length < this.maxBufferSize) { | ||
this.buffer.push(data) | ||
} | ||
|
||
if (this.canStart()) { | ||
this.readStart() | ||
} | ||
} | ||
|
||
canStart() { | ||
return this.readable | ||
} | ||
|
||
readStart() { | ||
this.readable = true | ||
|
||
const length = this.buffer.length | ||
for (let index = 0; index < length; index++) { | ||
if (!this.readableStream.push(this.buffer.shift())) { | ||
return this.readStop() | ||
} | ||
} | ||
} | ||
|
||
readStop() { | ||
this.readable = false | ||
} | ||
|
||
end() { | ||
this.readableStream.end() | ||
} | ||
|
||
pipe(writableFactory) { | ||
this.writableFactory = writableFactory | ||
this._pipe() | ||
} | ||
|
||
_pipe() { | ||
if (typeof this.writableFactory !== 'function') { | ||
return | ||
} | ||
|
||
const writableSteam = this.writableFactory() | ||
if (!writableSteam) { | ||
return | ||
} | ||
|
||
writableSteam.on('error', (error) => { | ||
if (error) { | ||
log.error('writable steam error', error) | ||
} | ||
}) | ||
|
||
writableSteam.on('unpipe', () => { | ||
this.readStop() | ||
}) | ||
|
||
writableSteam.on('close', () => { | ||
|
||
}) | ||
|
||
this.readableStream.pipe(writableSteam) | ||
this.writableSteam = writableSteam | ||
} | ||
|
||
setEncoding(encoding) { | ||
this.readableStream.setEncoding(encoding) | ||
} | ||
} | ||
|
||
module.exports = BoundedBufferReadableStream |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.