-
Notifications
You must be signed in to change notification settings - Fork 943
Implement a bundle reader for Web #3097
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
Changes from 1 commit
9602712
c5e783e
5e7fb89
1ee1615
18f0be1
aa455bf
782e76f
f5e4474
c60f8dc
e005485
6d9d5cd
a869466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ export class BundleReader { | |
} | ||
this.reader = (this.bundleStream as ReadableStream).getReader(); | ||
|
||
// Read the metadata (which is the first element). | ||
this.nextElement().then( | ||
element => { | ||
if (element && element.isBundleMetadata()) { | ||
|
@@ -109,9 +110,7 @@ export class BundleReader { | |
); | ||
} | ||
}, | ||
error => { | ||
this.metadata.reject(error); | ||
} | ||
error => this.metadata.reject(error) | ||
); | ||
} | ||
|
||
|
@@ -175,25 +174,29 @@ export class BundleReader { | |
* If reached end of the stream, returns a null. | ||
*/ | ||
private async readLength(): Promise<Uint8Array | null> { | ||
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. JSDoc 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. Done. |
||
let position = this.indexOfOpenBracket(); | ||
while (position < 0) { | ||
let position: number; | ||
while ((position = this.indexOfOpenBracket()) < 0) { | ||
const done = await this.pullMoreDataToBuffer(); | ||
if (done) { | ||
if (this.buffer.length === 0) { | ||
return null; | ||
} | ||
position = this.indexOfOpenBracket(); | ||
// Underlying stream is closed, and we still cannot find a '{'. | ||
if (position < 0) { | ||
this.raiseError( | ||
'Reached the end of bundle when a length string is expected.' | ||
); | ||
} | ||
} else { | ||
position = this.indexOfOpenBracket(); | ||
break; | ||
} | ||
} | ||
|
||
// Broke out of the loop because underlying stream is closed, and there | ||
// happens to be no more data to process. | ||
if (this.buffer.length === 0) { | ||
return null; | ||
} | ||
|
||
position = this.indexOfOpenBracket(); | ||
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. You can drop 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. Done. |
||
// Broke out of the loop because underlying stream is closed, but still | ||
// cannot find an open bracket. | ||
if (position < 0) { | ||
this.raiseError( | ||
'Reached the end of bundle when a length string is expected.' | ||
); | ||
} | ||
|
||
const result = this.buffer.slice(0, position); | ||
// Update the internal buffer to drop the read length. | ||
this.buffer = this.buffer.slice(position); | ||
|
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.
Maybe add a small inline comment before this line:
// Read the metadata (which is the first element)
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.
Done.