@webqit/webflo 0.11.21 → 0.11.24
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/.gitignore +7 -7
- package/LICENSE +20 -20
- package/README.md +2074 -2071
- package/package.json +82 -82
- package/src/Context.js +79 -79
- package/src/config-pi/deployment/Env.js +69 -69
- package/src/config-pi/deployment/Layout.js +65 -65
- package/src/config-pi/deployment/Origins.js +133 -133
- package/src/config-pi/deployment/Virtualization.js +65 -65
- package/src/config-pi/deployment/index.js +17 -17
- package/src/config-pi/index.js +15 -15
- package/src/config-pi/runtime/Client.js +101 -101
- package/src/config-pi/runtime/Server.js +128 -128
- package/src/config-pi/runtime/client/Worker.js +135 -135
- package/src/config-pi/runtime/client/index.js +11 -11
- package/src/config-pi/runtime/index.js +17 -17
- package/src/config-pi/runtime/server/Headers.js +77 -77
- package/src/config-pi/runtime/server/Redirects.js +73 -73
- package/src/config-pi/runtime/server/index.js +13 -13
- package/src/config-pi/static/Manifest.js +321 -321
- package/src/config-pi/static/Ssg.js +51 -51
- package/src/config-pi/static/index.js +13 -13
- package/src/deployment-pi/index.js +10 -10
- package/src/deployment-pi/origins/index.js +215 -215
- package/src/index.js +19 -19
- package/src/runtime-pi/Router.js +131 -131
- package/src/runtime-pi/client/Context.js +6 -6
- package/src/runtime-pi/client/Router.js +47 -47
- package/src/runtime-pi/client/Runtime.js +357 -341
- package/src/runtime-pi/client/RuntimeClient.js +98 -98
- package/src/runtime-pi/client/Storage.js +56 -56
- package/src/runtime-pi/client/Url.js +205 -205
- package/src/runtime-pi/client/Workport.js +163 -163
- package/src/runtime-pi/client/generate.js +467 -467
- package/src/runtime-pi/client/index.js +23 -23
- package/src/runtime-pi/client/oohtml/full.js +6 -6
- package/src/runtime-pi/client/oohtml/namespacing.js +6 -6
- package/src/runtime-pi/client/oohtml/scripting.js +7 -7
- package/src/runtime-pi/client/oohtml/templating.js +7 -7
- package/src/runtime-pi/client/whatwag.js +27 -27
- package/src/runtime-pi/client/worker/Context.js +6 -6
- package/src/runtime-pi/client/worker/Worker.js +291 -291
- package/src/runtime-pi/client/worker/WorkerClient.js +46 -46
- package/src/runtime-pi/client/worker/Workport.js +79 -79
- package/src/runtime-pi/client/worker/index.js +23 -23
- package/src/runtime-pi/index.js +13 -13
- package/src/runtime-pi/server/Context.js +15 -15
- package/src/runtime-pi/server/Router.js +157 -157
- package/src/runtime-pi/server/Runtime.js +547 -547
- package/src/runtime-pi/server/RuntimeClient.js +112 -112
- package/src/runtime-pi/server/index.js +23 -23
- package/src/runtime-pi/server/whatwag.js +35 -35
- package/src/runtime-pi/util.js +162 -162
- package/src/runtime-pi/xFormData.js +59 -59
- package/src/runtime-pi/xHeaders.js +87 -87
- package/src/runtime-pi/xHttpEvent.js +92 -92
- package/src/runtime-pi/xHttpMessage.js +179 -179
- package/src/runtime-pi/xRequest.js +73 -73
- package/src/runtime-pi/xRequestHeaders.js +94 -94
- package/src/runtime-pi/xResponse.js +68 -68
- package/src/runtime-pi/xResponseHeaders.js +109 -109
- package/src/runtime-pi/xURL.js +110 -110
- package/src/runtime-pi/xfetch.js +6 -6
- package/src/services-pi/certbot/http-auth-hook.js +22 -22
- package/src/services-pi/certbot/http-cleanup-hook.js +22 -22
- package/src/services-pi/certbot/index.js +79 -79
- package/src/services-pi/index.js +8 -8
- package/src/static-pi/index.js +10 -10
- package/src/webflo.js +31 -31
- package/test/index.test.js +26 -25
- package/test/site/package.json +9 -9
- package/test/site/public/bundle.html +5 -5
- package/test/site/public/bundle.html.json +3 -3
- package/test/site/public/bundle.js +2 -2
- package/test/site/public/bundle.webflo.js +15 -15
- package/test/site/public/index.html +29 -29
- package/test/site/public/index1.html +34 -34
- package/test/site/public/page-2/bundle.html +4 -4
- package/test/site/public/page-2/bundle.js +2 -2
- package/test/site/public/page-2/index.html +45 -45
- package/test/site/public/page-2/main.html +2 -2
- package/test/site/public/page-4/subpage/bundle.js +2 -2
- package/test/site/public/page-4/subpage/index.html +30 -30
- package/test/site/public/sparoots.json +4 -4
- package/test/site/public/worker.js +3 -3
- package/test/site/server/index.js +15 -15
|
@@ -1,93 +1,93 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @imports
|
|
4
|
-
*/
|
|
5
|
-
import { _isEmpty } from '@webqit/util/js/index.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The xHttpEvent Mixin
|
|
9
|
-
*/
|
|
10
|
-
const xHttpEvent = (Request, Response, URL) => {
|
|
11
|
-
|
|
12
|
-
const HttpEvent = class {
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Initializes a new HttpEvent instance.
|
|
16
|
-
*
|
|
17
|
-
* @param Request _request
|
|
18
|
-
* @param Object _detail
|
|
19
|
-
* @param Object _sessionFactory
|
|
20
|
-
*/
|
|
21
|
-
constructor(_request, _detail, _sessionFactory) {
|
|
22
|
-
this._request = _request;
|
|
23
|
-
this._detail = _detail || {};
|
|
24
|
-
this._sessionFactory = _sessionFactory;
|
|
25
|
-
// -------
|
|
26
|
-
this.Request = Request;
|
|
27
|
-
this.Response = Response;
|
|
28
|
-
this.URL = URL;
|
|
29
|
-
// -------
|
|
30
|
-
this.port = {
|
|
31
|
-
listeners: [],
|
|
32
|
-
post(message) {
|
|
33
|
-
const promises = this.listeners.map(listener => listener(message))
|
|
34
|
-
.filter(returnValue => returnValue instanceof Promise);
|
|
35
|
-
if (process.length) return Promise.all(promises);
|
|
36
|
-
},
|
|
37
|
-
listen(listener) { this.listeners.push(listener); },
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// url
|
|
42
|
-
get url() {
|
|
43
|
-
if (!this._url) {
|
|
44
|
-
this._url = new this.URL(this._request.url);
|
|
45
|
-
}
|
|
46
|
-
return this._url;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// request
|
|
50
|
-
get request() {
|
|
51
|
-
return this._request;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// detail
|
|
55
|
-
get detail() {
|
|
56
|
-
return this._detail;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Session
|
|
60
|
-
get session() {
|
|
61
|
-
if (!this._session) {
|
|
62
|
-
this._session = this.sessionFactory().get();
|
|
63
|
-
}
|
|
64
|
-
return this._session;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Session factory
|
|
68
|
-
sessionFactory(...args) {
|
|
69
|
-
return this._sessionFactory(...args);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// RDR
|
|
73
|
-
retarget(url, init = {}) {
|
|
74
|
-
var request;
|
|
75
|
-
if (url instanceof Request) {
|
|
76
|
-
if (!_isEmpty(init)) {
|
|
77
|
-
request = new Request(url, init);
|
|
78
|
-
} else {
|
|
79
|
-
request = url;
|
|
80
|
-
}
|
|
81
|
-
} else {
|
|
82
|
-
request = new Request(this._request, init);
|
|
83
|
-
request.attrs.url = `${this.url.origin}${url}`;
|
|
84
|
-
request.attrs.referrer = this.request.url;
|
|
85
|
-
}
|
|
86
|
-
return new HttpEvent(request, this.detail);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
return HttpEvent;
|
|
91
|
-
}
|
|
92
|
-
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @imports
|
|
4
|
+
*/
|
|
5
|
+
import { _isEmpty } from '@webqit/util/js/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The xHttpEvent Mixin
|
|
9
|
+
*/
|
|
10
|
+
const xHttpEvent = (Request, Response, URL) => {
|
|
11
|
+
|
|
12
|
+
const HttpEvent = class {
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Initializes a new HttpEvent instance.
|
|
16
|
+
*
|
|
17
|
+
* @param Request _request
|
|
18
|
+
* @param Object _detail
|
|
19
|
+
* @param Object _sessionFactory
|
|
20
|
+
*/
|
|
21
|
+
constructor(_request, _detail, _sessionFactory) {
|
|
22
|
+
this._request = _request;
|
|
23
|
+
this._detail = _detail || {};
|
|
24
|
+
this._sessionFactory = _sessionFactory;
|
|
25
|
+
// -------
|
|
26
|
+
this.Request = Request;
|
|
27
|
+
this.Response = Response;
|
|
28
|
+
this.URL = URL;
|
|
29
|
+
// -------
|
|
30
|
+
this.port = {
|
|
31
|
+
listeners: [],
|
|
32
|
+
post(message) {
|
|
33
|
+
const promises = this.listeners.map(listener => listener(message))
|
|
34
|
+
.filter(returnValue => returnValue instanceof Promise);
|
|
35
|
+
if (process.length) return Promise.all(promises);
|
|
36
|
+
},
|
|
37
|
+
listen(listener) { this.listeners.push(listener); },
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// url
|
|
42
|
+
get url() {
|
|
43
|
+
if (!this._url) {
|
|
44
|
+
this._url = new this.URL(this._request.url);
|
|
45
|
+
}
|
|
46
|
+
return this._url;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// request
|
|
50
|
+
get request() {
|
|
51
|
+
return this._request;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// detail
|
|
55
|
+
get detail() {
|
|
56
|
+
return this._detail;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Session
|
|
60
|
+
get session() {
|
|
61
|
+
if (!this._session) {
|
|
62
|
+
this._session = this.sessionFactory().get();
|
|
63
|
+
}
|
|
64
|
+
return this._session;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Session factory
|
|
68
|
+
sessionFactory(...args) {
|
|
69
|
+
return this._sessionFactory(...args);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// RDR
|
|
73
|
+
retarget(url, init = {}) {
|
|
74
|
+
var request;
|
|
75
|
+
if (url instanceof Request) {
|
|
76
|
+
if (!_isEmpty(init)) {
|
|
77
|
+
request = new Request(url, init);
|
|
78
|
+
} else {
|
|
79
|
+
request = url;
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
request = new Request(this._request, init);
|
|
83
|
+
request.attrs.url = `${this.url.origin}${url}`;
|
|
84
|
+
request.attrs.referrer = this.request.url;
|
|
85
|
+
}
|
|
86
|
+
return new HttpEvent(request, this.detail);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
return HttpEvent;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
93
|
export default xHttpEvent;
|
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @imports
|
|
4
|
-
*/
|
|
5
|
-
import { _isArray, _isEmpty, _isNumber, _isObject, _isPlainArray, _isPlainObject, _isString } from '@webqit/util/js/index.js';
|
|
6
|
-
import { formDataType } from './xFormData.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The _Request Mixin
|
|
10
|
-
*/
|
|
11
|
-
const xHttpMessage = (whatwagHttpMessage, Headers, FormData) => {
|
|
12
|
-
const HttpMessage = class extends whatwagHttpMessage {
|
|
13
|
-
|
|
14
|
-
constructor(input, init, bodyAttrs) {
|
|
15
|
-
if (('headers' in init) && !(init.headers instanceof Headers)) {
|
|
16
|
-
init = { ...init };
|
|
17
|
-
init.headers = new Headers(init.headers);
|
|
18
|
-
arguments[1] = init;
|
|
19
|
-
}
|
|
20
|
-
if (_isEmpty(init)) {
|
|
21
|
-
super(input);
|
|
22
|
-
} else {
|
|
23
|
-
super(input, init);
|
|
24
|
-
}
|
|
25
|
-
this._headers = init.headers;
|
|
26
|
-
if (bodyAttrs.headers) {
|
|
27
|
-
this.headers.json(bodyAttrs.headers);
|
|
28
|
-
}
|
|
29
|
-
let attrs = {};
|
|
30
|
-
Object.defineProperty(this, 'attrs', { get: () => attrs });
|
|
31
|
-
Object.defineProperty(this, 'bodyAttrs', { get: () => bodyAttrs });
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
clone() {
|
|
35
|
-
return new this.constructor(super.clone());
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get headers() {
|
|
39
|
-
if (!this._headers) {
|
|
40
|
-
this._headers = new Headers(super.headers);
|
|
41
|
-
}
|
|
42
|
-
return this._headers;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get attrs() {
|
|
46
|
-
return this.attrs;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get url() {
|
|
50
|
-
return 'url' in this.attrs ? this.attrs.url : super.url;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async arrayBuffer() {
|
|
54
|
-
if (this.bodyAttrs.arrayBuffer) {
|
|
55
|
-
return this.bodyAttrs.arrayBuffer;
|
|
56
|
-
}
|
|
57
|
-
return super.arrayBuffer();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async blob() {
|
|
61
|
-
if (this.bodyAttrs.blob) {
|
|
62
|
-
return this.bodyAttrs.blob;
|
|
63
|
-
}
|
|
64
|
-
return super.blob();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async formData() {
|
|
68
|
-
if (this.bodyAttrs.formData) {
|
|
69
|
-
return this.bodyAttrs.formData;
|
|
70
|
-
}
|
|
71
|
-
const formData = await super.formData();
|
|
72
|
-
formData.tee = FormData.prototype.tee.bind(formData);
|
|
73
|
-
formData.json = FormData.prototype.json.bind(formData);
|
|
74
|
-
return formData;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async json() {
|
|
78
|
-
if (this.bodyAttrs.json) {
|
|
79
|
-
return this.bodyAttrs.json;
|
|
80
|
-
}
|
|
81
|
-
return super.json();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async text() {
|
|
85
|
-
if (this.bodyAttrs.text) {
|
|
86
|
-
return this.bodyAttrs.text;
|
|
87
|
-
}
|
|
88
|
-
return super.text();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Resolve
|
|
92
|
-
jsonfy(force = false) {
|
|
93
|
-
if (!this.bodyAttrs.jsonfied || force) {
|
|
94
|
-
this.bodyAttrs.jsonfied = new Promise(async (resolve, reject) => {
|
|
95
|
-
var messageInstance = this, jsonfied, contentType = messageInstance.headers.get('content-type') || '';
|
|
96
|
-
var type = contentType === 'application/json' || this.bodyAttrs.json ? 'json' : (
|
|
97
|
-
contentType === 'application/x-www-form-urlencoded' || contentType.startsWith('multipart/form-data') || this.bodyAttrs.formData ? 'formData' : (
|
|
98
|
-
contentType === 'text/plain' ? 'plain' : 'other'
|
|
99
|
-
)
|
|
100
|
-
);
|
|
101
|
-
try {
|
|
102
|
-
if (type === 'formData') {
|
|
103
|
-
jsonfied = (await messageInstance.formData()).json();
|
|
104
|
-
} else {
|
|
105
|
-
jsonfied = type === 'json' ? await messageInstance.json() : (
|
|
106
|
-
type === 'plain' ? await messageInstance.text() : messageInstance.body
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
resolve(jsonfied);
|
|
110
|
-
} catch(e) {
|
|
111
|
-
reject(e);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
return this.bodyAttrs.jsonfied;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
};
|
|
119
|
-
// ----------
|
|
120
|
-
HttpMessage.Headers = Headers;
|
|
121
|
-
// ----------
|
|
122
|
-
return HttpMessage;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export default xHttpMessage;
|
|
126
|
-
export function encodeBody(body, FormData, Blob) {
|
|
127
|
-
const detailsObj = { body, input: body };
|
|
128
|
-
const encodeFormData = (detailsObj, formData) => {
|
|
129
|
-
if (!FormData.encode) return;
|
|
130
|
-
let [ body, headers ] = FormData.encode(formData);
|
|
131
|
-
detailsObj.body = body;
|
|
132
|
-
detailsObj.headers = headers;
|
|
133
|
-
}
|
|
134
|
-
if (_isString(body) || _isNumber(body)) {
|
|
135
|
-
detailsObj.inputType = 'text';
|
|
136
|
-
detailsObj.text = body;
|
|
137
|
-
detailsObj.headers = {
|
|
138
|
-
contentLength: (body + '').length,
|
|
139
|
-
};
|
|
140
|
-
return detailsObj;
|
|
141
|
-
}
|
|
142
|
-
detailsObj.inputType = formDataType(body);
|
|
143
|
-
if ([ 'Blob', 'File' ].includes(detailsObj.inputType)) {
|
|
144
|
-
detailsObj.blob = body;
|
|
145
|
-
detailsObj.headers = {
|
|
146
|
-
contentType: body.type,
|
|
147
|
-
contentLength: body.size,
|
|
148
|
-
};
|
|
149
|
-
} else if ([ 'Uint8Array', 'Uint16Array', 'Uint32Array', 'ArrayBuffer' ].includes(detailsObj.inputType)) {
|
|
150
|
-
detailsObj.arrayBuffer = body;
|
|
151
|
-
detailsObj.headers = {
|
|
152
|
-
contentLength: body.byteLength,
|
|
153
|
-
};
|
|
154
|
-
} else if (detailsObj.inputType === 'FormData') {
|
|
155
|
-
detailsObj.formData = body;
|
|
156
|
-
encodeFormData(detailsObj, body);
|
|
157
|
-
} else if ((_isObject(body) && _isPlainObject(body)) || (_isArray(body) && _isPlainArray(body))) {
|
|
158
|
-
detailsObj.inputType = 'object';
|
|
159
|
-
// Deserialize object while detecting if multipart
|
|
160
|
-
var hasBlobs, formData = new FormData;
|
|
161
|
-
formData.json(body, (path, value, objectType) => {
|
|
162
|
-
hasBlobs = hasBlobs || objectType;
|
|
163
|
-
return true;
|
|
164
|
-
});
|
|
165
|
-
if (hasBlobs) {
|
|
166
|
-
detailsObj.formData = formData;
|
|
167
|
-
encodeFormData(detailsObj, formData);
|
|
168
|
-
} else {
|
|
169
|
-
detailsObj.json = body;
|
|
170
|
-
detailsObj.body = JSON.stringify(body);
|
|
171
|
-
detailsObj.headers = {
|
|
172
|
-
contentType: 'application/json',
|
|
173
|
-
contentLength: (new Blob([ detailsObj.body ])).size, // Buffer.byteLength(detailsObj.body, 'utf8') isn't cross-environment
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
detailsObj.jsonfied = body;
|
|
177
|
-
}
|
|
178
|
-
return detailsObj;
|
|
179
|
-
}
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @imports
|
|
4
|
+
*/
|
|
5
|
+
import { _isArray, _isEmpty, _isNumber, _isObject, _isPlainArray, _isPlainObject, _isString } from '@webqit/util/js/index.js';
|
|
6
|
+
import { formDataType } from './xFormData.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The _Request Mixin
|
|
10
|
+
*/
|
|
11
|
+
const xHttpMessage = (whatwagHttpMessage, Headers, FormData) => {
|
|
12
|
+
const HttpMessage = class extends whatwagHttpMessage {
|
|
13
|
+
|
|
14
|
+
constructor(input, init, bodyAttrs) {
|
|
15
|
+
if (('headers' in init) && !(init.headers instanceof Headers)) {
|
|
16
|
+
init = { ...init };
|
|
17
|
+
init.headers = new Headers(init.headers);
|
|
18
|
+
arguments[1] = init;
|
|
19
|
+
}
|
|
20
|
+
if (_isEmpty(init)) {
|
|
21
|
+
super(input);
|
|
22
|
+
} else {
|
|
23
|
+
super(input, init);
|
|
24
|
+
}
|
|
25
|
+
this._headers = init.headers;
|
|
26
|
+
if (bodyAttrs.headers) {
|
|
27
|
+
this.headers.json(bodyAttrs.headers);
|
|
28
|
+
}
|
|
29
|
+
let attrs = {};
|
|
30
|
+
Object.defineProperty(this, 'attrs', { get: () => attrs });
|
|
31
|
+
Object.defineProperty(this, 'bodyAttrs', { get: () => bodyAttrs });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
clone() {
|
|
35
|
+
return new this.constructor(super.clone());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get headers() {
|
|
39
|
+
if (!this._headers) {
|
|
40
|
+
this._headers = new Headers(super.headers);
|
|
41
|
+
}
|
|
42
|
+
return this._headers;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get attrs() {
|
|
46
|
+
return this.attrs;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get url() {
|
|
50
|
+
return 'url' in this.attrs ? this.attrs.url : super.url;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async arrayBuffer() {
|
|
54
|
+
if (this.bodyAttrs.arrayBuffer) {
|
|
55
|
+
return this.bodyAttrs.arrayBuffer;
|
|
56
|
+
}
|
|
57
|
+
return super.arrayBuffer();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async blob() {
|
|
61
|
+
if (this.bodyAttrs.blob) {
|
|
62
|
+
return this.bodyAttrs.blob;
|
|
63
|
+
}
|
|
64
|
+
return super.blob();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async formData() {
|
|
68
|
+
if (this.bodyAttrs.formData) {
|
|
69
|
+
return this.bodyAttrs.formData;
|
|
70
|
+
}
|
|
71
|
+
const formData = await super.formData();
|
|
72
|
+
formData.tee = FormData.prototype.tee.bind(formData);
|
|
73
|
+
formData.json = FormData.prototype.json.bind(formData);
|
|
74
|
+
return formData;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async json() {
|
|
78
|
+
if (this.bodyAttrs.json) {
|
|
79
|
+
return this.bodyAttrs.json;
|
|
80
|
+
}
|
|
81
|
+
return super.json();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async text() {
|
|
85
|
+
if (this.bodyAttrs.text) {
|
|
86
|
+
return this.bodyAttrs.text;
|
|
87
|
+
}
|
|
88
|
+
return super.text();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Resolve
|
|
92
|
+
jsonfy(force = false) {
|
|
93
|
+
if (!this.bodyAttrs.jsonfied || force) {
|
|
94
|
+
this.bodyAttrs.jsonfied = new Promise(async (resolve, reject) => {
|
|
95
|
+
var messageInstance = this, jsonfied, contentType = messageInstance.headers.get('content-type') || '';
|
|
96
|
+
var type = contentType === 'application/json' || this.bodyAttrs.json ? 'json' : (
|
|
97
|
+
contentType === 'application/x-www-form-urlencoded' || contentType.startsWith('multipart/form-data') || this.bodyAttrs.formData ? 'formData' : (
|
|
98
|
+
contentType === 'text/plain' ? 'plain' : 'other'
|
|
99
|
+
)
|
|
100
|
+
);
|
|
101
|
+
try {
|
|
102
|
+
if (type === 'formData') {
|
|
103
|
+
jsonfied = (await messageInstance.formData()).json();
|
|
104
|
+
} else {
|
|
105
|
+
jsonfied = type === 'json' ? await messageInstance.json() : (
|
|
106
|
+
type === 'plain' ? await messageInstance.text() : messageInstance.body
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
resolve(jsonfied);
|
|
110
|
+
} catch(e) {
|
|
111
|
+
reject(e);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return this.bodyAttrs.jsonfied;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
};
|
|
119
|
+
// ----------
|
|
120
|
+
HttpMessage.Headers = Headers;
|
|
121
|
+
// ----------
|
|
122
|
+
return HttpMessage;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export default xHttpMessage;
|
|
126
|
+
export function encodeBody(body, FormData, Blob) {
|
|
127
|
+
const detailsObj = { body, input: body };
|
|
128
|
+
const encodeFormData = (detailsObj, formData) => {
|
|
129
|
+
if (!FormData.encode) return;
|
|
130
|
+
let [ body, headers ] = FormData.encode(formData);
|
|
131
|
+
detailsObj.body = body;
|
|
132
|
+
detailsObj.headers = headers;
|
|
133
|
+
}
|
|
134
|
+
if (_isString(body) || _isNumber(body)) {
|
|
135
|
+
detailsObj.inputType = 'text';
|
|
136
|
+
detailsObj.text = body;
|
|
137
|
+
detailsObj.headers = {
|
|
138
|
+
contentLength: (body + '').length,
|
|
139
|
+
};
|
|
140
|
+
return detailsObj;
|
|
141
|
+
}
|
|
142
|
+
detailsObj.inputType = formDataType(body);
|
|
143
|
+
if ([ 'Blob', 'File' ].includes(detailsObj.inputType)) {
|
|
144
|
+
detailsObj.blob = body;
|
|
145
|
+
detailsObj.headers = {
|
|
146
|
+
contentType: body.type,
|
|
147
|
+
contentLength: body.size,
|
|
148
|
+
};
|
|
149
|
+
} else if ([ 'Uint8Array', 'Uint16Array', 'Uint32Array', 'ArrayBuffer' ].includes(detailsObj.inputType)) {
|
|
150
|
+
detailsObj.arrayBuffer = body;
|
|
151
|
+
detailsObj.headers = {
|
|
152
|
+
contentLength: body.byteLength,
|
|
153
|
+
};
|
|
154
|
+
} else if (detailsObj.inputType === 'FormData') {
|
|
155
|
+
detailsObj.formData = body;
|
|
156
|
+
encodeFormData(detailsObj, body);
|
|
157
|
+
} else if ((_isObject(body) && _isPlainObject(body)) || (_isArray(body) && _isPlainArray(body))) {
|
|
158
|
+
detailsObj.inputType = 'object';
|
|
159
|
+
// Deserialize object while detecting if multipart
|
|
160
|
+
var hasBlobs, formData = new FormData;
|
|
161
|
+
formData.json(body, (path, value, objectType) => {
|
|
162
|
+
hasBlobs = hasBlobs || objectType;
|
|
163
|
+
return true;
|
|
164
|
+
});
|
|
165
|
+
if (hasBlobs) {
|
|
166
|
+
detailsObj.formData = formData;
|
|
167
|
+
encodeFormData(detailsObj, formData);
|
|
168
|
+
} else {
|
|
169
|
+
detailsObj.json = body;
|
|
170
|
+
detailsObj.body = JSON.stringify(body);
|
|
171
|
+
detailsObj.headers = {
|
|
172
|
+
contentType: 'application/json',
|
|
173
|
+
contentLength: (new Blob([ detailsObj.body ])).size, // Buffer.byteLength(detailsObj.body, 'utf8') isn't cross-environment
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
detailsObj.jsonfied = body;
|
|
177
|
+
}
|
|
178
|
+
return detailsObj;
|
|
179
|
+
}
|