Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 57659c0

Browse files
committed
refactor: convert some object and files API methods to async/await
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 0e01187 commit 57659c0

File tree

7 files changed

+97
-136
lines changed

7 files changed

+97
-136
lines changed

src/files/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

3+
const callbackify = require('callbackify')
34
const moduleConfig = require('../utils/module-config')
45

5-
module.exports = (arg) => {
6+
module.exports = (arg, config) => {
67
const send = moduleConfig(arg)
78

89
return {
@@ -17,7 +18,7 @@ module.exports = (arg) => {
1718
read: require('./read')(send),
1819
readReadableStream: require('./read-readable-stream')(send),
1920
readPullStream: require('./read-pull-stream')(send),
20-
write: require('./write')(send),
21+
write: callbackify.variadic(require('./write')(config)),
2122
mv: require('./mv')(send)
2223
}
2324
}

src/files/write.js

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
4-
const concatStream = require('concat-stream')
5-
const once = require('once')
6-
const SendFilesStream = require('../utils/send-files-stream')
7-
8-
module.exports = (send) => {
9-
const sendFilesStream = SendFilesStream(send, 'files/write')
10-
11-
return promisify((pathDst, _files, opts, _callback) => {
12-
if (typeof opts === 'function' &&
13-
!_callback) {
14-
_callback = opts
15-
opts = {}
16-
}
17-
18-
// opts is the real callback --
19-
// 'callback' is being injected by promisify
20-
if (typeof opts === 'function' &&
21-
typeof _callback === 'function') {
22-
_callback = opts
23-
opts = {}
24-
}
25-
26-
const files = [].concat(_files)
27-
const callback = once(_callback)
28-
29-
const options = {
30-
args: pathDst,
31-
qs: opts
32-
}
33-
34-
const stream = sendFilesStream({ qs: options })
35-
const concat = concatStream((result) => callback(null, result))
36-
stream.once('error', callback)
37-
stream.pipe(concat)
38-
39-
files.forEach((file) => stream.write(file))
40-
stream.end()
41-
})
42-
}
3+
const configure = require('../lib/configure')
4+
const toFormData = require('../lib/buffer-to-form-data')
5+
6+
module.exports = configure(({ ky }) => {
7+
return async (path, input, options) => {
8+
options = options || {}
9+
10+
const searchParams = new URLSearchParams(options.searchParams)
11+
searchParams.set('arg', path)
12+
searchParams.set('stream-channels', true)
13+
if (options.cidVersion) searchParams.set('cid-version', options.cidVersion)
14+
if (options.create != null) searchParams.set('create', options.create)
15+
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
16+
if (options.length != null) searchParams.set('length', options.length)
17+
if (options.offset != null) searchParams.set('offset', options.offset)
18+
if (options.parents != null) searchParams.set('parents', options.parents)
19+
if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves)
20+
if (options.truncate != null) searchParams.set('truncate', options.truncate)
21+
22+
const res = await ky.post('files/write', {
23+
timeout: options.timeout,
24+
signal: options.signal,
25+
headers: options.headers,
26+
searchParams,
27+
body: toFormData(input) // TODO: support inputs other than buffer as per spec
28+
})
29+
30+
return res.text()
31+
}
32+
})

src/object/appendData.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
4-
const once = require('once')
53
const CID = require('cids')
6-
const SendOneFile = require('../utils/send-one-file')
4+
const configure = require('../lib/configure')
5+
const toFormData = require('../lib/buffer-to-form-data')
76

8-
module.exports = (send) => {
9-
const sendOneFile = SendOneFile(send, 'object/patch/append-data')
7+
module.exports = configure(({ ky }) => {
8+
return async (cid, data, options) => {
9+
options = options || {}
1010

11-
return promisify((cid, data, opts, _callback) => {
12-
if (typeof opts === 'function') {
13-
_callback = opts
14-
opts = {}
15-
}
16-
const callback = once(_callback)
17-
if (!opts) {
18-
opts = {}
19-
}
11+
const searchParams = new URLSearchParams(options.searchParams)
12+
searchParams.set('arg', `${cid}`)
2013

21-
try {
22-
cid = new CID(cid)
23-
} catch (err) {
24-
return callback(err)
25-
}
14+
const { Hash } = await ky.post('object/patch/append-data', {
15+
timeout: options.timeout,
16+
signal: options.signal,
17+
headers: options.headers,
18+
searchParams,
19+
body: toFormData(data)
20+
}).json()
2621

27-
sendOneFile(data, { args: [cid.toString()] }, (err, result) => {
28-
if (err) {
29-
return callback(err)
30-
}
31-
32-
callback(null, new CID(result.Hash))
33-
})
34-
})
35-
}
22+
return new CID(Hash)
23+
}
24+
})

src/object/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
'use strict'
22

3+
const callbackify = require('callbackify')
34
const moduleConfig = require('../utils/module-config')
45

5-
module.exports = (arg) => {
6+
module.exports = (arg, config) => {
67
const send = moduleConfig(arg)
78

89
return {
910
get: require('./get')(send),
10-
put: require('./put')(send),
11+
put: callbackify.variadic(require('./put')(config)),
1112
data: require('./data')(send),
1213
links: require('./links')(send),
1314
stat: require('./stat')(send),
1415
new: require('./new')(send),
1516
patch: {
1617
addLink: require('./addLink')(send),
1718
rmLink: require('./rmLink')(send),
18-
setData: require('./setData')(send),
19-
appendData: require('./appendData')(send)
19+
setData: callbackify.variadic(require('./setData')(config)),
20+
appendData: callbackify.variadic(require('./appendData')(config))
2021
}
2122
}
2223
}

src/object/put.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
43
const CID = require('cids')
54
const { DAGNode } = require('ipld-dag-pb')
5+
const { Buffer } = require('buffer')
6+
const configure = require('../lib/configure')
7+
const toFormData = require('../lib/buffer-to-form-data')
68

7-
const SendOneFile = require('../utils/send-one-file')
8-
const once = require('once')
9-
10-
module.exports = (send) => {
11-
const sendOneFile = SendOneFile(send, 'object/put')
12-
13-
return promisify((obj, options, _callback) => {
14-
if (typeof options === 'function') {
15-
_callback = options
16-
options = {}
17-
}
18-
19-
const callback = once(_callback)
20-
21-
if (!options) {
22-
options = {}
23-
}
9+
module.exports = configure(({ ky }) => {
10+
return async (obj, options) => {
11+
options = options || {}
2412

2513
let tmpObj = {
2614
Data: null,
@@ -47,7 +35,7 @@ module.exports = (send) => {
4735
tmpObj.Data = obj.Data.toString()
4836
tmpObj.Links = obj.Links
4937
} else {
50-
return callback(new Error('obj not recognized'))
38+
throw new Error('obj not recognized')
5139
}
5240

5341
let buf
@@ -56,18 +44,20 @@ module.exports = (send) => {
5644
} else {
5745
buf = Buffer.from(JSON.stringify(tmpObj))
5846
}
59-
const enc = options.enc || 'json'
60-
61-
const sendOptions = {
62-
qs: { inputenc: enc }
63-
}
64-
65-
sendOneFile(buf, sendOptions, (err, result) => {
66-
if (err) {
67-
return callback(err) // early
68-
}
6947

70-
callback(null, new CID(result.Hash))
71-
})
72-
})
73-
}
48+
const searchParams = new URLSearchParams(options.searchParams)
49+
if (options.enc) searchParams.set('inputenc', options.enc)
50+
if (options.pin != null) searchParams.set('pin', options.pin)
51+
if (options.quiet != null) searchParams.set('quiet', options.quiet)
52+
53+
const { Hash } = await ky.post('object/put', {
54+
timeout: options.timeout,
55+
signal: options.signal,
56+
headers: options.headers,
57+
searchParams,
58+
body: toFormData(buf)
59+
}).json()
60+
61+
return new CID(Hash)
62+
}
63+
})

src/object/setData.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
4-
const once = require('once')
53
const CID = require('cids')
6-
const SendOneFile = require('../utils/send-one-file')
4+
const configure = require('../lib/configure')
5+
const toFormData = require('../lib/buffer-to-form-data')
76

8-
module.exports = (send) => {
9-
const sendOneFile = SendOneFile(send, 'object/patch/set-data')
7+
module.exports = configure(({ ky }) => {
8+
return async (cid, data, options) => {
9+
options = options || {}
1010

11-
return promisify((cid, data, opts, _callback) => {
12-
if (typeof opts === 'function') {
13-
_callback = opts
14-
opts = {}
15-
}
16-
const callback = once(_callback)
17-
if (!opts) {
18-
opts = {}
19-
}
11+
const searchParams = new URLSearchParams(options.searchParams)
12+
searchParams.set('arg', `${cid}`)
2013

21-
try {
22-
cid = new CID(cid)
23-
} catch (err) {
24-
return callback(err)
25-
}
14+
const { Hash } = await ky.post('object/patch/set-data', {
15+
timeout: options.timeout,
16+
signal: options.signal,
17+
headers: options.headers,
18+
searchParams,
19+
body: toFormData(data)
20+
}).json()
2621

27-
sendOneFile(data, { args: [cid.toString()] }, (err, result) => {
28-
if (err) {
29-
return callback(err)
30-
}
31-
callback(null, new CID(result.Hash))
32-
})
33-
})
34-
}
22+
return new CID(Hash)
23+
}
24+
})

src/utils/tar-stream-to-objects.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const tar = require('tar-stream')
3+
const tarExtract = require('tar-stream/extract')
44
const { EventIterator } = require('event-iterator')
55

66
function pipe (reader, writable) {
@@ -35,7 +35,7 @@ function pipe (reader, writable) {
3535
{ path: 'string', content: AsyncIterator<Buffer> }
3636
*/
3737
async function * tarStreamToObjects (inputStream) {
38-
const extractStream = tar.extract()
38+
const extractStream = tarExtract()
3939
let onEntry
4040

4141
const tarStream = new EventIterator(

0 commit comments

Comments
 (0)