Skip to content

Commit c14e0e4

Browse files
feat: sync with go-ipfs 0.5 (#3013)
New features: - `ipfs.dht.get` accepts string or buffer - http-client `ipfs.dht.get` returns a buffer to match core api Breaking changes: - ipfs.ls remove all options in core api, but keep `long` in http api and cli same as go-ipfs - remove sort from the cli and ignore go-ipfs `U` option - enable http api mfs tests - key.gen defaults, to rsa and 2048 - pin.list only supports streaming responses - -U argument to `files.ls` has been removed - dht put uses body to send data instead of query string Co-authored-by: Alex Potsides <[email protected]>
1 parent 8c9d17c commit c14e0e4

9 files changed

+32
-64
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"devDependencies": {
7171
"aegir": "^22.0.0",
7272
"cross-env": "^7.0.0",
73-
"go-ipfs-dep": "0.4.23-3",
73+
"go-ipfs-dep": "^0.5.1",
7474
"interface-ipfs-core": "^0.135.1",
7575
"ipfsd-ctl": "^4.1.1",
7676
"it-all": "^1.0.1",

src/dht/get.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
'use strict'
22

33
const { Buffer } = require('buffer')
4-
const encodeBufferURIComponent = require('../lib/encode-buffer-uri-component')
54
const configure = require('../lib/configure')
65
const toUrlSearchParams = require('../lib/to-url-search-params')
76
const { Value } = require('./response-types')
87

98
module.exports = configure(api => {
109
return async function get (key, options = {}) {
11-
if (!Buffer.isBuffer(key)) {
12-
throw new Error('invalid key')
13-
}
14-
1510
const res = await api.post('dht/get', {
1611
timeout: options.timeout,
1712
signal: options.signal,
1813
searchParams: toUrlSearchParams({
19-
key: encodeBufferURIComponent(key),
14+
arg: Buffer.isBuffer(key) ? key.toString() : key,
2015
...options
2116
}),
2217
headers: options.headers
2318
})
2419

2520
for await (const message of res.ndjson()) {
2621
if (message.Type === Value) {
27-
return message.Extra
22+
return Buffer.from(message.Extra, 'base64')
2823
}
2924
}
3025

src/dht/put.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ const multiaddr = require('multiaddr')
55
const toCamel = require('../lib/object-to-camel')
66
const configure = require('../lib/configure')
77
const toUrlSearchParams = require('../lib/to-url-search-params')
8+
const multipartRequest = require('../lib/multipart-request')
89

910
module.exports = configure(api => {
1011
return async function * put (key, value, options = {}) {
1112
const res = await api.post('dht/put', {
1213
timeout: options.timeout,
1314
signal: options.signal,
1415
searchParams: toUrlSearchParams({
15-
arg: [
16-
key,
17-
value
18-
],
16+
arg: key,
1917
...options
2018
}),
21-
headers: options.headers
19+
...(
20+
await multipartRequest(value, options.headers)
21+
)
2222
})
2323

2424
for await (let message of res.ndjson()) {

src/files/ls.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ module.exports = configure(api => {
1717
signal: options.signal,
1818
searchParams: toUrlSearchParams({
1919
arg: CID.isCID(path) ? `/ipfs/${path}` : path,
20-
21-
// TODO the args below are not in the go-ipfs or interface core docs
22-
long: options.long == null ? true : options.long,
23-
24-
// TODO: remove after go-ipfs 0.5 is released
25-
l: options.long == null ? true : options.long,
20+
// default long to true, diverges from go-ipfs where its false by default
21+
long: true,
2622
...options,
2723
stream: true
2824
}),

src/lib/encode-buffer-uri-component.js

-25
This file was deleted.

src/lib/multipart-request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const modeToString = require('../lib/mode-to-string')
77
const mtimeToObject = require('../lib/mtime-to-object')
88
const merge = require('merge-options').bind({ ignoreUndefined: true })
99

10-
async function multipartRequest (source, abortController, headers = {}, boundary = `-----------------------------${nanoid()}`) {
10+
async function multipartRequest (source = '', abortController, headers = {}, boundary = `-----------------------------${nanoid()}`) {
1111
async function * streamFiles (source) {
1212
try {
1313
let index = 0

src/pin/ls.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ module.exports = configure(api => {
2525
})
2626

2727
for await (const pin of res.ndjson()) {
28-
if (pin.Keys) { // non-streaming response
29-
// eslint-disable-next-line guard-for-in
30-
for (const key in pin.Keys) {
31-
yield { cid: new CID(key), type: pin.Keys[key].Type }
32-
}
33-
} else {
34-
yield { cid: new CID(pin.Cid), type: pin.Type }
35-
}
28+
yield { cid: new CID(pin.Cid), type: pin.Type }
3629
}
3730
}
3831
})

src/pubsub/publish.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
4-
const encodeBuffer = require('../lib/encode-buffer-uri-component')
53
const configure = require('../lib/configure')
64
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
const multipartRequest = require('../lib/multipart-request')
6+
const anySignal = require('any-signal')
7+
const AbortController = require('abort-controller')
78

89
module.exports = configure(api => {
910
return async (topic, data, options = {}) => {
10-
data = Buffer.from(data)
11-
1211
const searchParams = toUrlSearchParams({
13-
arg: [
14-
topic
15-
],
12+
arg: topic,
1613
...options
1714
})
1815

19-
const res = await api.post(`pubsub/pub?${searchParams}&arg=${encodeBuffer(data)}`, {
16+
// allow aborting requests on body errors
17+
const controller = new AbortController()
18+
const signal = anySignal([controller.signal, options.signal])
19+
20+
const res = await api.post('pubsub/pub', {
2021
timeout: options.timeout,
21-
signal: options.signal,
22-
headers: options.headers
22+
signal,
23+
searchParams,
24+
...(
25+
await multipartRequest(data, controller, options.headers)
26+
)
2327
})
2428

2529
await res.text()

test/interface.spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ describe('interface-ipfs-core tests', () => {
5151
]
5252
})
5353

54-
tests.bitswap(commonFactory)
54+
tests.bitswap(commonFactory, {
55+
skip: [{
56+
name: 'should get the wantlist by peer ID for a different node',
57+
reason: 'unskip when https://github.com/ipfs/go-bitswap/pull/390 is released in go-ipfs'
58+
}]
59+
})
5560

5661
tests.block(commonFactory, {
5762
skip: [{

0 commit comments

Comments
 (0)