From e841d0e8a557244fb5f89b3e596dc09889ebd5f6 Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Fri, 3 Jan 2025 19:17:23 -0300 Subject: [PATCH 1/6] Add TypeScript typings --- index.d.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 8 ++++-- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..6dc507e --- /dev/null +++ b/index.d.ts @@ -0,0 +1,71 @@ +import EventEmitter from 'bare-events' + +export class Socket extends EventEmitter { + constructor(opts?: { ipv6Only?: boolean; reuseAddress?: boolean }) + + address(): { address: string; family: string; port: number } | null + + remoteAddress(): { address: string; family: string; port: number } | null + + bind(port: number, address: string, cb?: Function): this + bind(opts: { port?: number; address?: string }, cb?: Function): this + bind(port: number, cb?: Function): this + bind(cb?: Function): this + + connect(port: number, address: string, cb?: Function): void + connect(port: number, cb?: Function): void + + close(cb?: Function): Promise + + send( + msg: unknown, + offset: number, + length: number, + port?: number, + address?: string, + cb?: Function + ): Promise + + send( + msg: unknown, + offset: number, + length: number, + port: number, + cb: Function + ): Promise + + send( + msg: unknown, + offset: number, + length: number, + address: string, + cb: Function + ): Promise + + send( + msg: unknown, + offset: number, + address: string, + cb: Function + ): Promise + + send( + msg: unknown, + offset: number, + length: number, + cb: Function + ): Promise + + send(msg: unknown, port: number, address: string): Promise + + send(msg: unknown, port: number, cb?: Function): Promise + + send(msg: unknown, address: string): Promise + + send(msg: unknown, cb: Function): Promise +} + +export function createSocket( + opts?: { ipv6Only?: boolean; reuseAddress?: boolean } | string, + cb?: Function +): Socket diff --git a/package.json b/package.json index 3b476a9..488f7d7 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,15 @@ "version": "1.0.1", "description": "Native UDP for JavaScript", "exports": { - ".": "./index.js", + ".": { + "types": "./index.d.ts", + "default": "./index.js" + }, "./package": "./package.json" }, "files": [ - "index.js" + "index.js", + "./index.d.ts" ], "scripts": { "test": "prettier . --check && bare test.js" From 6bd281382abbc181ca6feb1bcd74ea2698d2cf53 Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Mon, 6 Jan 2025 08:27:30 -0300 Subject: [PATCH 2/6] Stricter callback signatures --- index.d.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6dc507e..fc524be 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,15 +7,15 @@ export class Socket extends EventEmitter { remoteAddress(): { address: string; family: string; port: number } | null - bind(port: number, address: string, cb?: Function): this - bind(opts: { port?: number; address?: string }, cb?: Function): this - bind(port: number, cb?: Function): this - bind(cb?: Function): this + bind(port: number, address: string, cb?: () => void): this + bind(opts: { port?: number; address?: string }, cb?: () => void): this + bind(port: number, cb?: () => void): this + bind(cb?: () => void): this - connect(port: number, address: string, cb?: Function): void - connect(port: number, cb?: Function): void + connect(port: number, address: string, cb?: () => void): void + connect(port: number, cb?: () => void): void - close(cb?: Function): Promise + close(cb?: (err: Error) => void): Promise send( msg: unknown, @@ -23,7 +23,7 @@ export class Socket extends EventEmitter { length: number, port?: number, address?: string, - cb?: Function + cb?: (err: Error) => void ): Promise send( @@ -31,7 +31,7 @@ export class Socket extends EventEmitter { offset: number, length: number, port: number, - cb: Function + cb: (err: Error) => void ): Promise send( @@ -39,33 +39,33 @@ export class Socket extends EventEmitter { offset: number, length: number, address: string, - cb: Function + cb: (err: Error) => void ): Promise send( msg: unknown, offset: number, address: string, - cb: Function + cb: (err: Error) => void ): Promise send( msg: unknown, offset: number, length: number, - cb: Function + cb: (err: Error) => void ): Promise send(msg: unknown, port: number, address: string): Promise - send(msg: unknown, port: number, cb?: Function): Promise + send(msg: unknown, port: number, cb?: (err: Error) => void): Promise send(msg: unknown, address: string): Promise - send(msg: unknown, cb: Function): Promise + send(msg: unknown, cb: (err: Error) => void): Promise } export function createSocket( opts?: { ipv6Only?: boolean; reuseAddress?: boolean } | string, - cb?: Function + cb?: (message: unknown) => void ): Socket From b2ae29795837e0a1321769519d3b04d4722578ba Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Mon, 6 Jan 2025 13:17:12 -0300 Subject: [PATCH 3/6] Review --- index.d.ts | 38 +++++++++++++++++++++++++------------- package.json | 4 ++++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/index.d.ts b/index.d.ts index fc524be..c43febc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,14 @@ import EventEmitter from 'bare-events' +import Buffer from 'bare-buffer' -export class Socket extends EventEmitter { +export interface rinfo { + address: string + family: 'IPv4' | 'IPv6' + port: number + size: number +} + +export class Socket extends EventEmitter { constructor(opts?: { ipv6Only?: boolean; reuseAddress?: boolean }) address(): { address: string; family: string; port: number } | null @@ -18,7 +26,7 @@ export class Socket extends EventEmitter { close(cb?: (err: Error) => void): Promise send( - msg: unknown, + msg: string | Buffer, offset: number, length: number, port?: number, @@ -27,7 +35,7 @@ export class Socket extends EventEmitter { ): Promise send( - msg: unknown, + msg: string | Buffer, offset: number, length: number, port: number, @@ -35,7 +43,7 @@ export class Socket extends EventEmitter { ): Promise send( - msg: unknown, + msg: string | Buffer, offset: number, length: number, address: string, @@ -43,29 +51,33 @@ export class Socket extends EventEmitter { ): Promise send( - msg: unknown, + msg: string | Buffer, offset: number, address: string, cb: (err: Error) => void ): Promise send( - msg: unknown, + msg: string | Buffer, offset: number, length: number, cb: (err: Error) => void ): Promise - send(msg: unknown, port: number, address: string): Promise + send(msg: string | Buffer, port: number, address: string): Promise - send(msg: unknown, port: number, cb?: (err: Error) => void): Promise + send( + msg: string | Buffer, + port: number, + cb?: (err: Error) => void + ): Promise - send(msg: unknown, address: string): Promise + send(msg: string | Buffer, address: string): Promise - send(msg: unknown, cb: (err: Error) => void): Promise + send(msg: string | Buffer, cb: (err: Error) => void): Promise } -export function createSocket( +export function createSocket( opts?: { ipv6Only?: boolean; reuseAddress?: boolean } | string, - cb?: (message: unknown) => void -): Socket + cb?: (message: Buffer, address: rinfo) => void +): Socket diff --git a/package.json b/package.json index 488f7d7..a98a996 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,12 @@ "udx-native": "^1.11.2" }, "devDependencies": { + "bare-buffer": "^2.7.1", "brittle": "^3.7.0", "prettier": "^3.4.0", "prettier-config-standard": "^7.0.0" + }, + "peerDependencies": { + "bare-buffer": "^2.7.1" } } From 388555c2545fc11a4820a152d09c59568405ce33 Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Mon, 6 Jan 2025 13:25:36 -0300 Subject: [PATCH 4/6] Add EventMap --- index.d.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index c43febc..fc76c18 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,7 +8,13 @@ export interface rinfo { size: number } -export class Socket extends EventEmitter { +export class Socket extends EventEmitter<{ + close: [] + connect: [] + error: [Error] + listening: [] + message: [string | Buffer, rinfo] +}> { constructor(opts?: { ipv6Only?: boolean; reuseAddress?: boolean }) address(): { address: string; family: string; port: number } | null @@ -77,7 +83,7 @@ export class Socket extends EventEmitter { send(msg: string | Buffer, cb: (err: Error) => void): Promise } -export function createSocket( +export function createSocket( opts?: { ipv6Only?: boolean; reuseAddress?: boolean } | string, cb?: (message: Buffer, address: rinfo) => void -): Socket +): Socket From 0d855d5d636650f72806536a58297e0a6c952c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Mon, 6 Jan 2025 20:04:45 +0100 Subject: [PATCH 5/6] Small tweaks --- index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index fc76c18..9eaf4d2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,7 @@ import EventEmitter from 'bare-events' import Buffer from 'bare-buffer' -export interface rinfo { +export interface RemoteInfo { address: string family: 'IPv4' | 'IPv6' port: number @@ -11,9 +11,9 @@ export interface rinfo { export class Socket extends EventEmitter<{ close: [] connect: [] - error: [Error] + error: [err: Error] listening: [] - message: [string | Buffer, rinfo] + message: [message: string | Buffer, remote: RemoteInfo] }> { constructor(opts?: { ipv6Only?: boolean; reuseAddress?: boolean }) @@ -85,5 +85,5 @@ export class Socket extends EventEmitter<{ export function createSocket( opts?: { ipv6Only?: boolean; reuseAddress?: boolean } | string, - cb?: (message: Buffer, address: rinfo) => void + cb?: (message: Buffer, remote: RemoteInfo) => void ): Socket From e04368feb5f3b7abb78d7ef238f0efa940128071 Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Mon, 6 Jan 2025 16:39:50 -0300 Subject: [PATCH 6/6] Fix peer dependency range --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a98a996..ed5a9d7 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,6 @@ "prettier-config-standard": "^7.0.0" }, "peerDependencies": { - "bare-buffer": "^2.7.1" + "bare-buffer": "*" } }