Skip to content

Commit

Permalink
fix: do not accept single items for ipfs.add (#3900)
Browse files Browse the repository at this point in the history
The types allow passing single items to `ipfs.addAll` and multiple items
to `ipfs.add`.

Instead, only accept single items to `ipfs.add` and streams of item to
`ipfs.addAll` and fail with a more helpful error message if you do not
do this.

BREAKING CHANGE: errors will now be thrown if multiple items are passed to `ipfs.add` or single items to `ipfs.addAll` (n.b. you can still pass a list of a single item to `ipfs.addAll`)
  • Loading branch information
achingbrain committed Sep 29, 2021
1 parent f8367ca commit 886987e
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createAddAll } from './add-all.js'
import last from 'it-last'
import { configure } from './lib/configure.js'
import { normaliseInput } from 'ipfs-core-utils/files/normalise-input-single'

/**
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
Expand All @@ -18,7 +19,7 @@ export function createAdd (options) {
*/
async function add (input, options = {}) {
// @ts-ignore - last may return undefined if source is empty
return await last(all(input, options))
return await last(all(normaliseInput(input), options))
}
return add
})(options)
Expand Down
2 changes: 1 addition & 1 deletion src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const createPut = configure(api => {
signal: signal,
searchParams: toUrlSearchParams(options),
...(
await multipartRequest(data, controller, options.headers)
await multipartRequest([data], controller, options.headers)
)
})
res = await response.json()
Expand Down
2 changes: 1 addition & 1 deletion src/config/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const createReplace = configure(api => {
signal,
searchParams: toUrlSearchParams(options),
...(
await multipartRequest(uint8ArrayFromString(JSON.stringify(config)), controller, options.headers)
await multipartRequest([uint8ArrayFromString(JSON.stringify(config))], controller, options.headers)
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const createPut = (codecs, options) => {
signal,
searchParams: toUrlSearchParams(settings),
...(
await multipartRequest(serialized, controller, settings.headers)
await multipartRequest([serialized], controller, settings.headers)
)
})
const data = await res.json()
Expand Down
2 changes: 1 addition & 1 deletion src/dht/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const createPut = configure(api => {
...options
}),
...(
await multipartRequest(value, controller, options.headers)
await multipartRequest([value], controller, options.headers)
)
})

Expand Down
4 changes: 2 additions & 2 deletions src/files/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export const createWrite = configure(api => {
...options
}),
...(
await multipartRequest({
await multipartRequest([{
content: input,
path: 'arg',
mode: modeToString(options.mode),
mtime: parseMtime(options.mtime)
}, controller, options.headers)
}], controller, options.headers)
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/object/patch/append-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const createAppendData = configure(api => {
...options
}),
...(
await multipartRequest(data, controller, options.headers)
await multipartRequest([data], controller, options.headers)
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/object/patch/set-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const createSetData = configure(api => {
...options
}),
...(
await multipartRequest(data, controller, options.headers)
await multipartRequest([data], controller, options.headers)
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/pubsub/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createPublish = configure(api => {
signal,
searchParams,
...(
await multipartRequest(data, controller, options.headers)
await multipartRequest([data], controller, options.headers)
)
})

Expand Down

0 comments on commit 886987e

Please sign in to comment.