Repository navigation
Blob: convert a type that is not a string in new Blob, new File and slice
#47740
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
Open
robobun
wants to merge
5
commits into
main
Choose a base branch
from
robobun/9dce8142/blob-type-domstring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd9e901
Blob: convert a type that is not a string in new Blob, new File and s…
robobun b79fb01
Blob: new Blob and new File share the step that reads the type option
robobun b8aa5b8
Blob: read the type option by its pre-made name
robobun ab1e0c1
AOT: the table of built-ins says that a Blob converts its type
robobun f0454d9
bun-types: say what the type of a BlobPropertyBag is
robobun 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
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
21 changes: 21 additions & 0 deletions
21
...arallel/host-classes/a-blob-converts-a-string-object-whose-tostring-nothing-else-calls.js
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // `slice`, `new Blob()` and `new File()` convert a String object with a toString of its own. Nothing else in the program calls | ||
| // that toString, and nothing uses the results of the calls. In the place of start or end of `slice`, a String object is the contentType. | ||
| let conversions = 0; | ||
| const boxed = Object.assign(new String("x/boxed"), { | ||
| toString() { | ||
| conversions++; | ||
| return "x/own"; | ||
| }, | ||
| }); | ||
| const blob = new Blob(["abcd"]); | ||
|
|
||
| blob.slice(0, 2, boxed); | ||
| console.log(conversions); | ||
| blob.slice(boxed); | ||
| console.log(conversions); | ||
| blob.slice(0, boxed); | ||
| console.log(conversions); | ||
| new Blob(["a"], { type: boxed }); | ||
| console.log(conversions); | ||
| new File(["a"], "name", { type: boxed }); | ||
| console.log(conversions); |
48 changes: 48 additions & 0 deletions
48
test/aot/parallel/host-classes/a-blob-converts-a-type-that-is-not-a-string.js
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // `new Blob()` and `new File()` convert the `type` of their options to a string, and `slice` converts its contentType. | ||
| // The conversion runs the code of an object, also in a call whose result nothing uses. | ||
| let conversions = 0; | ||
| const type = { | ||
| toString() { | ||
| conversions++; | ||
| return "x/converted"; | ||
| }, | ||
| }; | ||
| const blob = new Blob(["abcd"]); | ||
|
|
||
| console.log(new Blob(["a"], { type }).type, conversions); | ||
| console.log(new File(["a"], "name", { type }).type, conversions); | ||
| console.log(blob.slice(0, 2, type).type, conversions); | ||
|
|
||
| new Blob(["a"], { type }); | ||
| console.log(conversions); | ||
| blob.slice(0, 2, type); | ||
| console.log(conversions); | ||
|
|
||
| // A string in the place of start is the contentType, and the operand after it is not read. | ||
| console.log(blob.slice("a/b", 1, type).type, conversions); | ||
| console.log(blob.slice(0, 2).type === "", blob.slice(0, 2, "A/B").type, blob.slice(0, 2, 1).type); | ||
| console.log(blob.slice(0, 2, null).type, blob.slice(0, 2, undefined).type === ""); | ||
|
|
||
| const options = { | ||
| reads: 0, | ||
| get type() { | ||
| this.reads++; | ||
| return ["x/from-a-getter"]; | ||
| }, | ||
| }; | ||
| console.log(new Blob(["a"], options).type, options.reads); | ||
|
|
||
| for (const value of [Symbol("type"), Object.create(null)]) { | ||
| try { | ||
| blob.slice(0, 1, value); | ||
| console.log("slice did not throw"); | ||
| } catch (error) { | ||
| console.log(error instanceof TypeError, conversions); | ||
| } | ||
| try { | ||
| new Blob(["a"], { type: value }); | ||
| console.log("new Blob did not throw"); | ||
| } catch (error) { | ||
| console.log(error instanceof TypeError, conversions); | ||
| } | ||
| } |
Oops, something went wrong.
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.
🔴 Programs that pass a MIME lookup result as
typenow uploadContent-Type: nullorContent-Type: falsewhere the base sent no header. set_type_from_options at Blob.rs:5268 callsto_utf8on every present value, sonullandfalsebecome the text "null" / "false" and passis_valid_blob_type. This follows from the stated DOMString purpose and the author flags it as a maintainer decision, but it is a silent wire-level break for servers that reject unknown media types. Fix: a maintainer must accept it explicitly; if the spec behaviour stays, say in thetypeJSDoc at packages/bun-types/globals.d.ts:1569 and the release notes thatnull/falsebecome "null"/"false" and are sent as the Content-Type. [also at: src/runtime/webcore/Blob.rs:1807 - Programs that slice with a null or false contentType now get a sub-Blob typed "null" or "false" and uploadContent-Type: null, where the base inherited or dropped the type.]Why this was flagged
A program does
new File([buf], name, { type: mimeTypes.lookup(name) })ornew Blob([body], { type: res.headers.get("content-type") })and the lookup misses, givingfalseornull. On the base, jsdom_blob_construct/jsdom_file_construct testedcontent_type.is_string()first and ignored the value, so.typewas "" and a fetch upload carried no Content-Type. After the change, set_type_from_options at src/runtime/webcore/Blob.rs:5268 runsto_utf8on the value, "null"/"false" is all printable ASCII sois_valid_blob_typeat Blob.rs:74 accepts it, and Blob.rs:5271-5272 store it as the type. A receiving server that validates Content-Type now gets a 415-class failure that it did not get before. No guard in the diff prevents it; the PR description lists it under Downsides as a question for a maintainer, and the .d.ts hunk at packages/bun-types/globals.d.ts:1569 does not mention it.Verification:
set_type_from_optionsat src/runtime/webcore/Blob.rs:5262-5276 callscontent_type.to_utf8(global_this)?(giving "null"/"false") andis_valid_blob_typeat Blob.rs:74-76 only checks printable ASCII, so these pass andcontent_type_was_setis set. Base code (removed hunks at old lines 2159-2170 and 5318-5329) guarded withcontent_type.is_string(), leaving.type === "".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.
Agreed: a maintainer must accept this. It is the first item under Downsides, and the Notes have the table of what changes on the wire.
The facts for that decision:
undefinedas absent for aDOMString. Node.js v26.3.0 gives"null"and"false"at the three sites. web-platform-tests asserts"null"forslice(null type Blob sliceinFileAPI/blob/Blob-slice.any.js).Content-Type: null.The code and the JSDoc stay as they are until a maintainer answers, because the two answers need different text. If the conversion stays, I add a sentence on
nullandfalseto the JSDoc ofBlobPropertyBag.type. If it does not, the constructors keepnullandfalseabsent.This thread stays open for that answer.