Socket
Socket
Sign inDemoInstall

server

Package Overview
Dependencies
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

server - npm Package Compare versions

Comparing version 1.0.0-alpha.42 to 1.0.0-alpha.43

src/router/generic.js

2

package.json
{
"name": "server",
"version": "1.0.0-alpha.42",
"version": "1.0.0-alpha.43",
"description": "A modern and powerful server for Node.js",

@@ -5,0 +5,0 @@ "main": "server.js",

@@ -7,3 +7,3 @@ const server = require('../../server');

it('is defined', () => {
server(parseInt(Math.random() * 10000)).then(ctx => {
server(parseInt(1000 + Math.random() * 10000)).then(ctx => {
expect(ctx.log).toBeDefined();

@@ -10,0 +10,0 @@ ctx.close();

// Perform the routing required
const join = require('../join');
const params = require('path-to-regexp-wrap')();
const generic = require('./generic');
const parse = require('./parse');
// Parse the request parameters
const parse = middle => {
const path = typeof middle[0] === 'string' ? middle.shift() : '*';
return { path, middle };
};
exports.join = join;
exports.socket = require('../../plugins/socket').router;
// Generic request handler
const generic = (method, ...all) => {
// Extracted or otherwise it'd shift once per call; also more performant
const { path, middle } = parse(all);
const match = params(path);
return async ctx => {
// A route should be solved only once per request
if (ctx.req.solved) return;
// Only for the correct method
if (method !== ctx.req.method) return;
// Only do this if the correct path
ctx.req.params = match(ctx.req.path);
if (!ctx.req.params) return;
// Perform this promise chain
await join(middle)(ctx);
ctx.req.solved = true;
if (ctx.ret && ctx.ret.res && ctx.ret.req && ctx.ret.options) {
ctx.log.warning('You should NOT return the ctx in middleware!');
}
if (ctx.ret && !ctx.res.headersSent) {
ctx.res.send(ctx.ret || '');
}
};
};
// Create a middleware that splits paths

@@ -51,4 +17,2 @@ exports.all = (...middle) => generic( 'ALL', ...middle);

exports.join = join;
exports.error = (...all) => {

@@ -73,4 +37,16 @@ // Extracted or otherwise it'd shift once per call; also more performant

exports.join = join;
exports.socket = require('../../plugins/socket').router;
exports.sub = (path, ...middle) => async ctx => {
const full = ctx.req.subdomains.join('.');
if ((typeof path === 'string' && path === full) ||
(path instanceof RegExp && path.test(full))) {
await join(middle)(ctx);
ctx.req.solved = true;
if (ctx.ret && ctx.ret.res && ctx.ret.req && ctx.ret.options) {
ctx.log.warning('You should NOT return the ctx in middleware!');
}
if (ctx.ret && !ctx.res.headersSent) {
ctx.res.send(ctx.ret || '');
}
}
};

@@ -81,15 +57,4 @@ // Allow for calling to routers that do not exist yet

if (orig[key]) return orig[key];
return (...middle) => {
const path = typeof middle[0] === 'string' ? middle.shift() : '*';
middle = join(middle);
let called;
return ctx => {
if (!called) {
called = true;
const routers = ctx.plugins.filter(p => p.name === key && p.router).map(p => p.router);
routers.forEach(router => router(ctx, path, middle));
}
}
}
throw new Error(`The router ${key} is not defined`);
}
});
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc