This commit is contained in:
2024-07-19 12:38:11 +08:00
parent fccf33d953
commit c216c0a62f
34 changed files with 1302 additions and 1196 deletions

184
types/deno.d.ts vendored
View File

@ -1,109 +1,109 @@
declare namespace Deno {
export const version: {
/** Deno's version. For example: `"1.0.0"` */
deno: string;
/** The V8 version used by Deno. For example: `"8.0.0.0"` */
v8: string;
/** The TypeScript version used by Deno. For example: `"4.0.0"` */
typescript: string;
};
export const version: {
/** Deno's version. For example: `"1.0.0"` */
deno: string;
/** The V8 version used by Deno. For example: `"8.0.0.0"` */
v8: string;
/** The TypeScript version used by Deno. For example: `"4.0.0"` */
typescript: string;
};
export interface NetAddr {
transport: "tcp" | "udp";
hostname: string;
port: number;
}
export interface NetAddr {
transport: 'tcp' | 'udp';
hostname: string;
port: number;
}
export type Addr = NetAddr;
export type Addr = NetAddr;
export interface Closer {
close(): void;
}
export interface Closer {
close(): void;
}
export interface Reader {
/** Reads up to `p.byteLength` bytes into `p`. It resolves to the number of
* bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error
* encountered. Even if `read()` resolves to `n` < `p.byteLength`, it may
* use all of `p` as scratch space during the call. If some data is
* available but not `p.byteLength` bytes, `read()` conventionally resolves
* to what is available instead of waiting for more.
*
* When `read()` encounters end-of-file condition, it resolves to EOF
* (`null`).
*
* When `read()` encounters an error, it rejects with an error.
*
* Callers should always process the `n` > `0` bytes returned before
* considering the EOF (`null`). Doing so correctly handles I/O errors that
* happen after reading some bytes and also both of the allowed EOF
* behaviors.
*
* Implementations should not retain a reference to `p`.
*
* Use `itereateReader` from from https://deno.land/std/streams/conversion.ts to
* turn a Reader into an AsyncIterator.
*/
read(p: Uint8Array): Promise<number | null>;
}
export interface Reader {
/** Reads up to `p.byteLength` bytes into `p`. It resolves to the number of
* bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error
* encountered. Even if `read()` resolves to `n` < `p.byteLength`, it may
* use all of `p` as scratch space during the call. If some data is
* available but not `p.byteLength` bytes, `read()` conventionally resolves
* to what is available instead of waiting for more.
*
* When `read()` encounters end-of-file condition, it resolves to EOF
* (`null`).
*
* When `read()` encounters an error, it rejects with an error.
*
* Callers should always process the `n` > `0` bytes returned before
* considering the EOF (`null`). Doing so correctly handles I/O errors that
* happen after reading some bytes and also both of the allowed EOF
* behaviors.
*
* Implementations should not retain a reference to `p`.
*
* Use `itereateReader` from from https://deno.land/std/streams/conversion.ts to
* turn a Reader into an AsyncIterator.
*/
read(p: Uint8Array): Promise<number | null>;
}
export interface Writer {
/** Writes `p.byteLength` bytes from `p` to the underlying data stream. It
* resolves to the number of bytes written from `p` (`0` <= `n` <=
* `p.byteLength`) or reject with the error encountered that caused the
* write to stop early. `write()` must reject with a non-null error if
* would resolve to `n` < `p.byteLength`. `write()` must not modify the
* slice data, even temporarily.
*
* Implementations should not retain a reference to `p`.
*/
write(p: Uint8Array): Promise<number>;
}
export interface Writer {
/** Writes `p.byteLength` bytes from `p` to the underlying data stream. It
* resolves to the number of bytes written from `p` (`0` <= `n` <=
* `p.byteLength`) or reject with the error encountered that caused the
* write to stop early. `write()` must reject with a non-null error if
* would resolve to `n` < `p.byteLength`. `write()` must not modify the
* slice data, even temporarily.
*
* Implementations should not retain a reference to `p`.
*/
write(p: Uint8Array): Promise<number>;
}
export interface Conn extends Reader, Writer, Closer {
/** The local address of the connection. */
readonly localAddr: Addr;
/** The remote address of the connection. */
readonly remoteAddr: Addr;
/** The resource ID of the connection. */
readonly rid: number;
/** Shuts down (`shutdown(2)`) the write side of the connection. Most
* callers should just use `close()`. */
closeWrite(): Promise<void>;
export interface Conn extends Reader, Writer, Closer {
/** The local address of the connection. */
readonly localAddr: Addr;
/** The remote address of the connection. */
readonly remoteAddr: Addr;
/** The resource ID of the connection. */
readonly rid: number;
/** Shuts down (`shutdown(2)`) the write side of the connection. Most
* callers should just use `close()`. */
closeWrite(): Promise<void>;
readonly readable: ReadableStream<Uint8Array>;
readonly writable: WritableStream<Uint8Array>;
}
readonly readable: ReadableStream<Uint8Array>;
readonly writable: WritableStream<Uint8Array>;
}
/** A generic network listener for stream-oriented protocols. */
export interface Listener extends AsyncIterable<Conn> {
/** Waits for and resolves to the next connection to the `Listener`. */
accept(): Promise<Conn>;
/** Close closes the listener. Any pending accept promises will be rejected
* with errors. */
close(): void;
/** Return the address of the `Listener`. */
readonly addr: Addr;
/** A generic network listener for stream-oriented protocols. */
export interface Listener extends AsyncIterable<Conn> {
/** Waits for and resolves to the next connection to the `Listener`. */
accept(): Promise<Conn>;
/** Close closes the listener. Any pending accept promises will be rejected
* with errors. */
close(): void;
/** Return the address of the `Listener`. */
readonly addr: Addr;
/** Return the rid of the `Listener`. */
readonly rid: number;
/** Return the rid of the `Listener`. */
readonly rid: number;
[Symbol.asyncIterator](): AsyncIterableIterator<Conn>;
}
[Symbol.asyncIterator](): AsyncIterableIterator<Conn>;
}
export interface ListenOptions {
port: number;
}
export interface ListenOptions {
port: number;
}
export function listen(
options: ListenOptions & { transport?: "tcp" }
): Listener;
export function listen(
options: ListenOptions & { transport?: 'tcp' },
): Listener;
export function serveHttp(conn: Conn): HttpConn;
export function serveHttp(conn: Conn): HttpConn;
export interface HttpConn extends AsyncIterable<FetchEvent> {
readonly rid: number;
export interface HttpConn extends AsyncIterable<FetchEvent> {
readonly rid: number;
nextRequest(): Promise<FetchEvent | null>;
close(): void;
}
nextRequest(): Promise<FetchEvent | null>;
close(): void;
}
}

32
types/txiki.d.ts vendored
View File

@ -1,7 +1,7 @@
type Transport = "tcp" | "udp" | "pipe";
type Transport = 'tcp' | 'udp' | 'pipe';
interface ListenOptions {
backlog?: number
backlog?: number;
// Disables dual stack mode.
ipv6Only?: boolean;
// Used on UDP only. Enable address reusing (when binding). What that means is that multiple threads or processes can bind to the same address without error (provided they all set the flag) but only the last one to bind will receive any traffic, in effect "stealing" the port from the previous listener.
@ -9,7 +9,13 @@ interface ListenOptions {
}
declare namespace tjs {
const versions: { curl: string; quickjs: string; tjs: string; uv: string; wasm3: string };
const versions: {
curl: string;
quickjs: string;
tjs: string;
uv: string;
wasm3: string;
};
export interface Address {
readonly family: number;
@ -24,15 +30,15 @@ declare namespace tjs {
readonly readable: ReadableStream<Uint8Array>;
readonly remoteAddress: Address;
readonly writeable: WritableStream<Uint8Array>
readonly writeable: WritableStream<Uint8Array>;
close(): void
read(buf: Uint8Array): Promise<number>
write(buf: Uint8Array): Promise<number>
close(): void;
read(buf: Uint8Array): Promise<number>;
write(buf: Uint8Array): Promise<number>;
setKeepAlive(enable?: boolean): void
setNoDelay(enable?: boolean): void
shutdown(): void
setKeepAlive(enable?: boolean): void;
setNoDelay(enable?: boolean): void;
shutdown(): void;
}
export interface Listener extends AsyncIterable<Connection> {
@ -48,6 +54,6 @@ declare namespace tjs {
transport: Transport,
host: string,
port?: string | number,
options?: ListenOptions
): Promise<Listener>
}
options?: ListenOptions,
): Promise<Listener>;
}