freertc 0.1.16 → 0.1.18
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 +1 -1
- package/bin/freertc.mjs +131 -0
- package/package.json +1 -1
- package/scripts/non-cloudflare-server.mjs +1 -1
- package/scripts/postinstall-message.mjs +85 -0
- package/src/index.js +1 -1
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.
|
|
219
|
+
{"ok":true,"version":"0.1.18","protocol_version":"1.0","peers":0}
|
|
220
220
|
```
|
|
221
221
|
|
|
222
222
|
## Auto WebRTC two-tab test
|
package/bin/freertc.mjs
CHANGED
|
@@ -102,6 +102,11 @@ function extractRouteHost(configText) {
|
|
|
102
102
|
return normalizeHost(pattern.split('/')[0]);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
function extractCurrentWorkerName(configText) {
|
|
106
|
+
const match = configText.match(/"name"\s*:\s*"([^"]+)"/);
|
|
107
|
+
return match?.[1] || null;
|
|
108
|
+
}
|
|
109
|
+
|
|
105
110
|
function extractRelayUrlHost(configText) {
|
|
106
111
|
const relayUrlMatch = configText.match(/"RELAY_URL"\s*:\s*"([^"]*)"/);
|
|
107
112
|
if (!relayUrlMatch) return null;
|
|
@@ -201,6 +206,131 @@ function patchWranglerScriptName(configPath, workerName) {
|
|
|
201
206
|
return true;
|
|
202
207
|
}
|
|
203
208
|
|
|
209
|
+
function getApiToken() {
|
|
210
|
+
return process.env.CLOUDFLARE_API_TOKEN || process.env.CF_API_TOKEN || '';
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function getAccountId() {
|
|
214
|
+
return process.env.CLOUDFLARE_ACCOUNT_ID || process.env.CF_ACCOUNT_ID || '';
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function guessZoneCandidates(host) {
|
|
218
|
+
const normalized = normalizeHost(host);
|
|
219
|
+
if (!normalized) return [];
|
|
220
|
+
const parts = normalized.split('.');
|
|
221
|
+
const candidates = [];
|
|
222
|
+
for (let i = 0; i <= Math.max(0, parts.length - 2); i += 1) {
|
|
223
|
+
candidates.push(parts.slice(i).join('.'));
|
|
224
|
+
}
|
|
225
|
+
return Array.from(new Set(candidates));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function routePatternMatchesHost(pattern, host) {
|
|
229
|
+
const normalizedHost = normalizeHost(host);
|
|
230
|
+
if (!pattern || !normalizedHost) return false;
|
|
231
|
+
const rawHost = normalizeHost(String(pattern).split('/')[0]);
|
|
232
|
+
if (!rawHost) return false;
|
|
233
|
+
if (rawHost === normalizedHost) return true;
|
|
234
|
+
if (rawHost.startsWith('*.')) {
|
|
235
|
+
const suffix = rawHost.slice(2);
|
|
236
|
+
return normalizedHost === suffix || normalizedHost.endsWith(`.${suffix}`);
|
|
237
|
+
}
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function apiGetJson(url, token) {
|
|
242
|
+
const response = await fetch(url, {
|
|
243
|
+
headers: {
|
|
244
|
+
Authorization: `Bearer ${token}`,
|
|
245
|
+
Accept: 'application/json'
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
if (!response.ok) {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
let payload;
|
|
253
|
+
try {
|
|
254
|
+
payload = await response.json();
|
|
255
|
+
} catch {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (!payload?.success) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return payload.result;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function resolveZoneIdForHost(host, token) {
|
|
267
|
+
const candidates = guessZoneCandidates(host);
|
|
268
|
+
for (const candidate of candidates) {
|
|
269
|
+
const result = await apiGetJson(`https://api.cloudflare.com/client/v4/zones?name=${encodeURIComponent(candidate)}`, token);
|
|
270
|
+
if (Array.isArray(result) && result.length > 0 && result[0]?.id) {
|
|
271
|
+
return result[0].id;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function findRouteOwnerForHost(host) {
|
|
278
|
+
const token = getApiToken();
|
|
279
|
+
const accountId = getAccountId();
|
|
280
|
+
if (!token || !accountId) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const zoneId = await resolveZoneIdForHost(host, token);
|
|
285
|
+
if (!zoneId) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const routes = await apiGetJson(`https://api.cloudflare.com/client/v4/zones/${zoneId}/workers/routes`, token);
|
|
290
|
+
if (!Array.isArray(routes)) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Prefer exact host matches over wildcard matches.
|
|
295
|
+
const exact = routes.find((route) => {
|
|
296
|
+
const patternHost = normalizeHost(String(route?.pattern || '').split('/')[0]);
|
|
297
|
+
const scriptName = route?.script || route?.script_name;
|
|
298
|
+
return patternHost === normalizeHost(host) && typeof scriptName === 'string' && scriptName.trim();
|
|
299
|
+
});
|
|
300
|
+
if (exact?.script || exact?.script_name) {
|
|
301
|
+
return exact.script || exact.script_name;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const wildcard = routes.find((route) => {
|
|
305
|
+
const scriptName = route?.script || route?.script_name;
|
|
306
|
+
return routePatternMatchesHost(route?.pattern, host) && typeof scriptName === 'string' && scriptName.trim();
|
|
307
|
+
});
|
|
308
|
+
return wildcard ? (wildcard.script || wildcard.script_name) : null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async function maybeAlignWorkerNameToRouteOwner(configPath) {
|
|
312
|
+
const text = fs.readFileSync(configPath, 'utf8');
|
|
313
|
+
const host = extractRouteHost(text) || extractRelayUrlHost(text) || extractDatabaseDomainHost(text);
|
|
314
|
+
if (!host) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const routeOwner = await findRouteOwnerForHost(host);
|
|
319
|
+
if (!routeOwner) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const currentName = extractCurrentWorkerName(text);
|
|
324
|
+
if (!currentName || currentName === routeOwner) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const patched = patchWranglerScriptName(configPath, routeOwner);
|
|
329
|
+
if (patched) {
|
|
330
|
+
console.log(`Detected route owner worker "${routeOwner}" for ${host}. Auto-updated wrangler.jsonc name before deploy.`);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
204
334
|
function runDeployWithAutoRouteRecovery(wrangler, configPath, extraArgs = []) {
|
|
205
335
|
const attemptedOwners = new Set();
|
|
206
336
|
|
|
@@ -363,6 +493,7 @@ async function main() {
|
|
|
363
493
|
ensureProjectFiles(PROJECT_ROOT);
|
|
364
494
|
const configPath = requireWranglerConfig();
|
|
365
495
|
autoPatchWranglerConfig(configPath);
|
|
496
|
+
await maybeAlignWorkerNameToRouteOwner(configPath);
|
|
366
497
|
validateWranglerConfigForDeploy(configPath);
|
|
367
498
|
const wrangler = resolveWranglerCommand(PROJECT_ROOT);
|
|
368
499
|
runDeployWithAutoRouteRecovery(wrangler, configPath, rest);
|
package/package.json
CHANGED
|
@@ -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.
|
|
10
|
+
const WORKER_VERSION = '0.1.18';
|
|
11
11
|
const DEFAULT_TTL_MS = 30_000;
|
|
12
12
|
const MAX_TTL_MS = 120_000;
|
|
13
13
|
const MAX_MESSAGE_SIZE = 64 * 1024;
|
|
@@ -1,7 +1,92 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { spawnSync } from 'node:child_process';
|
|
7
|
+
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const PACKAGE_ROOT = path.resolve(path.dirname(__filename), '..');
|
|
10
|
+
const PACKAGE_JSON = JSON.parse(fs.readFileSync(path.join(PACKAGE_ROOT, 'package.json'), 'utf8'));
|
|
11
|
+
const CLI_VERSION = PACKAGE_JSON.version;
|
|
12
|
+
|
|
3
13
|
const isGlobalInstall = process.env.npm_config_global === 'true';
|
|
4
14
|
|
|
15
|
+
function compareVersions(left, right) {
|
|
16
|
+
const leftParts = String(left).split('.').map((value) => Number.parseInt(value, 10) || 0);
|
|
17
|
+
const rightParts = String(right).split('.').map((value) => Number.parseInt(value, 10) || 0);
|
|
18
|
+
const length = Math.max(leftParts.length, rightParts.length);
|
|
19
|
+
|
|
20
|
+
for (let index = 0; index < length; index += 1) {
|
|
21
|
+
const leftValue = leftParts[index] ?? 0;
|
|
22
|
+
const rightValue = rightParts[index] ?? 0;
|
|
23
|
+
if (leftValue > rightValue) return 1;
|
|
24
|
+
if (leftValue < rightValue) return -1;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function fetchLatestCliVersion() {
|
|
31
|
+
const response = await fetch('https://registry.npmjs.org/freertc/latest', {
|
|
32
|
+
headers: { Accept: 'application/json' }
|
|
33
|
+
});
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
throw new Error(`npm registry responded with ${response.status}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const payload = await response.json();
|
|
39
|
+
const version = payload?.version;
|
|
40
|
+
if (typeof version !== 'string' || !version.trim()) {
|
|
41
|
+
throw new Error('npm registry response did not include a version');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return version;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function maybeSelfHealInstall() {
|
|
48
|
+
if (isGlobalInstall) return;
|
|
49
|
+
if (process.env.FREERTC_POSTINSTALL_SELF_HEAL === '0') return;
|
|
50
|
+
if (process.env.FREERTC_POSTINSTALL_RUNNING === '1') return;
|
|
51
|
+
|
|
52
|
+
const projectRoot = process.env.INIT_CWD ? path.resolve(process.env.INIT_CWD) : null;
|
|
53
|
+
if (!projectRoot) return;
|
|
54
|
+
if (projectRoot === PACKAGE_ROOT) return;
|
|
55
|
+
if (!fs.existsSync(path.join(projectRoot, 'package.json'))) return;
|
|
56
|
+
|
|
57
|
+
let latestVersion;
|
|
58
|
+
try {
|
|
59
|
+
latestVersion = await fetchLatestCliVersion();
|
|
60
|
+
} catch {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (compareVersions(CLI_VERSION, latestVersion) >= 0) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
console.log(`freertc postinstall detected stale version ${CLI_VERSION}; upgrading host project to ${latestVersion}...`);
|
|
69
|
+
|
|
70
|
+
const installResult = spawnSync(
|
|
71
|
+
'npm',
|
|
72
|
+
['install', `freertc@${latestVersion}`],
|
|
73
|
+
{
|
|
74
|
+
cwd: projectRoot,
|
|
75
|
+
stdio: 'inherit',
|
|
76
|
+
env: {
|
|
77
|
+
...process.env,
|
|
78
|
+
FREERTC_POSTINSTALL_RUNNING: '1'
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
if (installResult.status !== 0) {
|
|
84
|
+
console.warn('freertc postinstall self-heal failed; continuing with current install.');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await maybeSelfHealInstall();
|
|
89
|
+
|
|
5
90
|
const lines = [
|
|
6
91
|
'',
|
|
7
92
|
'freertc installed.',
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const PSP_VERSION = "1.0";
|
|
2
|
-
const WORKER_VERSION = "0.1.
|
|
2
|
+
const WORKER_VERSION = "0.1.18";
|
|
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"]);
|