diff --git a/src/AsyncSerialPort.node.ts b/src/AsyncSerialPort.node.ts index fb121fe..8029566 100644 --- a/src/AsyncSerialPort.node.ts +++ b/src/AsyncSerialPort.node.ts @@ -1,5 +1,5 @@ import { autoDetect } from '@sie-js/node-serialport-bindings-cpp'; -import type { BindingInterface } from '@serialport/bindings-interface'; +import type { BindingInterface, BindingPortInterface, OpenOptions } from '@serialport/bindings-interface'; import { AsyncSerialPort as BaseAsyncSerialPort, type AsyncSerialPortOptions as BaseAsyncSerialPortOptions, @@ -7,13 +7,14 @@ import { export { SerialReadTimeoutError } from './AsyncSerialPort.js'; -export type AsyncSerialPortOptions = Omit & { - binding?: BindingInterface; +export type AsyncSerialPortOptions = + Omit, 'binding'> & { + binding?: BindingInterface; }; -export class AsyncSerialPort extends BaseAsyncSerialPort { - constructor(options: AsyncSerialPortOptions) { - const { binding = autoDetect(), ...openOptions } = options; - super({ binding, ...openOptions }); +export class AsyncSerialPort extends BaseAsyncSerialPort { + constructor(options: AsyncSerialPortOptions) { + const binding: BindingInterface = options.binding ?? autoDetect(); + super({ ...options, binding }); } } diff --git a/src/AsyncSerialPort.test.ts b/src/AsyncSerialPort.test.ts index 1ad06b4..cbea19e 100644 --- a/src/AsyncSerialPort.test.ts +++ b/src/AsyncSerialPort.test.ts @@ -1,12 +1,14 @@ import { BindingInterface, BindingPortInterface, OpenOptions, PortStatus, SetOptions, UpdateOptions } from '@serialport/bindings-interface'; import { describe, expect, test } from 'vitest'; import { AsyncSerialPort, SerialReadTimeoutError } from './AsyncSerialPort.js'; +import { AsyncSerialPort as NodeAsyncSerialPort } from './AsyncSerialPort.node.js'; class TestPortBinding implements BindingPortInterface { readonly openOptions: Required; isOpen = true; readCalls = 0; writeCalls = 0; + writes: Buffer[] = []; drainCalls = 0; maxParallelWrites = 0; private activeWrites = 0; @@ -68,8 +70,9 @@ class TestPortBinding implements BindingPortInterface { }); } - async write(): Promise { + async write(buffer: Buffer): Promise { this.writeCalls++; + this.writes.push(buffer); this.activeWrites++; this.maxParallelWrites = Math.max(this.maxParallelWrites, this.activeWrites); if (this.writeCalls == 1) @@ -111,7 +114,7 @@ class TestPortBinding implements BindingPortInterface { } } -async function openTestPort(): Promise<{ +async function openTestPort(highWaterMark?: number): Promise<{ port: AsyncSerialPort; bindingPort: TestPortBinding; bindingPorts: TestPortBinding[]; @@ -126,7 +129,7 @@ async function openTestPort(): Promise<{ return bindingPort; }, }; - const port = new AsyncSerialPort({ binding, path: 'test', baudRate: 115200 }); + const port = new AsyncSerialPort({ binding, path: 'test', baudRate: 115200, highWaterMark }); await port.open(); return { port, bindingPort: bindingPort!, bindingPorts }; } @@ -173,6 +176,92 @@ describe('AsyncSerialPort', () => { await port.close(); }); + test('uses the configured high water mark as the read buffer capacity', async () => { + const { port, bindingPort } = await openTestPort(4); + bindingPort.emit(Buffer.from([1, 2, 3, 4, 5, 6])); + await nextTurn(); + expect(bindingPort.readCalls).toBe(1); + + expect(await port.read(2)).toEqual(Buffer.from([1, 2])); + await nextTurn(); + expect(bindingPort.readCalls).toBe(2); + expect(await port.readExact(4)).toEqual(Buffer.from([3, 4, 5, 6])); + await port.close(); + }); + + test('rejects an invalid high water mark', () => { + const binding: BindingInterface = { + list: async () => [], + open: async (options) => new TestPortBinding(options), + }; + expect(() => new AsyncSerialPort({ + binding, + path: 'test', + baudRate: 115200, + highWaterMark: 0, + })).toThrow('High water mark must be a positive safe integer.'); + }); + + test('passes binding-specific options without internal options', async () => { + interface CustomOpenOptions extends OpenOptions { + customOption: string; + } + + let receivedOptions: CustomOpenOptions | undefined; + const binding: BindingInterface = { + list: async () => [], + open: async (options) => { + receivedOptions = options; + return new TestPortBinding(options); + }, + }; + const port: AsyncSerialPort = new AsyncSerialPort({ + binding, + path: 'test', + baudRate: 115200, + customOption: 'custom', + highWaterMark: 8, + }); + await port.open(); + + expect(receivedOptions).toEqual({ + path: 'test', + baudRate: 115200, + customOption: 'custom', + }); + await port.close(); + }); + + test('passes binding-specific options through the Node entry point', async () => { + interface CustomOpenOptions extends OpenOptions { + customOption: string; + } + + let receivedOptions: CustomOpenOptions | undefined; + const binding: BindingInterface = { + list: async () => [], + open: async (options) => { + receivedOptions = options; + return new TestPortBinding(options); + }, + }; + const port: NodeAsyncSerialPort = new NodeAsyncSerialPort({ + binding, + path: 'test', + baudRate: 115200, + customOption: 'custom', + highWaterMark: 8, + }); + await port.open(); + + expect(receivedOptions).toEqual({ + path: 'test', + baudRate: 115200, + customOption: 'custom', + }); + await port.close(); + }); + test('preserves a native read that completes after a timeout', async () => { const { port, bindingPort } = await openTestPort(); const first = port.read(2, 20); @@ -322,13 +411,14 @@ describe('AsyncSerialPort', () => { test('serializes writes for bindings that reject overlap', async () => { const { port, bindingPort } = await openTestPort(); - const first = port.write(Buffer.from([1])); + const first = port.write(new Uint8Array([1])); const second = port.write(Buffer.from([2])); await nextTurn(); expect(bindingPort.writeCalls).toBe(1); bindingPort.unblockWrite(); await Promise.all([first, second]); expect(bindingPort.writeCalls).toBe(2); + expect(bindingPort.writes).toEqual([Buffer.from([1]), Buffer.from([2])]); expect(bindingPort.maxParallelWrites).toBe(1); await port.close(); }); diff --git a/src/AsyncSerialPort.ts b/src/AsyncSerialPort.ts index e76eba5..ebb6362 100644 --- a/src/AsyncSerialPort.ts +++ b/src/AsyncSerialPort.ts @@ -7,10 +7,11 @@ import type { UpdateOptions, } from '@serialport/bindings-interface'; -const READ_BUFFER_SIZE = 64 * 1024; +const DEFAULT_HIGH_WATER_MARK = 64 * 1024; -export type AsyncSerialPortOptions = OpenOptions & { - binding: BindingInterface; +export type AsyncSerialPortOptions = TOptions & { + binding: BindingInterface; + highWaterMark?: number; }; export class SerialReadTimeoutError extends Error { @@ -20,13 +21,13 @@ export class SerialReadTimeoutError extends Error { } } -export class AsyncSerialPort { - private readonly binding: BindingInterface; - private readonly openOptions: OpenOptions; +export class AsyncSerialPort { + private readonly binding: BindingInterface; + private readonly openOptions: TOptions; private port?: BindingPortInterface; private opening?: Promise; private readError?: unknown; - private readonly readBuffer = Buffer.allocUnsafe(READ_BUFFER_SIZE); + private readonly readBuffer: Buffer; private readBufferStart = 0; private readBufferLength = 0; private readonly dataWaiters = new Set<() => void>(); @@ -35,10 +36,15 @@ export class AsyncSerialPort { private pendingReadCount = 0; private writeQueueTail: Promise = Promise.resolve(); - constructor(options: AsyncSerialPortOptions) { - const { binding, ...openOptions } = options; + constructor(options: AsyncSerialPortOptions) { + const { binding, highWaterMark = DEFAULT_HIGH_WATER_MARK } = options; + if (!Number.isSafeInteger(highWaterMark) || highWaterMark < 1) + throw new RangeError('High water mark must be a positive safe integer.'); this.binding = binding; - this.openOptions = openOptions; + this.openOptions = { ...options }; + Reflect.deleteProperty(this.openOptions, 'binding'); + Reflect.deleteProperty(this.openOptions, 'highWaterMark'); + this.readBuffer = Buffer.allocUnsafe(highWaterMark); } get isOpen(): boolean { @@ -204,7 +210,7 @@ export class AsyncSerialPort { return this.queueRead(() => this.discardUntilIdleInternal(idleTimeout, maxTimeout, signal)); } - write(data: Buffer | string): Promise { + write(data: Uint8Array | string): Promise { let port: BindingPortInterface; try { port = this.getOpenPort();