Comparing version 4.2.3 to 4.2.4
@@ -1,5 +0,4 @@ | ||
import { HeadersJson, HttpHandler } from "../core/HttpMessage"; | ||
import { BodyContent, HeadersJson, HttpHandler } from "../core/HttpMessage"; | ||
import { Req } from "../core/Req"; | ||
import { Headers } from "../core/Headers"; | ||
import { BodyContent } from "../core/HttpMessage"; | ||
import { Body } from "../core/Body"; | ||
@@ -6,0 +5,0 @@ import { Uri } from "../core/Uri"; |
@@ -1,2 +0,2 @@ | ||
import {HeadersJson, HttpHandler} from "../core/HttpMessage"; | ||
import {BodyContent, HeadersJson, HttpHandler} from "../core/HttpMessage"; | ||
import {Req} from "../core/Req"; | ||
@@ -6,6 +6,5 @@ import {HttpClient} from "./HttpClient"; | ||
import {Headers} from "../core/Headers"; | ||
import {BodyContent} from "../core/HttpMessage"; | ||
import {Body} from "../core/Body"; | ||
import {Uri} from "../core/Uri"; | ||
import {Filter, Filters} from ".."; | ||
import {Filters} from ".."; | ||
@@ -12,0 +11,0 @@ |
@@ -18,2 +18,3 @@ "use strict"; | ||
var Headers_1 = require("../core/Headers"); | ||
var stream_1 = require("stream"); | ||
function HttpClient(request) { | ||
@@ -40,6 +41,8 @@ var req = request instanceof Req_1.Req | ||
else { | ||
clientRequest.write(req.bodyString()); | ||
clientRequest.end(); | ||
var bodyStream = new stream_1.Readable({ read: function () { } }); | ||
bodyStream.push(req.bodyString()); | ||
bodyStream.push(null); // No more data | ||
bodyStream.pipe(clientRequest); | ||
} | ||
}); | ||
} |
@@ -8,2 +8,3 @@ import * as http from "http"; | ||
import {ReqOptions} from "./Client"; | ||
import {Readable} from "stream"; | ||
@@ -38,6 +39,8 @@ export function HttpClient(request: Req | ReqOptions): Promise<Res> { | ||
} else { | ||
clientRequest.write(req.bodyString()); | ||
clientRequest.end(); | ||
const bodyStream = new Readable({ read() {} }); | ||
bodyStream.push(req.bodyString()); | ||
bodyStream.push(null); // No more data | ||
bodyStream.pipe(clientRequest); | ||
} | ||
}); | ||
} |
@@ -11,2 +11,5 @@ /// <reference types="node" /> | ||
bodyStream(): Readable | undefined; | ||
fullBodyString(): Promise<string>; | ||
private readable; | ||
private readBytes; | ||
} |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,4 +62,51 @@ var Body = /** @class */ (function () { | ||
}; | ||
Body.prototype.fullBodyString = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var bytes, string; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (this.bodystring !== undefined) { | ||
return [2 /*return*/, this.bodystring]; | ||
} | ||
return [4 /*yield*/, this.readBytes(this.readStream, 99999999)]; | ||
case 1: | ||
bytes = _a.sent(); | ||
string = bytes ? bytes.toString('utf-8') : ''; | ||
this.bodystring = string; | ||
return [2 /*return*/, string]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Body.prototype.readable = function (rs) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, new Promise(function (r) { return rs.on('readable', r); })]; | ||
}); | ||
}); | ||
}; | ||
Body.prototype.readBytes = function (readable, numberOfBytes) { | ||
if (numberOfBytes === void 0) { numberOfBytes = 0; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var buf; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
buf = readable.read(numberOfBytes); | ||
if (buf) { | ||
return [2 /*return*/, new Promise(function (r) { return r(buf); })]; | ||
} | ||
else { | ||
return [2 /*return*/, new Promise(function (r) { | ||
_this.readable(readable).then(function () { | ||
_this.readBytes(readable, numberOfBytes).then(function (b) { return r(b); }); | ||
}); | ||
})]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
}; | ||
return Body; | ||
}()); | ||
exports.Body = Body; |
@@ -34,2 +34,30 @@ import {Readable} from "stream"; | ||
async fullBodyString() { | ||
if (this.bodystring !== undefined) { | ||
return this.bodystring; | ||
} | ||
const bytes = await this.readBytes(this.readStream!, 99999999); | ||
const string = bytes ? bytes.toString('utf-8') : ''; | ||
this.bodystring = string; | ||
return string; | ||
} | ||
private async readable(rs: Readable): Promise<{}> { | ||
return new Promise(r => rs.on('readable', r)); | ||
} | ||
private async readBytes(readable: Readable, numberOfBytes: number = 0): Promise<Buffer> { | ||
let buf = readable.read(numberOfBytes); | ||
if (buf) { | ||
return new Promise<Buffer>(r => r(buf)); | ||
} | ||
else { | ||
return new Promise<Buffer>(r => { | ||
this.readable(readable).then(() => { | ||
this.readBytes(readable, numberOfBytes).then(b => r(b)); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
/// <reference types="node" /> | ||
import { HttpMessage, HeadersJson } from "./HttpMessage"; | ||
import { BodyContent, HeadersJson, HttpMessage } from "./HttpMessage"; | ||
import { Body } from "./Body"; | ||
import { Readable } from "stream"; | ||
import { Headers } from "./Headers"; | ||
import { BodyContent } from "./HttpMessage"; | ||
export declare class Res implements HttpMessage { | ||
@@ -21,2 +20,3 @@ headers: HeadersJson; | ||
bodyStream(): Readable | undefined; | ||
bigBodyString(): Promise<string>; | ||
static OK(body: BodyContent, headers?: {}): Res; | ||
@@ -23,0 +23,0 @@ static Created(body: BodyContent, headers?: {}): Res; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -47,2 +82,9 @@ var Body_1 = require("./Body"); | ||
}; | ||
Res.prototype.bigBodyString = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.body.fullBodyString()]; | ||
}); | ||
}); | ||
}; | ||
Res.OK = function (body, headers) { | ||
@@ -49,0 +91,0 @@ if (headers === void 0) { headers = {}; } |
@@ -1,6 +0,5 @@ | ||
import {HttpMessage, HeadersJson} from "./HttpMessage"; | ||
import {BodyContent, HeadersJson, HttpMessage} from "./HttpMessage"; | ||
import {Body} from "./Body"; | ||
import {Readable} from "stream"; | ||
import {Headers} from "./Headers"; | ||
import {BodyContent} from "./HttpMessage"; | ||
@@ -60,2 +59,6 @@ export class Res implements HttpMessage { | ||
async bigBodyString(): Promise<string> { | ||
return this.body.fullBodyString(); | ||
} | ||
static OK(body: BodyContent, headers: {} = {}): Res { | ||
@@ -62,0 +65,0 @@ return new Res(200, body, headers) |
import { Res } from "./Res"; | ||
import { HttpHandler, HeadersJson } from "./HttpMessage"; | ||
import { HeadersJson, HttpHandler } from "./HttpMessage"; | ||
import { Req } from "./Req"; | ||
@@ -4,0 +4,0 @@ import { Filter } from "./Filters"; |
@@ -136,3 +136,3 @@ "use strict"; | ||
}, matchedHandler.handler); | ||
return filteringHandler(req.withPathParamsFromTemplate(matchedHandler.path)).catch(function (e) { return Res_1.ResOf(500); }); | ||
return filteringHandler(req.withPathParamsFromTemplate(matchedHandler.path)).catch(function () { return Res_1.ResOf(500); }); | ||
}; | ||
@@ -139,0 +139,0 @@ Routing.prototype.match = function (req) { |
import {Res, ResOf} from "./Res"; | ||
import {HttpHandler, HeadersJson} from "./HttpMessage"; | ||
import {HeadersJson, HttpHandler} from "./HttpMessage"; | ||
import {Req} from "./Req"; | ||
@@ -91,3 +91,3 @@ import {Filter} from "./Filters"; | ||
return filteringHandler(req.withPathParamsFromTemplate(matchedHandler.path)).catch(e => ResOf(500)); | ||
return filteringHandler(req.withPathParamsFromTemplate(matchedHandler.path)).catch(() => ResOf(500)); | ||
} | ||
@@ -94,0 +94,0 @@ |
@@ -1,4 +0,1 @@ | ||
import {Res} from "./core/Res"; | ||
import {get} from "./core/Routing"; | ||
import {HttpServer} from "./servers/NativeServer"; | ||
export * from "./core/Routing"; | ||
@@ -5,0 +2,0 @@ export * from "./core/Req"; |
{ | ||
"name": "http4js", | ||
"version": "4.2.3", | ||
"version": "4.2.4", | ||
"description": "A lightweight HTTP toolkit", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -56,2 +56,4 @@ # http4js | ||
### 4.2.4 `res.fullBodyString() for bodies > 65kb` | ||
### 4.2.3 Adds gzip filter. Eg. `Filters.GZIP(HttpClient)` or `.withFilter(Filters.GZIP)` | ||
@@ -58,0 +60,0 @@ |
@@ -69,4 +69,7 @@ "use strict"; | ||
else { | ||
res.write(response.bodyString()); | ||
res.end(); | ||
var bodyString = response.bodyString(); | ||
var bodyStream_1 = new stream_1.Readable({ read: function () { } }); | ||
bodyStream_1.push(bodyString); | ||
bodyStream_1.push(null); // No more data | ||
bodyStream_1.pipe(res); | ||
} | ||
@@ -73,0 +76,0 @@ }).catch(function (rej) { return console.log(rej); }); |
import * as http from "http"; | ||
import {IncomingMessage, ServerResponse} from "http"; | ||
import * as https from "https"; | ||
import {IncomingMessage, ServerResponse} from "http"; | ||
import {Routing} from "../core/Routing"; | ||
@@ -43,4 +43,8 @@ import {ReqOf} from "../core/Req"; | ||
} else { | ||
res.write(response.bodyString()); | ||
res.end(); | ||
const bodyString = response.bodyString(); | ||
const bodyStream = new Readable({ read() {} }); | ||
bodyStream.push(bodyString); | ||
bodyStream.push(null); // No more data | ||
bodyStream.pipe(res); | ||
} | ||
@@ -47,0 +51,0 @@ }).catch(rej => console.log(rej)); |
138317
3109
176