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