-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApplicationScope.ts
More file actions
50 lines (45 loc) · 2 KB
/
ApplicationScope.ts
File metadata and controls
50 lines (45 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { Resources } from '../resources/Resources.ts';
import { type Server } from '../server/Server.ts';
import { forComponent } from '../utility/logging/harper_logger.js';
import { scopedImport } from '../security/jsLoader.ts';
import * as env from '../utility/environment/environmentManager.js';
import { CONFIG_PARAMS } from '../utility/hdbTerms.ts';
export class MissingDefaultFilesOptionError extends Error {
constructor() {
super('No default files option exists. Ensure `files` is specified in config.yaml');
this.name = 'MissingDefaultFilesOptionError';
}
}
/**
* This class is used to represent the application scope for the VM context used for loading modules within an application
*/
export class ApplicationScope {
logger: any;
resources: Resources;
server: Server;
mode?: 'native' | 'vm' | 'vm-current-context' | 'compartment'; // option to set this from the scope
dependencyLoader?: 'native' | 'app' | 'auto'; // option to set this from the scope
allowedPath?: string;
config: any;
moduleCache: any; // used by the loader to retain a cache of modules, type is an internal detail of the loader
constructor(name: string, resources: Resources, server: Server, isInternal = false) {
this.logger = forComponent(name, !isInternal);
this.resources = resources;
this.server = server;
this.mode = env.get(CONFIG_PARAMS.APPLICATIONS_MODULELOADER) ?? 'vm';
this.dependencyLoader = env.get(CONFIG_PARAMS.APPLICATIONS_DEPENDENCYLOADER);
if (env.get(CONFIG_PARAMS.APPLICATIONS_ALLOWEDDIRECTORY) !== 'app') this.allowedPath = ''; // this is used to match paths by startsWith, so empty string matches everything
}
/**
* The compartment that is used for this scope and any imports that it makes
*/
compartment?: Promise<any>;
/**
* Import a file into the scope's sandbox.
* @param filePath - The path of the file to import.
* @returns A promise that resolves with the imported module or value.
*/
async import(filePath: string): Promise<unknown> {
return scopedImport(filePath, this);
}
}