Skip to content

Commit fdbee13

Browse files
authored
chore: move mfs and multipart files into core (#2811)
BREAKING CHANGE: When the path passed to `ipfs.files.stat(path)` was a hamt sharded dir, the resovled value returned by js-ipfs previously had a `type` property of with a value of `'hamt-sharded-directory'`. To bring it in line with go-ipfs this value is now `'directory'`.
1 parent f745a15 commit fdbee13

29 files changed

+404
-640
lines changed

Diff for: package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
],
1616
"main": "src/index.js",
1717
"browser": {
18-
"./src/add/form-data.js": "./src/add/form-data.browser.js",
19-
"./src/lib/buffer-to-form-data.js": "./src/lib/buffer-to-form-data.browser.js",
18+
"./src/lib/to-stream.js": "./src/lib/to-stream.browser.js",
2019
"ipfs-utils/src/files/glob-source": false
2120
},
2221
"repository": {
@@ -53,19 +52,21 @@
5352
"ipld-raw": "^4.0.1",
5453
"iso-url": "^0.4.6",
5554
"it-tar": "^1.2.1",
55+
"it-to-buffer": "^1.0.0",
5656
"it-to-stream": "^0.1.1",
5757
"merge-options": "^2.0.0",
5858
"multiaddr": "^7.2.1",
5959
"multiaddr-to-uri": "^5.1.0",
6060
"multibase": "^0.6.0",
6161
"multicodec": "^1.0.0",
6262
"multihashes": "^0.4.14",
63+
"nanoid": "^2.1.11",
6364
"node-fetch": "^2.6.0",
6465
"parse-duration": "^0.1.2",
6566
"stream-to-it": "^0.2.0"
6667
},
6768
"devDependencies": {
68-
"aegir": "^21.3.0",
69+
"aegir": "21.3.0",
6970
"browser-process-platform": "^0.1.1",
7071
"cross-env": "^7.0.0",
7172
"go-ipfs-dep": "0.4.23-3",

Diff for: src/add/index.js renamed to src/add.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
'use strict'
22

33
const CID = require('cids')
4-
const merge = require('merge-options')
5-
const { toFormData } = require('./form-data')
6-
const toCamel = require('../lib/object-to-camel')
7-
const configure = require('../lib/configure')
4+
const toCamel = require('./lib/object-to-camel')
5+
const configure = require('./lib/configure')
6+
const multipartRequest = require('./lib/multipart-request')
7+
const toUrlSearchParams = require('./lib/to-url-search-params')
88

99
module.exports = configure((api) => {
1010
return async function * add (input, options = {}) {
1111
const progressFn = options.progress
12-
options = merge(
13-
options,
14-
{
15-
'stream-channels': true,
16-
progress: Boolean(progressFn),
17-
hash: options.hashAlg // TODO fix this either is hash or hashAlg
18-
}
19-
)
2012

2113
const res = await api.ndjson('add', {
2214
method: 'POST',
23-
searchParams: options,
24-
body: await toFormData(input),
15+
searchParams: toUrlSearchParams(null, {
16+
...options,
17+
'stream-channels': true,
18+
progress: Boolean(progressFn)
19+
}),
2520
timeout: options.timeout,
26-
signal: options.signal
21+
signal: options.signal,
22+
...(
23+
await multipartRequest(input)
24+
)
2725
})
2826

2927
for await (let file of res) {

Diff for: src/add/form-data.browser.js

-61
This file was deleted.

Diff for: src/add/form-data.js

-60
This file was deleted.

Diff for: src/block/put.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const Block = require('ipfs-block')
44
const CID = require('cids')
55
const multihash = require('multihashes')
6-
const toFormData = require('../lib/buffer-to-form-data')
6+
const multipartRequest = require('../lib/multipart-request')
77
const configure = require('../lib/configure')
88

99
module.exports = configure(api => {
@@ -37,7 +37,9 @@ module.exports = configure(api => {
3737
timeout: options.timeout,
3838
signal: options.signal,
3939
searchParams: options,
40-
body: toFormData(data)
40+
...(
41+
await multipartRequest(data)
42+
)
4143
})
4244
res = await response.json()
4345
} catch (err) {

Diff for: src/config/replace.js

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

33
const { Buffer } = require('buffer')
4-
const toFormData = require('../lib/buffer-to-form-data')
4+
const multipartRequest = require('../lib/multipart-request')
55
const configure = require('../lib/configure')
66

77
module.exports = configure(api => {
@@ -10,7 +10,9 @@ module.exports = configure(api => {
1010
timeout: options.timeout,
1111
signal: options.signal,
1212
searchParams: options,
13-
body: toFormData(Buffer.from(JSON.stringify(config)))
13+
...(
14+
await multipartRequest(Buffer.from(JSON.stringify(config)))
15+
)
1416
})
1517

1618
return res.text()

Diff for: src/dag/put.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const dagCBOR = require('ipld-dag-cbor')
44
const CID = require('cids')
55
const multihash = require('multihashes')
6-
const toFormData = require('../lib/buffer-to-form-data')
76
const configure = require('../lib/configure')
7+
const multipartRequest = require('../lib/multipart-request')
88

99
module.exports = configure(api => {
1010
return async (dagNode, options = {}) => {
@@ -51,7 +51,9 @@ module.exports = configure(api => {
5151
timeout: options.timeout,
5252
signal: options.signal,
5353
searchParams,
54-
body: toFormData(serialized)
54+
...(
55+
await multipartRequest(serialized)
56+
)
5557
})
5658
const data = await rsp.json()
5759

Diff for: src/files/chmod.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
'use strict'
22

3-
const modeToString = require('../lib/mode-to-string')
43
const configure = require('../lib/configure')
4+
const toUrlSearchParams = require('../lib/to-url-search-params')
55

66
module.exports = configure(api => {
77
return async function chmod (path, mode, options = {}) {
8-
options.arg = path
9-
options.mode = modeToString(mode)
10-
options.hash = options.hashAlg
11-
options.hashAlg = null
12-
138
const res = await api.post('files/chmod', {
149
timeout: options.timeout,
1510
signal: options.signal,
16-
searchParams: options
11+
searchParams: toUrlSearchParams(path, { ...options, mode })
1712
})
1813

19-
return res.text()
14+
await res.text()
2015
}
2116
})

Diff for: src/files/cp.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
const CID = require('cids')
44
const { findSources } = require('./utils')
55
const configure = require('../lib/configure')
6+
const toUrlSearchParams = require('../lib/to-url-search-params')
67

78
module.exports = configure(api => {
89
return async (...args) => {
910
const { sources, options } = findSources(args)
1011

11-
const searchParams = new URLSearchParams(options)
12-
sources.forEach(src => searchParams.append('arg', CID.isCID(src) ? `/ipfs/${src}` : src))
13-
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
14-
1512
const res = await api.post('files/cp', {
1613
timeout: options.timeout,
1714
signal: options.signal,
18-
searchParams
15+
searchParams: toUrlSearchParams(
16+
sources.map(src => CID.isCID(src) ? `/ipfs/${src}` : src),
17+
options
18+
)
1919
})
20-
return res.text()
20+
21+
await res.text()
2122
}
2223
})

Diff for: src/files/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
module.exports = config => ({
44
chmod: require('./chmod')(config),
55
cp: require('./cp')(config),
6-
mkdir: require('./mkdir')(config),
76
flush: require('./flush')(config),
8-
stat: require('./stat')(config),
9-
rm: require('./rm')(config),
107
ls: require('./ls')(config),
8+
mkdir: require('./mkdir')(config),
9+
mv: require('./mv')(config),
1110
read: require('./read')(config),
11+
rm: require('./rm')(config),
12+
stat: require('./stat')(config),
1213
touch: require('./touch')(config),
13-
write: require('./write')(config),
14-
mv: require('./mv')(config)
14+
write: require('./write')(config)
1515
})

Diff for: src/files/ls.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const CID = require('cids')
44
const toCamelWithMetadata = require('../lib/object-to-camel-with-metadata')
55
const configure = require('../lib/configure')
6+
const toUrlSearchParams = require('../lib/to-url-search-params')
67

78
module.exports = configure(api => {
89
return async function * ls (path, options = {}) {
@@ -11,19 +12,22 @@ module.exports = configure(api => {
1112
path = '/'
1213
}
1314

14-
const searchParams = new URLSearchParams(options)
15-
searchParams.set('arg', CID.isCID(path) ? `/ipfs/${path}` : path)
16-
// TODO the args below are not in the go-ipfs or interface core
17-
searchParams.set('stream', options.stream == null ? true : options.stream)
18-
searchParams.set('long', options.long == null ? true : options.long)
19-
// TODO: remove after go-ipfs 0.5 is released
20-
searchParams.set('l', options.long == null ? true : options.long)
21-
2215
const res = await api.ndjson('files/ls', {
2316
method: 'POST',
2417
timeout: options.timeout,
2518
signal: options.signal,
26-
searchParams
19+
searchParams: toUrlSearchParams(
20+
CID.isCID(path) ? `/ipfs/${path}` : path, {
21+
...options,
22+
23+
// TODO the args below are not in the go-ipfs or interface core
24+
stream: options.stream == null ? true : options.stream,
25+
long: options.long == null ? true : options.long,
26+
27+
// TODO: remove after go-ipfs 0.5 is released
28+
l: options.long == null ? true : options.long
29+
}
30+
)
2731
})
2832

2933
for await (const result of res) {

0 commit comments

Comments
 (0)