druxt 0.25.0-dev.20260922103051 → 0.25.0-dev.20260927224155
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/druxt.esm.js +196 -29
- package/dist/druxt.ssr.js +196 -29
- package/package.json +2 -2
package/dist/druxt.esm.js
CHANGED
|
@@ -8,7 +8,113 @@ import merge from 'deepmerge';
|
|
|
8
8
|
import md5 from 'md5';
|
|
9
9
|
import Vue from 'vue';
|
|
10
10
|
|
|
11
|
+
const SCOPES = Symbol.for("druxt.processCache");
|
|
12
|
+
const scopes = globalThis[SCOPES] || (globalThis[SCOPES] = new Map());
|
|
13
|
+
const credentialed = new WeakSet();
|
|
14
|
+
const watched = new WeakSet();
|
|
15
|
+
const runtime = { isServer: () => typeof window === "undefined" };
|
|
16
|
+
const DRUPAL_SESSION_COOKIE = "S?SESS[0-9a-f]+";
|
|
17
|
+
const consumerId = (axios) => {
|
|
18
|
+
const headers = ((axios || {}).defaults || {}).headers || {};
|
|
19
|
+
for (const set of [headers, headers.common, headers.get]) {
|
|
20
|
+
const match = Object.entries(set || {}).find(([name]) => name.toLowerCase() === "x-consumer-id");
|
|
21
|
+
if (match && match[1])
|
|
22
|
+
return String(match[1]);
|
|
23
|
+
}
|
|
24
|
+
return "";
|
|
25
|
+
};
|
|
26
|
+
const headersHaveCredentials = (headers, sessionCookie) => {
|
|
27
|
+
const session = new RegExp(`(?:^|;\\s*)(?:${sessionCookie})=`, "i");
|
|
28
|
+
return Object.entries(headers || {}).some(([name, value]) => {
|
|
29
|
+
if (!value || typeof value === "object")
|
|
30
|
+
return false;
|
|
31
|
+
const header = name.toLowerCase();
|
|
32
|
+
return header === "authorization" || header === "cookie" && session.test(String(value));
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
const configHasCredentials = (config, sessionCookie = DRUPAL_SESSION_COOKIE) => {
|
|
36
|
+
if (!config)
|
|
37
|
+
return false;
|
|
38
|
+
if (config.auth)
|
|
39
|
+
return true;
|
|
40
|
+
const headers = config.headers || {};
|
|
41
|
+
return headersHaveCredentials(headers, sessionCookie) || headersHaveCredentials(headers.common, sessionCookie) || headersHaveCredentials(headers.get, sessionCookie);
|
|
42
|
+
};
|
|
43
|
+
const hasCredentials = (axios, sessionCookie = DRUPAL_SESSION_COOKIE) => {
|
|
44
|
+
if (!axios)
|
|
45
|
+
return false;
|
|
46
|
+
if (credentialed.has(axios))
|
|
47
|
+
return true;
|
|
48
|
+
return configHasCredentials(axios.defaults, sessionCookie);
|
|
49
|
+
};
|
|
50
|
+
const watchCredentials = (axios, sessionCookie = DRUPAL_SESSION_COOKIE) => {
|
|
51
|
+
const interceptors = ((axios || {}).interceptors || {}).response;
|
|
52
|
+
if (!interceptors || typeof interceptors.use !== "function" || watched.has(axios))
|
|
53
|
+
return;
|
|
54
|
+
watched.add(axios);
|
|
55
|
+
const mark = (config) => {
|
|
56
|
+
if (configHasCredentials(config, sessionCookie))
|
|
57
|
+
credentialed.add(axios);
|
|
58
|
+
};
|
|
59
|
+
interceptors.use((response) => {
|
|
60
|
+
mark((response || {}).config);
|
|
61
|
+
return response;
|
|
62
|
+
}, (error) => {
|
|
63
|
+
mark((error || {}).config);
|
|
64
|
+
return Promise.reject(error);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
const parseCacheLifetime = (headers) => {
|
|
68
|
+
const header = (name) => Object.entries(headers || {}).filter(([key]) => key.toLowerCase() === name).map(([, value]) => String(value)).join(", ");
|
|
69
|
+
const directives = header("cache-control").toLowerCase().split(",").map((d) => d.trim());
|
|
70
|
+
if (directives.some((d) => ["private", "no-store", "no-cache"].includes(d)))
|
|
71
|
+
return 0;
|
|
72
|
+
const vary = header("vary").toLowerCase().split(",").map((v) => v.trim()).filter(Boolean);
|
|
73
|
+
if (vary.some((v) => !["cookie", "x-consumer-id", "accept-encoding"].includes(v)))
|
|
74
|
+
return 0;
|
|
75
|
+
const seconds = (name) => {
|
|
76
|
+
const directive = directives.find((d) => d.startsWith(`${name}=`));
|
|
77
|
+
const value = directive ? directive.slice(name.length + 1) : "";
|
|
78
|
+
return /^\d+$/.test(value) ? Number(value) : NaN;
|
|
79
|
+
};
|
|
80
|
+
const lifetime = directives.some((d) => d.startsWith("s-maxage=")) ? seconds("s-maxage") : seconds("max-age");
|
|
81
|
+
if (Number.isNaN(lifetime))
|
|
82
|
+
return 0;
|
|
83
|
+
const age = parseInt(header("age"), 10) || 0;
|
|
84
|
+
return Math.max(0, lifetime - age);
|
|
85
|
+
};
|
|
86
|
+
const processCache = (scope, { axios, ttl, sessionCookie } = {}) => {
|
|
87
|
+
if (!runtime.isServer() || hasCredentials(axios, sessionCookie))
|
|
88
|
+
return null;
|
|
89
|
+
if (!scopes.has(scope))
|
|
90
|
+
scopes.set(scope, new Map());
|
|
91
|
+
const consumers = scopes.get(scope);
|
|
92
|
+
const consumer = consumerId(axios);
|
|
93
|
+
if (!consumers.has(consumer))
|
|
94
|
+
consumers.set(consumer, new Map());
|
|
95
|
+
const entries = consumers.get(consumer);
|
|
96
|
+
const cap = (seconds) => typeof ttl === "number" ? Math.min(seconds, ttl) : seconds;
|
|
97
|
+
return {
|
|
98
|
+
get(key) {
|
|
99
|
+
const entry = entries.get(key);
|
|
100
|
+
if (!entry)
|
|
101
|
+
return void 0;
|
|
102
|
+
if (Date.now() - entry.created >= cap(entry.seconds) * 1e3)
|
|
103
|
+
return void 0;
|
|
104
|
+
return entry.value;
|
|
105
|
+
},
|
|
106
|
+
set(key, value, seconds) {
|
|
107
|
+
if (cap(seconds) > 0)
|
|
108
|
+
entries.set(key, { value, seconds: cap(seconds), created: Date.now() });
|
|
109
|
+
else
|
|
110
|
+
entries.delete(key);
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
11
116
|
const indexCache = new WeakMap();
|
|
117
|
+
const lifetimes = new WeakMap();
|
|
12
118
|
class DruxtClient {
|
|
13
119
|
constructor(baseUrl, options = {}) {
|
|
14
120
|
this.log = consola.create({ defaults: {
|
|
@@ -41,6 +147,8 @@ class DruxtClient {
|
|
|
41
147
|
jsonapiResourceConfig: "jsonapi_resource_config--jsonapi_resource_config",
|
|
42
148
|
...options
|
|
43
149
|
};
|
|
150
|
+
if (this.options.cache)
|
|
151
|
+
watchCredentials(this.axios, this.options.cache.sessionCookie);
|
|
44
152
|
const indexKey = JSON.stringify([baseUrl, this.options.endpoint, this.options.jsonapiResourceConfig]);
|
|
45
153
|
this.indexKey = indexKey;
|
|
46
154
|
if (!indexCache.has(this.axios))
|
|
@@ -49,6 +157,18 @@ class DruxtClient {
|
|
|
49
157
|
this.indexRequests = cache.requests;
|
|
50
158
|
this.index = cache.index[indexKey] || (cache.index[indexKey] = {});
|
|
51
159
|
}
|
|
160
|
+
processCache(scope) {
|
|
161
|
+
if (!this.options.cache)
|
|
162
|
+
return null;
|
|
163
|
+
const { ttl, sessionCookie } = this.options.cache;
|
|
164
|
+
return processCache(scope, { axios: this.axios, ttl, sessionCookie });
|
|
165
|
+
}
|
|
166
|
+
cacheLifetime(document) {
|
|
167
|
+
const record = document && typeof document === "object" && lifetimes.get(document);
|
|
168
|
+
if (!record)
|
|
169
|
+
return 0;
|
|
170
|
+
return Math.max(0, record.seconds - Math.floor((Date.now() - record.at) / 1e3));
|
|
171
|
+
}
|
|
52
172
|
addHeaders(headers) {
|
|
53
173
|
if (typeof headers === "undefined") {
|
|
54
174
|
return false;
|
|
@@ -142,6 +262,8 @@ class DruxtClient {
|
|
|
142
262
|
async get(url, options) {
|
|
143
263
|
try {
|
|
144
264
|
const res = await this.axios.get(url, options);
|
|
265
|
+
if (res && res.data && typeof res.data === "object")
|
|
266
|
+
lifetimes.set(res.data, { seconds: parseCacheLifetime(res.headers), at: Date.now() });
|
|
145
267
|
return res;
|
|
146
268
|
} catch (err) {
|
|
147
269
|
this.error(err, { url });
|
|
@@ -168,15 +290,26 @@ class DruxtClient {
|
|
|
168
290
|
return collections;
|
|
169
291
|
}
|
|
170
292
|
async getIndex(resource, prefix) {
|
|
293
|
+
if (!(this.index || {})[prefix]) {
|
|
294
|
+
const shared = this.processCache("index");
|
|
295
|
+
const sharedKey = [this.indexKey, prefix || ""].join(":");
|
|
296
|
+
const stored = shared && shared.get(sharedKey);
|
|
297
|
+
if (stored)
|
|
298
|
+
this.index[prefix] = stored;
|
|
299
|
+
}
|
|
171
300
|
if (!(this.index || {})[prefix]) {
|
|
172
301
|
const key = [this.indexKey, prefix || ""].join(":");
|
|
173
302
|
const request = this.indexRequests[key] || (this.indexRequests[key] = this.fetchIndex(prefix));
|
|
303
|
+
let lifetime;
|
|
174
304
|
try {
|
|
175
|
-
await request;
|
|
305
|
+
lifetime = await request;
|
|
176
306
|
} finally {
|
|
177
307
|
if (this.indexRequests[key] === request)
|
|
178
308
|
delete this.indexRequests[key];
|
|
179
309
|
}
|
|
310
|
+
const shared = this.processCache("index");
|
|
311
|
+
if (shared && this.index[prefix])
|
|
312
|
+
shared.set(key, this.index[prefix], lifetime);
|
|
180
313
|
}
|
|
181
314
|
return resource ? this.index[prefix][resource] || false : this.index[prefix];
|
|
182
315
|
}
|
|
@@ -193,10 +326,13 @@ class DruxtClient {
|
|
|
193
326
|
value.href = value.href.replace(baseUrl, "");
|
|
194
327
|
return [key, value];
|
|
195
328
|
}));
|
|
329
|
+
const documents = [data];
|
|
196
330
|
if (index[this.options.jsonapiResourceConfig]) {
|
|
197
331
|
let resources = [];
|
|
198
332
|
try {
|
|
199
|
-
|
|
333
|
+
const config = (await this.get(index[this.options.jsonapiResourceConfig].href)).data;
|
|
334
|
+
resources = config.data;
|
|
335
|
+
documents.push(config);
|
|
200
336
|
} catch (err) {
|
|
201
337
|
this.log.warn(err.message);
|
|
202
338
|
}
|
|
@@ -217,6 +353,7 @@ class DruxtClient {
|
|
|
217
353
|
}
|
|
218
354
|
}
|
|
219
355
|
this.index[prefix] = index;
|
|
356
|
+
return Math.min(...documents.map((document) => this.cacheLifetime(document)));
|
|
220
357
|
}
|
|
221
358
|
async getRelated(type, id, related, query, prefix = "") {
|
|
222
359
|
if (!id || !type || !related) {
|
|
@@ -263,7 +400,7 @@ class DruxtClient {
|
|
|
263
400
|
}
|
|
264
401
|
|
|
265
402
|
var name = "druxt";
|
|
266
|
-
var version = "0.25.0-dev.
|
|
403
|
+
var version = "0.25.0-dev.20260927224155";
|
|
267
404
|
var description = "The Fully Decoupled Drupal Framework for Nuxt.js.";
|
|
268
405
|
var keywords = [
|
|
269
406
|
"cms",
|
|
@@ -322,7 +459,7 @@ var dependencies = {
|
|
|
322
459
|
};
|
|
323
460
|
var devDependencies = {
|
|
324
461
|
"@storybook/addon-docs": "^6.5.16",
|
|
325
|
-
"druxt-test-utils": "1.0.0-dev.
|
|
462
|
+
"druxt-test-utils": "1.0.0-dev.20260927224155"
|
|
326
463
|
};
|
|
327
464
|
var peerDependencies = {
|
|
328
465
|
axios: "0.28.0",
|
|
@@ -365,6 +502,9 @@ const DruxtNuxtModule = async function(moduleOptions = {}) {
|
|
|
365
502
|
...moduleOptions,
|
|
366
503
|
...(this.options || {}).druxt
|
|
367
504
|
};
|
|
505
|
+
if (options.cache !== false) {
|
|
506
|
+
options.cache = this.options.dev ? false : { ...options.cache };
|
|
507
|
+
}
|
|
368
508
|
this.options.baseUrl = options.baseUrl = options.baseUrl.endsWith("/") ? options.baseUrl.slice(0, -1) : options.baseUrl;
|
|
369
509
|
this.options.endpoint = options.endpoint = options.endpoint.startsWith("/") ? options.endpoint : `/${options.endpoint}`;
|
|
370
510
|
const druxt = new DruxtClient(options.baseUrl, {
|
|
@@ -496,16 +636,45 @@ const dehydrateResources = ({ commit, queryObject, resources, prefix }) => {
|
|
|
496
636
|
return { id: data.id, type: data.type };
|
|
497
637
|
});
|
|
498
638
|
};
|
|
639
|
+
const collectionHash = (query) => {
|
|
640
|
+
if (!query)
|
|
641
|
+
return "_default";
|
|
642
|
+
const queryObject = getDrupalJsonApiParams(query).getQueryObject();
|
|
643
|
+
return md5(JSON.stringify({ ...queryObject, fields: {}, include: [] }));
|
|
644
|
+
};
|
|
645
|
+
const flush = (tree, key, keys, leaf) => {
|
|
646
|
+
const byKey = tree[key];
|
|
647
|
+
if (!byKey)
|
|
648
|
+
return;
|
|
649
|
+
if (!keys.length && leaf === void 0)
|
|
650
|
+
return Vue.delete(tree, key);
|
|
651
|
+
for (const k of keys.length ? keys : Object.keys(byKey)) {
|
|
652
|
+
if (!byKey[k])
|
|
653
|
+
continue;
|
|
654
|
+
if (leaf !== void 0)
|
|
655
|
+
Vue.delete(byKey[k], leaf);
|
|
656
|
+
else
|
|
657
|
+
Vue.delete(byKey, k);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
499
660
|
const DruxtStore = ({ store }) => {
|
|
500
661
|
if (typeof store === "undefined") {
|
|
501
662
|
throw new TypeError("Vuex store not found.");
|
|
502
663
|
}
|
|
503
664
|
const namespace = "druxt";
|
|
504
665
|
const inFlight = new Map();
|
|
666
|
+
const generation = { value: 0 };
|
|
667
|
+
const flushInFlight = () => {
|
|
668
|
+
inFlight.clear();
|
|
669
|
+
generation.value += 1;
|
|
670
|
+
};
|
|
505
671
|
const share = (key, request) => {
|
|
506
672
|
if (!inFlight.has(key)) {
|
|
507
|
-
const clear = () => inFlight.delete(key);
|
|
508
673
|
const promise = request();
|
|
674
|
+
const clear = () => {
|
|
675
|
+
if (inFlight.get(key) === promise)
|
|
676
|
+
inFlight.delete(key);
|
|
677
|
+
};
|
|
509
678
|
inFlight.set(key, promise);
|
|
510
679
|
promise.then(clear, clear);
|
|
511
680
|
}
|
|
@@ -560,31 +729,23 @@ const DruxtStore = ({ store }) => {
|
|
|
560
729
|
resource = merge(state.resources[type][id][prefix] || {}, resource, { arrayMerge: (dst, src) => src });
|
|
561
730
|
Vue.set(state.resources[type][id], prefix, resource);
|
|
562
731
|
},
|
|
563
|
-
flushCollection(state, { type, hash, prefix }) {
|
|
732
|
+
flushCollection(state, { type, hash, query, prefix } = {}) {
|
|
733
|
+
flushInFlight();
|
|
564
734
|
if (!type)
|
|
565
|
-
Vue.set(state, "collections", {});
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
else if (type && hash && !prefix)
|
|
569
|
-
Vue.set(state.collections[type], hash, {});
|
|
570
|
-
else if (type && hash && prefix)
|
|
571
|
-
Vue.set(state.collections[type][hash], prefix, {});
|
|
735
|
+
return Vue.set(state, "collections", {});
|
|
736
|
+
const key = hash !== void 0 ? hash : query !== void 0 ? collectionHash(query) : void 0;
|
|
737
|
+
flush(state.collections, type, key !== void 0 ? [key] : [], prefix);
|
|
572
738
|
},
|
|
573
|
-
flushResource(state, { type, id, prefix }) {
|
|
739
|
+
flushResource(state, { type, id, prefix } = {}) {
|
|
740
|
+
flushInFlight();
|
|
574
741
|
if (!type)
|
|
575
|
-
Vue.set(state, "resources", {});
|
|
576
|
-
|
|
577
|
-
Vue.set(state.resources, type, {});
|
|
578
|
-
else if (type && id && !prefix)
|
|
579
|
-
Vue.set(state.resources[type], id, {});
|
|
580
|
-
else if (type && id && prefix)
|
|
581
|
-
Vue.set(state.resources[type][id], prefix, {});
|
|
742
|
+
return Vue.set(state, "resources", {});
|
|
743
|
+
flush(state.resources, type, id !== void 0 ? [id] : [], prefix);
|
|
582
744
|
}
|
|
583
745
|
},
|
|
584
746
|
actions: {
|
|
585
747
|
async getCollection({ commit, state }, { type, query, prefix, bypassCache = false }) {
|
|
586
|
-
const
|
|
587
|
-
const hash = query ? md5(JSON.stringify({ ...queryObject, fields: {}, include: [] })) : "_default";
|
|
748
|
+
const hash = collectionHash(query);
|
|
588
749
|
if (!bypassCache && ((state.collections[type] || {})[hash] || {})[prefix]) {
|
|
589
750
|
const cached = state.collections[type][hash][prefix];
|
|
590
751
|
const hydrate = (o) => (((state.resources[o.type] || {})[o.id] || {})[prefix] || {}).data;
|
|
@@ -598,10 +759,12 @@ const DruxtStore = ({ store }) => {
|
|
|
598
759
|
};
|
|
599
760
|
}
|
|
600
761
|
}
|
|
601
|
-
const key = JSON.stringify(["collection", prefix, type, hash,
|
|
762
|
+
const key = JSON.stringify(["collection", prefix, type, hash, getDrupalJsonApiParams(query).getQueryObject()]);
|
|
602
763
|
return share(key, async () => {
|
|
764
|
+
const since = generation.value;
|
|
603
765
|
const collection = await this.$druxt.getCollection(type, query, prefix);
|
|
604
|
-
|
|
766
|
+
if (since === generation.value)
|
|
767
|
+
commit("addCollection", { collection: { ...collection }, type, hash, prefix });
|
|
605
768
|
return collection;
|
|
606
769
|
});
|
|
607
770
|
},
|
|
@@ -615,7 +778,7 @@ const DruxtStore = ({ store }) => {
|
|
|
615
778
|
queryObject.fields[type] = Array.from(new Set([...fields2, ...includes])).filter((s) => s).join(",");
|
|
616
779
|
}
|
|
617
780
|
let included = [];
|
|
618
|
-
if (queryObject.include && storedResource) {
|
|
781
|
+
if (queryObject.include && storedResource && !bypassCache) {
|
|
619
782
|
const resources = await Promise.all(queryObject.include.split(",").filter((s) => Object.keys(storedResource.data.relationships || {}).includes(s)).map((key) => {
|
|
620
783
|
let { data } = storedResource.data.relationships[key];
|
|
621
784
|
data = Array.isArray(data) ? data : [data];
|
|
@@ -639,7 +802,7 @@ const DruxtStore = ({ store }) => {
|
|
|
639
802
|
}
|
|
640
803
|
const isFull = typeof (queryObject.fields || {})[type] !== "string";
|
|
641
804
|
let fields = isFull ? true : (queryObject.fields || {})[type];
|
|
642
|
-
if (storedResource && !isFull && fields) {
|
|
805
|
+
if (storedResource && !isFull && fields && !bypassCache) {
|
|
643
806
|
const queryFields = fields.split(",");
|
|
644
807
|
const resourceFields = [
|
|
645
808
|
...Object.keys(((storedResource || {}).data || {}).attributes || {}),
|
|
@@ -650,18 +813,22 @@ const DruxtStore = ({ store }) => {
|
|
|
650
813
|
queryObject.fields[type] = (missingFields || []).join(",") || void 0;
|
|
651
814
|
}
|
|
652
815
|
let resource;
|
|
816
|
+
const since = generation.value;
|
|
653
817
|
if (bypassCache || !storedResource || fields) {
|
|
654
818
|
try {
|
|
655
819
|
const key = JSON.stringify(["resource", prefix, type, id, queryObject]);
|
|
656
820
|
resource = await share(key, async () => {
|
|
821
|
+
const started = generation.value;
|
|
657
822
|
const response = await this.$druxt.getResource(type, id, getDrupalJsonApiParams(queryObject), prefix);
|
|
658
|
-
|
|
823
|
+
if (started === generation.value)
|
|
824
|
+
commit("addResource", { prefix, resource: { ...response } });
|
|
659
825
|
return response;
|
|
660
826
|
});
|
|
661
827
|
} catch (e) {
|
|
662
828
|
}
|
|
663
829
|
}
|
|
664
|
-
const
|
|
830
|
+
const stored = ((state.resources[type] || {})[id] || {})[prefix];
|
|
831
|
+
const result = { ...since === generation.value && stored ? stored : resource || stored };
|
|
665
832
|
if (queryObject.include && ((resource || {}).included || (storedResource || {}).included)) {
|
|
666
833
|
included = [
|
|
667
834
|
...(resource || {}).included || [],
|
package/dist/druxt.ssr.js
CHANGED
|
@@ -21,7 +21,113 @@ const merge__default = /*#__PURE__*/_interopDefaultLegacy(merge);
|
|
|
21
21
|
const md5__default = /*#__PURE__*/_interopDefaultLegacy(md5);
|
|
22
22
|
const Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
|
|
23
23
|
|
|
24
|
+
const SCOPES = Symbol.for("druxt.processCache");
|
|
25
|
+
const scopes = globalThis[SCOPES] || (globalThis[SCOPES] = new Map());
|
|
26
|
+
const credentialed = new WeakSet();
|
|
27
|
+
const watched = new WeakSet();
|
|
28
|
+
const runtime = { isServer: () => typeof window === "undefined" };
|
|
29
|
+
const DRUPAL_SESSION_COOKIE = "S?SESS[0-9a-f]+";
|
|
30
|
+
const consumerId = (axios) => {
|
|
31
|
+
const headers = ((axios || {}).defaults || {}).headers || {};
|
|
32
|
+
for (const set of [headers, headers.common, headers.get]) {
|
|
33
|
+
const match = Object.entries(set || {}).find(([name]) => name.toLowerCase() === "x-consumer-id");
|
|
34
|
+
if (match && match[1])
|
|
35
|
+
return String(match[1]);
|
|
36
|
+
}
|
|
37
|
+
return "";
|
|
38
|
+
};
|
|
39
|
+
const headersHaveCredentials = (headers, sessionCookie) => {
|
|
40
|
+
const session = new RegExp(`(?:^|;\\s*)(?:${sessionCookie})=`, "i");
|
|
41
|
+
return Object.entries(headers || {}).some(([name, value]) => {
|
|
42
|
+
if (!value || typeof value === "object")
|
|
43
|
+
return false;
|
|
44
|
+
const header = name.toLowerCase();
|
|
45
|
+
return header === "authorization" || header === "cookie" && session.test(String(value));
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
const configHasCredentials = (config, sessionCookie = DRUPAL_SESSION_COOKIE) => {
|
|
49
|
+
if (!config)
|
|
50
|
+
return false;
|
|
51
|
+
if (config.auth)
|
|
52
|
+
return true;
|
|
53
|
+
const headers = config.headers || {};
|
|
54
|
+
return headersHaveCredentials(headers, sessionCookie) || headersHaveCredentials(headers.common, sessionCookie) || headersHaveCredentials(headers.get, sessionCookie);
|
|
55
|
+
};
|
|
56
|
+
const hasCredentials = (axios, sessionCookie = DRUPAL_SESSION_COOKIE) => {
|
|
57
|
+
if (!axios)
|
|
58
|
+
return false;
|
|
59
|
+
if (credentialed.has(axios))
|
|
60
|
+
return true;
|
|
61
|
+
return configHasCredentials(axios.defaults, sessionCookie);
|
|
62
|
+
};
|
|
63
|
+
const watchCredentials = (axios, sessionCookie = DRUPAL_SESSION_COOKIE) => {
|
|
64
|
+
const interceptors = ((axios || {}).interceptors || {}).response;
|
|
65
|
+
if (!interceptors || typeof interceptors.use !== "function" || watched.has(axios))
|
|
66
|
+
return;
|
|
67
|
+
watched.add(axios);
|
|
68
|
+
const mark = (config) => {
|
|
69
|
+
if (configHasCredentials(config, sessionCookie))
|
|
70
|
+
credentialed.add(axios);
|
|
71
|
+
};
|
|
72
|
+
interceptors.use((response) => {
|
|
73
|
+
mark((response || {}).config);
|
|
74
|
+
return response;
|
|
75
|
+
}, (error) => {
|
|
76
|
+
mark((error || {}).config);
|
|
77
|
+
return Promise.reject(error);
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const parseCacheLifetime = (headers) => {
|
|
81
|
+
const header = (name) => Object.entries(headers || {}).filter(([key]) => key.toLowerCase() === name).map(([, value]) => String(value)).join(", ");
|
|
82
|
+
const directives = header("cache-control").toLowerCase().split(",").map((d) => d.trim());
|
|
83
|
+
if (directives.some((d) => ["private", "no-store", "no-cache"].includes(d)))
|
|
84
|
+
return 0;
|
|
85
|
+
const vary = header("vary").toLowerCase().split(",").map((v) => v.trim()).filter(Boolean);
|
|
86
|
+
if (vary.some((v) => !["cookie", "x-consumer-id", "accept-encoding"].includes(v)))
|
|
87
|
+
return 0;
|
|
88
|
+
const seconds = (name) => {
|
|
89
|
+
const directive = directives.find((d) => d.startsWith(`${name}=`));
|
|
90
|
+
const value = directive ? directive.slice(name.length + 1) : "";
|
|
91
|
+
return /^\d+$/.test(value) ? Number(value) : NaN;
|
|
92
|
+
};
|
|
93
|
+
const lifetime = directives.some((d) => d.startsWith("s-maxage=")) ? seconds("s-maxage") : seconds("max-age");
|
|
94
|
+
if (Number.isNaN(lifetime))
|
|
95
|
+
return 0;
|
|
96
|
+
const age = parseInt(header("age"), 10) || 0;
|
|
97
|
+
return Math.max(0, lifetime - age);
|
|
98
|
+
};
|
|
99
|
+
const processCache = (scope, { axios, ttl, sessionCookie } = {}) => {
|
|
100
|
+
if (!runtime.isServer() || hasCredentials(axios, sessionCookie))
|
|
101
|
+
return null;
|
|
102
|
+
if (!scopes.has(scope))
|
|
103
|
+
scopes.set(scope, new Map());
|
|
104
|
+
const consumers = scopes.get(scope);
|
|
105
|
+
const consumer = consumerId(axios);
|
|
106
|
+
if (!consumers.has(consumer))
|
|
107
|
+
consumers.set(consumer, new Map());
|
|
108
|
+
const entries = consumers.get(consumer);
|
|
109
|
+
const cap = (seconds) => typeof ttl === "number" ? Math.min(seconds, ttl) : seconds;
|
|
110
|
+
return {
|
|
111
|
+
get(key) {
|
|
112
|
+
const entry = entries.get(key);
|
|
113
|
+
if (!entry)
|
|
114
|
+
return void 0;
|
|
115
|
+
if (Date.now() - entry.created >= cap(entry.seconds) * 1e3)
|
|
116
|
+
return void 0;
|
|
117
|
+
return entry.value;
|
|
118
|
+
},
|
|
119
|
+
set(key, value, seconds) {
|
|
120
|
+
if (cap(seconds) > 0)
|
|
121
|
+
entries.set(key, { value, seconds: cap(seconds), created: Date.now() });
|
|
122
|
+
else
|
|
123
|
+
entries.delete(key);
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
|
|
24
129
|
const indexCache = new WeakMap();
|
|
130
|
+
const lifetimes = new WeakMap();
|
|
25
131
|
class DruxtClient {
|
|
26
132
|
constructor(baseUrl, options = {}) {
|
|
27
133
|
this.log = consola__default["default"].create({ defaults: {
|
|
@@ -54,6 +160,8 @@ class DruxtClient {
|
|
|
54
160
|
jsonapiResourceConfig: "jsonapi_resource_config--jsonapi_resource_config",
|
|
55
161
|
...options
|
|
56
162
|
};
|
|
163
|
+
if (this.options.cache)
|
|
164
|
+
watchCredentials(this.axios, this.options.cache.sessionCookie);
|
|
57
165
|
const indexKey = JSON.stringify([baseUrl, this.options.endpoint, this.options.jsonapiResourceConfig]);
|
|
58
166
|
this.indexKey = indexKey;
|
|
59
167
|
if (!indexCache.has(this.axios))
|
|
@@ -62,6 +170,18 @@ class DruxtClient {
|
|
|
62
170
|
this.indexRequests = cache.requests;
|
|
63
171
|
this.index = cache.index[indexKey] || (cache.index[indexKey] = {});
|
|
64
172
|
}
|
|
173
|
+
processCache(scope) {
|
|
174
|
+
if (!this.options.cache)
|
|
175
|
+
return null;
|
|
176
|
+
const { ttl, sessionCookie } = this.options.cache;
|
|
177
|
+
return processCache(scope, { axios: this.axios, ttl, sessionCookie });
|
|
178
|
+
}
|
|
179
|
+
cacheLifetime(document) {
|
|
180
|
+
const record = document && typeof document === "object" && lifetimes.get(document);
|
|
181
|
+
if (!record)
|
|
182
|
+
return 0;
|
|
183
|
+
return Math.max(0, record.seconds - Math.floor((Date.now() - record.at) / 1e3));
|
|
184
|
+
}
|
|
65
185
|
addHeaders(headers) {
|
|
66
186
|
if (typeof headers === "undefined") {
|
|
67
187
|
return false;
|
|
@@ -155,6 +275,8 @@ class DruxtClient {
|
|
|
155
275
|
async get(url, options) {
|
|
156
276
|
try {
|
|
157
277
|
const res = await this.axios.get(url, options);
|
|
278
|
+
if (res && res.data && typeof res.data === "object")
|
|
279
|
+
lifetimes.set(res.data, { seconds: parseCacheLifetime(res.headers), at: Date.now() });
|
|
158
280
|
return res;
|
|
159
281
|
} catch (err) {
|
|
160
282
|
this.error(err, { url });
|
|
@@ -181,15 +303,26 @@ class DruxtClient {
|
|
|
181
303
|
return collections;
|
|
182
304
|
}
|
|
183
305
|
async getIndex(resource, prefix) {
|
|
306
|
+
if (!(this.index || {})[prefix]) {
|
|
307
|
+
const shared = this.processCache("index");
|
|
308
|
+
const sharedKey = [this.indexKey, prefix || ""].join(":");
|
|
309
|
+
const stored = shared && shared.get(sharedKey);
|
|
310
|
+
if (stored)
|
|
311
|
+
this.index[prefix] = stored;
|
|
312
|
+
}
|
|
184
313
|
if (!(this.index || {})[prefix]) {
|
|
185
314
|
const key = [this.indexKey, prefix || ""].join(":");
|
|
186
315
|
const request = this.indexRequests[key] || (this.indexRequests[key] = this.fetchIndex(prefix));
|
|
316
|
+
let lifetime;
|
|
187
317
|
try {
|
|
188
|
-
await request;
|
|
318
|
+
lifetime = await request;
|
|
189
319
|
} finally {
|
|
190
320
|
if (this.indexRequests[key] === request)
|
|
191
321
|
delete this.indexRequests[key];
|
|
192
322
|
}
|
|
323
|
+
const shared = this.processCache("index");
|
|
324
|
+
if (shared && this.index[prefix])
|
|
325
|
+
shared.set(key, this.index[prefix], lifetime);
|
|
193
326
|
}
|
|
194
327
|
return resource ? this.index[prefix][resource] || false : this.index[prefix];
|
|
195
328
|
}
|
|
@@ -206,10 +339,13 @@ class DruxtClient {
|
|
|
206
339
|
value.href = value.href.replace(baseUrl, "");
|
|
207
340
|
return [key, value];
|
|
208
341
|
}));
|
|
342
|
+
const documents = [data];
|
|
209
343
|
if (index[this.options.jsonapiResourceConfig]) {
|
|
210
344
|
let resources = [];
|
|
211
345
|
try {
|
|
212
|
-
|
|
346
|
+
const config = (await this.get(index[this.options.jsonapiResourceConfig].href)).data;
|
|
347
|
+
resources = config.data;
|
|
348
|
+
documents.push(config);
|
|
213
349
|
} catch (err) {
|
|
214
350
|
this.log.warn(err.message);
|
|
215
351
|
}
|
|
@@ -230,6 +366,7 @@ class DruxtClient {
|
|
|
230
366
|
}
|
|
231
367
|
}
|
|
232
368
|
this.index[prefix] = index;
|
|
369
|
+
return Math.min(...documents.map((document) => this.cacheLifetime(document)));
|
|
233
370
|
}
|
|
234
371
|
async getRelated(type, id, related, query, prefix = "") {
|
|
235
372
|
if (!id || !type || !related) {
|
|
@@ -276,7 +413,7 @@ class DruxtClient {
|
|
|
276
413
|
}
|
|
277
414
|
|
|
278
415
|
var name = "druxt";
|
|
279
|
-
var version = "0.25.0-dev.
|
|
416
|
+
var version = "0.25.0-dev.20260927224155";
|
|
280
417
|
var description = "The Fully Decoupled Drupal Framework for Nuxt.js.";
|
|
281
418
|
var keywords = [
|
|
282
419
|
"cms",
|
|
@@ -335,7 +472,7 @@ var dependencies = {
|
|
|
335
472
|
};
|
|
336
473
|
var devDependencies = {
|
|
337
474
|
"@storybook/addon-docs": "^6.5.16",
|
|
338
|
-
"druxt-test-utils": "1.0.0-dev.
|
|
475
|
+
"druxt-test-utils": "1.0.0-dev.20260927224155"
|
|
339
476
|
};
|
|
340
477
|
var peerDependencies = {
|
|
341
478
|
axios: "0.28.0",
|
|
@@ -378,6 +515,9 @@ const DruxtNuxtModule = async function(moduleOptions = {}) {
|
|
|
378
515
|
...moduleOptions,
|
|
379
516
|
...(this.options || {}).druxt
|
|
380
517
|
};
|
|
518
|
+
if (options.cache !== false) {
|
|
519
|
+
options.cache = this.options.dev ? false : { ...options.cache };
|
|
520
|
+
}
|
|
381
521
|
this.options.baseUrl = options.baseUrl = options.baseUrl.endsWith("/") ? options.baseUrl.slice(0, -1) : options.baseUrl;
|
|
382
522
|
this.options.endpoint = options.endpoint = options.endpoint.startsWith("/") ? options.endpoint : `/${options.endpoint}`;
|
|
383
523
|
const druxt = new DruxtClient(options.baseUrl, {
|
|
@@ -509,16 +649,45 @@ const dehydrateResources = ({ commit, queryObject, resources, prefix }) => {
|
|
|
509
649
|
return { id: data.id, type: data.type };
|
|
510
650
|
});
|
|
511
651
|
};
|
|
652
|
+
const collectionHash = (query) => {
|
|
653
|
+
if (!query)
|
|
654
|
+
return "_default";
|
|
655
|
+
const queryObject = getDrupalJsonApiParams(query).getQueryObject();
|
|
656
|
+
return md5__default["default"](JSON.stringify({ ...queryObject, fields: {}, include: [] }));
|
|
657
|
+
};
|
|
658
|
+
const flush = (tree, key, keys, leaf) => {
|
|
659
|
+
const byKey = tree[key];
|
|
660
|
+
if (!byKey)
|
|
661
|
+
return;
|
|
662
|
+
if (!keys.length && leaf === void 0)
|
|
663
|
+
return Vue__default["default"].delete(tree, key);
|
|
664
|
+
for (const k of keys.length ? keys : Object.keys(byKey)) {
|
|
665
|
+
if (!byKey[k])
|
|
666
|
+
continue;
|
|
667
|
+
if (leaf !== void 0)
|
|
668
|
+
Vue__default["default"].delete(byKey[k], leaf);
|
|
669
|
+
else
|
|
670
|
+
Vue__default["default"].delete(byKey, k);
|
|
671
|
+
}
|
|
672
|
+
};
|
|
512
673
|
const DruxtStore = ({ store }) => {
|
|
513
674
|
if (typeof store === "undefined") {
|
|
514
675
|
throw new TypeError("Vuex store not found.");
|
|
515
676
|
}
|
|
516
677
|
const namespace = "druxt";
|
|
517
678
|
const inFlight = new Map();
|
|
679
|
+
const generation = { value: 0 };
|
|
680
|
+
const flushInFlight = () => {
|
|
681
|
+
inFlight.clear();
|
|
682
|
+
generation.value += 1;
|
|
683
|
+
};
|
|
518
684
|
const share = (key, request) => {
|
|
519
685
|
if (!inFlight.has(key)) {
|
|
520
|
-
const clear = () => inFlight.delete(key);
|
|
521
686
|
const promise = request();
|
|
687
|
+
const clear = () => {
|
|
688
|
+
if (inFlight.get(key) === promise)
|
|
689
|
+
inFlight.delete(key);
|
|
690
|
+
};
|
|
522
691
|
inFlight.set(key, promise);
|
|
523
692
|
promise.then(clear, clear);
|
|
524
693
|
}
|
|
@@ -573,31 +742,23 @@ const DruxtStore = ({ store }) => {
|
|
|
573
742
|
resource = merge__default["default"](state.resources[type][id][prefix] || {}, resource, { arrayMerge: (dst, src) => src });
|
|
574
743
|
Vue__default["default"].set(state.resources[type][id], prefix, resource);
|
|
575
744
|
},
|
|
576
|
-
flushCollection(state, { type, hash, prefix }) {
|
|
745
|
+
flushCollection(state, { type, hash, query, prefix } = {}) {
|
|
746
|
+
flushInFlight();
|
|
577
747
|
if (!type)
|
|
578
|
-
Vue__default["default"].set(state, "collections", {});
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
else if (type && hash && !prefix)
|
|
582
|
-
Vue__default["default"].set(state.collections[type], hash, {});
|
|
583
|
-
else if (type && hash && prefix)
|
|
584
|
-
Vue__default["default"].set(state.collections[type][hash], prefix, {});
|
|
748
|
+
return Vue__default["default"].set(state, "collections", {});
|
|
749
|
+
const key = hash !== void 0 ? hash : query !== void 0 ? collectionHash(query) : void 0;
|
|
750
|
+
flush(state.collections, type, key !== void 0 ? [key] : [], prefix);
|
|
585
751
|
},
|
|
586
|
-
flushResource(state, { type, id, prefix }) {
|
|
752
|
+
flushResource(state, { type, id, prefix } = {}) {
|
|
753
|
+
flushInFlight();
|
|
587
754
|
if (!type)
|
|
588
|
-
Vue__default["default"].set(state, "resources", {});
|
|
589
|
-
|
|
590
|
-
Vue__default["default"].set(state.resources, type, {});
|
|
591
|
-
else if (type && id && !prefix)
|
|
592
|
-
Vue__default["default"].set(state.resources[type], id, {});
|
|
593
|
-
else if (type && id && prefix)
|
|
594
|
-
Vue__default["default"].set(state.resources[type][id], prefix, {});
|
|
755
|
+
return Vue__default["default"].set(state, "resources", {});
|
|
756
|
+
flush(state.resources, type, id !== void 0 ? [id] : [], prefix);
|
|
595
757
|
}
|
|
596
758
|
},
|
|
597
759
|
actions: {
|
|
598
760
|
async getCollection({ commit, state }, { type, query, prefix, bypassCache = false }) {
|
|
599
|
-
const
|
|
600
|
-
const hash = query ? md5__default["default"](JSON.stringify({ ...queryObject, fields: {}, include: [] })) : "_default";
|
|
761
|
+
const hash = collectionHash(query);
|
|
601
762
|
if (!bypassCache && ((state.collections[type] || {})[hash] || {})[prefix]) {
|
|
602
763
|
const cached = state.collections[type][hash][prefix];
|
|
603
764
|
const hydrate = (o) => (((state.resources[o.type] || {})[o.id] || {})[prefix] || {}).data;
|
|
@@ -611,10 +772,12 @@ const DruxtStore = ({ store }) => {
|
|
|
611
772
|
};
|
|
612
773
|
}
|
|
613
774
|
}
|
|
614
|
-
const key = JSON.stringify(["collection", prefix, type, hash,
|
|
775
|
+
const key = JSON.stringify(["collection", prefix, type, hash, getDrupalJsonApiParams(query).getQueryObject()]);
|
|
615
776
|
return share(key, async () => {
|
|
777
|
+
const since = generation.value;
|
|
616
778
|
const collection = await this.$druxt.getCollection(type, query, prefix);
|
|
617
|
-
|
|
779
|
+
if (since === generation.value)
|
|
780
|
+
commit("addCollection", { collection: { ...collection }, type, hash, prefix });
|
|
618
781
|
return collection;
|
|
619
782
|
});
|
|
620
783
|
},
|
|
@@ -628,7 +791,7 @@ const DruxtStore = ({ store }) => {
|
|
|
628
791
|
queryObject.fields[type] = Array.from(new Set([...fields2, ...includes])).filter((s) => s).join(",");
|
|
629
792
|
}
|
|
630
793
|
let included = [];
|
|
631
|
-
if (queryObject.include && storedResource) {
|
|
794
|
+
if (queryObject.include && storedResource && !bypassCache) {
|
|
632
795
|
const resources = await Promise.all(queryObject.include.split(",").filter((s) => Object.keys(storedResource.data.relationships || {}).includes(s)).map((key) => {
|
|
633
796
|
let { data } = storedResource.data.relationships[key];
|
|
634
797
|
data = Array.isArray(data) ? data : [data];
|
|
@@ -652,7 +815,7 @@ const DruxtStore = ({ store }) => {
|
|
|
652
815
|
}
|
|
653
816
|
const isFull = typeof (queryObject.fields || {})[type] !== "string";
|
|
654
817
|
let fields = isFull ? true : (queryObject.fields || {})[type];
|
|
655
|
-
if (storedResource && !isFull && fields) {
|
|
818
|
+
if (storedResource && !isFull && fields && !bypassCache) {
|
|
656
819
|
const queryFields = fields.split(",");
|
|
657
820
|
const resourceFields = [
|
|
658
821
|
...Object.keys(((storedResource || {}).data || {}).attributes || {}),
|
|
@@ -663,18 +826,22 @@ const DruxtStore = ({ store }) => {
|
|
|
663
826
|
queryObject.fields[type] = (missingFields || []).join(",") || void 0;
|
|
664
827
|
}
|
|
665
828
|
let resource;
|
|
829
|
+
const since = generation.value;
|
|
666
830
|
if (bypassCache || !storedResource || fields) {
|
|
667
831
|
try {
|
|
668
832
|
const key = JSON.stringify(["resource", prefix, type, id, queryObject]);
|
|
669
833
|
resource = await share(key, async () => {
|
|
834
|
+
const started = generation.value;
|
|
670
835
|
const response = await this.$druxt.getResource(type, id, getDrupalJsonApiParams(queryObject), prefix);
|
|
671
|
-
|
|
836
|
+
if (started === generation.value)
|
|
837
|
+
commit("addResource", { prefix, resource: { ...response } });
|
|
672
838
|
return response;
|
|
673
839
|
});
|
|
674
840
|
} catch (e) {
|
|
675
841
|
}
|
|
676
842
|
}
|
|
677
|
-
const
|
|
843
|
+
const stored = ((state.resources[type] || {})[id] || {})[prefix];
|
|
844
|
+
const result = { ...since === generation.value && stored ? stored : resource || stored };
|
|
678
845
|
if (queryObject.include && ((resource || {}).included || (storedResource || {}).included)) {
|
|
679
846
|
included = [
|
|
680
847
|
...(resource || {}).included || [],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "druxt",
|
|
3
|
-
"version": "0.25.0-dev.
|
|
3
|
+
"version": "0.25.0-dev.20260927224155",
|
|
4
4
|
"description": "The Fully Decoupled Drupal Framework for Nuxt.js.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cms",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@storybook/addon-docs": "^6.5.16",
|
|
62
|
-
"druxt-test-utils": "1.0.0-dev.
|
|
62
|
+
"druxt-test-utils": "1.0.0-dev.20260927224155"
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"axios": "0.28.0",
|