-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·219 lines (189 loc) · 6.57 KB
/
index.mjs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import express from 'express';
import bodyParser from 'body-parser';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import vm from 'vm';
import cors from 'cors';
import multer from 'multer';
import axios from 'axios';
import swaggerUi from 'swagger-ui-express';
import { v4 as uuidv4 } from 'uuid';
import dotenv from 'dotenv';
import jwt from 'jsonwebtoken';
dotenv.config();
/* Utility to get __dirname in ES modules */
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function generateAlphanumericName(length = 10) {
const alphanumericCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += alphanumericCharacters.charAt(Math.floor(Math.random() * alphanumericCharacters.length));
}
return result;
}
const upload = multer({ dest: 'uploads/' });
const app = express();
app.use(cors());
app.use(bodyParser.json());
/* OpenAPI DOC */
const openApiFilePath = path.join(__dirname, 'openapi.json');
const openApiDocument = JSON.parse(fs.readFileSync(openApiFilePath, 'utf8'));
app.use('/nodejsfaas/api-docs', swaggerUi.serve, swaggerUi.setup(openApiDocument));
/* Security Middleware */
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) return res.status(401).json({
"status": "ERROR",
"code": 401,
"message": "Unauthorized access",
"payload": {}
});
jwt.verify(token, process.env.JWT_KEY, (err, decoded) => {
if (err || (decoded.expire_at == undefined || decoded.issued_at == undefined || decoded.issuer == undefined || decoded.namespace == undefined)) {
return res.status(401).json({
"status": "ERROR",
"code": 401,
"message": "Unauthorized access",
"payload": {}
});
}
req.decoded = decoded;
next();
});
};
app.get('/nodejsfaas/server/health', (req, res) => {
res.status(200).send('OK');
});
app.head('/nodejsfaas/server/health', (req, res) => {
res.status(200).send();
});
app.get('/nodejsfaas/function/list', authenticateToken, (req, res) => {
fs.readdir(`functions/${req.decoded.namespace}`, (err, files) => {
if (err) {
return res.status(500).json({
"status": "ERROR",
"code": 500,
"message": "Namespace does not exist, create function before listing",
"payload": {}
});
}
// Remove extensions from filenames
const fileNamesWithoutExtension = files.map(file => path.basename(file, path.extname(file)));
res.status(200).json({
"status": "OK",
"code": 200,
"message": "",
"payload": {
"function_ids": fileNamesWithoutExtension
}
});
});
});
app.post('/nodejsfaas/function/create', authenticateToken, upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(400).json({
"status": "ERROR",
"code": 400,
"message": "No file uploaded",
"payload": {}
});
}
const file = req.file;
const file_id = generateAlphanumericName(25);
const file_name = `${file_id}.mjs`;
const directory = `functions/${req.decoded.namespace}`;
// Ensure the directory exists
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
// Rename the uploaded file and move it to the correct directory
fs.renameSync(file.path, path.join(directory, file_name));
res.status(200).json({
"status": "OK",
"code": 200,
"message": "File uploaded successfully",
"payload": {
"functions_id": file_id
}
});
});
app.delete('/nodejsfaas/function/:function_id', authenticateToken, (req, res) => {
const function_id = req.params.function_id;
const file_name = `${function_id}.mjs`;
fs.unlink(`functions/${req.decoded.namespace}/${file_name}`, (err) => {
if (err) {
return res.status(500).json({
"status": "ERROR",
"code": 500,
"message": "Failed to delete function",
"payload": {}
});
}
res.status(200).json({
"status": "OK",
"code": 200,
"message": "File deleted successfully",
"payload": {}
});
});
});
app.post('/nodejsfaas/function/execute/:function_id', authenticateToken, async (req, res) => {
await new Promise((resolve, reject) => {
if (!req.body || Object.keys(req.body).length === 0) {
reject(new Error("Request body is empty or not valid JSON"));
return;
}
const function_id = req.params.function_id;
fs.readFile(`functions/${req.decoded.namespace}/${function_id}.mjs`, 'utf8', (err, code) => {
if (err) {
reject(new Error("Function does not exist"));
return;
}
const { event_name, event_data } = req.body;
const context_object = {
console: console,
G_EVENT_NAME: event_name,
G_CONTEXT: event_data,
G_CALLBACK: (response_payload) => {
resolve(response_payload);
},
require: (module_name) => {
switch (module_name) {
case "axios":
return axios;
break;
default:
throw new Error(`Module '${module_name}' is not allowed.`);
break;
}
}
};
try {
vm.runInNewContext(code, context_object);
} catch (error) {
reject(new Error(error));
}
});
}).then((response_payload) => {
res.status(200).json({
"status": "OK",
"code": 200,
"message": "",
"payload": response_payload
});
}).catch((error) => {
res.status(400).json({
"status": "ERROR",
"code": 400,
"message": error.message,
"payload": {}
});
});
});
const PORT = process.env.PORT || 9256;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});