Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit c5e02da

Browse files
committed
fix: support @web-std/file in normalize input
1 parent d13d15f commit c5e02da

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

packages/ipfs-core-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"uint8arrays": "^2.1.6"
6060
},
6161
"devDependencies": {
62+
"@web-std/file": "^1.1.0",
6263
"aegir": "^34.0.2",
6364
"rimraf": "^3.0.2"
6465
}

packages/ipfs-core-utils/src/files/normalise-input/utils.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict'
22

3-
const { Blob } = globalThis
4-
53
/**
64
* @param {any} obj
75
* @returns {obj is ArrayBufferView|ArrayBuffer}
@@ -15,7 +13,7 @@ function isBytes (obj) {
1513
* @returns {obj is Blob}
1614
*/
1715
function isBlob (obj) {
18-
return typeof Blob !== 'undefined' && obj instanceof Blob
16+
return obj.constructor && (obj.constructor.name === 'Blob' || obj.constructor.name === 'File')
1917
}
2018

2119
/**

packages/ipfs-core-utils/test/files/normalise-input.spec.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { expect } = require('aegir/utils/chai')
66
const blobToIt = require('blob-to-it')
77
const uint8ArrayFromString = require('uint8arrays/from-string')
88
const all = require('it-all')
9+
const { File } = require('@web-std/file')
910
const { Blob, ReadableStream } = globalThis
1011
const { isBrowser, isWebWorker, isElectronRenderer } = require('ipfs-utils/src/env')
1112

@@ -35,7 +36,12 @@ async function verifyNormalisation (input) {
3536
let content = input[0].content
3637

3738
if (isBrowser || isWebWorker || isElectronRenderer) {
38-
expect(content).to.be.an.instanceOf(Blob)
39+
try {
40+
expect(content).to.be.an.instanceOf(Blob)
41+
} catch (err) {
42+
// Fallback to instance of File
43+
expect(content).to.be.an.instanceOf(File)
44+
}
3945
content = blobToIt(content)
4046
}
4147

@@ -173,6 +179,14 @@ describe('normalise-input', function () {
173179
testInputType(BLOB, 'Blob', false)
174180
})
175181

182+
describe('@web-std/file', () => {
183+
it('normalizes File input', async () => {
184+
const FILE = new File([BUFFER()], 'test-file.txt')
185+
186+
await testContent(FILE)
187+
})
188+
})
189+
176190
describe('Iterable<Number>', () => {
177191
testInputType(ARRAY, 'Iterable<Number>', false)
178192
})

0 commit comments

Comments
 (0)