-
Notifications
You must be signed in to change notification settings - Fork 10.5k
47685 stream to blob #62298
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
47685 stream to blob #62298
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -155,7 +155,7 @@ export module DotNet { | |
* @throws Error if the given value is not an Object. | ||
*/ | ||
export function createJSObjectReference(jsObject: any): any { | ||
if (jsObject && typeof jsObject === "object") { | ||
if (jsObject && (typeof jsObject === "object" || jsObject instanceof Function)) { | ||
cachedJSObjectsById[nextJsObjectId] = new JSObject(jsObject); | ||
|
||
const result = { | ||
|
@@ -573,7 +573,7 @@ export module DotNet { | |
} | ||
|
||
/** Traverses the object hierarchy to find an object member specified by the identifier. | ||
* | ||
* | ||
* @param obj Root object to search in. | ||
* @param identifier Complete identifier of the member to find, e.g. "document.location.href". | ||
* @returns A tuple containing the immediate parent of the member and the member name. | ||
|
@@ -586,19 +586,19 @@ export module DotNet { | |
// Error handling in case of undefined last key depends on the type of operation. | ||
for (let i = 0; i < keys.length - 1; i++) { | ||
const key = keys[i]; | ||
|
||
if (current && typeof current === 'object' && key in current) { | ||
current = current[key]; | ||
} else { | ||
throw new Error(`Could not find '${identifier}' ('${key}' was undefined).`); | ||
} | ||
} | ||
|
||
return [current, keys[keys.length - 1]]; | ||
} | ||
|
||
/** Takes an object member and a call type and returns a function that performs the operation specified by the call type on the member. | ||
* | ||
* | ||
* @param parent Immediate parent of the accessed object member. | ||
* @param memberName Name (key) of the accessed member. | ||
* @param callType The type of the operation to perform on the member. | ||
|
@@ -640,50 +640,50 @@ export module DotNet { | |
if (!(propName in obj)) { | ||
return false; | ||
} | ||
|
||
// If the property is present we examine its descriptor, potentially needing to walk up the prototype chain. | ||
while (obj !== undefined) { | ||
const descriptor = Object.getOwnPropertyDescriptor(obj, propName); | ||
|
||
if (descriptor) { | ||
// Return true for data property | ||
if (descriptor.hasOwnProperty('value')) { | ||
return true | ||
} | ||
|
||
// Return true for accessor property with defined getter. | ||
return descriptor.hasOwnProperty('get') && typeof descriptor.get === 'function'; | ||
} | ||
|
||
obj = Object.getPrototypeOf(obj); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isWritableProperty(obj: any, propName: string) { | ||
// Return true for missing property if the property can be added. | ||
if (!(propName in obj)) { | ||
return Object.isExtensible(obj); | ||
} | ||
|
||
// If the property is present we examine its descriptor, potentially needing to walk up the prototype chain. | ||
while (obj !== undefined) { | ||
const descriptor = Object.getOwnPropertyDescriptor(obj, propName); | ||
|
||
if (descriptor) { | ||
// Return true for writable data property. | ||
if (descriptor.hasOwnProperty('value') && descriptor.writable) { | ||
return true; | ||
} | ||
|
||
// Return true for accessor property with defined setter. | ||
return descriptor.hasOwnProperty('set') && typeof descriptor.set === 'function'; | ||
} | ||
|
||
obj = Object.getPrototypeOf(obj); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
@@ -760,6 +760,25 @@ export module DotNet { | |
async arrayBuffer(): Promise<ArrayBuffer> { | ||
return new Response(await this.stream()).arrayBuffer(); | ||
} | ||
|
||
/** | ||
* Returns a Blob from the stream. This uses the browser's stream reader. | ||
* The returned value is a string. | ||
*/ | ||
async readableStreamToBlob(mimeType: string = "application/octet-stream"): Promise<string> { | ||
const reader = (await this.stream()).getReader(); | ||
const chunks: BlobPart[] = []; | ||
|
||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
chunks.push(value); | ||
} | ||
|
||
const blob = new Blob(chunks, {type: mimeType}); | ||
const url = URL.createObjectURL(blob); | ||
return url; | ||
} | ||
Comment on lines
+768
to
+781
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. This is not building a blob, but an object URL |
||
} | ||
|
||
class PendingStream { | ||
|
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.
It's unclear why this change is needed