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C,CAAC,OAAO,eAAe,CAAC;QACjC,KAAK,GAAG,CAAC,CAAC,OAAO,kCAAkC,CAAC;QACpD,KAAK,GAAG,CAAC,CAAC,OAAO,qBAAqB,CAAC;QACvC,KAAK,GAAG,CAAC,CAAC,OAAO,wBAAwB,CAAC;QAC1C,KAAK,GAAG,CAAC,CAAC,OAAO,oBAAoB,CAAC;QACtC,KAAK,GAAG,CAAC,CAAC,OAAO,2CAA2C,CAAC;QAC7D,KAAK,GAAG,CAAC,CAAC,OAAO,wBAAwB,CAAC;QAC1C,KAAK,GAAG,CAAC,CAAC,OAAO,4EAA4E,CAAC;QAC9F,KAAK,GAAG,CAAC,CAAC,OAAO,uCAAuC,CAAC;QACzD,KAAK,GAAG,CAAC,CAAC,OAAO,yBAAyB,CAAC;QAC3C,KAAK,GAAG,CAAC,CAAC,OAAO,gCAAgC,CAAC;QAClD,KAAK,GAAG,CAAC,CAAC,OAAO,6BAA6B,CAAC;KAClD;IACD,OAAO,sBAAsB,GAAG,MAAM,CAAC;AAC3C,CAAC","file":"","sourceRoot":"/","sourcesContent":["export class HttpError extends Error {\r\n status: number;\r\n constructor(status: number, message: string) {\r\n super(message);\r\n this.status = status;\r\n }\r\n}\r\n\r\nexport interface HttpResponsePromise extends Promise<string | ArrayBuffer> {\r\n abort: () => void;\r\n}\r\n\r\nexport class HttpClient {\r\n private serverBaseUrl = \"\";\r\n private authorization: string = null;\r\n private maxConcurrency = 0;\r\n private currentRequestNum = 0;\r\n private pendingRequests: HttpRequest[] = [];\r\n private responsePreprocessor: (response: string | ArrayBuffer | Blob) => string | ArrayBuffer;\r\n\r\n constructor(baseUrl: string, maxConcurrency = 0) {\r\n this.serverBaseUrl = baseUrl;\r\n this.maxConcurrency = maxConcurrency;\r\n }\r\n\r\n setAuthorization(authorization: string) {\r\n this.authorization = authorization;\r\n }\r\n\r\n setResponsePreprocessor(func: (response: string | ArrayBuffer | Blob) => string | ArrayBuffer) {\r\n this.responsePreprocessor = func;\r\n }\r\n\r\n get(path: string = \"\", timeout = 5000, headers?: { [key: string]: string }) {\r\n return this._handleRequest({\r\n path,\r\n method: \"GET\",\r\n timeout,\r\n headers\r\n });\r\n }\r\n\r\n post(path: string = \"\", timeout = 5000, body?: string | ArrayBuffer, contentType?: string, headers?: { [key: string]: string }) {\r\n\r\n return this._handleRequest({\r\n path,\r\n method: \"POST\",\r\n timeout,\r\n contentType,\r\n headers\r\n }, body);\r\n }\r\n\r\n put(path: string = \"\", timeout = 5000, body?: string | ArrayBuffer, contentType?: string, headers?: { [key: string]: string }) {\r\n return this._handleRequest({\r\n path,\r\n method: \"POST\",\r\n timeout,\r\n contentType,\r\n headers\r\n }, body);\r\n }\r\n\r\n delete(path: string = \"\", timeout = 5000, headers?: { [key: string]: string }) {\r\n return this._handleRequest({\r\n path,\r\n method: \"GET\",\r\n timeout,\r\n headers\r\n });\r\n }\r\n\r\n private _handleRequest(request: HttpRequest, body?: string | ArrayBuffer): HttpResponsePromise {\r\n let promise: HttpResponsePromise = new Promise<string | ArrayBuffer>((resolve, reject) => {\r\n request.onSuccess = resolve;\r\n request.onError = reject;\r\n if (this.maxConcurrency > 0 && this.currentRequestNum >= this.maxConcurrency) {\r\n this.pendingRequests.push(request);\r\n } else {\r\n this._doRequest(request, body);\r\n }\r\n }) as HttpResponsePromise;\r\n request.promise = promise;\r\n if (request.abort) {\r\n promise.abort = request.abort;\r\n } else {\r\n promise.abort = () => {\r\n let idx = this.pendingRequests.indexOf(request);\r\n if (idx >= 0) {\r\n this.pendingRequests.splice(idx, 1);\r\n }\r\n }\r\n }\r\n return promise;\r\n }\r\n\r\n private _doRequest(request: HttpRequest, body?: string | ArrayBuffer) {\r\n this.currentRequestNum++;\r\n const xhr = new XMLHttpRequest();\r\n xhr.timeout = request.timeout;\r\n xhr.open(request.method, this.serverBaseUrl + request.path, true);\r\n if (request.contentType) {\r\n xhr.setRequestHeader(\"Content-Type\", request.contentType);\r\n }\r\n if (request.headers) {\r\n for (let key in request.headers) {\r\n xhr.setRequestHeader(key, request.headers[key]);\r\n }\r\n }\r\n let abort = () => {\r\n xhr.abort();\r\n };;\r\n if (request.promise) {\r\n request.promise.abort = abort;\r\n } else {\r\n request.abort = abort;\r\n }\r\n xhr.onreadystatechange = () => {\r\n if (xhr.readyState == 4) {\r\n if (!request.onError) return;\r\n if (xhr.status == 200) {\r\n let response = xhr.responseText ? xhr.responseText : xhr.response;\r\n if (response && this.responsePreprocessor) {\r\n response = this.responsePreprocessor(response);\r\n }\r\n request.onSuccess(response);\r\n } else {\r\n request.onError(new HttpError(xhr.status, getHttpStatusError(xhr.status)));\r\n }\r\n if (this.pendingRequests.length) {\r\n this._doRequest(this.pendingRequests.shift());\r\n } else {\r\n this.currentRequestNum--;\r\n }\r\n }\r\n };\r\n xhr.ontimeout = () => {\r\n if (request.onError) {\r\n request.onError(new HttpError(408, getHttpStatusError(408)));\r\n request.onError = null;\r\n request.onSuccess = null;\r\n if (this.pendingRequests.length) {\r\n this._doRequest(this.pendingRequests.shift());\r\n } else {\r\n this.currentRequestNum--;\r\n }\r\n }\r\n };\r\n xhr.onabort = () => {\r\n if (request.onError) {\r\n request.onError(new HttpError(-1, \"Request abort.\"));\r\n request.onError = null;\r\n request.onSuccess = null;\r\n if (this.pendingRequests.length) {\r\n this._doRequest(this.pendingRequests.shift());\r\n } else {\r\n this.currentRequestNum--;\r\n }\r\n }\r\n }\r\n\r\n if (this.authorization) {\r\n xhr.setRequestHeader(\"auth-token\", this.authorization);\r\n }\r\n\r\n xhr.send(body);\r\n }\r\n}\r\n\r\ninterface HttpRequest {\r\n path: string;\r\n method: string;\r\n timeout: number;\r\n promise?: HttpResponsePromise;\r\n contentType?: string;\r\n body?: string | ArrayBuffer;\r\n abort?: () => void;\r\n headers?: { [key: string]: string };\r\n onSuccess?: (result: string | ArrayBuffer) => void;\r\n onError?: (error: HttpError) => void;\r\n}\r\n\r\nfunction getHttpStatusError(status: number) {\r\n switch (status) {\r\n case 400: return \"Bad request.\";\r\n case 401: return \"Unauthorized.\";\r\n case 402: return \"Payment required.\";\r\n case 403: return \"Forbidden.\";\r\n case 404: return \"Not found.\";\r\n case 405: return \"Method not allowed.\";\r\n case 406: return \"Not acceptable.\";\r\n case 407: return \"Proxy authentication required.\";\r\n case 408: return \"Request timeout.\";\r\n case 409: return \"Conflict.\";\r\n case 410: return \"Gone.\";\r\n case 411: return \"Length required.\";\r\n case 412: return \"Precondition failed.\";\r\n case 413: return \"Payload too large.\";\r\n case 414: return \"URI too long.\";\r\n case 416: return \"Requested range not satisfiable.\";\r\n case 417: return \"Expectation failed.\";\r\n case 427: return \"Precondition required.\";\r\n case 429: return \"Too many requests.\";\r\n case 449: return \"Retry after doing the appropriate action.\";\r\n case 500: return \"Internal server error.\";\r\n case 501: return \"Server does not support the functionality required to fulfill the request.\";\r\n case 502: return \"Error response received from gateway.\";\r\n case 503: return \"Temporarily overloaded.\";\r\n case 504: return \"Timed out waiting for gateway.\";\r\n case 505: return \"HTTP version not supported.\";\r\n }\r\n return \"Unknown error code: \" + status;\r\n}\r\n"]}