-
Notifications
You must be signed in to change notification settings - Fork 193
polyfills:Implement streamable fetch in request/response, Make event.target refer to event.currentTarget. #706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9d095ba
polyfills:make event.target refer to event.currentTarget
partic2 d9e0efa
polyfills:implement streamable fetch for both request and response
partic2 3772a12
core:fix missing accept in connection_cb
partic2 d9e4cca
misc:update bundles
partic2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -98,7 +98,7 @@ class Event { | |
| * @type {EventTarget} | ||
| */ | ||
| get target() { | ||
| return null; | ||
| return this.currentTarget; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ function isDataView(obj) { | |
| } | ||
|
|
||
| function consumed(body) { | ||
| if (body._noBody) { | ||
| if (body._bodySize===0) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -14,48 +14,6 @@ function consumed(body) { | |
| body.bodyUsed = true; | ||
| } | ||
|
|
||
| function fileReaderReady(reader) { | ||
| return new Promise(function(resolve, reject) { | ||
| reader.onload = function() { | ||
| resolve(reader.result); | ||
| }; | ||
|
|
||
| reader.onerror = function() { | ||
| reject(reader.error); | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| function readBlobAsArrayBuffer(blob) { | ||
| var reader = new FileReader(); | ||
| var promise = fileReaderReady(reader); | ||
|
|
||
| reader.readAsArrayBuffer(blob); | ||
|
|
||
| return promise; | ||
| } | ||
|
|
||
| function readBlobAsText(blob) { | ||
| var reader = new FileReader(); | ||
| var promise = fileReaderReady(reader); | ||
| var match = /charset=([A-Za-z0-9_-]+)/.exec(blob.type); | ||
| var encoding = match ? match[1] : 'utf-8'; | ||
|
|
||
| reader.readAsText(blob, encoding); | ||
|
|
||
| return promise; | ||
| } | ||
|
|
||
| function readArrayBufferAsText(buf) { | ||
| var view = new Uint8Array(buf); | ||
| var chars = new Array(view.length); | ||
|
|
||
| for (var i = 0; i < view.length; i++) { | ||
| chars[i] = String.fromCharCode(view[i]); | ||
| } | ||
|
|
||
| return chars.join(''); | ||
| } | ||
|
|
||
| function bufferClone(buf) { | ||
| if (buf.slice) { | ||
|
|
@@ -80,24 +38,50 @@ export const BodyMixin = { | |
| this._bodyInit = body; | ||
|
|
||
| if (!body) { | ||
| this._noBody = true; | ||
| this._bodyText = ''; | ||
| this._bodySize = 0; | ||
| this._bodyReadable = ReadableStream.from([ new Uint8Array(0) ]); | ||
| } else if (typeof body === 'string') { | ||
| this._bodyText = body; | ||
| const bodyBuffer=new TextEncoder().encode(body); | ||
|
|
||
| this._bodySize=bodyBuffer.byteLength; | ||
| this._bodyReadable = ReadableStream.from([ bodyBuffer ]); | ||
| } else if (isPrototypeOf(Blob.prototype, body)) { | ||
| this._bodyBlob = body; | ||
| this._bodySize=body.size; | ||
| this._bodyReadable = body.stream; | ||
| } else if (isPrototypeOf(FormData.prototype, body)) { | ||
| this._bodyFormData = body; | ||
| // formdata polyfill | ||
| const bodyBuffer = body['_blob'](); | ||
|
|
||
| this._bodySize=bodyBuffer.byteLength; | ||
| this._bodyReadable = ReadableStream.from([ bodyBuffer ]); | ||
| } else if (isPrototypeOf(URLSearchParams.prototype, body)) { | ||
| this._bodyText = body.toString(); | ||
| const textEncoder=new TextEncoder(); | ||
| const bodyBuffer=textEncoder.encode(body); | ||
|
|
||
| this._bodySize=bodyBuffer.byteLength; | ||
| this._bodyReadable = ReadableStream.from([ textEncoder.encode(body.toString()) ]); | ||
| } else if (isDataView(body)) { | ||
| this._bodyArrayBuffer = bufferClone(body.buffer); | ||
| const bodyBuffer=new Uint8Array(bufferClone(body.buffer)); | ||
|
|
||
| this._bodySize=bodyBuffer.byteLength; | ||
| this._bodyReadable = ReadableStream.from([ bodyBuffer ]); | ||
| } else if (isPrototypeOf(ArrayBuffer.prototype, body) || ArrayBuffer.isView(body)) { | ||
| this._bodyArrayBuffer = bufferClone(body); | ||
| const bodyBuffer=new Uint8Array(bufferClone(body.buffer)); | ||
|
|
||
| this._bodySize=bodyBuffer.byteLength; | ||
| this._bodyReadable = ReadableStream.from([ bodyBuffer ]); | ||
| } else if (isPrototypeOf(ReadableStream.prototype, body)) { | ||
| this._bodySize=-1; | ||
| this._bodyReadable = body; | ||
| } else { | ||
| this._bodyText = body = Object.prototype.toString.call(body); | ||
| const bodyBuffer=new TextEncoder().encode(body.toString()); | ||
|
|
||
| this._bodySize=bodyBuffer.byteLength; | ||
| this._bodyReadable = ReadableStream.from([ bodyBuffer ]); | ||
| } | ||
|
|
||
| this.body=this._bodyReadable; | ||
|
|
||
| if (!this.headers.get('content-type')) { | ||
| if (typeof body === 'string') { | ||
| this.headers.set('content-type', 'text/plain;charset=UTF-8'); | ||
|
|
@@ -109,70 +93,50 @@ export const BodyMixin = { | |
| } | ||
| }, | ||
|
|
||
| blob() { | ||
| async blob() { | ||
| const rejected = consumed(this); | ||
|
|
||
| if (rejected) { | ||
| return rejected; | ||
| await rejected; | ||
| } | ||
|
|
||
| if (this._bodyBlob) { | ||
| return Promise.resolve(this._bodyBlob); | ||
| } else if (this._bodyArrayBuffer) { | ||
| return Promise.resolve(new Blob([ this._bodyArrayBuffer ])); | ||
| } else if (this._bodyFormData) { | ||
| throw new Error('could not read FormData body as blob'); | ||
| } else { | ||
| return Promise.resolve(new Blob([ this._bodyText ])); | ||
| if (!this._bodyReadable) { | ||
| throw new Error('Unknown body type'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we throw earlier? |
||
| } | ||
| }, | ||
|
|
||
| arrayBuffer() { | ||
| if (this._bodyArrayBuffer) { | ||
| var isConsumed = consumed(this); | ||
|
|
||
| if (isConsumed) { | ||
| return isConsumed; | ||
| } else if (ArrayBuffer.isView(this._bodyArrayBuffer)) { | ||
| return Promise.resolve( | ||
| this._bodyArrayBuffer.buffer.slice( | ||
| this._bodyArrayBuffer.byteOffset, | ||
| this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength | ||
| ) | ||
| ); | ||
| } else { | ||
| return Promise.resolve(this._bodyArrayBuffer); | ||
| const parts=[]; | ||
| const reader=this._bodyReadable.getReader(); | ||
|
|
||
| for (;;) { | ||
| const next=await reader.read(); | ||
|
|
||
| if (next.done) { | ||
| break; | ||
| } | ||
| } else { | ||
| return this.blob().then(readBlobAsArrayBuffer); | ||
|
|
||
| parts.push(next.value); | ||
| } | ||
| }, | ||
|
|
||
| text() { | ||
| const rejected = consumed(this); | ||
| return new Blob(parts); | ||
| }, | ||
|
|
||
| if (rejected) { | ||
| return rejected; | ||
| } | ||
| async arrayBuffer() { | ||
| // TODO: expose Blob.parts to reduce memeory copy? | ||
| return await (await this.blob()).arrayBuffer(); | ||
| }, | ||
|
|
||
| if (this._bodyBlob) { | ||
| return readBlobAsText(this._bodyBlob); | ||
| } else if (this._bodyArrayBuffer) { | ||
| return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer)); | ||
| } else if (this._bodyFormData) { | ||
| throw new Error('could not read FormData body as text'); | ||
| } else { | ||
| return Promise.resolve(this._bodyText); | ||
| } | ||
| async text() { | ||
| return new TextDecoder().decode(await this.arrayBuffer()); | ||
| }, | ||
|
|
||
| formData() { | ||
| return this.text().then(decode); | ||
| async formData() { | ||
| return decode(await this.text()); | ||
| }, | ||
|
|
||
| json() { | ||
| return this.text().then(JSON.parse); | ||
| async json() { | ||
| return JSON.parse(await this.text()); | ||
| }, | ||
|
|
||
| }; | ||
|
|
||
| function decode(body) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please run the linter? There are a number of style errors in the JS code.