tywrap 0.2.0 → 0.2.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.
- package/README.md +1 -1
- package/dist/index.d.ts +20 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime/base.d.ts +17 -7
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +18 -1
- package/dist/runtime/base.js.map +1 -1
- package/dist/runtime/bounded-context.d.ts +252 -0
- package/dist/runtime/bounded-context.d.ts.map +1 -0
- package/dist/runtime/bounded-context.js +454 -0
- package/dist/runtime/bounded-context.js.map +1 -0
- package/dist/runtime/bridge-protocol.d.ts +167 -0
- package/dist/runtime/bridge-protocol.d.ts.map +1 -0
- package/dist/runtime/bridge-protocol.js +247 -0
- package/dist/runtime/bridge-protocol.js.map +1 -0
- package/dist/runtime/disposable.d.ts +40 -0
- package/dist/runtime/disposable.d.ts.map +1 -0
- package/dist/runtime/disposable.js +49 -0
- package/dist/runtime/disposable.js.map +1 -0
- package/dist/runtime/http-io.d.ts +91 -0
- package/dist/runtime/http-io.d.ts.map +1 -0
- package/dist/runtime/http-io.js +195 -0
- package/dist/runtime/http-io.js.map +1 -0
- package/dist/runtime/http.d.ts +47 -13
- package/dist/runtime/http.d.ts.map +1 -1
- package/dist/runtime/http.js +55 -74
- package/dist/runtime/http.js.map +1 -1
- package/dist/runtime/node.d.ts +97 -130
- package/dist/runtime/node.d.ts.map +1 -1
- package/dist/runtime/node.js +256 -523
- package/dist/runtime/node.js.map +1 -1
- package/dist/runtime/pooled-transport.d.ts +131 -0
- package/dist/runtime/pooled-transport.d.ts.map +1 -0
- package/dist/runtime/pooled-transport.js +175 -0
- package/dist/runtime/pooled-transport.js.map +1 -0
- package/dist/runtime/process-io.d.ts +204 -0
- package/dist/runtime/process-io.d.ts.map +1 -0
- package/dist/runtime/process-io.js +695 -0
- package/dist/runtime/process-io.js.map +1 -0
- package/dist/runtime/pyodide-io.d.ts +155 -0
- package/dist/runtime/pyodide-io.d.ts.map +1 -0
- package/dist/runtime/pyodide-io.js +397 -0
- package/dist/runtime/pyodide-io.js.map +1 -0
- package/dist/runtime/pyodide.d.ts +51 -19
- package/dist/runtime/pyodide.d.ts.map +1 -1
- package/dist/runtime/pyodide.js +57 -186
- package/dist/runtime/pyodide.js.map +1 -1
- package/dist/runtime/safe-codec.d.ts +81 -0
- package/dist/runtime/safe-codec.d.ts.map +1 -0
- package/dist/runtime/safe-codec.js +345 -0
- package/dist/runtime/safe-codec.js.map +1 -0
- package/dist/runtime/transport.d.ts +186 -0
- package/dist/runtime/transport.d.ts.map +1 -0
- package/dist/runtime/transport.js +86 -0
- package/dist/runtime/transport.js.map +1 -0
- package/dist/runtime/validators.d.ts +131 -0
- package/dist/runtime/validators.d.ts.map +1 -0
- package/dist/runtime/validators.js +219 -0
- package/dist/runtime/validators.js.map +1 -0
- package/dist/runtime/worker-pool.d.ts +196 -0
- package/dist/runtime/worker-pool.d.ts.map +1 -0
- package/dist/runtime/worker-pool.js +371 -0
- package/dist/runtime/worker-pool.js.map +1 -0
- package/dist/utils/codec.d.ts.map +1 -1
- package/dist/utils/codec.js +120 -1
- package/dist/utils/codec.js.map +1 -1
- package/package.json +2 -2
- package/runtime/python_bridge.py +30 -3
- package/runtime/safe_codec.py +344 -0
- package/src/index.ts +48 -5
- package/src/runtime/base.ts +18 -26
- package/src/runtime/bounded-context.ts +608 -0
- package/src/runtime/bridge-protocol.ts +319 -0
- package/src/runtime/disposable.ts +65 -0
- package/src/runtime/http-io.ts +244 -0
- package/src/runtime/http.ts +71 -117
- package/src/runtime/node.ts +358 -691
- package/src/runtime/pooled-transport.ts +252 -0
- package/src/runtime/process-io.ts +902 -0
- package/src/runtime/pyodide-io.ts +485 -0
- package/src/runtime/pyodide.ts +75 -215
- package/src/runtime/safe-codec.ts +443 -0
- package/src/runtime/transport.ts +273 -0
- package/src/runtime/validators.ts +241 -0
- package/src/runtime/worker-pool.ts +498 -0
- package/src/utils/codec.ts +126 -1
package/src/runtime/http.ts
CHANGED
|
@@ -1,132 +1,86 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* HTTP runtime bridge
|
|
2
|
+
* HTTP runtime bridge for BridgeProtocol.
|
|
3
|
+
*
|
|
4
|
+
* HttpBridge extends BridgeProtocol and uses HttpIO transport for
|
|
5
|
+
* stateless HTTP POST-based communication with a Python server.
|
|
6
|
+
*
|
|
7
|
+
* @see https://github.com/bbopen/tywrap/issues/149
|
|
3
8
|
*/
|
|
4
9
|
|
|
5
|
-
import {
|
|
10
|
+
import { BridgeProtocol, type BridgeProtocolOptions } from './bridge-protocol.js';
|
|
11
|
+
import { HttpIO } from './http-io.js';
|
|
12
|
+
import type { CodecOptions } from './safe-codec.js';
|
|
6
13
|
|
|
7
|
-
|
|
14
|
+
// =============================================================================
|
|
15
|
+
// OPTIONS
|
|
16
|
+
// =============================================================================
|
|
8
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Configuration options for HttpBridge.
|
|
20
|
+
*/
|
|
9
21
|
export interface HttpBridgeOptions {
|
|
22
|
+
/** Base URL for the Python server (e.g., 'http://localhost:8000') */
|
|
10
23
|
baseURL: string;
|
|
11
|
-
headers?: Record<string, string>;
|
|
12
|
-
timeoutMs?: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface HttpCallPayload {
|
|
16
|
-
module: string;
|
|
17
|
-
functionName: string;
|
|
18
|
-
args: unknown[];
|
|
19
|
-
kwargs?: Record<string, unknown>;
|
|
20
|
-
}
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
className: string;
|
|
25
|
-
args: unknown[];
|
|
26
|
-
kwargs?: Record<string, unknown>;
|
|
27
|
-
}
|
|
25
|
+
/** Additional headers to include in each request */
|
|
26
|
+
headers?: Record<string, string>;
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
methodName: string;
|
|
32
|
-
args: unknown[];
|
|
33
|
-
kwargs?: Record<string, unknown>;
|
|
34
|
-
}
|
|
28
|
+
/** Timeout in ms for requests. Default: 30000 (30 seconds) */
|
|
29
|
+
timeoutMs?: number;
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
/** Codec options for validation/serialization */
|
|
32
|
+
codec?: CodecOptions;
|
|
38
33
|
}
|
|
39
34
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
private readonly timeoutMs: number;
|
|
44
|
-
|
|
45
|
-
constructor(options: HttpBridgeOptions = { baseURL: 'http://localhost:8000' }) {
|
|
46
|
-
super();
|
|
47
|
-
this.baseURL = options.baseURL.replace(/\/$/, '');
|
|
48
|
-
this.headers = { 'content-type': 'application/json', ...(options.headers ?? {}) };
|
|
49
|
-
this.timeoutMs = options.timeoutMs ?? 30000;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async call<T = unknown>(
|
|
53
|
-
module: string,
|
|
54
|
-
functionName: string,
|
|
55
|
-
args: unknown[],
|
|
56
|
-
kwargs?: Record<string, unknown>
|
|
57
|
-
): Promise<T> {
|
|
58
|
-
const payload: HttpCallPayload = { module, functionName, args, kwargs };
|
|
59
|
-
const res = await this.post(`${this.baseURL}/call`, payload);
|
|
60
|
-
return (await decodeValueAsync(res)) as T;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async instantiate<T = unknown>(
|
|
64
|
-
module: string,
|
|
65
|
-
className: string,
|
|
66
|
-
args: unknown[],
|
|
67
|
-
kwargs?: Record<string, unknown>
|
|
68
|
-
): Promise<T> {
|
|
69
|
-
const payload: HttpInstantiatePayload = { module, className, args, kwargs };
|
|
70
|
-
const res = await this.post(`${this.baseURL}/instantiate`, payload);
|
|
71
|
-
return (await decodeValueAsync(res)) as T;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async callMethod<T = unknown>(
|
|
75
|
-
handle: string,
|
|
76
|
-
methodName: string,
|
|
77
|
-
args: unknown[],
|
|
78
|
-
kwargs?: Record<string, unknown>
|
|
79
|
-
): Promise<T> {
|
|
80
|
-
const payload: HttpCallMethodPayload = { handle, methodName, args, kwargs };
|
|
81
|
-
const res = await this.post(`${this.baseURL}/call_method`, payload);
|
|
82
|
-
return (await decodeValueAsync(res)) as T;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async disposeInstance(handle: string): Promise<void> {
|
|
86
|
-
const payload: HttpDisposePayload = { handle };
|
|
87
|
-
await this.post(`${this.baseURL}/dispose_instance`, payload);
|
|
88
|
-
}
|
|
35
|
+
// =============================================================================
|
|
36
|
+
// HTTP BRIDGE
|
|
37
|
+
// =============================================================================
|
|
89
38
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
39
|
+
/**
|
|
40
|
+
* HTTP-based runtime bridge for executing Python code.
|
|
41
|
+
*
|
|
42
|
+
* HttpBridge provides a stateless HTTP transport for communication with
|
|
43
|
+
* a Python server. Each request is independent - no connection state is
|
|
44
|
+
* maintained between calls.
|
|
45
|
+
*
|
|
46
|
+
* Features:
|
|
47
|
+
* - Stateless HTTP POST communication
|
|
48
|
+
* - Timeout handling via AbortController
|
|
49
|
+
* - Full SafeCodec validation (NaN/Infinity rejection, key validation)
|
|
50
|
+
* - Automatic Arrow decoding for DataFrames/ndarrays
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const bridge = new HttpBridge({ baseURL: 'http://localhost:8000' });
|
|
55
|
+
* await bridge.init();
|
|
56
|
+
*
|
|
57
|
+
* const result = await bridge.call('math', 'sqrt', [16]);
|
|
58
|
+
* console.log(result); // 4.0
|
|
59
|
+
*
|
|
60
|
+
* await bridge.dispose();
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export class HttpBridge extends BridgeProtocol {
|
|
64
|
+
/**
|
|
65
|
+
* Create a new HttpBridge instance.
|
|
66
|
+
*
|
|
67
|
+
* @param options - Configuration options for the bridge
|
|
68
|
+
*/
|
|
69
|
+
constructor(options: HttpBridgeOptions) {
|
|
70
|
+
// Create HTTP transport
|
|
71
|
+
const transport = new HttpIO({
|
|
72
|
+
baseURL: options.baseURL,
|
|
73
|
+
headers: options.headers,
|
|
74
|
+
defaultTimeoutMs: options.timeoutMs,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Initialize BridgeProtocol with transport and codec options
|
|
78
|
+
const protocolOptions: BridgeProtocolOptions = {
|
|
79
|
+
transport,
|
|
80
|
+
codec: options.codec,
|
|
81
|
+
defaultTimeoutMs: options.timeoutMs,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
super(protocolOptions);
|
|
131
85
|
}
|
|
132
86
|
}
|