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

Commit

Permalink
fix: support @web-std/file in normalize input
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 20, 2021
1 parent d13d15f commit c5e02da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/ipfs-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"uint8arrays": "^2.1.6"
},
"devDependencies": {
"@web-std/file": "^1.1.0",
"aegir": "^34.0.2",
"rimraf": "^3.0.2"
}
Expand Down
4 changes: 1 addition & 3 deletions packages/ipfs-core-utils/src/files/normalise-input/utils.js
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}
Expand All @@ -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')
}

/**
Expand Down
16 changes: 15 additions & 1 deletion packages/ipfs-core-utils/test/files/normalise-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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 {
expect(content).to.be.an.instanceOf(Blob)
} catch (err) {
// Fallback to instance of File
expect(content).to.be.an.instanceOf(File)
}
content = blobToIt(content)
}

Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit c5e02da

Please sign in to comment.