index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use strict'
  2. const { parseSetCookie } = require('./parse')
  3. const { stringify, getHeadersList } = require('./util')
  4. const { webidl } = require('../fetch/webidl')
  5. const { Headers } = require('../fetch/headers')
  6. /**
  7. * @typedef {Object} Cookie
  8. * @property {string} name
  9. * @property {string} value
  10. * @property {Date|number|undefined} expires
  11. * @property {number|undefined} maxAge
  12. * @property {string|undefined} domain
  13. * @property {string|undefined} path
  14. * @property {boolean|undefined} secure
  15. * @property {boolean|undefined} httpOnly
  16. * @property {'Strict'|'Lax'|'None'} sameSite
  17. * @property {string[]} unparsed
  18. */
  19. /**
  20. * @param {Headers} headers
  21. * @returns {Record<string, string>}
  22. */
  23. function getCookies (headers) {
  24. webidl.argumentLengthCheck(arguments, 1, { header: 'getCookies' })
  25. webidl.brandCheck(headers, Headers, { strict: false })
  26. const cookie = headers.get('cookie')
  27. const out = {}
  28. if (!cookie) {
  29. return out
  30. }
  31. for (const piece of cookie.split(';')) {
  32. const [name, ...value] = piece.split('=')
  33. out[name.trim()] = value.join('=')
  34. }
  35. return out
  36. }
  37. /**
  38. * @param {Headers} headers
  39. * @param {string} name
  40. * @param {{ path?: string, domain?: string }|undefined} attributes
  41. * @returns {void}
  42. */
  43. function deleteCookie (headers, name, attributes) {
  44. webidl.argumentLengthCheck(arguments, 2, { header: 'deleteCookie' })
  45. webidl.brandCheck(headers, Headers, { strict: false })
  46. name = webidl.converters.DOMString(name)
  47. attributes = webidl.converters.DeleteCookieAttributes(attributes)
  48. // Matches behavior of
  49. // https://github.com/denoland/deno_std/blob/63827b16330b82489a04614027c33b7904e08be5/http/cookie.ts#L278
  50. setCookie(headers, {
  51. name,
  52. value: '',
  53. expires: new Date(0),
  54. ...attributes
  55. })
  56. }
  57. /**
  58. * @param {Headers} headers
  59. * @returns {Cookie[]}
  60. */
  61. function getSetCookies (headers) {
  62. webidl.argumentLengthCheck(arguments, 1, { header: 'getSetCookies' })
  63. webidl.brandCheck(headers, Headers, { strict: false })
  64. const cookies = getHeadersList(headers).cookies
  65. if (!cookies) {
  66. return []
  67. }
  68. // In older versions of undici, cookies is a list of name:value.
  69. return cookies.map((pair) => parseSetCookie(Array.isArray(pair) ? pair[1] : pair))
  70. }
  71. /**
  72. * @param {Headers} headers
  73. * @param {Cookie} cookie
  74. * @returns {void}
  75. */
  76. function setCookie (headers, cookie) {
  77. webidl.argumentLengthCheck(arguments, 2, { header: 'setCookie' })
  78. webidl.brandCheck(headers, Headers, { strict: false })
  79. cookie = webidl.converters.Cookie(cookie)
  80. const str = stringify(cookie)
  81. if (str) {
  82. headers.append('Set-Cookie', stringify(cookie))
  83. }
  84. }
  85. webidl.converters.DeleteCookieAttributes = webidl.dictionaryConverter([
  86. {
  87. converter: webidl.nullableConverter(webidl.converters.DOMString),
  88. key: 'path',
  89. defaultValue: null
  90. },
  91. {
  92. converter: webidl.nullableConverter(webidl.converters.DOMString),
  93. key: 'domain',
  94. defaultValue: null
  95. }
  96. ])
  97. webidl.converters.Cookie = webidl.dictionaryConverter([
  98. {
  99. converter: webidl.converters.DOMString,
  100. key: 'name'
  101. },
  102. {
  103. converter: webidl.converters.DOMString,
  104. key: 'value'
  105. },
  106. {
  107. converter: webidl.nullableConverter((value) => {
  108. if (typeof value === 'number') {
  109. return webidl.converters['unsigned long long'](value)
  110. }
  111. return new Date(value)
  112. }),
  113. key: 'expires',
  114. defaultValue: null
  115. },
  116. {
  117. converter: webidl.nullableConverter(webidl.converters['long long']),
  118. key: 'maxAge',
  119. defaultValue: null
  120. },
  121. {
  122. converter: webidl.nullableConverter(webidl.converters.DOMString),
  123. key: 'domain',
  124. defaultValue: null
  125. },
  126. {
  127. converter: webidl.nullableConverter(webidl.converters.DOMString),
  128. key: 'path',
  129. defaultValue: null
  130. },
  131. {
  132. converter: webidl.nullableConverter(webidl.converters.boolean),
  133. key: 'secure',
  134. defaultValue: null
  135. },
  136. {
  137. converter: webidl.nullableConverter(webidl.converters.boolean),
  138. key: 'httpOnly',
  139. defaultValue: null
  140. },
  141. {
  142. converter: webidl.converters.USVString,
  143. key: 'sameSite',
  144. allowedValues: ['Strict', 'Lax', 'None']
  145. },
  146. {
  147. converter: webidl.sequenceConverter(webidl.converters.DOMString),
  148. key: 'unparsed',
  149. defaultValue: []
  150. }
  151. ])
  152. module.exports = {
  153. getCookies,
  154. deleteCookie,
  155. getSetCookies,
  156. setCookie
  157. }