@freestyle-sh/with-nodejs 0.1.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/dist/index.d.ts +25 -0
- package/dist/index.js +89 -0
- package/package.json +25 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { VmWith, VmWithInstance, CreateVmOptions } from 'freestyle-sandboxes';
|
|
2
|
+
import { VmJavaScriptRuntimeInstance, JSONValue, RunCodeResponse, VmJavaScriptRuntime } from '@freestyle-sh/with-type-js';
|
|
3
|
+
|
|
4
|
+
type NodeJsOptions = {
|
|
5
|
+
version?: string;
|
|
6
|
+
workdir?: string;
|
|
7
|
+
};
|
|
8
|
+
type NodeJsResolvedOptions = {
|
|
9
|
+
version: string;
|
|
10
|
+
workdir?: string;
|
|
11
|
+
};
|
|
12
|
+
declare class VmNodeJs extends VmWith<NodeJsRuntimeInstance> implements VmJavaScriptRuntime<VmJavaScriptRuntimeInstance> {
|
|
13
|
+
options: NodeJsResolvedOptions;
|
|
14
|
+
constructor(options?: NodeJsOptions);
|
|
15
|
+
configure(existingConfig: CreateVmOptions): CreateVmOptions | Promise<CreateVmOptions>;
|
|
16
|
+
createInstance(): NodeJsRuntimeInstance;
|
|
17
|
+
installServiceName(): string;
|
|
18
|
+
}
|
|
19
|
+
declare class NodeJsRuntimeInstance extends VmWithInstance implements VmJavaScriptRuntimeInstance {
|
|
20
|
+
builder: VmNodeJs;
|
|
21
|
+
constructor(builder: VmNodeJs);
|
|
22
|
+
runCode<Result extends JSONValue = any>(code: string): Promise<RunCodeResponse<Result>>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { VmNodeJs };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { VmWith, VmTemplate, VmWithInstance } from 'freestyle-sandboxes';
|
|
2
|
+
|
|
3
|
+
class VmNodeJs extends VmWith {
|
|
4
|
+
options;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super();
|
|
7
|
+
this.options = {
|
|
8
|
+
version: options?.version ?? "24",
|
|
9
|
+
workdir: options?.workdir
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
configure(existingConfig) {
|
|
13
|
+
const installScript = `#!/bin/bash
|
|
14
|
+
set -e
|
|
15
|
+
export NVM_DIR="/opt/nvm"
|
|
16
|
+
mkdir -p "$NVM_DIR"
|
|
17
|
+
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
|
|
18
|
+
source "$NVM_DIR/nvm.sh"
|
|
19
|
+
nvm install ${this.options.version}
|
|
20
|
+
nvm alias default ${this.options.version}
|
|
21
|
+
node -v
|
|
22
|
+
npm -v
|
|
23
|
+
`;
|
|
24
|
+
const nvmInit = `export NVM_DIR="/opt/nvm"
|
|
25
|
+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
|
|
26
|
+
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
|
|
27
|
+
`;
|
|
28
|
+
const nodeJsConfig = {
|
|
29
|
+
template: new VmTemplate({
|
|
30
|
+
additionalFiles: {
|
|
31
|
+
"/opt/install-nodejs.sh": {
|
|
32
|
+
content: installScript
|
|
33
|
+
},
|
|
34
|
+
"/etc/profile.d/nvm.sh": {
|
|
35
|
+
content: nvmInit
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
systemd: {
|
|
39
|
+
services: [
|
|
40
|
+
{
|
|
41
|
+
name: "install-nodejs",
|
|
42
|
+
mode: "oneshot",
|
|
43
|
+
deleteAfterSuccess: true,
|
|
44
|
+
exec: ["bash /opt/install-nodejs.sh"],
|
|
45
|
+
timeoutSec: 300
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
};
|
|
51
|
+
return this.compose(existingConfig, nodeJsConfig);
|
|
52
|
+
}
|
|
53
|
+
createInstance() {
|
|
54
|
+
return new NodeJsRuntimeInstance(this);
|
|
55
|
+
}
|
|
56
|
+
installServiceName() {
|
|
57
|
+
return `install-nodejs.service`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
class NodeJsRuntimeInstance extends VmWithInstance {
|
|
61
|
+
builder;
|
|
62
|
+
constructor(builder) {
|
|
63
|
+
super();
|
|
64
|
+
this.builder = builder;
|
|
65
|
+
}
|
|
66
|
+
async runCode(code) {
|
|
67
|
+
const result = await this.vm.exec({
|
|
68
|
+
command: `node -e "${code.replace(/"/g, '\\"')}"`
|
|
69
|
+
});
|
|
70
|
+
let parsedResult = void 0;
|
|
71
|
+
if (result.stdout) {
|
|
72
|
+
try {
|
|
73
|
+
parsedResult = JSON.parse(result.stdout);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
if (result.stderr) {
|
|
76
|
+
`Failed to parse JSON output. Stderr: ${result.stderr}`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
result: parsedResult,
|
|
82
|
+
stdout: result.stdout ?? void 0,
|
|
83
|
+
stderr: result.stderr ?? void 0,
|
|
84
|
+
statusCode: result.statusCode ?? -1
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { VmNodeJs };
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@freestyle-sh/with-nodejs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"freestyle-sandboxes": "^0.1.1",
|
|
7
|
+
"@freestyle-sh/with-type-js": "^0.1.0"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"source": "./src/index.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "pkgroll"
|
|
24
|
+
}
|
|
25
|
+
}
|