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