Skip to content

Commit a55715a

Browse files
authored
Add types for @liamcottle/meshcore.js v1.10 (DefinitelyTyped#74396)
1 parent 1ff59a5 commit a55715a

File tree

6 files changed

+511
-0
lines changed

6 files changed

+511
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# TypeScript Definitions for @liamcottle/meshcore.js
2+
3+
Type definitions for [@liamcottle/meshcore.js](https://github.com/meshcore-dev/meshcore.js) v1.10.0
4+
5+
## About
6+
7+
This package provides TypeScript type definitions for the `@liamcottle/meshcore.js` library, which is used to connect to MeshCore devices over serial (USB) or TCP (WiFi).
8+
9+
## Usage
10+
11+
```typescript
12+
import { NodeJSSerialConnection, Constants } from '@liamcottle/meshcore.js';
13+
14+
const connection = new NodeJSSerialConnection('/dev/ttyUSB0');
15+
16+
connection.on('connected', async () => {
17+
const selfInfo = await connection.getSelfInfo();
18+
console.log('Connected:', selfInfo.name);
19+
20+
const channels = await connection.getChannels();
21+
await connection.sendChannelTextMessage(0, 'Hello mesh network!');
22+
});
23+
24+
connection.on(Constants.ResponseCodes.ChannelMsgRecv, (message) => {
25+
console.log('Received:', message.text);
26+
});
27+
28+
await connection.connect();
29+
```
30+
31+
## Note on Module Type
32+
33+
The @liamcottle/meshcore.js package uses ESM (`"type": "module"`) but doesn't declare an `exports` field, which may cause module resolution warnings in some tools. The types work correctly with both CommonJS and ESM projects.
34+
35+
## License
36+
37+
MIT
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/// <reference types="node" />
2+
3+
import { EventEmitter } from "events";
4+
5+
/**
6+
* Base connection class for MeshCore devices
7+
*/
8+
export class Connection extends EventEmitter {
9+
/**
10+
* Connect to the MeshCore device
11+
*/
12+
connect(): Promise<void>;
13+
14+
/**
15+
* Close the connection to the MeshCore device
16+
*/
17+
close(): Promise<void>;
18+
19+
/**
20+
* Get information about this device
21+
* @param timeout - Optional timeout in milliseconds
22+
*/
23+
getSelfInfo(timeout?: number): Promise<SelfInfo>;
24+
25+
/**
26+
* Get pending messages waiting to be received
27+
*/
28+
getWaitingMessages(): Promise<Message[]>;
29+
30+
/**
31+
* Get all channels configured on the device
32+
*/
33+
getChannels(): Promise<Channel[]>;
34+
35+
/**
36+
* Get contacts from the device
37+
* @param since - Optional timestamp to get contacts since
38+
*/
39+
getContacts(since?: number): Promise<Contact[]>;
40+
41+
/**
42+
* Sync the next pending message from the device
43+
*/
44+
syncNextMessage(): Promise<void>;
45+
46+
/**
47+
* Send a text message to a contact
48+
* @param publicKey - The recipient's public key
49+
* @param text - The message text
50+
*/
51+
sendTextMessage(publicKey: Buffer, text: string): Promise<void>;
52+
53+
/**
54+
* Send a text message to a channel
55+
* @param channelIdx - The channel index
56+
* @param text - The message text
57+
*/
58+
sendChannelTextMessage(channelIdx: number, text: string): Promise<void>;
59+
60+
/**
61+
* Find a contact by public key prefix
62+
* @param prefix - The public key prefix (first 6 bytes)
63+
*/
64+
findContactByPublicKeyPrefix(prefix: Buffer): Promise<Contact | null>;
65+
66+
/**
67+
* Internal method to handle received frames
68+
* @param frame - The received frame buffer
69+
*/
70+
onFrameReceived(frame: Buffer): void;
71+
72+
// Event emitter overrides for better typing
73+
on(event: "connected", listener: () => void): this;
74+
on(event: "disconnected", listener: () => void): this;
75+
on(event: number, listener: (data: any) => void): this;
76+
on(event: string | number | symbol, listener: (...args: any[]) => void): this;
77+
78+
emit(event: string | number | symbol, ...args: any[]): boolean;
79+
}
80+
81+
/**
82+
* NodeJS Serial connection for MeshCore devices over USB
83+
*/
84+
export class NodeJSSerialConnection extends Connection {
85+
/**
86+
* Create a new serial connection
87+
* @param port - Serial port path (e.g., "/dev/ttyUSB0", "COM3")
88+
*/
89+
constructor(port: string);
90+
}
91+
92+
/**
93+
* Web Serial API connection for MeshCore devices
94+
*/
95+
export class WebSerialConnection extends Connection {
96+
constructor();
97+
}
98+
99+
/**
100+
* TCP connection for MeshCore devices over WiFi
101+
*/
102+
export class TCPConnection extends Connection {
103+
/**
104+
* Create a new TCP connection
105+
* @param host - The host address
106+
* @param port - The port number
107+
*/
108+
constructor(host: string, port: number);
109+
}
110+
111+
/**
112+
* Web Bluetooth connection for MeshCore devices
113+
*/
114+
export class WebBleConnection extends Connection {
115+
constructor();
116+
}
117+
118+
/**
119+
* Generic serial connection
120+
*/
121+
export class SerialConnection extends Connection {
122+
constructor();
123+
}
124+
125+
/**
126+
* Information about the local device
127+
*/
128+
export interface SelfInfo {
129+
/** The device's public key */
130+
publicKey: Buffer;
131+
/** The device's name (optional) */
132+
name?: string;
133+
}
134+
135+
/**
136+
* A received message
137+
*/
138+
export interface Message {
139+
/** Public key prefix of the sender (first 6 bytes) */
140+
pubKeyPrefix: Buffer;
141+
/** Path length (number of hops) */
142+
pathLen: number;
143+
/** Text type identifier */
144+
txtType: number;
145+
/** Timestamp from the sender */
146+
senderTimestamp: number;
147+
/** The message text */
148+
text: string;
149+
}
150+
151+
/**
152+
* A MeshCore channel
153+
*/
154+
export interface Channel {
155+
/** The channel index */
156+
channelIdx: number;
157+
/** The channel name */
158+
name: string;
159+
/** The channel encryption secret */
160+
secret: Buffer;
161+
}
162+
163+
/**
164+
* A contact in the mesh network
165+
*/
166+
export interface Contact {
167+
/** The contact's public key */
168+
publicKey: Buffer;
169+
/** The contact's name (optional) */
170+
name?: string;
171+
/** Last seen timestamp (optional) */
172+
lastSeen?: number;
173+
}
174+
175+
/**
176+
* Constants used by the MeshCore protocol
177+
*/
178+
export class Constants {
179+
/** Supported companion protocol version */
180+
static readonly SupportedCompanionProtocolVersion: number;
181+
182+
/** Response codes from the device */
183+
static readonly ResponseCodes: {
184+
/** Contact message received */
185+
ContactMsgRecv: number;
186+
/** Channel message received */
187+
ChannelMsgRecv: number;
188+
[key: string]: number;
189+
};
190+
191+
/** Push notification codes */
192+
static readonly PushCodes: {
193+
/** Message waiting notification */
194+
MsgWaiting: number;
195+
/** New node advertisement */
196+
NewAdvert: number;
197+
[key: string]: number;
198+
};
199+
200+
/** Command codes sent to the device */
201+
static readonly CommandCodes: {
202+
/** App start command */
203+
AppStart: number;
204+
/** Send text message command */
205+
SendTxtMsg: number;
206+
/** Send channel text message command */
207+
SendChannelTxtMsg: number;
208+
/** Get contacts command */
209+
GetContacts: number;
210+
/** Get device time command */
211+
GetDeviceTime: number;
212+
/** Set device time command */
213+
SetDeviceTime: number;
214+
/** Send self advertisement command */
215+
SendSelfAdvert: number;
216+
/** Set advertisement name command */
217+
SetAdvertName: number;
218+
[key: string]: number;
219+
};
220+
}
221+
222+
/**
223+
* A node advertisement packet
224+
*/
225+
export class Advert {
226+
constructor(data: Buffer);
227+
/** The node's public key */
228+
publicKey: Buffer;
229+
/** The advertised name (optional) */
230+
advName?: string;
231+
}
232+
233+
/**
234+
* A protocol packet
235+
*/
236+
export class Packet {
237+
constructor(data: Buffer);
238+
}
239+
240+
/**
241+
* Buffer utility functions
242+
*/
243+
export class BufferUtils {
244+
/**
245+
* XOR two buffers
246+
*/
247+
static xor(a: Buffer, b: Buffer): Buffer;
248+
249+
/**
250+
* Concatenate multiple buffers
251+
*/
252+
static concat(...buffers: Buffer[]): Buffer;
253+
}
254+
255+
/**
256+
* Cayenne Low Power Payload encoder/decoder
257+
*/
258+
export class CayenneLpp {
259+
constructor();
260+
}

0 commit comments

Comments
 (0)