Dependencies upgrade (#510)

This commit is contained in:
Montassar Ghanmy
2024-05-07 17:01:56 +01:00
committed by GitHub
parent 0487ceb7b3
commit 3b20806622
15 changed files with 5080 additions and 5846 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ export interface authenticateDecorator {
declare module "fastify" {
interface FastifyInstance {
authenticate(): void;
authenticateOptional(): void;
authenticate(request: FastifyRequest): void;
authenticateOptional(request: FastifyRequest): void;
io: SocketIO.Server;
}
@@ -9,7 +9,6 @@ export type TdriveLogger = pino.Logger;
export const logger = pino({
name: "TdriveApp",
level: config.get("level", "info") || "info",
prettyPrint: false,
mixin() {
return executionStorage.getStore() ? executionStorage.getStore() : {};
},
@@ -1,5 +1,5 @@
import { FastifyPluginCallback, FastifyRequest } from "fastify";
import fastifyJwt from "fastify-jwt";
import fastifyJwt from "@fastify/jwt";
import cookie from "@fastify/cookie";
import fp from "fastify-plugin";
import config from "../../../../config";
@@ -1,6 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types */
import { EntityDefinition } from "../types";
type EntityOption = {
type?: string;
ttl?: number;
@@ -18,11 +16,10 @@ type EntityOption = {
export function Entity(name: string, options: EntityOption): ClassDecorator {
// eslint-disable-next-line @typescript-eslint/ban-types
return function (constructor: Function): void {
const entityDefinition: EntityDefinition = {
constructor.prototype._entity = {
name,
type: options.type || name,
options,
};
constructor.prototype._entity = entityDefinition;
};
}
@@ -115,6 +115,10 @@ export default class StorageService extends TdriveService<StorageAPI> implements
const chunks = options?.totalChunks || 1;
let count = 1;
//check that the file a really exists
await self._read(options?.totalChunks ? `${path}/chunk${count}` : path);
let stream: any;
async function factory(callback: (err?: Error, stream?: Stream) => unknown) {
if (count > chunks) {
@@ -136,6 +140,7 @@ export default class StorageService extends TdriveService<StorageAPI> implements
if (decipher) {
stream = stream.pipe(decipher);
}
callback(null, stream);
} catch (err) {
logger.error(err);
callback(err, null);
@@ -163,6 +168,7 @@ export default class StorageService extends TdriveService<StorageAPI> implements
stream = stream.pipe(decipher);
} catch (err) {
logger.error("Unable to createDecipheriv: %s", err);
throw err;
}
}
return stream;
@@ -1,19 +1,20 @@
import { logger, TdriveService } from "../../framework";
import { Server, IncomingMessage, ServerResponse } from "http";
import { FastifyInstance, fastify } from "fastify";
import sensible from "fastify-sensible";
import { FastifyInstance, fastify, FastifyRegisterOptions, FastifyServerOptions } from "fastify";
import sensible from "@fastify/sensible";
import multipart from "fastify-multipart";
import formbody from "@fastify/formbody";
import fastifyStatic from "@fastify/static";
import corsPlugin, { FastifyCorsOptions } from "fastify-cors";
import corsPlugin, { FastifyCorsOptions } from "@fastify/cors";
import { serverErrorHandler } from "./error";
import WebServerAPI from "./provider";
import jwtPlugin from "../auth/web/jwt";
import path from "path";
import swaggerPlugin from "fastify-swagger";
import swaggerPlugin, { FastifySwaggerOptions } from "@fastify/swagger";
import { SkipCLI } from "../../framework/decorators/skip";
import fs from "fs";
import { ExecutionContext, executionStorage } from "../../framework/execution-storage";
import { FastifyListenOptions } from "fastify/types/instance";
export default class WebServerService extends TdriveService<WebServerAPI> implements WebServerAPI {
name = "webserver";
@@ -102,9 +103,11 @@ export default class WebServerService extends TdriveService<WebServerAPI> implem
staticCSP: false,
transformStaticCSP: header => header,
exposeRoute: true,
});
} as FastifyRegisterOptions<FastifySwaggerOptions>);
this.server.register(jwtPlugin);
this.server.register(sensible, { errorHandler: false });
this.server.register(sensible, {
errorHandler: false,
} as FastifyRegisterOptions<FastifyServerOptions>);
this.server.register(multipart);
this.server.register(formbody);
this.server.register(corsPlugin, this.configuration.get<FastifyCorsOptions>("cors", {}));
@@ -138,7 +141,10 @@ export default class WebServerService extends TdriveService<WebServerAPI> implem
res.type("text/html").send(stream);
});
await this.server.listen(this.configuration.get<number>("port", 3000), "0.0.0.0");
await this.server.listen({
port: this.configuration.get<number>("port", 3000),
host: "0.0.0.0",
} as FastifyListenOptions);
this.server.ready(err => {
if (err) throw err;
@@ -1,4 +1,4 @@
import { FastifyInstance, FastifyReply, FastifyRequest, HTTPMethods } from "fastify";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import _ from "lodash";
import { v4 } from "uuid";
import { CrudException } from "../../../../core/platform/framework/api/crud-service";
@@ -155,7 +155,7 @@ export class ApplicationsApiController {
fastify.inject(
{
method: request.method as HTTPMethods,
method: request.method as any,
url: route,
payload: request.body as any,
headers: _.pick(request.headers, "authorization"),
@@ -20,7 +20,7 @@ export class FileController {
if (request.isMultipart()) {
file = await request.file();
}
const q = request.query;
const q: any = request.query;
const options: UploadOptions = {
totalChunks: parseInt(q.resumableTotalChunks || q.total_chunks) || 1,
totalSize: parseInt(q.resumableTotalSize || q.total_size) || 0,
@@ -126,7 +126,7 @@ export class FileController {
file = await request.file();
}
const q = request.query;
const q: any = request.query;
const options: any = {
totalChunks: parseInt(q.resumableTotalChunks || q.total_chunks) || 1,
chunkNumber: parseInt(q.resumableChunkNumber || q.chunk_number) || 1,
@@ -22,7 +22,6 @@ import {
WorkspaceBaseRequest,
WorkspaceInviteDomainBody,
WorkspaceRequest,
WorkspaceUsersBaseRequest,
WorkspaceUsersRequest,
} from "./types";
import { WorkspaceUsersCrudController } from "./controllers/workspace-users";
@@ -52,25 +51,28 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
}
};
const companyCheck = async (request: FastifyRequest<{ Params: WorkspaceBaseRequest }>) => {
await checkUserBelongsToCompany(request.currentUser.id, request.params.company_id);
const companyCheck = async (request: FastifyRequest) => {
await checkUserBelongsToCompany(
request.currentUser.id,
(request.params as WorkspaceBaseRequest).company_id,
);
};
const checkWorkspace = async (request: FastifyRequest<{ Params: WorkspaceUsersBaseRequest }>) => {
const checkWorkspace = async (request: FastifyRequest) => {
const params = request.params as WorkspaceUsersRequest;
const workspace = await gr.services.workspaces.get({
company_id: request.params.company_id,
id: request.params.workspace_id,
company_id: params.company_id,
id: params.workspace_id,
});
if (!workspace) {
throw fastify.httpErrors.notFound(`Workspace ${request.params.workspace_id} not found`);
throw fastify.httpErrors.notFound(`Workspace ${params.workspace_id} not found`);
}
};
const checkUserWorkspace = async (
request: FastifyRequest<{ Params: WorkspaceUsersRequest }>,
): Promise<WorkspaceUser> => {
const companyId = request.params.workspace_id;
const workspaceId = request.params.workspace_id;
const checkUserWorkspace = async (request: FastifyRequest): Promise<WorkspaceUser> => {
const params = request.params as WorkspaceUsersRequest;
const companyId = params.workspace_id;
const workspaceId = params.workspace_id;
const userId = request.currentUser.id;
const workspaceUser = await gr.services.workspaces.getUser({
workspaceId,
@@ -91,14 +93,12 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
return workspaceUser;
};
const checkUserHasCompanyMemberLevel = async (
request: FastifyRequest<{ Params: WorkspaceUsersRequest }>,
) => {
const checkUserHasCompanyMemberLevel = async (request: FastifyRequest) => {
if (!request.currentUser.id) {
throw fastify.httpErrors.forbidden("You must be authenticated");
}
const companyUser = await gr.services.companies.getCompanyUser(
{ id: request.params.company_id },
{ id: (request.params as WorkspaceUsersRequest).company_id },
{ id: request.currentUser.id },
);
@@ -107,15 +107,14 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
}
};
const checkUserIsWorkspaceAdmin = async (
request: FastifyRequest<{ Params: WorkspaceUsersRequest }>,
) => {
const checkUserIsWorkspaceAdmin = async (request: FastifyRequest) => {
if (!request.currentUser.id) {
throw fastify.httpErrors.forbidden("You must be authenticated");
}
const workspaceUser = await checkUserWorkspace(request);
const params = request.params as WorkspaceUsersRequest;
const companyUser = await gr.services.companies.getCompanyUser(
{ id: request.params.company_id },
{ id: params.company_id },
{ id: request.currentUser.id },
);
if (!hasWorkspaceAdminLevel(workspaceUser?.role, companyUser?.role)) {
@@ -123,15 +122,13 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
}
};
const checkUserIsWorkspaceMember = async (
request: FastifyRequest<{ Params: WorkspaceUsersRequest }>,
) => {
const checkUserIsWorkspaceMember = async (request: FastifyRequest) => {
if (!request.currentUser.id) {
throw fastify.httpErrors.forbidden("You must be authenticated");
}
const workspaceUser = await checkUserWorkspace(request);
const companyUser = await gr.services.companies.getCompanyUser(
{ id: request.params.company_id },
{ id: (request.params as WorkspaceUsersRequest).company_id },
{ id: request.currentUser.id },
);
if (!hasWorkspaceMemberLevel(workspaceUser?.role, companyUser?.role)) {
@@ -161,7 +158,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "GET",
url: `${workspacesUrl}`,
preHandler: [accessControl, companyCheck],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: getWorkspacesSchema,
handler: workspacesController.list.bind(workspacesController),
});
@@ -170,7 +167,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "GET",
url: `${workspacesUrl}/:id`,
preHandler: [accessControl, companyCheck],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: getWorkspaceSchema,
handler: workspacesController.get.bind(workspacesController),
});
@@ -185,7 +182,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "POST",
url: `${workspacesUrl}`,
preHandler: [accessControl, companyCheck],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: createWorkspaceSchema,
handler: workspacesController.save.bind(workspacesController),
});
@@ -194,7 +191,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "POST",
url: `${workspacesUrl}/:id`,
preHandler: [accessControl, companyCheck],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: updateWorkspaceSchema,
handler: workspacesController.save.bind(workspacesController),
});
@@ -203,7 +200,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "DELETE",
url: `${workspacesUrl}/:id`,
preHandler: [accessControl, companyCheck],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
handler: workspacesController.delete.bind(workspacesController),
});
@@ -211,7 +208,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "POST",
url: `${workspacesUrl}/:id/invite_domain`,
preHandler: [validateDomain, companyCheck],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
handler: workspacesController.setInviteDomain.bind(workspacesController),
});
@@ -219,7 +216,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "GET",
url: `${workspaceUsersUrl}`,
preHandler: [accessControl, companyCheck, checkWorkspace],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: getWorkspaceUsersSchema,
handler: workspaceUsersController.list.bind(workspaceUsersController),
});
@@ -228,7 +225,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "GET",
url: `${workspaceUsersUrl}/:user_id`,
preHandler: [accessControl, companyCheck, checkWorkspace],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: getWorkspaceUserSchema,
handler: workspaceUsersController.get.bind(workspaceUsersController),
});
@@ -237,7 +234,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "POST",
url: `${workspaceUsersUrl}`,
preHandler: [accessControl, companyCheck, checkUserIsWorkspaceAdmin],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: createWorkspaceUserSchema,
handler: workspaceUsersController.save.bind(workspaceUsersController),
});
@@ -246,7 +243,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "POST",
url: `${workspaceUsersUrl}/:user_id`,
preHandler: [accessControl, companyCheck, checkUserIsWorkspaceAdmin],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: updateWorkspaceUserSchema,
handler: workspaceUsersController.save.bind(workspaceUsersController),
});
@@ -255,7 +252,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "DELETE",
url: `${workspaceUsersUrl}/:user_id`,
preHandler: [accessControl, companyCheck, checkUserIsWorkspaceAdmin],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: deleteWorkspaceUserSchema,
handler: workspaceUsersController.delete.bind(workspaceUsersController),
});
@@ -269,7 +266,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
checkUserHasCompanyMemberLevel,
checkUserIsWorkspaceMember,
],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: inviteWorkspaceUserSchema,
handler: workspaceUsersController.invite.bind(workspaceUsersController),
});
@@ -283,7 +280,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
checkUserHasCompanyMemberLevel,
checkUserIsWorkspaceMember,
],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: deleteWorkspacePendingUsersSchema,
handler: workspaceUsersController.deletePending.bind(workspaceUsersController),
});
@@ -297,7 +294,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
checkUserHasCompanyMemberLevel,
checkUserIsWorkspaceMember,
],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: getWorkspacePendingUsersSchema,
handler: workspaceUsersController.listPending.bind(workspaceUsersController),
});
@@ -311,7 +308,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
checkUserIsWorkspaceMember,
checkUserHasCompanyMemberLevel,
],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: getWorkspaceInviteTokenSchema,
handler: workspaceInviteTokensController.list.bind(workspaceInviteTokensController),
});
@@ -325,7 +322,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
checkUserIsWorkspaceMember,
checkUserHasCompanyMemberLevel,
],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: postWorkspaceInviteTokenSchema,
handler: workspaceInviteTokensController.save.bind(workspaceInviteTokensController),
});
@@ -339,7 +336,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
checkUserIsWorkspaceMember,
checkUserHasCompanyMemberLevel,
],
preValidation: [fastify.authenticate],
preValidation: fastify.authenticate,
schema: deleteWorkspaceInviteTokenSchema,
handler: workspaceInviteTokensController.delete.bind(workspaceInviteTokensController),
});
@@ -348,7 +345,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
method: "POST",
url: "/join",
preHandler: [],
preValidation: [fastify.authenticateOptional],
preValidation: fastify.authenticateOptional,
schema: joinInviteTokenSchema,
handler: workspaceInviteTokensController.join.bind(workspaceInviteTokensController),
});
+1 -1
View File
@@ -1,5 +1,5 @@
import { FastifyReply } from "fastify";
import { HttpErrorCodes } from "fastify-sensible/lib/httpError";
import { HttpErrorCodes } from "@fastify/sensible/lib/httpError";
import { CrudException } from "../core/platform/framework/api/crud-service";
export function handleError(reply: FastifyReply, err: Error): void {