@ikonai/sdk 0.0.1

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/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@ikonai/sdk",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "types": "./index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./index.js",
10
+ "types": "./index.d.ts"
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,63 @@
1
+ import { ProtocolMessage } from '../../../platform-generated/src/index.ts';
2
+ /**
3
+ * Transport interface for Ikon server connections.
4
+ * Designed to support both WebSocket and WebTransport implementations.
5
+ */
6
+ export interface Transport {
7
+ /**
8
+ * Connect to the server and authenticate with the provided auth ticket.
9
+ * @param uri - The WebSocket or WebTransport URI
10
+ * @param authTicket - Binary auth ticket to send immediately after connection
11
+ */
12
+ connect(uri: string, authTicket: Uint8Array): Promise<void>;
13
+ /**
14
+ * Send a protocol message to the server.
15
+ */
16
+ send(message: ProtocolMessage): void;
17
+ /**
18
+ * Close the connection gracefully.
19
+ */
20
+ close(): void;
21
+ /**
22
+ * Check if the transport is currently connected.
23
+ */
24
+ readonly isConnected: boolean;
25
+ }
26
+ /**
27
+ * Callbacks for transport events.
28
+ */
29
+ export interface TransportCallbacks {
30
+ /**
31
+ * Called when a protocol message is received from the server.
32
+ * The SDK will handle keepalive internally; other messages are passed to this callback.
33
+ */
34
+ onProtocolMessage: (message: Uint8Array) => void;
35
+ /**
36
+ * Called when the connection is closed.
37
+ * @param reason - Optional reason for the close
38
+ * @param wasClean - Whether the close was clean (normal)
39
+ */
40
+ onClose: (reason?: string, wasClean?: boolean) => void;
41
+ /**
42
+ * Called when an error occurs.
43
+ */
44
+ onError: (error: Error) => void;
45
+ }
46
+ /**
47
+ * Configuration for creating a transport.
48
+ */
49
+ export interface TransportConfig {
50
+ /**
51
+ * Callbacks for transport events.
52
+ */
53
+ callbacks: TransportCallbacks;
54
+ /**
55
+ * Timeout in milliseconds for keepalive detection.
56
+ * If no keepalive is received within this time, the connection is considered dead.
57
+ */
58
+ keepaliveTimeoutMs: number;
59
+ /**
60
+ * Session ID for protocol messages.
61
+ */
62
+ sessionId: number;
63
+ }
@@ -0,0 +1,20 @@
1
+ import { ProtocolMessage } from '../../../platform-generated/src/index.ts';
2
+ import { Transport, TransportConfig } from './transport';
3
+ /**
4
+ * WebSocket transport implementation for Ikon server connections.
5
+ */
6
+ export declare class WebSocketTransport implements Transport {
7
+ private ws;
8
+ private keepaliveTimeout;
9
+ private readonly keepaliveTimeoutMs;
10
+ private readonly callbacks;
11
+ private readonly sessionId;
12
+ constructor(config: TransportConfig);
13
+ get isConnected(): boolean;
14
+ connect(uri: string, authTicket: Uint8Array): Promise<void>;
15
+ send(message: ProtocolMessage): void;
16
+ close(): void;
17
+ private handleProtocolMessage;
18
+ private resetKeepaliveTimeout;
19
+ private clearKeepaliveTimeout;
20
+ }
@@ -0,0 +1,52 @@
1
+ import { ProtocolMessage } from '../../../platform-generated/src/index.ts';
2
+ import { Transport, TransportConfig } from './transport';
3
+ /**
4
+ * Check if WebTransport is supported in the current environment.
5
+ */
6
+ export declare function isWebTransportSupported(): boolean;
7
+ /**
8
+ * WebTransport transport implementation for Ikon server connections.
9
+ *
10
+ * Uses a single bidirectional stream for reliable, ordered message delivery.
11
+ * This matches the WebSocket semantics expected by the Ikon protocol.
12
+ */
13
+ export declare class WebTransportTransport implements Transport {
14
+ private transport;
15
+ private stream;
16
+ private reader;
17
+ private writer;
18
+ private keepaliveTimeout;
19
+ private readLoopActive;
20
+ private readonly keepaliveTimeoutMs;
21
+ private readonly callbacks;
22
+ private readonly sessionId;
23
+ constructor(config: TransportConfig);
24
+ get isConnected(): boolean;
25
+ connect(uri: string, authTicket: Uint8Array): Promise<void>;
26
+ send(message: ProtocolMessage): void;
27
+ close(): void;
28
+ /**
29
+ * Start the read loop to receive messages.
30
+ */
31
+ private startReadLoop;
32
+ /**
33
+ * Handle incoming protocol message.
34
+ */
35
+ private handleProtocolMessage;
36
+ /**
37
+ * Handle connection close.
38
+ */
39
+ private handleClose;
40
+ /**
41
+ * Reset the keepalive timeout.
42
+ */
43
+ private resetKeepaliveTimeout;
44
+ /**
45
+ * Clear the keepalive timeout.
46
+ */
47
+ private clearKeepaliveTimeout;
48
+ /**
49
+ * Clean up resources.
50
+ */
51
+ private cleanup;
52
+ }
@@ -0,0 +1,60 @@
1
+ import { LogEntry, LogLevel } from './logger';
2
+ /**
3
+ * Configuration for the internal log sink.
4
+ */
5
+ export interface LogSinkConfig {
6
+ /**
7
+ * Maximum number of log entries to buffer.
8
+ * Oldest entries are dropped when buffer is full.
9
+ * Default: 1000
10
+ */
11
+ maxBufferSize?: number;
12
+ /**
13
+ * Minimum log level to buffer (for sending to backend).
14
+ * Logs below this level are not buffered.
15
+ * Default: LogLevel.INFO
16
+ */
17
+ minLevel?: LogLevel;
18
+ }
19
+ /**
20
+ * Initialize the internal log sink.
21
+ * Called automatically when SDK is imported.
22
+ */
23
+ export declare function initializeLogSink(config?: LogSinkConfig): void;
24
+ /**
25
+ * Set the callback for sending logs to backend.
26
+ * When set, you can call flushLogs() to send buffered logs.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * setSendLogsCallback((logs) => {
31
+ * // Send logs to backend via your transport
32
+ * connection.sendLogs(logs)
33
+ * })
34
+ * ```
35
+ */
36
+ export declare function setSendLogsCallback(callback: ((logs: LogEntry[]) => void) | null): void;
37
+ /**
38
+ * Get buffered logs without clearing the buffer.
39
+ */
40
+ export declare function getBufferedLogs(): readonly LogEntry[];
41
+ /**
42
+ * Get and clear buffered logs.
43
+ * Use this when you want to send logs and clear the buffer.
44
+ */
45
+ export declare function takeBufferedLogs(): LogEntry[];
46
+ /**
47
+ * Flush buffered logs to the backend via the send callback.
48
+ * Does nothing if no send callback is set or buffer is empty.
49
+ *
50
+ * @returns Number of logs flushed
51
+ */
52
+ export declare function flushLogs(): number;
53
+ /**
54
+ * Clear the log buffer without sending.
55
+ */
56
+ export declare function clearLogBuffer(): void;
57
+ /**
58
+ * Get the current buffer size.
59
+ */
60
+ export declare function getLogBufferSize(): number;
@@ -0,0 +1,69 @@
1
+ export declare enum LogLevel {
2
+ DEBUG = 0,
3
+ INFO = 1,
4
+ WARN = 2,
5
+ ERROR = 3,
6
+ NONE = 4
7
+ }
8
+ /**
9
+ * Log entry structure for forwarding to backend.
10
+ */
11
+ export interface LogEntry {
12
+ timestamp: string;
13
+ level: LogLevel;
14
+ levelName: string;
15
+ component: string;
16
+ message: string;
17
+ args: unknown[];
18
+ }
19
+ /**
20
+ * Log sink callback type.
21
+ * Called for every log message (regardless of log level filtering).
22
+ * The sink can decide what to do with the log (e.g., send to backend).
23
+ */
24
+ export type LogSink = (entry: LogEntry) => void;
25
+ /**
26
+ * Set the minimum log level for console output.
27
+ */
28
+ export declare function setLogLevel(level: LogLevel): void;
29
+ /**
30
+ * Get the current log level.
31
+ */
32
+ export declare function getLogLevel(): LogLevel;
33
+ /**
34
+ * Set a log sink to receive all log messages.
35
+ * Use this to forward logs to the backend.
36
+ * Pass null to remove the sink.
37
+ */
38
+ export declare function setLogSink(sink: LogSink | null): void;
39
+ /**
40
+ * Get the current log sink.
41
+ */
42
+ export declare function getLogSink(): LogSink | null;
43
+ /**
44
+ * Logger interface with component name.
45
+ */
46
+ export interface Logger {
47
+ debug(message: string, ...args: unknown[]): void;
48
+ info(message: string, ...args: unknown[]): void;
49
+ warn(message: string, ...args: unknown[]): void;
50
+ error(message: string, ...args: unknown[]): void;
51
+ }
52
+ /**
53
+ * Create a logger for a specific component.
54
+ *
55
+ * @param component - Component name (e.g., 'IkonClient', 'Connection', 'WebSocketTransport')
56
+ * @returns Logger instance with component name baked in
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const logger = createLogger('MyComponent')
61
+ * logger.info('Starting up') // [MyComponent] Starting up
62
+ * ```
63
+ */
64
+ export declare function createLogger(component: string): Logger;
65
+ /**
66
+ * Default logger for backwards compatibility.
67
+ * New code should use createLogger() with a component name.
68
+ */
69
+ export declare const logger: Logger;