zero-query 1.0.1 → 1.0.5
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/README.md +50 -9
- package/cli/help.js +2 -1
- package/cli/scaffold/default/app/app.js +3 -6
- package/cli/scaffold/default/global.css +12 -3
- package/cli/scaffold/default/index.html +8 -8
- package/cli/scaffold/minimal/app/app.js +3 -9
- package/cli/scaffold/minimal/global.css +12 -3
- package/cli/scaffold/minimal/index.html +3 -3
- package/cli/scaffold/ssr/app/app.js +18 -6
- package/cli/scaffold/ssr/app/components/about.js +42 -15
- package/cli/scaffold/ssr/app/components/blog/index.js +65 -0
- package/cli/scaffold/ssr/app/components/blog/post.js +86 -0
- package/cli/scaffold/ssr/app/routes.js +4 -2
- package/cli/scaffold/ssr/global.css +117 -1
- package/cli/scaffold/ssr/index.html +8 -2
- package/cli/scaffold/ssr/server/data/posts.js +144 -0
- package/cli/scaffold/ssr/server/index.js +117 -23
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +85 -20
- package/dist/zquery.min.js +2 -2
- package/index.d.ts +5 -2
- package/index.js +5 -4
- package/package.json +1 -1
- package/src/component.js +17 -2
- package/src/router.js +61 -12
- package/src/ssr.js +100 -0
- package/tests/component.test.js +480 -0
- package/tests/router.test.js +65 -1
- package/tests/ssr.test.js +175 -0
- package/tests/test-minifier.js +4 -4
- package/types/misc.d.ts +23 -1
- package/types/router.d.ts +25 -0
- package/types/ssr.d.ts +33 -0
package/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Lightweight modern frontend library - jQuery-like selectors, reactive
|
|
5
5
|
* components, SPA router, state management, HTTP client & utilities.
|
|
6
6
|
*
|
|
7
|
-
* @version 1.0.
|
|
7
|
+
* @version 1.0.5
|
|
8
8
|
* @license MIT
|
|
9
9
|
* @see https://z-query.com/docs
|
|
10
10
|
*/
|
|
@@ -46,8 +46,10 @@ export {
|
|
|
46
46
|
NavigationContext,
|
|
47
47
|
RouterConfig,
|
|
48
48
|
RouterInstance,
|
|
49
|
+
RouteMatch,
|
|
49
50
|
createRouter,
|
|
50
51
|
getRouter,
|
|
52
|
+
matchRoute,
|
|
51
53
|
} from './types/router';
|
|
52
54
|
|
|
53
55
|
export {
|
|
@@ -146,7 +148,7 @@ export {
|
|
|
146
148
|
import type { ZQueryCollection } from './types/collection';
|
|
147
149
|
import type { reactive, Signal, signal, computed, effect, batch, untracked } from './types/reactive';
|
|
148
150
|
import type { component, mount, mountAll, getInstance, destroy, getRegistry, prefetch, style } from './types/component';
|
|
149
|
-
import type { createRouter, getRouter } from './types/router';
|
|
151
|
+
import type { createRouter, getRouter, matchRoute } from './types/router';
|
|
150
152
|
import type { createStore, getStore } from './types/store';
|
|
151
153
|
import type { HttpClient } from './types/http';
|
|
152
154
|
import type {
|
|
@@ -264,6 +266,7 @@ interface ZQueryStatic {
|
|
|
264
266
|
// -- Router --------------------------------------------------------------
|
|
265
267
|
router: typeof createRouter;
|
|
266
268
|
getRouter: typeof getRouter;
|
|
269
|
+
matchRoute: typeof matchRoute;
|
|
267
270
|
|
|
268
271
|
// -- Store ---------------------------------------------------------------
|
|
269
272
|
store: typeof createStore;
|
package/index.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
import { query, queryAll, ZQueryCollection } from './src/core.js';
|
|
13
13
|
import { reactive, Signal, signal, computed, effect, batch, untracked } from './src/reactive.js';
|
|
14
14
|
import { component, mount, mountAll, getInstance, destroy, getRegistry, prefetch, style } from './src/component.js';
|
|
15
|
-
import { createRouter, getRouter } from './src/router.js';
|
|
15
|
+
import { createRouter, getRouter, matchRoute } from './src/router.js';
|
|
16
16
|
import { createStore, getStore } from './src/store.js';
|
|
17
17
|
import { http } from './src/http.js';
|
|
18
18
|
import { morph, morphElement } from './src/diff.js';
|
|
@@ -115,8 +115,9 @@ $.morphElement = morphElement;
|
|
|
115
115
|
$.safeEval = safeEval;
|
|
116
116
|
|
|
117
117
|
// --- Router ----------------------------------------------------------------
|
|
118
|
-
$.router
|
|
119
|
-
$.getRouter
|
|
118
|
+
$.router = createRouter;
|
|
119
|
+
$.getRouter = getRouter;
|
|
120
|
+
$.matchRoute = matchRoute;
|
|
120
121
|
|
|
121
122
|
// --- Store -----------------------------------------------------------------
|
|
122
123
|
$.store = createStore;
|
|
@@ -214,7 +215,7 @@ export {
|
|
|
214
215
|
component, mount, mountAll, getInstance, destroy, getRegistry, prefetch, style,
|
|
215
216
|
morph, morphElement,
|
|
216
217
|
safeEval,
|
|
217
|
-
createRouter, getRouter,
|
|
218
|
+
createRouter, getRouter, matchRoute,
|
|
218
219
|
createStore, getStore,
|
|
219
220
|
http,
|
|
220
221
|
ZQueryError, ErrorCode, onError, reportError, guardCallback, guardAsync, validate, formatError,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zero-query",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Lightweight modern frontend library - jQuery-like selectors, reactive components, SPA router, and state management with zero dependencies.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
package/src/component.js
CHANGED
|
@@ -678,13 +678,28 @@ class Component {
|
|
|
678
678
|
if (el.contains(e.target)) continue;
|
|
679
679
|
}
|
|
680
680
|
|
|
681
|
-
// Key modifiers - filter keyboard events by key
|
|
681
|
+
// Key modifiers - filter keyboard events by key.
|
|
682
|
+
// Named shortcuts map common names to their e.key values.
|
|
683
|
+
// Any modifier not recognised as a built-in behaviour, timing,
|
|
684
|
+
// or system modifier is matched against e.key (case-insensitive)
|
|
685
|
+
// so that arbitrary keys work: .a, .f1, .+, .0, .arrowup, etc.
|
|
682
686
|
const _keyMap = { enter: 'Enter', escape: 'Escape', tab: 'Tab', space: ' ', delete: 'Delete|Backspace', up: 'ArrowUp', down: 'ArrowDown', left: 'ArrowLeft', right: 'ArrowRight' };
|
|
687
|
+
const _nonKeyMods = new Set(['prevent','stop','self','once','outside','capture','passive','debounce','throttle','ctrl','shift','alt','meta']);
|
|
683
688
|
let keyFiltered = false;
|
|
684
|
-
for (
|
|
689
|
+
for (let mi = 0; mi < modifiers.length; mi++) {
|
|
690
|
+
const mod = modifiers[mi];
|
|
685
691
|
if (_keyMap[mod]) {
|
|
686
692
|
const keys = _keyMap[mod].split('|');
|
|
687
693
|
if (!e.key || !keys.includes(e.key)) { keyFiltered = true; break; }
|
|
694
|
+
} else if (_nonKeyMods.has(mod)) {
|
|
695
|
+
continue;
|
|
696
|
+
} else if (/^\d+$/.test(mod) && mi > 0 && (modifiers[mi - 1] === 'debounce' || modifiers[mi - 1] === 'throttle')) {
|
|
697
|
+
// Numeric value following debounce/throttle — skip (it's a ms parameter)
|
|
698
|
+
continue;
|
|
699
|
+
} else {
|
|
700
|
+
// Dynamic key match — compare modifier against e.key
|
|
701
|
+
// Case-insensitive: .a matches 'a' and 'A', .f1 matches 'F1'
|
|
702
|
+
if (!e.key || e.key.toLowerCase() !== mod.toLowerCase()) { keyFiltered = true; break; }
|
|
688
703
|
}
|
|
689
704
|
}
|
|
690
705
|
if (keyFiltered) continue;
|
package/src/router.js
CHANGED
|
@@ -195,24 +195,15 @@ class Router {
|
|
|
195
195
|
|
|
196
196
|
add(route) {
|
|
197
197
|
// Compile path pattern into regex
|
|
198
|
-
const keys =
|
|
199
|
-
const pattern = route.path
|
|
200
|
-
.replace(/:(\w+)/g, (_, key) => { keys.push(key); return '([^/]+)'; })
|
|
201
|
-
.replace(/\*/g, '(.*)');
|
|
202
|
-
const regex = new RegExp(`^${pattern}$`);
|
|
203
|
-
|
|
198
|
+
const { regex, keys } = compilePath(route.path);
|
|
204
199
|
this._routes.push({ ...route, _regex: regex, _keys: keys });
|
|
205
200
|
|
|
206
201
|
// Per-route fallback: register an alias path for the same component.
|
|
207
202
|
// e.g. { path: '/docs/:section', fallback: '/docs', component: 'docs-page' }
|
|
208
203
|
// When matched via fallback, missing params are undefined.
|
|
209
204
|
if (route.fallback) {
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
.replace(/:(\w+)/g, (_, key) => { fbKeys.push(key); return '([^/]+)'; })
|
|
213
|
-
.replace(/\*/g, '(.*)');
|
|
214
|
-
const fbRegex = new RegExp(`^${fbPattern}$`);
|
|
215
|
-
this._routes.push({ ...route, path: route.fallback, _regex: fbRegex, _keys: fbKeys });
|
|
205
|
+
const fb = compilePath(route.fallback);
|
|
206
|
+
this._routes.push({ ...route, path: route.fallback, _regex: fb.regex, _keys: fb.keys });
|
|
216
207
|
}
|
|
217
208
|
|
|
218
209
|
return this;
|
|
@@ -709,6 +700,64 @@ class Router {
|
|
|
709
700
|
}
|
|
710
701
|
|
|
711
702
|
|
|
703
|
+
// ---------------------------------------------------------------------------
|
|
704
|
+
// Path compilation (shared by Router.add and matchRoute)
|
|
705
|
+
// ---------------------------------------------------------------------------
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Compile a route path pattern into a RegExp and param key list.
|
|
709
|
+
* Supports `:param` segments and `*` wildcard.
|
|
710
|
+
* @param {string} path - e.g. '/user/:id' or '/files/*'
|
|
711
|
+
* @returns {{ regex: RegExp, keys: string[] }}
|
|
712
|
+
*/
|
|
713
|
+
function compilePath(path) {
|
|
714
|
+
const keys = [];
|
|
715
|
+
const pattern = path
|
|
716
|
+
.replace(/:(\w+)/g, (_, key) => { keys.push(key); return '([^/]+)'; })
|
|
717
|
+
.replace(/\*/g, '(.*)');
|
|
718
|
+
return { regex: new RegExp(`^${pattern}$`), keys };
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// ---------------------------------------------------------------------------
|
|
722
|
+
// Standalone route matcher (DOM-free — usable on server and client)
|
|
723
|
+
// ---------------------------------------------------------------------------
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Match a pathname against an array of route definitions.
|
|
727
|
+
* Returns `{ component, params }`. If no route matches, falls back to the
|
|
728
|
+
* `fallback` component name (default `'not-found'`).
|
|
729
|
+
*
|
|
730
|
+
* This is the same matching logic the client-side router uses internally,
|
|
731
|
+
* extracted so SSR servers can resolve URLs without the DOM.
|
|
732
|
+
*
|
|
733
|
+
* @param {Array<{ path: string, component: string, fallback?: string }>} routes
|
|
734
|
+
* @param {string} pathname - URL path to match, e.g. '/blog/my-post'
|
|
735
|
+
* @param {string} [fallback='not-found'] - Component name when nothing matches
|
|
736
|
+
* @returns {{ component: string, params: Record<string, string> }}
|
|
737
|
+
*/
|
|
738
|
+
export function matchRoute(routes, pathname, fallback = 'not-found') {
|
|
739
|
+
for (const route of routes) {
|
|
740
|
+
const { regex, keys } = compilePath(route.path);
|
|
741
|
+
const m = pathname.match(regex);
|
|
742
|
+
if (m) {
|
|
743
|
+
const params = {};
|
|
744
|
+
keys.forEach((key, i) => { params[key] = m[i + 1]; });
|
|
745
|
+
return { component: route.component, params };
|
|
746
|
+
}
|
|
747
|
+
// Per-route fallback alias (same as Router.add)
|
|
748
|
+
if (route.fallback) {
|
|
749
|
+
const fb = compilePath(route.fallback);
|
|
750
|
+
const fbm = pathname.match(fb.regex);
|
|
751
|
+
if (fbm) {
|
|
752
|
+
const params = {};
|
|
753
|
+
fb.keys.forEach((key, i) => { params[key] = fbm[i + 1]; });
|
|
754
|
+
return { component: route.component, params };
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return { component: fallback, params: {} };
|
|
759
|
+
}
|
|
760
|
+
|
|
712
761
|
// ---------------------------------------------------------------------------
|
|
713
762
|
// Factory
|
|
714
763
|
// ---------------------------------------------------------------------------
|
package/src/ssr.js
CHANGED
|
@@ -280,6 +280,103 @@ class SSRApp {
|
|
|
280
280
|
</body>
|
|
281
281
|
</html>`;
|
|
282
282
|
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Render a component into an existing HTML shell template.
|
|
286
|
+
*
|
|
287
|
+
* Unlike renderPage() which generates a full HTML document from scratch,
|
|
288
|
+
* renderShell() takes your own index.html (with nav, footer, custom markup)
|
|
289
|
+
* and injects the SSR-rendered component body plus metadata into it.
|
|
290
|
+
*
|
|
291
|
+
* Handles:
|
|
292
|
+
* - Component rendering into <z-outlet>
|
|
293
|
+
* - <title> replacement
|
|
294
|
+
* - <meta name="description"> replacement
|
|
295
|
+
* - Open Graph meta tag replacement (og:title, og:description, og:type, etc.)
|
|
296
|
+
* - window.__SSR_DATA__ hydration script injection
|
|
297
|
+
*
|
|
298
|
+
* @param {string} shell - HTML template string (your index.html)
|
|
299
|
+
* @param {object} options
|
|
300
|
+
* @param {string} options.component - registered component name to render
|
|
301
|
+
* @param {object} [options.props] - props passed to the component
|
|
302
|
+
* @param {string} [options.title] - page title (replaces <title>)
|
|
303
|
+
* @param {string} [options.description] - meta description (replaces <meta name="description">)
|
|
304
|
+
* @param {object} [options.og] - Open Graph tags to replace (e.g. { title, description, type, image })
|
|
305
|
+
* @param {any} [options.ssrData] - data to embed as window.__SSR_DATA__ for client hydration
|
|
306
|
+
* @param {object} [options.renderOptions] - options passed to renderToString (hydrate, mode)
|
|
307
|
+
* @returns {Promise<string>} - the shell with SSR content and metadata injected
|
|
308
|
+
*/
|
|
309
|
+
async renderShell(shell, options = {}) {
|
|
310
|
+
const {
|
|
311
|
+
component: comp,
|
|
312
|
+
props = {},
|
|
313
|
+
title,
|
|
314
|
+
description,
|
|
315
|
+
og,
|
|
316
|
+
ssrData,
|
|
317
|
+
renderOptions,
|
|
318
|
+
} = options;
|
|
319
|
+
|
|
320
|
+
// Render the component
|
|
321
|
+
let body = '';
|
|
322
|
+
if (comp) {
|
|
323
|
+
try {
|
|
324
|
+
body = await this.renderToString(comp, props, renderOptions);
|
|
325
|
+
} catch (err) {
|
|
326
|
+
reportError(ErrorCode.SSR_PAGE, `renderShell failed for component "${comp}"`, { component: comp }, err);
|
|
327
|
+
body = `<!-- SSR error: ${_escapeHtml(err.message)} -->`;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
let html = shell;
|
|
332
|
+
|
|
333
|
+
// Inject SSR body into <z-outlet>
|
|
334
|
+
// Use a replacer function to avoid $ substitution patterns in body
|
|
335
|
+
html = html.replace(/(<z-outlet[^>]*>)([\s\S]*?)(<\/z-outlet>)?/, (_, open) => `${open}${body}</z-outlet>`);
|
|
336
|
+
|
|
337
|
+
// Replace <title>
|
|
338
|
+
if (title != null) {
|
|
339
|
+
const safeTitle = _escapeHtml(title);
|
|
340
|
+
html = html.replace(/<title>[^<]*<\/title>/, () => `<title>${safeTitle}</title>`);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Replace <meta name="description">
|
|
344
|
+
if (description != null) {
|
|
345
|
+
const safeDesc = _escapeHtml(description);
|
|
346
|
+
html = html.replace(
|
|
347
|
+
/<meta\s+name="description"\s+content="[^"]*">/,
|
|
348
|
+
() => `<meta name="description" content="${safeDesc}">`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Replace Open Graph meta tags
|
|
353
|
+
if (og) {
|
|
354
|
+
for (const [key, value] of Object.entries(og)) {
|
|
355
|
+
// Sanitize key: allow only safe OG property characters (alphanumeric, hyphens, underscores, colons)
|
|
356
|
+
const safeKey = key.replace(/[^a-zA-Z0-9_:\-]/g, '');
|
|
357
|
+
if (!safeKey) continue;
|
|
358
|
+
const escaped = _escapeHtml(String(value));
|
|
359
|
+
// Escape key for use in RegExp to prevent ReDoS
|
|
360
|
+
const escapedKey = safeKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
361
|
+
const pattern = new RegExp(`<meta\\s+property="og:${escapedKey}"\\s+content="[^"]*">`);
|
|
362
|
+
if (pattern.test(html)) {
|
|
363
|
+
html = html.replace(pattern, () => `<meta property="og:${safeKey}" content="${escaped}">`);
|
|
364
|
+
} else {
|
|
365
|
+
// Tag doesn't exist — inject before </head>
|
|
366
|
+
html = html.replace('</head>', () => `<meta property="og:${safeKey}" content="${escaped}">\n</head>`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Inject hydration data as window.__SSR_DATA__
|
|
372
|
+
if (ssrData !== undefined) {
|
|
373
|
+
// Escape </script> and <!-- sequences to prevent breaking out of the script tag
|
|
374
|
+
const json = JSON.stringify(ssrData).replace(/<\//g, '<\\/').replace(/<!--/g, '<\\!--');
|
|
375
|
+
html = html.replace('</head>', () => `<script>window.__SSR_DATA__=${json};</script>\n</head>`);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return html;
|
|
379
|
+
}
|
|
283
380
|
}
|
|
284
381
|
|
|
285
382
|
// ---------------------------------------------------------------------------
|
|
@@ -316,3 +413,6 @@ export function renderToString(definition, props = {}) {
|
|
|
316
413
|
export function escapeHtml(str) {
|
|
317
414
|
return _escapeHtml(String(str));
|
|
318
415
|
}
|
|
416
|
+
|
|
417
|
+
// Re-export matchRoute so SSR servers can import from 'zero-query/ssr'
|
|
418
|
+
export { matchRoute } from './router.js';
|