@yefengr/remote-pi 0.9.7 → 0.9.9

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.
Files changed (53) hide show
  1. package/README.md +12 -2
  2. package/dist/pairing/identity-keyring.d.ts +22 -0
  3. package/dist/pairing/identity-keyring.js +94 -0
  4. package/dist/pairing/identity-keyring.js.map +1 -0
  5. package/dist/pairing/identity-lock.d.ts +14 -0
  6. package/dist/pairing/identity-lock.js +116 -0
  7. package/dist/pairing/identity-lock.js.map +1 -0
  8. package/dist/pairing/qr.d.ts +2 -3
  9. package/dist/pairing/qr.js +2 -3
  10. package/dist/pairing/qr.js.map +1 -1
  11. package/dist/pairing/storage.d.ts +19 -61
  12. package/dist/pairing/storage.js +196 -314
  13. package/dist/pairing/storage.js.map +1 -1
  14. package/dist/protocol/types.d.ts +2 -52
  15. package/dist/protocol/v2/codec.d.ts +5 -25
  16. package/dist/protocol/v2/codec.js +3 -206
  17. package/dist/protocol/v2/codec.js.map +1 -1
  18. package/dist/protocol/v2/index.d.ts +1 -0
  19. package/dist/protocol/v2/index.js +1 -0
  20. package/dist/protocol/v2/index.js.map +1 -1
  21. package/dist/protocol/v2/marker.d.ts +40 -0
  22. package/dist/protocol/v2/marker.js +47 -0
  23. package/dist/protocol/v2/marker.js.map +1 -0
  24. package/dist/protocol/v2/schemas.d.ts +3 -2024
  25. package/dist/protocol/v2/schemas.js +3 -433
  26. package/dist/protocol/v2/schemas.js.map +1 -1
  27. package/dist/runtime/owner_router.js +5 -1
  28. package/dist/runtime/owner_router.js.map +1 -1
  29. package/dist/transport/peer_channel.d.ts +2 -11
  30. package/dist/transport/peer_channel.js +7 -11
  31. package/dist/transport/peer_channel.js.map +1 -1
  32. package/dist/transport/relay_client.d.ts +2 -8
  33. package/dist/transport/relay_client.js +3 -2
  34. package/dist/transport/relay_client.js.map +1 -1
  35. package/dist/vendor/protocol/constants.d.ts +4 -0
  36. package/dist/vendor/protocol/constants.js +4 -0
  37. package/dist/vendor/protocol/encoding.d.ts +9 -0
  38. package/dist/vendor/protocol/encoding.js +66 -0
  39. package/dist/vendor/protocol/outer/codec.d.ts +13 -0
  40. package/dist/vendor/protocol/outer/codec.js +105 -0
  41. package/dist/vendor/protocol/outer/index.d.ts +4 -0
  42. package/dist/vendor/protocol/outer/index.js +4 -0
  43. package/dist/vendor/protocol/outer/types.d.ts +120 -0
  44. package/dist/vendor/protocol/outer/types.js +1 -0
  45. package/dist/vendor/protocol/session/codec.d.ts +41 -0
  46. package/dist/vendor/protocol/session/codec.js +236 -0
  47. package/dist/vendor/protocol/session/frames.d.ts +5750 -0
  48. package/dist/vendor/protocol/session/frames.js +406 -0
  49. package/dist/vendor/protocol/session/index.d.ts +3 -0
  50. package/dist/vendor/protocol/session/index.js +3 -0
  51. package/dist/vendor/protocol/session/schema.d.ts +1060 -0
  52. package/dist/vendor/protocol/session/schema.js +302 -0
  53. package/package.json +17 -25
@@ -0,0 +1,236 @@
1
+ import { clientFrameSchema, clientFrameTypes, serverFrameSchema, serverFrameTypes, sessionHistoryChunkFrameSchema, timelineEventFragmentFrameSchema, } from "./frames.js";
2
+ import { historyFragmentSchema, timelineEventFragmentSchema, timelineEventSchema, timelinePartialSchema, } from "./schema.js";
3
+ import { MAX_FRAGMENT_BYTES, MAX_FRAME_BYTES, MAX_HISTORY_CHUNK_BYTES, MAX_WINDOW_BYTES, } from "./schema.js";
4
+ export const DECODE_ERROR_CODES = {
5
+ invalid: "invalid",
6
+ unsupported: "unsupported",
7
+ version: "version",
8
+ schema: "schema",
9
+ size: "size",
10
+ direction: "direction",
11
+ };
12
+ export class DecodeError extends Error {
13
+ code;
14
+ name = "DecodeError";
15
+ constructor(code, message, options) {
16
+ super(message, options);
17
+ this.code = code;
18
+ }
19
+ }
20
+ const textEncoder = new TextEncoder();
21
+ const textDecoder = new TextDecoder("utf-8", { fatal: true });
22
+ export function isRecord(value) {
23
+ return typeof value === "object" && value !== null && !Array.isArray(value);
24
+ }
25
+ function encodeJson(value) {
26
+ try {
27
+ const encoded = JSON.stringify(value);
28
+ if (encoded === undefined)
29
+ throw new Error("value is not JSON serializable");
30
+ return textEncoder.encode(encoded);
31
+ }
32
+ catch (error) {
33
+ if (error instanceof DecodeError)
34
+ throw error;
35
+ throw new DecodeError("invalid", "value is not JSON serializable", { cause: error });
36
+ }
37
+ }
38
+ export function getUtf8ByteLengthV2(value) {
39
+ return encodeJson(value).byteLength;
40
+ }
41
+ function parseRaw(raw, maxBytes = MAX_WINDOW_BYTES) {
42
+ if (typeof raw === "string") {
43
+ const bytes = textEncoder.encode(raw).byteLength;
44
+ if (bytes > maxBytes)
45
+ throw new DecodeError("size", `raw value exceeds ${maxBytes} bytes`);
46
+ try {
47
+ return { value: JSON.parse(raw), bytes };
48
+ }
49
+ catch (error) {
50
+ throw new DecodeError("invalid", "raw string is not valid JSON", { cause: error });
51
+ }
52
+ }
53
+ if (raw instanceof Uint8Array) {
54
+ if (raw.byteLength > maxBytes)
55
+ throw new DecodeError("size", `raw value exceeds ${maxBytes} bytes`);
56
+ try {
57
+ return { value: JSON.parse(textDecoder.decode(raw)), bytes: raw.byteLength };
58
+ }
59
+ catch (error) {
60
+ throw new DecodeError("invalid", "raw bytes are not valid UTF-8 JSON", { cause: error });
61
+ }
62
+ }
63
+ if (isRecord(raw)) {
64
+ const bytes = getUtf8ByteLengthV2(raw);
65
+ if (bytes > maxBytes)
66
+ throw new DecodeError("size", `raw value exceeds ${maxBytes} bytes`);
67
+ return { value: raw, bytes };
68
+ }
69
+ throw new DecodeError("invalid", "raw frame must be an object, JSON string, or Uint8Array");
70
+ }
71
+ export function parseProtocolJsonV2(raw, maxBytes = MAX_WINDOW_BYTES) {
72
+ return parseRaw(raw, maxBytes).value;
73
+ }
74
+ function schemaMessage(error) {
75
+ return error.issues.map((issue) => `${issue.path.join(".") || "<root>"}: ${issue.message}`).join("; ");
76
+ }
77
+ function assertByteLimit(value, bytes, limit = MAX_FRAME_BYTES) {
78
+ if (bytes > limit) {
79
+ throw new DecodeError("size", `${String(value.type)} exceeds ${limit} encoded UTF-8 bytes`);
80
+ }
81
+ }
82
+ function assertVersion(value) {
83
+ if (value.protocol_version !== 2) {
84
+ throw new DecodeError("version", "protocol_version must be 2");
85
+ }
86
+ }
87
+ function assertDirection(type, direction) {
88
+ const knownClient = clientFrameTypes.has(type);
89
+ const knownServer = serverFrameTypes.has(type);
90
+ if (!knownClient && !knownServer) {
91
+ throw new DecodeError("unsupported", `unsupported frame type: ${type}`);
92
+ }
93
+ if ((direction === "client" && !knownClient) || (direction === "server" && !knownServer)) {
94
+ throw new DecodeError("direction", `${type} is not a ${direction} frame`);
95
+ }
96
+ }
97
+ function frameType(value) {
98
+ if (!isRecord(value) || typeof value.type !== "string") {
99
+ throw new DecodeError("invalid", "frame must contain a string type");
100
+ }
101
+ return value.type;
102
+ }
103
+ function parseFrame(raw, direction, schema) {
104
+ const parsed = parseRaw(raw, MAX_FRAME_BYTES);
105
+ const type = frameType(parsed.value);
106
+ assertDirection(type, direction);
107
+ assertVersion(parsed.value);
108
+ const result = schema.safeParse(parsed.value);
109
+ if (!result.success) {
110
+ throw new DecodeError("schema", schemaMessage(result.error), { cause: result.error });
111
+ }
112
+ assertByteLimit(parsed.value, parsed.bytes);
113
+ if (type === "timeline_event_fragment")
114
+ validateFragmentSizeV2(parsed.value);
115
+ if (type === "session_history_chunk")
116
+ validateHistoryChunkSizeV2(parsed.value);
117
+ return result.data;
118
+ }
119
+ export function decodeClientFrameV2(raw) {
120
+ return parseFrame(raw, "client", clientFrameSchema);
121
+ }
122
+ export function decodeServerFrameV2(raw) {
123
+ return parseFrame(raw, "server", serverFrameSchema);
124
+ }
125
+ function encodeFrame(frame, direction) {
126
+ const parsed = direction === "client"
127
+ ? parseFrame(frame, direction, clientFrameSchema)
128
+ : parseFrame(frame, direction, serverFrameSchema);
129
+ const encoded = encodeJson(parsed);
130
+ assertByteLimit(isRecord(parsed) ? parsed : { type: "frame" }, encoded.byteLength);
131
+ return encoded;
132
+ }
133
+ export function encodeClientFrameV2(frame) {
134
+ return encodeFrame(frame, "client");
135
+ }
136
+ export function encodeServerFrameV2(frame) {
137
+ return encodeFrame(frame, "server");
138
+ }
139
+ export function encodeClientFrameTextV2(frame) {
140
+ return textDecoder.decode(encodeClientFrameV2(frame));
141
+ }
142
+ export function encodeServerFrameTextV2(frame) {
143
+ return textDecoder.decode(encodeServerFrameV2(frame));
144
+ }
145
+ function decodeBase64Bytes(value) {
146
+ try {
147
+ const binary = atob(value);
148
+ const bytes = new Uint8Array(binary.length);
149
+ for (let index = 0; index < binary.length; index += 1) {
150
+ bytes[index] = binary.charCodeAt(index);
151
+ }
152
+ return bytes;
153
+ }
154
+ catch (error) {
155
+ throw new DecodeError("schema", "data_base64 is not valid base64", { cause: error });
156
+ }
157
+ }
158
+ export function validateFragmentSizeV2(fragment) {
159
+ const realtimeFrameResult = timelineEventFragmentFrameSchema.safeParse(fragment);
160
+ const realtimeResult = timelineEventFragmentSchema.safeParse(fragment);
161
+ const historyResult = historyFragmentSchema.safeParse(fragment);
162
+ if (!realtimeFrameResult.success && !realtimeResult.success && !historyResult.success) {
163
+ throw new DecodeError("schema", schemaMessage(realtimeFrameResult.error), { cause: realtimeFrameResult.error });
164
+ }
165
+ const dataBase64 = realtimeFrameResult.success
166
+ ? realtimeFrameResult.data.data_base64
167
+ : realtimeResult.success
168
+ ? realtimeResult.data.data_base64
169
+ : historyResult.success
170
+ ? historyResult.data.data_base64
171
+ : undefined;
172
+ if (dataBase64 === undefined)
173
+ throw new DecodeError("schema", "fragment data is missing");
174
+ const bytes = decodeBase64Bytes(dataBase64).byteLength;
175
+ if (bytes > MAX_FRAGMENT_BYTES) {
176
+ throw new DecodeError("size", `fragment exceeds ${MAX_FRAGMENT_BYTES} decoded bytes`);
177
+ }
178
+ return bytes;
179
+ }
180
+ export function validateHistoryChunkSizeV2(chunk) {
181
+ const result = sessionHistoryChunkFrameSchema.safeParse(chunk);
182
+ if (!result.success)
183
+ throw new DecodeError("schema", schemaMessage(result.error), { cause: result.error });
184
+ const bytes = getUtf8ByteLengthV2(result.data);
185
+ if (bytes > MAX_HISTORY_CHUNK_BYTES) {
186
+ throw new DecodeError("size", `history chunk exceeds ${MAX_HISTORY_CHUNK_BYTES} encoded UTF-8 bytes`);
187
+ }
188
+ for (const fragment of result.data.fragments)
189
+ validateFragmentSizeV2(fragment);
190
+ return bytes;
191
+ }
192
+ export function validateWindowSizeV2(events) {
193
+ let total = 0;
194
+ for (const event of events) {
195
+ const result = timelineEventSchema.safeParse(event);
196
+ if (!result.success)
197
+ throw new DecodeError("schema", schemaMessage(result.error), { cause: result.error });
198
+ total += getUtf8ByteLengthV2(result.data);
199
+ if (total > MAX_WINDOW_BYTES) {
200
+ throw new DecodeError("size", `decoded window exceeds ${MAX_WINDOW_BYTES} bytes`);
201
+ }
202
+ }
203
+ return total;
204
+ }
205
+ export function validateEncodedSizeV2(value, maxBytes = MAX_WINDOW_BYTES) {
206
+ const bytes = getUtf8ByteLengthV2(value);
207
+ if (bytes > maxBytes)
208
+ throw new DecodeError("size", `encoded value exceeds ${maxBytes} bytes`);
209
+ return bytes;
210
+ }
211
+ export function parseTimelineEventV2(raw) {
212
+ const value = parseProtocolJsonV2(raw);
213
+ const result = timelineEventSchema.safeParse(value);
214
+ if (!result.success)
215
+ throw new DecodeError("schema", schemaMessage(result.error), { cause: result.error });
216
+ validateEncodedSizeV2(result.data, MAX_WINDOW_BYTES);
217
+ return result.data;
218
+ }
219
+ export function parseTimelinePartialV2(raw) {
220
+ const value = parseProtocolJsonV2(raw);
221
+ const result = timelinePartialSchema.safeParse(value);
222
+ if (!result.success)
223
+ throw new DecodeError("schema", schemaMessage(result.error), { cause: result.error });
224
+ validateEncodedSizeV2(result.data, MAX_HISTORY_CHUNK_BYTES);
225
+ return result.data;
226
+ }
227
+ export function validateFrameSizeV2(raw, maxBytes = MAX_FRAME_BYTES) {
228
+ return parseRaw(raw, maxBytes).bytes;
229
+ }
230
+ export const validateTimelineEventFragmentV2 = validateFragmentSizeV2;
231
+ export const validateSessionHistoryChunkV2 = validateHistoryChunkSizeV2;
232
+ export const validateTimelineWindowV2 = validateWindowSizeV2;
233
+ export const validateFrameSize = validateFrameSizeV2;
234
+ export const validateFragmentSize = validateFragmentSizeV2;
235
+ export const validateSessionHistoryChunkSize = validateHistoryChunkSizeV2;
236
+ export const validateWindowSize = validateWindowSizeV2;