freertc 0.1.6 → 0.1.8

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 CHANGED
@@ -216,7 +216,7 @@ Quick checks:
216
216
  Expected `/health` response includes JSON like:
217
217
 
218
218
  ```json
219
- {"ok":true,"version":"0.1.6","protocol_version":"1.0","peers":0}
219
+ {"ok":true,"version":"0.1.8","protocol_version":"1.0","peers":0}
220
220
  ```
221
221
 
222
222
  ## Auto WebRTC two-tab test
package/bin/freertc.mjs CHANGED
@@ -9,6 +9,8 @@ import { ensureProjectFiles, resolveProjectRoot, resolveWranglerCommand } from '
9
9
  const __filename = fileURLToPath(import.meta.url);
10
10
  const PACKAGE_ROOT = path.resolve(path.dirname(__filename), '..');
11
11
  const PROJECT_ROOT = resolveProjectRoot(process.cwd());
12
+ const PACKAGE_JSON = JSON.parse(fs.readFileSync(path.join(PACKAGE_ROOT, 'package.json'), 'utf8'));
13
+ const CLI_VERSION = PACKAGE_JSON.version;
12
14
 
13
15
  function printHelp() {
14
16
  console.log(`freertc CLI
@@ -56,51 +58,146 @@ function runInProject(command, args, { bootstrap = false } = {}) {
56
58
  function requireWranglerConfig() {
57
59
  const configPath = path.join(PROJECT_ROOT, 'wrangler.jsonc');
58
60
  if (fs.existsSync(configPath)) {
59
- return;
61
+ return configPath;
60
62
  }
61
63
 
62
64
  console.error(`Missing ${configPath}. Run "npx freertc" or "npx freertc wizard" from this project directory first.`);
63
65
  process.exit(1);
64
66
  }
65
67
 
66
- const [, , subcommand, ...rest] = process.argv;
68
+ function validateWranglerConfigForDeploy(configPath) {
69
+ const configText = fs.readFileSync(configPath, 'utf8');
70
+ const failures = [];
71
+
72
+ const placeholderChecks = [
73
+ { test: /YOUR_D1_DATABASE_ID/, message: 'Replace YOUR_D1_DATABASE_ID in wrangler.jsonc.' },
74
+ { test: /Your App Relay/, message: 'Set vars.RELAY_NAME to your actual relay name.' },
75
+ { test: /wss:\/\/your-domain\.example\/ws/i, message: 'Set vars.RELAY_URL to your real deployed relay WebSocket URL.' },
76
+ { test: /"main"\s*:\s*"build\/worker\/shim\.mjs"/, message: 'Switch wrangler.jsonc main to src/index.js unless you intentionally use the Rust/WASM worker path.' },
77
+ { test: /"command"\s*:\s*"worker-build --release"/, message: 'Remove the worker-build step unless you intentionally deploy the Rust/WASM worker path.' }
78
+ ];
79
+
80
+ for (const check of placeholderChecks) {
81
+ if (check.test.test(configText)) {
82
+ failures.push(check.message);
83
+ }
84
+ }
67
85
 
68
- if (!subcommand) {
69
- runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), '--mode', 'both'], { bootstrap: true });
70
- }
86
+ const relayUrlMatch = configText.match(/"RELAY_URL"\s*:\s*"([^"]*)"/);
87
+ if (!relayUrlMatch || !/^wss:\/\/[^\s"/]+\/ws$/i.test(relayUrlMatch[1])) {
88
+ failures.push('Set vars.RELAY_URL to a valid wss://<host>/ws URL.');
89
+ }
71
90
 
72
- if (subcommand === '--help' || subcommand === '-h' || subcommand === 'help') {
73
- printHelp();
74
- process.exit(0);
75
- }
91
+ const relayNameMatch = configText.match(/"RELAY_NAME"\s*:\s*"([^"]*)"/);
92
+ if (!relayNameMatch || !relayNameMatch[1].trim()) {
93
+ failures.push('Set vars.RELAY_NAME to a non-empty relay name.');
94
+ }
76
95
 
77
- if (subcommand === 'wizard') {
78
- runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), ...rest], { bootstrap: true });
96
+ if (failures.length > 0) {
97
+ console.error(`Refusing to deploy with placeholder or invalid wrangler config: ${configPath}`);
98
+ for (const failure of failures) {
99
+ console.error(`- ${failure}`);
100
+ }
101
+ console.error('Run "npx freertc wizard" in the domain repo to patch wrangler.jsonc before deploying.');
102
+ process.exit(1);
103
+ }
79
104
  }
80
105
 
81
- if (subcommand === 'setup') {
82
- runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), '--mode', 'both', ...rest], { bootstrap: true });
83
- }
106
+ function compareVersions(left, right) {
107
+ const leftParts = String(left).split('.').map((value) => Number.parseInt(value, 10) || 0);
108
+ const rightParts = String(right).split('.').map((value) => Number.parseInt(value, 10) || 0);
109
+ const length = Math.max(leftParts.length, rightParts.length);
84
110
 
85
- if (subcommand === 'init' || subcommand === 'install') {
86
- runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), '--mode', 'both', ...rest], { bootstrap: true });
111
+ for (let index = 0; index < length; index += 1) {
112
+ const leftValue = leftParts[index] ?? 0;
113
+ const rightValue = rightParts[index] ?? 0;
114
+ if (leftValue > rightValue) return 1;
115
+ if (leftValue < rightValue) return -1;
116
+ }
117
+
118
+ return 0;
87
119
  }
88
120
 
89
- if (subcommand === 'deploy') {
90
- ensureProjectFiles(PROJECT_ROOT);
91
- requireWranglerConfig();
92
- const wrangler = resolveWranglerCommand(PROJECT_ROOT);
93
- runInProject(wrangler.command, [...wrangler.baseArgs, 'deploy', '--env', 'production', ...rest]);
121
+ async function fetchLatestCliVersion() {
122
+ const response = await fetch('https://registry.npmjs.org/freertc/latest', {
123
+ headers: { Accept: 'application/json' }
124
+ });
125
+ if (!response.ok) {
126
+ throw new Error(`npm registry responded with ${response.status}`);
127
+ }
128
+
129
+ const payload = await response.json();
130
+ const version = payload?.version;
131
+ if (typeof version !== 'string' || !version.trim()) {
132
+ throw new Error('npm registry response did not include a version');
133
+ }
134
+
135
+ return version;
94
136
  }
95
137
 
96
- if (subcommand === 'dev') {
97
- runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'non-cloudflare-server.mjs'), ...rest], { bootstrap: true });
138
+ async function assertLatestCliForDeploy() {
139
+ if (process.env.FREERTC_SKIP_LATEST_CHECK === '1') {
140
+ return;
141
+ }
142
+
143
+ try {
144
+ const latestVersion = await fetchLatestCliVersion();
145
+ if (compareVersions(CLI_VERSION, latestVersion) < 0) {
146
+ console.error(`freertc ${CLI_VERSION} is older than npm latest ${latestVersion}.`);
147
+ console.error('Use "npx freertc@latest deploy" or update the local freertc dependency before deploying.');
148
+ process.exit(1);
149
+ }
150
+ } catch (error) {
151
+ console.error(`Unable to verify freertc version before deploy: ${error?.message || String(error)}`);
152
+ console.error('Set FREERTC_SKIP_LATEST_CHECK=1 to bypass this check if you need an offline deploy.');
153
+ process.exit(1);
154
+ }
98
155
  }
99
156
 
100
- if (subcommand === 'dev:cf' || subcommand === 'dev-cf') {
101
- runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'dev-server.mjs'), ...rest], { bootstrap: true });
157
+ async function main() {
158
+ const [, , subcommand, ...rest] = process.argv;
159
+
160
+ if (!subcommand) {
161
+ runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), '--mode', 'both'], { bootstrap: true });
162
+ }
163
+
164
+ if (subcommand === '--help' || subcommand === '-h' || subcommand === 'help') {
165
+ printHelp();
166
+ process.exit(0);
167
+ }
168
+
169
+ if (subcommand === 'wizard') {
170
+ runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), ...rest], { bootstrap: true });
171
+ }
172
+
173
+ if (subcommand === 'setup') {
174
+ runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), '--mode', 'both', ...rest], { bootstrap: true });
175
+ }
176
+
177
+ if (subcommand === 'init' || subcommand === 'install') {
178
+ runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), '--mode', 'both', ...rest], { bootstrap: true });
179
+ }
180
+
181
+ if (subcommand === 'deploy') {
182
+ await assertLatestCliForDeploy();
183
+ ensureProjectFiles(PROJECT_ROOT);
184
+ const configPath = requireWranglerConfig();
185
+ validateWranglerConfigForDeploy(configPath);
186
+ const wrangler = resolveWranglerCommand(PROJECT_ROOT);
187
+ runInProject(wrangler.command, [...wrangler.baseArgs, 'deploy', '--env', 'production', ...rest]);
188
+ }
189
+
190
+ if (subcommand === 'dev') {
191
+ runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'non-cloudflare-server.mjs'), ...rest], { bootstrap: true });
192
+ }
193
+
194
+ if (subcommand === 'dev:cf' || subcommand === 'dev-cf') {
195
+ runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'dev-server.mjs'), ...rest], { bootstrap: true });
196
+ }
197
+
198
+ console.error(`Unknown command: ${subcommand}\n`);
199
+ printHelp();
200
+ process.exit(1);
102
201
  }
103
202
 
104
- console.error(`Unknown command: ${subcommand}\n`);
105
- printHelp();
106
- process.exit(1);
203
+ await main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freertc",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Cloudflare Worker signaling relay for WebRTC peers with D1 storage.",
5
5
  "keywords": [
6
6
  "webrtc",
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';
7
7
  import { WebSocketServer } from 'ws';
8
8
 
9
9
  const PSP_VERSION = '1.0';
10
- const WORKER_VERSION = '0.1.6';
10
+ const WORKER_VERSION = '0.1.8';
11
11
  const DEFAULT_TTL_MS = 30_000;
12
12
  const MAX_TTL_MS = 120_000;
13
13
  const MAX_MESSAGE_SIZE = 64 * 1024;
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const PSP_VERSION = "1.0";
2
- const WORKER_VERSION = "0.1.6";
2
+ const WORKER_VERSION = "0.1.8";
3
3
 
4
4
  const DISCOVERY_TYPES = new Set(["announce", "withdraw", "discover", "peer_list", "redirect"]);
5
5
  const NEGOTIATION_TYPES = new Set(["connect_request", "connect_accept", "connect_reject", "offer", "answer", "ice_candidate", "ice_end", "renegotiate"]);