mirror of
https://github.com/siemens-mobile-hacks/node-sie-serial.git
synced 2026-11-25 17:17:17 +00:00
Fix types
This commit is contained in:
parent
bc0d18d7ec
commit
e73cd43a3f
3 changed files with 119 additions and 22 deletions
|
|
@ -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<BaseAsyncSerialPortOptions, 'binding'> & {
|
||||
binding?: BindingInterface;
|
||||
export type AsyncSerialPortOptions<TOptions extends OpenOptions = OpenOptions> =
|
||||
Omit<BaseAsyncSerialPortOptions<TOptions>, 'binding'> & {
|
||||
binding?: BindingInterface<BindingPortInterface, TOptions>;
|
||||
};
|
||||
|
||||
export class AsyncSerialPort extends BaseAsyncSerialPort {
|
||||
constructor(options: AsyncSerialPortOptions) {
|
||||
const { binding = autoDetect(), ...openOptions } = options;
|
||||
super({ binding, ...openOptions });
|
||||
export class AsyncSerialPort<TOptions extends OpenOptions = OpenOptions> extends BaseAsyncSerialPort {
|
||||
constructor(options: AsyncSerialPortOptions<TOptions>) {
|
||||
const binding: BindingInterface = options.binding ?? autoDetect();
|
||||
super({ ...options, binding });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<OpenOptions>;
|
||||
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<void> {
|
||||
async write(buffer: Buffer): Promise<void> {
|
||||
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<TestPortBinding> = {
|
||||
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<TestPortBinding, CustomOpenOptions> = {
|
||||
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<TestPortBinding, CustomOpenOptions> = {
|
||||
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();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 extends OpenOptions = OpenOptions> = TOptions & {
|
||||
binding: BindingInterface<BindingPortInterface, TOptions>;
|
||||
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<TOptions extends OpenOptions = OpenOptions> {
|
||||
private readonly binding: BindingInterface<BindingPortInterface, TOptions>;
|
||||
private readonly openOptions: TOptions;
|
||||
private port?: BindingPortInterface;
|
||||
private opening?: Promise<void>;
|
||||
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<void> = Promise.resolve();
|
||||
|
||||
constructor(options: AsyncSerialPortOptions) {
|
||||
const { binding, ...openOptions } = options;
|
||||
constructor(options: AsyncSerialPortOptions<TOptions>) {
|
||||
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<void> {
|
||||
write(data: Uint8Array | string): Promise<void> {
|
||||
let port: BindingPortInterface;
|
||||
try {
|
||||
port = this.getOpenPort();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue