@wooksjs/http-body 0.4.9 → 0.4.10
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/dist/index.cjs +160 -153
- package/dist/index.mjs +160 -153
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -16,6 +16,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
16
16
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
17
|
PERFORMANCE OF THIS SOFTWARE.
|
|
18
18
|
***************************************************************************** */
|
|
19
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
20
|
+
|
|
19
21
|
|
|
20
22
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
21
23
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -25,162 +27,167 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
25
27
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
26
28
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
27
29
|
});
|
|
28
|
-
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
33
|
+
var e = new Error(message);
|
|
34
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
35
|
+
};
|
|
29
36
|
|
|
30
|
-
const compressors = {
|
|
31
|
-
identity: {
|
|
32
|
-
compress: (v) => v,
|
|
33
|
-
uncompress: (v) => v,
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
function uncompressBody(encodings, body) {
|
|
37
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
let newBody = body;
|
|
39
|
-
for (const e of encodings.reverse()) {
|
|
40
|
-
if (!compressors[e]) {
|
|
41
|
-
throw new Error(`Usupported compression type "${e}".`);
|
|
42
|
-
}
|
|
43
|
-
newBody = yield compressors[e].uncompress(body);
|
|
44
|
-
}
|
|
45
|
-
return newBody;
|
|
46
|
-
});
|
|
37
|
+
const compressors = {
|
|
38
|
+
identity: {
|
|
39
|
+
compress: (v) => v,
|
|
40
|
+
uncompress: (v) => v,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
function uncompressBody(encodings, body) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
let newBody = body;
|
|
46
|
+
for (const e of encodings.reverse()) {
|
|
47
|
+
if (!compressors[e]) {
|
|
48
|
+
throw new Error(`Usupported compression type "${e}".`);
|
|
49
|
+
}
|
|
50
|
+
newBody = yield compressors[e].uncompress(body);
|
|
51
|
+
}
|
|
52
|
+
return newBody;
|
|
53
|
+
});
|
|
47
54
|
}
|
|
48
55
|
|
|
49
|
-
function useBody() {
|
|
50
|
-
const { store } = eventHttp.useHttpContext();
|
|
51
|
-
const { init } = store('request');
|
|
52
|
-
const { rawBody } = eventHttp.useRequest();
|
|
53
|
-
const { 'content-type': contentType, 'content-encoding': contentEncoding } = eventHttp.useHeaders();
|
|
54
|
-
function contentIs(type) {
|
|
55
|
-
return (contentType || '').indexOf(type) >= 0;
|
|
56
|
-
}
|
|
57
|
-
const isJson = () => init('isJson', () => contentIs('application/json'));
|
|
58
|
-
const isHtml = () => init('isHtml', () => contentIs('text/html'));
|
|
59
|
-
const isXml = () => init('isXml', () => contentIs('text/xml'));
|
|
60
|
-
const isText = () => init('isText', () => contentIs('text/plain'));
|
|
61
|
-
const isBinary = () => init('isBinary', () => contentIs('application/octet-stream'));
|
|
62
|
-
const isFormData = () => init('isFormData', () => contentIs('multipart/form-data'));
|
|
63
|
-
const isUrlencoded = () => init('isUrlencoded', () => contentIs('application/x-www-form-urlencoded'));
|
|
64
|
-
const isCompressed = () => init('isCompressed', () => {
|
|
65
|
-
const parts = contentEncodings();
|
|
66
|
-
for (const p of parts) {
|
|
67
|
-
if (['deflate', 'gzip', 'br'].includes(p))
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
return false;
|
|
71
|
-
});
|
|
72
|
-
const contentEncodings = () => init('contentEncodings', () => (contentEncoding || '')
|
|
73
|
-
.split(',')
|
|
74
|
-
.map((p) => p.trim())
|
|
75
|
-
.filter((p) => !!p));
|
|
76
|
-
const parseBody = () => init('parsed', () => __awaiter(this, void 0, void 0, function* () {
|
|
77
|
-
const body = yield uncompressBody(contentEncodings(), (yield rawBody()).toString());
|
|
78
|
-
if (isJson())
|
|
79
|
-
return jsonParser(body);
|
|
80
|
-
else if (isFormData())
|
|
81
|
-
return formDataParser(body);
|
|
82
|
-
else if (isUrlencoded())
|
|
83
|
-
return urlEncodedParser(body);
|
|
84
|
-
else if (isBinary())
|
|
85
|
-
return textParser(body);
|
|
86
|
-
else
|
|
87
|
-
return textParser(body);
|
|
88
|
-
}));
|
|
89
|
-
function jsonParser(v) {
|
|
90
|
-
try {
|
|
91
|
-
return JSON.parse(v);
|
|
92
|
-
}
|
|
93
|
-
catch (e) {
|
|
94
|
-
throw new eventHttp.HttpError(400, e.message);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
function textParser(v) {
|
|
98
|
-
return v;
|
|
99
|
-
}
|
|
100
|
-
function formDataParser(v) {
|
|
101
|
-
const boundary = '--' +
|
|
102
|
-
(/boundary=([^;]+)(?:;|$)/.exec(contentType || '') || [, ''])[1];
|
|
103
|
-
if (!boundary)
|
|
104
|
-
throw new eventHttp.HttpError(eventHttp.EHttpStatusCode.BadRequest, 'form-data boundary not recognized');
|
|
105
|
-
const parts = v.trim().split(boundary);
|
|
106
|
-
const result = {};
|
|
107
|
-
let key = '';
|
|
108
|
-
let partContentType = 'text/plain';
|
|
109
|
-
for (const part of parts) {
|
|
110
|
-
parsePart();
|
|
111
|
-
key = '';
|
|
112
|
-
partContentType = 'text/plain';
|
|
113
|
-
let valueMode = false;
|
|
114
|
-
const lines = part
|
|
115
|
-
.trim()
|
|
116
|
-
.split(/\n/g)
|
|
117
|
-
.map((s) => s.trim());
|
|
118
|
-
for (const line of lines) {
|
|
119
|
-
if (valueMode) {
|
|
120
|
-
if (!result[key]) {
|
|
121
|
-
result[key] = line;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
result[key] += '\n' + line;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
if (!line || line === '--') {
|
|
129
|
-
valueMode = !!key;
|
|
130
|
-
if (valueMode) {
|
|
131
|
-
key = key.replace(/^["']/, '').replace(/["']$/, '');
|
|
132
|
-
}
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
if (line
|
|
136
|
-
.toLowerCase()
|
|
137
|
-
.startsWith('content-disposition: form-data;')) {
|
|
138
|
-
key = (/name=([^;]+)/.exec(line) || [])[1];
|
|
139
|
-
if (!key)
|
|
140
|
-
throw new eventHttp.HttpError(eventHttp.EHttpStatusCode.BadRequest, 'Could not read multipart name: ' + line);
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
if (line.toLowerCase().startsWith('content-type:')) {
|
|
144
|
-
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
145
|
-
if (!partContentType)
|
|
146
|
-
throw new eventHttp.HttpError(eventHttp.EHttpStatusCode.BadRequest, 'Could not read content-type: ' + line);
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
parsePart();
|
|
153
|
-
function parsePart() {
|
|
154
|
-
if (key) {
|
|
155
|
-
if (partContentType.indexOf('application/json') >= 0) {
|
|
156
|
-
result[key] = JSON.parse(result[key]);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return result;
|
|
161
|
-
}
|
|
162
|
-
function urlEncodedParser(v) {
|
|
163
|
-
return new eventHttp.WooksURLSearchParams(v.trim()).toJson();
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
isJson,
|
|
167
|
-
isHtml,
|
|
168
|
-
isXml,
|
|
169
|
-
isText,
|
|
170
|
-
isBinary,
|
|
171
|
-
isFormData,
|
|
172
|
-
isUrlencoded,
|
|
173
|
-
isCompressed,
|
|
174
|
-
contentEncodings,
|
|
175
|
-
parseBody,
|
|
176
|
-
rawBody,
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
function registerBodyCompressor(name, compressor) {
|
|
180
|
-
if (compressors[name]) {
|
|
181
|
-
throw new Error(`Body compressor "${name}" already registered.`);
|
|
182
|
-
}
|
|
183
|
-
compressors[name] = compressor;
|
|
56
|
+
function useBody() {
|
|
57
|
+
const { store } = eventHttp.useHttpContext();
|
|
58
|
+
const { init } = store('request');
|
|
59
|
+
const { rawBody } = eventHttp.useRequest();
|
|
60
|
+
const { 'content-type': contentType, 'content-encoding': contentEncoding } = eventHttp.useHeaders();
|
|
61
|
+
function contentIs(type) {
|
|
62
|
+
return (contentType || '').indexOf(type) >= 0;
|
|
63
|
+
}
|
|
64
|
+
const isJson = () => init('isJson', () => contentIs('application/json'));
|
|
65
|
+
const isHtml = () => init('isHtml', () => contentIs('text/html'));
|
|
66
|
+
const isXml = () => init('isXml', () => contentIs('text/xml'));
|
|
67
|
+
const isText = () => init('isText', () => contentIs('text/plain'));
|
|
68
|
+
const isBinary = () => init('isBinary', () => contentIs('application/octet-stream'));
|
|
69
|
+
const isFormData = () => init('isFormData', () => contentIs('multipart/form-data'));
|
|
70
|
+
const isUrlencoded = () => init('isUrlencoded', () => contentIs('application/x-www-form-urlencoded'));
|
|
71
|
+
const isCompressed = () => init('isCompressed', () => {
|
|
72
|
+
const parts = contentEncodings();
|
|
73
|
+
for (const p of parts) {
|
|
74
|
+
if (['deflate', 'gzip', 'br'].includes(p))
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
});
|
|
79
|
+
const contentEncodings = () => init('contentEncodings', () => (contentEncoding || '')
|
|
80
|
+
.split(',')
|
|
81
|
+
.map((p) => p.trim())
|
|
82
|
+
.filter((p) => !!p));
|
|
83
|
+
const parseBody = () => init('parsed', () => __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const body = yield uncompressBody(contentEncodings(), (yield rawBody()).toString());
|
|
85
|
+
if (isJson())
|
|
86
|
+
return jsonParser(body);
|
|
87
|
+
else if (isFormData())
|
|
88
|
+
return formDataParser(body);
|
|
89
|
+
else if (isUrlencoded())
|
|
90
|
+
return urlEncodedParser(body);
|
|
91
|
+
else if (isBinary())
|
|
92
|
+
return textParser(body);
|
|
93
|
+
else
|
|
94
|
+
return textParser(body);
|
|
95
|
+
}));
|
|
96
|
+
function jsonParser(v) {
|
|
97
|
+
try {
|
|
98
|
+
return JSON.parse(v);
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
throw new eventHttp.HttpError(400, e.message);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function textParser(v) {
|
|
105
|
+
return v;
|
|
106
|
+
}
|
|
107
|
+
function formDataParser(v) {
|
|
108
|
+
const boundary = '--' +
|
|
109
|
+
(/boundary=([^;]+)(?:;|$)/.exec(contentType || '') || [, ''])[1];
|
|
110
|
+
if (!boundary)
|
|
111
|
+
throw new eventHttp.HttpError(eventHttp.EHttpStatusCode.BadRequest, 'form-data boundary not recognized');
|
|
112
|
+
const parts = v.trim().split(boundary);
|
|
113
|
+
const result = {};
|
|
114
|
+
let key = '';
|
|
115
|
+
let partContentType = 'text/plain';
|
|
116
|
+
for (const part of parts) {
|
|
117
|
+
parsePart();
|
|
118
|
+
key = '';
|
|
119
|
+
partContentType = 'text/plain';
|
|
120
|
+
let valueMode = false;
|
|
121
|
+
const lines = part
|
|
122
|
+
.trim()
|
|
123
|
+
.split(/\n/g)
|
|
124
|
+
.map((s) => s.trim());
|
|
125
|
+
for (const line of lines) {
|
|
126
|
+
if (valueMode) {
|
|
127
|
+
if (!result[key]) {
|
|
128
|
+
result[key] = line;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
result[key] += '\n' + line;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
if (!line || line === '--') {
|
|
136
|
+
valueMode = !!key;
|
|
137
|
+
if (valueMode) {
|
|
138
|
+
key = key.replace(/^["']/, '').replace(/["']$/, '');
|
|
139
|
+
}
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (line
|
|
143
|
+
.toLowerCase()
|
|
144
|
+
.startsWith('content-disposition: form-data;')) {
|
|
145
|
+
key = (/name=([^;]+)/.exec(line) || [])[1];
|
|
146
|
+
if (!key)
|
|
147
|
+
throw new eventHttp.HttpError(eventHttp.EHttpStatusCode.BadRequest, 'Could not read multipart name: ' + line);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (line.toLowerCase().startsWith('content-type:')) {
|
|
151
|
+
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
152
|
+
if (!partContentType)
|
|
153
|
+
throw new eventHttp.HttpError(eventHttp.EHttpStatusCode.BadRequest, 'Could not read content-type: ' + line);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
parsePart();
|
|
160
|
+
function parsePart() {
|
|
161
|
+
if (key) {
|
|
162
|
+
if (partContentType.indexOf('application/json') >= 0) {
|
|
163
|
+
result[key] = JSON.parse(result[key]);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
function urlEncodedParser(v) {
|
|
170
|
+
return new eventHttp.WooksURLSearchParams(v.trim()).toJson();
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
isJson,
|
|
174
|
+
isHtml,
|
|
175
|
+
isXml,
|
|
176
|
+
isText,
|
|
177
|
+
isBinary,
|
|
178
|
+
isFormData,
|
|
179
|
+
isUrlencoded,
|
|
180
|
+
isCompressed,
|
|
181
|
+
contentEncodings,
|
|
182
|
+
parseBody,
|
|
183
|
+
rawBody,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function registerBodyCompressor(name, compressor) {
|
|
187
|
+
if (compressors[name]) {
|
|
188
|
+
throw new Error(`Body compressor "${name}" already registered.`);
|
|
189
|
+
}
|
|
190
|
+
compressors[name] = compressor;
|
|
184
191
|
}
|
|
185
192
|
|
|
186
193
|
exports.registerBodyCompressor = registerBodyCompressor;
|
package/dist/index.mjs
CHANGED
|
@@ -14,6 +14,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
14
14
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
15
|
PERFORMANCE OF THIS SOFTWARE.
|
|
16
16
|
***************************************************************************** */
|
|
17
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
18
|
+
|
|
17
19
|
|
|
18
20
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
19
21
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -23,162 +25,167 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
23
25
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
24
26
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
25
27
|
});
|
|
26
|
-
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
31
|
+
var e = new Error(message);
|
|
32
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
33
|
+
};
|
|
27
34
|
|
|
28
|
-
const compressors = {
|
|
29
|
-
identity: {
|
|
30
|
-
compress: (v) => v,
|
|
31
|
-
uncompress: (v) => v,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
function uncompressBody(encodings, body) {
|
|
35
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
let newBody = body;
|
|
37
|
-
for (const e of encodings.reverse()) {
|
|
38
|
-
if (!compressors[e]) {
|
|
39
|
-
throw new Error(`Usupported compression type "${e}".`);
|
|
40
|
-
}
|
|
41
|
-
newBody = yield compressors[e].uncompress(body);
|
|
42
|
-
}
|
|
43
|
-
return newBody;
|
|
44
|
-
});
|
|
35
|
+
const compressors = {
|
|
36
|
+
identity: {
|
|
37
|
+
compress: (v) => v,
|
|
38
|
+
uncompress: (v) => v,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
function uncompressBody(encodings, body) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
let newBody = body;
|
|
44
|
+
for (const e of encodings.reverse()) {
|
|
45
|
+
if (!compressors[e]) {
|
|
46
|
+
throw new Error(`Usupported compression type "${e}".`);
|
|
47
|
+
}
|
|
48
|
+
newBody = yield compressors[e].uncompress(body);
|
|
49
|
+
}
|
|
50
|
+
return newBody;
|
|
51
|
+
});
|
|
45
52
|
}
|
|
46
53
|
|
|
47
|
-
function useBody() {
|
|
48
|
-
const { store } = useHttpContext();
|
|
49
|
-
const { init } = store('request');
|
|
50
|
-
const { rawBody } = useRequest();
|
|
51
|
-
const { 'content-type': contentType, 'content-encoding': contentEncoding } = useHeaders();
|
|
52
|
-
function contentIs(type) {
|
|
53
|
-
return (contentType || '').indexOf(type) >= 0;
|
|
54
|
-
}
|
|
55
|
-
const isJson = () => init('isJson', () => contentIs('application/json'));
|
|
56
|
-
const isHtml = () => init('isHtml', () => contentIs('text/html'));
|
|
57
|
-
const isXml = () => init('isXml', () => contentIs('text/xml'));
|
|
58
|
-
const isText = () => init('isText', () => contentIs('text/plain'));
|
|
59
|
-
const isBinary = () => init('isBinary', () => contentIs('application/octet-stream'));
|
|
60
|
-
const isFormData = () => init('isFormData', () => contentIs('multipart/form-data'));
|
|
61
|
-
const isUrlencoded = () => init('isUrlencoded', () => contentIs('application/x-www-form-urlencoded'));
|
|
62
|
-
const isCompressed = () => init('isCompressed', () => {
|
|
63
|
-
const parts = contentEncodings();
|
|
64
|
-
for (const p of parts) {
|
|
65
|
-
if (['deflate', 'gzip', 'br'].includes(p))
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
return false;
|
|
69
|
-
});
|
|
70
|
-
const contentEncodings = () => init('contentEncodings', () => (contentEncoding || '')
|
|
71
|
-
.split(',')
|
|
72
|
-
.map((p) => p.trim())
|
|
73
|
-
.filter((p) => !!p));
|
|
74
|
-
const parseBody = () => init('parsed', () => __awaiter(this, void 0, void 0, function* () {
|
|
75
|
-
const body = yield uncompressBody(contentEncodings(), (yield rawBody()).toString());
|
|
76
|
-
if (isJson())
|
|
77
|
-
return jsonParser(body);
|
|
78
|
-
else if (isFormData())
|
|
79
|
-
return formDataParser(body);
|
|
80
|
-
else if (isUrlencoded())
|
|
81
|
-
return urlEncodedParser(body);
|
|
82
|
-
else if (isBinary())
|
|
83
|
-
return textParser(body);
|
|
84
|
-
else
|
|
85
|
-
return textParser(body);
|
|
86
|
-
}));
|
|
87
|
-
function jsonParser(v) {
|
|
88
|
-
try {
|
|
89
|
-
return JSON.parse(v);
|
|
90
|
-
}
|
|
91
|
-
catch (e) {
|
|
92
|
-
throw new HttpError(400, e.message);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
function textParser(v) {
|
|
96
|
-
return v;
|
|
97
|
-
}
|
|
98
|
-
function formDataParser(v) {
|
|
99
|
-
const boundary = '--' +
|
|
100
|
-
(/boundary=([^;]+)(?:;|$)/.exec(contentType || '') || [, ''])[1];
|
|
101
|
-
if (!boundary)
|
|
102
|
-
throw new HttpError(EHttpStatusCode.BadRequest, 'form-data boundary not recognized');
|
|
103
|
-
const parts = v.trim().split(boundary);
|
|
104
|
-
const result = {};
|
|
105
|
-
let key = '';
|
|
106
|
-
let partContentType = 'text/plain';
|
|
107
|
-
for (const part of parts) {
|
|
108
|
-
parsePart();
|
|
109
|
-
key = '';
|
|
110
|
-
partContentType = 'text/plain';
|
|
111
|
-
let valueMode = false;
|
|
112
|
-
const lines = part
|
|
113
|
-
.trim()
|
|
114
|
-
.split(/\n/g)
|
|
115
|
-
.map((s) => s.trim());
|
|
116
|
-
for (const line of lines) {
|
|
117
|
-
if (valueMode) {
|
|
118
|
-
if (!result[key]) {
|
|
119
|
-
result[key] = line;
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
result[key] += '\n' + line;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
if (!line || line === '--') {
|
|
127
|
-
valueMode = !!key;
|
|
128
|
-
if (valueMode) {
|
|
129
|
-
key = key.replace(/^["']/, '').replace(/["']$/, '');
|
|
130
|
-
}
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
if (line
|
|
134
|
-
.toLowerCase()
|
|
135
|
-
.startsWith('content-disposition: form-data;')) {
|
|
136
|
-
key = (/name=([^;]+)/.exec(line) || [])[1];
|
|
137
|
-
if (!key)
|
|
138
|
-
throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read multipart name: ' + line);
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
if (line.toLowerCase().startsWith('content-type:')) {
|
|
142
|
-
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
143
|
-
if (!partContentType)
|
|
144
|
-
throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read content-type: ' + line);
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
parsePart();
|
|
151
|
-
function parsePart() {
|
|
152
|
-
if (key) {
|
|
153
|
-
if (partContentType.indexOf('application/json') >= 0) {
|
|
154
|
-
result[key] = JSON.parse(result[key]);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return result;
|
|
159
|
-
}
|
|
160
|
-
function urlEncodedParser(v) {
|
|
161
|
-
return new WooksURLSearchParams(v.trim()).toJson();
|
|
162
|
-
}
|
|
163
|
-
return {
|
|
164
|
-
isJson,
|
|
165
|
-
isHtml,
|
|
166
|
-
isXml,
|
|
167
|
-
isText,
|
|
168
|
-
isBinary,
|
|
169
|
-
isFormData,
|
|
170
|
-
isUrlencoded,
|
|
171
|
-
isCompressed,
|
|
172
|
-
contentEncodings,
|
|
173
|
-
parseBody,
|
|
174
|
-
rawBody,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
function registerBodyCompressor(name, compressor) {
|
|
178
|
-
if (compressors[name]) {
|
|
179
|
-
throw new Error(`Body compressor "${name}" already registered.`);
|
|
180
|
-
}
|
|
181
|
-
compressors[name] = compressor;
|
|
54
|
+
function useBody() {
|
|
55
|
+
const { store } = useHttpContext();
|
|
56
|
+
const { init } = store('request');
|
|
57
|
+
const { rawBody } = useRequest();
|
|
58
|
+
const { 'content-type': contentType, 'content-encoding': contentEncoding } = useHeaders();
|
|
59
|
+
function contentIs(type) {
|
|
60
|
+
return (contentType || '').indexOf(type) >= 0;
|
|
61
|
+
}
|
|
62
|
+
const isJson = () => init('isJson', () => contentIs('application/json'));
|
|
63
|
+
const isHtml = () => init('isHtml', () => contentIs('text/html'));
|
|
64
|
+
const isXml = () => init('isXml', () => contentIs('text/xml'));
|
|
65
|
+
const isText = () => init('isText', () => contentIs('text/plain'));
|
|
66
|
+
const isBinary = () => init('isBinary', () => contentIs('application/octet-stream'));
|
|
67
|
+
const isFormData = () => init('isFormData', () => contentIs('multipart/form-data'));
|
|
68
|
+
const isUrlencoded = () => init('isUrlencoded', () => contentIs('application/x-www-form-urlencoded'));
|
|
69
|
+
const isCompressed = () => init('isCompressed', () => {
|
|
70
|
+
const parts = contentEncodings();
|
|
71
|
+
for (const p of parts) {
|
|
72
|
+
if (['deflate', 'gzip', 'br'].includes(p))
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
});
|
|
77
|
+
const contentEncodings = () => init('contentEncodings', () => (contentEncoding || '')
|
|
78
|
+
.split(',')
|
|
79
|
+
.map((p) => p.trim())
|
|
80
|
+
.filter((p) => !!p));
|
|
81
|
+
const parseBody = () => init('parsed', () => __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
const body = yield uncompressBody(contentEncodings(), (yield rawBody()).toString());
|
|
83
|
+
if (isJson())
|
|
84
|
+
return jsonParser(body);
|
|
85
|
+
else if (isFormData())
|
|
86
|
+
return formDataParser(body);
|
|
87
|
+
else if (isUrlencoded())
|
|
88
|
+
return urlEncodedParser(body);
|
|
89
|
+
else if (isBinary())
|
|
90
|
+
return textParser(body);
|
|
91
|
+
else
|
|
92
|
+
return textParser(body);
|
|
93
|
+
}));
|
|
94
|
+
function jsonParser(v) {
|
|
95
|
+
try {
|
|
96
|
+
return JSON.parse(v);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw new HttpError(400, e.message);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function textParser(v) {
|
|
103
|
+
return v;
|
|
104
|
+
}
|
|
105
|
+
function formDataParser(v) {
|
|
106
|
+
const boundary = '--' +
|
|
107
|
+
(/boundary=([^;]+)(?:;|$)/.exec(contentType || '') || [, ''])[1];
|
|
108
|
+
if (!boundary)
|
|
109
|
+
throw new HttpError(EHttpStatusCode.BadRequest, 'form-data boundary not recognized');
|
|
110
|
+
const parts = v.trim().split(boundary);
|
|
111
|
+
const result = {};
|
|
112
|
+
let key = '';
|
|
113
|
+
let partContentType = 'text/plain';
|
|
114
|
+
for (const part of parts) {
|
|
115
|
+
parsePart();
|
|
116
|
+
key = '';
|
|
117
|
+
partContentType = 'text/plain';
|
|
118
|
+
let valueMode = false;
|
|
119
|
+
const lines = part
|
|
120
|
+
.trim()
|
|
121
|
+
.split(/\n/g)
|
|
122
|
+
.map((s) => s.trim());
|
|
123
|
+
for (const line of lines) {
|
|
124
|
+
if (valueMode) {
|
|
125
|
+
if (!result[key]) {
|
|
126
|
+
result[key] = line;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
result[key] += '\n' + line;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
if (!line || line === '--') {
|
|
134
|
+
valueMode = !!key;
|
|
135
|
+
if (valueMode) {
|
|
136
|
+
key = key.replace(/^["']/, '').replace(/["']$/, '');
|
|
137
|
+
}
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (line
|
|
141
|
+
.toLowerCase()
|
|
142
|
+
.startsWith('content-disposition: form-data;')) {
|
|
143
|
+
key = (/name=([^;]+)/.exec(line) || [])[1];
|
|
144
|
+
if (!key)
|
|
145
|
+
throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read multipart name: ' + line);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (line.toLowerCase().startsWith('content-type:')) {
|
|
149
|
+
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
150
|
+
if (!partContentType)
|
|
151
|
+
throw new HttpError(EHttpStatusCode.BadRequest, 'Could not read content-type: ' + line);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
parsePart();
|
|
158
|
+
function parsePart() {
|
|
159
|
+
if (key) {
|
|
160
|
+
if (partContentType.indexOf('application/json') >= 0) {
|
|
161
|
+
result[key] = JSON.parse(result[key]);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
function urlEncodedParser(v) {
|
|
168
|
+
return new WooksURLSearchParams(v.trim()).toJson();
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
isJson,
|
|
172
|
+
isHtml,
|
|
173
|
+
isXml,
|
|
174
|
+
isText,
|
|
175
|
+
isBinary,
|
|
176
|
+
isFormData,
|
|
177
|
+
isUrlencoded,
|
|
178
|
+
isCompressed,
|
|
179
|
+
contentEncodings,
|
|
180
|
+
parseBody,
|
|
181
|
+
rawBody,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function registerBodyCompressor(name, compressor) {
|
|
185
|
+
if (compressors[name]) {
|
|
186
|
+
throw new Error(`Body compressor "${name}" already registered.`);
|
|
187
|
+
}
|
|
188
|
+
compressors[name] = compressor;
|
|
182
189
|
}
|
|
183
190
|
|
|
184
191
|
export { registerBodyCompressor, useBody };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-body",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10",
|
|
4
4
|
"description": "@wooksjs/http-body",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@wooksjs/event-http": "0.4.
|
|
34
|
+
"@wooksjs/event-http": "0.4.10"
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/http-body#readme"
|
|
37
37
|
}
|