mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { dirname } from 'node:path';
|
|
import { createContext, runInContext } from 'node:vm';
|
|
import type { PyodideInterface } from 'pyodide';
|
|
|
|
let pyodideInstance: PyodideInterface | undefined;
|
|
|
|
export async function LoadPyodide(packageCacheDir: string): Promise<PyodideInterface> {
|
|
if (pyodideInstance === undefined) {
|
|
const { loadPyodide } = await import('pyodide');
|
|
const { XMLHttpRequest } = await import('xmlhttprequest-ssl');
|
|
const indexURL = dirname(require.resolve('pyodide'));
|
|
const context = createContext({
|
|
loadPyodide,
|
|
indexURL,
|
|
packageCacheDir,
|
|
jsglobals: {
|
|
console,
|
|
fetch,
|
|
AbortController,
|
|
AbortSignal,
|
|
Object,
|
|
XMLHttpRequest,
|
|
},
|
|
});
|
|
pyodideInstance = (await runInContext(
|
|
'loadPyodide({ indexURL, packageCacheDir, jsglobals })',
|
|
context,
|
|
)) as PyodideInterface;
|
|
|
|
await pyodideInstance.runPythonAsync(`
|
|
import os
|
|
|
|
def blocked_system(*args, **kwargs):
|
|
raise RuntimeError("os.system is blocked for security reasons.")
|
|
|
|
os.system = blocked_system
|
|
|
|
from importlib.abc import MetaPathFinder
|
|
from importlib.machinery import ModuleSpec
|
|
from types import ModuleType
|
|
from typing import Sequence, Optional
|
|
|
|
from _pyodide_core import jsproxy_typedict
|
|
from js import Object
|
|
`);
|
|
}
|
|
|
|
return pyodideInstance;
|
|
}
|