util.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. 'use strict'
  2. const assert = require('assert')
  3. const { kDestroyed, kBodyUsed } = require('./symbols')
  4. const { IncomingMessage } = require('http')
  5. const stream = require('stream')
  6. const net = require('net')
  7. const { InvalidArgumentError } = require('./errors')
  8. const { Blob } = require('buffer')
  9. const nodeUtil = require('util')
  10. const { stringify } = require('querystring')
  11. const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(v => Number(v))
  12. function nop () {}
  13. function isStream (obj) {
  14. return obj && typeof obj === 'object' && typeof obj.pipe === 'function' && typeof obj.on === 'function'
  15. }
  16. // based on https://github.com/node-fetch/fetch-blob/blob/8ab587d34080de94140b54f07168451e7d0b655e/index.js#L229-L241 (MIT License)
  17. function isBlobLike (object) {
  18. return (Blob && object instanceof Blob) || (
  19. object &&
  20. typeof object === 'object' &&
  21. (typeof object.stream === 'function' ||
  22. typeof object.arrayBuffer === 'function') &&
  23. /^(Blob|File)$/.test(object[Symbol.toStringTag])
  24. )
  25. }
  26. function buildURL (url, queryParams) {
  27. if (url.includes('?') || url.includes('#')) {
  28. throw new Error('Query params cannot be passed when url already contains "?" or "#".')
  29. }
  30. const stringified = stringify(queryParams)
  31. if (stringified) {
  32. url += '?' + stringified
  33. }
  34. return url
  35. }
  36. function parseURL (url) {
  37. if (typeof url === 'string') {
  38. url = new URL(url)
  39. if (!/^https?:/.test(url.origin || url.protocol)) {
  40. throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.')
  41. }
  42. return url
  43. }
  44. if (!url || typeof url !== 'object') {
  45. throw new InvalidArgumentError('Invalid URL: The URL argument must be a non-null object.')
  46. }
  47. if (url.port != null && url.port !== '' && !Number.isFinite(parseInt(url.port))) {
  48. throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.')
  49. }
  50. if (url.path != null && typeof url.path !== 'string') {
  51. throw new InvalidArgumentError('Invalid URL path: the path must be a string or null/undefined.')
  52. }
  53. if (url.pathname != null && typeof url.pathname !== 'string') {
  54. throw new InvalidArgumentError('Invalid URL pathname: the pathname must be a string or null/undefined.')
  55. }
  56. if (url.hostname != null && typeof url.hostname !== 'string') {
  57. throw new InvalidArgumentError('Invalid URL hostname: the hostname must be a string or null/undefined.')
  58. }
  59. if (url.origin != null && typeof url.origin !== 'string') {
  60. throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.')
  61. }
  62. if (!/^https?:/.test(url.origin || url.protocol)) {
  63. throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.')
  64. }
  65. if (!(url instanceof URL)) {
  66. const port = url.port != null
  67. ? url.port
  68. : (url.protocol === 'https:' ? 443 : 80)
  69. let origin = url.origin != null
  70. ? url.origin
  71. : `${url.protocol}//${url.hostname}:${port}`
  72. let path = url.path != null
  73. ? url.path
  74. : `${url.pathname || ''}${url.search || ''}`
  75. if (origin.endsWith('/')) {
  76. origin = origin.substring(0, origin.length - 1)
  77. }
  78. if (path && !path.startsWith('/')) {
  79. path = `/${path}`
  80. }
  81. // new URL(path, origin) is unsafe when `path` contains an absolute URL
  82. // From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL:
  83. // If first parameter is a relative URL, second param is required, and will be used as the base URL.
  84. // If first parameter is an absolute URL, a given second param will be ignored.
  85. url = new URL(origin + path)
  86. }
  87. return url
  88. }
  89. function parseOrigin (url) {
  90. url = parseURL(url)
  91. if (url.pathname !== '/' || url.search || url.hash) {
  92. throw new InvalidArgumentError('invalid url')
  93. }
  94. return url
  95. }
  96. function getHostname (host) {
  97. if (host[0] === '[') {
  98. const idx = host.indexOf(']')
  99. assert(idx !== -1)
  100. return host.substr(1, idx - 1)
  101. }
  102. const idx = host.indexOf(':')
  103. if (idx === -1) return host
  104. return host.substr(0, idx)
  105. }
  106. // IP addresses are not valid server names per RFC6066
  107. // > Currently, the only server names supported are DNS hostnames
  108. function getServerName (host) {
  109. if (!host) {
  110. return null
  111. }
  112. assert.strictEqual(typeof host, 'string')
  113. const servername = getHostname(host)
  114. if (net.isIP(servername)) {
  115. return ''
  116. }
  117. return servername
  118. }
  119. function deepClone (obj) {
  120. return JSON.parse(JSON.stringify(obj))
  121. }
  122. function isAsyncIterable (obj) {
  123. return !!(obj != null && typeof obj[Symbol.asyncIterator] === 'function')
  124. }
  125. function isIterable (obj) {
  126. return !!(obj != null && (typeof obj[Symbol.iterator] === 'function' || typeof obj[Symbol.asyncIterator] === 'function'))
  127. }
  128. function bodyLength (body) {
  129. if (body == null) {
  130. return 0
  131. } else if (isStream(body)) {
  132. const state = body._readableState
  133. return state && state.ended === true && Number.isFinite(state.length)
  134. ? state.length
  135. : null
  136. } else if (isBlobLike(body)) {
  137. return body.size != null ? body.size : null
  138. } else if (isBuffer(body)) {
  139. return body.byteLength
  140. }
  141. return null
  142. }
  143. function isDestroyed (stream) {
  144. return !stream || !!(stream.destroyed || stream[kDestroyed])
  145. }
  146. function isReadableAborted (stream) {
  147. const state = stream && stream._readableState
  148. return isDestroyed(stream) && state && !state.endEmitted
  149. }
  150. function destroy (stream, err) {
  151. if (!isStream(stream) || isDestroyed(stream)) {
  152. return
  153. }
  154. if (typeof stream.destroy === 'function') {
  155. if (Object.getPrototypeOf(stream).constructor === IncomingMessage) {
  156. // See: https://github.com/nodejs/node/pull/38505/files
  157. stream.socket = null
  158. }
  159. stream.destroy(err)
  160. } else if (err) {
  161. process.nextTick((stream, err) => {
  162. stream.emit('error', err)
  163. }, stream, err)
  164. }
  165. if (stream.destroyed !== true) {
  166. stream[kDestroyed] = true
  167. }
  168. }
  169. const KEEPALIVE_TIMEOUT_EXPR = /timeout=(\d+)/
  170. function parseKeepAliveTimeout (val) {
  171. const m = val.toString().match(KEEPALIVE_TIMEOUT_EXPR)
  172. return m ? parseInt(m[1], 10) * 1000 : null
  173. }
  174. function parseHeaders (headers, obj = {}) {
  175. for (let i = 0; i < headers.length; i += 2) {
  176. const key = headers[i].toString().toLowerCase()
  177. let val = obj[key]
  178. if (!val) {
  179. if (Array.isArray(headers[i + 1])) {
  180. obj[key] = headers[i + 1]
  181. } else {
  182. obj[key] = headers[i + 1].toString('utf8')
  183. }
  184. } else {
  185. if (!Array.isArray(val)) {
  186. val = [val]
  187. obj[key] = val
  188. }
  189. val.push(headers[i + 1].toString('utf8'))
  190. }
  191. }
  192. // See https://github.com/nodejs/node/pull/46528
  193. if ('content-length' in obj && 'content-disposition' in obj) {
  194. obj['content-disposition'] = Buffer.from(obj['content-disposition']).toString('latin1')
  195. }
  196. return obj
  197. }
  198. function parseRawHeaders (headers) {
  199. const ret = []
  200. let hasContentLength = false
  201. let contentDispositionIdx = -1
  202. for (let n = 0; n < headers.length; n += 2) {
  203. const key = headers[n + 0].toString()
  204. const val = headers[n + 1].toString('utf8')
  205. if (key.length === 14 && (key === 'content-length' || key.toLowerCase() === 'content-length')) {
  206. ret.push(key, val)
  207. hasContentLength = true
  208. } else if (key.length === 19 && (key === 'content-disposition' || key.toLowerCase() === 'content-disposition')) {
  209. contentDispositionIdx = ret.push(key, val) - 1
  210. } else {
  211. ret.push(key, val)
  212. }
  213. }
  214. // See https://github.com/nodejs/node/pull/46528
  215. if (hasContentLength && contentDispositionIdx !== -1) {
  216. ret[contentDispositionIdx] = Buffer.from(ret[contentDispositionIdx]).toString('latin1')
  217. }
  218. return ret
  219. }
  220. function isBuffer (buffer) {
  221. // See, https://github.com/mcollina/undici/pull/319
  222. return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
  223. }
  224. function validateHandler (handler, method, upgrade) {
  225. if (!handler || typeof handler !== 'object') {
  226. throw new InvalidArgumentError('handler must be an object')
  227. }
  228. if (typeof handler.onConnect !== 'function') {
  229. throw new InvalidArgumentError('invalid onConnect method')
  230. }
  231. if (typeof handler.onError !== 'function') {
  232. throw new InvalidArgumentError('invalid onError method')
  233. }
  234. if (typeof handler.onBodySent !== 'function' && handler.onBodySent !== undefined) {
  235. throw new InvalidArgumentError('invalid onBodySent method')
  236. }
  237. if (upgrade || method === 'CONNECT') {
  238. if (typeof handler.onUpgrade !== 'function') {
  239. throw new InvalidArgumentError('invalid onUpgrade method')
  240. }
  241. } else {
  242. if (typeof handler.onHeaders !== 'function') {
  243. throw new InvalidArgumentError('invalid onHeaders method')
  244. }
  245. if (typeof handler.onData !== 'function') {
  246. throw new InvalidArgumentError('invalid onData method')
  247. }
  248. if (typeof handler.onComplete !== 'function') {
  249. throw new InvalidArgumentError('invalid onComplete method')
  250. }
  251. }
  252. }
  253. // A body is disturbed if it has been read from and it cannot
  254. // be re-used without losing state or data.
  255. function isDisturbed (body) {
  256. return !!(body && (
  257. stream.isDisturbed
  258. ? stream.isDisturbed(body) || body[kBodyUsed] // TODO (fix): Why is body[kBodyUsed] needed?
  259. : body[kBodyUsed] ||
  260. body.readableDidRead ||
  261. (body._readableState && body._readableState.dataEmitted) ||
  262. isReadableAborted(body)
  263. ))
  264. }
  265. function isErrored (body) {
  266. return !!(body && (
  267. stream.isErrored
  268. ? stream.isErrored(body)
  269. : /state: 'errored'/.test(nodeUtil.inspect(body)
  270. )))
  271. }
  272. function isReadable (body) {
  273. return !!(body && (
  274. stream.isReadable
  275. ? stream.isReadable(body)
  276. : /state: 'readable'/.test(nodeUtil.inspect(body)
  277. )))
  278. }
  279. function getSocketInfo (socket) {
  280. return {
  281. localAddress: socket.localAddress,
  282. localPort: socket.localPort,
  283. remoteAddress: socket.remoteAddress,
  284. remotePort: socket.remotePort,
  285. remoteFamily: socket.remoteFamily,
  286. timeout: socket.timeout,
  287. bytesWritten: socket.bytesWritten,
  288. bytesRead: socket.bytesRead
  289. }
  290. }
  291. let ReadableStream
  292. function ReadableStreamFrom (iterable) {
  293. if (!ReadableStream) {
  294. ReadableStream = require('stream/web').ReadableStream
  295. }
  296. if (ReadableStream.from) {
  297. // https://github.com/whatwg/streams/pull/1083
  298. return ReadableStream.from(iterable)
  299. }
  300. let iterator
  301. return new ReadableStream(
  302. {
  303. async start () {
  304. iterator = iterable[Symbol.asyncIterator]()
  305. },
  306. async pull (controller) {
  307. const { done, value } = await iterator.next()
  308. if (done) {
  309. queueMicrotask(() => {
  310. controller.close()
  311. })
  312. } else {
  313. const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
  314. controller.enqueue(new Uint8Array(buf))
  315. }
  316. return controller.desiredSize > 0
  317. },
  318. async cancel (reason) {
  319. await iterator.return()
  320. }
  321. },
  322. 0
  323. )
  324. }
  325. // The chunk should be a FormData instance and contains
  326. // all the required methods.
  327. function isFormDataLike (object) {
  328. return (
  329. object &&
  330. typeof object === 'object' &&
  331. typeof object.append === 'function' &&
  332. typeof object.delete === 'function' &&
  333. typeof object.get === 'function' &&
  334. typeof object.getAll === 'function' &&
  335. typeof object.has === 'function' &&
  336. typeof object.set === 'function' &&
  337. object[Symbol.toStringTag] === 'FormData'
  338. )
  339. }
  340. function throwIfAborted (signal) {
  341. if (!signal) { return }
  342. if (typeof signal.throwIfAborted === 'function') {
  343. signal.throwIfAborted()
  344. } else {
  345. if (signal.aborted) {
  346. // DOMException not available < v17.0.0
  347. const err = new Error('The operation was aborted')
  348. err.name = 'AbortError'
  349. throw err
  350. }
  351. }
  352. }
  353. let events
  354. function addAbortListener (signal, listener) {
  355. if (typeof Symbol.dispose === 'symbol') {
  356. if (!events) {
  357. events = require('events')
  358. }
  359. if (typeof events.addAbortListener === 'function' && 'aborted' in signal) {
  360. return events.addAbortListener(signal, listener)
  361. }
  362. }
  363. if ('addEventListener' in signal) {
  364. signal.addEventListener('abort', listener, { once: true })
  365. return () => signal.removeEventListener('abort', listener)
  366. }
  367. signal.addListener('abort', listener)
  368. return () => signal.removeListener('abort', listener)
  369. }
  370. const hasToWellFormed = !!String.prototype.toWellFormed
  371. /**
  372. * @param {string} val
  373. */
  374. function toUSVString (val) {
  375. if (hasToWellFormed) {
  376. return `${val}`.toWellFormed()
  377. } else if (nodeUtil.toUSVString) {
  378. return nodeUtil.toUSVString(val)
  379. }
  380. return `${val}`
  381. }
  382. const kEnumerableProperty = Object.create(null)
  383. kEnumerableProperty.enumerable = true
  384. module.exports = {
  385. kEnumerableProperty,
  386. nop,
  387. isDisturbed,
  388. isErrored,
  389. isReadable,
  390. toUSVString,
  391. isReadableAborted,
  392. isBlobLike,
  393. parseOrigin,
  394. parseURL,
  395. getServerName,
  396. isStream,
  397. isIterable,
  398. isAsyncIterable,
  399. isDestroyed,
  400. parseRawHeaders,
  401. parseHeaders,
  402. parseKeepAliveTimeout,
  403. destroy,
  404. bodyLength,
  405. deepClone,
  406. ReadableStreamFrom,
  407. isBuffer,
  408. validateHandler,
  409. getSocketInfo,
  410. isFormDataLike,
  411. buildURL,
  412. throwIfAborted,
  413. addAbortListener,
  414. nodeMajor,
  415. nodeMinor,
  416. nodeHasAutoSelectFamily: nodeMajor > 18 || (nodeMajor === 18 && nodeMinor >= 13)
  417. }