Skip to content

feat: support iterators and async iterators #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ jobs:

- stage: test
name: electron
before_script: export DISPLAY=:99.0; sh -e /etc/init.d/xvfb start
services:
- xvfb
script:
- npm run test:electron
- npm run test:electron-renderer
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"license": "MIT",
"dependencies": {
"buffer": "^5.2.1",
"is-buffer": "^2.0.3",
"is-electron": "^2.2.0",
"is-pull-stream": "0.0.0",
"is-stream": "^2.0.0",
Expand Down
8 changes: 3 additions & 5 deletions src/files/add-input-validation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

const kindOf = require('kind-of')
const isStream = require('is-stream')
const { isSource } = require('is-pull-stream')
const isBuffer = require('is-buffer')

const validateAddInput = (input) => {
// Buffer|ReadableStream|PullStream|File
const isPrimitive = obj => isBuffer(obj) || isStream.readable(obj) || isSource(obj) || kindOf(obj) === 'file'
// PullStream|File|Iterator|AsyncIterator
const isPrimitive = (obj) => isSource(obj) || kindOf(obj) === 'file' || obj[Symbol.iterator] || obj[Symbol.asyncIterator]

// An object like { content?, path? }, where content isBufferOrStream and path isString
const isContentObject = obj => {
Expand All @@ -24,7 +22,7 @@ const validateAddInput = (input) => {
if (isInput(input) || (Array.isArray(input) && input.every(isInput))) {
return true
} else {
throw new Error(`Input not supported. Expected Buffer|ReadableStream|PullStream|File|Array<Object> got ${kindOf(input)}. Check the documentation for more info https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/FILES.md#add`)
throw new Error(`Input not supported. Expected Buffer|ReadableStream|PullStream|File|Array<Object>| got ${kindOf(input)}. Check the documentation for more info https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/FILES.md#add`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error(`Input not supported. Expected Buffer|ReadableStream|PullStream|File|Array<Object>| got ${kindOf(input)}. Check the documentation for more info https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/FILES.md#add`)
throw new Error(`Input not supported. Expected Iterator|AsyncIterator|PullStream|File|Array<Object>| got ${kindOf(input)}. Check the documentation for more info https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/FILES.md#add`)

}
}

Expand Down
52 changes: 50 additions & 2 deletions test/files/add-input-validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ const expect = chai.expect

describe('add-input-validation', function () {
it('validates correct primitive input types', function () {
expect(validate('Hello world')).to.be.true()
expect(validate([0, 1, 2, 3])).to.be.true()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ test for async iterator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

expect(validate(Buffer.from(('test')))).to.be.true()
expect(validate(new Readable())).to.be.true()
expect(validate(empty())).to.be.true()
expect(validate(async function * () {}())).to.be.true()
expect(validate({
[Symbol.asyncIterator]: () => {},
next: () => {}
})).to.be.true()
expect(validate({
[Symbol.iterator]: () => {},
next: () => {}
})).to.be.true()

if (supportsFileReader) {
const file = new self.File(['test'], 'test.txt', { type: 'text/plain' })
Expand All @@ -28,6 +39,15 @@ describe('add-input-validation', function () {
expect(validate([Buffer.from('test'), Buffer.from('test')])).to.be.true()
expect(validate([new Readable(), new Readable()])).to.be.true()
expect(validate([empty(), empty()])).to.be.true()
expect(validate([(async function * () {}())])).to.be.true()
expect(validate([{
[Symbol.asyncIterator]: () => {},
next: () => {}
}])).to.be.true()
expect(validate([{
[Symbol.iterator]: () => {},
next: () => {}
}])).to.be.true()

if (supportsFileReader) {
const file = new self.File(['test'], 'test.txt', { type: 'text/plain' })
Expand All @@ -40,17 +60,45 @@ describe('add-input-validation', function () {
expect(validate({ path: '/path', content: Buffer.from('test') })).to.be.true()
expect(validate({ content: new Readable() })).to.be.true()
expect(validate({ content: empty() })).to.be.true()
expect(validate({ content: (async function * () {}()) })).to.be.true()
expect(validate({ content: {
[Symbol.asyncIterator]: () => {},
next: () => {}
} })).to.be.true()
expect(validate({ content: {
[Symbol.iterator]: () => {},
next: () => {}
} })).to.be.true()

if (supportsFileReader) {
expect(validate({ content: new Readable() })).to.be.true()
}
})

it('validates correct form array of of object input', function () {
expect(validate([{ path: '/path' }])).to.be.true()
expect(validate([{ path: '/path', content: Buffer.from('test') }])).to.be.true()
expect(validate([{ content: new Readable() }])).to.be.true()
expect(validate([{ content: empty() }])).to.be.true()
expect(validate([{ content: (async function * () {}()) }])).to.be.true()
expect(validate([{ content: {
[Symbol.asyncIterator]: () => {},
next: () => {}
} }])).to.be.true()
expect(validate([{ content: {
[Symbol.iterator]: () => {},
next: () => {}
} }])).to.be.true()

if (supportsFileReader) {
expect(validate({ content: new Readable() })).to.be.true()
}
})

it('should throw with bad input', function () {
const regex = /Input not supported/
expect(() => validate('test')).throw(regex)
expect(() => validate(2)).throw(regex)
expect(() => validate({ path: 3 })).throw(regex)
expect(() => validate({ path: 'path', content: 'test' })).throw(regex)
expect(() => validate({ path: 'path', content: 2 })).throw(regex)
expect(() => validate({})).throw(regex)
})
Expand Down