freertc 0.1.14 → 0.1.16

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.14","protocol_version":"1.0","peers":0}
219
+ {"ok":true,"version":"0.1.16","protocol_version":"1.0","peers":0}
220
220
  ```
221
221
 
222
222
  ## Auto WebRTC two-tab test
package/bin/freertc.mjs CHANGED
@@ -186,6 +186,61 @@ function validateWranglerConfigForDeploy(configPath) {
186
186
  }
187
187
  }
188
188
 
189
+ function extractRouteConflictOwner(outputText) {
190
+ if (!outputText) return null;
191
+ const match = outputText.match(/used by Worker:\s*([A-Za-z0-9._-]+)/i);
192
+ return match?.[1] || null;
193
+ }
194
+
195
+ function patchWranglerScriptName(configPath, workerName) {
196
+ if (!workerName) return false;
197
+ const text = fs.readFileSync(configPath, 'utf8');
198
+ const updated = text.replace(/^(\s*"name"\s*:\s*)"[^"]*"/gm, `$1"${workerName}"`);
199
+ if (updated === text) return false;
200
+ fs.writeFileSync(configPath, updated, 'utf8');
201
+ return true;
202
+ }
203
+
204
+ function runDeployWithAutoRouteRecovery(wrangler, configPath, extraArgs = []) {
205
+ const attemptedOwners = new Set();
206
+
207
+ for (let attempt = 0; attempt < 2; attempt += 1) {
208
+ const args = [...wrangler.baseArgs, 'deploy', '--config', configPath, '--env', 'production', ...extraArgs];
209
+ const result = spawnSync(wrangler.command, args, {
210
+ cwd: PROJECT_ROOT,
211
+ stdio: 'pipe',
212
+ encoding: 'utf8',
213
+ env: {
214
+ ...process.env,
215
+ FREERTC_PACKAGE_ROOT: PACKAGE_ROOT
216
+ }
217
+ });
218
+
219
+ if (result.stdout) process.stdout.write(result.stdout);
220
+ if (result.stderr) process.stderr.write(result.stderr);
221
+
222
+ if (typeof result.status === 'number' && result.status === 0) {
223
+ process.exit(0);
224
+ }
225
+
226
+ const combinedOutput = `${result.stdout || ''}\n${result.stderr || ''}`;
227
+ const conflictOwner = extractRouteConflictOwner(combinedOutput);
228
+ if (!conflictOwner || attemptedOwners.has(conflictOwner)) {
229
+ process.exit(typeof result.status === 'number' ? result.status : 1);
230
+ }
231
+
232
+ attemptedOwners.add(conflictOwner);
233
+ const patched = patchWranglerScriptName(configPath, conflictOwner);
234
+ if (!patched) {
235
+ process.exit(typeof result.status === 'number' ? result.status : 1);
236
+ }
237
+
238
+ console.log(`Detected route owned by worker "${conflictOwner}". Retrying deploy with that worker name automatically.`);
239
+ }
240
+
241
+ process.exit(1);
242
+ }
243
+
189
244
  function compareVersions(left, right) {
190
245
  const leftParts = String(left).split('.').map((value) => Number.parseInt(value, 10) || 0);
191
246
  const rightParts = String(right).split('.').map((value) => Number.parseInt(value, 10) || 0);
@@ -237,8 +292,50 @@ async function assertLatestCliForDeploy() {
237
292
  }
238
293
  }
239
294
 
295
+ async function maybeHandoffToLatestCli(rawArgs = []) {
296
+ if (process.env.FREERTC_NO_AUTO_HANDOFF === '1') {
297
+ return;
298
+ }
299
+
300
+ // Avoid network checks for explicit help output.
301
+ const firstArg = rawArgs[0];
302
+ if (firstArg === '--help' || firstArg === '-h' || firstArg === 'help') {
303
+ return;
304
+ }
305
+
306
+ try {
307
+ const latestVersion = await fetchLatestCliVersion();
308
+ if (compareVersions(CLI_VERSION, latestVersion) >= 0) {
309
+ return;
310
+ }
311
+
312
+ console.log(`Detected newer freertc version ${latestVersion} (current ${CLI_VERSION}).`);
313
+ console.log('Running command with latest CLI automatically...');
314
+
315
+ const args = ['-y', `freertc@${latestVersion}`, ...rawArgs];
316
+ const result = spawnSync('npx', args, {
317
+ cwd: PROJECT_ROOT,
318
+ stdio: 'inherit',
319
+ env: {
320
+ ...process.env,
321
+ FREERTC_NO_AUTO_HANDOFF: '1'
322
+ }
323
+ });
324
+
325
+ if (typeof result.status === 'number') {
326
+ process.exit(result.status);
327
+ }
328
+ process.exit(1);
329
+ } catch {
330
+ // Offline or registry hiccup: continue with local CLI.
331
+ }
332
+ }
333
+
240
334
  async function main() {
241
335
  const [, , subcommand, ...rest] = process.argv;
336
+ const rawArgs = process.argv.slice(2);
337
+
338
+ await maybeHandoffToLatestCli(rawArgs);
242
339
 
243
340
  if (!subcommand) {
244
341
  runInProject(process.execPath, [path.join(PACKAGE_ROOT, 'scripts', 'wrangler-install-wizard.mjs'), '--mode', 'both'], { bootstrap: true });
@@ -268,7 +365,7 @@ async function main() {
268
365
  autoPatchWranglerConfig(configPath);
269
366
  validateWranglerConfigForDeploy(configPath);
270
367
  const wrangler = resolveWranglerCommand(PROJECT_ROOT);
271
- runInProject(wrangler.command, [...wrangler.baseArgs, 'deploy', '--config', configPath, '--env', 'production', ...rest]);
368
+ runDeployWithAutoRouteRecovery(wrangler, configPath, rest);
272
369
  }
273
370
 
274
371
  if (subcommand === 'dev') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freertc",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
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.14';
10
+ const WORKER_VERSION = '0.1.16';
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.14";
2
+ const WORKER_VERSION = "0.1.16";
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"]);