8da3ad696ebc8fa0a8b0fa2c0be077caac88c28e
[squeep-api-dingus] / lib / enum.js
1 'use strict';
2
3 const ContentType = {
4 TextHTML: 'text/html',
5 TextPlain: 'text/plain',
6 ApplicationForm: 'application/x-www-form-urlencoded',
7 ApplicationJson: 'application/json',
8 };
9
10 // Supported encoding types
11 const EncodingType = {
12 Brotli: 'br',
13 Gzip: 'gzip',
14 XGzip: 'x-gzip',
15 Identity: 'identity',
16 };
17
18 // Filename suffixes for serving pre-encoded files
19 const EncodingTypeSuffix = {
20 [EncodingType.Brotli]: '.br',
21 [EncodingType.Gzip]: '.gz',
22 [EncodingType.XGzip]: '.gz',
23 };
24
25 const HTTPStatusCode = {
26
27 Continue: 100,
28 SwitchingProtocols: 101,
29 Processing: 102,
30 EarlyHints: 103,
31
32 OK: 200,
33 Created: 201,
34 Accepted: 202,
35 NonAuthoritativeInformation: 203,
36 NoContent: 204,
37 ResetContent: 205,
38 PartialContent: 206,
39 MultiStatus: 207,
40 AlreadyReported: 208,
41 IMUsed: 226,
42
43 MultipleChoices: 300,
44 MovedPermanently: 301,
45 Found: 302,
46 SeeOther: 303,
47 NotModified: 304,
48 UseProxy: 305, // Deprecated, do not use
49 SwitchProxy: 306, // No longer used
50 TemporaryRedirect: 307,
51 PermanentRedirect: 308,
52
53 BadRequest: 400,
54 Unauthorized: 401,
55 PaymentRequired: 402,
56 Forbidden: 403,
57 NotFound: 404,
58 MethodNotAllowed: 405,
59 NotAcceptable: 406,
60 ProxyAuthenticationRequired: 407,
61 RequestTimeout: 408,
62 Conflict: 409,
63 Gone: 410,
64 LengthRequired: 411,
65 PreconditionFailed: 412,
66 ContentTooLarge: 413,
67 RequestEntityTooLarge: 413, // Alternate name
68 URITooLong: 414,
69 UnsupportedMediaType: 415,
70 RangeNotSatisfiable: 416,
71 ExpectationFailed: 417,
72 ImATeapot: 418,
73 MisdirectedRequest: 421,
74 UnprocessableContent: 422,
75 Locked: 423,
76 FailedDependency: 424,
77 TooEarly: 425,
78 UpgradeRequired: 426,
79 PreconditionRequired: 428,
80 TooManyRequests: 429,
81 RequestHeaderFieldsTooLarge: 431,
82 UnavailableForLegalReasons: 451,
83
84 InternalServerError: 500,
85 NotImplemented: 501,
86 BadGateway: 502,
87 ServiceUnavailable: 503,
88 GatewayTimeout: 504,
89 HTTPVersionNotSupported: 505,
90 VariantAlsoNegotiates: 506,
91 InsufficientStorage: 507,
92 LoopDetected: 508,
93 NotExtended: 510,
94 NetworkAuthenticationRequired: 511,
95
96 };
97
98 const HTTPStatusMessage = {
99
100 100: 'Continue',
101 101: 'Switching Protocols',
102 102: 'Processing',
103 103: 'Early Hints',
104
105 200: 'OK',
106 201: 'Created',
107 202: 'Accepted',
108 203: 'Non-Authoritative Information',
109 204: 'No Content',
110 205: 'Reset Content',
111 206: 'Partial Content',
112 207: 'Multi-Status',
113 208: 'Already Reported',
114 226: 'IM USed',
115
116 300: 'Multiple Choices',
117 301: 'Moved Permanently',
118 302: 'Found',
119 303: 'See Other',
120 304: 'Not Modified',
121 305: 'Use Proxy',
122 306: 'Switch Proxy',
123 307: 'Temporary Redirect',
124 308: 'Permanent Redirect',
125
126 400: 'Bad Request',
127 401: 'Unauthorized',
128 402: 'Payment Required',
129 403: 'Forbidden',
130 '403.zalgo': 'F̦̩̫̼͔̫͓̃ͤ̈̆̀͑o̖̟͙̫̯̗̳̽ͦ̆́ͨr̩͉̰̗͉b̬̂͘į̟̬̓d͂͗҉̟͈̜͙ͅd͎̜̺̝͇͑̒̋̾ë̴̳̺͓̦̘́ͮ̈́ǹ͈̦̫̙',
131 404: 'Not Found',
132 405: 'Method Not Allowed',
133 406: 'Not Acceptable',
134 407: 'Proxy Authentication Required',
135 408: 'Request Timeout',
136 409: 'Conflict',
137 410: 'Gone',
138 411: 'Length Required',
139 412: 'Precondition Failed',
140 413: 'Content Too Large',
141 414: 'URI Too Long',
142 415: 'Unsupported Media Type',
143 416: 'Range Not Satisfiable',
144 417: 'Expectation Failed',
145 418: 'I\'m A Teapot',
146 421: 'Misdirected Request',
147 422: 'Unprocessable Content',
148 423: 'Locked',
149 424: 'Failed Dependency',
150 425: 'Too Early',
151 426: 'Upgrade Required',
152 428: 'Precondition Required',
153 429: 'Too Many Requests',
154 431: 'Request Header Fields Too Large',
155 451: 'Unavailable For Legal Reasons',
156
157 500: 'Internal Server Error',
158 501: 'Not Implemented',
159 502: 'Bad Gateway',
160 503: 'Service Unavailable',
161 504: 'Gateway Timeout',
162 505: 'HTTP Version Not Supported',
163 506: 'Variant Also Negotiates',
164 507: 'Insufficient Storage',
165 508: 'Loop Detected',
166 510: 'Not Extended',
167 511: 'Network Authentication Required',
168
169 };
170
171 function errorResponseGenerator () {
172 return Object.fromEntries(
173 Object.entries(HTTPStatusCode).map(([name, statusCode]) => {
174 const errorMessage = HTTPStatusMessage[statusCode]; // eslint-disable-line security/detect-object-injection
175 // istanbul ignore next
176 if (!errorMessage) {
177 throw new Error(`missing HTTPStatusMessage for ${statusCode} from HTTPStatusCode ${name}`);
178 }
179 return [name, { statusCode, errorMessage }];
180 }));
181 }
182
183 const ErrorResponse = errorResponseGenerator();
184
185 const ErrorResponseProxy = new Proxy(ErrorResponse, {
186 get: (target, property) => {
187 if (property in target) {
188 return target[property]; // eslint-disable-line security/detect-object-injection
189 } else {
190 const statusCode = HTTPStatusCode.InternalServerError;
191 const errorMessage = HTTPStatusMessage[statusCode]; // eslint-disable-line security/detect-object-injection
192 const details = [`undefined error response '${property}'`];
193 return { statusCode, errorMessage, details };
194 }
195 },
196 });
197
198 const Header = {
199 Accept: 'Accept',
200 AcceptEncoding: 'Accept-Encoding',
201 CacheControl: 'Cache-Control',
202 ContentEncoding: 'Content-Encoding',
203 ContentLength: 'Content-Length',
204 ContentType: 'Content-Type',
205 ETag: 'ETag',
206 IfModifiedSince: 'If-Modified-Since',
207 IfNoneMatch: 'If-None-Match',
208 LastModified: 'Last-Modified',
209 Location: 'Location',
210 RequestId: 'Request-ID',
211 Vary: 'Vary',
212 XCorrelationId: 'X-Correlation-ID',
213 XForwardedFor: 'X-Forwarded-For',
214 XForwardedProto: 'X-Forwarded-Proto',
215 XRealIP: 'X-Real-IP',
216 XRequestId: 'X-Request-ID',
217 };
218
219 module.exports = {
220 ContentType,
221 EncodingType,
222 EncodingTypeSuffix,
223 ErrorResponse: ErrorResponseProxy,
224 Header,
225 HTTPStatusCode,
226 HTTPStatusMessage,
227 };