@jxrstudios/jxr 1.0.3 → 1.0.4
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/bin/jxr.js +86 -0
- package/dist/deployer.js +1 -1
- package/dist/deployer.js.map +1 -1
- package/dist/index.js +1228 -16
- package/package.json +7 -3
package/bin/jxr.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from "child_process";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { readFileSync } from "fs";
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
|
|
10
|
+
const command = process.argv[2];
|
|
11
|
+
const isDev = command === "dev" || process.argv.includes("--dev");
|
|
12
|
+
|
|
13
|
+
// Show help if no command
|
|
14
|
+
if (!command || command === "--help" || command === "-h") {
|
|
15
|
+
console.log(`
|
|
16
|
+
JXR.js CLI
|
|
17
|
+
|
|
18
|
+
Usage: jxr <command> [options]
|
|
19
|
+
|
|
20
|
+
Commands:
|
|
21
|
+
dev Start development server with JXR runtime (zero build)
|
|
22
|
+
start Start production server
|
|
23
|
+
deploy Deploy to JXR Cloudflare infrastructure
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
--port <port> Port to run on (default: 3000)
|
|
27
|
+
--host <host> Host to bind to (default: localhost)
|
|
28
|
+
--help, -h Show this help message
|
|
29
|
+
|
|
30
|
+
Examples:
|
|
31
|
+
jxr dev
|
|
32
|
+
jxr dev --port 8080
|
|
33
|
+
jxr deploy
|
|
34
|
+
`);
|
|
35
|
+
process.exit(0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Development mode - use JXR Runtime dev server (zero build)
|
|
39
|
+
if (isDev) {
|
|
40
|
+
const port = process.argv.find((arg, i) => process.argv[i - 1] === "--port") || "3000";
|
|
41
|
+
const host = process.argv.find((arg, i) => process.argv[i - 1] === "--host") || "localhost";
|
|
42
|
+
|
|
43
|
+
console.log(`🚀 Starting JXR Dev Server on http://${host}:${port}/`);
|
|
44
|
+
|
|
45
|
+
// Run the JXR dev server with experimental TypeScript support (no build needed)
|
|
46
|
+
const serverPath = path.resolve(__dirname, "..", "server", "jxr-serve.mjs");
|
|
47
|
+
const node = spawn("node", ["--experimental-strip-types", serverPath, "--port", port, "--host", host], {
|
|
48
|
+
stdio: "inherit",
|
|
49
|
+
cwd: path.resolve(__dirname, ".."),
|
|
50
|
+
shell: true,
|
|
51
|
+
env: { ...process.env, NODE_ENV: "development", PORT: port },
|
|
52
|
+
});
|
|
53
|
+
node.on("close", (code) => process.exit(code));
|
|
54
|
+
}
|
|
55
|
+
// Production mode
|
|
56
|
+
else if (command === "start") {
|
|
57
|
+
console.log("🚀 Starting JXR Production Server...");
|
|
58
|
+
|
|
59
|
+
const serverPath = path.resolve(__dirname, "..", "server", "jxr-serve.mjs");
|
|
60
|
+
const node = spawn("node", ["--experimental-strip-types", serverPath], {
|
|
61
|
+
stdio: "inherit",
|
|
62
|
+
cwd: path.resolve(__dirname, ".."),
|
|
63
|
+
shell: true,
|
|
64
|
+
env: { ...process.env, NODE_ENV: "production" },
|
|
65
|
+
});
|
|
66
|
+
node.on("close", (code) => process.exit(code));
|
|
67
|
+
}
|
|
68
|
+
// Deploy mode
|
|
69
|
+
else if (command === "deploy") {
|
|
70
|
+
console.log("🚀 Deploying with JXR Deployer...");
|
|
71
|
+
|
|
72
|
+
const deployPath = path.resolve(__dirname, "..", "deploy-landing.mjs");
|
|
73
|
+
const node = spawn("node", ["--experimental-strip-types", deployPath, ...process.argv.slice(3)], {
|
|
74
|
+
stdio: "inherit",
|
|
75
|
+
cwd: path.resolve(__dirname, ".."),
|
|
76
|
+
shell: true,
|
|
77
|
+
env: { ...process.env, NODE_ENV: "production" },
|
|
78
|
+
});
|
|
79
|
+
node.on("close", (code) => process.exit(code));
|
|
80
|
+
}
|
|
81
|
+
// Unknown command
|
|
82
|
+
else {
|
|
83
|
+
console.error(`❌ Unknown command: ${command}`);
|
|
84
|
+
console.log("Run 'jxr --help' for usage information.");
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
package/dist/deployer.js
CHANGED
|
@@ -15,7 +15,7 @@ export class JXRDeployer {
|
|
|
15
15
|
projectId;
|
|
16
16
|
constructor(apiKey, projectId) {
|
|
17
17
|
this.apiKey = apiKey;
|
|
18
|
-
this.apiUrl = 'https://api.
|
|
18
|
+
this.apiUrl = 'https://api.jxrstudios.online/v1';
|
|
19
19
|
this.projectId = projectId || this.generateProjectId();
|
|
20
20
|
}
|
|
21
21
|
/**
|
package/dist/deployer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deployer.js","sourceRoot":"","sources":["../src/deployer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAuBH;;;GAGG;AACH,MAAM,OAAO,WAAW;IACd,MAAM,CAAS;IACf,MAAM,CAAS;IACf,SAAS,CAAS;IAE1B,YAAY,MAAc,EAAE,SAAkB;QAC5C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,
|
|
1
|
+
{"version":3,"file":"deployer.js","sourceRoot":"","sources":["../src/deployer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAuBH;;;GAGG;AACH,MAAM,OAAO,WAAW;IACd,MAAM,CAAS;IACf,MAAM,CAAS;IACf,SAAS,CAAS;IAE1B,YAAY,MAAc,EAAE,SAAkB;QAC5C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,kCAAkC,CAAC;QACjD,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;IACzD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,WAAmB,EAAE,SAAuB,EAAE;QACzD,MA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O,OAAO,SAAS,IAAI,MAAM,EAAE,CAAC;IACtC,CAAC;CACF;AAED,gCAAgC;AAChC,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,CACxC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,EAAE,EAC7B,OAAO,CAAC,GAAG,CAAC,cAAc,CAC3B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,1228 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
// src/worker-pool.ts
|
|
2
|
+
var PRIORITY_WEIGHTS = {
|
|
3
|
+
critical: 4,
|
|
4
|
+
high: 3,
|
|
5
|
+
normal: 2,
|
|
6
|
+
low: 1
|
|
7
|
+
};
|
|
8
|
+
var WorkerPool = class {
|
|
9
|
+
workers = /* @__PURE__ */ new Map();
|
|
10
|
+
taskQueue = [];
|
|
11
|
+
pendingTasks = /* @__PURE__ */ new Map();
|
|
12
|
+
metricsHistory = [];
|
|
13
|
+
throughputWindow = [];
|
|
14
|
+
maxWorkers;
|
|
15
|
+
workerScript;
|
|
16
|
+
taskCounter = 0;
|
|
17
|
+
listeners = /* @__PURE__ */ new Map();
|
|
18
|
+
constructor(workerScript, maxWorkers) {
|
|
19
|
+
this.workerScript = workerScript;
|
|
20
|
+
this.maxWorkers = maxWorkers ?? Math.max(2, (navigator.hardwareConcurrency ?? 4) - 1);
|
|
21
|
+
this.prewarm();
|
|
22
|
+
}
|
|
23
|
+
/** Pre-warm the pool to eliminate cold-start latency */
|
|
24
|
+
prewarm() {
|
|
25
|
+
const initialCount = Math.min(2, this.maxWorkers);
|
|
26
|
+
for (let i = 0; i < initialCount; i++) {
|
|
27
|
+
this.spawnWorker();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
spawnWorker() {
|
|
31
|
+
const workerId = `worker-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
32
|
+
const worker = new Worker(this.workerScript, { type: "module" });
|
|
33
|
+
const entry = {
|
|
34
|
+
worker,
|
|
35
|
+
status: "idle",
|
|
36
|
+
currentTaskId: null,
|
|
37
|
+
latencyHistory: [],
|
|
38
|
+
metrics: {
|
|
39
|
+
workerId,
|
|
40
|
+
status: "idle",
|
|
41
|
+
tasksCompleted: 0,
|
|
42
|
+
tasksFailed: 0,
|
|
43
|
+
avgLatencyMs: 0,
|
|
44
|
+
lastActiveAt: Date.now(),
|
|
45
|
+
cpuLoad: 0
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
worker.onmessage = (event) => this.handleWorkerMessage(workerId, event);
|
|
49
|
+
worker.onerror = (error) => this.handleWorkerError(workerId, error);
|
|
50
|
+
this.workers.set(workerId, entry);
|
|
51
|
+
return workerId;
|
|
52
|
+
}
|
|
53
|
+
handleWorkerMessage(workerId, event) {
|
|
54
|
+
const { taskId, result, error, type } = event.data;
|
|
55
|
+
const entry = this.workers.get(workerId);
|
|
56
|
+
if (!entry) return;
|
|
57
|
+
if (type === "metrics") {
|
|
58
|
+
entry.metrics.cpuLoad = event.data.cpuLoad ?? 0;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const task = this.pendingTasks.get(taskId);
|
|
62
|
+
if (!task) return;
|
|
63
|
+
const latency = Date.now() - task.timestamp;
|
|
64
|
+
entry.latencyHistory.push(latency);
|
|
65
|
+
if (entry.latencyHistory.length > 50) entry.latencyHistory.shift();
|
|
66
|
+
entry.metrics.avgLatencyMs = entry.latencyHistory.reduce((a, b) => a + b, 0) / entry.latencyHistory.length;
|
|
67
|
+
entry.metrics.lastActiveAt = Date.now();
|
|
68
|
+
if (error) {
|
|
69
|
+
entry.metrics.tasksFailed++;
|
|
70
|
+
task.reject(new Error(error));
|
|
71
|
+
} else {
|
|
72
|
+
entry.metrics.tasksCompleted++;
|
|
73
|
+
this.throughputWindow.push(Date.now());
|
|
74
|
+
task.resolve(result);
|
|
75
|
+
}
|
|
76
|
+
this.pendingTasks.delete(taskId);
|
|
77
|
+
entry.status = "idle";
|
|
78
|
+
entry.metrics.status = "idle";
|
|
79
|
+
entry.currentTaskId = null;
|
|
80
|
+
this.emitMetrics();
|
|
81
|
+
this.drainQueue();
|
|
82
|
+
}
|
|
83
|
+
handleWorkerError(workerId, error) {
|
|
84
|
+
const entry = this.workers.get(workerId);
|
|
85
|
+
if (!entry) return;
|
|
86
|
+
entry.status = "error";
|
|
87
|
+
entry.metrics.status = "error";
|
|
88
|
+
if (entry.currentTaskId) {
|
|
89
|
+
const task = this.pendingTasks.get(entry.currentTaskId);
|
|
90
|
+
if (task) {
|
|
91
|
+
task.reject(new Error(error.message));
|
|
92
|
+
this.pendingTasks.delete(entry.currentTaskId);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
entry.worker.terminate();
|
|
96
|
+
this.workers.delete(workerId);
|
|
97
|
+
this.spawnWorker();
|
|
98
|
+
this.drainQueue();
|
|
99
|
+
}
|
|
100
|
+
getIdleWorker() {
|
|
101
|
+
for (const [, entry] of Array.from(this.workers.entries())) {
|
|
102
|
+
if (entry.status === "idle") return entry;
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
drainQueue() {
|
|
107
|
+
if (this.taskQueue.length === 0) return;
|
|
108
|
+
this.taskQueue.sort(
|
|
109
|
+
(a, b) => PRIORITY_WEIGHTS[b.priority] - PRIORITY_WEIGHTS[a.priority]
|
|
110
|
+
);
|
|
111
|
+
let idleWorker = this.getIdleWorker();
|
|
112
|
+
if (!idleWorker && this.workers.size < this.maxWorkers) {
|
|
113
|
+
const newId = this.spawnWorker();
|
|
114
|
+
idleWorker = this.workers.get(newId) ?? null;
|
|
115
|
+
}
|
|
116
|
+
if (!idleWorker) return;
|
|
117
|
+
const task = this.taskQueue.shift();
|
|
118
|
+
this.dispatchToWorker(idleWorker, task);
|
|
119
|
+
}
|
|
120
|
+
dispatchToWorker(entry, task) {
|
|
121
|
+
entry.status = "busy";
|
|
122
|
+
entry.metrics.status = "busy";
|
|
123
|
+
entry.currentTaskId = task.id;
|
|
124
|
+
this.pendingTasks.set(task.id, task);
|
|
125
|
+
entry.worker.postMessage({
|
|
126
|
+
taskId: task.id,
|
|
127
|
+
type: task.type,
|
|
128
|
+
payload: task.payload
|
|
129
|
+
});
|
|
130
|
+
if (task.timeoutMs) {
|
|
131
|
+
setTimeout(() => {
|
|
132
|
+
if (this.pendingTasks.has(task.id)) {
|
|
133
|
+
task.reject(new Error(`Task ${task.id} timed out after ${task.timeoutMs}ms`));
|
|
134
|
+
this.pendingTasks.delete(task.id);
|
|
135
|
+
}
|
|
136
|
+
}, task.timeoutMs);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** Submit a task to the pool, returns a Promise */
|
|
140
|
+
submit(type, payload, options = {}) {
|
|
141
|
+
return new Promise((resolve, reject) => {
|
|
142
|
+
const task = {
|
|
143
|
+
id: `task-${++this.taskCounter}-${Date.now()}`,
|
|
144
|
+
type,
|
|
145
|
+
payload,
|
|
146
|
+
priority: options.priority ?? "normal",
|
|
147
|
+
timestamp: Date.now(),
|
|
148
|
+
timeoutMs: options.timeoutMs,
|
|
149
|
+
resolve,
|
|
150
|
+
reject
|
|
151
|
+
};
|
|
152
|
+
const idleWorker = this.getIdleWorker();
|
|
153
|
+
if (idleWorker) {
|
|
154
|
+
this.dispatchToWorker(idleWorker, task);
|
|
155
|
+
} else {
|
|
156
|
+
this.taskQueue.push(task);
|
|
157
|
+
if (this.workers.size < this.maxWorkers) {
|
|
158
|
+
this.spawnWorker();
|
|
159
|
+
this.drainQueue();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/** Get current pool metrics */
|
|
165
|
+
getMetrics() {
|
|
166
|
+
const now = Date.now();
|
|
167
|
+
this.throughputWindow = this.throughputWindow.filter((t) => now - t < 1e3);
|
|
168
|
+
const allLatencies = Array.from(this.workers.values()).flatMap(
|
|
169
|
+
(e) => e.latencyHistory
|
|
170
|
+
);
|
|
171
|
+
const avgLatency = allLatencies.length > 0 ? allLatencies.reduce((a, b) => a + b, 0) / allLatencies.length : 0;
|
|
172
|
+
const totalCompleted = Array.from(this.workers.values()).reduce(
|
|
173
|
+
(sum, e) => sum + e.metrics.tasksCompleted,
|
|
174
|
+
0
|
|
175
|
+
);
|
|
176
|
+
return {
|
|
177
|
+
totalWorkers: this.workers.size,
|
|
178
|
+
idleWorkers: Array.from(this.workers.values()).filter((e) => e.status === "idle").length,
|
|
179
|
+
busyWorkers: Array.from(this.workers.values()).filter((e) => e.status === "busy").length,
|
|
180
|
+
queueDepth: this.taskQueue.length,
|
|
181
|
+
throughputPerSec: this.throughputWindow.length,
|
|
182
|
+
avgLatencyMs: Math.round(avgLatency * 10) / 10,
|
|
183
|
+
totalTasksCompleted: totalCompleted
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
getWorkerMetrics() {
|
|
187
|
+
return Array.from(this.workers.values()).map((e) => ({ ...e.metrics }));
|
|
188
|
+
}
|
|
189
|
+
onMetrics(event, cb) {
|
|
190
|
+
if (!this.listeners.has(event)) this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
191
|
+
this.listeners.get(event).add(cb);
|
|
192
|
+
return () => this.listeners.get(event)?.delete(cb);
|
|
193
|
+
}
|
|
194
|
+
emitMetrics() {
|
|
195
|
+
const metrics = this.getMetrics();
|
|
196
|
+
this.listeners.get("metrics")?.forEach((cb) => cb(metrics));
|
|
197
|
+
}
|
|
198
|
+
terminate() {
|
|
199
|
+
for (const [, entry] of Array.from(this.workers.entries())) {
|
|
200
|
+
entry.worker.terminate();
|
|
201
|
+
}
|
|
202
|
+
this.workers.clear();
|
|
203
|
+
this.taskQueue.length = 0;
|
|
204
|
+
this.pendingTasks.clear();
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// src/moq-transport.ts
|
|
209
|
+
var MoQTransport = class {
|
|
210
|
+
state = "disconnected";
|
|
211
|
+
subscriptions = /* @__PURE__ */ new Map();
|
|
212
|
+
trackBuffers = /* @__PURE__ */ new Map();
|
|
213
|
+
metrics;
|
|
214
|
+
rttHistory = [];
|
|
215
|
+
bandwidthSamples = [];
|
|
216
|
+
metricsListeners = /* @__PURE__ */ new Set();
|
|
217
|
+
objectListeners = /* @__PURE__ */ new Map();
|
|
218
|
+
groupSequence = 0;
|
|
219
|
+
objectSequence = 0;
|
|
220
|
+
simulationInterval = null;
|
|
221
|
+
constructor() {
|
|
222
|
+
this.metrics = {
|
|
223
|
+
connectionState: "disconnected",
|
|
224
|
+
rttMs: 0,
|
|
225
|
+
bandwidthBps: 0,
|
|
226
|
+
packetsReceived: 0,
|
|
227
|
+
packetsSent: 0,
|
|
228
|
+
bytesReceived: 0,
|
|
229
|
+
bytesSent: 0,
|
|
230
|
+
activeSubscriptions: 0,
|
|
231
|
+
activePublications: 0,
|
|
232
|
+
lossRate: 0
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/** Connect to a MoQ relay endpoint */
|
|
236
|
+
async connect(endpoint) {
|
|
237
|
+
this.state = "connecting";
|
|
238
|
+
this.updateMetrics({ connectionState: "connecting" });
|
|
239
|
+
await this.simulateHandshake(endpoint);
|
|
240
|
+
this.state = "connected";
|
|
241
|
+
this.updateMetrics({ connectionState: "connected" });
|
|
242
|
+
this.startMetricsSimulation();
|
|
243
|
+
}
|
|
244
|
+
async simulateHandshake(endpoint) {
|
|
245
|
+
const startTime = performance.now();
|
|
246
|
+
const handshakeMs = endpoint.includes("local") ? 1 : Math.random() * 15 + 5;
|
|
247
|
+
await new Promise((r) => setTimeout(r, handshakeMs));
|
|
248
|
+
const rtt = performance.now() - startTime;
|
|
249
|
+
this.rttHistory.push(rtt);
|
|
250
|
+
}
|
|
251
|
+
/** Publish an object to a track */
|
|
252
|
+
async publish(track, payload, options = {}) {
|
|
253
|
+
if (this.state !== "connected") throw new Error("MoQ transport not connected");
|
|
254
|
+
if (options.newGroup) {
|
|
255
|
+
this.groupSequence++;
|
|
256
|
+
this.objectSequence = 0;
|
|
257
|
+
}
|
|
258
|
+
const obj = {
|
|
259
|
+
trackNamespace: track,
|
|
260
|
+
groupSequence: this.groupSequence,
|
|
261
|
+
objectSequence: this.objectSequence++,
|
|
262
|
+
sendOrder: options.sendOrder ?? this.objectSequence,
|
|
263
|
+
payload,
|
|
264
|
+
timestamp: performance.now(),
|
|
265
|
+
size: typeof payload === "string" ? payload.length * 2 : payload.byteLength
|
|
266
|
+
};
|
|
267
|
+
const trackKey = this.trackKey(track);
|
|
268
|
+
let buffer = this.trackBuffers.get(trackKey);
|
|
269
|
+
if (!buffer) {
|
|
270
|
+
buffer = { objects: [], maxBufferSize: 1e3, subscribers: /* @__PURE__ */ new Set() };
|
|
271
|
+
this.trackBuffers.set(trackKey, buffer);
|
|
272
|
+
}
|
|
273
|
+
buffer.objects.push(obj);
|
|
274
|
+
if (buffer.objects.length > buffer.maxBufferSize) {
|
|
275
|
+
buffer.objects.shift();
|
|
276
|
+
}
|
|
277
|
+
this.metrics.packetsSent++;
|
|
278
|
+
this.metrics.bytesSent += obj.size;
|
|
279
|
+
this.deliverToSubscribers(trackKey, obj);
|
|
280
|
+
this.updateMetrics({});
|
|
281
|
+
}
|
|
282
|
+
/** Subscribe to a track */
|
|
283
|
+
subscribe(subscription) {
|
|
284
|
+
this.subscriptions.set(subscription.id, subscription);
|
|
285
|
+
const trackKey = this.trackKey(subscription.track);
|
|
286
|
+
const buffer = this.trackBuffers.get(trackKey);
|
|
287
|
+
if (buffer) {
|
|
288
|
+
const objects = [...buffer.objects];
|
|
289
|
+
if (subscription.deliveryOrder === "descending") objects.reverse();
|
|
290
|
+
for (const obj of objects) {
|
|
291
|
+
if (subscription.startGroup === void 0 || obj.groupSequence >= subscription.startGroup) {
|
|
292
|
+
subscription.handler(obj);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
this.updateMetrics({ activeSubscriptions: this.subscriptions.size });
|
|
297
|
+
return () => {
|
|
298
|
+
this.subscriptions.delete(subscription.id);
|
|
299
|
+
this.updateMetrics({ activeSubscriptions: this.subscriptions.size });
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
deliverToSubscribers(trackKey, obj) {
|
|
303
|
+
for (const sub of Array.from(this.subscriptions.values())) {
|
|
304
|
+
if (this.trackKey(sub.track) === trackKey) {
|
|
305
|
+
const deliveryDelay = Math.random() * 2;
|
|
306
|
+
setTimeout(() => {
|
|
307
|
+
this.metrics.packetsReceived++;
|
|
308
|
+
this.metrics.bytesReceived += obj.size;
|
|
309
|
+
sub.handler(obj);
|
|
310
|
+
}, deliveryDelay);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
trackKey(track) {
|
|
315
|
+
return `${track.namespace}/${track.trackName}`;
|
|
316
|
+
}
|
|
317
|
+
startMetricsSimulation() {
|
|
318
|
+
this.simulationInterval = setInterval(() => {
|
|
319
|
+
const baseRtt = 8;
|
|
320
|
+
const rttJitter = (Math.random() - 0.5) * 4;
|
|
321
|
+
const rtt = Math.max(1, baseRtt + rttJitter);
|
|
322
|
+
this.rttHistory.push(rtt);
|
|
323
|
+
if (this.rttHistory.length > 100) this.rttHistory.shift();
|
|
324
|
+
const avgRtt = this.rttHistory.reduce((a, b) => a + b, 0) / this.rttHistory.length;
|
|
325
|
+
const bandwidth = (500 + Math.random() * 500) * 1e6;
|
|
326
|
+
this.bandwidthSamples.push(bandwidth);
|
|
327
|
+
if (this.bandwidthSamples.length > 20) this.bandwidthSamples.shift();
|
|
328
|
+
const avgBandwidth = this.bandwidthSamples.reduce((a, b) => a + b, 0) / this.bandwidthSamples.length;
|
|
329
|
+
this.updateMetrics({
|
|
330
|
+
rttMs: Math.round(avgRtt * 10) / 10,
|
|
331
|
+
bandwidthBps: Math.round(avgBandwidth),
|
|
332
|
+
lossRate: Math.random() * 1e-3
|
|
333
|
+
// <0.1% loss on edge
|
|
334
|
+
});
|
|
335
|
+
}, 500);
|
|
336
|
+
}
|
|
337
|
+
updateMetrics(partial) {
|
|
338
|
+
this.metrics = { ...this.metrics, ...partial };
|
|
339
|
+
this.metricsListeners.forEach((cb) => cb({ ...this.metrics }));
|
|
340
|
+
}
|
|
341
|
+
onMetrics(cb) {
|
|
342
|
+
this.metricsListeners.add(cb);
|
|
343
|
+
return () => this.metricsListeners.delete(cb);
|
|
344
|
+
}
|
|
345
|
+
getMetrics() {
|
|
346
|
+
return { ...this.metrics };
|
|
347
|
+
}
|
|
348
|
+
getState() {
|
|
349
|
+
return this.state;
|
|
350
|
+
}
|
|
351
|
+
disconnect() {
|
|
352
|
+
if (this.simulationInterval) clearInterval(this.simulationInterval);
|
|
353
|
+
this.state = "disconnected";
|
|
354
|
+
this.subscriptions.clear();
|
|
355
|
+
this.updateMetrics({ connectionState: "disconnected" });
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// src/web-crypto.ts
|
|
360
|
+
var subtle = globalThis.crypto.subtle;
|
|
361
|
+
var JXRCrypto = class {
|
|
362
|
+
signingKeyPair = null;
|
|
363
|
+
encryptionKeys = /* @__PURE__ */ new Map();
|
|
364
|
+
hashCache = /* @__PURE__ */ new Map();
|
|
365
|
+
/** Generate an ECDSA P-256 signing key pair for module manifests */
|
|
366
|
+
async generateSigningKeyPair() {
|
|
367
|
+
const keyPair = await subtle.generateKey(
|
|
368
|
+
{ name: "ECDSA", namedCurve: "P-256" },
|
|
369
|
+
true,
|
|
370
|
+
["sign", "verify"]
|
|
371
|
+
);
|
|
372
|
+
const spki = await subtle.exportKey("spki", keyPair.publicKey);
|
|
373
|
+
const publicKeyExported = this.toBase64Url(spki);
|
|
374
|
+
this.signingKeyPair = {
|
|
375
|
+
publicKey: keyPair.publicKey,
|
|
376
|
+
privateKey: keyPair.privateKey,
|
|
377
|
+
publicKeyExported
|
|
378
|
+
};
|
|
379
|
+
return this.signingKeyPair;
|
|
380
|
+
}
|
|
381
|
+
/** Hash a module's source code for integrity verification */
|
|
382
|
+
async hashModule(source, algorithm = "SHA-256") {
|
|
383
|
+
const cacheKey = `${algorithm}:${source.length}:${source.slice(0, 64)}`;
|
|
384
|
+
const cached = this.hashCache.get(cacheKey);
|
|
385
|
+
if (cached) return cached;
|
|
386
|
+
const encoded = new TextEncoder().encode(source);
|
|
387
|
+
const hashBuffer = await subtle.digest(algorithm, encoded);
|
|
388
|
+
const digest = this.toHex(hashBuffer);
|
|
389
|
+
const result = {
|
|
390
|
+
algorithm,
|
|
391
|
+
digest,
|
|
392
|
+
size: encoded.byteLength,
|
|
393
|
+
timestamp: Date.now()
|
|
394
|
+
};
|
|
395
|
+
this.hashCache.set(cacheKey, result);
|
|
396
|
+
return result;
|
|
397
|
+
}
|
|
398
|
+
/** Verify a module's integrity against a known hash */
|
|
399
|
+
async verifyModule(source, expected) {
|
|
400
|
+
const actual = await this.hashModule(source, expected.algorithm);
|
|
401
|
+
return actual.digest === expected.digest;
|
|
402
|
+
}
|
|
403
|
+
/** Sign a module manifest with ECDSA P-256 */
|
|
404
|
+
async signManifest(modules, projectId, version) {
|
|
405
|
+
if (!this.signingKeyPair) {
|
|
406
|
+
await this.generateSigningKeyPair();
|
|
407
|
+
}
|
|
408
|
+
const manifest = {
|
|
409
|
+
modules,
|
|
410
|
+
projectId,
|
|
411
|
+
version,
|
|
412
|
+
signedAt: Date.now()
|
|
413
|
+
};
|
|
414
|
+
const data = new TextEncoder().encode(JSON.stringify(manifest));
|
|
415
|
+
const signatureBuffer = await subtle.sign(
|
|
416
|
+
{ name: "ECDSA", hash: "SHA-256" },
|
|
417
|
+
this.signingKeyPair.privateKey,
|
|
418
|
+
data
|
|
419
|
+
);
|
|
420
|
+
return {
|
|
421
|
+
...manifest,
|
|
422
|
+
signature: this.toBase64Url(signatureBuffer),
|
|
423
|
+
publicKey: this.signingKeyPair.publicKeyExported
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
/** Verify a signed manifest */
|
|
427
|
+
async verifyManifest(manifest) {
|
|
428
|
+
try {
|
|
429
|
+
const spki = this.fromBase64Url(manifest.publicKey);
|
|
430
|
+
const publicKey = await subtle.importKey(
|
|
431
|
+
"spki",
|
|
432
|
+
spki,
|
|
433
|
+
{ name: "ECDSA", namedCurve: "P-256" },
|
|
434
|
+
false,
|
|
435
|
+
["verify"]
|
|
436
|
+
);
|
|
437
|
+
const { signature, publicKey: _pk, ...rest } = manifest;
|
|
438
|
+
const data = new TextEncoder().encode(JSON.stringify(rest));
|
|
439
|
+
const sigBuffer = this.fromBase64Url(signature);
|
|
440
|
+
return await subtle.verify(
|
|
441
|
+
{ name: "ECDSA", hash: "SHA-256" },
|
|
442
|
+
publicKey,
|
|
443
|
+
sigBuffer,
|
|
444
|
+
data
|
|
445
|
+
);
|
|
446
|
+
} catch {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
/** Generate or retrieve an AES-GCM-256 encryption key for a project */
|
|
451
|
+
async getEncryptionKey(keyId, projectSeed) {
|
|
452
|
+
const existing = this.encryptionKeys.get(keyId);
|
|
453
|
+
if (existing) return existing;
|
|
454
|
+
let key;
|
|
455
|
+
if (projectSeed) {
|
|
456
|
+
const seedBytes = new TextEncoder().encode(projectSeed);
|
|
457
|
+
const baseKey = await subtle.importKey("raw", seedBytes, "HKDF", false, ["deriveKey"]);
|
|
458
|
+
const salt = new TextEncoder().encode(`jxr-project-${keyId}`);
|
|
459
|
+
const info = new TextEncoder().encode("JXR.js Module Cache v1");
|
|
460
|
+
key = await subtle.deriveKey(
|
|
461
|
+
{ name: "HKDF", hash: "SHA-256", salt, info },
|
|
462
|
+
baseKey,
|
|
463
|
+
{ name: "AES-GCM", length: 256 },
|
|
464
|
+
false,
|
|
465
|
+
["encrypt", "decrypt"]
|
|
466
|
+
);
|
|
467
|
+
} else {
|
|
468
|
+
key = await subtle.generateKey({ name: "AES-GCM", length: 256 }, false, [
|
|
469
|
+
"encrypt",
|
|
470
|
+
"decrypt"
|
|
471
|
+
]);
|
|
472
|
+
}
|
|
473
|
+
this.encryptionKeys.set(keyId, key);
|
|
474
|
+
return key;
|
|
475
|
+
}
|
|
476
|
+
/** Encrypt a module for secure caching */
|
|
477
|
+
async encryptModule(source, keyId) {
|
|
478
|
+
const key = await this.getEncryptionKey(keyId);
|
|
479
|
+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
|
|
480
|
+
const encoded = new TextEncoder().encode(source);
|
|
481
|
+
const cipherBuffer = await subtle.encrypt({ name: "AES-GCM", iv }, key, encoded);
|
|
482
|
+
const ciphertext = cipherBuffer.slice(0, cipherBuffer.byteLength - 16);
|
|
483
|
+
const tag = cipherBuffer.slice(cipherBuffer.byteLength - 16);
|
|
484
|
+
return {
|
|
485
|
+
ciphertext: this.toBase64Url(ciphertext),
|
|
486
|
+
iv: this.toBase64Url(iv.buffer),
|
|
487
|
+
tag: this.toBase64Url(tag),
|
|
488
|
+
keyId
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
/** Decrypt a cached module */
|
|
492
|
+
async decryptModule(encrypted) {
|
|
493
|
+
const key = await this.getEncryptionKey(encrypted.keyId);
|
|
494
|
+
const iv = this.fromBase64Url(encrypted.iv);
|
|
495
|
+
const ciphertext = this.fromBase64Url(encrypted.ciphertext);
|
|
496
|
+
const tag = this.fromBase64Url(encrypted.tag);
|
|
497
|
+
const combined = new Uint8Array(ciphertext.byteLength + tag.byteLength);
|
|
498
|
+
combined.set(new Uint8Array(ciphertext));
|
|
499
|
+
combined.set(new Uint8Array(tag), ciphertext.byteLength);
|
|
500
|
+
const plainBuffer = await subtle.decrypt({ name: "AES-GCM", iv }, key, combined);
|
|
501
|
+
return new TextDecoder().decode(plainBuffer);
|
|
502
|
+
}
|
|
503
|
+
/** Generate a cryptographically secure random nonce */
|
|
504
|
+
generateNonce(bytes = 16) {
|
|
505
|
+
const nonce = globalThis.crypto.getRandomValues(new Uint8Array(bytes));
|
|
506
|
+
return this.toBase64Url(nonce.buffer);
|
|
507
|
+
}
|
|
508
|
+
/** Generate a project-unique ID using Web Crypto */
|
|
509
|
+
async generateProjectId(name, timestamp) {
|
|
510
|
+
const data = new TextEncoder().encode(`${name}:${timestamp}`);
|
|
511
|
+
const hash = await subtle.digest("SHA-256", data);
|
|
512
|
+
return this.toHex(hash).slice(0, 16);
|
|
513
|
+
}
|
|
514
|
+
// ─── Encoding utilities ───────────────────────────────────────────────────
|
|
515
|
+
toHex(buffer) {
|
|
516
|
+
return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
517
|
+
}
|
|
518
|
+
toBase64Url(buffer) {
|
|
519
|
+
const bytes = new Uint8Array(buffer);
|
|
520
|
+
let binary = "";
|
|
521
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
522
|
+
binary += String.fromCharCode(bytes[i]);
|
|
523
|
+
}
|
|
524
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
525
|
+
}
|
|
526
|
+
fromBase64Url(str) {
|
|
527
|
+
const padded = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
528
|
+
const padLength = (4 - padded.length % 4) % 4;
|
|
529
|
+
const base64 = padded + "=".repeat(padLength);
|
|
530
|
+
const binary = atob(base64);
|
|
531
|
+
const bytes = new Uint8Array(binary.length);
|
|
532
|
+
for (let i = 0; i < binary.length; i++) {
|
|
533
|
+
bytes[i] = binary.charCodeAt(i);
|
|
534
|
+
}
|
|
535
|
+
return bytes.buffer;
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
var jxrCrypto = new JXRCrypto();
|
|
539
|
+
|
|
540
|
+
// src/module-resolver.ts
|
|
541
|
+
var VirtualFS = class {
|
|
542
|
+
files = /* @__PURE__ */ new Map();
|
|
543
|
+
changeHandlers = /* @__PURE__ */ new Set();
|
|
544
|
+
constructor(initialFiles) {
|
|
545
|
+
if (initialFiles) {
|
|
546
|
+
for (const file of initialFiles) {
|
|
547
|
+
this.files.set(file.path, file);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
write(path, content) {
|
|
552
|
+
const existing = this.files.get(path);
|
|
553
|
+
const language = this.detectLanguage(path);
|
|
554
|
+
const file = {
|
|
555
|
+
path,
|
|
556
|
+
content,
|
|
557
|
+
language,
|
|
558
|
+
lastModified: Date.now(),
|
|
559
|
+
size: new TextEncoder().encode(content).byteLength,
|
|
560
|
+
dirty: true
|
|
561
|
+
};
|
|
562
|
+
this.files.set(path, file);
|
|
563
|
+
this.emit(file, existing ? "update" : "create");
|
|
564
|
+
return file;
|
|
565
|
+
}
|
|
566
|
+
read(path) {
|
|
567
|
+
return this.files.get(path) ?? null;
|
|
568
|
+
}
|
|
569
|
+
delete(path) {
|
|
570
|
+
const file = this.files.get(path);
|
|
571
|
+
if (!file) return false;
|
|
572
|
+
this.files.delete(path);
|
|
573
|
+
this.emit(file, "delete");
|
|
574
|
+
return true;
|
|
575
|
+
}
|
|
576
|
+
list(prefix) {
|
|
577
|
+
const all = Array.from(this.files.values());
|
|
578
|
+
return prefix ? all.filter((f) => f.path.startsWith(prefix)) : all;
|
|
579
|
+
}
|
|
580
|
+
exists(path) {
|
|
581
|
+
return this.files.has(path);
|
|
582
|
+
}
|
|
583
|
+
onChange(handler) {
|
|
584
|
+
this.changeHandlers.add(handler);
|
|
585
|
+
return () => this.changeHandlers.delete(handler);
|
|
586
|
+
}
|
|
587
|
+
emit(file, event) {
|
|
588
|
+
this.changeHandlers.forEach((h) => h(file, event));
|
|
589
|
+
}
|
|
590
|
+
buildTree(rootPath = "/") {
|
|
591
|
+
const files = this.list();
|
|
592
|
+
const root = {
|
|
593
|
+
path: rootPath,
|
|
594
|
+
name: rootPath === "/" ? "project" : rootPath.split("/").pop(),
|
|
595
|
+
children: [],
|
|
596
|
+
expanded: true
|
|
597
|
+
};
|
|
598
|
+
const dirs = /* @__PURE__ */ new Map();
|
|
599
|
+
dirs.set(rootPath, root);
|
|
600
|
+
const sorted = [...files].sort((a, b) => a.path.localeCompare(b.path));
|
|
601
|
+
for (const file of sorted) {
|
|
602
|
+
const parts = file.path.replace(rootPath, "").split("/").filter(Boolean);
|
|
603
|
+
let current = root;
|
|
604
|
+
let currentPath = rootPath;
|
|
605
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
606
|
+
currentPath = `${currentPath}${parts[i]}/`;
|
|
607
|
+
if (!dirs.has(currentPath)) {
|
|
608
|
+
const dir = {
|
|
609
|
+
path: currentPath,
|
|
610
|
+
name: parts[i],
|
|
611
|
+
children: [],
|
|
612
|
+
expanded: true
|
|
613
|
+
};
|
|
614
|
+
dirs.set(currentPath, dir);
|
|
615
|
+
current.children.push(dir);
|
|
616
|
+
}
|
|
617
|
+
current = dirs.get(currentPath);
|
|
618
|
+
}
|
|
619
|
+
current.children.push(file);
|
|
620
|
+
}
|
|
621
|
+
return root;
|
|
622
|
+
}
|
|
623
|
+
toJSON() {
|
|
624
|
+
const result = {};
|
|
625
|
+
for (const [path, file] of Array.from(this.files.entries())) {
|
|
626
|
+
result[path] = file.content;
|
|
627
|
+
}
|
|
628
|
+
return result;
|
|
629
|
+
}
|
|
630
|
+
detectLanguage(path) {
|
|
631
|
+
const ext = path.split(".").pop()?.toLowerCase();
|
|
632
|
+
const map = {
|
|
633
|
+
tsx: "tsx",
|
|
634
|
+
ts: "ts",
|
|
635
|
+
jsx: "jsx",
|
|
636
|
+
js: "js",
|
|
637
|
+
css: "css",
|
|
638
|
+
json: "json",
|
|
639
|
+
md: "md",
|
|
640
|
+
html: "html"
|
|
641
|
+
};
|
|
642
|
+
return map[ext ?? ""] ?? "js";
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
var JSXTransformer = class {
|
|
646
|
+
objectUrls = /* @__PURE__ */ new Map();
|
|
647
|
+
/**
|
|
648
|
+
* Transform JSX/TSX source to browser-executable ES module
|
|
649
|
+
* Uses the automatic JSX runtime (React 17+)
|
|
650
|
+
*/
|
|
651
|
+
transform(source, filePath) {
|
|
652
|
+
const isTS = filePath.endsWith(".ts") || filePath.endsWith(".tsx");
|
|
653
|
+
let result = source;
|
|
654
|
+
if (isTS) {
|
|
655
|
+
result = this.stripTypeScript(result);
|
|
656
|
+
}
|
|
657
|
+
if (filePath.endsWith(".jsx") || filePath.endsWith(".tsx")) {
|
|
658
|
+
result = this.transformJSX(result);
|
|
659
|
+
}
|
|
660
|
+
result = this.rewriteImports(result);
|
|
661
|
+
return result;
|
|
662
|
+
}
|
|
663
|
+
stripTypeScript(source) {
|
|
664
|
+
let result = source;
|
|
665
|
+
result = result.replace(/^import\s+type\s+.*?from\s+['"][^'"]+['"]\s*;?\s*$/gm, "");
|
|
666
|
+
result = result.replace(/\{\s*type\s+\w+\s*,?\s*/g, "{ ");
|
|
667
|
+
result = result.replace(/,\s*type\s+\w+\s*/g, ", ");
|
|
668
|
+
result = result.replace(/\s+as\s+[A-Z][A-Za-z<>\[\],\s|&]+(?=[,)\s;])/g, "");
|
|
669
|
+
result = result.replace(/<[A-Z][A-Za-z<>\[\],\s|&]*>\s*\(/g, "(");
|
|
670
|
+
result = result.replace(/^(export\s+)?interface\s+\w+[^{]*\{[^}]*\}/gm, "");
|
|
671
|
+
result = result.replace(/^(export\s+)?type\s+\w+\s*=\s*[^;]+;/gm, "");
|
|
672
|
+
result = result.replace(/:\s*[A-Z][A-Za-z<>\[\],\s|&]*(?=[,)=])/g, "");
|
|
673
|
+
result = result.replace(/\)\s*:\s*[A-Za-z<>\[\],\s|&]+\s*\{/g, ") {");
|
|
674
|
+
result = result.replace(/:\s*[A-Z][A-Za-z<>\[\],\s|&]*\s*=/g, " =");
|
|
675
|
+
return result;
|
|
676
|
+
}
|
|
677
|
+
transformJSX(source) {
|
|
678
|
+
const hasReactImport = /import\s+React/.test(source) || /import\s+\*\s+as\s+React/.test(source);
|
|
679
|
+
let result = source;
|
|
680
|
+
if (!hasReactImport) {
|
|
681
|
+
result = `import React from 'react';
|
|
682
|
+
` + result;
|
|
683
|
+
}
|
|
684
|
+
result = result.replace(/<([A-Z][A-Za-z.]*)\s*\/>/g, "React.createElement($1, null)");
|
|
685
|
+
result = result.replace(
|
|
686
|
+
/<([A-Z][A-Za-z.]*)\s+([^>]+?)\s*\/>/g,
|
|
687
|
+
(_, tag, props) => `React.createElement(${tag}, {${this.parseProps(props)}})`
|
|
688
|
+
);
|
|
689
|
+
result = result.replace(/<([a-z][a-z-]*)\s*\/>/g, `React.createElement('$1', null)`);
|
|
690
|
+
result = result.replace(/<>/g, "React.createElement(React.Fragment, null,");
|
|
691
|
+
result = result.replace(/<\/>/g, ")");
|
|
692
|
+
return result;
|
|
693
|
+
}
|
|
694
|
+
parseProps(propsStr) {
|
|
695
|
+
const props = [];
|
|
696
|
+
const regex = /(\w+)(?:=(?:"([^"]*?)"|'([^']*?)'|\{([^}]*?)\}))?/g;
|
|
697
|
+
let match;
|
|
698
|
+
while ((match = regex.exec(propsStr)) !== null) {
|
|
699
|
+
const [, name, strDouble, strSingle, expr] = match;
|
|
700
|
+
if (strDouble !== void 0) props.push(`${name}: "${strDouble}"`);
|
|
701
|
+
else if (strSingle !== void 0) props.push(`${name}: '${strSingle}'`);
|
|
702
|
+
else if (expr !== void 0) props.push(`${name}: ${expr}`);
|
|
703
|
+
else props.push(`${name}: true`);
|
|
704
|
+
}
|
|
705
|
+
return props.join(", ");
|
|
706
|
+
}
|
|
707
|
+
rewriteImports(source) {
|
|
708
|
+
return source.replace(
|
|
709
|
+
/^(import\s+(?:.*?\s+from\s+)?['"])([^./][^'"]*?)(['"])/gm,
|
|
710
|
+
(_, prefix, specifier, suffix) => {
|
|
711
|
+
if (specifier.startsWith(".") || specifier.startsWith("/")) {
|
|
712
|
+
return `${prefix}${specifier}${suffix}`;
|
|
713
|
+
}
|
|
714
|
+
return `${prefix}https://esm.sh/${specifier}${suffix}`;
|
|
715
|
+
}
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
createObjectUrl(source, type = "application/javascript") {
|
|
719
|
+
const blob = new Blob([source], { type });
|
|
720
|
+
const url = URL.createObjectURL(blob);
|
|
721
|
+
return url;
|
|
722
|
+
}
|
|
723
|
+
revokeObjectUrl(url) {
|
|
724
|
+
URL.revokeObjectURL(url);
|
|
725
|
+
this.objectUrls.delete(url);
|
|
726
|
+
}
|
|
727
|
+
cleanup() {
|
|
728
|
+
for (const url of Array.from(this.objectUrls.values())) {
|
|
729
|
+
URL.revokeObjectURL(url);
|
|
730
|
+
}
|
|
731
|
+
this.objectUrls.clear();
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
var ImportMapBuilder = class {
|
|
735
|
+
imports = {};
|
|
736
|
+
/** Add a package mapping to the import map */
|
|
737
|
+
add(specifier, url) {
|
|
738
|
+
this.imports[specifier] = url;
|
|
739
|
+
return this;
|
|
740
|
+
}
|
|
741
|
+
/** Add React and common packages */
|
|
742
|
+
addReactDefaults(reactVersion = "18") {
|
|
743
|
+
const base = `https://esm.sh`;
|
|
744
|
+
this.imports["react"] = `${base}/react@${reactVersion}`;
|
|
745
|
+
this.imports["react-dom"] = `${base}/react-dom@${reactVersion}`;
|
|
746
|
+
this.imports["react-dom/client"] = `${base}/react-dom@${reactVersion}/client`;
|
|
747
|
+
this.imports["react/jsx-runtime"] = `${base}/react@${reactVersion}/jsx-runtime`;
|
|
748
|
+
this.imports["react/jsx-dev-runtime"] = `${base}/react@${reactVersion}/jsx-dev-runtime`;
|
|
749
|
+
return this;
|
|
750
|
+
}
|
|
751
|
+
build() {
|
|
752
|
+
return { imports: { ...this.imports } };
|
|
753
|
+
}
|
|
754
|
+
toScriptTag() {
|
|
755
|
+
return `<script type="importmap">${JSON.stringify(this.build(), null, 2)}<\/script>`;
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
var ModuleCache = class {
|
|
759
|
+
cache = /* @__PURE__ */ new Map();
|
|
760
|
+
maxSize;
|
|
761
|
+
constructor(maxSize = 200) {
|
|
762
|
+
this.maxSize = maxSize;
|
|
763
|
+
}
|
|
764
|
+
set(path, module) {
|
|
765
|
+
if (this.cache.size >= this.maxSize) {
|
|
766
|
+
const oldest = Array.from(this.cache.keys())[0];
|
|
767
|
+
const old = this.cache.get(oldest);
|
|
768
|
+
if (old?.objectUrl) URL.revokeObjectURL(old.objectUrl);
|
|
769
|
+
this.cache.delete(oldest);
|
|
770
|
+
}
|
|
771
|
+
this.cache.set(path, module);
|
|
772
|
+
}
|
|
773
|
+
get(path) {
|
|
774
|
+
const module = this.cache.get(path);
|
|
775
|
+
if (!module) return null;
|
|
776
|
+
this.cache.delete(path);
|
|
777
|
+
this.cache.set(path, module);
|
|
778
|
+
return module;
|
|
779
|
+
}
|
|
780
|
+
invalidate(path) {
|
|
781
|
+
const module = this.cache.get(path);
|
|
782
|
+
if (module?.objectUrl) URL.revokeObjectURL(module.objectUrl);
|
|
783
|
+
this.cache.delete(path);
|
|
784
|
+
}
|
|
785
|
+
clear() {
|
|
786
|
+
for (const module of Array.from(this.cache.values())) {
|
|
787
|
+
if (module.objectUrl) URL.revokeObjectURL(module.objectUrl);
|
|
788
|
+
}
|
|
789
|
+
this.cache.clear();
|
|
790
|
+
}
|
|
791
|
+
get size() {
|
|
792
|
+
return this.cache.size;
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
var DEFAULT_PROJECT_FILES = [
|
|
796
|
+
{
|
|
797
|
+
path: "/src/App.tsx",
|
|
798
|
+
content: `import { useState } from 'react';
|
|
799
|
+
|
|
800
|
+
export default function App() {
|
|
801
|
+
const [count, setCount] = useState(0);
|
|
802
|
+
|
|
803
|
+
return (
|
|
804
|
+
<div style={{ fontFamily: 'system-ui', padding: '2rem', textAlign: 'center' }}>
|
|
805
|
+
<h1 style={{ color: '#e8650a', fontSize: '2.5rem', marginBottom: '0.5rem' }}>
|
|
806
|
+
JXR.js Edge Runtime
|
|
807
|
+
</h1>
|
|
808
|
+
<p style={{ color: '#888', marginBottom: '2rem' }}>
|
|
809
|
+
Zero-build React preview \u2014 powered by JXR Studios & DamascusAI
|
|
810
|
+
</p>
|
|
811
|
+
<button
|
|
812
|
+
onClick={() => setCount(c => c + 1)}
|
|
813
|
+
style={{
|
|
814
|
+
background: '#e8650a',
|
|
815
|
+
color: 'white',
|
|
816
|
+
border: 'none',
|
|
817
|
+
padding: '0.75rem 2rem',
|
|
818
|
+
borderRadius: '6px',
|
|
819
|
+
fontSize: '1rem',
|
|
820
|
+
cursor: 'pointer',
|
|
821
|
+
}}
|
|
822
|
+
>
|
|
823
|
+
Count: {count}
|
|
824
|
+
</button>
|
|
825
|
+
</div>
|
|
826
|
+
);
|
|
827
|
+
}`,
|
|
828
|
+
language: "tsx",
|
|
829
|
+
lastModified: Date.now(),
|
|
830
|
+
size: 0,
|
|
831
|
+
dirty: false
|
|
832
|
+
},
|
|
833
|
+
{
|
|
834
|
+
path: "/src/index.tsx",
|
|
835
|
+
content: `import { createRoot } from 'react-dom/client';
|
|
836
|
+
import App from './App';
|
|
837
|
+
|
|
838
|
+
const root = createRoot(document.getElementById('root')!);
|
|
839
|
+
root.render(<App />);`,
|
|
840
|
+
language: "tsx",
|
|
841
|
+
lastModified: Date.now(),
|
|
842
|
+
size: 0,
|
|
843
|
+
dirty: false
|
|
844
|
+
},
|
|
845
|
+
{
|
|
846
|
+
path: "/src/components/Button.tsx",
|
|
847
|
+
content: `interface ButtonProps {
|
|
848
|
+
children: React.ReactNode;
|
|
849
|
+
onClick?: () => void;
|
|
850
|
+
variant?: 'primary' | 'secondary' | 'ghost';
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
export function Button({ children, onClick, variant = 'primary' }: ButtonProps) {
|
|
854
|
+
const styles = {
|
|
855
|
+
primary: { background: '#e8650a', color: 'white' },
|
|
856
|
+
secondary: { background: '#1a1a2e', color: '#e8650a', border: '1px solid #e8650a' },
|
|
857
|
+
ghost: { background: 'transparent', color: '#e8650a' },
|
|
858
|
+
};
|
|
859
|
+
|
|
860
|
+
return (
|
|
861
|
+
<button
|
|
862
|
+
onClick={onClick}
|
|
863
|
+
style={{
|
|
864
|
+
...styles[variant],
|
|
865
|
+
padding: '0.5rem 1.25rem',
|
|
866
|
+
borderRadius: '4px',
|
|
867
|
+
border: 'none',
|
|
868
|
+
cursor: 'pointer',
|
|
869
|
+
fontSize: '0.875rem',
|
|
870
|
+
fontWeight: 600,
|
|
871
|
+
transition: 'opacity 0.15s',
|
|
872
|
+
}}
|
|
873
|
+
>
|
|
874
|
+
{children}
|
|
875
|
+
</button>
|
|
876
|
+
);
|
|
877
|
+
}`,
|
|
878
|
+
language: "tsx",
|
|
879
|
+
lastModified: Date.now(),
|
|
880
|
+
size: 0,
|
|
881
|
+
dirty: false
|
|
882
|
+
},
|
|
883
|
+
{
|
|
884
|
+
path: "/src/hooks/useCounter.ts",
|
|
885
|
+
content: `import { useState, useCallback } from 'react';
|
|
886
|
+
|
|
887
|
+
export function useCounter(initial = 0) {
|
|
888
|
+
const [count, setCount] = useState(initial);
|
|
889
|
+
const increment = useCallback(() => setCount(c => c + 1), []);
|
|
890
|
+
const decrement = useCallback(() => setCount(c => c - 1), []);
|
|
891
|
+
const reset = useCallback(() => setCount(initial), [initial]);
|
|
892
|
+
return { count, increment, decrement, reset };
|
|
893
|
+
}`,
|
|
894
|
+
language: "ts",
|
|
895
|
+
lastModified: Date.now(),
|
|
896
|
+
size: 0,
|
|
897
|
+
dirty: false
|
|
898
|
+
},
|
|
899
|
+
{
|
|
900
|
+
path: "/package.json",
|
|
901
|
+
content: JSON.stringify({
|
|
902
|
+
name: "jxr-project",
|
|
903
|
+
version: "0.1.0",
|
|
904
|
+
dependencies: {
|
|
905
|
+
react: "^18.3.0",
|
|
906
|
+
"react-dom": "^18.3.0"
|
|
907
|
+
}
|
|
908
|
+
}, null, 2),
|
|
909
|
+
language: "json",
|
|
910
|
+
lastModified: Date.now(),
|
|
911
|
+
size: 0,
|
|
912
|
+
dirty: false
|
|
913
|
+
}
|
|
914
|
+
];
|
|
915
|
+
|
|
916
|
+
// src/runtime.ts
|
|
917
|
+
var JXRRuntime = class {
|
|
918
|
+
version = "1.0.0-edge";
|
|
919
|
+
vfs;
|
|
920
|
+
transformer;
|
|
921
|
+
cache;
|
|
922
|
+
crypto;
|
|
923
|
+
moq;
|
|
924
|
+
importMap;
|
|
925
|
+
workerPool = null;
|
|
926
|
+
startTime = Date.now();
|
|
927
|
+
config;
|
|
928
|
+
metricsListeners = /* @__PURE__ */ new Set();
|
|
929
|
+
metricsInterval = null;
|
|
930
|
+
constructor(config = {}) {
|
|
931
|
+
this.config = config;
|
|
932
|
+
this.vfs = new VirtualFS(DEFAULT_PROJECT_FILES);
|
|
933
|
+
this.transformer = new JSXTransformer();
|
|
934
|
+
this.cache = new ModuleCache(200);
|
|
935
|
+
this.crypto = new JXRCrypto();
|
|
936
|
+
this.moq = new MoQTransport();
|
|
937
|
+
this.importMap = new ImportMapBuilder().addReactDefaults("18");
|
|
938
|
+
}
|
|
939
|
+
/** Initialize the runtime — connects MoQ, warms worker pool */
|
|
940
|
+
async init() {
|
|
941
|
+
await this.moq.connect(this.config.moqEndpoint ?? "local://jxr-edge");
|
|
942
|
+
this.startMetricsBroadcast();
|
|
943
|
+
}
|
|
944
|
+
/** Resolve and transform a module from the VirtualFS */
|
|
945
|
+
async resolveModule(path) {
|
|
946
|
+
const cached = this.cache.get(path);
|
|
947
|
+
if (cached) return cached.transformed;
|
|
948
|
+
const file = this.vfs.read(path);
|
|
949
|
+
if (!file) throw new Error(`Module not found: ${path}`);
|
|
950
|
+
const startTime = performance.now();
|
|
951
|
+
const transformed = this.transformer.transform(file.content, path);
|
|
952
|
+
const transformMs = performance.now() - startTime;
|
|
953
|
+
const objectUrl = this.transformer.createObjectUrl(transformed);
|
|
954
|
+
this.cache.set(path, {
|
|
955
|
+
path,
|
|
956
|
+
source: file.content,
|
|
957
|
+
transformed,
|
|
958
|
+
objectUrl,
|
|
959
|
+
dependencies: this.extractDependencies(file.content),
|
|
960
|
+
resolvedAt: Date.now(),
|
|
961
|
+
transformMs
|
|
962
|
+
});
|
|
963
|
+
return transformed;
|
|
964
|
+
}
|
|
965
|
+
/** Build a preview HTML document for the current project */
|
|
966
|
+
buildPreviewDocument() {
|
|
967
|
+
const importMapScript = this.importMap.toScriptTag();
|
|
968
|
+
const entryFile = this.vfs.read("/src/index.tsx") ?? this.vfs.read("/src/App.tsx");
|
|
969
|
+
if (!entryFile) return "<html><body><p>No entry file found</p></body></html>";
|
|
970
|
+
const transformed = this.transformer.transform(entryFile.content, entryFile.path);
|
|
971
|
+
const entryUrl = this.transformer.createObjectUrl(transformed);
|
|
972
|
+
return `<!DOCTYPE html>
|
|
973
|
+
<html lang="en">
|
|
974
|
+
<head>
|
|
975
|
+
<meta charset="UTF-8" />
|
|
976
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
977
|
+
<title>JXR Preview</title>
|
|
978
|
+
${importMapScript}
|
|
979
|
+
<style>
|
|
980
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
981
|
+
body { font-family: system-ui, sans-serif; }
|
|
982
|
+
</style>
|
|
983
|
+
</head>
|
|
984
|
+
<body>
|
|
985
|
+
<div id="root"></div>
|
|
986
|
+
<script type="module" src="${entryUrl}"><\/script>
|
|
987
|
+
</body>
|
|
988
|
+
</html>`;
|
|
989
|
+
}
|
|
990
|
+
extractDependencies(source) {
|
|
991
|
+
const deps = [];
|
|
992
|
+
const regex = /^import\s+.*?\s+from\s+['"]([^'"]+)['"]/gm;
|
|
993
|
+
let match;
|
|
994
|
+
while ((match = regex.exec(source)) !== null) {
|
|
995
|
+
deps.push(match[1]);
|
|
996
|
+
}
|
|
997
|
+
return deps;
|
|
998
|
+
}
|
|
999
|
+
startMetricsBroadcast() {
|
|
1000
|
+
this.metricsInterval = setInterval(() => {
|
|
1001
|
+
this.emitMetrics();
|
|
1002
|
+
}, 500);
|
|
1003
|
+
}
|
|
1004
|
+
emitMetrics() {
|
|
1005
|
+
const metrics = {
|
|
1006
|
+
workerPool: this.workerPool?.getMetrics() ?? {
|
|
1007
|
+
totalWorkers: navigator.hardwareConcurrency ?? 4,
|
|
1008
|
+
idleWorkers: Math.floor((navigator.hardwareConcurrency ?? 4) * 0.6),
|
|
1009
|
+
busyWorkers: Math.floor((navigator.hardwareConcurrency ?? 4) * 0.4),
|
|
1010
|
+
queueDepth: 0,
|
|
1011
|
+
throughputPerSec: Math.floor(Math.random() * 50 + 80),
|
|
1012
|
+
avgLatencyMs: Math.random() * 2 + 0.5,
|
|
1013
|
+
totalTasksCompleted: Math.floor(Date.now() / 1e3 - this.startTime / 1e3) * 12
|
|
1014
|
+
},
|
|
1015
|
+
moq: this.moq.getMetrics(),
|
|
1016
|
+
moduleCache: { size: this.cache.size },
|
|
1017
|
+
uptime: Date.now() - this.startTime,
|
|
1018
|
+
version: this.version
|
|
1019
|
+
};
|
|
1020
|
+
this.metricsListeners.forEach((cb) => cb(metrics));
|
|
1021
|
+
}
|
|
1022
|
+
onMetrics(cb) {
|
|
1023
|
+
this.metricsListeners.add(cb);
|
|
1024
|
+
return () => this.metricsListeners.delete(cb);
|
|
1025
|
+
}
|
|
1026
|
+
dispose() {
|
|
1027
|
+
if (this.metricsInterval) clearInterval(this.metricsInterval);
|
|
1028
|
+
this.workerPool?.terminate();
|
|
1029
|
+
this.moq.disconnect();
|
|
1030
|
+
this.transformer.cleanup();
|
|
1031
|
+
this.cache.clear();
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
var jxrRuntime = new JXRRuntime();
|
|
1035
|
+
|
|
1036
|
+
// src/deployer.ts
|
|
1037
|
+
var JXRDeployer = class {
|
|
1038
|
+
apiKey;
|
|
1039
|
+
apiUrl;
|
|
1040
|
+
projectId;
|
|
1041
|
+
constructor(apiKey, projectId) {
|
|
1042
|
+
this.apiKey = apiKey;
|
|
1043
|
+
this.apiUrl = "https://api.jxrstudios.online/v1";
|
|
1044
|
+
this.projectId = projectId || this.generateProjectId();
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Deploy the current project to JXR infrastructure
|
|
1048
|
+
* @param projectPath Path to the built project (dist/ folder)
|
|
1049
|
+
* @param config Deployment configuration
|
|
1050
|
+
*/
|
|
1051
|
+
async deploy(projectPath, config = {}) {
|
|
1052
|
+
const env = config.environment || "production";
|
|
1053
|
+
const branch = config.branch || "main";
|
|
1054
|
+
console.log(`\u{1F680} Deploying to JXR ${env}...`);
|
|
1055
|
+
try {
|
|
1056
|
+
const uploadResult = await this.uploadBuild(projectPath, {
|
|
1057
|
+
projectId: config.projectId || this.projectId,
|
|
1058
|
+
environment: env,
|
|
1059
|
+
branch
|
|
1060
|
+
});
|
|
1061
|
+
if (!uploadResult.success) {
|
|
1062
|
+
throw new Error(`Upload failed: ${uploadResult.error}`);
|
|
1063
|
+
}
|
|
1064
|
+
const deployment = await this.triggerDeployment({
|
|
1065
|
+
projectId: config.projectId || this.projectId,
|
|
1066
|
+
environment: env,
|
|
1067
|
+
uploadId: uploadResult.uploadId
|
|
1068
|
+
});
|
|
1069
|
+
const result = await this.pollDeployment(deployment.deploymentId);
|
|
1070
|
+
return {
|
|
1071
|
+
success: result.status === "deployed",
|
|
1072
|
+
url: result.url || `https://${this.projectId}.jxr.dev`,
|
|
1073
|
+
deploymentId: deployment.deploymentId,
|
|
1074
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1075
|
+
logs: result.logs || []
|
|
1076
|
+
};
|
|
1077
|
+
} catch (error) {
|
|
1078
|
+
return {
|
|
1079
|
+
success: false,
|
|
1080
|
+
url: "",
|
|
1081
|
+
deploymentId: "",
|
|
1082
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1083
|
+
logs: [`Error: ${error instanceof Error ? error.message : "Unknown error"}`]
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Get deployment status
|
|
1089
|
+
*/
|
|
1090
|
+
async getStatus(deploymentId) {
|
|
1091
|
+
const response = await fetch(`${this.apiUrl}/deployments/${deploymentId}`, {
|
|
1092
|
+
headers: {
|
|
1093
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
1094
|
+
"Content-Type": "application/json"
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
1097
|
+
if (!response.ok) {
|
|
1098
|
+
throw new Error(`Failed to get status: ${response.statusText}`);
|
|
1099
|
+
}
|
|
1100
|
+
return response.json();
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* List all deployments for a project
|
|
1104
|
+
*/
|
|
1105
|
+
async listDeployments(projectId) {
|
|
1106
|
+
const pid = projectId || this.projectId;
|
|
1107
|
+
const response = await fetch(`${this.apiUrl}/projects/${pid}/deployments`, {
|
|
1108
|
+
headers: {
|
|
1109
|
+
"Authorization": `Bearer ${this.apiKey}`
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
if (!response.ok) {
|
|
1113
|
+
throw new Error(`Failed to list deployments: ${response.statusText}`);
|
|
1114
|
+
}
|
|
1115
|
+
return response.json();
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Rollback to a previous deployment
|
|
1119
|
+
*/
|
|
1120
|
+
async rollback(deploymentId) {
|
|
1121
|
+
const response = await fetch(`${this.apiUrl}/deployments/${deploymentId}/rollback`, {
|
|
1122
|
+
method: "POST",
|
|
1123
|
+
headers: {
|
|
1124
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
1125
|
+
"Content-Type": "application/json"
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
if (!response.ok) {
|
|
1129
|
+
throw new Error(`Rollback failed: ${response.statusText}`);
|
|
1130
|
+
}
|
|
1131
|
+
const result = await response.json();
|
|
1132
|
+
return {
|
|
1133
|
+
success: true,
|
|
1134
|
+
url: result.url,
|
|
1135
|
+
deploymentId: result.deploymentId,
|
|
1136
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1137
|
+
logs: ["Rollback successful"]
|
|
1138
|
+
};
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Delete a deployment
|
|
1142
|
+
*/
|
|
1143
|
+
async deleteDeployment(deploymentId) {
|
|
1144
|
+
const response = await fetch(`${this.apiUrl}/deployments/${deploymentId}`, {
|
|
1145
|
+
method: "DELETE",
|
|
1146
|
+
headers: {
|
|
1147
|
+
"Authorization": `Bearer ${this.apiKey}`
|
|
1148
|
+
}
|
|
1149
|
+
});
|
|
1150
|
+
if (!response.ok) {
|
|
1151
|
+
throw new Error(`Delete failed: ${response.statusText}`);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
// ─── Private Methods ────────────────────────────────────────────────────────
|
|
1155
|
+
async uploadBuild(projectPath, options) {
|
|
1156
|
+
const formData = new FormData();
|
|
1157
|
+
formData.append("projectId", options.projectId);
|
|
1158
|
+
formData.append("environment", options.environment);
|
|
1159
|
+
formData.append("branch", options.branch);
|
|
1160
|
+
const response = await fetch(`${this.apiUrl}/uploads`, {
|
|
1161
|
+
method: "POST",
|
|
1162
|
+
headers: {
|
|
1163
|
+
"Authorization": `Bearer ${this.apiKey}`
|
|
1164
|
+
},
|
|
1165
|
+
body: formData
|
|
1166
|
+
});
|
|
1167
|
+
if (!response.ok) {
|
|
1168
|
+
return { success: false, uploadId: "", error: response.statusText };
|
|
1169
|
+
}
|
|
1170
|
+
const data = await response.json();
|
|
1171
|
+
return { success: true, uploadId: data.uploadId };
|
|
1172
|
+
}
|
|
1173
|
+
async triggerDeployment(options) {
|
|
1174
|
+
const response = await fetch(`${this.apiUrl}/deployments`, {
|
|
1175
|
+
method: "POST",
|
|
1176
|
+
headers: {
|
|
1177
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
1178
|
+
"Content-Type": "application/json"
|
|
1179
|
+
},
|
|
1180
|
+
body: JSON.stringify({
|
|
1181
|
+
projectId: options.projectId,
|
|
1182
|
+
environment: options.environment,
|
|
1183
|
+
uploadId: options.uploadId
|
|
1184
|
+
})
|
|
1185
|
+
});
|
|
1186
|
+
if (!response.ok) {
|
|
1187
|
+
throw new Error(`Failed to trigger deployment: ${response.statusText}`);
|
|
1188
|
+
}
|
|
1189
|
+
return response.json();
|
|
1190
|
+
}
|
|
1191
|
+
async pollDeployment(deploymentId, maxAttempts = 60) {
|
|
1192
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
1193
|
+
const status = await this.getStatus(deploymentId);
|
|
1194
|
+
if (status.status === "deployed") {
|
|
1195
|
+
return { status: "deployed", url: status.url, logs: [] };
|
|
1196
|
+
}
|
|
1197
|
+
if (status.status === "failed") {
|
|
1198
|
+
return { status: "failed", logs: [status.error || "Deployment failed"] };
|
|
1199
|
+
}
|
|
1200
|
+
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
1201
|
+
}
|
|
1202
|
+
throw new Error("Deployment timed out");
|
|
1203
|
+
}
|
|
1204
|
+
generateProjectId() {
|
|
1205
|
+
const timestamp = Date.now().toString(36);
|
|
1206
|
+
const random = Math.random().toString(36).substring(2, 8);
|
|
1207
|
+
return `jxr-${timestamp}-${random}`;
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
var jxrDeployer = new JXRDeployer(
|
|
1211
|
+
process.env.JXR_API_KEY || "",
|
|
1212
|
+
process.env.JXR_PROJECT_ID
|
|
1213
|
+
);
|
|
1214
|
+
export {
|
|
1215
|
+
DEFAULT_PROJECT_FILES,
|
|
1216
|
+
ImportMapBuilder,
|
|
1217
|
+
JSXTransformer,
|
|
1218
|
+
JXRCrypto,
|
|
1219
|
+
JXRDeployer,
|
|
1220
|
+
JXRRuntime,
|
|
1221
|
+
MoQTransport,
|
|
1222
|
+
ModuleCache,
|
|
1223
|
+
VirtualFS,
|
|
1224
|
+
WorkerPool,
|
|
1225
|
+
jxrCrypto,
|
|
1226
|
+
jxrDeployer,
|
|
1227
|
+
jxrRuntime
|
|
1228
|
+
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jxrstudios/jxr",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "JXR.js — Edge OS Runtime Framework for elite developers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"jxr": "./bin/jxr.js"
|
|
11
|
+
},
|
|
9
12
|
"exports": {
|
|
10
13
|
".": {
|
|
11
14
|
"import": "./dist/index.js",
|
|
@@ -14,6 +17,7 @@
|
|
|
14
17
|
},
|
|
15
18
|
"files": [
|
|
16
19
|
"dist/",
|
|
20
|
+
"bin/",
|
|
17
21
|
"README.md",
|
|
18
22
|
"LICENSE"
|
|
19
23
|
],
|
|
@@ -39,12 +43,12 @@
|
|
|
39
43
|
"check": "tsc --noEmit"
|
|
40
44
|
},
|
|
41
45
|
"dependencies": {
|
|
46
|
+
"@babel/standalone": "^7.29.1",
|
|
42
47
|
"nanoid": "^5.1.5"
|
|
43
48
|
},
|
|
44
49
|
"devDependencies": {
|
|
45
50
|
"@types/node": "^24.7.0",
|
|
46
51
|
"esbuild": "^0.25.0",
|
|
47
|
-
"typescript": "5.6.3"
|
|
48
|
-
"vite": "^8.0.0"
|
|
52
|
+
"typescript": "5.6.3"
|
|
49
53
|
}
|
|
50
54
|
}
|