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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +319 -0
  3. package/dist/cli.d.ts +3 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +263 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/config/index.d.ts +41 -0
  8. package/dist/config/index.d.ts.map +1 -0
  9. package/dist/config/index.js +234 -0
  10. package/dist/config/index.js.map +1 -0
  11. package/dist/core/analyzer.d.ts +63 -0
  12. package/dist/core/analyzer.d.ts.map +1 -0
  13. package/dist/core/analyzer.js +676 -0
  14. package/dist/core/analyzer.js.map +1 -0
  15. package/dist/core/discovery.d.ts +85 -0
  16. package/dist/core/discovery.d.ts.map +1 -0
  17. package/dist/core/discovery.js +344 -0
  18. package/dist/core/discovery.js.map +1 -0
  19. package/dist/core/generator.d.ts +37 -0
  20. package/dist/core/generator.d.ts.map +1 -0
  21. package/dist/core/generator.js +368 -0
  22. package/dist/core/generator.js.map +1 -0
  23. package/dist/core/mapper.d.ts +40 -0
  24. package/dist/core/mapper.d.ts.map +1 -0
  25. package/dist/core/mapper.js +401 -0
  26. package/dist/core/mapper.js.map +1 -0
  27. package/dist/core/validation.d.ts +79 -0
  28. package/dist/core/validation.d.ts.map +1 -0
  29. package/dist/core/validation.js +486 -0
  30. package/dist/core/validation.js.map +1 -0
  31. package/dist/index.d.ts +26 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +29 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/runtime/base.d.ts +12 -0
  36. package/dist/runtime/base.d.ts.map +1 -0
  37. package/dist/runtime/base.js +6 -0
  38. package/dist/runtime/base.js.map +1 -0
  39. package/dist/runtime/errors.d.ts +17 -0
  40. package/dist/runtime/errors.d.ts.map +1 -0
  41. package/dist/runtime/errors.js +21 -0
  42. package/dist/runtime/errors.js.map +1 -0
  43. package/dist/runtime/http.d.ts +22 -0
  44. package/dist/runtime/http.d.ts.map +1 -0
  45. package/dist/runtime/http.js +79 -0
  46. package/dist/runtime/http.js.map +1 -0
  47. package/dist/runtime/index.d.ts +8 -0
  48. package/dist/runtime/index.d.ts.map +1 -0
  49. package/dist/runtime/index.js +17 -0
  50. package/dist/runtime/index.js.map +1 -0
  51. package/dist/runtime/node.d.ts +48 -0
  52. package/dist/runtime/node.d.ts.map +1 -0
  53. package/dist/runtime/node.js +328 -0
  54. package/dist/runtime/node.js.map +1 -0
  55. package/dist/runtime/optimized-node.d.ts +131 -0
  56. package/dist/runtime/optimized-node.d.ts.map +1 -0
  57. package/dist/runtime/optimized-node.js +622 -0
  58. package/dist/runtime/optimized-node.js.map +1 -0
  59. package/dist/runtime/protocol.d.ts +3 -0
  60. package/dist/runtime/protocol.d.ts.map +1 -0
  61. package/dist/runtime/protocol.js +3 -0
  62. package/dist/runtime/protocol.js.map +1 -0
  63. package/dist/runtime/pyodide.d.ts +27 -0
  64. package/dist/runtime/pyodide.d.ts.map +1 -0
  65. package/dist/runtime/pyodide.js +191 -0
  66. package/dist/runtime/pyodide.js.map +1 -0
  67. package/dist/tools/python_suite.js +173 -0
  68. package/dist/tools/python_suite.js.map +1 -0
  69. package/dist/types/index.d.ts +299 -0
  70. package/dist/types/index.d.ts.map +1 -0
  71. package/dist/types/index.js +5 -0
  72. package/dist/types/index.js.map +1 -0
  73. package/dist/tywrap.d.ts +29 -0
  74. package/dist/tywrap.d.ts.map +1 -0
  75. package/dist/tywrap.js +589 -0
  76. package/dist/tywrap.js.map +1 -0
  77. package/dist/utils/bundle-optimizer.d.ts +182 -0
  78. package/dist/utils/bundle-optimizer.d.ts.map +1 -0
  79. package/dist/utils/bundle-optimizer.js +680 -0
  80. package/dist/utils/bundle-optimizer.js.map +1 -0
  81. package/dist/utils/cache.d.ts +149 -0
  82. package/dist/utils/cache.d.ts.map +1 -0
  83. package/dist/utils/cache.js +492 -0
  84. package/dist/utils/cache.js.map +1 -0
  85. package/dist/utils/codec.d.ts +94 -0
  86. package/dist/utils/codec.d.ts.map +1 -0
  87. package/dist/utils/codec.js +150 -0
  88. package/dist/utils/codec.js.map +1 -0
  89. package/dist/utils/logger.d.ts +42 -0
  90. package/dist/utils/logger.d.ts.map +1 -0
  91. package/dist/utils/logger.js +140 -0
  92. package/dist/utils/logger.js.map +1 -0
  93. package/dist/utils/memory-profiler.d.ts +123 -0
  94. package/dist/utils/memory-profiler.d.ts.map +1 -0
  95. package/dist/utils/memory-profiler.js +426 -0
  96. package/dist/utils/memory-profiler.js.map +1 -0
  97. package/dist/utils/parallel-processor.d.ts +146 -0
  98. package/dist/utils/parallel-processor.d.ts.map +1 -0
  99. package/dist/utils/parallel-processor.js +636 -0
  100. package/dist/utils/parallel-processor.js.map +1 -0
  101. package/dist/utils/python.d.ts +8 -0
  102. package/dist/utils/python.d.ts.map +1 -0
  103. package/dist/utils/python.js +56 -0
  104. package/dist/utils/python.js.map +1 -0
  105. package/dist/utils/runtime.d.ts +153 -0
  106. package/dist/utils/runtime.d.ts.map +1 -0
  107. package/dist/utils/runtime.js +519 -0
  108. package/dist/utils/runtime.js.map +1 -0
  109. package/package.json +136 -0
  110. package/runtime/python_bridge.py +476 -0
  111. package/src/cli.ts +331 -0
  112. package/src/config/index.ts +289 -0
  113. package/src/core/analyzer.ts +788 -0
  114. package/src/core/discovery.ts +415 -0
  115. package/src/core/generator.ts +447 -0
  116. package/src/core/mapper.ts +504 -0
  117. package/src/core/validation.ts +693 -0
  118. package/src/index.ts +83 -0
  119. package/src/runtime/base.ts +32 -0
  120. package/src/runtime/errors.ts +19 -0
  121. package/src/runtime/http.ts +132 -0
  122. package/src/runtime/index.ts +24 -0
  123. package/src/runtime/node.ts +431 -0
  124. package/src/runtime/optimized-node.ts +838 -0
  125. package/src/runtime/protocol.ts +2 -0
  126. package/src/runtime/pyodide.ts +228 -0
  127. package/src/types/global.d.ts +60 -0
  128. package/src/types/index.ts +410 -0
  129. package/src/tywrap.ts +669 -0
  130. package/src/utils/bundle-optimizer.ts +876 -0
  131. package/src/utils/cache.ts +634 -0
  132. package/src/utils/codec.ts +275 -0
  133. package/src/utils/logger.ts +201 -0
  134. package/src/utils/memory-profiler.ts +582 -0
  135. package/src/utils/parallel-processor.ts +879 -0
  136. package/src/utils/python.ts +79 -0
  137. 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
+ }