@basmilius/apple-encoding 0.7.2 → 0.8.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/index.js DELETED
@@ -1,1687 +0,0 @@
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/daap.ts
13
- var exports_daap = {};
14
- __export(exports_daap, {
15
- encodeTrackMetadata: () => encodeTrackMetadata,
16
- encodeTagWithSize: () => encodeTagWithSize,
17
- encodeTag: () => encodeTag,
18
- encodePlaybackStatus: () => encodePlaybackStatus,
19
- encodeContainer: () => encodeContainer,
20
- decodeTrackMetadata: () => decodeTrackMetadata,
21
- decodeToObject: () => decodeToObject,
22
- decodeTag: () => decodeTag,
23
- decode: () => decode,
24
- TagType: () => TagType,
25
- ContentCode: () => ContentCode
26
- });
27
- var ContentCode = {
28
- mlit: "dmap.listingitem",
29
- mlcl: "dmap.listing",
30
- msrv: "dmap.serverinforesponse",
31
- mcon: "dmap.container",
32
- miid: "dmap.itemid",
33
- minm: "dmap.itemname",
34
- mikd: "dmap.itemkind",
35
- mper: "dmap.persistentid",
36
- asal: "daap.songalbum",
37
- asar: "daap.songartist",
38
- asaa: "daap.songalbumartist",
39
- ascp: "daap.songcomposer",
40
- asgn: "daap.songgenre",
41
- astm: "daap.songtime",
42
- astn: "daap.songtracknumber",
43
- asdc: "daap.songdisccount",
44
- asdn: "daap.songdiscnumber",
45
- astc: "daap.songtrackcount",
46
- asyr: "daap.songyear",
47
- asbr: "daap.songbitrate",
48
- assr: "daap.songsamplerate",
49
- assz: "daap.songsize",
50
- caps: "daap.songplaystatus",
51
- cash: "daap.songshufflestate",
52
- carp: "daap.songrepeatstate",
53
- cavs: "daap.songvisiblestate",
54
- aePP: "com.apple.itunes.photo-properties"
55
- };
56
- var TagType = {
57
- mlit: 12,
58
- mlcl: 12,
59
- mcon: 12,
60
- msrv: 12,
61
- miid: 5,
62
- minm: 9,
63
- mikd: 1,
64
- mper: 7,
65
- asal: 9,
66
- asar: 9,
67
- asaa: 9,
68
- ascp: 9,
69
- asgn: 9,
70
- astm: 5,
71
- astn: 3,
72
- asdc: 3,
73
- asdn: 3,
74
- astc: 3,
75
- asyr: 3,
76
- asbr: 3,
77
- assr: 5,
78
- assz: 5,
79
- caps: 1,
80
- cash: 1,
81
- carp: 1,
82
- cavs: 1,
83
- aePP: 9
84
- };
85
- function encodeTag(tag, value) {
86
- if (tag.length !== 4) {
87
- throw new Error(`Invalid DAAP tag: ${tag}. Tags must be exactly 4 characters.`);
88
- }
89
- const tagBuffer = Buffer.from(tag, "ascii");
90
- let valueBuffer;
91
- if (typeof value === "string") {
92
- valueBuffer = Buffer.from(value, "utf8");
93
- } else if (typeof value === "bigint") {
94
- valueBuffer = Buffer.alloc(8);
95
- valueBuffer.writeBigUInt64BE(value, 0);
96
- } else if (typeof value === "number") {
97
- if (value <= 255 && value >= 0) {
98
- valueBuffer = Buffer.alloc(1);
99
- valueBuffer.writeUInt8(value, 0);
100
- } else if (value <= 65535 && value >= 0) {
101
- valueBuffer = Buffer.alloc(2);
102
- valueBuffer.writeUInt16BE(value, 0);
103
- } else if (value <= 4294967295 && value >= 0) {
104
- valueBuffer = Buffer.alloc(4);
105
- valueBuffer.writeUInt32BE(value, 0);
106
- } else {
107
- valueBuffer = Buffer.alloc(8);
108
- valueBuffer.writeBigInt64BE(BigInt(value), 0);
109
- }
110
- } else {
111
- valueBuffer = value;
112
- }
113
- const lengthBuffer = Buffer.allocUnsafe(4);
114
- lengthBuffer.writeUInt32BE(valueBuffer.length, 0);
115
- return Buffer.concat([tagBuffer, lengthBuffer, valueBuffer]);
116
- }
117
- function encodeContainer(tag, content) {
118
- if (tag.length !== 4) {
119
- throw new Error(`Invalid DAAP tag: ${tag}. Tags must be exactly 4 characters.`);
120
- }
121
- const tagBuffer = Buffer.from(tag, "ascii");
122
- const lengthBuffer = Buffer.alloc(4);
123
- lengthBuffer.writeUInt32BE(content.length, 0);
124
- return Buffer.concat([tagBuffer, lengthBuffer, content]);
125
- }
126
- function encodePlaybackStatus(status) {
127
- const tags = [];
128
- if (status.playing !== undefined) {
129
- tags.push(encodeTagWithSize("caps", status.playing ? 4 : 3, 1));
130
- }
131
- if (status.shuffle !== undefined) {
132
- tags.push(encodeTagWithSize("cash", status.shuffle ? 1 : 0, 1));
133
- }
134
- if (status.repeat !== undefined) {
135
- let repeatValue = 0;
136
- if (status.repeat === "one")
137
- repeatValue = 1;
138
- else if (status.repeat === "all")
139
- repeatValue = 2;
140
- tags.push(encodeTagWithSize("carp", repeatValue, 1));
141
- }
142
- return Buffer.concat(tags);
143
- }
144
- function encodeTagWithSize(tag, value, byteSize) {
145
- if (tag.length !== 4) {
146
- throw new Error(`Invalid DAAP tag: ${tag}. Tags must be exactly 4 characters.`);
147
- }
148
- const tagBuffer = Buffer.from(tag, "ascii");
149
- const valueBuffer = Buffer.alloc(byteSize);
150
- switch (byteSize) {
151
- case 1:
152
- valueBuffer.writeUInt8(value, 0);
153
- break;
154
- case 2:
155
- valueBuffer.writeUInt16BE(value, 0);
156
- break;
157
- case 4:
158
- valueBuffer.writeUInt32BE(value, 0);
159
- break;
160
- case 8:
161
- valueBuffer.writeBigUInt64BE(BigInt(value), 0);
162
- break;
163
- }
164
- const lengthBuffer = Buffer.alloc(4);
165
- lengthBuffer.writeUInt32BE(byteSize, 0);
166
- return Buffer.concat([tagBuffer, lengthBuffer, valueBuffer]);
167
- }
168
- function encodeTrackMetadata(metadata) {
169
- const tags = [];
170
- if (metadata.title !== undefined) {
171
- tags.push(encodeTag("minm", metadata.title));
172
- }
173
- if (metadata.artist !== undefined) {
174
- tags.push(encodeTag("asar", metadata.artist));
175
- }
176
- if (metadata.albumArtist !== undefined) {
177
- tags.push(encodeTag("asaa", metadata.albumArtist));
178
- }
179
- if (metadata.album !== undefined) {
180
- tags.push(encodeTag("asal", metadata.album));
181
- }
182
- if (metadata.composer !== undefined) {
183
- tags.push(encodeTag("ascp", metadata.composer));
184
- }
185
- if (metadata.genre !== undefined) {
186
- tags.push(encodeTag("asgn", metadata.genre));
187
- }
188
- if (metadata.duration !== undefined) {
189
- tags.push(encodeTagWithSize("astm", Math.floor(metadata.duration * 1000), 4));
190
- }
191
- if (metadata.trackNumber !== undefined) {
192
- tags.push(encodeTagWithSize("astn", metadata.trackNumber, 2));
193
- }
194
- if (metadata.trackCount !== undefined) {
195
- tags.push(encodeTagWithSize("astc", metadata.trackCount, 2));
196
- }
197
- if (metadata.discNumber !== undefined) {
198
- tags.push(encodeTagWithSize("asdn", metadata.discNumber, 2));
199
- }
200
- if (metadata.discCount !== undefined) {
201
- tags.push(encodeTagWithSize("asdc", metadata.discCount, 2));
202
- }
203
- if (metadata.year !== undefined) {
204
- tags.push(encodeTagWithSize("asyr", metadata.year, 2));
205
- }
206
- if (metadata.bitrate !== undefined) {
207
- tags.push(encodeTagWithSize("asbr", metadata.bitrate, 2));
208
- }
209
- if (metadata.sampleRate !== undefined) {
210
- tags.push(encodeTagWithSize("assr", metadata.sampleRate, 4));
211
- }
212
- if (metadata.size !== undefined) {
213
- tags.push(encodeTagWithSize("assz", metadata.size, 4));
214
- }
215
- const content = Buffer.concat(tags);
216
- return encodeContainer("mlit", content);
217
- }
218
- function decode(buffer) {
219
- const tags = [];
220
- let remaining = buffer;
221
- while (remaining.length > 0) {
222
- const result = decodeTag(remaining);
223
- if (!result)
224
- break;
225
- const [tag, rest] = result;
226
- tags.push(tag);
227
- remaining = rest;
228
- }
229
- return tags;
230
- }
231
- function decodeTag(buffer) {
232
- if (buffer.length < 8) {
233
- return null;
234
- }
235
- const tag = buffer.subarray(0, 4).toString("ascii");
236
- const length = buffer.readUInt32BE(4);
237
- if (buffer.length < 8 + length) {
238
- return null;
239
- }
240
- const value = buffer.subarray(8, 8 + length);
241
- const remaining = buffer.subarray(8 + length);
242
- return [{ tag, length, value }, remaining];
243
- }
244
- function decodeToObject(buffer) {
245
- const result = {};
246
- const tags = decode(buffer);
247
- for (const { tag, value } of tags) {
248
- const tagType = TagType[tag];
249
- if (tagType === undefined) {
250
- result[tag] = value;
251
- } else if (tagType === 12) {
252
- result[tag] = decodeToObject(value);
253
- } else if (tagType === 9) {
254
- result[tag] = value.toString("utf8");
255
- } else if (tagType === 1 || tagType === 2) {
256
- result[tag] = value.readUInt8(0);
257
- } else if (tagType === 3 || tagType === 4) {
258
- result[tag] = value.readUInt16BE(0);
259
- } else if (tagType === 5 || tagType === 6) {
260
- result[tag] = value.readUInt32BE(0);
261
- } else if (tagType === 7 || tagType === 8) {
262
- result[tag] = value.readBigUInt64BE(0);
263
- } else {
264
- result[tag] = value;
265
- }
266
- }
267
- return result;
268
- }
269
- function decodeTrackMetadata(buffer) {
270
- const obj = decodeToObject(buffer);
271
- const mlit = obj.mlit ?? obj;
272
- return {
273
- title: mlit.minm,
274
- artist: mlit.asar,
275
- albumArtist: mlit.asaa,
276
- album: mlit.asal,
277
- composer: mlit.ascp,
278
- genre: mlit.asgn,
279
- duration: mlit.astm !== undefined ? mlit.astm / 1000 : undefined,
280
- trackNumber: mlit.astn,
281
- trackCount: mlit.astc,
282
- discNumber: mlit.asdn,
283
- discCount: mlit.asdc,
284
- year: mlit.asyr,
285
- bitrate: mlit.asbr,
286
- sampleRate: mlit.assr,
287
- size: mlit.assz
288
- };
289
- }
290
- // src/ntp.ts
291
- var exports_ntp = {};
292
- __export(exports_ntp, {
293
- parts: () => parts,
294
- ns: () => ns,
295
- now: () => now,
296
- encode: () => encode,
297
- decode: () => decode2
298
- });
299
- var EPOCH = 0x83AA7E80n;
300
- function now() {
301
- const now2 = ns() / 1000n;
302
- const seconds = now2 / 1000000n;
303
- const frac = now2 - seconds * 1000000n;
304
- return seconds + EPOCH << 32n | (frac << 32n) / 1000000n;
305
- }
306
- function ns() {
307
- return process.hrtime.bigint();
308
- }
309
- function parts(ntp) {
310
- return [
311
- Number(ntp >> 32n),
312
- Number(ntp & 0xFFFFFFFFn)
313
- ];
314
- }
315
- function decode2(buffer) {
316
- if (buffer.length < 24) {
317
- throw new RangeError(`NTP packet too small: expected at least 24 bytes, got ${buffer.length}`);
318
- }
319
- return {
320
- proto: buffer.readUInt8(0),
321
- type: buffer.readUInt8(1),
322
- seqno: buffer.readUInt16BE(2),
323
- padding: buffer.readUInt32BE(4),
324
- reftime_sec: buffer.readUInt32BE(8),
325
- reftime_frac: buffer.readUInt32BE(12),
326
- recvtime_sec: buffer.readUInt32BE(16),
327
- recvtime_frac: buffer.readUInt32BE(20),
328
- sendtime_sec: buffer.length >= 28 ? buffer.readUInt32BE(24) : 0,
329
- sendtime_frac: buffer.length >= 32 ? buffer.readUInt32BE(28) : 0
330
- };
331
- }
332
- function encode(fields) {
333
- const buffer = Buffer.allocUnsafe(32);
334
- buffer.writeUInt8(fields.proto, 0);
335
- buffer.writeUInt8(fields.type, 1);
336
- buffer.writeUInt16BE(fields.seqno, 2);
337
- buffer.writeUInt32BE(fields.padding, 4);
338
- buffer.writeUInt32BE(fields.reftime_sec, 8);
339
- buffer.writeUInt32BE(fields.reftime_frac, 12);
340
- buffer.writeUInt32BE(fields.recvtime_sec, 16);
341
- buffer.writeUInt32BE(fields.recvtime_frac, 20);
342
- buffer.writeUInt32BE(fields.sendtime_sec, 24);
343
- buffer.writeUInt32BE(fields.sendtime_frac, 28);
344
- return buffer;
345
- }
346
- // src/opack.ts
347
- var exports_opack = {};
348
- __export(exports_opack, {
349
- sizedInteger: () => sizedInteger,
350
- int: () => int,
351
- float: () => float,
352
- encode: () => encode2,
353
- decode: () => decode3
354
- });
355
- var TAG = {
356
- TRUE: 1,
357
- FALSE: 2,
358
- TERMINATOR: 3,
359
- NULL: 4,
360
- UUID: 5,
361
- TIMESTAMP: 6,
362
- INT_BASE: 8,
363
- INT_INLINE_MAX_VALUE: 39,
364
- INT_MAX_INLINE: 47,
365
- INT_1BYTE: 48,
366
- INT_2BYTE: 49,
367
- INT_4BYTE: 50,
368
- INT_8BYTE: 51,
369
- FLOAT32: 53,
370
- FLOAT64: 54,
371
- STR_BASE: 64,
372
- STR_MAX_INLINE: 96,
373
- STR_1BYTE_LEN: 97,
374
- STR_2BYTE_LEN: 98,
375
- STR_3BYTE_LEN: 99,
376
- STR_4BYTE_LEN: 100,
377
- BYTES_BASE: 112,
378
- BYTES_MAX_INLINE: 144,
379
- BYTES_1BYTE_LEN: 145,
380
- BYTES_2BYTE_LEN: 146,
381
- BYTES_4BYTE_LEN: 147,
382
- REF_BASE: 160,
383
- REF_MAX_INLINE: 192,
384
- REF_1BYTE: 193,
385
- REF_2BYTE: 194,
386
- REF_4BYTE: 195,
387
- REF_8BYTE: 196,
388
- ARRAY_BASE: 208,
389
- ARRAY_VARIABLE: 223,
390
- DICT_BASE: 224,
391
- DICT_VARIABLE: 239,
392
- DICT_TERMINATOR: 129
393
- };
394
- var textDecoder = new TextDecoder;
395
- var textEncoder = new TextEncoder;
396
-
397
- class Float {
398
- get value() {
399
- return this.#value;
400
- }
401
- #value;
402
- constructor(value) {
403
- this.#value = value;
404
- }
405
- }
406
-
407
- class Integer {
408
- get value() {
409
- return this.#value;
410
- }
411
- #value;
412
- constructor(value) {
413
- this.#value = value;
414
- }
415
- }
416
-
417
- class SizedInteger {
418
- get size() {
419
- return this.#size;
420
- }
421
- get value() {
422
- return this.#value;
423
- }
424
- #size;
425
- #value;
426
- constructor(value, size) {
427
- this.#size = size;
428
- this.#value = value;
429
- }
430
- valueOf() {
431
- return this.#value;
432
- }
433
- }
434
- function float(value) {
435
- return new Float(value);
436
- }
437
- function int(value) {
438
- return new Integer(value);
439
- }
440
- function sizedInteger(value, size) {
441
- return new SizedInteger(value, size);
442
- }
443
- function decode3(data) {
444
- const result = _unpackAt(data, 0, []);
445
- return result.value;
446
- }
447
- function encode2(data) {
448
- return _pack(data, []);
449
- }
450
- function u8(b) {
451
- return Uint8Array.of(b);
452
- }
453
- function uintToLEBytes(value, byteLen) {
454
- const out = new Uint8Array(byteLen);
455
- let v = BigInt(value);
456
- for (let i = 0;i < byteLen; i++) {
457
- out[i] = Number(v & 0xffn);
458
- v >>= 8n;
459
- }
460
- return out;
461
- }
462
- function readLittleEndian(buf, offset, len) {
463
- if (len === 1) {
464
- return buf[offset];
465
- } else if (len === 2) {
466
- return buf[offset] | buf[offset + 1] << 8;
467
- } else if (len === 4) {
468
- return buf[offset] | buf[offset + 1] << 8 | buf[offset + 2] << 16 | buf[offset + 3] << 24 >>> 0;
469
- } else {
470
- let v = 0n;
471
- for (let i = len - 1;i >= 0; i--) {
472
- v = v << 8n | BigInt(buf[offset + i]);
473
- }
474
- return Number(v);
475
- }
476
- }
477
- function _pack(data, objectList) {
478
- let packed = null;
479
- if (data === null || data === undefined) {
480
- packed = u8(TAG.NULL);
481
- } else if (typeof data === "boolean") {
482
- packed = u8(data ? TAG.TRUE : TAG.FALSE);
483
- } else if (data instanceof Float) {
484
- packed = new Uint8Array(9);
485
- packed[0] = TAG.FLOAT64;
486
- new DataView(packed.buffer, packed.byteOffset + 1, 8).setFloat64(0, data.value, true);
487
- } else if (data instanceof Integer) {
488
- const val = data.value;
489
- if (val <= TAG.INT_INLINE_MAX_VALUE) {
490
- packed = u8(TAG.INT_BASE + val);
491
- } else if (val <= 255) {
492
- packed = new Uint8Array(2);
493
- packed[0] = TAG.INT_1BYTE;
494
- packed[1] = val;
495
- } else if (val <= 65535) {
496
- packed = new Uint8Array(3);
497
- packed[0] = TAG.INT_2BYTE;
498
- packed[1] = val & 255;
499
- packed[2] = val >> 8 & 255;
500
- } else if (val <= 4294967295) {
501
- packed = new Uint8Array(5);
502
- packed[0] = TAG.INT_4BYTE;
503
- packed[1] = val & 255;
504
- packed[2] = val >> 8 & 255;
505
- packed[3] = val >> 16 & 255;
506
- packed[4] = val >> 24 & 255;
507
- } else {
508
- packed = new Uint8Array(9);
509
- packed[0] = TAG.INT_8BYTE;
510
- packed.set(uintToLEBytes(val, 8), 1);
511
- }
512
- } else if (typeof data === "number") {
513
- if (!Number.isInteger(data)) {
514
- packed = new Uint8Array(9);
515
- packed[0] = TAG.FLOAT64;
516
- new DataView(packed.buffer, packed.byteOffset + 1, 8).setFloat64(0, data, true);
517
- } else {
518
- if (data <= TAG.INT_INLINE_MAX_VALUE) {
519
- packed = u8(TAG.INT_BASE + data);
520
- } else if (data <= 255) {
521
- packed = new Uint8Array(2);
522
- packed[0] = TAG.INT_1BYTE;
523
- packed[1] = data;
524
- } else if (data <= 65535) {
525
- packed = new Uint8Array(3);
526
- packed[0] = TAG.INT_2BYTE;
527
- packed[1] = data & 255;
528
- packed[2] = data >> 8 & 255;
529
- } else if (data <= 4294967295) {
530
- packed = new Uint8Array(5);
531
- packed[0] = TAG.INT_4BYTE;
532
- packed[1] = data & 255;
533
- packed[2] = data >> 8 & 255;
534
- packed[3] = data >> 16 & 255;
535
- packed[4] = data >> 24 & 255;
536
- } else {
537
- packed = new Uint8Array(9);
538
- packed[0] = TAG.INT_8BYTE;
539
- packed.set(uintToLEBytes(data, 8), 1);
540
- }
541
- }
542
- } else if (data instanceof SizedInteger) {
543
- const byteSize = data.size;
544
- packed = new Uint8Array(byteSize + 1);
545
- packed[0] = TAG.INT_1BYTE + (31 - Math.clz32(byteSize));
546
- if (byteSize === 1) {
547
- packed[1] = data.valueOf();
548
- } else if (byteSize === 2) {
549
- const val = data.valueOf();
550
- packed[1] = val & 255;
551
- packed[2] = val >> 8 & 255;
552
- } else if (byteSize === 4) {
553
- const val = data.valueOf();
554
- packed[1] = val & 255;
555
- packed[2] = val >> 8 & 255;
556
- packed[3] = val >> 16 & 255;
557
- packed[4] = val >> 24 & 255;
558
- } else {
559
- packed.set(uintToLEBytes(data.valueOf(), 8), 1);
560
- }
561
- } else if (typeof data === "string") {
562
- const b = textEncoder.encode(data);
563
- const len = b.length;
564
- if (len <= 32) {
565
- packed = new Uint8Array(1 + len);
566
- packed[0] = TAG.STR_BASE + len;
567
- packed.set(b, 1);
568
- } else if (len <= 255) {
569
- packed = new Uint8Array(2 + len);
570
- packed[0] = TAG.STR_1BYTE_LEN;
571
- packed[1] = len;
572
- packed.set(b, 2);
573
- } else if (len <= 65535) {
574
- packed = new Uint8Array(3 + len);
575
- packed[0] = TAG.STR_2BYTE_LEN;
576
- packed[1] = len & 255;
577
- packed[2] = len >> 8 & 255;
578
- packed.set(b, 3);
579
- } else if (len <= 16777215) {
580
- packed = new Uint8Array(4 + len);
581
- packed[0] = TAG.STR_3BYTE_LEN;
582
- packed[1] = len & 255;
583
- packed[2] = len >> 8 & 255;
584
- packed[3] = len >> 16 & 255;
585
- packed.set(b, 4);
586
- } else {
587
- packed = new Uint8Array(5 + len);
588
- packed[0] = TAG.STR_4BYTE_LEN;
589
- packed[1] = len & 255;
590
- packed[2] = len >> 8 & 255;
591
- packed[3] = len >> 16 & 255;
592
- packed[4] = len >> 24 & 255;
593
- packed.set(b, 5);
594
- }
595
- } else if (data instanceof Uint8Array || Buffer.isBuffer(data)) {
596
- const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
597
- const len = bytes.length;
598
- if (len <= 32) {
599
- packed = new Uint8Array(1 + len);
600
- packed[0] = TAG.BYTES_BASE + len;
601
- packed.set(bytes, 1);
602
- } else if (len <= 255) {
603
- packed = new Uint8Array(2 + len);
604
- packed[0] = TAG.BYTES_1BYTE_LEN;
605
- packed[1] = len;
606
- packed.set(bytes, 2);
607
- } else if (len <= 65535) {
608
- packed = new Uint8Array(3 + len);
609
- packed[0] = TAG.BYTES_2BYTE_LEN;
610
- packed[1] = len & 255;
611
- packed[2] = len >> 8 & 255;
612
- packed.set(bytes, 3);
613
- } else {
614
- packed = new Uint8Array(5 + len);
615
- packed[0] = TAG.BYTES_4BYTE_LEN;
616
- packed[1] = len & 255;
617
- packed[2] = len >> 8 & 255;
618
- packed[3] = len >> 16 & 255;
619
- packed[4] = len >> 24 & 255;
620
- packed.set(bytes, 5);
621
- }
622
- } else if (Array.isArray(data)) {
623
- const parts2 = data.map((d) => _pack(d, objectList));
624
- const bodyLen = parts2.reduce((sum, p) => sum + p.length, 0);
625
- const len = data.length;
626
- if (len < 15) {
627
- packed = new Uint8Array(1 + bodyLen);
628
- packed[0] = TAG.ARRAY_BASE + len;
629
- let pos = 1;
630
- for (const part of parts2) {
631
- packed.set(part, pos);
632
- pos += part.length;
633
- }
634
- } else {
635
- packed = new Uint8Array(2 + bodyLen);
636
- packed[0] = TAG.ARRAY_VARIABLE;
637
- let pos = 1;
638
- for (const part of parts2) {
639
- packed.set(part, pos);
640
- pos += part.length;
641
- }
642
- packed[pos] = TAG.TERMINATOR;
643
- }
644
- } else if (typeof data === "object") {
645
- const keys = Object.keys(data);
646
- const len = keys.length;
647
- const parts2 = [];
648
- let bodyLen = 0;
649
- for (const k of keys) {
650
- const keyPacked = _pack(k, objectList);
651
- const valPacked = _pack(data[k], objectList);
652
- parts2.push(keyPacked, valPacked);
653
- bodyLen += keyPacked.length + valPacked.length;
654
- }
655
- const needsTerminator = len >= 15;
656
- packed = new Uint8Array(1 + bodyLen + (needsTerminator ? 1 : 0));
657
- packed[0] = len <= 15 ? TAG.DICT_BASE + len : TAG.DICT_VARIABLE;
658
- let pos = 1;
659
- for (const part of parts2) {
660
- packed.set(part, pos);
661
- pos += part.length;
662
- }
663
- if (needsTerminator) {
664
- packed[pos] = TAG.DICT_TERMINATOR;
665
- }
666
- } else {
667
- throw new TypeError(typeof data + "");
668
- }
669
- const idx = objectList.findIndex((v) => v.length === packed.length && v.every((x, i) => x === packed[i]));
670
- if (idx >= 0) {
671
- if (idx < 33) {
672
- packed = u8(TAG.REF_BASE + idx);
673
- } else if (idx <= 255) {
674
- packed = new Uint8Array(2);
675
- packed[0] = TAG.REF_1BYTE;
676
- packed[1] = idx;
677
- } else if (idx <= 65535) {
678
- packed = new Uint8Array(3);
679
- packed[0] = TAG.REF_2BYTE;
680
- packed[1] = idx & 255;
681
- packed[2] = idx >> 8 & 255;
682
- } else if (idx <= 4294967295) {
683
- packed = new Uint8Array(5);
684
- packed[0] = TAG.REF_4BYTE;
685
- packed[1] = idx & 255;
686
- packed[2] = idx >> 8 & 255;
687
- packed[3] = idx >> 16 & 255;
688
- packed[4] = idx >> 24 & 255;
689
- } else {
690
- packed = new Uint8Array(9);
691
- packed[0] = TAG.REF_8BYTE;
692
- packed.set(uintToLEBytes(idx, 8), 1);
693
- }
694
- } else if (packed.length > 1) {
695
- objectList.push(packed);
696
- }
697
- return packed;
698
- }
699
- function _unpackAt(data, offset, objectList) {
700
- if (offset >= data.length)
701
- throw new TypeError("No data to unpack");
702
- const tag = data[offset];
703
- let addToObjectList = true;
704
- let value;
705
- let newOffset;
706
- if (tag === TAG.TRUE) {
707
- value = true;
708
- newOffset = offset + 1;
709
- } else if (tag === TAG.FALSE) {
710
- value = false;
711
- newOffset = offset + 1;
712
- } else if (tag === TAG.NULL) {
713
- value = null;
714
- newOffset = offset + 1;
715
- } else if (tag === TAG.UUID) {
716
- value = data.subarray(offset + 1, offset + 17);
717
- newOffset = offset + 17;
718
- } else if (tag === TAG.TIMESTAMP) {
719
- value = readLittleEndian(data, offset + 1, 8);
720
- newOffset = offset + 9;
721
- } else if (tag >= TAG.INT_BASE && tag <= TAG.INT_MAX_INLINE) {
722
- value = tag - TAG.INT_BASE;
723
- newOffset = offset + 1;
724
- } else if (tag === TAG.FLOAT32) {
725
- const view = new DataView(data.buffer, data.byteOffset + offset + 1, 4);
726
- value = view.getFloat32(0, true);
727
- newOffset = offset + 5;
728
- } else if (tag === TAG.FLOAT64) {
729
- const view = new DataView(data.buffer, data.byteOffset + offset + 1, 8);
730
- value = view.getFloat64(0, true);
731
- newOffset = offset + 9;
732
- } else if ((tag & 240) === TAG.INT_1BYTE) {
733
- const noOfBytes = 1 << (tag & 15);
734
- const val = readLittleEndian(data, offset + 1, noOfBytes);
735
- value = sizedInteger(val, noOfBytes);
736
- newOffset = offset + 1 + noOfBytes;
737
- } else if (tag >= TAG.STR_BASE && tag <= TAG.STR_MAX_INLINE) {
738
- const length = tag - TAG.STR_BASE;
739
- value = textDecoder.decode(data.subarray(offset + 1, offset + 1 + length));
740
- newOffset = offset + 1 + length;
741
- } else if (tag >= TAG.STR_1BYTE_LEN && tag <= TAG.STR_4BYTE_LEN) {
742
- const lenBytes = tag & 15;
743
- const length = readLittleEndian(data, offset + 1, lenBytes);
744
- const start = offset + 1 + lenBytes;
745
- value = textDecoder.decode(data.subarray(start, start + length));
746
- newOffset = start + length;
747
- } else if (tag >= TAG.BYTES_BASE && tag <= TAG.BYTES_MAX_INLINE) {
748
- const length = tag - TAG.BYTES_BASE;
749
- value = data.subarray(offset + 1, offset + 1 + length);
750
- newOffset = offset + 1 + length;
751
- } else if (tag >= TAG.BYTES_1BYTE_LEN && tag <= TAG.BYTES_4BYTE_LEN) {
752
- const noOfBytes = 1 << (tag & 15) - 1;
753
- const length = readLittleEndian(data, offset + 1, noOfBytes);
754
- const start = offset + 1 + noOfBytes;
755
- value = data.subarray(start, start + length);
756
- newOffset = start + length;
757
- } else if ((tag & 240) === TAG.ARRAY_BASE) {
758
- const count = tag & 15;
759
- let pos = offset + 1;
760
- if (count === 15) {
761
- const arr = [];
762
- while (data[pos] !== TAG.TERMINATOR) {
763
- const result = _unpackAt(data, pos, objectList);
764
- arr.push(result.value);
765
- pos = result.offset;
766
- }
767
- pos++;
768
- value = arr;
769
- } else {
770
- const arr = new Array(count);
771
- for (let i = 0;i < count; i++) {
772
- const result = _unpackAt(data, pos, objectList);
773
- arr[i] = result.value;
774
- pos = result.offset;
775
- }
776
- value = arr;
777
- }
778
- newOffset = pos;
779
- addToObjectList = false;
780
- } else if ((tag & 240) === TAG.DICT_BASE) {
781
- const count = tag & 15;
782
- const obj = {};
783
- let pos = offset + 1;
784
- if (count === 15) {
785
- while (data[pos] !== TAG.TERMINATOR) {
786
- const keyResult = _unpackAt(data, pos, objectList);
787
- const valResult = _unpackAt(data, keyResult.offset, objectList);
788
- obj[keyResult.value] = valResult.value;
789
- pos = valResult.offset;
790
- }
791
- pos++;
792
- } else {
793
- for (let i = 0;i < count; i++) {
794
- const keyResult = _unpackAt(data, pos, objectList);
795
- const valResult = _unpackAt(data, keyResult.offset, objectList);
796
- obj[keyResult.value] = valResult.value;
797
- pos = valResult.offset;
798
- }
799
- }
800
- value = obj;
801
- newOffset = pos;
802
- addToObjectList = false;
803
- } else if (tag >= TAG.REF_BASE && tag <= TAG.REF_MAX_INLINE) {
804
- const idx = tag - TAG.REF_BASE;
805
- if (idx >= objectList.length) {
806
- throw new TypeError(`Reference index ${idx} out of range`);
807
- }
808
- value = objectList[idx];
809
- newOffset = offset + 1;
810
- addToObjectList = false;
811
- } else if (tag >= TAG.REF_1BYTE && tag <= TAG.REF_8BYTE) {
812
- const len = tag - TAG.REF_MAX_INLINE;
813
- const uid = readLittleEndian(data, offset + 1, len);
814
- if (uid >= objectList.length) {
815
- throw new TypeError(`UID ${uid} out of range`);
816
- }
817
- value = objectList[uid];
818
- newOffset = offset + 1 + len;
819
- addToObjectList = false;
820
- } else {
821
- throw new TypeError(`Unknown tag 0x${tag.toString(16)}`);
822
- }
823
- if (addToObjectList) {
824
- objectList.push(value);
825
- }
826
- return { value, offset: newOffset };
827
- }
828
- // src/plist.ts
829
- var exports_plist = {};
830
- __export(exports_plist, {
831
- serialize: () => serialize,
832
- parse: () => parse
833
- });
834
- // ../../node_modules/@plist/common/lib/esm/constants.js
835
- var EPOCH2 = 978307200000;
836
- var HEADER_BINARY = "bplist00";
837
- var PlistFormat;
838
- (function(PlistFormat2) {
839
- PlistFormat2[PlistFormat2["BINARY"] = 0] = "BINARY";
840
- PlistFormat2[PlistFormat2["XML"] = 1] = "XML";
841
- PlistFormat2[PlistFormat2["OPENSTEP"] = 2] = "OPENSTEP";
842
- })(PlistFormat || (PlistFormat = {}));
843
- // ../../node_modules/@plist/binary.parse/lib/esm/index.js
844
- var maxObjectSize = 100 * 1000 * 1000;
845
- var maxObjectCount = 32768;
846
- var DECODER_UTF8 = new TextDecoder("utf-8");
847
- var DECODER_UTF16 = new TextDecoder("utf-16");
848
- function readDouble(buffer, start = 0) {
849
- return new DataView(buffer).getFloat64(start, false);
850
- }
851
- function readFloat(buffer, start = 0) {
852
- return new DataView(buffer).getFloat32(start, false);
853
- }
854
- function readUInt8(buffer, start = 0) {
855
- return new DataView(buffer).getUint8(start);
856
- }
857
- function readUInt16(buffer, start = 0) {
858
- return new DataView(buffer).getUint16(start, false);
859
- }
860
- function readUInt32(buffer, start = 0) {
861
- return new DataView(buffer).getUint32(start, false);
862
- }
863
- function readUInt64(buffer, start = 0) {
864
- return new DataView(buffer).getBigUint64(start, false);
865
- }
866
- function readUInt(buffer) {
867
- switch (buffer.byteLength) {
868
- case 1:
869
- return readUInt8(buffer);
870
- case 2:
871
- return readUInt16(buffer);
872
- case 4:
873
- return readUInt32(buffer);
874
- case 8:
875
- return readUInt64(buffer);
876
- case 16:
877
- return readUInt64(buffer, 8);
878
- }
879
- throw new Error(`Invalid unsigned int length: ${buffer.byteLength}`);
880
- }
881
- function readInt8(buffer, start = 0) {
882
- return new DataView(buffer).getInt8(start);
883
- }
884
- function readInt16(buffer, start = 0) {
885
- return new DataView(buffer).getInt16(start, false);
886
- }
887
- function readInt32(buffer, start = 0) {
888
- return new DataView(buffer).getInt32(start, false);
889
- }
890
- function readInt64(buffer, start = 0) {
891
- return new DataView(buffer).getBigInt64(start, false);
892
- }
893
- function readInt(buffer) {
894
- switch (buffer.byteLength) {
895
- case 1:
896
- return readInt8(buffer);
897
- case 2:
898
- return readInt16(buffer);
899
- case 4:
900
- return readInt32(buffer);
901
- case 8:
902
- return readInt64(buffer);
903
- case 16:
904
- return readUInt64(buffer, 8);
905
- }
906
- throw new Error(`Invalid int length: ${buffer.byteLength}`);
907
- }
908
- function swapBytes(buffer) {
909
- const array = new Uint8Array(buffer);
910
- for (let i = 0;i < array.length; i += 2) {
911
- const a = array[i];
912
- array[i] = array[i + 1];
913
- array[i + 1] = a;
914
- }
915
- return array.buffer;
916
- }
917
- var parse = (buffer) => {
918
- const headerBytes = buffer.slice(0, HEADER_BINARY.length);
919
- if (DECODER_UTF8.decode(headerBytes) !== HEADER_BINARY) {
920
- throw new Error(`Invalid binary plist. Expected '${HEADER_BINARY}' at offset 0.`);
921
- }
922
- const trailer = buffer.slice(buffer.byteLength - 32, buffer.byteLength);
923
- const offsetSize = readUInt8(trailer, 6);
924
- const objectRefSize = readUInt8(trailer, 7);
925
- const numObjects = Number(readUInt64(trailer, 8));
926
- const topObject = Number(readUInt64(trailer, 16));
927
- const offsetTableOffset = Number(readUInt64(trailer, 24));
928
- if (numObjects > maxObjectCount) {
929
- throw new Error("maxObjectCount exceeded");
930
- }
931
- const offsetTable = [];
932
- for (let i = 0;i < numObjects; i++) {
933
- const offsetBytes = buffer.slice(offsetTableOffset + i * offsetSize, offsetTableOffset + (i + 1) * offsetSize);
934
- offsetTable[i] = Number(readUInt(offsetBytes));
935
- }
936
- function parseObject(tableOffset) {
937
- const offset = offsetTable[tableOffset];
938
- const type = readUInt8(buffer, offset);
939
- const objType = (type & 240) >> 4;
940
- const objInfo = type & 15;
941
- switch (objType) {
942
- case 0:
943
- return parseSimple();
944
- case 1:
945
- return parseInteger();
946
- case 8:
947
- return parseUID();
948
- case 2:
949
- return parseReal();
950
- case 3:
951
- return parseDate();
952
- case 4:
953
- return parseData();
954
- case 5:
955
- return parsePlistString();
956
- case 6:
957
- return parsePlistString(true);
958
- case 10:
959
- return parseArray();
960
- case 13:
961
- return parseDictionary();
962
- default:
963
- throw new Error("Unhandled type 0x" + objType.toString(16));
964
- }
965
- function parseSimple() {
966
- switch (objInfo) {
967
- case 0:
968
- return null;
969
- case 8:
970
- return false;
971
- case 9:
972
- return true;
973
- case 15:
974
- return null;
975
- default:
976
- throw new Error("Unhandled simple type 0x" + objType.toString(16));
977
- }
978
- }
979
- function parseInteger() {
980
- const length = Math.pow(2, objInfo);
981
- if (length < maxObjectSize) {
982
- const data = buffer.slice(offset + 1, offset + 1 + length);
983
- return readInt(data);
984
- }
985
- throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
986
- }
987
- function parseUID() {
988
- const length = objInfo + 1;
989
- if (length < maxObjectSize) {
990
- return {
991
- CF$UID: readUInt(buffer.slice(offset + 1, offset + 1 + length))
992
- };
993
- }
994
- throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
995
- }
996
- function parseReal() {
997
- const length = Math.pow(2, objInfo);
998
- if (length < maxObjectSize) {
999
- const realBuffer = buffer.slice(offset + 1, offset + 1 + length);
1000
- if (length === 4) {
1001
- return readFloat(realBuffer);
1002
- } else if (length === 8) {
1003
- return readDouble(realBuffer);
1004
- }
1005
- throw new Error(`Invalid real length: ${length}`);
1006
- } else {
1007
- throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
1008
- }
1009
- }
1010
- function parseDate() {
1011
- if (objInfo != 3) {
1012
- console.error("Unknown date type :" + objInfo + ". Parsing anyway...");
1013
- }
1014
- const dateBuffer = buffer.slice(offset + 1, offset + 9);
1015
- return new Date(EPOCH2 + 1000 * readDouble(dateBuffer));
1016
- }
1017
- function parseData() {
1018
- let dataoffset = 1;
1019
- let length = objInfo;
1020
- if (objInfo == 15) {
1021
- const int_type = readInt8(buffer, offset + 1);
1022
- const intType = (int_type & 240) / 16;
1023
- if (intType != 1) {
1024
- console.error("0x4: UNEXPECTED LENGTH-INT TYPE! " + intType);
1025
- }
1026
- const intInfo = int_type & 15;
1027
- const intLength = Math.pow(2, intInfo);
1028
- dataoffset = 2 + intLength;
1029
- length = Number(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
1030
- }
1031
- if (length < maxObjectSize) {
1032
- return buffer.slice(offset + dataoffset, offset + dataoffset + Number(length));
1033
- }
1034
- throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
1035
- }
1036
- function parsePlistString(isUtf16 = false) {
1037
- let length = objInfo;
1038
- let stroffset = 1;
1039
- if (objInfo == 15) {
1040
- const int_type = readUInt8(buffer, offset + 1);
1041
- const intType = (int_type & 240) / 16;
1042
- if (intType != 1) {
1043
- console.error("UNEXPECTED LENGTH-INT TYPE! " + intType);
1044
- }
1045
- const intInfo = int_type & 15;
1046
- const intLength = Math.pow(2, intInfo);
1047
- stroffset = 2 + intLength;
1048
- length = Number(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
1049
- }
1050
- length *= isUtf16 ? 2 : 1;
1051
- if (length < maxObjectSize) {
1052
- let plistString = buffer.slice(offset + stroffset, offset + stroffset + length);
1053
- if (isUtf16) {
1054
- plistString = swapBytes(plistString);
1055
- return DECODER_UTF16.decode(plistString);
1056
- } else {
1057
- return DECODER_UTF8.decode(plistString);
1058
- }
1059
- }
1060
- throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
1061
- }
1062
- function parseArray() {
1063
- let length = objInfo;
1064
- let arrayoffset = 1;
1065
- if (objInfo == 15) {
1066
- const int_type = readUInt8(buffer, offset + 1);
1067
- const intType = (int_type & 240) / 16;
1068
- if (intType != 1) {
1069
- console.error("0xa: UNEXPECTED LENGTH-INT TYPE! " + intType);
1070
- }
1071
- const intInfo = int_type & 15;
1072
- const intLength = Math.pow(2, intInfo);
1073
- arrayoffset = 2 + intLength;
1074
- length = Number(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
1075
- }
1076
- if (length * objectRefSize > maxObjectSize) {
1077
- throw new Error("Too little heap space available!");
1078
- }
1079
- const array = [];
1080
- for (let i = 0;i < length; i++) {
1081
- const objRef = Number(readUInt(buffer.slice(offset + arrayoffset + i * objectRefSize, offset + arrayoffset + (i + 1) * objectRefSize)));
1082
- array[i] = parseObject(objRef);
1083
- }
1084
- return array;
1085
- }
1086
- function parseDictionary() {
1087
- let length = objInfo;
1088
- let dictoffset = 1;
1089
- if (objInfo == 15) {
1090
- const int_type = readUInt8(buffer, offset + 1);
1091
- const intType = (int_type & 240) / 16;
1092
- if (intType != 1) {
1093
- console.error("0xD: UNEXPECTED LENGTH-INT TYPE! " + intType);
1094
- }
1095
- const intInfo = int_type & 15;
1096
- const intLength = Math.pow(2, intInfo);
1097
- dictoffset = 2 + intLength;
1098
- length = Number(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
1099
- }
1100
- if (length * 2 * objectRefSize > maxObjectSize) {
1101
- throw new Error("Too little heap space available!");
1102
- }
1103
- const dict = {};
1104
- for (let i = 0;i < length; i++) {
1105
- const keyRef = Number(readUInt(buffer.slice(offset + dictoffset + i * objectRefSize, offset + dictoffset + (i + 1) * objectRefSize)));
1106
- const valRef = Number(readUInt(buffer.slice(offset + dictoffset + length * objectRefSize + i * objectRefSize, offset + dictoffset + length * objectRefSize + (i + 1) * objectRefSize)));
1107
- const key = parseObject(keyRef);
1108
- if (typeof key !== "string") {
1109
- throw new Error("Invalid key type.");
1110
- }
1111
- if (key === "__proto__") {
1112
- throw new Error("Attempted prototype pollution");
1113
- }
1114
- const val = parseObject(valRef);
1115
- dict[key] = val;
1116
- }
1117
- return dict;
1118
- }
1119
- }
1120
- return parseObject(topObject);
1121
- };
1122
- // ../../node_modules/@plist/binary.serialize/lib/esm/index.js
1123
- var encoder = new TextEncoder;
1124
- var nullBytes = new Uint8Array([0, 0, 0, 0, 0, 0]);
1125
- var fromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
1126
- var isUID = (value) => !!value && typeof value === "object" && Object.keys(value).length == 1 && typeof value.CF$UID === "number";
1127
- var toUTF16 = (string) => {
1128
- const buf = new ArrayBuffer(string.length * 2);
1129
- const bufView = new Uint16Array(buf);
1130
- for (let i = 0, strLen = string.length;i < strLen; i++) {
1131
- bufView[i] = string.charCodeAt(i);
1132
- }
1133
- return new Uint8Array(buf);
1134
- };
1135
- var concat = (first, second) => {
1136
- const third = new Uint8Array(first.length + second.length);
1137
- third.set(first);
1138
- third.set(second, first.length);
1139
- return third;
1140
- };
1141
- var writeUInt = (buffer, int2, size) => {
1142
- const output = new Uint8Array(buffer.length + size);
1143
- const dataView = new DataView(output.buffer);
1144
- output.set(buffer);
1145
- switch (size) {
1146
- case 1:
1147
- dataView.setUint8(buffer.length, Number(int2));
1148
- break;
1149
- case 2:
1150
- dataView.setUint16(buffer.length, Number(int2), false);
1151
- break;
1152
- case 4:
1153
- dataView.setUint32(buffer.length, Number(int2), false);
1154
- break;
1155
- case 8:
1156
- dataView.setBigUint64(buffer.length, BigInt(int2), false);
1157
- break;
1158
- default:
1159
- throw new Error("Unsupported int size");
1160
- }
1161
- return output;
1162
- };
1163
- var writeInt = (buffer, int2, size) => {
1164
- const output = new Uint8Array(buffer.length + size);
1165
- const dataView = new DataView(output.buffer);
1166
- output.set(buffer);
1167
- switch (size) {
1168
- case 1:
1169
- dataView.setInt8(buffer.length, Number(int2));
1170
- break;
1171
- case 2:
1172
- dataView.setInt16(buffer.length, Number(int2), false);
1173
- break;
1174
- case 4:
1175
- dataView.setInt32(buffer.length, Number(int2), false);
1176
- break;
1177
- case 8:
1178
- dataView.setBigInt64(buffer.length, BigInt(int2), false);
1179
- break;
1180
- default:
1181
- throw new Error("Unsupported int size");
1182
- }
1183
- return output;
1184
- };
1185
- var writeDouble = (buffer, double) => {
1186
- const output = new Uint8Array(buffer.length + 8);
1187
- const dataView = new DataView(output.buffer);
1188
- output.set(buffer);
1189
- dataView.setFloat64(buffer.length, double);
1190
- return output;
1191
- };
1192
- var serialize = (value) => {
1193
- let buffer = encoder.encode(HEADER_BINARY);
1194
- if (value instanceof Array && value.length === 1) {
1195
- value = value[0];
1196
- }
1197
- let entries = toEntries(value);
1198
- const idSizeInBytes = computeIdSizeInBytes(entries.length);
1199
- const offsets = [];
1200
- let offsetSizeInBytes;
1201
- let offsetTableOffset;
1202
- updateEntryIds();
1203
- entries.forEach((entry, entryIdx) => {
1204
- offsets[entryIdx] = buffer.byteLength;
1205
- if (typeof entry === "undefined" || entry === null) {
1206
- buffer = writeUInt(buffer, 0, 1);
1207
- } else {
1208
- write(entry);
1209
- }
1210
- });
1211
- writeOffsetTable();
1212
- writeTrailer();
1213
- return buffer.buffer;
1214
- function updateEntryIds() {
1215
- const strings = {};
1216
- let entryId = 0;
1217
- entries.forEach((entry) => {
1218
- if (entry.id) {
1219
- return;
1220
- }
1221
- if (typeof entry.value === "string") {
1222
- if (strings.hasOwnProperty(entry.value)) {
1223
- entry.type = "stringref";
1224
- entry.id = strings[entry.value];
1225
- } else {
1226
- strings[entry.value] = entry.id = entryId++;
1227
- }
1228
- } else {
1229
- entry.id = entryId++;
1230
- }
1231
- });
1232
- entries = entries.filter((entry) => {
1233
- return entry.type !== "stringref";
1234
- });
1235
- }
1236
- function writeTrailer() {
1237
- buffer = concat(buffer, nullBytes);
1238
- buffer = concat(buffer, new Uint8Array([offsetSizeInBytes, idSizeInBytes]));
1239
- buffer = writeUInt(buffer, BigInt(entries.length), 8);
1240
- buffer = writeUInt(buffer, BigInt("0"), 8);
1241
- buffer = writeUInt(buffer, BigInt(offsetTableOffset), 8);
1242
- }
1243
- function writeOffsetTable() {
1244
- offsetTableOffset = buffer.byteLength;
1245
- offsetSizeInBytes = computeOffsetSizeInBytes(offsetTableOffset);
1246
- offsets.forEach((offset) => {
1247
- buffer = writeUInt(buffer, offset, offsetSizeInBytes);
1248
- });
1249
- }
1250
- function write(entry) {
1251
- if (entry.type === "primitive") {
1252
- const value2 = entry.value;
1253
- switch (typeof value2) {
1254
- case "number":
1255
- case "bigint":
1256
- writeNumber(value2);
1257
- break;
1258
- case "string":
1259
- writeString(value2);
1260
- break;
1261
- case "boolean":
1262
- writeBoolean(value2);
1263
- break;
1264
- }
1265
- if (value2 instanceof Date) {
1266
- writeDate(value2);
1267
- } else if (value2 instanceof ArrayBuffer) {
1268
- writeData(value2);
1269
- } else if (isUID(value2)) {
1270
- writeUID(value2.CF$UID);
1271
- }
1272
- return;
1273
- }
1274
- switch (entry.type) {
1275
- case "dict":
1276
- writeDict(entry);
1277
- break;
1278
- case "array":
1279
- writeArray(entry);
1280
- break;
1281
- default:
1282
- throw new Error("unhandled entry type: " + entry.type);
1283
- }
1284
- }
1285
- function writeDate(value2) {
1286
- buffer = writeUInt(buffer, 51, 1);
1287
- const date = (value2.getTime() - EPOCH2) / 1000;
1288
- buffer = writeDouble(buffer, date);
1289
- }
1290
- function writeDict(entry) {
1291
- writeIntHeader(13, entry.entryKeys.length);
1292
- entry.entryKeys.forEach((entry2) => {
1293
- writeID(entry2.id);
1294
- });
1295
- entry.entryValues.forEach((entry2) => {
1296
- writeID(entry2.id);
1297
- });
1298
- }
1299
- function writeNumber(value2) {
1300
- if (typeof value2 === "bigint") {
1301
- const width = 16;
1302
- const hex = value2.toString(16);
1303
- const buf = fromHexString(hex.padStart(width * 2, "0").slice(0, width * 2));
1304
- buffer = writeUInt(buffer, 20, 1);
1305
- buffer = concat(buffer, buf);
1306
- } else {
1307
- if (Number.isInteger(value2)) {
1308
- if (value2 < 0) {
1309
- buffer = writeUInt(buffer, 19, 1);
1310
- buffer = writeInt(buffer, value2, 8);
1311
- } else if (value2 <= 255) {
1312
- buffer = writeUInt(buffer, 16, 1);
1313
- buffer = writeUInt(buffer, value2, 1);
1314
- } else if (value2 <= 65535) {
1315
- buffer = writeUInt(buffer, 17, 1);
1316
- buffer = writeUInt(buffer, value2, 2);
1317
- } else if (value2 <= 4294967295) {
1318
- buffer = writeUInt(buffer, 18, 1);
1319
- buffer = writeUInt(buffer, value2, 4);
1320
- } else {
1321
- buffer = writeUInt(buffer, 19, 1);
1322
- buffer = writeUInt(buffer, value2, 8);
1323
- }
1324
- } else {
1325
- buffer = writeUInt(buffer, 35, 1);
1326
- buffer = writeDouble(buffer, value2);
1327
- }
1328
- }
1329
- }
1330
- function writeUID(uid) {
1331
- writeIntHeader(8, 0);
1332
- writeID(uid);
1333
- }
1334
- function writeArray(entry) {
1335
- writeIntHeader(10, entry.entries.length);
1336
- entry.entries.forEach((e) => {
1337
- writeID(e.id);
1338
- });
1339
- }
1340
- function writeBoolean(value2) {
1341
- buffer = writeUInt(buffer, value2 ? 9 : 8, 1);
1342
- }
1343
- function writeString(value2) {
1344
- if (mustBeUtf16(value2)) {
1345
- const utf16 = toUTF16(value2);
1346
- writeIntHeader(6, utf16.length / 2);
1347
- for (let i = 0;i < utf16.length; i += 2) {
1348
- const t = utf16[i + 0];
1349
- utf16[i + 0] = utf16[i + 1];
1350
- utf16[i + 1] = t;
1351
- }
1352
- buffer = concat(buffer, utf16);
1353
- } else {
1354
- const utf8 = encoder.encode(value2);
1355
- writeIntHeader(5, utf8.length);
1356
- buffer = concat(buffer, utf8);
1357
- }
1358
- }
1359
- function writeData(data) {
1360
- writeIntHeader(4, data.byteLength);
1361
- buffer = concat(buffer, new Uint8Array(data));
1362
- }
1363
- function writeIntHeader(kind, value2) {
1364
- if (value2 < 15) {
1365
- buffer = writeUInt(buffer, (kind << 4) + value2, 1);
1366
- } else {
1367
- buffer = writeUInt(buffer, (kind << 4) + 15, 1);
1368
- writeNumber(value2);
1369
- }
1370
- }
1371
- function writeID(id) {
1372
- buffer = writeUInt(buffer, id, idSizeInBytes);
1373
- }
1374
- function mustBeUtf16(string) {
1375
- return encoder.encode(string).byteLength != string.length;
1376
- }
1377
- };
1378
- var typeofPrimitive = ["string", "number", "boolean", "bigint"];
1379
- function toEntries(value) {
1380
- if (typeofPrimitive.includes(typeof value) || value instanceof ArrayBuffer || value instanceof Date || isUID(value)) {
1381
- return [
1382
- {
1383
- type: "primitive",
1384
- value
1385
- }
1386
- ];
1387
- }
1388
- if (value != null && typeof value === "object") {
1389
- return Array.isArray(value) ? toEntriesArray(value) : toEntriesObject(value);
1390
- }
1391
- throw new Error("unhandled entry: " + value);
1392
- }
1393
- function toEntriesArray(array) {
1394
- const entries = array.map(toEntries);
1395
- return [
1396
- {
1397
- type: "array",
1398
- value: undefined,
1399
- entries: entries.map((entries2) => entries2[0])
1400
- },
1401
- ...entries.flat()
1402
- ];
1403
- }
1404
- function toEntriesObject(dict) {
1405
- const entryKeys = Object.keys(dict).map(toEntries).flat(1);
1406
- const entryValues = Object.values(dict).map(toEntries);
1407
- return [
1408
- {
1409
- type: "dict",
1410
- value: undefined,
1411
- entryKeys,
1412
- entryValues: entryValues.map((entries) => entries[0])
1413
- },
1414
- ...entryKeys,
1415
- ...entryValues.flat()
1416
- ];
1417
- }
1418
- function computeOffsetSizeInBytes(maxOffset) {
1419
- if (maxOffset < 256) {
1420
- return 1;
1421
- }
1422
- if (maxOffset < 65536) {
1423
- return 2;
1424
- }
1425
- if (maxOffset < 4294967296) {
1426
- return 4;
1427
- }
1428
- return 8;
1429
- }
1430
- function computeIdSizeInBytes(numberOfIds) {
1431
- if (numberOfIds < 256) {
1432
- return 1;
1433
- }
1434
- if (numberOfIds < 65536) {
1435
- return 2;
1436
- }
1437
- return 4;
1438
- }
1439
- // src/rtsp.ts
1440
- var exports_rtsp = {};
1441
- __export(exports_rtsp, {
1442
- makeResponse: () => makeResponse,
1443
- makeRequest: () => makeRequest,
1444
- makeHeader: () => makeHeader
1445
- });
1446
- function makeHeader(method, path, headers, cseq, activeRemote, dacpId, sessionId) {
1447
- const lines = [];
1448
- lines.push(`${method} ${path} RTSP/1.0`);
1449
- lines.push(`CSeq: ${cseq}`);
1450
- lines.push(`Active-Remote: ${activeRemote}`);
1451
- lines.push(`Client-Instance: ${dacpId}`);
1452
- lines.push(`DACP-ID: ${dacpId}`);
1453
- lines.push("User-Agent: AirPlay/320.20");
1454
- lines.push("X-Apple-ProtocolVersion: 1");
1455
- lines.push(`X-Apple-Session-ID: ${sessionId}`);
1456
- lines.push("X-ProtocolVersion: 1");
1457
- for (const [name, value] of Object.entries(headers)) {
1458
- lines.push(`${name}: ${value}`);
1459
- }
1460
- lines.push("");
1461
- lines.push("");
1462
- return lines.join(`\r
1463
- `);
1464
- }
1465
- function makeRequest(buffer) {
1466
- const headerLength = buffer.indexOf(`\r
1467
- \r
1468
- `);
1469
- const { headers, method, path } = parseRequestHeaders(buffer.subarray(0, headerLength));
1470
- let contentLength = headers["Content-Length"] ? Number(headers["Content-Length"]) : 0;
1471
- if (isNaN(contentLength)) {
1472
- contentLength = 0;
1473
- }
1474
- const requestLength = headerLength + 4 + contentLength;
1475
- if (buffer.byteLength < requestLength) {
1476
- return null;
1477
- }
1478
- const body = buffer.subarray(headerLength + 4, requestLength);
1479
- return {
1480
- headers,
1481
- method,
1482
- path,
1483
- body,
1484
- requestLength
1485
- };
1486
- }
1487
- function makeResponse(buffer) {
1488
- const headerLength = buffer.indexOf(`\r
1489
- \r
1490
- `);
1491
- const { headers, status, statusText } = parseResponseHeaders(buffer.subarray(0, headerLength));
1492
- let contentLength = headers["Content-Length"] ? Number(headers["Content-Length"]) : 0;
1493
- if (isNaN(contentLength)) {
1494
- contentLength = 0;
1495
- }
1496
- const responseLength = headerLength + 4 + contentLength;
1497
- if (buffer.byteLength < responseLength) {
1498
- return null;
1499
- }
1500
- const body = buffer.subarray(headerLength + 4, responseLength);
1501
- const response = new Response(body, {
1502
- status,
1503
- statusText,
1504
- headers
1505
- });
1506
- return {
1507
- response,
1508
- responseLength
1509
- };
1510
- }
1511
- function parseHeaders(lines) {
1512
- const headers = {};
1513
- for (let i = 0;i < lines.length; i++) {
1514
- const colon = lines[i].indexOf(":");
1515
- if (colon <= 0) {
1516
- continue;
1517
- }
1518
- const name = lines[i].substring(0, colon).trim();
1519
- headers[name] = lines[i].substring(colon + 1).trim();
1520
- }
1521
- return headers;
1522
- }
1523
- function parseRequestHeaders(buffer) {
1524
- const lines = buffer.toString("utf8").split(`\r
1525
- `);
1526
- const rawRequest = lines[0].match(/^(\S+)\s+(\S+)\s+RTSP\/1\.0$/);
1527
- const method = rawRequest[1];
1528
- const path = rawRequest[2];
1529
- const headers = parseHeaders(lines.slice(1));
1530
- return {
1531
- headers,
1532
- method,
1533
- path
1534
- };
1535
- }
1536
- function parseResponseHeaders(buffer) {
1537
- const lines = buffer.toString("utf8").split(`\r
1538
- `);
1539
- const rawStatus = lines[0].match(/(HTTP|RTSP)\/[\d.]+\s+(\d+)\s+(.+)/);
1540
- const status = Number(rawStatus[2]);
1541
- const statusText = rawStatus[3];
1542
- const headers = parseHeaders(lines.slice(1));
1543
- return {
1544
- headers,
1545
- status,
1546
- statusText
1547
- };
1548
- }
1549
- // src/tlv8.ts
1550
- var exports_tlv8 = {};
1551
- __export(exports_tlv8, {
1552
- encode: () => encode3,
1553
- decode: () => decode4,
1554
- bail: () => bail,
1555
- Value: () => Value,
1556
- State: () => State,
1557
- Method: () => Method,
1558
- Flags: () => Flags,
1559
- ErrorCode: () => ErrorCode
1560
- });
1561
- var Flags = {
1562
- TransientPairing: 16
1563
- };
1564
- var ErrorCode = {
1565
- Unknown: 1,
1566
- Authentication: 2,
1567
- BackOff: 3,
1568
- MaxPeers: 4,
1569
- MaxTries: 5,
1570
- Unavailable: 6,
1571
- Busy: 7
1572
- };
1573
- var Method = {
1574
- PairSetup: 0,
1575
- PairSetupWithAuth: 1,
1576
- PairVerify: 2,
1577
- AddPairing: 3,
1578
- RemovePairing: 4,
1579
- ListPairing: 5
1580
- };
1581
- var State = {
1582
- M1: 1,
1583
- M2: 2,
1584
- M3: 3,
1585
- M4: 4,
1586
- M5: 5,
1587
- M6: 6
1588
- };
1589
- var Value = {
1590
- Method: 0,
1591
- Identifier: 1,
1592
- Salt: 2,
1593
- PublicKey: 3,
1594
- Proof: 4,
1595
- EncryptedData: 5,
1596
- State: 6,
1597
- Error: 7,
1598
- BackOff: 8,
1599
- Certificate: 9,
1600
- Signature: 10,
1601
- Permissions: 11,
1602
- FragmentData: 12,
1603
- FragmentLast: 13,
1604
- Name: 17,
1605
- Flags: 19
1606
- };
1607
- function bail(data) {
1608
- if (data.has(Value.BackOff)) {
1609
- const buffer = data.get(Value.BackOff);
1610
- const time = buffer.readUintLE(0, buffer.length);
1611
- throw new Error(`Device is busy, try again in ${time} seconds.`);
1612
- }
1613
- if (data.has(Value.Error)) {
1614
- const errorCodeEntries = Object.entries(ErrorCode);
1615
- const errorCode = errorCodeEntries.find(([_, code]) => code === data.get(Value.Error).readUint8());
1616
- if (!errorCode) {
1617
- throw new Error(`Device returned an unknown error code: ${data.get(Value.Error).readUint8()}`);
1618
- }
1619
- throw new Error(`Device returned an error code: ${errorCode[0]}`);
1620
- }
1621
- throw new Error("Invalid response");
1622
- }
1623
- function encode3(entries) {
1624
- let totalSize = 0;
1625
- for (const [, valueRaw] of entries) {
1626
- const len = typeof valueRaw === "number" ? 1 : valueRaw.length;
1627
- const chunks = Math.max(1, Math.ceil(len / 255));
1628
- totalSize += chunks * 2 + len;
1629
- }
1630
- const result = Buffer.allocUnsafe(totalSize);
1631
- let pos = 0;
1632
- for (const [type, valueRaw] of entries) {
1633
- let value;
1634
- let valueLen;
1635
- if (typeof valueRaw === "number") {
1636
- value = Buffer.allocUnsafe(1);
1637
- value[0] = valueRaw;
1638
- valueLen = 1;
1639
- } else {
1640
- value = valueRaw;
1641
- valueLen = value.length;
1642
- }
1643
- let offset = 0;
1644
- do {
1645
- const len = Math.min(valueLen - offset, 255);
1646
- result[pos++] = type;
1647
- result[pos++] = len;
1648
- if (len > 0) {
1649
- if (value instanceof Buffer) {
1650
- value.copy(result, pos, offset, offset + len);
1651
- } else {
1652
- result.set(value.subarray(offset, offset + len), pos);
1653
- }
1654
- pos += len;
1655
- }
1656
- offset += len;
1657
- } while (offset < valueLen);
1658
- }
1659
- return result;
1660
- }
1661
- function decode4(buf) {
1662
- const map = new Map;
1663
- let i = 0;
1664
- while (i < buf.length) {
1665
- const type = buf[i++];
1666
- const len = buf[i++];
1667
- const existing = map.get(type);
1668
- if (existing) {
1669
- const newBuf = Buffer.allocUnsafe(existing.length + len);
1670
- existing.copy(newBuf, 0);
1671
- buf.copy(newBuf, existing.length, i, i + len);
1672
- map.set(type, newBuf);
1673
- } else {
1674
- map.set(type, buf.subarray(i, i + len));
1675
- }
1676
- i += len;
1677
- }
1678
- return map;
1679
- }
1680
- export {
1681
- exports_tlv8 as TLV8,
1682
- exports_rtsp as RTSP,
1683
- exports_plist as Plist,
1684
- exports_opack as OPack,
1685
- exports_ntp as NTP,
1686
- exports_daap as DAAP
1687
- };