Socket
Socket
Sign inDemoInstall

@foal/core

Package Overview
Dependencies
7
Maintainers
1
Versions
96
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.3.1 to 1.5.0

lib/common/utils/500.debug.html

2

lib/common/hooks/validate-body.hook.js

@@ -37,3 +37,3 @@ "use strict";

}
const requestBody = is_function_util_1.isFunction(schema) ? c => makeRequestBody(schema(c)) : makeRequestBody(schema);
const requestBody = is_function_util_1.isFunction(schema) ? (c) => makeRequestBody(schema(c)) : makeRequestBody(schema);
if (propertyKey) {

@@ -40,0 +40,0 @@ openapi_1.ApiRequestBody(requestBody)(target, propertyKey);

@@ -47,3 +47,3 @@ "use strict";

}
const apiCookieParameter = is_function_util_1.isFunction(schema) ? c => makeParameter(schema(c)) : makeParameter(schema);
const apiCookieParameter = is_function_util_1.isFunction(schema) ? (c) => makeParameter(schema(c)) : makeParameter(schema);
openapi_1.ApiParameter(apiCookieParameter)(target, propertyKey);

@@ -50,0 +50,0 @@ openapi_1.ApiResponse(400, { description: 'Bad request.' })(target, propertyKey);

@@ -48,3 +48,3 @@ "use strict";

}
const apiHeaderParameter = is_function_util_1.isFunction(schema) ? c => makeParameter(schema(c)) : makeParameter(schema);
const apiHeaderParameter = is_function_util_1.isFunction(schema) ? (c) => makeParameter(schema(c)) : makeParameter(schema);
openapi_1.ApiParameter(apiHeaderParameter)(target, propertyKey);

@@ -51,0 +51,0 @@ openapi_1.ApiResponse(400, { description: 'Bad request.' })(target, propertyKey);

@@ -41,3 +41,3 @@ "use strict";

}
const apiPathParameter = is_function_util_1.isFunction(schema) ? c => makeParameter(schema(c)) : makeParameter(schema);
const apiPathParameter = is_function_util_1.isFunction(schema) ? (c) => makeParameter(schema(c)) : makeParameter(schema);
openapi_1.ApiParameter(apiPathParameter)(target, propertyKey);

@@ -44,0 +44,0 @@ openapi_1.ApiResponse(400, { description: 'Bad request.' })(target, propertyKey);

@@ -47,3 +47,3 @@ "use strict";

}
const apiQueryParameter = is_function_util_1.isFunction(schema) ? c => makeParameter(schema(c)) : makeParameter(schema);
const apiQueryParameter = is_function_util_1.isFunction(schema) ? (c) => makeParameter(schema(c)) : makeParameter(schema);
openapi_1.ApiParameter(apiQueryParameter)(target, propertyKey);

@@ -50,0 +50,0 @@ openapi_1.ApiResponse(400, { description: 'Bad request.' })(target, propertyKey);

@@ -8,5 +8,5 @@ /**

* @export
* @param {object} object - The object which contains the property to escape.
* @param {any} object - The object which contains the property to escape.
* @param {string} propName - The property name.
*/
export declare function escapeProp(object: object, propName: string): void;
export declare function escapeProp(object: any, propName: string): void;

@@ -11,3 +11,3 @@ "use strict";

* @export
* @param {object} object - The object which contains the property to escape.
* @param {any} object - The object which contains the property to escape.
* @param {string} propName - The property name.

@@ -14,0 +14,0 @@ */

@@ -7,4 +7,5 @@ export { controller } from './controller.util';

export { isInFile } from './is-in-file.util';
export { renderError } from './render-error.util';
export { render } from './render.util';
export { validate } from './validate.util';
export { verifyPassword } from './verify-password.util';

@@ -15,2 +15,4 @@ "use strict";

exports.isInFile = is_in_file_util_1.isInFile;
var render_error_util_1 = require("./render-error.util");
exports.renderError = render_error_util_1.renderError;
var render_util_1 = require("./render.util");

@@ -17,0 +19,0 @@ exports.render = render_util_1.render;

@@ -9,6 +9,6 @@ import { HttpResponseOK } from '../../core';

* @param {string} template - The template.
* @param {object} locals - The variables required by the template.
* @param {any} locals - The variables required by the template.
* @returns {string} The rendered template.
*/
export declare function renderToString(template: string, locals: object): string;
export declare function renderToString(template: string, locals: any): string;
/**

@@ -15,0 +15,0 @@ * Render a template in a new HttpResponseOK object.

@@ -16,3 +16,3 @@ "use strict";

* @param {string} template - The template.
* @param {object} locals - The variables required by the template.
* @param {any} locals - The variables required by the template.
* @returns {string} The rendered template.

@@ -19,0 +19,0 @@ */

@@ -16,3 +16,3 @@ /**

secure?: boolean;
sameSite?: 'strict' | 'lax';
sameSite?: 'strict' | 'lax' | 'none';
}

@@ -19,0 +19,0 @@ /**

@@ -45,3 +45,5 @@ "use strict";

}
controllerOrServiceDependencies.forEach(dep => service[dep.propertyKey] = serviceManager.get(dep.serviceClass));
controllerOrServiceDependencies.forEach(dep => {
service[dep.propertyKey] = serviceManager.get(dep.serviceClass);
});
return service;

@@ -48,0 +50,0 @@ }

import * as express from 'express';
import { Class } from '../core';
interface ExpressApplication extends express.Express {
export interface ExpressApplication extends express.Express {
[name: string]: any;
}
interface ExpressOptions {
export interface CreateAppOptions {
expressInstance?: ExpressApplication;
methods?: {
handleError?: boolean;
};
preMiddlewares?: (express.RequestHandler | express.ErrorRequestHandler)[];

@@ -15,7 +18,9 @@ postMiddlewares?: (express.RequestHandler | express.ErrorRequestHandler)[];

* @export
* @param {Class} rootControllerClass - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|ExpressOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares.
* @param {Class} AppController - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|CreateAppOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares or other settings.
* @param {ExpressApplication} [expressInstanceOrOptions.expressInstance] - Express instance to be used as base for the
* returned application.
* @param {boolean} [expressInstanceOrOptions.methods.handleError] - Specifies if AppController.handleError should be
* used to handle errors.
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.preMiddlewares] Express

@@ -27,3 +32,3 @@ * middlewares to be executed before the controllers and hooks.

*/
export declare function createApp(rootControllerClass: Class, expressInstanceOrOptions?: ExpressApplication | ExpressOptions): ExpressApplication;
export declare function createApp(AppController: Class, expressInstanceOrOptions?: ExpressApplication | CreateAppOptions): ExpressApplication;
/**

@@ -33,5 +38,9 @@ * Create an Express application from the root controller and call its "init" method if it exists.

* @export
* @param {Class} rootControllerClass - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|ExpressOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares.
* @param {Class} AppController - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|CreateAppOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares or other settings.
* @param {ExpressApplication} [expressInstanceOrOptions.expressInstance] - Express instance to be used as base for the
* returned application.
* @param {boolean} [expressInstanceOrOptions.methods.handleError] - Specifies if AppController.handleError should be
* used to handle errors.
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.preMiddlewares] Express

@@ -43,3 +52,2 @@ * middlewares to be executed before the controllers and hooks.

*/
export declare function createAndInitApp(rootControllerClass: Class, expressInstanceOrOptions?: ExpressApplication | ExpressOptions): Promise<ExpressApplication>;
export {};
export declare function createAndInitApp(AppController: Class, expressInstanceOrOptions?: ExpressApplication | CreateAppOptions): Promise<ExpressApplication>;

@@ -12,2 +12,31 @@ "use strict";

const not_found_1 = require("./not-found");
function handleJsonErrors(err, req, res, next) {
if (err.type !== 'entity.parse.failed') {
next(err);
return;
}
res.status(err.status).send({
body: err.body,
message: err.message
});
}
function protectionHeaders(req, res, next) {
res.removeHeader('X-Powered-By');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-DNS-Prefetch-Control', 'off');
res.setHeader('X-Download-Options', 'noopen');
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
res.setHeader('X-XSS-Protection', '1; mode=block');
res.setHeader('Strict-Transport-Security', 'max-age=15552000; includeSubDomains');
next();
}
function getOptions(expressInstanceOrOptions) {
if (!expressInstanceOrOptions) {
return {};
}
if (typeof expressInstanceOrOptions === 'function') {
return { expressInstance: expressInstanceOrOptions };
}
return expressInstanceOrOptions;
}
/**

@@ -17,7 +46,9 @@ * Create an Express application from the root controller.

* @export
* @param {Class} rootControllerClass - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|ExpressOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares.
* @param {Class} AppController - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|CreateAppOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares or other settings.
* @param {ExpressApplication} [expressInstanceOrOptions.expressInstance] - Express instance to be used as base for the
* returned application.
* @param {boolean} [expressInstanceOrOptions.methods.handleError] - Specifies if AppController.handleError should be
* used to handle errors.
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.preMiddlewares] Express

@@ -29,80 +60,41 @@ * middlewares to be executed before the controllers and hooks.

*/
function createApp(rootControllerClass, expressInstanceOrOptions) {
let app = express();
if (expressInstanceOrOptions && typeof expressInstanceOrOptions === 'function') {
app = expressInstanceOrOptions;
function createApp(AppController, expressInstanceOrOptions) {
const options = getOptions(expressInstanceOrOptions);
const app = options.expressInstance || express();
// Add optional pre-middlewares.
for (const middleware of options.preMiddlewares || []) {
app.use(middleware);
}
if (expressInstanceOrOptions && typeof expressInstanceOrOptions === 'object') {
if (expressInstanceOrOptions.expressInstance) {
app = expressInstanceOrOptions.expressInstance;
}
for (const middleware of expressInstanceOrOptions.preMiddlewares || []) {
app.use(middleware);
}
}
const LOG_FORMAT_NONE = 'none';
// Log requests.
const loggerFormat = core_1.Config.get('settings.loggerFormat', '[:date] ":method :url HTTP/:http-version" :status - :response-time ms');
if (loggerFormat !== LOG_FORMAT_NONE) {
if (loggerFormat !== 'none') {
app.use(logger(loggerFormat));
}
app.use((_, res, next) => {
res.removeHeader('X-Powered-By');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-DNS-Prefetch-Control', 'off');
res.setHeader('X-Download-Options', 'noopen');
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
res.setHeader('X-XSS-Protection', '1; mode=block');
res.setHeader('Strict-Transport-Security', 'max-age=15552000; includeSubDomains');
next();
});
app.use(protectionHeaders);
// Serve static files.
app.use(core_1.Config.get('settings.staticPathPrefix', ''), express.static(core_1.Config.get('settings.staticPath', 'public')));
app.use(express.json());
app.use((err, req, res, next) => {
if (err.type !== 'entity.parse.failed') {
next(err);
return;
}
res.status(err.status).send({
body: err.body,
message: err.message
});
});
app.use(express.urlencoded({ extended: false }));
app.use(express.text({ type: ['text/*', 'application/graphql'] }));
// Parse request body.
const limit = core_1.Config.get('settings.bodyParser.limit');
app.use(express.json({ limit }));
app.use(handleJsonErrors);
app.use(express.urlencoded({ extended: false, limit }));
app.use(express.text({ type: ['text/*', 'application/graphql'], limit }));
// Parse cookies.
app.use(cookieParser());
// Create the service and controller manager.
const services = new core_1.ServiceManager();
app.foal = { services };
const routes = core_1.makeControllerRoutes('', [], rootControllerClass, services);
// Resolve the controllers and hooks and add them to the express instance.
const routes = core_1.makeControllerRoutes('', [], AppController, services);
for (const route of routes) {
switch (route.httpMethod) {
case 'DELETE':
app.delete(route.path, create_middleware_1.createMiddleware(route, services));
break;
case 'GET':
app.get(route.path, create_middleware_1.createMiddleware(route, services));
break;
case 'PATCH':
app.patch(route.path, create_middleware_1.createMiddleware(route, services));
break;
case 'POST':
app.post(route.path, create_middleware_1.createMiddleware(route, services));
break;
case 'PUT':
app.put(route.path, create_middleware_1.createMiddleware(route, services));
break;
case 'HEAD':
app.head(route.path, create_middleware_1.createMiddleware(route, services));
break;
case 'OPTIONS':
app.options(route.path, create_middleware_1.createMiddleware(route, services));
break;
}
app[route.httpMethod.toLowerCase()](route.path, create_middleware_1.createMiddleware(route, services));
}
if (expressInstanceOrOptions && typeof expressInstanceOrOptions === 'object') {
for (const middleware of expressInstanceOrOptions.postMiddlewares || []) {
app.use(middleware);
}
// Add optional post-middlewares.
for (const middleware of options.postMiddlewares || []) {
app.use(middleware);
}
// Handle errors.
app.use(not_found_1.notFound());
app.use(handle_errors_1.handleErrors(core_1.Config.get('settings.debug', false), console.error));
const controller = app.foal.services.get(AppController);
app.use(handle_errors_1.handleErrors(options, controller));
return app;

@@ -115,5 +107,9 @@ }

* @export
* @param {Class} rootControllerClass - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|ExpressOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares.
* @param {Class} AppController - The root controller, usually called `AppController` and located in `src/app`.
* @param {(ExpressApplication|CreateAppOptions)} [expressInstanceOrOptions] - Express instance or options containaining
* Express middlewares or other settings.
* @param {ExpressApplication} [expressInstanceOrOptions.expressInstance] - Express instance to be used as base for the
* returned application.
* @param {boolean} [expressInstanceOrOptions.methods.handleError] - Specifies if AppController.handleError should be
* used to handle errors.
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.preMiddlewares] Express

@@ -125,5 +121,5 @@ * middlewares to be executed before the controllers and hooks.

*/
async function createAndInitApp(rootControllerClass, expressInstanceOrOptions) {
const app = createApp(rootControllerClass, expressInstanceOrOptions);
const controller = app.foal.services.get(rootControllerClass);
async function createAndInitApp(AppController, expressInstanceOrOptions) {
const app = createApp(AppController, expressInstanceOrOptions);
const controller = app.foal.services.get(AppController);
if (controller.init) {

@@ -130,0 +126,0 @@ await controller.init();

@@ -0,1 +1,2 @@

import { RequestHandler } from 'express';
import { Route, ServiceManager } from '../core';

@@ -10,2 +11,2 @@ /**

*/
export declare function createMiddleware(route: Route, services: ServiceManager): (...args: any[]) => any;
export declare function createMiddleware(route: Route, services: ServiceManager): RequestHandler;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// FoalTS
const core_1 = require("../core");
const send_response_1 = require("./send-response");
/**

@@ -16,2 +18,3 @@ * Create an express middleware from a Route and the application services.

const ctx = new core_1.Context(req);
req.foal = { ctx };
let response;

@@ -38,26 +41,3 @@ const hookPostFunctions = [];

}
res.status(response.statusCode);
res.set(response.getHeaders());
const cookies = response.getCookies();
// tslint:disable-next-line:forin
for (const cookieName in cookies) {
const options = cookies[cookieName].options;
if (options.maxAge !== undefined) {
// Convert seconds to milliseconds to make it work with Express.
options.maxAge = options.maxAge * 1000;
}
res.cookie(cookieName, cookies[cookieName].value, options);
}
if (core_1.isHttpResponseRedirect(response) || core_1.isHttpResponseMovedPermanently(response)) {
res.redirect(response.statusCode, response.path);
return;
}
if (typeof response.body === 'number') {
response.body = response.body.toString();
}
if (response.stream === true) {
response.body.pipe(res);
return;
}
res.send(response.body);
send_response_1.sendResponse(response, res);
}

@@ -64,0 +44,0 @@ catch (err) {

@@ -0,1 +1,3 @@

import { ErrorRequestHandler } from 'express';
import { CreateAppOptions } from './create-app';
/**

@@ -5,9 +7,10 @@ * Create an express middleware to return a 500 HTML page if an error is thrown and is not caught.

* @export
* @param {boolean} debug - Specify if the error stack should be included in the page.
* @param {CreateAppOptions} options - Options supplied to `createApp`.
* @param {*} appController - Instance of the root controller class (App).
* @param {*} [logFn=console.error]
* @returns The express middleware.
* @returns {ErrorRequestHandler}
*/
export declare function handleErrors(debug: boolean, logFn?: {
export declare function handleErrors(options: CreateAppOptions, appController: any, logFn?: {
(message?: any, ...optionalParams: any[]): void;
(message?: any, ...optionalParams: any[]): void;
}): (err: any, req: any, res: any, next: any) => Promise<void>;
}): ErrorRequestHandler;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const path_1 = require("path");
const util_1 = require("util");
const render_util_1 = require("../common/utils/render.util");
const page500 = '<html><head><title>INTERNAL SERVER ERROR</title></head><body>'
+ '<h1>500 - INTERNAL SERVER ERROR</h1></body></html>';
// FoalTS
const common_1 = require("../common");
const core_1 = require("../core");
const send_response_1 = require("./send-response");
/**

@@ -13,7 +11,8 @@ * Create an express middleware to return a 500 HTML page if an error is thrown and is not caught.

* @export
* @param {boolean} debug - Specify if the error stack should be included in the page.
* @param {CreateAppOptions} options - Options supplied to `createApp`.
* @param {*} appController - Instance of the root controller class (App).
* @param {*} [logFn=console.error]
* @returns The express middleware.
* @returns {ErrorRequestHandler}
*/
function handleErrors(debug, logFn = console.error) {
function handleErrors(options, appController, logFn = console.error) {
return async (err, req, res, next) => {

@@ -25,14 +24,18 @@ if (err.expose && err.status) {

logFn(err.stack);
if (!debug) {
res.status(500).send(page500);
return;
const ctx = req.foal ? req.foal.ctx : new core_1.Context(req);
let response;
if (options.methods && options.methods.handleError && appController.handleError) {
try {
response = await appController.handleError(err, ctx);
}
catch (error) {
response = await common_1.renderError(err, ctx);
}
}
const template = await util_1.promisify(fs_1.readFile)(path_1.join(__dirname, '500.debug.html'), 'utf8');
res.status(500).send(render_util_1.renderToString(template, {
message: err.message,
name: err.name,
stack: err.stack,
}));
else {
response = await common_1.renderError(err, ctx);
}
send_response_1.sendResponse(response, res);
};
}
exports.handleErrors = handleErrors;

@@ -1,1 +0,1 @@

export { createApp, createAndInitApp } from './create-app';
export { createApp, createAndInitApp, ExpressApplication } from './create-app';

@@ -0,1 +1,2 @@

import { RequestHandler } from 'express';
/**

@@ -7,2 +8,2 @@ * Create an express middleware to display a 404 HTML page.

*/
export declare function notFound(): (req: any, res: any) => void;
export declare function notFound(): RequestHandler;
/**
* FoalTS
* Copyright(c) 2017-2019 Loïc Poullain <loic.poullain@centraliens.net>
* Copyright(c) 2017-2020 Loïc Poullain <loic.poullain@centraliens.net>
* Released under the MIT License.

@@ -5,0 +5,0 @@ */

"use strict";
/**
* FoalTS
* Copyright(c) 2017-2019 Loïc Poullain <loic.poullain@centraliens.net>
* Copyright(c) 2017-2020 Loïc Poullain <loic.poullain@centraliens.net>
* Released under the MIT License.

@@ -6,0 +6,0 @@ */

@@ -21,3 +21,3 @@ /**

private modified;
constructor(sessionID: string, sessionContent: object, createdAt: number);
constructor(sessionID: string, sessionContent: any, createdAt: number);
/**

@@ -24,0 +24,0 @@ * Return true if an element was added/replaces in the session

{
"name": "@foal/core",
"version": "1.3.1",
"description": "High level web framework to create enterprise-grade Node.JS applications.",
"version": "1.5.0",
"description": "A Node.js and TypeScript framework, all-inclusive.",
"main": "./lib/index.js",

@@ -88,3 +88,3 @@ "types": "./lib/index.d.ts",

"devDependencies": {
"@foal/ejs": "^1.3.1",
"@foal/ejs": "^1.5.0",
"@types/mocha": "~2.2.43",

@@ -110,3 +110,3 @@ "@types/node": "~10.1.2",

},
"gitHead": "4c486fbd9dc2a0a62e64b04d6178dd441d7fd106"
"gitHead": "686e16a63d3417f05afbdf7feab9462aa5dde777"
}

@@ -9,3 +9,3 @@ <p align="center">

<p align="center">
<i>A Web framework to create enterprise-grade Node.JS applications</i>
<i>A Node.js and TypeScript framework, all-inclusive.</i>
<br>

@@ -12,0 +12,0 @@ <br>

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc