@metascraper/helpers 5.49.24 → 5.50.0
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/package.json +3 -3
- package/src/index.js +120 -6
- package/src/load-iframe/index.js +30 -5
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@metascraper/helpers",
|
|
3
3
|
"description": "Collection of helper functions used by metascraper",
|
|
4
4
|
"homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.50.0",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"cheerio": "latest"
|
|
54
54
|
},
|
|
55
55
|
"engines": {
|
|
56
|
-
"node": ">=
|
|
56
|
+
"node": ">= 22"
|
|
57
57
|
},
|
|
58
58
|
"files": [
|
|
59
59
|
"src"
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
"publishConfig": {
|
|
66
66
|
"access": "public"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "bf71c86e241b0cafa64dc697e3d6d2c065422a0b"
|
|
69
69
|
}
|
package/src/index.js
CHANGED
|
@@ -349,8 +349,10 @@ const title = (value, { removeSeparator = false, ...opts } = {}) =>
|
|
|
349
349
|
const isMime = (contentType, type) =>
|
|
350
350
|
type === get(EXTENSIONS, mimeExtension(contentType))
|
|
351
351
|
|
|
352
|
+
const isSameHtmlDom = (newHtmlDom, oldHtmlDom) => newHtmlDom === oldHtmlDom
|
|
353
|
+
|
|
352
354
|
memoizeOne.EqualityUrlAndHtmlDom = (newArgs, oldArgs) =>
|
|
353
|
-
newArgs[0] === oldArgs[0] && newArgs[1]
|
|
355
|
+
newArgs[0] === oldArgs[0] && isSameHtmlDom(newArgs[1], oldArgs[1])
|
|
354
356
|
|
|
355
357
|
memoizeOne.EqualityFirstArgument = (newArgs, oldArgs) =>
|
|
356
358
|
newArgs[0] === oldArgs[0]
|
|
@@ -384,16 +386,88 @@ const jsonld = memoizeOne(
|
|
|
384
386
|
})
|
|
385
387
|
.get()
|
|
386
388
|
.filter(Boolean),
|
|
387
|
-
(newArgs, oldArgs) => newArgs[0]
|
|
389
|
+
(newArgs, oldArgs) => isSameHtmlDom(newArgs[0], oldArgs[0])
|
|
388
390
|
)
|
|
389
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Recursive schema.org search with array traversal and object-to-name resolution.
|
|
394
|
+
* Returns array of primitive values (strings, numbers, booleans) for the given path.
|
|
395
|
+
*/
|
|
396
|
+
function searchSchemaResults (data, props, isExact) {
|
|
397
|
+
if (data === null || data === undefined) return []
|
|
398
|
+
if (
|
|
399
|
+
typeof data === 'string' ||
|
|
400
|
+
typeof data === 'number' ||
|
|
401
|
+
typeof data === 'boolean'
|
|
402
|
+
) {
|
|
403
|
+
return props.length === 0 ? [data] : []
|
|
404
|
+
}
|
|
405
|
+
if (Array.isArray(data)) {
|
|
406
|
+
const current = props[0]
|
|
407
|
+
if (current && /^\[\d+\]$/.test(current)) {
|
|
408
|
+
const index = parseInt(current.slice(1, -1), 10)
|
|
409
|
+
if (data[index] !== undefined) {
|
|
410
|
+
return searchSchemaResults(data[index], props.slice(1), isExact)
|
|
411
|
+
}
|
|
412
|
+
return []
|
|
413
|
+
}
|
|
414
|
+
if (
|
|
415
|
+
props.length === 0 &&
|
|
416
|
+
data.every(item => typeof item === 'string' || typeof item === 'number')
|
|
417
|
+
) {
|
|
418
|
+
return data.map(String)
|
|
419
|
+
}
|
|
420
|
+
return data.flatMap(item => searchSchemaResults(item, props, isExact))
|
|
421
|
+
}
|
|
422
|
+
if (typeof data === 'object') {
|
|
423
|
+
const [currentProp, ...rest] = props
|
|
424
|
+
if (!currentProp) {
|
|
425
|
+
if (data.name != null && typeof data.name === 'string') return [data.name]
|
|
426
|
+
return []
|
|
427
|
+
}
|
|
428
|
+
if (Object.prototype.hasOwnProperty.call(data, currentProp)) {
|
|
429
|
+
const result = searchSchemaResults(data[currentProp], rest, isExact)
|
|
430
|
+
if (result.length > 0) return result
|
|
431
|
+
}
|
|
432
|
+
if (!isExact) {
|
|
433
|
+
const nested = []
|
|
434
|
+
for (const key of Object.keys(data)) {
|
|
435
|
+
if (key.startsWith('@')) continue
|
|
436
|
+
nested.push(...searchSchemaResults(data[key], props, false))
|
|
437
|
+
}
|
|
438
|
+
return nested
|
|
439
|
+
}
|
|
440
|
+
return []
|
|
441
|
+
}
|
|
442
|
+
return []
|
|
443
|
+
}
|
|
444
|
+
|
|
390
445
|
const $jsonld = propName => $ => {
|
|
391
446
|
const collection = jsonld($)
|
|
447
|
+
const props = propName.split('.')
|
|
392
448
|
let value
|
|
393
|
-
|
|
449
|
+
let fallback
|
|
450
|
+
|
|
451
|
+
for (const item of collection) {
|
|
394
452
|
value = get(item, propName)
|
|
395
|
-
|
|
396
|
-
|
|
453
|
+
if (!isEmpty(value) || isNumber(value) || isBoolean(value)) break
|
|
454
|
+
if (value != null) continue
|
|
455
|
+
if (fallback !== undefined) continue
|
|
456
|
+
const exactResults = searchSchemaResults(item, props, true)
|
|
457
|
+
if (exactResults.length > 0) {
|
|
458
|
+
const filtered = exactResults.filter(v => v != null)
|
|
459
|
+
fallback = filtered.length > 1 ? filtered.join(' and ') : filtered[0]
|
|
460
|
+
} else {
|
|
461
|
+
const fuzzyResults = searchSchemaResults(item, props, false)
|
|
462
|
+
if (fuzzyResults.length > 0) {
|
|
463
|
+
fallback = fuzzyResults.find(v => v != null)
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (value == null && !isNumber(value) && !isBoolean(value)) {
|
|
469
|
+
value = fallback
|
|
470
|
+
}
|
|
397
471
|
|
|
398
472
|
return isString(value) ? decodeHTML(value) : value
|
|
399
473
|
}
|
|
@@ -481,6 +555,43 @@ const has = value =>
|
|
|
481
555
|
|
|
482
556
|
const getUrls = input => String(input).match(urlRegexForMatch) ?? []
|
|
483
557
|
|
|
558
|
+
const htmlCache = new WeakMap()
|
|
559
|
+
|
|
560
|
+
const getHtml = htmlDom => {
|
|
561
|
+
if (!htmlCache.has(htmlDom)) {
|
|
562
|
+
htmlCache.set(htmlDom, htmlDom.html())
|
|
563
|
+
}
|
|
564
|
+
return htmlCache.get(htmlDom)
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const loadIframe = require('./load-iframe')
|
|
568
|
+
|
|
569
|
+
const defaultGetIframe = (url, $, { src }) =>
|
|
570
|
+
loadIframe(url, $.load(`<iframe src="${src}"></iframe>`))
|
|
571
|
+
|
|
572
|
+
const createGetIframeCached = getIframe => {
|
|
573
|
+
const cacheByHtmlDom = new WeakMap()
|
|
574
|
+
|
|
575
|
+
return async (url, $, src) => {
|
|
576
|
+
let cacheBySrc = cacheByHtmlDom.get($)
|
|
577
|
+
if (!cacheBySrc) {
|
|
578
|
+
cacheBySrc = new Map()
|
|
579
|
+
cacheByHtmlDom.set($, cacheBySrc)
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
const cachedHtmlDom = cacheBySrc.get(src)
|
|
583
|
+
if (cachedHtmlDom) return cachedHtmlDom
|
|
584
|
+
|
|
585
|
+
const pendingHtmlDom = getIframe(url, $, { src }).catch(error => {
|
|
586
|
+
cacheBySrc.delete(src)
|
|
587
|
+
throw error
|
|
588
|
+
})
|
|
589
|
+
|
|
590
|
+
cacheBySrc.set(src, pendingHtmlDom)
|
|
591
|
+
return pendingHtmlDom
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
484
595
|
module.exports = {
|
|
485
596
|
$filter,
|
|
486
597
|
$jsonld,
|
|
@@ -489,11 +600,14 @@ module.exports = {
|
|
|
489
600
|
audioExtensions,
|
|
490
601
|
author,
|
|
491
602
|
composeRule,
|
|
603
|
+
createGetIframeCached,
|
|
492
604
|
date,
|
|
605
|
+
defaultGetIframe,
|
|
493
606
|
description,
|
|
494
607
|
extension,
|
|
495
608
|
fileExtension,
|
|
496
609
|
findRule,
|
|
610
|
+
getHtml,
|
|
497
611
|
getUrls,
|
|
498
612
|
has,
|
|
499
613
|
image,
|
|
@@ -515,7 +629,7 @@ module.exports = {
|
|
|
515
629
|
iso6393,
|
|
516
630
|
jsonld,
|
|
517
631
|
lang,
|
|
518
|
-
loadIframe
|
|
632
|
+
loadIframe,
|
|
519
633
|
logo,
|
|
520
634
|
memoizeOne,
|
|
521
635
|
mimeExtension,
|
package/src/load-iframe/index.js
CHANGED
|
@@ -10,12 +10,37 @@ const SCRIPT_PATH = (
|
|
|
10
10
|
|
|
11
11
|
module.exports = (url, $, { timeout = 5000 } = {}) => {
|
|
12
12
|
const worker = new Worker(SCRIPT_PATH(), {
|
|
13
|
-
workerData: { url, html: $.html(), timeout }
|
|
14
|
-
stdout: true,
|
|
15
|
-
stderr: true
|
|
13
|
+
workerData: { url, html: $.html(), timeout }
|
|
16
14
|
})
|
|
17
15
|
const { promise, resolve, reject } = Promise.withResolvers()
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
let settled = false
|
|
17
|
+
|
|
18
|
+
const onMessage = html => {
|
|
19
|
+
if (settled) return
|
|
20
|
+
settled = true
|
|
21
|
+
resolve($.load(html || ''))
|
|
22
|
+
worker.terminate().catch(() => {})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const onError = error => {
|
|
26
|
+
if (settled) return
|
|
27
|
+
settled = true
|
|
28
|
+
reject(error)
|
|
29
|
+
worker.terminate().catch(() => {})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const onExit = code => {
|
|
33
|
+
if (settled) return
|
|
34
|
+
settled = true
|
|
35
|
+
if (code === 0) {
|
|
36
|
+
resolve($.load(''))
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
reject(new Error(`loadIframe worker exited unexpectedly with code ${code}`))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
worker.once('message', onMessage)
|
|
43
|
+
worker.once('error', onError)
|
|
44
|
+
worker.once('exit', onExit)
|
|
20
45
|
return promise
|
|
21
46
|
}
|