@http4t/core
Advanced tools
Comparing version 0.0.158 to 0.1.19
@@ -10,1 +10,2 @@ import { HttpHandler, HttpRequest, HttpResponse } from "./contract"; | ||
} | ||
//# sourceMappingURL=bin.d.ts.map |
142
bin.js
@@ -11,29 +11,2 @@ "use strict"; | ||
}; | ||
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 }; | ||
} | ||
}; | ||
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } | ||
@@ -51,91 +24,50 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BinHandler = void 0; | ||
var random_1 = require("./util/random"); | ||
var responses_1 = require("./responses"); | ||
var bodies_1 = require("./bodies"); | ||
const random_1 = require("./util/random"); | ||
const responses_1 = require("./responses"); | ||
const bodies_1 = require("./bodies"); | ||
/** | ||
* Represents an in memory version of http://httpbin.org/ | ||
*/ | ||
var BinHandler = /** @class */ (function () { | ||
function BinHandler() { | ||
class BinHandler { | ||
handle(request) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { method, uri: { path } } = request; | ||
if (method === 'GET' && path.startsWith('/stream-bytes')) { | ||
const match = /\/stream-bytes\/(.+)/.exec(path); | ||
return this.streamBytes(Number.parseInt((match && match[1]) || "12")); | ||
} | ||
if (method === 'GET') | ||
return responses_1.ok(); | ||
if (method === 'POST') | ||
return BinHandler.echo(request); | ||
if (method === 'PUT') | ||
return BinHandler.echo(request); | ||
if (method === 'PATCH') | ||
return BinHandler.echo(request); | ||
if (method === 'DELETE') | ||
return BinHandler.echo(request); | ||
return responses_1.notFound(); | ||
}); | ||
} | ||
BinHandler.prototype.handle = function (request) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var method, path, match; | ||
return __generator(this, function (_a) { | ||
method = request.method, path = request.uri.path; | ||
if (method === 'GET' && path.startsWith('/stream-bytes')) { | ||
match = /\/stream-bytes\/(.+)/.exec(path); | ||
return [2 /*return*/, this.streamBytes(Number.parseInt((match && match[1]) || "12"))]; | ||
} | ||
if (method === 'GET') | ||
return [2 /*return*/, responses_1.ok()]; | ||
if (method === 'POST') | ||
return [2 /*return*/, BinHandler.echo(request)]; | ||
if (method === 'PUT') | ||
return [2 /*return*/, BinHandler.echo(request)]; | ||
if (method === 'PATCH') | ||
return [2 /*return*/, BinHandler.echo(request)]; | ||
if (method === 'DELETE') | ||
return [2 /*return*/, BinHandler.echo(request)]; | ||
return [2 /*return*/, responses_1.notFound()]; | ||
}); | ||
static echo({ headers, body }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// TODO convert headers to object instead of array | ||
const jsonedHeaders = headers.reduce((acc, [n, v]) => { | ||
acc[n] = v; | ||
return acc; | ||
}, {}); | ||
return responses_1.ok(JSON.stringify({ data: yield bodies_1.bufferText(body), headers: jsonedHeaders })); | ||
}); | ||
}; | ||
BinHandler.echo = function (_a) { | ||
var headers = _a.headers, body = _a.body; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var jsonedHeaders, _b, _c, _d, _e; | ||
return __generator(this, function (_f) { | ||
switch (_f.label) { | ||
case 0: | ||
jsonedHeaders = headers.reduce(function (acc, _a) { | ||
var _b = __read(_a, 2), n = _b[0], v = _b[1]; | ||
acc[n] = v; | ||
return acc; | ||
}, {}); | ||
_b = responses_1.ok; | ||
_d = (_c = JSON).stringify; | ||
_e = {}; | ||
return [4 /*yield*/, bodies_1.bufferText(body)]; | ||
case 1: return [2 /*return*/, _b.apply(void 0, [_d.apply(_c, [(_e.data = _f.sent(), _e.headers = jsonedHeaders, _e)])])]; | ||
} | ||
}); | ||
}); | ||
}; | ||
BinHandler.prototype.streamBytes = function (size) { | ||
} | ||
streamBytes(size) { | ||
return responses_1.ok((function () { | ||
return __asyncGenerator(this, arguments, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, __await(random_1.Random.bytes(size))]; | ||
case 1: return [4 /*yield*/, _a.sent()]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
return __asyncGenerator(this, arguments, function* () { | ||
yield yield __await(random_1.Random.bytes(size)); | ||
}); | ||
})()); | ||
}; | ||
return BinHandler; | ||
}()); | ||
} | ||
} | ||
exports.BinHandler = BinHandler; | ||
//# sourceMappingURL=bin.js.map |
@@ -18,1 +18,2 @@ import { Data, HttpBody } from "./contract"; | ||
export declare function merge(a: Uint8Array | undefined, b: Uint8Array): Uint8Array; | ||
//# sourceMappingURL=bodies.d.ts.map |
466
bodies.js
@@ -11,29 +11,2 @@ "use strict"; | ||
}; | ||
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 }; | ||
} | ||
}; | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
@@ -58,16 +31,5 @@ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.merge = exports.isPromiseLike = exports.isData = exports.isUint8Array = exports.isIterable = exports.isAsyncIterable = exports.typeDescription = exports.asBinary = exports.asString = exports.streamBinary = exports.streamText = exports.bufferBinary = exports.bufferText = void 0; | ||
var textencoding_1 = require("./util/textencoding"); | ||
const textencoding_1 = require("./util/textencoding"); | ||
/* | ||
@@ -81,54 +43,30 @@ ----------------------------------- | ||
var e_1, _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _b, result, data, e_1_1; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (!isPromiseLike(body)) return [3 /*break*/, 2]; | ||
_b = asString; | ||
return [4 /*yield*/, body]; | ||
case 1: return [2 /*return*/, _b.apply(void 0, [_c.sent()])]; | ||
case 2: | ||
if (!isAsyncIterable(body)) return [3 /*break*/, 15]; | ||
result = ""; | ||
_c.label = 3; | ||
case 3: | ||
_c.trys.push([3, 8, 9, 14]); | ||
body_1 = __asyncValues(body); | ||
_c.label = 4; | ||
case 4: return [4 /*yield*/, body_1.next()]; | ||
case 5: | ||
if (!(body_1_1 = _c.sent(), !body_1_1.done)) return [3 /*break*/, 7]; | ||
data = body_1_1.value; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (isPromiseLike(body)) { | ||
return asString(yield body); | ||
} | ||
if (isAsyncIterable(body)) { | ||
let result = ""; | ||
try { | ||
for (body_1 = __asyncValues(body); body_1_1 = yield body_1.next(), !body_1_1.done;) { | ||
const data = body_1_1.value; | ||
result += asString(data); | ||
_c.label = 6; | ||
case 6: return [3 /*break*/, 4]; | ||
case 7: return [3 /*break*/, 14]; | ||
case 8: | ||
e_1_1 = _c.sent(); | ||
e_1 = { error: e_1_1 }; | ||
return [3 /*break*/, 14]; | ||
case 9: | ||
_c.trys.push([9, , 12, 13]); | ||
if (!(body_1_1 && !body_1_1.done && (_a = body_1.return))) return [3 /*break*/, 11]; | ||
return [4 /*yield*/, _a.call(body_1)]; | ||
case 10: | ||
_c.sent(); | ||
_c.label = 11; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: | ||
if (e_1) throw e_1.error; | ||
return [7 /*endfinally*/]; | ||
case 13: return [7 /*endfinally*/]; | ||
case 14: return [2 /*return*/, result]; | ||
case 15: | ||
if (isData(body)) { | ||
return [2 /*return*/, asString(body)]; | ||
} | ||
if (isIterable(body)) { | ||
return [2 /*return*/, Array.from(body).map(asString).join("")]; | ||
} | ||
throw new Error("Not a valid body: '" + body + "' (" + typeDescription(body) + ")"); | ||
} | ||
} | ||
}); | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (body_1_1 && !body_1_1.done && (_a = body_1.return)) yield _a.call(body_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return result; | ||
} | ||
if (isData(body)) { | ||
return asString(body); | ||
} | ||
if (isIterable(body)) { | ||
return Array.from(body).map(asString).join(""); | ||
} | ||
throw new Error(`Not a valid body: '${body}' (${typeDescription(body)})`); | ||
}); | ||
@@ -140,54 +78,30 @@ } | ||
var e_2, _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _b, result, data, e_2_1; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (!isPromiseLike(body)) return [3 /*break*/, 2]; | ||
_b = asBinary; | ||
return [4 /*yield*/, body]; | ||
case 1: return [2 /*return*/, _b.apply(void 0, [_c.sent()])]; | ||
case 2: | ||
if (!isAsyncIterable(body)) return [3 /*break*/, 15]; | ||
result = undefined; | ||
_c.label = 3; | ||
case 3: | ||
_c.trys.push([3, 8, 9, 14]); | ||
body_2 = __asyncValues(body); | ||
_c.label = 4; | ||
case 4: return [4 /*yield*/, body_2.next()]; | ||
case 5: | ||
if (!(body_2_1 = _c.sent(), !body_2_1.done)) return [3 /*break*/, 7]; | ||
data = body_2_1.value; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (isPromiseLike(body)) { | ||
return asBinary(yield body); | ||
} | ||
if (isAsyncIterable(body)) { | ||
let result = undefined; | ||
try { | ||
for (body_2 = __asyncValues(body); body_2_1 = yield body_2.next(), !body_2_1.done;) { | ||
const data = body_2_1.value; | ||
result = merge(result, asBinary(data)); | ||
_c.label = 6; | ||
case 6: return [3 /*break*/, 4]; | ||
case 7: return [3 /*break*/, 14]; | ||
case 8: | ||
e_2_1 = _c.sent(); | ||
e_2 = { error: e_2_1 }; | ||
return [3 /*break*/, 14]; | ||
case 9: | ||
_c.trys.push([9, , 12, 13]); | ||
if (!(body_2_1 && !body_2_1.done && (_a = body_2.return))) return [3 /*break*/, 11]; | ||
return [4 /*yield*/, _a.call(body_2)]; | ||
case 10: | ||
_c.sent(); | ||
_c.label = 11; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: | ||
if (e_2) throw e_2.error; | ||
return [7 /*endfinally*/]; | ||
case 13: return [7 /*endfinally*/]; | ||
case 14: return [2 /*return*/, result || new Uint8Array()]; | ||
case 15: | ||
if (isData(body)) { | ||
return [2 /*return*/, asBinary(body)]; | ||
} | ||
if (isIterable(body)) { | ||
return [2 /*return*/, Array.from(body).map(asBinary).reduce(merge)]; | ||
} | ||
throw new Error("Not a valid body: '" + body + "' (" + typeDescription(body) + ")"); | ||
} | ||
} | ||
}); | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (body_2_1 && !body_2_1.done && (_a = body_2.return)) yield _a.call(body_2); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
return result || new Uint8Array(); | ||
} | ||
if (isData(body)) { | ||
return asBinary(body); | ||
} | ||
if (isIterable(body)) { | ||
return Array.from(body).map(asBinary).reduce(merge); | ||
} | ||
throw new Error(`Not a valid body: '${body}' (${typeDescription(body)})`); | ||
}); | ||
@@ -197,100 +111,45 @@ } | ||
function streamText(body) { | ||
return __asyncGenerator(this, arguments, function streamText_1() { | ||
var _a, body_3, body_3_1, data, e_3_1, body_4, body_4_1, data, e_4_1; | ||
var e_3, _b, e_4, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
if (!isPromiseLike(body)) return [3 /*break*/, 5]; | ||
_a = asString; | ||
return [4 /*yield*/, __await(body)]; | ||
case 1: return [4 /*yield*/, __await.apply(void 0, [_a.apply(void 0, [_d.sent()])])]; | ||
case 2: return [4 /*yield*/, _d.sent()]; | ||
case 3: | ||
_d.sent(); | ||
return [4 /*yield*/, __await(void 0)]; | ||
case 4: return [2 /*return*/, _d.sent()]; | ||
case 5: | ||
if (!isAsyncIterable(body)) return [3 /*break*/, 21]; | ||
_d.label = 6; | ||
case 6: | ||
_d.trys.push([6, 13, 14, 19]); | ||
body_3 = __asyncValues(body); | ||
_d.label = 7; | ||
case 7: return [4 /*yield*/, __await(body_3.next())]; | ||
case 8: | ||
if (!(body_3_1 = _d.sent(), !body_3_1.done)) return [3 /*break*/, 12]; | ||
data = body_3_1.value; | ||
return [4 /*yield*/, __await(asString(data))]; | ||
case 9: return [4 /*yield*/, _d.sent()]; | ||
case 10: | ||
_d.sent(); | ||
_d.label = 11; | ||
case 11: return [3 /*break*/, 7]; | ||
case 12: return [3 /*break*/, 19]; | ||
case 13: | ||
e_3_1 = _d.sent(); | ||
e_3 = { error: e_3_1 }; | ||
return [3 /*break*/, 19]; | ||
case 14: | ||
_d.trys.push([14, , 17, 18]); | ||
if (!(body_3_1 && !body_3_1.done && (_b = body_3.return))) return [3 /*break*/, 16]; | ||
return [4 /*yield*/, __await(_b.call(body_3))]; | ||
case 15: | ||
_d.sent(); | ||
_d.label = 16; | ||
case 16: return [3 /*break*/, 18]; | ||
case 17: | ||
if (e_3) throw e_3.error; | ||
return [7 /*endfinally*/]; | ||
case 18: return [7 /*endfinally*/]; | ||
case 19: return [4 /*yield*/, __await(void 0)]; | ||
case 20: return [2 /*return*/, _d.sent()]; | ||
case 21: | ||
if (!isData(body)) return [3 /*break*/, 25]; | ||
return [4 /*yield*/, __await(asString(body))]; | ||
case 22: return [4 /*yield*/, _d.sent()]; | ||
case 23: | ||
_d.sent(); | ||
return [4 /*yield*/, __await(void 0)]; | ||
case 24: return [2 /*return*/, _d.sent()]; | ||
case 25: | ||
if (!isIterable(body)) return [3 /*break*/, 41]; | ||
_d.label = 26; | ||
case 26: | ||
_d.trys.push([26, 33, 34, 39]); | ||
body_4 = __asyncValues(body); | ||
_d.label = 27; | ||
case 27: return [4 /*yield*/, __await(body_4.next())]; | ||
case 28: | ||
if (!(body_4_1 = _d.sent(), !body_4_1.done)) return [3 /*break*/, 32]; | ||
data = body_4_1.value; | ||
return [4 /*yield*/, __await(asString(data))]; | ||
case 29: return [4 /*yield*/, _d.sent()]; | ||
case 30: | ||
_d.sent(); | ||
_d.label = 31; | ||
case 31: return [3 /*break*/, 27]; | ||
case 32: return [3 /*break*/, 39]; | ||
case 33: | ||
e_4_1 = _d.sent(); | ||
e_4 = { error: e_4_1 }; | ||
return [3 /*break*/, 39]; | ||
case 34: | ||
_d.trys.push([34, , 37, 38]); | ||
if (!(body_4_1 && !body_4_1.done && (_c = body_4.return))) return [3 /*break*/, 36]; | ||
return [4 /*yield*/, __await(_c.call(body_4))]; | ||
case 35: | ||
_d.sent(); | ||
_d.label = 36; | ||
case 36: return [3 /*break*/, 38]; | ||
case 37: | ||
if (e_4) throw e_4.error; | ||
return [7 /*endfinally*/]; | ||
case 38: return [7 /*endfinally*/]; | ||
case 39: return [4 /*yield*/, __await(void 0)]; | ||
case 40: return [2 /*return*/, _d.sent()]; | ||
case 41: throw new Error("Not a valid body: '" + body + "' (" + typeDescription(body) + ")"); | ||
return __asyncGenerator(this, arguments, function* streamText_1() { | ||
var e_3, _a, e_4, _b; | ||
if (isPromiseLike(body)) { | ||
yield yield __await(asString(yield __await(body))); | ||
return yield __await(void 0); | ||
} | ||
if (isAsyncIterable(body)) { | ||
try { | ||
for (var body_3 = __asyncValues(body), body_3_1; body_3_1 = yield __await(body_3.next()), !body_3_1.done;) { | ||
const data = body_3_1.value; | ||
yield yield __await(asString(data)); | ||
} | ||
} | ||
}); | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (body_3_1 && !body_3_1.done && (_a = body_3.return)) yield __await(_a.call(body_3)); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return yield __await(void 0); | ||
} | ||
if (isData(body)) { | ||
yield yield __await(asString(body)); | ||
return yield __await(void 0); | ||
} | ||
if (isIterable(body)) { | ||
try { | ||
for (var body_4 = __asyncValues(body), body_4_1; body_4_1 = yield __await(body_4.next()), !body_4_1.done;) { | ||
const data = body_4_1.value; | ||
yield yield __await(asString(data)); | ||
} | ||
} | ||
catch (e_4_1) { e_4 = { error: e_4_1 }; } | ||
finally { | ||
try { | ||
if (body_4_1 && !body_4_1.done && (_b = body_4.return)) yield __await(_b.call(body_4)); | ||
} | ||
finally { if (e_4) throw e_4.error; } | ||
} | ||
return yield __await(void 0); | ||
} | ||
throw new Error(`Not a valid body: '${body}' (${typeDescription(body)})`); | ||
}); | ||
@@ -300,96 +159,35 @@ } | ||
function streamBinary(body) { | ||
return __asyncGenerator(this, arguments, function streamBinary_1() { | ||
var _a, body_5, body_5_1, chunk, e_5_1, body_6, body_6_1, chunk, e_6_1; | ||
var e_6, _b; | ||
var e_5, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
if (!isPromiseLike(body)) return [3 /*break*/, 5]; | ||
_a = asBinary; | ||
return [4 /*yield*/, __await(body)]; | ||
case 1: return [4 /*yield*/, __await.apply(void 0, [_a.apply(void 0, [_d.sent()])])]; | ||
case 2: return [4 /*yield*/, _d.sent()]; | ||
case 3: | ||
_d.sent(); | ||
return [4 /*yield*/, __await(void 0)]; | ||
case 4: return [2 /*return*/, _d.sent()]; | ||
case 5: | ||
if (!isAsyncIterable(body)) return [3 /*break*/, 21]; | ||
_d.label = 6; | ||
case 6: | ||
_d.trys.push([6, 13, 14, 19]); | ||
body_5 = __asyncValues(body); | ||
_d.label = 7; | ||
case 7: return [4 /*yield*/, __await(body_5.next())]; | ||
case 8: | ||
if (!(body_5_1 = _d.sent(), !body_5_1.done)) return [3 /*break*/, 12]; | ||
chunk = body_5_1.value; | ||
return [4 /*yield*/, __await(asBinary(chunk))]; | ||
case 9: return [4 /*yield*/, _d.sent()]; | ||
case 10: | ||
_d.sent(); | ||
_d.label = 11; | ||
case 11: return [3 /*break*/, 7]; | ||
case 12: return [3 /*break*/, 19]; | ||
case 13: | ||
e_5_1 = _d.sent(); | ||
e_5 = { error: e_5_1 }; | ||
return [3 /*break*/, 19]; | ||
case 14: | ||
_d.trys.push([14, , 17, 18]); | ||
if (!(body_5_1 && !body_5_1.done && (_c = body_5.return))) return [3 /*break*/, 16]; | ||
return [4 /*yield*/, __await(_c.call(body_5))]; | ||
case 15: | ||
_d.sent(); | ||
_d.label = 16; | ||
case 16: return [3 /*break*/, 18]; | ||
case 17: | ||
if (e_5) throw e_5.error; | ||
return [7 /*endfinally*/]; | ||
case 18: return [7 /*endfinally*/]; | ||
case 19: return [4 /*yield*/, __await(void 0)]; | ||
case 20: return [2 /*return*/, _d.sent()]; | ||
case 21: | ||
if (!isData(body)) return [3 /*break*/, 25]; | ||
return [4 /*yield*/, __await(asBinary(body))]; | ||
case 22: return [4 /*yield*/, _d.sent()]; | ||
case 23: | ||
_d.sent(); | ||
return [4 /*yield*/, __await(void 0)]; | ||
case 24: return [2 /*return*/, _d.sent()]; | ||
case 25: | ||
if (!isIterable(body)) return [3 /*break*/, 36]; | ||
_d.label = 26; | ||
case 26: | ||
_d.trys.push([26, 32, 33, 34]); | ||
body_6 = __values(body), body_6_1 = body_6.next(); | ||
_d.label = 27; | ||
case 27: | ||
if (!!body_6_1.done) return [3 /*break*/, 31]; | ||
chunk = body_6_1.value; | ||
return [4 /*yield*/, __await(asBinary(chunk))]; | ||
case 28: return [4 /*yield*/, _d.sent()]; | ||
case 29: | ||
_d.sent(); | ||
_d.label = 30; | ||
case 30: | ||
body_6_1 = body_6.next(); | ||
return [3 /*break*/, 27]; | ||
case 31: return [3 /*break*/, 34]; | ||
case 32: | ||
e_6_1 = _d.sent(); | ||
e_6 = { error: e_6_1 }; | ||
return [3 /*break*/, 34]; | ||
case 33: | ||
try { | ||
if (body_6_1 && !body_6_1.done && (_b = body_6.return)) _b.call(body_6); | ||
} | ||
finally { if (e_6) throw e_6.error; } | ||
return [7 /*endfinally*/]; | ||
case 34: return [4 /*yield*/, __await(void 0)]; | ||
case 35: return [2 /*return*/, _d.sent()]; | ||
case 36: throw new Error("Not a valid body: '" + body + "' (" + typeDescription(body) + ")"); | ||
return __asyncGenerator(this, arguments, function* streamBinary_1() { | ||
var e_5, _a; | ||
if (isPromiseLike(body)) { | ||
yield yield __await(asBinary(yield __await(body))); | ||
return yield __await(void 0); | ||
} | ||
if (isAsyncIterable(body)) { | ||
try { | ||
for (var body_5 = __asyncValues(body), body_5_1; body_5_1 = yield __await(body_5.next()), !body_5_1.done;) { | ||
const chunk = body_5_1.value; | ||
yield yield __await(asBinary(chunk)); | ||
} | ||
} | ||
}); | ||
catch (e_5_1) { e_5 = { error: e_5_1 }; } | ||
finally { | ||
try { | ||
if (body_5_1 && !body_5_1.done && (_a = body_5.return)) yield __await(_a.call(body_5)); | ||
} | ||
finally { if (e_5) throw e_5.error; } | ||
} | ||
return yield __await(void 0); | ||
} | ||
if (isData(body)) { | ||
yield yield __await(asBinary(body)); | ||
return yield __await(void 0); | ||
} | ||
if (isIterable(body)) { | ||
for (const chunk of body) { | ||
yield yield __await(asBinary(chunk)); | ||
} | ||
return yield __await(void 0); | ||
} | ||
throw new Error(`Not a valid body: '${body}' (${typeDescription(body)})`); | ||
}); | ||
@@ -404,3 +202,3 @@ } | ||
return textencoding_1.textDecoder().decode(data); | ||
throw new Error("Not supported " + typeDescription(data)); | ||
throw new Error(`Not supported ${typeDescription(data)}`); | ||
} | ||
@@ -414,3 +212,3 @@ exports.asString = asString; | ||
return textencoding_1.textEncoder().encode(data); | ||
throw new Error("Not supported " + typeDescription(data)); | ||
throw new Error(`Not supported ${typeDescription(data)}`); | ||
} | ||
@@ -429,6 +227,6 @@ exports.asBinary = asBinary; | ||
return 'null'; | ||
var t = typeof x; | ||
let t = typeof x; | ||
if (t !== 'object') | ||
return t; | ||
var p = Object.getPrototypeOf(x); | ||
const p = Object.getPrototypeOf(x); | ||
if (p !== Object.prototype) | ||
@@ -466,3 +264,3 @@ return p.constructor.name; | ||
return a; | ||
var result = new Uint8Array(a.length + b.length); | ||
const result = new Uint8Array(a.length + b.length); | ||
result.set(a); | ||
@@ -469,0 +267,0 @@ result.set(b, a.length); |
@@ -92,1 +92,2 @@ /** | ||
} | ||
//# sourceMappingURL=contract.d.ts.map |
import { HttpHandler, HttpRequest, HttpResponse } from "./contract"; | ||
export declare function handler(f: (request: HttpRequest) => Promise<HttpResponse>): HttpHandler; | ||
export declare type HttpHandlerFn = (request: HttpRequest) => Promise<HttpResponse>; | ||
export declare function handler(f: HttpHandlerFn): HttpHandler; | ||
//# sourceMappingURL=handlers.d.ts.map |
import {HttpHandler, HttpRequest, HttpResponse} from "./contract"; | ||
export function handler(f: (request: HttpRequest) => Promise<HttpResponse>): HttpHandler { | ||
export type HttpHandlerFn = (request: HttpRequest) => Promise<HttpResponse>; | ||
export function handler(f: HttpHandlerFn): HttpHandler { | ||
return {handle: f}; | ||
} | ||
} |
@@ -25,1 +25,2 @@ import { Header, HeaderName, HeaderValue } from './contract'; | ||
export declare function headerValue(header: Header): HeaderValue; | ||
//# sourceMappingURL=headers.d.ts.map |
"use strict"; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -39,20 +8,9 @@ exports.headerValue = exports.headerName = exports.header = exports.removeHeader = exports.updateHeaders = exports.setHeader = exports.getHeaderValues = exports.getHeaderValue = void 0; | ||
function getHeaderValue(headers, name) { | ||
var e_1, _a; | ||
if (typeof headers === 'undefined') | ||
return undefined; | ||
var lowerCaseName = name.toLowerCase(); | ||
try { | ||
for (var headers_1 = __values(headers), headers_1_1 = headers_1.next(); !headers_1_1.done; headers_1_1 = headers_1.next()) { | ||
var _b = __read(headers_1_1.value, 2), n = _b[0], v = _b[1]; | ||
if (n.toLowerCase() === lowerCaseName) | ||
return v; | ||
} | ||
const lowerCaseName = name.toLowerCase(); | ||
for (const [n, v] of headers) { | ||
if (n.toLowerCase() === lowerCaseName) | ||
return v; | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (headers_1_1 && !headers_1_1.done && (_a = headers_1.return)) _a.call(headers_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return undefined; | ||
@@ -67,12 +25,6 @@ } | ||
return []; | ||
var lowerCaseName = name.toLowerCase(); | ||
const lowerCaseName = name.toLowerCase(); | ||
return headers | ||
.filter(function (_a) { | ||
var _b = __read(_a, 2), name = _b[0], _ = _b[1]; | ||
return name.toLowerCase() === lowerCaseName; | ||
}) | ||
.map(function (_a) { | ||
var _b = __read(_a, 2), _ = _b[0], value = _b[1]; | ||
return value; | ||
}); | ||
.filter(([name, _]) => name.toLowerCase() === lowerCaseName) | ||
.map(([_, value]) => value); | ||
} | ||
@@ -86,23 +38,14 @@ exports.getHeaderValues = getHeaderValues; | ||
function setHeader(headers, header) { | ||
var lowerCaseName = headerName(header).toLowerCase(); | ||
return __spread(headers.filter(function (_a) { | ||
var _b = __read(_a, 1), name = _b[0]; | ||
return name.toLowerCase() !== lowerCaseName; | ||
}), [header]); | ||
const lowerCaseName = headerName(header).toLowerCase(); | ||
return [...headers.filter(([name]) => name.toLowerCase() !== lowerCaseName), header]; | ||
} | ||
exports.setHeader = setHeader; | ||
function updateHeaders(headers, name, f) { | ||
var lowerCaseName = name.toLowerCase(); | ||
return headers.map(function (_a) { | ||
var _b = __read(_a, 2), name = _b[0], value = _b[1]; | ||
return (name.toLowerCase() === lowerCaseName) ? [name, f(value)] : [name, value]; | ||
}); | ||
const lowerCaseName = name.toLowerCase(); | ||
return headers.map(([name, value]) => (name.toLowerCase() === lowerCaseName) ? [name, f(value)] : [name, value]); | ||
} | ||
exports.updateHeaders = updateHeaders; | ||
function removeHeader(headers, name) { | ||
var lowerCaseName = name.toLowerCase(); | ||
return headers.filter(function (_a) { | ||
var _b = __read(_a, 1), name = _b[0]; | ||
return name.toLowerCase() !== lowerCaseName; | ||
}); | ||
const lowerCaseName = name.toLowerCase(); | ||
return headers.filter(([name]) => name.toLowerCase() !== lowerCaseName); | ||
} | ||
@@ -109,0 +52,0 @@ exports.removeHeader = removeHeader; |
@@ -11,1 +11,2 @@ import { HttpHandler, HttpRequest, HttpResponse } from "./contract"; | ||
} | ||
//# sourceMappingURL=host.d.ts.map |
13
host.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HostHandler = void 0; | ||
var messages_1 = require("./messages"); | ||
const messages_1 = require("./messages"); | ||
/** | ||
* Sets Host header on requests | ||
*/ | ||
var HostHandler = /** @class */ (function () { | ||
function HostHandler(handler, host) { | ||
class HostHandler { | ||
constructor(handler, host) { | ||
this.handler = handler; | ||
this.host = host; | ||
} | ||
HostHandler.prototype.handle = function (request) { | ||
handle(request) { | ||
return this.handler.handle(messages_1.setHeader(request, 'Host', this.host)); | ||
}; | ||
return HostHandler; | ||
}()); | ||
} | ||
} | ||
exports.HostHandler = HostHandler; | ||
//# sourceMappingURL=host.js.map |
@@ -29,1 +29,2 @@ import { Data, HttpBody, HttpMessage, HttpRequest, HttpResponse } from "./contract"; | ||
export declare function parseJson<T extends HttpRequest | HttpResponse>(message: T): Promise<T>; | ||
//# sourceMappingURL=json.d.ts.map |
97
json.js
@@ -11,29 +11,2 @@ "use strict"; | ||
}; | ||
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 }; | ||
} | ||
}; | ||
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } | ||
@@ -53,31 +26,22 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { | ||
exports.parseJson = exports.bodyJson = exports.jsonBody = exports.JsonBody = void 0; | ||
var bodies_1 = require("./bodies"); | ||
var contract_1 = require("./contract"); | ||
var objects_1 = require("./util/objects"); | ||
const bodies_1 = require("./bodies"); | ||
const contract_1 = require("./contract"); | ||
const objects_1 = require("./util/objects"); | ||
function yieldStringify(data) { | ||
return __asyncGenerator(this, arguments, function yieldStringify_1() { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, __await(typeof data === 'undefined' ? "" : JSON.stringify(data))]; | ||
case 1: return [4 /*yield*/, _a.sent()]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
return __asyncGenerator(this, arguments, function* yieldStringify_1() { | ||
yield yield __await(typeof data === 'undefined' ? "" : JSON.stringify(data)); | ||
}); | ||
} | ||
var JsonBody = /** @class */ (function () { | ||
function JsonBody(data) { | ||
class JsonBody { | ||
constructor(data) { | ||
this.data = data; | ||
this.ifYouAreSeeingThisInATestAssertion = "You probably forgot to call toJSON(message) on your HttpMessage, which would have called Symbol.asyncIterator on this body."; | ||
} | ||
JsonBody.prototype[Symbol.asyncIterator] = function () { | ||
[Symbol.asyncIterator]() { | ||
return yieldStringify(this.data)[Symbol.asyncIterator](); | ||
}; | ||
JsonBody.prototype.toJSON = function () { | ||
} | ||
toJSON() { | ||
return this.data; | ||
}; | ||
return JsonBody; | ||
}()); | ||
} | ||
} | ||
exports.JsonBody = JsonBody; | ||
@@ -107,18 +71,10 @@ /** | ||
function bodyJson(value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var body, text; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
body = contract_1.isMessage(value) ? value.body : value; | ||
if (body instanceof JsonBody) | ||
return [2 /*return*/, body.data]; | ||
return [4 /*yield*/, bodies_1.bufferText(body)]; | ||
case 1: | ||
text = _a.sent(); | ||
if (text === "") | ||
return [2 /*return*/, undefined]; | ||
return [2 /*return*/, JSON.parse(text)]; | ||
} | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const body = contract_1.isMessage(value) ? value.body : value; | ||
if (body instanceof JsonBody) | ||
return body.data; | ||
const text = yield bodies_1.bufferText(body); | ||
if (text === "") | ||
return undefined; | ||
return JSON.parse(text); | ||
}); | ||
@@ -134,10 +90,7 @@ } | ||
function parseJson(message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var parsed; | ||
return __generator(this, function (_a) { | ||
if (message.body instanceof JsonBody) | ||
return [2 /*return*/, message]; | ||
parsed = bodyJson(message.body); | ||
return [2 /*return*/, objects_1.modify(message, { body: jsonBody(parsed) })]; | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (message.body instanceof JsonBody) | ||
return message; | ||
const parsed = bodyJson(message.body); | ||
return objects_1.modify(message, { body: jsonBody(parsed) }); | ||
}); | ||
@@ -144,0 +97,0 @@ } |
@@ -10,1 +10,2 @@ import { HeaderName, HeaderValue, HttpBody, HttpMessage, HttpRequest, HttpResponse } from "./contract"; | ||
export declare function toJSON<T extends HttpMessage>(message: T): Promise<T>; | ||
//# sourceMappingURL=messages.d.ts.map |
100
messages.js
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
@@ -22,55 +30,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
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 }; | ||
} | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toJSON = exports.isRequest = exports.isResponse = exports.setBody = exports.updateHeaders = exports.removeHeaders = exports.appendHeader = exports.setHeader = void 0; | ||
var h = require("./headers"); | ||
var headers_1 = require("./headers"); | ||
var objects_1 = require("./util/objects"); | ||
var bodies_1 = require("./bodies"); | ||
const h = __importStar(require("./headers")); | ||
const headers_1 = require("./headers"); | ||
const objects_1 = require("./util/objects"); | ||
const bodies_1 = require("./bodies"); | ||
/* | ||
@@ -85,3 +46,3 @@ * these operate on headers in a case-insensitive way | ||
function appendHeader(message, name, value) { | ||
return objects_1.modify(message, { headers: __spread(message.headers, [[name, value]]) }); | ||
return objects_1.modify(message, { headers: [...message.headers, [name, value]] }); | ||
} | ||
@@ -98,3 +59,3 @@ exports.appendHeader = appendHeader; | ||
function setBody(message, body) { | ||
return objects_1.modify(message, { body: body }); | ||
return objects_1.modify(message, { body }); | ||
} | ||
@@ -112,13 +73,4 @@ exports.setBody = setBody; | ||
function toJSON(message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_a = [__assign({}, message)]; | ||
_b = {}; | ||
return [4 /*yield*/, bodies_1.bufferText(message.body)]; | ||
case 1: return [2 /*return*/, __assign.apply(void 0, _a.concat([(_b.body = _c.sent(), _b)]))]; | ||
} | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return Object.assign(Object.assign({}, message), { body: yield bodies_1.bufferText(message.body) }); | ||
}); | ||
@@ -125,0 +77,0 @@ } |
{ | ||
"name": "@http4t/core", | ||
"version": "0.0.158", | ||
"license": "Apache-2.0" | ||
"version": "0.1.19", | ||
"license": "Apache-2.0", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc --build" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
@@ -26,1 +26,2 @@ import { DecodedPair } from "./urlEncoding"; | ||
export declare function queries(query: string | undefined, name: string): (string | undefined)[]; | ||
//# sourceMappingURL=queries.d.ts.map |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.queries = exports.query = exports.setQueries = exports.setQuery = exports.removeQueries = exports.removeQuery = exports.appendQueries = exports.appendQuery = exports.expand = void 0; | ||
var urlEncoding_1 = require("./urlEncoding"); | ||
const urlEncoding_1 = require("./urlEncoding"); | ||
/** | ||
@@ -27,6 +11,5 @@ * Takes a list of pairs where the value might be an array, and returns a list of simple name-value pairs, | ||
*/ | ||
function expand(_a) { | ||
var _b = __read(_a, 2), name = _b[0], value = _b[1]; | ||
function expand([name, value]) { | ||
return Array.isArray(value) | ||
? value.map(function (value) { return [name, value]; }) | ||
? value.map(value => [name, value]) | ||
: [[name, value]]; | ||
@@ -36,11 +19,10 @@ } | ||
function appendQuery(query, name, value) { | ||
var newQuery = urlEncoding_1.encodePairs(expand([name, value])); | ||
const newQuery = urlEncoding_1.encodePairs(expand([name, value])); | ||
if (typeof query === 'undefined' || query.length === 0) | ||
return newQuery; | ||
return query + "&" + newQuery; | ||
return `${query}&${newQuery}`; | ||
} | ||
exports.appendQuery = appendQuery; | ||
function appendQueries(query, queries) { | ||
return Object.entries(queries).reduce(function (acc, _a) { | ||
var _b = __read(_a, 2), name = _b[0], value = _b[1]; | ||
return Object.entries(queries).reduce((acc, [name, value]) => { | ||
return appendQuery(acc, name, value); | ||
@@ -51,15 +33,8 @@ }, query); | ||
function removeQuery(query, name) { | ||
var filtered = urlEncoding_1.decodePairs(query).filter(function (_a) { | ||
var _b = __read(_a, 1), n = _b[0]; | ||
return n !== name; | ||
}); | ||
const filtered = urlEncoding_1.decodePairs(query).filter(([n]) => n !== name); | ||
return urlEncoding_1.encodePairs(filtered); | ||
} | ||
exports.removeQuery = removeQuery; | ||
function removeQueries(query) { | ||
var names = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
names[_i - 1] = arguments[_i]; | ||
} | ||
return names.reduce(function (result, name) { return removeQuery(result, name); }, query); | ||
function removeQueries(query, ...names) { | ||
return names.reduce((result, name) => removeQuery(result, name), query); | ||
} | ||
@@ -72,8 +47,5 @@ exports.removeQueries = removeQueries; | ||
function setQueries(query, queries) { | ||
var existingKeys = new Set(Object.keys(queries)); | ||
var filtered = urlEncoding_1.decodePairs(query) | ||
.filter(function (_a) { | ||
var _b = __read(_a, 1), name = _b[0]; | ||
return !existingKeys.has(name); | ||
}); | ||
const existingKeys = new Set(Object.keys(queries)); | ||
const filtered = urlEncoding_1.decodePairs(query) | ||
.filter(([name]) => !existingKeys.has(name)); | ||
return appendQueries(urlEncoding_1.encodePairs(filtered), queries); | ||
@@ -84,18 +56,9 @@ } | ||
var _a; | ||
return (_a = urlEncoding_1.decodePairs(query).find(function (_a) { | ||
var _b = __read(_a, 1), n = _b[0]; | ||
return n === name; | ||
})) === null || _a === void 0 ? void 0 : _a[1]; | ||
return (_a = urlEncoding_1.decodePairs(query).find(([n]) => n === name)) === null || _a === void 0 ? void 0 : _a[1]; | ||
} | ||
exports.query = query; | ||
function queries(query, name) { | ||
return urlEncoding_1.decodePairs(query).filter(function (_a) { | ||
var _b = __read(_a, 1), n = _b[0]; | ||
return n === name; | ||
}).map(function (_a) { | ||
var _b = __read(_a, 2), _ = _b[0], v = _b[1]; | ||
return v; | ||
}); | ||
return urlEncoding_1.decodePairs(query).filter(([n]) => n === name).map(([_, v]) => v); | ||
} | ||
exports.queries = queries; | ||
//# sourceMappingURL=queries.js.map |
@@ -25,1 +25,2 @@ import { Header, HttpBody, HttpRequest, Method, ParsedAuthority } from "./contract"; | ||
export declare function uriString(request: HttpRequest): string; | ||
//# sourceMappingURL=requests.d.ts.map |
104
requests.js
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.uriString = exports.uri = exports.authority = exports.removeQueries = exports.removeQuery = exports.setQuery = exports.setQueries = exports.appendQuery = exports.appendQueries = exports.queries = exports.query = exports.delete_ = exports.patch = exports.put = exports.post = exports.get = exports.requestOf = void 0; | ||
var headers_1 = require("./headers"); | ||
var queries_1 = require("./queries"); | ||
var uri_1 = require("./uri"); | ||
var objects_1 = require("./util/objects"); | ||
function requestOf(method, uri, body) { | ||
var headers = []; | ||
for (var _i = 3; _i < arguments.length; _i++) { | ||
headers[_i - 3] = arguments[_i]; | ||
} | ||
const headers_1 = require("./headers"); | ||
const queries_1 = require("./queries"); | ||
const uri_1 = require("./uri"); | ||
const objects_1 = require("./util/objects"); | ||
function requestOf(method, uri, body, ...headers) { | ||
return { | ||
method: method, | ||
method, | ||
uri: uri_1.Uri.of(uri), | ||
@@ -41,44 +17,24 @@ headers: headers, | ||
exports.requestOf = requestOf; | ||
function get(uri) { | ||
var headers = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
headers[_i - 1] = arguments[_i]; | ||
} | ||
return requestOf.apply(void 0, __spread(["GET", uri, undefined], headers)); | ||
function get(uri, ...headers) { | ||
return requestOf("GET", uri, undefined, ...headers); | ||
} | ||
exports.get = get; | ||
function post(uri, body) { | ||
var headers = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
headers[_i - 2] = arguments[_i]; | ||
} | ||
return requestOf.apply(void 0, __spread(["POST", uri, body], headers)); | ||
function post(uri, body, ...headers) { | ||
return requestOf("POST", uri, body, ...headers); | ||
} | ||
exports.post = post; | ||
function put(uri, body) { | ||
var headers = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
headers[_i - 2] = arguments[_i]; | ||
} | ||
return requestOf.apply(void 0, __spread(["PUT", uri, body], headers)); | ||
function put(uri, body, ...headers) { | ||
return requestOf("PUT", uri, body, ...headers); | ||
} | ||
exports.put = put; | ||
function patch(uri, body) { | ||
var headers = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
headers[_i - 2] = arguments[_i]; | ||
} | ||
return requestOf.apply(void 0, __spread(["PATCH", uri, body], headers)); | ||
function patch(uri, body, ...headers) { | ||
return requestOf("PATCH", uri, body, ...headers); | ||
} | ||
exports.patch = patch; | ||
function delete_(uri) { | ||
var headers = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
headers[_i - 1] = arguments[_i]; | ||
} | ||
return requestOf.apply(void 0, __spread(["DELETE", uri, undefined], headers)); | ||
function delete_(uri, ...headers) { | ||
return requestOf("DELETE", uri, undefined, ...headers); | ||
} | ||
exports.delete_ = delete_; | ||
function query(message, name) { | ||
var query = message.uri.query; | ||
const query = message.uri.query; | ||
return queries_1.query(query, name); | ||
@@ -88,3 +44,3 @@ } | ||
function queries(message, name) { | ||
var query = message.uri.query; | ||
const query = message.uri.query; | ||
return queries_1.queries(query, name); | ||
@@ -94,3 +50,3 @@ } | ||
function appendQueries(message, queries) { | ||
var query = message.uri.query; | ||
const query = message.uri.query; | ||
return objects_1.modify(message, { uri: objects_1.modify(message.uri, { query: queries_1.appendQueries(query, queries) }) }); | ||
@@ -100,3 +56,3 @@ } | ||
function appendQuery(message, name, value) { | ||
var query = message.uri.query; | ||
const query = message.uri.query; | ||
return objects_1.modify(message, { uri: objects_1.modify(message.uri, { query: queries_1.appendQuery(query, name, value) }) }); | ||
@@ -106,3 +62,3 @@ } | ||
function setQueries(message, queries) { | ||
var query = message.uri.query; | ||
const query = message.uri.query; | ||
return objects_1.modify(message, { uri: objects_1.modify(message.uri, { query: queries_1.setQueries(query, queries) }) }); | ||
@@ -112,3 +68,3 @@ } | ||
function setQuery(message, name, value) { | ||
var query = message.uri.query; | ||
const query = message.uri.query; | ||
return objects_1.modify(message, { uri: objects_1.modify(message.uri, { query: queries_1.setQuery(query, name, value) }) }); | ||
@@ -118,13 +74,9 @@ } | ||
function removeQuery(message, name) { | ||
var query = message.uri.query; | ||
const query = message.uri.query; | ||
return objects_1.modify(message, { uri: objects_1.modify(message.uri, { query: queries_1.removeQuery(query, name) }) }); | ||
} | ||
exports.removeQuery = removeQuery; | ||
function removeQueries(message) { | ||
var names = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
names[_i - 1] = arguments[_i]; | ||
} | ||
var query = message.uri.query; | ||
return objects_1.modify(message, { uri: objects_1.modify(message.uri, { query: queries_1.removeQueries.apply(void 0, __spread([query], names)) }) }); | ||
function removeQueries(message, ...names) { | ||
const query = message.uri.query; | ||
return objects_1.modify(message, { uri: objects_1.modify(message.uri, { query: queries_1.removeQueries(query, ...names) }) }); | ||
} | ||
@@ -135,5 +87,5 @@ exports.removeQueries = removeQueries; | ||
return request.uri.authority; | ||
var value = headers_1.getHeaderValue(request.headers, 'Host'); | ||
const value = headers_1.getHeaderValue(request.headers, 'Host'); | ||
if (typeof value != 'string') | ||
throw new Error("Could not get authority from request uri '" + request.uri + "'"); | ||
throw new Error(`Could not get authority from request uri '${uri_1.Uri.of(request.uri).toString()}'`); | ||
return uri_1.Authority.parse(value); | ||
@@ -140,0 +92,0 @@ } |
@@ -94,3 +94,4 @@ import {Header, HttpBody, HttpRequest, Method, ParsedAuthority} from "./contract"; | ||
const value = getHeaderValue(request.headers, 'Host'); | ||
if (typeof value != 'string') throw new Error(`Could not get authority from request uri '${request.uri}'`); | ||
if (typeof value != 'string') | ||
throw new Error(`Could not get authority from request uri '${Uri.of(request.uri).toString()}'`); | ||
@@ -106,2 +107,2 @@ return Authority.parse(value); | ||
return Uri.of(request.uri).toString(); | ||
} | ||
} |
@@ -5,1 +5,2 @@ import { Header, HttpBody, HttpResponse } from "./contract"; | ||
export declare function notFound(body?: HttpBody, ...headers: Header[]): HttpResponse; | ||
//# sourceMappingURL=responses.d.ts.map |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.notFound = exports.ok = exports.responseOf = void 0; | ||
function responseOf(status, body) { | ||
var headers = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
headers[_i - 2] = arguments[_i]; | ||
} | ||
return { status: status, headers: headers, body: body ? body : '' }; | ||
function responseOf(status, body, ...headers) { | ||
return { status, headers: headers, body: body ? body : '' }; | ||
} | ||
exports.responseOf = responseOf; | ||
function ok(body) { | ||
var headers = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
headers[_i - 1] = arguments[_i]; | ||
} | ||
return responseOf.apply(void 0, __spread([200, body], headers)); | ||
function ok(body, ...headers) { | ||
return responseOf(200, body, ...headers); | ||
} | ||
exports.ok = ok; | ||
function notFound(body) { | ||
var headers = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
headers[_i - 1] = arguments[_i]; | ||
} | ||
return responseOf.apply(void 0, __spread([404, body], headers)); | ||
function notFound(body, ...headers) { | ||
return responseOf(404, body, ...headers); | ||
} | ||
exports.notFound = notFound; | ||
//# sourceMappingURL=responses.js.map |
@@ -1,7 +0,8 @@ | ||
import { HttpHandler, ParsedUri } from "./contract"; | ||
import { ParsedUri } from "./contract"; | ||
export interface Closeable<T = unknown> { | ||
close(): Promise<T>; | ||
} | ||
export interface Server extends HttpHandler, Closeable { | ||
export interface Server extends Closeable { | ||
url(): Promise<ParsedUri>; | ||
} | ||
//# sourceMappingURL=server.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import {HttpHandler, ParsedUri} from "./contract"; | ||
import {ParsedUri} from "./contract"; | ||
@@ -7,4 +7,4 @@ export interface Closeable<T = unknown> { | ||
export interface Server extends HttpHandler, Closeable { | ||
export interface Server extends Closeable { | ||
url(): Promise<ParsedUri> | ||
} |
@@ -37,1 +37,2 @@ import { ParsedAuthority, ParsedUri } from "./contract"; | ||
export declare function joinPaths(...segments: string[]): string; | ||
//# sourceMappingURL=uri.d.ts.map |
108
uri.js
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.joinPaths = exports.stripSlashes = exports.trailing = exports.leading = exports.Authority = exports.Uri = void 0; | ||
var Uri = /** @class */ (function () { | ||
function Uri(_a) { | ||
var scheme = _a.scheme, authority = _a.authority, path = _a.path, query = _a.query, fragment = _a.fragment; | ||
class Uri { | ||
constructor({ scheme, authority, path, query, fragment }) { | ||
this.scheme = scheme; | ||
@@ -30,21 +13,21 @@ this.authority = authority; | ||
/** {@link https://tools.ietf.org/html/rfc3986#appendix-B } */ | ||
Uri.parse = function (uri) { | ||
var match = Uri.RFC_3986.exec(uri); | ||
static parse(uri) { | ||
const match = Uri.RFC_3986.exec(uri); | ||
if (!match) | ||
throw new Error("Invalid Uri: " + uri); | ||
var _a = __read(match, 10), scheme = _a[2], authorityString = _a[4], path = _a[5], query = _a[7], fragment = _a[9]; | ||
var authority = typeof authorityString !== 'undefined' ? Authority.parse(authorityString) : undefined; | ||
return new Uri({ scheme: scheme, authority: authority, path: path, query: query, fragment: fragment }); | ||
}; | ||
Uri.of = function (uri) { | ||
throw new Error(`Invalid Uri: ${uri}`); | ||
const [, , scheme, , authorityString, path, , query, , fragment] = match; | ||
const authority = typeof authorityString !== 'undefined' ? Authority.parse(authorityString) : undefined; | ||
return new Uri({ scheme, authority, path, query, fragment }); | ||
} | ||
static of(uri) { | ||
if (uri instanceof Uri) | ||
return uri; | ||
return typeof uri === 'string' ? Uri.parse(uri) : new Uri(uri); | ||
}; | ||
Uri.modify = function (uri, modifications) { | ||
} | ||
static modify(uri, modifications) { | ||
return new Uri(Object.assign({}, uri, modifications)); | ||
}; | ||
} | ||
/** {@link https://tools.ietf.org/html/rfc3986#section-5.3} */ | ||
Uri.prototype.toString = function () { | ||
var result = []; | ||
toString() { | ||
const result = []; | ||
if (typeof this.scheme != 'undefined') | ||
@@ -60,13 +43,11 @@ result.push(this.scheme, ":"); | ||
return result.join(''); | ||
}; | ||
Uri.prototype.toJSON = function () { | ||
} | ||
toJSON() { | ||
return this.toString(); | ||
}; | ||
Uri.RFC_3986 = /^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/; | ||
return Uri; | ||
}()); | ||
} | ||
} | ||
exports.Uri = Uri; | ||
var Authority = /** @class */ (function () { | ||
function Authority(_a) { | ||
var user = _a.user, host = _a.host, port = _a.port; | ||
Uri.RFC_3986 = /^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/; | ||
class Authority { | ||
constructor({ user, host, port }) { | ||
if (typeof port !== 'undefined' && Number.isNaN(port)) | ||
@@ -80,21 +61,21 @@ throw Error("Port was NaN"); | ||
} | ||
Authority.of = function (authority) { | ||
static of(authority) { | ||
if (authority instanceof Authority) | ||
return authority; | ||
return typeof authority === 'string' ? Authority.parse(authority) : new Authority(authority); | ||
}; | ||
Authority.parse = function (input) { | ||
} | ||
static parse(input) { | ||
if (input === '') | ||
return Authority.of({ host: '' }); | ||
var match = Authority.REGEX.exec(input); | ||
const match = Authority.REGEX.exec(input); | ||
if (!match) | ||
throw new Error("Invalid Authority: " + input); | ||
var _a = __read(match, 4), user = _a[1], host = _a[2], portString = _a[3]; | ||
var port = portString ? Number.parseInt(portString) : undefined; | ||
throw new Error(`Invalid Authority: ${input}`); | ||
const [, user, host, portString] = match; | ||
const port = portString ? Number.parseInt(portString) : undefined; | ||
if (typeof port !== 'undefined' && Number.isNaN(port)) | ||
throw new Error("Invalid port '" + portString + "' in Authority: " + input); | ||
return Authority.of({ user: user, host: host, port: port }); | ||
}; | ||
Authority.prototype.toString = function () { | ||
var result = []; | ||
throw new Error(`Invalid port '${portString}' in Authority: ${input}`); | ||
return Authority.of({ user, host, port }); | ||
} | ||
toString() { | ||
const result = []; | ||
if (typeof this.user != 'undefined') | ||
@@ -107,10 +88,9 @@ result.push(this.user, "@"); | ||
return result.join(''); | ||
}; | ||
Authority.prototype.toJSON = function () { | ||
} | ||
toJSON() { | ||
return this.toString(); | ||
}; | ||
Authority.REGEX = /^(?:([^@]+)@)?(\[.+\]|[^:]+)(?:\:([\d]+))?$/; | ||
return Authority; | ||
}()); | ||
} | ||
} | ||
exports.Authority = Authority; | ||
Authority.REGEX = /^(?:([^@]+)@)?(\[.+\]|[^:]+)(?:\:([\d]+))?$/; | ||
exports.leading = /^\/*/; | ||
@@ -124,12 +104,8 @@ exports.trailing = /\/*$/; | ||
exports.stripSlashes = stripSlashes; | ||
function joinPaths() { | ||
var segments = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
segments[_i] = arguments[_i]; | ||
} | ||
return segments.reduce(function (acc, segment) { return segment === '/' | ||
function joinPaths(...segments) { | ||
return segments.reduce((acc, segment) => segment === '/' | ||
? acc | ||
: acc + "/" + stripSlashes(segment); }, ""); | ||
: `${acc}/${stripSlashes(segment)}`, ""); | ||
} | ||
exports.joinPaths = joinPaths; | ||
//# sourceMappingURL=uri.js.map |
@@ -23,1 +23,2 @@ import { Uri } from "./uri"; | ||
export {}; | ||
//# sourceMappingURL=UriTemplate.d.ts.map |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.uriTemplate = exports.UriTemplate = void 0; | ||
var regex_1 = require("./util/regex"); | ||
var uri_1 = require("./uri"); | ||
var UriTemplate = /** @class */ (function () { | ||
function UriTemplate(template) { | ||
const regex_1 = require("./util/regex"); | ||
const uri_1 = require("./uri"); | ||
class UriTemplate { | ||
constructor(template) { | ||
this.template = template; | ||
this.pathVariableValuesCapturingRegexp = new regex_1.Regex("^" + this.pathVariableCapturingTemplate() + "$"); | ||
this.pathVariableValuesCapturingRegexp = new regex_1.Regex(`^${this.pathVariableCapturingTemplate()}$`); | ||
} | ||
UriTemplate.of = function (template) { | ||
static of(template) { | ||
return new UriTemplate(template); | ||
}; | ||
UriTemplate.prototype.matches = function (uri) { | ||
var pathNoTrailingSlash = uri_1.Uri.of(uri).path.replace(/\/$/g, ''); | ||
} | ||
matches(uri) { | ||
const pathNoTrailingSlash = uri_1.Uri.of(uri).path.replace(/\/$/g, ''); | ||
return this.pathVariableValuesCapturingRegexp.match(pathNoTrailingSlash) !== null; | ||
}; | ||
UriTemplate.prototype.extract = function (uri) { | ||
} | ||
extract(uri) { | ||
return { | ||
@@ -39,12 +23,12 @@ query: this.extractQueryCaptures(uri), | ||
}; | ||
}; | ||
UriTemplate.prototype.expand = function (captures) { | ||
return Object.keys(captures.path).reduce(function (uri, captureName) { | ||
return uri.replace("{" + captureName + "}", encodeURIComponent(captures.path[captureName])); | ||
} | ||
expand(captures) { | ||
return Object.keys(captures.path).reduce((uri, captureName) => { | ||
return uri.replace(`{${captureName}}`, encodeURIComponent(captures.path[captureName])); | ||
}, this.template); | ||
}; | ||
UriTemplate.prototype.extractPathCaptures = function (path) { | ||
var pathVariableNames = (this.template.match(/{[^:}]+/g) || []).map(function (name) { return name.replace('{', ''); }); | ||
var values = this.pathVariableValuesCapturingRegexp.match(path); | ||
return pathVariableNames.reduce(function (captures, pathParam, index) { | ||
} | ||
extractPathCaptures(path) { | ||
const pathVariableNames = (this.template.match(/{[^:}]+/g) || []).map(name => name.replace('{', '')); | ||
const values = this.pathVariableValuesCapturingRegexp.match(path); | ||
return pathVariableNames.reduce((captures, pathParam, index) => { | ||
if (values && values[index + 1]) | ||
@@ -54,9 +38,9 @@ captures[pathParam] = decodeURIComponent(values[index + 1]); | ||
}, {}); | ||
}; | ||
UriTemplate.prototype.extractQueryCaptures = function (uri) { | ||
var query = uri_1.Uri.of(uri).query; | ||
} | ||
extractQueryCaptures(uri) { | ||
const query = uri_1.Uri.of(uri).query; | ||
if (!query) | ||
return {}; | ||
return (query).split('&').reduce(function (capture, query) { | ||
var _a = __read(query.split('='), 2), key = _a[0], value = _a[1]; | ||
return (query).split('&').reduce((capture, query) => { | ||
const [key, value] = query.split('='); | ||
if (capture[key]) { | ||
@@ -75,14 +59,13 @@ if (typeof capture[key] === 'string') { | ||
}, {}); | ||
}; | ||
UriTemplate.prototype.pathVariableCapturingTemplate = function () { | ||
var templateNoTrailingSlash = this.template.replace(/\/$/g, ''); | ||
var templateRewritingRegex = new regex_1.Regex('{([^}]+?)(?::([^}]+))?}'); | ||
var matches = Array.from(templateRewritingRegex.matches(templateNoTrailingSlash)); | ||
return matches.reduce(function (pathVariableCapturingTemplate, match) { | ||
var regexCapture = match && match[2]; | ||
return pathVariableCapturingTemplate.replace(/{[^}]+}/, regexCapture ? "(" + match[2] + ")" : '([^\/]+?)'); | ||
} | ||
pathVariableCapturingTemplate() { | ||
const templateNoTrailingSlash = this.template.replace(/\/$/g, ''); | ||
const templateRewritingRegex = new regex_1.Regex('{([^}]+?)(?::([^}]+))?}'); | ||
const matches = Array.from(templateRewritingRegex.matches(templateNoTrailingSlash)); | ||
return matches.reduce((pathVariableCapturingTemplate, match) => { | ||
const regexCapture = match && match[2]; | ||
return pathVariableCapturingTemplate.replace(/{[^}]+}/, regexCapture ? `(${match[2]})` : '([^\/]+?)'); | ||
}, templateNoTrailingSlash); | ||
}; | ||
return UriTemplate; | ||
}()); | ||
} | ||
} | ||
exports.UriTemplate = UriTemplate; | ||
@@ -89,0 +72,0 @@ function uriTemplate(template) { |
@@ -8,1 +8,2 @@ export declare type DecodedPair = [string, string | undefined]; | ||
export declare function encodePairs(values: DecodedPair[]): string | undefined; | ||
//# sourceMappingURL=urlEncoding.d.ts.map |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -29,4 +13,4 @@ exports.encodePairs = exports.encodePair = exports.decodePairs = exports.decodePair = exports.encode = exports.decode = void 0; | ||
function decodePair(pair) { | ||
var _a = __read(pair.split('='), 2), name = _a[0], value = _a[1]; | ||
var decoded = pair.indexOf('=') < 0 ? undefined : decode(value); | ||
const [name, value] = pair.split('='); | ||
const decoded = pair.indexOf('=') < 0 ? undefined : decode(value); | ||
return [decode(name), decoded]; | ||
@@ -40,11 +24,10 @@ } | ||
.split('&') | ||
.map(function (p) { return decodePair(p); }); | ||
.map(p => decodePair(p)); | ||
} | ||
exports.decodePairs = decodePairs; | ||
function encodePair(_a) { | ||
var _b = __read(_a, 2), name = _b[0], value = _b[1]; | ||
var encodedValue = value === undefined | ||
function encodePair([name, value]) { | ||
const encodedValue = value === undefined | ||
? "" | ||
: "=" + encode(value); | ||
return "" + encode(name) + encodedValue; | ||
: `=${encode(value)}`; | ||
return `${encode(name)}${encodedValue}`; | ||
} | ||
@@ -51,0 +34,0 @@ exports.encodePair = encodePair; |
@@ -12,1 +12,2 @@ export declare class AsyncIteratorHandler<T> implements AsyncIterator<T> { | ||
} | ||
//# sourceMappingURL=AsyncIteratorHandler.d.ts.map |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AsyncIteratorHandler = void 0; | ||
// TODO: can this be (much) more terse, and obviously correct? | ||
var AsyncIteratorHandler = /** @class */ (function () { | ||
function AsyncIteratorHandler() { | ||
class AsyncIteratorHandler { | ||
constructor() { | ||
this.pullQueue = []; | ||
this.pushQueue = []; | ||
} | ||
AsyncIteratorHandler.prototype.next = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
_this.pull([resolve, reject]); | ||
next() { | ||
return new Promise((resolve, reject) => { | ||
this.pull([resolve, reject]); | ||
}); | ||
}; | ||
AsyncIteratorHandler.prototype.push = function (value) { | ||
var serve = function (_a) { | ||
var _b = __read(_a, 2), resolve = _b[0], _ = _b[1]; | ||
return resolve({ | ||
done: false, | ||
value: value | ||
}); | ||
}; | ||
var waiting = this.pullQueue.shift(); | ||
} | ||
push(value) { | ||
const serve = ([resolve, _]) => resolve({ | ||
done: false, | ||
value: value | ||
}); | ||
const waiting = this.pullQueue.shift(); | ||
if (waiting) | ||
@@ -45,5 +25,5 @@ serve(waiting); | ||
this.pushQueue.push(serve); | ||
}; | ||
AsyncIteratorHandler.prototype.pull = function (thing) { | ||
var waiting = this.pushQueue.shift(); | ||
} | ||
pull(thing) { | ||
const waiting = this.pushQueue.shift(); | ||
if (waiting) | ||
@@ -53,38 +33,31 @@ waiting(thing); | ||
this.pullQueue.push(thing); | ||
}; | ||
AsyncIteratorHandler.prototype.end = function () { | ||
this.close(function (_a) { | ||
var _b = __read(_a, 2), resolve = _b[0], _ = _b[1]; | ||
return resolve({ | ||
done: true, | ||
value: undefined /* they got the interface wrong*/ | ||
}); | ||
}); | ||
this.end = function () { | ||
} | ||
end() { | ||
this.close(([resolve, _]) => resolve({ | ||
done: true, | ||
value: undefined /* they got the interface wrong*/ | ||
})); | ||
this.end = () => { | ||
}; | ||
}; | ||
AsyncIteratorHandler.prototype.error = function (e) { | ||
this.close(function (_a) { | ||
var _b = __read(_a, 2), _ = _b[0], err = _b[1]; | ||
} | ||
error(e) { | ||
this.close(([_, err]) => { | ||
err(e); | ||
}); | ||
}; | ||
} | ||
// TODO: this is being too cute. Make it simpler? | ||
AsyncIteratorHandler.prototype.close = function (final) { | ||
var oldPull = this.pull; | ||
var self = this; | ||
close(final) { | ||
const oldPull = this.pull; | ||
const self = this; | ||
this.pullQueue.forEach(final); | ||
this.pull = function (thing) { | ||
return self.pushQueue.length === 0 ? final(thing) : oldPull.bind(self)(thing); | ||
}; | ||
this.pull = (thing) => self.pushQueue.length === 0 ? final(thing) : oldPull.bind(self)(thing); | ||
// TODO: check the contract for AsyncIterator to confirm behaviour | ||
this.push = this.closed; | ||
this.close = this.closed; | ||
}; | ||
AsyncIteratorHandler.prototype.closed = function () { | ||
} | ||
closed() { | ||
throw new Error("Iterator is closed"); | ||
}; | ||
return AsyncIteratorHandler; | ||
}()); | ||
} | ||
} | ||
exports.AsyncIteratorHandler = AsyncIteratorHandler; | ||
//# sourceMappingURL=AsyncIteratorHandler.js.map |
export declare function modify<T extends object>(value: T, modifications: Partial<T>): T; | ||
//# sourceMappingURL=objects.d.ts.map |
@@ -5,1 +5,2 @@ export declare class Random { | ||
export declare function randomBytes(size: number): Uint8Array; | ||
//# sourceMappingURL=random.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.randomBytes = exports.Random = void 0; | ||
var Random = /** @class */ (function () { | ||
function Random() { | ||
} | ||
Random.bytes = randomBytes; | ||
return Random; | ||
}()); | ||
class Random { | ||
} | ||
exports.Random = Random; | ||
Random.bytes = randomBytes; | ||
function randomBytes(size) { | ||
var sizeInFloats = Math.round(size / 4) + 1; | ||
var randomFloats = new Array(sizeInFloats); | ||
for (var i = 0; i < randomFloats.length; i++) | ||
const sizeInFloats = Math.round(size / 4) + 1; | ||
const randomFloats = new Array(sizeInFloats); | ||
for (let i = 0; i < randomFloats.length; i++) | ||
randomFloats[i] = Math.random(); | ||
var buffer = Float32Array.from(randomFloats).buffer; | ||
const buffer = Float32Array.from(randomFloats).buffer; | ||
return new Uint8Array(buffer).slice(0, size); | ||
@@ -18,0 +15,0 @@ } |
@@ -9,1 +9,2 @@ export declare class Regex { | ||
} | ||
//# sourceMappingURL=regex.d.ts.map |
"use strict"; | ||
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 }); | ||
exports.Regex = void 0; | ||
var Regex = /** @class */ (function () { | ||
function Regex(pattern) { | ||
class Regex { | ||
constructor(pattern) { | ||
this.pattern = pattern; | ||
@@ -39,35 +12,25 @@ this.matched = []; | ||
} | ||
Regex.prototype.match = function (against) { | ||
match(against) { | ||
return new RegExp(this.pattern).exec(against); | ||
}; | ||
Regex.prototype.matches = function (against) { | ||
var regex, start, nonMatch, match, end; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
regex = new RegExp(this.pattern, 'g'); | ||
start = 0; | ||
nonMatch = ''; | ||
_a.label = 1; | ||
case 1: | ||
if (!(match = regex.exec(against))) return [3 /*break*/, 4]; | ||
if (!match) return [3 /*break*/, 3]; | ||
end = against.indexOf(match[0]); | ||
nonMatch = against.slice(start, end); | ||
start = end + match[0].length; | ||
// rest = against.slice(start); | ||
this.matched.push(match); | ||
this.nonMatched.push(nonMatch); | ||
return [4 /*yield*/, match]; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
*matches(against) { | ||
const regex = new RegExp(this.pattern, 'g'); | ||
let start = 0; | ||
let nonMatch = ''; | ||
// let rest = ''; | ||
let match; | ||
while (match = regex.exec(against)) { | ||
if (match) { | ||
const end = against.indexOf(match[0]); | ||
nonMatch = against.slice(start, end); | ||
start = end + match[0].length; | ||
// rest = against.slice(start); | ||
this.matched.push(match); | ||
this.nonMatched.push(nonMatch); | ||
yield match; | ||
} | ||
}); | ||
}; | ||
return Regex; | ||
}()); | ||
} | ||
} | ||
} | ||
exports.Regex = Regex; | ||
//# sourceMappingURL=regex.js.map |
export declare function textDecoder(): TextDecoder; | ||
export declare function textEncoder(): TextEncoder; | ||
//# sourceMappingURL=textencoding.d.ts.map |
@@ -7,3 +7,3 @@ "use strict"; | ||
return new TextDecoder('utf-8'); | ||
var util = require('util'); | ||
const util = require('util'); | ||
return new util.TextDecoder('utf-8'); | ||
@@ -15,3 +15,3 @@ } | ||
return new TextEncoder(); | ||
var util = require('util'); | ||
const util = require('util'); | ||
return new util.TextEncoder(); | ||
@@ -18,0 +18,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
204268
114
1
4
Yes
2501
2