Socket
Socket
Sign inDemoInstall

express-js-mod

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-js-mod - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

express-js-mod/helpers/bodyParsers.js

56

express-js-mod/express-js-mod.js
import { MessageValue } from "helpers/messageTranslation";
import Response from "helpers/Response";
const validMethods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"];
import parseQuery from 'helpers/parseQuery';
import { parseJson } from "helpers/bodyParsers";
const validMethods = Object.freeze(["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]);
const Default404 = Object.freeze({ headers: Object.freeze(["Content-type", "text/html"]), body: "Resource Not Found", status: 404 });
const bodyParseMap = Object.freeze({
'application/json': parseJson,
'application/text+html': (data) => data,
'application/text': (data) => data,
'application/html': (data) => data,
'text/html': (data) => data,
});
export default class Express {

@@ -9,7 +19,7 @@ serverInstance;

routes = {
get: {},
post: {},
put: {},
patch: {},
delete: {},
get: new Map(),
post: new Map(),
put: new Map(),
patch: new Map(),
delete: new Map(),
};

@@ -38,3 +48,3 @@ constructor(ModdableHttpServer) {

state: 'status',
headers: [],
headers: {},
path: null,

@@ -47,3 +57,7 @@ httpMethod: null,

case MessageValue.status:
this.inboundRequest.path = val1;
let parsed = parseQuery(val1);
this.inboundRequest.path = parsed.path;
this.inboundRequest.params = parsed.search;
this.inboundRequest.rawQuery = parsed.query;
this.inboundRequest.location = parsed.relative;
this.inboundRequest.httpMethod = val2.toLowerCase();

@@ -55,4 +69,3 @@ this.inboundRequest.state = 'headers';

break;
this.inboundRequest.headers.push(val1);
this.inboundRequest.headers.push(val2);
this.inboundRequest.headers[`${val1}`] = val2;
break;

@@ -65,2 +78,8 @@ case MessageValue.headersComplete:

case MessageValue.requestComplete:
if (!bodyParseMap[this.inboundRequest.headers["content-type"]]) {
this.inboundRequest.data = val1;
}
else {
this.inboundRequest.data = bodyParseMap[this.inboundRequest.headers["content-type"]](val1);
}
this.inboundRequest.state = "done";

@@ -71,5 +90,8 @@ break;

const res = this.outboundResponse;
const handlerCb = self.routes[req.httpMethod][`${req.path}`].cb;
const route = self.routes[req.httpMethod].get(req.path);
if (!route)
return Default404;
const handlerCb = route.cb;
if (!handlerCb)
return { headers: ["Content-type", "text/html"], body: "Resource Not Found", status: 404 };
return Default404;
// Do stuff here with routes

@@ -80,3 +102,3 @@ let result = () => {

};
return result;
return result();
case MessageValue.responseFragment:

@@ -103,7 +125,7 @@ // do stuff here with chunked data

if (!path || !cb)
throw new Error('There was a big no-no');
let routeExist = this.routes[method][`${path}`];
throw new Error('Path or callback was not given');
let routeExist = this.routes[method].get(`${path}`);
if (routeExist)
throw new Error('There was a big no-no');
this.routes[method][path] = { cb };
throw new Error('The defined route already exists');
this.routes[method].set(path, Object.freeze({ cb }));
};

@@ -110,0 +132,0 @@ };

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

export const MessageTranslations = {
export const MessageTranslations = Object.freeze({
[-1]: "error",

@@ -12,15 +12,14 @@ 1: "connection",

10: "responseComplete"
};
export var MessageValue;
(function (MessageValue) {
MessageValue[MessageValue["error"] = -1] = "error";
MessageValue[MessageValue["connection"] = 1] = "connection";
MessageValue[MessageValue["status"] = 2] = "status";
MessageValue[MessageValue["header"] = 3] = "header";
MessageValue[MessageValue["headersComplete"] = 4] = "headersComplete";
MessageValue[MessageValue["requestFragment"] = 5] = "requestFragment";
MessageValue[MessageValue["requestComplete"] = 6] = "requestComplete";
MessageValue[MessageValue["prepareResponse"] = 8] = "prepareResponse";
MessageValue[MessageValue["responseFragment"] = 9] = "responseFragment";
MessageValue[MessageValue["responseComplete"] = 10] = "responseComplete";
})(MessageValue || (MessageValue = {}));
});
export const MessageValue = Object.freeze({
"error": -1,
"connection": 1,
"status": 2,
"header": 3,
"headersComplete": 4,
"requestFragment": 5,
"requestComplete": 6,
"prepareResponse": 8,
"responseFragment": 9,
"responseComplete": 10
});

@@ -10,4 +10,3 @@ export default class Response {

try {
let res = JSON.stringify(obj);
this.body = res;
this.body = JSON.stringify(obj);
this.headers.push('Content-Type');

@@ -18,3 +17,3 @@ this.headers.push('application/json');

trace('INVALID JSON DATA');
throw new Error('Not valid JSON');
throw new Error('INVALID JSON DATA');
}

@@ -21,0 +20,0 @@ }

{
"preload": [
"helpers/*"
],
"helpers/*",
"express-js-mod",
"http"
],
"modules": {

@@ -6,0 +8,0 @@ "*": [

{
"name": "express-js-mod",
"version": "1.0.1",
"version": "1.0.2",
"description": "An express like router for moddable sdk and XS javascript engine",

@@ -5,0 +5,0 @@ "main": "./express-js-mod/express-js-mod.js",

# Express JS Mod
This is a express like package for working in the [Moddable SDK](https://www.moddable.com/).
This is not meant to be a 1-1 port, but it attempts to maintain the same interface
we know from [express](https://expressjs.com/).

@@ -16,22 +13,5 @@

Copy the `/express-js-mod` folder into your moddable sdk project,
then make sure to add the module for the contents of the folder as follows:
then add the express-js-mod package to your projects manifest.json includes:
manifest.json
{
"include": [
... other files
"./express-js-mod/manifest.json"
],
"preload": [
... other files
"express-js-mod/*"
],
"modules": {
"*": [
... other files
"./express-js-mod/*"
],
"express-js-mod/*": "./express-js-mod/*"
}
}
`"./express-js-mod/manifest.json"`

@@ -38,0 +18,0 @@ ## Usage

@@ -49,7 +49,7 @@ import Express, { HttpServer } from '../src/express-js-mod';

expect(Object.keys(express.allRoutes.get)).toHaveLength(2)
express.allRoutes.get['/path'].cb()
expect(express.allRoutes.get.size).toBe(2)
express.allRoutes.get.get('/path').cb()
expect(fakeCbOne).toHaveBeenCalledTimes(1)
express.allRoutes.get['/hello'].cb()
express.allRoutes.get.get('/hello').cb()
expect(fakeCbTwo).toHaveBeenCalledTimes(1)

@@ -65,6 +65,6 @@ })

expect(Object.keys(express.allRoutes.get)).toHaveLength(2)
express.allRoutes.post['/path'].cb()
expect(express.allRoutes.get.size).toBe(2)
express.allRoutes.post.get('/path').cb()
expect(fakeCbOne).toHaveBeenCalledTimes(1)
express.allRoutes.post['/hello'].cb()
express.allRoutes.post.get('/hello').cb()
expect(fakeCbTwo).toHaveBeenCalledTimes(1)

@@ -71,0 +71,0 @@ })

import { MessageValue } from "helpers/messageTranslation";
import Response from "helpers/Response";
import parseQuery from 'helpers/parseQuery'
import { parseJson, parseText } from "helpers/bodyParsers";
const validMethods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
const validMethods:ReadonlyArray<String> = Object.freeze(["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])

@@ -14,3 +16,12 @@ interface RouteMap {

const Default404: Readonly<{}> = Object.freeze({ headers: Object.freeze(["Content-type", "text/html"]), body: "Resource Not Found", status: 404 })
const bodyParseMap: any = Object.freeze({
'application/json': parseJson,
'application/text+html': (data:string) => data,
'application/text': (data:string) => data,
'application/html': (data:string) => data,
'text/html': (data:string) => data,
})
export interface HttpServer {

@@ -37,7 +48,7 @@ port?: Number

private routes: RouteMap = {
get: {},
post: {},
put: {},
patch: {},
delete: {},
get: new Map(),
post: new Map(),
put: new Map(),
patch: new Map(),
delete: new Map(),
}

@@ -69,3 +80,3 @@ constructor(ModdableHttpServer: any) {

state: 'status',
headers: [],
headers: {},
path: null,

@@ -78,3 +89,7 @@ httpMethod: null,

case MessageValue.status:
this.inboundRequest.path = val1
let parsed = parseQuery(val1)
this.inboundRequest.path = parsed.path
this.inboundRequest.params = parsed.search
this.inboundRequest.rawQuery = parsed.query
this.inboundRequest.location = parsed.relative
this.inboundRequest.httpMethod = val2.toLowerCase()

@@ -85,4 +100,3 @@ this.inboundRequest.state = 'headers'

if(!val1 || !val2) break
this.inboundRequest.headers.push(val1)
this.inboundRequest.headers.push(val2)
this.inboundRequest.headers[`${val1}`] = val2
break;

@@ -95,2 +109,7 @@ case MessageValue.headersComplete:

case MessageValue.requestComplete:
if(!bodyParseMap[this.inboundRequest.headers["content-type"]]) {
this.inboundRequest.data = val1
} else {
this.inboundRequest.data = bodyParseMap[this.inboundRequest.headers["content-type"]](val1)
}
this.inboundRequest.state = "done"

@@ -101,4 +120,6 @@ break;

const res = this.outboundResponse
const handlerCb = self.routes[req.httpMethod][`${req.path}`].cb
if(!handlerCb) return { headers: ["Content-type", "text/html"], body: "Resource Not Found", status: 404 };
const route = self.routes[req.httpMethod].get(req.path)
if(!route) return Default404
const handlerCb = route.cb
if(!handlerCb) return Default404;
// Do stuff here with routes

@@ -109,3 +130,3 @@ let result = () => {

}
return result
return result()
case MessageValue.responseFragment:

@@ -135,6 +156,6 @@ // do stuff here with chunked data

return (path: string, cb: () => void) => {
if(!path || !cb) throw new Error('There was a big no-no')
let routeExist = this.routes[method][`${path}`]
if(routeExist) throw new Error('There was a big no-no')
this.routes[method][path] = {cb}
if(!path || !cb) throw new Error('Path or callback was not given')
let routeExist = this.routes[method].get(`${path}`)
if(routeExist) throw new Error('The defined route already exists')
this.routes[method].set(path, Object.freeze({cb}))
}

@@ -141,0 +162,0 @@ }

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

export const MessageTranslations:any = {
export const MessageTranslations: Readonly<any> = Object.freeze({
[-1]: "error",

@@ -13,15 +12,16 @@ 1: "connection",

10: "responseComplete"
}
})
export enum MessageValue {
"error" = -1,
"connection" = 1,
"status" = 2,
"header" = 3,
"headersComplete" = 4,
"requestFragment" = 5,
"requestComplete" = 6,
"prepareResponse" = 8,
"responseFragment" = 9,
"responseComplete" = 10
}
export const MessageValue: Readonly<any> = Object.freeze({
"error" : -1,
"connection" : 1,
"status" : 2,
"header" : 3,
"headersComplete" : 4,
"requestFragment" : 5,
"requestComplete" : 6,
"prepareResponse" : 8,
"responseFragment" : 9,
"responseComplete" : 10
})

@@ -1,7 +0,5 @@

export default class Response {
headers: String[] = [];
headers: string[] = [];
requestStatus: Number = 201;
body: String = ''
body: string = ''
status(status: Number){

@@ -13,4 +11,3 @@ this.requestStatus = status

try {
let res = JSON.stringify(obj)
this.body = res
this.body = JSON.stringify(obj)
this.headers.push('Content-Type');

@@ -20,3 +17,3 @@ this.headers.push('application/json')

trace('INVALID JSON DATA')
throw new Error('Not valid JSON')
throw new Error('INVALID JSON DATA')
}

@@ -23,0 +20,0 @@ }

{
"preload": [
"helpers/*"
],
"helpers/*",
"express-js-mod",
"http"
],
"modules": {

@@ -6,0 +8,0 @@ "*": [

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