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

Commit 3cd19e7

Browse files
committed
feat: callbackify and top level jsdoc and more
1 parent 300af44 commit 3cd19e7

File tree

4 files changed

+132
-9
lines changed

4 files changed

+132
-9
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"once": "^1.4.0",
5555
"peer-id": "~0.11.0",
5656
"peer-info": "~0.14.1",
57+
"promise-nodeify": "^3.0.1",
5758
"promisify-es6": "^1.0.3",
5859
"pull-defer": "~0.2.2",
5960
"pull-pushable": "^2.2.0",

src/add2/add2.js

+80-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
const { Readable } = require('stream')
44
const toPull = require('stream-to-pull-stream')
5+
const promiseNodeify = require('promise-nodeify')
56
const concatStream = require('concat-stream')
67
const pump = require('pump')
78
const SendStream = require('./send-stream')
89

10+
/** @module api/add */
11+
12+
/**
13+
* Converts an array to a stream
14+
*
15+
* @private
16+
* @param {Array} data
17+
* @returns {Readable}
18+
*/
919
const arrayToStream = (data) => {
1020
let i = 0
1121
return new Readable({
@@ -16,10 +26,59 @@ const arrayToStream = (data) => {
1626
})
1727
}
1828

19-
const add = (send) => (files, options) => {
20-
// check if we can receive pull-stream after callbackify
29+
/**
30+
* @typedef {Object} AddOptions
31+
* @property {number} chunkSize - Value of array element
32+
* @property {number} [cidVersion=0] - Defaults to 0. The CID version to use when storing the data (storage keys are based on the CID, including it's version)
33+
* @property {function(bytes: number): void} progress - function that will be called with the byte length of chunks as a file is added to ipfs.
34+
* @property {Boolean} recursive - When a Path is passed, this option can be enabled to add recursively all the files.
35+
* @property {string} hashAlg - Multihash hashing algorithm to use. (default: sha2-256) The list of all possible values {@link https://github.com/multiformats/js-multihash/blob/master/src/constants.js#L5-L343 hashAlg values}
36+
* @property {Boolean} wrapWithDirectory - Adds a wrapping node around the content.
37+
* @property {Boolean} onlyHash - Doesn't actually add the file to IPFS, but rather calculates its hash.
38+
* @property {Boolean} [pin=true] - Defaults to true. Pin this object when adding.
39+
* @property {Boolean} [rawLeaves=false] - Defaults to false. If true, DAG leaves will contain raw file data and not be wrapped in a protobuf
40+
* @property {string} [chunker=size-262144] Chunking algorithm used to build ipfs DAGs. Available formats:
41+
* - size-{size}
42+
* - rabin
43+
* - rabin-{avg}
44+
* - rabin-{min}-{avg}-{max}
45+
*/
46+
47+
/**
48+
* @typedef {Object} AddResult
49+
* @property {string} path
50+
* @property {string} hash
51+
* @property {number} size
52+
*/
53+
54+
/**
55+
* This callback is displayed as a global member.
56+
* @callback AddCallback
57+
* @param {Error} err
58+
* @param {AddResult[]} res
59+
*/
60+
61+
/** @typedef {Function} PullStream */
62+
/** @typedef {(Object[]|Readable|File|PullStream|Buffer)} AddData */
63+
/**
64+
* @typedef {function(AddData, AddOptions, AddCallback): (Promise.<AddResult[]>|void)} AddFunction
65+
*/
66+
67+
/**
68+
* Add to data to ipfs
69+
*
70+
* @param {Function} send
71+
* @returns {AddFunction}
72+
* @memberof api/add
73+
*/
74+
const add = (send) => (files, options, callback) => {
75+
if (typeof options === 'function') {
76+
callback = options
77+
options = {}
78+
}
79+
2180
let result = []
22-
return new Promise((resolve, reject) => {
81+
const r = new Promise((resolve, reject) => {
2382
pump(
2483
arrayToStream([].concat(files)),
2584
new SendStream(send, options),
@@ -32,13 +91,29 @@ const add = (send) => (files, options) => {
3291
}
3392
)
3493
})
94+
95+
return promiseNodeify(r, callback)
3596
}
3697

37-
const addReadableStream = (send) => (options) => {
98+
/**
99+
* Add to data to ipfs
100+
*
101+
* @param {Function} send
102+
* @returns {function(AddOptions): Readable}
103+
* @memberof api/add
104+
*/
105+
const addReadableStream = (send) => (options = {}) => {
38106
return new SendStream(send, options)
39107
}
40108

41-
const addPullStream = (send) => (options) => {
109+
/**
110+
* Add to data to ipfs
111+
*
112+
* @param {Function} send
113+
* @returns {function(AddOptions): PullStream}
114+
* @memberof api/add
115+
*/
116+
const addPullStream = (send) => (options = {}) => {
42117
return toPull(new SendStream(send, options))
43118
}
44119

src/add2/multipart2.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ const { Duplex } = require('stream')
44
const { isSource } = require('is-pull-stream')
55
const toStream = require('pull-stream-to-stream')
66

7+
/** @private @typedef {import("./add2").AddOptions} AddOptions */
8+
79
const PADDING = '--'
810
const NEW_LINE = '\r\n'
911
const NEW_LINE_BUFFER = Buffer.from(NEW_LINE)
1012

13+
/**
14+
* Generate a random boundary to use in a multipart request
15+
*
16+
* @private
17+
* @returns {string}
18+
*/
1119
const generateBoundary = () => {
1220
var boundary = '--------------------------'
1321
for (var i = 0; i < 24; i++) {
@@ -17,6 +25,14 @@ const generateBoundary = () => {
1725
return boundary
1826
}
1927

28+
/**
29+
* Generate leading section for a multipart body
30+
*
31+
* @private
32+
* @param {Object} [headers={}]
33+
* @param {string} boundary
34+
* @returns {string}
35+
*/
2036
const leading = (headers = {}, boundary) => {
2137
var leading = [PADDING + boundary]
2238

@@ -32,16 +48,27 @@ const leading = (headers = {}, boundary) => {
3248
return Buffer.from(leadingStr)
3349
}
3450

51+
/**
52+
* Multipart class to generate a multipart body chunked and non chunked
53+
*
54+
* @private
55+
* @class Multipart
56+
* @extends {Duplex}
57+
*/
3558
class Multipart extends Duplex {
36-
constructor ({ chunkSize }) {
59+
/**
60+
* Creates an instance of Multipart.
61+
* @param {AddOptions} options
62+
*/
63+
constructor (options) {
3764
super({
3865
writableObjectMode: true,
3966
writableHighWaterMark: 1
4067
})
4168

4269
this._boundary = generateBoundary()
4370
this.source = null
44-
this.chunkSize = chunkSize || 0
71+
this.chunkSize = options.chunkSize || 0
4572
this.buffer = Buffer.alloc(this.chunkSize)
4673
this.bufferOffset = 0
4774
this.extraBytes = 0
@@ -97,7 +124,7 @@ class Multipart extends Duplex {
97124
return this.push(null)
98125
}
99126

100-
if (!this.chunkSize) {
127+
if (this.chunkSize === 0) {
101128
return this.push(chunk)
102129
}
103130

src/add2/send-stream.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ const pump = require('pump')
66
const Multipart = require('./multipart2')
77
const { prepareWithHeaders } = require('./../utils/prepare-file')
88

9+
/** @private @typedef {import("./add2").AddOptions} AddOptions */
10+
911
const noop = () => {}
1012

13+
/**
14+
* Factory for prepare stream
15+
* @private
16+
* @param {*} options
17+
* @returns {Function}
18+
*/
1119
const prepareTransform = (options) => new Transform({
1220
objectMode: true,
1321
transform (chunk, encoding, callback) {
@@ -19,8 +27,20 @@ const prepareTransform = (options) => new Transform({
1927
}
2028
})
2129

30+
/**
31+
* Class to create a stream to send data to the API
32+
*
33+
* @private
34+
* @class SendStream
35+
* @extends {Duplex}
36+
*/
2237
class SendStream extends Duplex {
23-
constructor (send, options) {
38+
/**
39+
* Creates an instance of SendStream.
40+
* @param {Function} send
41+
* @param {AddOptions} [options={}]
42+
*/
43+
constructor (send, options = {}) {
2444
super({ objectMode: true, highWaterMark: 1 })
2545
this.waiting = null
2646
this.options = options

0 commit comments

Comments
 (0)