@metascraper/helpers 5.54.1 → 5.55.2
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 +2 -2
- package/src/index.js +56 -3
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@metascraper/helpers",
|
|
3
3
|
"description": "Shared helper utilities used across metascraper rules for parsing and normalizing metadata.",
|
|
4
4
|
"homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.55.2",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -73,5 +73,5 @@
|
|
|
73
73
|
"publishConfig": {
|
|
74
74
|
"access": "public"
|
|
75
75
|
},
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "d55833693c1b1736bbc56e0d477b1135871837c6"
|
|
77
77
|
}
|
package/src/index.js
CHANGED
|
@@ -176,14 +176,66 @@ const protocol = url => {
|
|
|
176
176
|
return ''
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
|
|
180
|
-
|
|
179
|
+
/**
|
|
180
|
+
* `normalize-url` treats `+` as a space in query keys that have no `=`
|
|
181
|
+
* (CGI-style `?241+ful+HB1288+pdf`) and emits `%20`. Restore those pluses —
|
|
182
|
+
* they are delimiters, not encoded spaces.
|
|
183
|
+
*
|
|
184
|
+
* @param {string} original
|
|
185
|
+
* @param {string} normalized
|
|
186
|
+
* @returns {string}
|
|
187
|
+
*/
|
|
188
|
+
const restoreCgiPlus = (original, normalized) => {
|
|
189
|
+
const origMark = original.indexOf('?')
|
|
190
|
+
const normMark = normalized.indexOf('?')
|
|
191
|
+
if (origMark === -1 || normMark === -1) return normalized
|
|
192
|
+
|
|
193
|
+
const origHash = original.indexOf('#', origMark)
|
|
194
|
+
const origSearch = original.slice(
|
|
195
|
+
origMark + 1,
|
|
196
|
+
origHash === -1 ? undefined : origHash
|
|
197
|
+
)
|
|
198
|
+
if (!origSearch.includes('+')) return normalized
|
|
199
|
+
|
|
200
|
+
const origByEncoded = new Map()
|
|
201
|
+
for (const orig of origSearch.split('&')) {
|
|
202
|
+
if (!orig.includes('=') && orig.includes('+')) {
|
|
203
|
+
origByEncoded.set(orig.replaceAll('+', '%20'), orig)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (origByEncoded.size === 0) return normalized
|
|
207
|
+
|
|
208
|
+
const normHash = normalized.indexOf('#', normMark)
|
|
209
|
+
const normSearch = normalized.slice(
|
|
210
|
+
normMark + 1,
|
|
211
|
+
normHash === -1 ? undefined : normHash
|
|
212
|
+
)
|
|
213
|
+
let changed = false
|
|
214
|
+
const restored = normSearch.split('&').map(norm => {
|
|
215
|
+
const orig = origByEncoded.get(norm)
|
|
216
|
+
if (!orig) return norm
|
|
217
|
+
changed = true
|
|
218
|
+
return orig
|
|
219
|
+
})
|
|
220
|
+
if (!changed) return normalized
|
|
221
|
+
|
|
222
|
+
const hash = normHash === -1 ? '' : normalized.slice(normHash)
|
|
223
|
+
return `${normalized.slice(0, normMark + 1)}${restored.join('&')}${hash}`
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const sanetizeUrl = (url, opts) => {
|
|
227
|
+
const normalized = _normalizeUrl(url, {
|
|
181
228
|
stripWWW: false,
|
|
182
229
|
sortQueryParameters: false,
|
|
183
230
|
removeSingleSlash: false,
|
|
184
231
|
removeTrailingSlash: false,
|
|
185
232
|
...opts
|
|
186
233
|
})
|
|
234
|
+
const queryStart = url.indexOf('?')
|
|
235
|
+
return queryStart !== -1 && url.indexOf('+', queryStart) !== -1
|
|
236
|
+
? restoreCgiPlus(url, normalized)
|
|
237
|
+
: normalized
|
|
238
|
+
}
|
|
187
239
|
|
|
188
240
|
const normalizeUrl = (baseUrl, relativePath, opts) => {
|
|
189
241
|
try {
|
|
@@ -564,6 +616,7 @@ const findRule = async (rules, args = {}, propName) => {
|
|
|
564
616
|
let index = 0
|
|
565
617
|
let value
|
|
566
618
|
let hasValue = false
|
|
619
|
+
const validateArgs = args.validate ? { ...args, propName } : args
|
|
567
620
|
|
|
568
621
|
do {
|
|
569
622
|
const rule = rules[index++]
|
|
@@ -573,7 +626,7 @@ const findRule = async (rules, args = {}, propName) => {
|
|
|
573
626
|
value = await rule(args)
|
|
574
627
|
hasValue = has(value)
|
|
575
628
|
const isRejected =
|
|
576
|
-
hasValue && args.validate && !(await isValidValue(value,
|
|
629
|
+
hasValue && args.validate && !(await isValidValue(value, validateArgs))
|
|
577
630
|
if (isRejected) {
|
|
578
631
|
value = undefined
|
|
579
632
|
hasValue = false
|