forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
buffer.resolveObjectURL
(oven-sh#12324)
- Loading branch information
1 parent
5a0b935
commit c486a04
Showing
3 changed files
with
93 additions
and
1 deletion.
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 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,49 @@ | ||
import { Blob, resolveObjectURL } from "buffer"; | ||
import { URL } from "url"; | ||
import { test, expect } from "bun:test"; | ||
|
||
// https://github.com/nodejs/node/blob/2eff28fb7a93d3f672f80b582f664a7c701569fb/lib/internal/blob.js#L441 | ||
// https://nodejs.org/api/buffer.html#bufferresolveobjecturlid | ||
// https://github.com/nodejs/node/blob/2eff28fb7a93d3f672f80b582f664a7c701569fb/test/parallel/test-blob-createobjecturl.js#L35 | ||
test("buffer.resolveObjectURL", async () => { | ||
const blob = new Blob(["hello"]); | ||
const id = URL.createObjectURL(blob); | ||
expect(id).toBeString(); | ||
const otherBlob = resolveObjectURL(id)!; | ||
expect(otherBlob).toBeInstanceOf(Blob); | ||
expect(otherBlob.constructor).toStrictEqual(Blob); | ||
expect(otherBlob.size).toStrictEqual(5); | ||
expect(await otherBlob.text()).toStrictEqual("hello"); | ||
URL.revokeObjectURL(id); | ||
|
||
// should do nothing | ||
URL.revokeObjectURL(id); | ||
|
||
expect(resolveObjectURL(id)).toBeUndefined(); | ||
}); | ||
|
||
test("buffer.resolveObjectURL empty blob", async () => { | ||
const blob = new Blob(); | ||
const id = URL.createObjectURL(blob); | ||
expect( | ||
resolveObjectURL( | ||
id.slice(0, id.length - 1) + String.fromCharCode(id.slice(id.length - 1, id.length).charCodeAt(0) + 1), | ||
), | ||
).toBeUndefined(); | ||
URL.revokeObjectURL(id); | ||
expect(await blob.text()).toBe(""); | ||
}); | ||
|
||
test("buffer.resolveObjectURL args", async () => { | ||
expect(resolveObjectURL()).toBeUndefined(); | ||
expect(resolveObjectURL(1)).toBeUndefined(); | ||
expect(resolveObjectURL("foo")).toBeUndefined(); | ||
const blob = new Blob(["hello"]); | ||
const id = URL.createObjectURL(blob); | ||
expect( | ||
resolveObjectURL( | ||
id.slice(0, id.length - 1) + String.fromCharCode(id.slice(id.length - 1, id.length).charCodeAt(0) + 1), | ||
), | ||
).toBeUndefined(); | ||
URL.revokeObjectURL(id); | ||
}); |