Skip to content

Commit

Permalink
Add TypeScript typings (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Kasper Isager Dalsgarð <[email protected]>
  • Loading branch information
yassernasc and kasperisager authored Jan 6, 2025
1 parent 7b44dc0 commit 973972b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
89 changes: 89 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import EventEmitter from 'bare-events'
import Buffer from 'bare-buffer'

export interface RemoteInfo {
address: string
family: 'IPv4' | 'IPv6'
port: number
size: number
}

export class Socket extends EventEmitter<{
close: []
connect: []
error: [err: Error]
listening: []
message: [message: string | Buffer, remote: RemoteInfo]
}> {
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?: () => 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?: () => void): void
connect(port: number, cb?: () => void): void

close(cb?: (err: Error) => void): Promise<void>

send(
msg: string | Buffer,
offset: number,
length: number,
port?: number,
address?: string,
cb?: (err: Error) => void
): Promise<void>

send(
msg: string | Buffer,
offset: number,
length: number,
port: number,
cb: (err: Error) => void
): Promise<void>

send(
msg: string | Buffer,
offset: number,
length: number,
address: string,
cb: (err: Error) => void
): Promise<void>

send(
msg: string | Buffer,
offset: number,
address: string,
cb: (err: Error) => void
): Promise<void>

send(
msg: string | Buffer,
offset: number,
length: number,
cb: (err: Error) => void
): Promise<void>

send(msg: string | Buffer, port: number, address: string): Promise<void>

send(
msg: string | Buffer,
port: number,
cb?: (err: Error) => void
): Promise<void>

send(msg: string | Buffer, address: string): Promise<void>

send(msg: string | Buffer, cb: (err: Error) => void): Promise<void>
}

export function createSocket(
opts?: { ipv6Only?: boolean; reuseAddress?: boolean } | string,
cb?: (message: Buffer, remote: RemoteInfo) => void
): Socket
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,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": "*"
}
}

0 comments on commit 973972b

Please sign in to comment.