amiudmodz 6.3.0 → 6.3.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/lib/Foundation/ai-rich.d.ts +123 -0
- package/lib/Foundation/ai-rich.d.ts.map +1 -0
- package/lib/Foundation/ai-rich.js +542 -0
- package/lib/Foundation/ai-rich.js.map +1 -0
- package/lib/Foundation/audio-duration.d.ts +3 -0
- package/lib/Foundation/audio-duration.d.ts.map +1 -0
- package/lib/Foundation/audio-duration.js +514 -0
- package/lib/Foundation/audio-duration.js.map +1 -0
- package/lib/Foundation/audio-samples.d.ts +3 -0
- package/lib/Foundation/audio-samples.d.ts.map +1 -0
- package/lib/Foundation/audio-samples.js +294 -0
- package/lib/Foundation/audio-samples.js.map +1 -0
- package/lib/Foundation/boom.d.ts +17 -0
- package/lib/Foundation/boom.d.ts.map +1 -0
- package/lib/Foundation/boom.js +37 -0
- package/lib/Foundation/boom.js.map +1 -0
- package/lib/Foundation/cache.d.ts +25 -0
- package/lib/Foundation/cache.d.ts.map +1 -0
- package/lib/Foundation/cache.js +112 -0
- package/lib/Foundation/cache.js.map +1 -0
- package/lib/Foundation/concurrency.d.ts +13 -0
- package/lib/Foundation/concurrency.d.ts.map +1 -0
- package/lib/Foundation/concurrency.js +62 -0
- package/lib/Foundation/concurrency.js.map +1 -0
- package/lib/Foundation/curve25519.d.ts +11 -0
- package/lib/Foundation/curve25519.d.ts.map +1 -0
- package/lib/Foundation/curve25519.js +178 -0
- package/lib/Foundation/curve25519.js.map +1 -0
- package/lib/Foundation/image-thumb.d.ts +13 -0
- package/lib/Foundation/image-thumb.d.ts.map +1 -0
- package/lib/Foundation/image-thumb.js +65 -0
- package/lib/Foundation/image-thumb.js.map +1 -0
- package/lib/Foundation/link-preview.d.ts +29 -0
- package/lib/Foundation/link-preview.d.ts.map +1 -0
- package/lib/Foundation/link-preview.js +309 -0
- package/lib/Foundation/link-preview.js.map +1 -0
- package/lib/Foundation/logger.d.ts +31 -0
- package/lib/Foundation/logger.d.ts.map +1 -0
- package/lib/Foundation/logger.js +54 -0
- package/lib/Foundation/logger.js.map +1 -0
- package/lib/Foundation/long.d.ts +33 -0
- package/lib/Foundation/long.d.ts.map +1 -0
- package/lib/Foundation/long.js +135 -0
- package/lib/Foundation/long.js.map +1 -0
- package/lib/Foundation/protobuf-minimal.d.ts +115 -0
- package/lib/Foundation/protobuf-minimal.d.ts.map +1 -0
- package/lib/Foundation/protobuf-minimal.js +614 -0
- package/lib/Foundation/protobuf-minimal.js.map +1 -0
- package/lib/Foundation/qrcode-terminal.d.ts +2 -0
- package/lib/Foundation/qrcode-terminal.d.ts.map +1 -0
- package/lib/Foundation/qrcode-terminal.js +405 -0
- package/lib/Foundation/qrcode-terminal.js.map +1 -0
- package/lib/Foundation/sql-database.d.ts +15 -0
- package/lib/Foundation/sql-database.d.ts.map +1 -0
- package/lib/Foundation/sql-database.js +719 -0
- package/lib/Foundation/sql-database.js.map +1 -0
- package/lib/Foundation/wa-crypto.d.ts +14 -0
- package/lib/Foundation/wa-crypto.d.ts.map +1 -0
- package/lib/Foundation/wa-crypto.js +48 -0
- package/lib/Foundation/wa-crypto.js.map +1 -0
- package/lib/Foundation/zip.d.ts +2 -0
- package/lib/Foundation/zip.d.ts.map +1 -0
- package/lib/Foundation/zip.js +98 -0
- package/lib/Foundation/zip.js.map +1 -0
- package/lib/Signal/libsignal.js +0 -3
- package/lib/Signal/libsignal.js.map +1 -1
- package/lib/Socket/business.d.ts +3 -1
- package/lib/Socket/business.d.ts.map +1 -1
- package/lib/Socket/communities.d.ts +3 -1
- package/lib/Socket/communities.d.ts.map +1 -1
- package/lib/Socket/index.d.ts.map +1 -1
- package/lib/Socket/index.js +19 -14
- package/lib/Socket/index.js.map +1 -1
- package/lib/Socket/messages-recv.d.ts +3 -1
- package/lib/Socket/messages-recv.d.ts.map +1 -1
- package/lib/Socket/messages-recv.js +6 -2
- package/lib/Socket/messages-recv.js.map +1 -1
- package/lib/Socket/messages-send.d.ts +3 -1
- package/lib/Socket/messages-send.d.ts.map +1 -1
- package/lib/Socket/messages-send.js +298 -52
- package/lib/Socket/messages-send.js.map +1 -1
- package/lib/Utils/messages-media.d.ts.map +1 -1
- package/lib/Utils/messages-media.js +89 -26
- package/lib/Utils/messages-media.js.map +1 -1
- package/lib/Utils/messages.d.ts +6 -0
- package/lib/Utils/messages.d.ts.map +1 -1
- package/lib/Utils/messages.js +198 -27
- package/lib/Utils/messages.js.map +1 -1
- package/lib/VoIP/WaVoipCoordinator.d.ts +39 -0
- package/lib/VoIP/WaVoipCoordinator.d.ts.map +1 -0
- package/lib/VoIP/WaVoipCoordinator.js +156 -0
- package/lib/VoIP/WaVoipCoordinator.js.map +1 -0
- package/lib/VoIP/app-data/WaAppDataStream.d.ts +35 -0
- package/lib/VoIP/app-data/WaAppDataStream.d.ts.map +1 -0
- package/lib/VoIP/app-data/WaAppDataStream.js +174 -0
- package/lib/VoIP/app-data/WaAppDataStream.js.map +1 -0
- package/lib/VoIP/app-data/protocol.d.ts +28 -0
- package/lib/VoIP/app-data/protocol.d.ts.map +1 -0
- package/lib/VoIP/app-data/protocol.js +167 -0
- package/lib/VoIP/app-data/protocol.js.map +1 -0
- package/lib/VoIP/bytes.d.ts +19 -0
- package/lib/VoIP/bytes.d.ts.map +1 -0
- package/lib/VoIP/bytes.js +64 -0
- package/lib/VoIP/bytes.js.map +1 -0
- package/lib/VoIP/call/WaCallManager.d.ts +58 -0
- package/lib/VoIP/call/WaCallManager.d.ts.map +1 -0
- package/lib/VoIP/call/WaCallManager.js +434 -0
- package/lib/VoIP/call/WaCallManager.js.map +1 -0
- package/lib/VoIP/call/WaCallMediaSession.d.ts +143 -0
- package/lib/VoIP/call/WaCallMediaSession.d.ts.map +1 -0
- package/lib/VoIP/call/WaCallMediaSession.js +1674 -0
- package/lib/VoIP/call/WaCallMediaSession.js.map +1 -0
- package/lib/VoIP/call/call-state.d.ts +34 -0
- package/lib/VoIP/call/call-state.d.ts.map +1 -0
- package/lib/VoIP/call/call-state.js +189 -0
- package/lib/VoIP/call/call-state.js.map +1 -0
- package/lib/VoIP/crypto/encryption.d.ts +6 -0
- package/lib/VoIP/crypto/encryption.d.ts.map +1 -0
- package/lib/VoIP/crypto/encryption.js +14 -0
- package/lib/VoIP/crypto/encryption.js.map +1 -0
- package/lib/VoIP/crypto/primitives.d.ts +5 -0
- package/lib/VoIP/crypto/primitives.d.ts.map +1 -0
- package/lib/VoIP/crypto/primitives.js +22 -0
- package/lib/VoIP/crypto/primitives.js.map +1 -0
- package/lib/VoIP/crypto/rtcp.d.ts +6 -0
- package/lib/VoIP/crypto/rtcp.d.ts.map +1 -0
- package/lib/VoIP/crypto/rtcp.js +71 -0
- package/lib/VoIP/crypto/rtcp.js.map +1 -0
- package/lib/VoIP/crypto/srtp.d.ts +40 -0
- package/lib/VoIP/crypto/srtp.d.ts.map +1 -0
- package/lib/VoIP/crypto/srtp.js +174 -0
- package/lib/VoIP/crypto/srtp.js.map +1 -0
- package/lib/VoIP/crypto/ssrc.d.ts +4 -0
- package/lib/VoIP/crypto/ssrc.d.ts.map +1 -0
- package/lib/VoIP/crypto/ssrc.js +15 -0
- package/lib/VoIP/crypto/ssrc.js.map +1 -0
- package/lib/VoIP/index.d.ts +82 -104
- package/lib/VoIP/index.d.ts.map +1 -1
- package/lib/VoIP/index.js +596 -906
- package/lib/VoIP/index.js.map +1 -1
- package/lib/VoIP/media/WaAudioEngine.d.ts +79 -0
- package/lib/VoIP/media/WaAudioEngine.d.ts.map +1 -0
- package/lib/VoIP/media/WaAudioEngine.js +478 -0
- package/lib/VoIP/media/WaAudioEngine.js.map +1 -0
- package/lib/VoIP/media/WaVideoEngine.d.ts +31 -0
- package/lib/VoIP/media/WaVideoEngine.d.ts.map +1 -0
- package/lib/VoIP/media/WaVideoEngine.js +261 -0
- package/lib/VoIP/media/WaVideoEngine.js.map +1 -0
- package/lib/VoIP/media/audio-codec.d.ts +74 -0
- package/lib/VoIP/media/audio-codec.d.ts.map +1 -0
- package/lib/VoIP/media/audio-codec.js +0 -0
- package/lib/VoIP/media/audio-codec.js.map +1 -0
- package/lib/VoIP/media/audio-reorder.d.ts +19 -0
- package/lib/VoIP/media/audio-reorder.d.ts.map +1 -0
- package/lib/VoIP/media/audio-reorder.js +112 -0
- package/lib/VoIP/media/audio-reorder.js.map +1 -0
- package/lib/VoIP/media/h264.d.ts +21 -0
- package/lib/VoIP/media/h264.d.ts.map +1 -0
- package/lib/VoIP/media/h264.js +239 -0
- package/lib/VoIP/media/h264.js.map +1 -0
- package/lib/VoIP/media/mlow-codec.d.ts +77 -0
- package/lib/VoIP/media/mlow-codec.d.ts.map +1 -0
- package/lib/VoIP/media/mlow-codec.js +467 -0
- package/lib/VoIP/media/mlow-codec.js.map +1 -0
- package/lib/VoIP/media/rtcp.d.ts +10 -0
- package/lib/VoIP/media/rtcp.d.ts.map +1 -0
- package/lib/VoIP/media/rtcp.js +61 -0
- package/lib/VoIP/media/rtcp.js.map +1 -0
- package/lib/VoIP/media/rtp.d.ts +56 -0
- package/lib/VoIP/media/rtp.d.ts.map +1 -0
- package/lib/VoIP/media/rtp.js +225 -0
- package/lib/VoIP/media/rtp.js.map +1 -0
- package/lib/VoIP/protobuf.d.ts +3 -0
- package/lib/VoIP/protobuf.d.ts.map +1 -0
- package/lib/VoIP/protobuf.js +30 -0
- package/lib/VoIP/protobuf.js.map +1 -0
- package/lib/VoIP/relay/WaManualRelay.d.ts +62 -0
- package/lib/VoIP/relay/WaManualRelay.d.ts.map +1 -0
- package/lib/VoIP/relay/WaManualRelay.js +619 -0
- package/lib/VoIP/relay/WaManualRelay.js.map +1 -0
- package/lib/VoIP/relay/datachannel/pre-negotiated.d.ts +22 -0
- package/lib/VoIP/relay/datachannel/pre-negotiated.d.ts.map +1 -0
- package/lib/VoIP/relay/datachannel/pre-negotiated.js +34 -0
- package/lib/VoIP/relay/datachannel/pre-negotiated.js.map +1 -0
- package/lib/VoIP/relay/dtls/aead.d.ts +23 -0
- package/lib/VoIP/relay/dtls/aead.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/aead.js +41 -0
- package/lib/VoIP/relay/dtls/aead.js.map +1 -0
- package/lib/VoIP/relay/dtls/cert-builder.d.ts +6 -0
- package/lib/VoIP/relay/dtls/cert-builder.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/cert-builder.js +68 -0
- package/lib/VoIP/relay/dtls/cert-builder.js.map +1 -0
- package/lib/VoIP/relay/dtls/der.d.ts +19 -0
- package/lib/VoIP/relay/dtls/der.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/der.js +96 -0
- package/lib/VoIP/relay/dtls/der.js.map +1 -0
- package/lib/VoIP/relay/dtls/ecdhe.d.ts +9 -0
- package/lib/VoIP/relay/dtls/ecdhe.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/ecdhe.js +33 -0
- package/lib/VoIP/relay/dtls/ecdhe.js.map +1 -0
- package/lib/VoIP/relay/dtls/handshake-framing.d.ts +49 -0
- package/lib/VoIP/relay/dtls/handshake-framing.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/handshake-framing.js +102 -0
- package/lib/VoIP/relay/dtls/handshake-framing.js.map +1 -0
- package/lib/VoIP/relay/dtls/handshake-messages.d.ts +64 -0
- package/lib/VoIP/relay/dtls/handshake-messages.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/handshake-messages.js +154 -0
- package/lib/VoIP/relay/dtls/handshake-messages.js.map +1 -0
- package/lib/VoIP/relay/dtls/handshake.d.ts +28 -0
- package/lib/VoIP/relay/dtls/handshake.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/handshake.js +555 -0
- package/lib/VoIP/relay/dtls/handshake.js.map +1 -0
- package/lib/VoIP/relay/dtls/prf.d.ts +13 -0
- package/lib/VoIP/relay/dtls/prf.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/prf.js +35 -0
- package/lib/VoIP/relay/dtls/prf.js.map +1 -0
- package/lib/VoIP/relay/dtls/record.d.ts +25 -0
- package/lib/VoIP/relay/dtls/record.d.ts.map +1 -0
- package/lib/VoIP/relay/dtls/record.js +49 -0
- package/lib/VoIP/relay/dtls/record.js.map +1 -0
- package/lib/VoIP/relay/relay-ack.d.ts +31 -0
- package/lib/VoIP/relay/relay-ack.d.ts.map +1 -0
- package/lib/VoIP/relay/relay-ack.js +142 -0
- package/lib/VoIP/relay/relay-ack.js.map +1 -0
- package/lib/VoIP/relay/sctp/association.d.ts +22 -0
- package/lib/VoIP/relay/sctp/association.d.ts.map +1 -0
- package/lib/VoIP/relay/sctp/association.js +434 -0
- package/lib/VoIP/relay/sctp/association.js.map +1 -0
- package/lib/VoIP/relay/sctp/crc32c.d.ts +2 -0
- package/lib/VoIP/relay/sctp/crc32c.d.ts.map +1 -0
- package/lib/VoIP/relay/sctp/crc32c.js +20 -0
- package/lib/VoIP/relay/sctp/crc32c.js.map +1 -0
- package/lib/VoIP/relay/sctp/wire.d.ts +96 -0
- package/lib/VoIP/relay/sctp/wire.d.ts.map +1 -0
- package/lib/VoIP/relay/sctp/wire.js +206 -0
- package/lib/VoIP/relay/sctp/wire.js.map +1 -0
- package/lib/VoIP/relay/stun.d.ts +47 -0
- package/lib/VoIP/relay/stun.d.ts.map +1 -0
- package/lib/VoIP/relay/stun.js +459 -0
- package/lib/VoIP/relay/stun.js.map +1 -0
- package/lib/VoIP/shim/baileys-resolve.d.ts +2 -0
- package/lib/VoIP/shim/baileys-resolve.d.ts.map +1 -0
- package/lib/VoIP/shim/baileys-resolve.js +22 -0
- package/lib/VoIP/shim/baileys-resolve.js.map +1 -0
- package/lib/VoIP/shim/core.d.ts +32 -0
- package/lib/VoIP/shim/core.d.ts.map +1 -0
- package/lib/VoIP/shim/core.js +67 -0
- package/lib/VoIP/shim/core.js.map +1 -0
- package/lib/VoIP/shim/crypto.d.ts +2 -0
- package/lib/VoIP/shim/crypto.d.ts.map +1 -0
- package/lib/VoIP/shim/crypto.js +7 -0
- package/lib/VoIP/shim/crypto.js.map +1 -0
- package/lib/VoIP/shim/proto.d.ts +2 -0
- package/lib/VoIP/shim/proto.d.ts.map +1 -0
- package/lib/VoIP/shim/proto.js +10 -0
- package/lib/VoIP/shim/proto.js.map +1 -0
- package/lib/VoIP/shim/protocol.d.ts +32 -0
- package/lib/VoIP/shim/protocol.d.ts.map +1 -0
- package/lib/VoIP/shim/protocol.js +131 -0
- package/lib/VoIP/shim/protocol.js.map +1 -0
- package/lib/VoIP/shim/transport.d.ts +20 -0
- package/lib/VoIP/shim/transport.d.ts.map +1 -0
- package/lib/VoIP/shim/transport.js +83 -0
- package/lib/VoIP/shim/transport.js.map +1 -0
- package/lib/VoIP/shim/util.d.ts +13 -0
- package/lib/VoIP/shim/util.d.ts.map +1 -0
- package/lib/VoIP/shim/util.js +208 -0
- package/lib/VoIP/shim/util.js.map +1 -0
- package/lib/VoIP/signaling/bridge.d.ts +4 -0
- package/lib/VoIP/signaling/bridge.d.ts.map +1 -0
- package/lib/VoIP/signaling/bridge.js +103 -0
- package/lib/VoIP/signaling/bridge.js.map +1 -0
- package/lib/VoIP/signaling/screen-share.d.ts +40 -0
- package/lib/VoIP/signaling/screen-share.d.ts.map +1 -0
- package/lib/VoIP/signaling/screen-share.js +55 -0
- package/lib/VoIP/signaling/screen-share.js.map +1 -0
- package/lib/VoIP/signaling/signaling.d.ts +396 -0
- package/lib/VoIP/signaling/signaling.d.ts.map +1 -0
- package/lib/VoIP/signaling/signaling.js +488 -0
- package/lib/VoIP/signaling/signaling.js.map +1 -0
- package/lib/VoIP/types.d.ts +37 -12
- package/lib/VoIP/types.d.ts.map +1 -1
- package/lib/VoIP/types.js +65 -12
- package/lib/VoIP/types.js.map +1 -1
- package/lib/VoIP/voip-deps.d.ts +46 -0
- package/lib/VoIP/voip-deps.d.ts.map +1 -0
- package/lib/VoIP/voip-deps.js +244 -0
- package/lib/VoIP/voip-deps.js.map +1 -0
- package/lib/VoIP/voipClient.d.ts +34 -0
- package/lib/VoIP/voipClient.d.ts.map +1 -0
- package/lib/VoIP/voipClient.js +351 -0
- package/lib/VoIP/voipClient.js.map +1 -0
- package/lib/VoIP/wasm-engine.d.ts +2 -2
- package/lib/WABinary/generic-utils.js +1 -1
- package/lib/WABinary/generic-utils.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { createHash, createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, randomBytes } from 'node:crypto';
|
|
2
|
+
const P = (1n << 255n) - 19n;
|
|
3
|
+
const L = (1n << 252n) + 27742317777372353535851937790883648493n;
|
|
4
|
+
const D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;
|
|
5
|
+
const SQRT_M1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
|
|
6
|
+
const BASE_Y = 46316835694926478169428394003475163141307993866256225615783033603165251855960n;
|
|
7
|
+
const BASE_X = 15112221349535400772501151409588531511454012693041857206046113283949847762202n;
|
|
8
|
+
const mod = (a, m = P) => {
|
|
9
|
+
const r = a % m;
|
|
10
|
+
return r >= 0n ? r : r + m;
|
|
11
|
+
};
|
|
12
|
+
const pow = (b, e, m = P) => {
|
|
13
|
+
let r = 1n;
|
|
14
|
+
b = mod(b, m);
|
|
15
|
+
while (e > 0n) {
|
|
16
|
+
if (e & 1n)
|
|
17
|
+
r = (r * b) % m;
|
|
18
|
+
b = (b * b) % m;
|
|
19
|
+
e >>= 1n;
|
|
20
|
+
}
|
|
21
|
+
return r;
|
|
22
|
+
};
|
|
23
|
+
const inv = a => pow(a, P - 2n);
|
|
24
|
+
const bytesToNumLE = b => {
|
|
25
|
+
let n = 0n;
|
|
26
|
+
for (let i = b.length - 1; i >= 0; i--)
|
|
27
|
+
n = (n << 8n) | BigInt(b[i]);
|
|
28
|
+
return n;
|
|
29
|
+
};
|
|
30
|
+
const numToBytesLE = (n, len) => {
|
|
31
|
+
const out = Buffer.alloc(len);
|
|
32
|
+
for (let i = 0; i < len; i++) {
|
|
33
|
+
out[i] = Number(n & 0xffn);
|
|
34
|
+
n >>= 8n;
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
};
|
|
38
|
+
const ZERO = [0n, 1n, 1n, 0n];
|
|
39
|
+
const BASE = [BASE_X, BASE_Y, 1n, mod(BASE_X * BASE_Y)];
|
|
40
|
+
const pointAdd = (p, q) => {
|
|
41
|
+
const [X1, Y1, Z1, T1] = p;
|
|
42
|
+
const [X2, Y2, Z2, T2] = q;
|
|
43
|
+
const A = mod((Y1 - X1) * (Y2 - X2));
|
|
44
|
+
const B = mod((Y1 + X1) * (Y2 + X2));
|
|
45
|
+
const C = mod(2n * D * T1 * T2);
|
|
46
|
+
const Dd = mod(2n * Z1 * Z2);
|
|
47
|
+
const E = B - A;
|
|
48
|
+
const F = Dd - C;
|
|
49
|
+
const G = Dd + C;
|
|
50
|
+
const H = B + A;
|
|
51
|
+
return [mod(E * F), mod(G * H), mod(F * G), mod(E * H)];
|
|
52
|
+
};
|
|
53
|
+
const scalarMult = (point, scalar) => {
|
|
54
|
+
let r = ZERO;
|
|
55
|
+
let q = point;
|
|
56
|
+
while (scalar > 0n) {
|
|
57
|
+
if (scalar & 1n)
|
|
58
|
+
r = pointAdd(r, q);
|
|
59
|
+
q = pointAdd(q, q);
|
|
60
|
+
scalar >>= 1n;
|
|
61
|
+
}
|
|
62
|
+
return r;
|
|
63
|
+
};
|
|
64
|
+
const encodePoint = p => {
|
|
65
|
+
const zi = inv(p[2]);
|
|
66
|
+
const x = mod(p[0] * zi);
|
|
67
|
+
const y = mod(p[1] * zi);
|
|
68
|
+
const out = numToBytesLE(y, 32);
|
|
69
|
+
if (x & 1n)
|
|
70
|
+
out[31] |= 0x80;
|
|
71
|
+
return out;
|
|
72
|
+
};
|
|
73
|
+
const decodePoint = bytes => {
|
|
74
|
+
const b = Buffer.from(bytes);
|
|
75
|
+
const sign = (b[31] & 0x80) >> 7;
|
|
76
|
+
b[31] &= 0x7f;
|
|
77
|
+
const y = bytesToNumLE(b);
|
|
78
|
+
if (y >= P)
|
|
79
|
+
return null;
|
|
80
|
+
const y2 = mod(y * y);
|
|
81
|
+
const u = mod(y2 - 1n);
|
|
82
|
+
const v = mod(D * y2 + 1n);
|
|
83
|
+
const v3 = mod(v * v * v);
|
|
84
|
+
const v7 = mod(v3 * v3 * v);
|
|
85
|
+
let x = mod(u * v3 * pow(mod(u * v7), (P - 5n) / 8n));
|
|
86
|
+
const vx2 = mod(v * x * x);
|
|
87
|
+
if (vx2 !== mod(u)) {
|
|
88
|
+
if (vx2 !== mod(-u))
|
|
89
|
+
return null;
|
|
90
|
+
x = mod(x * SQRT_M1);
|
|
91
|
+
}
|
|
92
|
+
if (Number(x & 1n) !== sign)
|
|
93
|
+
x = mod(-x);
|
|
94
|
+
return [x, y, 1n, mod(x * y)];
|
|
95
|
+
};
|
|
96
|
+
const sha512 = (...parts) => {
|
|
97
|
+
const h = createHash('sha512');
|
|
98
|
+
for (const p of parts)
|
|
99
|
+
h.update(p);
|
|
100
|
+
return h.digest();
|
|
101
|
+
};
|
|
102
|
+
const clamp = key => {
|
|
103
|
+
const k = Buffer.from(key);
|
|
104
|
+
k[0] &= 248;
|
|
105
|
+
k[31] &= 127;
|
|
106
|
+
k[31] |= 64;
|
|
107
|
+
return k;
|
|
108
|
+
};
|
|
109
|
+
const PUB_DER = Buffer.from('302a300506032b656e032100', 'hex');
|
|
110
|
+
const PRIV_DER = Buffer.from('302e020100300506032b656e04220420', 'hex');
|
|
111
|
+
export const sharedKey = (priv, pub) => diffieHellman({
|
|
112
|
+
privateKey: createPrivateKey({ key: Buffer.concat([PRIV_DER, priv]), format: 'der', type: 'pkcs8' }),
|
|
113
|
+
publicKey: createPublicKey({ key: Buffer.concat([PUB_DER, pub]), format: 'der', type: 'spki' })
|
|
114
|
+
});
|
|
115
|
+
export const randomX25519 = () => {
|
|
116
|
+
const { publicKey, privateKey } = generateKeyPairSync('x25519', {
|
|
117
|
+
publicKeyEncoding: { format: 'der', type: 'spki' },
|
|
118
|
+
privateKeyEncoding: { format: 'der', type: 'pkcs8' }
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
pub: publicKey.subarray(PUB_DER.length, PUB_DER.length + 32),
|
|
122
|
+
priv: privateKey.subarray(PRIV_DER.length, PRIV_DER.length + 32)
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
export const publicFromPrivate = seed => {
|
|
126
|
+
if (seed.length !== 32)
|
|
127
|
+
throw new Error('wrong seed length');
|
|
128
|
+
const priv = Buffer.from(seed);
|
|
129
|
+
const base = Buffer.alloc(32);
|
|
130
|
+
base[0] = 9;
|
|
131
|
+
const pub = Buffer.from(sharedKey(priv, base));
|
|
132
|
+
pub[31] &= 0x7f;
|
|
133
|
+
return pub;
|
|
134
|
+
};
|
|
135
|
+
const montToEdY = pk => {
|
|
136
|
+
const b = Buffer.from(pk);
|
|
137
|
+
b[31] &= 0x7f;
|
|
138
|
+
const u = mod(bytesToNumLE(b));
|
|
139
|
+
const y = mod((u - 1n) * inv(mod(u + 1n)));
|
|
140
|
+
return numToBytesLE(y, 32);
|
|
141
|
+
};
|
|
142
|
+
export const sign = (secretKey, msg) => {
|
|
143
|
+
if (secretKey.length !== 32)
|
|
144
|
+
throw new Error('wrong secret key length');
|
|
145
|
+
const a = clamp(secretKey);
|
|
146
|
+
const aNum = bytesToNumLE(a);
|
|
147
|
+
const A = encodePoint(scalarMult(BASE, aNum));
|
|
148
|
+
const signBit = A[31] & 0x80;
|
|
149
|
+
const r = mod(bytesToNumLE(sha512(a, msg)), L);
|
|
150
|
+
const R = encodePoint(scalarMult(BASE, r));
|
|
151
|
+
const Acompressed = Buffer.from(A);
|
|
152
|
+
const h = mod(bytesToNumLE(sha512(R, Acompressed, msg)), L);
|
|
153
|
+
const S = mod(r + h * aNum, L);
|
|
154
|
+
const sig = Buffer.concat([R, numToBytesLE(S, 32)]);
|
|
155
|
+
sig[63] |= signBit;
|
|
156
|
+
return sig;
|
|
157
|
+
};
|
|
158
|
+
export const verify = (publicKey, msg, signature) => {
|
|
159
|
+
if (signature.length !== 64)
|
|
160
|
+
throw new Error('wrong signature length');
|
|
161
|
+
if (publicKey.length !== 32)
|
|
162
|
+
throw new Error('wrong public key length');
|
|
163
|
+
const sig = Buffer.from(signature);
|
|
164
|
+
const edpk = montToEdY(publicKey);
|
|
165
|
+
edpk[31] |= sig[63] & 0x80;
|
|
166
|
+
sig[63] &= 0x7f;
|
|
167
|
+
const A = decodePoint(edpk);
|
|
168
|
+
if (!A)
|
|
169
|
+
return false;
|
|
170
|
+
const R = sig.subarray(0, 32);
|
|
171
|
+
const S = bytesToNumLE(sig.subarray(32));
|
|
172
|
+
const h = mod(bytesToNumLE(sha512(R, edpk, msg)), L);
|
|
173
|
+
const negA = [mod(-A[0]), A[1], A[2], mod(-A[3])];
|
|
174
|
+
const check = pointAdd(scalarMult(negA, h), scalarMult(BASE, S));
|
|
175
|
+
return Buffer.compare(encodePoint(check), R) === 0;
|
|
176
|
+
};
|
|
177
|
+
export { randomBytes };
|
|
178
|
+
//# sourceMappingURL=curve25519.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"curve25519.js","sourceRoot":"","sources":["../../src/Foundation/curve25519.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,eAAe,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE5H,MAAM,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,CAAA;AAC5B,MAAM,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,GAAG,uCAAuC,CAAA;AAEhE,MAAM,CAAC,GAAG,8EAA8E,CAAA;AACxF,MAAM,OAAO,GAAG,8EAA8E,CAAA;AAC9F,MAAM,MAAM,GAAG,8EAA8E,CAAA;AAC7F,MAAM,MAAM,GAAG,8EAA8E,CAAA;AAE7F,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC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AAC,CAAA;IACjD,MAAM,KAAK,GAAG,QAAQ,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAA;IAChE,OAAO,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAA;AACnD,CAAC,CAAA;AAED,OAAO,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {Buffer | string} input image bytes or a file path
|
|
3
|
+
* @param {number} width thumbnail width in px
|
|
4
|
+
* @returns {Promise<{ buffer: Buffer, original: { width?: number, height?: number } }>}
|
|
5
|
+
*/
|
|
6
|
+
export function extractImageThumbFallback(input: Buffer | string, width?: number): Promise<{
|
|
7
|
+
buffer: Buffer;
|
|
8
|
+
original: {
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
};
|
|
12
|
+
}>;
|
|
13
|
+
//# sourceMappingURL=image-thumb.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-thumb.d.ts","sourceRoot":"","sources":["../../src/Foundation/image-thumb.js"],"names":[],"mappings":"AAqDA;;;;GAIG;AACH,iDAJW,MAAM,GAAG,MAAM,UACf,MAAM,GACJ,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAC,CAQtF"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { Boom } from './boom.js';
|
|
4
|
+
// Dependency-free image thumbnails. Used when none of sharp / @napi-rs/image / jimp is installed,
|
|
5
|
+
// so a missing optional library never results in a media message without jpegThumbnail
|
|
6
|
+
// (the recipient sees that as a grey box). Uses ffmpeg.
|
|
7
|
+
const FFMPEG_TIMEOUT_MS = 15000;
|
|
8
|
+
async function runFfmpeg(command, args, input) {
|
|
9
|
+
const child = spawn(command, args, { stdio: [Buffer.isBuffer(input) ? 'pipe' : 'ignore', 'pipe', 'pipe'] });
|
|
10
|
+
const out = [];
|
|
11
|
+
const err = [];
|
|
12
|
+
child.stdout.on('data', c => out.push(c));
|
|
13
|
+
child.stderr.on('data', c => err.push(c));
|
|
14
|
+
if (Buffer.isBuffer(input)) {
|
|
15
|
+
child.stdin.on('error', () => { });
|
|
16
|
+
child.stdin.end(input);
|
|
17
|
+
}
|
|
18
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), FFMPEG_TIMEOUT_MS);
|
|
19
|
+
try {
|
|
20
|
+
const [code] = await once(child, 'close');
|
|
21
|
+
if (code !== 0)
|
|
22
|
+
throw new Boom(`${command} failed (code ${code}): ${Buffer.concat(err).toString('utf8').trim()}`);
|
|
23
|
+
return Buffer.concat(out);
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function probeDimensions(input) {
|
|
30
|
+
try {
|
|
31
|
+
const src = Buffer.isBuffer(input) ? 'pipe:0' : input;
|
|
32
|
+
const args = ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=p=0', src];
|
|
33
|
+
const [width, height] = (await runFfmpeg('ffprobe', args, input)).toString('utf8').trim().split(',').map(Number);
|
|
34
|
+
return width > 0 && height > 0 ? { width, height } : {};
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function ffmpegThumb(input, width) {
|
|
41
|
+
const src = Buffer.isBuffer(input) ? 'pipe:0' : input;
|
|
42
|
+
const args = [
|
|
43
|
+
'-loglevel', 'error', '-i', src, '-an', '-sn', '-dn', '-map_metadata', '-1',
|
|
44
|
+
'-vf', `scale=w='min(${width},iw)':h=-2`,
|
|
45
|
+
'-frames:v', '1', '-q:v', '12', '-c:v', 'mjpeg', '-f', 'image2pipe', 'pipe:1'
|
|
46
|
+
];
|
|
47
|
+
const buffer = await runFfmpeg('ffmpeg', args, input);
|
|
48
|
+
if (!buffer.length)
|
|
49
|
+
throw new Boom('ffmpeg produced no output');
|
|
50
|
+
return { buffer, original: await probeDimensions(input) };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @param {Buffer | string} input image bytes or a file path
|
|
54
|
+
* @param {number} width thumbnail width in px
|
|
55
|
+
* @returns {Promise<{ buffer: Buffer, original: { width?: number, height?: number } }>}
|
|
56
|
+
*/
|
|
57
|
+
export async function extractImageThumbFallback(input, width = 32) {
|
|
58
|
+
try {
|
|
59
|
+
return await ffmpegThumb(input, width);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
throw new Boom(`could not generate image thumbnail (install sharp, @napi-rs/image or jimp, or ffmpeg): ${err?.message ?? String(err)}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=image-thumb.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-thumb.js","sourceRoot":"","sources":["../../src/Foundation/image-thumb.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,kGAAkG;AAClG,uFAAuF;AACvF,wDAAwD;AAExD,MAAM,iBAAiB,GAAG,KAAM,CAAA;AAEhC,KAAK,UAAU,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK;IAC5C,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,MAAM,CA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AAM,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;IACrD,IAAI,CAAC,MAAM,CAAC,MAAM;QAAE,MAAM,IAAI,IAAI,CAAC,2BAA2B,CAAC,CAAA;IAC/D,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC,KAAK,CAAC,EAAE,CAAA;AAC1D,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAAC,KAAK,EAAE,KAAK,GAAG,EAAE;IAChE,IAAI,CAAC;QACJ,OAAO,MAAM,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IACvC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,MAAM,IAAI,IAAI,CAAC,0FAA0F,GAAG,EAAE,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IACxI,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function scanHtml(html: any): {
|
|
2
|
+
metas: never[];
|
|
3
|
+
links: never[];
|
|
4
|
+
imgs: never[];
|
|
5
|
+
title: undefined;
|
|
6
|
+
base: undefined;
|
|
7
|
+
};
|
|
8
|
+
export function getLinkPreview(text: any, options?: {}): Promise<{
|
|
9
|
+
url: string;
|
|
10
|
+
mediaType: string | undefined;
|
|
11
|
+
contentType: string;
|
|
12
|
+
favicons: string[];
|
|
13
|
+
title?: undefined;
|
|
14
|
+
siteName?: undefined;
|
|
15
|
+
description?: undefined;
|
|
16
|
+
images?: undefined;
|
|
17
|
+
videos?: undefined;
|
|
18
|
+
} | {
|
|
19
|
+
url: string;
|
|
20
|
+
title: any;
|
|
21
|
+
siteName: any;
|
|
22
|
+
description: any;
|
|
23
|
+
mediaType: any;
|
|
24
|
+
contentType: string;
|
|
25
|
+
images: any[];
|
|
26
|
+
videos: any[];
|
|
27
|
+
favicons: string[];
|
|
28
|
+
}>;
|
|
29
|
+
//# sourceMappingURL=link-preview.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-preview.d.ts","sourceRoot":"","sources":["../../src/Foundation/link-preview.js"],"names":[],"mappings":"AA2CA;;;;;;EA8GC;AAoDD;;;;;;;;;;;;;;;;;;;;GAwEC"}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
const REGEX_VALID_URL = /^(?:https?:\/\/)[^\s]+$/i;
|
|
2
|
+
const DEFAULT_UA = 'Mozilla/5.0 (compatible; baileys-lite link preview)';
|
|
3
|
+
const MAX_BYTES = 1024 * 1024;
|
|
4
|
+
const NAMED = {
|
|
5
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: '\u00a0', copy: '©', reg: '®', trade: '™', hellip: '…', mdash: '—', ndash: '–',
|
|
6
|
+
lsquo: '‘', rsquo: '’', ldquo: '“', rdquo: '”', laquo: '«', raquo: '»', bull: '•', middot: '·', euro: '€', pound: '£', yen: '¥', cent: '¢',
|
|
7
|
+
sect: '§', deg: '°', plusmn: '±', times: '×', divide: '÷', para: '¶', iexcl: '¡', iquest: '¿', shy: '\u00ad', larr: '←', rarr: '→', uarr: '↑',
|
|
8
|
+
darr: '↓', hearts: '♥', check: '✓', star: '☆', infin: '∞', ne: '≠', le: '≤', ge: '≥', frac12: '½', frac14: '¼', frac34: '¾', sup2: '²', sup3: '³',
|
|
9
|
+
micro: 'µ', agrave: 'à', aacute: 'á', acirc: 'â', atilde: 'ã', auml: 'ä', aring: 'å', aelig: 'æ', ccedil: 'ç', egrave: 'è', eacute: 'é', ecirc: 'ê',
|
|
10
|
+
euml: 'ë', igrave: 'ì', iacute: 'í', icirc: 'î', iuml: 'ï', ntilde: 'ñ', ograve: 'ò', oacute: 'ó', ocirc: 'ô', otilde: 'õ', ouml: 'ö', oslash: 'ø',
|
|
11
|
+
ugrave: 'ù', uacute: 'ú', ucirc: 'û', uuml: 'ü', yacute: 'ý', yuml: 'ÿ', szlig: 'ß', Agrave: 'À', Aacute: 'Á', Acirc: 'Â', Atilde: 'Ã', Auml: 'Ä',
|
|
12
|
+
Aring: 'Å', AElig: 'Æ', Ccedil: 'Ç', Egrave: 'È', Eacute: 'É', Ecirc: 'Ê', Euml: 'Ë', Igrave: 'Ì', Iacute: 'Í', Icirc: 'Î', Iuml: 'Ï', Ntilde: 'Ñ',
|
|
13
|
+
Ograve: 'Ò', Oacute: 'Ó', Ocirc: 'Ô', Otilde: 'Õ', Ouml: 'Ö', Oslash: 'Ø', Ugrave: 'Ù', Uacute: 'Ú', Ucirc: 'Û', Uuml: 'Ü', Yacute: 'Ý'
|
|
14
|
+
};
|
|
15
|
+
const LEGACY = /^(amp|lt|gt|quot|nbsp|copy|reg|deg|plusmn|times|divide|para|sect|shy|micro|middot|laquo|raquo|iexcl|iquest|cent|pound|yen)$/;
|
|
16
|
+
const decodeEntities = (s, inAttr = false) => {
|
|
17
|
+
if (!s.includes('&'))
|
|
18
|
+
return s;
|
|
19
|
+
return s.replace(/&(?:#(\d+)|#[xX]([0-9a-fA-F]+)|([A-Za-z][A-Za-z0-9]*))(;?)/g, (m, dec, hex, name, semi, offset) => {
|
|
20
|
+
if (name) {
|
|
21
|
+
if (semi && Object.prototype.hasOwnProperty.call(NAMED, name))
|
|
22
|
+
return NAMED[name];
|
|
23
|
+
for (let len = name.length; len >= 2; len--) {
|
|
24
|
+
const prefix = name.slice(0, len);
|
|
25
|
+
if (!LEGACY.test(prefix))
|
|
26
|
+
continue;
|
|
27
|
+
const rest = name.slice(len) + semi;
|
|
28
|
+
const next = rest[0] ?? s[offset + m.length];
|
|
29
|
+
if (inAttr && next !== undefined && /[=A-Za-z0-9]/.test(next))
|
|
30
|
+
return m;
|
|
31
|
+
return NAMED[prefix] + rest;
|
|
32
|
+
}
|
|
33
|
+
return m;
|
|
34
|
+
}
|
|
35
|
+
const cp = dec !== undefined ? parseInt(dec, 10) : parseInt(hex, 16);
|
|
36
|
+
if (!cp || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff))
|
|
37
|
+
return '\ufffd';
|
|
38
|
+
return String.fromCodePoint(cp);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
const TEXT_ELEMENTS = new Set(['script', 'style', 'textarea', 'title', 'xmp', 'iframe', 'noembed', 'noframes', 'noscript']);
|
|
42
|
+
const WS = new Set([' ', '\t', '\n', '\f', '\r']);
|
|
43
|
+
const isAlpha = c => c !== undefined && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
|
|
44
|
+
export function scanHtml(html) {
|
|
45
|
+
const out = { metas: [], links: [], imgs: [], title: undefined, base: undefined };
|
|
46
|
+
const n = html.length;
|
|
47
|
+
let i = 0;
|
|
48
|
+
while (i < n) {
|
|
49
|
+
const lt = html.indexOf('<', i);
|
|
50
|
+
if (lt === -1)
|
|
51
|
+
break;
|
|
52
|
+
i = lt + 1;
|
|
53
|
+
const c = html[i];
|
|
54
|
+
if (c === '!') {
|
|
55
|
+
if (html.startsWith('--', i + 1)) {
|
|
56
|
+
if (html[i + 3] === '>')
|
|
57
|
+
i += 4;
|
|
58
|
+
else if (html.startsWith('->', i + 3))
|
|
59
|
+
i += 5;
|
|
60
|
+
else {
|
|
61
|
+
const end = html.indexOf('-->', i + 3);
|
|
62
|
+
const alt = html.indexOf('--!>', i + 3);
|
|
63
|
+
const e = end === -1 ? alt : alt === -1 ? end : Math.min(end, alt);
|
|
64
|
+
if (e === -1)
|
|
65
|
+
break;
|
|
66
|
+
i = e + (html.startsWith('--!>', e) ? 4 : 3);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const gt = html.indexOf('>', i);
|
|
71
|
+
if (gt === -1)
|
|
72
|
+
break;
|
|
73
|
+
i = gt + 1;
|
|
74
|
+
}
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (c === '?') {
|
|
78
|
+
const gt = html.indexOf('>', i);
|
|
79
|
+
if (gt === -1)
|
|
80
|
+
break;
|
|
81
|
+
i = gt + 1;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (c === '/') {
|
|
85
|
+
const d = html[i + 1];
|
|
86
|
+
if (d === '>') {
|
|
87
|
+
i += 2;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const gt = html.indexOf('>', i);
|
|
91
|
+
if (gt === -1)
|
|
92
|
+
break;
|
|
93
|
+
i = gt + 1;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (!isAlpha(c))
|
|
97
|
+
continue;
|
|
98
|
+
let j = i;
|
|
99
|
+
while (j < n && !WS.has(html[j]) && html[j] !== '/' && html[j] !== '>')
|
|
100
|
+
j++;
|
|
101
|
+
let name = html.slice(i, j).toLowerCase();
|
|
102
|
+
if (name === 'image')
|
|
103
|
+
name = 'img';
|
|
104
|
+
const attrs = {};
|
|
105
|
+
let closed = false;
|
|
106
|
+
for (;;) {
|
|
107
|
+
while (j < n && (WS.has(html[j]) || html[j] === '/'))
|
|
108
|
+
j++;
|
|
109
|
+
if (j >= n)
|
|
110
|
+
break;
|
|
111
|
+
if (html[j] === '>') {
|
|
112
|
+
j++;
|
|
113
|
+
closed = true;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
let k = j + 1;
|
|
117
|
+
while (k < n && !WS.has(html[k]) && html[k] !== '/' && html[k] !== '>' && html[k] !== '=')
|
|
118
|
+
k++;
|
|
119
|
+
const aname = html.slice(j, k).toLowerCase().replace(/\0/g, '\ufffd');
|
|
120
|
+
j = k;
|
|
121
|
+
while (j < n && WS.has(html[j]))
|
|
122
|
+
j++;
|
|
123
|
+
let value = '';
|
|
124
|
+
if (html[j] === '=') {
|
|
125
|
+
j++;
|
|
126
|
+
while (j < n && WS.has(html[j]))
|
|
127
|
+
j++;
|
|
128
|
+
const q = html[j];
|
|
129
|
+
if (q === '"' || q === "'") {
|
|
130
|
+
const e = html.indexOf(q, j + 1);
|
|
131
|
+
if (e === -1) {
|
|
132
|
+
j = n;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
value = html.slice(j + 1, e);
|
|
136
|
+
j = e + 1;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
let e = j;
|
|
140
|
+
while (e < n && !WS.has(html[e]) && html[e] !== '>')
|
|
141
|
+
e++;
|
|
142
|
+
value = html.slice(j, e);
|
|
143
|
+
j = e;
|
|
144
|
+
}
|
|
145
|
+
value = decodeEntities(value, true);
|
|
146
|
+
}
|
|
147
|
+
if (!(aname in attrs))
|
|
148
|
+
attrs[aname] = value;
|
|
149
|
+
}
|
|
150
|
+
if (!closed)
|
|
151
|
+
break;
|
|
152
|
+
i = j;
|
|
153
|
+
if (name === 'meta')
|
|
154
|
+
out.metas.push(attrs);
|
|
155
|
+
else if (name === 'link')
|
|
156
|
+
out.links.push(attrs);
|
|
157
|
+
else if (name === 'img')
|
|
158
|
+
out.imgs.push(attrs);
|
|
159
|
+
else if (name === 'base') {
|
|
160
|
+
if (out.base === undefined && attrs.href)
|
|
161
|
+
out.base = attrs.href;
|
|
162
|
+
}
|
|
163
|
+
if (TEXT_ELEMENTS.has(name)) {
|
|
164
|
+
const re = new RegExp('</' + name + '(?=[\\s/>])', 'i');
|
|
165
|
+
const m = re.exec(html.slice(i));
|
|
166
|
+
const end = m ? i + m.index : n;
|
|
167
|
+
if (name === 'title' && out.title === undefined)
|
|
168
|
+
out.title = decodeEntities(html.slice(i, end));
|
|
169
|
+
i = end;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
const resolveUrl = (href, base) => {
|
|
175
|
+
try {
|
|
176
|
+
return new URL(href.trim(), base).href;
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const charsetFrom = (contentType, head) => {
|
|
183
|
+
const h = /charset\s*=\s*["']?([\w.:-]+)/i.exec(contentType || '');
|
|
184
|
+
if (h)
|
|
185
|
+
return h[1];
|
|
186
|
+
const m = /<meta[^>]+charset\s*=\s*["']?([\w.:-]+)/i.exec(head) || /<meta[^>]+content\s*=\s*["'][^"']*charset=([\w.:-]+)/i.exec(head);
|
|
187
|
+
return m ? m[1] : 'utf-8';
|
|
188
|
+
};
|
|
189
|
+
const decodeBody = (bytes, contentType) => {
|
|
190
|
+
const head = Buffer.from(bytes.subarray(0, 2048)).toString('latin1');
|
|
191
|
+
const charset = charsetFrom(contentType, head);
|
|
192
|
+
try {
|
|
193
|
+
return new TextDecoder(charset).decode(bytes);
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return new TextDecoder('utf-8').decode(bytes);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
const readBody = async (res, max) => {
|
|
200
|
+
const reader = res.body?.getReader();
|
|
201
|
+
if (!reader)
|
|
202
|
+
return new Uint8Array(await res.arrayBuffer()).subarray(0, max);
|
|
203
|
+
const chunks = [];
|
|
204
|
+
let total = 0;
|
|
205
|
+
let tail = '';
|
|
206
|
+
while (total < max) {
|
|
207
|
+
const { done, value } = await reader.read();
|
|
208
|
+
if (done)
|
|
209
|
+
break;
|
|
210
|
+
chunks.push(value);
|
|
211
|
+
total += value.length;
|
|
212
|
+
tail = (tail + Buffer.from(value).toString('latin1')).slice(-16);
|
|
213
|
+
if (/<\/head\s*>/i.test(tail))
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
reader.cancel().catch(() => { });
|
|
217
|
+
const buf = new Uint8Array(Math.min(total, max));
|
|
218
|
+
let o = 0;
|
|
219
|
+
for (const c of chunks) {
|
|
220
|
+
const take = Math.min(c.length, buf.length - o);
|
|
221
|
+
buf.set(c.subarray(0, take), o);
|
|
222
|
+
o += take;
|
|
223
|
+
}
|
|
224
|
+
return buf;
|
|
225
|
+
};
|
|
226
|
+
export async function getLinkPreview(text, options = {}) {
|
|
227
|
+
if (typeof text !== 'string')
|
|
228
|
+
throw new Error('link-preview did not receive a valid a url or text');
|
|
229
|
+
const detected = text.replace(/\n/g, ' ').split(' ').find(token => REGEX_VALID_URL.test(token));
|
|
230
|
+
if (!detected)
|
|
231
|
+
throw new Error('link-preview did not receive a valid a url or text');
|
|
232
|
+
const { timeout = 3000, followRedirects = 'follow', handleRedirects, maxBytes = MAX_BYTES } = options;
|
|
233
|
+
const headers = { 'user-agent': DEFAULT_UA, accept: 'text/html,application/xhtml+xml,*/*;q=0.8', 'accept-language': 'en-US,en;q=0.9', ...(options.headers || {}) };
|
|
234
|
+
let url = detected;
|
|
235
|
+
let res;
|
|
236
|
+
for (let hops = 0;; hops++) {
|
|
237
|
+
res = await fetch(url, { headers, redirect: followRedirects === 'manual' ? 'manual' : followRedirects, signal: AbortSignal.timeout(timeout) });
|
|
238
|
+
if (followRedirects === 'manual' && res.status >= 300 && res.status < 400 && res.headers.get('location')) {
|
|
239
|
+
const forwarded = new URL(res.headers.get('location'), url).href;
|
|
240
|
+
res.body?.cancel().catch(() => { });
|
|
241
|
+
if (hops >= 10 || !handleRedirects || !handleRedirects(url, forwarded))
|
|
242
|
+
throw new Error('link-preview could not handle redirect');
|
|
243
|
+
url = forwarded;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
const finalUrl = res.url || url;
|
|
249
|
+
const contentType = (res.headers.get('content-type') || '').toLowerCase();
|
|
250
|
+
const type = contentType.split(';')[0].trim();
|
|
251
|
+
const origin = (() => {
|
|
252
|
+
try {
|
|
253
|
+
return new URL(finalUrl).origin;
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return '';
|
|
257
|
+
}
|
|
258
|
+
})();
|
|
259
|
+
const defaultFavicons = origin ? [origin + '/favicon.ico'] : [];
|
|
260
|
+
if (type.startsWith('image/')) {
|
|
261
|
+
res.body?.cancel().catch(() => { });
|
|
262
|
+
return { url: finalUrl, mediaType: 'image', contentType: type, favicons: defaultFavicons };
|
|
263
|
+
}
|
|
264
|
+
if (type.startsWith('audio/') || type.startsWith('video/') || type.startsWith('application/') && !type.includes('html') && !type.includes('xml')) {
|
|
265
|
+
res.body?.cancel().catch(() => { });
|
|
266
|
+
return { url: finalUrl, mediaType: type.split('/')[0], contentType: type, favicons: defaultFavicons };
|
|
267
|
+
}
|
|
268
|
+
if (type && !type.includes('html') && !type.includes('xml') && !type.startsWith('text/'))
|
|
269
|
+
throw new Error(`link-preview: unsupported content type ${type}`);
|
|
270
|
+
const html = decodeBody(await readBody(res, maxBytes), contentType);
|
|
271
|
+
const doc = scanHtml(html);
|
|
272
|
+
const base = doc.base ? (resolveUrl(doc.base, finalUrl) ?? finalUrl) : finalUrl;
|
|
273
|
+
const meta = (...keys) => {
|
|
274
|
+
for (const k of keys) {
|
|
275
|
+
const hit = doc.metas.find(a => (a.property ?? a.name ?? '').toLowerCase() === k && a.content !== undefined && a.content.trim() !== '');
|
|
276
|
+
if (hit)
|
|
277
|
+
return hit.content.trim();
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
const metaAll = (...keys) => doc.metas.filter(a => keys.includes((a.property ?? a.name ?? '').toLowerCase()) && a.content?.trim()).map(a => a.content.trim());
|
|
281
|
+
const title = meta('og:title', 'twitter:title') ?? doc.title?.trim() ?? undefined;
|
|
282
|
+
const description = meta('og:description', 'twitter:description', 'description');
|
|
283
|
+
const siteName = meta('og:site_name');
|
|
284
|
+
const mediaType = meta('og:type') ?? 'website';
|
|
285
|
+
const images = [];
|
|
286
|
+
const pushUrl = (list, href) => {
|
|
287
|
+
if (!href || /^data:/i.test(href.trim()))
|
|
288
|
+
return;
|
|
289
|
+
const u = resolveUrl(href, base);
|
|
290
|
+
if (u && !list.includes(u))
|
|
291
|
+
list.push(u);
|
|
292
|
+
};
|
|
293
|
+
for (const v of metaAll('og:image', 'og:image:url', 'og:image:secure_url', 'twitter:image', 'twitter:image:src'))
|
|
294
|
+
pushUrl(images, v);
|
|
295
|
+
if (!images.length)
|
|
296
|
+
for (const img of doc.imgs.slice(0, 20))
|
|
297
|
+
pushUrl(images, img.src);
|
|
298
|
+
const videos = [];
|
|
299
|
+
for (const v of metaAll('og:video', 'og:video:url', 'og:video:secure_url'))
|
|
300
|
+
pushUrl(videos, v);
|
|
301
|
+
const favicons = [];
|
|
302
|
+
for (const l of doc.links)
|
|
303
|
+
if (/(^|\s)(shortcut\s+)?icon(\s|$)|apple-touch-icon/i.test(l.rel ?? '') && l.href)
|
|
304
|
+
pushUrl(favicons, l.href);
|
|
305
|
+
if (!favicons.length)
|
|
306
|
+
favicons.push(...defaultFavicons);
|
|
307
|
+
return { url: finalUrl, title, siteName, description, mediaType, contentType: type || 'text/html', images, videos, favicons };
|
|
308
|
+
}
|
|
309
|
+
//# sourceMappingURL=link-preview.js.map
|