javascript-solid-server 0.0.193 → 0.0.194
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/package.json +1 -1
- package/src/handlers/resource.js +85 -13
- package/test/vary-cache-headers.test.js +59 -0
package/package.json
CHANGED
package/src/handlers/resource.js
CHANGED
|
@@ -10,7 +10,8 @@ import {
|
|
|
10
10
|
canAcceptInput,
|
|
11
11
|
toJsonLd,
|
|
12
12
|
fromJsonLd,
|
|
13
|
-
RDF_TYPES
|
|
13
|
+
RDF_TYPES,
|
|
14
|
+
getVaryHeader
|
|
14
15
|
} from '../rdf/conneg.js';
|
|
15
16
|
import { emitChange } from '../notifications/events.js';
|
|
16
17
|
import { checkIfMatch, checkIfNoneMatchForGet, checkIfNoneMatchForWrite } from '../utils/conditional.js';
|
|
@@ -119,6 +120,22 @@ function parseRangeHeader(rangeHeader, fileSize) {
|
|
|
119
120
|
return { start, end };
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Compute a content-type-aware ETag. When mashlib will wrap an RDF
|
|
125
|
+
* resource in HTML, the response body differs from the raw resource,
|
|
126
|
+
* so the ETag must differ too — otherwise browsers confuse cached
|
|
127
|
+
* JSON-LD with the HTML variant despite Vary: Accept (#456).
|
|
128
|
+
*/
|
|
129
|
+
function getMashlibEtag(request, stats, storagePath) {
|
|
130
|
+
const storedType = stats.isDirectory ? 'application/ld+json' : getContentType(storagePath);
|
|
131
|
+
const willServeMashlib =
|
|
132
|
+
shouldServeMashlib(request, request.mashlibEnabled, storedType);
|
|
133
|
+
const effectiveEtag = willServeMashlib
|
|
134
|
+
? stats.etag.replace(/"$/, '-html"')
|
|
135
|
+
: stats.etag;
|
|
136
|
+
return { willServeMashlib, effectiveEtag };
|
|
137
|
+
}
|
|
138
|
+
|
|
122
139
|
/**
|
|
123
140
|
* Handle GET request
|
|
124
141
|
*/
|
|
@@ -134,11 +151,18 @@ export async function handleGet(request, reply) {
|
|
|
134
151
|
return reply.code(404).send({ error: 'Not Found' });
|
|
135
152
|
}
|
|
136
153
|
|
|
137
|
-
|
|
154
|
+
const { willServeMashlib, effectiveEtag } = getMashlibEtag(request, stats, storagePath);
|
|
155
|
+
|
|
156
|
+
// For non-containers, check If-None-Match early using the effective
|
|
157
|
+
// ETag. For containers, defer the check until we know which branch
|
|
158
|
+
// (index.html vs listing vs mashlib) will run — each uses a
|
|
159
|
+
// different ETag source (#456).
|
|
138
160
|
const ifNoneMatch = request.headers['if-none-match'];
|
|
139
|
-
if (ifNoneMatch) {
|
|
140
|
-
const check = checkIfNoneMatchForGet(ifNoneMatch,
|
|
161
|
+
if (ifNoneMatch && !stats.isDirectory) {
|
|
162
|
+
const check = checkIfNoneMatchForGet(ifNoneMatch, effectiveEtag);
|
|
141
163
|
if (!check.ok && check.notModified) {
|
|
164
|
+
reply.header('ETag', effectiveEtag);
|
|
165
|
+
reply.header('Vary', getVaryHeader(request.connegEnabled, request.mashlibEnabled));
|
|
142
166
|
return reply.code(304).send();
|
|
143
167
|
}
|
|
144
168
|
}
|
|
@@ -158,6 +182,17 @@ export async function handleGet(request, reply) {
|
|
|
158
182
|
const content = await storage.read(indexPath);
|
|
159
183
|
const indexStats = await storage.stat(indexPath);
|
|
160
184
|
|
|
185
|
+
// Deferred 304 check for index.html containers (#456)
|
|
186
|
+
const indexEtag = indexStats?.etag || stats.etag;
|
|
187
|
+
if (ifNoneMatch) {
|
|
188
|
+
const check = checkIfNoneMatchForGet(ifNoneMatch, indexEtag);
|
|
189
|
+
if (!check.ok && check.notModified) {
|
|
190
|
+
reply.header('ETag', indexEtag);
|
|
191
|
+
reply.header('Vary', getVaryHeader(connegEnabled, request.mashlibEnabled));
|
|
192
|
+
return reply.code(304).send();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
161
196
|
// Pick the negotiated RDF type using q-aware Accept parsing. The
|
|
162
197
|
// naive `acceptHeader.includes('text/turtle')` we used to do here
|
|
163
198
|
// ignored q-weights — `Accept: application/ld+json, text/turtle;q=0.1`
|
|
@@ -251,6 +286,16 @@ export async function handleGet(request, reply) {
|
|
|
251
286
|
}
|
|
252
287
|
|
|
253
288
|
// No index.html, return JSON-LD container listing
|
|
289
|
+
// Deferred 304 check for container listings (#456)
|
|
290
|
+
if (ifNoneMatch) {
|
|
291
|
+
const check = checkIfNoneMatchForGet(ifNoneMatch, effectiveEtag);
|
|
292
|
+
if (!check.ok && check.notModified) {
|
|
293
|
+
reply.header('ETag', effectiveEtag);
|
|
294
|
+
reply.header('Vary', getVaryHeader(connegEnabled, request.mashlibEnabled));
|
|
295
|
+
return reply.code(304).send();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
254
299
|
const entries = await storage.listContainer(storagePath);
|
|
255
300
|
const jsonLd = generateContainerJsonLd(resourceUrl, entries || []);
|
|
256
301
|
|
|
@@ -273,7 +318,7 @@ export async function handleGet(request, reply) {
|
|
|
273
318
|
);
|
|
274
319
|
const headers = getAllHeaders({
|
|
275
320
|
isContainer: true,
|
|
276
|
-
etag:
|
|
321
|
+
etag: effectiveEtag,
|
|
277
322
|
contentType: 'text/html',
|
|
278
323
|
origin,
|
|
279
324
|
resourceUrl,
|
|
@@ -410,7 +455,7 @@ export async function handleGet(request, reply) {
|
|
|
410
455
|
);
|
|
411
456
|
const headers = getAllHeaders({
|
|
412
457
|
isContainer: false,
|
|
413
|
-
etag:
|
|
458
|
+
etag: effectiveEtag,
|
|
414
459
|
contentType: 'text/html',
|
|
415
460
|
origin,
|
|
416
461
|
resourceUrl,
|
|
@@ -595,6 +640,8 @@ export async function handleHead(request, reply) {
|
|
|
595
640
|
const origin = request.headers.origin;
|
|
596
641
|
const connegEnabled = request.connegEnabled || false;
|
|
597
642
|
let contentType;
|
|
643
|
+
let headEtag = stats.etag;
|
|
644
|
+
let isMashlibResponse = false;
|
|
598
645
|
|
|
599
646
|
if (stats.isDirectory) {
|
|
600
647
|
const indexPath = storagePath.endsWith('/') ? `${storagePath}index.html` : `${storagePath}/index.html`;
|
|
@@ -615,9 +662,6 @@ export async function handleHead(request, reply) {
|
|
|
615
662
|
if (wantsTurtle) {
|
|
616
663
|
contentType = 'text/turtle';
|
|
617
664
|
} else if (wantsJsonLd) {
|
|
618
|
-
// For an index.html container, only override to JSON-LD if the
|
|
619
|
-
// Accept header explicitly asked for JSON; otherwise fall back
|
|
620
|
-
// to text/html so HEAD matches the index.html that GET serves.
|
|
621
665
|
const explicitJson = EXPLICIT_JSON_RE.test(acceptHeader);
|
|
622
666
|
contentType = (indexExists && !explicitJson) ? 'text/html' : 'application/ld+json';
|
|
623
667
|
} else {
|
|
@@ -628,20 +672,48 @@ export async function handleHead(request, reply) {
|
|
|
628
672
|
} else {
|
|
629
673
|
contentType = 'application/ld+json';
|
|
630
674
|
}
|
|
675
|
+
|
|
676
|
+
if (indexExists) {
|
|
677
|
+
// Mirror GET: containers with index.html use the index file's ETag
|
|
678
|
+
const indexStats = await storage.stat(indexPath);
|
|
679
|
+
headEtag = indexStats?.etag || stats.etag;
|
|
680
|
+
} else if (shouldServeMashlib(request, request.mashlibEnabled, 'application/ld+json')) {
|
|
681
|
+
// Container listing via mashlib — suffix the ETag (#456)
|
|
682
|
+
headEtag = stats.etag.replace(/"$/, '-html"');
|
|
683
|
+
contentType = 'text/html';
|
|
684
|
+
isMashlibResponse = true;
|
|
685
|
+
}
|
|
631
686
|
} else {
|
|
632
|
-
|
|
687
|
+
const { willServeMashlib, effectiveEtag } = getMashlibEtag(request, stats, storagePath);
|
|
688
|
+
headEtag = effectiveEtag;
|
|
689
|
+
isMashlibResponse = willServeMashlib;
|
|
690
|
+
contentType = willServeMashlib ? 'text/html' : getContentType(storagePath);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// Check If-None-Match using the final ETag (#456)
|
|
694
|
+
const ifNoneMatch = request.headers['if-none-match'];
|
|
695
|
+
if (ifNoneMatch) {
|
|
696
|
+
const check = checkIfNoneMatchForGet(ifNoneMatch, headEtag);
|
|
697
|
+
if (!check.ok && check.notModified) {
|
|
698
|
+
reply.header('ETag', headEtag);
|
|
699
|
+
reply.header('Vary', getVaryHeader(connegEnabled, request.mashlibEnabled));
|
|
700
|
+
return reply.code(304).send();
|
|
701
|
+
}
|
|
633
702
|
}
|
|
634
703
|
|
|
635
704
|
const headers = getAllHeaders({
|
|
636
705
|
isContainer: stats.isDirectory,
|
|
637
|
-
etag:
|
|
706
|
+
etag: headEtag,
|
|
638
707
|
contentType,
|
|
639
708
|
origin,
|
|
640
709
|
resourceUrl,
|
|
641
|
-
connegEnabled
|
|
710
|
+
connegEnabled,
|
|
711
|
+
mashlibEnabled: request.mashlibEnabled
|
|
642
712
|
});
|
|
643
713
|
|
|
644
|
-
|
|
714
|
+
// Content-Length: only set when the file size matches the response body.
|
|
715
|
+
// Mashlib HTML and containers are dynamically generated.
|
|
716
|
+
if (!stats.isDirectory && !isMashlibResponse) {
|
|
645
717
|
headers['Content-Length'] = stats.size;
|
|
646
718
|
}
|
|
647
719
|
|
|
@@ -83,6 +83,65 @@ describe('Vary / Cache-Control consistency (#315)', () => {
|
|
|
83
83
|
}
|
|
84
84
|
});
|
|
85
85
|
|
|
86
|
+
it('mashlib HTML ETag differs from raw RDF ETag (#456)', async () => {
|
|
87
|
+
const htmlRes = await request('/varytest/public/card.jsonld', {
|
|
88
|
+
headers: { Accept: 'text/html,*/*;q=0.8' }
|
|
89
|
+
});
|
|
90
|
+
const jsonRes = await request('/varytest/public/card.jsonld', {
|
|
91
|
+
headers: { Accept: 'application/ld+json' }
|
|
92
|
+
});
|
|
93
|
+
const htmlEtag = htmlRes.headers.get('etag');
|
|
94
|
+
const jsonEtag = jsonRes.headers.get('etag');
|
|
95
|
+
assert.ok(htmlEtag, 'HTML variant should have ETag');
|
|
96
|
+
assert.ok(jsonEtag, 'JSON-LD variant should have ETag');
|
|
97
|
+
assert.notStrictEqual(htmlEtag, jsonEtag,
|
|
98
|
+
'mashlib HTML and raw JSON-LD must have different ETags');
|
|
99
|
+
assert.ok(htmlEtag.endsWith('-html"'),
|
|
100
|
+
`HTML ETag should end with -html", got: ${htmlEtag}`);
|
|
101
|
+
assert.ok(!jsonEtag.includes('-html'),
|
|
102
|
+
`JSON-LD ETag should not contain -html, got: ${jsonEtag}`);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('If-None-Match with HTML ETag does not 304 the JSON-LD variant (#456)', async () => {
|
|
106
|
+
const htmlRes = await request('/varytest/public/card.jsonld', {
|
|
107
|
+
headers: { Accept: 'text/html,*/*;q=0.8' }
|
|
108
|
+
});
|
|
109
|
+
const htmlEtag = htmlRes.headers.get('etag');
|
|
110
|
+
// Use the HTML ETag to request JSON-LD — should NOT get 304
|
|
111
|
+
const jsonRes = await request('/varytest/public/card.jsonld', {
|
|
112
|
+
headers: { Accept: 'application/ld+json', 'If-None-Match': htmlEtag }
|
|
113
|
+
});
|
|
114
|
+
assert.strictEqual(jsonRes.status, 200,
|
|
115
|
+
'JSON-LD request with HTML ETag should get 200, not 304');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('If-None-Match with JSON-LD ETag does not 304 the HTML variant (#456)', async () => {
|
|
119
|
+
const jsonRes = await request('/varytest/public/card.jsonld', {
|
|
120
|
+
headers: { Accept: 'application/ld+json' }
|
|
121
|
+
});
|
|
122
|
+
const jsonEtag = jsonRes.headers.get('etag');
|
|
123
|
+
// Use the JSON-LD ETag to request HTML — should NOT get 304
|
|
124
|
+
const htmlRes = await request('/varytest/public/card.jsonld', {
|
|
125
|
+
headers: { Accept: 'text/html,*/*;q=0.8', 'If-None-Match': jsonEtag }
|
|
126
|
+
});
|
|
127
|
+
assert.strictEqual(htmlRes.status, 200,
|
|
128
|
+
'HTML request with JSON-LD ETag should get 200, not 304');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('HEAD returns same ETag as GET for each variant (#456)', async () => {
|
|
132
|
+
for (const accept of ['text/html,*/*;q=0.8', 'application/ld+json']) {
|
|
133
|
+
const getRes = await request('/varytest/public/card.jsonld', {
|
|
134
|
+
headers: { Accept: accept }
|
|
135
|
+
});
|
|
136
|
+
const headRes = await request('/varytest/public/card.jsonld', {
|
|
137
|
+
method: 'HEAD',
|
|
138
|
+
headers: { Accept: accept }
|
|
139
|
+
});
|
|
140
|
+
assert.strictEqual(headRes.headers.get('etag'), getRes.headers.get('etag'),
|
|
141
|
+
`HEAD and GET ETags must match for Accept: ${accept}`);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
86
145
|
it('container index.html data-island variants also carry revalidating Cache-Control', async () => {
|
|
87
146
|
// Publish an index.html with a JSON-LD data island; conneg should extract
|
|
88
147
|
// and serve it as Turtle/JSON-LD. Those variants were missing
|