zero-query 1.1.1 → 1.2.2

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.
Files changed (156) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -0
  3. package/cli/args.js +33 -33
  4. package/cli/commands/build-api.js +442 -442
  5. package/cli/commands/build.js +253 -247
  6. package/cli/commands/bundle.js +1226 -1224
  7. package/cli/commands/create.js +178 -121
  8. package/cli/commands/dev/devtools/index.js +56 -56
  9. package/cli/commands/dev/devtools/js/components.js +49 -49
  10. package/cli/commands/dev/devtools/js/core.js +423 -423
  11. package/cli/commands/dev/devtools/js/elements.js +421 -421
  12. package/cli/commands/dev/devtools/js/network.js +166 -166
  13. package/cli/commands/dev/devtools/js/performance.js +73 -73
  14. package/cli/commands/dev/devtools/js/router.js +105 -105
  15. package/cli/commands/dev/devtools/js/source.js +132 -132
  16. package/cli/commands/dev/devtools/js/stats.js +35 -35
  17. package/cli/commands/dev/devtools/js/tabs.js +79 -79
  18. package/cli/commands/dev/devtools/panel.html +95 -95
  19. package/cli/commands/dev/devtools/styles.css +244 -244
  20. package/cli/commands/dev/index.js +107 -107
  21. package/cli/commands/dev/logger.js +75 -75
  22. package/cli/commands/dev/overlay.js +858 -858
  23. package/cli/commands/dev/server.js +220 -220
  24. package/cli/commands/dev/validator.js +94 -94
  25. package/cli/commands/dev/watcher.js +172 -172
  26. package/cli/help.js +114 -112
  27. package/cli/index.js +52 -52
  28. package/cli/scaffold/default/LICENSE +21 -21
  29. package/cli/scaffold/default/app/app.js +207 -207
  30. package/cli/scaffold/default/app/components/about.js +201 -201
  31. package/cli/scaffold/default/app/components/api-demo.js +143 -143
  32. package/cli/scaffold/default/app/components/contact-card.js +231 -231
  33. package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
  34. package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
  35. package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
  36. package/cli/scaffold/default/app/components/counter.js +127 -127
  37. package/cli/scaffold/default/app/components/home.js +249 -249
  38. package/cli/scaffold/default/app/components/not-found.js +16 -16
  39. package/cli/scaffold/default/app/components/playground/playground.css +115 -115
  40. package/cli/scaffold/default/app/components/playground/playground.html +161 -161
  41. package/cli/scaffold/default/app/components/playground/playground.js +116 -116
  42. package/cli/scaffold/default/app/components/todos.js +225 -225
  43. package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
  44. package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
  45. package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
  46. package/cli/scaffold/default/app/routes.js +15 -15
  47. package/cli/scaffold/default/app/store.js +101 -101
  48. package/cli/scaffold/default/global.css +552 -552
  49. package/cli/scaffold/default/index.html +99 -99
  50. package/cli/scaffold/minimal/app/app.js +85 -85
  51. package/cli/scaffold/minimal/app/components/about.js +68 -68
  52. package/cli/scaffold/minimal/app/components/counter.js +122 -122
  53. package/cli/scaffold/minimal/app/components/home.js +68 -68
  54. package/cli/scaffold/minimal/app/components/not-found.js +16 -16
  55. package/cli/scaffold/minimal/app/routes.js +9 -9
  56. package/cli/scaffold/minimal/app/store.js +36 -36
  57. package/cli/scaffold/minimal/global.css +300 -300
  58. package/cli/scaffold/minimal/index.html +44 -44
  59. package/cli/scaffold/ssr/app/app.js +41 -41
  60. package/cli/scaffold/ssr/app/components/about.js +55 -55
  61. package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
  62. package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
  63. package/cli/scaffold/ssr/app/components/home.js +37 -37
  64. package/cli/scaffold/ssr/app/components/not-found.js +15 -15
  65. package/cli/scaffold/ssr/app/routes.js +8 -8
  66. package/cli/scaffold/ssr/global.css +228 -228
  67. package/cli/scaffold/ssr/index.html +37 -37
  68. package/cli/scaffold/ssr/package.json +8 -8
  69. package/cli/scaffold/ssr/server/data/posts.js +144 -144
  70. package/cli/scaffold/ssr/server/index.js +213 -213
  71. package/cli/scaffold/webrtc/app/app.js +11 -0
  72. package/cli/scaffold/webrtc/app/components/video-room.js +441 -0
  73. package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
  74. package/cli/scaffold/webrtc/global.css +250 -0
  75. package/cli/scaffold/webrtc/index.html +21 -0
  76. package/cli/scaffold/webrtc/package.json +16 -0
  77. package/cli/scaffold/webrtc/server/index.js +198 -0
  78. package/cli/utils.js +305 -287
  79. package/dist/API.md +661 -0
  80. package/dist/zquery.dist.zip +0 -0
  81. package/dist/zquery.js +10454 -6614
  82. package/dist/zquery.min.js +8 -631
  83. package/index.d.ts +570 -371
  84. package/index.js +311 -240
  85. package/package.json +77 -70
  86. package/src/component.js +1758 -1691
  87. package/src/core.js +929 -921
  88. package/src/diff.js +497 -497
  89. package/src/errors.js +209 -209
  90. package/src/expression.js +929 -922
  91. package/src/http.js +242 -242
  92. package/src/package.json +1 -1
  93. package/src/reactive.js +271 -255
  94. package/src/router.js +878 -843
  95. package/src/ssr.js +421 -418
  96. package/src/store.js +325 -318
  97. package/src/utils.js +534 -515
  98. package/src/webrtc/e2ee.js +351 -0
  99. package/src/webrtc/errors.js +116 -0
  100. package/src/webrtc/ice.js +301 -0
  101. package/src/webrtc/index.js +131 -0
  102. package/src/webrtc/joinToken.js +119 -0
  103. package/src/webrtc/observe.js +172 -0
  104. package/src/webrtc/peer.js +351 -0
  105. package/src/webrtc/reactive.js +268 -0
  106. package/src/webrtc/room.js +625 -0
  107. package/src/webrtc/sdp.js +302 -0
  108. package/src/webrtc/sfu/index.js +43 -0
  109. package/src/webrtc/sfu/livekit.js +131 -0
  110. package/src/webrtc/sfu/mediasoup.js +150 -0
  111. package/src/webrtc/signaling.js +373 -0
  112. package/src/webrtc/turn.js +237 -0
  113. package/tests/_helpers/webrtcFakes.js +289 -0
  114. package/tests/audit.test.js +4158 -4158
  115. package/tests/bench/render.bench.js +77 -0
  116. package/tests/cli.test.js +1136 -1103
  117. package/tests/compare.test.js +497 -486
  118. package/tests/component.test.js +4076 -3938
  119. package/tests/core.test.js +1926 -1910
  120. package/tests/dev-server.test.js +489 -489
  121. package/tests/diff.test.js +1416 -1416
  122. package/tests/docs.test.js +1664 -1650
  123. package/tests/electron-features.test.js +864 -864
  124. package/tests/errors.test.js +619 -619
  125. package/tests/expression.test.js +1087 -1056
  126. package/tests/http.test.js +693 -648
  127. package/tests/reactive.test.js +844 -819
  128. package/tests/router.test.js +2423 -2327
  129. package/tests/ssr.test.js +916 -870
  130. package/tests/store.test.js +840 -830
  131. package/tests/test-minifier.js +153 -153
  132. package/tests/test-ssr.js +27 -27
  133. package/tests/utils.test.js +1411 -1377
  134. package/tests/webrtc/e2ee.test.js +283 -0
  135. package/tests/webrtc/ice.test.js +202 -0
  136. package/tests/webrtc/joinToken.test.js +89 -0
  137. package/tests/webrtc/observe.test.js +111 -0
  138. package/tests/webrtc/peer.test.js +373 -0
  139. package/tests/webrtc/reactive.test.js +235 -0
  140. package/tests/webrtc/room.test.js +406 -0
  141. package/tests/webrtc/sdp.test.js +151 -0
  142. package/tests/webrtc/sfu-livekit.test.js +119 -0
  143. package/tests/webrtc/sfu.test.js +160 -0
  144. package/tests/webrtc/signaling.test.js +251 -0
  145. package/tests/webrtc/turn.test.js +256 -0
  146. package/types/collection.d.ts +383 -383
  147. package/types/component.d.ts +186 -186
  148. package/types/errors.d.ts +135 -135
  149. package/types/http.d.ts +92 -92
  150. package/types/misc.d.ts +201 -201
  151. package/types/reactive.d.ts +98 -98
  152. package/types/router.d.ts +190 -190
  153. package/types/ssr.d.ts +102 -102
  154. package/types/store.d.ts +146 -146
  155. package/types/utils.d.ts +245 -245
  156. package/types/webrtc.d.ts +653 -0
@@ -1,247 +1,253 @@
1
- /**
2
- * cli/commands/build.js - library build command
3
- *
4
- * Concatenates the zQuery source modules into dist/zquery.js and
5
- * dist/zquery.min.js.
6
- */
7
-
8
- 'use strict';
9
-
10
- const fs = require('fs');
11
- const path = require('path');
12
- const zlib = require('zlib');
13
- const { execSync } = require('child_process');
14
- const { minify, sizeKB } = require('../utils');
15
-
16
- function buildLibrary() {
17
- const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8'));
18
- const VERSION = pkg.version;
19
-
20
- const modules = [
21
- 'src/errors.js',
22
- 'src/reactive.js', 'src/diff.js', 'src/core.js', 'src/expression.js',
23
- 'src/component.js', 'src/router.js', 'src/store.js', 'src/http.js',
24
- 'src/utils.js',
25
- ];
26
-
27
- // Guard: ensure we're inside the zero-query source repo
28
- const missing = modules.filter(m => !fs.existsSync(path.join(process.cwd(), m)));
29
- if (missing.length > 0) {
30
- console.error(` "zquery build" must be run from the zero-query source repository.`);
31
- console.error(` Missing source files: ${missing.join(', ')}`);
32
- console.error(` To bundle your app for production, use "zquery bundle" instead.\n`);
33
- process.exit(1);
34
- }
35
-
36
- const DIST = path.join(process.cwd(), 'dist');
37
- const OUT_FILE = path.join(DIST, 'zquery.js');
38
- const MIN_FILE = path.join(DIST, 'zquery.min.js');
39
-
40
- const start = Date.now();
41
- if (!fs.existsSync(DIST)) fs.mkdirSync(DIST, { recursive: true });
42
-
43
- // -----------------------------------------------------------------------
44
- // Run unit tests and capture results for $.unitTests
45
- // -----------------------------------------------------------------------
46
- let testResults = { passed: 0, failed: 0, total: 0, suites: 0, duration: 0, ok: false };
47
- try {
48
- const json = execSync('npx vitest run --reporter=json', {
49
- cwd: process.cwd(),
50
- encoding: 'utf-8',
51
- stdio: ['pipe', 'pipe', 'pipe'],
52
- timeout: 120000,
53
- });
54
- // vitest --reporter=json outputs JSON to stdout; may include non-JSON lines
55
- const jsonStart = json.indexOf('{');
56
- if (jsonStart !== -1) {
57
- const parsed = JSON.parse(json.slice(jsonStart));
58
- const passed = parsed.numPassedTests || 0;
59
- const failed = parsed.numFailedTests || 0;
60
- const total = parsed.numTotalTests || 0;
61
- const suites = parsed.numTotalTestSuites || 0;
62
- const dur = Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0));
63
- testResults = { passed, failed, total, suites, duration: dur, ok: parsed.success !== false };
64
- }
65
- console.log(` ✓ Tests: ${testResults.passed}/${testResults.total} passed (${testResults.suites} suites)\n`);
66
- } catch (err) {
67
- // Tests may fail but we still want to capture the numbers
68
- const out = (err.stdout || '') + (err.stderr || '');
69
- const jsonStart = out.indexOf('{');
70
- if (jsonStart !== -1) {
71
- try {
72
- const parsed = JSON.parse(out.slice(jsonStart));
73
- testResults = {
74
- passed: parsed.numPassedTests || 0,
75
- failed: parsed.numFailedTests || 0,
76
- total: parsed.numTotalTests || 0,
77
- suites: parsed.numTotalTestSuites || 0,
78
- duration: Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0)),
79
- ok: false,
80
- };
81
- } catch (_) { /* keep defaults */ }
82
- }
83
- console.log(` ⚠ Tests: ${testResults.passed}/${testResults.total} passed, ${testResults.failed} failed\n`);
84
- }
85
-
86
- const parts = modules.map(file => {
87
- let code = fs.readFileSync(path.join(process.cwd(), file), 'utf-8');
88
- code = code.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
89
- code = code.replace(/^export\s+(default\s+)?/gm, '');
90
- code = code.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
91
- return `// --- ${file} ${'-'.repeat(60 - file.length)}\n${code.trim()}`;
92
- });
93
-
94
- let indexCode = fs.readFileSync(path.join(process.cwd(), 'index.js'), 'utf-8');
95
- indexCode = indexCode.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
96
- indexCode = indexCode.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
97
- indexCode = indexCode.replace(/^export\s+(default\s+)?/gm, '');
98
-
99
- const banner = `/**\n * zQuery (zeroQuery) v${VERSION}\n * Lightweight Frontend Library\n * https://github.com/tonywied17/zero-query\n * (c) ${new Date().getFullYear()} Anthony Wiedman - MIT License\n */`;
100
-
101
- const bundle = `${banner}\n(function(global) {\n 'use strict';\n\n${parts.join('\n\n')}\n\n// --- index.js (assembly) ${'-'.repeat(42)}\n${indexCode.trim().replace("'__VERSION__'", `'${VERSION}'`)}\n\n})(typeof window !== 'undefined' ? window : globalThis);\n`;
102
-
103
- fs.writeFileSync(OUT_FILE, bundle, 'utf-8');
104
- const minified = minify(bundle, banner);
105
- fs.writeFileSync(MIN_FILE, minified, 'utf-8');
106
-
107
- // Inject actual minified library size into both outputs
108
- const libSizeKB = Math.round(Buffer.from(minified).length / 1024);
109
- const libSizeStr = `~${libSizeKB} KB`;
110
- const testObj = JSON.stringify(testResults);
111
- let outContent = fs.readFileSync(OUT_FILE, 'utf-8').replace("'__LIB_SIZE__'", `'${libSizeStr}'`);
112
- let minContent = minified.replace("'__LIB_SIZE__'", `'${libSizeStr}'`);
113
- outContent = outContent.replace("'__UNIT_TESTS__'", testObj);
114
- minContent = minContent.replace("'__UNIT_TESTS__'", testObj);
115
- fs.writeFileSync(OUT_FILE, outContent, 'utf-8');
116
- fs.writeFileSync(MIN_FILE, minContent, 'utf-8');
117
-
118
- const elapsed = Date.now() - start;
119
- console.log(` ✓ dist/zquery.js (${sizeKB(fs.readFileSync(OUT_FILE))} KB)`);
120
- console.log(` ✓ dist/zquery.min.js (${sizeKB(fs.readFileSync(MIN_FILE))} KB)`);
121
- console.log(` Done in ${elapsed}ms\n`);
122
-
123
- // --- Generate API.md before zipping --------------------------------------
124
- const root = process.cwd();
125
- try {
126
- const buildApi = require('./build-api');
127
- // buildApi() is async (dynamic imports), so we run it synchronously via
128
- // a child process to keep the build function synchronous.
129
- execSync('node cli/commands/build-api.js', {
130
- cwd: root,
131
- stdio: 'inherit',
132
- timeout: 60000,
133
- });
134
- } catch (err) {
135
- console.log(' API.md generation skipped:', err.message || 'unknown error');
136
- }
137
-
138
- // --- Copy API.md into dist -----------------------------------------------
139
- const apiSrc = path.join(root, 'API.md');
140
- const apiDist = path.join(DIST, 'API.md');
141
- if (fs.existsSync(apiSrc)) {
142
- fs.copyFileSync(apiSrc, apiDist);
143
- console.log(` ✓ dist/API.md (${sizeKB(fs.readFileSync(apiDist))} KB)`);
144
- }
145
-
146
- // --- Create dist/zquery.dist.zip -----------------------------------------
147
- const zipFiles = [
148
- { src: OUT_FILE, name: 'zquery.js' },
149
- { src: MIN_FILE, name: 'zquery.min.js' },
150
- { src: path.join(root, 'LICENSE'), name: 'LICENSE' },
151
- { src: apiDist, name: 'API.md' },
152
- { src: path.join(root, 'README.md'), name: 'README.md' },
153
- ];
154
-
155
- // Minimal ZIP builder (deflate via zlib, no external deps)
156
- function buildZip(entries) {
157
- const localHeaders = [];
158
- const centralHeaders = [];
159
- let offset = 0;
160
-
161
- for (const { name, data } of entries) {
162
- const nameBytes = Buffer.from(name, 'utf-8');
163
- const compressed = zlib.deflateRawSync(data, { level: 9 });
164
- const crc = crc32(data);
165
- const compLen = compressed.length;
166
- const uncompLen = data.length;
167
-
168
- // Local file header
169
- const local = Buffer.alloc(30 + nameBytes.length);
170
- local.writeUInt32LE(0x04034b50, 0); // signature
171
- local.writeUInt16LE(20, 4); // version needed
172
- local.writeUInt16LE(0, 6); // flags
173
- local.writeUInt16LE(8, 8); // compression: deflate
174
- local.writeUInt16LE(0, 10); // mod time
175
- local.writeUInt16LE(0, 12); // mod date
176
- local.writeUInt32LE(crc, 14);
177
- local.writeUInt32LE(compLen, 18);
178
- local.writeUInt32LE(uncompLen, 22);
179
- local.writeUInt16LE(nameBytes.length, 26);
180
- local.writeUInt16LE(0, 28); // extra field length
181
- nameBytes.copy(local, 30);
182
-
183
- localHeaders.push(Buffer.concat([local, compressed]));
184
-
185
- // Central directory header
186
- const central = Buffer.alloc(46 + nameBytes.length);
187
- central.writeUInt32LE(0x02014b50, 0);
188
- central.writeUInt16LE(20, 4); // version made by
189
- central.writeUInt16LE(20, 6); // version needed
190
- central.writeUInt16LE(0, 8); // flags
191
- central.writeUInt16LE(8, 10); // compression: deflate
192
- central.writeUInt16LE(0, 12); // mod time
193
- central.writeUInt16LE(0, 14); // mod date
194
- central.writeUInt32LE(crc, 16);
195
- central.writeUInt32LE(compLen, 20);
196
- central.writeUInt32LE(uncompLen, 24);
197
- central.writeUInt16LE(nameBytes.length, 28);
198
- central.writeUInt16LE(0, 30); // extra field length
199
- central.writeUInt16LE(0, 32); // comment length
200
- central.writeUInt16LE(0, 34); // disk number
201
- central.writeUInt16LE(0, 36); // internal attrs
202
- central.writeUInt32LE(0, 38); // external attrs
203
- central.writeUInt32LE(offset, 42); // local header offset
204
- nameBytes.copy(central, 46);
205
- centralHeaders.push(central);
206
-
207
- offset += local.length + compressed.length;
208
- }
209
-
210
- const centralDir = Buffer.concat(centralHeaders);
211
- const eocd = Buffer.alloc(22);
212
- eocd.writeUInt32LE(0x06054b50, 0);
213
- eocd.writeUInt16LE(0, 4); // disk number
214
- eocd.writeUInt16LE(0, 6); // central dir disk
215
- eocd.writeUInt16LE(entries.length, 8); // entries on disk
216
- eocd.writeUInt16LE(entries.length, 10); // total entries
217
- eocd.writeUInt32LE(centralDir.length, 12);
218
- eocd.writeUInt32LE(offset, 16); // central dir offset
219
- eocd.writeUInt16LE(0, 20); // comment length
220
-
221
- return Buffer.concat([...localHeaders, centralDir, eocd]);
222
- }
223
-
224
- function crc32(buf) {
225
- let crc = 0xFFFFFFFF;
226
- for (let i = 0; i < buf.length; i++) {
227
- crc ^= buf[i];
228
- for (let j = 0; j < 8; j++) {
229
- crc = (crc >>> 1) ^ ((crc & 1) ? 0xEDB88320 : 0);
230
- }
231
- }
232
- return (crc ^ 0xFFFFFFFF) >>> 0;
233
- }
234
-
235
- const entries = zipFiles
236
- .filter(f => fs.existsSync(f.src))
237
- .map(f => ({ name: f.name, data: fs.readFileSync(f.src) }));
238
-
239
- const zipBuf = buildZip(entries);
240
- const zipPath = path.join(DIST, 'zquery.dist.zip');
241
- fs.writeFileSync(zipPath, zipBuf);
242
- console.log(` ✓ dist/zquery.dist.zip (${sizeKB(zipBuf)} KB) - ${entries.length} files`);
243
-
244
- return { DIST, OUT_FILE, MIN_FILE };
245
- }
246
-
247
- module.exports = buildLibrary;
1
+ /**
2
+ * cli/commands/build.js - library build command
3
+ *
4
+ * Concatenates the zQuery source modules into dist/zquery.js and
5
+ * dist/zquery.min.js.
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ const fs = require('fs');
11
+ const path = require('path');
12
+ const zlib = require('zlib');
13
+ const { execSync } = require('child_process');
14
+ const { minify, sizeKB } = require('../utils');
15
+
16
+ function buildLibrary() {
17
+ const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8'));
18
+ const VERSION = pkg.version;
19
+
20
+ const modules = [
21
+ 'src/errors.js',
22
+ 'src/webrtc/errors.js', 'src/webrtc/sdp.js', 'src/webrtc/ice.js', 'src/webrtc/signaling.js', 'src/webrtc/peer.js', 'src/webrtc/room.js', 'src/webrtc/reactive.js', 'src/webrtc/turn.js', 'src/webrtc/e2ee.js', 'src/webrtc/joinToken.js', 'src/webrtc/observe.js', 'src/webrtc/sfu/mediasoup.js', 'src/webrtc/sfu/livekit.js', 'src/webrtc/sfu/index.js', 'src/webrtc/index.js',
23
+ 'src/reactive.js', 'src/diff.js', 'src/core.js', 'src/expression.js',
24
+ 'src/component.js', 'src/router.js', 'src/store.js', 'src/http.js',
25
+ 'src/utils.js',
26
+ ];
27
+
28
+ // Guard: ensure we're inside the zero-query source repo
29
+ const missing = modules.filter(m => !fs.existsSync(path.join(process.cwd(), m)));
30
+ if (missing.length > 0) {
31
+ console.error(` "zquery build" must be run from the zero-query source repository.`);
32
+ console.error(` Missing source files: ${missing.join(', ')}`);
33
+ console.error(` To bundle your app for production, use "zquery bundle" instead.\n`);
34
+ process.exit(1);
35
+ }
36
+
37
+ const DIST = path.join(process.cwd(), 'dist');
38
+ const OUT_FILE = path.join(DIST, 'zquery.js');
39
+ const MIN_FILE = path.join(DIST, 'zquery.min.js');
40
+
41
+ const start = Date.now();
42
+ if (!fs.existsSync(DIST)) fs.mkdirSync(DIST, { recursive: true });
43
+
44
+ // -----------------------------------------------------------------------
45
+ // Run unit tests and capture results for $.unitTests
46
+ // -----------------------------------------------------------------------
47
+ let testResults = { passed: 0, failed: 0, total: 0, suites: 0, duration: 0, ok: false };
48
+ try {
49
+ const json = execSync('npx vitest run --reporter=json', {
50
+ cwd: process.cwd(),
51
+ encoding: 'utf-8',
52
+ stdio: ['pipe', 'pipe', 'pipe'],
53
+ timeout: 120000,
54
+ });
55
+ // vitest --reporter=json outputs JSON to stdout; may include non-JSON lines
56
+ const jsonStart = json.indexOf('{');
57
+ if (jsonStart !== -1) {
58
+ const parsed = JSON.parse(json.slice(jsonStart));
59
+ const passed = parsed.numPassedTests || 0;
60
+ const failed = parsed.numFailedTests || 0;
61
+ const total = parsed.numTotalTests || 0;
62
+ const suites = parsed.numTotalTestSuites || 0;
63
+ const dur = Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0));
64
+ testResults = { passed, failed, total, suites, duration: dur, ok: parsed.success !== false };
65
+ }
66
+ console.log(` ✓ Tests: ${testResults.passed}/${testResults.total} passed (${testResults.suites} suites)\n`);
67
+ } catch (err) {
68
+ // Tests may fail but we still want to capture the numbers
69
+ const out = (err.stdout || '') + (err.stderr || '');
70
+ const jsonStart = out.indexOf('{');
71
+ if (jsonStart !== -1) {
72
+ try {
73
+ const parsed = JSON.parse(out.slice(jsonStart));
74
+ testResults = {
75
+ passed: parsed.numPassedTests || 0,
76
+ failed: parsed.numFailedTests || 0,
77
+ total: parsed.numTotalTests || 0,
78
+ suites: parsed.numTotalTestSuites || 0,
79
+ duration: Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0)),
80
+ ok: false,
81
+ };
82
+ } catch (_) { /* keep defaults */ }
83
+ }
84
+ console.log(` ⚠ Tests: ${testResults.passed}/${testResults.total} passed, ${testResults.failed} failed\n`);
85
+ }
86
+
87
+ const parts = modules.map(file => {
88
+ let code = fs.readFileSync(path.join(process.cwd(), file), 'utf-8');
89
+ code = code.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
90
+ code = code.replace(/^export\s+(?:\*|\{[\s\S]*?\})\s+from\s+['"].*?['"];?\s*$/gm, '');
91
+ code = code.replace(/^export\s+(default\s+)?/gm, '');
92
+ code = code.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
93
+ return `// --- ${file} ${'-'.repeat(60 - file.length)}\n${code.trim()}`;
94
+ });
95
+
96
+ let indexCode = fs.readFileSync(path.join(process.cwd(), 'index.js'), 'utf-8');
97
+ indexCode = indexCode.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
98
+ indexCode = indexCode.replace(/^export\s+(?:\*|\{[\s\S]*?\})\s+from\s+['"].*?['"];?\s*$/gm, '');
99
+ indexCode = indexCode.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
100
+ indexCode = indexCode.replace(/^export\s+(default\s+)?/gm, '');
101
+
102
+ const banner = `/**\n * zQuery (zeroQuery) v${VERSION}\n * Lightweight Frontend Library\n * https://github.com/tonywied17/zero-query\n * (c) ${new Date().getFullYear()} Anthony Wiedman - MIT License\n */`;
103
+
104
+ const bundle = `${banner}\n(function(global) {\n 'use strict';\n\n${parts.join('\n\n')}\n\n// --- index.js (assembly) ${'-'.repeat(42)}\n${indexCode.trim().replace("'__VERSION__'", `'${VERSION}'`)}\n\n})(typeof window !== 'undefined' ? window : globalThis);\n`;
105
+
106
+ fs.writeFileSync(OUT_FILE, bundle, 'utf-8');
107
+ const minified = minify(bundle, banner);
108
+ fs.writeFileSync(MIN_FILE, minified, 'utf-8');
109
+
110
+ // Inject actual minified library size into both outputs.
111
+ // The unminified bundle keeps single quotes; esbuild rewrites the minified
112
+ // output with double quotes, so we tolerate both.
113
+ const libSizeKB = Math.round(Buffer.from(minified).length / 1024);
114
+ const libSizeStr = `~${libSizeKB} KB`;
115
+ const testObj = JSON.stringify(testResults);
116
+ const libRe = /(['"])__LIB_SIZE__\1/g;
117
+ const testRe = /(['"])__UNIT_TESTS__\1/g;
118
+ let outContent = fs.readFileSync(OUT_FILE, 'utf-8').replace(libRe, `'${libSizeStr}'`);
119
+ let minContent = minified.replace(libRe, JSON.stringify(libSizeStr));
120
+ outContent = outContent.replace(testRe, testObj);
121
+ minContent = minContent.replace(testRe, testObj);
122
+ fs.writeFileSync(OUT_FILE, outContent, 'utf-8');
123
+ fs.writeFileSync(MIN_FILE, minContent, 'utf-8');
124
+
125
+ const elapsed = Date.now() - start;
126
+ console.log(` ✓ dist/zquery.js (${sizeKB(fs.readFileSync(OUT_FILE))} KB)`);
127
+ console.log(` ✓ dist/zquery.min.js (${sizeKB(fs.readFileSync(MIN_FILE))} KB)`);
128
+ console.log(` Done in ${elapsed}ms\n`);
129
+
130
+ // --- Generate API.md before zipping --------------------------------------
131
+ const root = process.cwd();
132
+ try {
133
+ // buildApi() is async (dynamic imports), so we run it synchronously via
134
+ // a child process to keep the build function synchronous.
135
+ execSync('node cli/commands/build-api.js', {
136
+ cwd: root,
137
+ stdio: 'inherit',
138
+ timeout: 60000,
139
+ });
140
+ } catch (err) {
141
+ console.log(' API.md generation skipped:', err.message || 'unknown error');
142
+ }
143
+
144
+ // --- Copy API.md into dist -----------------------------------------------
145
+ const apiSrc = path.join(root, 'API.md');
146
+ const apiDist = path.join(DIST, 'API.md');
147
+ if (fs.existsSync(apiSrc)) {
148
+ fs.copyFileSync(apiSrc, apiDist);
149
+ console.log(` ✓ dist/API.md (${sizeKB(fs.readFileSync(apiDist))} KB)`);
150
+ }
151
+
152
+ // --- Create dist/zquery.dist.zip -----------------------------------------
153
+ const zipFiles = [
154
+ { src: OUT_FILE, name: 'zquery.js' },
155
+ { src: MIN_FILE, name: 'zquery.min.js' },
156
+ { src: path.join(root, 'LICENSE'), name: 'LICENSE' },
157
+ { src: apiDist, name: 'API.md' },
158
+ { src: path.join(root, 'README.md'), name: 'README.md' },
159
+ ];
160
+
161
+ // Minimal ZIP builder (deflate via zlib, no external deps)
162
+ function buildZip(entries) {
163
+ const localHeaders = [];
164
+ const centralHeaders = [];
165
+ let offset = 0;
166
+
167
+ for (const { name, data } of entries) {
168
+ const nameBytes = Buffer.from(name, 'utf-8');
169
+ const compressed = zlib.deflateRawSync(data, { level: 9 });
170
+ const crc = crc32(data);
171
+ const compLen = compressed.length;
172
+ const uncompLen = data.length;
173
+
174
+ // Local file header
175
+ const local = Buffer.alloc(30 + nameBytes.length);
176
+ local.writeUInt32LE(0x04034b50, 0); // signature
177
+ local.writeUInt16LE(20, 4); // version needed
178
+ local.writeUInt16LE(0, 6); // flags
179
+ local.writeUInt16LE(8, 8); // compression: deflate
180
+ local.writeUInt16LE(0, 10); // mod time
181
+ local.writeUInt16LE(0, 12); // mod date
182
+ local.writeUInt32LE(crc, 14);
183
+ local.writeUInt32LE(compLen, 18);
184
+ local.writeUInt32LE(uncompLen, 22);
185
+ local.writeUInt16LE(nameBytes.length, 26);
186
+ local.writeUInt16LE(0, 28); // extra field length
187
+ nameBytes.copy(local, 30);
188
+
189
+ localHeaders.push(Buffer.concat([local, compressed]));
190
+
191
+ // Central directory header
192
+ const central = Buffer.alloc(46 + nameBytes.length);
193
+ central.writeUInt32LE(0x02014b50, 0);
194
+ central.writeUInt16LE(20, 4); // version made by
195
+ central.writeUInt16LE(20, 6); // version needed
196
+ central.writeUInt16LE(0, 8); // flags
197
+ central.writeUInt16LE(8, 10); // compression: deflate
198
+ central.writeUInt16LE(0, 12); // mod time
199
+ central.writeUInt16LE(0, 14); // mod date
200
+ central.writeUInt32LE(crc, 16);
201
+ central.writeUInt32LE(compLen, 20);
202
+ central.writeUInt32LE(uncompLen, 24);
203
+ central.writeUInt16LE(nameBytes.length, 28);
204
+ central.writeUInt16LE(0, 30); // extra field length
205
+ central.writeUInt16LE(0, 32); // comment length
206
+ central.writeUInt16LE(0, 34); // disk number
207
+ central.writeUInt16LE(0, 36); // internal attrs
208
+ central.writeUInt32LE(0, 38); // external attrs
209
+ central.writeUInt32LE(offset, 42); // local header offset
210
+ nameBytes.copy(central, 46);
211
+ centralHeaders.push(central);
212
+
213
+ offset += local.length + compressed.length;
214
+ }
215
+
216
+ const centralDir = Buffer.concat(centralHeaders);
217
+ const eocd = Buffer.alloc(22);
218
+ eocd.writeUInt32LE(0x06054b50, 0);
219
+ eocd.writeUInt16LE(0, 4); // disk number
220
+ eocd.writeUInt16LE(0, 6); // central dir disk
221
+ eocd.writeUInt16LE(entries.length, 8); // entries on disk
222
+ eocd.writeUInt16LE(entries.length, 10); // total entries
223
+ eocd.writeUInt32LE(centralDir.length, 12);
224
+ eocd.writeUInt32LE(offset, 16); // central dir offset
225
+ eocd.writeUInt16LE(0, 20); // comment length
226
+
227
+ return Buffer.concat([...localHeaders, centralDir, eocd]);
228
+ }
229
+
230
+ function crc32(buf) {
231
+ let crc = 0xFFFFFFFF;
232
+ for (let i = 0; i < buf.length; i++) {
233
+ crc ^= buf[i];
234
+ for (let j = 0; j < 8; j++) {
235
+ crc = (crc >>> 1) ^ ((crc & 1) ? 0xEDB88320 : 0);
236
+ }
237
+ }
238
+ return (crc ^ 0xFFFFFFFF) >>> 0;
239
+ }
240
+
241
+ const entries = zipFiles
242
+ .filter(f => fs.existsSync(f.src))
243
+ .map(f => ({ name: f.name, data: fs.readFileSync(f.src) }));
244
+
245
+ const zipBuf = buildZip(entries);
246
+ const zipPath = path.join(DIST, 'zquery.dist.zip');
247
+ fs.writeFileSync(zipPath, zipBuf);
248
+ console.log(` ✓ dist/zquery.dist.zip (${sizeKB(zipBuf)} KB) - ${entries.length} files`);
249
+
250
+ return { DIST, OUT_FILE, MIN_FILE };
251
+ }
252
+
253
+ module.exports = buildLibrary;