@absolutejs/absolute 0.19.0-beta.130 → 0.19.0-beta.131

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,25 @@
1
+ /* Client-side constants — local copy for dist/dev/client/ portability.
2
+ These values MUST stay in sync with the root constants.ts.
3
+ This file exists because the client files are copied raw to dist/dev/client/
4
+ and cannot import from ../../constants when installed from npm. */
5
+
6
+ export const ANGULAR_INIT_TIMEOUT_MS = 500;
7
+ export const CSS_ERROR_RESOLVE_DELAY_MS = 50;
8
+ export const CSS_MAX_CHECK_ATTEMPTS = 10;
9
+ export const CSS_MAX_PARSE_TIMEOUT_MS = 500;
10
+ export const CSS_SHEET_READY_TIMEOUT_MS = 100;
11
+ export const DOM_UPDATE_DELAY_MS = 50;
12
+ export const FOCUS_ID_PREFIX_LENGTH = 3;
13
+ export const FOCUS_IDX_PREFIX_LENGTH = 4;
14
+ export const FOCUS_NAME_PREFIX_LENGTH = 5;
15
+ export const HMR_UPDATE_TIMEOUT_MS = 2000;
16
+ export const MAX_RECONNECT_ATTEMPTS = 60;
17
+ export const OVERLAY_FADE_DURATION_MS = 150;
18
+ export const PING_INTERVAL_MS = 30_000;
19
+ export const RAF_BATCH_COUNT = 3;
20
+ export const REBUILD_RELOAD_DELAY_MS = 200;
21
+ export const RECONNECT_INITIAL_DELAY_MS = 500;
22
+ export const RECONNECT_POLL_INTERVAL_MS = 300;
23
+ export const SVELTE_CSS_LOAD_TIMEOUT_MS = 500;
24
+ export const UNFOUND_INDEX = -1;
25
+ export const WEBSOCKET_NORMAL_CLOSURE = 1000;
@@ -0,0 +1,305 @@
1
+ /* CSS reload/preload utilities for HMR */
2
+
3
+ import { type CSSUpdateResult, hmrState } from '../../../types/client';
4
+ import {
5
+ CSS_ERROR_RESOLVE_DELAY_MS,
6
+ CSS_MAX_CHECK_ATTEMPTS,
7
+ CSS_MAX_PARSE_TIMEOUT_MS,
8
+ CSS_SHEET_READY_TIMEOUT_MS,
9
+ DOM_UPDATE_DELAY_MS,
10
+ RAF_BATCH_COUNT
11
+ } from './constants';
12
+
13
+ export const getCSSBaseName = (href: string) => {
14
+ const fileName = href.split('?')[0]?.split('/').pop() || '';
15
+
16
+ return fileName.split('.')[0] ?? '';
17
+ };
18
+
19
+ const baseNamesMatch = (baseA: string, baseB: string) =>
20
+ baseA === baseB || baseA.includes(baseB) || baseB.includes(baseA);
21
+
22
+ const findMatchingLink = (baseNew: string) => {
23
+ const links = document.head.querySelectorAll('link[rel="stylesheet"]');
24
+ for (const existing of links) {
25
+ if (!(existing instanceof HTMLLinkElement)) continue;
26
+ const existingHref = existing.getAttribute('href') || '';
27
+ const baseExisting = getCSSBaseName(existingHref);
28
+ if (baseNamesMatch(baseExisting, baseNew)) {
29
+ return existing;
30
+ }
31
+ }
32
+
33
+ return null;
34
+ };
35
+
36
+ const createTimestampedLink = (href: string) => {
37
+ const newLinkElement = document.createElement('link');
38
+ newLinkElement.rel = 'stylesheet';
39
+ newLinkElement.media = 'print';
40
+ const newHref = `${href + (href.includes('?') ? '&' : '?')}t=${Date.now()}`;
41
+ newLinkElement.href = newHref;
42
+
43
+ return { newHref, newLinkElement };
44
+ };
45
+
46
+ const processNewLink = (
47
+ newLink: Element,
48
+ linksToRemove: HTMLLinkElement[],
49
+ linksToActivate: HTMLLinkElement[],
50
+ linksToWaitFor: Promise<void>[]
51
+ ) => {
52
+ const href = newLink.getAttribute('href');
53
+ if (!href) return;
54
+
55
+ const baseNew = getCSSBaseName(href);
56
+ const existingLink = findMatchingLink(baseNew);
57
+
58
+ if (!existingLink) {
59
+ const { newHref, newLinkElement } = createTimestampedLink(href);
60
+ linksToActivate.push(newLinkElement);
61
+ const loadPromise = createCSSLoadPromise(newLinkElement, newHref);
62
+ document.head.appendChild(newLinkElement);
63
+ linksToWaitFor.push(loadPromise);
64
+
65
+ return;
66
+ }
67
+
68
+ const existingHrefAttr = existingLink.getAttribute('href');
69
+ const existingHref = existingHrefAttr ? existingHrefAttr.split('?')[0] : '';
70
+ const [newHrefBase] = href.split('?');
71
+ if (existingHref === newHrefBase) return;
72
+
73
+ const { newHref, newLinkElement } = createTimestampedLink(href);
74
+ linksToRemove.push(existingLink);
75
+ linksToActivate.push(newLinkElement);
76
+ const loadPromise = createCSSLoadPromise(newLinkElement, newHref);
77
+ document.head.appendChild(newLinkElement);
78
+ linksToWaitFor.push(loadPromise);
79
+ };
80
+
81
+ export const processCSSLinks = (headHTML: string) => {
82
+ const tempDiv = document.createElement('div');
83
+ tempDiv.innerHTML = headHTML;
84
+ const newStylesheets = tempDiv.querySelectorAll('link[rel="stylesheet"]');
85
+ const existingStylesheets = Array.from(
86
+ document.head.querySelectorAll<HTMLLinkElement>(
87
+ 'link[rel="stylesheet"]'
88
+ )
89
+ );
90
+
91
+ const newHrefs = Array.from(newStylesheets).map((link) => {
92
+ const href = link.getAttribute('href') || '';
93
+
94
+ return getCSSBaseName(href);
95
+ });
96
+
97
+ const linksToRemove: HTMLLinkElement[] = [];
98
+ const linksToWaitFor: Promise<void>[] = [];
99
+ const linksToActivate: HTMLLinkElement[] = [];
100
+
101
+ newStylesheets.forEach((newLink) => {
102
+ processNewLink(newLink, linksToRemove, linksToActivate, linksToWaitFor);
103
+ });
104
+
105
+ existingStylesheets.forEach((existingLink) => {
106
+ const existingHref = existingLink.getAttribute('href') || '';
107
+ const baseExisting = getCSSBaseName(existingHref);
108
+ const stillExists = newHrefs.some((newBase) =>
109
+ baseNamesMatch(baseExisting, newBase)
110
+ );
111
+ if (stillExists) return;
112
+
113
+ const wasHandled = Array.from(newStylesheets).some((newLink) => {
114
+ const newHref = newLink.getAttribute('href') || '';
115
+ const baseNewLocal = getCSSBaseName(newHref);
116
+
117
+ return baseNamesMatch(baseExisting, baseNewLocal);
118
+ });
119
+
120
+ if (!wasHandled) {
121
+ linksToRemove.push(existingLink);
122
+ }
123
+ });
124
+
125
+ return { linksToActivate, linksToRemove, linksToWaitFor };
126
+ };
127
+
128
+ const findManifestHref = (
129
+ manifest: Record<string, string>,
130
+ baseName: string
131
+ ) => {
132
+ const manifestKey = `${baseName
133
+ .split('-')
134
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
135
+ .join('')}CSS`;
136
+
137
+ if (manifest[manifestKey]) {
138
+ return manifest[manifestKey];
139
+ }
140
+
141
+ for (const [key, value] of Object.entries(manifest)) {
142
+ if (key.endsWith('CSS') && value.includes(baseName)) {
143
+ return value;
144
+ }
145
+ }
146
+
147
+ return null;
148
+ };
149
+
150
+ const updateStylesheetLink = (
151
+ link: Element,
152
+ manifest: Record<string, string>
153
+ ) => {
154
+ if (!(link instanceof HTMLLinkElement)) return;
155
+ const href = link.getAttribute('href');
156
+ if (!href || href.includes('htmx.min.js')) return;
157
+
158
+ let newHref: string | null = null;
159
+ if (manifest) {
160
+ const baseName = getCSSBaseName(href);
161
+ newHref = findManifestHref(manifest, baseName);
162
+ }
163
+
164
+ if (newHref && newHref !== href) {
165
+ link.href = `${newHref}?t=${Date.now()}`;
166
+ } else {
167
+ const url = new URL(href, window.location.origin);
168
+ url.searchParams.set('t', Date.now().toString());
169
+ link.href = url.toString();
170
+ }
171
+ };
172
+
173
+ export const reloadCSSStylesheets = (manifest: Record<string, string>) => {
174
+ const stylesheets = document.querySelectorAll('link[rel="stylesheet"]');
175
+ stylesheets.forEach((link) => {
176
+ updateStylesheetLink(link, manifest);
177
+ });
178
+ };
179
+
180
+ const createCSSLoadPromise = (linkElement: HTMLLinkElement, newHref: string) =>
181
+ // eslint-disable-next-line promise/avoid-new
182
+ new Promise<void>((resolve) => {
183
+ let resolved = false;
184
+ const doResolve = function () {
185
+ if (resolved) return;
186
+ resolved = true;
187
+ resolve();
188
+ };
189
+
190
+ const verifyCSSOM = function () {
191
+ try {
192
+ const sheets = Array.from(document.styleSheets);
193
+
194
+ return sheets.some(
195
+ (sheet) =>
196
+ sheet.href &&
197
+ sheet.href.includes(newHref.split('?')[0] ?? '')
198
+ );
199
+ } catch {
200
+ return false;
201
+ }
202
+ };
203
+
204
+ linkElement.onload = function () {
205
+ let checkCount = 0;
206
+ const checkCSSOM = function () {
207
+ checkCount++;
208
+ if (verifyCSSOM() || checkCount > CSS_MAX_CHECK_ATTEMPTS) {
209
+ doResolve();
210
+ } else {
211
+ requestAnimationFrame(checkCSSOM);
212
+ }
213
+ };
214
+ requestAnimationFrame(checkCSSOM);
215
+ };
216
+
217
+ linkElement.onerror = function () {
218
+ setTimeout(() => {
219
+ doResolve();
220
+ }, CSS_ERROR_RESOLVE_DELAY_MS);
221
+ };
222
+
223
+ setTimeout(() => {
224
+ if (linkElement.sheet && !resolved) {
225
+ doResolve();
226
+ }
227
+ }, CSS_SHEET_READY_TIMEOUT_MS);
228
+
229
+ setTimeout(() => {
230
+ if (!resolved) {
231
+ doResolve();
232
+ }
233
+ }, CSS_MAX_PARSE_TIMEOUT_MS);
234
+ });
235
+
236
+ const removeLinks = (linksToRemove: HTMLLinkElement[]) => {
237
+ linksToRemove.forEach((link) => {
238
+ if (link.parentNode) {
239
+ link.remove();
240
+ }
241
+ });
242
+ };
243
+
244
+ const activateLinks = (linksToActivate: HTMLLinkElement[]) => {
245
+ linksToActivate.forEach((link) => {
246
+ link.media = 'all';
247
+ });
248
+ };
249
+
250
+ const chainRAF = (depth: number, callback: () => void) => {
251
+ if (depth <= 0) {
252
+ callback();
253
+
254
+ return;
255
+ }
256
+ requestAnimationFrame(() => {
257
+ chainRAF(depth - 1, callback);
258
+ });
259
+ };
260
+
261
+ /* Coordinate CSS load with body update: waits for CSS, patches body,
262
+ activates new CSS, removes old CSS. Handles first-update delay. */
263
+ export const waitForCSSAndUpdate = (
264
+ cssResult: CSSUpdateResult,
265
+ updateBody: () => void
266
+ ) => {
267
+ const { linksToActivate, linksToRemove, linksToWaitFor } = cssResult;
268
+
269
+ if (linksToWaitFor.length > 0) {
270
+ void Promise.all(linksToWaitFor).then(() => {
271
+ setTimeout(() => {
272
+ chainRAF(RAF_BATCH_COUNT, () => {
273
+ updateBody();
274
+ activateLinks(linksToActivate);
275
+ requestAnimationFrame(() => {
276
+ removeLinks(linksToRemove);
277
+ if (hmrState.isFirstHMRUpdate) {
278
+ hmrState.isFirstHMRUpdate = false;
279
+ }
280
+ });
281
+ });
282
+ }, DOM_UPDATE_DELAY_MS);
283
+
284
+ return undefined;
285
+ });
286
+
287
+ return;
288
+ }
289
+
290
+ const doUpdate = function () {
291
+ chainRAF(RAF_BATCH_COUNT, () => {
292
+ updateBody();
293
+ requestAnimationFrame(() => {
294
+ removeLinks(linksToRemove);
295
+ });
296
+ });
297
+ };
298
+
299
+ if (hmrState.isFirstHMRUpdate) {
300
+ hmrState.isFirstHMRUpdate = false;
301
+ setTimeout(doUpdate, DOM_UPDATE_DELAY_MS);
302
+ } else {
303
+ doUpdate();
304
+ }
305
+ };
@@ -0,0 +1,225 @@
1
+ /* DOM diffing/patching for in-place updates (zero flicker) */
2
+
3
+ import { UNFOUND_INDEX } from './constants';
4
+
5
+ type KeyedEntry = {
6
+ index: number;
7
+ node: Node;
8
+ };
9
+
10
+ const getElementKey = (elem: Node, index: number) => {
11
+ if (elem.nodeType !== Node.ELEMENT_NODE) return `text_${index}`;
12
+ if (!(elem instanceof Element)) return `text_${index}`;
13
+ if (elem.id) return `id_${elem.id}`;
14
+ if (elem.hasAttribute('data-key'))
15
+ return `key_${elem.getAttribute('data-key')}`;
16
+
17
+ return `tag_${elem.tagName}_${index}`;
18
+ };
19
+
20
+ const updateElementAttributes = (oldEl: Element, newEl: Element) => {
21
+ const newAttrs = Array.from(newEl.attributes);
22
+ const oldAttrs = Array.from(oldEl.attributes);
23
+ const runtimeAttrs = ['data-hmr-listeners-attached'];
24
+
25
+ oldAttrs.forEach((oldAttr) => {
26
+ if (
27
+ !newEl.hasAttribute(oldAttr.name) &&
28
+ runtimeAttrs.indexOf(oldAttr.name) === UNFOUND_INDEX
29
+ ) {
30
+ oldEl.removeAttribute(oldAttr.name);
31
+ }
32
+ });
33
+
34
+ newAttrs.forEach((newAttr) => {
35
+ if (
36
+ runtimeAttrs.indexOf(newAttr.name) !== UNFOUND_INDEX &&
37
+ oldEl.hasAttribute(newAttr.name)
38
+ ) {
39
+ return;
40
+ }
41
+ const oldValue = oldEl.getAttribute(newAttr.name);
42
+ if (oldValue !== newAttr.value) {
43
+ oldEl.setAttribute(newAttr.name, newAttr.value);
44
+ }
45
+ });
46
+ };
47
+
48
+ const updateTextNode = (oldNode: Node, newNode: Node) => {
49
+ if (oldNode.nodeValue !== newNode.nodeValue) {
50
+ oldNode.nodeValue = newNode.nodeValue;
51
+ }
52
+ };
53
+
54
+ const matchChildren = (oldChildren: Node[], newChildren: Node[]) => {
55
+ const oldMap = new Map<string, KeyedEntry[]>();
56
+ const newMap = new Map<string, KeyedEntry[]>();
57
+
58
+ oldChildren.forEach((child, idx) => {
59
+ const key = getElementKey(child, idx);
60
+ if (!oldMap.has(key)) {
61
+ oldMap.set(key, []);
62
+ }
63
+ oldMap.get(key)?.push({ index: idx, node: child });
64
+ });
65
+
66
+ newChildren.forEach((child, idx) => {
67
+ const key = getElementKey(child, idx);
68
+ if (!newMap.has(key)) {
69
+ newMap.set(key, []);
70
+ }
71
+ newMap.get(key)?.push({ index: idx, node: child });
72
+ });
73
+
74
+ return { newMap, oldMap };
75
+ };
76
+
77
+ const isHMRScript = (elem: Node) =>
78
+ elem instanceof Element && elem.hasAttribute('data-hmr-client');
79
+
80
+ const isHMRPreserved = (elem: Node) =>
81
+ isHMRScript(elem) ||
82
+ (elem instanceof Element && elem.hasAttribute('data-hmr-overlay'));
83
+
84
+ const isNonHMRScript = (child: Node) =>
85
+ child instanceof Element && child.tagName === 'SCRIPT';
86
+
87
+ const findBestMatch = (oldMatches: KeyedEntry[], matchedOld: Set<Node>) => {
88
+ const unmatched = oldMatches.find((entry) => !matchedOld.has(entry.node));
89
+ if (unmatched) return unmatched;
90
+ if (oldMatches.length > 0) return oldMatches[0] ?? null;
91
+
92
+ return null;
93
+ };
94
+
95
+ const reconcileChild = (
96
+ newChild: Node,
97
+ newIndex: number,
98
+ oldMap: Map<string, KeyedEntry[]>,
99
+ matchedOld: Set<Node>,
100
+ parentNode: Node,
101
+ oldChildrenFiltered: Node[]
102
+ ) => {
103
+ const newKey = getElementKey(newChild, newIndex);
104
+ const oldMatches = oldMap.get(newKey) || [];
105
+
106
+ if (oldMatches.length === 0) {
107
+ const clone = newChild.cloneNode(true);
108
+ parentNode.insertBefore(clone, oldChildrenFiltered[newIndex] || null);
109
+
110
+ return;
111
+ }
112
+
113
+ const bestMatch = findBestMatch(oldMatches, matchedOld);
114
+ if (bestMatch && !matchedOld.has(bestMatch.node)) {
115
+ matchedOld.add(bestMatch.node);
116
+ patchNode(bestMatch.node, newChild);
117
+
118
+ return;
119
+ }
120
+
121
+ const clone = newChild.cloneNode(true);
122
+ parentNode.insertBefore(clone, oldChildrenFiltered[newIndex] || null);
123
+ };
124
+
125
+ const patchNode = (oldNode: Node, newNode: Node) => {
126
+ if (
127
+ oldNode.nodeType === Node.TEXT_NODE &&
128
+ newNode.nodeType === Node.TEXT_NODE
129
+ ) {
130
+ updateTextNode(oldNode, newNode);
131
+
132
+ return;
133
+ }
134
+
135
+ if (
136
+ oldNode.nodeType !== Node.ELEMENT_NODE ||
137
+ newNode.nodeType !== Node.ELEMENT_NODE
138
+ ) {
139
+ return;
140
+ }
141
+
142
+ if (!(oldNode instanceof Element) || !(newNode instanceof Element)) return;
143
+ const oldEl = oldNode;
144
+ const newEl = newNode;
145
+
146
+ if (oldEl.tagName !== newEl.tagName) {
147
+ const clone = newEl.cloneNode(true);
148
+ oldEl.replaceWith(clone);
149
+
150
+ return;
151
+ }
152
+
153
+ updateElementAttributes(oldEl, newEl);
154
+
155
+ const oldChildren = Array.from(oldNode.childNodes);
156
+ const newChildren = Array.from(newNode.childNodes);
157
+
158
+ const oldChildrenFiltered = oldChildren.filter(
159
+ (child) => !isHMRScript(child) && !isNonHMRScript(child)
160
+ );
161
+ const newChildrenFiltered = newChildren.filter(
162
+ (child) => !isHMRScript(child) && !isNonHMRScript(child)
163
+ );
164
+
165
+ const { oldMap } = matchChildren(oldChildrenFiltered, newChildrenFiltered);
166
+ const matchedOld = new Set<Node>();
167
+
168
+ newChildrenFiltered.forEach((newChild, newIndex) => {
169
+ reconcileChild(
170
+ newChild,
171
+ newIndex,
172
+ oldMap,
173
+ matchedOld,
174
+ oldNode,
175
+ oldChildrenFiltered
176
+ );
177
+ });
178
+
179
+ oldChildrenFiltered.forEach((oldChild) => {
180
+ if (!matchedOld.has(oldChild) && !isHMRPreserved(oldChild)) {
181
+ oldChild.remove();
182
+ }
183
+ });
184
+ };
185
+
186
+ export const patchDOMInPlace = (oldContainer: HTMLElement, newHTML: string) => {
187
+ const tempDiv = document.createElement('div');
188
+ tempDiv.innerHTML = newHTML;
189
+ const newContainer = tempDiv;
190
+
191
+ const oldChildren = Array.from(oldContainer.childNodes);
192
+ const newChildren = Array.from(newContainer.childNodes);
193
+
194
+ const oldChildrenFiltered = oldChildren.filter(
195
+ (child) =>
196
+ !(
197
+ child instanceof Element &&
198
+ child.tagName === 'SCRIPT' &&
199
+ !child.hasAttribute('data-hmr-client')
200
+ )
201
+ );
202
+ const newChildrenFiltered = newChildren.filter(
203
+ (child) => !isNonHMRScript(child)
204
+ );
205
+
206
+ const { oldMap } = matchChildren(oldChildrenFiltered, newChildrenFiltered);
207
+ const matchedOld = new Set<Node>();
208
+
209
+ newChildrenFiltered.forEach((newChild, newIndex) => {
210
+ reconcileChild(
211
+ newChild,
212
+ newIndex,
213
+ oldMap,
214
+ matchedOld,
215
+ oldContainer,
216
+ oldChildrenFiltered
217
+ );
218
+ });
219
+
220
+ oldChildrenFiltered.forEach((oldChild) => {
221
+ if (matchedOld.has(oldChild)) return;
222
+ if (isHMRPreserved(oldChild)) return;
223
+ oldChild.remove();
224
+ });
225
+ };