Skip to content

Commit

Permalink
Merge branch 'master' into refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
khareyash05 authored Mar 15, 2024
2 parents 51f3b17 + 97eaf35 commit 846394e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 72 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll": true
"source.organizeImports": "explicit",
"source.fixAll": "explicit"
},
"editor.rulers": [
80
Expand Down
49 changes: 24 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
"name": "@metacall/faas",
"version": "0.1.0",
"description": "Reimplementation of MetaCall FaaS platform written in TypeScript.",
"main": "dist/index.js",
"main": "dist/server.js",
"scripts": {
"test": "npm run --silent build && mocha dist/test",
"unit": "npm run --silent test -- --ignore **/integration**",
"prepublishOnly": "npm run --silent build",
"build": "npm run --silent lint && tsc",
"lint": "eslint . --ignore-pattern dist",
"postinstall": "npm run fix && npm run build",
"fix": "eslint . --ignore-pattern dist --fix",
"lint": "eslint . --max-warnings=0 --ignore-pattern dist",
"fix": "eslint . --max-warnings=0 --ignore-pattern dist --fix",
"start": "npm run build && metacall dist/server.js"
},
"repository": {
Expand Down
70 changes: 43 additions & 27 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isIAllApps
} from './utils/utils';

import { PackageError } from '@metacall/protocol/package';
import { appsDirectory } from './utils/config';

const appsDir = appsDirectory();
Expand Down Expand Up @@ -220,49 +221,64 @@ export const fetchFileList = catchAsync(
export const deploy = catchAsync(
async (
req: Omit<Request, 'body'> & { body: deployBody },
res: Response
res: Response,
next: NextFunction
) => {
req.body.resourceType == 'Repository' && (await calculatePackages());
try {
req.body.resourceType == 'Repository' &&
(await calculatePackages(next));

// TODO Currently Deploy function will only work for workdir, we will add the addRepo
// TODO Currently Deploy function will only work for workdir, we will add the addRepo

await installDependencies();
await installDependencies();

const desiredPath = path.join(__dirname, '/worker/index.js');
const desiredPath = path.join(__dirname, '/worker/index.js');

const proc = spawn('metacall', [desiredPath], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
});
const proc = spawn('metacall', [desiredPath], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
});

proc.send({
type: protocol.l,
currentFile
});
proc.send({
type: protocol.l,
currentFile
});

proc.stdout?.on('data', (data: Buffer) => {
console.log(data.toString().green);
});
proc.stderr?.on('data', (data: Buffer) => {
console.log(data.toString().red);
});
proc.stdout?.on('data', (data: Buffer) => {
console.log(data.toString().green);
});
proc.stderr?.on('data', (data: Buffer) => {
console.log(data.toString().red);
});

proc.on('message', (data: childProcessResponse) => {
if (data.type === protocol.g) {
if (isIAllApps(data.data)) {
const appName = Object.keys(data.data)[0];
childProcesses[appName] = proc;
allApplications[appName] = data.data[appName];
}
}
});

res.status(200).json({
suffix: hostname(),
prefix: currentFile.id,
version: 'v1'
});
proc.on('message', (data: childProcessResponse) => {
if (data.type === protocol.g) {
if (isIAllApps(data.data)) {
const appName = Object.keys(data.data)[0];
cps[appName] = proc;
allApplications[appName] = data.data[appName];
}
}
});

// Handle err == PackageError.Empty, use next function for error handling
res.status(200).json({
suffix: hostname(),
prefix: currentFile.id,
version: 'v1'
});
} catch (err) {
// Check if the error is PackageError.Empty
if (err === PackageError.Empty) {
return next(err);
}
return next(err);
}
}
);

Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ export interface childProcessResponse {
type: keyof typeof protocol;
data: unknown;
}

export interface InspectObject {
[key: string]: Array<{ name: string }>;
}
2 changes: 1 addition & 1 deletion src/controller/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ensureFolderExists } from '../utils/utils';
export default (
req: Omit<Request, 'body'> & { body: deleteBody },
res: Response
) => {
): Response => {
// Extract the suffix (application name) of the application from the request body
const { suffix: app } = req.body;

Expand Down
26 changes: 13 additions & 13 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { NextFunction, Request, RequestHandler, Response } from 'express';
import {
createInstallDependenciesScript,
currentFile,
IAllApps
IAllApps,
InspectObject
} from '../constants';

export const dirName = (gitUrl: string): string =>
Expand All @@ -31,10 +32,12 @@ export const installDependencies = async (): Promise<void> => {
};

//check if repo contains metacall-*.json if not create and calculate runners then install dependencies
export const calculatePackages = async (): Promise<void> => {
export const calculatePackages = async (next: NextFunction): Promise<void> => {
const data = await generatePackage(currentFile.path);

if (data.error == PackageError.Empty) throw PackageError.Empty;
if (data.error == PackageError.Empty) {
return next(new Error(PackageError.Empty));
}
// currentFile.jsons = JSON.parse(data.jsons.toString()); FIXME Fix this line
currentFile.runners = data.runners;
};
Expand Down Expand Up @@ -119,23 +122,20 @@ export const getLangId = (input: string): LanguageId => {
return extension as LanguageId;
};

//eslint-disable-next-line
export const diff = (object1: any, object2: any): any => {
export const diff = (
object1: InspectObject,
object2: InspectObject
): InspectObject => {
for (const key in object2) {
//eslint-disable-next-line
if (Array.isArray(object2[key])) {
//eslint-disable-next-line
object1[key] = object1[key].filter(
(
item: any // eslint-disable-line
) => !object2[key].find((i: any) => i.name === item.name) // eslint-disable-line
item => !object2[key].find(i => i.name === item.name)
);
}
}

return object1; // eslint-disable-line
return object1;
};

export function isIAllApps(data: any): data is IAllApps {
export function isIAllApps(data: unknown): data is IAllApps {
return typeof data === 'object' && data !== null;
}

0 comments on commit 846394e

Please sign in to comment.