nodepyx 1.0.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 +22 -0
- package/README.md +399 -0
- package/binding.gyp +73 -0
- package/dist/core/PyCallable.d.ts +65 -0
- package/dist/core/PyCallable.d.ts.map +1 -0
- package/dist/core/PyCallable.js +109 -0
- package/dist/core/PyCallable.js.map +1 -0
- package/dist/core/PyContext.d.ts +76 -0
- package/dist/core/PyContext.d.ts.map +1 -0
- package/dist/core/PyContext.js +228 -0
- package/dist/core/PyContext.js.map +1 -0
- package/dist/core/PyIterator.d.ts +84 -0
- package/dist/core/PyIterator.d.ts.map +1 -0
- package/dist/core/PyIterator.js +243 -0
- package/dist/core/PyIterator.js.map +1 -0
- package/dist/core/PyModule.d.ts +55 -0
- package/dist/core/PyModule.d.ts.map +1 -0
- package/dist/core/PyModule.js +172 -0
- package/dist/core/PyModule.js.map +1 -0
- package/dist/core/PyProxy.d.ts +65 -0
- package/dist/core/PyProxy.d.ts.map +1 -0
- package/dist/core/PyProxy.js +483 -0
- package/dist/core/PyProxy.js.map +1 -0
- package/dist/core/PyRuntime.d.ts +105 -0
- package/dist/core/PyRuntime.d.ts.map +1 -0
- package/dist/core/PyRuntime.js +438 -0
- package/dist/core/PyRuntime.js.map +1 -0
- package/dist/env/CondaManager.d.ts +118 -0
- package/dist/env/CondaManager.d.ts.map +1 -0
- package/dist/env/CondaManager.js +401 -0
- package/dist/env/CondaManager.js.map +1 -0
- package/dist/env/PackageInstaller.d.ts +233 -0
- package/dist/env/PackageInstaller.d.ts.map +1 -0
- package/dist/env/PackageInstaller.js +609 -0
- package/dist/env/PackageInstaller.js.map +1 -0
- package/dist/env/PythonDetector.d.ts +103 -0
- package/dist/env/PythonDetector.d.ts.map +1 -0
- package/dist/env/PythonDetector.js +381 -0
- package/dist/env/PythonDetector.js.map +1 -0
- package/dist/env/VenvManager.d.ts +117 -0
- package/dist/env/VenvManager.d.ts.map +1 -0
- package/dist/env/VenvManager.js +331 -0
- package/dist/env/VenvManager.js.map +1 -0
- package/dist/index.d.ts +169 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +393 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/Plugin.interface.d.ts +41 -0
- package/dist/plugins/Plugin.interface.d.ts.map +1 -0
- package/dist/plugins/Plugin.interface.js +12 -0
- package/dist/plugins/Plugin.interface.js.map +1 -0
- package/dist/plugins/PluginManager.d.ts +26 -0
- package/dist/plugins/PluginManager.d.ts.map +1 -0
- package/dist/plugins/PluginManager.js +174 -0
- package/dist/plugins/PluginManager.js.map +1 -0
- package/dist/plugins/builtin/NumpyPlugin.d.ts +17 -0
- package/dist/plugins/builtin/NumpyPlugin.d.ts.map +1 -0
- package/dist/plugins/builtin/NumpyPlugin.js +41 -0
- package/dist/plugins/builtin/NumpyPlugin.js.map +1 -0
- package/dist/plugins/builtin/PandasPlugin.d.ts +19 -0
- package/dist/plugins/builtin/PandasPlugin.d.ts.map +1 -0
- package/dist/plugins/builtin/PandasPlugin.js +57 -0
- package/dist/plugins/builtin/PandasPlugin.js.map +1 -0
- package/dist/plugins/builtin/TorchPlugin.d.ts +23 -0
- package/dist/plugins/builtin/TorchPlugin.d.ts.map +1 -0
- package/dist/plugins/builtin/TorchPlugin.js +50 -0
- package/dist/plugins/builtin/TorchPlugin.js.map +1 -0
- package/dist/plugins/index.d.ts +7 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +12 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/serialization/DataFrameBridge.d.ts +141 -0
- package/dist/serialization/DataFrameBridge.d.ts.map +1 -0
- package/dist/serialization/DataFrameBridge.js +355 -0
- package/dist/serialization/DataFrameBridge.js.map +1 -0
- package/dist/serialization/MsgPackSerializer.d.ts +45 -0
- package/dist/serialization/MsgPackSerializer.d.ts.map +1 -0
- package/dist/serialization/MsgPackSerializer.js +242 -0
- package/dist/serialization/MsgPackSerializer.js.map +1 -0
- package/dist/serialization/NumpyBridge.d.ts +96 -0
- package/dist/serialization/NumpyBridge.d.ts.map +1 -0
- package/dist/serialization/NumpyBridge.js +323 -0
- package/dist/serialization/NumpyBridge.js.map +1 -0
- package/dist/serialization/Serializer.d.ts +78 -0
- package/dist/serialization/Serializer.d.ts.map +1 -0
- package/dist/serialization/Serializer.js +281 -0
- package/dist/serialization/Serializer.js.map +1 -0
- package/dist/types/PythonTypeMapper.d.ts +87 -0
- package/dist/types/PythonTypeMapper.d.ts.map +1 -0
- package/dist/types/PythonTypeMapper.js +449 -0
- package/dist/types/PythonTypeMapper.js.map +1 -0
- package/dist/types/StubCache.d.ts +109 -0
- package/dist/types/StubCache.d.ts.map +1 -0
- package/dist/types/StubCache.js +333 -0
- package/dist/types/StubCache.js.map +1 -0
- package/dist/types/TypeGenerator.d.ts +139 -0
- package/dist/types/TypeGenerator.d.ts.map +1 -0
- package/dist/types/TypeGenerator.js +372 -0
- package/dist/types/TypeGenerator.js.map +1 -0
- package/dist/types/addon.d.ts +114 -0
- package/dist/types/addon.d.ts.map +1 -0
- package/dist/types/addon.js +32 -0
- package/dist/types/addon.js.map +1 -0
- package/dist/types/config.d.ts +175 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +35 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +12 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/python.d.ts +235 -0
- package/dist/types/python.d.ts.map +1 -0
- package/dist/types/python.js +7 -0
- package/dist/types/python.js.map +1 -0
- package/dist/utils/ErrorTranslator.d.ts +83 -0
- package/dist/utils/ErrorTranslator.d.ts.map +1 -0
- package/dist/utils/ErrorTranslator.js +210 -0
- package/dist/utils/ErrorTranslator.js.map +1 -0
- package/dist/utils/Logger.d.ts +27 -0
- package/dist/utils/Logger.d.ts.map +1 -0
- package/dist/utils/Logger.js +115 -0
- package/dist/utils/Logger.js.map +1 -0
- package/dist/utils/MemoryMonitor.d.ts +44 -0
- package/dist/utils/MemoryMonitor.d.ts.map +1 -0
- package/dist/utils/MemoryMonitor.js +143 -0
- package/dist/utils/MemoryMonitor.js.map +1 -0
- package/package.json +177 -0
- package/python/error_handler.py +433 -0
- package/python/nodepyx_runtime.py +575 -0
- package/python/serializer.py +379 -0
- package/python/type_inspector.py +288 -0
- package/scripts/build-native.js +68 -0
- package/scripts/download-prebuilds.js +99 -0
- package/scripts/generate-stubs.js +405 -0
- package/scripts/install.js +260 -0
- package/src/core/PyCallable.ts +137 -0
- package/src/core/PyContext.ts +296 -0
- package/src/core/PyIterator.ts +294 -0
- package/src/core/PyModule.ts +194 -0
- package/src/core/PyProxy.ts +605 -0
- package/src/core/PyRuntime.ts +504 -0
- package/src/env/CondaManager.ts +451 -0
- package/src/env/PackageInstaller.ts +738 -0
- package/src/env/PythonDetector.ts +414 -0
- package/src/env/VenvManager.ts +396 -0
- package/src/index.ts +425 -0
- package/src/native/gil_guard.cpp +26 -0
- package/src/native/gil_guard.h +175 -0
- package/src/native/nodepyx_addon.cpp +886 -0
- package/src/native/python_bridge.cpp +790 -0
- package/src/native/python_bridge.h +257 -0
- package/src/native/thread_pool.cpp +336 -0
- package/src/native/thread_pool.h +175 -0
- package/src/native/type_converter.cpp +901 -0
- package/src/native/type_converter.h +272 -0
- package/src/nextjs/PyProvider.tsx +123 -0
- package/src/nextjs/index.ts +21 -0
- package/src/nextjs/usePython.ts +106 -0
- package/src/nextjs/withnodepyx.ts +88 -0
- package/src/plugins/Plugin.interface.ts +51 -0
- package/src/plugins/PluginManager.ts +155 -0
- package/src/plugins/builtin/NumpyPlugin.ts +36 -0
- package/src/plugins/builtin/PandasPlugin.ts +49 -0
- package/src/plugins/builtin/TorchPlugin.ts +56 -0
- package/src/plugins/index.ts +7 -0
- package/src/serialization/DataFrameBridge.ts +398 -0
- package/src/serialization/MsgPackSerializer.ts +220 -0
- package/src/serialization/NumpyBridge.ts +332 -0
- package/src/serialization/Serializer.ts +320 -0
- package/src/types/PythonTypeMapper.ts +495 -0
- package/src/types/StubCache.ts +340 -0
- package/src/types/TypeGenerator.ts +491 -0
- package/src/types/addon.ts +170 -0
- package/src/types/config.ts +226 -0
- package/src/types/index.ts +55 -0
- package/src/types/python.ts +309 -0
- package/src/types/stubs/numpy.d.ts +441 -0
- package/src/types/stubs/pandas.d.ts +575 -0
- package/src/types/stubs/sklearn.d.ts +728 -0
- package/src/types/stubs/torch.d.ts +694 -0
- package/src/utils/ErrorTranslator.ts +220 -0
- package/src/utils/Logger.ts +119 -0
- package/src/utils/MemoryMonitor.ts +175 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* nodepyx — VenvManager
|
|
4
|
+
* Creates and manages Python virtualenvs for isolated package installation.
|
|
5
|
+
*
|
|
6
|
+
* Lifecycle:
|
|
7
|
+
* 1. Check if the venv directory exists and is valid.
|
|
8
|
+
* 2. If not, run `python3 -m venv <path>` to create it.
|
|
9
|
+
* 3. Upgrade pip to the latest version.
|
|
10
|
+
* 4. Install configured packages via pip.
|
|
11
|
+
* 5. Return the path to the Python executable inside the venv.
|
|
12
|
+
*
|
|
13
|
+
* State file (.nodepyx/venv-state.json) tracks installed packages
|
|
14
|
+
* to avoid re-running pip on every startup.
|
|
15
|
+
*/
|
|
16
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
19
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
20
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
21
|
+
}
|
|
22
|
+
Object.defineProperty(o, k2, desc);
|
|
23
|
+
}) : (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
o[k2] = m[k];
|
|
26
|
+
}));
|
|
27
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
28
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
29
|
+
}) : function(o, v) {
|
|
30
|
+
o["default"] = v;
|
|
31
|
+
});
|
|
32
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
33
|
+
var ownKeys = function(o) {
|
|
34
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
35
|
+
var ar = [];
|
|
36
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
37
|
+
return ar;
|
|
38
|
+
};
|
|
39
|
+
return ownKeys(o);
|
|
40
|
+
};
|
|
41
|
+
return function (mod) {
|
|
42
|
+
if (mod && mod.__esModule) return mod;
|
|
43
|
+
var result = {};
|
|
44
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
45
|
+
__setModuleDefault(result, mod);
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
48
|
+
})();
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.PackageInstallError = exports.VenvCreationError = exports.VenvManager = void 0;
|
|
51
|
+
const child_process_1 = require("child_process");
|
|
52
|
+
const fs = __importStar(require("fs"));
|
|
53
|
+
const path = __importStar(require("path"));
|
|
54
|
+
const Logger_1 = require("../utils/Logger");
|
|
55
|
+
const logger = new Logger_1.Logger('VenvManager');
|
|
56
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
57
|
+
function isWindows() {
|
|
58
|
+
return process.platform === 'win32';
|
|
59
|
+
}
|
|
60
|
+
function venvBinDir(venvPath) {
|
|
61
|
+
return path.join(venvPath, isWindows() ? 'Scripts' : 'bin');
|
|
62
|
+
}
|
|
63
|
+
function venvPython(venvPath) {
|
|
64
|
+
return path.join(venvBinDir(venvPath), isWindows() ? 'python.exe' : 'python3');
|
|
65
|
+
}
|
|
66
|
+
function venvPip(venvPath) {
|
|
67
|
+
return path.join(venvBinDir(venvPath), isWindows() ? 'pip.exe' : 'pip3');
|
|
68
|
+
}
|
|
69
|
+
const STATE_FILENAME = '.nodepyx-venv-state.json';
|
|
70
|
+
/**
|
|
71
|
+
* VenvManager — creates and manages Python virtualenvs.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const manager = new VenvManager({
|
|
76
|
+
* virtualenv: {
|
|
77
|
+
* path: './.venv',
|
|
78
|
+
* packages: ['pandas', 'numpy', 'scikit-learn'],
|
|
79
|
+
* autoInstall: true,
|
|
80
|
+
* }
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* const result = await manager.setup();
|
|
84
|
+
* console.log(result.pythonExecutable); // ./.venv/bin/python3
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
class VenvManager {
|
|
88
|
+
_config;
|
|
89
|
+
_basePython;
|
|
90
|
+
_installTimeout;
|
|
91
|
+
_skipPipUpgrade;
|
|
92
|
+
_venvPath;
|
|
93
|
+
_statePath;
|
|
94
|
+
constructor(options) {
|
|
95
|
+
this._config = options.virtualenv;
|
|
96
|
+
this._basePython = options.basePython ?? 'python3';
|
|
97
|
+
this._installTimeout = options.installTimeout ?? 300_000;
|
|
98
|
+
this._skipPipUpgrade = options.skipPipUpgrade ?? false;
|
|
99
|
+
this._venvPath = path.resolve(this._config.path);
|
|
100
|
+
this._statePath = path.join(this._venvPath, STATE_FILENAME);
|
|
101
|
+
}
|
|
102
|
+
// ─── Public API ──────────────────────────────────────────────────────────
|
|
103
|
+
/**
|
|
104
|
+
* Set up the virtualenv and install packages if needed.
|
|
105
|
+
* Returns the path to the Python executable inside the venv.
|
|
106
|
+
*/
|
|
107
|
+
async setup() {
|
|
108
|
+
let created = false;
|
|
109
|
+
let packagesInstalled = false;
|
|
110
|
+
// ── Create venv if it doesn't exist ──────────────────────────────────
|
|
111
|
+
if (!this._venvExists()) {
|
|
112
|
+
logger.info(`VenvManager: creating virtualenv at ${this._venvPath}`);
|
|
113
|
+
this._createVenv();
|
|
114
|
+
created = true;
|
|
115
|
+
if (!this._skipPipUpgrade) {
|
|
116
|
+
this._upgradePip();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const pythonExec = venvPython(this._venvPath);
|
|
120
|
+
const pipExec = venvPip(this._venvPath);
|
|
121
|
+
// ── Install packages ─────────────────────────────────────────────────
|
|
122
|
+
if (this._config.autoInstall && this._config.packages.length > 0) {
|
|
123
|
+
const missingPackages = this._findMissingPackages(pythonExec);
|
|
124
|
+
if (missingPackages.length > 0 || created) {
|
|
125
|
+
logger.info(`VenvManager: installing ${missingPackages.length > 0 ? missingPackages.join(', ') : this._config.packages.join(', ')}`);
|
|
126
|
+
this._installPackages(pipExec, missingPackages.length > 0 ? missingPackages : this._config.packages);
|
|
127
|
+
packagesInstalled = true;
|
|
128
|
+
this._updateState(pythonExec);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
logger.info(`VenvManager: ready — ${pythonExec}`);
|
|
132
|
+
return {
|
|
133
|
+
pythonExecutable: pythonExec,
|
|
134
|
+
pipExecutable: pipExec,
|
|
135
|
+
created,
|
|
136
|
+
packagesInstalled,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Install additional packages into the venv.
|
|
141
|
+
*/
|
|
142
|
+
async install(packages) {
|
|
143
|
+
if (!this._venvExists()) {
|
|
144
|
+
throw new Error(`VenvManager: venv does not exist at ${this._venvPath}`);
|
|
145
|
+
}
|
|
146
|
+
const pip = venvPip(this._venvPath);
|
|
147
|
+
this._installPackages(pip, packages);
|
|
148
|
+
this._updateState(venvPython(this._venvPath));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Check which of the configured packages are not installed in the venv.
|
|
152
|
+
*/
|
|
153
|
+
getMissingPackages() {
|
|
154
|
+
if (!this._venvExists()) {
|
|
155
|
+
return this._config.packages;
|
|
156
|
+
}
|
|
157
|
+
return this._findMissingPackages(venvPython(this._venvPath));
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Return the Python executable path inside the venv (without setup).
|
|
161
|
+
*/
|
|
162
|
+
get pythonExecutable() {
|
|
163
|
+
return venvPython(this._venvPath);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Return true if the venv exists and appears valid.
|
|
167
|
+
*/
|
|
168
|
+
isValid() {
|
|
169
|
+
return this._venvExists();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Destroy the venv directory.
|
|
173
|
+
*/
|
|
174
|
+
destroy() {
|
|
175
|
+
if (fs.existsSync(this._venvPath)) {
|
|
176
|
+
fs.rmSync(this._venvPath, { recursive: true, force: true });
|
|
177
|
+
logger.info(`VenvManager: destroyed venv at ${this._venvPath}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Return the list of installed packages (pip freeze format).
|
|
182
|
+
*/
|
|
183
|
+
listInstalled() {
|
|
184
|
+
const state = this._loadState();
|
|
185
|
+
return state?.installedPackages ?? [];
|
|
186
|
+
}
|
|
187
|
+
// ─── Private ─────────────────────────────────────────────────────────────
|
|
188
|
+
_venvExists() {
|
|
189
|
+
const pyExec = venvPython(this._venvPath);
|
|
190
|
+
return fs.existsSync(pyExec);
|
|
191
|
+
}
|
|
192
|
+
_createVenv() {
|
|
193
|
+
fs.mkdirSync(path.dirname(this._venvPath), { recursive: true });
|
|
194
|
+
// Try with pyvenv-cfg check
|
|
195
|
+
const result = (0, child_process_1.spawnSync)(this._basePython, ['-m', 'venv', '--clear', this._venvPath], {
|
|
196
|
+
encoding: 'utf8',
|
|
197
|
+
timeout: 60_000,
|
|
198
|
+
stdio: 'pipe',
|
|
199
|
+
});
|
|
200
|
+
if (result.status !== 0) {
|
|
201
|
+
const err = result.stderr?.trim() || result.error?.message || 'unknown error';
|
|
202
|
+
throw new VenvCreationError(`Failed to create virtualenv at ${this._venvPath}: ${err}`);
|
|
203
|
+
}
|
|
204
|
+
logger.info(`VenvManager: virtualenv created at ${this._venvPath}`);
|
|
205
|
+
}
|
|
206
|
+
_upgradePip() {
|
|
207
|
+
const pip = venvPip(this._venvPath);
|
|
208
|
+
try {
|
|
209
|
+
const result = (0, child_process_1.spawnSync)(pip, ['install', '--upgrade', 'pip', '--quiet'], { encoding: 'utf8', timeout: 60_000, stdio: 'pipe' });
|
|
210
|
+
if (result.status === 0) {
|
|
211
|
+
logger.debug('VenvManager: pip upgraded');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
logger.warn('VenvManager: pip upgrade failed (continuing)');
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
_installPackages(pip, packages) {
|
|
219
|
+
if (packages.length === 0) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const args = ['install', '--quiet', ...packages];
|
|
223
|
+
if (this._config.pipIndexUrl) {
|
|
224
|
+
args.push('--index-url', this._config.pipIndexUrl);
|
|
225
|
+
}
|
|
226
|
+
if (this._config.pipArgs) {
|
|
227
|
+
args.push(...this._config.pipArgs);
|
|
228
|
+
}
|
|
229
|
+
logger.info(`VenvManager: running pip install ${packages.join(' ')}`);
|
|
230
|
+
const result = (0, child_process_1.spawnSync)(pip, args, {
|
|
231
|
+
encoding: 'utf8',
|
|
232
|
+
timeout: this._installTimeout,
|
|
233
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
234
|
+
});
|
|
235
|
+
if (result.status !== 0) {
|
|
236
|
+
const err = result.stderr?.trim() || 'pip install failed';
|
|
237
|
+
throw new PackageInstallError(packages, err);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
_findMissingPackages(pythonExec) {
|
|
241
|
+
const state = this._loadState();
|
|
242
|
+
if (!state) {
|
|
243
|
+
return this._config.packages;
|
|
244
|
+
}
|
|
245
|
+
// Quick check via import
|
|
246
|
+
const missing = [];
|
|
247
|
+
for (const pkg of this._config.packages) {
|
|
248
|
+
// Strip version specifiers for import test
|
|
249
|
+
const importName = this._packageToImportName(pkg);
|
|
250
|
+
const result = (0, child_process_1.spawnSync)(pythonExec, ['-c', `import ${importName}`], { encoding: 'utf8', timeout: 5000, stdio: 'pipe' });
|
|
251
|
+
if (result.status !== 0) {
|
|
252
|
+
missing.push(pkg);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return missing;
|
|
256
|
+
}
|
|
257
|
+
_packageToImportName(pkg) {
|
|
258
|
+
// Strip version specifiers: pandas>=2.0 → pandas
|
|
259
|
+
const stripped = pkg.replace(/[>=<!].*/g, '').trim();
|
|
260
|
+
// Normalize: scikit-learn → sklearn, Pillow → PIL
|
|
261
|
+
const importAliases = {
|
|
262
|
+
'scikit-learn': 'sklearn',
|
|
263
|
+
'scikit_learn': 'sklearn',
|
|
264
|
+
'Pillow': 'PIL',
|
|
265
|
+
'pillow': 'PIL',
|
|
266
|
+
'opencv-python': 'cv2',
|
|
267
|
+
'pyyaml': 'yaml',
|
|
268
|
+
'beautifulsoup4': 'bs4',
|
|
269
|
+
};
|
|
270
|
+
return importAliases[stripped] ?? stripped;
|
|
271
|
+
}
|
|
272
|
+
_updateState(pythonExec) {
|
|
273
|
+
try {
|
|
274
|
+
const result = (0, child_process_1.spawnSync)(pythonExec, ['-m', 'pip', 'freeze', '--quiet'], { encoding: 'utf8', timeout: 30_000, stdio: 'pipe' });
|
|
275
|
+
const installed = (result.stdout ?? '').trim().split('\n').filter(Boolean);
|
|
276
|
+
const state = {
|
|
277
|
+
createdAt: this._loadState()?.createdAt ?? Date.now(),
|
|
278
|
+
pythonVersion: this._getPythonVersion(pythonExec),
|
|
279
|
+
installedPackages: installed,
|
|
280
|
+
lastInstallAt: Date.now(),
|
|
281
|
+
};
|
|
282
|
+
fs.writeFileSync(this._statePath, JSON.stringify(state, null, 2), 'utf8');
|
|
283
|
+
}
|
|
284
|
+
catch (err) {
|
|
285
|
+
logger.warn('VenvManager: failed to update state file', err);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
_loadState() {
|
|
289
|
+
try {
|
|
290
|
+
if (fs.existsSync(this._statePath)) {
|
|
291
|
+
return JSON.parse(fs.readFileSync(this._statePath, 'utf8'));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
// ignore
|
|
296
|
+
}
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
_getPythonVersion(pythonExec) {
|
|
300
|
+
try {
|
|
301
|
+
const result = (0, child_process_1.spawnSync)(pythonExec, ['--version'], {
|
|
302
|
+
encoding: 'utf8',
|
|
303
|
+
timeout: 5000,
|
|
304
|
+
stdio: 'pipe',
|
|
305
|
+
});
|
|
306
|
+
return (result.stdout || result.stderr || '').trim().replace('Python ', '');
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return 'unknown';
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
exports.VenvManager = VenvManager;
|
|
314
|
+
// ─── Error types ─────────────────────────────────────────────────────────────
|
|
315
|
+
class VenvCreationError extends Error {
|
|
316
|
+
constructor(message) {
|
|
317
|
+
super(message);
|
|
318
|
+
this.name = 'VenvCreationError';
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
exports.VenvCreationError = VenvCreationError;
|
|
322
|
+
class PackageInstallError extends Error {
|
|
323
|
+
packages;
|
|
324
|
+
constructor(packages, detail) {
|
|
325
|
+
super(`Failed to install packages [${packages.join(', ')}]: ${detail}`);
|
|
326
|
+
this.name = 'PackageInstallError';
|
|
327
|
+
this.packages = packages;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
exports.PackageInstallError = PackageInstallError;
|
|
331
|
+
//# sourceMappingURL=VenvManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VenvManager.js","sourceRoot":"","sources":["../../src/env/VenvManager.ts"],"names":[],"mapp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c;QAC5C,KAAK,CAAC,+BAA+B,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,MAAM,EAAE,CAAC,CAAC;QACxE,IAAI,CAAC,IAAI,GAAM,qBAAqB,CAAC;QACrC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAPD,kDAOC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* nodepyx — Main Entry Point
|
|
3
|
+
*
|
|
4
|
+
* Run Python libraries from Node.js as if they were Native.
|
|
5
|
+
* Embeds CPython in-process via N-API addon with full TypeScript types,
|
|
6
|
+
* Proxy-based API, async/await support, and Next.js integration.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { init, py } from 'nodepyx';
|
|
11
|
+
*
|
|
12
|
+
* await init({
|
|
13
|
+
* virtualenv: {
|
|
14
|
+
* path: './.venv',
|
|
15
|
+
* packages: ['pandas', 'numpy'],
|
|
16
|
+
* autoInstall: true,
|
|
17
|
+
* },
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* const pd = await py.import('pandas');
|
|
21
|
+
* const df = await pd.read_csv('data.csv');
|
|
22
|
+
* const result = await df.describe().to_dict('records');
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
import { PyRuntime } from './core/PyRuntime';
|
|
26
|
+
import type { nodepyxConfig } from './types/config';
|
|
27
|
+
import type { PyProxyInterface } from './types/python';
|
|
28
|
+
/**
|
|
29
|
+
* The main `py` object — a proxy over the PyRuntime singleton.
|
|
30
|
+
* Provides access to all PyRuntime methods after initialization.
|
|
31
|
+
*
|
|
32
|
+
* @throws {nodepyxNotInitializedError} if accessed before calling `init()`
|
|
33
|
+
*/
|
|
34
|
+
export declare const py: PyRuntime;
|
|
35
|
+
/**
|
|
36
|
+
* Initialize the nodepyx runtime.
|
|
37
|
+
* Must be called before using `py` or any other nodepyx API.
|
|
38
|
+
*
|
|
39
|
+
* Calling `init()` multiple times is safe — subsequent calls are no-ops
|
|
40
|
+
* unless `shutdown()` was called first, or `force: true` is passed.
|
|
41
|
+
*
|
|
42
|
+
* @param config - nodepyx configuration options
|
|
43
|
+
* @param options - Additional initialization options
|
|
44
|
+
* @returns Promise that resolves when Python is ready
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* await init({
|
|
49
|
+
* virtualenv: { path: '.venv', packages: ['pandas'], autoInstall: true },
|
|
50
|
+
* logLevel: 'info',
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function init(config?: nodepyxConfig, options?: {
|
|
55
|
+
force?: boolean;
|
|
56
|
+
}): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Shut down the nodepyx runtime and release all Python resources.
|
|
59
|
+
* After calling this, `py` is no longer accessible until `init()` is called again.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* await shutdown();
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function shutdown(): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Check if nodepyx is initialized and ready to use.
|
|
69
|
+
*/
|
|
70
|
+
export declare function isInitialized(): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Get the current PyRuntime instance.
|
|
73
|
+
* @throws {nodepyxNotInitializedError} if not initialized
|
|
74
|
+
*/
|
|
75
|
+
export declare function getRuntime(): PyRuntime;
|
|
76
|
+
/**
|
|
77
|
+
* Import a Python module and return a Proxy.
|
|
78
|
+
* Shorthand for `py.import(moduleName)`.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const np = await importModule('numpy');
|
|
83
|
+
* const arr = await np.array([1, 2, 3]);
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function importModule(moduleName: string): Promise<PyProxyInterface>;
|
|
87
|
+
/**
|
|
88
|
+
* Evaluate a Python expression and return the result.
|
|
89
|
+
* Shorthand for `py.eval(expression)`.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const result = await evalPython('1 + 2 + 3');
|
|
94
|
+
* // result === 6
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare function evalPython(expression: string): Promise<unknown>;
|
|
98
|
+
/**
|
|
99
|
+
* Execute Python code (statements, not expressions).
|
|
100
|
+
* Shorthand for `py.exec(code)`.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* await execPython(`
|
|
105
|
+
* import sys
|
|
106
|
+
* print(f"Python {sys.version}")
|
|
107
|
+
* `);
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export declare function execPython(code: string): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Run a Python script file.
|
|
113
|
+
*/
|
|
114
|
+
export declare function runFile(filePath: string): Promise<unknown>;
|
|
115
|
+
/**
|
|
116
|
+
* Install Python packages into the configured virtualenv.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* await installPackages(['pandas', 'numpy>=1.24.0', 'scikit-learn']);
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare function installPackages(packages: string[]): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
* Get memory statistics for the Python heap.
|
|
126
|
+
*/
|
|
127
|
+
export declare function getMemoryStats(): Promise<{
|
|
128
|
+
pythonHeapBytes: number;
|
|
129
|
+
pythonHeapPeakBytes: number;
|
|
130
|
+
trackedObjects: number;
|
|
131
|
+
pendingCallbacks: number;
|
|
132
|
+
}>;
|
|
133
|
+
/**
|
|
134
|
+
* Trigger Python garbage collection.
|
|
135
|
+
*/
|
|
136
|
+
export declare function collectGarbage(): Promise<void>;
|
|
137
|
+
export { PyRuntime } from './core/PyRuntime';
|
|
138
|
+
export { PyProxy } from './core/PyProxy';
|
|
139
|
+
export { PyModule } from './core/PyModule';
|
|
140
|
+
export { PyCallable } from './core/PyCallable';
|
|
141
|
+
export { PyIterator } from './core/PyIterator';
|
|
142
|
+
export { PyContext } from './core/PyContext';
|
|
143
|
+
export { Serializer } from './serialization/Serializer';
|
|
144
|
+
export { NumpyBridge } from './serialization/NumpyBridge';
|
|
145
|
+
export { DataFrameBridge } from './serialization/DataFrameBridge';
|
|
146
|
+
export { PythonError, PythonValueError, PythonTypeError, PythonKeyError, PythonIndexError, PythonAttributeError, PythonImportError, PythonModuleNotFoundError, PythonFileNotFoundError, PythonPermissionError, PythonRuntimeError, PythonMemoryError, PythonStopIteration, PythonTimeoutError, nodepyxNotInitializedError, nodepyxShutdownError, translatePythonError, isPythonError, isPythonErrorOfType, } from './utils/ErrorTranslator';
|
|
147
|
+
export { TypeGenerator } from './types/TypeGenerator';
|
|
148
|
+
export { PythonDetector } from './env/PythonDetector';
|
|
149
|
+
export { VenvManager } from './env/VenvManager';
|
|
150
|
+
export { CondaManager } from './env/CondaManager';
|
|
151
|
+
export { PackageInstaller } from './env/PackageInstaller';
|
|
152
|
+
export { PluginManager } from './plugins/PluginManager';
|
|
153
|
+
export type { nodepyxConfig, ResolvednodepyxConfig, VirtualenvConfig, CondaConfig, LogLevel, PyObjectId, PyTypeInfo, PyParamInfo, PyCallableInfo, PyClassInfo, PyModuleInspection, SerializedFormat, SerializedValue, PyCallResult, PyErrorInfo, DataFrameResult, SeriesResult, NumPyArrayResult, PyProxyInterface, AttributePath, NativeAddon, } from './types/index';
|
|
154
|
+
declare const _default: {
|
|
155
|
+
init: typeof init;
|
|
156
|
+
shutdown: typeof shutdown;
|
|
157
|
+
isInitialized: typeof isInitialized;
|
|
158
|
+
getRuntime: typeof getRuntime;
|
|
159
|
+
py: PyRuntime;
|
|
160
|
+
import: typeof importModule;
|
|
161
|
+
eval: typeof evalPython;
|
|
162
|
+
exec: typeof execPython;
|
|
163
|
+
runFile: typeof runFile;
|
|
164
|
+
installPackages: typeof installPackages;
|
|
165
|
+
getMemoryStats: typeof getMemoryStats;
|
|
166
|
+
collectGarbage: typeof collectGarbage;
|
|
167
|
+
};
|
|
168
|
+
export default _default;
|
|
169
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAG7C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAYvD;;;;;GAKG;AACH,eAAO,MAAM,EAAE,EAAE,SAgCf,CAAC;AAIH;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,IAAI,CACxB,MAAM,CAAC,EAAE,aAAa,EACtB,OAAO,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,GAC5B,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAiBD;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAiB9C;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,OAAO,CAEvC;AAED;;;GAGG;AACH,wBAAgB,UAAU,IAAI,SAAS,CAKtC;AAID;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAGhF;AAED;;;;;;;;;GASG;AACH,wBAAsB,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAGrE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC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