devframe 0.2.2 → 0.2.3
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/adapters/build.d.mts +1 -1
- package/dist/adapters/build.mjs +6 -6
- package/dist/adapters/cli.d.mts +1 -1
- package/dist/adapters/cli.mjs +2 -2
- package/dist/adapters/dev.d.mts +2 -2
- package/dist/adapters/dev.mjs +1 -1
- package/dist/adapters/embedded.d.mts +1 -1
- package/dist/adapters/mcp.d.mts +1 -1
- package/dist/adapters/mcp.mjs +3 -3
- package/dist/client/index.d.mts +18 -4
- package/dist/client/index.mjs +314 -8
- package/dist/{context-BDpuScNZ.d.mts → context-GUzIxrEK.d.mts} +1 -1
- package/dist/{dev-CZ9ZQkuO.mjs → dev-BVtS7qBK.mjs} +4 -4
- package/dist/{devframe-DAfZWnpI.d.mts → devframe-C4DNeFuG.d.mts} +3 -52
- package/dist/diagnostics-D7miaTpL.mjs +36 -0
- package/dist/{rpc-BlVCyk9W.mjs → dump-C5LPsKJC.mjs} +139 -126
- package/dist/helpers/vite.d.mts +1 -1
- package/dist/helpers/vite.mjs +3 -3
- package/dist/{host-h3-DEfOpa0j.mjs → host-h3-DYFKJxAV.mjs} +255 -7
- package/dist/human-id-CiBWzVcg.mjs +803 -0
- package/dist/{index-BNAsgVNz.d.mts → index-CSi2Y5JU.d.mts} +31 -78
- package/dist/index-DWczSqdd.d.mts +79 -0
- package/dist/index-aNunf5Za.d.mts +639 -0
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/node/auth.d.mts +2 -2
- package/dist/node/auth.mjs +1 -2
- package/dist/node/index.d.mts +5 -5
- package/dist/node/index.mjs +3 -3
- package/dist/node/internal.d.mts +2 -2
- package/dist/node/internal.mjs +3 -4
- package/dist/recipes/open-helpers.d.mts +1 -1
- package/dist/recipes/open-helpers.mjs +2 -2
- package/dist/rpc/dump.d.mts +2 -0
- package/dist/rpc/dump.mjs +2 -0
- package/dist/rpc/index.d.mts +2 -2
- package/dist/rpc/index.mjs +129 -3
- package/dist/rpc/transports/ws-client.d.mts +25 -1
- package/dist/rpc/transports/ws-client.mjs +2 -2
- package/dist/rpc/transports/ws-server.d.mts +1 -1
- package/dist/rpc/transports/ws-server.mjs +2 -2
- package/dist/{serialization-CwO31axa.mjs → serialization-lBLV02kQ.mjs} +2 -36
- package/dist/{server-BGSrxsQb.d.mts → server-DjMtJQhO.d.mts} +1 -1
- package/dist/shared-state-SzgfilJP.mjs +1058 -0
- package/dist/{shared-state-BJXkPxtf.mjs → shared-state-u0y123fi.mjs} +6 -6
- package/dist/{storage-D-jLaxDA.mjs → storage-CTlHUtXI.mjs} +1 -2
- package/dist/structured-clone-qP1sPovr.mjs +208 -0
- package/dist/types/index.d.mts +3 -3
- package/dist/utils/events.d.mts +1 -1
- package/dist/utils/hash.mjs +1 -1
- package/dist/utils/open.mjs +1 -1
- package/dist/utils/shared-state.d.mts +1 -1
- package/dist/utils/shared-state.mjs +1 -1
- package/dist/utils/streaming-channel.d.mts +1 -1
- package/dist/utils/structured-clone.mjs +1 -1
- package/dist/{ws-server-DKlqI8XP.d.mts → ws-server-lHQaafrS.d.mts} +1 -1
- package/package.json +2 -1
- package/dist/diagnostics-DNxVjo6X.mjs +0 -56
- package/dist/revoke-DDa7VSJa.mjs +0 -35
- package/dist/ws-client-CrLsflnY.d.mts +0 -26
- /package/dist/{_shared-CUFqO4kJ.mjs → _shared-BNoPn1NG.mjs} +0 -0
- /package/dist/{define-CW9gLnyG.mjs → define-Bb4zh-Dc.mjs} +0 -0
- /package/dist/{hash-CIBte1yS.mjs → hash-bwOD8RgU.mjs} +0 -0
- /package/dist/{open-DiQn6zCH.mjs → open-CX4CiMyw.mjs} +0 -0
- /package/dist/{server-BO8IDVsJ.mjs → server-DxiOFSpu.mjs} +0 -0
- /package/dist/{structured-clone-CD2l4fI3.mjs → structured-clone-PdCZwt7F.mjs} +0 -0
- /package/dist/{transports-DTFoMUbE.mjs → transports-Bl1zuYhZ.mjs} +0 -0
- /package/dist/{types-DmcujDa5.d.mts → types-kKDaD2NT.d.mts} +0 -0
|
@@ -1,133 +1,128 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { i as logger } from "./serialization-CwO31axa.mjs";
|
|
1
|
+
import { n as logger } from "./diagnostics-D7miaTpL.mjs";
|
|
3
2
|
import { DEVTOOLS_RPC_DUMP_DIRNAME } from "./constants.mjs";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
//#region ../../node_modules/.pnpm/ohash@2.0.11/node_modules/ohash/dist/shared/ohash.D__AXeF1.mjs
|
|
5
|
+
function serialize(o) {
|
|
6
|
+
return typeof o == "string" ? `'${o}'` : new c().serialize(o);
|
|
7
|
+
}
|
|
8
|
+
const c = /* @__PURE__ */ function() {
|
|
9
|
+
class o {
|
|
10
|
+
#t = /* @__PURE__ */ new Map();
|
|
11
|
+
compare(t, r) {
|
|
12
|
+
const e = typeof t, n = typeof r;
|
|
13
|
+
return e === "string" && n === "string" ? t.localeCompare(r) : e === "number" && n === "number" ? t - r : String.prototype.localeCompare.call(this.serialize(t, true), this.serialize(r, true));
|
|
14
|
+
}
|
|
15
|
+
serialize(t, r) {
|
|
16
|
+
if (t === null) return "null";
|
|
17
|
+
switch (typeof t) {
|
|
18
|
+
case "string": return r ? t : `'${t}'`;
|
|
19
|
+
case "bigint": return `${t}n`;
|
|
20
|
+
case "object": return this.$object(t);
|
|
21
|
+
case "function": return this.$function(t);
|
|
22
|
+
}
|
|
23
|
+
return String(t);
|
|
24
|
+
}
|
|
25
|
+
serializeObject(t) {
|
|
26
|
+
const r = Object.prototype.toString.call(t);
|
|
27
|
+
if (r !== "[object Object]") return this.serializeBuiltInType(r.length < 10 ? `unknown:${r}` : r.slice(8, -1), t);
|
|
28
|
+
const e = t.constructor, n = e === Object || e === void 0 ? "" : e.name;
|
|
29
|
+
if (n !== "" && globalThis[n] === e) return this.serializeBuiltInType(n, t);
|
|
30
|
+
if (typeof t.toJSON == "function") {
|
|
31
|
+
const i = t.toJSON();
|
|
32
|
+
return n + (i !== null && typeof i == "object" ? this.$object(i) : `(${this.serialize(i)})`);
|
|
33
|
+
}
|
|
34
|
+
return this.serializeObjectEntries(n, Object.entries(t));
|
|
35
|
+
}
|
|
36
|
+
serializeBuiltInType(t, r) {
|
|
37
|
+
const e = this["$" + t];
|
|
38
|
+
if (e) return e.call(this, r);
|
|
39
|
+
if (typeof r?.entries == "function") return this.serializeObjectEntries(t, r.entries());
|
|
40
|
+
throw new Error(`Cannot serialize ${t}`);
|
|
41
|
+
}
|
|
42
|
+
serializeObjectEntries(t, r) {
|
|
43
|
+
const e = Array.from(r).sort((i, a) => this.compare(i[0], a[0]));
|
|
44
|
+
let n = `${t}{`;
|
|
45
|
+
for (let i = 0; i < e.length; i++) {
|
|
46
|
+
const [a, l] = e[i];
|
|
47
|
+
n += `${this.serialize(a, true)}:${this.serialize(l)}`, i < e.length - 1 && (n += ",");
|
|
48
|
+
}
|
|
49
|
+
return n + "}";
|
|
50
|
+
}
|
|
51
|
+
$object(t) {
|
|
52
|
+
let r = this.#t.get(t);
|
|
53
|
+
return r === void 0 && (this.#t.set(t, `#${this.#t.size}`), r = this.serializeObject(t), this.#t.set(t, r)), r;
|
|
54
|
+
}
|
|
55
|
+
$function(t) {
|
|
56
|
+
const r = Function.prototype.toString.call(t);
|
|
57
|
+
return r.slice(-15) === "[native code] }" ? `${t.name || ""}()[native]` : `${t.name}(${t.length})${r.replace(/\s*\n\s*/g, "")}`;
|
|
58
|
+
}
|
|
59
|
+
$Array(t) {
|
|
60
|
+
let r = "[";
|
|
61
|
+
for (let e = 0; e < t.length; e++) r += this.serialize(t[e]), e < t.length - 1 && (r += ",");
|
|
62
|
+
return r + "]";
|
|
63
|
+
}
|
|
64
|
+
$Date(t) {
|
|
65
|
+
try {
|
|
66
|
+
return `Date(${t.toISOString()})`;
|
|
67
|
+
} catch {
|
|
68
|
+
return "Date(null)";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
$ArrayBuffer(t) {
|
|
72
|
+
return `ArrayBuffer[${new Uint8Array(t).join(",")}]`;
|
|
73
|
+
}
|
|
74
|
+
$Set(t) {
|
|
75
|
+
return `Set${this.$Array(Array.from(t).sort((r, e) => this.compare(r, e)))}`;
|
|
76
|
+
}
|
|
77
|
+
$Map(t) {
|
|
78
|
+
return this.serializeObjectEntries("Map", t.entries());
|
|
79
|
+
}
|
|
37
80
|
}
|
|
38
|
-
|
|
81
|
+
for (const s of [
|
|
82
|
+
"Error",
|
|
83
|
+
"RegExp",
|
|
84
|
+
"URL"
|
|
85
|
+
]) o.prototype["$" + s] = function(t) {
|
|
86
|
+
return `${s}(${t})`;
|
|
87
|
+
};
|
|
88
|
+
for (const s of [
|
|
89
|
+
"Int8Array",
|
|
90
|
+
"Uint8Array",
|
|
91
|
+
"Uint8ClampedArray",
|
|
92
|
+
"Int16Array",
|
|
93
|
+
"Uint16Array",
|
|
94
|
+
"Int32Array",
|
|
95
|
+
"Uint32Array",
|
|
96
|
+
"Float32Array",
|
|
97
|
+
"Float64Array"
|
|
98
|
+
]) o.prototype["$" + s] = function(t) {
|
|
99
|
+
return `${s}[${t.join(",")}]`;
|
|
100
|
+
};
|
|
101
|
+
for (const s of ["BigInt64Array", "BigUint64Array"]) o.prototype["$" + s] = function(t) {
|
|
102
|
+
return `${s}[${t.join("n,")}${t.length > 0 ? "n" : ""}]`;
|
|
103
|
+
};
|
|
104
|
+
return o;
|
|
105
|
+
}();
|
|
39
106
|
//#endregion
|
|
40
|
-
//#region
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
definition.__promise = void 0;
|
|
47
|
-
return r;
|
|
48
|
-
});
|
|
49
|
-
return definition.__resolved ??= await definition.__promise;
|
|
107
|
+
//#region ../../node_modules/.pnpm/ohash@2.0.11/node_modules/ohash/dist/crypto/node/index.mjs
|
|
108
|
+
const e = globalThis.process?.getBuiltinModule?.("crypto")?.hash, r = "sha256", s = "base64url";
|
|
109
|
+
function digest(t) {
|
|
110
|
+
if (e) return e(r, t, s);
|
|
111
|
+
const o = createHash(r).update(t);
|
|
112
|
+
return globalThis.process?.versions?.webcontainer ? o.digest().toString(s) : o.digest(s);
|
|
50
113
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return result.handler;
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region ../../node_modules/.pnpm/ohash@2.0.11/node_modules/ohash/dist/index.mjs
|
|
116
|
+
function hash$1(input) {
|
|
117
|
+
return digest(serialize(input));
|
|
56
118
|
}
|
|
57
119
|
//#endregion
|
|
58
|
-
//#region src/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
constructor(context) {
|
|
65
|
-
this.context = context;
|
|
66
|
-
const definitions = this.definitions;
|
|
67
|
-
const self = this;
|
|
68
|
-
this.functions = new Proxy({}, {
|
|
69
|
-
get(_, prop) {
|
|
70
|
-
const definition = definitions.get(prop);
|
|
71
|
-
if (!definition) return void 0;
|
|
72
|
-
return getRpcHandler(definition, self.context);
|
|
73
|
-
},
|
|
74
|
-
has(_, prop) {
|
|
75
|
-
return definitions.has(prop);
|
|
76
|
-
},
|
|
77
|
-
getOwnPropertyDescriptor(_, prop) {
|
|
78
|
-
return {
|
|
79
|
-
value: definitions.get(prop)?.handler,
|
|
80
|
-
configurable: true,
|
|
81
|
-
enumerable: true
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
ownKeys() {
|
|
85
|
-
return Array.from(definitions.keys());
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
register(fn, force = false) {
|
|
90
|
-
if (this.definitions.has(fn.name) && !force) throw logger.DF0021({ name: fn.name }).throw();
|
|
91
|
-
assertAgentJsonSerializable(fn);
|
|
92
|
-
this.definitions.set(fn.name, fn);
|
|
93
|
-
this._onChanged.forEach((cb) => cb(fn.name));
|
|
94
|
-
}
|
|
95
|
-
update(fn, force = false) {
|
|
96
|
-
if (!this.definitions.has(fn.name) && !force) throw logger.DF0022({ name: fn.name }).throw();
|
|
97
|
-
assertAgentJsonSerializable(fn);
|
|
98
|
-
this.definitions.set(fn.name, fn);
|
|
99
|
-
this._onChanged.forEach((cb) => cb(fn.name));
|
|
100
|
-
}
|
|
101
|
-
onChanged(fn) {
|
|
102
|
-
this._onChanged.push(fn);
|
|
103
|
-
return () => {
|
|
104
|
-
const index = this._onChanged.indexOf(fn);
|
|
105
|
-
if (index !== -1) this._onChanged.splice(index, 1);
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
async getHandler(name) {
|
|
109
|
-
return await getRpcHandler(this.definitions.get(name), this.context);
|
|
110
|
-
}
|
|
111
|
-
getSchema(name) {
|
|
112
|
-
const definition = this.definitions.get(name);
|
|
113
|
-
if (!definition) throw logger.DF0023({ name: String(name) }).throw();
|
|
114
|
-
return {
|
|
115
|
-
args: definition.args,
|
|
116
|
-
returns: definition.returns
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
has(name) {
|
|
120
|
-
return this.definitions.has(name);
|
|
121
|
-
}
|
|
122
|
-
get(name) {
|
|
123
|
-
return this.definitions.get(name);
|
|
124
|
-
}
|
|
125
|
-
list() {
|
|
126
|
-
return Array.from(this.definitions.keys());
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
function assertAgentJsonSerializable(fn) {
|
|
130
|
-
if (fn.agent && fn.jsonSerializable !== true) throw logger.DF0019({ name: fn.name }).throw();
|
|
120
|
+
//#region src/utils/hash.ts
|
|
121
|
+
/**
|
|
122
|
+
* Stable, deterministic hash of any structured-cloneable value.
|
|
123
|
+
*/
|
|
124
|
+
function hash(value) {
|
|
125
|
+
return hash$1(value);
|
|
131
126
|
}
|
|
132
127
|
//#endregion
|
|
133
128
|
//#region ../../node_modules/.pnpm/yocto-queue@1.2.2/node_modules/yocto-queue/index.js
|
|
@@ -339,7 +334,7 @@ function isPlainErrorShape(value) {
|
|
|
339
334
|
return typeof value === "object" && value !== null && typeof value.message === "string" && typeof value.name === "string";
|
|
340
335
|
}
|
|
341
336
|
//#endregion
|
|
342
|
-
//#region src/rpc/dump/
|
|
337
|
+
//#region src/rpc/dump/collect.ts
|
|
343
338
|
function getDumpRecordKey(functionName, args) {
|
|
344
339
|
return `${functionName}---${hash(args)}`;
|
|
345
340
|
}
|
|
@@ -501,6 +496,24 @@ function getDefinitionsWithDumps(definitions) {
|
|
|
501
496
|
return definitions.filter((def) => def.dump !== void 0);
|
|
502
497
|
}
|
|
503
498
|
//#endregion
|
|
499
|
+
//#region src/rpc/handler.ts
|
|
500
|
+
async function getRpcResolvedSetupResult(definition, context) {
|
|
501
|
+
if (definition.__resolved) return definition.__resolved;
|
|
502
|
+
if (!definition.setup) return {};
|
|
503
|
+
definition.__promise ??= Promise.resolve(definition.setup(context)).then((r) => {
|
|
504
|
+
definition.__resolved = r;
|
|
505
|
+
definition.__promise = void 0;
|
|
506
|
+
return r;
|
|
507
|
+
});
|
|
508
|
+
return definition.__resolved ??= await definition.__promise;
|
|
509
|
+
}
|
|
510
|
+
async function getRpcHandler(definition, context) {
|
|
511
|
+
if (definition.handler) return definition.handler;
|
|
512
|
+
const result = await getRpcResolvedSetupResult(definition, context);
|
|
513
|
+
if (!result.handler) throw logger.DF0024({ name: definition.name }).throw();
|
|
514
|
+
return result.handler;
|
|
515
|
+
}
|
|
516
|
+
//#endregion
|
|
504
517
|
//#region src/rpc/dump/static.ts
|
|
505
518
|
function makeDumpKey(name) {
|
|
506
519
|
return encodeURIComponent(name.replaceAll(":", "~"));
|
|
@@ -578,4 +591,4 @@ async function collectStaticRpcDump(definitions, context) {
|
|
|
578
591
|
};
|
|
579
592
|
}
|
|
580
593
|
//#endregion
|
|
581
|
-
export {
|
|
594
|
+
export { dumpFunctions as a, serializeDumpError as c, hash as d, createClientFromDump as i, validateDefinition as l, getRpcHandler as n, getDefinitionsWithDumps as o, getRpcResolvedSetupResult as r, reviveDumpError as s, collectStaticRpcDump as t, validateDefinitions as u };
|
package/dist/helpers/vite.d.mts
CHANGED
package/dist/helpers/vite.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { DEVTOOLS_CONNECTION_META_FILENAME } from "../constants.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import { n as resolveBasePath } from "../_shared-
|
|
2
|
+
import { a as logger } from "../shared-state-SzgfilJP.mjs";
|
|
3
|
+
import { n as resolveBasePath } from "../_shared-BNoPn1NG.mjs";
|
|
4
4
|
import { serveStaticNodeMiddleware } from "../utils/serve-static.mjs";
|
|
5
|
-
import { n as resolveDevServerPort, t as createDevServer } from "../dev-
|
|
5
|
+
import { n as resolveDevServerPort, t as createDevServer } from "../dev-BVtS7qBK.mjs";
|
|
6
6
|
import { resolve } from "pathe";
|
|
7
7
|
//#region src/helpers/vite.ts
|
|
8
8
|
/**
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { t as diagnostics } from "./diagnostics-D7miaTpL.mjs";
|
|
2
|
+
import { RpcFunctionsCollectorBase } from "./rpc/index.mjs";
|
|
3
3
|
import { defineRpcFunction } from "./index.mjs";
|
|
4
|
-
import {
|
|
5
|
-
import { n as logger, t as diagnostics$1 } from "./diagnostics-DNxVjo6X.mjs";
|
|
6
|
-
import { createEventEmitter } from "./utils/events.mjs";
|
|
7
|
-
import { t as createSharedState } from "./shared-state-BJXkPxtf.mjs";
|
|
8
|
-
import { createStreamReader, createStreamSink } from "./utils/streaming-channel.mjs";
|
|
4
|
+
import { a as logger, i as diagnostics$1, n as nanoid, o as colors$1, r as createEventEmitter, t as createSharedState } from "./shared-state-SzgfilJP.mjs";
|
|
9
5
|
import { consoleReporter, createLogger, defineDiagnostics } from "logs-sdk";
|
|
10
6
|
import { ansiFormatter } from "logs-sdk/formatters/ansi";
|
|
11
7
|
import { isatty } from "node:tty";
|
|
@@ -576,6 +572,258 @@ function createRpcSharedStateServerHost(rpc) {
|
|
|
576
572
|
return host;
|
|
577
573
|
}
|
|
578
574
|
//#endregion
|
|
575
|
+
//#region src/utils/streaming-channel.ts
|
|
576
|
+
const DEFAULT_HIGH_WATER_MARK = 256;
|
|
577
|
+
var StreamClosedError = class extends Error {
|
|
578
|
+
name = "StreamClosedError";
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* Build a server-side stream sink. RPC-agnostic — the RPC host wires
|
|
582
|
+
* `events.on('chunk' | 'end')` to broadcast, and reads `buffer` to replay
|
|
583
|
+
* for late or reconnecting subscribers.
|
|
584
|
+
*/
|
|
585
|
+
function createStreamSink(options = {}) {
|
|
586
|
+
const id = options.id ?? nanoid();
|
|
587
|
+
const replayWindow = Math.max(0, options.replayWindow ?? 0);
|
|
588
|
+
const events = createEventEmitter();
|
|
589
|
+
const controller = new AbortController();
|
|
590
|
+
const buffer = [];
|
|
591
|
+
let closed = false;
|
|
592
|
+
let lastSeq = 0;
|
|
593
|
+
function write(chunk) {
|
|
594
|
+
if (closed) throw new StreamClosedError(`Cannot write to a closed stream "${id}"`);
|
|
595
|
+
lastSeq += 1;
|
|
596
|
+
if (replayWindow > 0) {
|
|
597
|
+
buffer.push({
|
|
598
|
+
seq: lastSeq,
|
|
599
|
+
chunk
|
|
600
|
+
});
|
|
601
|
+
if (buffer.length > replayWindow) buffer.splice(0, buffer.length - replayWindow);
|
|
602
|
+
}
|
|
603
|
+
events.emit("chunk", lastSeq, chunk);
|
|
604
|
+
}
|
|
605
|
+
function error(reason) {
|
|
606
|
+
if (closed) return;
|
|
607
|
+
closed = true;
|
|
608
|
+
const payload = toErrorPayload(reason);
|
|
609
|
+
controller.abort(reason);
|
|
610
|
+
events.emit("end", payload);
|
|
611
|
+
}
|
|
612
|
+
function close() {
|
|
613
|
+
if (closed) return;
|
|
614
|
+
closed = true;
|
|
615
|
+
if (!controller.signal.aborted) controller.abort("stream closed");
|
|
616
|
+
events.emit("end", void 0);
|
|
617
|
+
}
|
|
618
|
+
function abort(reason) {
|
|
619
|
+
if (closed) return;
|
|
620
|
+
if (!controller.signal.aborted) controller.abort(reason ?? "aborted");
|
|
621
|
+
}
|
|
622
|
+
const writable = new WritableStream({
|
|
623
|
+
write(chunk) {
|
|
624
|
+
write(chunk);
|
|
625
|
+
},
|
|
626
|
+
close() {
|
|
627
|
+
close();
|
|
628
|
+
},
|
|
629
|
+
abort(reason) {
|
|
630
|
+
error(reason);
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
return {
|
|
634
|
+
id,
|
|
635
|
+
signal: controller.signal,
|
|
636
|
+
get closed() {
|
|
637
|
+
return closed;
|
|
638
|
+
},
|
|
639
|
+
get lastSeq() {
|
|
640
|
+
return lastSeq;
|
|
641
|
+
},
|
|
642
|
+
write,
|
|
643
|
+
error,
|
|
644
|
+
close,
|
|
645
|
+
abort,
|
|
646
|
+
writable,
|
|
647
|
+
events,
|
|
648
|
+
buffer
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Build a client-side stream reader. RPC-agnostic — the RPC host calls
|
|
653
|
+
* `_push(seq, chunk)` on each incoming chunk and `_end(error?)` on the
|
|
654
|
+
* terminal frame. Consumers iterate with `for await` or pipe `readable`.
|
|
655
|
+
*/
|
|
656
|
+
function createStreamReader(options = {}) {
|
|
657
|
+
const id = options.id ?? nanoid();
|
|
658
|
+
const highWaterMark = Math.max(1, options.highWaterMark ?? DEFAULT_HIGH_WATER_MARK);
|
|
659
|
+
const queue = [];
|
|
660
|
+
let lastSeenSeq = 0;
|
|
661
|
+
let done = false;
|
|
662
|
+
let cancelled = false;
|
|
663
|
+
let endError;
|
|
664
|
+
let pending;
|
|
665
|
+
let pullController;
|
|
666
|
+
let readableInstance;
|
|
667
|
+
function drainNext() {
|
|
668
|
+
if (!pending) return;
|
|
669
|
+
if (queue.length > 0) {
|
|
670
|
+
const value = queue.shift();
|
|
671
|
+
const r = pending;
|
|
672
|
+
pending = void 0;
|
|
673
|
+
r.resolve({
|
|
674
|
+
value,
|
|
675
|
+
done: false
|
|
676
|
+
});
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
if (done) {
|
|
680
|
+
const r = pending;
|
|
681
|
+
pending = void 0;
|
|
682
|
+
if (endError) {
|
|
683
|
+
const err = new Error(endError.message);
|
|
684
|
+
err.name = endError.name;
|
|
685
|
+
r.reject(err);
|
|
686
|
+
} else r.resolve({
|
|
687
|
+
value: void 0,
|
|
688
|
+
done: true
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
function feedReadable() {
|
|
693
|
+
if (!pullController) return;
|
|
694
|
+
while (queue.length > 0) {
|
|
695
|
+
const v = queue.shift();
|
|
696
|
+
try {
|
|
697
|
+
pullController.enqueue(v);
|
|
698
|
+
} catch {
|
|
699
|
+
break;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
if (done && pullController) {
|
|
703
|
+
try {
|
|
704
|
+
if (endError) {
|
|
705
|
+
const err = new Error(endError.message);
|
|
706
|
+
err.name = endError.name;
|
|
707
|
+
pullController.error(err);
|
|
708
|
+
} else pullController.close();
|
|
709
|
+
} catch {}
|
|
710
|
+
pullController = void 0;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
function push(seq, chunk) {
|
|
714
|
+
if (done || cancelled) return;
|
|
715
|
+
if (seq <= lastSeenSeq) return;
|
|
716
|
+
lastSeenSeq = seq;
|
|
717
|
+
queue.push(chunk);
|
|
718
|
+
if (queue.length > highWaterMark) {
|
|
719
|
+
const overflow = queue.length - highWaterMark;
|
|
720
|
+
queue.splice(0, overflow);
|
|
721
|
+
options.onOverflow?.(overflow);
|
|
722
|
+
}
|
|
723
|
+
drainNext();
|
|
724
|
+
if (readableInstance) feedReadable();
|
|
725
|
+
}
|
|
726
|
+
function end(error) {
|
|
727
|
+
if (done) return;
|
|
728
|
+
done = true;
|
|
729
|
+
endError = error;
|
|
730
|
+
drainNext();
|
|
731
|
+
if (readableInstance) feedReadable();
|
|
732
|
+
}
|
|
733
|
+
function cancel() {
|
|
734
|
+
if (cancelled || done) return;
|
|
735
|
+
cancelled = true;
|
|
736
|
+
options.onCancel?.();
|
|
737
|
+
end(void 0);
|
|
738
|
+
}
|
|
739
|
+
function getReadable() {
|
|
740
|
+
if (readableInstance) return readableInstance;
|
|
741
|
+
readableInstance = new ReadableStream({
|
|
742
|
+
start(controller) {
|
|
743
|
+
pullController = controller;
|
|
744
|
+
feedReadable();
|
|
745
|
+
},
|
|
746
|
+
cancel() {
|
|
747
|
+
cancel();
|
|
748
|
+
}
|
|
749
|
+
});
|
|
750
|
+
return readableInstance;
|
|
751
|
+
}
|
|
752
|
+
return {
|
|
753
|
+
id,
|
|
754
|
+
get cancelled() {
|
|
755
|
+
return cancelled;
|
|
756
|
+
},
|
|
757
|
+
get done() {
|
|
758
|
+
return done;
|
|
759
|
+
},
|
|
760
|
+
get lastSeenSeq() {
|
|
761
|
+
return lastSeenSeq;
|
|
762
|
+
},
|
|
763
|
+
get readable() {
|
|
764
|
+
return getReadable();
|
|
765
|
+
},
|
|
766
|
+
cancel,
|
|
767
|
+
_push: push,
|
|
768
|
+
_end: end,
|
|
769
|
+
[Symbol.asyncIterator]() {
|
|
770
|
+
return {
|
|
771
|
+
next() {
|
|
772
|
+
if (queue.length > 0) return Promise.resolve({
|
|
773
|
+
value: queue.shift(),
|
|
774
|
+
done: false
|
|
775
|
+
});
|
|
776
|
+
if (done) {
|
|
777
|
+
if (endError) {
|
|
778
|
+
const err = new Error(endError.message);
|
|
779
|
+
err.name = endError.name;
|
|
780
|
+
return Promise.reject(err);
|
|
781
|
+
}
|
|
782
|
+
return Promise.resolve({
|
|
783
|
+
value: void 0,
|
|
784
|
+
done: true
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
return new Promise((resolve, reject) => {
|
|
788
|
+
pending = {
|
|
789
|
+
resolve,
|
|
790
|
+
reject
|
|
791
|
+
};
|
|
792
|
+
});
|
|
793
|
+
},
|
|
794
|
+
return() {
|
|
795
|
+
cancel();
|
|
796
|
+
return Promise.resolve({
|
|
797
|
+
value: void 0,
|
|
798
|
+
done: true
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
function toErrorPayload(reason) {
|
|
806
|
+
if (reason instanceof Error) return {
|
|
807
|
+
name: reason.name || "Error",
|
|
808
|
+
message: reason.message
|
|
809
|
+
};
|
|
810
|
+
if (typeof reason === "string") return {
|
|
811
|
+
name: "Error",
|
|
812
|
+
message: reason
|
|
813
|
+
};
|
|
814
|
+
try {
|
|
815
|
+
return {
|
|
816
|
+
name: "Error",
|
|
817
|
+
message: JSON.stringify(reason)
|
|
818
|
+
};
|
|
819
|
+
} catch {
|
|
820
|
+
return {
|
|
821
|
+
name: "Error",
|
|
822
|
+
message: String(reason)
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
//#endregion
|
|
579
827
|
//#region src/node/rpc-streaming.ts
|
|
580
828
|
const debug = createDebug("vite:devtools:rpc:streaming");
|
|
581
829
|
const STREAM_KEY_SEPARATOR = "";
|