Skip to content
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

fix(ignore): import using node: prefix for built-in #1268

Merged
merged 9 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/adapter/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import events from 'events';
import events from 'node:events';

import * as Models from '../models';
import {BroadcastAddress} from '../zspec/enums';
Expand All @@ -17,7 +17,14 @@ interface AdapterEventMap {
deviceLeave: [payload: AdapterEvents.DeviceLeavePayload];
}

abstract class Adapter extends events.EventEmitter<AdapterEventMap> {
type AdapterConstructor = new (
networkOptions: TsType.NetworkOptions,
serialPortOptions: TsType.SerialPortOptions,
backupPath: string,
adapterOptions: TsType.AdapterOptions,
) => Adapter;

export abstract class Adapter extends events.EventEmitter<AdapterEventMap> {
public hasZdoMessageOverhead: boolean;
public manufacturerID: Zcl.ManufacturerCode;
protected networkOptions: TsType.NetworkOptions;
Expand Down Expand Up @@ -50,28 +57,25 @@ abstract class Adapter extends events.EventEmitter<AdapterEventMap> {
backupPath: string,
adapterOptions: TsType.AdapterOptions,
): Promise<Adapter> {
const {ZStackAdapter} = await import('./z-stack/adapter');
const {DeconzAdapter} = await import('./deconz/adapter');
const {ZiGateAdapter} = await import('./zigate/adapter');
const {EZSPAdapter} = await import('./ezsp/adapter');
const {EmberAdapter} = await import('./ember/adapter');
const {ZBOSSAdapter} = await import('./zboss/adapter');
const adapterLookup = {
zstack: ZStackAdapter,
deconz: DeconzAdapter,
zigate: ZiGateAdapter,
ezsp: EZSPAdapter,
ember: EmberAdapter,
zboss: ZBOSSAdapter,
deconz: ['./deconz/adapter/deconzAdapter', 'DeconzAdapter'],
ember: ['./ember/adapter/emberAdapter', 'EmberAdapter'],
ezsp: ['./ezsp/adapter/ezspAdapter', 'EZSPAdapter'],
zstack: ['./z-stack/adapter/zStackAdapter', 'ZStackAdapter'],
zboss: ['./zboss/adapter/zbossAdapter', 'ZBOSSAdapter'],
zigate: ['./zigate/adapter/zigateAdapter', 'ZiGateAdapter'],
};

const [adapter, path] = await discoverAdapter(serialPortOptions.adapter, serialPortOptions.path);
const detectedAdapter = adapterLookup[adapter];

if (adapterLookup[adapter]) {
if (detectedAdapter) {
serialPortOptions.adapter = adapter;
serialPortOptions.path = path;

return new adapterLookup[adapter](networkOptions, serialPortOptions, backupPath, adapterOptions);
const adapterModule = await import(detectedAdapter[0]);
const AdapterCtor = adapterModule[detectedAdapter[1]] as AdapterConstructor;

return new AdapterCtor(networkOptions, serialPortOptions, backupPath, adapterOptions);
} else {
throw new Error(`Adapter '${adapter}' does not exists, possible options: ${Object.keys(adapterLookup).join(', ')}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/adapterDiscovery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import {platform} from 'os';
import assert from 'node:assert';
import {platform} from 'node:os';

import {PortInfo} from '@serialport/bindings-cpp';
import {Bonjour, Service} from 'bonjour-service';
Expand Down
18 changes: 8 additions & 10 deletions src/adapter/deconz/adapter/deconzAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Device from '../../../controller/model/device';
import * as Models from '../../../models';
import {Wait, Waitress} from '../../../utils';
import {wait, Waitress} from '../../../utils';
import {logger} from '../../../utils/logger';
import * as ZSpec from '../../../zspec';
import {BroadcastAddress} from '../../../zspec/enums';
Expand All @@ -28,7 +28,7 @@ interface WaitressMatcher {
direction: number;
}

class DeconzAdapter extends Adapter {
export class DeconzAdapter extends Adapter {
private driver: Driver;
private openRequestsQueue: WaitForDataRequest[];
private transactionID: number;
Expand Down Expand Up @@ -156,7 +156,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.CHANNEL_MASK, setChannelMask);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set channel: ' + error, NS);
Expand All @@ -172,7 +172,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.PAN_ID, this.networkOptions.panID);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set panid: ' + error, NS);
Expand All @@ -192,7 +192,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.APS_EXT_PAN_ID, this.networkOptions.extendedPanID!);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set extended panid: ' + error, NS);
Expand All @@ -208,7 +208,7 @@ class DeconzAdapter extends Adapter {

try {
await this.driver.writeParameterRequest(PARAM.PARAM.Network.NETWORK_KEY, this.networkOptions.networkKey!);
await Wait(500);
await wait(500);
changed = true;
} catch (error) {
logger.debug('Could not set network key: ' + error, NS);
Expand All @@ -217,9 +217,9 @@ class DeconzAdapter extends Adapter {

if (changed) {
await this.driver.changeNetworkStateRequest(PARAM.PARAM.Network.NET_OFFLINE);
await Wait(2000);
await wait(2000);
await this.driver.changeNetworkStateRequest(PARAM.PARAM.Network.NET_CONNECTED);
await Wait(2000);
await wait(2000);
}

// write endpoints
Expand Down Expand Up @@ -754,5 +754,3 @@ class DeconzAdapter extends Adapter {
);
}
}

export default DeconzAdapter;
3 changes: 0 additions & 3 deletions src/adapter/deconz/adapter/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/adapter/deconz/driver/driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* istanbul ignore file */

import events from 'events';
import net from 'net';
import events from 'node:events';
import net from 'node:net';

import slip from 'slip';

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/deconz/driver/frameParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */

import {EventEmitter} from 'stream';
import {EventEmitter} from 'node:events';

import {logger} from '../../../utils/logger';
import * as Zdo from '../../../zspec/zdo';
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/deconz/driver/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */

import {Transform, TransformCallback} from 'stream';
import {Transform, TransformCallback} from 'node:stream';

import slip from 'slip';

Expand Down Expand Up @@ -34,7 +34,7 @@ class Parser extends Transform {
logger.debug(`<-- error '${error}'`, NS);
}

public _transform(chunk: Buffer, _: string, cb: TransformCallback): void {
public override _transform(chunk: Buffer, _: string, cb: TransformCallback): void {
//logger.debug(`<-- [${[...chunk]}]`, NS);
this.decoder.decode(chunk);
//logger.debug(`<-- [${[...chunk]}]`, NS);
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/deconz/driver/writer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */

import * as stream from 'stream';
import * as stream from 'node:stream';

import slip from 'slip';

Expand All @@ -16,7 +16,7 @@ class Writer extends stream.Readable {
this.push(buffer);
}

public _read(): void {}
public override _read(): void {}
}

export default Writer;
22 changes: 11 additions & 11 deletions src/adapter/ember/adapter/emberAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {randomBytes} from 'crypto';
import {existsSync, readFileSync, renameSync} from 'fs';
import path from 'path';
import {randomBytes} from 'node:crypto';
import {existsSync, readFileSync, renameSync} from 'node:fs';
import path from 'node:path';

import equals from 'fast-deep-equal/es6';

import {Adapter, TsType} from '../..';
import {Backup, UnifiedBackupStorage} from '../../../models';
import {BackupUtils, Queue, Wait} from '../../../utils';
import {BackupUtils, Queue, wait} from '../../../utils';
import {logger} from '../../../utils/logger';
import * as ZSpec from '../../../zspec';
import {EUI64, ExtendedPanId, NodeId, PanId} from '../../../zspec/tstypes';
Expand Down Expand Up @@ -921,7 +921,7 @@ export class EmberAdapter extends Adapter {
'[INIT TC] Leave network',
);

await Wait(200); // settle down
await wait(200); // settle down

action = NetworkInitAction.LEFT;
}
Expand Down Expand Up @@ -1271,7 +1271,7 @@ export class EmberAdapter extends Adapter {

// XXX: this will block other requests for a while, but should ensure the key propagates without interference?
// could also stop dispatching entirely and do this outside the queue if necessary/better
await Wait(BROADCAST_NETWORK_KEY_SWITCH_WAIT_TIME);
await wait(BROADCAST_NETWORK_KEY_SWITCH_WAIT_TIME);

status = await this.ezsp.ezspBroadcastNetworkKeySwitch();

Expand Down Expand Up @@ -2009,9 +2009,9 @@ export class EmberAdapter extends Adapter {
`~x~> [ZCL to=${ieeeAddr}:${networkAddress} apsFrame=${JSON.stringify(apsFrame)}] Failed to send request with status=${SLStatus[status]}.`,
);
} else if (status === SLStatus.ZIGBEE_MAX_MESSAGE_LIMIT_REACHED || status === SLStatus.BUSY) {
await Wait(QUEUE_BUSY_DEFER_MSEC);
await wait(QUEUE_BUSY_DEFER_MSEC);
} else if (status === SLStatus.NETWORK_DOWN) {
await Wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);
await wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);
} else {
throw new Error(
`~x~> [ZCL to=${ieeeAddr}:${networkAddress} apsFrame=${JSON.stringify(apsFrame)}] Failed to send request with status=${SLStatus[status]}.`,
Expand Down Expand Up @@ -2075,7 +2075,7 @@ export class EmberAdapter extends Adapter {
}

// NOTE: since ezspMessageSentHandler could take a while here, we don't block, it'll just be logged if the delivery failed
await Wait(QUEUE_BUSY_DEFER_MSEC);
await wait(QUEUE_BUSY_DEFER_MSEC);
});
}

Expand Down Expand Up @@ -2117,7 +2117,7 @@ export class EmberAdapter extends Adapter {
}

// NOTE: since ezspMessageSentHandler could take a while here, we don't block, it'll just be logged if the delivery failed
await Wait(QUEUE_BUSY_DEFER_MSEC);
await wait(QUEUE_BUSY_DEFER_MSEC);
});
}

Expand Down Expand Up @@ -2246,7 +2246,7 @@ export class EmberAdapter extends Adapter {
}

// let adapter settle down
await Wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);
await wait(QUEUE_NETWORK_DOWN_DEFER_MSEC);

this.interpanLock = false;
});
Expand Down
3 changes: 0 additions & 3 deletions src/adapter/ember/adapter/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/adapter/ember/ezsp/ezsp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */

import EventEmitter from 'events';
import EventEmitter from 'node:events';

import {Queue} from '../../../utils';
import {logger} from '../../../utils/logger';
Expand Down
8 changes: 4 additions & 4 deletions src/adapter/ember/uart/ash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* istanbul ignore file */

import {Socket} from 'net';
import {EventEmitter} from 'stream';
import {EventEmitter} from 'node:events';
import {Socket} from 'node:net';

import {Wait} from '../../../utils';
import {wait} from '../../../utils';
import {logger} from '../../../utils/logger';
import {SerialPort} from '../../serialPort';
import SocketPortUtils from '../../socketPortUtils';
Expand Down Expand Up @@ -640,7 +640,7 @@ export class UartAsh extends EventEmitter<UartAshEventMap> {
}

logger.debug(`Waiting for RSTACK... ${i}/${CONFIG_TIME_RST}`, NS);
await Wait(CONFIG_TIME_RST_CHECK);
await wait(CONFIG_TIME_RST_CHECK);
}

return EzspStatus.HOST_FATAL_ERROR;
Expand Down
6 changes: 3 additions & 3 deletions src/adapter/ember/uart/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */

import {Transform, TransformCallback, TransformOptions} from 'stream';
import {Transform, TransformCallback, TransformOptions} from 'node:stream';

// import {logger} from '../../../utils/logger';
import {AshReservedByte} from './enums';
Expand All @@ -16,7 +16,7 @@ export class AshParser extends Transform {
this.buffer = Buffer.alloc(0);
}

_transform(chunk: Buffer, encoding: BufferEncoding, cb: TransformCallback): void {
override _transform(chunk: Buffer, encoding: BufferEncoding, cb: TransformCallback): void {
let data = Buffer.concat([this.buffer, chunk]);
let position: number;

Expand All @@ -37,7 +37,7 @@ export class AshParser extends Transform {
cb();
}

_flush(cb: TransformCallback): void {
override _flush(cb: TransformCallback): void {
this.push(this.buffer);

this.buffer = Buffer.alloc(0);
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/ember/uart/writer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */

import {Readable, ReadableOptions} from 'stream';
import {Readable, ReadableOptions} from 'node:stream';

// import {logger} from '../../../utils/logger';

Expand Down Expand Up @@ -49,5 +49,5 @@ export class AshWriter extends Readable {
}
}

public _read(): void {}
public override _read(): void {}
}
8 changes: 3 additions & 5 deletions src/adapter/events.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Header as ZclHeader} from '../zspec/zcl';

type DeviceJoinedPayload = {
export type DeviceJoinedPayload = {
networkAddress: number;
ieeeAddr: string;
};

type DeviceLeavePayload = {networkAddress?: number; ieeeAddr: string} | {networkAddress: number; ieeeAddr?: string};
export type DeviceLeavePayload = {networkAddress?: number; ieeeAddr: string} | {networkAddress: number; ieeeAddr?: string};

interface ZclPayload {
export interface ZclPayload {
clusterID: number;
address: number | string;
header: ZclHeader | undefined;
Expand All @@ -19,5 +19,3 @@ interface ZclPayload {
wasBroadcast: boolean;
destinationEndpoint: number;
}

export {DeviceJoinedPayload, ZclPayload, DeviceLeavePayload};
2 changes: 1 addition & 1 deletion src/adapter/ezsp/adapter/backup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */

import * as fs from 'fs';
import * as fs from 'node:fs';

import * as Models from '../../../models';
import {BackupUtils} from '../../../utils';
Expand Down
Loading
Loading