@basmilius/apple-encoding 0.0.76
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/LICENSE +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1 -0
- package/dist/ntp.d.ts +17 -0
- package/dist/opack.d.ts +10 -0
- package/dist/plist.d.ts +2 -0
- package/dist/tlv8.d.ts +49 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2025 Bas Milius
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var D=Object.defineProperty;var g=(e,t)=>{for(var r in t)D(e,r,{get:t[r],enumerable:!0,configurable:!0,set:(f)=>t[r]=()=>f})};var I={};g(I,{parts:()=>d,ns:()=>F,now:()=>N,encode:()=>S,decode:()=>$});var O=0x83AA7E80n;function N(){let e=F()/1000n,t=e/1000000n,r=e-t*1000000n;return t+O<<32n|(r<<32n)/1000000n}function F(){return process.hrtime.bigint()}function d(e){return[Number(e>>32n),Number(e&0xFFFFFFFFn)]}function $(e){return{proto:e.readUInt8(0),type:e.readUInt8(1),seqno:e.readUInt16BE(2),padding:e.readUInt32BE(4),reftime_sec:e.readUInt32BE(8),reftime_frac:e.readUInt32BE(12),recvtime_sec:e.readUInt32BE(16),recvtime_frac:e.readUInt32BE(20),sendtime_sec:e.readUInt32BE(24),sendtime_frac:e.readUInt32BE(28)}}function S(e){let t=Buffer.alloc(32);return t.writeUInt8(e.proto,0),t.writeUInt8(e.type,1),t.writeUInt16BE(e.seqno,2),t.writeUInt32BE(e.padding,4),t.writeUInt32BE(e.reftime_sec,8),t.writeUInt32BE(e.reftime_frac,12),t.writeUInt32BE(e.recvtime_sec,16),t.writeUInt32BE(e.recvtime_frac,20),t.writeUInt32BE(e.sendtime_sec,24),t.writeUInt32BE(e.sendtime_frac,28),t}var k={};g(k,{sizedInt:()=>_,int:()=>z,float:()=>V,encode:()=>R,decode:()=>q});class E extends Number{size;constructor(e,t){super(e);this.size=t}}class A{value;constructor(e){this.value=e}}class v{value;constructor(e){this.value=e}}function _(e,t){return new E(e,t)}function V(e){return new A(e)}function z(e){return new v(e)}function c(e){let t=e.reduce((n,s)=>n+s.length,0),r=new Uint8Array(t),f=0;for(let n of e)r.set(n,f),f+=n.length;return r}function u(e){return Uint8Array.of(e)}function x(e,t){let r=new Uint8Array(t),f=BigInt(e);for(let n=0;n<t;n++)r[n]=Number(f&0xffn),f>>=8n;return r}function m(e){let t=e.reduce((n,s)=>n+s.length,0),r=new Uint8Array(t),f=0;for(let n of e)r.set(n,f),f+=n.length;return r}function R(e){return U(e,[])}function U(e,t){let r=null;if(e===null||e===void 0)r=u(4);else if(typeof e==="boolean")r=u(e?1:2);else if(e instanceof A){let n=new ArrayBuffer(8);new DataView(n).setFloat64(0,e.value,!0),r=c([u(54),new Uint8Array(n)])}else if(e instanceof v){let n=e.value;if(n<40)r=u(8+n);else if(n<=255)r=m([u(48),x(n,1)]);else if(n<=65535)r=m([u(49),x(n,2)]);else if(n<=4294967295)r=m([u(50),x(n,4)]);else r=m([u(51),x(n,8)])}else if(typeof e==="number")if(!Number.isInteger(e)){let n=new ArrayBuffer(8);new DataView(n).setFloat64(0,e,!0),r=c([u(54),new Uint8Array(n)])}else if(e<40)r=u(8+e);else if(e<=255)r=c([u(48),x(e,1)]);else if(e<=65535)r=c([u(49),x(e,2)]);else if(e<=4294967295)r=c([u(50),x(e,4)]);else r=c([u(51),x(e,8)]);else if(e instanceof E)r=c([u(48+Math.log2(e.size)),x(e.valueOf(),e.size)]);else if(typeof e==="string"){let n=new TextEncoder().encode(e),s=n.length;if(s<=32)r=c([u(64+s),n]);else if(s<=255)r=c([u(97),x(s,1),n]);else if(s<=65535)r=c([u(98),x(s,2),n]);else if(s<=16777215)r=c([u(99),x(s,3),n]);else r=c([u(100),x(s,4),n])}else if(e instanceof Uint8Array||Buffer.isBuffer(e)){let n=e instanceof Uint8Array?e:new Uint8Array(e),s=n.length;if(s<=32)r=c([u(112+s),n]);else if(s<=255)r=c([u(145),x(s,1),n]);else if(s<=65535)r=c([u(146),x(s,2),n]);else r=c([u(147),x(s,4),n])}else if(Array.isArray(e)){let n=c(e.map((o)=>U(o,t))),s=e.length;if(s<=15){if(r=c([u(208+s),n]),s>=15)r=c([r,u(3)])}else r=c([u(223),n,u(3)])}else if(typeof e==="object"){let n=Object.keys(e),s=n.length,o=[];for(let l of n)o.push(U(l,t)),o.push(U(e[l],t));let i;if(s<=15)i=u(224+s);else i=u(239);if(r=m([i,m(o)]),s>=15||t.some((l)=>l===r))r=m([r,u(129)])}else throw TypeError(typeof e);let f=t.findIndex((n)=>n.length===r.length&&n.every((s,o)=>s===r[o]));if(f>=0)if(f<33)r=u(160+f);else if(f<=255)r=c([u(193),x(f,1)]);else if(f<=65535)r=c([u(194),x(f,2)]);else if(f<=4294967295)r=c([u(195),x(f,4)]);else r=c([u(196),x(f,8)]);else if(r.length>1)t.push(r);return r}function q(e){let[t]=p(e,[]);return t}function H(e,t){if(e.length<t)throw TypeError(`Not enough data: need ${t} bytes, have ${e.length}`)}function h(e,t,r){H(e.subarray(t),r);let f=0n;for(let n=r-1;n>=0;n--)f=f<<8n|BigInt(e[t+n]);return Number(f)}function p(e,t){if(e.length===0)throw TypeError("No data to unpack");let r=e[0],f=!0,n,s;if(r===1)n=!0,s=e.subarray(1);else if(r===2)n=!1,s=e.subarray(1);else if(r===4)n=null,s=e.subarray(1);else if(r===5)n=e.subarray(1,17),s=e.subarray(17);else if(r===6)n=h(e,1,8),s=e.subarray(9);else if(r>=8&&r<=47)n=r-8,s=e.subarray(1);else if(r===53)n=new DataView(e.buffer,e.byteOffset+1,4).getFloat32(0,!0),s=e.subarray(5);else if(r===54)n=new DataView(e.buffer,e.byteOffset+1,8).getFloat64(0,!0),s=e.subarray(9);else if((r&240)===48){let o=2**(r&15),i=h(e,1,o);n=_(i,o),s=e.subarray(1+o)}else if(r>=64&&r<=96){let o=r-64;n=new TextDecoder().decode(e.subarray(1,1+o)),s=e.subarray(1+o)}else if(r>=97&&r<=100){let o=r&15,i=h(e,1,o);n=new TextDecoder().decode(e.subarray(1+o,1+o+i)),s=e.subarray(1+o+i)}else if(r>=112&&r<=144){let o=r-112;n=e.subarray(1,1+o),s=e.subarray(1+o)}else if(r>=145&&r<=148){let o=1<<(r&15)-1,i=h(e,1,o),l=1+o;n=e.subarray(l,l+i),s=e.subarray(l+i)}else if((r&240)===208){let o=r&15,i=e.subarray(1),l=[];if(o===15){while(i[0]!==3){let[a,y]=p(i,t);l.push(a),i=y}i=i.subarray(1)}else for(let a=0;a<o;a++){let[y,w]=p(i,t);l.push(y),i=w}n=l,s=i,f=!1}else if((r&224)===224){let o=r&15,i=e.subarray(1),l={};if(o===15){while(i[0]!==3){let[a,y]=p(i,t),[w,B]=p(y,t);l[a]=w,i=B}i=i.subarray(1)}else for(let a=0;a<o;a++){let[y,w]=p(i,t),[B,T]=p(w,t);l[y]=B,i=T}n=l,s=i,f=!1}else if(r>=160&&r<=192){let o=r-160;if(o>=t.length)throw TypeError(`Reference index ${o} out of range`);n=t[o],s=e.subarray(1),f=!1}else if(r>=193&&r<=196){let o=r-192,i=h(e,1,o);if(i>=t.length)throw TypeError(`UID ${i} out of range`);n=t[i],s=e.subarray(1+o),f=!1}else throw TypeError(`Unknown tag 0x${r.toString(16)}`);if(f)t.push(n);return[n,s]}var P={};g(P,{serialize:()=>W,parse:()=>K});import{parse as K}from"@plist/binary.parse";import{serialize as W}from"@plist/binary.serialize";var M={};g(M,{encode:()=>Y,decode:()=>Z,bail:()=>X,Value:()=>b,State:()=>Q,Method:()=>J,Flags:()=>G,ErrorCode:()=>C});var G={TransientPairing:16},C={Unknown:1,Authentication:2,BackOff:3,MaxPeers:4,MaxTries:5,Unavailable:6,Busy:7},J={PairSetup:0,PairSetupWithAuth:1,PairVerify:2,AddPairing:3,RemovePairing:4,ListPairing:5},Q={M1:1,M2:2,M3:3,M4:4,M5:5,M6:6},b={Method:0,Identifier:1,Salt:2,PublicKey:3,Proof:4,EncryptedData:5,State:6,Error:7,BackOff:8,Certificate:9,Signature:10,Permissions:11,FragmentData:12,FragmentLast:13,Name:17,Flags:19};function X(e){if(e.has(b.BackOff)){let t=e.get(b.BackOff),r=t.readUintLE(0,t.length);throw Error(`Device is busy, try again in ${r} seconds.`)}if(e.has(b.Error)){let r=Object.entries(C).find(([f,n])=>n===e.get(b.Error).readUint8());if(!r)throw Error(`Device returned an unknown error code: ${e.get(b.Error).readUint8()}`);throw Error(`Device returned an error code: ${r[0]}`)}throw Error("Invalid response")}function Y(e){let t=[];for(let[r,f]of e){let n;if(typeof f==="number")n=Buffer.from([f]);else n=f;let s=0;do{let o=Math.min(n.length-s,255);if(t.push(r,o),o>0)for(let i=0;i<o;i++)t.push(n[s+i]);s+=o}while(s<n.length)}return Buffer.from(t)}function Z(e){let t=new Map,r=0;while(r<e.length){let f=e[r++],n=e[r++],s=new Uint8Array(e).slice(r,r+n);r+=n;let o=t.get(f);if(o)t.set(f,Buffer.concat([o,s]));else t.set(f,Buffer.from(s))}return t}export{M as TLV8,P as Plist,k as OPack,I as NTP};
|
package/dist/ntp.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare function now(): bigint;
|
|
2
|
+
export declare function ns(): bigint;
|
|
3
|
+
export declare function parts(ntp: bigint): [number, number];
|
|
4
|
+
export declare function decode(buffer: Buffer): PacketFields;
|
|
5
|
+
export declare function encode(fields: PacketFields): Buffer;
|
|
6
|
+
export type PacketFields = {
|
|
7
|
+
proto: number;
|
|
8
|
+
type: number;
|
|
9
|
+
seqno: number;
|
|
10
|
+
padding: number;
|
|
11
|
+
reftime_sec: number;
|
|
12
|
+
reftime_frac: number;
|
|
13
|
+
recvtime_sec: number;
|
|
14
|
+
recvtime_frac: number;
|
|
15
|
+
sendtime_sec: number;
|
|
16
|
+
sendtime_frac: number;
|
|
17
|
+
};
|
package/dist/opack.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare class OPackSizedInt extends Number {
|
|
2
|
+
size: number;
|
|
3
|
+
constructor(value: number, size: number);
|
|
4
|
+
}
|
|
5
|
+
export declare function sizedInt(value: number, size: number): OPackSizedInt;
|
|
6
|
+
export declare function float(value: number);
|
|
7
|
+
export declare function int(value: number);
|
|
8
|
+
export declare function encode(data: any): Uint8Array;
|
|
9
|
+
export declare function decode(data: Uint8Array): any;
|
|
10
|
+
export {};
|
package/dist/plist.d.ts
ADDED
package/dist/tlv8.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export declare const Flags: {
|
|
2
|
+
readonly TransientPairing: 0x10;
|
|
3
|
+
};
|
|
4
|
+
export declare const ErrorCode: {
|
|
5
|
+
readonly Unknown: 0x01;
|
|
6
|
+
readonly Authentication: 0x02;
|
|
7
|
+
readonly BackOff: 0x03;
|
|
8
|
+
readonly MaxPeers: 0x04;
|
|
9
|
+
readonly MaxTries: 0x05;
|
|
10
|
+
readonly Unavailable: 0x06;
|
|
11
|
+
readonly Busy: 0x07;
|
|
12
|
+
};
|
|
13
|
+
export declare const Method: {
|
|
14
|
+
readonly PairSetup: 0x00;
|
|
15
|
+
readonly PairSetupWithAuth: 0x01;
|
|
16
|
+
readonly PairVerify: 0x02;
|
|
17
|
+
readonly AddPairing: 0x03;
|
|
18
|
+
readonly RemovePairing: 0x04;
|
|
19
|
+
readonly ListPairing: 0x05;
|
|
20
|
+
};
|
|
21
|
+
export declare const State: {
|
|
22
|
+
readonly M1: 0x01;
|
|
23
|
+
readonly M2: 0x02;
|
|
24
|
+
readonly M3: 0x03;
|
|
25
|
+
readonly M4: 0x04;
|
|
26
|
+
readonly M5: 0x05;
|
|
27
|
+
readonly M6: 0x06;
|
|
28
|
+
};
|
|
29
|
+
export declare const Value: {
|
|
30
|
+
readonly Method: 0x00;
|
|
31
|
+
readonly Identifier: 0x01;
|
|
32
|
+
readonly Salt: 0x02;
|
|
33
|
+
readonly PublicKey: 0x03;
|
|
34
|
+
readonly Proof: 0x04;
|
|
35
|
+
readonly EncryptedData: 0x05;
|
|
36
|
+
readonly State: 0x06;
|
|
37
|
+
readonly Error: 0x07;
|
|
38
|
+
readonly BackOff: 0x08;
|
|
39
|
+
readonly Certificate: 0x09;
|
|
40
|
+
readonly Signature: 0x0A;
|
|
41
|
+
readonly Permissions: 0x0B;
|
|
42
|
+
readonly FragmentData: 0x0C;
|
|
43
|
+
readonly FragmentLast: 0x0D;
|
|
44
|
+
readonly Name: 0x11;
|
|
45
|
+
readonly Flags: 0x13;
|
|
46
|
+
};
|
|
47
|
+
export declare function bail(data: Map<number, Buffer>): never;
|
|
48
|
+
export declare function encode(entries: [number, number | Buffer][]): Buffer;
|
|
49
|
+
export declare function decode(buf: Buffer): Map<number, Buffer>;
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@basmilius/apple-encoding",
|
|
3
|
+
"description": "Common encoding utilities for Apple Protocols.",
|
|
4
|
+
"version": "0.0.76",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Bas Milius",
|
|
9
|
+
"email": "bas@mili.us",
|
|
10
|
+
"url": "https://bas.dev"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"apple",
|
|
14
|
+
"airplay",
|
|
15
|
+
"companion link",
|
|
16
|
+
"opack",
|
|
17
|
+
"plist",
|
|
18
|
+
"tlv8"
|
|
19
|
+
],
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"provenance": true
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc && bun -b build.ts"
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"typings": "./dist/index.d.ts",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@plist/binary.parse": "^1.1.0",
|
|
43
|
+
"@plist/binary.serialize": "^1.1.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@basmilius/tools": "^2.23.0",
|
|
47
|
+
"@types/bun": "^1.3.5"
|
|
48
|
+
}
|
|
49
|
+
}
|