forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional.js
More file actions
153 lines (131 loc) · 4.21 KB
/
conditional.js
File metadata and controls
153 lines (131 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Conditional Request Utilities
*
* Implements HTTP conditional request headers:
* - If-Match: Proceed only if ETag matches (for safe updates)
* - If-None-Match: Proceed only if ETag doesn't match (for caching/create-only)
*/
/**
* Normalize an ETag value (remove weak prefix and quotes)
* @param {string} etag
* @returns {string}
*/
function normalizeEtag(etag) {
if (!etag) return '';
// Remove weak prefix W/
let normalized = etag.replace(/^W\//, '');
// Remove surrounding quotes
normalized = normalized.replace(/^"(.*)"$/, '$1');
return normalized;
}
/**
* Parse an If-Match or If-None-Match header value
* @param {string} headerValue
* @returns {string[]} Array of ETags, or ['*'] for wildcard
*/
function parseEtagHeader(headerValue) {
if (!headerValue) return [];
if (headerValue.trim() === '*') return ['*'];
// Split by comma and normalize each ETag
return headerValue.split(',').map(etag => normalizeEtag(etag.trim()));
}
/**
* Check If-Match header
* Returns true if the request should proceed, false if it should be rejected (412)
*
* @param {string} ifMatchHeader - The If-Match header value
* @param {string|null} currentEtag - Current ETag of the resource (null if doesn't exist)
* @returns {{ ok: boolean, status?: number, error?: string }}
*/
export function checkIfMatch(ifMatchHeader, currentEtag) {
if (!ifMatchHeader) {
return { ok: true }; // No If-Match header, proceed
}
const etags = parseEtagHeader(ifMatchHeader);
// If resource doesn't exist, If-Match always fails
if (currentEtag === null) {
return {
ok: false,
status: 412,
error: 'Precondition Failed: Resource does not exist'
};
}
// Wildcard matches any existing resource
if (etags.includes('*')) {
return { ok: true };
}
// Check if any ETag matches
const normalizedCurrent = normalizeEtag(currentEtag);
const matches = etags.some(etag => etag === normalizedCurrent);
if (!matches) {
return {
ok: false,
status: 412,
error: 'Precondition Failed: ETag mismatch'
};
}
return { ok: true };
}
/**
* Check If-None-Match header for GET/HEAD (caching)
* Returns true if the request should proceed, false if 304 Not Modified
*
* @param {string} ifNoneMatchHeader - The If-None-Match header value
* @param {string|null} currentEtag - Current ETag of the resource
* @returns {{ ok: boolean, notModified?: boolean }}
*/
export function checkIfNoneMatchForGet(ifNoneMatchHeader, currentEtag) {
if (!ifNoneMatchHeader || currentEtag === null) {
return { ok: true }; // No header or no resource, proceed
}
const etags = parseEtagHeader(ifNoneMatchHeader);
// Wildcard matches any existing resource
if (etags.includes('*')) {
return { ok: false, notModified: true };
}
// Check if any ETag matches
const normalizedCurrent = normalizeEtag(currentEtag);
const matches = etags.some(etag => etag === normalizedCurrent);
if (matches) {
return { ok: false, notModified: true };
}
return { ok: true };
}
/**
* Check If-None-Match header for PUT/POST (create-only semantics)
* Returns true if the request should proceed, false if 412 Precondition Failed
*
* @param {string} ifNoneMatchHeader - The If-None-Match header value
* @param {string|null} currentEtag - Current ETag of the resource (null if doesn't exist)
* @returns {{ ok: boolean, status?: number, error?: string }}
*/
export function checkIfNoneMatchForWrite(ifNoneMatchHeader, currentEtag) {
if (!ifNoneMatchHeader) {
return { ok: true }; // No header, proceed
}
const etags = parseEtagHeader(ifNoneMatchHeader);
// If-None-Match: * means "only if resource doesn't exist"
if (etags.includes('*')) {
if (currentEtag !== null) {
return {
ok: false,
status: 412,
error: 'Precondition Failed: Resource already exists'
};
}
return { ok: true };
}
// Check if any ETag matches (if so, fail)
if (currentEtag !== null) {
const normalizedCurrent = normalizeEtag(currentEtag);
const matches = etags.some(etag => etag === normalizedCurrent);
if (matches) {
return {
ok: false,
status: 412,
error: 'Precondition Failed: ETag matches'
};
}
}
return { ok: true };
}