@query-farm/vgi-rpc 0.1.0
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/LICENSE.md +191 -0
- package/README.md +332 -0
- package/dist/client/connect.d.ts +10 -0
- package/dist/client/connect.d.ts.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/introspect.d.ts +30 -0
- package/dist/client/introspect.d.ts.map +1 -0
- package/dist/client/ipc.d.ts +34 -0
- package/dist/client/ipc.d.ts.map +1 -0
- package/dist/client/pipe.d.ts +63 -0
- package/dist/client/pipe.d.ts.map +1 -0
- package/dist/client/stream.d.ts +52 -0
- package/dist/client/stream.d.ts.map +1 -0
- package/dist/client/types.d.ts +25 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/constants.d.ts +15 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/dispatch/describe.d.ts +14 -0
- package/dist/dispatch/describe.d.ts.map +1 -0
- package/dist/dispatch/stream.d.ts +20 -0
- package/dist/dispatch/stream.d.ts.map +1 -0
- package/dist/dispatch/unary.d.ts +9 -0
- package/dist/dispatch/unary.d.ts.map +1 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/http/common.d.ts +16 -0
- package/dist/http/common.d.ts.map +1 -0
- package/dist/http/dispatch.d.ts +18 -0
- package/dist/http/dispatch.d.ts.map +1 -0
- package/dist/http/handler.d.ts +16 -0
- package/dist/http/handler.d.ts.map +1 -0
- package/dist/http/index.d.ts +4 -0
- package/dist/http/index.d.ts.map +1 -0
- package/dist/http/token.d.ts +24 -0
- package/dist/http/token.d.ts.map +1 -0
- package/dist/http/types.d.ts +30 -0
- package/dist/http/types.d.ts.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2493 -0
- package/dist/index.js.map +34 -0
- package/dist/protocol.d.ts +62 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/schema.d.ts +38 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/server.d.ts +19 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/types.d.ts +71 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/util/schema.d.ts +20 -0
- package/dist/util/schema.d.ts.map +1 -0
- package/dist/util/zstd.d.ts +5 -0
- package/dist/util/zstd.d.ts.map +1 -0
- package/dist/wire/reader.d.ts +40 -0
- package/dist/wire/reader.d.ts.map +1 -0
- package/dist/wire/request.d.ts +15 -0
- package/dist/wire/request.d.ts.map +1 -0
- package/dist/wire/response.d.ts +25 -0
- package/dist/wire/response.d.ts.map +1 -0
- package/dist/wire/writer.d.ts +59 -0
- package/dist/wire/writer.d.ts.map +1 -0
- package/package.json +32 -0
- package/src/client/connect.ts +310 -0
- package/src/client/index.ts +14 -0
- package/src/client/introspect.ts +138 -0
- package/src/client/ipc.ts +225 -0
- package/src/client/pipe.ts +661 -0
- package/src/client/stream.ts +297 -0
- package/src/client/types.ts +31 -0
- package/src/constants.ts +22 -0
- package/src/dispatch/describe.ts +155 -0
- package/src/dispatch/stream.ts +151 -0
- package/src/dispatch/unary.ts +35 -0
- package/src/errors.ts +22 -0
- package/src/http/common.ts +89 -0
- package/src/http/dispatch.ts +340 -0
- package/src/http/handler.ts +247 -0
- package/src/http/index.ts +6 -0
- package/src/http/token.ts +149 -0
- package/src/http/types.ts +49 -0
- package/src/index.ts +52 -0
- package/src/protocol.ts +144 -0
- package/src/schema.ts +114 -0
- package/src/server.ts +159 -0
- package/src/types.ts +162 -0
- package/src/util/schema.ts +31 -0
- package/src/util/zstd.ts +49 -0
- package/src/wire/reader.ts +113 -0
- package/src/wire/request.ts +98 -0
- package/src/wire/response.ts +181 -0
- package/src/wire/writer.ts +137 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2025-2026, Query.Farm LLC - https://query.farm
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# vgi-rpc
|
|
2
|
+
|
|
3
|
+
TypeScript server library for the [vgi-rpc](https://github.com/rustyconover/vgi-rpc) framework. Implements RPC servers that communicate over stdin/stdout using [Apache Arrow](https://arrow.apache.org/) IPC serialization.
|
|
4
|
+
|
|
5
|
+
Define RPC methods with Arrow-typed schemas, serve them over stdin/stdout, and interact with them using the Python `vgi-rpc` CLI or any vgi-rpc client. Unlike JSON-over-HTTP, structured data stays in Arrow columnar format for efficient transfer.
|
|
6
|
+
|
|
7
|
+
**Key features:**
|
|
8
|
+
|
|
9
|
+
- **Three method types** — unary (request-response), producer (server-streaming), and exchange (bidirectional-streaming)
|
|
10
|
+
- **Apache Arrow IPC wire format** — efficient columnar serialization compatible with the Python vgi-rpc framework
|
|
11
|
+
- **Schema shorthand** — declare schemas with `{ name: str, count: int }` instead of manual `Schema`/`Field` construction
|
|
12
|
+
- **Fluent protocol builder** — chain `.unary()`, `.producer()`, `.exchange()` calls to define your service
|
|
13
|
+
- **Type-safe streaming state** — generic `<S>` parameter threads state types through init and produce/exchange functions
|
|
14
|
+
- **Runtime introspection** — opt-in `__describe__` method for dynamic service discovery via the CLI
|
|
15
|
+
- **Result validation** — missing required fields in handler results throw descriptive errors at emit time
|
|
16
|
+
- **Three client transports** — HTTP, subprocess, and raw pipe, all sharing a unified `RpcClient` interface
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bun add vgi-rpc
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Requires [Bun](https://bun.sh/) runtime.
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { Protocol, VgiRpcServer, str, float } from "vgi-rpc";
|
|
30
|
+
|
|
31
|
+
const protocol = new Protocol("Calculator");
|
|
32
|
+
|
|
33
|
+
protocol.unary("add", {
|
|
34
|
+
params: { a: float, b: float },
|
|
35
|
+
result: { result: float },
|
|
36
|
+
handler: async ({ a, b }) => ({ result: a + b }),
|
|
37
|
+
doc: "Add two numbers.",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
protocol.unary("greet", {
|
|
41
|
+
params: { name: str },
|
|
42
|
+
result: { result: str },
|
|
43
|
+
handler: async ({ name }) => ({ result: `Hello, ${name}!` }),
|
|
44
|
+
doc: "Greet someone by name.",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const server = new VgiRpcServer(protocol, { enableDescribe: true });
|
|
48
|
+
server.run();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Client
|
|
52
|
+
|
|
53
|
+
Connect to any vgi-rpc server programmatically:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { httpConnect, subprocessConnect } from "vgi-rpc";
|
|
57
|
+
|
|
58
|
+
// HTTP transport
|
|
59
|
+
const client = httpConnect("http://localhost:8080");
|
|
60
|
+
const result = await client.call("add", { a: 2, b: 3 });
|
|
61
|
+
console.log(result); // { result: 5 }
|
|
62
|
+
client.close();
|
|
63
|
+
|
|
64
|
+
// Subprocess transport (spawns server, communicates over pipes)
|
|
65
|
+
const client2 = subprocessConnect(["bun", "run", "server.ts"]);
|
|
66
|
+
const result2 = await client2.call("greet", { name: "World" });
|
|
67
|
+
console.log(result2); // { result: "Hello, World!" }
|
|
68
|
+
client2.close();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
All transports share the same `RpcClient` interface: `call()`, `stream()`, `describe()`, `close()`.
|
|
72
|
+
|
|
73
|
+
## Testing with the Python CLI
|
|
74
|
+
|
|
75
|
+
Test it with the Python CLI:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Describe the service
|
|
79
|
+
vgi-rpc --cmd "bun run server.ts" describe
|
|
80
|
+
|
|
81
|
+
# Call a method
|
|
82
|
+
vgi-rpc --cmd "bun run server.ts" call add a=1.0 b=2.0
|
|
83
|
+
# {"result": 3.0}
|
|
84
|
+
|
|
85
|
+
vgi-rpc --cmd "bun run server.ts" call greet name=World
|
|
86
|
+
# {"result": "Hello, World!"}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Defining Methods
|
|
90
|
+
|
|
91
|
+
### Unary
|
|
92
|
+
|
|
93
|
+
Single request, single response:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
protocol.unary("add", {
|
|
97
|
+
params: { a: float, b: float },
|
|
98
|
+
result: { result: float },
|
|
99
|
+
handler: async ({ a, b }) => ({ result: a + b }),
|
|
100
|
+
doc: "Add two numbers.",
|
|
101
|
+
defaults: { b: 1.0 },
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
The handler receives parsed parameters and returns a record matching the result schema. The optional `defaults` field provides default values for omitted parameters.
|
|
106
|
+
|
|
107
|
+
### Producer (Server Streaming)
|
|
108
|
+
|
|
109
|
+
The server produces multiple output batches. The generic `<S>` parameter infers state types from the `init` return value:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
protocol.producer<{ limit: number; current: number }>("count", {
|
|
113
|
+
params: { limit: int32, batch_size: int32 },
|
|
114
|
+
outputSchema: { n: int32, n_squared: int32 },
|
|
115
|
+
init: async ({ limit, batch_size }) => ({
|
|
116
|
+
limit,
|
|
117
|
+
current: 0,
|
|
118
|
+
batchSize: batch_size,
|
|
119
|
+
}),
|
|
120
|
+
produce: async (state, out) => {
|
|
121
|
+
if (state.current >= state.limit) {
|
|
122
|
+
out.finish();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
out.emitRow({ n: state.current, n_squared: state.current ** 2 });
|
|
126
|
+
state.current++;
|
|
127
|
+
},
|
|
128
|
+
doc: "Count from 0 to limit-1.",
|
|
129
|
+
defaults: { batch_size: 1 },
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`produce` is called repeatedly. Call `out.finish()` to end the stream. Mutate `state` in-place between calls.
|
|
134
|
+
|
|
135
|
+
### Exchange (Bidirectional Streaming)
|
|
136
|
+
|
|
137
|
+
Client sends batches, server responds one output batch per input batch:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
protocol.exchange<{ factor: number }>("scale", {
|
|
141
|
+
params: { factor: float },
|
|
142
|
+
inputSchema: { value: float },
|
|
143
|
+
outputSchema: { value: float },
|
|
144
|
+
init: async ({ factor }) => ({ factor }),
|
|
145
|
+
exchange: async (state, input, out) => {
|
|
146
|
+
const value = input.getChildAt(0)?.get(0) as number;
|
|
147
|
+
out.emitRow({ value: value * state.factor });
|
|
148
|
+
},
|
|
149
|
+
doc: "Scale input values by a factor.",
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Stream Headers
|
|
154
|
+
|
|
155
|
+
Producer and exchange methods can send a one-time header before the data stream:
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
protocol.producer<{ count: number; current: number }>("produce_with_header", {
|
|
159
|
+
params: { count: int },
|
|
160
|
+
outputSchema: { index: int, value: int },
|
|
161
|
+
headerSchema: { total_expected: int, description: str },
|
|
162
|
+
headerInit: (params) => ({
|
|
163
|
+
total_expected: params.count,
|
|
164
|
+
description: `producing ${params.count} batches`,
|
|
165
|
+
}),
|
|
166
|
+
init: ({ count }) => ({ count, current: 0 }),
|
|
167
|
+
produce: (state, out) => {
|
|
168
|
+
if (state.current >= state.count) {
|
|
169
|
+
out.finish();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
out.emitRow({ index: state.current, value: state.current * 10 });
|
|
173
|
+
state.current++;
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Schema Shorthand
|
|
179
|
+
|
|
180
|
+
Declare schemas using convenient type singletons instead of manual `Schema`/`Field` construction:
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { str, bytes, int, int32, float, float32, bool } from "vgi-rpc";
|
|
184
|
+
|
|
185
|
+
// Shorthand
|
|
186
|
+
protocol.unary("echo", {
|
|
187
|
+
params: { name: str, count: int, value: float },
|
|
188
|
+
result: { result: str },
|
|
189
|
+
handler: ({ name }) => ({ result: name }),
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Equivalent verbose form
|
|
193
|
+
import { Schema, Field, Utf8, Int64, Float64 } from "apache-arrow";
|
|
194
|
+
|
|
195
|
+
protocol.unary("echo", {
|
|
196
|
+
params: new Schema([
|
|
197
|
+
new Field("name", new Utf8(), false),
|
|
198
|
+
new Field("count", new Int64(), false),
|
|
199
|
+
new Field("value", new Float64(), false),
|
|
200
|
+
]),
|
|
201
|
+
result: new Schema([new Field("result", new Utf8(), false)]),
|
|
202
|
+
handler: ({ name }) => ({ result: name }),
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Type singletons
|
|
207
|
+
|
|
208
|
+
| Singleton | Arrow Type | Python equivalent |
|
|
209
|
+
|-----------|-----------|-------------------|
|
|
210
|
+
| `str` | Utf8 | `str` |
|
|
211
|
+
| `bytes` | Binary | `bytes` |
|
|
212
|
+
| `int` | Int64 | `int` |
|
|
213
|
+
| `int32` | Int32 | `Annotated[int, ArrowType(pa.int32())]` |
|
|
214
|
+
| `float` | Float64 | `float` |
|
|
215
|
+
| `float32` | Float32 | `Annotated[float, ArrowType(pa.float32())]` |
|
|
216
|
+
| `bool` | Bool | `bool` |
|
|
217
|
+
|
|
218
|
+
For complex types (List, Map, Dictionary, nullable fields), use the full `Schema`/`Field` constructors from `apache-arrow`.
|
|
219
|
+
|
|
220
|
+
## Emitting Output
|
|
221
|
+
|
|
222
|
+
The `OutputCollector` provides three ways to emit data:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
// Column arrays — most efficient for multi-row batches
|
|
226
|
+
out.emit({ name: ["alice", "bob"], value: [1.0, 2.0] });
|
|
227
|
+
|
|
228
|
+
// Single-row convenience
|
|
229
|
+
out.emitRow({ name: "alice", value: 1.0 });
|
|
230
|
+
|
|
231
|
+
// Pre-built RecordBatch
|
|
232
|
+
out.emit(batch);
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Int64 columns automatically coerce JavaScript Numbers to BigInt.
|
|
236
|
+
|
|
237
|
+
## Client-Directed Logging
|
|
238
|
+
|
|
239
|
+
Handler functions can emit log messages that travel over the wire to the client:
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
protocol.unary("process", {
|
|
243
|
+
params: { data: str },
|
|
244
|
+
result: { result: str },
|
|
245
|
+
handler: (params, ctx) => {
|
|
246
|
+
ctx.clientLog("INFO", `Processing: ${params.data}`);
|
|
247
|
+
ctx.clientLog("DEBUG", "Transform complete", { detail: "extra info" });
|
|
248
|
+
return { result: params.data.toUpperCase() };
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
In streaming methods, use `out.clientLog()`:
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
produce: (state, out) => {
|
|
257
|
+
out.clientLog("INFO", `Producing batch ${state.current}`);
|
|
258
|
+
out.emitRow({ value: state.current });
|
|
259
|
+
state.current++;
|
|
260
|
+
},
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Error Handling
|
|
264
|
+
|
|
265
|
+
Exceptions thrown in handlers are propagated to the client as `RpcError`:
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
handler: async ({ a, b }) => {
|
|
269
|
+
if (b === 0) throw new Error("Division by zero");
|
|
270
|
+
return { result: a / b };
|
|
271
|
+
},
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Errors are transmitted as zero-row Arrow batches with `EXCEPTION`-level metadata. The transport remains clean for subsequent requests.
|
|
275
|
+
|
|
276
|
+
## Testing with the Python CLI
|
|
277
|
+
|
|
278
|
+
The [vgi-rpc CLI](https://github.com/rustyconover/vgi-rpc) can introspect and call methods on any TypeScript server:
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
pip install vgi-rpc[cli]
|
|
282
|
+
|
|
283
|
+
# Describe the service
|
|
284
|
+
vgi-rpc --cmd "bun run examples/calculator.ts" describe
|
|
285
|
+
|
|
286
|
+
# Unary call
|
|
287
|
+
vgi-rpc --cmd "bun run examples/calculator.ts" call add a=2.0 b=3.0
|
|
288
|
+
|
|
289
|
+
# Producer stream
|
|
290
|
+
vgi-rpc --cmd "bun run examples/streaming.ts" call count limit=5 --format table
|
|
291
|
+
|
|
292
|
+
# Exchange stream
|
|
293
|
+
vgi-rpc --cmd "bun run examples/streaming.ts" call scale factor=2.0
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## Wire Protocol Compatibility
|
|
297
|
+
|
|
298
|
+
This library implements the same wire protocol as the Python [vgi-rpc](https://github.com/rustyconover/vgi-rpc) framework:
|
|
299
|
+
|
|
300
|
+
- Multiple sequential Arrow IPC streams on stdin/stdout
|
|
301
|
+
- Request batches carry method name and version in batch metadata
|
|
302
|
+
- Lockstep streaming: one output batch per input batch
|
|
303
|
+
- Zero-row batches for log messages and errors
|
|
304
|
+
- `__describe__` introspection method for cross-language service discovery
|
|
305
|
+
|
|
306
|
+
See the Python README's [Wire Protocol Specification](https://github.com/rustyconover/vgi-rpc#wire-protocol-specification) for the full protocol details.
|
|
307
|
+
|
|
308
|
+
## Examples
|
|
309
|
+
|
|
310
|
+
| Example | Description |
|
|
311
|
+
|---------|-------------|
|
|
312
|
+
| [`calculator.ts`](examples/calculator.ts) | Unary methods: add, multiply, divide |
|
|
313
|
+
| [`greeter.ts`](examples/greeter.ts) | String parameters and results |
|
|
314
|
+
| [`streaming.ts`](examples/streaming.ts) | Producer and exchange stream patterns |
|
|
315
|
+
| [`conformance.ts`](examples/conformance.ts) | 46-method reference service for wire-protocol conformance testing |
|
|
316
|
+
|
|
317
|
+
## Development
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
# Install dependencies
|
|
321
|
+
bun install
|
|
322
|
+
|
|
323
|
+
# Run tests
|
|
324
|
+
bun test
|
|
325
|
+
|
|
326
|
+
# Build (types + bundle)
|
|
327
|
+
bun run build
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## License
|
|
331
|
+
|
|
332
|
+
Copyright 2026 Query.Farm LLC [https://query.farm](https://query.farm). All rights reserved. See [LICENSE.md](LICENSE.md).
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ServiceDescription } from "./introspect.js";
|
|
2
|
+
import type { HttpConnectOptions, StreamSession } from "./types.js";
|
|
3
|
+
export interface RpcClient {
|
|
4
|
+
call(method: string, params?: Record<string, any>): Promise<Record<string, any> | null>;
|
|
5
|
+
stream(method: string, params?: Record<string, any>): Promise<StreamSession>;
|
|
6
|
+
describe(): Promise<ServiceDescription>;
|
|
7
|
+
close(): void;
|
|
8
|
+
}
|
|
9
|
+
export declare function httpConnect(baseUrl: string, options?: HttpConnectOptions): RpcClient;
|
|
10
|
+
//# sourceMappingURL=connect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/client/connect.ts"],"names":[],"mappings":"AAaA,OAAO,EAAmC,KAAK,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE3F,OAAO,KAAK,EAAE,kBAAkB,EAAc,aAAa,EAAE,MAAM,YAAY,CAAC;AAKhF,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;IACxF,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAC7E,QAAQ,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACxC,KAAK,IAAI,IAAI,CAAC;CACf;AAED,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,kBAAkB,GAC3B,SAAS,CAuRX"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { httpConnect, type RpcClient } from "./connect.js";
|
|
2
|
+
export { httpIntrospect, parseDescribeResponse, type ServiceDescription, type MethodInfo } from "./introspect.js";
|
|
3
|
+
export { HttpStreamSession } from "./stream.js";
|
|
4
|
+
export { pipeConnect, subprocessConnect, PipeStreamSession } from "./pipe.js";
|
|
5
|
+
export { type HttpConnectOptions, type LogMessage, type StreamSession, type PipeConnectOptions, type SubprocessConnectOptions, } from "./types.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,qBAAqB,EAAE,KAAK,kBAAkB,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,wBAAwB,GAC9B,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { RecordBatch, type Schema } from "apache-arrow";
|
|
2
|
+
import type { LogMessage } from "./types.js";
|
|
3
|
+
export interface MethodInfo {
|
|
4
|
+
name: string;
|
|
5
|
+
type: "unary" | "stream";
|
|
6
|
+
paramsSchema: Schema;
|
|
7
|
+
resultSchema: Schema;
|
|
8
|
+
inputSchema?: Schema;
|
|
9
|
+
outputSchema?: Schema;
|
|
10
|
+
headerSchema?: Schema;
|
|
11
|
+
doc?: string;
|
|
12
|
+
paramTypes?: Record<string, string>;
|
|
13
|
+
defaults?: Record<string, any>;
|
|
14
|
+
}
|
|
15
|
+
export interface ServiceDescription {
|
|
16
|
+
protocolName: string;
|
|
17
|
+
methods: MethodInfo[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parse a __describe__ response from batches into a ServiceDescription.
|
|
21
|
+
* Reusable across transports (HTTP, pipe, subprocess).
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseDescribeResponse(batches: RecordBatch[], onLog?: (msg: LogMessage) => void): Promise<ServiceDescription>;
|
|
24
|
+
/**
|
|
25
|
+
* Send a __describe__ request and return a ServiceDescription.
|
|
26
|
+
*/
|
|
27
|
+
export declare function httpIntrospect(baseUrl: string, options?: {
|
|
28
|
+
prefix?: string;
|
|
29
|
+
}): Promise<ServiceDescription>;
|
|
30
|
+
//# sourceMappingURL=introspect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"introspect.d.ts","sourceRoot":"","sources":["../../src/client/introspect.ts"],"names":[],"mappings":"AAGA,OAAO,EAAqB,WAAW,EAAE,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AAS3E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,OAAO,GAAG,QAAQ,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,kBAAkB;IACjC,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,UAAU,EAAE,CAAC;CACvB;AASD;;;GAGG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,WAAW,EAAE,EACtB,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,IAAI,GAChC,OAAO,CAAC,kBAAkB,CAAC,CAoE7B;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAC5B,OAAO,CAAC,kBAAkB,CAAC,CAe7B"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { RecordBatch, Schema, DataType } from "apache-arrow";
|
|
2
|
+
import { IpcStreamReader } from "../wire/reader.js";
|
|
3
|
+
import type { LogMessage } from "./types.js";
|
|
4
|
+
/** Infer an Arrow DataType from a JS value. */
|
|
5
|
+
export declare function inferArrowType(value: any): DataType;
|
|
6
|
+
/**
|
|
7
|
+
* Build a 1-row Arrow IPC request batch with method metadata.
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildRequestIpc(schema: Schema, params: Record<string, any>, method: string): Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* Read schema + all batches from an IPC stream body.
|
|
12
|
+
*/
|
|
13
|
+
export declare function readResponseBatches(body: Uint8Array): Promise<{
|
|
14
|
+
schema: Schema;
|
|
15
|
+
batches: RecordBatch[];
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Check if a zero-row batch carries log/error metadata.
|
|
19
|
+
* If EXCEPTION → throw RpcError.
|
|
20
|
+
* If other level → call onLog.
|
|
21
|
+
* Returns true if the batch was consumed as a log/error.
|
|
22
|
+
*/
|
|
23
|
+
export declare function dispatchLogOrError(batch: RecordBatch, onLog?: (msg: LogMessage) => void): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Extract all rows from a batch as Record<string, any>[].
|
|
26
|
+
* Converts BigInt to Number when safe.
|
|
27
|
+
*/
|
|
28
|
+
export declare function extractBatchRows(batch: RecordBatch): Record<string, any>[];
|
|
29
|
+
/**
|
|
30
|
+
* Read sequential IPC streams from a response body.
|
|
31
|
+
* Returns an IpcStreamReader for reading header + data streams.
|
|
32
|
+
*/
|
|
33
|
+
export declare function readSequentialStreams(body: Uint8Array): Promise<IpcStreamReader>;
|
|
34
|
+
//# sourceMappingURL=ipc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipc.d.ts","sourceRoot":"","sources":["../../src/client/ipc.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,WAAW,EAEX,MAAM,EACN,QAAQ,EAST,MAAM,cAAc,CAAC;AAWtB,OAAO,EAAE,eAAe,EAAsB,MAAM,mBAAmB,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,+CAA+C;AAC/C,wBAAgB,cAAc,CAAC,KAAK,EAAE,GAAG,GAAG,QAAQ,CAOnD;AAyCD;;GAEG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3B,MAAM,EAAE,MAAM,GACb,UAAU,CAgCZ;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,UAAU,GACf,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,WAAW,EAAE,CAAA;CAAE,CAAC,CASrD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,WAAW,EAClB,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,IAAI,GAChC,OAAO,CAqCT;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAoB1E;AAED;;;GAGG;AACH,wBAAsB,qBAAqB,CACzC,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,eAAe,CAAC,CAQ1B"}
|