-
-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate from Node.js streams to Web Streams API (#7457)
- Loading branch information
Showing
60 changed files
with
2,038 additions
and
1,523 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
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.