Skip to content

Commit

Permalink
typings for embedded:network (io) modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tve authored and mkellner committed Jan 15, 2024
1 parent 4b3cc20 commit 4d5eafd
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/io/tcp/httpsclient/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UDP from "embedded:io/socket/udp";
import Resolver from "embedded:network/dns/resolver/udp";

import HTTPClient from "embedded:network/http/client";
import TLSSocket from "tlssocket";
import TLSSocket from "embedded:io/socket/tlssocket";

const dns = {
io: Resolver,
Expand Down
2 changes: 1 addition & 1 deletion examples/io/tcp/httpsclient/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

import TextDecoder from "text/decoder"
import TLSSocket from "tlssocket";
import TLSSocket from "embedded:io/socket/tlssocket";

const http = new device.network.http.io({
...device.network.http,
Expand Down
13 changes: 13 additions & 0 deletions examples/io/tcp/mqtt/manifest_mqtt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"include": [
"$(MODDABLE)/examples/io/tcp/mqttclient/manifest_mqttclient.json"
],
"modules": {
"embedded:network/mqtt/mqtt": [
"./mqtt"
]
},
"preload": [
"embedded:network/mqtt/mqtt"
]
}
2 changes: 1 addition & 1 deletion examples/io/tcp/tlssocket/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$(MODDABLE)/modules/crypt/tls_419.json"
],
"modules": {
"*": "./tlssocket"
"embedded:io/socket/tlssocket": "./tlssocket"
},
"preload": "tlssocket"
}
12 changes: 12 additions & 0 deletions examples/manifest_typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
],
"embedded:io/socket/*": [
"$(TYPINGS)/embedded_io/socket/*"
],
"embedded:network/http/*": [
"$(TYPINGS)/embedded_network/http/*"
],
"embedded:network/mqtt/*": [
"$(TYPINGS)/embedded_network/mqtt/*"
],
"embedded:network/dns/resolver/*": [
"$(TYPINGS)/embedded_network/dns/resolver/*"
],
"embedded:network/*": [
"$(TYPINGS)/embedded_network/*"
]
}
}
38 changes: 19 additions & 19 deletions typings/embedded_io/socket/tcp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@

declare module "embedded:io/socket/tcp" {
import type { Buffer } from "embedded:io/_common";
class TCP {
constructor(options: ((({
address: string;
} | {
host: string;
}) & {
port: number;
}) | {
from: TCP;
}) & {
nodelay?: boolean;
onReadable?: (this: TCP, bytes: number) => void;
onWritable?: (this: TCP, bytes: number) => void;
onError?: (this: TCP) => void;
format?: "number" | "buffer";
target?: any;
})
export type Options = ((({
address: string;
} | {
host: string;
}) & {
port: number;
}) | {
from: TCP;
}) & {
nodelay?: boolean;
onReadable?: (this: TCP, bytes: number) => void;
onWritable?: (this: TCP, bytes: number) => void;
onError?: (this: TCP) => void;
format?: "number" | "buffer";
target?: any;
};

export default class TCP {
constructor(options: Options)
readonly remoteAddress: string | undefined;
readonly remotePort: number | undefined;
read(byteLength?: number): number | ArrayBuffer;
Expand All @@ -46,6 +48,4 @@ declare module "embedded:io/socket/tcp" {
get format(): "number" | "buffer"
set format(value: "number" | "buffer")
}

export default TCP;
}
16 changes: 16 additions & 0 deletions typings/embedded_io/socket/tlssocket.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
declare module "embedded:io/socket/tlssocket" {
import { Options as TCPOptions } from "embedded:io/socket/tcp"
export type Options = TCPOptions & {
host: string
secure: Record<string, any> // should be called "tls" according to std?
}
export default class TLSSocket {
constructor(options: Options)
close(): undefined
read(count: number|ArrayBufferLike) : undefined|ArrayBufferLike
write(buffer: ArrayBufferLike) : number
set format(format: string)
get format() : string
}
}

37 changes: 37 additions & 0 deletions typings/embedded_network/dns/resolver/udp.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 Shinya Ishikawa
*
* This file is part of the Moddable SDK Tools.
*
* The Moddable SDK Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Moddable SDK Tools. If not, see <http://www.gnu.org/licenses/>.
*
*/

declare module "embedded:network/dns/resolver/udp" {

interface Options {
host: string
onResolved: ((host:string, address:any) => null)
onError: ((host:string) => null)
}

class Resolver {
constructor(options: Record<string, any>)
close(): null
resolve(options: Options): null

}

export default Resolver
}
54 changes: 54 additions & 0 deletions typings/embedded_network/http/client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022 Shinya Ishikawa
*
* This file is part of the Moddable SDK Tools.
*
* The Moddable SDK Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Moddable SDK Tools. If not, see <http://www.gnu.org/licenses/>.
*
*/

declare module "embedded:network/http/client" {
import type { Buffer } from "embedded:io/_common"

export interface ClientOptions {
socket: any
port?:number
host: string
dns: any
onError: (err:any)=>void
}

export interface RequestOptions {
method?: string
path?: string
headers?: Map<string, string|string[]>
onHeaders?: (this: HTTPRequest, status:number, headers:Record<string, string>) => void
onReadable?: (this: HTTPRequest, count: number) => void
onWritable?: (this: HTTPRequest, count: number) => void
onDone?: (this: HTTPRequest, error: Error|null) => void // note: error is empty Error object
}

export interface HTTPRequest {
read(byteLength?: number): ArrayBuffer|undefined;
read(buffer: Buffer): void;
write(value: Buffer|undefined): void;
}

export default class HTTPClient {
constructor(options: ClientOptions)
request(options: RequestOptions): HTTPRequest
close(): void
}

}
74 changes: 74 additions & 0 deletions typings/embedded_network/mqtt/client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
declare module "embedded:network/mqtt/client" {
import type { Buffer } from "embedded:io/_common"

interface onReadableOptions {
more: boolean,
topic?: string,
QoS?: number,
byteLength?: number,
}

interface onControlMessage {
operation: number, // MQTTClient.CONNACK, etc...
id: number,
payload?: Buffer, // for SUBACK
}

interface Will {
topic?: string,
message?: string|Buffer,
QoS?: number,
retain?: boolean,
}

interface Options {
host: string,
port?: number,
user?: string,
password?: string,
id?: string,
clean?: boolean,
will?: any,
keepAlive?: number, // milliseconds
onReadable?: (count: number, options: onReadableOptions) => void,
onWritable?: (count: number) => void,
onControl?: (opcode: number, message: onControlMessage) => void,
onClose?: () => void,
onError?: (error: any) => void,
dns: Record<string, any>,
socket: Record<string, any>,
}

interface WriteOptions {
operation?: number,
id?: number,
topic: string,
QoS?: number,
retain?: boolean,
duplicate?: boolean,
byteLength?: number,
items?: any[], // being lazy here
}

export default class MQTTClient {
constructor(options: Options)
read(count: number): ArrayBuffer
write(data: Buffer, options: WriteOptions): number
close(): void

static CONNECT: 1
static CONNACK: 2
static PUBLISH: 3
static PUBACK: 4
static PUBREC: 5
static PUBREL: 6
static PUBCOMP: 7
static SUBSCRIBE: 8
static SUBACK: 9
static UNSUBSCRIBE: 10
static UNSUBACK: 11
static PINGREQ: 12
static PINGRESP: 13
static DISCONNECT: 14
}
}
Loading

0 comments on commit 4d5eafd

Please sign in to comment.