New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

http4js

Package Overview
Dependencies
Maintainers
1
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http4js - npm Package Compare versions

Comparing version 1.2.6 to 1.2.7

5

dist/main/core/Request.d.ts

@@ -12,3 +12,2 @@ import { Body } from "./Body";

constructor(method: string, uri: Uri | string, body?: Body | string, headers?: any);
private getQueryParams();
setUri(uri: Uri | string): Request;

@@ -21,5 +20,7 @@ getHeader(name: string): string;

bodyString(): string;
query(name: string, value: string): Request;
setQuery(name: string, value: string): Request;
getQuery(name: string): string;
private getQueryParams();
private static clone(a);
}
export declare function request(method: string, uri: Uri | string, body?: Body | string, headers?: any): Request;

75

dist/main/core/Request.js

@@ -34,48 +34,43 @@ "use strict";

}
Request.prototype.getQueryParams = function () {
var _this = this;
if (util_1.isNullOrUndefined(this.uri.query))
return {};
var pairs = this.uri.query.split("&");
pairs.map(function (pair) {
var split = pair.split("=");
_this.queries[split[0]] = split[1];
});
return this.queries;
};
Request.prototype.setUri = function (uri) {
var request = Request.clone(this);
if (typeof uri == "string") {
this.uri = Uri_1.Uri.of(uri);
request.uri = Uri_1.Uri.of(uri);
}
return this;
return request;
};
Request.prototype.getHeader = function (name) {
return this.headers[name.toLowerCase()];
var request = Request.clone(this);
return request.headers[name.toLowerCase()];
};
Request.prototype.setHeader = function (name, value) {
var request = Request.clone(this);
var caseInsensitiveName = name.toLowerCase();
if (this.headers[caseInsensitiveName] == null) {
this.headers[caseInsensitiveName] = value;
if (request.headers[caseInsensitiveName] == null) {
request.headers[caseInsensitiveName] = value;
}
else if (typeof this.headers[caseInsensitiveName] == "string") {
this.headers[caseInsensitiveName] = [this.headers[caseInsensitiveName], value];
else if (typeof request.headers[caseInsensitiveName] == "string") {
request.headers[caseInsensitiveName] = [request.headers[caseInsensitiveName], value];
}
else {
this.headers[caseInsensitiveName].push(value);
request.headers[caseInsensitiveName].push(value);
}
return this;
return request;
};
Request.prototype.replaceHeader = function (name, value) {
this.headers[name] = value;
return this;
var request = Request.clone(this);
request.headers[name] = value;
return request;
};
Request.prototype.removeHeader = function (name) {
delete (this.headers[name]);
return this;
var request = Request.clone(this);
delete (request.headers[name]);
return request;
};
Request.prototype.setBody = function (body) {
var request = Request.clone(this);
typeof body == "string"
? this.body.bytes = body
: this.body = body;
return this;
? request.body.bytes = body
: request.body = body;
return request;
};

@@ -85,10 +80,26 @@ Request.prototype.bodyString = function () {

};
Request.prototype.query = function (name, value) {
this.queries[name] = value;
this.uri = this.uri.withQuery(name, value);
return this;
Request.prototype.setQuery = function (name, value) {
var request = Request.clone(this);
request.queries[name] = value;
request.uri = request.uri.withQuery(name, value);
return request;
};
Request.prototype.getQuery = function (name) {
return this.queries[name];
var request = Request.clone(this);
return request.queries[name];
};
Request.prototype.getQueryParams = function () {
var _this = this;
if (util_1.isNullOrUndefined(this.uri.query))
return {};
var pairs = this.uri.query.split("&");
pairs.map(function (pair) {
var split = pair.split("=");
_this.queries[split[0]] = split[1];
});
return this.queries;
};
Request.clone = function (a) {
return Object.assign(Object.create(a), a);
};
return Request;

@@ -95,0 +106,0 @@ }());

@@ -13,3 +13,3 @@ import { HttpMessage } from "./HttpMessage";

setHeaders(headers: object): Response;
allHeaders(headers: object): Response;
replaceAllHeaders(headers: object): Response;
replaceHeader(name: string, value: string): Response;

@@ -19,3 +19,4 @@ removeHeader(name: string): Response;

bodyString(): string;
private static clone(a);
}
export declare function response(status?: number, body?: Body | string): Response;

@@ -18,34 +18,41 @@ "use strict";

Response.prototype.setHeader = function (name, value) {
var response = Response.clone(this);
var lowercaseName = name.toLowerCase();
if (this.headers[lowercaseName] == null) {
this.headers[lowercaseName] = value;
if (response.headers[lowercaseName] == null) {
response.headers[lowercaseName] = value;
}
else if (typeof this.headers[lowercaseName] == "string") {
this.headers[lowercaseName] = [this.headers[lowercaseName], value];
else if (typeof response.headers[lowercaseName] == "string") {
response.headers[lowercaseName] = [response.headers[lowercaseName], value];
}
else {
this.headers[lowercaseName].push(value);
response.headers[lowercaseName].push(value);
}
return this;
return response;
};
Response.prototype.setHeaders = function (headers) {
this.headers = headers;
return this;
var response = Response.clone(this);
response.headers = headers;
return response;
};
Response.prototype.allHeaders = function (headers) {
return undefined;
Response.prototype.replaceAllHeaders = function (headers) {
var response = Response.clone(this);
response.headers = headers;
return response;
};
Response.prototype.replaceHeader = function (name, value) {
this.headers[name] = value;
return this;
var response = Response.clone(this);
response.headers[name] = value;
return response;
};
Response.prototype.removeHeader = function (name) {
delete this.headers[name];
return this;
var response = Response.clone(this);
delete response.headers[name];
return response;
};
Response.prototype.setBody = function (body) {
var response = Response.clone(this);
typeof body == "string"
? this.body.bytes = body
: this.body = body;
return this;
? response.body.bytes = body
: response.body = body;
return response;
};

@@ -55,2 +62,5 @@ Response.prototype.bodyString = function () {

};
Response.clone = function (a) {
return Object.assign(Object.create(a), a);
};
return Response;

@@ -57,0 +67,0 @@ }());

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

var request = new Request_1.Request("GET", baseUrl)
.query("tomQuery", "likes to party");
.setQuery("tomQuery", "likes to party");
return Client_1.HttpClient(request)

@@ -82,0 +82,0 @@ .then(function (succ) {

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

var request = new Request_1.Request("GET", baseUrl)
.query("tomQuery", "likes to party");
.setQuery("tomQuery", "likes to party");
return Client_1.HttpClient(request)

@@ -80,0 +80,0 @@ .then(function (succ) {

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

var request = new Request_1.Request("GET", baseUrl)
.query("tomQuery", "likes to party");
.setQuery("tomQuery", "likes to party");
return Client_1.HttpClient(request)

@@ -66,0 +66,0 @@ .then(function (succ) {

@@ -6,3 +6,9 @@ "use strict";

var Request_1 = require("../../main/core/Request");
var assert_2 = require("assert");
describe("in mem request", function () {
it("is immutable", function () {
var request1 = new Request_1.Request("GET", "/");
var request2 = request1.setHeader("tom", "tosh");
assert_2.notEqual(request1, request2);
});
it("set method is case insensitive", function () {

@@ -30,4 +36,4 @@ assert_1.equal(new Request_1.Request("gEt", "/")

assert_1.equal(new Request_1.Request("GET", "/tom")
.query("tom", "tosh")
.query("ben", "bosh")
.setQuery("tom", "tosh")
.setQuery("ben", "bosh")
.uri

@@ -34,0 +40,0 @@ .query, "tom=tosh&ben=bosh");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var assert = require("assert");
var assert_1 = require("assert");
var Response_1 = require("../../main/core/Response");
describe("in mem response", function () {
it("is immutable", function () {
var response1 = new Response_1.Response(200, "OK");
var response2 = response1.setHeader("tom", "tosh");
assert_1.notEqual(response1, response2);
});
it("set body", function () {

@@ -23,3 +27,3 @@ assert_1.equal(new Response_1.Response()

it("concat same header on response", function () {
assert.deepEqual(new Response_1.Response()
assert_1.deepEqual(new Response_1.Response()
.setHeader("tom", "smells")

@@ -26,0 +30,0 @@ .setHeader("tom", "smells more")

@@ -14,3 +14,3 @@ ### Table of Contents

```typescript
const upstream = getTo("/", (req: Request) => {
const upstream = routes(".*", ".*", (req: Request) => {
let response = new Response(200, req.headers);

@@ -24,4 +24,5 @@ console.log("*** UPSTREAM RESPONSE ***");

const proxy = getTo("/", (req: Request) => {
let rewrittenRequest = req.setUri("http://localhost:3001/").setHeader("x-proxy", "header from proxy");
const proxy = routes(".*", ".*", (req: Request) => {
let rewrittenRequest = req.setUri("http://localhost:3001/")
.setHeader("x-proxy", "header from proxy");
console.log("*** REWRITTEN REQUEST ***");

@@ -31,3 +32,3 @@ console.log(rewrittenRequest);

})
.asServer()
.asServer(new NativeServer(3000))
.start();

@@ -34,0 +35,0 @@

@@ -6,29 +6,56 @@ "use strict";

exports.__esModule = true;
var RoutingHttpHandler_1 = require("./src/main/core/RoutingHttpHandler");
var Client_1 = require("./src/main/core/Client");
var Response_1 = require("./src/main/core/Response");
var NativeServer_1 = require("./src/main/core/NativeServer");
__export(require("./dist/main/core/RoutingHttpHandler"));
var Status_1 = require("./src/main/core/Status");
var Request_1 = require("./dist/main/core/Request");
var Routing_1 = require("./dist/main/core/Routing");
var Response_1 = require("./dist/main/core/Response");
var Client_1 = require("./dist/main/client/Client");
var Uri_1 = require("./dist/main/core/Uri");
var Headers_1 = require("./src/main/core/Headers");
var Methods_1 = require("./src/main/core/Methods");
__export(require("./dist/main/core/Routing"));
__export(require("./dist/main/core/Request"));
__export(require("./dist/main/core/Response"));
__export(require("./dist/main/core/Client"));
__export(require("./dist/main/core/Body"));
__export(require("./dist/main/core/Uri"));
var upstream = RoutingHttpHandler_1.routes(".*", ".*", function (req) {
var response = new Response_1.Response(200, req.headers);
console.log("*** UPSTREAM RESPONSE ***");
console.log(response);
return Promise.resolve(response);
})
.asServer(new NativeServer_1.NativeServer(3001))
.start();
var proxy = RoutingHttpHandler_1.routes(".*", ".*", function (req) {
var rewrittenRequest = req.setUri("http://localhost:3001/")
.setHeader("x-proxy", "header from proxy")
.setHeader("host", req.uri.href);
console.log("*** REWRITTEN REQUEST ***");
console.log(rewrittenRequest);
return Client_1.HttpClient(rewrittenRequest);
})
__export(require("./dist/main/core/Headers"));
__export(require("./dist/main/core/Methods"));
__export(require("./dist/main/core/Filters"));
__export(require("./dist/main/core/Status"));
__export(require("./dist/main/servers/ExpressServer"));
__export(require("./dist/main/servers/KoaServer"));
__export(require("./dist/main/client/Client"));
//handler takes a request and promises a response
var handler = function (req) {
var html = "<h1>" + req.method + " to " + req.uri.href + " with req headers " + Object.keys(req.headers) + "</h1>";
return Promise.resolve(new Response_1.Response(Status_1.Status.OK, html));
};
//add header to every request
var headerFilter = function (handler) {
return function (req) {
return handler(req.setHeader(Headers_1.Headers.X_CSRF_TOKEN, Math.random()))
.then(function (response) { return response.setHeader(Headers_1.Headers.VARY, "gzip"); });
};
};
//define our server routes and start on port 3000
Routing_1.routes(Methods_1.Method.GET, ".*", handler)
.withFilter(headerFilter)
.asServer()
.start();
//make an http request to our server and log the response
Client_1.HttpClient(new Request_1.Request(Methods_1.Method.GET, Uri_1.Uri.of("http://localhost:3000/any/path"))).then(function (response) {
console.log(response);
console.log(response.bodyString());
});
/*
Response {
headers:
{ vary: 'gzip',
date: 'Sun, 08 Apr 2018 08:26:20 GMT',
connection: 'close',
'transfer-encoding': 'chunked' },
body:
Body {
bytes: <Buffer 3c 68 31 3e 47 45 54 20 74 6f 20 2f 61 6e 79 2f 70 61 74 68 20 77 69 74 68 20 72 65 71 20 68 65 61 64 65 72 73 20 68 6f 73 74 2c 63 6f 6e 6e 65 63 74 ... > },
status: 200 }
<h1>GET to /any/path with req headers host,connection,x-csrf-token</h1>
*/
{
"name": "http4js",
"version": "1.2.6",
"version": "1.2.7",
"description": "A lightweight HTTP toolkit",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -5,3 +5,3 @@ ## http4js

Read the [docs](https://tomshacham.github.io/http4js/) here
## Read the [docs](https://tomshacham.github.io/http4js/)

@@ -16,38 +16,38 @@ #### To install:

An example server and client
```typescript
import {Status} from "./src/main/core/Status";
import {Request} from "./dist/main/core/Request";
import {HttpHandler} from "./dist/main/core/HttpMessage";
import {routes} from "./dist/main/core/RoutingHttpHandler";
import {routes} from "./dist/main/core/Routing";
import {Response} from "./dist/main/core/Response";
import {HttpClient} from "./dist/main/core/Client";
import {Body} from "./dist/main/core/Body";
import {HttpClient} from "./dist/main/client/Client";
import {Uri} from "./dist/main/core/Uri";
import {Headers} from "./src/main/core/Headers";
import {Method} from "./src/main/core/Methods";
//handler takes a request and promises a response
let handler = (req: Request) => {
let html = `<h1>${req.method} to ${req.uri.href} with headers ${Object.keys(req.headers)}</h1>`;
return new Promise(resolve => resolve(new Response(200, new Body(Buffer.from(html)))));
let html = `<h1>${req.method} to ${req.uri.href} with req headers ${Object.keys(req.headers)}</h1>`;
return Promise.resolve(new Response(Status.OK, html));
};
//add header to every request
let headerFilter = (handler: HttpHandler) => {
return (req: Request) => {
return handler(req.setHeader("filter", "1"));
return handler(req.setHeader(Headers.X_CSRF_TOKEN, Math.random()))
.then(response => response.setHeader(Headers.VARY, "gzip"));
}
};
let moreRoutes = routes("/bob/{id}", "POST", (req) => {
return new Promise(resolve => {
resolve(new Response(201, `created id of ${req.path}`))
});
});
routes("GET", "/path", handler)
.withHandler("/tom", "GET", handler)
.withRoutes(moreRoutes)
//define our server routes and start on port 3000
routes(Method.GET, ".*", handler)
.withFilter(headerFilter)
.asServer(3000)
.asServer()
.start();
//make an http request to our server and log the response
HttpClient(
new Request("GET", Uri.of("http://localhost:3000/path/tom"))
new Request(Method.GET, Uri.of("http://localhost:3000/{id}/path"))
).then(response => {

@@ -57,15 +57,15 @@ console.log(response);

});
/*
Response {
headers:
{ date: 'Sun, 25 Mar 2018 11:15:12 GMT',
connection: 'close',
'transfer-encoding': 'chunked' },
body:
Body {
bytes: <Buffer 3c 68 31 3e 47 45 54 20 74 6f 20 2f 70 61 74 68 2f 74 6f 6d 20 77 69 74 68 20 68 65 61 64 65 72 73 20 68 6f 73 74 2c 63 6f 6e 6e 65 63 74 69 6f 6e 2c ... > },
status: 200 }
<h1>GET to /path/tom with headers host,connection,filter</h1>
headers:
{ vary: 'gzip',
date: 'Sun, 08 Apr 2018 08:26:20 GMT',
connection: 'close',
'transfer-encoding': 'chunked' },
body:
Body {
bytes: <Buffer 3c 68 31 3e 47 45 54 20 74 6f 20 2f 61 6e 79 2f 70 61 74 68 20 77 69 74 68 20 72 65 71 20 68 65 61 64 65 72 73 20 68 6f 73 74 2c 63 6f 6e 6e 65 63 74 ... > },
status: 200 }
<h1>GET to /any/path with req headers host,connection,x-csrf-token</h1>
*/

@@ -120,5 +120,3 @@ ```

- support regex in path like *
- status enum
- complete http4js-eg
- document unit testing routing and fakes

Sorry, the diff of this file is not supported yet

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