@adea-ai/runtime-gateway-protocol 1.16.1 → 1.17.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/dist/device-sync.d.ts +2 -0
- package/dist/device-sync.js +163 -43
- package/package.json +3 -3
package/dist/device-sync.d.ts
CHANGED
|
@@ -23,7 +23,9 @@ export interface SyncBlobBinding {
|
|
|
23
23
|
readonly objectId: string;
|
|
24
24
|
}
|
|
25
25
|
export declare function generateSyncDataKey(): Uint8Array;
|
|
26
|
+
export declare function sealSyncBlobStream(dataKey: Uint8Array, binding: SyncBlobBinding, plaintextBytes: number, source: AsyncIterable<Uint8Array>): AsyncGenerator<Uint8Array>;
|
|
26
27
|
export declare function sealSyncBlob(dataKey: Uint8Array, binding: SyncBlobBinding, plaintext: Uint8Array): Promise<Uint8Array>;
|
|
28
|
+
export declare function openSyncBlobStream(dataKey: Uint8Array, binding: SyncBlobBinding, sealed: AsyncIterable<Uint8Array>): AsyncGenerator<Uint8Array>;
|
|
27
29
|
export declare function openSyncBlob(dataKey: Uint8Array, binding: SyncBlobBinding, sealed: Uint8Array): Promise<Uint8Array>;
|
|
28
30
|
export declare function sealSyncDataKey(contentKey: ContentKey, binding: SyncBlobBinding, dataKey: Uint8Array): Promise<{
|
|
29
31
|
ckv: number;
|
package/dist/device-sync.js
CHANGED
|
@@ -94,69 +94,189 @@ function chunkAad(binding, index, count) {
|
|
|
94
94
|
count,
|
|
95
95
|
}));
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
97
|
+
class ByteReader {
|
|
98
|
+
iterator;
|
|
99
|
+
pending = new Uint8Array(0);
|
|
100
|
+
done = false;
|
|
101
|
+
constructor(source) {
|
|
102
|
+
this.iterator = source[Symbol.asyncIterator]();
|
|
103
|
+
}
|
|
104
|
+
async read(size) {
|
|
105
|
+
const parts = [];
|
|
106
|
+
let have = 0;
|
|
107
|
+
while (have < size) {
|
|
108
|
+
if (this.pending.byteLength === 0) {
|
|
109
|
+
if (this.done)
|
|
110
|
+
break;
|
|
111
|
+
const next = await this.iterator.next();
|
|
112
|
+
if (next.done) {
|
|
113
|
+
this.done = true;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
this.pending = next.value;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const take = Math.min(size - have, this.pending.byteLength);
|
|
120
|
+
parts.push(this.pending.subarray(0, take));
|
|
121
|
+
this.pending = this.pending.subarray(take);
|
|
122
|
+
have += take;
|
|
123
|
+
}
|
|
124
|
+
return concatBytes(parts, have);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function concatBytes(parts, total) {
|
|
128
|
+
if (parts.length === 1)
|
|
129
|
+
return parts[0];
|
|
105
130
|
const out = new Uint8Array(total);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
for (let index = 0; index < count; index += 1) {
|
|
111
|
-
const chunk = padded.subarray(index * SYNC_CHUNK_BYTES, (index + 1) * SYNC_CHUNK_BYTES);
|
|
112
|
-
const sealed = new Uint8Array(await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: chunkNonce(index), additionalData: chunkAad(bound, index, count) }, key, chunk));
|
|
113
|
-
out.set(sealed, offset);
|
|
114
|
-
offset += sealed.byteLength;
|
|
131
|
+
let offset = 0;
|
|
132
|
+
for (const part of parts) {
|
|
133
|
+
out.set(part, offset);
|
|
134
|
+
offset += part.byteLength;
|
|
115
135
|
}
|
|
116
|
-
padded.fill(0);
|
|
117
136
|
return out;
|
|
118
137
|
}
|
|
119
|
-
|
|
138
|
+
async function collect(stream) {
|
|
139
|
+
const parts = [];
|
|
140
|
+
let total = 0;
|
|
141
|
+
for await (const part of stream) {
|
|
142
|
+
parts.push(part);
|
|
143
|
+
total += part.byteLength;
|
|
144
|
+
}
|
|
145
|
+
return concatBytes(parts, total);
|
|
146
|
+
}
|
|
147
|
+
async function* once(bytes) {
|
|
148
|
+
yield bytes;
|
|
149
|
+
}
|
|
150
|
+
export async function* sealSyncBlobStream(dataKey, binding, plaintextBytes, source) {
|
|
151
|
+
const bound = parseBinding(binding);
|
|
152
|
+
if (!Number.isSafeInteger(plaintextBytes) || plaintextBytes < 0) {
|
|
153
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
154
|
+
}
|
|
155
|
+
sealedBlobLength(plaintextBytes);
|
|
156
|
+
const padded = padmeLength(plaintextBytes + LENGTH_PREFIX_BYTES);
|
|
157
|
+
const count = Math.max(1, Math.ceil(padded / SYNC_CHUNK_BYTES));
|
|
158
|
+
const key = await importDataKey(dataKey);
|
|
159
|
+
const header = new Uint8Array(HEADER_BYTES);
|
|
160
|
+
header.set(MAGIC, 0);
|
|
161
|
+
header[4] = SYNC_BLOB_VERSION;
|
|
162
|
+
new DataView(header.buffer).setUint32(8, count);
|
|
163
|
+
yield header;
|
|
164
|
+
const buffer = new Uint8Array(SYNC_CHUNK_BYTES);
|
|
165
|
+
let filled = 0;
|
|
166
|
+
let index = 0;
|
|
167
|
+
async function seal() {
|
|
168
|
+
const sealed = new Uint8Array(await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: chunkNonce(index), additionalData: chunkAad(bound, index, count) }, key, buffer.subarray(0, filled)));
|
|
169
|
+
index += 1;
|
|
170
|
+
filled = 0;
|
|
171
|
+
return sealed;
|
|
172
|
+
}
|
|
173
|
+
async function* push(bytes) {
|
|
174
|
+
let offset = 0;
|
|
175
|
+
while (offset < bytes.byteLength) {
|
|
176
|
+
const take = Math.min(bytes.byteLength - offset, SYNC_CHUNK_BYTES - filled);
|
|
177
|
+
buffer.set(bytes.subarray(offset, offset + take), filled);
|
|
178
|
+
filled += take;
|
|
179
|
+
offset += take;
|
|
180
|
+
if (filled === SYNC_CHUNK_BYTES)
|
|
181
|
+
yield await seal();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
const prefix = new Uint8Array(LENGTH_PREFIX_BYTES);
|
|
186
|
+
new DataView(prefix.buffer).setBigUint64(0, BigInt(plaintextBytes));
|
|
187
|
+
yield* push(prefix);
|
|
188
|
+
let received = 0;
|
|
189
|
+
for await (const part of source) {
|
|
190
|
+
received += part.byteLength;
|
|
191
|
+
if (received > plaintextBytes)
|
|
192
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
193
|
+
yield* push(part);
|
|
194
|
+
}
|
|
195
|
+
if (received !== plaintextBytes)
|
|
196
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
197
|
+
const zeros = new Uint8Array(SYNC_CHUNK_BYTES);
|
|
198
|
+
for (let remaining = padded - LENGTH_PREFIX_BYTES - plaintextBytes; remaining > 0; remaining -= SYNC_CHUNK_BYTES) {
|
|
199
|
+
yield* push(zeros.subarray(0, Math.min(remaining, SYNC_CHUNK_BYTES)));
|
|
200
|
+
}
|
|
201
|
+
if (filled > 0)
|
|
202
|
+
yield await seal();
|
|
203
|
+
if (index !== count)
|
|
204
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
buffer.fill(0);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export async function sealSyncBlob(dataKey, binding, plaintext) {
|
|
211
|
+
return collect(sealSyncBlobStream(dataKey, binding, plaintext.byteLength, once(plaintext)));
|
|
212
|
+
}
|
|
213
|
+
export async function* openSyncBlobStream(dataKey, binding, sealed) {
|
|
120
214
|
const bound = parseBinding(binding);
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
215
|
+
const reader = new ByteReader(sealed);
|
|
216
|
+
const header = await reader.read(HEADER_BYTES);
|
|
217
|
+
if (header.byteLength !== HEADER_BYTES ||
|
|
218
|
+
MAGIC.some((byte, index) => header[index] !== byte) ||
|
|
219
|
+
header[4] !== SYNC_BLOB_VERSION ||
|
|
220
|
+
header[5] !== 0 ||
|
|
221
|
+
header[6] !== 0 ||
|
|
222
|
+
header[7] !== 0) {
|
|
127
223
|
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
128
224
|
}
|
|
129
|
-
const count = new DataView(
|
|
130
|
-
const
|
|
131
|
-
if (count < 1 ||
|
|
132
|
-
body < LENGTH_PREFIX_BYTES ||
|
|
133
|
-
body > padmeLength(MAX_SYNC_OBJECT_BYTES + LENGTH_PREFIX_BYTES) ||
|
|
134
|
-
Math.max(1, Math.ceil(body / SYNC_CHUNK_BYTES)) !== count) {
|
|
225
|
+
const count = new DataView(header.buffer, header.byteOffset, header.byteLength).getUint32(8);
|
|
226
|
+
const largestBody = padmeLength(MAX_SYNC_OBJECT_BYTES + LENGTH_PREFIX_BYTES);
|
|
227
|
+
if (count < 1 || count > Math.ceil(largestBody / SYNC_CHUNK_BYTES)) {
|
|
135
228
|
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
136
229
|
}
|
|
137
230
|
const key = await importDataKey(dataKey);
|
|
138
|
-
|
|
139
|
-
let
|
|
231
|
+
let body = 0;
|
|
232
|
+
let length = 0;
|
|
233
|
+
let remaining = 0;
|
|
140
234
|
for (let index = 0; index < count; index += 1) {
|
|
141
|
-
const
|
|
142
|
-
const
|
|
143
|
-
|
|
235
|
+
const last = index === count - 1;
|
|
236
|
+
const fullChunk = SYNC_CHUNK_BYTES + AEAD_TAG_BYTES;
|
|
237
|
+
const chunk = await reader.read(last ? fullChunk + 1 : fullChunk);
|
|
238
|
+
if (chunk.byteLength < AEAD_TAG_BYTES + (index === 0 ? LENGTH_PREFIX_BYTES : 0) ||
|
|
239
|
+
chunk.byteLength > fullChunk ||
|
|
240
|
+
(!last && chunk.byteLength !== fullChunk)) {
|
|
241
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
242
|
+
}
|
|
144
243
|
let opened;
|
|
145
244
|
try {
|
|
146
|
-
opened = await globalThis.crypto.subtle.decrypt({ name: 'AES-GCM', iv: chunkNonce(index), additionalData: chunkAad(bound, index, count) }, key, Uint8Array.from(chunk));
|
|
245
|
+
opened = new Uint8Array(await globalThis.crypto.subtle.decrypt({ name: 'AES-GCM', iv: chunkNonce(index), additionalData: chunkAad(bound, index, count) }, key, Uint8Array.from(chunk)));
|
|
147
246
|
}
|
|
148
247
|
catch {
|
|
149
248
|
throw new DeviceSyncError('SYNC_DECRYPTION_FAILED');
|
|
150
249
|
}
|
|
151
|
-
|
|
250
|
+
let payload = opened;
|
|
251
|
+
if (index === 0) {
|
|
252
|
+
length = Number(new DataView(opened.buffer).getBigUint64(0));
|
|
253
|
+
if (length > MAX_SYNC_OBJECT_BYTES)
|
|
254
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
255
|
+
body = padmeLength(length + LENGTH_PREFIX_BYTES);
|
|
256
|
+
if (Math.max(1, Math.ceil(body / SYNC_CHUNK_BYTES)) !== count) {
|
|
257
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
258
|
+
}
|
|
259
|
+
remaining = length;
|
|
260
|
+
payload = opened.subarray(LENGTH_PREFIX_BYTES);
|
|
261
|
+
}
|
|
262
|
+
const expected = Math.min(SYNC_CHUNK_BYTES, body - index * SYNC_CHUNK_BYTES);
|
|
263
|
+
if (opened.byteLength !== expected)
|
|
264
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
265
|
+
const data = payload.subarray(0, Math.min(remaining, payload.byteLength));
|
|
266
|
+
if (payload.subarray(data.byteLength).some((byte) => byte !== 0)) {
|
|
267
|
+
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
268
|
+
}
|
|
269
|
+
remaining -= data.byteLength;
|
|
270
|
+
if (data.byteLength > 0)
|
|
271
|
+
yield data.slice();
|
|
272
|
+
opened.fill(0);
|
|
152
273
|
}
|
|
153
|
-
|
|
154
|
-
if (length > body - LENGTH_PREFIX_BYTES ||
|
|
155
|
-
padmeLength(length + LENGTH_PREFIX_BYTES) !== body ||
|
|
156
|
-
padded.subarray(LENGTH_PREFIX_BYTES + length).some((byte) => byte !== 0)) {
|
|
274
|
+
if (remaining !== 0) {
|
|
157
275
|
throw new DeviceSyncError('SYNC_BLOB_INVALID');
|
|
158
276
|
}
|
|
159
|
-
|
|
277
|
+
}
|
|
278
|
+
export async function openSyncBlob(dataKey, binding, sealed) {
|
|
279
|
+
return collect(openSyncBlobStream(dataKey, binding, once(sealed)));
|
|
160
280
|
}
|
|
161
281
|
export async function sealSyncDataKey(contentKey, binding, dataKey) {
|
|
162
282
|
const bound = parseBinding(binding);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adea-ai/runtime-gateway-protocol",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.1",
|
|
4
4
|
"description": "Device relay envelopes and workspace content keys shared by the Control Plane and Adea clients.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsc -p tsconfig.json",
|
|
50
|
-
"typecheck": "bun check -p tsconfig.json",
|
|
50
|
+
"typecheck": "bun check -p tsconfig.json && bun check -p tsconfig.test.json",
|
|
51
51
|
"lint": "oxlint src",
|
|
52
52
|
"test": "bun test src"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@adea-ai/contracts": "^
|
|
55
|
+
"@adea-ai/contracts": "^5.0.0",
|
|
56
56
|
"@hpke/core": "1.9.0",
|
|
57
57
|
"@hpke/dhkem-x25519": "1.8.0",
|
|
58
58
|
"zod": "4.6.6"
|