@ublitzjs/core 0.1.1 → 1.0.1

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.
@@ -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
- };
@@ -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/logo.png DELETED
Binary file
@@ -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
- };
@@ -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/tsconfig.json DELETED
@@ -1,41 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Enable latest features
4
- "lib": ["ESNext"],
5
- "target": "ESNext",
6
- "module": "esnext",
7
- "moduleDetection": "force",
8
- "allowJs": true,
9
-
10
- // Bundler mode
11
- "moduleResolution": "bundler",
12
- "allowImportingTsExtensions": true,
13
- "verbatimModuleSyntax": true,
14
- "noEmit": true,
15
- "skipLibCheck": false,
16
- "noLib": false,
17
- // Best practices
18
- "strict": true,
19
- "alwaysStrict": true,
20
- "noFallthroughCasesInSwitch": true,
21
- "removeComments": true,
22
-
23
- // Some stricter flags (disabled by default)
24
- "noUnusedLocals": true,
25
- "allowUnusedLabels": false,
26
- "strictBuiltinIteratorReturn": true,
27
- "strictBindCallApply": true,
28
- "noUnusedParameters": true,
29
- "noImplicitAny": true,
30
- "noImplicitReturns": true,
31
- "noUncheckedIndexedAccess": true,
32
- "noUncheckedSideEffectImports": true,
33
- "strictNullChecks": true,
34
- "noImplicitThis": true,
35
- "strictPropertyInitialization": true,
36
- "strictFunctionTypes": true,
37
- "allowUnreachableCode": false,
38
- "noImplicitOverride": true,
39
- "noPropertyAccessFromIndexSignature": false
40
- }
41
- }
@@ -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;