yinzerflow 0.1.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/LICENSE +21 -0
- package/README.md +340 -0
- package/index.d.ts +244 -0
- package/index.js +1002 -0
- package/index.js.map +16 -0
- package/package.json +63 -0
package/index.js
ADDED
|
@@ -0,0 +1,1002 @@
|
|
|
1
|
+
import {createRequire} from "node:module";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
+
for (let key of __getOwnPropNames(mod))
|
|
11
|
+
if (!__hasOwnProp.call(to, key))
|
|
12
|
+
__defProp(to, key, {
|
|
13
|
+
get: () => mod[key],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
19
|
+
var __require = createRequire(import.meta.url);
|
|
20
|
+
|
|
21
|
+
// node_modules/ip/lib/ip.js
|
|
22
|
+
var require_ip = __commonJS((exports) => {
|
|
23
|
+
var _normalizeFamily = function(family) {
|
|
24
|
+
if (family === 4) {
|
|
25
|
+
return "ipv4";
|
|
26
|
+
}
|
|
27
|
+
if (family === 6) {
|
|
28
|
+
return "ipv6";
|
|
29
|
+
}
|
|
30
|
+
return family ? family.toLowerCase() : "ipv4";
|
|
31
|
+
};
|
|
32
|
+
var ip = exports;
|
|
33
|
+
var { Buffer: Buffer2 } = __require("buffer");
|
|
34
|
+
var os = __require("os");
|
|
35
|
+
ip.toBuffer = function(ip2, buff, offset) {
|
|
36
|
+
offset = ~~offset;
|
|
37
|
+
let result;
|
|
38
|
+
if (this.isV4Format(ip2)) {
|
|
39
|
+
result = buff || Buffer2.alloc(offset + 4);
|
|
40
|
+
ip2.split(/\./g).map((byte) => {
|
|
41
|
+
result[offset++] = parseInt(byte, 10) & 255;
|
|
42
|
+
});
|
|
43
|
+
} else if (this.isV6Format(ip2)) {
|
|
44
|
+
const sections = ip2.split(":", 8);
|
|
45
|
+
let i;
|
|
46
|
+
for (i = 0;i < sections.length; i++) {
|
|
47
|
+
const isv4 = this.isV4Format(sections[i]);
|
|
48
|
+
let v4Buffer;
|
|
49
|
+
if (isv4) {
|
|
50
|
+
v4Buffer = this.toBuffer(sections[i]);
|
|
51
|
+
sections[i] = v4Buffer.slice(0, 2).toString("hex");
|
|
52
|
+
}
|
|
53
|
+
if (v4Buffer && ++i < 8) {
|
|
54
|
+
sections.splice(i, 0, v4Buffer.slice(2, 4).toString("hex"));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (sections[0] === "") {
|
|
58
|
+
while (sections.length < 8)
|
|
59
|
+
sections.unshift("0");
|
|
60
|
+
} else if (sections[sections.length - 1] === "") {
|
|
61
|
+
while (sections.length < 8)
|
|
62
|
+
sections.push("0");
|
|
63
|
+
} else if (sections.length < 8) {
|
|
64
|
+
for (i = 0;i < sections.length && sections[i] !== ""; i++)
|
|
65
|
+
;
|
|
66
|
+
const argv = [i, 1];
|
|
67
|
+
for (i = 9 - sections.length;i > 0; i--) {
|
|
68
|
+
argv.push("0");
|
|
69
|
+
}
|
|
70
|
+
sections.splice(...argv);
|
|
71
|
+
}
|
|
72
|
+
result = buff || Buffer2.alloc(offset + 16);
|
|
73
|
+
for (i = 0;i < sections.length; i++) {
|
|
74
|
+
const word = parseInt(sections[i], 16);
|
|
75
|
+
result[offset++] = word >> 8 & 255;
|
|
76
|
+
result[offset++] = word & 255;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (!result) {
|
|
80
|
+
throw Error(`Invalid ip address: ${ip2}`);
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
};
|
|
84
|
+
ip.toString = function(buff, offset, length) {
|
|
85
|
+
offset = ~~offset;
|
|
86
|
+
length = length || buff.length - offset;
|
|
87
|
+
let result = [];
|
|
88
|
+
if (length === 4) {
|
|
89
|
+
for (let i = 0;i < length; i++) {
|
|
90
|
+
result.push(buff[offset + i]);
|
|
91
|
+
}
|
|
92
|
+
result = result.join(".");
|
|
93
|
+
} else if (length === 16) {
|
|
94
|
+
for (let i = 0;i < length; i += 2) {
|
|
95
|
+
result.push(buff.readUInt16BE(offset + i).toString(16));
|
|
96
|
+
}
|
|
97
|
+
result = result.join(":");
|
|
98
|
+
result = result.replace(/(^|:)0(:0)*:0(:|$)/, "$1::$3");
|
|
99
|
+
result = result.replace(/:{3,4}/, "::");
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
};
|
|
103
|
+
var ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/;
|
|
104
|
+
var ipv6Regex = /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;
|
|
105
|
+
ip.isV4Format = function(ip2) {
|
|
106
|
+
return ipv4Regex.test(ip2);
|
|
107
|
+
};
|
|
108
|
+
ip.isV6Format = function(ip2) {
|
|
109
|
+
return ipv6Regex.test(ip2);
|
|
110
|
+
};
|
|
111
|
+
ip.fromPrefixLen = function(prefixlen, family) {
|
|
112
|
+
if (prefixlen > 32) {
|
|
113
|
+
family = "ipv6";
|
|
114
|
+
} else {
|
|
115
|
+
family = _normalizeFamily(family);
|
|
116
|
+
}
|
|
117
|
+
let len = 4;
|
|
118
|
+
if (family === "ipv6") {
|
|
119
|
+
len = 16;
|
|
120
|
+
}
|
|
121
|
+
const buff = Buffer2.alloc(len);
|
|
122
|
+
for (let i = 0, n = buff.length;i < n; ++i) {
|
|
123
|
+
let bits = 8;
|
|
124
|
+
if (prefixlen < 8) {
|
|
125
|
+
bits = prefixlen;
|
|
126
|
+
}
|
|
127
|
+
prefixlen -= bits;
|
|
128
|
+
buff[i] = ~(255 >> bits) & 255;
|
|
129
|
+
}
|
|
130
|
+
return ip.toString(buff);
|
|
131
|
+
};
|
|
132
|
+
ip.mask = function(addr, mask) {
|
|
133
|
+
addr = ip.toBuffer(addr);
|
|
134
|
+
mask = ip.toBuffer(mask);
|
|
135
|
+
const result = Buffer2.alloc(Math.max(addr.length, mask.length));
|
|
136
|
+
let i;
|
|
137
|
+
if (addr.length === mask.length) {
|
|
138
|
+
for (i = 0;i < addr.length; i++) {
|
|
139
|
+
result[i] = addr[i] & mask[i];
|
|
140
|
+
}
|
|
141
|
+
} else if (mask.length === 4) {
|
|
142
|
+
for (i = 0;i < mask.length; i++) {
|
|
143
|
+
result[i] = addr[addr.length - 4 + i] & mask[i];
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
for (i = 0;i < result.length - 6; i++) {
|
|
147
|
+
result[i] = 0;
|
|
148
|
+
}
|
|
149
|
+
result[10] = 255;
|
|
150
|
+
result[11] = 255;
|
|
151
|
+
for (i = 0;i < addr.length; i++) {
|
|
152
|
+
result[i + 12] = addr[i] & mask[i + 12];
|
|
153
|
+
}
|
|
154
|
+
i += 12;
|
|
155
|
+
}
|
|
156
|
+
for (;i < result.length; i++) {
|
|
157
|
+
result[i] = 0;
|
|
158
|
+
}
|
|
159
|
+
return ip.toString(result);
|
|
160
|
+
};
|
|
161
|
+
ip.cidr = function(cidrString) {
|
|
162
|
+
const cidrParts = cidrString.split("/");
|
|
163
|
+
const addr = cidrParts[0];
|
|
164
|
+
if (cidrParts.length !== 2) {
|
|
165
|
+
throw new Error(`invalid CIDR subnet: ${addr}`);
|
|
166
|
+
}
|
|
167
|
+
const mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
|
|
168
|
+
return ip.mask(addr, mask);
|
|
169
|
+
};
|
|
170
|
+
ip.subnet = function(addr, mask) {
|
|
171
|
+
const networkAddress = ip.toLong(ip.mask(addr, mask));
|
|
172
|
+
const maskBuffer = ip.toBuffer(mask);
|
|
173
|
+
let maskLength = 0;
|
|
174
|
+
for (let i = 0;i < maskBuffer.length; i++) {
|
|
175
|
+
if (maskBuffer[i] === 255) {
|
|
176
|
+
maskLength += 8;
|
|
177
|
+
} else {
|
|
178
|
+
let octet = maskBuffer[i] & 255;
|
|
179
|
+
while (octet) {
|
|
180
|
+
octet = octet << 1 & 255;
|
|
181
|
+
maskLength++;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const numberOfAddresses = 2 ** (32 - maskLength);
|
|
186
|
+
return {
|
|
187
|
+
networkAddress: ip.fromLong(networkAddress),
|
|
188
|
+
firstAddress: numberOfAddresses <= 2 ? ip.fromLong(networkAddress) : ip.fromLong(networkAddress + 1),
|
|
189
|
+
lastAddress: numberOfAddresses <= 2 ? ip.fromLong(networkAddress + numberOfAddresses - 1) : ip.fromLong(networkAddress + numberOfAddresses - 2),
|
|
190
|
+
broadcastAddress: ip.fromLong(networkAddress + numberOfAddresses - 1),
|
|
191
|
+
subnetMask: mask,
|
|
192
|
+
subnetMaskLength: maskLength,
|
|
193
|
+
numHosts: numberOfAddresses <= 2 ? numberOfAddresses : numberOfAddresses - 2,
|
|
194
|
+
length: numberOfAddresses,
|
|
195
|
+
contains(other) {
|
|
196
|
+
return networkAddress === ip.toLong(ip.mask(other, mask));
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
ip.cidrSubnet = function(cidrString) {
|
|
201
|
+
const cidrParts = cidrString.split("/");
|
|
202
|
+
const addr = cidrParts[0];
|
|
203
|
+
if (cidrParts.length !== 2) {
|
|
204
|
+
throw new Error(`invalid CIDR subnet: ${addr}`);
|
|
205
|
+
}
|
|
206
|
+
const mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
|
|
207
|
+
return ip.subnet(addr, mask);
|
|
208
|
+
};
|
|
209
|
+
ip.not = function(addr) {
|
|
210
|
+
const buff = ip.toBuffer(addr);
|
|
211
|
+
for (let i = 0;i < buff.length; i++) {
|
|
212
|
+
buff[i] = 255 ^ buff[i];
|
|
213
|
+
}
|
|
214
|
+
return ip.toString(buff);
|
|
215
|
+
};
|
|
216
|
+
ip.or = function(a, b) {
|
|
217
|
+
a = ip.toBuffer(a);
|
|
218
|
+
b = ip.toBuffer(b);
|
|
219
|
+
if (a.length === b.length) {
|
|
220
|
+
for (let i = 0;i < a.length; ++i) {
|
|
221
|
+
a[i] |= b[i];
|
|
222
|
+
}
|
|
223
|
+
return ip.toString(a);
|
|
224
|
+
}
|
|
225
|
+
let buff = a;
|
|
226
|
+
let other = b;
|
|
227
|
+
if (b.length > a.length) {
|
|
228
|
+
buff = b;
|
|
229
|
+
other = a;
|
|
230
|
+
}
|
|
231
|
+
const offset = buff.length - other.length;
|
|
232
|
+
for (let i = offset;i < buff.length; ++i) {
|
|
233
|
+
buff[i] |= other[i - offset];
|
|
234
|
+
}
|
|
235
|
+
return ip.toString(buff);
|
|
236
|
+
};
|
|
237
|
+
ip.isEqual = function(a, b) {
|
|
238
|
+
a = ip.toBuffer(a);
|
|
239
|
+
b = ip.toBuffer(b);
|
|
240
|
+
if (a.length === b.length) {
|
|
241
|
+
for (let i = 0;i < a.length; i++) {
|
|
242
|
+
if (a[i] !== b[i])
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
if (b.length === 4) {
|
|
248
|
+
const t = b;
|
|
249
|
+
b = a;
|
|
250
|
+
a = t;
|
|
251
|
+
}
|
|
252
|
+
for (let i = 0;i < 10; i++) {
|
|
253
|
+
if (b[i] !== 0)
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
const word = b.readUInt16BE(10);
|
|
257
|
+
if (word !== 0 && word !== 65535)
|
|
258
|
+
return false;
|
|
259
|
+
for (let i = 0;i < 4; i++) {
|
|
260
|
+
if (a[i] !== b[i + 12])
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
};
|
|
265
|
+
ip.isPrivate = function(addr) {
|
|
266
|
+
if (ip.isLoopback(addr)) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
if (!ip.isV6Format(addr)) {
|
|
270
|
+
const ipl = ip.normalizeToLong(addr);
|
|
271
|
+
if (ipl < 0) {
|
|
272
|
+
throw new Error("invalid ipv4 address");
|
|
273
|
+
}
|
|
274
|
+
addr = ip.fromLong(ipl);
|
|
275
|
+
}
|
|
276
|
+
return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || /^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || /^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || /^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || /^f[cd][0-9a-f]{2}:/i.test(addr) || /^fe80:/i.test(addr) || /^::1$/.test(addr) || /^::$/.test(addr);
|
|
277
|
+
};
|
|
278
|
+
ip.isPublic = function(addr) {
|
|
279
|
+
return !ip.isPrivate(addr);
|
|
280
|
+
};
|
|
281
|
+
ip.isLoopback = function(addr) {
|
|
282
|
+
if (!/\./.test(addr) && !/:/.test(addr)) {
|
|
283
|
+
addr = ip.fromLong(Number(addr));
|
|
284
|
+
}
|
|
285
|
+
return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || /^0177\./.test(addr) || /^0x7f\./i.test(addr) || /^fe80::1$/i.test(addr) || /^::1$/.test(addr) || /^::$/.test(addr);
|
|
286
|
+
};
|
|
287
|
+
ip.loopback = function(family) {
|
|
288
|
+
family = _normalizeFamily(family);
|
|
289
|
+
if (family !== "ipv4" && family !== "ipv6") {
|
|
290
|
+
throw new Error("family must be ipv4 or ipv6");
|
|
291
|
+
}
|
|
292
|
+
return family === "ipv4" ? "127.0.0.1" : "fe80::1";
|
|
293
|
+
};
|
|
294
|
+
ip.address = function(name, family) {
|
|
295
|
+
const interfaces = os.networkInterfaces();
|
|
296
|
+
family = _normalizeFamily(family);
|
|
297
|
+
if (name && name !== "private" && name !== "public") {
|
|
298
|
+
const res = interfaces[name].filter((details) => {
|
|
299
|
+
const itemFamily = _normalizeFamily(details.family);
|
|
300
|
+
return itemFamily === family;
|
|
301
|
+
});
|
|
302
|
+
if (res.length === 0) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
return res[0].address;
|
|
306
|
+
}
|
|
307
|
+
const all = Object.keys(interfaces).map((nic) => {
|
|
308
|
+
const addresses = interfaces[nic].filter((details) => {
|
|
309
|
+
details.family = _normalizeFamily(details.family);
|
|
310
|
+
if (details.family !== family || ip.isLoopback(details.address)) {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
if (!name) {
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
return name === "public" ? ip.isPrivate(details.address) : ip.isPublic(details.address);
|
|
317
|
+
});
|
|
318
|
+
return addresses.length ? addresses[0].address : undefined;
|
|
319
|
+
}).filter(Boolean);
|
|
320
|
+
return !all.length ? ip.loopback(family) : all[0];
|
|
321
|
+
};
|
|
322
|
+
ip.toLong = function(ip2) {
|
|
323
|
+
let ipl = 0;
|
|
324
|
+
ip2.split(".").forEach((octet) => {
|
|
325
|
+
ipl <<= 8;
|
|
326
|
+
ipl += parseInt(octet);
|
|
327
|
+
});
|
|
328
|
+
return ipl >>> 0;
|
|
329
|
+
};
|
|
330
|
+
ip.fromLong = function(ipl) {
|
|
331
|
+
return `${ipl >>> 24}.${ipl >> 16 & 255}.${ipl >> 8 & 255}.${ipl & 255}`;
|
|
332
|
+
};
|
|
333
|
+
ip.normalizeToLong = function(addr) {
|
|
334
|
+
const parts = addr.split(".").map((part) => {
|
|
335
|
+
if (part.startsWith("0x") || part.startsWith("0X")) {
|
|
336
|
+
return parseInt(part, 16);
|
|
337
|
+
} else if (part.startsWith("0") && part !== "0" && /^[0-7]+$/.test(part)) {
|
|
338
|
+
return parseInt(part, 8);
|
|
339
|
+
} else if (/^[1-9]\d*$/.test(part) || part === "0") {
|
|
340
|
+
return parseInt(part, 10);
|
|
341
|
+
} else {
|
|
342
|
+
return NaN;
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
if (parts.some(isNaN))
|
|
346
|
+
return -1;
|
|
347
|
+
let val = 0;
|
|
348
|
+
const n = parts.length;
|
|
349
|
+
switch (n) {
|
|
350
|
+
case 1:
|
|
351
|
+
val = parts[0];
|
|
352
|
+
break;
|
|
353
|
+
case 2:
|
|
354
|
+
if (parts[0] > 255 || parts[1] > 16777215)
|
|
355
|
+
return -1;
|
|
356
|
+
val = parts[0] << 24 | parts[1] & 16777215;
|
|
357
|
+
break;
|
|
358
|
+
case 3:
|
|
359
|
+
if (parts[0] > 255 || parts[1] > 255 || parts[2] > 65535)
|
|
360
|
+
return -1;
|
|
361
|
+
val = parts[0] << 24 | parts[1] << 16 | parts[2] & 65535;
|
|
362
|
+
break;
|
|
363
|
+
case 4:
|
|
364
|
+
if (parts.some((part) => part > 255))
|
|
365
|
+
return -1;
|
|
366
|
+
val = parts[0] << 24 | parts[1] << 16 | parts[2] << 8 | parts[3];
|
|
367
|
+
break;
|
|
368
|
+
default:
|
|
369
|
+
return -1;
|
|
370
|
+
}
|
|
371
|
+
return val >>> 0;
|
|
372
|
+
};
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// node_modules/dayjs/dayjs.min.js
|
|
376
|
+
var require_dayjs_min = __commonJS((exports, module) => {
|
|
377
|
+
(function(t, e) {
|
|
378
|
+
typeof exports == "object" && typeof module != "undefined" ? module.exports = e() : typeof define == "function" && define.amd ? define(e) : (t = typeof globalThis != "undefined" ? globalThis : t || self).dayjs = e();
|
|
379
|
+
})(exports, function() {
|
|
380
|
+
var t = 1000, e = 60000, n = 3600000, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = { name: "en", weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), ordinal: function(t2) {
|
|
381
|
+
var e2 = ["th", "st", "nd", "rd"], n2 = t2 % 100;
|
|
382
|
+
return "[" + t2 + (e2[(n2 - 20) % 10] || e2[n2] || e2[0]) + "]";
|
|
383
|
+
} }, m = function(t2, e2, n2) {
|
|
384
|
+
var r2 = String(t2);
|
|
385
|
+
return !r2 || r2.length >= e2 ? t2 : "" + Array(e2 + 1 - r2.length).join(n2) + t2;
|
|
386
|
+
}, v = { s: m, z: function(t2) {
|
|
387
|
+
var e2 = -t2.utcOffset(), n2 = Math.abs(e2), r2 = Math.floor(n2 / 60), i2 = n2 % 60;
|
|
388
|
+
return (e2 <= 0 ? "+" : "-") + m(r2, 2, "0") + ":" + m(i2, 2, "0");
|
|
389
|
+
}, m: function t(e2, n2) {
|
|
390
|
+
if (e2.date() < n2.date())
|
|
391
|
+
return -t(n2, e2);
|
|
392
|
+
var r2 = 12 * (n2.year() - e2.year()) + (n2.month() - e2.month()), i2 = e2.clone().add(r2, c), s2 = n2 - i2 < 0, u2 = e2.clone().add(r2 + (s2 ? -1 : 1), c);
|
|
393
|
+
return +(-(r2 + (n2 - i2) / (s2 ? i2 - u2 : u2 - i2)) || 0);
|
|
394
|
+
}, a: function(t2) {
|
|
395
|
+
return t2 < 0 ? Math.ceil(t2) || 0 : Math.floor(t2);
|
|
396
|
+
}, p: function(t2) {
|
|
397
|
+
return { M: c, y: h, w: o, d: a, D: d, h: u, m: s, s: i, ms: r, Q: f }[t2] || String(t2 || "").toLowerCase().replace(/s$/, "");
|
|
398
|
+
}, u: function(t2) {
|
|
399
|
+
return t2 === undefined;
|
|
400
|
+
} }, g = "en", D = {};
|
|
401
|
+
D[g] = M;
|
|
402
|
+
var p = "$isDayjsObject", S = function(t2) {
|
|
403
|
+
return t2 instanceof _ || !(!t2 || !t2[p]);
|
|
404
|
+
}, w = function t(e2, n2, r2) {
|
|
405
|
+
var i2;
|
|
406
|
+
if (!e2)
|
|
407
|
+
return g;
|
|
408
|
+
if (typeof e2 == "string") {
|
|
409
|
+
var s2 = e2.toLowerCase();
|
|
410
|
+
D[s2] && (i2 = s2), n2 && (D[s2] = n2, i2 = s2);
|
|
411
|
+
var u2 = e2.split("-");
|
|
412
|
+
if (!i2 && u2.length > 1)
|
|
413
|
+
return t(u2[0]);
|
|
414
|
+
} else {
|
|
415
|
+
var a2 = e2.name;
|
|
416
|
+
D[a2] = e2, i2 = a2;
|
|
417
|
+
}
|
|
418
|
+
return !r2 && i2 && (g = i2), i2 || !r2 && g;
|
|
419
|
+
}, O = function(t2, e2) {
|
|
420
|
+
if (S(t2))
|
|
421
|
+
return t2.clone();
|
|
422
|
+
var n2 = typeof e2 == "object" ? e2 : {};
|
|
423
|
+
return n2.date = t2, n2.args = arguments, new _(n2);
|
|
424
|
+
}, b = v;
|
|
425
|
+
b.l = w, b.i = S, b.w = function(t2, e2) {
|
|
426
|
+
return O(t2, { locale: e2.$L, utc: e2.$u, x: e2.$x, $offset: e2.$offset });
|
|
427
|
+
};
|
|
428
|
+
var _ = function() {
|
|
429
|
+
function M2(t2) {
|
|
430
|
+
this.$L = w(t2.locale, null, true), this.parse(t2), this.$x = this.$x || t2.x || {}, this[p] = true;
|
|
431
|
+
}
|
|
432
|
+
var m2 = M2.prototype;
|
|
433
|
+
return m2.parse = function(t2) {
|
|
434
|
+
this.$d = function(t3) {
|
|
435
|
+
var { date: e2, utc: n2 } = t3;
|
|
436
|
+
if (e2 === null)
|
|
437
|
+
return new Date(NaN);
|
|
438
|
+
if (b.u(e2))
|
|
439
|
+
return new Date;
|
|
440
|
+
if (e2 instanceof Date)
|
|
441
|
+
return new Date(e2);
|
|
442
|
+
if (typeof e2 == "string" && !/Z$/i.test(e2)) {
|
|
443
|
+
var r2 = e2.match($);
|
|
444
|
+
if (r2) {
|
|
445
|
+
var i2 = r2[2] - 1 || 0, s2 = (r2[7] || "0").substring(0, 3);
|
|
446
|
+
return n2 ? new Date(Date.UTC(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2)) : new Date(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return new Date(e2);
|
|
450
|
+
}(t2), this.init();
|
|
451
|
+
}, m2.init = function() {
|
|
452
|
+
var t2 = this.$d;
|
|
453
|
+
this.$y = t2.getFullYear(), this.$M = t2.getMonth(), this.$D = t2.getDate(), this.$W = t2.getDay(), this.$H = t2.getHours(), this.$m = t2.getMinutes(), this.$s = t2.getSeconds(), this.$ms = t2.getMilliseconds();
|
|
454
|
+
}, m2.$utils = function() {
|
|
455
|
+
return b;
|
|
456
|
+
}, m2.isValid = function() {
|
|
457
|
+
return !(this.$d.toString() === l);
|
|
458
|
+
}, m2.isSame = function(t2, e2) {
|
|
459
|
+
var n2 = O(t2);
|
|
460
|
+
return this.startOf(e2) <= n2 && n2 <= this.endOf(e2);
|
|
461
|
+
}, m2.isAfter = function(t2, e2) {
|
|
462
|
+
return O(t2) < this.startOf(e2);
|
|
463
|
+
}, m2.isBefore = function(t2, e2) {
|
|
464
|
+
return this.endOf(e2) < O(t2);
|
|
465
|
+
}, m2.$g = function(t2, e2, n2) {
|
|
466
|
+
return b.u(t2) ? this[e2] : this.set(n2, t2);
|
|
467
|
+
}, m2.unix = function() {
|
|
468
|
+
return Math.floor(this.valueOf() / 1000);
|
|
469
|
+
}, m2.valueOf = function() {
|
|
470
|
+
return this.$d.getTime();
|
|
471
|
+
}, m2.startOf = function(t2, e2) {
|
|
472
|
+
var n2 = this, r2 = !!b.u(e2) || e2, f2 = b.p(t2), l2 = function(t3, e3) {
|
|
473
|
+
var i2 = b.w(n2.$u ? Date.UTC(n2.$y, e3, t3) : new Date(n2.$y, e3, t3), n2);
|
|
474
|
+
return r2 ? i2 : i2.endOf(a);
|
|
475
|
+
}, $2 = function(t3, e3) {
|
|
476
|
+
return b.w(n2.toDate()[t3].apply(n2.toDate("s"), (r2 ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(e3)), n2);
|
|
477
|
+
}, y2 = this.$W, M3 = this.$M, m3 = this.$D, v2 = "set" + (this.$u ? "UTC" : "");
|
|
478
|
+
switch (f2) {
|
|
479
|
+
case h:
|
|
480
|
+
return r2 ? l2(1, 0) : l2(31, 11);
|
|
481
|
+
case c:
|
|
482
|
+
return r2 ? l2(1, M3) : l2(0, M3 + 1);
|
|
483
|
+
case o:
|
|
484
|
+
var g2 = this.$locale().weekStart || 0, D2 = (y2 < g2 ? y2 + 7 : y2) - g2;
|
|
485
|
+
return l2(r2 ? m3 - D2 : m3 + (6 - D2), M3);
|
|
486
|
+
case a:
|
|
487
|
+
case d:
|
|
488
|
+
return $2(v2 + "Hours", 0);
|
|
489
|
+
case u:
|
|
490
|
+
return $2(v2 + "Minutes", 1);
|
|
491
|
+
case s:
|
|
492
|
+
return $2(v2 + "Seconds", 2);
|
|
493
|
+
case i:
|
|
494
|
+
return $2(v2 + "Milliseconds", 3);
|
|
495
|
+
default:
|
|
496
|
+
return this.clone();
|
|
497
|
+
}
|
|
498
|
+
}, m2.endOf = function(t2) {
|
|
499
|
+
return this.startOf(t2, false);
|
|
500
|
+
}, m2.$set = function(t2, e2) {
|
|
501
|
+
var n2, o2 = b.p(t2), f2 = "set" + (this.$u ? "UTC" : ""), l2 = (n2 = {}, n2[a] = f2 + "Date", n2[d] = f2 + "Date", n2[c] = f2 + "Month", n2[h] = f2 + "FullYear", n2[u] = f2 + "Hours", n2[s] = f2 + "Minutes", n2[i] = f2 + "Seconds", n2[r] = f2 + "Milliseconds", n2)[o2], $2 = o2 === a ? this.$D + (e2 - this.$W) : e2;
|
|
502
|
+
if (o2 === c || o2 === h) {
|
|
503
|
+
var y2 = this.clone().set(d, 1);
|
|
504
|
+
y2.$d[l2]($2), y2.init(), this.$d = y2.set(d, Math.min(this.$D, y2.daysInMonth())).$d;
|
|
505
|
+
} else
|
|
506
|
+
l2 && this.$d[l2]($2);
|
|
507
|
+
return this.init(), this;
|
|
508
|
+
}, m2.set = function(t2, e2) {
|
|
509
|
+
return this.clone().$set(t2, e2);
|
|
510
|
+
}, m2.get = function(t2) {
|
|
511
|
+
return this[b.p(t2)]();
|
|
512
|
+
}, m2.add = function(r2, f2) {
|
|
513
|
+
var d2, l2 = this;
|
|
514
|
+
r2 = Number(r2);
|
|
515
|
+
var $2 = b.p(f2), y2 = function(t2) {
|
|
516
|
+
var e2 = O(l2);
|
|
517
|
+
return b.w(e2.date(e2.date() + Math.round(t2 * r2)), l2);
|
|
518
|
+
};
|
|
519
|
+
if ($2 === c)
|
|
520
|
+
return this.set(c, this.$M + r2);
|
|
521
|
+
if ($2 === h)
|
|
522
|
+
return this.set(h, this.$y + r2);
|
|
523
|
+
if ($2 === a)
|
|
524
|
+
return y2(1);
|
|
525
|
+
if ($2 === o)
|
|
526
|
+
return y2(7);
|
|
527
|
+
var M3 = (d2 = {}, d2[s] = e, d2[u] = n, d2[i] = t, d2)[$2] || 1, m3 = this.$d.getTime() + r2 * M3;
|
|
528
|
+
return b.w(m3, this);
|
|
529
|
+
}, m2.subtract = function(t2, e2) {
|
|
530
|
+
return this.add(-1 * t2, e2);
|
|
531
|
+
}, m2.format = function(t2) {
|
|
532
|
+
var e2 = this, n2 = this.$locale();
|
|
533
|
+
if (!this.isValid())
|
|
534
|
+
return n2.invalidDate || l;
|
|
535
|
+
var r2 = t2 || "YYYY-MM-DDTHH:mm:ssZ", i2 = b.z(this), s2 = this.$H, u2 = this.$m, a2 = this.$M, o2 = n2.weekdays, c2 = n2.months, f2 = n2.meridiem, h2 = function(t3, n3, i3, s3) {
|
|
536
|
+
return t3 && (t3[n3] || t3(e2, r2)) || i3[n3].slice(0, s3);
|
|
537
|
+
}, d2 = function(t3) {
|
|
538
|
+
return b.s(s2 % 12 || 12, t3, "0");
|
|
539
|
+
}, $2 = f2 || function(t3, e3, n3) {
|
|
540
|
+
var r3 = t3 < 12 ? "AM" : "PM";
|
|
541
|
+
return n3 ? r3.toLowerCase() : r3;
|
|
542
|
+
};
|
|
543
|
+
return r2.replace(y, function(t3, r3) {
|
|
544
|
+
return r3 || function(t4) {
|
|
545
|
+
switch (t4) {
|
|
546
|
+
case "YY":
|
|
547
|
+
return String(e2.$y).slice(-2);
|
|
548
|
+
case "YYYY":
|
|
549
|
+
return b.s(e2.$y, 4, "0");
|
|
550
|
+
case "M":
|
|
551
|
+
return a2 + 1;
|
|
552
|
+
case "MM":
|
|
553
|
+
return b.s(a2 + 1, 2, "0");
|
|
554
|
+
case "MMM":
|
|
555
|
+
return h2(n2.monthsShort, a2, c2, 3);
|
|
556
|
+
case "MMMM":
|
|
557
|
+
return h2(c2, a2);
|
|
558
|
+
case "D":
|
|
559
|
+
return e2.$D;
|
|
560
|
+
case "DD":
|
|
561
|
+
return b.s(e2.$D, 2, "0");
|
|
562
|
+
case "d":
|
|
563
|
+
return String(e2.$W);
|
|
564
|
+
case "dd":
|
|
565
|
+
return h2(n2.weekdaysMin, e2.$W, o2, 2);
|
|
566
|
+
case "ddd":
|
|
567
|
+
return h2(n2.weekdaysShort, e2.$W, o2, 3);
|
|
568
|
+
case "dddd":
|
|
569
|
+
return o2[e2.$W];
|
|
570
|
+
case "H":
|
|
571
|
+
return String(s2);
|
|
572
|
+
case "HH":
|
|
573
|
+
return b.s(s2, 2, "0");
|
|
574
|
+
case "h":
|
|
575
|
+
return d2(1);
|
|
576
|
+
case "hh":
|
|
577
|
+
return d2(2);
|
|
578
|
+
case "a":
|
|
579
|
+
return $2(s2, u2, true);
|
|
580
|
+
case "A":
|
|
581
|
+
return $2(s2, u2, false);
|
|
582
|
+
case "m":
|
|
583
|
+
return String(u2);
|
|
584
|
+
case "mm":
|
|
585
|
+
return b.s(u2, 2, "0");
|
|
586
|
+
case "s":
|
|
587
|
+
return String(e2.$s);
|
|
588
|
+
case "ss":
|
|
589
|
+
return b.s(e2.$s, 2, "0");
|
|
590
|
+
case "SSS":
|
|
591
|
+
return b.s(e2.$ms, 3, "0");
|
|
592
|
+
case "Z":
|
|
593
|
+
return i2;
|
|
594
|
+
}
|
|
595
|
+
return null;
|
|
596
|
+
}(t3) || i2.replace(":", "");
|
|
597
|
+
});
|
|
598
|
+
}, m2.utcOffset = function() {
|
|
599
|
+
return 15 * -Math.round(this.$d.getTimezoneOffset() / 15);
|
|
600
|
+
}, m2.diff = function(r2, d2, l2) {
|
|
601
|
+
var $2, y2 = this, M3 = b.p(d2), m3 = O(r2), v2 = (m3.utcOffset() - this.utcOffset()) * e, g2 = this - m3, D2 = function() {
|
|
602
|
+
return b.m(y2, m3);
|
|
603
|
+
};
|
|
604
|
+
switch (M3) {
|
|
605
|
+
case h:
|
|
606
|
+
$2 = D2() / 12;
|
|
607
|
+
break;
|
|
608
|
+
case c:
|
|
609
|
+
$2 = D2();
|
|
610
|
+
break;
|
|
611
|
+
case f:
|
|
612
|
+
$2 = D2() / 3;
|
|
613
|
+
break;
|
|
614
|
+
case o:
|
|
615
|
+
$2 = (g2 - v2) / 604800000;
|
|
616
|
+
break;
|
|
617
|
+
case a:
|
|
618
|
+
$2 = (g2 - v2) / 86400000;
|
|
619
|
+
break;
|
|
620
|
+
case u:
|
|
621
|
+
$2 = g2 / n;
|
|
622
|
+
break;
|
|
623
|
+
case s:
|
|
624
|
+
$2 = g2 / e;
|
|
625
|
+
break;
|
|
626
|
+
case i:
|
|
627
|
+
$2 = g2 / t;
|
|
628
|
+
break;
|
|
629
|
+
default:
|
|
630
|
+
$2 = g2;
|
|
631
|
+
}
|
|
632
|
+
return l2 ? $2 : b.a($2);
|
|
633
|
+
}, m2.daysInMonth = function() {
|
|
634
|
+
return this.endOf(c).$D;
|
|
635
|
+
}, m2.$locale = function() {
|
|
636
|
+
return D[this.$L];
|
|
637
|
+
}, m2.locale = function(t2, e2) {
|
|
638
|
+
if (!t2)
|
|
639
|
+
return this.$L;
|
|
640
|
+
var n2 = this.clone(), r2 = w(t2, e2, true);
|
|
641
|
+
return r2 && (n2.$L = r2), n2;
|
|
642
|
+
}, m2.clone = function() {
|
|
643
|
+
return b.w(this.$d, this);
|
|
644
|
+
}, m2.toDate = function() {
|
|
645
|
+
return new Date(this.valueOf());
|
|
646
|
+
}, m2.toJSON = function() {
|
|
647
|
+
return this.isValid() ? this.toISOString() : null;
|
|
648
|
+
}, m2.toISOString = function() {
|
|
649
|
+
return this.$d.toISOString();
|
|
650
|
+
}, m2.toString = function() {
|
|
651
|
+
return this.$d.toUTCString();
|
|
652
|
+
}, M2;
|
|
653
|
+
}(), k = _.prototype;
|
|
654
|
+
return O.prototype = k, [["$ms", r], ["$s", i], ["$m", s], ["$H", u], ["$W", a], ["$M", c], ["$y", h], ["$D", d]].forEach(function(t2) {
|
|
655
|
+
k[t2[1]] = function(e2) {
|
|
656
|
+
return this.$g(e2, t2[0], t2[1]);
|
|
657
|
+
};
|
|
658
|
+
}), O.extend = function(t2, e2) {
|
|
659
|
+
return t2.$i || (t2(e2, _, O), t2.$i = true), O;
|
|
660
|
+
}, O.locale = w, O.isDayjs = S, O.unix = function(t2) {
|
|
661
|
+
return O(1000 * t2);
|
|
662
|
+
}, O.en = D[g], O.Ls = D, O.p = {}, O;
|
|
663
|
+
});
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
// app/index.ts
|
|
667
|
+
var import_ip = __toESM(require_ip(), 1);
|
|
668
|
+
import {createServer} from "net";
|
|
669
|
+
|
|
670
|
+
// app/HttpRequest.ts
|
|
671
|
+
var HttpMethod = {
|
|
672
|
+
DELETE: "DELETE",
|
|
673
|
+
GET: "GET",
|
|
674
|
+
POST: "POST",
|
|
675
|
+
PUT: "PUT",
|
|
676
|
+
PATCH: "PATCH",
|
|
677
|
+
HEAD: "HEAD",
|
|
678
|
+
OPTIONS: "OPTIONS"
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
class HttpRequest {
|
|
682
|
+
protocol;
|
|
683
|
+
method;
|
|
684
|
+
path;
|
|
685
|
+
headers;
|
|
686
|
+
body;
|
|
687
|
+
constructor(request) {
|
|
688
|
+
const { protocol, method, path, headers, body } = this._parseRequest(request);
|
|
689
|
+
this.protocol = protocol;
|
|
690
|
+
this.method = method;
|
|
691
|
+
this.path = path;
|
|
692
|
+
this.headers = headers;
|
|
693
|
+
this.body = body;
|
|
694
|
+
}
|
|
695
|
+
_parseRequest(request) {
|
|
696
|
+
if (!request)
|
|
697
|
+
throw new Error("Invalid request");
|
|
698
|
+
const [firstLine, rest] = this._divideStringOn(request, "\r\n");
|
|
699
|
+
const [method, path, protocol] = firstLine.split(" ", 3);
|
|
700
|
+
let [headers, body] = this._divideStringOn(rest, "\r\n\r\n");
|
|
701
|
+
const parsedHeaders = [];
|
|
702
|
+
for (const header of headers.split("\r\n")) {
|
|
703
|
+
if (!header.includes(": "))
|
|
704
|
+
throw new Error("Invalid header");
|
|
705
|
+
const [key, value] = this._divideStringOn(header, ": ");
|
|
706
|
+
if (!key || !value)
|
|
707
|
+
throw new Error("Invalid header");
|
|
708
|
+
parsedHeaders.push({ key, value });
|
|
709
|
+
}
|
|
710
|
+
if (method === HttpMethod.GET || method === HttpMethod.HEAD)
|
|
711
|
+
return { protocol, method, path, headers: parsedHeaders };
|
|
712
|
+
return { protocol, method, path, headers: parsedHeaders, body };
|
|
713
|
+
}
|
|
714
|
+
_divideStringOn(s, search) {
|
|
715
|
+
const index = s.indexOf(search);
|
|
716
|
+
const first = s.slice(0, index);
|
|
717
|
+
const rest = s.slice(index + search.length);
|
|
718
|
+
return [first, rest];
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// app/HttpResponse.ts
|
|
723
|
+
var import_dayjs = __toESM(require_dayjs_min(), 1);
|
|
724
|
+
|
|
725
|
+
// app/utils/calculateContentLength.utils.ts
|
|
726
|
+
function calculateContentLength(body) {
|
|
727
|
+
return Buffer.byteLength(body);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// app/HttpResponse.ts
|
|
731
|
+
var HttpStatus = {
|
|
732
|
+
OK: "OK",
|
|
733
|
+
CREATED: "Created",
|
|
734
|
+
NO_CONTENT: "No Content",
|
|
735
|
+
BAD_REQUEST: "Bad Request",
|
|
736
|
+
UNAUTHORIZED: "Unauthorized",
|
|
737
|
+
FORBIDDEN: "Forbidden",
|
|
738
|
+
NOT_FOUND: "Not Found",
|
|
739
|
+
METHOD_NOT_ALLOWED: "Method Not Allowed",
|
|
740
|
+
TOO_MANY_REQUESTS: "Too Many Requests",
|
|
741
|
+
INTERNAL_SERVER_ERROR: "Internal Server Error"
|
|
742
|
+
};
|
|
743
|
+
var HttpStatusCode = {
|
|
744
|
+
OK: 200,
|
|
745
|
+
CREATED: 201,
|
|
746
|
+
NO_CONTENT: 204,
|
|
747
|
+
BAD_REQUEST: 400,
|
|
748
|
+
UNAUTHORIZED: 401,
|
|
749
|
+
FORBIDDEN: 403,
|
|
750
|
+
NOT_FOUND: 404,
|
|
751
|
+
METHOD_NOT_ALLOWED: 405,
|
|
752
|
+
TOO_MANY_REQUESTS: 429,
|
|
753
|
+
INTERNAL_SERVER_ERROR: 500
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
class HttpResponse {
|
|
757
|
+
protocol;
|
|
758
|
+
method;
|
|
759
|
+
path;
|
|
760
|
+
status;
|
|
761
|
+
statusCode;
|
|
762
|
+
headers = {};
|
|
763
|
+
body = "";
|
|
764
|
+
constructor(request) {
|
|
765
|
+
this.method = request.method;
|
|
766
|
+
this.path = request.path;
|
|
767
|
+
this.protocol = request.protocol;
|
|
768
|
+
this.statusCode = HttpStatusCode.OK;
|
|
769
|
+
this.status = HttpStatus.OK;
|
|
770
|
+
this.headers = { Server: "YinzJS", Date: import_dayjs.default().format("ddd, DD MMM YYYY HH:mm:ss [GMT]"), Connection: "keep-alive", "Keep-Alive": "timeout=5, max=1000" };
|
|
771
|
+
}
|
|
772
|
+
addHeaders(headers) {
|
|
773
|
+
for (const header of headers) {
|
|
774
|
+
this.headers = { ...this.headers, ...header };
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
removeHeaders(headers) {
|
|
778
|
+
for (const header of headers)
|
|
779
|
+
delete this.headers[header];
|
|
780
|
+
}
|
|
781
|
+
setStatus(status) {
|
|
782
|
+
switch (status) {
|
|
783
|
+
case HttpStatusCode.OK:
|
|
784
|
+
this.statusCode = HttpStatusCode.OK;
|
|
785
|
+
this.status = HttpStatus.OK;
|
|
786
|
+
break;
|
|
787
|
+
case HttpStatusCode.CREATED:
|
|
788
|
+
this.statusCode = HttpStatusCode.CREATED;
|
|
789
|
+
this.status = HttpStatus.CREATED;
|
|
790
|
+
break;
|
|
791
|
+
case HttpStatusCode.BAD_REQUEST:
|
|
792
|
+
this.statusCode = HttpStatusCode.BAD_REQUEST;
|
|
793
|
+
this.status = HttpStatus.BAD_REQUEST;
|
|
794
|
+
break;
|
|
795
|
+
case HttpStatusCode.UNAUTHORIZED:
|
|
796
|
+
this.statusCode = HttpStatusCode.UNAUTHORIZED;
|
|
797
|
+
this.status = HttpStatus.UNAUTHORIZED;
|
|
798
|
+
break;
|
|
799
|
+
case HttpStatusCode.FORBIDDEN:
|
|
800
|
+
this.statusCode = HttpStatusCode.FORBIDDEN;
|
|
801
|
+
this.status = HttpStatus.FORBIDDEN;
|
|
802
|
+
break;
|
|
803
|
+
case HttpStatusCode.NOT_FOUND:
|
|
804
|
+
this.statusCode = HttpStatusCode.NOT_FOUND;
|
|
805
|
+
this.status = HttpStatus.NOT_FOUND;
|
|
806
|
+
break;
|
|
807
|
+
case HttpStatusCode.METHOD_NOT_ALLOWED:
|
|
808
|
+
this.statusCode = HttpStatusCode.METHOD_NOT_ALLOWED;
|
|
809
|
+
this.status = HttpStatus.METHOD_NOT_ALLOWED;
|
|
810
|
+
break;
|
|
811
|
+
case HttpStatusCode.TOO_MANY_REQUESTS:
|
|
812
|
+
this.statusCode = HttpStatusCode.TOO_MANY_REQUESTS;
|
|
813
|
+
this.status = HttpStatus.TOO_MANY_REQUESTS;
|
|
814
|
+
break;
|
|
815
|
+
case HttpStatusCode.INTERNAL_SERVER_ERROR:
|
|
816
|
+
this.statusCode = HttpStatusCode.INTERNAL_SERVER_ERROR;
|
|
817
|
+
this.status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
818
|
+
break;
|
|
819
|
+
default:
|
|
820
|
+
throw new Error("Invalid status or status argument not set");
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
setBody(body) {
|
|
824
|
+
this.body = body;
|
|
825
|
+
if (typeof body === "object") {
|
|
826
|
+
this.addHeaders([{ "Content-Type": "application/json", "Content-Length": String(calculateContentLength(JSON.stringify(body))) }]);
|
|
827
|
+
}
|
|
828
|
+
if (typeof body === "string" && body.includes("<html>")) {
|
|
829
|
+
this.addHeaders([{ "Content-Type": "text/html", "Content-Length": String(calculateContentLength(body)) }]);
|
|
830
|
+
}
|
|
831
|
+
if (typeof body === "string") {
|
|
832
|
+
this.addHeaders([{ "Content-Type": "text/plain", "Content-Length": String(calculateContentLength(body)) }]);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
formatHttpResponse() {
|
|
836
|
+
let { body } = this;
|
|
837
|
+
if (typeof body === "object")
|
|
838
|
+
body = JSON.stringify(this.body);
|
|
839
|
+
const formattedHeaders = Object.entries(this.headers).map(([key, value]) => `${key}: ${value}`).join("\r\n");
|
|
840
|
+
return `HTTP/1.1 ${this.statusCode} ${this.status}\r\n${formattedHeaders}\r\n\r\n${String(body)}`;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
// app/Context.utils.ts
|
|
845
|
+
class Context {
|
|
846
|
+
request;
|
|
847
|
+
response;
|
|
848
|
+
constructor(request, response) {
|
|
849
|
+
this.request = request;
|
|
850
|
+
this.response = response;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// app/index.ts
|
|
855
|
+
class YinzJS {
|
|
856
|
+
backlog = 511;
|
|
857
|
+
ip = import_ip.default.address();
|
|
858
|
+
port = 5000;
|
|
859
|
+
_routes = [];
|
|
860
|
+
middleware = [];
|
|
861
|
+
constructor(options) {
|
|
862
|
+
if (options && options.port)
|
|
863
|
+
this.port = options.port;
|
|
864
|
+
}
|
|
865
|
+
_handleMiddleware(route, ctx) {
|
|
866
|
+
let result = undefined;
|
|
867
|
+
if (this.middleware.length) {
|
|
868
|
+
for (const middleware of this.middleware) {
|
|
869
|
+
if (middleware.paths === "allButExcluded" && !middleware.excluded.includes(route.path)) {
|
|
870
|
+
result = middleware.fn(ctx);
|
|
871
|
+
if (result) {
|
|
872
|
+
if (typeof result === "object")
|
|
873
|
+
result = JSON.stringify(result);
|
|
874
|
+
return result;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
if (middleware.paths.includes(route.path)) {
|
|
878
|
+
result = middleware.fn(ctx);
|
|
879
|
+
if (result) {
|
|
880
|
+
if (typeof result === "object")
|
|
881
|
+
result = JSON.stringify(result);
|
|
882
|
+
return result;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
_handleRoute(route, ctx) {
|
|
890
|
+
let result = undefined;
|
|
891
|
+
if (route.beforeGroup) {
|
|
892
|
+
result = route.beforeGroup(ctx);
|
|
893
|
+
if (result) {
|
|
894
|
+
return result;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
if (route.beforeHandler) {
|
|
898
|
+
console.log("beforeHandler", route);
|
|
899
|
+
result = route.beforeHandler(ctx);
|
|
900
|
+
if (result) {
|
|
901
|
+
return result;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
result = route.handler(ctx);
|
|
905
|
+
if (route.afterHandler)
|
|
906
|
+
route.afterHandler(ctx);
|
|
907
|
+
return result;
|
|
908
|
+
}
|
|
909
|
+
_handleRequest(request) {
|
|
910
|
+
const ctx = new Context(request, new HttpResponse(request));
|
|
911
|
+
const route = this._routes.find((r) => r.path === ctx.request.path && r.method === ctx.request.method);
|
|
912
|
+
if (!route) {
|
|
913
|
+
ctx.response.setStatus(HttpStatusCode.NOT_FOUND);
|
|
914
|
+
ctx.response.setBody("404 Not Found");
|
|
915
|
+
return ctx.response;
|
|
916
|
+
}
|
|
917
|
+
let result = undefined;
|
|
918
|
+
result = this._handleMiddleware(route, ctx);
|
|
919
|
+
if (result) {
|
|
920
|
+
ctx.response.setBody(result);
|
|
921
|
+
return ctx.response;
|
|
922
|
+
}
|
|
923
|
+
result = this._handleRoute(route, ctx);
|
|
924
|
+
ctx.response.setBody(result ?? "Server Error");
|
|
925
|
+
return ctx.response;
|
|
926
|
+
}
|
|
927
|
+
_route(path, handler, options = {
|
|
928
|
+
method: HttpMethod.GET
|
|
929
|
+
}) {
|
|
930
|
+
const { beforeHandler, afterHandler } = options;
|
|
931
|
+
const route = {
|
|
932
|
+
path,
|
|
933
|
+
method: options.method,
|
|
934
|
+
handler,
|
|
935
|
+
beforeHandler: beforeHandler ?? undefined,
|
|
936
|
+
afterHandler: afterHandler ?? undefined
|
|
937
|
+
};
|
|
938
|
+
this._routes.push(route);
|
|
939
|
+
return route;
|
|
940
|
+
}
|
|
941
|
+
get(path, handler, options) {
|
|
942
|
+
return this._route(path, handler, { method: HttpMethod.GET, ...options });
|
|
943
|
+
}
|
|
944
|
+
post(path, handler, options) {
|
|
945
|
+
return this._route(path, handler, { method: HttpMethod.POST, ...options });
|
|
946
|
+
}
|
|
947
|
+
put(path, handler, options) {
|
|
948
|
+
return this._route(path, handler, { method: HttpMethod.PUT, ...options });
|
|
949
|
+
}
|
|
950
|
+
delete(path, handler, options) {
|
|
951
|
+
return this._route(path, handler, { method: HttpMethod.DELETE, ...options });
|
|
952
|
+
}
|
|
953
|
+
patch(path, handler, options) {
|
|
954
|
+
return this._route(path, handler, { method: HttpMethod.PATCH, ...options });
|
|
955
|
+
}
|
|
956
|
+
group(prefix, routes, options) {
|
|
957
|
+
for (const route of routes)
|
|
958
|
+
this._routes.push({ ...route, path: `${prefix}${route.path}`, beforeGroup: options?.beforeGroup });
|
|
959
|
+
}
|
|
960
|
+
routes(routes) {
|
|
961
|
+
for (const route of routes)
|
|
962
|
+
this._routes.push(route);
|
|
963
|
+
}
|
|
964
|
+
beforeAll(fn, options) {
|
|
965
|
+
if (!options) {
|
|
966
|
+
this.middleware.push({ paths: [], excluded: [], fn });
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
if ("excluded" in options && Array.isArray(options.excluded)) {
|
|
970
|
+
this.middleware.push({ paths: "allButExcluded", excluded: options.excluded, fn });
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
if ("paths" in options && Array.isArray(options.paths)) {
|
|
974
|
+
this.middleware.push({ paths: options.paths, excluded: [], fn });
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
listen() {
|
|
979
|
+
const server = createServer().listen(this.port, this.ip, this.backlog);
|
|
980
|
+
server.on("listening", () => {
|
|
981
|
+
console.log(`Server listening on ${this.ip}:${this.port}`);
|
|
982
|
+
});
|
|
983
|
+
server.on("connection", (socket) => {
|
|
984
|
+
socket.on("data", (buffer) => {
|
|
985
|
+
const request = new HttpRequest(buffer.toString());
|
|
986
|
+
const response = this._handleRequest(request);
|
|
987
|
+
socket.write(response.formatHttpResponse());
|
|
988
|
+
socket.end();
|
|
989
|
+
});
|
|
990
|
+
});
|
|
991
|
+
server.on("error", (error) => {
|
|
992
|
+
console.error(error);
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
export {
|
|
997
|
+
YinzJS,
|
|
998
|
+
HttpStatusCode,
|
|
999
|
+
HttpMethod
|
|
1000
|
+
};
|
|
1001
|
+
|
|
1002
|
+
//# debugId=C00C6BDE7F1FBDB364756E2164756E21
|