From c5e02dae2c05bb994d6fc606afedf720930304e9 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 20 Jul 2021 09:06:21 +0200 Subject: [PATCH 1/5] fix: support @web-std/file in normalize input --- packages/ipfs-core-utils/package.json | 1 + .../src/files/normalise-input/utils.js | 4 +--- .../test/files/normalise-input.spec.js | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/ipfs-core-utils/package.json b/packages/ipfs-core-utils/package.json index 343452667a..f37161a509 100644 --- a/packages/ipfs-core-utils/package.json +++ b/packages/ipfs-core-utils/package.json @@ -59,6 +59,7 @@ "uint8arrays": "^2.1.6" }, "devDependencies": { + "@web-std/file": "^1.1.0", "aegir": "^34.0.2", "rimraf": "^3.0.2" } diff --git a/packages/ipfs-core-utils/src/files/normalise-input/utils.js b/packages/ipfs-core-utils/src/files/normalise-input/utils.js index 990546971b..4cbf6ed59b 100644 --- a/packages/ipfs-core-utils/src/files/normalise-input/utils.js +++ b/packages/ipfs-core-utils/src/files/normalise-input/utils.js @@ -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') } /** diff --git a/packages/ipfs-core-utils/test/files/normalise-input.spec.js b/packages/ipfs-core-utils/test/files/normalise-input.spec.js index e052368f85..1b2ec38964 100644 --- a/packages/ipfs-core-utils/test/files/normalise-input.spec.js +++ b/packages/ipfs-core-utils/test/files/normalise-input.spec.js @@ -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 { + 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', () => { testInputType(ARRAY, 'Iterable', false) }) From ded6c36f73d844ca55bf7136d958cb6c849f93d3 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 20 Jul 2021 10:27:32 +0200 Subject: [PATCH 2/5] chore: validate blob/file have a stream property --- packages/ipfs-core-utils/src/files/normalise-input/utils.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/ipfs-core-utils/src/files/normalise-input/utils.js b/packages/ipfs-core-utils/src/files/normalise-input/utils.js index 4cbf6ed59b..afd9642b7c 100644 --- a/packages/ipfs-core-utils/src/files/normalise-input/utils.js +++ b/packages/ipfs-core-utils/src/files/normalise-input/utils.js @@ -10,10 +10,12 @@ function isBytes (obj) { /** * @param {any} obj - * @returns {obj is Blob} + * @returns {obj is globalThis.Blob} */ function isBlob (obj) { - return obj.constructor && (obj.constructor.name === 'Blob' || obj.constructor.name === 'File') + return obj.constructor && + (obj.constructor.name === 'Blob' || obj.constructor.name === 'File') && + typeof obj.stream === 'function' } /** From 5f798efac8d8682a7d76ecc07d8ee108c83ea17f Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 20 Jul 2021 15:45:28 +0200 Subject: [PATCH 3/5] chore: fallback to web-std blob --- packages/ipfs-core-utils/test/files/normalise-input.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/ipfs-core-utils/test/files/normalise-input.spec.js b/packages/ipfs-core-utils/test/files/normalise-input.spec.js index 1b2ec38964..964f530f68 100644 --- a/packages/ipfs-core-utils/test/files/normalise-input.spec.js +++ b/packages/ipfs-core-utils/test/files/normalise-input.spec.js @@ -6,7 +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 { File, Blob: WsBlob } = require('@web-std/file') const { Blob, ReadableStream } = globalThis const { isBrowser, isWebWorker, isElectronRenderer } = require('ipfs-utils/src/env') @@ -39,8 +39,8 @@ async function verifyNormalisation (input) { try { expect(content).to.be.an.instanceOf(Blob) } catch (err) { - // Fallback to instance of File - expect(content).to.be.an.instanceOf(File) + // Fallback to instance of WsBlob + expect(content).to.be.an.instanceOf(WsBlob) } content = blobToIt(content) } From 5e8c99208da951e998bf6fc9c639750906cf7e03 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 20 Jul 2021 21:19:37 +0200 Subject: [PATCH 4/5] fix: tests --- .../test/files/normalise-input.spec.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/ipfs-core-utils/test/files/normalise-input.spec.js b/packages/ipfs-core-utils/test/files/normalise-input.spec.js index 964f530f68..7c2261107a 100644 --- a/packages/ipfs-core-utils/test/files/normalise-input.spec.js +++ b/packages/ipfs-core-utils/test/files/normalise-input.spec.js @@ -6,7 +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, Blob: WsBlob } = require('@web-std/file') +const { File } = require('@web-std/file') const { Blob, ReadableStream } = globalThis const { isBrowser, isWebWorker, isElectronRenderer } = require('ipfs-utils/src/env') @@ -36,12 +36,7 @@ async function verifyNormalisation (input) { let content = input[0].content if (isBrowser || isWebWorker || isElectronRenderer) { - try { - expect(content).to.be.an.instanceOf(Blob) - } catch (err) { - // Fallback to instance of WsBlob - expect(content).to.be.an.instanceOf(WsBlob) - } + expect(content).to.be.an.instanceOf(Blob) content = blobToIt(content) } @@ -183,6 +178,8 @@ describe('normalise-input', function () { it('normalizes File input', async () => { const FILE = new File([BUFFER()], 'test-file.txt') + // expect(FILE).to.be.an.instanceOf(globalThis.Blob) + await testContent(FILE) }) }) From d0c30cec5270dc035b34f981a5d36be918d48a13 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 23 Jul 2021 10:01:34 +0200 Subject: [PATCH 5/5] chore: use latest file version --- packages/ipfs-core-utils/package.json | 2 +- packages/ipfs-core-utils/test/files/normalise-input.spec.js | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/ipfs-core-utils/package.json b/packages/ipfs-core-utils/package.json index f37161a509..cd13ad6420 100644 --- a/packages/ipfs-core-utils/package.json +++ b/packages/ipfs-core-utils/package.json @@ -59,7 +59,7 @@ "uint8arrays": "^2.1.6" }, "devDependencies": { - "@web-std/file": "^1.1.0", + "@web-std/file": "^1.1.2", "aegir": "^34.0.2", "rimraf": "^3.0.2" } diff --git a/packages/ipfs-core-utils/test/files/normalise-input.spec.js b/packages/ipfs-core-utils/test/files/normalise-input.spec.js index 7c2261107a..ef8f1ae774 100644 --- a/packages/ipfs-core-utils/test/files/normalise-input.spec.js +++ b/packages/ipfs-core-utils/test/files/normalise-input.spec.js @@ -178,8 +178,6 @@ describe('normalise-input', function () { it('normalizes File input', async () => { const FILE = new File([BUFFER()], 'test-file.txt') - // expect(FILE).to.be.an.instanceOf(globalThis.Blob) - await testContent(FILE) }) })