@ublitzjs/core 1.0.0 → 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.
package/logo.png DELETED
Binary file
package/src/http-codes.ts DELETED
@@ -1,94 +0,0 @@
1
- "use strict";
2
- import { toAB } from "./index.js";
3
- var c405Message = toAB("Method is not allowed");
4
- var allowHeader = toAB("Allow");
5
- var checkHeader = toAB("content-length");
6
- var checkMessage = toAB("Content-Length is required to be > 0 and to be an integer");
7
- import type { HttpResponse as uwsHttpResponse } from "uWebSockets.js";
8
- import type { HttpRequest, HttpMethods } from "./index.ts";
9
- /**
10
- * If something wrong is to content-length, sends 411 code and throws error with a "cause" == { CL : string}, sets res.finished = true
11
- */
12
- export function checkContentLength(
13
- res: uwsHttpResponse,
14
- req: HttpRequest
15
- ): number {
16
- var header = req.getHeader(checkHeader);
17
- var CL: number;
18
- if (!header || !Number.isInteger(CL = Number(header))) {
19
- res.finished = true;
20
- res.cork(() => res.writeStatus(c411).end(checkMessage));
21
- throw new Error("Wrong content-length", { cause: { CL: header} });
22
- }
23
- return CL;
24
- }
25
- /**
26
- * sends http 400 and throws an Error with "causeForYou", sets res.finished = true
27
- */
28
- export function badRequest(
29
- res: uwsHttpResponse,
30
- error: string,
31
- causeForYou: string
32
- ): void {
33
- res.finished = true;
34
- if (!res.aborted) res.cork(() => res.writeStatus(c400).end(toAB(error)));
35
- throw new Error("Bad request", { cause: causeForYou });
36
- }
37
- /**
38
- * sends http 413, but doesn't throw an Error, sets res.finished = true
39
- */
40
- export function tooLargeBody(res: uwsHttpResponse, limit: number): void {
41
- var message = toAB("Body is too large. Limit in bytes - " + limit);
42
- if (!res.aborted) res.cork(() => res.writeStatus(c413).end(message));
43
- res.finished = true;
44
- }
45
- /**
46
- * Constructs function, which sends http 405 and sets http Allow header with all methods you passed.
47
- * It ignores "ws" and replaces "del" on "DELETE"
48
- */
49
- export function seeOtherMethods(
50
- methodsArr: HttpMethods[]
51
- ): (res: uwsHttpResponse, req: any) => any {
52
- if(new Set(methodsArr).size != methodsArr.length) throw new Error("the methods repeat")
53
- var arr: string[] = []
54
- loop: for(var method of methodsArr){
55
- switch(method){
56
- case "ws": continue loop;
57
- case "del": arr.push("DELETE"); break;
58
- default: arr.push(method.toUpperCase())
59
- }
60
- }
61
- var methods = toAB(arr.join(", "));
62
- return (res) =>
63
- res.writeStatus(c405).writeHeader(allowHeader, methods).end(c405Message);
64
- }
65
-
66
- /**
67
- * Constructs the function, which sets 404 http code and sends the message you have specified
68
- */
69
- export function notFoundConstructor(
70
- message: string = "Not found"
71
- ): (res: uwsHttpResponse, req: any) => any {
72
- var mes = toAB(message);
73
- return (res) => res.writeStatus(c404).end(mes, true);
74
- }
75
- /**
76
- * code: required content length
77
- */
78
- export var c411 = toAB("411");
79
- /**
80
- * code: bad request
81
- */
82
- export var c400 = toAB("400");
83
- /**
84
- * code: payload too large
85
- */
86
- export var c413 = toAB("413");
87
- /**
88
- * code: method not allowed
89
- */
90
- export var c405 = toAB("405");
91
- /**
92
- * code: not found
93
- */
94
- export var c404 = toAB("404");