@univerjs/network 0.5.4 → 0.5.5-experimental.20250123-34738ff
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/lib/cjs/facade.js +1 -1
- package/lib/cjs/index.js +2 -2
- package/lib/es/facade.js +57 -53
- package/lib/es/index.js +468 -451
- package/lib/types/facade/f-network.d.ts +29 -22
- package/lib/types/facade/f-univer.d.ts +3 -0
- package/lib/umd/facade.js +1 -1
- package/lib/umd/index.js +2 -2
- package/package.json +4 -4
- package/LICENSE +0 -176
- package/lib/types/services/http/interceptors/__tests__/auth-interceptor.spec.d.ts +0 -16
- package/lib/types/services/http/interceptors/__tests__/merge-interceptor.spec.d.ts +0 -16
- package/lib/types/services/http/interceptors/__tests__/retry-interceptor.spec.d.ts +0 -16
- package/lib/types/services/http/interceptors/__tests__/threshold-interceptor.spec.d.ts +0 -16
package/lib/es/index.js
CHANGED
|
@@ -1,67 +1,69 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
class
|
|
9
|
-
constructor(
|
|
10
|
-
|
|
11
|
-
typeof
|
|
12
|
-
}
|
|
13
|
-
forEach(
|
|
14
|
-
this._headers.forEach((
|
|
15
|
-
}
|
|
16
|
-
has(
|
|
17
|
-
return !!this._headers.has(
|
|
18
|
-
}
|
|
19
|
-
get(
|
|
20
|
-
const
|
|
21
|
-
return this._headers.has(
|
|
22
|
-
}
|
|
23
|
-
set(
|
|
24
|
-
this._setHeader(
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
|
|
5
|
+
import { createIdentifier, Disposable, toDisposable, remove, ILogService, Inject, Injector, Plugin, Quantity, LookUp, registerDependencies, mergeOverrideWithDependencies, DisposableCollection } from "@univerjs/core";
|
|
6
|
+
import { of, firstValueFrom, Observable, catchError, throwError } from "rxjs";
|
|
7
|
+
import { concatMap, share, retry } from "rxjs/operators";
|
|
8
|
+
const ApplicationJSONType = "application/json", _HTTPHeaders = class _HTTPHeaders {
|
|
9
|
+
constructor(headers) {
|
|
10
|
+
__publicField(this, "_headers", /* @__PURE__ */ new Map());
|
|
11
|
+
typeof headers == "string" ? this._handleHeadersString(headers) : headers instanceof Headers ? this._handleHeaders(headers) : headers && this._handleHeadersConstructorProps(headers);
|
|
12
|
+
}
|
|
13
|
+
forEach(callback) {
|
|
14
|
+
this._headers.forEach((v, key) => callback(key, v));
|
|
15
|
+
}
|
|
16
|
+
has(key) {
|
|
17
|
+
return !!this._headers.has(key.toLowerCase());
|
|
18
|
+
}
|
|
19
|
+
get(key) {
|
|
20
|
+
const k = key.toLowerCase();
|
|
21
|
+
return this._headers.has(k) ? this._headers.get(k) : null;
|
|
22
|
+
}
|
|
23
|
+
set(key, value) {
|
|
24
|
+
this._setHeader(key, value);
|
|
25
25
|
}
|
|
26
26
|
toHeadersInit() {
|
|
27
|
-
var
|
|
28
|
-
const
|
|
29
|
-
return this._headers.forEach((
|
|
30
|
-
|
|
31
|
-
}), (
|
|
32
|
-
}
|
|
33
|
-
_setHeader(
|
|
34
|
-
const
|
|
35
|
-
this._headers.has(
|
|
36
|
-
}
|
|
37
|
-
_handleHeadersString(
|
|
38
|
-
|
|
39
|
-
`).forEach((
|
|
40
|
-
const [
|
|
41
|
-
|
|
27
|
+
var _a5, _b;
|
|
28
|
+
const headers = {};
|
|
29
|
+
return this._headers.forEach((values, key) => {
|
|
30
|
+
headers[key] = values.join(",");
|
|
31
|
+
}), (_a5 = headers.accept) != null || (headers.accept = "application/json, text/plain, */*"), (_b = headers["content-type"]) != null || (headers["content-type"] = "application/json;charset=UTF-8"), headers;
|
|
32
|
+
}
|
|
33
|
+
_setHeader(name, value) {
|
|
34
|
+
const lowerCase = name.toLowerCase();
|
|
35
|
+
this._headers.has(lowerCase) ? this._headers.get(lowerCase).push(value.toString()) : this._headers.set(lowerCase, [value.toString()]);
|
|
36
|
+
}
|
|
37
|
+
_handleHeadersString(headers) {
|
|
38
|
+
headers.split(`
|
|
39
|
+
`).forEach((header) => {
|
|
40
|
+
const [name, value] = header.split(":");
|
|
41
|
+
name && value && this._setHeader(name, value);
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
|
-
_handleHeadersConstructorProps(
|
|
45
|
-
Object.entries(
|
|
44
|
+
_handleHeadersConstructorProps(headers) {
|
|
45
|
+
Object.entries(headers).forEach(([name, value]) => this._setHeader(name, value));
|
|
46
46
|
}
|
|
47
|
-
_handleHeaders(
|
|
48
|
-
|
|
47
|
+
_handleHeaders(headers) {
|
|
48
|
+
headers.forEach((value, name) => this._setHeader(name, value));
|
|
49
49
|
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
};
|
|
51
|
+
__name(_HTTPHeaders, "HTTPHeaders");
|
|
52
|
+
let HTTPHeaders = _HTTPHeaders;
|
|
53
|
+
const IHTTPImplementation = createIdentifier("network.http-implementation"), _HTTPParams = class _HTTPParams {
|
|
54
|
+
constructor(params) {
|
|
55
|
+
this.params = params;
|
|
55
56
|
}
|
|
56
57
|
toString() {
|
|
57
|
-
return this.params ? Object.keys(this.params).map((
|
|
58
|
+
return this.params ? Object.keys(this.params).map((key) => `${key}=${this.params[key]}`).join("&") : "";
|
|
58
59
|
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
this
|
|
60
|
+
};
|
|
61
|
+
__name(_HTTPParams, "HTTPParams");
|
|
62
|
+
let HTTPParams = _HTTPParams, HTTPRequestUID = 0;
|
|
63
|
+
const _HTTPRequest = class _HTTPRequest {
|
|
64
|
+
constructor(method, url, requestParams) {
|
|
65
|
+
__publicField(this, "uid", HTTPRequestUID++);
|
|
66
|
+
this.method = method, this.url = url, this.requestParams = requestParams;
|
|
65
67
|
}
|
|
66
68
|
get headers() {
|
|
67
69
|
return this.requestParams.headers;
|
|
@@ -73,31 +75,33 @@ class G {
|
|
|
73
75
|
return this.requestParams.responseType;
|
|
74
76
|
}
|
|
75
77
|
getUrlWithParams() {
|
|
76
|
-
var
|
|
77
|
-
const
|
|
78
|
-
return
|
|
78
|
+
var _a5, _b;
|
|
79
|
+
const params = (_b = (_a5 = this.requestParams) == null ? void 0 : _a5.params) == null ? void 0 : _b.toString();
|
|
80
|
+
return params ? `${this.url}${this.url.includes("?") ? "&" : "?"}${params}` : this.url;
|
|
79
81
|
}
|
|
80
82
|
getBody() {
|
|
81
|
-
var
|
|
82
|
-
const
|
|
83
|
-
return
|
|
83
|
+
var _a5, _b;
|
|
84
|
+
const contentType = (_a5 = this.headers.get("Content-Type")) != null ? _a5 : ApplicationJSONType, body = (_b = this.requestParams) == null ? void 0 : _b.body;
|
|
85
|
+
return contentType === ApplicationJSONType && body && typeof body == "object" ? JSON.stringify(body) : body ? `${body}` : null;
|
|
84
86
|
}
|
|
85
87
|
getHeadersInit() {
|
|
86
88
|
return this.headers.toHeadersInit();
|
|
87
89
|
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
90
|
+
};
|
|
91
|
+
__name(_HTTPRequest, "HTTPRequest");
|
|
92
|
+
let HTTPRequest = _HTTPRequest;
|
|
93
|
+
var __defProp$3 = Object.defineProperty, __getOwnPropDesc$3 = Object.getOwnPropertyDescriptor, __decorateClass$3 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
|
|
94
|
+
for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$3(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
|
|
95
|
+
(decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
|
|
96
|
+
return kind && result && __defProp$3(target, key, result), result;
|
|
97
|
+
}, "__decorateClass$3"), __decorateParam$3 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$3"), _a;
|
|
98
|
+
let HTTPService = (_a = class extends Disposable {
|
|
99
|
+
constructor(_http) {
|
|
96
100
|
super();
|
|
97
|
-
|
|
101
|
+
__publicField(this, "_interceptors", []);
|
|
98
102
|
// eslint-disable-next-line ts/no-explicit-any
|
|
99
|
-
|
|
100
|
-
this._http =
|
|
103
|
+
__publicField(this, "_pipe");
|
|
104
|
+
this._http = _http;
|
|
101
105
|
}
|
|
102
106
|
/**
|
|
103
107
|
* Register an HTTP interceptor.
|
|
@@ -105,479 +109,492 @@ let x = class extends z {
|
|
|
105
109
|
* @param interceptor the http interceptor
|
|
106
110
|
* @returns a disposable handler to remove the interceptor
|
|
107
111
|
*/
|
|
108
|
-
registerHTTPInterceptor(
|
|
109
|
-
if (this._interceptors.indexOf(
|
|
112
|
+
registerHTTPInterceptor(interceptor) {
|
|
113
|
+
if (this._interceptors.indexOf(interceptor) !== -1)
|
|
110
114
|
throw new Error("[HTTPService]: The interceptor has already been registered!");
|
|
111
|
-
return this._interceptors.push(
|
|
112
|
-
var
|
|
113
|
-
return ((
|
|
114
|
-
}), this._pipe = null,
|
|
115
|
+
return this._interceptors.push(interceptor), this._interceptors = this._interceptors.sort((a, b) => {
|
|
116
|
+
var _a5, _b;
|
|
117
|
+
return ((_a5 = a.priority) != null ? _a5 : 0) - ((_b = b.priority) != null ? _b : 0);
|
|
118
|
+
}), this._pipe = null, toDisposable(() => remove(this._interceptors, interceptor));
|
|
115
119
|
}
|
|
116
|
-
get(
|
|
117
|
-
return this._request("GET",
|
|
120
|
+
get(url, params) {
|
|
121
|
+
return this._request("GET", url, params);
|
|
118
122
|
}
|
|
119
|
-
post(
|
|
120
|
-
return this._request("POST",
|
|
123
|
+
post(url, params) {
|
|
124
|
+
return this._request("POST", url, params);
|
|
121
125
|
}
|
|
122
|
-
put(
|
|
123
|
-
return this._request("PUT",
|
|
126
|
+
put(url, params) {
|
|
127
|
+
return this._request("PUT", url, params);
|
|
124
128
|
}
|
|
125
|
-
delete(
|
|
126
|
-
return this._request("DELETE",
|
|
129
|
+
delete(url, params) {
|
|
130
|
+
return this._request("DELETE", url, params);
|
|
127
131
|
}
|
|
128
|
-
patch(
|
|
129
|
-
return this._request("PATCH",
|
|
132
|
+
patch(url, params) {
|
|
133
|
+
return this._request("PATCH", url, params);
|
|
130
134
|
}
|
|
131
|
-
getSSE(
|
|
132
|
-
var
|
|
133
|
-
const
|
|
134
|
-
headers
|
|
135
|
-
params
|
|
136
|
-
withCredentials: (
|
|
135
|
+
getSSE(method, url, _params) {
|
|
136
|
+
var _a5, _b;
|
|
137
|
+
const headers = new HTTPHeaders(_params == null ? void 0 : _params.headers), params = new HTTPParams(_params == null ? void 0 : _params.params), request = new HTTPRequest(method, url, {
|
|
138
|
+
headers,
|
|
139
|
+
params,
|
|
140
|
+
withCredentials: (_a5 = _params == null ? void 0 : _params.withCredentials) != null ? _a5 : !1,
|
|
137
141
|
reportProgress: !0,
|
|
138
|
-
responseType: (
|
|
139
|
-
body: ["GET", "DELETE"].includes(
|
|
142
|
+
responseType: (_b = _params == null ? void 0 : _params.responseType) != null ? _b : "json",
|
|
143
|
+
body: ["GET", "DELETE"].includes(method) || _params == null ? void 0 : _params.body
|
|
140
144
|
});
|
|
141
|
-
return
|
|
145
|
+
return of(request).pipe(concatMap((request2) => this._runInterceptorsAndImplementation(request2)));
|
|
142
146
|
}
|
|
143
147
|
/** The HTTP request implementations */
|
|
144
|
-
async _request(
|
|
145
|
-
var
|
|
146
|
-
const
|
|
147
|
-
headers
|
|
148
|
-
params
|
|
149
|
-
withCredentials: (
|
|
148
|
+
async _request(method, url, options) {
|
|
149
|
+
var _a5, _b;
|
|
150
|
+
const headers = new HTTPHeaders(options == null ? void 0 : options.headers), params = new HTTPParams(options == null ? void 0 : options.params), request = new HTTPRequest(method, url, {
|
|
151
|
+
headers,
|
|
152
|
+
params,
|
|
153
|
+
withCredentials: (_a5 = options == null ? void 0 : options.withCredentials) != null ? _a5 : !1,
|
|
150
154
|
// default value for withCredentials is false by MDN
|
|
151
|
-
responseType: (
|
|
152
|
-
body: ["GET", "DELETE"].includes(
|
|
153
|
-
}),
|
|
154
|
-
|
|
155
|
+
responseType: (_b = options == null ? void 0 : options.responseType) != null ? _b : "json",
|
|
156
|
+
body: ["GET", "DELETE"].includes(method) || options == null ? void 0 : options.body
|
|
157
|
+
}), events$ = of(request).pipe(
|
|
158
|
+
concatMap((request2) => this._runInterceptorsAndImplementation(request2))
|
|
155
159
|
);
|
|
156
|
-
return await
|
|
160
|
+
return await firstValueFrom(events$);
|
|
157
161
|
}
|
|
158
162
|
// eslint-disable-next-line ts/no-explicit-any
|
|
159
|
-
_runInterceptorsAndImplementation(
|
|
160
|
-
return this._pipe || (this._pipe = this._interceptors.map((
|
|
161
|
-
(
|
|
162
|
-
(
|
|
163
|
+
_runInterceptorsAndImplementation(request) {
|
|
164
|
+
return this._pipe || (this._pipe = this._interceptors.map((handler) => handler.interceptor).reduceRight(
|
|
165
|
+
(nextHandlerFunction, interceptorFunction) => chainInterceptorFn(nextHandlerFunction, interceptorFunction),
|
|
166
|
+
(requestFromPrevInterceptor, finalHandler) => finalHandler(requestFromPrevInterceptor)
|
|
163
167
|
)), this._pipe(
|
|
164
|
-
|
|
165
|
-
(
|
|
168
|
+
request,
|
|
169
|
+
(requestToNext) => this._http.send(requestToNext)
|
|
166
170
|
/* final handler */
|
|
167
171
|
);
|
|
168
172
|
}
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
],
|
|
173
|
-
function
|
|
174
|
-
return (
|
|
173
|
+
}, __name(_a, "HTTPService"), _a);
|
|
174
|
+
HTTPService = __decorateClass$3([
|
|
175
|
+
__decorateParam$3(0, IHTTPImplementation)
|
|
176
|
+
], HTTPService);
|
|
177
|
+
function chainInterceptorFn(afterInterceptorChain, currentInterceptorFn) {
|
|
178
|
+
return (prevRequest, nextHandlerFn) => currentInterceptorFn(prevRequest, (nextRequest) => afterInterceptorChain(nextRequest, nextHandlerFn));
|
|
175
179
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
180
|
+
__name(chainInterceptorFn, "chainInterceptorFn");
|
|
181
|
+
const SuccessStatusCodeLowerBound = 200, ErrorStatusCodeLowerBound = 300;
|
|
182
|
+
var HTTPStatusCode = /* @__PURE__ */ ((HTTPStatusCode2) => (HTTPStatusCode2[HTTPStatusCode2.Continue = 100] = "Continue", HTTPStatusCode2[HTTPStatusCode2.SwitchingProtocols = 101] = "SwitchingProtocols", HTTPStatusCode2[HTTPStatusCode2.Processing = 102] = "Processing", HTTPStatusCode2[HTTPStatusCode2.EarlyHints = 103] = "EarlyHints", HTTPStatusCode2[HTTPStatusCode2.Ok = 200] = "Ok", HTTPStatusCode2[HTTPStatusCode2.Created = 201] = "Created", HTTPStatusCode2[HTTPStatusCode2.Accepted = 202] = "Accepted", HTTPStatusCode2[HTTPStatusCode2.NonAuthoritativeInformation = 203] = "NonAuthoritativeInformation", HTTPStatusCode2[HTTPStatusCode2.NoContent = 204] = "NoContent", HTTPStatusCode2[HTTPStatusCode2.ResetContent = 205] = "ResetContent", HTTPStatusCode2[HTTPStatusCode2.PartialContent = 206] = "PartialContent", HTTPStatusCode2[HTTPStatusCode2.MultiStatus = 207] = "MultiStatus", HTTPStatusCode2[HTTPStatusCode2.AlreadyReported = 208] = "AlreadyReported", HTTPStatusCode2[HTTPStatusCode2.ImUsed = 226] = "ImUsed", HTTPStatusCode2[HTTPStatusCode2.MultipleChoices = 300] = "MultipleChoices", HTTPStatusCode2[HTTPStatusCode2.MovedPermanently = 301] = "MovedPermanently", HTTPStatusCode2[HTTPStatusCode2.Found = 302] = "Found", HTTPStatusCode2[HTTPStatusCode2.SeeOther = 303] = "SeeOther", HTTPStatusCode2[HTTPStatusCode2.NotModified = 304] = "NotModified", HTTPStatusCode2[HTTPStatusCode2.UseProxy = 305] = "UseProxy", HTTPStatusCode2[HTTPStatusCode2.Unused = 306] = "Unused", HTTPStatusCode2[HTTPStatusCode2.TemporaryRedirect = 307] = "TemporaryRedirect", HTTPStatusCode2[HTTPStatusCode2.PermanentRedirect = 308] = "PermanentRedirect", HTTPStatusCode2[HTTPStatusCode2.BadRequest = 400] = "BadRequest", HTTPStatusCode2[HTTPStatusCode2.Unauthorized = 401] = "Unauthorized", HTTPStatusCode2[HTTPStatusCode2.PaymentRequired = 402] = "PaymentRequired", HTTPStatusCode2[HTTPStatusCode2.Forbidden = 403] = "Forbidden", HTTPStatusCode2[HTTPStatusCode2.NotFound = 404] = "NotFound", HTTPStatusCode2[HTTPStatusCode2.MethodNotAllowed = 405] = "MethodNotAllowed", HTTPStatusCode2[HTTPStatusCode2.NotAcceptable = 406] = "NotAcceptable", HTTPStatusCode2[HTTPStatusCode2.ProxyAuthenticationRequired = 407] = "ProxyAuthenticationRequired", HTTPStatusCode2[HTTPStatusCode2.RequestTimeout = 408] = "RequestTimeout", HTTPStatusCode2[HTTPStatusCode2.Conflict = 409] = "Conflict", HTTPStatusCode2[HTTPStatusCode2.Gone = 410] = "Gone", HTTPStatusCode2[HTTPStatusCode2.LengthRequired = 411] = "LengthRequired", HTTPStatusCode2[HTTPStatusCode2.PreconditionFailed = 412] = "PreconditionFailed", HTTPStatusCode2[HTTPStatusCode2.PayloadTooLarge = 413] = "PayloadTooLarge", HTTPStatusCode2[HTTPStatusCode2.UriTooLong = 414] = "UriTooLong", HTTPStatusCode2[HTTPStatusCode2.UnsupportedMediaType = 415] = "UnsupportedMediaType", HTTPStatusCode2[HTTPStatusCode2.RangeNotSatisfiable = 416] = "RangeNotSatisfiable", HTTPStatusCode2[HTTPStatusCode2.ExpectationFailed = 417] = "ExpectationFailed", HTTPStatusCode2[HTTPStatusCode2.ImATeapot = 418] = "ImATeapot", HTTPStatusCode2[HTTPStatusCode2.MisdirectedRequest = 421] = "MisdirectedRequest", HTTPStatusCode2[HTTPStatusCode2.UnprocessableEntity = 422] = "UnprocessableEntity", HTTPStatusCode2[HTTPStatusCode2.Locked = 423] = "Locked", HTTPStatusCode2[HTTPStatusCode2.FailedDependency = 424] = "FailedDependency", HTTPStatusCode2[HTTPStatusCode2.TooEarly = 425] = "TooEarly", HTTPStatusCode2[HTTPStatusCode2.UpgradeRequired = 426] = "UpgradeRequired", HTTPStatusCode2[HTTPStatusCode2.PreconditionRequired = 428] = "PreconditionRequired", HTTPStatusCode2[HTTPStatusCode2.TooManyRequests = 429] = "TooManyRequests", HTTPStatusCode2[HTTPStatusCode2.RequestHeaderFieldsTooLarge = 431] = "RequestHeaderFieldsTooLarge", HTTPStatusCode2[HTTPStatusCode2.UnavailableForLegalReasons = 451] = "UnavailableForLegalReasons", HTTPStatusCode2[HTTPStatusCode2.InternalServerError = 500] = "InternalServerError", HTTPStatusCode2[HTTPStatusCode2.NotImplemented = 501] = "NotImplemented", HTTPStatusCode2[HTTPStatusCode2.BadGateway = 502] = "BadGateway", HTTPStatusCode2[HTTPStatusCode2.ServiceUnavailable = 503] = "ServiceUnavailable", HTTPStatusCode2[HTTPStatusCode2.GatewayTimeout = 504] = "GatewayTimeout", HTTPStatusCode2[HTTPStatusCode2.HttpVersionNotSupported = 505] = "HttpVersionNotSupported", HTTPStatusCode2[HTTPStatusCode2.VariantAlsoNegotiates = 506] = "VariantAlsoNegotiates", HTTPStatusCode2[HTTPStatusCode2.InsufficientStorage = 507] = "InsufficientStorage", HTTPStatusCode2[HTTPStatusCode2.LoopDetected = 508] = "LoopDetected", HTTPStatusCode2[HTTPStatusCode2.NotExtended = 510] = "NotExtended", HTTPStatusCode2[HTTPStatusCode2.NetworkAuthenticationRequired = 511] = "NetworkAuthenticationRequired", HTTPStatusCode2))(HTTPStatusCode || {}), HTTPEventType = /* @__PURE__ */ ((HTTPEventType2) => (HTTPEventType2[HTTPEventType2.DownloadProgress = 0] = "DownloadProgress", HTTPEventType2[HTTPEventType2.Response = 1] = "Response", HTTPEventType2))(HTTPEventType || {});
|
|
183
|
+
const _HTTPResponse = class _HTTPResponse {
|
|
179
184
|
constructor({
|
|
180
|
-
body
|
|
181
|
-
headers
|
|
182
|
-
status
|
|
183
|
-
statusText
|
|
185
|
+
body,
|
|
186
|
+
headers,
|
|
187
|
+
status,
|
|
188
|
+
statusText
|
|
184
189
|
}) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.body =
|
|
190
|
+
__publicField(this, "type", 1);
|
|
191
|
+
__publicField(this, "body");
|
|
192
|
+
__publicField(this, "headers");
|
|
193
|
+
__publicField(this, "status");
|
|
194
|
+
__publicField(this, "statusText");
|
|
195
|
+
this.body = body, this.headers = headers, this.status = status, this.statusText = statusText;
|
|
191
196
|
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
+
};
|
|
198
|
+
__name(_HTTPResponse, "HTTPResponse");
|
|
199
|
+
let HTTPResponse = _HTTPResponse;
|
|
200
|
+
const _HTTPProgress = class _HTTPProgress {
|
|
201
|
+
constructor(total, loaded, partialText) {
|
|
202
|
+
__publicField(this, "type", 0);
|
|
203
|
+
this.total = total, this.loaded = loaded, this.partialText = partialText;
|
|
197
204
|
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
205
|
+
};
|
|
206
|
+
__name(_HTTPProgress, "HTTPProgress");
|
|
207
|
+
let HTTPProgress = _HTTPProgress;
|
|
208
|
+
const _ResponseHeader = class _ResponseHeader {
|
|
209
|
+
constructor(headers, status, statusText) {
|
|
210
|
+
this.headers = headers, this.status = status, this.statusText = statusText;
|
|
202
211
|
}
|
|
203
|
-
}
|
|
204
|
-
|
|
212
|
+
};
|
|
213
|
+
__name(_ResponseHeader, "ResponseHeader");
|
|
214
|
+
let ResponseHeader = _ResponseHeader;
|
|
215
|
+
const _HTTPResponseError = class _HTTPResponseError {
|
|
205
216
|
constructor({
|
|
206
|
-
request
|
|
207
|
-
headers
|
|
208
|
-
status
|
|
209
|
-
statusText
|
|
210
|
-
error
|
|
217
|
+
request,
|
|
218
|
+
headers,
|
|
219
|
+
status,
|
|
220
|
+
statusText,
|
|
221
|
+
error
|
|
211
222
|
}) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
this.request =
|
|
223
|
+
__publicField(this, "request");
|
|
224
|
+
__publicField(this, "headers");
|
|
225
|
+
__publicField(this, "status");
|
|
226
|
+
__publicField(this, "statusText");
|
|
227
|
+
__publicField(this, "error");
|
|
228
|
+
this.request = request, this.headers = headers, this.status = status, this.statusText = statusText, this.error = error;
|
|
218
229
|
}
|
|
219
|
-
}
|
|
220
|
-
|
|
230
|
+
};
|
|
231
|
+
__name(_HTTPResponseError, "HTTPResponseError");
|
|
232
|
+
let HTTPResponseError = _HTTPResponseError;
|
|
233
|
+
function parseFetchParamsFromRequest(request) {
|
|
221
234
|
return {
|
|
222
|
-
method:
|
|
223
|
-
headers:
|
|
224
|
-
body:
|
|
225
|
-
credentials:
|
|
235
|
+
method: request.method,
|
|
236
|
+
headers: request.getHeadersInit(),
|
|
237
|
+
body: request.getBody(),
|
|
238
|
+
credentials: request.withCredentials ? "include" : void 0
|
|
226
239
|
};
|
|
227
240
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
241
|
+
__name(parseFetchParamsFromRequest, "parseFetchParamsFromRequest");
|
|
242
|
+
var __defProp$2 = Object.defineProperty, __getOwnPropDesc$2 = Object.getOwnPropertyDescriptor, __decorateClass$2 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
|
|
243
|
+
for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$2(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
|
|
244
|
+
(decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
|
|
245
|
+
return kind && result && __defProp$2(target, key, result), result;
|
|
246
|
+
}, "__decorateClass$2"), __decorateParam$2 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$2"), _a2;
|
|
247
|
+
let FetchHTTPImplementation = (_a2 = class {
|
|
248
|
+
constructor(_logService) {
|
|
249
|
+
this._logService = _logService;
|
|
250
|
+
}
|
|
251
|
+
send(request) {
|
|
252
|
+
return new Observable((subscriber) => {
|
|
253
|
+
const abortController = new AbortController();
|
|
254
|
+
return this._send(request, subscriber, abortController).catch((error) => {
|
|
255
|
+
subscriber.error(new HTTPResponseError({
|
|
256
|
+
error,
|
|
257
|
+
request
|
|
244
258
|
}));
|
|
245
|
-
}), () =>
|
|
259
|
+
}), () => abortController.abort();
|
|
246
260
|
});
|
|
247
261
|
}
|
|
248
|
-
async _send(
|
|
249
|
-
var
|
|
250
|
-
let
|
|
262
|
+
async _send(request, subscriber, abortController) {
|
|
263
|
+
var _a5, _b;
|
|
264
|
+
let response;
|
|
251
265
|
try {
|
|
252
|
-
const
|
|
253
|
-
signal:
|
|
254
|
-
...
|
|
266
|
+
const fetchParams = parseFetchParamsFromRequest(request), urlWithParams = request.getUrlWithParams(), fetchPromise = fetch(urlWithParams, {
|
|
267
|
+
signal: abortController.signal,
|
|
268
|
+
...fetchParams
|
|
255
269
|
});
|
|
256
|
-
this._logService.debug(`[FetchHTTPImplementation]: sending request to url ${
|
|
257
|
-
} catch (
|
|
258
|
-
const
|
|
259
|
-
request
|
|
260
|
-
error
|
|
261
|
-
status: (
|
|
262
|
-
statusText: (
|
|
263
|
-
headers:
|
|
270
|
+
this._logService.debug(`[FetchHTTPImplementation]: sending request to url ${urlWithParams} with params ${fetchParams}`), response = await fetchPromise;
|
|
271
|
+
} catch (error) {
|
|
272
|
+
const e = new HTTPResponseError({
|
|
273
|
+
request,
|
|
274
|
+
error,
|
|
275
|
+
status: (_a5 = error.status) != null ? _a5 : 0,
|
|
276
|
+
statusText: (_b = error.statusText) != null ? _b : "Unknown Error",
|
|
277
|
+
headers: error.headers
|
|
264
278
|
});
|
|
265
|
-
this._logService.error("[FetchHTTPImplementation]: network error",
|
|
279
|
+
this._logService.error("[FetchHTTPImplementation]: network error", e), subscriber.error(e);
|
|
266
280
|
return;
|
|
267
281
|
}
|
|
268
|
-
const
|
|
269
|
-
let
|
|
270
|
-
if (
|
|
271
|
-
|
|
272
|
-
body
|
|
273
|
-
headers:
|
|
274
|
-
status
|
|
275
|
-
statusText
|
|
282
|
+
const responseHeaders = new HTTPHeaders(response.headers), status = response.status, statusText = response.statusText;
|
|
283
|
+
let body = null;
|
|
284
|
+
if (response.body && (body = await this._readBody(request, response, subscriber)), status >= HTTPStatusCode.Ok && status < HTTPStatusCode.MultipleChoices)
|
|
285
|
+
subscriber.next(new HTTPResponse({
|
|
286
|
+
body,
|
|
287
|
+
headers: responseHeaders,
|
|
288
|
+
status,
|
|
289
|
+
statusText
|
|
276
290
|
}));
|
|
277
291
|
else {
|
|
278
|
-
const
|
|
279
|
-
request
|
|
280
|
-
error:
|
|
281
|
-
status
|
|
282
|
-
statusText
|
|
283
|
-
headers:
|
|
292
|
+
const e = new HTTPResponseError({
|
|
293
|
+
request,
|
|
294
|
+
error: body,
|
|
295
|
+
status,
|
|
296
|
+
statusText,
|
|
297
|
+
headers: responseHeaders
|
|
284
298
|
});
|
|
285
|
-
this._logService.error("[FetchHTTPImplementation]: network error",
|
|
299
|
+
this._logService.error("[FetchHTTPImplementation]: network error", e), subscriber.error(e);
|
|
286
300
|
}
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
async _readBody(
|
|
290
|
-
var
|
|
291
|
-
const
|
|
292
|
-
let
|
|
293
|
-
const
|
|
294
|
-
let
|
|
301
|
+
subscriber.complete();
|
|
302
|
+
}
|
|
303
|
+
async _readBody(request, response, subscriber) {
|
|
304
|
+
var _a5, _b;
|
|
305
|
+
const chunks = [], reader = response.body.getReader(), contentLength = response.headers.get("content-length");
|
|
306
|
+
let receivedLength = 0;
|
|
307
|
+
const reportProgress = (_a5 = request.requestParams) == null ? void 0 : _a5.reportProgress, responseType = request.responseType;
|
|
308
|
+
let partialText, decoder;
|
|
295
309
|
for (; ; ) {
|
|
296
|
-
const { done
|
|
297
|
-
if (
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
310
|
+
const { done, value } = await reader.read();
|
|
311
|
+
if (done) break;
|
|
312
|
+
chunks.push(value), receivedLength += value.length, reportProgress && responseType === "text" && (partialText = (partialText != null ? partialText : "") + (decoder != null ? decoder : decoder = new TextDecoder()).decode(value, { stream: !0 }), subscriber.next(new HTTPProgress(
|
|
313
|
+
contentLength ? Number.parseInt(contentLength, 10) : void 0,
|
|
314
|
+
receivedLength,
|
|
315
|
+
partialText
|
|
302
316
|
)));
|
|
303
317
|
}
|
|
304
|
-
const
|
|
318
|
+
const all = mergeChunks(chunks, receivedLength);
|
|
305
319
|
try {
|
|
306
|
-
const
|
|
307
|
-
return
|
|
308
|
-
} catch (
|
|
309
|
-
const
|
|
310
|
-
request
|
|
311
|
-
error
|
|
312
|
-
status:
|
|
313
|
-
statusText:
|
|
314
|
-
headers: new
|
|
320
|
+
const contentType = (_b = response.headers.get("content-type")) != null ? _b : "";
|
|
321
|
+
return deserialize(request, all, contentType);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
const e = new HTTPResponseError({
|
|
324
|
+
request,
|
|
325
|
+
error,
|
|
326
|
+
status: response.status,
|
|
327
|
+
statusText: response.statusText,
|
|
328
|
+
headers: new HTTPHeaders(response.headers)
|
|
315
329
|
});
|
|
316
|
-
return this._logService.error("[FetchHTTPImplementation]: network error",
|
|
330
|
+
return this._logService.error("[FetchHTTPImplementation]: network error", e), subscriber.error(e), null;
|
|
317
331
|
}
|
|
318
332
|
}
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
],
|
|
323
|
-
function
|
|
324
|
-
const
|
|
325
|
-
let
|
|
326
|
-
for (const
|
|
327
|
-
|
|
328
|
-
return
|
|
333
|
+
}, __name(_a2, "FetchHTTPImplementation"), _a2);
|
|
334
|
+
FetchHTTPImplementation = __decorateClass$2([
|
|
335
|
+
__decorateParam$2(0, ILogService)
|
|
336
|
+
], FetchHTTPImplementation);
|
|
337
|
+
function mergeChunks(chunks, totalLength) {
|
|
338
|
+
const all = new Uint8Array(totalLength);
|
|
339
|
+
let position = 0;
|
|
340
|
+
for (const chunk of chunks)
|
|
341
|
+
all.set(chunk, position), position += chunk.length;
|
|
342
|
+
return all;
|
|
329
343
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
344
|
+
__name(mergeChunks, "mergeChunks");
|
|
345
|
+
const XSSI_PREFIX = /^\)\]\}',?\n/;
|
|
346
|
+
function deserialize(request, bin, contentType) {
|
|
347
|
+
switch (request.responseType) {
|
|
333
348
|
case "json":
|
|
334
|
-
const
|
|
335
|
-
return
|
|
349
|
+
const text = new TextDecoder().decode(bin).replace(XSSI_PREFIX, "");
|
|
350
|
+
return text === "" ? null : JSON.parse(text);
|
|
336
351
|
case "text":
|
|
337
|
-
return new TextDecoder().decode(
|
|
352
|
+
return new TextDecoder().decode(bin);
|
|
338
353
|
case "blob":
|
|
339
|
-
return new Blob([
|
|
354
|
+
return new Blob([bin], { type: contentType });
|
|
340
355
|
case "arraybuffer":
|
|
341
|
-
return
|
|
356
|
+
return bin.buffer;
|
|
342
357
|
default:
|
|
343
|
-
throw new Error(`[FetchHTTPImplementation]: unknown response type: ${
|
|
358
|
+
throw new Error(`[FetchHTTPImplementation]: unknown response type: ${request.responseType}.`);
|
|
344
359
|
}
|
|
345
360
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
361
|
+
__name(deserialize, "deserialize");
|
|
362
|
+
var __defProp$1 = Object.defineProperty, __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor, __decorateClass$1 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
|
|
363
|
+
for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
|
|
364
|
+
(decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
|
|
365
|
+
return kind && result && __defProp$1(target, key, result), result;
|
|
366
|
+
}, "__decorateClass$1"), __decorateParam$1 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$1"), _a3;
|
|
367
|
+
let XHRHTTPImplementation = (_a3 = class {
|
|
368
|
+
constructor(_logService) {
|
|
369
|
+
this._logService = _logService;
|
|
370
|
+
}
|
|
371
|
+
send(request) {
|
|
372
|
+
return new Observable((observer) => {
|
|
373
|
+
const xhr = new XMLHttpRequest(), urlWithParams = request.getUrlWithParams(), fetchParams = parseFetchParamsFromRequest(request);
|
|
374
|
+
xhr.open(request.method, urlWithParams), request.withCredentials && (xhr.withCredentials = !0), fetchParams.headers && Object.entries(fetchParams.headers).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
375
|
+
const buildResponseHeader = /* @__PURE__ */ __name(() => {
|
|
376
|
+
const statusText = xhr.statusText || "OK", headers = new HTTPHeaders(xhr.getAllResponseHeaders());
|
|
377
|
+
return new ResponseHeader(headers, xhr.status, statusText);
|
|
378
|
+
}, "buildResponseHeader"), onLoadHandler = /* @__PURE__ */ __name(() => {
|
|
379
|
+
const { headers, statusText, status } = buildResponseHeader(), { responseType } = request;
|
|
380
|
+
let body2 = null, error = null;
|
|
381
|
+
status !== HTTPStatusCode.NoContent && (body2 = typeof xhr.response > "u" ? xhr.responseText : xhr.response);
|
|
382
|
+
let success = status >= SuccessStatusCodeLowerBound && status < ErrorStatusCodeLowerBound;
|
|
383
|
+
if (responseType === "json" && typeof body2 == "string") {
|
|
384
|
+
const originalBody = body2;
|
|
369
385
|
try {
|
|
370
|
-
|
|
371
|
-
} catch (
|
|
372
|
-
|
|
386
|
+
body2 = body2 ? JSON.parse(body2) : null;
|
|
387
|
+
} catch (e) {
|
|
388
|
+
success = !1, body2 = originalBody, error = e;
|
|
373
389
|
}
|
|
374
390
|
}
|
|
375
|
-
if (
|
|
376
|
-
|
|
377
|
-
new
|
|
378
|
-
body:
|
|
379
|
-
headers
|
|
380
|
-
status
|
|
381
|
-
statusText
|
|
391
|
+
if (success)
|
|
392
|
+
observer.next(
|
|
393
|
+
new HTTPResponse({
|
|
394
|
+
body: body2,
|
|
395
|
+
headers,
|
|
396
|
+
status,
|
|
397
|
+
statusText
|
|
382
398
|
})
|
|
383
399
|
);
|
|
384
400
|
else {
|
|
385
|
-
const
|
|
386
|
-
request
|
|
387
|
-
error
|
|
388
|
-
headers
|
|
389
|
-
status
|
|
390
|
-
statusText
|
|
401
|
+
const e = new HTTPResponseError({
|
|
402
|
+
request,
|
|
403
|
+
error,
|
|
404
|
+
headers,
|
|
405
|
+
status,
|
|
406
|
+
statusText
|
|
391
407
|
});
|
|
392
|
-
this._logService.error("[XHRHTTPImplementation]: network error",
|
|
408
|
+
this._logService.error("[XHRHTTPImplementation]: network error", e), observer.error(e);
|
|
393
409
|
}
|
|
394
|
-
},
|
|
395
|
-
const
|
|
396
|
-
request
|
|
397
|
-
error
|
|
398
|
-
status:
|
|
399
|
-
statusText:
|
|
400
|
-
headers:
|
|
410
|
+
}, "onLoadHandler"), onErrorHandler = /* @__PURE__ */ __name((error) => {
|
|
411
|
+
const e = new HTTPResponseError({
|
|
412
|
+
request,
|
|
413
|
+
error,
|
|
414
|
+
status: xhr.status || 0,
|
|
415
|
+
statusText: xhr.statusText || "Unknown Error",
|
|
416
|
+
headers: buildResponseHeader().headers
|
|
401
417
|
});
|
|
402
|
-
this._logService.error("[XHRHTTPImplementation]: network error",
|
|
403
|
-
};
|
|
404
|
-
|
|
405
|
-
const
|
|
406
|
-
return
|
|
407
|
-
|
|
418
|
+
this._logService.error("[XHRHTTPImplementation]: network error", e), observer.error(e);
|
|
419
|
+
}, "onErrorHandler");
|
|
420
|
+
xhr.addEventListener("load", onLoadHandler), xhr.addEventListener("error", onErrorHandler), xhr.addEventListener("abort", onErrorHandler), xhr.addEventListener("timeout", onErrorHandler);
|
|
421
|
+
const body = request.getBody();
|
|
422
|
+
return xhr.send(body), this._logService.debug(`[XHRHTTPImplementation]: sending request to url ${urlWithParams} with params ${fetchParams}`), () => {
|
|
423
|
+
xhr.readyState !== xhr.DONE && xhr.abort(), xhr.removeEventListener("load", onLoadHandler), xhr.removeEventListener("error", onErrorHandler), xhr.removeEventListener("abort", onErrorHandler), xhr.removeEventListener("timeout", onErrorHandler);
|
|
408
424
|
};
|
|
409
425
|
});
|
|
410
426
|
}
|
|
411
|
-
};
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
],
|
|
415
|
-
var
|
|
416
|
-
for (var
|
|
417
|
-
(
|
|
418
|
-
return
|
|
419
|
-
},
|
|
420
|
-
let
|
|
421
|
-
constructor(
|
|
422
|
-
super(), this._config =
|
|
427
|
+
}, __name(_a3, "XHRHTTPImplementation"), _a3);
|
|
428
|
+
XHRHTTPImplementation = __decorateClass$1([
|
|
429
|
+
__decorateParam$1(0, ILogService)
|
|
430
|
+
], XHRHTTPImplementation);
|
|
431
|
+
var __defProp2 = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __decorateClass = /* @__PURE__ */ __name((decorators, target, key, kind) => {
|
|
432
|
+
for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
|
|
433
|
+
(decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
|
|
434
|
+
return kind && result && __defProp2(target, key, result), result;
|
|
435
|
+
}, "__decorateClass"), __decorateParam = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam"), _a4;
|
|
436
|
+
let UniverNetworkPlugin = (_a4 = class extends Plugin {
|
|
437
|
+
constructor(_config = void 0, _logger, _injector) {
|
|
438
|
+
super(), this._config = _config, this._logger = _logger, this._injector = _injector;
|
|
423
439
|
}
|
|
424
440
|
onStarting() {
|
|
425
|
-
var
|
|
426
|
-
if (this._injector.get(
|
|
441
|
+
var _a5, _b, _c;
|
|
442
|
+
if (this._injector.get(HTTPService, Quantity.OPTIONAL, LookUp.SKIP_SELF) && !((_a5 = this._config) != null && _a5.forceUseNewInstance)) {
|
|
427
443
|
this._logger.warn(
|
|
428
444
|
"[UniverNetworkPlugin]",
|
|
429
445
|
'HTTPService is already registered in an ancestor interceptor. Skipping registration. If you want to force a new instance, set "forceUseNewInstance" to true in the plugin configuration.'
|
|
430
446
|
);
|
|
431
447
|
return;
|
|
432
448
|
}
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
[
|
|
436
|
-
[
|
|
437
|
-
], (
|
|
438
|
-
}
|
|
439
|
-
},
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
],
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
createSocket(r) {
|
|
449
|
+
const impl = (_b = this._config) != null && _b.useFetchImpl ? FetchHTTPImplementation : typeof window < "u" ? XHRHTTPImplementation : FetchHTTPImplementation;
|
|
450
|
+
registerDependencies(this._injector, mergeOverrideWithDependencies([
|
|
451
|
+
[HTTPService],
|
|
452
|
+
[IHTTPImplementation, { useClass: impl }]
|
|
453
|
+
], (_c = this._config) == null ? void 0 : _c.override));
|
|
454
|
+
}
|
|
455
|
+
}, __name(_a4, "UniverNetworkPlugin"), __publicField(_a4, "pluginName", "UNIVER_NETWORK_PLUGIN"), _a4);
|
|
456
|
+
UniverNetworkPlugin = __decorateClass([
|
|
457
|
+
__decorateParam(1, ILogService),
|
|
458
|
+
__decorateParam(2, Inject(Injector))
|
|
459
|
+
], UniverNetworkPlugin);
|
|
460
|
+
const ISocketService = createIdentifier("univer.network.socket.service"), _WebSocketService = class _WebSocketService extends Disposable {
|
|
461
|
+
createSocket(URL) {
|
|
447
462
|
try {
|
|
448
|
-
const
|
|
463
|
+
const connection = new WebSocket(URL), disposables = new DisposableCollection();
|
|
449
464
|
return {
|
|
450
|
-
URL
|
|
451
|
-
close: (
|
|
452
|
-
|
|
453
|
-
},
|
|
454
|
-
send: (
|
|
455
|
-
|
|
456
|
-
},
|
|
457
|
-
open$: new
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
}).pipe(
|
|
461
|
-
close$: new
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
}).pipe(
|
|
465
|
-
error$: new
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
}).pipe(
|
|
469
|
-
message$: new
|
|
470
|
-
const
|
|
471
|
-
|
|
472
|
-
}).pipe(
|
|
465
|
+
URL,
|
|
466
|
+
close: /* @__PURE__ */ __name((code, reason) => {
|
|
467
|
+
connection.close(code, reason), disposables.dispose();
|
|
468
|
+
}, "close"),
|
|
469
|
+
send: /* @__PURE__ */ __name((data) => {
|
|
470
|
+
connection.send(data);
|
|
471
|
+
}, "send"),
|
|
472
|
+
open$: new Observable((subscriber) => {
|
|
473
|
+
const callback = /* @__PURE__ */ __name((event) => subscriber.next(event), "callback");
|
|
474
|
+
connection.addEventListener("open", callback), disposables.add(toDisposable(() => connection.removeEventListener("open", callback)));
|
|
475
|
+
}).pipe(share()),
|
|
476
|
+
close$: new Observable((subscriber) => {
|
|
477
|
+
const callback = /* @__PURE__ */ __name((event) => subscriber.next(event), "callback");
|
|
478
|
+
connection.addEventListener("close", callback), disposables.add(toDisposable(() => connection.removeEventListener("close", callback)));
|
|
479
|
+
}).pipe(share()),
|
|
480
|
+
error$: new Observable((subscriber) => {
|
|
481
|
+
const callback = /* @__PURE__ */ __name((event) => subscriber.next(event), "callback");
|
|
482
|
+
connection.addEventListener("error", callback), disposables.add(toDisposable(() => connection.removeEventListener("error", callback)));
|
|
483
|
+
}).pipe(share()),
|
|
484
|
+
message$: new Observable((subscriber) => {
|
|
485
|
+
const callback = /* @__PURE__ */ __name((event) => subscriber.next(event), "callback");
|
|
486
|
+
connection.addEventListener("message", callback), disposables.add(toDisposable(() => connection.removeEventListener("message", callback)));
|
|
487
|
+
}).pipe(share())
|
|
473
488
|
};
|
|
474
|
-
} catch (
|
|
475
|
-
return console.error(
|
|
489
|
+
} catch (e) {
|
|
490
|
+
return console.error(e), null;
|
|
476
491
|
}
|
|
477
492
|
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
493
|
+
};
|
|
494
|
+
__name(_WebSocketService, "WebSocketService");
|
|
495
|
+
let WebSocketService = _WebSocketService;
|
|
496
|
+
const DEFAULT_MAX_RETRY_ATTEMPTS = 3, DELAY_INTERVAL = 1e3, RetryInterceptorFactory = /* @__PURE__ */ __name((params) => {
|
|
497
|
+
var _a5, _b;
|
|
498
|
+
const maxRetryAttempts = (_a5 = params == null ? void 0 : params.maxRetryAttempts) != null ? _a5 : DEFAULT_MAX_RETRY_ATTEMPTS, delayInterval = (_b = params == null ? void 0 : params.delayInterval) != null ? _b : DELAY_INTERVAL;
|
|
499
|
+
return (request, next) => next(request).pipe(retry({ delay: delayInterval, count: maxRetryAttempts }));
|
|
500
|
+
}, "RetryInterceptorFactory"), ThresholdInterceptorFactory = /* @__PURE__ */ __name((params) => {
|
|
501
|
+
const handlers = [], ongoingHandlers = /* @__PURE__ */ new Set(), tick = /* @__PURE__ */ __name(() => {
|
|
502
|
+
var _a5;
|
|
503
|
+
for (; ongoingHandlers.size < ((_a5 = params == null ? void 0 : params.maxParallel) != null ? _a5 : 1) && handlers.length > 0; ) {
|
|
504
|
+
const handler = handlers.shift();
|
|
505
|
+
ongoingHandlers.add(handler), handler();
|
|
489
506
|
}
|
|
490
|
-
};
|
|
491
|
-
return (
|
|
492
|
-
const
|
|
493
|
-
next: (
|
|
494
|
-
error: (
|
|
495
|
-
complete: () =>
|
|
496
|
-
}),
|
|
497
|
-
|
|
498
|
-
};
|
|
499
|
-
return
|
|
507
|
+
}, "tick");
|
|
508
|
+
return (request, next) => new Observable((observer) => {
|
|
509
|
+
const handler = /* @__PURE__ */ __name(() => next(request).subscribe({
|
|
510
|
+
next: /* @__PURE__ */ __name((event) => observer.next(event), "next"),
|
|
511
|
+
error: /* @__PURE__ */ __name((err) => observer.error(err), "error"),
|
|
512
|
+
complete: /* @__PURE__ */ __name(() => observer.complete(), "complete")
|
|
513
|
+
}), "handler"), teardown = /* @__PURE__ */ __name(() => {
|
|
514
|
+
ongoingHandlers.delete(handler), remove(handlers, handler), tick();
|
|
515
|
+
}, "teardown");
|
|
516
|
+
return handlers.push(handler), tick(), teardown;
|
|
500
517
|
});
|
|
501
|
-
},
|
|
502
|
-
const { errorStatusCodes
|
|
503
|
-
return (
|
|
504
|
-
|
|
505
|
-
);
|
|
506
|
-
},
|
|
507
|
-
let
|
|
508
|
-
};
|
|
509
|
-
return (
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
},
|
|
514
|
-
|
|
515
|
-
clearTimeout(
|
|
516
|
-
};
|
|
518
|
+
}, "ThresholdInterceptorFactory"), AuthInterceptorFactory = /* @__PURE__ */ __name((params) => {
|
|
519
|
+
const { errorStatusCodes, onAuthError } = params;
|
|
520
|
+
return /* @__PURE__ */ __name((request, next) => next(request).pipe(
|
|
521
|
+
catchError((error) => (error instanceof HTTPResponseError && errorStatusCodes.some((c) => c === error.status) && onAuthError(), throwError(() => error)))
|
|
522
|
+
), "authInterceptor");
|
|
523
|
+
}, "AuthInterceptorFactory"), createDefaultFetchCheck = /* @__PURE__ */ __name((time = 300) => {
|
|
524
|
+
let cancel = /* @__PURE__ */ __name(() => {
|
|
525
|
+
}, "noop");
|
|
526
|
+
return (_currentConfig) => new Promise((res) => {
|
|
527
|
+
cancel();
|
|
528
|
+
const t = setTimeout(() => {
|
|
529
|
+
res(!0);
|
|
530
|
+
}, time);
|
|
531
|
+
cancel = /* @__PURE__ */ __name(() => {
|
|
532
|
+
clearTimeout(t), res(!1);
|
|
533
|
+
}, "cancel");
|
|
517
534
|
});
|
|
518
|
-
},
|
|
519
|
-
const { isMatch
|
|
520
|
-
return (
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
next: (
|
|
524
|
-
error: (
|
|
525
|
-
config:
|
|
535
|
+
}, "createDefaultFetchCheck"), createDistributeResult = /* @__PURE__ */ __name(() => (result, list) => list.map((config) => ({ config, result })), "createDistributeResult"), MergeInterceptorFactory = /* @__PURE__ */ __name((config, options = {}) => {
|
|
536
|
+
const { isMatch, getParamsFromRequest, mergeParamsToRequest } = config, { fetchCheck = createDefaultFetchCheck(300), distributeResult = createDistributeResult() } = options, hookList = [], getPlainList = /* @__PURE__ */ __name((_list) => _list.map((item) => item.config), "getPlainList");
|
|
537
|
+
return (requestConfig, next) => isMatch(requestConfig) ? new Observable((observer) => {
|
|
538
|
+
const params = getParamsFromRequest(requestConfig);
|
|
539
|
+
hookList.push({
|
|
540
|
+
next: /* @__PURE__ */ __name((v) => observer.next(v), "next"),
|
|
541
|
+
error: /* @__PURE__ */ __name((error) => observer.error(error), "error"),
|
|
542
|
+
config: params
|
|
526
543
|
});
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
if (
|
|
530
|
-
const
|
|
531
|
-
|
|
532
|
-
const
|
|
533
|
-
if (
|
|
534
|
-
const [
|
|
535
|
-
|
|
544
|
+
const list = getPlainList(hookList);
|
|
545
|
+
fetchCheck(requestConfig).then((isFetch) => {
|
|
546
|
+
if (isFetch) {
|
|
547
|
+
const currentHookList = [];
|
|
548
|
+
list.forEach((config2) => {
|
|
549
|
+
const index = hookList.findIndex((item) => item.config === config2);
|
|
550
|
+
if (index >= 0) {
|
|
551
|
+
const [hook] = hookList.splice(index, 1);
|
|
552
|
+
currentHookList.push(hook);
|
|
536
553
|
}
|
|
537
|
-
}),
|
|
538
|
-
next: (
|
|
539
|
-
if (
|
|
540
|
-
const
|
|
541
|
-
|
|
542
|
-
const
|
|
543
|
-
if (
|
|
544
|
-
const
|
|
545
|
-
body:
|
|
546
|
-
headers:
|
|
547
|
-
status:
|
|
548
|
-
statusText:
|
|
554
|
+
}), next(mergeParamsToRequest(list, requestConfig)).subscribe({
|
|
555
|
+
next: /* @__PURE__ */ __name((e) => {
|
|
556
|
+
if (e.type === HTTPEventType.Response) {
|
|
557
|
+
const body = e.body, configList = distributeResult(body, list);
|
|
558
|
+
currentHookList.forEach((hookItem) => {
|
|
559
|
+
const res = configList.find((item) => item.config === hookItem.config);
|
|
560
|
+
if (res) {
|
|
561
|
+
const response = new HTTPResponse({
|
|
562
|
+
body: res.result,
|
|
563
|
+
headers: e.headers,
|
|
564
|
+
status: e.status,
|
|
565
|
+
statusText: e.statusText
|
|
549
566
|
});
|
|
550
|
-
|
|
567
|
+
hookItem.next(response);
|
|
551
568
|
} else
|
|
552
|
-
|
|
569
|
+
hookItem.error("batch error");
|
|
553
570
|
});
|
|
554
571
|
}
|
|
555
|
-
},
|
|
556
|
-
complete: () =>
|
|
557
|
-
error: (
|
|
572
|
+
}, "next"),
|
|
573
|
+
complete: /* @__PURE__ */ __name(() => observer.complete(), "complete"),
|
|
574
|
+
error: /* @__PURE__ */ __name((e) => observer.error(e), "error")
|
|
558
575
|
});
|
|
559
576
|
}
|
|
560
577
|
});
|
|
561
|
-
}) :
|
|
562
|
-
};
|
|
578
|
+
}) : next(requestConfig);
|
|
579
|
+
}, "MergeInterceptorFactory");
|
|
563
580
|
export {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
581
|
+
AuthInterceptorFactory,
|
|
582
|
+
FetchHTTPImplementation,
|
|
583
|
+
HTTPEventType,
|
|
584
|
+
HTTPHeaders,
|
|
585
|
+
HTTPProgress,
|
|
586
|
+
HTTPRequest,
|
|
587
|
+
HTTPResponse,
|
|
588
|
+
HTTPResponseError,
|
|
589
|
+
HTTPService,
|
|
590
|
+
HTTPStatusCode,
|
|
591
|
+
IHTTPImplementation,
|
|
592
|
+
ISocketService,
|
|
593
|
+
MergeInterceptorFactory,
|
|
594
|
+
ResponseHeader,
|
|
595
|
+
RetryInterceptorFactory,
|
|
596
|
+
ThresholdInterceptorFactory,
|
|
597
|
+
UniverNetworkPlugin,
|
|
598
|
+
WebSocketService,
|
|
599
|
+
XHRHTTPImplementation
|
|
583
600
|
};
|