@metascraper/helpers 5.52.0 → 5.53.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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +34 -6
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.52.0",
5
+ "version": "5.53.0",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -65,5 +65,5 @@
65
65
  "publishConfig": {
66
66
  "access": "public"
67
67
  },
68
- "gitHead": "15039a21cdccb414f96ed9ef2687fecaac882597"
68
+ "gitHead": "440ebd15db49ef9cf793869bceba5121bca49ad1"
69
69
  }
package/src/index.js CHANGED
@@ -123,6 +123,8 @@ const REGEX_LOCATION = /^[A-Z\s]+\s+[-—–]\s+/
123
123
 
124
124
  const REGEX_TITLE_SEPARATOR = /^[^|\-/•—]+/
125
125
 
126
+ const REGEX_URL_TAB_OR_NEWLINE = /[\t\n\r]/g
127
+
126
128
  const AUTHOR_MAX_LENGTH = 128
127
129
 
128
130
  const removeLocation = value => replace(value, REGEX_LOCATION, '')
@@ -143,6 +145,37 @@ const absoluteUrl = (baseUrl, relativePath) => {
143
145
  return urlObject(relativePath, baseUrl).toString()
144
146
  }
145
147
 
148
+ // https://url.spec.whatwg.org/#scheme-state
149
+ const protocol = url => {
150
+ if (!isString(url)) return ''
151
+ const { length } = url
152
+ let start = 0
153
+ while (start < length && url.charCodeAt(start) <= 0x20) start++
154
+ const head = url.charCodeAt(start)
155
+ let isLowerCased = head >= 0x61 && head <= 0x7a
156
+ if (!isLowerCased && !(head >= 0x41 && head <= 0x5a)) return ''
157
+ for (let index = start + 1; index < length; index++) {
158
+ const code = url.charCodeAt(index)
159
+ if (code === 0x3a) {
160
+ const scheme = url.slice(start, index)
161
+ return isLowerCased ? scheme : scheme.toLowerCase()
162
+ }
163
+ if (code >= 0x61 && code <= 0x7a) continue
164
+ if (code >= 0x41 && code <= 0x5a) {
165
+ isLowerCased = false
166
+ continue
167
+ }
168
+ if (code >= 0x30 && code <= 0x39) continue
169
+ if (code === 0x2b || code === 0x2d || code === 0x2e) continue
170
+ // `mai\tlto:a@b` declares `mailto`
171
+ if (code === 0x09 || code === 0x0a || code === 0x0d) {
172
+ return protocol(url.replace(REGEX_URL_TAB_OR_NEWLINE, ''))
173
+ }
174
+ return ''
175
+ }
176
+ return ''
177
+ }
178
+
146
179
  const sanetizeUrl = (url, opts) =>
147
180
  _normalizeUrl(url, {
148
181
  stripWWW: false,
@@ -156,7 +189,7 @@ const normalizeUrl = (baseUrl, relativePath, opts) => {
156
189
  try {
157
190
  const absolute = absoluteUrl(baseUrl, relativePath)
158
191
  // normalize-url v9 no longer rejects `javascript:` URLs; keep them out
159
- if (urlObject(absolute).protocol === 'javascript:') return undefined
192
+ if (protocol(absolute) === 'javascript') return undefined
160
193
  return sanetizeUrl(absolute, opts)
161
194
  } catch (_) {}
162
195
  }
@@ -205,11 +238,6 @@ const isAuthor = (str, opts = { relative: false }) =>
205
238
  const getAuthor = (str, { removeBy = true, ...opts } = {}) =>
206
239
  titleize(str, { removeBy, ...opts })
207
240
 
208
- const protocol = url => {
209
- const { protocol = '' } = urlObject(url)
210
- return protocol.replace(':', '')
211
- }
212
-
213
241
  const isExtension = (url, type, ext = extension(url)) =>
214
242
  type === EXTENSIONS[ext]
215
243