@irvinebroque/http-rfc-utils 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +222 -0
  3. package/dist/auth.d.ts +139 -0
  4. package/dist/auth.d.ts.map +1 -0
  5. package/dist/auth.js +991 -0
  6. package/dist/auth.js.map +1 -0
  7. package/dist/cache-status.d.ts +15 -0
  8. package/dist/cache-status.d.ts.map +1 -0
  9. package/dist/cache-status.js +152 -0
  10. package/dist/cache-status.js.map +1 -0
  11. package/dist/cache.d.ts +94 -0
  12. package/dist/cache.d.ts.map +1 -0
  13. package/dist/cache.js +244 -0
  14. package/dist/cache.js.map +1 -0
  15. package/dist/client-hints.d.ts +23 -0
  16. package/dist/client-hints.d.ts.map +1 -0
  17. package/dist/client-hints.js +81 -0
  18. package/dist/client-hints.js.map +1 -0
  19. package/dist/conditional.d.ts +97 -0
  20. package/dist/conditional.d.ts.map +1 -0
  21. package/dist/conditional.js +300 -0
  22. package/dist/conditional.js.map +1 -0
  23. package/dist/content-disposition.d.ts +23 -0
  24. package/dist/content-disposition.d.ts.map +1 -0
  25. package/dist/content-disposition.js +122 -0
  26. package/dist/content-disposition.js.map +1 -0
  27. package/dist/cookie.d.ts +43 -0
  28. package/dist/cookie.d.ts.map +1 -0
  29. package/dist/cookie.js +472 -0
  30. package/dist/cookie.js.map +1 -0
  31. package/dist/cors.d.ts +53 -0
  32. package/dist/cors.d.ts.map +1 -0
  33. package/dist/cors.js +170 -0
  34. package/dist/cors.js.map +1 -0
  35. package/dist/datetime.d.ts +53 -0
  36. package/dist/datetime.d.ts.map +1 -0
  37. package/dist/datetime.js +205 -0
  38. package/dist/datetime.js.map +1 -0
  39. package/dist/digest.d.ts +220 -0
  40. package/dist/digest.d.ts.map +1 -0
  41. package/dist/digest.js +355 -0
  42. package/dist/digest.js.map +1 -0
  43. package/dist/encoding.d.ts +14 -0
  44. package/dist/encoding.d.ts.map +1 -0
  45. package/dist/encoding.js +86 -0
  46. package/dist/encoding.js.map +1 -0
  47. package/dist/etag.d.ts +55 -0
  48. package/dist/etag.d.ts.map +1 -0
  49. package/dist/etag.js +182 -0
  50. package/dist/etag.js.map +1 -0
  51. package/dist/ext-value.d.ts +40 -0
  52. package/dist/ext-value.d.ts.map +1 -0
  53. package/dist/ext-value.js +119 -0
  54. package/dist/ext-value.js.map +1 -0
  55. package/dist/forwarded.d.ts +14 -0
  56. package/dist/forwarded.d.ts.map +1 -0
  57. package/dist/forwarded.js +93 -0
  58. package/dist/forwarded.js.map +1 -0
  59. package/dist/header-utils.d.ts +71 -0
  60. package/dist/header-utils.d.ts.map +1 -0
  61. package/dist/header-utils.js +143 -0
  62. package/dist/header-utils.js.map +1 -0
  63. package/dist/headers.d.ts +71 -0
  64. package/dist/headers.d.ts.map +1 -0
  65. package/dist/headers.js +134 -0
  66. package/dist/headers.js.map +1 -0
  67. package/dist/hsts.d.ts +15 -0
  68. package/dist/hsts.d.ts.map +1 -0
  69. package/dist/hsts.js +106 -0
  70. package/dist/hsts.js.map +1 -0
  71. package/dist/http-signatures.d.ts +202 -0
  72. package/dist/http-signatures.d.ts.map +1 -0
  73. package/dist/http-signatures.js +720 -0
  74. package/dist/http-signatures.js.map +1 -0
  75. package/dist/index.d.ts +41 -0
  76. package/dist/index.d.ts.map +1 -0
  77. package/dist/index.js +125 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/json-pointer.d.ts +97 -0
  80. package/dist/json-pointer.d.ts.map +1 -0
  81. package/dist/json-pointer.js +278 -0
  82. package/dist/json-pointer.js.map +1 -0
  83. package/dist/jsonpath.d.ts +98 -0
  84. package/dist/jsonpath.d.ts.map +1 -0
  85. package/dist/jsonpath.js +1470 -0
  86. package/dist/jsonpath.js.map +1 -0
  87. package/dist/language.d.ts +14 -0
  88. package/dist/language.d.ts.map +1 -0
  89. package/dist/language.js +95 -0
  90. package/dist/language.js.map +1 -0
  91. package/dist/link.d.ts +102 -0
  92. package/dist/link.d.ts.map +1 -0
  93. package/dist/link.js +437 -0
  94. package/dist/link.js.map +1 -0
  95. package/dist/linkset.d.ts +111 -0
  96. package/dist/linkset.d.ts.map +1 -0
  97. package/dist/linkset.js +501 -0
  98. package/dist/linkset.js.map +1 -0
  99. package/dist/negotiate.d.ts +71 -0
  100. package/dist/negotiate.d.ts.map +1 -0
  101. package/dist/negotiate.js +357 -0
  102. package/dist/negotiate.js.map +1 -0
  103. package/dist/pagination.d.ts +80 -0
  104. package/dist/pagination.d.ts.map +1 -0
  105. package/dist/pagination.js +188 -0
  106. package/dist/pagination.js.map +1 -0
  107. package/dist/prefer.d.ts +18 -0
  108. package/dist/prefer.d.ts.map +1 -0
  109. package/dist/prefer.js +93 -0
  110. package/dist/prefer.js.map +1 -0
  111. package/dist/problem.d.ts +54 -0
  112. package/dist/problem.d.ts.map +1 -0
  113. package/dist/problem.js +104 -0
  114. package/dist/problem.js.map +1 -0
  115. package/dist/proxy-status.d.ts +28 -0
  116. package/dist/proxy-status.d.ts.map +1 -0
  117. package/dist/proxy-status.js +220 -0
  118. package/dist/proxy-status.js.map +1 -0
  119. package/dist/range.d.ts +28 -0
  120. package/dist/range.d.ts.map +1 -0
  121. package/dist/range.js +243 -0
  122. package/dist/range.js.map +1 -0
  123. package/dist/response.d.ts +101 -0
  124. package/dist/response.d.ts.map +1 -0
  125. package/dist/response.js +200 -0
  126. package/dist/response.js.map +1 -0
  127. package/dist/sorting.d.ts +66 -0
  128. package/dist/sorting.d.ts.map +1 -0
  129. package/dist/sorting.js +168 -0
  130. package/dist/sorting.js.map +1 -0
  131. package/dist/structured-fields.d.ts +30 -0
  132. package/dist/structured-fields.d.ts.map +1 -0
  133. package/dist/structured-fields.js +468 -0
  134. package/dist/structured-fields.js.map +1 -0
  135. package/dist/types.d.ts +772 -0
  136. package/dist/types.d.ts.map +1 -0
  137. package/dist/types.js +8 -0
  138. package/dist/types.js.map +1 -0
  139. package/dist/uri-template.d.ts +48 -0
  140. package/dist/uri-template.d.ts.map +1 -0
  141. package/dist/uri-template.js +483 -0
  142. package/dist/uri-template.js.map +1 -0
  143. package/dist/uri.d.ts +80 -0
  144. package/dist/uri.d.ts.map +1 -0
  145. package/dist/uri.js +423 -0
  146. package/dist/uri.js.map +1 -0
  147. package/package.json +66 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Brendan Irvine-Broque
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,222 @@
1
+ # http-rfc-utils
2
+
3
+ RFC-aligned HTTP utilities for APIs: ETags, conditional requests, caching, link headers, content negotiation, pagination, CORS, and Problem Details.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install http-rfc-utils
9
+ ```
10
+
11
+ ## Supported RFCs
12
+
13
+ - [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html), Sections 5.6.7, 8.8.2-8.8.3.2, 10.2.3, 12.4.2, 12.5.3-12.5.5, 13.1.1-13.1.5, 13.2.2, 14.1.2-14.4, 15.4.5, 15.5.13, 15.5.17 (HTTP semantics)
14
+ - [RFC 9111](https://www.rfc-editor.org/rfc/rfc9111.html), Sections 1.2.2, 5.2, 5.2.2 (Cache-Control)
15
+ - [RFC 5861](https://www.rfc-editor.org/rfc/rfc5861.html), Section 3 (Cache-Control extensions)
16
+ - [RFC 8246](https://www.rfc-editor.org/rfc/rfc8246.html), Section 2 (Immutable Cache-Control extension)
17
+ - [RFC 8288](https://www.rfc-editor.org/rfc/rfc8288.html), Sections 3.1-3.4, 6 (Web Linking)
18
+ - [RFC 9264](https://www.rfc-editor.org/rfc/rfc9264.html), Sections 4.1-4.2, 6 (Linkset media types)
19
+ - [RFC 9727](https://www.rfc-editor.org/rfc/rfc9727.html), Sections 2-4, 7 (api-catalog well-known URI and link relation)
20
+ - [RFC 7231](https://www.rfc-editor.org/rfc/rfc7231.html), Sections 5.3.1-5.3.2 (Accept header) — obsoleted by RFC 9110
21
+ - [RFC 7240](https://www.rfc-editor.org/rfc/rfc7240.html), Sections 2-3 (Prefer)
22
+ - [RFC 7239](https://www.rfc-editor.org/rfc/rfc7239.html), Section 4 (Forwarded header)
23
+ - [RFC 6265](https://www.rfc-editor.org/rfc/rfc6265.html), Sections 4.1.1, 4.2.1, 5.1.1, 5.1.3-5.1.4, 5.2-5.4 (Cookies)
24
+ - [RFC 6266](https://www.rfc-editor.org/rfc/rfc6266.html), Sections 4-4.3 (Content-Disposition)
25
+ - [RFC 8187](https://www.rfc-editor.org/rfc/rfc8187.html), Section 3.2 (Header parameter encoding)
26
+ - [RFC 6797](https://www.rfc-editor.org/rfc/rfc6797.html), Sections 6.1-6.1.2 (Strict-Transport-Security)
27
+ - [RFC 7617](https://www.rfc-editor.org/rfc/rfc7617.html), Sections 2-2.1 (Basic authentication)
28
+ - [RFC 6750](https://www.rfc-editor.org/rfc/rfc6750.html), Sections 2.1, 3 (Bearer tokens)
29
+ - [RFC 7616](https://www.rfc-editor.org/rfc/rfc7616.html), Sections 3.3-3.5 (Digest authentication)
30
+ - [RFC 4647](https://www.rfc-editor.org/rfc/rfc4647.html), Section 3 (Language tag matching)
31
+ - [RFC 8941](https://www.rfc-editor.org/rfc/rfc8941.html), Sections 3-4 (Structured Field Values)
32
+ - [RFC 8942](https://www.rfc-editor.org/rfc/rfc8942.html), Sections 2.2, 3.1-3.2, 4.2 (Client Hints)
33
+ - [RFC 9211](https://www.rfc-editor.org/rfc/rfc9211.html), Sections 2-2.8 (Cache-Status)
34
+ - [RFC 9209](https://www.rfc-editor.org/rfc/rfc9209.html), Sections 2-2.4 (Proxy-Status)
35
+ - [RFC 9530](https://www.rfc-editor.org/rfc/rfc9530.html), Sections 2-5 (Digest Fields)
36
+ - [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html), Sections 3.1-3.2, 4.1 (Problem Details)
37
+ - [RFC 6901](https://www.rfc-editor.org/rfc/rfc6901.html), Sections 3-7 (JSON Pointer)
38
+ - [RFC 9535](https://www.rfc-editor.org/rfc/rfc9535.html), Sections 2.1-2.7 (JSONPath)
39
+ - [RFC 3339](https://www.rfc-editor.org/rfc/rfc3339.html), Section 5.6 (timestamps)
40
+ - [RFC 850](https://www.rfc-editor.org/rfc/rfc850.html), Section 2 (legacy HTTP-date format, obsolete)
41
+ - [RFC 8594](https://www.rfc-editor.org/rfc/rfc8594.html), Sections 3, 6 (Sunset header)
42
+ - [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986.html), Sections 2, 3.1, 3.2.2, 5.2.4, 6.2 (URI Generic Syntax)
43
+ - [RFC 6570](https://www.rfc-editor.org/rfc/rfc6570.html), Sections 1.2, 2-3 (URI Template)
44
+ - [RFC 9421](https://www.rfc-editor.org/rfc/rfc9421.html), Sections 2-4 (HTTP Message Signatures)
45
+ - Fetch/CORS specs (CORS headers)
46
+
47
+ ## RFC References
48
+
49
+ Use these RFCs as the source of truth for HTTP behavior and field formats:
50
+
51
+ - [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html) - Sections 5.6.7, 8.8.2-8.8.3.2, 10.2.3, 12.4.2, 12.5.3-12.5.5, 13.1.1-13.1.5, 13.2.2, 14.1.2-14.4, 15.4.5, 15.5.13, 15.5.17
52
+ - [RFC 9111](https://www.rfc-editor.org/rfc/rfc9111.html) - Sections 1.2.2, 5.2, 5.2.2
53
+ - [RFC 5861](https://www.rfc-editor.org/rfc/rfc5861.html) - Section 3 (stale-while-revalidate, stale-if-error)
54
+ - [RFC 8246](https://www.rfc-editor.org/rfc/rfc8246.html) - Section 2 (immutable)
55
+ - [RFC 8288](https://www.rfc-editor.org/rfc/rfc8288.html) - Sections 3.1-3.4, 6
56
+ - [RFC 9264](https://www.rfc-editor.org/rfc/rfc9264.html) - Sections 4.1-4.2, 6
57
+ - [RFC 9727](https://www.rfc-editor.org/rfc/rfc9727.html) - Sections 2-4, 7
58
+ - [RFC 7231](https://www.rfc-editor.org/rfc/rfc7231.html) - Sections 5.3.1-5.3.2 (obsoleted by RFC 9110)
59
+ - [RFC 7240](https://www.rfc-editor.org/rfc/rfc7240.html) - Sections 2-3
60
+ - [RFC 7239](https://www.rfc-editor.org/rfc/rfc7239.html) - Section 4
61
+ - [RFC 6265](https://www.rfc-editor.org/rfc/rfc6265.html) - Sections 4.1.1, 4.2.1, 5.1.1, 5.1.3-5.1.4, 5.2-5.4
62
+ - [RFC 6266](https://www.rfc-editor.org/rfc/rfc6266.html) - Sections 4-4.3
63
+ - [RFC 8187](https://www.rfc-editor.org/rfc/rfc8187.html) - Section 3.2
64
+ - [RFC 6797](https://www.rfc-editor.org/rfc/rfc6797.html) - Sections 6.1-6.1.2
65
+ - [RFC 7617](https://www.rfc-editor.org/rfc/rfc7617.html) - Sections 2-2.1
66
+ - [RFC 6750](https://www.rfc-editor.org/rfc/rfc6750.html) - Sections 2.1, 3
67
+ - [RFC 7616](https://www.rfc-editor.org/rfc/rfc7616.html) - Sections 3.3-3.5
68
+ - [RFC 4647](https://www.rfc-editor.org/rfc/rfc4647.html) - Section 3 (basic filtering)
69
+ - [RFC 8941](https://www.rfc-editor.org/rfc/rfc8941.html) - Sections 3-4
70
+ - [RFC 8942](https://www.rfc-editor.org/rfc/rfc8942.html) - Sections 2.2, 3.1-3.2, 4.2
71
+ - [RFC 9211](https://www.rfc-editor.org/rfc/rfc9211.html) - Sections 2-2.8
72
+ - [RFC 9209](https://www.rfc-editor.org/rfc/rfc9209.html) - Sections 2-2.4
73
+ - [RFC 9530](https://www.rfc-editor.org/rfc/rfc9530.html) - Sections 2-5
74
+ - [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) - Sections 3.1-3.2, 4.1
75
+ - [RFC 6901](https://www.rfc-editor.org/rfc/rfc6901.html) - Sections 3-7
76
+ - [RFC 9535](https://www.rfc-editor.org/rfc/rfc9535.html) - Sections 2.1-2.7
77
+ - [RFC 3339](https://www.rfc-editor.org/rfc/rfc3339.html) - Section 5.6
78
+ - [RFC 850](https://www.rfc-editor.org/rfc/rfc850.html) - Section 2
79
+ - [RFC 8594](https://www.rfc-editor.org/rfc/rfc8594.html) - Sections 3, 6
80
+ - [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986.html) - Sections 2, 3.1, 3.2.2, 5.2.4, 6.2
81
+ - [RFC 6570](https://www.rfc-editor.org/rfc/rfc6570.html) - Sections 1.2, 2-3
82
+ - [RFC 9421](https://www.rfc-editor.org/rfc/rfc9421.html) - Sections 2-4
83
+
84
+ ## Requirements
85
+
86
+ - Node.js 25 or later
87
+
88
+ ## RFC Map
89
+
90
+ | Module | RFCs | Exports | Usage |
91
+ | --- | --- | --- | --- |
92
+ | `src/etag.ts` | RFC 9110 §§8.8.3-8.8.3.2, 13.1.1-13.1.2 | `generateETag`, `generateETagAsync`, `parseETag`, `formatETag`, `compareETags`, `compareETagStrings`, `ETag` | Generate ETags for representations; parse/compare `If-Match` and `If-None-Match`. |
93
+ | `src/conditional.ts` | RFC 9110 §§13.1.1-13.1.4, 13.2.2, 15.4.5, 15.5.13 | `evaluatePreconditions`, `handleConditionalRequest`, `parseIfNoneMatch`, `parseIfMatch`, `evaluateIfMatch`, `evaluateIfNoneMatch`, `evaluateIfModifiedSince`, `evaluateIfUnmodifiedSince` | Evaluate conditional request headers and decide `304/412`. |
94
+ | `src/datetime.ts` | RFC 3339 §5.6; RFC 9110 §5.6.7; RFC 850 §2 | `toRFC3339`, `parseRFC3339`, `formatHTTPDate`, `parseHTTPDate`, `isExpired`, `secondsUntil` | Format JSON timestamps and HTTP date headers. |
95
+ | `src/cache.ts` | RFC 9111 §§1.2.2, 5.2, 5.2.2; RFC 5861 §3; RFC 8246 §2 | `cacheControl`, `getCacheHeaders`, `parseCacheControl`, `CachePresets` | Build/parse `Cache-Control`, add `ETag` + `Last-Modified`. |
96
+ | `src/range.ts` | RFC 9110 §§13.1.5, 14.1.2-14.4, 15.5.17 | `parseRange`, `formatContentRange`, `parseContentRange`, `acceptRanges`, `evaluateRange` | Parse and evaluate Range requests; build `Content-Range`. |
97
+ | `src/prefer.ts` | RFC 7240 §§2-3 | `parsePrefer`, `formatPrefer`, `formatPreferenceApplied` | Parse `Prefer` and emit `Preference-Applied`. |
98
+ | `src/forwarded.ts` | RFC 7239 §4 | `parseForwarded`, `formatForwarded` | Parse and format `Forwarded` header values. |
99
+ | `src/ext-value.ts` | RFC 8187 §3.2 | `decodeExtValue`, `encodeExtValue`, `needsExtendedEncoding`, `isAttrChar` | Decode/encode RFC 8187 ext-value for `title*`, `filename*`, etc. |
100
+ | `src/content-disposition.ts` | RFC 6266 §§4-4.3; RFC 8187 §3.2 | `parseContentDisposition`, `formatContentDisposition`, `formatHeaderParam` | Parse/format `Content-Disposition` with `filename*` support. |
101
+ | `src/cookie.ts` | RFC 6265 §§4.1.1, 4.2.1, 5.1.1, 5.1.3-5.1.4, 5.2-5.4 | `parseCookie`, `formatCookie`, `parseSetCookie`, `formatSetCookie`, `parseCookieDate`, `domainMatches`, `defaultPath`, `pathMatches`, `buildCookieHeader` | Parse/format cookies and build Cookie headers. |
102
+ | `src/auth.ts` | RFC 7617 §§2-2.1; RFC 6750 §§2.1, 3; RFC 7616 §§3.3-3.5 | `parseAuthorization`, `formatAuthorization`, `parseWWWAuthenticate`, `formatWWWAuthenticate`, `parseBasicAuthorization`, `formatBasicAuthorization`, `parseBasicChallenge`, `formatBasicChallenge`, `parseBearerAuthorization`, `formatBearerAuthorization`, `parseBearerChallenge`, `formatBearerChallenge`, `parseDigestChallenge`, `formatDigestChallenge`, `parseDigestAuthorization`, `formatDigestAuthorization`, `parseDigestAuthenticationInfo`, `formatDigestAuthenticationInfo`, `computeDigestResponse`, `computeA1`, `computeA2`, `hashDigestUsername`, `DIGEST_AUTH_ALGORITHMS` | Parse/format Basic, Bearer, and Digest auth headers; compute Digest response values. |
103
+ | `src/hsts.ts` | RFC 6797 §§6.1-6.1.2, 8.1 | `parseStrictTransportSecurity`, `formatStrictTransportSecurity` | Parse and format `Strict-Transport-Security`. |
104
+ | `src/language.ts` | RFC 9110 §§12.4.2, 12.5.4; RFC 4647 §3 | `parseAcceptLanguage`, `negotiateLanguage` | Parse and negotiate `Accept-Language` with basic filtering. |
105
+ | `src/encoding.ts` | RFC 9110 §§12.4.2, 12.4.3, 12.5.3 | `parseAcceptEncoding`, `negotiateEncoding` | Parse and negotiate `Accept-Encoding` values. |
106
+ | `src/headers.ts` | RFC 9110 §§10.2.3, 12.5.5; RFC 8594 §3 | `parseRetryAfter`, `formatRetryAfter`, `mergeVary`, `parseSunset`, `formatSunset`, `isSunsetImminent` | Retry-After parsing/formatting, Vary merging, and Sunset header. |
107
+ | `src/structured-fields.ts` | RFC 8941 §§3-4 | `parseSfList`, `parseSfDict`, `parseSfItem`, `serializeSfList`, `serializeSfDict`, `serializeSfItem` | Structured field parsing and serialization. |
108
+ | `src/client-hints.ts` | RFC 8942 §§2.2, 3.1-3.2, 4.2 | `parseAcceptCH`, `formatAcceptCH`, `filterClientHints`, `mergeClientHintsVary` | Accept-CH parsing and Vary helper for Client Hints. |
109
+ | `src/cache-status.ts` | RFC 9211 §§2-2.8 | `parseCacheStatus`, `formatCacheStatus` | Parse and format Cache-Status responses. |
110
+ | `src/proxy-status.ts` | RFC 9209 §§2-2.4 | `parseProxyStatus`, `formatProxyStatus`, `isProxyErrorType`, `PROXY_ERROR_TYPES` | Parse and format Proxy-Status responses; validate error types. |
111
+ | `src/digest.ts` | RFC 9530 §§2-5 | `parseContentDigest`, `parseReprDigest`, `parseWantContentDigest`, `parseWantReprDigest`, `formatContentDigest`, `formatReprDigest`, `formatWantContentDigest`, `formatWantReprDigest`, `generateDigest`, `verifyDigest`, `DIGEST_ALGORITHMS`, `isActiveAlgorithm`, `isDeprecatedAlgorithm` | Parse/format Content-Digest, Repr-Digest, and Want-* preference headers; generate and verify SHA-256/SHA-512 digests. |
112
+ | `src/link.ts` | RFC 8288 §§3-3.5, 6; RFC 8187 §3.2; RFC 9264 §6 | `formatLink`, `formatLinkHeader`, `parseLinkHeader`, `buildLinkHeader`, `quoteIfNeeded`, `unquote`, `LinkRelation` | Assemble and parse `Link` headers with `title*` (RFC 8187) and multiple `hreflang` support. |
113
+ | `src/linkset.ts` | RFC 9264 §§4.1-4.2, 6; RFC 9727 §§2-4, 7 | `parseLinkset`, `parseLinksetJson`, `formatLinkset`, `formatLinksetJson`, `linksetToJson`, `jsonToLinkset`, `isValidLinkset`, `linksetToLinks`, `linksToLinkset`, `createApiCatalog`, `parseApiCatalog`, `isApiCatalog`, `LINKSET_MEDIA_TYPE`, `API_CATALOG_PROFILE`, `API_CATALOG_PATH` | Parse/format linkset documents; create/parse API catalogs per RFC 9727. |
114
+ | `src/negotiate.ts` | RFC 7231 §§5.3.1-5.3.2 | `parseAccept`, `negotiate`, `getResponseFormat`, `toCSV`, `MEDIA_TYPES`, `MIME_TO_FORMAT` | Negotiate response media types and build CSV. |
115
+ | `src/response.ts` | RFC 9110 §§8.8.2-8.8.3; RFC 9111 §5.2.2; RFC 8288 §3; RFC 6266 §4 | `optionsResponse`, `headResponse`, `jsonResponse`, `csvResponse`, `redirectResponse`, `simpleJsonResponse`, `noContentResponse`, `textResponse` | Opinionated response helpers (CORS + cache + pagination). |
116
+ | `src/pagination.ts` | RFC 8288 §§3, 3.3 + API conventions | `parsePaginationParams`, `decodeCursor`, `encodeCursor`, `buildPaginationLinks`, `lastPageOffset`, `isFirstPage`, `isLastPage` | Cursor/offset parsing and pagination links. |
117
+ | `src/problem.ts` | RFC 9457 §§3.1-3.2, 4.1 | `createProblem`, `problemResponse`, `Problems` | Return structured error responses. |
118
+ | `src/json-pointer.ts` | RFC 6901 §§3-7 | `parseJsonPointer`, `formatJsonPointer`, `evaluateJsonPointer`, `toUriFragment`, `fromUriFragment`, `isValidJsonPointer` | Parse, format, and evaluate JSON Pointers; URI fragment encoding. |
119
+ | `src/jsonpath.ts` | RFC 9535 §§2.1-2.7 | `parseJsonPath`, `queryJsonPath`, `queryJsonPathNodes`, `isValidJsonPath`, `formatNormalizedPath`, `compileJsonPath` | Parse and execute JSONPath queries; return values or nodes with normalized paths. |
120
+ | `src/uri.ts` | RFC 3986 §§2, 3.1, 3.2.2, 5.2.4, 6.2 | `percentEncode`, `percentDecode`, `normalizeUri`, `removeDotSegments`, `compareUris`, `isUnreserved`, `isReserved` | URI percent-encoding, normalization, and comparison. |
121
+ | `src/uri-template.ts` | RFC 6570 §§1.2, 2-3 | `parseUriTemplate`, `expandUriTemplate`, `isValidUriTemplate`, `getTemplateVariables`, `compileUriTemplate` | Parse and expand URI Templates with support for all Level 4 operators and modifiers. |
122
+ | `src/http-signatures.ts` | RFC 9421 §§2-4 | `parseSignatureInput`, `formatSignatureInput`, `parseSignature`, `formatSignature`, `parseComponentIdentifier`, `formatComponentIdentifier`, `canonicalizeFieldValue`, `binaryWrapFieldValues`, `deriveComponentValue`, `createSignatureBase`, `isDerivedComponent`, `DERIVED_COMPONENTS` | Parse/format Signature-Input and Signature fields; create signature base strings for HTTP message signatures. |
123
+ | `src/cors.ts` | Fetch/CORS specs | `defaultCorsHeaders`, `buildCorsHeaders`, `buildCorsHeadersForOrigin`, `buildPreflightHeaders`, `isOriginAllowed`, `corsHeaders` | Build CORS headers for single or multiple origins. |
124
+
125
+ ## Recipes
126
+
127
+ ### Conditional GET with ETag + Last-Modified
128
+
129
+ 1. Generate an ETag (`generateETag` or `generateETagAsync`).
130
+ 2. Determine `lastModified`.
131
+ 3. Call `evaluatePreconditions(request, currentETag, lastModified)`.
132
+ 4. If `proceed === false`, return `304` or `412` with the provided headers.
133
+ 5. Otherwise return your response with `ETag` and `Last-Modified`.
134
+
135
+ ```ts
136
+ import { evaluatePreconditions } from 'http-rfc-utils';
137
+
138
+ const result = evaluatePreconditions(request, currentETag, lastModified);
139
+
140
+ if (!result.proceed) {
141
+ return new Response(null, {
142
+ status: result.status,
143
+ headers: result.headers,
144
+ });
145
+ }
146
+ ```
147
+
148
+ ### Cache-Control for API responses
149
+
150
+ 1. Pick `CachePresets` or custom `CacheOptions`.
151
+ 2. Use `cacheControl` or `getCacheHeaders`.
152
+ 3. Combine with `jsonResponse`/`simpleJsonResponse`.
153
+
154
+ ```ts
155
+ import { cacheControl, CachePresets } from 'http-rfc-utils';
156
+
157
+ const headers = {
158
+ 'Cache-Control': cacheControl(CachePresets.revalidate),
159
+ };
160
+ ```
161
+
162
+ ### Accept negotiation + CSV
163
+
164
+ 1. `negotiate(request, ['application/json', 'text/csv'])`.
165
+ 2. If CSV, `toCSV(data)` and `csvResponse`.
166
+ 3. Otherwise `jsonResponse`.
167
+
168
+ ### Link header parsing
169
+
170
+ ```ts
171
+ import { parseLinkHeader } from 'http-rfc-utils';
172
+
173
+ const links = parseLinkHeader(response.headers.get('Link') ?? '');
174
+ ```
175
+
176
+ ## API Summary
177
+
178
+ - `etag`: generate/parse/compare validators; async uses `crypto.subtle` for hashing.
179
+ - `conditional`: RFC 9110 precondition evaluation with correct precedence.
180
+ - `datetime`: RFC 3339 timestamps and RFC 9110 HTTP dates.
181
+ - `cache`: Cache-Control builder/parser plus combined cache headers.
182
+ - `link`: RFC 8288 Link formatter/parser; handles quoted commas and escapes.
183
+ - `linkset`: RFC 9264 linkset document parser/formatter; RFC 9727 API catalog creation/parsing.
184
+ - `negotiate`: Accept parsing + media type negotiation; `toCSV` helper.
185
+ - `language`: Accept-Language parsing + basic filtering negotiation.
186
+ - `encoding`: Accept-Encoding parsing + negotiation.
187
+ - `range`: Range parsing + evaluation helpers.
188
+ - `prefer`: Prefer parsing + Preference-Applied formatting.
189
+ - `forwarded`: Forwarded header parsing + formatting.
190
+ - `content-disposition`: Content-Disposition parsing + formatting with RFC 8187 ext-value utilities.
191
+ - `cookie`: Cookie/Set-Cookie parsing, matching helpers, and Cookie header generation.
192
+ - `auth`: Basic, Bearer, and Digest Authorization + WWW-Authenticate parsing/formatting; Digest response computation.
193
+ - `hsts`: Strict-Transport-Security parsing/formatting.
194
+ - `headers`: Retry-After parsing/formatting, Vary merging, and Sunset header.
195
+ - `structured-fields`: Structured Field Values parsing + serialization.
196
+ - `client-hints`: Accept-CH parsing and Vary helper for Client Hints.
197
+ - `cache-status`: Cache-Status parsing and formatting.
198
+ - `proxy-status`: Proxy-Status parsing and formatting; error type validation.
199
+ - `digest`: RFC 9530 Content-Digest and Repr-Digest parsing/formatting; Want-* preferences; SHA-256/SHA-512 generation and verification.
200
+ - `response`: opinionated helpers for API responses with CORS + caching.
201
+ - `pagination`: cursor/limit parsing and Link header pagination.
202
+ - `problem`: RFC 9457 Problem Details responses.
203
+ - `json-pointer`: RFC 6901 JSON Pointer parsing, formatting, and evaluation.
204
+ - `jsonpath`: RFC 9535 JSONPath query parsing and execution with filters, slices, and built-in functions.
205
+ - `uri`: RFC 3986 URI percent-encoding, normalization, and comparison.
206
+ - `uri-template`: RFC 6570 URI Template parsing and expansion; all Level 4 operators and modifiers.
207
+ - `http-signatures`: RFC 9421 HTTP Message Signatures; Signature-Input/Signature field parsing; signature base creation.
208
+ - `cors`: permissive defaults plus origin-aware header builder.
209
+
210
+ ## Notes
211
+
212
+ - `jsonResponse` returns `{ data, meta }`; use `simpleJsonResponse` for raw payloads.
213
+ - `csvResponse` only serializes data rows; pagination metadata remains in headers.
214
+ - `generateETagAsync` accepts `ArrayBuffer`/`ArrayBufferView` and hashes bytes directly.
215
+ - `generateETag` and `generateETagAsync` rely on `JSON.stringify` for non-binary objects; property order affects the resulting ETag.
216
+ - For multiple CORS origins, use `buildCorsHeadersForOrigin(requestOrigin, options)` and include `Vary: Origin`.
217
+
218
+ ## Testing
219
+
220
+ ```bash
221
+ npm test
222
+ ```
package/dist/auth.d.ts ADDED
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Authorization and WWW-Authenticate utilities for Basic, Bearer, and Digest.
3
+ * RFC 7617 §2, §2.1; RFC 6750 §2.1, §3; RFC 7616 §3.3-3.5.
4
+ * @see https://www.rfc-editor.org/rfc/rfc7617.html#section-2
5
+ * @see https://www.rfc-editor.org/rfc/rfc7616.html
6
+ */
7
+ import type { AuthChallenge, AuthCredentials, BasicChallenge, BasicCredentials, BearerChallenge, DigestAuthAlgorithm, DigestAuthenticationInfo, DigestAuthQop, DigestChallenge, DigestComputeOptions, DigestCredentials } from './types.js';
8
+ /**
9
+ * Parse Authorization header into scheme + token68 or auth-params.
10
+ */
11
+ export declare function parseAuthorization(header: string): AuthCredentials | null;
12
+ /**
13
+ * Format Authorization header from credentials.
14
+ */
15
+ export declare function formatAuthorization(credentials: AuthCredentials): string;
16
+ /**
17
+ * Parse WWW-Authenticate header into challenges.
18
+ */
19
+ export declare function parseWWWAuthenticate(header: string): AuthChallenge[];
20
+ /**
21
+ * Format WWW-Authenticate header from challenges.
22
+ */
23
+ export declare function formatWWWAuthenticate(challenges: AuthChallenge[]): string;
24
+ /**
25
+ * Parse Basic Authorization header.
26
+ */
27
+ export declare function parseBasicAuthorization(header: string, options?: {
28
+ encoding?: 'utf-8' | 'latin1';
29
+ }): BasicCredentials | null;
30
+ /**
31
+ * Format Basic Authorization header.
32
+ */
33
+ export declare function formatBasicAuthorization(username: string, password: string, options?: {
34
+ encoding?: 'utf-8' | 'latin1';
35
+ }): string | null;
36
+ /**
37
+ * Parse Basic WWW-Authenticate challenge.
38
+ */
39
+ export declare function parseBasicChallenge(header: string): BasicChallenge | null;
40
+ /**
41
+ * Format Basic WWW-Authenticate challenge.
42
+ */
43
+ export declare function formatBasicChallenge(realm: string, options?: {
44
+ charset?: 'UTF-8';
45
+ }): string;
46
+ /**
47
+ * Parse Bearer Authorization header.
48
+ */
49
+ export declare function parseBearerAuthorization(header: string): string | null;
50
+ /**
51
+ * Format Bearer Authorization header.
52
+ */
53
+ export declare function formatBearerAuthorization(token: string): string | null;
54
+ /**
55
+ * Parse Bearer WWW-Authenticate challenge.
56
+ */
57
+ export declare function parseBearerChallenge(header: string): BearerChallenge | null;
58
+ /**
59
+ * Format Bearer WWW-Authenticate challenge.
60
+ */
61
+ export declare function formatBearerChallenge(params: BearerChallenge): string;
62
+ /**
63
+ * Supported Digest authentication algorithms.
64
+ * RFC 7616 §3.3: SHA-256 MUST be supported; MD5 for backward compatibility.
65
+ */
66
+ export declare const DIGEST_AUTH_ALGORITHMS: DigestAuthAlgorithm[];
67
+ /**
68
+ * Parse Digest WWW-Authenticate challenge.
69
+ * RFC 7616 §3.3.
70
+ */
71
+ export declare function parseDigestChallenge(challenge: AuthChallenge): DigestChallenge | null;
72
+ /**
73
+ * Format Digest WWW-Authenticate challenge.
74
+ * RFC 7616 §3.3.
75
+ */
76
+ export declare function formatDigestChallenge(challenge: DigestChallenge): string;
77
+ /**
78
+ * Parse Digest Authorization credentials.
79
+ * RFC 7616 §3.4.
80
+ */
81
+ export declare function parseDigestAuthorization(credentials: AuthCredentials): DigestCredentials | null;
82
+ /**
83
+ * Format Digest Authorization credentials.
84
+ * RFC 7616 §3.4.
85
+ */
86
+ export declare function formatDigestAuthorization(credentials: DigestCredentials): string;
87
+ /**
88
+ * Parse Authentication-Info header.
89
+ * RFC 7616 §3.5.
90
+ */
91
+ export declare function parseDigestAuthenticationInfo(value: string): DigestAuthenticationInfo | null;
92
+ /**
93
+ * Format Authentication-Info header.
94
+ * RFC 7616 §3.5.
95
+ */
96
+ export declare function formatDigestAuthenticationInfo(info: DigestAuthenticationInfo): string;
97
+ /**
98
+ * Compute A1 value for Digest authentication.
99
+ * RFC 7616 §3.4.2.
100
+ *
101
+ * @param username - Username
102
+ * @param realm - Realm
103
+ * @param password - Password
104
+ * @param algorithm - Algorithm (session algorithms require nonce and cnonce)
105
+ * @param nonce - Server nonce (required for -sess algorithms)
106
+ * @param cnonce - Client nonce (required for -sess algorithms)
107
+ * @returns A1 hash value (hex string)
108
+ */
109
+ export declare function computeA1(username: string, realm: string, password: string, algorithm?: DigestAuthAlgorithm, nonce?: string, cnonce?: string): Promise<string>;
110
+ /**
111
+ * Compute A2 value for Digest authentication.
112
+ * RFC 7616 §3.4.3.
113
+ *
114
+ * @param method - HTTP method
115
+ * @param uri - Request URI
116
+ * @param qop - Quality of protection (optional)
117
+ * @param entityBody - Entity body for auth-int (optional)
118
+ * @returns A2 string value
119
+ */
120
+ export declare function computeA2(method: string, uri: string, qop?: DigestAuthQop, _entityBody?: Uint8Array): string;
121
+ /**
122
+ * Compute the Digest response value.
123
+ * RFC 7616 §3.4.1.
124
+ *
125
+ * @param options - Computation options
126
+ * @returns Response hash value (hex string)
127
+ */
128
+ export declare function computeDigestResponse(options: DigestComputeOptions): Promise<string>;
129
+ /**
130
+ * Hash a username for userhash support.
131
+ * RFC 7616 §3.4.4.
132
+ *
133
+ * @param username - Username to hash
134
+ * @param realm - Realm
135
+ * @param algorithm - Algorithm to use
136
+ * @returns Hashed username (hex string)
137
+ */
138
+ export declare function hashDigestUsername(username: string, realm: string, algorithm?: DigestAuthAlgorithm): Promise<string>;
139
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,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