tywrap 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 +21 -0
- package/README.md +319 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +263 -0
- package/dist/cli.js.map +1 -0
- package/dist/config/index.d.ts +41 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +234 -0
- package/dist/config/index.js.map +1 -0
- package/dist/core/analyzer.d.ts +63 -0
- package/dist/core/analyzer.d.ts.map +1 -0
- package/dist/core/analyzer.js +676 -0
- package/dist/core/analyzer.js.map +1 -0
- package/dist/core/discovery.d.ts +85 -0
- package/dist/core/discovery.d.ts.map +1 -0
- package/dist/core/discovery.js +344 -0
- package/dist/core/discovery.js.map +1 -0
- package/dist/core/generator.d.ts +37 -0
- package/dist/core/generator.d.ts.map +1 -0
- package/dist/core/generator.js +368 -0
- package/dist/core/generator.js.map +1 -0
- package/dist/core/mapper.d.ts +40 -0
- package/dist/core/mapper.d.ts.map +1 -0
- package/dist/core/mapper.js +401 -0
- package/dist/core/mapper.js.map +1 -0
- package/dist/core/validation.d.ts +79 -0
- package/dist/core/validation.d.ts.map +1 -0
- package/dist/core/validation.js +486 -0
- package/dist/core/validation.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/base.d.ts +12 -0
- package/dist/runtime/base.d.ts.map +1 -0
- package/dist/runtime/base.js +6 -0
- package/dist/runtime/base.js.map +1 -0
- package/dist/runtime/errors.d.ts +17 -0
- package/dist/runtime/errors.d.ts.map +1 -0
- package/dist/runtime/errors.js +21 -0
- package/dist/runtime/errors.js.map +1 -0
- package/dist/runtime/http.d.ts +22 -0
- package/dist/runtime/http.d.ts.map +1 -0
- package/dist/runtime/http.js +79 -0
- package/dist/runtime/http.js.map +1 -0
- package/dist/runtime/index.d.ts +8 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +17 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/node.d.ts +48 -0
- package/dist/runtime/node.d.ts.map +1 -0
- package/dist/runtime/node.js +328 -0
- package/dist/runtime/node.js.map +1 -0
- package/dist/runtime/optimized-node.d.ts +131 -0
- package/dist/runtime/optimized-node.d.ts.map +1 -0
- package/dist/runtime/optimized-node.js +622 -0
- package/dist/runtime/optimized-node.js.map +1 -0
- package/dist/runtime/protocol.d.ts +3 -0
- package/dist/runtime/protocol.d.ts.map +1 -0
- package/dist/runtime/protocol.js +3 -0
- package/dist/runtime/protocol.js.map +1 -0
- package/dist/runtime/pyodide.d.ts +27 -0
- package/dist/runtime/pyodide.d.ts.map +1 -0
- package/dist/runtime/pyodide.js +191 -0
- package/dist/runtime/pyodide.js.map +1 -0
- package/dist/tools/python_suite.js +173 -0
- package/dist/tools/python_suite.js.map +1 -0
- package/dist/types/index.d.ts +299 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +5 -0
- package/dist/types/index.js.map +1 -0
- package/dist/tywrap.d.ts +29 -0
- package/dist/tywrap.d.ts.map +1 -0
- package/dist/tywrap.js +589 -0
- package/dist/tywrap.js.map +1 -0
- package/dist/utils/bundle-optimizer.d.ts +182 -0
- package/dist/utils/bundle-optimizer.d.ts.map +1 -0
- package/dist/utils/bundle-optimizer.js +680 -0
- package/dist/utils/bundle-optimizer.js.map +1 -0
- package/dist/utils/cache.d.ts +149 -0
- package/dist/utils/cache.d.ts.map +1 -0
- package/dist/utils/cache.js +492 -0
- package/dist/utils/cache.js.map +1 -0
- package/dist/utils/codec.d.ts +94 -0
- package/dist/utils/codec.d.ts.map +1 -0
- package/dist/utils/codec.js +150 -0
- package/dist/utils/codec.js.map +1 -0
- package/dist/utils/logger.d.ts +42 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +140 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/memory-profiler.d.ts +123 -0
- package/dist/utils/memory-profiler.d.ts.map +1 -0
- package/dist/utils/memory-profiler.js +426 -0
- package/dist/utils/memory-profiler.js.map +1 -0
- package/dist/utils/parallel-processor.d.ts +146 -0
- package/dist/utils/parallel-processor.d.ts.map +1 -0
- package/dist/utils/parallel-processor.js +636 -0
- package/dist/utils/parallel-processor.js.map +1 -0
- package/dist/utils/python.d.ts +8 -0
- package/dist/utils/python.d.ts.map +1 -0
- package/dist/utils/python.js +56 -0
- package/dist/utils/python.js.map +1 -0
- package/dist/utils/runtime.d.ts +153 -0
- package/dist/utils/runtime.d.ts.map +1 -0
- package/dist/utils/runtime.js +519 -0
- package/dist/utils/runtime.js.map +1 -0
- package/package.json +136 -0
- package/runtime/python_bridge.py +476 -0
- package/src/cli.ts +331 -0
- package/src/config/index.ts +289 -0
- package/src/core/analyzer.ts +788 -0
- package/src/core/discovery.ts +415 -0
- package/src/core/generator.ts +447 -0
- package/src/core/mapper.ts +504 -0
- package/src/core/validation.ts +693 -0
- package/src/index.ts +83 -0
- package/src/runtime/base.ts +32 -0
- package/src/runtime/errors.ts +19 -0
- package/src/runtime/http.ts +132 -0
- package/src/runtime/index.ts +24 -0
- package/src/runtime/node.ts +431 -0
- package/src/runtime/optimized-node.ts +838 -0
- package/src/runtime/protocol.ts +2 -0
- package/src/runtime/pyodide.ts +228 -0
- package/src/types/global.d.ts +60 -0
- package/src/types/index.ts +410 -0
- package/src/tywrap.ts +669 -0
- package/src/utils/bundle-optimizer.ts +876 -0
- package/src/utils/cache.ts +634 -0
- package/src/utils/codec.ts +275 -0
- package/src/utils/logger.ts +201 -0
- package/src/utils/memory-profiler.ts +582 -0
- package/src/utils/parallel-processor.ts +879 -0
- package/src/utils/python.ts +79 -0
- package/src/utils/runtime.ts +616 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tywrap - TypeScript wrapper for Python libraries with full type safety
|
|
3
|
+
*
|
|
4
|
+
* @description Build-time code generation system that makes Python libraries
|
|
5
|
+
* feel native in TypeScript with zero runtime overhead
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { tywrap } from './tywrap.js';
|
|
9
|
+
|
|
10
|
+
export type { TywrapConfig } from './config/index.js';
|
|
11
|
+
export { defineConfig, resolveConfig } from './config/index.js';
|
|
12
|
+
export { RuntimeBridge } from './runtime/base.js';
|
|
13
|
+
export {
|
|
14
|
+
BridgeError,
|
|
15
|
+
BridgeProtocolError,
|
|
16
|
+
BridgeTimeoutError,
|
|
17
|
+
BridgeDisposedError,
|
|
18
|
+
BridgeExecutionError,
|
|
19
|
+
} from './runtime/errors.js';
|
|
20
|
+
export { getRuntimeBridge, setRuntimeBridge, clearRuntimeBridge } from './runtime/index.js';
|
|
21
|
+
|
|
22
|
+
// Runtime-specific exports
|
|
23
|
+
export { NodeBridge } from './runtime/node.js';
|
|
24
|
+
export { PyodideBridge } from './runtime/pyodide.js';
|
|
25
|
+
export { HttpBridge } from './runtime/http.js';
|
|
26
|
+
|
|
27
|
+
// Core types
|
|
28
|
+
export type {
|
|
29
|
+
PythonModule,
|
|
30
|
+
PythonFunction,
|
|
31
|
+
PythonClass,
|
|
32
|
+
PythonType,
|
|
33
|
+
PrimitiveType,
|
|
34
|
+
CollectionType,
|
|
35
|
+
UnionType,
|
|
36
|
+
OptionalType,
|
|
37
|
+
CustomType,
|
|
38
|
+
GenericType,
|
|
39
|
+
Parameter,
|
|
40
|
+
Property,
|
|
41
|
+
PythonImport,
|
|
42
|
+
TypescriptType,
|
|
43
|
+
RuntimeStrategy,
|
|
44
|
+
TywrapOptions,
|
|
45
|
+
PythonModuleConfig,
|
|
46
|
+
OutputConfig,
|
|
47
|
+
RuntimeConfig,
|
|
48
|
+
PyodideConfig,
|
|
49
|
+
NodeConfig,
|
|
50
|
+
HttpConfig,
|
|
51
|
+
PerformanceConfig,
|
|
52
|
+
DevelopmentConfig,
|
|
53
|
+
TypeMappingConfig,
|
|
54
|
+
TypePreset,
|
|
55
|
+
BridgeInfo,
|
|
56
|
+
AnalysisResult,
|
|
57
|
+
AnalysisError,
|
|
58
|
+
AnalysisWarning,
|
|
59
|
+
AnalysisStatistics,
|
|
60
|
+
GeneratedCode,
|
|
61
|
+
} from './types/index.js';
|
|
62
|
+
|
|
63
|
+
// Main API
|
|
64
|
+
export { tywrap } from './tywrap.js';
|
|
65
|
+
export { generate } from './tywrap.js';
|
|
66
|
+
|
|
67
|
+
// Runtime detection utilities
|
|
68
|
+
export { detectRuntime, isNodejs, isDeno, isBun, isBrowser } from './utils/runtime.js';
|
|
69
|
+
export {
|
|
70
|
+
decodeValue,
|
|
71
|
+
decodeValueAsync,
|
|
72
|
+
registerArrowDecoder,
|
|
73
|
+
clearArrowDecoder,
|
|
74
|
+
} from './utils/codec.js';
|
|
75
|
+
|
|
76
|
+
// Version info
|
|
77
|
+
export const VERSION = '0.1.0';
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Quick setup function for getting started
|
|
81
|
+
*/
|
|
82
|
+
// Default export for convenience
|
|
83
|
+
export default tywrap;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base runtime bridge
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { RuntimeExecution } from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
export abstract class RuntimeBridge implements RuntimeExecution {
|
|
8
|
+
abstract call<T = unknown>(
|
|
9
|
+
module: string,
|
|
10
|
+
functionName: string,
|
|
11
|
+
args: unknown[],
|
|
12
|
+
kwargs?: Record<string, unknown>
|
|
13
|
+
): Promise<T>;
|
|
14
|
+
|
|
15
|
+
abstract instantiate<T = unknown>(
|
|
16
|
+
module: string,
|
|
17
|
+
className: string,
|
|
18
|
+
args: unknown[],
|
|
19
|
+
kwargs?: Record<string, unknown>
|
|
20
|
+
): Promise<T>;
|
|
21
|
+
|
|
22
|
+
abstract callMethod<T = unknown>(
|
|
23
|
+
handle: string,
|
|
24
|
+
methodName: string,
|
|
25
|
+
args: unknown[],
|
|
26
|
+
kwargs?: Record<string, unknown>
|
|
27
|
+
): Promise<T>;
|
|
28
|
+
|
|
29
|
+
abstract disposeInstance(handle: string): Promise<void>;
|
|
30
|
+
|
|
31
|
+
abstract dispose(): Promise<void>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class BridgeError extends Error {
|
|
2
|
+
code?: string;
|
|
3
|
+
constructor(message: string, options?: { code?: string; cause?: unknown }) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = new.target.name;
|
|
6
|
+
this.code = options?.code;
|
|
7
|
+
if (options?.cause) {
|
|
8
|
+
(this as Error & { cause?: unknown }).cause = options.cause;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class BridgeProtocolError extends BridgeError {}
|
|
14
|
+
export class BridgeTimeoutError extends BridgeError {}
|
|
15
|
+
export class BridgeDisposedError extends BridgeError {}
|
|
16
|
+
|
|
17
|
+
export class BridgeExecutionError extends BridgeError {
|
|
18
|
+
traceback?: string;
|
|
19
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP runtime bridge
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { decodeValueAsync } from '../utils/codec.js';
|
|
6
|
+
|
|
7
|
+
import { RuntimeBridge } from './base.js';
|
|
8
|
+
|
|
9
|
+
export interface HttpBridgeOptions {
|
|
10
|
+
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
|
+
|
|
22
|
+
interface HttpInstantiatePayload {
|
|
23
|
+
module: string;
|
|
24
|
+
className: string;
|
|
25
|
+
args: unknown[];
|
|
26
|
+
kwargs?: Record<string, unknown>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface HttpCallMethodPayload {
|
|
30
|
+
handle: string;
|
|
31
|
+
methodName: string;
|
|
32
|
+
args: unknown[];
|
|
33
|
+
kwargs?: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface HttpDisposePayload {
|
|
37
|
+
handle: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class HttpBridge extends RuntimeBridge {
|
|
41
|
+
private readonly baseURL: string;
|
|
42
|
+
private readonly headers: Record<string, string>;
|
|
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
|
+
}
|
|
89
|
+
|
|
90
|
+
async dispose(): Promise<void> {
|
|
91
|
+
// stateless
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private async post(url: string, body: unknown): Promise<unknown> {
|
|
95
|
+
const controller = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
|
|
96
|
+
const timer = controller ? setTimeout(() => controller.abort(), this.timeoutMs) : undefined;
|
|
97
|
+
try {
|
|
98
|
+
const resp = await fetch(url, {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
headers: this.headers,
|
|
101
|
+
body: JSON.stringify(body),
|
|
102
|
+
signal: controller?.signal,
|
|
103
|
+
});
|
|
104
|
+
if (!resp.ok) {
|
|
105
|
+
const text = await safeText(resp);
|
|
106
|
+
throw new Error(`HTTP ${resp.status}: ${text || resp.statusText}`);
|
|
107
|
+
}
|
|
108
|
+
const ct = resp.headers.get('content-type') ?? '';
|
|
109
|
+
if (ct.includes('application/json')) {
|
|
110
|
+
return (await resp.json()) as unknown;
|
|
111
|
+
}
|
|
112
|
+
const text = await resp.text();
|
|
113
|
+
try {
|
|
114
|
+
return JSON.parse(text) as unknown;
|
|
115
|
+
} catch {
|
|
116
|
+
return text as unknown;
|
|
117
|
+
}
|
|
118
|
+
} finally {
|
|
119
|
+
if (timer) {
|
|
120
|
+
clearTimeout(timer);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function safeText(resp: Response): Promise<string> {
|
|
127
|
+
try {
|
|
128
|
+
return await resp.text();
|
|
129
|
+
} catch {
|
|
130
|
+
return '';
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime bridge registry for generated wrappers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { RuntimeExecution } from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
let runtimeBridge: RuntimeExecution | null = null;
|
|
8
|
+
|
|
9
|
+
export function setRuntimeBridge(bridge: RuntimeExecution): void {
|
|
10
|
+
runtimeBridge = bridge;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getRuntimeBridge(): RuntimeExecution {
|
|
14
|
+
if (!runtimeBridge) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
'No runtime bridge configured. Call setRuntimeBridge(new NodeBridge(...)) or setRuntimeBridge(new PyodideBridge(...)) before using generated modules.'
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
return runtimeBridge;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function clearRuntimeBridge(): void {
|
|
23
|
+
runtimeBridge = null;
|
|
24
|
+
}
|