@e-mc/request 0.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/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@e-mc/request",
3
+ "version": "0.0.1",
4
+ "description": "Request constructor for e-mc.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/anpham6/e-mc.git",
13
+ "directory": "src/request"
14
+ },
15
+ "keywords": [
16
+ "squared",
17
+ "squared-functions"
18
+ ],
19
+ "author": "An Pham <anpham6@gmail.com>",
20
+ "license": "BSD 3-Clause",
21
+ "homepage": "https://github.com/anpham6/e-mc#readme",
22
+ "dependencies": {
23
+ "@e-mc/module": "0.0.1",
24
+ "@e-mc/types": "0.0.1",
25
+ "combined-stream": "^1.0.8",
26
+ "js-yaml": "^4.1.0",
27
+ "which": "^2.0.2"
28
+ }
29
+ }
package/util.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
2
+
3
+ declare namespace util {
4
+ function parseHeader<T = unknown>(headers: IncomingHttpHeaders, name: string): T | undefined;
5
+ function normalizeHeaders(headers: OutgoingHttpHeaders): OutgoingHttpHeaders;
6
+ function getBasicAuth(auth: AuthValue): string;
7
+ function getBasicAuth(username: unknown, password?: unknown): string;
8
+ function hasBasicAuth(value: string): boolean;
9
+ function checkRetryable(err: unknown): boolean;
10
+ function isRetryable(value: number, timeout?: boolean): boolean;
11
+ function trimPath(value: string): string;
12
+ function asInt(value: unknown): number;
13
+ function asFloat(value: unknown): number;
14
+ function fromSeconds(value: unknown): number;
15
+ }
16
+
17
+ export = util;
package/util.js ADDED
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromSeconds = exports.asFloat = exports.asInt = exports.trimPath = exports.isRetryable = exports.checkRetryable = exports.hasBasicAuth = exports.getBasicAuth = exports.normalizeHeaders = exports.parseHeader = void 0;
4
+ const module_1 = require("../module");
5
+ const types_1 = require("../types");
6
+ const safeInt = (value) => value >= 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : NaN;
7
+ function parseHeader(headers, name) {
8
+ const value = headers[name];
9
+ if (value) {
10
+ switch (name.toLowerCase()) {
11
+ case 'content-disposition': {
12
+ const pattern = /\bfilename(?:\*\s*=\s*UTF-8''([^\s;]+)|\s*=\s*(?:"([^"]+)"|([^\s;]+)))/gi;
13
+ let result, match;
14
+ while (match = pattern.exec(value)) {
15
+ if (match[1]) {
16
+ return decodeURIComponent(match[1]).trim();
17
+ }
18
+ result = (match[2] || match[3]).trim();
19
+ }
20
+ return result;
21
+ }
22
+ }
23
+ }
24
+ }
25
+ exports.parseHeader = parseHeader;
26
+ function normalizeHeaders(headers) {
27
+ const result = Object.create(null);
28
+ for (const name in headers) {
29
+ let value = headers[name];
30
+ switch (typeof value) {
31
+ case 'number':
32
+ value = value.toString();
33
+ break;
34
+ case 'string':
35
+ value = value.trim();
36
+ break;
37
+ default:
38
+ if (Array.isArray(value)) {
39
+ value = value.map(out => module_1.default.asString(out).trim());
40
+ break;
41
+ }
42
+ continue;
43
+ }
44
+ if (value) {
45
+ result[trimPath(name.trim().toLowerCase())] = value;
46
+ }
47
+ }
48
+ return result;
49
+ }
50
+ exports.normalizeHeaders = normalizeHeaders;
51
+ function getBasicAuth(username, password) {
52
+ if ((0, types_1.isObject)(username)) {
53
+ ({ username, password } = username);
54
+ }
55
+ return (0, types_1.isString)(username) ? encodeURIComponent(username) + ((0, types_1.isString)(password) ? ':' + encodeURIComponent(password) : '') + '@' : '';
56
+ }
57
+ exports.getBasicAuth = getBasicAuth;
58
+ function hasBasicAuth(value) {
59
+ try {
60
+ return (0, types_1.isString)(new URL(value).username);
61
+ }
62
+ catch {
63
+ }
64
+ return false;
65
+ }
66
+ exports.hasBasicAuth = hasBasicAuth;
67
+ function checkRetryable(err) {
68
+ if (err instanceof Error) {
69
+ const { code, errno } = err;
70
+ switch (code) {
71
+ case 'ETIMEDOUT':
72
+ case 'ECONNRESET':
73
+ case 'EADDRINUSE':
74
+ case 'ECONNREFUSED':
75
+ case 'EPIPE':
76
+ case 'ENOTFOUND':
77
+ case 'ENETUNREACH':
78
+ case 'EAI_AGAIN':
79
+ case 'EPROTO':
80
+ return true;
81
+ default:
82
+ return typeof errno === 'number' && isRetryable(Math.abs(errno));
83
+ }
84
+ }
85
+ return false;
86
+ }
87
+ exports.checkRetryable = checkRetryable;
88
+ function isRetryable(value, timeout) {
89
+ switch (value) {
90
+ case 408 /* HTTP_STATUS.REQUEST_TIMEOUT */:
91
+ case 504 /* HTTP_STATUS.GATEWAY_TIMEOUT */:
92
+ case 522 /* HTTP_STATUS.CONNECTION_TIMED_OUT */:
93
+ case 524 /* HTTP_STATUS.A_TIMEOUT_OCCURRED */:
94
+ if (timeout) {
95
+ return true;
96
+ }
97
+ case 429 /* HTTP_STATUS.TOO_MANY_REQUESTS */:
98
+ case 499 /* HTTP_STATUS.CLIENT_CLOSED_REQUEST */:
99
+ case 500 /* HTTP_STATUS.INTERNAL_SERVER_ERROR */:
100
+ case 502 /* HTTP_STATUS.BAD_GATEWAY */:
101
+ case 503 /* HTTP_STATUS.SERVICE_UNAVAILABLE */:
102
+ case 521 /* HTTP_STATUS.WEB_SERVER_IS_DOWN */:
103
+ if (!timeout) {
104
+ return true;
105
+ }
106
+ default:
107
+ return false;
108
+ }
109
+ }
110
+ exports.isRetryable = isRetryable;
111
+ function trimPath(value) {
112
+ const length = value.length - 1;
113
+ return value[length] === '/' ? value.substring(0, length) : value;
114
+ }
115
+ exports.trimPath = trimPath;
116
+ function asInt(value) {
117
+ switch (typeof value) {
118
+ case 'string':
119
+ return safeInt(parseInt(value));
120
+ case 'number':
121
+ return safeInt(Math.floor(value));
122
+ default:
123
+ return NaN;
124
+ }
125
+ }
126
+ exports.asInt = asInt;
127
+ function asFloat(value) {
128
+ switch (typeof value) {
129
+ case 'string':
130
+ return safeInt(parseFloat(value));
131
+ case 'number':
132
+ return safeInt(value);
133
+ default:
134
+ return NaN;
135
+ }
136
+ }
137
+ exports.asFloat = asFloat;
138
+ function fromSeconds(value) {
139
+ switch (typeof value) {
140
+ case 'string':
141
+ return safeInt(Math.ceil(parseFloat(value)) * 1000 /* TIME.S */);
142
+ case 'number':
143
+ return safeInt(value * 1000 /* TIME.S */);
144
+ default:
145
+ return NaN;
146
+ }
147
+ }
148
+ exports.fromSeconds = fromSeconds;