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
@@ -0,0 +1,21 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>{{NAME}} - zQuery WebRTC Demo</title>
7
+ <base href="/">
8
+ <link rel="stylesheet" href="global.css">
9
+ <script src="zquery.min.js"></script>
10
+ <script type="module" src="app/app.js"></script>
11
+ </head>
12
+ <body>
13
+ <header class="topbar">
14
+ <span class="brand">⚡ {{NAME}}</span>
15
+ <small>zQuery <span id="nav-version"></span> &middot; WebRTC demo</small>
16
+ </header>
17
+ <main>
18
+ <video-room></video-room>
19
+ </main>
20
+ </body>
21
+ </html>
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "{{NAME}}",
3
+ "private": true,
4
+ "type": "commonjs",
5
+ "scripts": {
6
+ "start": "node server/index.js",
7
+ "dev": "npx zquery dev"
8
+ },
9
+ "dependencies": {
10
+ "zero-query": "latest"
11
+ },
12
+ "devDependencies": {
13
+ "@zero-server/sdk": "^0.9.8",
14
+ "@zero-server/webrtc": "^0.9.8"
15
+ }
16
+ }
@@ -0,0 +1,198 @@
1
+ // server/index.js - WebRTC signaling + TURN backend
2
+ //
3
+ // Hosts the zQuery webrtc demo over real WebSocket signaling using
4
+ // @zero-server/webrtc's SignalingHub. Browsers connect with
5
+ // `$.webrtc.join('ws://host:PORT/rtc', { room })` and exchange offers /
6
+ // answers / ICE through the hub - so two real machines (not just two
7
+ // tabs in the same browser) can call each other.
8
+ //
9
+ // Usage:
10
+ // node server/index.js # listens on PORT (default 3000)
11
+ // PORT=8080 node server/index.js
12
+ //
13
+ // Optional environment variables:
14
+ // WEBRTC_JWT_SECRET - if set, joins must include a signed join token.
15
+ // GET /rtc/token/:room returns a short-TTL token.
16
+ // TURN_SECRET - if set, GET /rtc/turn returns ephemeral
17
+ // RFC 7635 TURN credentials. Otherwise it returns
18
+ // a plain STUN-only iceServers list.
19
+ // TURN_URLS - comma-separated TURN/STUN URLs to hand out
20
+ // (e.g. 'turn:turn.example.com:3478?transport=udp').
21
+ //
22
+ // @zero-server/sdk and @zero-server/webrtc are devDependencies declared in
23
+ // package.json. If they aren't installed yet, this script prompts to
24
+ // install them once on first run (same pattern as the dev server does
25
+ // for zero-http).
26
+
27
+ 'use strict';
28
+
29
+ const path = require('node:path');
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // Install prompt
33
+ // ---------------------------------------------------------------------------
34
+
35
+ /**
36
+ * Prompt the user to install a missing devDependency. Resolves true on yes.
37
+ */
38
+ function promptInstall(label) {
39
+ const rl = require('node:readline').createInterface({
40
+ input: process.stdin,
41
+ output: process.stdout,
42
+ });
43
+ return new Promise((resolve) => {
44
+ rl.question(
45
+ '\n This server needs ' + label + ', which is not installed.\n' +
46
+ ' These packages are only used by the demo server and are not\n' +
47
+ ' required for the client bundle, building, or production.\n' +
48
+ ' Install them now? (y/n): ',
49
+ (answer) => {
50
+ rl.close();
51
+ resolve(answer.trim().toLowerCase() === 'y');
52
+ }
53
+ );
54
+ });
55
+ }
56
+
57
+ /**
58
+ * `require` a package; on failure, prompt the user to npm-install it as a
59
+ * devDependency, then re-require. Exits the process if the user declines.
60
+ */
61
+ async function requireOrInstall(pkgs) {
62
+ try {
63
+ return pkgs.map((p) => require(p));
64
+ } catch (err) {
65
+ if (err.code !== 'MODULE_NOT_FOUND') throw err;
66
+ const ok = await promptInstall(pkgs.join(' + '));
67
+ if (!ok) {
68
+ console.error('\n ✖ Cannot start the WebRTC server without these packages.\n');
69
+ process.exit(1);
70
+ }
71
+ const { execSync } = require('node:child_process');
72
+ const args = pkgs.join(' ');
73
+ console.log('\n Installing ' + args + '...\n');
74
+ execSync('npm install --save-dev ' + args, { stdio: 'inherit' });
75
+ return pkgs.map((p) => require(p));
76
+ }
77
+ }
78
+
79
+ // ---------------------------------------------------------------------------
80
+ // Bootstrap
81
+ // ---------------------------------------------------------------------------
82
+
83
+ async function main() {
84
+ const [sdk, webrtc] = await requireOrInstall([
85
+ '@zero-server/sdk',
86
+ '@zero-server/webrtc',
87
+ ]);
88
+
89
+ const {
90
+ createApp,
91
+ helmet,
92
+ cors,
93
+ compress,
94
+ static: serveStatic,
95
+ } = sdk;
96
+ const {
97
+ SignalingHub,
98
+ signJoinToken,
99
+ issueTurnCredentials,
100
+ } = webrtc;
101
+
102
+ const PORT = parseInt(process.env.PORT || '3000', 10);
103
+ const ROOT = path.resolve(__dirname, '..');
104
+ const JWT = process.env.WEBRTC_JWT_SECRET || null;
105
+ const TURN_SEC = process.env.TURN_SECRET || null;
106
+ const TURN_URLS = (process.env.TURN_URLS || '').split(',').map((s) => s.trim()).filter(Boolean);
107
+ const STUN_FALLBACK = ['stun:stun.l.google.com:19302'];
108
+
109
+ const app = createApp();
110
+
111
+ // ---- Security & static assets ----
112
+ app.use(helmet({
113
+ contentSecurityPolicy: {
114
+ directives: {
115
+ defaultSrc: ["'self'"],
116
+ scriptSrc: ["'self'", "'unsafe-inline'"],
117
+ styleSrc: ["'self'", "'unsafe-inline'"],
118
+ imgSrc: ["'self'", 'data:', 'blob:'],
119
+ mediaSrc: ["'self'", 'blob:'],
120
+ connectSrc: ["'self'", 'ws:', 'wss:'],
121
+ fontSrc: ["'self'", 'data:'],
122
+ },
123
+ },
124
+ hsts: false,
125
+ }));
126
+ app.use(cors());
127
+ app.use(compress({ threshold: 1024 }));
128
+ app.use(serveStatic(ROOT, { index: 'index.html' }));
129
+
130
+ // ---- Signaling hub ----
131
+ const hubOpts = {};
132
+ if (JWT) hubOpts.joinTokenSecret = JWT;
133
+ const hub = new SignalingHub(hubOpts);
134
+
135
+ app.ws('/rtc', (ws, req) => {
136
+ hub.attach(ws, {
137
+ ip: req.ip,
138
+ origin: req.headers && req.headers.origin,
139
+ });
140
+ });
141
+
142
+ hub.on('join', ({ peer, room }) => console.log(' + peer', peer.id, 'joined', room.name));
143
+ hub.on('leave', ({ peer, room }) => console.log(' - peer', peer.id, 'left', room.name));
144
+
145
+ // ---- Join token endpoint (only when WEBRTC_JWT_SECRET is set) ----
146
+ app.get('/rtc/token/:room', (req, res) => {
147
+ if (!JWT) return res.json({ wsUrl: _wsUrl(req), token: null });
148
+ const token = signJoinToken({
149
+ secret: JWT,
150
+ user: { id: 'anon-' + Math.random().toString(36).slice(2, 10) },
151
+ room: req.params.room,
152
+ ttl: 300,
153
+ });
154
+ res.json({ wsUrl: _wsUrl(req), token });
155
+ });
156
+
157
+ // ---- TURN/STUN credential endpoint ----
158
+ app.get('/rtc/turn', (req, res) => {
159
+ if (TURN_SEC && TURN_URLS.length) {
160
+ const creds = issueTurnCredentials({
161
+ secret: TURN_SEC,
162
+ userId: 'anon-' + Math.random().toString(36).slice(2, 10),
163
+ ttl: '20m',
164
+ servers: TURN_URLS,
165
+ });
166
+ return res.json({ iceServers: [creds] });
167
+ }
168
+ // Dev fallback - public STUN only. Works for two machines on the
169
+ // same network but cannot traverse symmetric NATs without TURN.
170
+ const urls = TURN_URLS.length ? TURN_URLS : STUN_FALLBACK;
171
+ res.json({ iceServers: [{ urls }] });
172
+ });
173
+
174
+ // ---- Listen ----
175
+ app.listen(PORT, () => {
176
+ console.log('\n ⚡ WebRTC server → http://localhost:' + PORT);
177
+ console.log(' • signaling ws://localhost:' + PORT + '/rtc');
178
+ console.log(' • turn creds http://localhost:' + PORT + '/rtc/turn');
179
+ if (JWT) {
180
+ console.log(' • join tokens http://localhost:' + PORT + '/rtc/token/<room>');
181
+ } else {
182
+ console.log(' • join tokens disabled (set WEBRTC_JWT_SECRET to enable)');
183
+ }
184
+ console.log('');
185
+ });
186
+ }
187
+
188
+ function _wsUrl(req) {
189
+ const host = (req.headers && req.headers.host) || 'localhost:3000';
190
+ const proto = (req.headers && req.headers['x-forwarded-proto'] === 'https') ? 'wss' : 'ws';
191
+ return proto + '://' + host + '/rtc';
192
+ }
193
+
194
+ main().catch((err) => {
195
+ console.error('\n ✖ WebRTC server failed to start:\n');
196
+ console.error(err);
197
+ process.exit(1);
198
+ });