-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: support @web-std/file in normalize input #3750
Changes from 1 commit
c5e02da
ded6c36
5f798ef
5e8c992
d0c30ce
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
'use strict' | ||
|
||
const { Blob } = globalThis | ||
|
||
/** | ||
* @param {any} obj | ||
* @returns {obj is ArrayBufferView|ArrayBuffer} | ||
|
@@ -15,7 +13,7 @@ function isBytes (obj) { | |
* @returns {obj is Blob} | ||
*/ | ||
function isBlob (obj) { | ||
return typeof Blob !== 'undefined' && obj instanceof Blob | ||
return obj.constructor && (obj.constructor.name === 'Blob' || obj.constructor.name === 'File') | ||
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. How does this prevent an instance of any old class named "File" or "Blob" being detected as a Maybe ensure that methods are present on 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 sounds a good idea |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ const { expect } = require('aegir/utils/chai') | |
const blobToIt = require('blob-to-it') | ||
const uint8ArrayFromString = require('uint8arrays/from-string') | ||
const all = require('it-all') | ||
const { File } = require('@web-std/file') | ||
const { Blob, ReadableStream } = globalThis | ||
const { isBrowser, isWebWorker, isElectronRenderer } = require('ipfs-utils/src/env') | ||
|
||
|
@@ -35,7 +36,12 @@ async function verifyNormalisation (input) { | |
let content = input[0].content | ||
|
||
if (isBrowser || isWebWorker || isElectronRenderer) { | ||
expect(content).to.be.an.instanceOf(Blob) | ||
try { | ||
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.
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.
But, somehow this will not work with instanceOf Blob, and works with 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 sounds like 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. Also of note here is that node 16 has a Blob implementation. 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.
It does: https://github.com/web-std/io/blob/main/blob/src/lib.js but instanceOf returns false 🤷🏼♂️ 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. I changed it now to Blob to be: const { Blob } = require('@web-std/blob') instead of const { Blob } = globalThis It works this way, but then other tests fail (which probably use the native Blob 🤷🏼♂️ 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. reverting this, as this is even more problematic 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. So, when creating a https://github.com/web-std/io/tree/main/file in the browser, it will not be an instance of This codepen shows that a browser File should be instance of both Blob and ws Blob. My only assumption for this to not be the case in the tests here, is that somehow playwright has a different globalThis scope in the test that makes them being different... @hugomrdias is this reasonable? do you have any clues? 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. web-std/io#10 this fixed the issue 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. Now |
||
expect(content).to.be.an.instanceOf(Blob) | ||
} catch (err) { | ||
// Fallback to instance of File | ||
expect(content).to.be.an.instanceOf(File) | ||
} | ||
content = blobToIt(content) | ||
} | ||
|
||
|
@@ -173,6 +179,14 @@ describe('normalise-input', function () { | |
testInputType(BLOB, 'Blob', false) | ||
}) | ||
|
||
describe('@web-std/file', () => { | ||
it('normalizes File input', async () => { | ||
const FILE = new File([BUFFER()], 'test-file.txt') | ||
|
||
await testContent(FILE) | ||
}) | ||
}) | ||
|
||
describe('Iterable<Number>', () => { | ||
testInputType(ARRAY, 'Iterable<Number>', false) | ||
}) | ||
|
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.
Because the
const { Blob } = ...
line was removed, this is now the globalBlob
so TS will be unhappy if someone explicitly passes@web-std/file
objects in your code.Might just need to remove the
@returns
annotation so tsc will derive a boolean return 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.
return the
@returns
causes typescript to also not be happy, I added globalThis to the type