-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stream example #34
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,35 +4,37 @@ const { Duplex } = require('stream'); | |
|
||
class PortDuplex extends Duplex { | ||
#port = undefined; | ||
#transfer = false; | ||
|
||
constructor (port, options) { | ||
const { | ||
readable = true, | ||
writable = true, | ||
transfer = false | ||
writable = true | ||
} = { ...options }; | ||
if (typeof readable !== 'boolean') { | ||
throw new TypeError('readable must be a boolean'); | ||
} | ||
if (typeof writable !== 'boolean') { | ||
throw new TypeError('writable must be a boolean'); | ||
} | ||
if (typeof transfer !== 'boolean') { | ||
throw new TypeError('transfer must be a boolean'); | ||
} | ||
super({ autoDestroy: true, readable, writable }); | ||
this.#port = port; | ||
this.#transfer = transfer; | ||
this.#port.onmessage = PortDuplex.#onmessage.bind(this); | ||
} | ||
|
||
_write (chunk, encoding, callback) { | ||
if (typeof chunk === 'string') { | ||
chunk = Buffer.from(chunk, encoding); | ||
} | ||
const transferList = this.#transfer ? [chunk.buffer] : undefined; | ||
this.#port.postMessage(chunk, transferList); | ||
// Be sure to always copy the chunk here and never use a | ||
// transferList. There are several reasons: | ||
// a) Buffer instances are most often created off a pool | ||
// and share the same underlying common ArrayBuffer, | ||
// transferring those can break Node.js in many ways. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the comment in the Node.js thread, this is true but not necessarily a concern in the future. The sceond concern is definitely there, though :) |
||
// b) The Buffer instance may still be used by some | ||
// other upstream component. Transferring it here | ||
// will cause unexpected and undefined behavior that | ||
// will likely crash the Node.js process. | ||
this.#port.postMessage(chunk); | ||
callback(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a feeling there should be some sort of backpressure built in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely it should |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will always be a buffer, you are not allowing the encoding to be changed.