@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 connekt contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,62 @@
1
- # Temporary Holding Version
1
+ # `@connekt/client` (TypeScript / Node / Electron / Browser)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Zero-dependency TypeScript client for `connekt-daemon` with the same orthogonal API model as the Android and Python SDKs:
4
+
5
+ - **Root `ConnektClient`**: `connect`, `close`, `health`, `peers`, `refreshPeers`, `onPeersChanged`, `subscribeFilter`, `channel(name)`, `onAnyMessage`
6
+ - **Channel `ConnektChannel`**:
7
+ - `send(to, data)` — single `peer_id`, multicast array `string[]`, or `Target.ALL` (`"*"`)
8
+ - `request(to, data?, timeoutMs?)` — strict 1-to-1 RPC returning `Payload`
9
+ - `openStream(to, meta?)` — 0-RTT binary WebSocket stream (`write`, `tryWrite`, `close`, `abort`) supporting single peer, subset array, or `Target.ALL`
10
+ - `onMessage(listener)` / `onRequest(handler)` / `onStream(handler)` — all return `Subscription` (`cancel()`)
11
+ - **Unified types**: `Payload` (`.text`, `.bytes`, `.json()`), `OutboundStream`, `InboundStream` (`AsyncIterable<Payload>`, `meta`, `reject(reason)`, `cancel(reason)`), `Target.ALL`, `Subscription.cancel()`
12
+
13
+ ## Quick Start (Node / Electron Sidecar)
14
+
15
+ ```ts
16
+ import { spawnDaemon, Target } from "@connekt/client/sidecar";
17
+
18
+ const daemon = await spawnDaemon({ binaryPath: "./connekt-daemon" });
19
+ const { client } = daemon;
20
+
21
+ // 1. Subscribe to incoming DATA & streams
22
+ const msgSub = client.channel("network").onMessage((msg) => {
23
+ console.log("HTTP packet:", msg.src, msg.text);
24
+ });
25
+
26
+ const streamSub = client.channel("files").onStream(async (stream) => {
27
+ if (stream.meta.mime === "video/mp4") {
28
+ stream.reject("video/mp4 rejected");
29
+ return;
30
+ }
31
+ for await (const chunk of stream) {
32
+ processChunk(chunk.bytes);
33
+ }
34
+ });
35
+
36
+ // 2. RPC, multicast send, and zero-copy binary streaming
37
+ const peers = await client.refreshPeers();
38
+ const targets = peers.slice(0, 2).map((p) => p.peer_id);
39
+
40
+ const resp = await client.channel("echo").request(targets[0], "hello");
41
+ console.log(resp.text);
42
+
43
+ await client.channel("notice").send(targets, "group-sync");
44
+ await client.channel("notice").send(Target.ALL, "reload");
45
+
46
+ const out = client.channel("files").openStream(targets, { mime: "image/png" });
47
+ await out.write(pngBytes); // auto-chunked into 64 KiB binary WS frames
48
+ out.close();
49
+
50
+ msgSub.cancel();
51
+ streamSub.cancel();
52
+ await daemon.stop();
53
+ ```
54
+
55
+ ## Quick Start (Connect to Running Daemon / Browser)
56
+
57
+ ```ts
58
+ import { ConnektClient } from "@connekt/client";
59
+
60
+ const client = new ConnektClient({ baseUrl: "http://127.0.0.1:9527" });
61
+ await client.connect();
62
+ ```
@@ -0,0 +1,75 @@
1
+ export type PayloadEncoding = "utf8" | "base64";
2
+
3
+ export type TransportKind = "adb" | "usbmux" | "lan" | "relay" | (string & {});
4
+
5
+ export type ErrorCode =
6
+ | "PEER_OFFLINE"
7
+ | "TIMEOUT"
8
+ | "NO_HANDLER"
9
+ | "HANDLER_ERROR"
10
+ | "INVALID_REQUEST"
11
+ | "INTERNAL_ERROR"
12
+ | "STREAM_REJECTED"
13
+ | "STREAM_CANCELLED"
14
+ | (string & {});
15
+
16
+ export declare const Target: {
17
+ readonly PC: "pc";
18
+ readonly ALL: "*";
19
+ };
20
+
21
+ export type TargetInput = string | readonly string[];
22
+
23
+ export type StreamMeta = Record<string, string>;
24
+
25
+ export declare const DEFAULT_STREAM_CHUNK_SIZE: number;
26
+ export declare const textEncoder: TextEncoder;
27
+ export declare const textDecoder: TextDecoder;
28
+
29
+ export declare function bytesToBase64(bytes: Uint8Array): string;
30
+ export declare function base64ToBytes(b64: string): Uint8Array;
31
+
32
+ export declare class Payload {
33
+ readonly bytes: Uint8Array;
34
+ readonly encoding: PayloadEncoding;
35
+ constructor(bytes: Uint8Array, encoding?: PayloadEncoding, text?: string);
36
+ get text(): string;
37
+ get size(): number;
38
+ get isEmpty(): boolean;
39
+ json<T = unknown>(): T;
40
+ static isPayload(value: unknown): value is Payload;
41
+ static of(input: string | Uint8Array | Payload): Payload;
42
+ static json(value: unknown): Payload;
43
+ static readonly EMPTY: Payload;
44
+ }
45
+
46
+ export type PayloadInput = string | Uint8Array | Payload;
47
+
48
+ export declare function toRawBytes(input: PayloadInput): Uint8Array;
49
+
50
+ export interface PeerInfo {
51
+ peer_id: string;
52
+ device_id: string;
53
+ serial?: string;
54
+ package_name: string;
55
+ app_name: string;
56
+ process_name: string;
57
+ pid: number;
58
+ platform: string;
59
+ model: string;
60
+ manufacturer: string;
61
+ os_version: string;
62
+ sdk_version: string;
63
+ transports: string[];
64
+ active_transport: string;
65
+ connected_at_ms: number;
66
+ }
67
+
68
+ export interface Subscription {
69
+ cancel(): void;
70
+ }
71
+
72
+ export declare class ConnektError extends Error {
73
+ readonly code: ErrorCode;
74
+ constructor(code: ErrorCode, message: string);
75
+ }
package/dist/common.js ADDED
@@ -0,0 +1,85 @@
1
+ export const Target = {
2
+ PC: "pc",
3
+ ALL: "*"
4
+ };
5
+ export const DEFAULT_STREAM_CHUNK_SIZE = 64 * 1024;
6
+ export const textEncoder = new TextEncoder();
7
+ export const textDecoder = new TextDecoder();
8
+ export function bytesToBase64(bytes) {
9
+ if (typeof Buffer !== "undefined") {
10
+ return Buffer.from(bytes).toString("base64");
11
+ }
12
+ let binary = "";
13
+ for(let i = 0; i < bytes.length; i++){
14
+ binary += String.fromCharCode(bytes[i]);
15
+ }
16
+ return btoa(binary);
17
+ }
18
+ export function base64ToBytes(b64) {
19
+ if (typeof Buffer !== "undefined") {
20
+ return new Uint8Array(Buffer.from(b64, "base64"));
21
+ }
22
+ const binary = atob(b64);
23
+ const out = new Uint8Array(binary.length);
24
+ for(let i = 0; i < binary.length; i++){
25
+ out[i] = binary.charCodeAt(i);
26
+ }
27
+ return out;
28
+ }
29
+ const PAYLOAD_BRAND = Symbol.for("connekt.Payload");
30
+ export class Payload {
31
+ [PAYLOAD_BRAND] = true;
32
+ bytes;
33
+ encoding;
34
+ cachedText;
35
+ constructor(bytes, encoding = "base64", text){
36
+ this.bytes = bytes;
37
+ this.encoding = encoding;
38
+ this.cachedText = text ?? null;
39
+ }
40
+ get text() {
41
+ if (this.cachedText === null) {
42
+ this.cachedText = textDecoder.decode(this.bytes);
43
+ }
44
+ return this.cachedText;
45
+ }
46
+ get size() {
47
+ return this.bytes.byteLength;
48
+ }
49
+ get isEmpty() {
50
+ return this.bytes.byteLength === 0;
51
+ }
52
+ json() {
53
+ return JSON.parse(this.text);
54
+ }
55
+ static isPayload(value) {
56
+ return value instanceof Payload || typeof value === "object" && value !== null && value[PAYLOAD_BRAND] === true && value.bytes instanceof Uint8Array;
57
+ }
58
+ static of(input) {
59
+ if (input instanceof Payload) return input;
60
+ if (Payload.isPayload(input)) {
61
+ return new Payload(input.bytes, input.encoding, input.encoding === "utf8" ? input.text : undefined);
62
+ }
63
+ if (typeof input === "string") {
64
+ return new Payload(textEncoder.encode(input), "utf8", input);
65
+ }
66
+ return new Payload(input, "base64");
67
+ }
68
+ static json(value) {
69
+ return Payload.of(JSON.stringify(value));
70
+ }
71
+ static EMPTY = new Payload(new Uint8Array(0), "utf8", "");
72
+ }
73
+ export function toRawBytes(input) {
74
+ if (Payload.isPayload(input)) return input.bytes;
75
+ if (typeof input === "string") return textEncoder.encode(input);
76
+ return input;
77
+ }
78
+ export class ConnektError extends Error {
79
+ code;
80
+ constructor(code, message){
81
+ super(`[${code}] ${message}`);
82
+ this.name = "ConnektError";
83
+ this.code = code;
84
+ }
85
+ }
@@ -0,0 +1,122 @@
1
+ import {
2
+ ConnektError,
3
+ DEFAULT_STREAM_CHUNK_SIZE,
4
+ Payload,
5
+ Target,
6
+ type ErrorCode,
7
+ type PayloadEncoding,
8
+ type PayloadInput,
9
+ type PeerInfo,
10
+ type StreamMeta,
11
+ type Subscription,
12
+ type TargetInput,
13
+ type TransportKind,
14
+ } from "./common.js";
15
+
16
+ export {
17
+ ConnektError,
18
+ DEFAULT_STREAM_CHUNK_SIZE,
19
+ Payload,
20
+ Target,
21
+ type ErrorCode,
22
+ type PayloadEncoding,
23
+ type PayloadInput,
24
+ type PeerInfo,
25
+ type StreamMeta,
26
+ type Subscription,
27
+ type TargetInput,
28
+ type TransportKind,
29
+ };
30
+
31
+ export interface HealthInfo {
32
+ status: string;
33
+ version: string;
34
+ web_ui?: boolean;
35
+ uptime_ms: number;
36
+ online_peers: number;
37
+ transports: {
38
+ adb: { enabled: boolean; reverse_port: number };
39
+ usbmux?: { enabled: boolean; port_range: [number, number] };
40
+ lan_ws: { enabled: boolean; listen: string; lan_urls?: string[] };
41
+ bonjour?: { enabled: boolean };
42
+ };
43
+ }
44
+
45
+ export interface IncomingMessage {
46
+ src: string;
47
+ dst: string;
48
+ channel: string;
49
+ payload: Payload;
50
+ text: string;
51
+ bytes: Uint8Array;
52
+ tsMs: number;
53
+ }
54
+
55
+ export interface IncomingRequest {
56
+ reqId: number;
57
+ src: string;
58
+ channel: string;
59
+ payload: Payload;
60
+ text: string;
61
+ bytes: Uint8Array;
62
+ tsMs: number;
63
+ }
64
+
65
+ export interface ConnektClientOptions {
66
+ baseUrl?: string;
67
+ defaultTimeoutMs?: number;
68
+ }
69
+
70
+ export declare class OutboundStream {
71
+ readonly streamId: number;
72
+ readonly channel: string;
73
+ constructor(client: ConnektClient, streamId: number, channel: string);
74
+ get isClosed(): boolean;
75
+ write(data: PayloadInput): Promise<void>;
76
+ tryWrite(data: PayloadInput): boolean;
77
+ close(): void;
78
+ abort(reason?: string): void;
79
+ }
80
+
81
+ export declare class InboundStream implements AsyncIterable<Payload> {
82
+ readonly streamId: number;
83
+ readonly src: string;
84
+ readonly channel: string;
85
+ readonly meta: StreamMeta;
86
+ constructor(
87
+ client: ConnektClient,
88
+ streamId: number,
89
+ src: string,
90
+ channel: string,
91
+ meta: StreamMeta,
92
+ );
93
+ get isFinished(): boolean;
94
+ reject(reason?: string): void;
95
+ cancel(reason?: string): void;
96
+ [Symbol.asyncIterator](): AsyncIterator<Payload>;
97
+ }
98
+
99
+ export declare class ConnektChannel {
100
+ readonly name: string;
101
+ constructor(client: ConnektClient, name: string);
102
+ send(to: TargetInput, data: PayloadInput): Promise<number>;
103
+ request(to: string, data?: PayloadInput, timeoutMs?: number): Promise<Payload>;
104
+ openStream(to: TargetInput, meta?: StreamMeta): OutboundStream;
105
+ onMessage(listener: (msg: IncomingMessage) => void): Subscription;
106
+ onRequest(handler: (req: IncomingRequest) => PayloadInput | Promise<PayloadInput>): Subscription;
107
+ onStream(handler: (stream: InboundStream) => void | Promise<void>): Subscription;
108
+ }
109
+
110
+ export declare class ConnektClient {
111
+ constructor(options?: ConnektClientOptions);
112
+ static connect(options?: ConnektClientOptions): Promise<ConnektClient>;
113
+ get peers(): PeerInfo[];
114
+ channel(name: string): ConnektChannel;
115
+ connect(): Promise<void>;
116
+ close(): void;
117
+ health(): Promise<HealthInfo>;
118
+ refreshPeers(): Promise<PeerInfo[]>;
119
+ onPeersChanged(listener: (peers: PeerInfo[]) => void): Subscription;
120
+ onAnyMessage(listener: (msg: IncomingMessage) => void): Subscription;
121
+ subscribeFilter(channels?: string[], peers?: string[]): void;
122
+ }