util.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict'
  2. const assert = require('assert')
  3. const { URLSerializer } = require('../fetch/dataURL')
  4. const { isValidHeaderName } = require('../fetch/util')
  5. /**
  6. * @see https://url.spec.whatwg.org/#concept-url-equals
  7. * @param {URL} A
  8. * @param {URL} B
  9. * @param {boolean | undefined} excludeFragment
  10. * @returns {boolean}
  11. */
  12. function urlEquals (A, B, excludeFragment = false) {
  13. const serializedA = URLSerializer(A, excludeFragment)
  14. const serializedB = URLSerializer(B, excludeFragment)
  15. return serializedA === serializedB
  16. }
  17. /**
  18. * @see https://github.com/chromium/chromium/blob/694d20d134cb553d8d89e5500b9148012b1ba299/content/browser/cache_storage/cache_storage_cache.cc#L260-L262
  19. * @param {string} header
  20. */
  21. function fieldValues (header) {
  22. assert(header !== null)
  23. const values = []
  24. for (let value of header.split(',')) {
  25. value = value.trim()
  26. if (!value.length) {
  27. continue
  28. } else if (!isValidHeaderName(value)) {
  29. continue
  30. }
  31. values.push(value)
  32. }
  33. return values
  34. }
  35. module.exports = {
  36. urlEquals,
  37. fieldValues
  38. }