@univerjs/network 0.1.9 → 0.1.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/lib/cjs/index.js +2 -2
- package/lib/es/index.js +320 -198
- package/lib/types/index.d.ts +2 -0
- package/lib/types/services/http/__testing__/http-testing-utils.d.ts +5 -5
- package/lib/types/services/http/headers.d.ts +5 -1
- package/lib/types/services/http/http.service.d.ts +5 -5
- package/lib/types/services/http/implementations/fetch.d.ts +16 -0
- package/lib/types/services/http/implementations/implementation.d.ts +2 -2
- package/lib/types/services/http/implementations/xhr.d.ts +4 -4
- package/lib/types/services/http/interceptor.d.ts +3 -3
- package/lib/types/services/http/request.d.ts +3 -2
- package/lib/types/services/http/response.d.ts +7 -6
- package/lib/types/services/web-socket/web-socket.service.d.ts +1 -1
- package/lib/umd/index.js +2 -2
- package/package.json +8 -8
package/lib/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`).forEach(n=>{const[r,s]=n.split(":");r&&s&&this._setHeader(r,s)}):t&&Object.keys(t).forEach(([n,r])=>{this._setHeader(n,r)})}forEach(t){this._headers.forEach((n,r)=>t(r,n))}has(t){return!!this._headers.has(t.toLowerCase())}get(t){const n=t.toLowerCase();return this._headers.has(n)?this._headers.get(n):null}_setHeader(t,n){const r=t.toLowerCase();this._headers.has(r)?this._headers.get(r).push(n.toString()):this._headers.set(r,[n.toString()])}}const L=_.createIdentifier("network.http-implementation");class O{constructor(t){this.params=t}toString(){return this.params?Object.keys(this.params).map(t=>`${t}=${this.params[t]}`).join("&"):""}}let M=0;class q{constructor(t,n,r){h(this,"uid",M++);this.method=t,this.url=n,this.requestParams=r}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var n,r;const t=(r=(n=this.requestParams)==null?void 0:n.params)==null?void 0:r.toString();return t?`${this.url}${this.url.includes("?")?"&":"?"}${t}`:this.url}getBody(){var r,s;const t=(r=this.headers.get("Content-Type"))!=null?r:u,n=(s=this.requestParams)==null?void 0:s.body;return t===u&&n&&typeof n=="object"?JSON.stringify(n):n?`${n}`:null}}class g{constructor({body:t,headers:n,status:r,statusText:s}){h(this,"body");h(this,"headers");h(this,"status");h(this,"statusText");this.body=t,this.headers=n,this.status=r,this.statusText=s}}class f{constructor({headers:t,status:n,statusText:r,error:s}){h(this,"headers");h(this,"status");h(this,"statusText");h(this,"error");this.headers=t,this.status=n,this.statusText=r,this.error=s}}class F{constructor(t,n,r){this.headers=t,this.status=n,this.statusText=r}}var k=Object.defineProperty,D=Object.getOwnPropertyDescriptor,j=(e,t,n,r)=>{for(var s=r>1?void 0:r?D(t,n):t,o=e.length-1,i;o>=0;o--)(i=e[o])&&(s=(r?i(t,n,s):i(s))||s);return r&&s&&k(t,n,s),s},$=(e,t)=>(n,r)=>t(n,r,e);exports.HTTPService=class extends y.Disposable{constructor(n){super();h(this,"_interceptors",[]);h(this,"_pipe");this._http=n}registerHTTPInterceptor(n){if(this._interceptors.indexOf(n)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(n),this._interceptors=this._interceptors.sort((r,s)=>{var o,i;return((o=r.priority)!=null?o:0)-((i=s.priority)!=null?i:0)}),this._pipe=null,y.toDisposable(()=>y.remove(this._interceptors,n))}get(n,r){return this._request("GET",n,r)}post(n,r){return this._request("POST",n,r)}put(n,r){return this._request("PUT",n,r)}delete(n,r){return this._request("DELETE",n,r)}async _request(n,r,s){var p,v;const o=new w(s==null?void 0:s.headers),i=new O(s==null?void 0:s.params),c=new q(n,r,{headers:o,params:i,withCredentials:(p=s==null?void 0:s.withCredentials)!=null?p:!1,responseType:(v=s==null?void 0:s.responseType)!=null?v:"json",body:n==="GET"||s==null?void 0:s.body}),a=d.of(c).pipe(b.concatMap(m=>this._runInterceptorsAndImplementation(m))),l=await d.firstValueFrom(a);if(l instanceof g)return l;throw new Error(`${l.error}`)}_runInterceptorsAndImplementation(n){return this._pipe||(this._pipe=this._interceptors.map(r=>r.interceptor).reduceRight((r,s)=>B(r,s),(r,s)=>s(r))),this._pipe(n,r=>this._http.send(r))}};exports.HTTPService=j([$(0,L)],exports.HTTPService);function B(e,t){return(n,r)=>t(n,s=>e(s,r))}const G=200,V=300;var x=(e=>(e[e.Continue=100]="Continue",e[e.SwitchingProtocols=101]="SwitchingProtocols",e[e.Processing=102]="Processing",e[e.EarlyHints=103]="EarlyHints",e[e.Ok=200]="Ok",e[e.Created=201]="Created",e[e.Accepted=202]="Accepted",e[e.NonAuthoritativeInformation=203]="NonAuthoritativeInformation",e[e.NoContent=204]="NoContent",e[e.ResetContent=205]="ResetContent",e[e.PartialContent=206]="PartialContent",e[e.MultiStatus=207]="MultiStatus",e[e.AlreadyReported=208]="AlreadyReported",e[e.ImUsed=226]="ImUsed",e[e.MultipleChoices=300]="MultipleChoices",e[e.MovedPermanently=301]="MovedPermanently",e[e.Found=302]="Found",e[e.SeeOther=303]="SeeOther",e[e.NotModified=304]="NotModified",e[e.UseProxy=305]="UseProxy",e[e.Unused=306]="Unused",e[e.TemporaryRedirect=307]="TemporaryRedirect",e[e.PermanentRedirect=308]="PermanentRedirect",e[e.BadRequest=400]="BadRequest",e[e.Unauthorized=401]="Unauthorized",e[e.PaymentRequired=402]="PaymentRequired",e[e.Forbidden=403]="Forbidden",e[e.NotFound=404]="NotFound",e[e.MethodNotAllowed=405]="MethodNotAllowed",e[e.NotAcceptable=406]="NotAcceptable",e[e.ProxyAuthenticationRequired=407]="ProxyAuthenticationRequired",e[e.RequestTimeout=408]="RequestTimeout",e[e.Conflict=409]="Conflict",e[e.Gone=410]="Gone",e[e.LengthRequired=411]="LengthRequired",e[e.PreconditionFailed=412]="PreconditionFailed",e[e.PayloadTooLarge=413]="PayloadTooLarge",e[e.UriTooLong=414]="UriTooLong",e[e.UnsupportedMediaType=415]="UnsupportedMediaType",e[e.RangeNotSatisfiable=416]="RangeNotSatisfiable",e[e.ExpectationFailed=417]="ExpectationFailed",e[e.ImATeapot=418]="ImATeapot",e[e.MisdirectedRequest=421]="MisdirectedRequest",e[e.UnprocessableEntity=422]="UnprocessableEntity",e[e.Locked=423]="Locked",e[e.FailedDependency=424]="FailedDependency",e[e.TooEarly=425]="TooEarly",e[e.UpgradeRequired=426]="UpgradeRequired",e[e.PreconditionRequired=428]="PreconditionRequired",e[e.TooManyRequests=429]="TooManyRequests",e[e.RequestHeaderFieldsTooLarge=431]="RequestHeaderFieldsTooLarge",e[e.UnavailableForLegalReasons=451]="UnavailableForLegalReasons",e[e.InternalServerError=500]="InternalServerError",e[e.NotImplemented=501]="NotImplemented",e[e.BadGateway=502]="BadGateway",e[e.ServiceUnavailable=503]="ServiceUnavailable",e[e.GatewayTimeout=504]="GatewayTimeout",e[e.HttpVersionNotSupported=505]="HttpVersionNotSupported",e[e.VariantAlsoNegotiates=506]="VariantAlsoNegotiates",e[e.InsufficientStorage=507]="InsufficientStorage",e[e.LoopDetected=508]="LoopDetected",e[e.NotExtended=510]="NotExtended",e[e.NetworkAuthenticationRequired=511]="NetworkAuthenticationRequired",e))(x||{});class W{send(t){return new d.Observable(n=>{const r=new XMLHttpRequest;r.open(t.method,t.getUrlWithParams()),t.withCredentials&&(r.withCredentials=!0),t.headers.forEach((a,l)=>r.setRequestHeader(a,l.join(","))),t.headers.has("Accept")||r.setRequestHeader("Accept","application/json, text/plain, */*"),t.headers.has("Content-Type")||r.setRequestHeader("Content-Type","application/json;charset=UTF-8");const s=()=>{const a=r.statusText||"OK",l=new w(r.getAllResponseHeaders());return new F(l,r.status,a)},o=()=>{const{headers:a,statusText:l,status:p}=s(),{responseType:v}=t;let m=null,R=null;p!==x.NoContent&&(m=typeof r.response>"u"?r.responseText:r.response);let E=p>=G&&p<V;if(v==="json"&&typeof m=="string"){const I=m;try{m=m?JSON.parse(m):null}catch(N){E=!1,m=I,R=N}}E?n.next(new g({body:m,headers:a,status:p,statusText:l})):n.next(new f({error:R,headers:a,status:p,statusText:l}))},i=a=>{const l=new f({error:a,status:r.status||0,statusText:r.statusText||"Unknown Error",headers:s().headers});n.next(l)};r.addEventListener("load",o),r.addEventListener("error",i),r.addEventListener("abort",i),r.addEventListener("timeout",i);const c=t.getBody();return r.send(c),()=>{r.readyState!==r.DONE&&r.abort(),r.removeEventListener("load",o),r.removeEventListener("error",i),r.removeEventListener("abort",i),r.removeEventListener("timeout",i)}})}}const X=_.createIdentifier("univer.socket");class z extends y.Disposable{createSocket(t){try{const n=new WebSocket(t),r=new y.DisposableCollection;return{URL:t,close:(o,i)=>{n.close(o,i),r.dispose()},send:o=>{n.send(o)},open$:new d.Observable(o=>{const i=c=>o.next(c);n.addEventListener("open",i),r.add(y.toDisposable(()=>n.removeEventListener("open",i)))}).pipe(b.share()),close$:new d.Observable(o=>{const i=c=>o.next(c);n.addEventListener("close",i),r.add(y.toDisposable(()=>n.removeEventListener("close",i)))}).pipe(b.share()),error$:new d.Observable(o=>{const i=c=>o.next(c);n.addEventListener("error",i),r.add(y.toDisposable(()=>n.removeEventListener("error",i)))}).pipe(b.share()),message$:new d.Observable(o=>{const i=c=>o.next(c);n.addEventListener("message",i),r.add(y.toDisposable(()=>n.removeEventListener("message",i)))}).pipe(b.share())}}catch(n){return console.error(n),null}}}const J=2,Y=1e3,K=(e,t)=>t(e).pipe(b.retryWhen(n=>n.pipe(b.concatMap((r,s)=>s>J?d.throwError(r):d.of(r)),d.delay(Y)))),Q=(e,t)=>{const n=[],r=new Set,s=()=>{var o;for(;r.size<((o=t==null?void 0:t.maxParallel)!=null?o:1)&&n.length>0;){const i=n.shift();r.add(i),i()}};return(o,i)=>new d.Observable(c=>{const a=()=>i(o).subscribe({next:p=>c.next(p),error:p=>c.next(p),complete:()=>c.complete()}),l=()=>{r.delete(a),y.remove(n,a),s()};return n.push(a),s(),l})};exports.HTTPHeaders=w;exports.HTTPRequest=q;exports.HTTPResponse=g;exports.HTTPResponseError=f;exports.IHTTPImplementation=L;exports.ISocketService=X;exports.ThresholdInterceptorFactory=Q;exports.WebSocketService=z;exports.XHRHTTPImplementation=W;exports.retryInterceptor=K;
|
|
1
|
+
"use strict";var N=Object.defineProperty;var F=(e,n,r)=>n in e?N(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r;var d=(e,n,r)=>(F(e,typeof n!="symbol"?n+"":n,r),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@univerjs/core"),m=require("rxjs"),f=require("rxjs/operators"),x=require("@wendellhu/redi"),E="application/json";class g{constructor(n){d(this,"_headers",new Map);typeof n=="string"?this._handleHeadersString(n):n instanceof Headers?this._handleHeaders(n):n&&this._handleHeadersConstructorProps(n)}forEach(n){this._headers.forEach((r,t)=>n(t,r))}has(n){return!!this._headers.has(n.toLowerCase())}get(n){const r=n.toLowerCase();return this._headers.has(r)?this._headers.get(r):null}toHeadersInit(){var r;const n={};return this._headers.forEach((t,s)=>{n[s]=t.join(",")}),(r=n.Accept)!=null||(n.Accept="application/json, text/plain, */*"),n}_setHeader(n,r){const t=n.toLowerCase();this._headers.has(t)?this._headers.get(t).push(r.toString()):this._headers.set(t,[r.toString()])}_handleHeadersString(n){n.split(`
|
|
2
|
+
`).forEach(r=>{const[t,s]=r.split(":");t&&s&&this._setHeader(t,s)})}_handleHeadersConstructorProps(n){Object.keys(n).forEach(([r,t])=>{this._setHeader(r,t)})}_handleHeaders(n){n.forEach((r,t)=>{this._setHeader(t,r)})}}const I=x.createIdentifier("network.http-implementation");class k{constructor(n){this.params=n}toString(){return this.params?Object.keys(this.params).map(n=>`${n}=${this.params[n]}`).join("&"):""}}let U=0;class L{constructor(n,r,t){d(this,"uid",U++);this.method=n,this.url=r,this.requestParams=t}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var r,t;const n=(t=(r=this.requestParams)==null?void 0:r.params)==null?void 0:t.toString();return n?`${this.url}${this.url.includes("?")?"&":"?"}${n}`:this.url}getBody(){var t,s;const n=(t=this.headers.get("Content-Type"))!=null?t:E,r=(s=this.requestParams)==null?void 0:s.body;return n===E&&r&&typeof r=="object"?JSON.stringify(r):r?`${r}`:null}getHeadersInit(){return this.headers.toHeadersInit()}}class v{constructor({body:n,headers:r,status:t,statusText:s}){d(this,"body");d(this,"headers");d(this,"status");d(this,"statusText");this.body=n,this.headers=r,this.status=t,this.statusText=s}}class w{constructor({headers:n,status:r,statusText:t,error:s}){d(this,"headers");d(this,"status");d(this,"statusText");d(this,"error");this.headers=n,this.status=r,this.statusText=t,this.error=s}}class O{constructor(n,r,t){this.headers=n,this.status=r,this.statusText=t}}var M=Object.defineProperty,D=Object.getOwnPropertyDescriptor,j=(e,n,r,t)=>{for(var s=t>1?void 0:t?D(n,r):n,o=e.length-1,i;o>=0;o--)(i=e[o])&&(s=(t?i(n,r,s):i(s))||s);return t&&s&&M(n,r,s),s},B=(e,n)=>(r,t)=>n(r,t,e);exports.HTTPService=class extends y.Disposable{constructor(r){super();d(this,"_interceptors",[]);d(this,"_pipe");this._http=r}registerHTTPInterceptor(r){if(this._interceptors.indexOf(r)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(r),this._interceptors=this._interceptors.sort((t,s)=>{var o,i;return((o=t.priority)!=null?o:0)-((i=s.priority)!=null?i:0)}),this._pipe=null,y.toDisposable(()=>y.remove(this._interceptors,r))}get(r,t){return this._request("GET",r,t)}post(r,t){return this._request("POST",r,t)}put(r,t){return this._request("PUT",r,t)}delete(r,t){return this._request("DELETE",r,t)}async _request(r,t,s){var h,u;const o=new g(s==null?void 0:s.headers),i=new k(s==null?void 0:s.params),l=new L(r,t,{headers:o,params:i,withCredentials:(h=s==null?void 0:s.withCredentials)!=null?h:!1,responseType:(u=s==null?void 0:s.responseType)!=null?u:"json",body:r==="GET"||s==null?void 0:s.body}),a=m.of(l).pipe(f.concatMap(p=>this._runInterceptorsAndImplementation(p))),c=await m.firstValueFrom(a);if(c instanceof v)return c;throw new Error(`${c.error}`)}_runInterceptorsAndImplementation(r){return this._pipe||(this._pipe=this._interceptors.map(t=>t.interceptor).reduceRight((t,s)=>$(t,s),(t,s)=>s(t))),this._pipe(r,t=>this._http.send(t))}};exports.HTTPService=j([B(0,I)],exports.HTTPService);function $(e,n){return(r,t)=>n(r,s=>e(s,t))}const G=200,W=300;var b=(e=>(e[e.Continue=100]="Continue",e[e.SwitchingProtocols=101]="SwitchingProtocols",e[e.Processing=102]="Processing",e[e.EarlyHints=103]="EarlyHints",e[e.Ok=200]="Ok",e[e.Created=201]="Created",e[e.Accepted=202]="Accepted",e[e.NonAuthoritativeInformation=203]="NonAuthoritativeInformation",e[e.NoContent=204]="NoContent",e[e.ResetContent=205]="ResetContent",e[e.PartialContent=206]="PartialContent",e[e.MultiStatus=207]="MultiStatus",e[e.AlreadyReported=208]="AlreadyReported",e[e.ImUsed=226]="ImUsed",e[e.MultipleChoices=300]="MultipleChoices",e[e.MovedPermanently=301]="MovedPermanently",e[e.Found=302]="Found",e[e.SeeOther=303]="SeeOther",e[e.NotModified=304]="NotModified",e[e.UseProxy=305]="UseProxy",e[e.Unused=306]="Unused",e[e.TemporaryRedirect=307]="TemporaryRedirect",e[e.PermanentRedirect=308]="PermanentRedirect",e[e.BadRequest=400]="BadRequest",e[e.Unauthorized=401]="Unauthorized",e[e.PaymentRequired=402]="PaymentRequired",e[e.Forbidden=403]="Forbidden",e[e.NotFound=404]="NotFound",e[e.MethodNotAllowed=405]="MethodNotAllowed",e[e.NotAcceptable=406]="NotAcceptable",e[e.ProxyAuthenticationRequired=407]="ProxyAuthenticationRequired",e[e.RequestTimeout=408]="RequestTimeout",e[e.Conflict=409]="Conflict",e[e.Gone=410]="Gone",e[e.LengthRequired=411]="LengthRequired",e[e.PreconditionFailed=412]="PreconditionFailed",e[e.PayloadTooLarge=413]="PayloadTooLarge",e[e.UriTooLong=414]="UriTooLong",e[e.UnsupportedMediaType=415]="UnsupportedMediaType",e[e.RangeNotSatisfiable=416]="RangeNotSatisfiable",e[e.ExpectationFailed=417]="ExpectationFailed",e[e.ImATeapot=418]="ImATeapot",e[e.MisdirectedRequest=421]="MisdirectedRequest",e[e.UnprocessableEntity=422]="UnprocessableEntity",e[e.Locked=423]="Locked",e[e.FailedDependency=424]="FailedDependency",e[e.TooEarly=425]="TooEarly",e[e.UpgradeRequired=426]="UpgradeRequired",e[e.PreconditionRequired=428]="PreconditionRequired",e[e.TooManyRequests=429]="TooManyRequests",e[e.RequestHeaderFieldsTooLarge=431]="RequestHeaderFieldsTooLarge",e[e.UnavailableForLegalReasons=451]="UnavailableForLegalReasons",e[e.InternalServerError=500]="InternalServerError",e[e.NotImplemented=501]="NotImplemented",e[e.BadGateway=502]="BadGateway",e[e.ServiceUnavailable=503]="ServiceUnavailable",e[e.GatewayTimeout=504]="GatewayTimeout",e[e.HttpVersionNotSupported=505]="HttpVersionNotSupported",e[e.VariantAlsoNegotiates=506]="VariantAlsoNegotiates",e[e.InsufficientStorage=507]="InsufficientStorage",e[e.LoopDetected=508]="LoopDetected",e[e.NotExtended=510]="NotExtended",e[e.NetworkAuthenticationRequired=511]="NetworkAuthenticationRequired",e))(b||{});class V{send(n){return new m.Observable(r=>{const t=new AbortController;return this._send(n,r,t).then(()=>{},s=>{r.error(new w({error:s}))}),()=>t.abort()})}async _send(n,r,t){var h,u;let s;try{const p=this._parseFetchParamsFromRequest(n);s=await fetch(n.getUrlWithParams(),{signal:t.signal,...p})}catch(p){r.error(new w({error:p,status:(h=p.status)!=null?h:0,statusText:(u=p.statusText)!=null?u:"Unknown Error",headers:p.headers}));return}const o=new g(s.headers),i=s.status,l=s.statusText;let a=null;s.body&&(a=await this._readBody(n,s,r)),i>=b.Ok&&i<b.MultipleChoices?r.next(new v({body:a,headers:o,status:i,statusText:l})):r.error(new w({error:a,status:i,statusText:l,headers:o})),r.complete()}async _readBody(n,r,t){var a;const s=[],o=r.body.getReader();let i=0;for(;;){const{done:c,value:h}=await o.read();if(c)break;s.push(h),i+=h.length}const l=X(s,i);try{const c=(a=r.headers.get("content-type"))!=null?a:"";return J(n,l,c)}catch(c){return t.error(new w({error:c,status:r.status,statusText:r.statusText,headers:new g(r.headers)})),null}}_parseFetchParamsFromRequest(n){return{method:n.method,headers:n.getHeadersInit(),body:n.getBody(),credentials:n.withCredentials?"include":void 0}}}function X(e,n){const r=new Uint8Array(n);let t=0;for(const s of e)r.set(s,t),t+=s.length;return r}const z=/^\)\]\}',?\n/;function J(e,n,r){switch(e.responseType){case"json":const t=new TextDecoder().decode(n).replace(z,"");return t===""?null:JSON.parse(t);case"text":return new TextDecoder().decode(n);case"blob":return new Blob([n],{type:r});case"arraybuffer":return n.buffer;default:throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}class Y{send(n){return new m.Observable(r=>{const t=new XMLHttpRequest;t.open(n.method,n.getUrlWithParams()),n.withCredentials&&(t.withCredentials=!0),n.headers.forEach((a,c)=>t.setRequestHeader(a,c.join(","))),n.headers.has("Accept")||t.setRequestHeader("Accept","application/json, text/plain, */*"),n.headers.has("Content-Type")||t.setRequestHeader("Content-Type","application/json;charset=UTF-8");const s=()=>{const a=t.statusText||"OK",c=new g(t.getAllResponseHeaders());return new O(c,t.status,a)},o=()=>{const{headers:a,statusText:c,status:h}=s(),{responseType:u}=n;let p=null,_=null;h!==b.NoContent&&(p=typeof t.response>"u"?t.responseText:t.response);let R=h>=G&&h<W;if(u==="json"&&typeof p=="string"){const q=p;try{p=p?JSON.parse(p):null}catch(A){R=!1,p=q,_=A}}R?r.next(new v({body:p,headers:a,status:h,statusText:c})):r.next(new w({error:_,headers:a,status:h,statusText:c}))},i=a=>{const c=new w({error:a,status:t.status||0,statusText:t.statusText||"Unknown Error",headers:s().headers});r.next(c)};t.addEventListener("load",o),t.addEventListener("error",i),t.addEventListener("abort",i),t.addEventListener("timeout",i);const l=n.getBody();return t.send(l),()=>{t.readyState!==t.DONE&&t.abort(),t.removeEventListener("load",o),t.removeEventListener("error",i),t.removeEventListener("abort",i),t.removeEventListener("timeout",i)}})}}const K=x.createIdentifier("univer.socket");class Q extends y.Disposable{createSocket(n){try{const r=new WebSocket(n),t=new y.DisposableCollection;return{URL:n,close:(o,i)=>{r.close(o,i),t.dispose()},send:o=>{r.send(o)},open$:new m.Observable(o=>{const i=l=>o.next(l);r.addEventListener("open",i),t.add(y.toDisposable(()=>r.removeEventListener("open",i)))}).pipe(f.share()),close$:new m.Observable(o=>{const i=l=>o.next(l);r.addEventListener("close",i),t.add(y.toDisposable(()=>r.removeEventListener("close",i)))}).pipe(f.share()),error$:new m.Observable(o=>{const i=l=>o.next(l);r.addEventListener("error",i),t.add(y.toDisposable(()=>r.removeEventListener("error",i)))}).pipe(f.share()),message$:new m.Observable(o=>{const i=l=>o.next(l);r.addEventListener("message",i),t.add(y.toDisposable(()=>r.removeEventListener("message",i)))}).pipe(f.share())}}catch(r){return console.error(r),null}}}const Z=2,P=1e3,H=(e,n)=>n(e).pipe(f.retryWhen(r=>r.pipe(f.concatMap((t,s)=>s>Z?m.throwError(t):m.of(t)),m.delay(P)))),S=(e,n)=>{const r=[],t=new Set,s=()=>{var o;for(;t.size<((o=n==null?void 0:n.maxParallel)!=null?o:1)&&r.length>0;){const i=r.shift();t.add(i),i()}};return(o,i)=>new m.Observable(l=>{const a=()=>i(o).subscribe({next:h=>l.next(h),error:h=>l.next(h),complete:()=>l.complete()}),c=()=>{t.delete(a),y.remove(r,a),s()};return r.push(a),s(),c})};exports.FetchHTTPImplementation=V;exports.HTTPHeaders=g;exports.HTTPRequest=L;exports.HTTPResponse=v;exports.HTTPResponseError=w;exports.HTTPStatusCode=b;exports.IHTTPImplementation=I;exports.ISocketService=K;exports.ThresholdInterceptorFactory=S;exports.WebSocketService=Q;exports.XHRHTTPImplementation=Y;exports.retryInterceptor=H;
|
package/lib/es/index.js
CHANGED
|
@@ -1,51 +1,69 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { Disposable as
|
|
5
|
-
import { of as
|
|
6
|
-
import { concatMap as
|
|
1
|
+
var k = Object.defineProperty;
|
|
2
|
+
var M = (e, r, t) => r in e ? k(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t;
|
|
3
|
+
var d = (e, r, t) => (M(e, typeof r != "symbol" ? r + "" : r, t), t);
|
|
4
|
+
import { Disposable as L, toDisposable as w, remove as I, DisposableCollection as O } from "@univerjs/core";
|
|
5
|
+
import { of as A, firstValueFrom as j, Observable as m, throwError as D, delay as B } from "rxjs";
|
|
6
|
+
import { concatMap as N, share as u, retryWhen as $ } from "rxjs/operators";
|
|
7
7
|
import { createIdentifier as q } from "@wendellhu/redi";
|
|
8
|
-
const
|
|
9
|
-
class
|
|
10
|
-
constructor(
|
|
11
|
-
|
|
12
|
-
typeof
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}) : t && Object.keys(t).forEach(([n, r]) => {
|
|
17
|
-
this._setHeader(n, r);
|
|
18
|
-
});
|
|
8
|
+
const x = "application/json";
|
|
9
|
+
class g {
|
|
10
|
+
constructor(r) {
|
|
11
|
+
d(this, "_headers", /* @__PURE__ */ new Map());
|
|
12
|
+
typeof r == "string" ? this._handleHeadersString(r) : r instanceof Headers ? this._handleHeaders(r) : r && this._handleHeadersConstructorProps(r);
|
|
13
|
+
}
|
|
14
|
+
forEach(r) {
|
|
15
|
+
this._headers.forEach((t, n) => r(n, t));
|
|
19
16
|
}
|
|
20
|
-
|
|
21
|
-
this._headers.
|
|
17
|
+
has(r) {
|
|
18
|
+
return !!this._headers.has(r.toLowerCase());
|
|
22
19
|
}
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
get(r) {
|
|
21
|
+
const t = r.toLowerCase();
|
|
22
|
+
return this._headers.has(t) ? this._headers.get(t) : null;
|
|
25
23
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
toHeadersInit() {
|
|
25
|
+
var t;
|
|
26
|
+
const r = {};
|
|
27
|
+
return this._headers.forEach((n, s) => {
|
|
28
|
+
r[s] = n.join(",");
|
|
29
|
+
}), (t = r.Accept) != null || (r.Accept = "application/json, text/plain, */*"), r;
|
|
29
30
|
}
|
|
30
|
-
_setHeader(
|
|
31
|
-
const
|
|
32
|
-
this._headers.has(
|
|
31
|
+
_setHeader(r, t) {
|
|
32
|
+
const n = r.toLowerCase();
|
|
33
|
+
this._headers.has(n) ? this._headers.get(n).push(t.toString()) : this._headers.set(n, [t.toString()]);
|
|
34
|
+
}
|
|
35
|
+
_handleHeadersString(r) {
|
|
36
|
+
r.split(`
|
|
37
|
+
`).forEach((t) => {
|
|
38
|
+
const [n, s] = t.split(":");
|
|
39
|
+
n && s && this._setHeader(n, s);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
_handleHeadersConstructorProps(r) {
|
|
43
|
+
Object.keys(r).forEach(([t, n]) => {
|
|
44
|
+
this._setHeader(t, n);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
_handleHeaders(r) {
|
|
48
|
+
r.forEach((t, n) => {
|
|
49
|
+
this._setHeader(n, t);
|
|
50
|
+
});
|
|
33
51
|
}
|
|
34
52
|
}
|
|
35
53
|
const G = q("network.http-implementation");
|
|
36
54
|
class V {
|
|
37
|
-
constructor(
|
|
38
|
-
this.params =
|
|
55
|
+
constructor(r) {
|
|
56
|
+
this.params = r;
|
|
39
57
|
}
|
|
40
58
|
toString() {
|
|
41
|
-
return this.params ? Object.keys(this.params).map((
|
|
59
|
+
return this.params ? Object.keys(this.params).map((r) => `${r}=${this.params[r]}`).join("&") : "";
|
|
42
60
|
}
|
|
43
61
|
}
|
|
44
62
|
let W = 0;
|
|
45
|
-
class
|
|
46
|
-
constructor(t, n
|
|
47
|
-
|
|
48
|
-
this.method =
|
|
63
|
+
class X {
|
|
64
|
+
constructor(r, t, n) {
|
|
65
|
+
d(this, "uid", W++);
|
|
66
|
+
this.method = r, this.url = t, this.requestParams = n;
|
|
49
67
|
}
|
|
50
68
|
get headers() {
|
|
51
69
|
return this.requestParams.headers;
|
|
@@ -57,61 +75,64 @@ class z {
|
|
|
57
75
|
return this.requestParams.responseType;
|
|
58
76
|
}
|
|
59
77
|
getUrlWithParams() {
|
|
60
|
-
var
|
|
61
|
-
const
|
|
62
|
-
return
|
|
78
|
+
var t, n;
|
|
79
|
+
const r = (n = (t = this.requestParams) == null ? void 0 : t.params) == null ? void 0 : n.toString();
|
|
80
|
+
return r ? `${this.url}${this.url.includes("?") ? "&" : "?"}${r}` : this.url;
|
|
63
81
|
}
|
|
64
82
|
getBody() {
|
|
65
|
-
var
|
|
66
|
-
const
|
|
67
|
-
return
|
|
83
|
+
var n, s;
|
|
84
|
+
const r = (n = this.headers.get("Content-Type")) != null ? n : x, t = (s = this.requestParams) == null ? void 0 : s.body;
|
|
85
|
+
return r === x && t && typeof t == "object" ? JSON.stringify(t) : t ? `${t}` : null;
|
|
68
86
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
constructor({
|
|
72
|
-
body: t,
|
|
73
|
-
headers: n,
|
|
74
|
-
status: r,
|
|
75
|
-
statusText: i
|
|
76
|
-
}) {
|
|
77
|
-
h(this, "body");
|
|
78
|
-
h(this, "headers");
|
|
79
|
-
h(this, "status");
|
|
80
|
-
h(this, "statusText");
|
|
81
|
-
this.body = t, this.headers = n, this.status = r, this.statusText = i;
|
|
87
|
+
getHeadersInit() {
|
|
88
|
+
return this.headers.toHeadersInit();
|
|
82
89
|
}
|
|
83
90
|
}
|
|
84
91
|
class R {
|
|
85
92
|
constructor({
|
|
93
|
+
body: r,
|
|
86
94
|
headers: t,
|
|
87
95
|
status: n,
|
|
88
|
-
statusText:
|
|
89
|
-
|
|
96
|
+
statusText: s
|
|
97
|
+
}) {
|
|
98
|
+
d(this, "body");
|
|
99
|
+
d(this, "headers");
|
|
100
|
+
d(this, "status");
|
|
101
|
+
d(this, "statusText");
|
|
102
|
+
this.body = r, this.headers = t, this.status = n, this.statusText = s;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
class f {
|
|
106
|
+
constructor({
|
|
107
|
+
headers: r,
|
|
108
|
+
status: t,
|
|
109
|
+
statusText: n,
|
|
110
|
+
error: s
|
|
90
111
|
}) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
this.headers =
|
|
112
|
+
d(this, "headers");
|
|
113
|
+
d(this, "status");
|
|
114
|
+
d(this, "statusText");
|
|
115
|
+
d(this, "error");
|
|
116
|
+
this.headers = r, this.status = t, this.statusText = n, this.error = s;
|
|
96
117
|
}
|
|
97
118
|
}
|
|
98
|
-
class
|
|
99
|
-
constructor(t, n
|
|
100
|
-
this.headers =
|
|
119
|
+
class z {
|
|
120
|
+
constructor(r, t, n) {
|
|
121
|
+
this.headers = r, this.status = t, this.statusText = n;
|
|
101
122
|
}
|
|
102
123
|
}
|
|
103
|
-
var
|
|
104
|
-
for (var
|
|
105
|
-
(
|
|
106
|
-
return
|
|
107
|
-
}, Q = (e,
|
|
108
|
-
let
|
|
109
|
-
constructor(
|
|
124
|
+
var J = Object.defineProperty, Y = Object.getOwnPropertyDescriptor, K = (e, r, t, n) => {
|
|
125
|
+
for (var s = n > 1 ? void 0 : n ? Y(r, t) : r, o = e.length - 1, i; o >= 0; o--)
|
|
126
|
+
(i = e[o]) && (s = (n ? i(r, t, s) : i(s)) || s);
|
|
127
|
+
return n && s && J(r, t, s), s;
|
|
128
|
+
}, Q = (e, r) => (t, n) => r(t, n, e);
|
|
129
|
+
let b = class extends L {
|
|
130
|
+
constructor(r) {
|
|
110
131
|
super();
|
|
111
|
-
|
|
132
|
+
d(this, "_interceptors", []);
|
|
112
133
|
// eslint-disable-next-line ts/no-explicit-any
|
|
113
|
-
|
|
114
|
-
this._http =
|
|
134
|
+
d(this, "_pipe");
|
|
135
|
+
this._http = r;
|
|
115
136
|
}
|
|
116
137
|
/**
|
|
117
138
|
* Register an HTTP interceptor.
|
|
@@ -119,185 +140,286 @@ let _ = class extends b {
|
|
|
119
140
|
* @param interceptor the http interceptor
|
|
120
141
|
* @returns a disposable handler to remove the interceptor
|
|
121
142
|
*/
|
|
122
|
-
registerHTTPInterceptor(
|
|
123
|
-
if (this._interceptors.indexOf(
|
|
143
|
+
registerHTTPInterceptor(r) {
|
|
144
|
+
if (this._interceptors.indexOf(r) !== -1)
|
|
124
145
|
throw new Error("[HTTPService]: The interceptor has already been registered!");
|
|
125
|
-
return this._interceptors.push(
|
|
126
|
-
var
|
|
127
|
-
return ((
|
|
128
|
-
}), this._pipe = null,
|
|
146
|
+
return this._interceptors.push(r), this._interceptors = this._interceptors.sort((t, n) => {
|
|
147
|
+
var s, o;
|
|
148
|
+
return ((s = t.priority) != null ? s : 0) - ((o = n.priority) != null ? o : 0);
|
|
149
|
+
}), this._pipe = null, w(() => I(this._interceptors, r));
|
|
129
150
|
}
|
|
130
|
-
get(
|
|
131
|
-
return this._request("GET",
|
|
151
|
+
get(r, t) {
|
|
152
|
+
return this._request("GET", r, t);
|
|
132
153
|
}
|
|
133
|
-
post(
|
|
134
|
-
return this._request("POST",
|
|
154
|
+
post(r, t) {
|
|
155
|
+
return this._request("POST", r, t);
|
|
135
156
|
}
|
|
136
|
-
put(
|
|
137
|
-
return this._request("PUT",
|
|
157
|
+
put(r, t) {
|
|
158
|
+
return this._request("PUT", r, t);
|
|
138
159
|
}
|
|
139
|
-
delete(
|
|
140
|
-
return this._request("DELETE",
|
|
160
|
+
delete(r, t) {
|
|
161
|
+
return this._request("DELETE", r, t);
|
|
141
162
|
}
|
|
142
163
|
/** The HTTP request implementations */
|
|
143
|
-
async _request(t, n
|
|
144
|
-
var l,
|
|
145
|
-
const
|
|
146
|
-
headers:
|
|
164
|
+
async _request(r, t, n) {
|
|
165
|
+
var l, h;
|
|
166
|
+
const s = new g(n == null ? void 0 : n.headers), o = new V(n == null ? void 0 : n.params), i = new X(r, t, {
|
|
167
|
+
headers: s,
|
|
147
168
|
params: o,
|
|
148
|
-
withCredentials: (l =
|
|
169
|
+
withCredentials: (l = n == null ? void 0 : n.withCredentials) != null ? l : !1,
|
|
149
170
|
// default value for withCredentials is false by MDN
|
|
150
|
-
responseType: (
|
|
151
|
-
body:
|
|
152
|
-
}),
|
|
153
|
-
|
|
154
|
-
),
|
|
155
|
-
if (
|
|
156
|
-
return
|
|
157
|
-
throw new Error(`${
|
|
171
|
+
responseType: (h = n == null ? void 0 : n.responseType) != null ? h : "json",
|
|
172
|
+
body: r === "GET" || n == null ? void 0 : n.body
|
|
173
|
+
}), c = A(i).pipe(
|
|
174
|
+
N((y) => this._runInterceptorsAndImplementation(y))
|
|
175
|
+
), a = await j(c);
|
|
176
|
+
if (a instanceof R)
|
|
177
|
+
return a;
|
|
178
|
+
throw new Error(`${a.error}`);
|
|
158
179
|
}
|
|
159
180
|
// eslint-disable-next-line ts/no-explicit-any
|
|
160
|
-
_runInterceptorsAndImplementation(
|
|
161
|
-
return this._pipe || (this._pipe = this._interceptors.map((
|
|
162
|
-
(
|
|
163
|
-
(
|
|
181
|
+
_runInterceptorsAndImplementation(r) {
|
|
182
|
+
return this._pipe || (this._pipe = this._interceptors.map((t) => t.interceptor).reduceRight(
|
|
183
|
+
(t, n) => Z(t, n),
|
|
184
|
+
(t, n) => n(t)
|
|
164
185
|
)), this._pipe(
|
|
165
|
-
|
|
166
|
-
(
|
|
186
|
+
r,
|
|
187
|
+
(t) => this._http.send(t)
|
|
167
188
|
/* final handler */
|
|
168
189
|
);
|
|
169
190
|
}
|
|
170
191
|
};
|
|
171
|
-
|
|
192
|
+
b = K([
|
|
172
193
|
Q(0, G)
|
|
173
|
-
],
|
|
174
|
-
function Z(e,
|
|
175
|
-
return (
|
|
194
|
+
], b);
|
|
195
|
+
function Z(e, r) {
|
|
196
|
+
return (t, n) => r(t, (s) => e(s, n));
|
|
176
197
|
}
|
|
177
|
-
const P = 200,
|
|
178
|
-
var
|
|
179
|
-
class
|
|
180
|
-
send(
|
|
181
|
-
return new m((
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
198
|
+
const P = 200, H = 300;
|
|
199
|
+
var _ = /* @__PURE__ */ ((e) => (e[e.Continue = 100] = "Continue", e[e.SwitchingProtocols = 101] = "SwitchingProtocols", e[e.Processing = 102] = "Processing", e[e.EarlyHints = 103] = "EarlyHints", e[e.Ok = 200] = "Ok", e[e.Created = 201] = "Created", e[e.Accepted = 202] = "Accepted", e[e.NonAuthoritativeInformation = 203] = "NonAuthoritativeInformation", e[e.NoContent = 204] = "NoContent", e[e.ResetContent = 205] = "ResetContent", e[e.PartialContent = 206] = "PartialContent", e[e.MultiStatus = 207] = "MultiStatus", e[e.AlreadyReported = 208] = "AlreadyReported", e[e.ImUsed = 226] = "ImUsed", e[e.MultipleChoices = 300] = "MultipleChoices", e[e.MovedPermanently = 301] = "MovedPermanently", e[e.Found = 302] = "Found", e[e.SeeOther = 303] = "SeeOther", e[e.NotModified = 304] = "NotModified", e[e.UseProxy = 305] = "UseProxy", e[e.Unused = 306] = "Unused", e[e.TemporaryRedirect = 307] = "TemporaryRedirect", e[e.PermanentRedirect = 308] = "PermanentRedirect", e[e.BadRequest = 400] = "BadRequest", e[e.Unauthorized = 401] = "Unauthorized", e[e.PaymentRequired = 402] = "PaymentRequired", e[e.Forbidden = 403] = "Forbidden", e[e.NotFound = 404] = "NotFound", e[e.MethodNotAllowed = 405] = "MethodNotAllowed", e[e.NotAcceptable = 406] = "NotAcceptable", e[e.ProxyAuthenticationRequired = 407] = "ProxyAuthenticationRequired", e[e.RequestTimeout = 408] = "RequestTimeout", e[e.Conflict = 409] = "Conflict", e[e.Gone = 410] = "Gone", e[e.LengthRequired = 411] = "LengthRequired", e[e.PreconditionFailed = 412] = "PreconditionFailed", e[e.PayloadTooLarge = 413] = "PayloadTooLarge", e[e.UriTooLong = 414] = "UriTooLong", e[e.UnsupportedMediaType = 415] = "UnsupportedMediaType", e[e.RangeNotSatisfiable = 416] = "RangeNotSatisfiable", e[e.ExpectationFailed = 417] = "ExpectationFailed", e[e.ImATeapot = 418] = "ImATeapot", e[e.MisdirectedRequest = 421] = "MisdirectedRequest", e[e.UnprocessableEntity = 422] = "UnprocessableEntity", e[e.Locked = 423] = "Locked", e[e.FailedDependency = 424] = "FailedDependency", e[e.TooEarly = 425] = "TooEarly", e[e.UpgradeRequired = 426] = "UpgradeRequired", e[e.PreconditionRequired = 428] = "PreconditionRequired", e[e.TooManyRequests = 429] = "TooManyRequests", e[e.RequestHeaderFieldsTooLarge = 431] = "RequestHeaderFieldsTooLarge", e[e.UnavailableForLegalReasons = 451] = "UnavailableForLegalReasons", e[e.InternalServerError = 500] = "InternalServerError", e[e.NotImplemented = 501] = "NotImplemented", e[e.BadGateway = 502] = "BadGateway", e[e.ServiceUnavailable = 503] = "ServiceUnavailable", e[e.GatewayTimeout = 504] = "GatewayTimeout", e[e.HttpVersionNotSupported = 505] = "HttpVersionNotSupported", e[e.VariantAlsoNegotiates = 506] = "VariantAlsoNegotiates", e[e.InsufficientStorage = 507] = "InsufficientStorage", e[e.LoopDetected = 508] = "LoopDetected", e[e.NotExtended = 510] = "NotExtended", e[e.NetworkAuthenticationRequired = 511] = "NetworkAuthenticationRequired", e))(_ || {});
|
|
200
|
+
class ae {
|
|
201
|
+
send(r) {
|
|
202
|
+
return new m((t) => {
|
|
203
|
+
const n = new AbortController();
|
|
204
|
+
return this._send(r, t, n).then(() => {
|
|
205
|
+
}, (s) => {
|
|
206
|
+
t.error(new f({
|
|
207
|
+
error: s
|
|
208
|
+
}));
|
|
209
|
+
}), () => n.abort();
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
async _send(r, t, n) {
|
|
213
|
+
var h, y;
|
|
214
|
+
let s;
|
|
215
|
+
try {
|
|
216
|
+
const p = this._parseFetchParamsFromRequest(r);
|
|
217
|
+
s = await fetch(r.getUrlWithParams(), {
|
|
218
|
+
signal: n.signal,
|
|
219
|
+
...p
|
|
220
|
+
});
|
|
221
|
+
} catch (p) {
|
|
222
|
+
t.error(new f({
|
|
223
|
+
error: p,
|
|
224
|
+
status: (h = p.status) != null ? h : 0,
|
|
225
|
+
statusText: (y = p.statusText) != null ? y : "Unknown Error",
|
|
226
|
+
headers: p.headers
|
|
227
|
+
}));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const o = new g(s.headers), i = s.status, c = s.statusText;
|
|
231
|
+
let a = null;
|
|
232
|
+
s.body && (a = await this._readBody(r, s, t)), i >= _.Ok && i < _.MultipleChoices ? t.next(new R({
|
|
233
|
+
body: a,
|
|
234
|
+
headers: o,
|
|
235
|
+
status: i,
|
|
236
|
+
statusText: c
|
|
237
|
+
})) : t.error(new f({
|
|
238
|
+
error: a,
|
|
239
|
+
status: i,
|
|
240
|
+
statusText: c,
|
|
241
|
+
headers: o
|
|
242
|
+
})), t.complete();
|
|
243
|
+
}
|
|
244
|
+
async _readBody(r, t, n) {
|
|
245
|
+
var a;
|
|
246
|
+
const s = [], o = t.body.getReader();
|
|
247
|
+
let i = 0;
|
|
248
|
+
for (; ; ) {
|
|
249
|
+
const { done: l, value: h } = await o.read();
|
|
250
|
+
if (l)
|
|
251
|
+
break;
|
|
252
|
+
s.push(h), i += h.length;
|
|
253
|
+
}
|
|
254
|
+
const c = S(s, i);
|
|
255
|
+
try {
|
|
256
|
+
const l = (a = t.headers.get("content-type")) != null ? a : "";
|
|
257
|
+
return T(r, c, l);
|
|
258
|
+
} catch (l) {
|
|
259
|
+
return n.error(new f({
|
|
260
|
+
error: l,
|
|
261
|
+
status: t.status,
|
|
262
|
+
statusText: t.statusText,
|
|
263
|
+
headers: new g(t.headers)
|
|
264
|
+
})), null;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
_parseFetchParamsFromRequest(r) {
|
|
268
|
+
return {
|
|
269
|
+
method: r.method,
|
|
270
|
+
headers: r.getHeadersInit(),
|
|
271
|
+
body: r.getBody(),
|
|
272
|
+
credentials: r.withCredentials ? "include" : void 0
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function S(e, r) {
|
|
277
|
+
const t = new Uint8Array(r);
|
|
278
|
+
let n = 0;
|
|
279
|
+
for (const s of e)
|
|
280
|
+
t.set(s, n), n += s.length;
|
|
281
|
+
return t;
|
|
282
|
+
}
|
|
283
|
+
const C = /^\)\]\}',?\n/;
|
|
284
|
+
function T(e, r, t) {
|
|
285
|
+
switch (e.responseType) {
|
|
286
|
+
case "json":
|
|
287
|
+
const n = new TextDecoder().decode(r).replace(C, "");
|
|
288
|
+
return n === "" ? null : JSON.parse(n);
|
|
289
|
+
case "text":
|
|
290
|
+
return new TextDecoder().decode(r);
|
|
291
|
+
case "blob":
|
|
292
|
+
return new Blob([r], { type: t });
|
|
293
|
+
case "arraybuffer":
|
|
294
|
+
return r.buffer;
|
|
295
|
+
default:
|
|
296
|
+
throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
class ce {
|
|
300
|
+
send(r) {
|
|
301
|
+
return new m((t) => {
|
|
302
|
+
const n = new XMLHttpRequest();
|
|
303
|
+
n.open(r.method, r.getUrlWithParams()), r.withCredentials && (n.withCredentials = !0), r.headers.forEach((a, l) => n.setRequestHeader(a, l.join(","))), r.headers.has("Accept") || n.setRequestHeader("Accept", "application/json, text/plain, */*"), r.headers.has("Content-Type") || n.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
|
304
|
+
const s = () => {
|
|
305
|
+
const a = n.statusText || "OK", l = new g(n.getAllResponseHeaders());
|
|
306
|
+
return new z(l, n.status, a);
|
|
187
307
|
}, o = () => {
|
|
188
|
-
const { headers:
|
|
189
|
-
let
|
|
190
|
-
|
|
191
|
-
let
|
|
192
|
-
if (
|
|
193
|
-
const U =
|
|
308
|
+
const { headers: a, statusText: l, status: h } = s(), { responseType: y } = r;
|
|
309
|
+
let p = null, E = null;
|
|
310
|
+
h !== _.NoContent && (p = typeof n.response > "u" ? n.responseText : n.response);
|
|
311
|
+
let v = h >= P && h < H;
|
|
312
|
+
if (y === "json" && typeof p == "string") {
|
|
313
|
+
const U = p;
|
|
194
314
|
try {
|
|
195
|
-
|
|
196
|
-
} catch (
|
|
197
|
-
|
|
315
|
+
p = p ? JSON.parse(p) : null;
|
|
316
|
+
} catch (F) {
|
|
317
|
+
v = !1, p = U, E = F;
|
|
198
318
|
}
|
|
199
319
|
}
|
|
200
|
-
|
|
201
|
-
new
|
|
202
|
-
body:
|
|
203
|
-
headers:
|
|
204
|
-
status:
|
|
320
|
+
v ? t.next(
|
|
321
|
+
new R({
|
|
322
|
+
body: p,
|
|
323
|
+
headers: a,
|
|
324
|
+
status: h,
|
|
205
325
|
statusText: l
|
|
206
326
|
})
|
|
207
|
-
) :
|
|
208
|
-
new
|
|
209
|
-
error:
|
|
210
|
-
headers:
|
|
211
|
-
status:
|
|
327
|
+
) : t.next(
|
|
328
|
+
new f({
|
|
329
|
+
error: E,
|
|
330
|
+
headers: a,
|
|
331
|
+
status: h,
|
|
212
332
|
statusText: l
|
|
213
333
|
})
|
|
214
334
|
);
|
|
215
|
-
},
|
|
216
|
-
const l = new
|
|
217
|
-
error:
|
|
218
|
-
status:
|
|
219
|
-
statusText:
|
|
220
|
-
headers:
|
|
335
|
+
}, i = (a) => {
|
|
336
|
+
const l = new f({
|
|
337
|
+
error: a,
|
|
338
|
+
status: n.status || 0,
|
|
339
|
+
statusText: n.statusText || "Unknown Error",
|
|
340
|
+
headers: s().headers
|
|
221
341
|
});
|
|
222
|
-
|
|
342
|
+
t.next(l);
|
|
223
343
|
};
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
return
|
|
227
|
-
|
|
344
|
+
n.addEventListener("load", o), n.addEventListener("error", i), n.addEventListener("abort", i), n.addEventListener("timeout", i);
|
|
345
|
+
const c = r.getBody();
|
|
346
|
+
return n.send(c), () => {
|
|
347
|
+
n.readyState !== n.DONE && n.abort(), n.removeEventListener("load", o), n.removeEventListener("error", i), n.removeEventListener("abort", i), n.removeEventListener("timeout", i);
|
|
228
348
|
};
|
|
229
349
|
});
|
|
230
350
|
}
|
|
231
351
|
}
|
|
232
|
-
const
|
|
233
|
-
class
|
|
234
|
-
createSocket(
|
|
352
|
+
const le = q("univer.socket");
|
|
353
|
+
class he extends L {
|
|
354
|
+
createSocket(r) {
|
|
235
355
|
try {
|
|
236
|
-
const
|
|
356
|
+
const t = new WebSocket(r), n = new O();
|
|
237
357
|
return {
|
|
238
|
-
URL:
|
|
239
|
-
close: (o,
|
|
240
|
-
|
|
358
|
+
URL: r,
|
|
359
|
+
close: (o, i) => {
|
|
360
|
+
t.close(o, i), n.dispose();
|
|
241
361
|
},
|
|
242
362
|
send: (o) => {
|
|
243
|
-
|
|
363
|
+
t.send(o);
|
|
244
364
|
},
|
|
245
365
|
open$: new m((o) => {
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
}).pipe(
|
|
366
|
+
const i = (c) => o.next(c);
|
|
367
|
+
t.addEventListener("open", i), n.add(w(() => t.removeEventListener("open", i)));
|
|
368
|
+
}).pipe(u()),
|
|
249
369
|
close$: new m((o) => {
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
}).pipe(
|
|
370
|
+
const i = (c) => o.next(c);
|
|
371
|
+
t.addEventListener("close", i), n.add(w(() => t.removeEventListener("close", i)));
|
|
372
|
+
}).pipe(u()),
|
|
253
373
|
error$: new m((o) => {
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
}).pipe(
|
|
374
|
+
const i = (c) => o.next(c);
|
|
375
|
+
t.addEventListener("error", i), n.add(w(() => t.removeEventListener("error", i)));
|
|
376
|
+
}).pipe(u()),
|
|
257
377
|
message$: new m((o) => {
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
}).pipe(
|
|
378
|
+
const i = (c) => o.next(c);
|
|
379
|
+
t.addEventListener("message", i), n.add(w(() => t.removeEventListener("message", i)));
|
|
380
|
+
}).pipe(u())
|
|
261
381
|
};
|
|
262
|
-
} catch (
|
|
263
|
-
return console.error(
|
|
382
|
+
} catch (t) {
|
|
383
|
+
return console.error(t), null;
|
|
264
384
|
}
|
|
265
385
|
}
|
|
266
386
|
}
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
387
|
+
const ee = 2, re = 1e3, pe = (e, r) => r(e).pipe(
|
|
388
|
+
$((t) => t.pipe(
|
|
389
|
+
N((n, s) => s > ee ? D(n) : A(n)),
|
|
390
|
+
B(re)
|
|
271
391
|
))
|
|
272
|
-
),
|
|
273
|
-
const
|
|
392
|
+
), de = (e, r) => {
|
|
393
|
+
const t = [], n = /* @__PURE__ */ new Set(), s = () => {
|
|
274
394
|
var o;
|
|
275
|
-
for (;
|
|
276
|
-
const
|
|
277
|
-
|
|
395
|
+
for (; n.size < ((o = r == null ? void 0 : r.maxParallel) != null ? o : 1) && t.length > 0; ) {
|
|
396
|
+
const i = t.shift();
|
|
397
|
+
n.add(i), i();
|
|
278
398
|
}
|
|
279
399
|
};
|
|
280
|
-
return (o,
|
|
281
|
-
const
|
|
282
|
-
next: (
|
|
283
|
-
error: (
|
|
284
|
-
complete: () =>
|
|
400
|
+
return (o, i) => new m((c) => {
|
|
401
|
+
const a = () => i(o).subscribe({
|
|
402
|
+
next: (h) => c.next(h),
|
|
403
|
+
error: (h) => c.next(h),
|
|
404
|
+
complete: () => c.complete()
|
|
285
405
|
}), l = () => {
|
|
286
|
-
|
|
406
|
+
n.delete(a), I(t, a), s();
|
|
287
407
|
};
|
|
288
|
-
return
|
|
408
|
+
return t.push(a), s(), l;
|
|
289
409
|
});
|
|
290
410
|
};
|
|
291
411
|
export {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
R as
|
|
296
|
-
|
|
412
|
+
ae as FetchHTTPImplementation,
|
|
413
|
+
g as HTTPHeaders,
|
|
414
|
+
X as HTTPRequest,
|
|
415
|
+
R as HTTPResponse,
|
|
416
|
+
f as HTTPResponseError,
|
|
417
|
+
b as HTTPService,
|
|
418
|
+
_ as HTTPStatusCode,
|
|
297
419
|
G as IHTTPImplementation,
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
420
|
+
le as ISocketService,
|
|
421
|
+
de as ThresholdInterceptorFactory,
|
|
422
|
+
he as WebSocketService,
|
|
423
|
+
ce as XHRHTTPImplementation,
|
|
424
|
+
pe as retryInterceptor
|
|
303
425
|
};
|
package/lib/types/index.d.ts
CHANGED
|
@@ -16,8 +16,10 @@
|
|
|
16
16
|
export { HTTPHeaders } from './services/http/headers';
|
|
17
17
|
export { HTTPService } from './services/http/http.service';
|
|
18
18
|
export { IHTTPImplementation } from './services/http/implementations/implementation';
|
|
19
|
+
export { FetchHTTPImplementation } from './services/http/implementations/fetch';
|
|
19
20
|
export { XHRHTTPImplementation } from './services/http/implementations/xhr';
|
|
20
21
|
export { HTTPRequest, type HTTPRequestMethod } from './services/http/request';
|
|
22
|
+
export { type HTTPResponseType, HTTPStatusCode } from './services/http/http';
|
|
21
23
|
export { HTTPResponse, type HTTPEvent, HTTPResponseError } from './services/http/response';
|
|
22
24
|
export { type ISocket, ISocketService, type SocketBodyType, WebSocketService, } from './services/web-socket/web-socket.service';
|
|
23
25
|
export type { IRequestParams, IPostRequestParams } from './services/http/http.service';
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { HTTPEvent, HTTPResponse, HTTPResponseError } from '../response';
|
|
2
|
-
import { HTTPRequest } from '../request';
|
|
3
|
-
import { IHTTPImplementation } from '../implementations/implementation';
|
|
4
|
-
import { Injector } from '@wendellhu/redi';
|
|
5
|
-
import { Disposable } from '@univerjs/core';
|
|
6
1
|
import { Observable } from 'rxjs';
|
|
2
|
+
import { Disposable } from '@univerjs/core';
|
|
3
|
+
import { Injector } from '@wendellhu/redi';
|
|
4
|
+
import { IHTTPImplementation } from '../implementations/implementation';
|
|
5
|
+
import { HTTPRequest } from '../request';
|
|
6
|
+
import { HTTPEvent, HTTPResponse, HTTPResponseError } from '../response';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* A mocked HTTP implementation service for testing purposes. Besides methods in the interface, it
|
|
@@ -22,10 +22,14 @@ export declare const ApplicationJSONType = "application/json";
|
|
|
22
22
|
*/
|
|
23
23
|
export declare class HTTPHeaders {
|
|
24
24
|
private readonly _headers;
|
|
25
|
-
constructor(headers?: IHeadersConstructorProps | string);
|
|
25
|
+
constructor(headers?: IHeadersConstructorProps | Headers | string);
|
|
26
26
|
forEach(callback: (name: string, value: string[]) => void): void;
|
|
27
27
|
has(key: string): boolean;
|
|
28
28
|
get(key: string): string[] | null;
|
|
29
|
+
toHeadersInit(): HeadersInit;
|
|
29
30
|
private _setHeader;
|
|
31
|
+
private _handleHeadersString;
|
|
32
|
+
private _handleHeadersConstructorProps;
|
|
33
|
+
private _handleHeaders;
|
|
30
34
|
}
|
|
31
35
|
export {};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { HTTPInterceptorFn } from './interceptor';
|
|
2
|
-
import { HTTPResponse } from './response';
|
|
3
|
-
import { IHTTPImplementation } from './implementations/implementation';
|
|
4
|
-
import { HTTPResponseType } from './http';
|
|
5
|
-
import { IDisposable } from '@wendellhu/redi';
|
|
6
1
|
import { Disposable } from '@univerjs/core';
|
|
2
|
+
import { IDisposable } from '@wendellhu/redi';
|
|
3
|
+
import { HTTPResponseType } from './http';
|
|
4
|
+
import { IHTTPImplementation } from './implementations/implementation';
|
|
5
|
+
import { HTTPResponse } from './response';
|
|
6
|
+
import { HTTPInterceptorFn } from './interceptor';
|
|
7
7
|
|
|
8
8
|
export interface IRequestParams {
|
|
9
9
|
/** Query params. These params would be append to the url before the request is sent. */
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { HTTPRequest } from '../request';
|
|
3
|
+
import { HTTPEvent } from '../response';
|
|
4
|
+
import { IHTTPImplementation } from './implementation';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* An HTTP implementation using Fetch API. This implementation can both run in browser and Node.js.
|
|
8
|
+
*
|
|
9
|
+
* It does not support streaming response yet (May 12, 2024).
|
|
10
|
+
*/
|
|
11
|
+
export declare class FetchHTTPImplementation implements IHTTPImplementation {
|
|
12
|
+
send(request: HTTPRequest): Observable<HTTPEvent<any>>;
|
|
13
|
+
private _send;
|
|
14
|
+
private _readBody;
|
|
15
|
+
private _parseFetchParamsFromRequest;
|
|
16
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HTTPEvent } from '../response';
|
|
2
|
-
import { HTTPRequest } from '../request';
|
|
3
1
|
import { Observable } from 'rxjs';
|
|
2
|
+
import { HTTPRequest } from '../request';
|
|
3
|
+
import { HTTPEvent } from '../response';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* HTTP service could be implemented differently on platforms.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { IHTTPImplementation } from './implementation';
|
|
2
|
-
import { HTTPEvent } from '../response';
|
|
3
|
-
import { HTTPRequest } from '../request';
|
|
4
1
|
import { Observable } from 'rxjs';
|
|
2
|
+
import { HTTPRequest } from '../request';
|
|
3
|
+
import { HTTPEvent } from '../response';
|
|
4
|
+
import { IHTTPImplementation } from './implementation';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* An HTTP implementation using XHR. HTTP service provided by this service could only be async (we do not support sync XHR now).
|
|
8
8
|
*/
|
|
9
9
|
export declare class XHRHTTPImplementation implements IHTTPImplementation {
|
|
10
10
|
send(request: HTTPRequest): Observable<HTTPEvent<any>>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { HTTPEvent } from './response';
|
|
2
|
-
import { HTTPRequest } from './request';
|
|
3
|
-
import { IAccessor } from '@wendellhu/redi';
|
|
4
1
|
import { Observable } from 'rxjs';
|
|
2
|
+
import { IAccessor } from '@wendellhu/redi';
|
|
3
|
+
import { HTTPRequest } from './request';
|
|
4
|
+
import { HTTPEvent } from './response';
|
|
5
5
|
|
|
6
6
|
export type HTTPHandlerFn = (request: HTTPRequest) => Observable<HTTPEvent<unknown>>;
|
|
7
7
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HTTPParams } from './params';
|
|
2
|
-
import { HTTPResponseType } from './http';
|
|
3
1
|
import { HTTPHeaders } from './headers';
|
|
2
|
+
import { HTTPResponseType } from './http';
|
|
3
|
+
import { HTTPParams } from './params';
|
|
4
4
|
|
|
5
5
|
export type HTTPRequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
6
6
|
export interface IHTTPRequestParams {
|
|
@@ -22,4 +22,5 @@ export declare class HTTPRequest {
|
|
|
22
22
|
constructor(method: HTTPRequestMethod, url: string, requestParams?: IHTTPRequestParams | undefined);
|
|
23
23
|
getUrlWithParams(): string;
|
|
24
24
|
getBody(): string | null;
|
|
25
|
+
getHeadersInit(): HeadersInit;
|
|
25
26
|
}
|
|
@@ -4,6 +4,7 @@ import { HTTPHeaders } from './headers';
|
|
|
4
4
|
* There are multiple events could be resolved from the HTTP server.
|
|
5
5
|
*/
|
|
6
6
|
export type HTTPEvent<T> = HTTPResponse<T> | HTTPResponseError;
|
|
7
|
+
export type HTTPResponseBody = string | ArrayBuffer | Blob | object | null;
|
|
7
8
|
/** Wraps (success) response info. */
|
|
8
9
|
export declare class HTTPResponse<T> {
|
|
9
10
|
readonly body: T;
|
|
@@ -18,14 +19,14 @@ export declare class HTTPResponse<T> {
|
|
|
18
19
|
});
|
|
19
20
|
}
|
|
20
21
|
export declare class HTTPResponseError {
|
|
21
|
-
readonly headers
|
|
22
|
-
readonly status
|
|
23
|
-
readonly statusText
|
|
22
|
+
readonly headers?: HTTPHeaders;
|
|
23
|
+
readonly status?: number;
|
|
24
|
+
readonly statusText?: string;
|
|
24
25
|
readonly error: any;
|
|
25
26
|
constructor({ headers, status, statusText, error, }: {
|
|
26
|
-
headers
|
|
27
|
-
status
|
|
28
|
-
statusText
|
|
27
|
+
headers?: HTTPHeaders;
|
|
28
|
+
status?: number;
|
|
29
|
+
statusText?: string;
|
|
29
30
|
error: any;
|
|
30
31
|
});
|
|
31
32
|
}
|
package/lib/umd/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`).forEach(n=>{const[r,i]=n.split(":");r&&i&&this._setHeader(r,i)}):t&&Object.keys(t).forEach(([n,r])=>{this._setHeader(n,r)})}forEach(t){this._headers.forEach((n,r)=>t(r,n))}has(t){return!!this._headers.has(t.toLowerCase())}get(t){const n=t.toLowerCase();return this._headers.has(n)?this._headers.get(n):null}_setHeader(t,n){const r=t.toLowerCase();this._headers.has(r)?this._headers.get(r).push(n.toString()):this._headers.set(r,[n.toString()])}}const _=E.createIdentifier("network.http-implementation");class A{constructor(t){this.params=t}toString(){return this.params?Object.keys(this.params).map(t=>`${t}=${this.params[t]}`).join("&"):""}}let U=0;class L{constructor(t,n,r){m(this,"uid",U++);this.method=t,this.url=n,this.requestParams=r}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var n,r;const t=(r=(n=this.requestParams)==null?void 0:n.params)==null?void 0:r.toString();return t?`${this.url}${this.url.includes("?")?"&":"?"}${t}`:this.url}getBody(){var r,i;const t=(r=this.headers.get("Content-Type"))!=null?r:g,n=(i=this.requestParams)==null?void 0:i.body;return t===g&&n&&typeof n=="object"?JSON.stringify(n):n?`${n}`:null}}class u{constructor({body:t,headers:n,status:r,statusText:i}){m(this,"body");m(this,"headers");m(this,"status");m(this,"statusText");this.body=t,this.headers=n,this.status=r,this.statusText=i}}class R{constructor({headers:t,status:n,statusText:r,error:i}){m(this,"headers");m(this,"status");m(this,"statusText");m(this,"error");this.headers=t,this.status=n,this.statusText=r,this.error=i}}class O{constructor(t,n,r){this.headers=t,this.status=n,this.statusText=r}}var M=Object.defineProperty,x=Object.getOwnPropertyDescriptor,F=(e,t,n,r)=>{for(var i=r>1?void 0:r?x(t,n):t,c=e.length-1,s;c>=0;c--)(s=e[c])&&(i=(r?s(t,n,i):s(i))||i);return r&&i&&M(t,n,i),i},k=(e,t)=>(n,r)=>t(n,r,e);o.HTTPService=class extends a.Disposable{constructor(n){super();m(this,"_interceptors",[]);m(this,"_pipe");this._http=n}registerHTTPInterceptor(n){if(this._interceptors.indexOf(n)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(n),this._interceptors=this._interceptors.sort((r,i)=>{var c,s;return((c=r.priority)!=null?c:0)-((s=i.priority)!=null?s:0)}),this._pipe=null,a.toDisposable(()=>a.remove(this._interceptors,n))}get(n,r){return this._request("GET",n,r)}post(n,r){return this._request("POST",n,r)}put(n,r){return this._request("PUT",n,r)}delete(n,r){return this._request("DELETE",n,r)}async _request(n,r,i){var y,w;const c=new b(i==null?void 0:i.headers),s=new A(i==null?void 0:i.params),h=new L(n,r,{headers:c,params:s,withCredentials:(y=i==null?void 0:i.withCredentials)!=null?y:!1,responseType:(w=i==null?void 0:i.responseType)!=null?w:"json",body:n==="GET"||i==null?void 0:i.body}),p=l.of(h).pipe(v.concatMap(f=>this._runInterceptorsAndImplementation(f))),d=await l.firstValueFrom(p);if(d instanceof u)return d;throw new Error(`${d.error}`)}_runInterceptorsAndImplementation(n){return this._pipe||(this._pipe=this._interceptors.map(r=>r.interceptor).reduceRight((r,i)=>D(r,i),(r,i)=>i(r))),this._pipe(n,r=>this._http.send(r))}},o.HTTPService=F([k(0,_)],o.HTTPService);function D(e,t){return(n,r)=>t(n,i=>e(i,r))}const j=200,$=300;var q=(e=>(e[e.Continue=100]="Continue",e[e.SwitchingProtocols=101]="SwitchingProtocols",e[e.Processing=102]="Processing",e[e.EarlyHints=103]="EarlyHints",e[e.Ok=200]="Ok",e[e.Created=201]="Created",e[e.Accepted=202]="Accepted",e[e.NonAuthoritativeInformation=203]="NonAuthoritativeInformation",e[e.NoContent=204]="NoContent",e[e.ResetContent=205]="ResetContent",e[e.PartialContent=206]="PartialContent",e[e.MultiStatus=207]="MultiStatus",e[e.AlreadyReported=208]="AlreadyReported",e[e.ImUsed=226]="ImUsed",e[e.MultipleChoices=300]="MultipleChoices",e[e.MovedPermanently=301]="MovedPermanently",e[e.Found=302]="Found",e[e.SeeOther=303]="SeeOther",e[e.NotModified=304]="NotModified",e[e.UseProxy=305]="UseProxy",e[e.Unused=306]="Unused",e[e.TemporaryRedirect=307]="TemporaryRedirect",e[e.PermanentRedirect=308]="PermanentRedirect",e[e.BadRequest=400]="BadRequest",e[e.Unauthorized=401]="Unauthorized",e[e.PaymentRequired=402]="PaymentRequired",e[e.Forbidden=403]="Forbidden",e[e.NotFound=404]="NotFound",e[e.MethodNotAllowed=405]="MethodNotAllowed",e[e.NotAcceptable=406]="NotAcceptable",e[e.ProxyAuthenticationRequired=407]="ProxyAuthenticationRequired",e[e.RequestTimeout=408]="RequestTimeout",e[e.Conflict=409]="Conflict",e[e.Gone=410]="Gone",e[e.LengthRequired=411]="LengthRequired",e[e.PreconditionFailed=412]="PreconditionFailed",e[e.PayloadTooLarge=413]="PayloadTooLarge",e[e.UriTooLong=414]="UriTooLong",e[e.UnsupportedMediaType=415]="UnsupportedMediaType",e[e.RangeNotSatisfiable=416]="RangeNotSatisfiable",e[e.ExpectationFailed=417]="ExpectationFailed",e[e.ImATeapot=418]="ImATeapot",e[e.MisdirectedRequest=421]="MisdirectedRequest",e[e.UnprocessableEntity=422]="UnprocessableEntity",e[e.Locked=423]="Locked",e[e.FailedDependency=424]="FailedDependency",e[e.TooEarly=425]="TooEarly",e[e.UpgradeRequired=426]="UpgradeRequired",e[e.PreconditionRequired=428]="PreconditionRequired",e[e.TooManyRequests=429]="TooManyRequests",e[e.RequestHeaderFieldsTooLarge=431]="RequestHeaderFieldsTooLarge",e[e.UnavailableForLegalReasons=451]="UnavailableForLegalReasons",e[e.InternalServerError=500]="InternalServerError",e[e.NotImplemented=501]="NotImplemented",e[e.BadGateway=502]="BadGateway",e[e.ServiceUnavailable=503]="ServiceUnavailable",e[e.GatewayTimeout=504]="GatewayTimeout",e[e.HttpVersionNotSupported=505]="HttpVersionNotSupported",e[e.VariantAlsoNegotiates=506]="VariantAlsoNegotiates",e[e.InsufficientStorage=507]="InsufficientStorage",e[e.LoopDetected=508]="LoopDetected",e[e.NotExtended=510]="NotExtended",e[e.NetworkAuthenticationRequired=511]="NetworkAuthenticationRequired",e))(q||{});class B{send(t){return new l.Observable(n=>{const r=new XMLHttpRequest;r.open(t.method,t.getUrlWithParams()),t.withCredentials&&(r.withCredentials=!0),t.headers.forEach((p,d)=>r.setRequestHeader(p,d.join(","))),t.headers.has("Accept")||r.setRequestHeader("Accept","application/json, text/plain, */*"),t.headers.has("Content-Type")||r.setRequestHeader("Content-Type","application/json;charset=UTF-8");const i=()=>{const p=r.statusText||"OK",d=new b(r.getAllResponseHeaders());return new O(d,r.status,p)},c=()=>{const{headers:p,statusText:d,status:y}=i(),{responseType:w}=t;let f=null,I=null;y!==q.NoContent&&(f=typeof r.response>"u"?r.responseText:r.response);let N=y>=j&&y<$;if(w==="json"&&typeof f=="string"){const Y=f;try{f=f?JSON.parse(f):null}catch(K){N=!1,f=Y,I=K}}N?n.next(new u({body:f,headers:p,status:y,statusText:d})):n.next(new R({error:I,headers:p,status:y,statusText:d}))},s=p=>{const d=new R({error:p,status:r.status||0,statusText:r.statusText||"Unknown Error",headers:i().headers});n.next(d)};r.addEventListener("load",c),r.addEventListener("error",s),r.addEventListener("abort",s),r.addEventListener("timeout",s);const h=t.getBody();return r.send(h),()=>{r.readyState!==r.DONE&&r.abort(),r.removeEventListener("load",c),r.removeEventListener("error",s),r.removeEventListener("abort",s),r.removeEventListener("timeout",s)}})}}const G=E.createIdentifier("univer.socket");class V extends a.Disposable{createSocket(t){try{const n=new WebSocket(t),r=new a.DisposableCollection;return{URL:t,close:(c,s)=>{n.close(c,s),r.dispose()},send:c=>{n.send(c)},open$:new l.Observable(c=>{const s=h=>c.next(h);n.addEventListener("open",s),r.add(a.toDisposable(()=>n.removeEventListener("open",s)))}).pipe(v.share()),close$:new l.Observable(c=>{const s=h=>c.next(h);n.addEventListener("close",s),r.add(a.toDisposable(()=>n.removeEventListener("close",s)))}).pipe(v.share()),error$:new l.Observable(c=>{const s=h=>c.next(h);n.addEventListener("error",s),r.add(a.toDisposable(()=>n.removeEventListener("error",s)))}).pipe(v.share()),message$:new l.Observable(c=>{const s=h=>c.next(h);n.addEventListener("message",s),r.add(a.toDisposable(()=>n.removeEventListener("message",s)))}).pipe(v.share())}}catch(n){return console.error(n),null}}}const W=2,X=1e3,z=(e,t)=>t(e).pipe(v.retryWhen(n=>n.pipe(v.concatMap((r,i)=>i>W?l.throwError(r):l.of(r)),l.delay(X)))),J=(e,t)=>{const n=[],r=new Set,i=()=>{var c;for(;r.size<((c=t==null?void 0:t.maxParallel)!=null?c:1)&&n.length>0;){const s=n.shift();r.add(s),s()}};return(c,s)=>new l.Observable(h=>{const p=()=>s(c).subscribe({next:y=>h.next(y),error:y=>h.next(y),complete:()=>h.complete()}),d=()=>{r.delete(p),a.remove(n,p),i()};return n.push(p),i(),d})};o.HTTPHeaders=b,o.HTTPRequest=L,o.HTTPResponse=u,o.HTTPResponseError=R,o.IHTTPImplementation=_,o.ISocketService=G,o.ThresholdInterceptorFactory=J,o.WebSocketService=V,o.XHRHTTPImplementation=B,o.retryInterceptor=z,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(a,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("rxjs"),require("rxjs/operators"),require("@wendellhu/redi")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs","rxjs/operators","@wendellhu/redi"],c):(a=typeof globalThis<"u"?globalThis:a||self,c(a.UniverNetwork={},a.UniverCore,a.rxjs,a.rxjs.operators,a["@wendellhu/redi"]))})(this,function(a,c,m,f,E){"use strict";var S=Object.defineProperty;var C=(a,c,m)=>c in a?S(a,c,{enumerable:!0,configurable:!0,writable:!0,value:m}):a[c]=m;var u=(a,c,m)=>(C(a,typeof c!="symbol"?c+"":c,m),m);const I="application/json";class g{constructor(t){u(this,"_headers",new Map);typeof t=="string"?this._handleHeadersString(t):t instanceof Headers?this._handleHeaders(t):t&&this._handleHeadersConstructorProps(t)}forEach(t){this._headers.forEach((n,r)=>t(r,n))}has(t){return!!this._headers.has(t.toLowerCase())}get(t){const n=t.toLowerCase();return this._headers.has(n)?this._headers.get(n):null}toHeadersInit(){var n;const t={};return this._headers.forEach((r,s)=>{t[s]=r.join(",")}),(n=t.Accept)!=null||(t.Accept="application/json, text/plain, */*"),t}_setHeader(t,n){const r=t.toLowerCase();this._headers.has(r)?this._headers.get(r).push(n.toString()):this._headers.set(r,[n.toString()])}_handleHeadersString(t){t.split(`
|
|
2
|
+
`).forEach(n=>{const[r,s]=n.split(":");r&&s&&this._setHeader(r,s)})}_handleHeadersConstructorProps(t){Object.keys(t).forEach(([n,r])=>{this._setHeader(n,r)})}_handleHeaders(t){t.forEach((n,r)=>{this._setHeader(r,n)})}}const L=E.createIdentifier("network.http-implementation");class A{constructor(t){this.params=t}toString(){return this.params?Object.keys(this.params).map(t=>`${t}=${this.params[t]}`).join("&"):""}}let x=0;class q{constructor(t,n,r){u(this,"uid",x++);this.method=t,this.url=n,this.requestParams=r}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var n,r;const t=(r=(n=this.requestParams)==null?void 0:n.params)==null?void 0:r.toString();return t?`${this.url}${this.url.includes("?")?"&":"?"}${t}`:this.url}getBody(){var r,s;const t=(r=this.headers.get("Content-Type"))!=null?r:I,n=(s=this.requestParams)==null?void 0:s.body;return t===I&&n&&typeof n=="object"?JSON.stringify(n):n?`${n}`:null}getHeadersInit(){return this.headers.toHeadersInit()}}class _{constructor({body:t,headers:n,status:r,statusText:s}){u(this,"body");u(this,"headers");u(this,"status");u(this,"statusText");this.body=t,this.headers=n,this.status=r,this.statusText=s}}class w{constructor({headers:t,status:n,statusText:r,error:s}){u(this,"headers");u(this,"status");u(this,"statusText");u(this,"error");this.headers=t,this.status=n,this.statusText=r,this.error=s}}class U{constructor(t,n,r){this.headers=t,this.status=n,this.statusText=r}}var k=Object.defineProperty,F=Object.getOwnPropertyDescriptor,O=(e,t,n,r)=>{for(var s=r>1?void 0:r?F(t,n):t,o=e.length-1,i;o>=0;o--)(i=e[o])&&(s=(r?i(t,n,s):i(s))||s);return r&&s&&k(t,n,s),s},M=(e,t)=>(n,r)=>t(n,r,e);a.HTTPService=class extends c.Disposable{constructor(n){super();u(this,"_interceptors",[]);u(this,"_pipe");this._http=n}registerHTTPInterceptor(n){if(this._interceptors.indexOf(n)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(n),this._interceptors=this._interceptors.sort((r,s)=>{var o,i;return((o=r.priority)!=null?o:0)-((i=s.priority)!=null?i:0)}),this._pipe=null,c.toDisposable(()=>c.remove(this._interceptors,n))}get(n,r){return this._request("GET",n,r)}post(n,r){return this._request("POST",n,r)}put(n,r){return this._request("PUT",n,r)}delete(n,r){return this._request("DELETE",n,r)}async _request(n,r,s){var p,v;const o=new g(s==null?void 0:s.headers),i=new A(s==null?void 0:s.params),d=new q(n,r,{headers:o,params:i,withCredentials:(p=s==null?void 0:s.withCredentials)!=null?p:!1,responseType:(v=s==null?void 0:s.responseType)!=null?v:"json",body:n==="GET"||s==null?void 0:s.body}),l=m.of(d).pipe(f.concatMap(y=>this._runInterceptorsAndImplementation(y))),h=await m.firstValueFrom(l);if(h instanceof _)return h;throw new Error(`${h.error}`)}_runInterceptorsAndImplementation(n){return this._pipe||(this._pipe=this._interceptors.map(r=>r.interceptor).reduceRight((r,s)=>D(r,s),(r,s)=>s(r))),this._pipe(n,r=>this._http.send(r))}},a.HTTPService=O([M(0,L)],a.HTTPService);function D(e,t){return(n,r)=>t(n,s=>e(s,r))}const j=200,B=300;var b=(e=>(e[e.Continue=100]="Continue",e[e.SwitchingProtocols=101]="SwitchingProtocols",e[e.Processing=102]="Processing",e[e.EarlyHints=103]="EarlyHints",e[e.Ok=200]="Ok",e[e.Created=201]="Created",e[e.Accepted=202]="Accepted",e[e.NonAuthoritativeInformation=203]="NonAuthoritativeInformation",e[e.NoContent=204]="NoContent",e[e.ResetContent=205]="ResetContent",e[e.PartialContent=206]="PartialContent",e[e.MultiStatus=207]="MultiStatus",e[e.AlreadyReported=208]="AlreadyReported",e[e.ImUsed=226]="ImUsed",e[e.MultipleChoices=300]="MultipleChoices",e[e.MovedPermanently=301]="MovedPermanently",e[e.Found=302]="Found",e[e.SeeOther=303]="SeeOther",e[e.NotModified=304]="NotModified",e[e.UseProxy=305]="UseProxy",e[e.Unused=306]="Unused",e[e.TemporaryRedirect=307]="TemporaryRedirect",e[e.PermanentRedirect=308]="PermanentRedirect",e[e.BadRequest=400]="BadRequest",e[e.Unauthorized=401]="Unauthorized",e[e.PaymentRequired=402]="PaymentRequired",e[e.Forbidden=403]="Forbidden",e[e.NotFound=404]="NotFound",e[e.MethodNotAllowed=405]="MethodNotAllowed",e[e.NotAcceptable=406]="NotAcceptable",e[e.ProxyAuthenticationRequired=407]="ProxyAuthenticationRequired",e[e.RequestTimeout=408]="RequestTimeout",e[e.Conflict=409]="Conflict",e[e.Gone=410]="Gone",e[e.LengthRequired=411]="LengthRequired",e[e.PreconditionFailed=412]="PreconditionFailed",e[e.PayloadTooLarge=413]="PayloadTooLarge",e[e.UriTooLong=414]="UriTooLong",e[e.UnsupportedMediaType=415]="UnsupportedMediaType",e[e.RangeNotSatisfiable=416]="RangeNotSatisfiable",e[e.ExpectationFailed=417]="ExpectationFailed",e[e.ImATeapot=418]="ImATeapot",e[e.MisdirectedRequest=421]="MisdirectedRequest",e[e.UnprocessableEntity=422]="UnprocessableEntity",e[e.Locked=423]="Locked",e[e.FailedDependency=424]="FailedDependency",e[e.TooEarly=425]="TooEarly",e[e.UpgradeRequired=426]="UpgradeRequired",e[e.PreconditionRequired=428]="PreconditionRequired",e[e.TooManyRequests=429]="TooManyRequests",e[e.RequestHeaderFieldsTooLarge=431]="RequestHeaderFieldsTooLarge",e[e.UnavailableForLegalReasons=451]="UnavailableForLegalReasons",e[e.InternalServerError=500]="InternalServerError",e[e.NotImplemented=501]="NotImplemented",e[e.BadGateway=502]="BadGateway",e[e.ServiceUnavailable=503]="ServiceUnavailable",e[e.GatewayTimeout=504]="GatewayTimeout",e[e.HttpVersionNotSupported=505]="HttpVersionNotSupported",e[e.VariantAlsoNegotiates=506]="VariantAlsoNegotiates",e[e.InsufficientStorage=507]="InsufficientStorage",e[e.LoopDetected=508]="LoopDetected",e[e.NotExtended=510]="NotExtended",e[e.NetworkAuthenticationRequired=511]="NetworkAuthenticationRequired",e))(b||{});class ${send(t){return new m.Observable(n=>{const r=new AbortController;return this._send(t,n,r).then(()=>{},s=>{n.error(new w({error:s}))}),()=>r.abort()})}async _send(t,n,r){var p,v;let s;try{const y=this._parseFetchParamsFromRequest(t);s=await fetch(t.getUrlWithParams(),{signal:r.signal,...y})}catch(y){n.error(new w({error:y,status:(p=y.status)!=null?p:0,statusText:(v=y.statusText)!=null?v:"Unknown Error",headers:y.headers}));return}const o=new g(s.headers),i=s.status,d=s.statusText;let l=null;s.body&&(l=await this._readBody(t,s,n)),i>=b.Ok&&i<b.MultipleChoices?n.next(new _({body:l,headers:o,status:i,statusText:d})):n.error(new w({error:l,status:i,statusText:d,headers:o})),n.complete()}async _readBody(t,n,r){var l;const s=[],o=n.body.getReader();let i=0;for(;;){const{done:h,value:p}=await o.read();if(h)break;s.push(p),i+=p.length}const d=G(s,i);try{const h=(l=n.headers.get("content-type"))!=null?l:"";return V(t,d,h)}catch(h){return r.error(new w({error:h,status:n.status,statusText:n.statusText,headers:new g(n.headers)})),null}}_parseFetchParamsFromRequest(t){return{method:t.method,headers:t.getHeadersInit(),body:t.getBody(),credentials:t.withCredentials?"include":void 0}}}function G(e,t){const n=new Uint8Array(t);let r=0;for(const s of e)n.set(s,r),r+=s.length;return n}const W=/^\)\]\}',?\n/;function V(e,t,n){switch(e.responseType){case"json":const r=new TextDecoder().decode(t).replace(W,"");return r===""?null:JSON.parse(r);case"text":return new TextDecoder().decode(t);case"blob":return new Blob([t],{type:n});case"arraybuffer":return t.buffer;default:throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}class X{send(t){return new m.Observable(n=>{const r=new XMLHttpRequest;r.open(t.method,t.getUrlWithParams()),t.withCredentials&&(r.withCredentials=!0),t.headers.forEach((l,h)=>r.setRequestHeader(l,h.join(","))),t.headers.has("Accept")||r.setRequestHeader("Accept","application/json, text/plain, */*"),t.headers.has("Content-Type")||r.setRequestHeader("Content-Type","application/json;charset=UTF-8");const s=()=>{const l=r.statusText||"OK",h=new g(r.getAllResponseHeaders());return new U(h,r.status,l)},o=()=>{const{headers:l,statusText:h,status:p}=s(),{responseType:v}=t;let y=null,R=null;p!==b.NoContent&&(y=typeof r.response>"u"?r.responseText:r.response);let N=p>=j&&p<B;if(v==="json"&&typeof y=="string"){const P=y;try{y=y?JSON.parse(y):null}catch(H){N=!1,y=P,R=H}}N?n.next(new _({body:y,headers:l,status:p,statusText:h})):n.next(new w({error:R,headers:l,status:p,statusText:h}))},i=l=>{const h=new w({error:l,status:r.status||0,statusText:r.statusText||"Unknown Error",headers:s().headers});n.next(h)};r.addEventListener("load",o),r.addEventListener("error",i),r.addEventListener("abort",i),r.addEventListener("timeout",i);const d=t.getBody();return r.send(d),()=>{r.readyState!==r.DONE&&r.abort(),r.removeEventListener("load",o),r.removeEventListener("error",i),r.removeEventListener("abort",i),r.removeEventListener("timeout",i)}})}}const z=E.createIdentifier("univer.socket");class J extends c.Disposable{createSocket(t){try{const n=new WebSocket(t),r=new c.DisposableCollection;return{URL:t,close:(o,i)=>{n.close(o,i),r.dispose()},send:o=>{n.send(o)},open$:new m.Observable(o=>{const i=d=>o.next(d);n.addEventListener("open",i),r.add(c.toDisposable(()=>n.removeEventListener("open",i)))}).pipe(f.share()),close$:new m.Observable(o=>{const i=d=>o.next(d);n.addEventListener("close",i),r.add(c.toDisposable(()=>n.removeEventListener("close",i)))}).pipe(f.share()),error$:new m.Observable(o=>{const i=d=>o.next(d);n.addEventListener("error",i),r.add(c.toDisposable(()=>n.removeEventListener("error",i)))}).pipe(f.share()),message$:new m.Observable(o=>{const i=d=>o.next(d);n.addEventListener("message",i),r.add(c.toDisposable(()=>n.removeEventListener("message",i)))}).pipe(f.share())}}catch(n){return console.error(n),null}}}const Y=2,K=1e3,Q=(e,t)=>t(e).pipe(f.retryWhen(n=>n.pipe(f.concatMap((r,s)=>s>Y?m.throwError(r):m.of(r)),m.delay(K)))),Z=(e,t)=>{const n=[],r=new Set,s=()=>{var o;for(;r.size<((o=t==null?void 0:t.maxParallel)!=null?o:1)&&n.length>0;){const i=n.shift();r.add(i),i()}};return(o,i)=>new m.Observable(d=>{const l=()=>i(o).subscribe({next:p=>d.next(p),error:p=>d.next(p),complete:()=>d.complete()}),h=()=>{r.delete(l),c.remove(n,l),s()};return n.push(l),s(),h})};a.FetchHTTPImplementation=$,a.HTTPHeaders=g,a.HTTPRequest=q,a.HTTPResponse=_,a.HTTPResponseError=w,a.HTTPStatusCode=b,a.IHTTPImplementation=L,a.ISocketService=z,a.ThresholdInterceptorFactory=Z,a.WebSocketService=J,a.XHRHTTPImplementation=X,a.retryInterceptor=Q,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/network",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "DreamNum <developer@univer.ai>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -49,18 +49,18 @@
|
|
|
49
49
|
"npm": ">=8.0.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@wendellhu/redi": "^0.
|
|
52
|
+
"@wendellhu/redi": "^0.15.1",
|
|
53
53
|
"rxjs": ">=7.0.0",
|
|
54
|
-
"@univerjs/core": "0.1.
|
|
54
|
+
"@univerjs/core": "0.1.11"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@wendellhu/redi": "^0.
|
|
57
|
+
"@wendellhu/redi": "^0.15.1",
|
|
58
58
|
"rxjs": "^7.8.1",
|
|
59
59
|
"typescript": "^5.4.5",
|
|
60
|
-
"vite": "^5.2.
|
|
61
|
-
"vitest": "^1.
|
|
62
|
-
"@univerjs/core": "0.1.
|
|
63
|
-
"@univerjs/shared": "0.1.
|
|
60
|
+
"vite": "^5.2.11",
|
|
61
|
+
"vitest": "^1.6.0",
|
|
62
|
+
"@univerjs/core": "0.1.11",
|
|
63
|
+
"@univerjs/shared": "0.1.11"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"test": "vitest run",
|