@kevisual/router 0.1.0 → 0.1.2
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/dist/app.js +774 -281
- package/dist/commander.d.ts +9 -11
- package/dist/commander.js +562 -15
- package/dist/opencode.d.ts +0 -8
- package/dist/opencode.js +567 -84
- package/dist/router-browser.d.ts +0 -8
- package/dist/router-browser.js +18868 -275
- package/dist/router-define.d.ts +0 -8
- package/dist/router-simple.js +16 -2
- package/dist/router.d.ts +0 -8
- package/dist/router.js +752 -259
- package/dist/ws.d.ts +0 -8
- package/dist/ws.js +49 -24
- package/package.json +13 -8
- package/src/app.ts +1 -1
- package/src/commander.ts +71 -10
- package/src/route.ts +6 -15
- package/src/utils/random.ts +11 -2
package/dist/commander.js
CHANGED
|
@@ -4,21 +4,35 @@ var __getProtoOf = Object.getPrototypeOf;
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
function __accessProp(key) {
|
|
8
|
+
return this[key];
|
|
9
|
+
}
|
|
10
|
+
var __toESMCache_node;
|
|
11
|
+
var __toESMCache_esm;
|
|
7
12
|
var __toESM = (mod, isNodeMode, target) => {
|
|
13
|
+
var canCache = mod != null && typeof mod === "object";
|
|
14
|
+
if (canCache) {
|
|
15
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
16
|
+
var cached = cache.get(mod);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
8
20
|
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
21
|
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
22
|
for (let key of __getOwnPropNames(mod))
|
|
11
23
|
if (!__hasOwnProp.call(to, key))
|
|
12
24
|
__defProp(to, key, {
|
|
13
|
-
get: (
|
|
25
|
+
get: __accessProp.bind(mod, key),
|
|
14
26
|
enumerable: true
|
|
15
27
|
});
|
|
28
|
+
if (canCache)
|
|
29
|
+
cache.set(mod, to);
|
|
16
30
|
return to;
|
|
17
31
|
};
|
|
18
32
|
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
19
33
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
20
34
|
|
|
21
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/error.js
|
|
35
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/error.js
|
|
22
36
|
var require_error = __commonJS((exports) => {
|
|
23
37
|
class CommanderError extends Error {
|
|
24
38
|
constructor(exitCode, code, message) {
|
|
@@ -42,7 +56,7 @@ var require_error = __commonJS((exports) => {
|
|
|
42
56
|
exports.InvalidArgumentError = InvalidArgumentError;
|
|
43
57
|
});
|
|
44
58
|
|
|
45
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/argument.js
|
|
59
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/argument.js
|
|
46
60
|
var require_argument = __commonJS((exports) => {
|
|
47
61
|
var { InvalidArgumentError } = require_error();
|
|
48
62
|
|
|
@@ -122,7 +136,7 @@ var require_argument = __commonJS((exports) => {
|
|
|
122
136
|
exports.humanReadableArgName = humanReadableArgName;
|
|
123
137
|
});
|
|
124
138
|
|
|
125
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/help.js
|
|
139
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/help.js
|
|
126
140
|
var require_help = __commonJS((exports) => {
|
|
127
141
|
var { humanReadableArgName } = require_argument();
|
|
128
142
|
|
|
@@ -479,7 +493,7 @@ ${itemIndentStr}`);
|
|
|
479
493
|
exports.stripColor = stripColor;
|
|
480
494
|
});
|
|
481
495
|
|
|
482
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/option.js
|
|
496
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/option.js
|
|
483
497
|
var require_option = __commonJS((exports) => {
|
|
484
498
|
var { InvalidArgumentError } = require_error();
|
|
485
499
|
|
|
@@ -663,7 +677,7 @@ var require_option = __commonJS((exports) => {
|
|
|
663
677
|
exports.DualOptions = DualOptions;
|
|
664
678
|
});
|
|
665
679
|
|
|
666
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/suggestSimilar.js
|
|
680
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/suggestSimilar.js
|
|
667
681
|
var require_suggestSimilar = __commonJS((exports) => {
|
|
668
682
|
var maxDistance = 3;
|
|
669
683
|
function editDistance(a, b) {
|
|
@@ -736,7 +750,7 @@ var require_suggestSimilar = __commonJS((exports) => {
|
|
|
736
750
|
exports.suggestSimilar = suggestSimilar;
|
|
737
751
|
});
|
|
738
752
|
|
|
739
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/command.js
|
|
753
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/lib/command.js
|
|
740
754
|
var require_command = __commonJS((exports) => {
|
|
741
755
|
var EventEmitter = __require("node:events").EventEmitter;
|
|
742
756
|
var childProcess = __require("node:child_process");
|
|
@@ -2091,7 +2105,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
|
|
|
2091
2105
|
exports.useColor = useColor;
|
|
2092
2106
|
});
|
|
2093
2107
|
|
|
2094
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/index.js
|
|
2108
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/index.js
|
|
2095
2109
|
var require_commander = __commonJS((exports) => {
|
|
2096
2110
|
var { Argument } = require_argument();
|
|
2097
2111
|
var { Command } = require_command();
|
|
@@ -2111,7 +2125,7 @@ var require_commander = __commonJS((exports) => {
|
|
|
2111
2125
|
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
2112
2126
|
});
|
|
2113
2127
|
|
|
2114
|
-
// node_modules/.pnpm/commander@14.0.3/node_modules/commander/esm.mjs
|
|
2128
|
+
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/esm.mjs
|
|
2115
2129
|
var import__ = __toESM(require_commander(), 1);
|
|
2116
2130
|
var {
|
|
2117
2131
|
program,
|
|
@@ -2127,6 +2141,490 @@ var {
|
|
|
2127
2141
|
Help
|
|
2128
2142
|
} = import__.default;
|
|
2129
2143
|
|
|
2144
|
+
// ../../node_modules/.pnpm/@kevisual+remote-app@0.0.7/node_modules/@kevisual/remote-app/dist/app.js
|
|
2145
|
+
var __create2 = Object.create;
|
|
2146
|
+
var __getProtoOf2 = Object.getPrototypeOf;
|
|
2147
|
+
var __defProp2 = Object.defineProperty;
|
|
2148
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
2149
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
2150
|
+
var __toESM2 = (mod, isNodeMode, target) => {
|
|
2151
|
+
target = mod != null ? __create2(__getProtoOf2(mod)) : {};
|
|
2152
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", { value: mod, enumerable: true }) : target;
|
|
2153
|
+
for (let key of __getOwnPropNames2(mod))
|
|
2154
|
+
if (!__hasOwnProp2.call(to, key))
|
|
2155
|
+
__defProp2(to, key, {
|
|
2156
|
+
get: () => mod[key],
|
|
2157
|
+
enumerable: true
|
|
2158
|
+
});
|
|
2159
|
+
return to;
|
|
2160
|
+
};
|
|
2161
|
+
var __commonJS2 = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
2162
|
+
var require_eventemitter3 = __commonJS2((exports, module) => {
|
|
2163
|
+
var has = Object.prototype.hasOwnProperty;
|
|
2164
|
+
var prefix = "~";
|
|
2165
|
+
function Events() {}
|
|
2166
|
+
if (Object.create) {
|
|
2167
|
+
Events.prototype = Object.create(null);
|
|
2168
|
+
if (!new Events().__proto__)
|
|
2169
|
+
prefix = false;
|
|
2170
|
+
}
|
|
2171
|
+
function EE(fn, context, once) {
|
|
2172
|
+
this.fn = fn;
|
|
2173
|
+
this.context = context;
|
|
2174
|
+
this.once = once || false;
|
|
2175
|
+
}
|
|
2176
|
+
function addListener(emitter, event, fn, context, once) {
|
|
2177
|
+
if (typeof fn !== "function") {
|
|
2178
|
+
throw new TypeError("The listener must be a function");
|
|
2179
|
+
}
|
|
2180
|
+
var listener = new EE(fn, context || emitter, once), evt = prefix ? prefix + event : event;
|
|
2181
|
+
if (!emitter._events[evt])
|
|
2182
|
+
emitter._events[evt] = listener, emitter._eventsCount++;
|
|
2183
|
+
else if (!emitter._events[evt].fn)
|
|
2184
|
+
emitter._events[evt].push(listener);
|
|
2185
|
+
else
|
|
2186
|
+
emitter._events[evt] = [emitter._events[evt], listener];
|
|
2187
|
+
return emitter;
|
|
2188
|
+
}
|
|
2189
|
+
function clearEvent(emitter, evt) {
|
|
2190
|
+
if (--emitter._eventsCount === 0)
|
|
2191
|
+
emitter._events = new Events;
|
|
2192
|
+
else
|
|
2193
|
+
delete emitter._events[evt];
|
|
2194
|
+
}
|
|
2195
|
+
function EventEmitter() {
|
|
2196
|
+
this._events = new Events;
|
|
2197
|
+
this._eventsCount = 0;
|
|
2198
|
+
}
|
|
2199
|
+
EventEmitter.prototype.eventNames = function eventNames() {
|
|
2200
|
+
var names = [], events, name;
|
|
2201
|
+
if (this._eventsCount === 0)
|
|
2202
|
+
return names;
|
|
2203
|
+
for (name in events = this._events) {
|
|
2204
|
+
if (has.call(events, name))
|
|
2205
|
+
names.push(prefix ? name.slice(1) : name);
|
|
2206
|
+
}
|
|
2207
|
+
if (Object.getOwnPropertySymbols) {
|
|
2208
|
+
return names.concat(Object.getOwnPropertySymbols(events));
|
|
2209
|
+
}
|
|
2210
|
+
return names;
|
|
2211
|
+
};
|
|
2212
|
+
EventEmitter.prototype.listeners = function listeners(event) {
|
|
2213
|
+
var evt = prefix ? prefix + event : event, handlers = this._events[evt];
|
|
2214
|
+
if (!handlers)
|
|
2215
|
+
return [];
|
|
2216
|
+
if (handlers.fn)
|
|
2217
|
+
return [handlers.fn];
|
|
2218
|
+
for (var i = 0, l = handlers.length, ee = new Array(l);i < l; i++) {
|
|
2219
|
+
ee[i] = handlers[i].fn;
|
|
2220
|
+
}
|
|
2221
|
+
return ee;
|
|
2222
|
+
};
|
|
2223
|
+
EventEmitter.prototype.listenerCount = function listenerCount(event) {
|
|
2224
|
+
var evt = prefix ? prefix + event : event, listeners = this._events[evt];
|
|
2225
|
+
if (!listeners)
|
|
2226
|
+
return 0;
|
|
2227
|
+
if (listeners.fn)
|
|
2228
|
+
return 1;
|
|
2229
|
+
return listeners.length;
|
|
2230
|
+
};
|
|
2231
|
+
EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
|
|
2232
|
+
var evt = prefix ? prefix + event : event;
|
|
2233
|
+
if (!this._events[evt])
|
|
2234
|
+
return false;
|
|
2235
|
+
var listeners = this._events[evt], len = arguments.length, args, i;
|
|
2236
|
+
if (listeners.fn) {
|
|
2237
|
+
if (listeners.once)
|
|
2238
|
+
this.removeListener(event, listeners.fn, undefined, true);
|
|
2239
|
+
switch (len) {
|
|
2240
|
+
case 1:
|
|
2241
|
+
return listeners.fn.call(listeners.context), true;
|
|
2242
|
+
case 2:
|
|
2243
|
+
return listeners.fn.call(listeners.context, a1), true;
|
|
2244
|
+
case 3:
|
|
2245
|
+
return listeners.fn.call(listeners.context, a1, a2), true;
|
|
2246
|
+
case 4:
|
|
2247
|
+
return listeners.fn.call(listeners.context, a1, a2, a3), true;
|
|
2248
|
+
case 5:
|
|
2249
|
+
return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
|
|
2250
|
+
case 6:
|
|
2251
|
+
return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
|
|
2252
|
+
}
|
|
2253
|
+
for (i = 1, args = new Array(len - 1);i < len; i++) {
|
|
2254
|
+
args[i - 1] = arguments[i];
|
|
2255
|
+
}
|
|
2256
|
+
listeners.fn.apply(listeners.context, args);
|
|
2257
|
+
} else {
|
|
2258
|
+
var length = listeners.length, j;
|
|
2259
|
+
for (i = 0;i < length; i++) {
|
|
2260
|
+
if (listeners[i].once)
|
|
2261
|
+
this.removeListener(event, listeners[i].fn, undefined, true);
|
|
2262
|
+
switch (len) {
|
|
2263
|
+
case 1:
|
|
2264
|
+
listeners[i].fn.call(listeners[i].context);
|
|
2265
|
+
break;
|
|
2266
|
+
case 2:
|
|
2267
|
+
listeners[i].fn.call(listeners[i].context, a1);
|
|
2268
|
+
break;
|
|
2269
|
+
case 3:
|
|
2270
|
+
listeners[i].fn.call(listeners[i].context, a1, a2);
|
|
2271
|
+
break;
|
|
2272
|
+
case 4:
|
|
2273
|
+
listeners[i].fn.call(listeners[i].context, a1, a2, a3);
|
|
2274
|
+
break;
|
|
2275
|
+
default:
|
|
2276
|
+
if (!args)
|
|
2277
|
+
for (j = 1, args = new Array(len - 1);j < len; j++) {
|
|
2278
|
+
args[j - 1] = arguments[j];
|
|
2279
|
+
}
|
|
2280
|
+
listeners[i].fn.apply(listeners[i].context, args);
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
return true;
|
|
2285
|
+
};
|
|
2286
|
+
EventEmitter.prototype.on = function on(event, fn, context) {
|
|
2287
|
+
return addListener(this, event, fn, context, false);
|
|
2288
|
+
};
|
|
2289
|
+
EventEmitter.prototype.once = function once(event, fn, context) {
|
|
2290
|
+
return addListener(this, event, fn, context, true);
|
|
2291
|
+
};
|
|
2292
|
+
EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
|
|
2293
|
+
var evt = prefix ? prefix + event : event;
|
|
2294
|
+
if (!this._events[evt])
|
|
2295
|
+
return this;
|
|
2296
|
+
if (!fn) {
|
|
2297
|
+
clearEvent(this, evt);
|
|
2298
|
+
return this;
|
|
2299
|
+
}
|
|
2300
|
+
var listeners = this._events[evt];
|
|
2301
|
+
if (listeners.fn) {
|
|
2302
|
+
if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
|
|
2303
|
+
clearEvent(this, evt);
|
|
2304
|
+
}
|
|
2305
|
+
} else {
|
|
2306
|
+
for (var i = 0, events = [], length = listeners.length;i < length; i++) {
|
|
2307
|
+
if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) {
|
|
2308
|
+
events.push(listeners[i]);
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
if (events.length)
|
|
2312
|
+
this._events[evt] = events.length === 1 ? events[0] : events;
|
|
2313
|
+
else
|
|
2314
|
+
clearEvent(this, evt);
|
|
2315
|
+
}
|
|
2316
|
+
return this;
|
|
2317
|
+
};
|
|
2318
|
+
EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
|
|
2319
|
+
var evt;
|
|
2320
|
+
if (event) {
|
|
2321
|
+
evt = prefix ? prefix + event : event;
|
|
2322
|
+
if (this._events[evt])
|
|
2323
|
+
clearEvent(this, evt);
|
|
2324
|
+
} else {
|
|
2325
|
+
this._events = new Events;
|
|
2326
|
+
this._eventsCount = 0;
|
|
2327
|
+
}
|
|
2328
|
+
return this;
|
|
2329
|
+
};
|
|
2330
|
+
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
|
|
2331
|
+
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
|
|
2332
|
+
EventEmitter.prefixed = prefix;
|
|
2333
|
+
EventEmitter.EventEmitter = EventEmitter;
|
|
2334
|
+
if (typeof module !== "undefined") {
|
|
2335
|
+
module.exports = EventEmitter;
|
|
2336
|
+
}
|
|
2337
|
+
});
|
|
2338
|
+
var import__2 = __toESM2(require_eventemitter3(), 1);
|
|
2339
|
+
|
|
2340
|
+
class RemoteApp {
|
|
2341
|
+
mainApp;
|
|
2342
|
+
url;
|
|
2343
|
+
id;
|
|
2344
|
+
username;
|
|
2345
|
+
emitter;
|
|
2346
|
+
isConnected;
|
|
2347
|
+
isVerified;
|
|
2348
|
+
ws;
|
|
2349
|
+
remoteIsConnected;
|
|
2350
|
+
isError = false;
|
|
2351
|
+
autoReconnect;
|
|
2352
|
+
maxReconnectAttempts;
|
|
2353
|
+
reconnectDelay;
|
|
2354
|
+
maxReconnectDelay;
|
|
2355
|
+
enableBackoff;
|
|
2356
|
+
reconnectAttempts = 0;
|
|
2357
|
+
reconnectTimer = null;
|
|
2358
|
+
isManuallyClosed = false;
|
|
2359
|
+
isInitializing = false;
|
|
2360
|
+
initId = 0;
|
|
2361
|
+
constructor(opts) {
|
|
2362
|
+
this.mainApp = opts?.app;
|
|
2363
|
+
const token = opts.token;
|
|
2364
|
+
const url = opts.url || "https://kevisual.cn/ws/proxy";
|
|
2365
|
+
const id = opts.id;
|
|
2366
|
+
const username = opts.username;
|
|
2367
|
+
this.username = username;
|
|
2368
|
+
this.emitter = opts?.emitter || new import__2.default;
|
|
2369
|
+
const _url = new URL(url);
|
|
2370
|
+
if (token) {
|
|
2371
|
+
_url.searchParams.set("token", token);
|
|
2372
|
+
}
|
|
2373
|
+
_url.searchParams.set("id", id);
|
|
2374
|
+
if (!token && !username) {
|
|
2375
|
+
console.error(`[remote-app] 不存在用户名和token ${id}. 权限认证会失败。`);
|
|
2376
|
+
}
|
|
2377
|
+
this.url = _url.toString();
|
|
2378
|
+
this.id = id;
|
|
2379
|
+
this.autoReconnect = opts?.autoReconnect ?? true;
|
|
2380
|
+
this.maxReconnectAttempts = opts?.maxReconnectAttempts ?? Infinity;
|
|
2381
|
+
this.reconnectDelay = opts?.reconnectDelay ?? 1000;
|
|
2382
|
+
this.maxReconnectDelay = opts?.maxReconnectDelay ?? 30000;
|
|
2383
|
+
this.enableBackoff = opts?.enableBackoff ?? true;
|
|
2384
|
+
this.init();
|
|
2385
|
+
}
|
|
2386
|
+
async isConnect() {
|
|
2387
|
+
const that = this;
|
|
2388
|
+
if (this.isConnected) {
|
|
2389
|
+
return true;
|
|
2390
|
+
}
|
|
2391
|
+
if (this.reconnectTimer !== null) {
|
|
2392
|
+
console.log(`远程应用 ${this.id} 正在重连中...`);
|
|
2393
|
+
}
|
|
2394
|
+
return new Promise((resolve) => {
|
|
2395
|
+
const timeout = setTimeout(() => {
|
|
2396
|
+
resolve(false);
|
|
2397
|
+
that.emitter.off("open", listenOnce);
|
|
2398
|
+
}, 5000);
|
|
2399
|
+
const listenOnce = () => {
|
|
2400
|
+
clearTimeout(timeout);
|
|
2401
|
+
that.isConnected = true;
|
|
2402
|
+
that.remoteIsConnected = true;
|
|
2403
|
+
resolve(true);
|
|
2404
|
+
};
|
|
2405
|
+
that.emitter.once("open", listenOnce);
|
|
2406
|
+
});
|
|
2407
|
+
}
|
|
2408
|
+
async waitVerify() {
|
|
2409
|
+
if (this.isVerified) {
|
|
2410
|
+
return true;
|
|
2411
|
+
}
|
|
2412
|
+
return new Promise((resolve) => {
|
|
2413
|
+
const listenOnce = () => {
|
|
2414
|
+
this.isVerified = true;
|
|
2415
|
+
resolve(true);
|
|
2416
|
+
};
|
|
2417
|
+
this.emitter.once("verified", listenOnce);
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
getWsURL(url) {
|
|
2421
|
+
const { protocol } = new URL(url);
|
|
2422
|
+
if (protocol.startsWith("ws")) {
|
|
2423
|
+
return url.toString();
|
|
2424
|
+
}
|
|
2425
|
+
const wsProtocol = protocol === "https:" ? "wss:" : "ws:";
|
|
2426
|
+
const wsURL = url.toString().replace(protocol, wsProtocol);
|
|
2427
|
+
return wsURL;
|
|
2428
|
+
}
|
|
2429
|
+
async init() {
|
|
2430
|
+
if (this.isInitializing) {
|
|
2431
|
+
return;
|
|
2432
|
+
}
|
|
2433
|
+
this.isInitializing = true;
|
|
2434
|
+
const currentInitId = ++this.initId;
|
|
2435
|
+
if (!this.url) {
|
|
2436
|
+
this.isInitializing = false;
|
|
2437
|
+
throw new Error("No url provided for remote app");
|
|
2438
|
+
}
|
|
2439
|
+
if (!this.id) {
|
|
2440
|
+
this.isInitializing = false;
|
|
2441
|
+
throw new Error("No id provided for remote app");
|
|
2442
|
+
}
|
|
2443
|
+
this.isError = false;
|
|
2444
|
+
if (this.ws) {
|
|
2445
|
+
this.ws.close();
|
|
2446
|
+
}
|
|
2447
|
+
const ws = new WebSocket(this.getWsURL(this.url));
|
|
2448
|
+
const that = this;
|
|
2449
|
+
ws.onopen = function() {
|
|
2450
|
+
if (currentInitId !== that.initId) {
|
|
2451
|
+
ws.close();
|
|
2452
|
+
return;
|
|
2453
|
+
}
|
|
2454
|
+
that.isConnected = true;
|
|
2455
|
+
that.isInitializing = false;
|
|
2456
|
+
that.onOpen();
|
|
2457
|
+
console.log("[remote-app] WebSocket connection opened");
|
|
2458
|
+
};
|
|
2459
|
+
ws.onclose = function() {
|
|
2460
|
+
if (currentInitId !== that.initId) {
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
that.isInitializing = false;
|
|
2464
|
+
that.isConnected = false;
|
|
2465
|
+
that.onClose();
|
|
2466
|
+
};
|
|
2467
|
+
ws.onmessage = function(event) {
|
|
2468
|
+
that.onMessage(event.data);
|
|
2469
|
+
};
|
|
2470
|
+
ws.onerror = function(error) {
|
|
2471
|
+
if (currentInitId !== that.initId) {
|
|
2472
|
+
return;
|
|
2473
|
+
}
|
|
2474
|
+
that.isInitializing = false;
|
|
2475
|
+
that.onError(error);
|
|
2476
|
+
};
|
|
2477
|
+
this.ws = ws;
|
|
2478
|
+
}
|
|
2479
|
+
onOpen() {
|
|
2480
|
+
this.isError = false;
|
|
2481
|
+
this.reconnectAttempts = 0;
|
|
2482
|
+
if (this.reconnectTimer) {
|
|
2483
|
+
clearTimeout(this.reconnectTimer);
|
|
2484
|
+
this.reconnectTimer = null;
|
|
2485
|
+
}
|
|
2486
|
+
this.emitter.emit("open", this.id);
|
|
2487
|
+
}
|
|
2488
|
+
onClose() {
|
|
2489
|
+
console.log("远程应用关闭:", this.id);
|
|
2490
|
+
this.isConnected = false;
|
|
2491
|
+
this.emitter.emit("close", this.id);
|
|
2492
|
+
if (this.autoReconnect && !this.isManuallyClosed) {
|
|
2493
|
+
this.scheduleReconnect();
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
calculateReconnectDelay() {
|
|
2497
|
+
if (!this.enableBackoff) {
|
|
2498
|
+
return this.reconnectDelay;
|
|
2499
|
+
}
|
|
2500
|
+
const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts);
|
|
2501
|
+
return Math.min(delay, this.maxReconnectDelay);
|
|
2502
|
+
}
|
|
2503
|
+
scheduleReconnect() {
|
|
2504
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
2505
|
+
console.error(`远程应用 ${this.id} 已达到最大重连次数 ${this.maxReconnectAttempts},停止重连`);
|
|
2506
|
+
this.emitter.emit("maxReconnectAttemptsReached", this.id);
|
|
2507
|
+
return;
|
|
2508
|
+
}
|
|
2509
|
+
if (this.reconnectTimer) {
|
|
2510
|
+
clearTimeout(this.reconnectTimer);
|
|
2511
|
+
}
|
|
2512
|
+
const delay = this.calculateReconnectDelay();
|
|
2513
|
+
this.reconnectAttempts++;
|
|
2514
|
+
console.log(`远程应用 ${this.id} 将在 ${delay}ms 后尝试第 ${this.reconnectAttempts} 次重连`);
|
|
2515
|
+
this.reconnectTimer = setTimeout(() => {
|
|
2516
|
+
this.reconnectTimer = null;
|
|
2517
|
+
try {
|
|
2518
|
+
this.init();
|
|
2519
|
+
} catch (error) {
|
|
2520
|
+
console.error(`远程应用 ${this.id} 重连失败:`, error);
|
|
2521
|
+
this.emitter.emit("reconnectFailed", { id: this.id, attempt: this.reconnectAttempts, error });
|
|
2522
|
+
this.scheduleReconnect();
|
|
2523
|
+
}
|
|
2524
|
+
}, delay);
|
|
2525
|
+
}
|
|
2526
|
+
disconnect() {
|
|
2527
|
+
this.isManuallyClosed = true;
|
|
2528
|
+
this.autoReconnect = false;
|
|
2529
|
+
if (this.reconnectTimer) {
|
|
2530
|
+
clearTimeout(this.reconnectTimer);
|
|
2531
|
+
this.reconnectTimer = null;
|
|
2532
|
+
}
|
|
2533
|
+
if (this.ws) {
|
|
2534
|
+
this.ws.close();
|
|
2535
|
+
}
|
|
2536
|
+
}
|
|
2537
|
+
reconnect() {
|
|
2538
|
+
this.isManuallyClosed = false;
|
|
2539
|
+
this.reconnectAttempts = 0;
|
|
2540
|
+
if (this.reconnectTimer) {
|
|
2541
|
+
clearTimeout(this.reconnectTimer);
|
|
2542
|
+
this.reconnectTimer = null;
|
|
2543
|
+
}
|
|
2544
|
+
this.init();
|
|
2545
|
+
}
|
|
2546
|
+
onMessage(data) {
|
|
2547
|
+
this.emitter.emit("message", data);
|
|
2548
|
+
}
|
|
2549
|
+
onError(error) {
|
|
2550
|
+
console.error(`[remote-app] 远程应用错误: ${this.id}`, error);
|
|
2551
|
+
this.isError = true;
|
|
2552
|
+
this.emitter.emit("error", error);
|
|
2553
|
+
}
|
|
2554
|
+
on(event, listener) {
|
|
2555
|
+
this.emitter.on(event, listener);
|
|
2556
|
+
return () => {
|
|
2557
|
+
this.emitter.off(event, listener);
|
|
2558
|
+
};
|
|
2559
|
+
}
|
|
2560
|
+
json(data) {
|
|
2561
|
+
this.ws.send(JSON.stringify(data));
|
|
2562
|
+
}
|
|
2563
|
+
listenProxy() {
|
|
2564
|
+
const remoteApp = this;
|
|
2565
|
+
const app = this.mainApp;
|
|
2566
|
+
const username = this.username;
|
|
2567
|
+
const listenFn = async (event) => {
|
|
2568
|
+
try {
|
|
2569
|
+
const data = event.toString();
|
|
2570
|
+
const body = JSON.parse(data);
|
|
2571
|
+
const bodyData = body?.data;
|
|
2572
|
+
const message = bodyData?.message || {};
|
|
2573
|
+
const context = bodyData?.context || {};
|
|
2574
|
+
console.log("[remote-app] 远程应用收到消息:", body);
|
|
2575
|
+
if (body?.code === 401) {
|
|
2576
|
+
console.error("[remote-app] 远程应用认证失败,请检查 token 是否正确");
|
|
2577
|
+
this.isError = true;
|
|
2578
|
+
return;
|
|
2579
|
+
}
|
|
2580
|
+
if (body?.type === "verified") {
|
|
2581
|
+
remoteApp.emitter.emit("verified");
|
|
2582
|
+
return;
|
|
2583
|
+
}
|
|
2584
|
+
if (body?.type !== "proxy")
|
|
2585
|
+
return;
|
|
2586
|
+
if (!body.id) {
|
|
2587
|
+
remoteApp.json({
|
|
2588
|
+
id: body.id,
|
|
2589
|
+
data: {
|
|
2590
|
+
code: 400,
|
|
2591
|
+
message: "id is required"
|
|
2592
|
+
}
|
|
2593
|
+
});
|
|
2594
|
+
return;
|
|
2595
|
+
}
|
|
2596
|
+
const res = await app.run(message, context);
|
|
2597
|
+
remoteApp.json({
|
|
2598
|
+
id: body.id,
|
|
2599
|
+
data: {
|
|
2600
|
+
code: res.code,
|
|
2601
|
+
data: res.data,
|
|
2602
|
+
message: res.message
|
|
2603
|
+
}
|
|
2604
|
+
});
|
|
2605
|
+
} catch (error) {
|
|
2606
|
+
console.error("[remote-app] 处理远程代理请求出错:", error);
|
|
2607
|
+
}
|
|
2608
|
+
};
|
|
2609
|
+
remoteApp.json({
|
|
2610
|
+
id: this.id,
|
|
2611
|
+
type: "registryClient",
|
|
2612
|
+
username
|
|
2613
|
+
});
|
|
2614
|
+
console.log(`[remote-app] 远程应用 ${this.id} (${username}) 已注册到主应用,等待消息...`);
|
|
2615
|
+
remoteApp.emitter.on("message", listenFn);
|
|
2616
|
+
const closeMessage = () => {
|
|
2617
|
+
remoteApp.emitter.off("message", listenFn);
|
|
2618
|
+
};
|
|
2619
|
+
remoteApp.emitter.once("close", () => {
|
|
2620
|
+
closeMessage();
|
|
2621
|
+
});
|
|
2622
|
+
return () => {
|
|
2623
|
+
closeMessage();
|
|
2624
|
+
};
|
|
2625
|
+
}
|
|
2626
|
+
}
|
|
2627
|
+
|
|
2130
2628
|
// src/commander.ts
|
|
2131
2629
|
var groupByPath = (routes) => {
|
|
2132
2630
|
return routes.reduce((acc, route) => {
|
|
@@ -2148,7 +2646,14 @@ var parseArgs = (args) => {
|
|
|
2148
2646
|
for (const pair of pairs) {
|
|
2149
2647
|
const idx = pair.indexOf("=");
|
|
2150
2648
|
const key = pair.slice(0, idx);
|
|
2151
|
-
const
|
|
2649
|
+
const raw = pair.slice(idx + 1);
|
|
2650
|
+
let value = raw;
|
|
2651
|
+
if (raw === "true")
|
|
2652
|
+
value = true;
|
|
2653
|
+
else if (raw === "false")
|
|
2654
|
+
value = false;
|
|
2655
|
+
else if (raw !== "" && !isNaN(Number(raw)))
|
|
2656
|
+
value = Number(raw);
|
|
2152
2657
|
result[key] = value;
|
|
2153
2658
|
}
|
|
2154
2659
|
return result;
|
|
@@ -2189,7 +2694,8 @@ var parseDescription = (route) => {
|
|
|
2189
2694
|
return desc;
|
|
2190
2695
|
};
|
|
2191
2696
|
var createCommand2 = (opts) => {
|
|
2192
|
-
const {
|
|
2697
|
+
const { program: program2 } = opts;
|
|
2698
|
+
const app = opts.app;
|
|
2193
2699
|
const routes = app.routes;
|
|
2194
2700
|
const groupRoutes = groupByPath(routes);
|
|
2195
2701
|
for (const path in groupRoutes) {
|
|
@@ -2200,7 +2706,7 @@ var createCommand2 = (opts) => {
|
|
|
2200
2706
|
if (!route.key)
|
|
2201
2707
|
return;
|
|
2202
2708
|
const description = parseDescription(route);
|
|
2203
|
-
subProgram.command(route.key).description(description || "").option("--args <args>", "JSON
|
|
2709
|
+
subProgram.command(route.key).description(description || "").option("--args <args>", `命令参数,支持 JSON 格式或 key=value 形式,例如: --args '{"a":1}' 或 --args 'a=1 b=2'`).argument("[args...]", `位置参数(推荐通过 -- 分隔传入),支持 JSON 或 key=value 格式,例如: -- a=1 b=2 或 -- '{"a":1}'`).action(async (passedArgs, options, _command) => {
|
|
2204
2710
|
const output = (data) => {
|
|
2205
2711
|
if (typeof data === "object") {
|
|
2206
2712
|
process.stdout.write(JSON.stringify(data, null, 2) + `
|
|
@@ -2211,7 +2717,12 @@ var createCommand2 = (opts) => {
|
|
|
2211
2717
|
}
|
|
2212
2718
|
};
|
|
2213
2719
|
try {
|
|
2214
|
-
|
|
2720
|
+
let args = {};
|
|
2721
|
+
if (options.args) {
|
|
2722
|
+
args = parseArgs(options.args);
|
|
2723
|
+
} else if (passedArgs.length > 0) {
|
|
2724
|
+
args = parseArgs(passedArgs.join(" "));
|
|
2725
|
+
}
|
|
2215
2726
|
const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId });
|
|
2216
2727
|
if (res.code === 200) {
|
|
2217
2728
|
output(res.data);
|
|
@@ -2225,11 +2736,47 @@ var createCommand2 = (opts) => {
|
|
|
2225
2736
|
});
|
|
2226
2737
|
}
|
|
2227
2738
|
};
|
|
2228
|
-
var parse = (opts) => {
|
|
2229
|
-
const {
|
|
2739
|
+
var parse = async (opts) => {
|
|
2740
|
+
const { description, parse: parse2 = true, version } = opts;
|
|
2741
|
+
const app = opts.app;
|
|
2230
2742
|
const _program = opts.program || program;
|
|
2231
2743
|
_program.description(description || "Router 命令行工具");
|
|
2744
|
+
if (version) {
|
|
2745
|
+
_program.version(version);
|
|
2746
|
+
}
|
|
2747
|
+
app.createRouteList();
|
|
2748
|
+
app.route({
|
|
2749
|
+
path: "cli",
|
|
2750
|
+
key: "list"
|
|
2751
|
+
}).define(async () => {
|
|
2752
|
+
const routes = app.routes.map((route) => {
|
|
2753
|
+
return {
|
|
2754
|
+
path: route.path,
|
|
2755
|
+
key: route.key,
|
|
2756
|
+
description: route?.metadata?.summary || route.description || ""
|
|
2757
|
+
};
|
|
2758
|
+
});
|
|
2759
|
+
const table = routes.map((route) => {
|
|
2760
|
+
return `${route.path} ${route.key} - ${route.description}`;
|
|
2761
|
+
}).join(`
|
|
2762
|
+
`);
|
|
2763
|
+
console.log(table);
|
|
2764
|
+
}).addTo(app, { overwrite: false });
|
|
2232
2765
|
createCommand2({ app, program: _program });
|
|
2766
|
+
if (opts.remote) {
|
|
2767
|
+
const { token, username, id } = opts.remote;
|
|
2768
|
+
const remoteApp = new RemoteApp({
|
|
2769
|
+
token,
|
|
2770
|
+
username,
|
|
2771
|
+
id
|
|
2772
|
+
});
|
|
2773
|
+
const isConnect = await remoteApp.isConnect();
|
|
2774
|
+
if (isConnect) {
|
|
2775
|
+
remoteApp.listenProxy();
|
|
2776
|
+
console.log("已连接到远程应用,正在监听命令...");
|
|
2777
|
+
}
|
|
2778
|
+
return;
|
|
2779
|
+
}
|
|
2233
2780
|
if (parse2) {
|
|
2234
2781
|
_program.parse(process.argv);
|
|
2235
2782
|
}
|
package/dist/opencode.d.ts
CHANGED
|
@@ -147,14 +147,6 @@ type RouteOpts<U = {}, T = SimpleObject> = {
|
|
|
147
147
|
metadata?: T;
|
|
148
148
|
middleware?: RouteMiddleware[];
|
|
149
149
|
type?: 'route' | 'middleware' | 'compound';
|
|
150
|
-
/**
|
|
151
|
-
* $#$ will be used to split path and key
|
|
152
|
-
*/
|
|
153
|
-
idUsePath?: boolean;
|
|
154
|
-
/**
|
|
155
|
-
* id 合并的分隔符,默认为 $#$
|
|
156
|
-
*/
|
|
157
|
-
delimiter?: string;
|
|
158
150
|
isDebug?: boolean;
|
|
159
151
|
};
|
|
160
152
|
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
|