-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move browser source code to TypeScript
- Loading branch information
Showing
16 changed files
with
290 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { HttpProgressHandler, HttpRequest, HttpResponse, HttpStack } from '../upload' | ||
|
||
// TODO: Can we remove this eslint-disable line | ||
/* eslint-disable max-classes-per-file */ | ||
export default class XHRHttpStack implements HttpStack<Blob> { | ||
createRequest(method, url) { | ||
return new XHRRequest(method, url) | ||
} | ||
|
||
getName() { | ||
return 'XHRHttpStack' | ||
} | ||
} | ||
|
||
class XHRRequest implements HttpRequest<Blob> { | ||
_xhr = new XMLHttpRequest() | ||
_method: string | ||
_url: string | ||
_headers: Record<string, string> = {} | ||
|
||
constructor(method: string, url: string) { | ||
this._xhr.open(method, url, true) | ||
|
||
this._method = method | ||
this._url = url | ||
} | ||
|
||
getMethod(): string { | ||
return this._method | ||
} | ||
|
||
getURL(): string { | ||
return this._url | ||
} | ||
|
||
setHeader(header: string, value: string): void { | ||
this._xhr.setRequestHeader(header, value) | ||
this._headers[header] = value | ||
} | ||
|
||
getHeader(header: string) { | ||
return this._headers[header] | ||
} | ||
|
||
setProgressHandler(progressHandler: HttpProgressHandler): void { | ||
// Test support for progress events before attaching an event listener | ||
if (!('upload' in this._xhr)) { | ||
return | ||
} | ||
|
||
this._xhr.upload.onprogress = (e) => { | ||
if (!e.lengthComputable) { | ||
return | ||
} | ||
|
||
progressHandler(e.loaded) | ||
} | ||
} | ||
|
||
send(body?: Blob): Promise<XHRResponse> { | ||
return new Promise((resolve, reject) => { | ||
this._xhr.onload = () => { | ||
resolve(new XHRResponse(this._xhr)) | ||
} | ||
|
||
this._xhr.onerror = (err) => { | ||
reject(err) | ||
} | ||
|
||
this._xhr.send(body) | ||
}) | ||
} | ||
|
||
abort(): Promise<void> { | ||
this._xhr.abort() | ||
return Promise.resolve() | ||
} | ||
|
||
getUnderlyingObject(): XMLHttpRequest { | ||
return this._xhr | ||
} | ||
} | ||
|
||
class XHRResponse implements HttpResponse { | ||
_xhr: XMLHttpRequest | ||
|
||
constructor(xhr: XMLHttpRequest) { | ||
this._xhr = xhr | ||
} | ||
|
||
getStatus(): number { | ||
return this._xhr.status | ||
} | ||
|
||
getHeader(header: string): string | undefined { | ||
return this._xhr.getResponseHeader(header) || undefined | ||
} | ||
|
||
getBody(): string { | ||
return this._xhr.responseText | ||
} | ||
|
||
getUnderlyingObject(): XMLHttpRequest { | ||
return this._xhr | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.