@connekt/client 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +61 -2
- package/dist/common.d.ts +75 -0
- package/dist/common.js +85 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.js +662 -0
- package/dist/peer.d.ts +158 -0
- package/dist/peer.js +1119 -0
- package/dist/protocol.d.ts +85 -0
- package/dist/protocol.js +109 -0
- package/dist/sidecar.d.ts +49 -0
- package/dist/sidecar.js +90 -0
- package/package.json +56 -4
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { ErrorCode, PeerInfo, StreamMeta, TransportKind } from "./common.js";
|
|
2
|
+
|
|
3
|
+
export declare const MAGIC_0: number;
|
|
4
|
+
export declare const MAGIC_1: number;
|
|
5
|
+
export declare const PROTOCOL_VERSION: number;
|
|
6
|
+
export declare const FIXED_HEADER_LEN: number;
|
|
7
|
+
export declare const DEFAULT_MAX_PAYLOAD_LEN: number;
|
|
8
|
+
export declare const FLAG_IS_ERROR: number;
|
|
9
|
+
|
|
10
|
+
export declare const FrameType: {
|
|
11
|
+
readonly HELLO: 0x01;
|
|
12
|
+
readonly HELLO_ACK: 0x02;
|
|
13
|
+
readonly PING: 0x03;
|
|
14
|
+
readonly PONG: 0x04;
|
|
15
|
+
readonly PEER_LIST: 0x05;
|
|
16
|
+
readonly DATA: 0x10;
|
|
17
|
+
readonly REQ: 0x11;
|
|
18
|
+
readonly RESP: 0x12;
|
|
19
|
+
readonly STREAM_OPEN: 0x20;
|
|
20
|
+
readonly STREAM_CHUNK: 0x21;
|
|
21
|
+
readonly STREAM_CLOSE: 0x22;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type FrameTypeValue = (typeof FrameType)[keyof typeof FrameType];
|
|
25
|
+
|
|
26
|
+
export interface Frame {
|
|
27
|
+
version: number;
|
|
28
|
+
frameType: FrameTypeValue;
|
|
29
|
+
flags: number;
|
|
30
|
+
reqId: number;
|
|
31
|
+
streamId: number;
|
|
32
|
+
header: Uint8Array;
|
|
33
|
+
payload: Uint8Array;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface HelloHeader {
|
|
37
|
+
peer_id: string;
|
|
38
|
+
device_id: string;
|
|
39
|
+
package_name: string;
|
|
40
|
+
app_name: string;
|
|
41
|
+
process_name: string;
|
|
42
|
+
pid: number;
|
|
43
|
+
platform: string;
|
|
44
|
+
model: string;
|
|
45
|
+
manufacturer: string;
|
|
46
|
+
os_version: string;
|
|
47
|
+
transport: TransportKind;
|
|
48
|
+
sdk_version: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface HelloAckHeader {
|
|
52
|
+
hub_id: string;
|
|
53
|
+
hub_version: string;
|
|
54
|
+
accepted_peer_id: string;
|
|
55
|
+
heartbeat_interval_ms: number;
|
|
56
|
+
lan_urls?: string[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface PeerListHeader {
|
|
60
|
+
peers: PeerInfo[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface RouteHeader {
|
|
64
|
+
src?: string;
|
|
65
|
+
dst?: string;
|
|
66
|
+
dsts?: string[];
|
|
67
|
+
ch?: string;
|
|
68
|
+
meta?: StreamMeta;
|
|
69
|
+
err_code?: ErrorCode;
|
|
70
|
+
err_msg?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export declare function makeFrame(opts: {
|
|
74
|
+
frameType: FrameTypeValue;
|
|
75
|
+
flags?: number;
|
|
76
|
+
reqId?: number;
|
|
77
|
+
streamId?: number;
|
|
78
|
+
headerObj?: unknown;
|
|
79
|
+
payload?: Uint8Array;
|
|
80
|
+
}): Frame;
|
|
81
|
+
|
|
82
|
+
export declare function encodeFrame(frame: Frame, maxPayloadLen?: number): Uint8Array;
|
|
83
|
+
export declare function decodeFrame(raw: Uint8Array, maxPayloadLen?: number): Frame;
|
|
84
|
+
export declare function parseFrameHeader<T>(frame: Frame): T;
|
|
85
|
+
export declare function isFrameError(frame: Frame): boolean;
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { ConnektError, textDecoder, textEncoder } from "./common.js";
|
|
2
|
+
export const MAGIC_0 = 0x43;
|
|
3
|
+
export const MAGIC_1 = 0x4b;
|
|
4
|
+
export const PROTOCOL_VERSION = 1;
|
|
5
|
+
export const FIXED_HEADER_LEN = 19;
|
|
6
|
+
export const DEFAULT_MAX_PAYLOAD_LEN = 16 * 1024 * 1024;
|
|
7
|
+
export const FLAG_IS_ERROR = 0x01;
|
|
8
|
+
export const FrameType = {
|
|
9
|
+
HELLO: 0x01,
|
|
10
|
+
HELLO_ACK: 0x02,
|
|
11
|
+
PING: 0x03,
|
|
12
|
+
PONG: 0x04,
|
|
13
|
+
PEER_LIST: 0x05,
|
|
14
|
+
DATA: 0x10,
|
|
15
|
+
REQ: 0x11,
|
|
16
|
+
RESP: 0x12,
|
|
17
|
+
STREAM_OPEN: 0x20,
|
|
18
|
+
STREAM_CHUNK: 0x21,
|
|
19
|
+
STREAM_CLOSE: 0x22
|
|
20
|
+
};
|
|
21
|
+
const VALID_FRAME_TYPES = new Set(Object.values(FrameType));
|
|
22
|
+
const EMPTY_BYTES = new Uint8Array(0);
|
|
23
|
+
export function makeFrame(opts) {
|
|
24
|
+
const header = opts.headerObj !== undefined ? textEncoder.encode(JSON.stringify(opts.headerObj)) : EMPTY_BYTES;
|
|
25
|
+
return {
|
|
26
|
+
version: PROTOCOL_VERSION,
|
|
27
|
+
frameType: opts.frameType,
|
|
28
|
+
flags: opts.flags ?? 0,
|
|
29
|
+
reqId: opts.reqId ?? 0,
|
|
30
|
+
streamId: opts.streamId ?? 0,
|
|
31
|
+
header,
|
|
32
|
+
payload: opts.payload ?? EMPTY_BYTES
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export function encodeFrame(frame, maxPayloadLen = DEFAULT_MAX_PAYLOAD_LEN) {
|
|
36
|
+
if (frame.header.byteLength > 0xffff) {
|
|
37
|
+
throw new ConnektError("INVALID_REQUEST", `header length ${frame.header.byteLength} exceeds 65535`);
|
|
38
|
+
}
|
|
39
|
+
if (frame.payload.byteLength > maxPayloadLen) {
|
|
40
|
+
throw new ConnektError("INVALID_REQUEST", `payload length ${frame.payload.byteLength} exceeds limit ${maxPayloadLen}`);
|
|
41
|
+
}
|
|
42
|
+
const totalLen = FIXED_HEADER_LEN + frame.header.byteLength + frame.payload.byteLength;
|
|
43
|
+
const out = new Uint8Array(totalLen);
|
|
44
|
+
const view = new DataView(out.buffer, out.byteOffset, out.byteLength);
|
|
45
|
+
out[0] = MAGIC_0;
|
|
46
|
+
out[1] = MAGIC_1;
|
|
47
|
+
out[2] = frame.version;
|
|
48
|
+
out[3] = frame.frameType;
|
|
49
|
+
out[4] = frame.flags;
|
|
50
|
+
view.setUint32(5, frame.reqId >>> 0, false);
|
|
51
|
+
view.setUint32(9, frame.streamId >>> 0, false);
|
|
52
|
+
view.setUint16(13, frame.header.byteLength, false);
|
|
53
|
+
view.setUint32(15, frame.payload.byteLength >>> 0, false);
|
|
54
|
+
if (frame.header.byteLength > 0) {
|
|
55
|
+
out.set(frame.header, FIXED_HEADER_LEN);
|
|
56
|
+
}
|
|
57
|
+
if (frame.payload.byteLength > 0) {
|
|
58
|
+
out.set(frame.payload, FIXED_HEADER_LEN + frame.header.byteLength);
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
export function decodeFrame(raw, maxPayloadLen = DEFAULT_MAX_PAYLOAD_LEN) {
|
|
63
|
+
if (raw.byteLength < FIXED_HEADER_LEN) {
|
|
64
|
+
throw new ConnektError("INVALID_REQUEST", `frame too short: ${raw.byteLength} < ${FIXED_HEADER_LEN}`);
|
|
65
|
+
}
|
|
66
|
+
if (raw[0] !== MAGIC_0 || raw[1] !== MAGIC_1) {
|
|
67
|
+
throw new ConnektError("INVALID_REQUEST", `invalid frame magic: 0x${raw[0].toString(16)} 0x${raw[1].toString(16)}`);
|
|
68
|
+
}
|
|
69
|
+
const version = raw[2];
|
|
70
|
+
if (version !== PROTOCOL_VERSION) {
|
|
71
|
+
throw new ConnektError("INVALID_REQUEST", `unsupported protocol version: ${version}`);
|
|
72
|
+
}
|
|
73
|
+
const rawType = raw[3];
|
|
74
|
+
if (!VALID_FRAME_TYPES.has(rawType)) {
|
|
75
|
+
throw new ConnektError("INVALID_REQUEST", `unknown frame type: 0x${rawType.toString(16)}`);
|
|
76
|
+
}
|
|
77
|
+
const flags = raw[4];
|
|
78
|
+
const view = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
79
|
+
const reqId = view.getUint32(5, false);
|
|
80
|
+
const streamId = view.getUint32(9, false);
|
|
81
|
+
const headerLen = view.getUint16(13, false);
|
|
82
|
+
const payloadLen = view.getUint32(15, false);
|
|
83
|
+
if (payloadLen > maxPayloadLen) {
|
|
84
|
+
throw new ConnektError("INVALID_REQUEST", `payload length ${payloadLen} exceeds limit ${maxPayloadLen}`);
|
|
85
|
+
}
|
|
86
|
+
const expectedLen = FIXED_HEADER_LEN + headerLen + payloadLen;
|
|
87
|
+
if (raw.byteLength !== expectedLen) {
|
|
88
|
+
throw new ConnektError("INVALID_REQUEST", `frame length mismatch: got ${raw.byteLength}, expected ${expectedLen}`);
|
|
89
|
+
}
|
|
90
|
+
const headerEnd = FIXED_HEADER_LEN + headerLen;
|
|
91
|
+
return {
|
|
92
|
+
version,
|
|
93
|
+
frameType: rawType,
|
|
94
|
+
flags,
|
|
95
|
+
reqId,
|
|
96
|
+
streamId,
|
|
97
|
+
header: raw.subarray(FIXED_HEADER_LEN, headerEnd),
|
|
98
|
+
payload: raw.subarray(headerEnd, expectedLen)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export function parseFrameHeader(frame) {
|
|
102
|
+
if (frame.header.byteLength === 0) {
|
|
103
|
+
throw new ConnektError("INVALID_REQUEST", "missing JSON header");
|
|
104
|
+
}
|
|
105
|
+
return JSON.parse(textDecoder.decode(frame.header));
|
|
106
|
+
}
|
|
107
|
+
export function isFrameError(frame) {
|
|
108
|
+
return (frame.flags & FLAG_IS_ERROR) !== 0;
|
|
109
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ChildProcess } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
ConnektClient,
|
|
4
|
+
ConnektError,
|
|
5
|
+
InboundStream,
|
|
6
|
+
OutboundStream,
|
|
7
|
+
Payload,
|
|
8
|
+
Target,
|
|
9
|
+
} from "./index.js";
|
|
10
|
+
|
|
11
|
+
export { ConnektClient, ConnektError, InboundStream, OutboundStream, Payload, Target };
|
|
12
|
+
|
|
13
|
+
export interface SpawnDaemonOptions {
|
|
14
|
+
binaryPath?: string;
|
|
15
|
+
listen?: string;
|
|
16
|
+
noAdb?: boolean;
|
|
17
|
+
noUsbmux?: boolean;
|
|
18
|
+
noLanWs?: boolean;
|
|
19
|
+
noUdpDiscovery?: boolean;
|
|
20
|
+
noBonjour?: boolean;
|
|
21
|
+
adbReversePort?: number;
|
|
22
|
+
startupTimeoutMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface DaemonReadyInfo {
|
|
26
|
+
event: "ready";
|
|
27
|
+
version: string;
|
|
28
|
+
web_ui?: boolean;
|
|
29
|
+
addr: string;
|
|
30
|
+
http_url: string;
|
|
31
|
+
api_url: string;
|
|
32
|
+
control_ws_url: string;
|
|
33
|
+
device_ws_url: string;
|
|
34
|
+
lan_urls?: string[];
|
|
35
|
+
adb_enabled: boolean;
|
|
36
|
+
usbmux_enabled?: boolean;
|
|
37
|
+
lan_ws_enabled: boolean;
|
|
38
|
+
bonjour_enabled?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export declare class ManagedDaemon {
|
|
42
|
+
readonly ready: DaemonReadyInfo;
|
|
43
|
+
readonly client: ConnektClient;
|
|
44
|
+
readonly child: ChildProcess;
|
|
45
|
+
constructor(ready: DaemonReadyInfo, client: ConnektClient, child: ChildProcess);
|
|
46
|
+
stop(): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export declare function spawnDaemon(options?: SpawnDaemonOptions): Promise<ManagedDaemon>;
|
package/dist/sidecar.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { ConnektClient, ConnektError, InboundStream, OutboundStream, Payload, Target } from "./index.js";
|
|
4
|
+
export { ConnektClient, ConnektError, InboundStream, OutboundStream, Payload, Target };
|
|
5
|
+
export class ManagedDaemon {
|
|
6
|
+
ready;
|
|
7
|
+
client;
|
|
8
|
+
child;
|
|
9
|
+
constructor(ready, client, child){
|
|
10
|
+
this.ready = ready;
|
|
11
|
+
this.client = client;
|
|
12
|
+
this.child = child;
|
|
13
|
+
}
|
|
14
|
+
async stop() {
|
|
15
|
+
this.client.close();
|
|
16
|
+
if (this.child.exitCode === null && !this.child.killed) {
|
|
17
|
+
await new Promise((resolve)=>{
|
|
18
|
+
const timer = setTimeout(()=>{
|
|
19
|
+
this.child.kill("SIGKILL");
|
|
20
|
+
resolve();
|
|
21
|
+
}, 2_000);
|
|
22
|
+
this.child.once("exit", ()=>{
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
resolve();
|
|
25
|
+
});
|
|
26
|
+
this.child.kill("SIGINT");
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export async function spawnDaemon(options = {}) {
|
|
32
|
+
const binary = options.binaryPath ?? "connekt-hub";
|
|
33
|
+
const args = [
|
|
34
|
+
"--listen",
|
|
35
|
+
options.listen ?? "127.0.0.1:0",
|
|
36
|
+
"--ready-json"
|
|
37
|
+
];
|
|
38
|
+
if (options.noAdb) args.push("--no-adb");
|
|
39
|
+
if (options.noUsbmux) args.push("--no-usbmux");
|
|
40
|
+
if (options.noLanWs) args.push("--no-lan-ws");
|
|
41
|
+
if (options.noUdpDiscovery) args.push("--no-udp-discovery");
|
|
42
|
+
if (options.noBonjour) args.push("--no-bonjour");
|
|
43
|
+
if (options.adbReversePort !== undefined) {
|
|
44
|
+
args.push("--adb-reverse-port", String(options.adbReversePort));
|
|
45
|
+
}
|
|
46
|
+
const child = spawn(binary, args, {
|
|
47
|
+
stdio: [
|
|
48
|
+
"ignore",
|
|
49
|
+
"pipe",
|
|
50
|
+
"pipe"
|
|
51
|
+
]
|
|
52
|
+
});
|
|
53
|
+
const timeoutMs = options.startupTimeoutMs ?? 10_000;
|
|
54
|
+
const ready = await new Promise((resolve, reject)=>{
|
|
55
|
+
const rl = createInterface({
|
|
56
|
+
input: child.stdout
|
|
57
|
+
});
|
|
58
|
+
let stderrBuf = "";
|
|
59
|
+
child.stderr.on("data", (chunk)=>{
|
|
60
|
+
stderrBuf += chunk.toString("utf8");
|
|
61
|
+
});
|
|
62
|
+
const timer = setTimeout(()=>{
|
|
63
|
+
rl.close();
|
|
64
|
+
child.kill("SIGKILL");
|
|
65
|
+
reject(new ConnektError("TIMEOUT", `connekt-daemon did not become ready within ${timeoutMs}ms`));
|
|
66
|
+
}, timeoutMs);
|
|
67
|
+
child.once("exit", (code)=>{
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
rl.close();
|
|
70
|
+
reject(new ConnektError("INTERNAL_ERROR", `connekt-daemon exited early (code=${code}): ${stderrBuf.trim()}`));
|
|
71
|
+
});
|
|
72
|
+
rl.on("line", (line)=>{
|
|
73
|
+
const trimmed = line.trim();
|
|
74
|
+
if (!trimmed.startsWith("{")) return;
|
|
75
|
+
try {
|
|
76
|
+
const parsed = JSON.parse(trimmed);
|
|
77
|
+
if (parsed.event === "ready") {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
rl.close();
|
|
80
|
+
resolve(parsed);
|
|
81
|
+
}
|
|
82
|
+
} catch {}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
const client = new ConnektClient({
|
|
86
|
+
baseUrl: ready.http_url
|
|
87
|
+
});
|
|
88
|
+
await client.connect();
|
|
89
|
+
return new ManagedDaemon(ready, client, child);
|
|
90
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@connekt/client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zero-dependency TypeScript/JavaScript SDK for Connekt (PC Control API, Browser/H5 Peer, and Node.js Sidecar)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"homepage": "https://github.com/K0ST/Connekt",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/K0ST/Connekt.git",
|
|
12
|
+
"directory": "clients/js"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"connekt",
|
|
16
|
+
"client",
|
|
17
|
+
"peer",
|
|
18
|
+
"sidecar",
|
|
19
|
+
"adb",
|
|
20
|
+
"usbmux",
|
|
21
|
+
"websocket",
|
|
22
|
+
"rpc",
|
|
23
|
+
"stream"
|
|
24
|
+
],
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./peer": {
|
|
33
|
+
"types": "./dist/peer.d.ts",
|
|
34
|
+
"import": "./dist/peer.js"
|
|
35
|
+
},
|
|
36
|
+
"./protocol": {
|
|
37
|
+
"types": "./dist/protocol.d.ts",
|
|
38
|
+
"import": "./dist/protocol.js"
|
|
39
|
+
},
|
|
40
|
+
"./sidecar": {
|
|
41
|
+
"types": "./dist/sidecar.d.ts",
|
|
42
|
+
"import": "./dist/sidecar.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"README.md",
|
|
48
|
+
"LICENSE"
|
|
49
|
+
],
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "node ../../tools/release/build-js.mjs",
|
|
52
|
+
"prepack": "node ../../tools/release/build-js.mjs",
|
|
53
|
+
"test": "node --experimental-strip-types --test test/client.test.ts test/peer.test.ts"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=22.6.0"
|
|
57
|
+
}
|
|
58
|
+
}
|