@itznomo/baileys 0.0.0-stage → 0.0.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/BUTTONS.md +153 -0
- package/Example/call-bot.mjs +38 -0
- package/Example/example.mjs +36 -0
- package/Example/full-bot-commands.mjs +319 -0
- package/Example/full-bot.mjs +77 -0
- package/LICENSE +21 -0
- package/README.md +543 -2
- package/WAProto/@dnuzi +1 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +113300 -0
- package/assets/banner.svg +17 -0
- package/assets/devider.svg +1 -0
- package/assets/features.svg +21 -0
- package/docs/CALLS.md +106 -0
- package/docs/FEATURES.md +1152 -0
- package/engine-requirements.js +19 -0
- package/index.d.ts +815 -0
- package/package.json +51 -4
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +627 -0
- package/src/constants.js +147 -0
- package/src/defaults.js +106 -0
- package/src/foundation/ai-rich.js +654 -0
- package/src/foundation/audio-duration.js +470 -0
- package/src/foundation/audio-samples.js +250 -0
- package/src/foundation/boom.js +39 -0
- package/src/foundation/builders.js +851 -0
- package/src/foundation/cache.js +134 -0
- package/src/foundation/concurrency.js +69 -0
- package/src/foundation/curve25519.js +189 -0
- package/src/foundation/image-thumb.js +65 -0
- package/src/foundation/link-preview.js +278 -0
- package/src/foundation/logger.js +78 -0
- package/src/foundation/long.js +130 -0
- package/src/foundation/performance.js +385 -0
- package/src/foundation/protobuf-minimal.js +587 -0
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +716 -0
- package/src/foundation/wa-crypto.js +47 -0
- package/src/foundation/zip.js +108 -0
- package/src/index.js +67 -0
- package/src/signal/libsignal.js +996 -0
- package/src/signal/signal-group.js +455 -0
- package/src/signal/signal-repository.js +635 -0
- package/src/socket/business-communities.js +811 -0
- package/src/socket/chats.js +702 -0
- package/src/socket/groups-newsletter.js +524 -0
- package/src/socket/messages-recv.js +1815 -0
- package/src/socket/messages-send.js +1526 -0
- package/src/socket/socket-builder.js +131 -0
- package/src/socket/socket-core.js +913 -0
- package/src/socket/usync.js +287 -0
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +170 -0
- package/src/utils/auth-state-core.js +731 -0
- package/src/utils/auth-state-storage.js +594 -0
- package/src/utils/chat-sync.js +1046 -0
- package/src/utils/check-status.js +159 -0
- package/src/utils/cstoken.js +17 -0
- package/src/utils/media.js +807 -0
- package/src/utils/message-compose.js +1747 -0
- package/src/utils/message-processing.js +912 -0
- package/src/utils/modern-messages.js +435 -0
- package/src/utils/nomo-buttons.js +138 -0
- package/src/utils/nomo-commands.js +168 -0
- package/src/utils/spam-report-utils.js +136 -0
- package/src/utils/use-keyv-auth-state.js +307 -0
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +1227 -0
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/auto.js +139 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1727 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +730 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +22852 -0
- package/src/wam/wam.js +146 -0
- package/voip.d.ts +144 -0
|
@@ -0,0 +1,278 @@
|
|
|
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
|
+
|
|
5
|
+
const NAMED = {
|
|
6
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: '\u00a0', copy: '©', reg: '®', trade: '™', hellip: '…', mdash: '—', ndash: '–',
|
|
7
|
+
lsquo: '‘', rsquo: '’', ldquo: '“', rdquo: '”', laquo: '«', raquo: '»', bull: '•', middot: '·', euro: '€', pound: '£', yen: '¥', cent: '¢',
|
|
8
|
+
sect: '§', deg: '°', plusmn: '±', times: '×', divide: '÷', para: '¶', iexcl: '¡', iquest: '¿', shy: '\u00ad', larr: '←', rarr: '→', uarr: '↑',
|
|
9
|
+
darr: '↓', hearts: '♥', check: '✓', star: '☆', infin: '∞', ne: '≠', le: '≤', ge: '≥', frac12: '½', frac14: '¼', frac34: '¾', sup2: '²', sup3: '³',
|
|
10
|
+
micro: 'µ', agrave: 'à', aacute: 'á', acirc: 'â', atilde: 'ã', auml: 'ä', aring: 'å', aelig: 'æ', ccedil: 'ç', egrave: 'è', eacute: 'é', ecirc: 'ê',
|
|
11
|
+
euml: 'ë', igrave: 'ì', iacute: 'í', icirc: 'î', iuml: 'ï', ntilde: 'ñ', ograve: 'ò', oacute: 'ó', ocirc: 'ô', otilde: 'õ', ouml: 'ö', oslash: 'ø',
|
|
12
|
+
ugrave: 'ù', uacute: 'ú', ucirc: 'û', uuml: 'ü', yacute: 'ý', yuml: 'ÿ', szlig: 'ß', Agrave: 'À', Aacute: 'Á', Acirc: 'Â', Atilde: 'Ã', Auml: 'Ä',
|
|
13
|
+
Aring: 'Å', AElig: 'Æ', Ccedil: 'Ç', Egrave: 'È', Eacute: 'É', Ecirc: 'Ê', Euml: 'Ë', Igrave: 'Ì', Iacute: 'Í', Icirc: 'Î', Iuml: 'Ï', Ntilde: 'Ñ',
|
|
14
|
+
Ograve: 'Ò', Oacute: 'Ó', Ocirc: 'Ô', Otilde: 'Õ', Ouml: 'Ö', Oslash: 'Ø', Ugrave: 'Ù', Uacute: 'Ú', Ucirc: 'Û', Uuml: 'Ü', Yacute: 'Ý'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
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)$/
|
|
18
|
+
|
|
19
|
+
const decodeEntities = (s, inAttr = false) => {
|
|
20
|
+
if (!s.includes('&')) return s
|
|
21
|
+
return s.replace(/&(?:#(\d+)|#[xX]([0-9a-fA-F]+)|([A-Za-z][A-Za-z0-9]*))(;?)/g, (m, dec, hex, name, semi, offset) => {
|
|
22
|
+
if (name) {
|
|
23
|
+
if (semi && Object.prototype.hasOwnProperty.call(NAMED, name)) return NAMED[name]
|
|
24
|
+
for (let len = name.length; len >= 2; len--) {
|
|
25
|
+
const prefix = name.slice(0, len)
|
|
26
|
+
if (!LEGACY.test(prefix)) 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)) return m
|
|
30
|
+
return NAMED[prefix] + rest
|
|
31
|
+
}
|
|
32
|
+
return m
|
|
33
|
+
}
|
|
34
|
+
const cp = dec !== undefined ? parseInt(dec, 10) : parseInt(hex, 16)
|
|
35
|
+
if (!cp || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) return '\ufffd'
|
|
36
|
+
return String.fromCodePoint(cp)
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const TEXT_ELEMENTS = new Set(['script', 'style', 'textarea', 'title', 'xmp', 'iframe', 'noembed', 'noframes', 'noscript'])
|
|
41
|
+
const WS = new Set([' ', '\t', '\n', '\f', '\r'])
|
|
42
|
+
const isAlpha = c => c !== undefined && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
|
|
43
|
+
|
|
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) break
|
|
51
|
+
i = lt + 1
|
|
52
|
+
const c = html[i]
|
|
53
|
+
if (c === '!') {
|
|
54
|
+
|
|
55
|
+
if (html.startsWith('--', i + 1)) {
|
|
56
|
+
if (html[i + 3] === '>') i += 4
|
|
57
|
+
else if (html.startsWith('->', i + 3)) i += 5
|
|
58
|
+
else {
|
|
59
|
+
const end = html.indexOf('-->', i + 3)
|
|
60
|
+
const alt = html.indexOf('--!>', i + 3)
|
|
61
|
+
const e = end === -1 ? alt : alt === -1 ? end : Math.min(end, alt)
|
|
62
|
+
if (e === -1) break
|
|
63
|
+
i = e + (html.startsWith('--!>', e) ? 4 : 3)
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
const gt = html.indexOf('>', i)
|
|
67
|
+
if (gt === -1) break
|
|
68
|
+
i = gt + 1
|
|
69
|
+
}
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
if (c === '?') {
|
|
73
|
+
const gt = html.indexOf('>', i)
|
|
74
|
+
if (gt === -1) break
|
|
75
|
+
i = gt + 1
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
if (c === '/') {
|
|
79
|
+
|
|
80
|
+
const d = html[i + 1]
|
|
81
|
+
if (d === '>') {
|
|
82
|
+
i += 2
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
const gt = html.indexOf('>', i)
|
|
86
|
+
if (gt === -1) break
|
|
87
|
+
i = gt + 1
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
if (!isAlpha(c)) continue
|
|
91
|
+
|
|
92
|
+
let j = i
|
|
93
|
+
while (j < n && !WS.has(html[j]) && html[j] !== '/' && html[j] !== '>') j++
|
|
94
|
+
let name = html.slice(i, j).toLowerCase()
|
|
95
|
+
if (name === 'image') name = 'img'
|
|
96
|
+
const attrs = {}
|
|
97
|
+
let closed = false
|
|
98
|
+
|
|
99
|
+
for (;;) {
|
|
100
|
+
while (j < n && (WS.has(html[j]) || html[j] === '/')) j++
|
|
101
|
+
if (j >= n) break
|
|
102
|
+
if (html[j] === '>') {
|
|
103
|
+
j++
|
|
104
|
+
closed = true
|
|
105
|
+
break
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let k = j + 1
|
|
109
|
+
while (k < n && !WS.has(html[k]) && html[k] !== '/' && html[k] !== '>' && html[k] !== '=') k++
|
|
110
|
+
const aname = html.slice(j, k).toLowerCase().replace(/\0/g, '\ufffd')
|
|
111
|
+
j = k
|
|
112
|
+
while (j < n && WS.has(html[j])) j++
|
|
113
|
+
let value = ''
|
|
114
|
+
if (html[j] === '=') {
|
|
115
|
+
j++
|
|
116
|
+
while (j < n && WS.has(html[j])) j++
|
|
117
|
+
const q = html[j]
|
|
118
|
+
if (q === '"' || q === "'") {
|
|
119
|
+
const e = html.indexOf(q, j + 1)
|
|
120
|
+
if (e === -1) {
|
|
121
|
+
j = n
|
|
122
|
+
break
|
|
123
|
+
}
|
|
124
|
+
value = html.slice(j + 1, e)
|
|
125
|
+
j = e + 1
|
|
126
|
+
} else {
|
|
127
|
+
let e = j
|
|
128
|
+
while (e < n && !WS.has(html[e]) && html[e] !== '>') e++
|
|
129
|
+
value = html.slice(j, e)
|
|
130
|
+
j = e
|
|
131
|
+
}
|
|
132
|
+
value = decodeEntities(value, true)
|
|
133
|
+
}
|
|
134
|
+
if (!(aname in attrs)) attrs[aname] = value
|
|
135
|
+
}
|
|
136
|
+
if (!closed) break
|
|
137
|
+
i = j
|
|
138
|
+
if (name === 'meta') out.metas.push(attrs)
|
|
139
|
+
else if (name === 'link') out.links.push(attrs)
|
|
140
|
+
else if (name === 'img') out.imgs.push(attrs)
|
|
141
|
+
else if (name === 'base') {
|
|
142
|
+
if (out.base === undefined && attrs.href) out.base = attrs.href
|
|
143
|
+
}
|
|
144
|
+
if (TEXT_ELEMENTS.has(name)) {
|
|
145
|
+
|
|
146
|
+
const re = new RegExp('</' + name + '(?=[\\s/>])', 'i')
|
|
147
|
+
const m = re.exec(html.slice(i))
|
|
148
|
+
const end = m ? i + m.index : n
|
|
149
|
+
if (name === 'title' && out.title === undefined) out.title = decodeEntities(html.slice(i, end))
|
|
150
|
+
i = end
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return out
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const resolveUrl = (href, base) => {
|
|
157
|
+
try {
|
|
158
|
+
return new URL(href.trim(), base).href
|
|
159
|
+
} catch {
|
|
160
|
+
return undefined
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const charsetFrom = (contentType, head) => {
|
|
165
|
+
const h = /charset\s*=\s*["']?([\w.:-]+)/i.exec(contentType || '')
|
|
166
|
+
if (h) return h[1]
|
|
167
|
+
const m = /<meta[^>]+charset\s*=\s*["']?([\w.:-]+)/i.exec(head) || /<meta[^>]+content\s*=\s*["'][^"']*charset=([\w.:-]+)/i.exec(head)
|
|
168
|
+
return m ? m[1] : 'utf-8'
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const decodeBody = (bytes, contentType) => {
|
|
172
|
+
const head = Buffer.from(bytes.subarray(0, 2048)).toString('latin1')
|
|
173
|
+
const charset = charsetFrom(contentType, head)
|
|
174
|
+
try {
|
|
175
|
+
return new TextDecoder(charset).decode(bytes)
|
|
176
|
+
} catch {
|
|
177
|
+
return new TextDecoder('utf-8').decode(bytes)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const readBody = async (res, max) => {
|
|
182
|
+
const reader = res.body?.getReader()
|
|
183
|
+
if (!reader) return new Uint8Array(await res.arrayBuffer()).subarray(0, max)
|
|
184
|
+
const chunks = []
|
|
185
|
+
let total = 0
|
|
186
|
+
let tail = ''
|
|
187
|
+
while (total < max) {
|
|
188
|
+
const { done, value } = await reader.read()
|
|
189
|
+
if (done) break
|
|
190
|
+
chunks.push(value)
|
|
191
|
+
total += value.length
|
|
192
|
+
tail = (tail + Buffer.from(value).toString('latin1')).slice(-16)
|
|
193
|
+
if (/<\/head\s*>/i.test(tail)) break
|
|
194
|
+
}
|
|
195
|
+
reader.cancel().catch(() => {})
|
|
196
|
+
const buf = new Uint8Array(Math.min(total, max))
|
|
197
|
+
let o = 0
|
|
198
|
+
for (const c of chunks) {
|
|
199
|
+
const take = Math.min(c.length, buf.length - o)
|
|
200
|
+
buf.set(c.subarray(0, take), o)
|
|
201
|
+
o += take
|
|
202
|
+
}
|
|
203
|
+
return buf
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export async function getLinkPreview(text, options = {}) {
|
|
207
|
+
if (typeof text !== 'string') throw new Error('link-preview did not receive a valid a url or text')
|
|
208
|
+
const detected = text.replace(/\n/g, ' ').split(' ').find(token => REGEX_VALID_URL.test(token))
|
|
209
|
+
if (!detected) throw new Error('link-preview did not receive a valid a url or text')
|
|
210
|
+
const { timeout = 3000, followRedirects = 'follow', handleRedirects, maxBytes = MAX_BYTES } = options
|
|
211
|
+
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 || {}) }
|
|
212
|
+
|
|
213
|
+
let url = detected
|
|
214
|
+
let res
|
|
215
|
+
for (let hops = 0; ; hops++) {
|
|
216
|
+
res = await fetch(url, { headers, redirect: followRedirects === 'manual' ? 'manual' : followRedirects, signal: AbortSignal.timeout(timeout) })
|
|
217
|
+
if (followRedirects === 'manual' && res.status >= 300 && res.status < 400 && res.headers.get('location')) {
|
|
218
|
+
const forwarded = new URL(res.headers.get('location'), url).href
|
|
219
|
+
res.body?.cancel().catch(() => {})
|
|
220
|
+
if (hops >= 10 || !handleRedirects || !handleRedirects(url, forwarded)) throw new Error('link-preview could not handle redirect')
|
|
221
|
+
url = forwarded
|
|
222
|
+
continue
|
|
223
|
+
}
|
|
224
|
+
break
|
|
225
|
+
}
|
|
226
|
+
const finalUrl = res.url || url
|
|
227
|
+
const contentType = (res.headers.get('content-type') || '').toLowerCase()
|
|
228
|
+
const type = contentType.split(';')[0].trim()
|
|
229
|
+
const origin = (() => {
|
|
230
|
+
try {
|
|
231
|
+
return new URL(finalUrl).origin
|
|
232
|
+
} catch {
|
|
233
|
+
return ''
|
|
234
|
+
}
|
|
235
|
+
})()
|
|
236
|
+
const defaultFavicons = origin ? [origin + '/favicon.ico'] : []
|
|
237
|
+
|
|
238
|
+
if (type.startsWith('image/')) {
|
|
239
|
+
res.body?.cancel().catch(() => {})
|
|
240
|
+
return { url: finalUrl, mediaType: 'image', contentType: type, favicons: defaultFavicons }
|
|
241
|
+
}
|
|
242
|
+
if (type.startsWith('audio/') || type.startsWith('video/') || type.startsWith('application/') && !type.includes('html') && !type.includes('xml')) {
|
|
243
|
+
res.body?.cancel().catch(() => {})
|
|
244
|
+
return { url: finalUrl, mediaType: type.split('/')[0], contentType: type, favicons: defaultFavicons }
|
|
245
|
+
}
|
|
246
|
+
if (type && !type.includes('html') && !type.includes('xml') && !type.startsWith('text/')) throw new Error(`link-preview: unsupported content type ${type}`)
|
|
247
|
+
|
|
248
|
+
const html = decodeBody(await readBody(res, maxBytes), contentType)
|
|
249
|
+
const doc = scanHtml(html)
|
|
250
|
+
const base = doc.base ? (resolveUrl(doc.base, finalUrl) ?? finalUrl) : finalUrl
|
|
251
|
+
const meta = (...keys) => {
|
|
252
|
+
for (const k of keys) {
|
|
253
|
+
const hit = doc.metas.find(a => (a.property ?? a.name ?? '').toLowerCase() === k && a.content !== undefined && a.content.trim() !== '')
|
|
254
|
+
if (hit) return hit.content.trim()
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const metaAll = (...keys) => doc.metas.filter(a => keys.includes((a.property ?? a.name ?? '').toLowerCase()) && a.content?.trim()).map(a => a.content.trim())
|
|
258
|
+
const title = meta('og:title', 'twitter:title') ?? doc.title?.trim() ?? undefined
|
|
259
|
+
const description = meta('og:description', 'twitter:description', 'description')
|
|
260
|
+
const siteName = meta('og:site_name')
|
|
261
|
+
const mediaType = meta('og:type') ?? 'website'
|
|
262
|
+
|
|
263
|
+
const images = []
|
|
264
|
+
const pushUrl = (list, href) => {
|
|
265
|
+
if (!href || /^data:/i.test(href.trim())) return
|
|
266
|
+
const u = resolveUrl(href, base)
|
|
267
|
+
if (u && !list.includes(u)) list.push(u)
|
|
268
|
+
}
|
|
269
|
+
for (const v of metaAll('og:image', 'og:image:url', 'og:image:secure_url', 'twitter:image', 'twitter:image:src')) pushUrl(images, v)
|
|
270
|
+
if (!images.length) for (const img of doc.imgs.slice(0, 20)) pushUrl(images, img.src)
|
|
271
|
+
const videos = []
|
|
272
|
+
for (const v of metaAll('og:video', 'og:video:url', 'og:video:secure_url')) pushUrl(videos, v)
|
|
273
|
+
const favicons = []
|
|
274
|
+
for (const l of doc.links) if (/(^|\s)(shortcut\s+)?icon(\s|$)|apple-touch-icon/i.test(l.rel ?? '') && l.href) pushUrl(favicons, l.href)
|
|
275
|
+
if (!favicons.length) favicons.push(...defaultFavicons)
|
|
276
|
+
|
|
277
|
+
return { url: finalUrl, title, siteName, description, mediaType, contentType: type || 'text/html', images, videos, favicons }
|
|
278
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const LEVELS = { trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60 }
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns true when the logger would actually emit at the given numeric level value.
|
|
5
|
+
* Use this before constructing expensive log arguments.
|
|
6
|
+
* @param {object} state - internal logger state reference
|
|
7
|
+
* @param {number} targetLevel - numeric level value from LEVELS
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
function _isEnabled(state, targetLevel) {
|
|
11
|
+
return targetLevel >= (LEVELS[state.level] ?? LEVELS.info)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function serializeError(err) {
|
|
15
|
+
if (!(err instanceof Error)) return err
|
|
16
|
+
return { type: err.name, message: err.message, stack: err.stack, ...err }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createLogger(bindings = {}, level = 'info') {
|
|
20
|
+
const state = { level }
|
|
21
|
+
|
|
22
|
+
function write(levelName, args) {
|
|
23
|
+
if (LEVELS[levelName] < LEVELS[state.level]) return
|
|
24
|
+
|
|
25
|
+
let obj = {}
|
|
26
|
+
let msg = ''
|
|
27
|
+
if (args.length && typeof args[0] === 'object' && args[0] !== null) {
|
|
28
|
+
obj = args[0]
|
|
29
|
+
msg = args[1] ?? ''
|
|
30
|
+
} else {
|
|
31
|
+
msg = args[0] ?? ''
|
|
32
|
+
}
|
|
33
|
+
if (obj.err) obj = { ...obj, err: serializeError(obj.err) }
|
|
34
|
+
|
|
35
|
+
const line = {
|
|
36
|
+
level: LEVELS[levelName],
|
|
37
|
+
time: Date.now(),
|
|
38
|
+
...bindings,
|
|
39
|
+
...obj,
|
|
40
|
+
msg
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const out = levelName === 'error' || levelName === 'fatal' ? process.stderr : process.stdout
|
|
44
|
+
out.write(JSON.stringify(line) + '\n')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const logger = {
|
|
48
|
+
get level() {
|
|
49
|
+
return state.level
|
|
50
|
+
},
|
|
51
|
+
set level(v) {
|
|
52
|
+
state.level = v
|
|
53
|
+
},
|
|
54
|
+
/**
|
|
55
|
+
* Returns true when the logger would emit at the given level.
|
|
56
|
+
* Use this guard before constructing expensive log objects.
|
|
57
|
+
* @param {'trace'|'debug'|'info'|'warn'|'error'|'fatal'} levelName
|
|
58
|
+
* @returns {boolean}
|
|
59
|
+
*/
|
|
60
|
+
isLevelEnabled(levelName) {
|
|
61
|
+
return _isEnabled(state, LEVELS[levelName] ?? LEVELS.info)
|
|
62
|
+
},
|
|
63
|
+
trace: (...a) => { if (_isEnabled(state, 10)) write('trace', a) },
|
|
64
|
+
debug: (...a) => { if (_isEnabled(state, 20)) write('debug', a) },
|
|
65
|
+
info: (...a) => { if (_isEnabled(state, 30)) write('info', a) },
|
|
66
|
+
warn: (...a) => { if (_isEnabled(state, 40)) write('warn', a) },
|
|
67
|
+
error: (...a) => { if (_isEnabled(state, 50)) write('error', a) },
|
|
68
|
+
fatal: (...a) => { if (_isEnabled(state, 60)) write('fatal', a) },
|
|
69
|
+
child(childBindings = {}) {
|
|
70
|
+
return createLogger({ ...bindings, ...childBindings }, state.level)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return logger
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default createLogger()
|
|
78
|
+
export { createLogger }
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
const TWO_64 = 1n << 64n
|
|
2
|
+
const MASK64 = TWO_64 - 1n
|
|
3
|
+
|
|
4
|
+
const isLong = obj => (obj && obj.__isLong__) === true
|
|
5
|
+
|
|
6
|
+
export class Long {
|
|
7
|
+
constructor(low, high, unsigned) {
|
|
8
|
+
this.low = low | 0
|
|
9
|
+
this.high = high | 0
|
|
10
|
+
this.unsigned = !!unsigned
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_bits() {
|
|
14
|
+
return (BigInt(this.high >>> 0) << 32n) | BigInt(this.low >>> 0)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
toBigInt() {
|
|
18
|
+
const b = this._bits()
|
|
19
|
+
return this.unsigned ? b : BigInt.asIntN(64, b)
|
|
20
|
+
}
|
|
21
|
+
toNumber() {
|
|
22
|
+
if (this.unsigned) return (this.high >>> 0) * 4294967296 + (this.low >>> 0)
|
|
23
|
+
return this.high * 4294967296 + (this.low >>> 0)
|
|
24
|
+
}
|
|
25
|
+
toInt() {
|
|
26
|
+
return this.unsigned ? this.low >>> 0 : this.low
|
|
27
|
+
}
|
|
28
|
+
toString(radix) {
|
|
29
|
+
return this.toBigInt().toString(radix || 10)
|
|
30
|
+
}
|
|
31
|
+
isZero() {
|
|
32
|
+
return this.high === 0 && this.low === 0
|
|
33
|
+
}
|
|
34
|
+
isNegative() {
|
|
35
|
+
return !this.unsigned && this.high < 0
|
|
36
|
+
}
|
|
37
|
+
equals(other) {
|
|
38
|
+
if (!isLong(other)) other = Long.fromValue(other)
|
|
39
|
+
if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1) return false
|
|
40
|
+
return this.high === other.high && this.low === other.low
|
|
41
|
+
}
|
|
42
|
+
eq(other) {
|
|
43
|
+
return this.equals(other)
|
|
44
|
+
}
|
|
45
|
+
getHighBits() {
|
|
46
|
+
return this.high
|
|
47
|
+
}
|
|
48
|
+
getHighBitsUnsigned() {
|
|
49
|
+
return this.high >>> 0
|
|
50
|
+
}
|
|
51
|
+
getLowBits() {
|
|
52
|
+
return this.low
|
|
53
|
+
}
|
|
54
|
+
getLowBitsUnsigned() {
|
|
55
|
+
return this.low >>> 0
|
|
56
|
+
}
|
|
57
|
+
toSigned() {
|
|
58
|
+
return this.unsigned ? new Long(this.low, this.high, false) : this
|
|
59
|
+
}
|
|
60
|
+
toUnsigned() {
|
|
61
|
+
return this.unsigned ? this : new Long(this.low, this.high, true)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static fromBits(low, high, unsigned) {
|
|
65
|
+
return new Long(low, high, unsigned)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static fromBigInt(value, unsigned) {
|
|
69
|
+
const b = BigInt.asUintN(64, value)
|
|
70
|
+
return new Long(Number(BigInt.asIntN(32, b)), Number(BigInt.asIntN(32, b >> 32n)), unsigned)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static fromNumber(value, unsigned) {
|
|
74
|
+
if (Number.isNaN(value)) return new Long(0, 0, unsigned)
|
|
75
|
+
if (unsigned) {
|
|
76
|
+
if (value < 0) return new Long(0, 0, true)
|
|
77
|
+
if (value >= 18446744073709551616) return new Long(-1, -1, true)
|
|
78
|
+
} else {
|
|
79
|
+
if (value <= -9223372036854775808) return new Long(0, -2147483648, false)
|
|
80
|
+
if (value + 1 >= 9223372036854775808) return new Long(-1, 2147483647, false)
|
|
81
|
+
}
|
|
82
|
+
return Long.fromBigInt(BigInt(Math.trunc(value)), unsigned)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static fromString(str, unsigned, radix) {
|
|
86
|
+
if (str.length === 0) throw Error('empty string')
|
|
87
|
+
if (typeof unsigned === 'number') {
|
|
88
|
+
radix = unsigned
|
|
89
|
+
unsigned = false
|
|
90
|
+
} else {
|
|
91
|
+
unsigned = !!unsigned
|
|
92
|
+
}
|
|
93
|
+
if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity') return new Long(0, 0, unsigned)
|
|
94
|
+
radix = radix || 10
|
|
95
|
+
if (radix < 2 || radix > 36) throw RangeError('radix')
|
|
96
|
+
const p = str.indexOf('-')
|
|
97
|
+
if (p > 0) throw Error('interior hyphen')
|
|
98
|
+
const neg = p === 0
|
|
99
|
+
const digits = neg ? str.substring(1) : str
|
|
100
|
+
|
|
101
|
+
let result = 0n
|
|
102
|
+
const R = BigInt(radix)
|
|
103
|
+
for (let i = 0; i < digits.length; i += 8) {
|
|
104
|
+
const size = Math.min(8, digits.length - i)
|
|
105
|
+
const value = parseInt(digits.substring(i, i + size), radix)
|
|
106
|
+
result = result * R ** BigInt(size) + BigInt(Number.isNaN(value) ? 0 : value)
|
|
107
|
+
}
|
|
108
|
+
if (neg) result = -result
|
|
109
|
+
return Long.fromBigInt(result, unsigned)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static fromValue(val, unsigned) {
|
|
113
|
+
if (typeof val === 'number') return Long.fromNumber(val, unsigned)
|
|
114
|
+
if (typeof val === 'string') return Long.fromString(val, unsigned)
|
|
115
|
+
if (typeof val === 'bigint') return Long.fromBigInt(val, unsigned)
|
|
116
|
+
|
|
117
|
+
return new Long(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static isLong(obj) {
|
|
121
|
+
return isLong(obj)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
Object.defineProperty(Long.prototype, '__isLong__', { value: true })
|
|
126
|
+
|
|
127
|
+
export const ZERO = new Long(0, 0, false)
|
|
128
|
+
export const UZERO = new Long(0, 0, true)
|
|
129
|
+
export { MASK64 }
|
|
130
|
+
export default Long
|