@basmilius/apple-encoding 0.0.80 → 0.0.81
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/index.js +515 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1,515 @@
|
|
|
1
|
-
var
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, {
|
|
5
|
+
get: all[name],
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
set: (newValue) => all[name] = () => newValue
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/ntp.ts
|
|
13
|
+
var exports_ntp = {};
|
|
14
|
+
__export(exports_ntp, {
|
|
15
|
+
parts: () => parts,
|
|
16
|
+
ns: () => ns,
|
|
17
|
+
now: () => now,
|
|
18
|
+
encode: () => encode,
|
|
19
|
+
decode: () => decode
|
|
20
|
+
});
|
|
21
|
+
var EPOCH = 0x83AA7E80n;
|
|
22
|
+
function now() {
|
|
23
|
+
const now2 = ns() / 1000n;
|
|
24
|
+
const seconds = now2 / 1000000n;
|
|
25
|
+
const frac = now2 - seconds * 1000000n;
|
|
26
|
+
return seconds + EPOCH << 32n | (frac << 32n) / 1000000n;
|
|
27
|
+
}
|
|
28
|
+
function ns() {
|
|
29
|
+
return process.hrtime.bigint();
|
|
30
|
+
}
|
|
31
|
+
function parts(ntp) {
|
|
32
|
+
return [
|
|
33
|
+
Number(ntp >> 32n),
|
|
34
|
+
Number(ntp & 0xFFFFFFFFn)
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
function decode(buffer) {
|
|
38
|
+
return {
|
|
39
|
+
proto: buffer.readUInt8(0),
|
|
40
|
+
type: buffer.readUInt8(1),
|
|
41
|
+
seqno: buffer.readUInt16BE(2),
|
|
42
|
+
padding: buffer.readUInt32BE(4),
|
|
43
|
+
reftime_sec: buffer.readUInt32BE(8),
|
|
44
|
+
reftime_frac: buffer.readUInt32BE(12),
|
|
45
|
+
recvtime_sec: buffer.readUInt32BE(16),
|
|
46
|
+
recvtime_frac: buffer.readUInt32BE(20),
|
|
47
|
+
sendtime_sec: buffer.readUInt32BE(24),
|
|
48
|
+
sendtime_frac: buffer.readUInt32BE(28)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function encode(fields) {
|
|
52
|
+
const buffer = Buffer.alloc(32);
|
|
53
|
+
buffer.writeUInt8(fields.proto, 0);
|
|
54
|
+
buffer.writeUInt8(fields.type, 1);
|
|
55
|
+
buffer.writeUInt16BE(fields.seqno, 2);
|
|
56
|
+
buffer.writeUInt32BE(fields.padding, 4);
|
|
57
|
+
buffer.writeUInt32BE(fields.reftime_sec, 8);
|
|
58
|
+
buffer.writeUInt32BE(fields.reftime_frac, 12);
|
|
59
|
+
buffer.writeUInt32BE(fields.recvtime_sec, 16);
|
|
60
|
+
buffer.writeUInt32BE(fields.recvtime_frac, 20);
|
|
61
|
+
buffer.writeUInt32BE(fields.sendtime_sec, 24);
|
|
62
|
+
buffer.writeUInt32BE(fields.sendtime_frac, 28);
|
|
63
|
+
return buffer;
|
|
64
|
+
}
|
|
65
|
+
// src/opack.ts
|
|
66
|
+
var exports_opack = {};
|
|
67
|
+
__export(exports_opack, {
|
|
68
|
+
sizedInt: () => sizedInt,
|
|
69
|
+
int: () => int,
|
|
70
|
+
float: () => float,
|
|
71
|
+
encode: () => encode2,
|
|
72
|
+
decode: () => decode2
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
class OPackSizedInt extends Number {
|
|
76
|
+
size;
|
|
77
|
+
constructor(value, size) {
|
|
78
|
+
super(value);
|
|
79
|
+
this.size = size;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
class OPackFloat {
|
|
84
|
+
value;
|
|
85
|
+
constructor(value) {
|
|
86
|
+
this.value = value;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
class OPackInteger {
|
|
91
|
+
value;
|
|
92
|
+
constructor(value) {
|
|
93
|
+
this.value = value;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function sizedInt(value, size) {
|
|
97
|
+
return new OPackSizedInt(value, size);
|
|
98
|
+
}
|
|
99
|
+
function float(value) {
|
|
100
|
+
return new OPackFloat(value);
|
|
101
|
+
}
|
|
102
|
+
function int(value) {
|
|
103
|
+
return new OPackInteger(value);
|
|
104
|
+
}
|
|
105
|
+
function concat(arr) {
|
|
106
|
+
const total = arr.reduce((s, a) => s + a.length, 0);
|
|
107
|
+
const out = new Uint8Array(total);
|
|
108
|
+
let off = 0;
|
|
109
|
+
for (const a of arr) {
|
|
110
|
+
out.set(a, off);
|
|
111
|
+
off += a.length;
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
function u8(b) {
|
|
116
|
+
return Uint8Array.of(b);
|
|
117
|
+
}
|
|
118
|
+
function uintToLEBytes(value, byteLen) {
|
|
119
|
+
const out = new Uint8Array(byteLen);
|
|
120
|
+
let v = BigInt(value);
|
|
121
|
+
for (let i = 0;i < byteLen; i++) {
|
|
122
|
+
out[i] = Number(v & 0xffn);
|
|
123
|
+
v >>= 8n;
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
function concatUint8Arrays(arrays) {
|
|
128
|
+
const total = arrays.reduce((sum, a) => sum + a.length, 0);
|
|
129
|
+
const out = new Uint8Array(total);
|
|
130
|
+
let offset = 0;
|
|
131
|
+
for (const a of arrays) {
|
|
132
|
+
out.set(a, offset);
|
|
133
|
+
offset += a.length;
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
function encode2(data) {
|
|
138
|
+
return _pack(data, []);
|
|
139
|
+
}
|
|
140
|
+
function _pack(data, objectList) {
|
|
141
|
+
let packed = null;
|
|
142
|
+
if (data === null || data === undefined)
|
|
143
|
+
packed = u8(4);
|
|
144
|
+
else if (typeof data === "boolean")
|
|
145
|
+
packed = u8(data ? 1 : 2);
|
|
146
|
+
else if (data instanceof OPackFloat) {
|
|
147
|
+
const buf = new ArrayBuffer(8);
|
|
148
|
+
new DataView(buf).setFloat64(0, data.value, true);
|
|
149
|
+
packed = concat([u8(54), new Uint8Array(buf)]);
|
|
150
|
+
} else if (data instanceof OPackInteger) {
|
|
151
|
+
const val = data.value;
|
|
152
|
+
if (val < 40)
|
|
153
|
+
packed = u8(8 + val);
|
|
154
|
+
else if (val <= 255)
|
|
155
|
+
packed = concatUint8Arrays([u8(48), uintToLEBytes(val, 1)]);
|
|
156
|
+
else if (val <= 65535)
|
|
157
|
+
packed = concatUint8Arrays([u8(49), uintToLEBytes(val, 2)]);
|
|
158
|
+
else if (val <= 4294967295)
|
|
159
|
+
packed = concatUint8Arrays([u8(50), uintToLEBytes(val, 4)]);
|
|
160
|
+
else
|
|
161
|
+
packed = concatUint8Arrays([u8(51), uintToLEBytes(val, 8)]);
|
|
162
|
+
} else if (typeof data === "number") {
|
|
163
|
+
if (!Number.isInteger(data)) {
|
|
164
|
+
const buf = new ArrayBuffer(8);
|
|
165
|
+
new DataView(buf).setFloat64(0, data, true);
|
|
166
|
+
packed = concat([u8(54), new Uint8Array(buf)]);
|
|
167
|
+
} else {
|
|
168
|
+
if (data < 40)
|
|
169
|
+
packed = u8(8 + data);
|
|
170
|
+
else if (data <= 255)
|
|
171
|
+
packed = concat([u8(48), uintToLEBytes(data, 1)]);
|
|
172
|
+
else if (data <= 65535)
|
|
173
|
+
packed = concat([u8(49), uintToLEBytes(data, 2)]);
|
|
174
|
+
else if (data <= 4294967295)
|
|
175
|
+
packed = concat([u8(50), uintToLEBytes(data, 4)]);
|
|
176
|
+
else
|
|
177
|
+
packed = concat([u8(51), uintToLEBytes(data, 8)]);
|
|
178
|
+
}
|
|
179
|
+
} else if (data instanceof OPackSizedInt) {
|
|
180
|
+
packed = concat([u8(48 + Math.log2(data.size)), uintToLEBytes(data.valueOf(), data.size)]);
|
|
181
|
+
} else if (typeof data === "string") {
|
|
182
|
+
const b = new TextEncoder().encode(data);
|
|
183
|
+
const len = b.length;
|
|
184
|
+
if (len <= 32)
|
|
185
|
+
packed = concat([u8(64 + len), b]);
|
|
186
|
+
else if (len <= 255)
|
|
187
|
+
packed = concat([u8(97), uintToLEBytes(len, 1), b]);
|
|
188
|
+
else if (len <= 65535)
|
|
189
|
+
packed = concat([u8(98), uintToLEBytes(len, 2), b]);
|
|
190
|
+
else if (len <= 16777215)
|
|
191
|
+
packed = concat([u8(99), uintToLEBytes(len, 3), b]);
|
|
192
|
+
else
|
|
193
|
+
packed = concat([u8(100), uintToLEBytes(len, 4), b]);
|
|
194
|
+
} else if (data instanceof Uint8Array || Buffer.isBuffer(data)) {
|
|
195
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
196
|
+
const len = bytes.length;
|
|
197
|
+
if (len <= 32)
|
|
198
|
+
packed = concat([u8(112 + len), bytes]);
|
|
199
|
+
else if (len <= 255)
|
|
200
|
+
packed = concat([u8(145), uintToLEBytes(len, 1), bytes]);
|
|
201
|
+
else if (len <= 65535)
|
|
202
|
+
packed = concat([u8(146), uintToLEBytes(len, 2), bytes]);
|
|
203
|
+
else
|
|
204
|
+
packed = concat([u8(147), uintToLEBytes(len, 4), bytes]);
|
|
205
|
+
} else if (Array.isArray(data)) {
|
|
206
|
+
const body = concat(data.map((d) => _pack(d, objectList)));
|
|
207
|
+
const len = data.length;
|
|
208
|
+
if (len <= 15) {
|
|
209
|
+
packed = concat([u8(208 + len), body]);
|
|
210
|
+
if (len >= 15)
|
|
211
|
+
packed = concat([packed, u8(3)]);
|
|
212
|
+
} else
|
|
213
|
+
packed = concat([u8(223), body, u8(3)]);
|
|
214
|
+
} else if (typeof data === "object") {
|
|
215
|
+
const keys = Object.keys(data);
|
|
216
|
+
const len = keys.length;
|
|
217
|
+
const pairs = [];
|
|
218
|
+
for (const k of keys) {
|
|
219
|
+
pairs.push(_pack(k, objectList));
|
|
220
|
+
pairs.push(_pack(data[k], objectList));
|
|
221
|
+
}
|
|
222
|
+
let header;
|
|
223
|
+
if (len <= 15) {
|
|
224
|
+
header = u8(224 + len);
|
|
225
|
+
} else {
|
|
226
|
+
header = u8(239);
|
|
227
|
+
}
|
|
228
|
+
packed = concatUint8Arrays([header, concatUint8Arrays(pairs)]);
|
|
229
|
+
if (len >= 15 || objectList.some((v) => v === packed)) {
|
|
230
|
+
packed = concatUint8Arrays([packed, u8(129)]);
|
|
231
|
+
}
|
|
232
|
+
} else
|
|
233
|
+
throw new TypeError(typeof data + "");
|
|
234
|
+
const idx = objectList.findIndex((v) => v.length === packed.length && v.every((x, i) => x === packed[i]));
|
|
235
|
+
if (idx >= 0) {
|
|
236
|
+
if (idx < 33)
|
|
237
|
+
packed = u8(160 + idx);
|
|
238
|
+
else if (idx <= 255)
|
|
239
|
+
packed = concat([u8(193), uintToLEBytes(idx, 1)]);
|
|
240
|
+
else if (idx <= 65535)
|
|
241
|
+
packed = concat([u8(194), uintToLEBytes(idx, 2)]);
|
|
242
|
+
else if (idx <= 4294967295)
|
|
243
|
+
packed = concat([u8(195), uintToLEBytes(idx, 4)]);
|
|
244
|
+
else
|
|
245
|
+
packed = concat([u8(196), uintToLEBytes(idx, 8)]);
|
|
246
|
+
} else if (packed.length > 1)
|
|
247
|
+
objectList.push(packed);
|
|
248
|
+
return packed;
|
|
249
|
+
}
|
|
250
|
+
function decode2(data) {
|
|
251
|
+
const [value] = _unpack(data, []);
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
function ensureAvailable(buf, need) {
|
|
255
|
+
if (buf.length < need)
|
|
256
|
+
throw new TypeError(`Not enough data: need ${need} bytes, have ${buf.length}`);
|
|
257
|
+
}
|
|
258
|
+
function readLittleEndian(buf, offset, len) {
|
|
259
|
+
ensureAvailable(buf.subarray(offset), len);
|
|
260
|
+
let v = 0n;
|
|
261
|
+
for (let i = len - 1;i >= 0; i--)
|
|
262
|
+
v = v << 8n | BigInt(buf[offset + i]);
|
|
263
|
+
return Number(v);
|
|
264
|
+
}
|
|
265
|
+
function _unpack(data, objectList) {
|
|
266
|
+
if (data.length === 0)
|
|
267
|
+
throw new TypeError("No data to unpack");
|
|
268
|
+
const tag = data[0];
|
|
269
|
+
let addToObjectList = true;
|
|
270
|
+
let value;
|
|
271
|
+
let rest;
|
|
272
|
+
if (tag === 1) {
|
|
273
|
+
value = true;
|
|
274
|
+
rest = data.subarray(1);
|
|
275
|
+
} else if (tag === 2) {
|
|
276
|
+
value = false;
|
|
277
|
+
rest = data.subarray(1);
|
|
278
|
+
} else if (tag === 4) {
|
|
279
|
+
value = null;
|
|
280
|
+
rest = data.subarray(1);
|
|
281
|
+
} else if (tag === 5) {
|
|
282
|
+
value = data.subarray(1, 17);
|
|
283
|
+
rest = data.subarray(17);
|
|
284
|
+
} else if (tag === 6) {
|
|
285
|
+
value = readLittleEndian(data, 1, 8);
|
|
286
|
+
rest = data.subarray(9);
|
|
287
|
+
} else if (tag >= 8 && tag <= 47) {
|
|
288
|
+
value = tag - 8;
|
|
289
|
+
rest = data.subarray(1);
|
|
290
|
+
} else if (tag === 53) {
|
|
291
|
+
const view = new DataView(data.buffer, data.byteOffset + 1, 4);
|
|
292
|
+
value = view.getFloat32(0, true);
|
|
293
|
+
rest = data.subarray(5);
|
|
294
|
+
} else if (tag === 54) {
|
|
295
|
+
const view = new DataView(data.buffer, data.byteOffset + 1, 8);
|
|
296
|
+
value = view.getFloat64(0, true);
|
|
297
|
+
rest = data.subarray(9);
|
|
298
|
+
} else if ((tag & 240) === 48) {
|
|
299
|
+
const noOfBytes = 2 ** (tag & 15);
|
|
300
|
+
const val = readLittleEndian(data, 1, noOfBytes);
|
|
301
|
+
value = sizedInt(val, noOfBytes);
|
|
302
|
+
rest = data.subarray(1 + noOfBytes);
|
|
303
|
+
} else if (tag >= 64 && tag <= 96) {
|
|
304
|
+
const length = tag - 64;
|
|
305
|
+
value = new TextDecoder().decode(data.subarray(1, 1 + length));
|
|
306
|
+
rest = data.subarray(1 + length);
|
|
307
|
+
} else if (tag >= 97 && tag <= 100) {
|
|
308
|
+
const lenBytes = tag & 15;
|
|
309
|
+
const length = readLittleEndian(data, 1, lenBytes);
|
|
310
|
+
value = new TextDecoder().decode(data.subarray(1 + lenBytes, 1 + lenBytes + length));
|
|
311
|
+
rest = data.subarray(1 + lenBytes + length);
|
|
312
|
+
} else if (tag >= 112 && tag <= 144) {
|
|
313
|
+
const length = tag - 112;
|
|
314
|
+
value = data.subarray(1, 1 + length);
|
|
315
|
+
rest = data.subarray(1 + length);
|
|
316
|
+
} else if (tag >= 145 && tag <= 148) {
|
|
317
|
+
const noOfBytes = 1 << (tag & 15) - 1;
|
|
318
|
+
const length = readLittleEndian(data, 1, noOfBytes);
|
|
319
|
+
const start = 1 + noOfBytes;
|
|
320
|
+
value = data.subarray(start, start + length);
|
|
321
|
+
rest = data.subarray(start + length);
|
|
322
|
+
} else if ((tag & 240) === 208) {
|
|
323
|
+
const count = tag & 15;
|
|
324
|
+
let ptr = data.subarray(1);
|
|
325
|
+
const arr = [];
|
|
326
|
+
if (count === 15) {
|
|
327
|
+
while (ptr[0] !== 3) {
|
|
328
|
+
const [v, r] = _unpack(ptr, objectList);
|
|
329
|
+
arr.push(v);
|
|
330
|
+
ptr = r;
|
|
331
|
+
}
|
|
332
|
+
ptr = ptr.subarray(1);
|
|
333
|
+
} else {
|
|
334
|
+
for (let i = 0;i < count; i++) {
|
|
335
|
+
const [v, r] = _unpack(ptr, objectList);
|
|
336
|
+
arr.push(v);
|
|
337
|
+
ptr = r;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
value = arr;
|
|
341
|
+
rest = ptr;
|
|
342
|
+
addToObjectList = false;
|
|
343
|
+
} else if ((tag & 224) === 224) {
|
|
344
|
+
const count = tag & 15;
|
|
345
|
+
let ptr = data.subarray(1);
|
|
346
|
+
const obj = {};
|
|
347
|
+
if (count === 15) {
|
|
348
|
+
while (ptr[0] !== 3) {
|
|
349
|
+
const [k, r1] = _unpack(ptr, objectList);
|
|
350
|
+
const [v, r2] = _unpack(r1, objectList);
|
|
351
|
+
obj[k] = v;
|
|
352
|
+
ptr = r2;
|
|
353
|
+
}
|
|
354
|
+
ptr = ptr.subarray(1);
|
|
355
|
+
} else {
|
|
356
|
+
for (let i = 0;i < count; i++) {
|
|
357
|
+
const [k, r1] = _unpack(ptr, objectList);
|
|
358
|
+
const [v, r2] = _unpack(r1, objectList);
|
|
359
|
+
obj[k] = v;
|
|
360
|
+
ptr = r2;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
value = obj;
|
|
364
|
+
rest = ptr;
|
|
365
|
+
addToObjectList = false;
|
|
366
|
+
} else if (tag >= 160 && tag <= 192) {
|
|
367
|
+
const idx = tag - 160;
|
|
368
|
+
if (idx >= objectList.length)
|
|
369
|
+
throw new TypeError(`Reference index ${idx} out of range`);
|
|
370
|
+
value = objectList[idx];
|
|
371
|
+
rest = data.subarray(1);
|
|
372
|
+
addToObjectList = false;
|
|
373
|
+
} else if (tag >= 193 && tag <= 196) {
|
|
374
|
+
const len = tag - 192;
|
|
375
|
+
const uid = readLittleEndian(data, 1, len);
|
|
376
|
+
if (uid >= objectList.length)
|
|
377
|
+
throw new TypeError(`UID ${uid} out of range`);
|
|
378
|
+
value = objectList[uid];
|
|
379
|
+
rest = data.subarray(1 + len);
|
|
380
|
+
addToObjectList = false;
|
|
381
|
+
} else {
|
|
382
|
+
throw new TypeError(`Unknown tag 0x${tag.toString(16)}`);
|
|
383
|
+
}
|
|
384
|
+
if (addToObjectList)
|
|
385
|
+
objectList.push(value);
|
|
386
|
+
return [value, rest];
|
|
387
|
+
}
|
|
388
|
+
// src/plist.ts
|
|
389
|
+
var exports_plist = {};
|
|
390
|
+
__export(exports_plist, {
|
|
391
|
+
serialize: () => serialize,
|
|
392
|
+
parse: () => parse
|
|
393
|
+
});
|
|
394
|
+
import { parse } from "@plist/binary.parse";
|
|
395
|
+
import { serialize } from "@plist/binary.serialize";
|
|
396
|
+
// src/tlv8.ts
|
|
397
|
+
var exports_tlv8 = {};
|
|
398
|
+
__export(exports_tlv8, {
|
|
399
|
+
encode: () => encode3,
|
|
400
|
+
decode: () => decode3,
|
|
401
|
+
bail: () => bail,
|
|
402
|
+
Value: () => Value,
|
|
403
|
+
State: () => State,
|
|
404
|
+
Method: () => Method,
|
|
405
|
+
Flags: () => Flags,
|
|
406
|
+
ErrorCode: () => ErrorCode
|
|
407
|
+
});
|
|
408
|
+
var Flags = {
|
|
409
|
+
TransientPairing: 16
|
|
410
|
+
};
|
|
411
|
+
var ErrorCode = {
|
|
412
|
+
Unknown: 1,
|
|
413
|
+
Authentication: 2,
|
|
414
|
+
BackOff: 3,
|
|
415
|
+
MaxPeers: 4,
|
|
416
|
+
MaxTries: 5,
|
|
417
|
+
Unavailable: 6,
|
|
418
|
+
Busy: 7
|
|
419
|
+
};
|
|
420
|
+
var Method = {
|
|
421
|
+
PairSetup: 0,
|
|
422
|
+
PairSetupWithAuth: 1,
|
|
423
|
+
PairVerify: 2,
|
|
424
|
+
AddPairing: 3,
|
|
425
|
+
RemovePairing: 4,
|
|
426
|
+
ListPairing: 5
|
|
427
|
+
};
|
|
428
|
+
var State = {
|
|
429
|
+
M1: 1,
|
|
430
|
+
M2: 2,
|
|
431
|
+
M3: 3,
|
|
432
|
+
M4: 4,
|
|
433
|
+
M5: 5,
|
|
434
|
+
M6: 6
|
|
435
|
+
};
|
|
436
|
+
var Value = {
|
|
437
|
+
Method: 0,
|
|
438
|
+
Identifier: 1,
|
|
439
|
+
Salt: 2,
|
|
440
|
+
PublicKey: 3,
|
|
441
|
+
Proof: 4,
|
|
442
|
+
EncryptedData: 5,
|
|
443
|
+
State: 6,
|
|
444
|
+
Error: 7,
|
|
445
|
+
BackOff: 8,
|
|
446
|
+
Certificate: 9,
|
|
447
|
+
Signature: 10,
|
|
448
|
+
Permissions: 11,
|
|
449
|
+
FragmentData: 12,
|
|
450
|
+
FragmentLast: 13,
|
|
451
|
+
Name: 17,
|
|
452
|
+
Flags: 19
|
|
453
|
+
};
|
|
454
|
+
function bail(data) {
|
|
455
|
+
if (data.has(Value.BackOff)) {
|
|
456
|
+
const buffer = data.get(Value.BackOff);
|
|
457
|
+
const time = buffer.readUintLE(0, buffer.length);
|
|
458
|
+
throw new Error(`Device is busy, try again in ${time} seconds.`);
|
|
459
|
+
}
|
|
460
|
+
if (data.has(Value.Error)) {
|
|
461
|
+
const errorCodeEntries = Object.entries(ErrorCode);
|
|
462
|
+
const errorCode = errorCodeEntries.find(([_, code]) => code === data.get(Value.Error).readUint8());
|
|
463
|
+
if (!errorCode) {
|
|
464
|
+
throw new Error(`Device returned an unknown error code: ${data.get(Value.Error).readUint8()}`);
|
|
465
|
+
}
|
|
466
|
+
throw new Error(`Device returned an error code: ${errorCode[0]}`);
|
|
467
|
+
}
|
|
468
|
+
throw new Error("Invalid response");
|
|
469
|
+
}
|
|
470
|
+
function encode3(entries) {
|
|
471
|
+
const chunks = [];
|
|
472
|
+
for (const [type, valueRaw] of entries) {
|
|
473
|
+
let value;
|
|
474
|
+
if (typeof valueRaw === "number") {
|
|
475
|
+
value = Buffer.from([valueRaw]);
|
|
476
|
+
} else {
|
|
477
|
+
value = valueRaw;
|
|
478
|
+
}
|
|
479
|
+
let offset = 0;
|
|
480
|
+
do {
|
|
481
|
+
const len = Math.min(value.length - offset, 255);
|
|
482
|
+
chunks.push(type, len);
|
|
483
|
+
if (len > 0) {
|
|
484
|
+
for (let i = 0;i < len; i++) {
|
|
485
|
+
chunks.push(value[offset + i]);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
offset += len;
|
|
489
|
+
} while (offset < value.length);
|
|
490
|
+
}
|
|
491
|
+
return Buffer.from(chunks);
|
|
492
|
+
}
|
|
493
|
+
function decode3(buf) {
|
|
494
|
+
const map = new Map;
|
|
495
|
+
let i = 0;
|
|
496
|
+
while (i < buf.length) {
|
|
497
|
+
const type = buf[i++];
|
|
498
|
+
const len = buf[i++];
|
|
499
|
+
const value = new Uint8Array(buf).slice(i, i + len);
|
|
500
|
+
i += len;
|
|
501
|
+
const existing = map.get(type);
|
|
502
|
+
if (existing) {
|
|
503
|
+
map.set(type, Buffer.concat([existing, value]));
|
|
504
|
+
} else {
|
|
505
|
+
map.set(type, Buffer.from(value));
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return map;
|
|
509
|
+
}
|
|
510
|
+
export {
|
|
511
|
+
exports_tlv8 as TLV8,
|
|
512
|
+
exports_plist as Plist,
|
|
513
|
+
exports_opack as OPack,
|
|
514
|
+
exports_ntp as NTP
|
|
515
|
+
};
|