MCPcopy Create free account
hub / github.com/nodejs/node / stringify

Function stringify

deps/undici/src/lib/web/cookies/util.js:209–273  ·  view source on GitHub ↗

* @see https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1 * @param {import('./index').Cookie} cookie

(cookie)

Source from the content-addressed store, hash-verified

207 * @param {import('./index').Cookie} cookie
208 */
209function stringify (cookie) {
210 if (cookie.name.length === 0) {
211 return null
212 }
213
214 validateCookieName(cookie.name)
215 validateCookieValue(cookie.value)
216
217 const out = [`${cookie.name}=${cookie.value}`]
218
219 // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1
220 // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.2
221 if (cookie.name.startsWith('__Secure-')) {
222 cookie.secure = true
223 }
224
225 if (cookie.name.startsWith('__Host-')) {
226 cookie.secure = true
227 cookie.domain = null
228 cookie.path = '/'
229 }
230
231 if (cookie.secure) {
232 out.push('Secure')
233 }
234
235 if (cookie.httpOnly) {
236 out.push('HttpOnly')
237 }
238
239 if (typeof cookie.maxAge === 'number') {
240 validateCookieMaxAge(cookie.maxAge)
241 out.push(`Max-Age=${cookie.maxAge}`)
242 }
243
244 if (cookie.domain) {
245 validateCookieDomain(cookie.domain)
246 out.push(`Domain=${cookie.domain}`)
247 }
248
249 if (cookie.path) {
250 validateCookiePath(cookie.path)
251 out.push(`Path=${cookie.path}`)
252 }
253
254 if (cookie.expires && cookie.expires.toString() !== 'Invalid Date') {
255 out.push(`Expires=${toIMFDate(cookie.expires)}`)
256 }
257
258 if (cookie.sameSite) {
259 out.push(`SameSite=${cookie.sameSite}`)
260 }
261
262 for (const part of cookie.unparsed) {
263 if (!part.includes('=')) {
264 throw new Error('Invalid unparsed')
265 }
266

Callers 3

setCookieFunction · 0.70
serializePathWithQueryFunction · 0.50
serializePathWithQueryFunction · 0.50

Calls 11

validateCookieNameFunction · 0.85
validateCookieValueFunction · 0.85
validateCookieMaxAgeFunction · 0.85
validateCookieDomainFunction · 0.85
validateCookiePathFunction · 0.85
toIMFDateFunction · 0.85
includesMethod · 0.80
pushMethod · 0.45
toStringMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected