@ublitzjs/core 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -14
- package/USAGE.md +16 -5
- package/babel.config.json +8 -0
- package/bun.lock +627 -0
- package/dist/cjs/http-codes.js +99 -0
- package/dist/cjs/http-headers.js +118 -0
- package/dist/cjs/index.js +104 -0
- package/dist/esm/http-codes.js +84 -0
- package/dist/esm/http-headers.js +119 -0
- package/dist/esm/index.js +71 -0
- package/dist/esm/package.json +1 -0
- package/dist/types/http-codes.d.ts +43 -0
- package/dist/types/http-headers.d.ts +1040 -0
- package/dist/types/index.d.ts +249 -0
- package/package.json +27 -7
- package/src/http-codes.ts +94 -0
- package/{types/http-headers.d.ts → src/http-headers.ts} +75 -18
- package/src/index.ts +360 -0
- package/tmp/cjs.cjs +75 -0
- package/tmp/esm.mjs +75 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +9 -20
- package/tsconfig.types.json +11 -0
- package/cjs/http-codes.cjs +0 -56
- package/cjs/http-headers.cjs +0 -64
- package/cjs/index.cjs +0 -61
- package/mjs/http-codes.mjs +0 -52
- package/mjs/http-headers.mjs +0 -63
- package/mjs/index.mjs +0 -51
- package/types/http-codes.d.ts +0 -54
- package/types/index.d.ts +0 -170
- package/types/uws-types.d.ts +0 -123
package/cjs/http-codes.cjs
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var { toAB } = require("./index.cjs");
|
|
3
|
-
var c411 = toAB("411");
|
|
4
|
-
var c400 = toAB("400");
|
|
5
|
-
var c413 = toAB("413");
|
|
6
|
-
var c405 = toAB("405");
|
|
7
|
-
var c404 = toAB("404");
|
|
8
|
-
var c405Message = toAB("Method is not allowed");
|
|
9
|
-
var allowHeader = toAB("Allow");
|
|
10
|
-
function checkContentLength(res, req) {
|
|
11
|
-
var CL = Number(req.getHeader("content-length"));
|
|
12
|
-
if (!CL) {
|
|
13
|
-
res.finished = true;
|
|
14
|
-
res.cork(() => res.writeStatus(c411).endWithoutBody(0, true));
|
|
15
|
-
throw new Error("Wrong content-length", { cause: { CL } });
|
|
16
|
-
}
|
|
17
|
-
return CL;
|
|
18
|
-
}
|
|
19
|
-
function badRequest(res, error, causeForYou) {
|
|
20
|
-
res.finished = true;
|
|
21
|
-
if (!res.aborted) res.cork(() => res.writeStatus(c400).end(toAB(error)));
|
|
22
|
-
throw new Error("Bad request", { cause: causeForYou });
|
|
23
|
-
}
|
|
24
|
-
function tooLargeBody(res, limit) {
|
|
25
|
-
var message = toAB("Body is too large. Limit in bytes - " + limit);
|
|
26
|
-
if (!res.aborted) res.cork(() => res.writeStatus(c413).end(message));
|
|
27
|
-
res.finished = true;
|
|
28
|
-
throw new Error("body too large", { cause: { limit } });
|
|
29
|
-
}
|
|
30
|
-
function seeOtherMethods(methodsArr) {
|
|
31
|
-
var methods = toAB(
|
|
32
|
-
methodsArr
|
|
33
|
-
.map((method) => method.toUpperCase())
|
|
34
|
-
.join(", ")
|
|
35
|
-
.replace("DEL", "DELETE")
|
|
36
|
-
.replace(/ WS,*/g, "")
|
|
37
|
-
);
|
|
38
|
-
return (res) =>
|
|
39
|
-
res.writeStatus(c405).writeHeader(allowHeader, methods).end(c405Message);
|
|
40
|
-
}
|
|
41
|
-
function notFoundConstructor(message = "Not found") {
|
|
42
|
-
var mes = toAB(message);
|
|
43
|
-
return (res) => res.writeStatus(c404).end(mes, true);
|
|
44
|
-
}
|
|
45
|
-
module.exports = {
|
|
46
|
-
badRequest,
|
|
47
|
-
checkContentLength,
|
|
48
|
-
notFoundConstructor,
|
|
49
|
-
seeOtherMethods,
|
|
50
|
-
tooLargeBody,
|
|
51
|
-
c400,
|
|
52
|
-
c404,
|
|
53
|
-
c405,
|
|
54
|
-
c411,
|
|
55
|
-
c413,
|
|
56
|
-
};
|
package/cjs/http-headers.cjs
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var { toAB } = require("./index.cjs");
|
|
3
|
-
var helmetHeaders = {
|
|
4
|
-
"X-Content-Type-Options": "nosniff",
|
|
5
|
-
"X-DNS-Prefetch-Control": "off",
|
|
6
|
-
"X-Frame-Options": "DENY",
|
|
7
|
-
"Referrer-Policy": "same-origin",
|
|
8
|
-
"X-Permitted-Cross-Domain-Policies": "none",
|
|
9
|
-
"X-Download-Options": "noopen",
|
|
10
|
-
"Cross-Origin-Resource-Policy": "same-origin",
|
|
11
|
-
"Cross-Origin-Opener-Policy": "same-origin",
|
|
12
|
-
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
13
|
-
"Origin-Agent-Cluster": "?1",
|
|
14
|
-
//"Content-Security-Policy-Report-Only":"",
|
|
15
|
-
//"Strict-Transport-Security":`max-age=${60 * 60 * 24 * 365}; includeSubDomains`,
|
|
16
|
-
};
|
|
17
|
-
class HeadersMap extends Map {
|
|
18
|
-
constructor(opts) {
|
|
19
|
-
super();
|
|
20
|
-
this.currentHeaders = opts;
|
|
21
|
-
}
|
|
22
|
-
remove(keys) {
|
|
23
|
-
for (const key of arguments) delete this.currentHeaders[key];
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
26
|
-
prepare() {
|
|
27
|
-
for (const key in this.currentHeaders)
|
|
28
|
-
this.set(toAB(key), toAB(this.currentHeaders[key]));
|
|
29
|
-
return delete this.currentHeaders, (res) => this.toRes(res);
|
|
30
|
-
}
|
|
31
|
-
toRes(res) {
|
|
32
|
-
for (const pair of this) res.writeHeader(pair[0], pair[1]);
|
|
33
|
-
return res;
|
|
34
|
-
}
|
|
35
|
-
static baseObj = helmetHeaders;
|
|
36
|
-
}
|
|
37
|
-
function setCSP(mainCSP, ...remove) {
|
|
38
|
-
var CSPstring = "";
|
|
39
|
-
for (const dir of remove) delete mainCSP[dir];
|
|
40
|
-
for (var key in mainCSP)
|
|
41
|
-
CSPstring += key + " " + mainCSP[key].join(" ") + "; ";
|
|
42
|
-
return CSPstring;
|
|
43
|
-
}
|
|
44
|
-
var CSPDirs = {
|
|
45
|
-
"default-src": ["'self'"],
|
|
46
|
-
"base-uri": ["'self'"],
|
|
47
|
-
"font-src": ["'self'"],
|
|
48
|
-
"form-action": ["'self'"],
|
|
49
|
-
"frame-ancestors": ["'none'"],
|
|
50
|
-
"img-src": ["'self'"],
|
|
51
|
-
"connect-src": ["'self'"],
|
|
52
|
-
"object-src": ["'none'"],
|
|
53
|
-
"script-src": ["'self'"],
|
|
54
|
-
"script-src-attr": ["'none'"],
|
|
55
|
-
"script-src-elem": ["'self'"],
|
|
56
|
-
"style-src": ["'self'"],
|
|
57
|
-
"style-src-attr": ["'none'"],
|
|
58
|
-
"style-src-elem": ["'self'"],
|
|
59
|
-
"trusted-types": ["'none'"],
|
|
60
|
-
"upgrade-insecure-requests": [],
|
|
61
|
-
"worker-src": ["'self'"],
|
|
62
|
-
"media-src": ["'self'"],
|
|
63
|
-
};
|
|
64
|
-
module.exports = { CSPDirs, HeadersMap, setCSP };
|
package/cjs/index.cjs
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var uWS = require("uWebSockets.js");
|
|
3
|
-
var { Buffer } = require("node:buffer");
|
|
4
|
-
|
|
5
|
-
var { EventEmitter } = require("tseep");
|
|
6
|
-
function registerAbort(res) {
|
|
7
|
-
if (typeof res.aborted === "boolean")
|
|
8
|
-
throw new Error("abort already registered");
|
|
9
|
-
res.aborted = false;
|
|
10
|
-
res.emitter = new EventEmitter();
|
|
11
|
-
return res.onAborted(() => {
|
|
12
|
-
res.aborted = true;
|
|
13
|
-
res.emitter.emit("abort");
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
var closure = (param) => param();
|
|
17
|
-
function extendApp(app, ...rest) {
|
|
18
|
-
app.register = function (plugin) {
|
|
19
|
-
return plugin(this), this;
|
|
20
|
-
};
|
|
21
|
-
app.onError = function (fn) {
|
|
22
|
-
return (this._errHandler = fn), this;
|
|
23
|
-
};
|
|
24
|
-
app._startPromises = [];
|
|
25
|
-
app.ready = function () {
|
|
26
|
-
return Promise.all(this._startPromises).then(
|
|
27
|
-
() => (this._startPromises = [])
|
|
28
|
-
);
|
|
29
|
-
};
|
|
30
|
-
app.route = function (opts, plugins) {
|
|
31
|
-
if (plugins) for (const p of plugins) p(opts, this);
|
|
32
|
-
return this[opts.method](opts.path, opts.controller);
|
|
33
|
-
};
|
|
34
|
-
for (const extension of rest) Object.assign(app, extension);
|
|
35
|
-
return app;
|
|
36
|
-
}
|
|
37
|
-
function toAB(data) {
|
|
38
|
-
var NodeBuf = data instanceof Buffer ? data : Buffer.from(data);
|
|
39
|
-
return NodeBuf.buffer.slice(
|
|
40
|
-
NodeBuf.byteOffset,
|
|
41
|
-
NodeBuf.byteOffset + NodeBuf.byteLength
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
var DeclarativeResponse = uWS.DeclarativeResponse;
|
|
45
|
-
DeclarativeResponse.prototype.writeHeaders = function (headers) {
|
|
46
|
-
for (const key in headers) this.writeHeader(key, headers[key]);
|
|
47
|
-
return this;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
module.exports = {
|
|
51
|
-
DeclarativeResponse,
|
|
52
|
-
extendApp,
|
|
53
|
-
registerAbort,
|
|
54
|
-
toAB,
|
|
55
|
-
closure,
|
|
56
|
-
};
|
|
57
|
-
Object.assign(
|
|
58
|
-
module.exports,
|
|
59
|
-
require("./http-headers.cjs"),
|
|
60
|
-
require("./http-codes.cjs")
|
|
61
|
-
);
|
package/mjs/http-codes.mjs
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
import { toAB } from "./index.mjs";
|
|
3
|
-
export var c411 = toAB("411");
|
|
4
|
-
export var c400 = toAB("400");
|
|
5
|
-
export var c413 = toAB("413");
|
|
6
|
-
export var c405 = toAB("405");
|
|
7
|
-
export var c404 = toAB("404");
|
|
8
|
-
var c405Message = toAB("Method is not allowed");
|
|
9
|
-
var allowHeader = toAB("Allow");
|
|
10
|
-
var checkHeader = toAB("content-length");
|
|
11
|
-
var checkMessage = toAB("Content-Length required to be > 0");
|
|
12
|
-
function checkContentLength(res, req) {
|
|
13
|
-
var CL = Number(req.getHeader(checkHeader));
|
|
14
|
-
if (!CL) {
|
|
15
|
-
res.finished = true;
|
|
16
|
-
res.cork(() => res.writeStatus(c411).end(checkMessage));
|
|
17
|
-
throw new Error("Wrong content-length", { cause: { CL } });
|
|
18
|
-
}
|
|
19
|
-
return CL;
|
|
20
|
-
}
|
|
21
|
-
function badRequest(res, error, causeForYou) {
|
|
22
|
-
res.finished = true;
|
|
23
|
-
if (!res.aborted) res.cork(() => res.writeStatus(c400).end(toAB(error)));
|
|
24
|
-
throw new Error("Bad request", { cause: causeForYou });
|
|
25
|
-
}
|
|
26
|
-
function tooLargeBody(res, limit) {
|
|
27
|
-
var message = toAB("Body is too large. Limit in bytes - " + limit);
|
|
28
|
-
if (!res.aborted) res.cork(() => res.writeStatus(c413).end(message));
|
|
29
|
-
res.finished = true;
|
|
30
|
-
}
|
|
31
|
-
function seeOtherMethods(methodsArr) {
|
|
32
|
-
var methods = toAB(
|
|
33
|
-
methodsArr
|
|
34
|
-
.map((method) => method.toUpperCase())
|
|
35
|
-
.join(", ")
|
|
36
|
-
.replace("DEL", "DELETE")
|
|
37
|
-
.replace(/ WS,*/g, "")
|
|
38
|
-
);
|
|
39
|
-
return (res) =>
|
|
40
|
-
res.writeStatus(c405).writeHeader(allowHeader, methods).end(c405Message);
|
|
41
|
-
}
|
|
42
|
-
function notFoundConstructor(message = "Not found") {
|
|
43
|
-
var mes = toAB(message);
|
|
44
|
-
return (res) => res.writeStatus(c404).end(mes, true);
|
|
45
|
-
}
|
|
46
|
-
export {
|
|
47
|
-
badRequest,
|
|
48
|
-
checkContentLength,
|
|
49
|
-
notFoundConstructor,
|
|
50
|
-
seeOtherMethods,
|
|
51
|
-
tooLargeBody,
|
|
52
|
-
};
|
package/mjs/http-headers.mjs
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
import { toAB } from "./index.mjs";
|
|
3
|
-
var helmetHeaders = {
|
|
4
|
-
"X-Content-Type-Options": "nosniff",
|
|
5
|
-
"X-DNS-Prefetch-Control": "off",
|
|
6
|
-
"X-Frame-Options": "DENY",
|
|
7
|
-
"Referrer-Policy": "same-origin",
|
|
8
|
-
"X-Permitted-Cross-Domain-Policies": "none",
|
|
9
|
-
"X-Download-Options": "noopen",
|
|
10
|
-
"Cross-Origin-Resource-Policy": "same-origin",
|
|
11
|
-
"Cross-Origin-Opener-Policy": "same-origin",
|
|
12
|
-
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
13
|
-
"Origin-Agent-Cluster": "?1",
|
|
14
|
-
//"Content-Security-Policy-Report-Only":"",
|
|
15
|
-
//"Strict-Transport-Security":`max-age=${60 * 60 * 24 * 365}; includeSubDomains`,
|
|
16
|
-
};
|
|
17
|
-
class HeadersMap extends Map {
|
|
18
|
-
constructor(opts) {
|
|
19
|
-
super();
|
|
20
|
-
this.currentHeaders = opts;
|
|
21
|
-
}
|
|
22
|
-
remove(keys) {
|
|
23
|
-
for (const key of arguments) delete this.currentHeaders[key];
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
26
|
-
prepare() {
|
|
27
|
-
for (const key in this.currentHeaders)
|
|
28
|
-
this.set(toAB(key), toAB(this.currentHeaders[key]));
|
|
29
|
-
return delete this.currentHeaders, (res) => this.toRes(res);
|
|
30
|
-
}
|
|
31
|
-
toRes(res) {
|
|
32
|
-
for (var pair of this) res.writeHeader(pair[0], pair[1]);
|
|
33
|
-
return res;
|
|
34
|
-
}
|
|
35
|
-
static baseObj = helmetHeaders;
|
|
36
|
-
}
|
|
37
|
-
function setCSP(mainCSP, ...remove) {
|
|
38
|
-
var CSPstring = "";
|
|
39
|
-
for (const dir of remove) delete mainCSP[dir];
|
|
40
|
-
for (var key in mainCSP)
|
|
41
|
-
CSPstring += key + " " + mainCSP[key].join(" ") + "; ";
|
|
42
|
-
return CSPstring;
|
|
43
|
-
}
|
|
44
|
-
var CSPDirs = {
|
|
45
|
-
"default-src": ["'self'"],
|
|
46
|
-
"base-uri": ["'self'"],
|
|
47
|
-
"font-src": ["'self'"],
|
|
48
|
-
"form-action": ["'self'"],
|
|
49
|
-
"frame-ancestors": ["'none'"],
|
|
50
|
-
"img-src": ["'self'"],
|
|
51
|
-
"connect-src": ["'self'"],
|
|
52
|
-
"object-src": ["'none'"],
|
|
53
|
-
"script-src": ["'self'"],
|
|
54
|
-
"script-src-attr": ["'none'"],
|
|
55
|
-
"script-src-elem": ["'self'"],
|
|
56
|
-
"style-src": ["'self'"],
|
|
57
|
-
"style-src-attr": ["'none'"],
|
|
58
|
-
"style-src-elem": ["'self'"],
|
|
59
|
-
"trusted-types": ["'none'"],
|
|
60
|
-
"worker-src": ["'self'"],
|
|
61
|
-
"media-src": ["'self'"],
|
|
62
|
-
};
|
|
63
|
-
export { CSPDirs, HeadersMap, setCSP };
|
package/mjs/index.mjs
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
import uWS from "uWebSockets.js";
|
|
3
|
-
import { Buffer } from "node:buffer";
|
|
4
|
-
export * from "./http-headers.mjs";
|
|
5
|
-
export * from "./http-codes.mjs";
|
|
6
|
-
import { EventEmitter } from "tseep";
|
|
7
|
-
function registerAbort(res) {
|
|
8
|
-
if (typeof res.aborted === "boolean")
|
|
9
|
-
throw new Error("abort already registered");
|
|
10
|
-
res.aborted = false;
|
|
11
|
-
res.emitter = new EventEmitter();
|
|
12
|
-
return res.onAborted(() => {
|
|
13
|
-
res.aborted = true;
|
|
14
|
-
res.emitter.emit("abort");
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
var closure = (param) => param();
|
|
18
|
-
function extendApp(app, ...rest) {
|
|
19
|
-
app.register = function (plugin) {
|
|
20
|
-
return plugin(this), this;
|
|
21
|
-
};
|
|
22
|
-
app.onError = function (fn) {
|
|
23
|
-
return (this._errHandler = fn), this;
|
|
24
|
-
};
|
|
25
|
-
app._startPromises = [];
|
|
26
|
-
app.ready = function () {
|
|
27
|
-
return Promise.all(this._startPromises).then(
|
|
28
|
-
() => (this._startPromises = [])
|
|
29
|
-
);
|
|
30
|
-
};
|
|
31
|
-
app.route = function (opts, plugins) {
|
|
32
|
-
if (plugins) for (const p of plugins) p(opts, this);
|
|
33
|
-
return this[opts.method](opts.path, opts.controller);
|
|
34
|
-
};
|
|
35
|
-
for (const extension of rest) Object.assign(app, extension);
|
|
36
|
-
return app;
|
|
37
|
-
}
|
|
38
|
-
function toAB(data) {
|
|
39
|
-
var NodeBuf = data instanceof Buffer ? data : Buffer.from(data);
|
|
40
|
-
return NodeBuf.buffer.slice(
|
|
41
|
-
NodeBuf.byteOffset,
|
|
42
|
-
NodeBuf.byteOffset + NodeBuf.byteLength
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
var DeclarativeResponse = uWS.DeclarativeResponse;
|
|
46
|
-
DeclarativeResponse.prototype.writeHeaders = function (headers) {
|
|
47
|
-
for (const key in headers) this.writeHeader(key, headers[key]);
|
|
48
|
-
return this;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export { DeclarativeResponse, extendApp, registerAbort, toAB, closure };
|
package/types/http-codes.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { HttpResponse as uwsHttpResponse } from "uWebSockets.js";
|
|
2
|
-
import type { HttpRequest, HttpMethods } from ".";
|
|
3
|
-
/**
|
|
4
|
-
* If something wrong is to content-length, sends 411 code and throws error with a "cause"
|
|
5
|
-
*/
|
|
6
|
-
export function checkContentLength(
|
|
7
|
-
res: uwsHttpResponse,
|
|
8
|
-
req: HttpRequest
|
|
9
|
-
): number;
|
|
10
|
-
/**
|
|
11
|
-
* sends http 400 and throws an Error with "causeForYou"
|
|
12
|
-
*/
|
|
13
|
-
export function badRequest(
|
|
14
|
-
res: uwsHttpResponse,
|
|
15
|
-
error: string,
|
|
16
|
-
causeForYou: string
|
|
17
|
-
): void;
|
|
18
|
-
/**
|
|
19
|
-
* sends http 413 And throws, but doesn't throw an Error
|
|
20
|
-
*/
|
|
21
|
-
export function tooLargeBody(res: uwsHttpResponse, limit: number): void;
|
|
22
|
-
/**
|
|
23
|
-
* Constructs function, which sends http 405 and sets http Allow header with all methods you passed
|
|
24
|
-
*/
|
|
25
|
-
export function seeOtherMethods(
|
|
26
|
-
methodsArr: HttpMethods[]
|
|
27
|
-
): (res: uwsHttpResponse, req: any) => any;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Constructs the function, which sets 404 http code and sends the message you have specified
|
|
31
|
-
*/
|
|
32
|
-
export function notFoundConstructor(
|
|
33
|
-
message?: string
|
|
34
|
-
): (res: uwsHttpResponse, req: any) => any;
|
|
35
|
-
/**
|
|
36
|
-
* code: required content length
|
|
37
|
-
*/
|
|
38
|
-
export var c411: ArrayBuffer;
|
|
39
|
-
/**
|
|
40
|
-
* code: bad request
|
|
41
|
-
*/
|
|
42
|
-
export var c400: ArrayBuffer;
|
|
43
|
-
/**
|
|
44
|
-
* code: payload too large
|
|
45
|
-
*/
|
|
46
|
-
export var c413: ArrayBuffer;
|
|
47
|
-
/**
|
|
48
|
-
* code: method not allowed
|
|
49
|
-
*/
|
|
50
|
-
export var c405: ArrayBuffer;
|
|
51
|
-
/**
|
|
52
|
-
* code: not found
|
|
53
|
-
*/
|
|
54
|
-
export var c404: ArrayBuffer;
|
package/types/index.d.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
HttpRequest as uwsHttpRequest,
|
|
3
|
-
TemplatedApp,
|
|
4
|
-
HttpResponse as uwsHttpResponse,
|
|
5
|
-
RecognizedString,
|
|
6
|
-
us_socket_context_t,
|
|
7
|
-
} from "uWebSockets.js";
|
|
8
|
-
import { Buffer } from "node:buffer";
|
|
9
|
-
|
|
10
|
-
import { EventEmitter } from "tseep";
|
|
11
|
-
import type { DocumentedWSBehavior } from "./uws-types";
|
|
12
|
-
/**
|
|
13
|
-
* function to effortlessly mark response as aborted AND to attach an event emitter, so that you can easily scale the handler. If you don't need event emitter and only some res.aborted - set it by yourself (no overkill for the handler)
|
|
14
|
-
* @param res
|
|
15
|
-
*/
|
|
16
|
-
export function registerAbort(res: uwsHttpResponse): HttpResponse;
|
|
17
|
-
/**
|
|
18
|
-
* An extended version of uWS.App . It provides you with several features:
|
|
19
|
-
* 1) plugin registration (just like in Fastify);
|
|
20
|
-
* 2) "ready" function and _startPromises array (use it to make your code more asynchronous and safe)
|
|
21
|
-
* 3) "route" function for more descriptive adn extensible way of registering handlers
|
|
22
|
-
* 4) "onError" function (mainly used by @ublitzjs/router)
|
|
23
|
-
*/
|
|
24
|
-
export interface Server extends TemplatedApp {
|
|
25
|
-
/**
|
|
26
|
-
* It is same as plugins in Fastify -> you register some routes in remove file
|
|
27
|
-
* @param plugin
|
|
28
|
-
* @returns itself for chaining methods
|
|
29
|
-
*/
|
|
30
|
-
register(plugin: (server: this) => void): this;
|
|
31
|
-
/** set global _errHandler (in fact it is used only by @ublitzjs/router) */
|
|
32
|
-
onError(fn: (error: Error, res: HttpResponse, data: any) => any): this;
|
|
33
|
-
_errHandler?(error: Error, res: HttpResponse, data: any): any;
|
|
34
|
-
/**some undocumented property in uWS - get it here */
|
|
35
|
-
addChildAppDescriptor(...any: any[]): this;
|
|
36
|
-
/**a function, which awaits all promises inside _startPromises array and then clears it*/
|
|
37
|
-
ready: () => Promise<any[]>;
|
|
38
|
-
/**
|
|
39
|
-
* simple array of promises. You can push several inside and await all of them using server.ready() method
|
|
40
|
-
*/
|
|
41
|
-
_startPromises: Promise<any>[];
|
|
42
|
-
/**
|
|
43
|
-
* this function allows you to create new handlers but more dynamically than uWS. Best use case - development. By default, the first param you pass includes a method, path, and a controller. However you can configure it for additional properties, which can be consumed on startup by second param - plugins
|
|
44
|
-
* @example
|
|
45
|
-
* server.route<onlyHttpMethods, {deprecated:boolean}>({
|
|
46
|
-
* method:"any",
|
|
47
|
-
* path: "/",
|
|
48
|
-
* controller(res){ res.end("hello") },
|
|
49
|
-
* deprecated:true
|
|
50
|
-
* },
|
|
51
|
-
* [
|
|
52
|
-
* (opts)=>{
|
|
53
|
-
* if(opts.deprecated) console.error("DEPRECATION FOUND", opts.path)
|
|
54
|
-
* }
|
|
55
|
-
* ])
|
|
56
|
-
*/
|
|
57
|
-
route<T extends HttpMethods, obj extends object = {}>(
|
|
58
|
-
opts: routeFNOpts<T> & obj,
|
|
59
|
-
plugins?: ((param: typeof opts, server: this) => void)[]
|
|
60
|
-
): this;
|
|
61
|
-
get(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
62
|
-
post(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
63
|
-
options(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
64
|
-
del(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
65
|
-
patch(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
66
|
-
put(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
67
|
-
head(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
68
|
-
connect(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
69
|
-
trace(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
70
|
-
any(pattern: RecognizedString, handler: HttpControllerFn): this;
|
|
71
|
-
}
|
|
72
|
-
export type routeFNOpts<T extends HttpMethods> = {
|
|
73
|
-
method: T;
|
|
74
|
-
path: RecognizedString;
|
|
75
|
-
controller: T extends "ws" ? DocumentedWSBehavior<any> : HttpControllerFn;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Utility for making closures. Exists for organizing code and caching values in non-global scope.
|
|
80
|
-
* @param param
|
|
81
|
-
* @returns what you passed
|
|
82
|
-
*/
|
|
83
|
-
export var closure: <T>(param: () => T) => T;
|
|
84
|
-
/**
|
|
85
|
-
* little more typed response which has:
|
|
86
|
-
* 1) "emitter", that comes from "tseep" package
|
|
87
|
-
* 2) "aborted" flag
|
|
88
|
-
* 3) "finished" flag
|
|
89
|
-
* 4) "collect" function, which comes from original uWS (and isn't documented), but the purpose remains unknown
|
|
90
|
-
*/
|
|
91
|
-
export interface HttpResponse<UserDataForWS extends object = {}>
|
|
92
|
-
extends uwsHttpResponse {
|
|
93
|
-
upgrade<UserData = UserDataForWS>(
|
|
94
|
-
userData: UserData,
|
|
95
|
-
secWebSocketKey: RecognizedString,
|
|
96
|
-
secWebSocketProtocol: RecognizedString,
|
|
97
|
-
secWebSocketExtensions: RecognizedString,
|
|
98
|
-
context: us_socket_context_t
|
|
99
|
-
): void;
|
|
100
|
-
/**
|
|
101
|
-
* This method actually exists in original uWebSockets.js, but is undocumented. I'll put it here for your IDE to be happy
|
|
102
|
-
*/
|
|
103
|
-
collect: (...any: any[]) => any;
|
|
104
|
-
/**
|
|
105
|
-
* An event emitter, which lets you subscribe several listeners to "abort" event OR your own events, defined with Symbol().
|
|
106
|
-
*/
|
|
107
|
-
emitter: EventEmitter<{
|
|
108
|
-
abort: () => void;
|
|
109
|
-
[k: symbol]: (...any: any[]) => void;
|
|
110
|
-
}>;
|
|
111
|
-
/**
|
|
112
|
-
* changes when res.onAborted fires.
|
|
113
|
-
*/
|
|
114
|
-
aborted?: boolean;
|
|
115
|
-
/**
|
|
116
|
-
* You should set it manually when ending the response. Particularly useful if some error has fired and you are doubting whether res.aborted is a sufficient flag.
|
|
117
|
-
*/
|
|
118
|
-
finished: boolean;
|
|
119
|
-
}
|
|
120
|
-
/**This HttpRequest is same as original uWS.HttpRequest, but getHeader method is typed for additional tips
|
|
121
|
-
* @example
|
|
122
|
-
* import {lowHeaders} from "ublitzjs"
|
|
123
|
-
* // some handler later
|
|
124
|
-
* req.getHeader<lowHeaders>("content-type")
|
|
125
|
-
*/
|
|
126
|
-
export interface HttpRequest extends uwsHttpRequest {
|
|
127
|
-
getHeader<T extends RecognizedString = RecognizedString>(a: T): string;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export type HttpControllerFn = (
|
|
131
|
-
res: HttpResponse,
|
|
132
|
-
req: HttpRequest
|
|
133
|
-
) => any | Promise<any>;
|
|
134
|
-
/**
|
|
135
|
-
* only http methods without "ws"
|
|
136
|
-
*/
|
|
137
|
-
export type onlyHttpMethods =
|
|
138
|
-
| "get"
|
|
139
|
-
| "post"
|
|
140
|
-
| "del"
|
|
141
|
-
| "patch"
|
|
142
|
-
| "put"
|
|
143
|
-
| "head"
|
|
144
|
-
| "trace"
|
|
145
|
-
| "options"
|
|
146
|
-
| "connect"
|
|
147
|
-
| "any";
|
|
148
|
-
/**
|
|
149
|
-
* all httpMethods with "ws" method
|
|
150
|
-
*/
|
|
151
|
-
export type HttpMethods = onlyHttpMethods | "ws"; //NOT A HTTP METHOD, but had to put it here
|
|
152
|
-
type MergeObjects<T extends any[]> = T extends [infer First, ...infer Rest]
|
|
153
|
-
? First & MergeObjects<Rest extends any[] ? Rest : []>
|
|
154
|
-
: {};
|
|
155
|
-
/**
|
|
156
|
-
* extends uWS.App() or uWS.SSLApp. See interface Server
|
|
157
|
-
* @param app uWS.App()
|
|
158
|
-
*/
|
|
159
|
-
export function extendApp<T extends object[]>(
|
|
160
|
-
app: TemplatedApp,
|
|
161
|
-
...rest: T
|
|
162
|
-
): Server & MergeObjects<T>;
|
|
163
|
-
/**
|
|
164
|
-
* conversion to ArrayBuffer ('cause transferring strings to uWS is really slow)
|
|
165
|
-
*/
|
|
166
|
-
export function toAB(data: Buffer | string): ArrayBuffer;
|
|
167
|
-
|
|
168
|
-
export * from "./http-headers";
|
|
169
|
-
export * from "./http-codes";
|
|
170
|
-
export * from "./uws-types";
|