hadars 0.1.35 → 0.1.37

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.
@@ -0,0 +1,60 @@
1
+ // src/utils/segmentCache.ts
2
+ function getStore() {
3
+ const g = globalThis;
4
+ if (!g.__hadarsSegmentStore) {
5
+ g.__hadarsSegmentStore = /* @__PURE__ */ new Map();
6
+ }
7
+ return g.__hadarsSegmentStore;
8
+ }
9
+ function getSegment(key) {
10
+ const entry = getStore().get(key);
11
+ if (!entry) return null;
12
+ if (entry.expiresAt !== null && Date.now() >= entry.expiresAt) {
13
+ getStore().delete(key);
14
+ return null;
15
+ }
16
+ return entry.html;
17
+ }
18
+ function setSegment(key, html, ttl) {
19
+ getStore().set(key, {
20
+ html,
21
+ expiresAt: ttl != null ? Date.now() + ttl : null
22
+ });
23
+ }
24
+ function deleteSegment(key) {
25
+ getStore().delete(key);
26
+ }
27
+ function clearSegments() {
28
+ getStore().clear();
29
+ }
30
+ var CACHE_TAG = "hadars-c";
31
+ function processSegmentCache(html) {
32
+ let prev;
33
+ do {
34
+ prev = html;
35
+ html = html.replace(
36
+ /<hadars-c([^>]*)>([\s\S]*?)<\/hadars-c>/g,
37
+ (match, attrs, content) => {
38
+ const cacheM = /data-cache="([^"]+)"/.exec(attrs);
39
+ const keyM = /data-key="([^"]+)"/.exec(attrs);
40
+ const ttlM = /data-ttl="(\d+)"/.exec(attrs);
41
+ if (!cacheM || !keyM) return match;
42
+ if (cacheM[1] === "miss") {
43
+ setSegment(keyM[1], content, ttlM ? Number(ttlM[1]) : void 0);
44
+ return content;
45
+ }
46
+ if (cacheM[1] === "hit") return content;
47
+ return match;
48
+ }
49
+ );
50
+ } while (html !== prev);
51
+ return html;
52
+ }
53
+
54
+ export {
55
+ getSegment,
56
+ deleteSegment,
57
+ clearSegments,
58
+ CACHE_TAG,
59
+ processSegmentCache
60
+ };