From 6faf8e5622b8098154511fdfbaf17f41ead4cf1e Mon Sep 17 00:00:00 2001 From: lostrepo Date: Sat, 29 Jun 2024 21:04:14 +0300 Subject: [PATCH] feat(lib/[udp|system].js): added types, a bit of refactoring --- lib/system.js | 29 ++++++++++++++++++++-------- lib/udp.js | 52 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/lib/system.js b/lib/system.js index 3d5e678..f4119c0 100644 --- a/lib/system.js +++ b/lib/system.js @@ -1,10 +1,9 @@ -const { system } = lo.load('system') +// const { system } = lo.load('system') const { cstr, ptr } = lo -const { sysconf, _SC_CLK_TCK } = system - -//const { wrap } = lo +// const { sysconf, _SC_CLK_TCK } = system +/** @param {string[]} args */ function make_args (args) { const argb = new Array(args.length) if (!args.length) return { args: new Uint8Array(0) } @@ -22,6 +21,7 @@ function make_args (args) { function wrapStrError () { const eb = new Uint8Array(1024) + const decoder = new TextDecoder() const { strerror_r } = system system.strerror = (errnum = lo.errno) => { const rc = strerror_r(errnum, eb, 1024) @@ -39,6 +39,7 @@ function wrapCPUTime () { } class SystemError extends Error { + /** @param {string} name */ constructor (name) { const { errno } = lo super(`${name} ${system.strerror(errno)}`) @@ -49,13 +50,25 @@ class SystemError extends Error { /* const sys_assert = (...args) => { - return lo.assert(...[...args, err => console.log(`errno ${lo.errno}\n${strerror()}\n${err.stack}`)]) + return lo.assert(...[...args, err => console.log(`errno ${lo.errno}\n${strerror()}\n${err.stack}`)]) } */ -system.clock_ticks_per_second = sysconf(_SC_CLK_TCK) -const decoder = new TextDecoder() -system.SystemError = SystemError +const system = (() => { + const strerror = (_ = 0) => _ == 0 ? void 0 : `${_}: ${''}` + const cputime = () => new Uint32Array(0) + const system = lo.load('system').system + const clock_ticks_per_second = system.sysconf(system._SC_CLK_TCK) + + return Object.assign({}, system, { + clock_ticks_per_second, + SystemError, + // type placeholders + strerror, + cputime + }) +})() + wrapStrError() wrapCPUTime() diff --git a/lib/udp.js b/lib/udp.js index 594607e..7df4cb2 100644 --- a/lib/udp.js +++ b/lib/udp.js @@ -3,9 +3,9 @@ import { net } from 'lib/net.js' //const { strerror } = system const { assert, ptr, core } = lo -const { +const { sendmsg, recvmsg, connect, listen, - AF_INET, SOCK_DGRAM, SOCK_NONBLOCK, SOCKADDR_LEN, EINPROGRESS + AF_INET, SOCK_DGRAM, SOCK_NONBLOCK, SOCKADDR_LEN } = net const { socket, bind, close } = net const { sockaddr_in } = net.types @@ -19,13 +19,18 @@ class IoVec { this.view = new DataView(this.raw.buffer) } - set payload (u8) { - if (u8 === this.u8) return + /** @param {Uint8Array & Partial>} _u8 */ + set payload (_u8) { + if (_u8 === this.u8) return + const u8 = /**@type {Ptr}*/( + _u8.ptr ? _u8 : ptr(_u8) + ) this.u8 = u8.ptr ? u8 : ptr(u8) this.view.setBigUint64(0, BigInt(u8.ptr), true) this.view.setUint32(8, u8.length, true) } + /** @param {number} len */ set length (len) { this.view.setUint32(8, len, true) } @@ -38,13 +43,18 @@ class MsgHdr { this.view = new DataView(this.raw.buffer) } - set address (sockaddr) { - if (sockaddr === this.sockaddr) return - this.sockaddr = sockaddr.ptr ? sockaddr : ptr(sockaddr) + /** @param {Uint8Array & Partial>} _sockaddr */ + set address (_sockaddr) { + if (_sockaddr === this.sockaddr) return + const sockaddr = /**@type {Ptr}*/( + _sockaddr.ptr ? _sockaddr : ptr(_sockaddr) + ) + this.sockaddr = sockaddr this.view.setBigUint64(0, BigInt(sockaddr.ptr), true) this.view.setUint32(8, sockaddr.length, true) } + /** @param {IoVec} iov */ set payload (iov) { if (iov === this.iov) return this.iov = iov @@ -57,24 +67,34 @@ class MsgHdr { this.view.setUint32(40, u8.length, true) } */ + /** @param {number} flags */ set flags (flags) { this.view.setUint32(48, flags, true) } } class Node { + /** @type {import('lib/loop.d.ts').bindings['Loop'] | undefined} */ loop = undefined fd = -1 msghdr = new MsgHdr() iov = new IoVec() #port = 0 address = '127.0.0.1' + /** @type {ReturnType | undefined} */ peer_address = undefined + /** + * @param {Node['loop']} loop + */ constructor (loop) { if (loop) this.loop = loop } + /** + * @param {string} address + * @param {(fd: number, events: number) => void} on_readable + */ bind (address, on_readable, port = 0, on_error = noop) { assert(this.fd === -1) const { loop } = this @@ -93,6 +113,10 @@ class Node { if (!loop) lo.nextTick(on_readable) } + /** + * @param {string} address + * @param {number} port + */ connect (address, port) { return connect(this.fd, sockaddr_in(address, port), SOCKADDR_LEN) } @@ -101,21 +125,31 @@ class Node { assert(listen(this.fd, 128) === 0) } + /** + * @param {IoVec['payload']} u8 + */ recv (u8, flags = 0) { this.iov.payload = u8 this.iov.length = u8.length return recvmsg(this.fd, this.msghdr.raw, flags) } + /** + * @param {IoVec['payload']} u8 + */ send (u8, size = u8.length, flags = 0) { this.iov.payload = u8 this.iov.length = size return sendmsg(this.fd, this.msghdr.raw, flags) } + /** + * @param {string} address + * @param {number} port + */ peer (address, port) { this.peer_address = sockaddr_in(address, port) - this.msghdr.address = this.peer_address + this.msghdr.address = /**@type {MsgHdr['address']}*/(this.peer_address) } close () { @@ -129,7 +163,7 @@ class Node { const src = sockaddr_in(this.address, this.#port) net.get_sockname(this.fd, src) this.#port = (new DataView(src.buffer)).getUint16(2, true) - return this.#port + return this.#port } }