@mdream/js 1.0.7 → 1.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.
- package/dist/negotiate.d.mts +22 -10
- package/dist/negotiate.mjs +65 -18
- package/package.json +1 -1
package/dist/negotiate.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
type ContentNegotiationResult = 'markdown' | 'html' | 'not-acceptable';
|
|
1
2
|
interface AcceptEntry {
|
|
2
3
|
type: string;
|
|
3
4
|
q: number;
|
|
@@ -9,16 +10,27 @@ interface AcceptEntry {
|
|
|
9
10
|
*/
|
|
10
11
|
declare function parseAcceptHeader(accept: string): AcceptEntry[];
|
|
11
12
|
/**
|
|
12
|
-
*
|
|
13
|
+
* Perform RFC 7231 content negotiation for HTML vs Markdown.
|
|
13
14
|
*
|
|
14
|
-
*
|
|
15
|
-
* -
|
|
16
|
-
* -
|
|
17
|
-
* -
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
15
|
+
* Resolution rules:
|
|
16
|
+
* - `Sec-Fetch-Dest: document` always returns `'html'` (browser navigation).
|
|
17
|
+
* - Missing or empty Accept header returns `'html'` (server picks default).
|
|
18
|
+
* - q=0 entries are treated as explicit rejections and ignored for matching
|
|
19
|
+
* (but still count towards "something was listed").
|
|
20
|
+
* - `text/markdown` and `text/plain` are the markdown-capable types.
|
|
21
|
+
* - `text/html` and `application/xhtml+xml` are the html-capable types.
|
|
22
|
+
* - `*_/_*` and `text/*` are wildcards; they satisfy 406 but never on their
|
|
23
|
+
* own tip negotiation towards markdown (preserves OG crawler behavior).
|
|
24
|
+
* - If nothing in the Accept header can be served (no explicit match, no
|
|
25
|
+
* wildcard), returns `'not-acceptable'` so the caller can send 406.
|
|
26
|
+
* - Otherwise, compares best markdown entry vs best html-or-wildcard entry
|
|
27
|
+
* by q, then by position.
|
|
28
|
+
*/
|
|
29
|
+
declare function negotiateContent(acceptHeader?: string, secFetchDest?: string): ContentNegotiationResult;
|
|
30
|
+
/**
|
|
31
|
+
* Determine if a client prefers markdown over HTML. Convenience wrapper over
|
|
32
|
+
* {@link negotiateContent}; treats `'not-acceptable'` the same as `'html'`
|
|
33
|
+
* (callers that want 406 semantics should use `negotiateContent` directly).
|
|
22
34
|
*/
|
|
23
35
|
declare function shouldServeMarkdown(acceptHeader?: string, secFetchDest?: string): boolean;
|
|
24
|
-
export { parseAcceptHeader, shouldServeMarkdown };
|
|
36
|
+
export { ContentNegotiationResult, negotiateContent, parseAcceptHeader, shouldServeMarkdown };
|
package/dist/negotiate.mjs
CHANGED
|
@@ -28,18 +28,25 @@ function parseAcceptHeader(accept) {
|
|
|
28
28
|
}
|
|
29
29
|
return entries;
|
|
30
30
|
}
|
|
31
|
-
function
|
|
32
|
-
if (secFetchDest === "document") return
|
|
31
|
+
function negotiateContent(acceptHeader, secFetchDest) {
|
|
32
|
+
if (secFetchDest === "document") return "html";
|
|
33
33
|
const accept = acceptHeader || "";
|
|
34
|
-
if (!accept) return
|
|
35
|
-
const parts = accept.split(",");
|
|
34
|
+
if (!accept) return "html";
|
|
36
35
|
let bestMdQ = -1;
|
|
37
36
|
let bestMdPos = -1;
|
|
38
|
-
let
|
|
39
|
-
let
|
|
37
|
+
let bestHtmlQ = -1;
|
|
38
|
+
let bestHtmlPos = -1;
|
|
39
|
+
let bestWildcardQ = -1;
|
|
40
|
+
let bestWildcardPos = -1;
|
|
41
|
+
let sawAnyEntry = false;
|
|
42
|
+
let sawAcceptable = false;
|
|
43
|
+
let rejectedMd = false;
|
|
44
|
+
let rejectedHtml = false;
|
|
45
|
+
const parts = accept.split(",");
|
|
40
46
|
for (let i = 0; i < parts.length; i++) {
|
|
41
47
|
const part = parts[i].trim();
|
|
42
48
|
if (!part) continue;
|
|
49
|
+
sawAnyEntry = true;
|
|
43
50
|
const semicolonIdx = part.indexOf(";");
|
|
44
51
|
let type;
|
|
45
52
|
let q = 1;
|
|
@@ -47,7 +54,14 @@ function shouldServeMarkdown(acceptHeader, secFetchDest) {
|
|
|
47
54
|
else {
|
|
48
55
|
type = part.slice(0, semicolonIdx).trim();
|
|
49
56
|
const paramStr = part.slice(semicolonIdx + 1);
|
|
50
|
-
|
|
57
|
+
let qIdx = -1;
|
|
58
|
+
for (let j = 0; j < paramStr.length - 1; j++) {
|
|
59
|
+
const c = paramStr.charCodeAt(j);
|
|
60
|
+
if ((c === 113 || c === 81) && paramStr.charCodeAt(j + 1) === 61) {
|
|
61
|
+
qIdx = j;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
51
65
|
if (qIdx !== -1) {
|
|
52
66
|
const qStart = qIdx + 2;
|
|
53
67
|
let qEnd = qStart;
|
|
@@ -55,20 +69,53 @@ function shouldServeMarkdown(acceptHeader, secFetchDest) {
|
|
|
55
69
|
q = +paramStr.slice(qStart, qEnd) || 0;
|
|
56
70
|
}
|
|
57
71
|
}
|
|
58
|
-
|
|
59
|
-
|
|
72
|
+
const normalized = type.toLowerCase();
|
|
73
|
+
if (normalized === "text/markdown" || normalized === "text/plain") {
|
|
74
|
+
if (q === 0) {
|
|
75
|
+
rejectedMd = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
sawAcceptable = true;
|
|
79
|
+
if (q > bestMdQ || q === bestMdQ && bestMdPos === -1) {
|
|
60
80
|
bestMdQ = q;
|
|
61
81
|
bestMdPos = i;
|
|
62
82
|
}
|
|
63
|
-
} else if (
|
|
64
|
-
|
|
65
|
-
|
|
83
|
+
} else if (normalized === "text/html" || normalized === "application/xhtml+xml") {
|
|
84
|
+
if (q === 0) {
|
|
85
|
+
rejectedHtml = true;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
sawAcceptable = true;
|
|
89
|
+
if (q > bestHtmlQ || q === bestHtmlQ && bestHtmlPos === -1) {
|
|
90
|
+
bestHtmlQ = q;
|
|
91
|
+
bestHtmlPos = i;
|
|
92
|
+
}
|
|
93
|
+
} else if (normalized === "*/*" || normalized === "text/*") {
|
|
94
|
+
if (q === 0) continue;
|
|
95
|
+
sawAcceptable = true;
|
|
96
|
+
if (q > bestWildcardQ || q === bestWildcardQ && bestWildcardPos === -1) {
|
|
97
|
+
bestWildcardQ = q;
|
|
98
|
+
bestWildcardPos = i;
|
|
99
|
+
}
|
|
66
100
|
}
|
|
67
101
|
}
|
|
68
|
-
if (
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
102
|
+
if (sawAnyEntry && !sawAcceptable) return "not-acceptable";
|
|
103
|
+
if (bestMdPos === -1 && !rejectedMd && bestWildcardPos !== -1) {
|
|
104
|
+
bestMdQ = bestWildcardQ;
|
|
105
|
+
bestMdPos = bestWildcardPos;
|
|
106
|
+
}
|
|
107
|
+
if (bestHtmlPos === -1 && !rejectedHtml && bestWildcardPos !== -1) {
|
|
108
|
+
bestHtmlQ = bestWildcardQ;
|
|
109
|
+
bestHtmlPos = bestWildcardPos;
|
|
110
|
+
}
|
|
111
|
+
if (bestMdPos === -1 && bestHtmlPos === -1) return "not-acceptable";
|
|
112
|
+
if (bestMdPos === -1) return "html";
|
|
113
|
+
if (bestHtmlPos === -1) return "markdown";
|
|
114
|
+
if (bestMdQ > bestHtmlQ) return "markdown";
|
|
115
|
+
if (bestMdQ === bestHtmlQ && bestMdPos < bestHtmlPos) return "markdown";
|
|
116
|
+
return "html";
|
|
117
|
+
}
|
|
118
|
+
function shouldServeMarkdown(acceptHeader, secFetchDest) {
|
|
119
|
+
return negotiateContent(acceptHeader, secFetchDest) === "markdown";
|
|
73
120
|
}
|
|
74
|
-
export { parseAcceptHeader, shouldServeMarkdown };
|
|
121
|
+
export { negotiateContent, parseAcceptHeader, shouldServeMarkdown };
|
package/package.json
CHANGED