bareserver 0.1.2 → 0.1.4
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/index.js +71 -22
- package/package.json +5 -5
package/index.js
CHANGED
|
@@ -32,30 +32,49 @@ class BareRequest extends Readable {
|
|
|
32
32
|
super();
|
|
33
33
|
this._raw = raw;
|
|
34
34
|
this._headers = null;
|
|
35
|
+
this._rawHeaders = null;
|
|
35
36
|
this._rawBody = raw.rawBody;
|
|
36
37
|
this._parsedBody = undefined;
|
|
37
38
|
this._cookies = undefined;
|
|
38
39
|
this._streamEnded = false;
|
|
39
40
|
this.method = raw.method;
|
|
40
41
|
this.path = raw.path;
|
|
41
|
-
this.
|
|
42
|
-
this.originalUrl = this.url;
|
|
42
|
+
this._url = null;
|
|
43
43
|
this.baseUrl = '';
|
|
44
44
|
this._query = null;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
this._params = raw.params;
|
|
46
|
+
this._paramsDecoded = false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get url() {
|
|
50
|
+
if (this._url !== null) return this._url;
|
|
51
|
+
return (this._url = this._raw.query ? `${this._raw.path}?${this._raw.query}` : this._raw.path);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get originalUrl() {
|
|
55
|
+
return this.url;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get params() {
|
|
59
|
+
if (this._paramsDecoded) return this._params;
|
|
60
|
+
const params = this._params || {};
|
|
61
|
+
for (const k in params) {
|
|
62
|
+
const val = params[k];
|
|
63
|
+
if (typeof val === 'string' && val.includes('%')) {
|
|
64
|
+
try {
|
|
65
|
+
params[k] = decodeURIComponent(val);
|
|
66
|
+
} catch {
|
|
67
|
+
// Keep raw if invalid encoding
|
|
55
68
|
}
|
|
56
69
|
}
|
|
57
70
|
}
|
|
58
|
-
this.
|
|
71
|
+
this._paramsDecoded = true;
|
|
72
|
+
return (this._params = params);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
set params(val) {
|
|
76
|
+
this._params = val;
|
|
77
|
+
this._paramsDecoded = true;
|
|
59
78
|
}
|
|
60
79
|
|
|
61
80
|
_read() {
|
|
@@ -88,24 +107,54 @@ class BareRequest extends Readable {
|
|
|
88
107
|
return (this._query = obj);
|
|
89
108
|
}
|
|
90
109
|
|
|
110
|
+
get rawHeaders() {
|
|
111
|
+
if (this._rawHeaders !== null) return this._rawHeaders;
|
|
112
|
+
const s = this._raw.rawHeadersStr;
|
|
113
|
+
if (!s) return (this._rawHeaders = []);
|
|
114
|
+
const parts = s.split('\n');
|
|
115
|
+
if (parts.length > 0 && parts[parts.length - 1] === '') {
|
|
116
|
+
parts.pop();
|
|
117
|
+
}
|
|
118
|
+
return (this._rawHeaders = parts);
|
|
119
|
+
}
|
|
120
|
+
|
|
91
121
|
get headers() {
|
|
92
122
|
if (this._headers !== null) return this._headers;
|
|
93
123
|
const obj = {};
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
124
|
+
const s = this._raw.rawHeadersStr;
|
|
125
|
+
if (s) {
|
|
126
|
+
let start = 0;
|
|
127
|
+
const len = s.length;
|
|
128
|
+
while (start < len) {
|
|
129
|
+
const nlKey = s.indexOf('\n', start);
|
|
130
|
+
if (nlKey === -1) break;
|
|
131
|
+
const nlVal = s.indexOf('\n', nlKey + 1);
|
|
132
|
+
const valEnd = nlVal === -1 ? len : nlVal;
|
|
133
|
+
const k = s.slice(start, nlKey).toLowerCase();
|
|
134
|
+
const v = s.slice(nlKey + 1, valEnd);
|
|
135
|
+
obj[k] = v;
|
|
136
|
+
start = valEnd + 1;
|
|
98
137
|
}
|
|
99
138
|
}
|
|
100
139
|
return (this._headers = obj);
|
|
101
140
|
}
|
|
102
141
|
|
|
103
142
|
get(field) {
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
143
|
+
const s = this._raw.rawHeadersStr;
|
|
144
|
+
if (!s) return undefined;
|
|
145
|
+
const target = field.toLowerCase();
|
|
146
|
+
let start = 0;
|
|
147
|
+
const len = s.length;
|
|
148
|
+
while (start < len) {
|
|
149
|
+
const nlKey = s.indexOf('\n', start);
|
|
150
|
+
if (nlKey === -1) break;
|
|
151
|
+
const nlVal = s.indexOf('\n', nlKey + 1);
|
|
152
|
+
const valEnd = nlVal === -1 ? len : nlVal;
|
|
153
|
+
const k = s.slice(start, nlKey);
|
|
154
|
+
if (k.toLowerCase() === target) {
|
|
155
|
+
return s.slice(nlKey + 1, valEnd);
|
|
156
|
+
}
|
|
157
|
+
start = valEnd + 1;
|
|
109
158
|
}
|
|
110
159
|
return undefined;
|
|
111
160
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bareserver",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Zero-dependency, native-fast HTTP server for Node.js",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"LICENSE"
|
|
12
12
|
],
|
|
13
13
|
"optionalDependencies": {
|
|
14
|
-
"bareserver-darwin-arm64": "0.1.
|
|
15
|
-
"bareserver-darwin-x64": "0.1.
|
|
16
|
-
"bareserver-linux-x64-gnu": "0.1.
|
|
17
|
-
"bareserver-win32-x64-msvc": "0.1.
|
|
14
|
+
"bareserver-darwin-arm64": "0.1.4",
|
|
15
|
+
"bareserver-darwin-x64": "0.1.4",
|
|
16
|
+
"bareserver-linux-x64-gnu": "0.1.4",
|
|
17
|
+
"bareserver-win32-x64-msvc": "0.1.4"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"build": "cargo build --release && (cp target/release/libbareserver.dylib index.node 2>/dev/null || cp target/release/libbareserver.so index.node 2>/dev/null || true)",
|