@yuhere/js-jsonrpc2 0.0.0-stage → 2.1.1

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.
Files changed (174) hide show
  1. package/README.md +277 -2
  2. package/SKILL.md +237 -0
  3. package/dist/cli/cli.d.ts +3 -0
  4. package/dist/cli/cli.d.ts.map +1 -0
  5. package/dist/cli/cli.js +132 -0
  6. package/dist/cli/cli.js.map +1 -0
  7. package/dist/cli/demo-methods.d.ts +10 -0
  8. package/dist/cli/demo-methods.d.ts.map +1 -0
  9. package/dist/cli/demo-methods.js +477 -0
  10. package/dist/cli/demo-methods.js.map +1 -0
  11. package/dist/cli/express.d.ts +2 -0
  12. package/dist/cli/express.d.ts.map +1 -0
  13. package/dist/cli/express.js +42 -0
  14. package/dist/cli/express.js.map +1 -0
  15. package/dist/cli/stdio.d.ts +2 -0
  16. package/dist/cli/stdio.d.ts.map +1 -0
  17. package/dist/cli/stdio.js +5 -0
  18. package/dist/cli/stdio.js.map +1 -0
  19. package/dist/core/Repository.d.ts +24 -0
  20. package/dist/core/Repository.d.ts.map +1 -0
  21. package/dist/core/Repository.js +153 -0
  22. package/dist/core/Repository.js.map +1 -0
  23. package/dist/core/convert.d.ts +4 -0
  24. package/dist/core/convert.d.ts.map +1 -0
  25. package/dist/core/convert.js +29 -0
  26. package/dist/core/convert.js.map +1 -0
  27. package/dist/core/descriptor.d.ts +27 -0
  28. package/dist/core/descriptor.d.ts.map +1 -0
  29. package/dist/core/descriptor.js +83 -0
  30. package/dist/core/descriptor.js.map +1 -0
  31. package/dist/core/index.d.ts +4 -0
  32. package/dist/core/index.d.ts.map +1 -0
  33. package/dist/core/index.js +3 -0
  34. package/dist/core/index.js.map +1 -0
  35. package/dist/core/permission.d.ts +4 -0
  36. package/dist/core/permission.d.ts.map +1 -0
  37. package/dist/core/permission.js +18 -0
  38. package/dist/core/permission.js.map +1 -0
  39. package/dist/core/spec-check.d.ts +3 -0
  40. package/dist/core/spec-check.d.ts.map +1 -0
  41. package/dist/core/spec-check.js +11 -0
  42. package/dist/core/spec-check.js.map +1 -0
  43. package/dist/core/system-methods.d.ts +3 -0
  44. package/dist/core/system-methods.d.ts.map +1 -0
  45. package/dist/core/system-methods.js +76 -0
  46. package/dist/core/system-methods.js.map +1 -0
  47. package/dist/errors/RpcError.d.ts +9 -0
  48. package/dist/errors/RpcError.d.ts.map +1 -0
  49. package/dist/errors/RpcError.js +23 -0
  50. package/dist/errors/RpcError.js.map +1 -0
  51. package/dist/errors/codes.d.ts +7 -0
  52. package/dist/errors/codes.d.ts.map +1 -0
  53. package/dist/errors/codes.js +7 -0
  54. package/dist/errors/codes.js.map +1 -0
  55. package/dist/errors/index.d.ts +3 -0
  56. package/dist/errors/index.d.ts.map +1 -0
  57. package/dist/errors/index.js +3 -0
  58. package/dist/errors/index.js.map +1 -0
  59. package/dist/index.d.ts +18 -0
  60. package/dist/index.d.ts.map +1 -0
  61. package/dist/index.js +17 -0
  62. package/dist/index.js.map +1 -0
  63. package/dist/schema/index.d.ts +4 -0
  64. package/dist/schema/index.d.ts.map +1 -0
  65. package/dist/schema/index.js +4 -0
  66. package/dist/schema/index.js.map +1 -0
  67. package/dist/schema/json/index.d.ts +19 -0
  68. package/dist/schema/json/index.d.ts.map +1 -0
  69. package/dist/schema/json/index.js +6 -0
  70. package/dist/schema/json/index.js.map +1 -0
  71. package/dist/schema/json/marker.d.ts +23 -0
  72. package/dist/schema/json/marker.d.ts.map +1 -0
  73. package/dist/schema/json/marker.js +27 -0
  74. package/dist/schema/json/marker.js.map +1 -0
  75. package/dist/schema/json/primitives.d.ts +202 -0
  76. package/dist/schema/json/primitives.d.ts.map +1 -0
  77. package/dist/schema/json/primitives.js +598 -0
  78. package/dist/schema/json/primitives.js.map +1 -0
  79. package/dist/schema/primitives.d.ts +25 -0
  80. package/dist/schema/primitives.d.ts.map +1 -0
  81. package/dist/schema/primitives.js +153 -0
  82. package/dist/schema/primitives.js.map +1 -0
  83. package/dist/schema/rpc.d.ts +21 -0
  84. package/dist/schema/rpc.d.ts.map +1 -0
  85. package/dist/schema/rpc.js +17 -0
  86. package/dist/schema/rpc.js.map +1 -0
  87. package/dist/schema/z/protocol.d.ts +9 -0
  88. package/dist/schema/z/protocol.d.ts.map +1 -0
  89. package/dist/schema/z/protocol.js +9 -0
  90. package/dist/schema/z/protocol.js.map +1 -0
  91. package/dist/schema/z/register.d.ts +9 -0
  92. package/dist/schema/z/register.d.ts.map +1 -0
  93. package/dist/schema/z/register.js +21 -0
  94. package/dist/schema/z/register.js.map +1 -0
  95. package/dist/stream/index.d.ts +3 -0
  96. package/dist/stream/index.d.ts.map +1 -0
  97. package/dist/stream/index.js +2 -0
  98. package/dist/stream/index.js.map +1 -0
  99. package/dist/stream/stringify-stream.d.ts +15 -0
  100. package/dist/stream/stringify-stream.d.ts.map +1 -0
  101. package/dist/stream/stringify-stream.js +322 -0
  102. package/dist/stream/stringify-stream.js.map +1 -0
  103. package/dist/stream/stringify.d.ts +19 -0
  104. package/dist/stream/stringify.d.ts.map +1 -0
  105. package/dist/stream/stringify.js +217 -0
  106. package/dist/stream/stringify.js.map +1 -0
  107. package/dist/transport/express/body.d.ts +9 -0
  108. package/dist/transport/express/body.d.ts.map +1 -0
  109. package/dist/transport/express/body.js +28 -0
  110. package/dist/transport/express/body.js.map +1 -0
  111. package/dist/transport/express/index.d.ts +3 -0
  112. package/dist/transport/express/index.d.ts.map +1 -0
  113. package/dist/transport/express/index.js +2 -0
  114. package/dist/transport/express/index.js.map +1 -0
  115. package/dist/transport/express/middleware.d.ts +12 -0
  116. package/dist/transport/express/middleware.d.ts.map +1 -0
  117. package/dist/transport/express/middleware.js +73 -0
  118. package/dist/transport/express/middleware.js.map +1 -0
  119. package/dist/transport/express/respond.d.ts +4 -0
  120. package/dist/transport/express/respond.d.ts.map +1 -0
  121. package/dist/transport/express/respond.js +125 -0
  122. package/dist/transport/express/respond.js.map +1 -0
  123. package/dist/transport/stdio/index.d.ts +2 -0
  124. package/dist/transport/stdio/index.d.ts.map +1 -0
  125. package/dist/transport/stdio/index.js +2 -0
  126. package/dist/transport/stdio/index.js.map +1 -0
  127. package/dist/transport/stdio/middleware.d.ts +34 -0
  128. package/dist/transport/stdio/middleware.d.ts.map +1 -0
  129. package/dist/transport/stdio/middleware.js +274 -0
  130. package/dist/transport/stdio/middleware.js.map +1 -0
  131. package/dist/transport/ws/index.d.ts +3 -0
  132. package/dist/transport/ws/index.d.ts.map +1 -0
  133. package/dist/transport/ws/index.js +3 -0
  134. package/dist/transport/ws/index.js.map +1 -0
  135. package/dist/transport/ws/middleware.d.ts +25 -0
  136. package/dist/transport/ws/middleware.d.ts.map +1 -0
  137. package/dist/transport/ws/middleware.js +209 -0
  138. package/dist/transport/ws/middleware.js.map +1 -0
  139. package/dist/transport/ws/standalone.d.ts +22 -0
  140. package/dist/transport/ws/standalone.d.ts.map +1 -0
  141. package/dist/transport/ws/standalone.js +39 -0
  142. package/dist/transport/ws/standalone.js.map +1 -0
  143. package/dist/types.d.ts +75 -0
  144. package/dist/types.d.ts.map +1 -0
  145. package/dist/types.js +2 -0
  146. package/dist/types.js.map +1 -0
  147. package/htdoc/assets/index-CQr-lnlD.js +9286 -0
  148. package/htdoc/assets/index-CQr-lnlD.js.map +1 -0
  149. package/htdoc/assets/index-hcP2-9jw.css +729 -0
  150. package/htdoc/index.html +13 -0
  151. package/package.json +89 -3
  152. package/webui/index.html +12 -0
  153. package/webui/src/App.tsx +56 -0
  154. package/webui/src/api/httpTransport.ts +148 -0
  155. package/webui/src/api/paramForm.ts +79 -0
  156. package/webui/src/api/rpc.ts +57 -0
  157. package/webui/src/api/schema.ts +12 -0
  158. package/webui/src/api/types.ts +16 -0
  159. package/webui/src/api/wsTransport.ts +305 -0
  160. package/webui/src/components/MethodDetail.tsx +43 -0
  161. package/webui/src/components/MethodTree.tsx +118 -0
  162. package/webui/src/components/SchemaForm.tsx +696 -0
  163. package/webui/src/components/SchemaView.tsx +151 -0
  164. package/webui/src/components/TestPanel.tsx +279 -0
  165. package/webui/src/components/Toolbar.tsx +70 -0
  166. package/webui/src/hooks/useMethodDetail.ts +61 -0
  167. package/webui/src/hooks/useMethods.ts +27 -0
  168. package/webui/src/main.tsx +13 -0
  169. package/webui/src/styles/geek.css +729 -0
  170. package/webui/src/theme/ThemeProvider.tsx +79 -0
  171. package/webui/src/vite-env.d.ts +1 -0
  172. package/webui/tsconfig.json +34 -0
  173. package/webui/tsconfig.node.json +13 -0
  174. package/webui/vite.config.ts +15 -0
@@ -0,0 +1,153 @@
1
+ import { z } from 'zod';
2
+ export function getMeta(schema) {
3
+ return schema.__meta ?? {};
4
+ }
5
+ function withMeta(schema, meta) {
6
+ const target = schema;
7
+ target.__meta = meta;
8
+ return schema;
9
+ }
10
+ function describe(meta) {
11
+ const kind = meta.kind ?? 'any';
12
+ switch (kind) {
13
+ case 'void':
14
+ return 'void';
15
+ case 'injectable':
16
+ return `injectable:${meta.injectable ?? 'unknown'}`;
17
+ case 'arrayOf':
18
+ return `arrayOf(${String(meta.of ?? 'any')})`;
19
+ case 'objectOf':
20
+ return `objectOf(${String(meta.of ?? 'any')})`;
21
+ case 'oneOf':
22
+ return `oneOf(${JSON.stringify(meta.of ?? [])})`;
23
+ case 'oneOfType':
24
+ return `oneOfType(${JSON.stringify(meta.of ?? [])})`;
25
+ case 'shape':
26
+ return `shape(${JSON.stringify(meta.of ?? {})})`;
27
+ default:
28
+ return kind;
29
+ }
30
+ }
31
+ export function setJSONString(schema, description) {
32
+ const target = schema;
33
+ target.toJSONString = () => description;
34
+ return schema;
35
+ }
36
+ export function toJSONString(schema) {
37
+ const target = schema;
38
+ if (typeof target.toJSONString === 'function') {
39
+ return target.toJSONString();
40
+ }
41
+ return describe(getMeta(schema));
42
+ }
43
+ export function voidSchema() {
44
+ const schema = z.undefined();
45
+ const result = withMeta(schema, { kind: 'void', required: 'none' });
46
+ return setJSONString(result, 'void');
47
+ }
48
+ export function anySchema() {
49
+ const schema = z.any();
50
+ const result = withMeta(schema, { kind: 'any' });
51
+ return setJSONString(result, 'any');
52
+ }
53
+ export function injectableSchema(name) {
54
+ const schema = z.any();
55
+ const result = withMeta(schema, { kind: 'injectable', injectable: name });
56
+ return setJSONString(result, `injectable:${name}`);
57
+ }
58
+ export function named(schema, naming) {
59
+ const meta = getMeta(schema);
60
+ withMeta(schema, { ...meta, naming });
61
+ const base = toJSONString(schema);
62
+ return setJSONString(schema, `${base}.named(${naming})`);
63
+ }
64
+ export function required(schema) {
65
+ const meta = getMeta(schema);
66
+ withMeta(schema, { ...meta, required: 'isRequired' });
67
+ const base = toJSONString(schema);
68
+ return setJSONString(schema, `${base}.isRequired`);
69
+ }
70
+ export function requiredNotNull(schema) {
71
+ const meta = getMeta(schema);
72
+ withMeta(schema, { ...meta, required: 'isRequiredNotNull' });
73
+ const base = toJSONString(schema);
74
+ return setJSONString(schema, `${base}.isRequiredNotNull`);
75
+ }
76
+ export function primitiveSchema(kind) {
77
+ let schema;
78
+ switch (kind) {
79
+ case 'string':
80
+ schema = z.string();
81
+ break;
82
+ case 'number':
83
+ schema = z.number();
84
+ break;
85
+ case 'boolean':
86
+ schema = z.boolean();
87
+ break;
88
+ case 'function':
89
+ schema = z.function();
90
+ break;
91
+ case 'object':
92
+ schema = z.object({}).passthrough();
93
+ break;
94
+ case 'array':
95
+ schema = z.array(z.any());
96
+ break;
97
+ case 'symbol':
98
+ schema = z.custom((value) => typeof value === 'symbol');
99
+ break;
100
+ case 'void':
101
+ schema = z.void();
102
+ break;
103
+ }
104
+ const result = withMeta(schema, { kind });
105
+ return setJSONString(result, kind);
106
+ }
107
+ export function arrayOf(schema) {
108
+ const inner = z.array(schema);
109
+ const description = describe({ kind: 'arrayOf', of: toJSONString(schema) });
110
+ const result = withMeta(inner, { kind: 'arrayOf', of: toJSONString(schema) });
111
+ return setJSONString(result, description);
112
+ }
113
+ export function objectOf(schema) {
114
+ const inner = z.record(schema);
115
+ const description = describe({ kind: 'objectOf', of: toJSONString(schema) });
116
+ const result = withMeta(inner, { kind: 'objectOf', of: toJSONString(schema) });
117
+ return setJSONString(result, description);
118
+ }
119
+ export function oneOf(values) {
120
+ const schema = z.enum(values);
121
+ const description = `oneOf(${JSON.stringify(values)})`;
122
+ const result = withMeta(schema, { kind: 'oneOf', of: values });
123
+ return setJSONString(result, description);
124
+ }
125
+ export function oneOfType(schemas) {
126
+ if (schemas.length === 0) {
127
+ throw new Error('oneOfType requires at least one schema.');
128
+ }
129
+ const [first, ...rest] = schemas;
130
+ if (!first)
131
+ throw new Error('oneOfType requires at least one schema.');
132
+ const union = rest.length === 0
133
+ ? first
134
+ : z.union([first, ...rest]);
135
+ const description = `oneOfType(${JSON.stringify(schemas.map((s) => toJSONString(s)))})`;
136
+ const result = withMeta(union, { kind: 'oneOfType', of: schemas.map((s) => toJSONString(s)) });
137
+ return setJSONString(result, description);
138
+ }
139
+ export function shape(shapeMap) {
140
+ const objectShape = z.object(shapeMap);
141
+ const description = `shape(${JSON.stringify(Object.fromEntries(Object.entries(shapeMap).map(([k, v]) => [k, toJSONString(v)])))})`;
142
+ const result = withMeta(objectShape, { kind: 'shape', of: shapeMap });
143
+ return setJSONString(result, description);
144
+ }
145
+ export function instanceOf(cls) {
146
+ // zod's instanceof typing is strict about concrete constructors;
147
+ // runtime behaviour is identical, so we bridge via unknown.
148
+ const schema = z.instanceof(cls);
149
+ const name = cls.name || '<<anonymous>>';
150
+ const result = withMeta(schema, { kind: 'instanceOf', of: name });
151
+ return setJSONString(result, `instanceOf(${name})`);
152
+ }
153
+ //# sourceMappingURL=primitives.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/schema/primitives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgCxB,MAAM,UAAU,OAAO,CAAC,MAAmB;IACzC,OAAQ,MAA6B,CAAC,MAAM,IAAI,EAAE,CAAC;AACrD,CAAC;AAED,SAAS,QAAQ,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CAAC,CAAC;IAC/F,OAAO,aAAa,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,KAAK,CAAwC,QAAW;IACtE,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACvC,MAAM,WAAW,GAAG,SAAS,IAAI,CAAC,SAAS,CACzC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CACnF,GAAG,CAAC;IACL,MAAM,MAAM,GAAG,QAAQ,CAAC,WAAW,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC;IACtE,OAAO,aAAa,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,UAAU,CAAiD,GAAM;IAC/E,iEAAiE;IACjE,4DAA4D;IAC5D,MAAM,MAAM,GAAG,CAAC,CAAC,UAAU,CAAC,GAAyD,CAAC,CAAC;IACvF,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,IAAI,eAAe,CAAC;IACzC,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IAClE,OAAO,aAAa,CAAC,MAAM,EAAE,cAAc,IAAI,GAAG,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { z } from 'zod';
2
+ /** Strict JSON-RPC 2.0 request: id is required (string | number). */
3
+ export declare const RequestSchema: z.ZodObject<{
4
+ jsonrpc: z.ZodLiteral<"2.0">;
5
+ method: z.ZodString;
6
+ params: z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodUnknown, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>]>>;
7
+ id: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
8
+ }, "strip", z.ZodTypeAny, {
9
+ jsonrpc: "2.0";
10
+ method: string;
11
+ id: string | number;
12
+ params?: Record<string, unknown> | unknown[] | undefined;
13
+ }, {
14
+ jsonrpc: "2.0";
15
+ method: string;
16
+ id: string | number;
17
+ params?: Record<string, unknown> | unknown[] | undefined;
18
+ }>;
19
+ export type ParsedRequest = z.infer<typeof RequestSchema>;
20
+ export declare function formatIssues(error: z.ZodError): string;
21
+ //# sourceMappingURL=rpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../src/schema/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,qEAAqE;AACrE,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;EAKxB,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAE1D,wBAAgB,YAAY,CAAC,KAAK,EAAE,CAAC,CAAC,QAAQ,GAAG,MAAM,CAOtD"}
@@ -0,0 +1,17 @@
1
+ import { z } from 'zod';
2
+ /** Strict JSON-RPC 2.0 request: id is required (string | number). */
3
+ export const RequestSchema = z.object({
4
+ jsonrpc: z.literal('2.0'),
5
+ method: z.string().min(1),
6
+ params: z.union([z.array(z.unknown()), z.record(z.unknown())]).optional(),
7
+ id: z.union([z.string(), z.number()]),
8
+ });
9
+ export function formatIssues(error) {
10
+ return error.issues
11
+ .map((issue) => {
12
+ const path = issue.path.length > 0 ? issue.path.join('.') : '(root)';
13
+ return `${path}: ${issue.message}`;
14
+ })
15
+ .join('; ');
16
+ }
17
+ //# sourceMappingURL=rpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../src/schema/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,qEAAqE;AACrE,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC;IACzB,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACzB,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE;IACzE,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;CACtC,CAAC,CAAC;AAIH,MAAM,UAAU,YAAY,CAAC,KAAiB;IAC5C,OAAO,KAAK,CAAC,MAAM;SAChB,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACb,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;QACrE,OAAO,GAAG,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;IACrC,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
@@ -0,0 +1,9 @@
1
+ export declare const RPCVersion = "2.0";
2
+ export type RPCVersion = '2.0';
3
+ export declare const RPCRequestSchema: import("../json").ObjectSchema<{
4
+ jsonrpc: import("../json").LiteralSchema<"2.0">;
5
+ method: import("../json").StringSchema<"string">;
6
+ params: import("../json").NullableSchema<import("../json").JSONValue[]>;
7
+ id: import("../json").UnionSchema<readonly [import("../json").StringSchema<"string">, import("../json").NumberSchema]>;
8
+ }, true>;
9
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../../src/schema/z/protocol.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU,QAAQ,CAAC;AAChC,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC;AAE/B,eAAO,MAAM,gBAAgB;;;;;QAKlB,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { s } from "../json";
2
+ export const RPCVersion = '2.0';
3
+ export const RPCRequestSchema = s.object({
4
+ jsonrpc: s.literal(RPCVersion),
5
+ method: s.string().min(1),
6
+ params: s.array(s.unknown()).nullable(),
7
+ id: s.union([s.string().min(1), s.number()]),
8
+ }).strict();
9
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../../src/schema/z/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,SAAS,CAAC;AAE5B,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,CAAC;AAGhC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC;IACrC,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC;IAC9B,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACzB,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,QAAQ,EAAE;IACvC,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;CAC/C,CAAC,CAAC,MAAM,EAAE,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { z } from "zod";
2
+ import { Schema, VoidMarker, InjectableMarker, StreamMarker } from "../json";
3
+ export declare const registerSchema: z.ZodTuple<[z.ZodObject<{
4
+ namespace: z.ZodString;
5
+ document: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>]>>;
6
+ grantTo: z.ZodOptional<z.ZodArray<z.ZodString>>;
7
+ signature: z.ZodTuple<[z.ZodUnion<readonly [z.ZodInstanceOf<Schema<unknown>>, z.ZodInstanceOf<VoidMarker>, z.ZodInstanceOf<StreamMarker<unknown>>]>], z.ZodUnion<readonly [z.ZodInstanceOf<Schema<unknown>>, z.ZodInstanceOf<InjectableMarker<string>>]>>;
8
+ }, z.core.$strict>, z.ZodFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>], null>;
9
+ //# sourceMappingURL=register.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../../../src/schema/z/register.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EACH,MAAM,EACN,UAAU,EACV,gBAAgB,EAChB,YAAY,EACf,MAAM,SAAS,CAAC;AAmBjB,eAAO,MAAM,cAAc;;;;;0FAGzB,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { z } from "zod";
2
+ import { Schema, VoidMarker, InjectableMarker, StreamMarker, } from "../json";
3
+ const schemaInstance = z.instanceof(Schema);
4
+ const voidMarker = z.instanceof(VoidMarker);
5
+ const injectableMarker = z.instanceof(InjectableMarker);
6
+ const streamMarker = z.instanceof(StreamMarker);
7
+ const returnSchema = z.union([schemaInstance, voidMarker, streamMarker]);
8
+ const parameterSchema = z.union([schemaInstance, injectableMarker]);
9
+ const signatureSchema = z.tuple([returnSchema]).rest(parameterSchema);
10
+ const documentSchema = z.union([z.string(), z.function()]);
11
+ const descriptorSchema = z.object({
12
+ namespace: z.string().min(1),
13
+ document: documentSchema.optional(),
14
+ grantTo: z.array(z.string()).optional(),
15
+ signature: signatureSchema,
16
+ }).strict();
17
+ export const registerSchema = z.tuple([
18
+ descriptorSchema,
19
+ z.function(),
20
+ ]);
21
+ //# sourceMappingURL=register.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.js","sourceRoot":"","sources":["../../../src/schema/z/register.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EACH,MAAM,EACN,UAAU,EACV,gBAAgB,EAChB,YAAY,GACf,MAAM,SAAS,CAAC;AAEjB,MAAM,cAAc,GAAG,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;AAC5C,MAAM,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;AAC5C,MAAM,gBAAgB,GAAG,CAAC,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;AACxD,MAAM,YAAY,GAAG,CAAC,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;AAEhD,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,cAAc,EAAE,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC;AACzE,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC,CAAC;AACpE,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AACtE,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;AAE3D,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC9B,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5B,QAAQ,EAAE,cAAc,CAAC,QAAQ,EAAE;IACnC,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE;IACvC,SAAS,EAAE,eAAe;CAC7B,CAAC,CAAC,MAAM,EAAE,CAAC;AAEZ,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC;IAClC,gBAAgB;IAChB,CAAC,CAAC,QAAQ,EAAE;CACf,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { stringifySync, pipeStringify } from './stringify';
2
+ export type { PipeOptions } from './stringify';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC3D,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { stringifySync, pipeStringify } from './stringify';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 流式 JSON 序列化 AsyncGenerator
3
+ * 尽可能兼容原生 JSON.stringify,同时支持 Promise / Generator / AsyncGenerator / Buffer
4
+ */
5
+ export interface StringifyStreamOptions {
6
+ /** 缓冲大小(字符数),默认 16KB */
7
+ bufferSize?: number;
8
+ /** 缩进,同 JSON.stringify 的 space */
9
+ space?: string | number;
10
+ /** replacer,同 JSON.stringify */
11
+ replacer?: ((this: any, key: string, value: any) => any) | (string | number)[] | null;
12
+ }
13
+ export declare function stringifyStream(input: unknown, options?: StringifyStreamOptions): AsyncGenerator<string, void, unknown>;
14
+ export declare function stringify(input: unknown, options?: StringifyStreamOptions): Promise<string>;
15
+ //# sourceMappingURL=stringify-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringify-stream.d.ts","sourceRoot":"","sources":["../../src/stream/stringify-stream.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,sBAAsB;IACnC,wBAAwB;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kCAAkC;IAClC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,gCAAgC;IAChC,QAAQ,CAAC,EACP,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC,GAC7C,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GACnB,IAAI,CAAC;CACV;AAKD,wBAAuB,eAAe,CAClC,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,sBAA2B,GACrC,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CA2XvC;AAGD,wBAAsB,SAAS,CAC3B,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,sBAA2B,GACrC,OAAO,CAAC,MAAM,CAAC,CAOjB"}
@@ -0,0 +1,322 @@
1
+ /**
2
+ * 流式 JSON 序列化 AsyncGenerator
3
+ * 尽可能兼容原生 JSON.stringify,同时支持 Promise / Generator / AsyncGenerator / Buffer
4
+ */
5
+ const MAX_GAP_LENGTH = 10;
6
+ const DEFAULT_BUFFER_SIZE = 16 * 1024;
7
+ export async function* stringifyStream(input, options = {}) {
8
+ const { bufferSize: rawBufferSize = DEFAULT_BUFFER_SIZE, space, replacer, } = options;
9
+ const bufferSize = Math.max(1, rawBufferSize | 0);
10
+ const seen = new WeakSet();
11
+ let gap = '';
12
+ if (typeof space === 'number') {
13
+ gap = ' '.repeat(Math.min(MAX_GAP_LENGTH, Math.max(0, Math.floor(space))));
14
+ }
15
+ else if (typeof space === 'string') {
16
+ gap = space.slice(0, MAX_GAP_LENGTH);
17
+ }
18
+ let replacerFn = null;
19
+ let propertyList = null;
20
+ if (typeof replacer === 'function') {
21
+ replacerFn = replacer;
22
+ }
23
+ else if (Array.isArray(replacer)) {
24
+ propertyList = new Set();
25
+ for (const p of replacer) {
26
+ if (typeof p === 'string' || typeof p === 'number') {
27
+ propertyList.add(String(p));
28
+ }
29
+ }
30
+ }
31
+ let buffer = '';
32
+ function* flush(force = false) {
33
+ while (buffer.length >= bufferSize || (force && buffer.length > 0)) {
34
+ const chunk = buffer.slice(0, bufferSize);
35
+ buffer = buffer.slice(bufferSize);
36
+ yield chunk;
37
+ }
38
+ }
39
+ function write(str) {
40
+ buffer += str;
41
+ }
42
+ function writeJSONString(str) {
43
+ write(JSON.stringify(str));
44
+ }
45
+ function checkCycle(value) {
46
+ if (seen.has(value)) {
47
+ throw new TypeError('Converting circular structure to JSON');
48
+ }
49
+ seen.add(value);
50
+ }
51
+ function uncheckCycle(value) {
52
+ seen.delete(value);
53
+ }
54
+ function isPrimitive(v) {
55
+ const t = typeof v;
56
+ return (v === null ||
57
+ t === 'string' ||
58
+ t === 'number' ||
59
+ t === 'boolean' ||
60
+ t === 'undefined' ||
61
+ t === 'symbol' ||
62
+ t === 'bigint');
63
+ }
64
+ function assertNoBigInt(v) {
65
+ if (typeof v === 'bigint') {
66
+ throw new TypeError('Do not know how to serialize a BigInt');
67
+ }
68
+ }
69
+ function isTypedArray(value) {
70
+ return ArrayBuffer.isView(value) && !(value instanceof DataView);
71
+ }
72
+ function prepareValue(value, key) {
73
+ // 1. toJSON(跳过 Buffer)
74
+ if (value !== null &&
75
+ typeof value === 'object' &&
76
+ typeof value.toJSON === 'function' &&
77
+ !(typeof Buffer !== 'undefined' && Buffer.isBuffer(value))) {
78
+ value = value.toJSON(key);
79
+ }
80
+ // 2. replacer
81
+ if (replacerFn) {
82
+ value = replacerFn.call(key === '' ? undefined : null, key, value);
83
+ }
84
+ return value;
85
+ }
86
+ async function* serialize(value, key, depth) {
87
+ // ========== 只在这里做一次 prepare ==========
88
+ value = prepareValue(value, key);
89
+ // undefined / function / symbol → 由调用方决定(对象省略,数组写 null)
90
+ if (value === undefined ||
91
+ typeof value === 'function' ||
92
+ typeof value === 'symbol') {
93
+ return;
94
+ }
95
+ assertNoBigInt(value);
96
+ // Promise(replacer 或 toJSON 返回的 Promise 在这里统一处理)
97
+ if (value instanceof Promise) {
98
+ value = await Promise.resolve(value);
99
+ // 注意:await 之后要重新走完整流程(包括可能的再次 toJSON/replacer?)
100
+ // 为了与「只 prepare 一次」一致,这里选择不再 prepare,直接递归时跳过 prepare
101
+ // 最简单且安全的做法:再调一次 serialize(此时 value 已是 resolved 后的值)
102
+ // 但为了避免对 resolved 后的 number 再次触发 replacer,我们改成内部递归时传一个 flag
103
+ yield* serializePrepared(value, key, depth);
104
+ return;
105
+ }
106
+ yield* serializePrepared(value, key, depth);
107
+ }
108
+ /** 已经完成 prepare 的值,直接序列化(不再调用 toJSON/replacer) */
109
+ async function* serializePrepared(value, key, depth) {
110
+ if (value === undefined ||
111
+ typeof value === 'function' ||
112
+ typeof value === 'symbol') {
113
+ return;
114
+ }
115
+ assertNoBigInt(value);
116
+ if (isPrimitive(value)) {
117
+ write(JSON.stringify(value));
118
+ return;
119
+ }
120
+ if (value instanceof Date) {
121
+ if (Number.isNaN(value.getTime())) {
122
+ write('null');
123
+ }
124
+ else {
125
+ writeJSONString(value.toISOString());
126
+ }
127
+ return;
128
+ }
129
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
130
+ writeJSONString(value.toString('hex'));
131
+ return;
132
+ }
133
+ // boxed primitives
134
+ if (value instanceof Number) {
135
+ write(JSON.stringify(value.valueOf()));
136
+ return;
137
+ }
138
+ if (value instanceof String) {
139
+ write(JSON.stringify(value.valueOf()));
140
+ return;
141
+ }
142
+ if (value instanceof Boolean) {
143
+ write(JSON.stringify(value.valueOf()));
144
+ return;
145
+ }
146
+ // 数组
147
+ if (Array.isArray(value)) {
148
+ checkCycle(value);
149
+ write('[');
150
+ let first = true;
151
+ for (let i = 0; i < value.length; i++) {
152
+ if (!first)
153
+ write(',');
154
+ first = false;
155
+ if (gap)
156
+ write('\n' + gap.repeat(depth + 1));
157
+ let elem = prepareValue(value[i], String(i));
158
+ if (elem instanceof Promise) {
159
+ elem = await Promise.resolve(elem);
160
+ }
161
+ if (elem === undefined ||
162
+ typeof elem === 'function' ||
163
+ typeof elem === 'symbol') {
164
+ write('null');
165
+ }
166
+ else {
167
+ yield* serializePrepared(elem, String(i), depth + 1);
168
+ }
169
+ yield* flush();
170
+ }
171
+ if (gap && value.length > 0) {
172
+ write('\n' + gap.repeat(depth));
173
+ }
174
+ write(']');
175
+ uncheckCycle(value);
176
+ return;
177
+ }
178
+ // TypedArray
179
+ if (isTypedArray(value)) {
180
+ checkCycle(value);
181
+ write('{');
182
+ let first = true;
183
+ const arr = value;
184
+ for (let i = 0; i < arr.length; i++) {
185
+ if (!first)
186
+ write(',');
187
+ first = false;
188
+ if (gap)
189
+ write('\n' + gap.repeat(depth + 1));
190
+ writeJSONString(String(i));
191
+ write(gap ? ': ' : ':');
192
+ write(JSON.stringify(arr[i]));
193
+ yield* flush();
194
+ }
195
+ if (gap && arr.length > 0) {
196
+ write('\n' + gap.repeat(depth));
197
+ }
198
+ write('}');
199
+ uncheckCycle(value);
200
+ return;
201
+ }
202
+ // 可迭代
203
+ const hasAsyncIterator = value != null &&
204
+ typeof value[Symbol.asyncIterator] === 'function';
205
+ const hasIterator = value != null &&
206
+ typeof value[Symbol.iterator] === 'function';
207
+ const isIteratorLike = value != null &&
208
+ typeof value.next === 'function';
209
+ const shouldTreatAsIterable = (hasAsyncIterator || hasIterator || isIteratorLike) &&
210
+ !(value instanceof Map) &&
211
+ !(value instanceof Set) &&
212
+ !(value instanceof WeakMap) &&
213
+ !(value instanceof WeakSet);
214
+ if (shouldTreatAsIterable) {
215
+ write('[');
216
+ let first = true;
217
+ let idx = 0;
218
+ let iterator;
219
+ if (hasAsyncIterator) {
220
+ iterator = value[Symbol.asyncIterator]();
221
+ }
222
+ else if (hasIterator) {
223
+ iterator = value[Symbol.iterator]();
224
+ }
225
+ else {
226
+ iterator = value;
227
+ }
228
+ while (true) {
229
+ const step = await Promise.resolve(iterator.next());
230
+ if (step.done)
231
+ break;
232
+ if (!first)
233
+ write(',');
234
+ first = false;
235
+ if (gap)
236
+ write('\n' + gap.repeat(depth + 1));
237
+ // const before = buffer.length;
238
+ // yield* serialize(step.value, String(idx), depth + 1);
239
+ // if (buffer.length === before) {
240
+ // write('null');
241
+ // }
242
+ // prepare + await 后再决定写 null 还是序列化(与对象路径一致)
243
+ let elem = prepareValue(step.value, String(idx));
244
+ if (elem instanceof Promise) {
245
+ elem = await Promise.resolve(elem);
246
+ }
247
+ if (elem === undefined ||
248
+ typeof elem === 'function' ||
249
+ typeof elem === 'symbol') {
250
+ write('null');
251
+ }
252
+ else {
253
+ yield* serializePrepared(elem, String(idx), depth + 1);
254
+ }
255
+ idx++;
256
+ yield* flush();
257
+ }
258
+ if (gap && !first) {
259
+ write('\n' + gap.repeat(depth));
260
+ }
261
+ write(']');
262
+ return;
263
+ }
264
+ // 普通对象
265
+ checkCycle(value);
266
+ if (value instanceof Set ||
267
+ value instanceof Map ||
268
+ value instanceof WeakSet ||
269
+ value instanceof WeakMap) {
270
+ write('{}');
271
+ uncheckCycle(value);
272
+ return;
273
+ }
274
+ write('{');
275
+ let first = true;
276
+ const keys = propertyList
277
+ ? [...propertyList].filter((k) => Object.prototype.hasOwnProperty.call(value, k))
278
+ : Object.keys(value);
279
+ for (const k of keys) {
280
+ // 1. 先 prepare
281
+ let propValue = prepareValue(value[k], k);
282
+ // 2. 如果是 Promise,先 await(关键!)
283
+ if (propValue instanceof Promise) {
284
+ propValue = await Promise.resolve(propValue);
285
+ }
286
+ // 3. 现在再判断是否应该省略
287
+ if (propValue === undefined ||
288
+ typeof propValue === 'function' ||
289
+ typeof propValue === 'symbol') {
290
+ continue;
291
+ }
292
+ // 4. 可以安全地写 key 了
293
+ if (!first)
294
+ write(',');
295
+ first = false;
296
+ if (gap) {
297
+ write('\n' + gap.repeat(depth + 1));
298
+ }
299
+ writeJSONString(k);
300
+ write(gap ? ': ' : ':');
301
+ // 5. 直接序列化已经 prepare + await 过的值
302
+ yield* serializePrepared(propValue, k, depth + 1);
303
+ yield* flush();
304
+ }
305
+ if (gap && !first) {
306
+ write('\n' + gap.repeat(depth));
307
+ }
308
+ write('}');
309
+ uncheckCycle(value);
310
+ }
311
+ yield* serialize(input, '', 0);
312
+ yield* flush(true);
313
+ }
314
+ export async function stringify(input, options = {}) {
315
+ const gen = stringifyStream(input, options);
316
+ const chunks = [];
317
+ for await (const chunk of gen) {
318
+ chunks.push(chunk);
319
+ }
320
+ return chunks.join('');
321
+ }
322
+ //# sourceMappingURL=stringify-stream.js.map