-
-
Notifications
You must be signed in to change notification settings - Fork 630
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
refactor: migrate from Node.js streams to Web Streams API #7457
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
37a880e
refactor: add web-compatible versions of transform streams
AlCalzone c29afa5
refactor: migrate to web streams (WIP)
AlCalzone 26974ed
fix: handle errors while (re)opening the serial port
AlCalzone 20316e6
feat: restore functionality of socket serial
AlCalzone 3d9387d
refactor: remove legacy serial implementations
AlCalzone bd7669b
test: update tests
AlCalzone 1978cbe
fix: defer serial data handling to next event loop iteration
AlCalzone 58d759b
test: restore test
AlCalzone c1653a2
fix: avoid backpressure in un-read mock port
AlCalzone a1416c6
fix: bootloader tests
AlCalzone fa61643
test: fix broken test
AlCalzone 73a1c45
refactor: migrate Zniffer to Web streams
AlCalzone a205fc3
refactor: remove legacy parsers
AlCalzone 5a59ff8
refactor: remove "Web" from stream names
AlCalzone bba8a6b
fix: types
AlCalzone 6b52a22
fix: lint
AlCalzone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./mock/MockSerialPort.js"; | ||
export * from "./mock/MockPort.js"; | ||
export * from "./mock/SerialPortBindingMock.js"; | ||
export * from "./mock/SerialPortMock.js"; |
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,81 @@ | ||
import { ZWaveLogContainer } from "@zwave-js/core"; | ||
import type { UnderlyingSink, UnderlyingSource } from "node:stream/web"; | ||
import { | ||
type ZWaveSerialBindingFactory, | ||
type ZWaveSerialStream, | ||
ZWaveSerialStreamFactory, | ||
} from "../serialport/ZWaveSerialStream.js"; | ||
|
||
export class MockPort { | ||
public constructor() { | ||
const { readable, writable: sink } = new TransformStream<Uint8Array>(); | ||
this.#sink = sink; | ||
this.readable = readable; | ||
} | ||
|
||
// Remembers the last written data | ||
public lastWrite: Uint8Array | undefined; | ||
|
||
// Internal stream to allow emitting data from the port | ||
#sourceController: ReadableStreamDefaultController<Uint8Array> | undefined; | ||
|
||
// Public readable stream to allow handling the written data | ||
#sink: WritableStream<Uint8Array>; | ||
/** Exposes the data written by the host as a readable stream */ | ||
public readonly readable: ReadableStream<Uint8Array>; | ||
|
||
public factory(): ZWaveSerialBindingFactory { | ||
return () => { | ||
const sink: UnderlyingSink<Uint8Array> = { | ||
write: async (chunk, _controller) => { | ||
// Remember the last written data | ||
this.lastWrite = chunk; | ||
// Only write to the sink if its readable side has a reader attached. | ||
// Otherwise, we get backpressure on the writable side of the mock port | ||
if (this.readable.locked) { | ||
const writer = this.#sink.getWriter(); | ||
try { | ||
await writer.write(chunk); | ||
} finally { | ||
writer.releaseLock(); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
const source: UnderlyingSource<Uint8Array> = { | ||
start: (controller) => { | ||
this.#sourceController = controller; | ||
}, | ||
}; | ||
|
||
return Promise.resolve({ sink, source }); | ||
}; | ||
} | ||
|
||
public emitData(data: Uint8Array): void { | ||
this.#sourceController?.enqueue(data); | ||
} | ||
|
||
public destroy(): void { | ||
try { | ||
this.#sourceController?.close(); | ||
this.#sourceController = undefined; | ||
} catch { | ||
// Ignore - the controller might already be closed | ||
} | ||
} | ||
} | ||
|
||
export async function createAndOpenMockedZWaveSerialPort(): Promise<{ | ||
port: MockPort; | ||
serial: ZWaveSerialStream; | ||
}> { | ||
const port = new MockPort(); | ||
const factory = new ZWaveSerialStreamFactory( | ||
port.factory(), | ||
new ZWaveLogContainer({ enabled: false }), | ||
); | ||
const serial = await factory.createStream(); | ||
return { port, serial }; | ||
} |
File renamed without changes.
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Superfluous trailing arguments Warning
Copilot Autofix AI about 1 month ago
Copilot could not generate an autofix suggestion
Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.