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

Function isEtagUsable

deps/undici/src/lib/util/cache.js:315–338  ·  view source on GitHub ↗

* Note: this deviates from the spec a little. Empty etags ("", W/"") are valid, * however, including them in cached resposnes serves little to no purpose. * * @see https://www.rfc-editor.org/rfc/rfc9110.html#name-etag * * @param {string} etag * @returns {boolean}

(etag)

Source from the content-addressed store, hash-verified

313 * @returns {boolean}
314 */
315function isEtagUsable (etag) {
316 if (etag.length <= 2) {
317 // Shortest an etag can be is two chars (just ""). This is where we deviate
318 // from the spec requiring a min of 3 chars however
319 return false
320 }
321
322 if (etag[0] === '"' && etag[etag.length - 1] === '"') {
323 // ETag: ""asd123"" or ETag: "W/"asd123"", kinda undefined behavior in the
324 // spec. Some servers will accept these while others don't.
325 // ETag: "asd123"
326 return !(etag[1] === '"' || etag.startsWith('"W/'))
327 }
328
329 if (etag.startsWith('W/"') && etag[etag.length - 1] === '"') {
330 // ETag: W/"", also where we deviate from the spec & require a min of 3
331 // chars
332 // ETag: for W/"", W/"asd123"
333 return etag.length !== 4
334 }
335
336 // Anything else
337 return false
338}
339
340/**
341 * @param {unknown} store

Callers 1

onResponseStartMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected