deduplicate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. const diagnosticsChannel = require('node:diagnostics_channel')
  3. const util = require('../core/util')
  4. const DeduplicationHandler = require('../handler/deduplication-handler')
  5. const { normalizeHeaders, makeCacheKey, makeDeduplicationKey } = require('../util/cache.js')
  6. const pendingRequestsChannel = diagnosticsChannel.channel('undici:request:pending-requests')
  7. /**
  8. * @param {import('../../types/interceptors.d.ts').default.DeduplicateInterceptorOpts} [opts]
  9. * @returns {import('../../types/dispatcher.d.ts').default.DispatcherComposeInterceptor}
  10. */
  11. module.exports = (opts = {}) => {
  12. const {
  13. methods = ['GET'],
  14. skipHeaderNames = [],
  15. excludeHeaderNames = []
  16. } = opts
  17. if (typeof opts !== 'object' || opts === null) {
  18. throw new TypeError(`expected type of opts to be an Object, got ${opts === null ? 'null' : typeof opts}`)
  19. }
  20. if (!Array.isArray(methods)) {
  21. throw new TypeError(`expected opts.methods to be an array, got ${typeof methods}`)
  22. }
  23. for (const method of methods) {
  24. if (!util.safeHTTPMethods.includes(method)) {
  25. throw new TypeError(`expected opts.methods to only contain safe HTTP methods, got ${method}`)
  26. }
  27. }
  28. if (!Array.isArray(skipHeaderNames)) {
  29. throw new TypeError(`expected opts.skipHeaderNames to be an array, got ${typeof skipHeaderNames}`)
  30. }
  31. if (!Array.isArray(excludeHeaderNames)) {
  32. throw new TypeError(`expected opts.excludeHeaderNames to be an array, got ${typeof excludeHeaderNames}`)
  33. }
  34. // Convert to lowercase Set for case-insensitive header matching
  35. const skipHeaderNamesSet = new Set(skipHeaderNames.map(name => name.toLowerCase()))
  36. // Convert to lowercase Set for case-insensitive header exclusion from deduplication key
  37. const excludeHeaderNamesSet = new Set(excludeHeaderNames.map(name => name.toLowerCase()))
  38. /**
  39. * Map of pending requests for deduplication
  40. * @type {Map<string, DeduplicationHandler>}
  41. */
  42. const pendingRequests = new Map()
  43. return dispatch => {
  44. return (opts, handler) => {
  45. if (!opts.origin || methods.includes(opts.method) === false) {
  46. return dispatch(opts, handler)
  47. }
  48. opts = {
  49. ...opts,
  50. headers: normalizeHeaders(opts)
  51. }
  52. // Skip deduplication if request contains any of the specified headers
  53. if (skipHeaderNamesSet.size > 0) {
  54. for (const headerName of Object.keys(opts.headers)) {
  55. if (skipHeaderNamesSet.has(headerName.toLowerCase())) {
  56. return dispatch(opts, handler)
  57. }
  58. }
  59. }
  60. const cacheKey = makeCacheKey(opts)
  61. const dedupeKey = makeDeduplicationKey(cacheKey, excludeHeaderNamesSet)
  62. // Check if there's already a pending request for this key
  63. const pendingHandler = pendingRequests.get(dedupeKey)
  64. if (pendingHandler) {
  65. // Add this handler to the waiting list
  66. pendingHandler.addWaitingHandler(handler)
  67. return true
  68. }
  69. // Create a new deduplication handler
  70. const deduplicationHandler = new DeduplicationHandler(
  71. handler,
  72. () => {
  73. // Clean up when request completes
  74. pendingRequests.delete(dedupeKey)
  75. if (pendingRequestsChannel.hasSubscribers) {
  76. pendingRequestsChannel.publish({ size: pendingRequests.size, key: dedupeKey, type: 'removed' })
  77. }
  78. }
  79. )
  80. // Register the pending request
  81. pendingRequests.set(dedupeKey, deduplicationHandler)
  82. if (pendingRequestsChannel.hasSubscribers) {
  83. pendingRequestsChannel.publish({ size: pendingRequests.size, key: dedupeKey, type: 'added' })
  84. }
  85. return dispatch(opts, deduplicationHandler)
  86. }
  87. }
  88. }