feat(core): Add permission-controlled Deno execution, configurable script server port, improved downloader, dependency bumps and test updates
This commit is contained in:
@@ -1,21 +1,69 @@
|
||||
import type { ScriptServer } from './classes.scriptserver.js';
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
/* This file contains logic to execute deno commands in an ephermal way */
|
||||
export type TDenoPermission =
|
||||
| 'all'
|
||||
| 'env'
|
||||
| 'ffi'
|
||||
| 'hrtime'
|
||||
| 'net'
|
||||
| 'read'
|
||||
| 'run'
|
||||
| 'sys'
|
||||
| 'write';
|
||||
|
||||
export interface IDenoExecutionOptions {
|
||||
permissions?: TDenoPermission[];
|
||||
denoBinaryPath?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class contains logic to execute deno commands in an ephemeral way
|
||||
*/
|
||||
export class DenoExecution {
|
||||
public id: string;
|
||||
public scriptserverRef: ScriptServer;
|
||||
public script: string;
|
||||
public options: IDenoExecutionOptions;
|
||||
|
||||
constructor(scriptserverRef: ScriptServer, scriptArg: string) {
|
||||
constructor(scriptserverRef: ScriptServer, scriptArg: string, options: IDenoExecutionOptions = {}) {
|
||||
this.scriptserverRef = scriptserverRef;
|
||||
this.script = scriptArg;
|
||||
this.options = options;
|
||||
this.id = plugins.smartunique.shortId();
|
||||
}
|
||||
|
||||
public async execute() {
|
||||
this.scriptserverRef.executionMap.add(this);
|
||||
await this.scriptserverRef.smartshellInstance.exec(`deno run http://localhost:3210/getscript/${this.id}`)
|
||||
private buildPermissionFlags(): string {
|
||||
const permissions = this.options.permissions || [];
|
||||
if (permissions.length === 0) {
|
||||
return '';
|
||||
}
|
||||
if (permissions.includes('all')) {
|
||||
return '-A';
|
||||
}
|
||||
return permissions.map(p => `--allow-${p}`).join(' ');
|
||||
}
|
||||
}
|
||||
|
||||
public async execute(): Promise<{ exitCode: number; stdout: string; stderr: string }> {
|
||||
this.scriptserverRef.executionMap.add(this);
|
||||
|
||||
try {
|
||||
const denoBinary = this.options.denoBinaryPath || 'deno';
|
||||
const permissionFlags = this.buildPermissionFlags();
|
||||
const port = this.scriptserverRef.getPort();
|
||||
const scriptUrl = `http://localhost:${port}/getscript/${this.id}`;
|
||||
|
||||
const command = `${denoBinary} run ${permissionFlags} ${scriptUrl}`.replace(/\s+/g, ' ').trim();
|
||||
const result = await this.scriptserverRef.smartshellInstance.exec(command);
|
||||
|
||||
return {
|
||||
exitCode: result.exitCode,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr,
|
||||
};
|
||||
} finally {
|
||||
// Clean up: remove from execution map after execution completes
|
||||
this.scriptserverRef.executionMap.remove(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user