Skip to content

Commit c216960

Browse files
committed
feat(apps): adapt apps package to accept node-runtime
refactor(apps): make NodeSubprocessController use the BaseSubprocessController
1 parent 69b7fa4 commit c216960

3 files changed

Lines changed: 52 additions & 76 deletions

File tree

packages/apps/src/server/managers/AppRuntimeManager.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AppManager } from '../AppManager';
22
import type { IParseAppPackageResult } from '../compiler';
33
import type { IRuntimeController } from '../runtime/IRuntimeController';
44
import { DenoRuntimeSubprocessController } from '../runtime/deno/AppsEngineDenoRuntime';
5+
import { NodeRuntimeSubprocessController } from '../runtime/node/AppsEngineNodeRuntime';
56
import type { IAppStorageItem } from '../storage';
67

78
export type AppRuntimeParams = {
@@ -18,9 +19,16 @@ export type ExecRequestOptions = {
1819
timeout?: number;
1920
};
2021

21-
const defaultRuntimeFactory = (manager: AppManager, appPackage: IParseAppPackageResult, storageItem: IAppStorageItem) =>
22+
const { APPS_ENGINE_RUNTIME_BACKEND = 'deno' } = process.env;
23+
24+
export const nodeRuntimeFactory = (manager: AppManager, appPackage: IParseAppPackageResult, storageItem: IAppStorageItem) =>
25+
new NodeRuntimeSubprocessController(manager, appPackage, storageItem);
26+
27+
export const denoRuntimeFactory = (manager: AppManager, appPackage: IParseAppPackageResult, storageItem: IAppStorageItem) =>
2228
new DenoRuntimeSubprocessController(manager, appPackage, storageItem);
2329

30+
const defaultRuntimeFactory = APPS_ENGINE_RUNTIME_BACKEND === 'node' ? nodeRuntimeFactory : denoRuntimeFactory;
31+
2432
export class AppRuntimeManager {
2533
private readonly subprocesses: Record<string, IRuntimeController> = {};
2634

packages/apps/src/server/runtime/AppsEngineNodeRuntime.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as path from 'node:path';
2+
3+
import type { AppManager } from '../../AppManager';
4+
import type { IParseAppPackageResult } from '../../compiler';
5+
import type { IAppStorageItem } from '../../storage';
6+
import { BaseRuntimeSubprocessController, type ProcessConfiguration } from '../base/BaseRuntimeSubprocessController';
7+
8+
export class NodeRuntimeSubprocessController extends BaseRuntimeSubprocessController {
9+
private readonly nodeBin = 'node';
10+
11+
private readonly scriptRuntimePath: string;
12+
13+
constructor(manager: AppManager, appPackage: IParseAppPackageResult, storageItem: IAppStorageItem) {
14+
super('node', manager, appPackage, storageItem);
15+
16+
this.scriptRuntimePath = require.resolve('../../../../node-runtime/dist/main.js');
17+
}
18+
19+
protected buildProcessConfiguration(): ProcessConfiguration {
20+
const allowedDirs = [this.tempFilePath, path.resolve(path.dirname(this.scriptRuntimePath), '..', '..'), this.appsEnginePath];
21+
22+
const args = [
23+
'--permission',
24+
...allowedDirs.map((dir) => `--allow-fs-read=${dir}`),
25+
this.scriptRuntimePath,
26+
'--subprocess',
27+
this.appPackage.info.id,
28+
'--spawnId',
29+
String(this.spawnId++),
30+
];
31+
32+
// SECURITY: We control the command, the arguments and the script that will be executed.
33+
return {
34+
command: this.nodeBin,
35+
args,
36+
options: {
37+
env: {
38+
PATH: process.env.PATH,
39+
},
40+
},
41+
};
42+
}
43+
}

0 commit comments

Comments
 (0)