Skip to content

Commit 7541399

Browse files
committed
Improve overall code, solved bugs and make tests work.
1 parent 16c492f commit 7541399

22 files changed

+380
-514
lines changed

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

package-lock.json

+11-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"url": "https://github.com/metacall/faas/issues"
2828
},
2929
"homepage": "https://github.com/metacall/faas#readme",
30+
"engines": {
31+
"npm": ">=10.0.0",
32+
"node": ">=20.1.0 || >=v18.17.0"
33+
},
3034
"prettier": {
3135
"tabWidth": 4,
3236
"useTabs": true,
@@ -90,14 +94,12 @@
9094
"colors": "^1.4.0",
9195
"dotenv": "^16.0.3",
9296
"express": "^4.18.2",
93-
"git-clone": "^0.2.0",
9497
"unzipper": "^0.10.11"
9598
},
9699
"devDependencies": {
97100
"@types/busboy": "^1.3.0",
98101
"@types/express": "^4.17.15",
99-
"@types/git-clone": "^0.2.0",
100-
"@types/node": "^14.14.7",
102+
"@types/node": "^18.17.0",
101103
"@types/unzipper": "^0.10.5",
102104
"@typescript-eslint/eslint-plugin": "^4.7.0",
103105
"@typescript-eslint/parser": "^4.7.0",
@@ -109,4 +111,4 @@
109111
"prettier": "^2.1.2",
110112
"typescript": "^4.3.2"
111113
}
112-
}
114+
}

src/constants.ts

+3-65
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
import { DeployStatus, MetaCallJSON } from '@metacall/protocol/deployment';
2-
import { ChildProcess } from 'child_process';
32

43
export interface Deployment {
54
id: string;
6-
type?: string;
5+
path: string;
76
jsons: MetaCallJSON[];
87
runners?: string[];
9-
path: string;
8+
type?: string;
109
blob?: string;
1110
}
1211

1312
export const deploymentMap: Record<string, Promise<Deployment>> = {};
1413

15-
export const createInstallDependenciesScript = (
16-
runner: string,
17-
path: string
18-
): string => {
19-
const installDependenciesScript: Record<string, string> = {
20-
python: `cd ${path} && metacall pip3 install -r requirements.txt`,
21-
nodejs: `cd ${path} && metacall npm i`,
22-
csharp: `cd ${path} && metacall dotnet restore && metacall dotnet release;`,
23-
ruby: `cd ${path} && metacall bundle install`
24-
};
25-
return installDependenciesScript[runner];
26-
};
27-
2814
export type tpackages = Record<string, unknown>;
2915

3016
// TODO: Isn't this available inside protocol package? We MUST reuse it
@@ -63,54 +49,6 @@ export class App implements IApp {
6349
}
6450
}
6551

66-
export type IAppWithFunctions = IApp & {
67-
funcs: string[];
68-
};
69-
70-
export type IAllApps = Record<string, IAppWithFunctions>;
52+
export type IAllApps = Record<string, IApp>;
7153

7254
export const allApplications: IAllApps = {};
73-
74-
export enum ProtocolMessageType {
75-
Install = 'InstallDependencies',
76-
Load = 'LoadFunctions',
77-
MetaData = 'GetApplicationMetadata',
78-
Invoke = 'CallFunction',
79-
InvokeResult = 'FunctionInvokeResult'
80-
}
81-
82-
export const childProcesses: { [key: string]: ChildProcess } = {};
83-
84-
export interface WorkerMessage<T> {
85-
type: ProtocolMessageType;
86-
data: T;
87-
}
88-
89-
export type WorkerMessageUnknown = WorkerMessage<unknown>;
90-
91-
export interface InspectObject {
92-
[key: string]: Array<{ name: string }>;
93-
}
94-
export interface LogMessage {
95-
deploymentName: string;
96-
workerPID: number;
97-
message: string;
98-
}
99-
100-
export const ANSICode: number[] = [
101-
166, 154, 142, 118, 203, 202, 190, 215, 214, 32, 6, 4, 220, 208, 184, 172
102-
];
103-
104-
export interface PIDToColorCodeMapType {
105-
[key: string]: number;
106-
}
107-
108-
export interface AssignedColorCodesType {
109-
[key: string]: boolean;
110-
}
111-
112-
// Maps a PID to a color code
113-
export const PIDToColorCodeMap: PIDToColorCodeMapType = {};
114-
115-
// Tracks whether a color code is assigned
116-
export const assignedColorCodes: AssignedColorCodesType = {};

src/controller/call.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { NextFunction, Request, Response } from 'express';
2-
import {
3-
ProtocolMessageType,
4-
WorkerMessageUnknown,
5-
childProcesses
6-
} from '../constants';
72
import AppError from '../utils/appError';
3+
import {
4+
Processes,
5+
WorkerMessageType,
6+
WorkerMessageUnknown
7+
} from '../worker/master';
88

99
export const callFunction = (
1010
req: Request,
@@ -22,7 +22,7 @@ export const callFunction = (
2222
const { appName: app, name } = req.params;
2323
const args = Object.values(req.body);
2424

25-
if (!(app in childProcesses)) {
25+
if (!(app in Processes)) {
2626
return res
2727
.status(404)
2828
.send(
@@ -33,18 +33,18 @@ export const callFunction = (
3333
let responseSent = false; // Flag to track if response has been sent
3434
let errorCame = false;
3535

36-
childProcesses[app].send({
37-
type: ProtocolMessageType.Invoke,
36+
Processes[app].send({
37+
type: WorkerMessageType.Invoke,
3838
data: {
3939
name,
4040
args
4141
}
4242
});
4343

44-
childProcesses[app].on('message', (message: WorkerMessageUnknown) => {
44+
Processes[app].on('message', (message: WorkerMessageUnknown) => {
4545
if (!responseSent) {
4646
// Check if response has already been sent
47-
if (message.type === ProtocolMessageType.InvokeResult) {
47+
if (message.type === WorkerMessageType.InvokeResult) {
4848
responseSent = true; // Set flag to true to indicate response has been sent
4949
return res.send(JSON.stringify(message.data));
5050
} else {

src/controller/catch.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NextFunction, Request, RequestHandler, Response } from 'express';
2+
3+
export const catchAsync = (
4+
fn: (
5+
req: Request,
6+
res: Response,
7+
next: NextFunction
8+
) => Promise<Response | void>
9+
): RequestHandler => {
10+
return (req: Request, res: Response, next: NextFunction) => {
11+
return fn(req, res, next).catch(err => next(err));
12+
};
13+
};

src/controller/delete.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { NextFunction, Request, Response } from 'express';
22

3-
import { ChildProcess } from 'child_process';
43
import { rm } from 'fs/promises';
54
import { join } from 'path';
65

7-
import { allApplications, childProcesses } from '../constants';
6+
import { allApplications } from '../constants';
87
import { appsDirectory } from '../utils/config';
9-
import { catchAsync, ensureFolderExists } from '../utils/utils';
8+
import { ensureFolderExists } from '../utils/filesystem';
9+
import { Processes } from '../worker/master';
10+
import { catchAsync } from './catch';
1011

1112
const deleteStatusMessage = (
1213
app: string
@@ -41,21 +42,20 @@ export const deployDelete = catchAsync(
4142
// Initialize isError flag
4243
let isError = false;
4344

44-
// Check if the application exists in childProcesses and allApplications objects
45-
if (!(app in childProcesses && app in allApplications)) {
45+
// Check if the application exists in Processes and allApplications objects
46+
if (!(app in Processes && app in allApplications)) {
4647
isError = true;
4748
return res.send(deleteStatusMessage(app)['error']);
4849
}
4950

5051
// Retrieve the child process associated with the application and kill it
51-
const childProcessesInApplications: ChildProcess = childProcesses[app];
52-
childProcessesInApplications.kill();
52+
Processes[app].kill();
5353

54-
// Remove the application from childProcesses and allApplications objects
55-
delete childProcesses[app];
54+
// Remove the application from Processes and allApplications objects
55+
delete Processes[app];
5656
delete allApplications[app];
5757

58-
if (app in childProcesses && app in allApplications) {
58+
if (app in Processes && app in allApplications) {
5959
isError = true;
6060
return res.send(deleteStatusMessage(app)['appShouldntExist']);
6161
}

0 commit comments

Comments
 (0)