freertc 0.1.7 → 0.1.9

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.7","protocol_version":"1.0","peers":0}
219
+ {"ok":true,"version":"0.1.9","protocol_version":"1.0","peers":0}
220
220
  ```
221
221
 
222
222
  ## Auto WebRTC two-tab test
package/bin/freertc.mjs CHANGED
@@ -58,13 +58,122 @@ function runInProject(command, args, { bootstrap = false } = {}) {
58
58
  function requireWranglerConfig() {
59
59
  const configPath = path.join(PROJECT_ROOT, 'wrangler.jsonc');
60
60
  if (fs.existsSync(configPath)) {
61
- return;
61
+ return configPath;
62
62
  }
63
63
 
64
64
  console.error(`Missing ${configPath}. Run "npx freertc" or "npx freertc wizard" from this project directory first.`);
65
65
  process.exit(1);
66
66
  }
67
67
 
68
+ function normalizeHost(value) {
69
+ if (!value || typeof value !== 'string') return null;
70
+ let host = value.trim().toLowerCase();
71
+ if (!host) return null;
72
+ host = host.replace(/^https?:\/\//, '');
73
+ host = host.replace(/^wss?:\/\//, '');
74
+ host = host.replace(/\/.*$/, '');
75
+ host = host.replace(/:\d+$/, '');
76
+ return host || null;
77
+ }
78
+
79
+ function sanitizeDomain(domain) {
80
+ if (!domain || typeof domain !== 'string') return null;
81
+ return domain
82
+ .trim()
83
+ .toLowerCase()
84
+ .replace(/[^a-z0-9-]/g, '-')
85
+ .replace(/-+/g, '-')
86
+ .replace(/^-|-$/g, '') || null;
87
+ }
88
+
89
+ function patchJsoncVar(text, varName, value) {
90
+ const escaped = varName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
91
+ const re = new RegExp(`("${escaped}"\\s*:\\s*)"[^"]*"`, 'g');
92
+ if (re.test(text)) {
93
+ return text.replace(re, `$1"${value}"`);
94
+ }
95
+ return text;
96
+ }
97
+
98
+ function extractRouteHost(configText) {
99
+ const routeMatch = configText.match(/"pattern"\s*:\s*"([^"]+)"/);
100
+ if (!routeMatch) return null;
101
+ const pattern = routeMatch[1];
102
+ return normalizeHost(pattern.split('/')[0]);
103
+ }
104
+
105
+ function extractRelayUrlHost(configText) {
106
+ const relayUrlMatch = configText.match(/"RELAY_URL"\s*:\s*"([^"]*)"/);
107
+ if (!relayUrlMatch) return null;
108
+ const raw = relayUrlMatch[1];
109
+ if (/your-domain\.example/i.test(raw)) return null;
110
+ return normalizeHost(raw);
111
+ }
112
+
113
+ function autoPatchWranglerConfig(configPath) {
114
+ let text = fs.readFileSync(configPath, 'utf8');
115
+
116
+ const routeHost = extractRouteHost(text);
117
+ const envDomainHost = normalizeHost(process.env.CF_DOMAIN || process.env.CLOUD_FLARE_DOMAIN || '');
118
+ const relayHost = routeHost || envDomainHost || extractRelayUrlHost(text);
119
+
120
+ if (!relayHost) {
121
+ return;
122
+ }
123
+
124
+ const relayWsUrl = `wss://${relayHost}/ws`;
125
+ const relayName = relayHost;
126
+ const dbName = `freertc-signal-${sanitizeDomain(relayHost)}`;
127
+
128
+ const before = text;
129
+ text = patchJsoncVar(text, 'RELAY_URL', relayWsUrl);
130
+ text = patchJsoncVar(text, 'RELAY_NAME', relayName);
131
+ text = text.replace(/("database_name"\s*:\s*)"freertc-signal(?:-your-domain)?"/g, `$1"${dbName}"`);
132
+
133
+ if (text !== before) {
134
+ fs.writeFileSync(configPath, text, 'utf8');
135
+ console.log(`Auto-patched wrangler.jsonc for deploy: RELAY_URL=${relayWsUrl}, RELAY_NAME=${relayName}`);
136
+ }
137
+ }
138
+
139
+ function validateWranglerConfigForDeploy(configPath) {
140
+ const configText = fs.readFileSync(configPath, 'utf8');
141
+ const failures = [];
142
+
143
+ const placeholderChecks = [
144
+ { test: /YOUR_D1_DATABASE_ID/, message: 'Replace YOUR_D1_DATABASE_ID in wrangler.jsonc.' },
145
+ { test: /Your App Relay/, message: 'Set vars.RELAY_NAME to your actual relay name.' },
146
+ { test: /wss:\/\/your-domain\.example\/ws/i, message: 'Set vars.RELAY_URL to your real deployed relay WebSocket URL.' },
147
+ { 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.' },
148
+ { test: /"command"\s*:\s*"worker-build --release"/, message: 'Remove the worker-build step unless you intentionally deploy the Rust/WASM worker path.' }
149
+ ];
150
+
151
+ for (const check of placeholderChecks) {
152
+ if (check.test.test(configText)) {
153
+ failures.push(check.message);
154
+ }
155
+ }
156
+
157
+ const relayUrlMatch = configText.match(/"RELAY_URL"\s*:\s*"([^"]*)"/);
158
+ if (!relayUrlMatch || !/^wss:\/\/[^\s"/]+\/ws$/i.test(relayUrlMatch[1])) {
159
+ failures.push('Set vars.RELAY_URL to a valid wss://<host>/ws URL.');
160
+ }
161
+
162
+ const relayNameMatch = configText.match(/"RELAY_NAME"\s*:\s*"([^"]*)"/);
163
+ if (!relayNameMatch || !relayNameMatch[1].trim()) {
164
+ failures.push('Set vars.RELAY_NAME to a non-empty relay name.');
165
+ }
166
+
167
+ if (failures.length > 0) {
168
+ console.error(`Refusing to deploy with placeholder or invalid wrangler config: ${configPath}`);
169
+ for (const failure of failures) {
170
+ console.error(`- ${failure}`);
171
+ }
172
+ console.error('Run "npx freertc wizard" in the domain repo to patch wrangler.jsonc before deploying.');
173
+ process.exit(1);
174
+ }
175
+ }
176
+
68
177
  function compareVersions(left, right) {
69
178
  const leftParts = String(left).split('.').map((value) => Number.parseInt(value, 10) || 0);
70
179
  const rightParts = String(right).split('.').map((value) => Number.parseInt(value, 10) || 0);
@@ -143,7 +252,9 @@ async function main() {
143
252
  if (subcommand === 'deploy') {
144
253
  await assertLatestCliForDeploy();
145
254
  ensureProjectFiles(PROJECT_ROOT);
146
- requireWranglerConfig();
255
+ const configPath = requireWranglerConfig();
256
+ autoPatchWranglerConfig(configPath);
257
+ validateWranglerConfigForDeploy(configPath);
147
258
  const wrangler = resolveWranglerCommand(PROJECT_ROOT);
148
259
  runInProject(wrangler.command, [...wrangler.baseArgs, 'deploy', '--env', 'production', ...rest]);
149
260
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freertc",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
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.7';
10
+ const WORKER_VERSION = '0.1.9';
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.7";
2
+ const WORKER_VERSION = "0.1.9";
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"]);