Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit c2ec920

Browse files
committed
chore: factor out types into ipfs-interface
1 parent a699b9d commit c2ec920

35 files changed

+645
-334
lines changed

packages/ipfs-core-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"cids": "^1.0.0",
4545
"err-code": "^2.0.3",
4646
"ipfs-utils": "^5.0.0",
47+
"ipfs-interface": "^0.1.0",
4748
"it-all": "^1.0.4",
4849
"it-map": "^1.0.4",
4950
"it-peekable": "^1.0.1",

packages/ipfs-core-utils/src/files/format-mode.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function checkPermission (mode, perm, type, output) {
2626

2727
/**
2828
*
29-
* @param {Mode} mode
29+
* @param {import('ipfs-interface/src/files').Mode} mode
3030
* @param {boolean} isDirectory
3131
* @returns {string}
3232
*/
@@ -70,7 +70,3 @@ function formatMode (mode, isDirectory) {
7070
}
7171

7272
module.exports = formatMode
73-
74-
/**
75-
* @typedef {number} Mode
76-
*/

packages/ipfs-core-utils/src/files/format-mtime.js

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

33
/**
4-
* @param {MTime} mtime
4+
* @param {import('ipfs-interface/src/files').MTime} mtime
55
* @returns {string}
66
*/
77
function formatMtime (mtime) {
@@ -22,12 +22,4 @@ function formatMtime (mtime) {
2222
})
2323
}
2424

25-
/**
26-
* @typedef {object} MTime
27-
* @property {number} secs - the number of seconds since (positive) or before
28-
* (negative) the Unix Epoch began
29-
* @property {number} nsecs - the number of nanoseconds since the last full
30-
* second.
31-
*/
32-
3325
module.exports = formatMtime

packages/ipfs-core-utils/src/files/normalise-input/index.browser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const normaliseInput = require('./normalise-input')
1212
*
1313
* See https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#ipfsadddata-options
1414
*
15-
* @param {import('./normalise-input').Source} input
16-
* @returns {AsyncIterable<import('./normalise-input').Entry<Blob>>}
15+
* @param {import('ipfs-interface/src/files').ImportSource} input
16+
* @returns {AsyncIterable<import('ipfs-interface/src/files').Entry<Blob>>}
1717
*/
1818
module.exports = (input) => normaliseInput(input, normaliseContent)

packages/ipfs-core-utils/src/files/normalise-input/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const normaliseInput = require('./normalise-input')
1212
*
1313
* See https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#ipfsadddata-options
1414
*
15-
* @param {import('./normalise-input').Source} input
16-
* @returns {AsyncIterable<import('./normalise-input').Entry<AsyncIterable<Uint8Array>>>}
15+
* @param {import('ipfs-interface/src/files').ImportSource} input
16+
* @returns {AsyncIterable<import('ipfs-interface/src/files').Entry<AsyncIterable<Uint8Array>>>}
1717
*/
1818
module.exports = (input) => normaliseInput(input, normaliseContent)

packages/ipfs-core-utils/src/files/normalise-input/normalise-input.js

+7-43
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ const {
1515

1616
// eslint-disable-next-line complexity
1717

18+
/**
19+
* @typedef {import('ipfs-interface/src/files').ToContent} ToContent
20+
*/
1821
/**
1922
* @template {Blob|AsyncIterable<Uint8Array>} Content
20-
* @param {Source} input
23+
* @param {import('ipfs-interface/src/files').ImportSource} input
2124
* @param {(content:ToContent) => Content|Promise<Content>} normaliseContent
22-
* @returns {AsyncIterable<Entry<Content>>}
25+
* @returns {AsyncIterable<import('ipfs-interface/src/files').Entry<Content>>}
2326
*/
2427
// eslint-disable-next-line complexity
2528
module.exports = async function * normaliseInput (input, normaliseContent) {
@@ -100,9 +103,9 @@ module.exports = async function * normaliseInput (input, normaliseContent) {
100103

101104
/**
102105
* @template {Blob|AsyncIterable<Uint8Array>} Content
103-
* @param {ToFile} input
106+
* @param {import('ipfs-interface/src/files').ToEntry} input
104107
* @param {(content:ToContent) => Content|Promise<Content>} normaliseContent
105-
* @returns {Promise<Entry<Content>>}
108+
* @returns {Promise<import('ipfs-interface/src/files').Entry<Content>>}
106109
*/
107110
async function toFileObject (input, normaliseContent) {
108111
// @ts-ignore - Those properties don't exist on most input types
@@ -119,42 +122,3 @@ async function toFileObject (input, normaliseContent) {
119122

120123
return file
121124
}
122-
123-
/**
124-
* @typedef {import('../format-mtime').MTime} MTime
125-
* @typedef {import('../format-mode').Mode} Mode
126-
* @typedef {Object} Directory
127-
* @property {string} path
128-
* @property {Mode} [mode]
129-
* @property {MTime} [mtime]
130-
* @property {undefined} [content]
131-
*
132-
* @typedef {Object} FileInput
133-
* @property {string} [path]
134-
* @property {ToContent} [content]
135-
* @property {number | string} [mode]
136-
* @property {UnixTime} [mtime]
137-
*
138-
* @typedef {Date | MTime | HRTime} UnixTime
139-
*
140-
* Time representation as tuple of two integers, as per the output of
141-
* [`process.hrtime()`](https://nodejs.org/dist/latest/docs/api/process.html#process_process_hrtime_time).
142-
* @typedef {[number, number]} HRTime
143-
*
144-
* @typedef {string|InstanceType<typeof window.String>|ArrayBufferView|ArrayBuffer|Blob|Iterable<Uint8Array> | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>} ToContent
145-
* @typedef {ToContent|FileInput} ToFile
146-
* @typedef {Iterable<ToFile> | AsyncIterable<ToFile> | ReadableStream<ToFile>} Source
147-
*/
148-
/**
149-
* @template {AsyncIterable<Uint8Array>|Blob} Content
150-
* @typedef {Object} File
151-
* @property {string} path
152-
* @property {Mode} [mode]
153-
* @property {MTime} [mtime]
154-
* @property {Content} [content]
155-
*/
156-
157-
/**
158-
* @template {AsyncIterable<Uint8Array>|Blob} Content
159-
* @typedef {File<Content>|Directory} Entry
160-
*/

packages/ipfs-core-utils/src/files/normalise-input/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function isBlob (obj) {
2222
* An object with a path or content property
2323
*
2424
* @param {any} obj
25-
* @returns {obj is import('./normalise-input').FileInput}
25+
* @returns {obj is import('ipfs-interface/src/files').ToEntry}
2626
*/
2727
function isFileObject (obj) {
2828
return typeof obj === 'object' && (obj.path || obj.content)

packages/ipfs-core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"ipfs-bitswap": "^4.0.0",
7272
"ipfs-block-service": "^0.18.0",
7373
"ipfs-core-utils": "^0.5.3",
74+
"ipfs-interface": "^0.1.0",
7475
"ipfs-repo": "^7.0.0",
7576
"ipfs-unixfs": "^2.0.3",
7677
"ipfs-unixfs-exporter": "^3.0.4",

packages/ipfs-core/src/components/add-all/index.js

+3-48
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })
1313
* @param {import('..').GCLock} config.gcLock
1414
* @param {import('..').Preload} config.preload
1515
* @param {import('..').Pin} config.pin
16-
* @param {ShardingOptions} [config.options]
16+
* @param {import('ipfs-interface/src/root').ShardingOptions} [config.options]
1717
*/
1818
module.exports = ({ block, gcLock, preload, pin, options }) => {
1919
const isShardingEnabled = options && options.sharding
20-
/**
21-
* Import multiple files and data into IPFS.
22-
*
23-
* @param {FileStream} source
24-
* @param {AddAllOptions & AbortOptions} [options]
25-
* @returns {AsyncIterable<UnixFSEntry>}
26-
*/
20+
21+
/** @type {import('ipfs-interface/src/root').AddAll} */
2722
async function * addAll (source, options = {}) {
2823
const opts = mergeOptions({
2924
shardSplitThreshold: isShardingEnabled ? 1000 : Infinity,
@@ -169,43 +164,3 @@ function pinFile (pin, opts) {
169164
}
170165
}
171166
}
172-
173-
/**
174-
* @typedef {object} UnixFSEntry
175-
* @property {string} path
176-
* @property {CID} cid
177-
* @property {number} [mode]
178-
* @property {MTime} [mtime]
179-
* @property {number} size
180-
*
181-
* @typedef {Object} AddAllOptions
182-
* @property {string} [chunker='size-262144'] - Chunking algorithm used to build
183-
* ipfs DAGs.
184-
* @property {0|1} [cidVersion=0] - The CID version to use when storing the data.
185-
* @property {boolean} [enableShardingExperiment=false] - Allows to create
186-
* directories with an unlimited number of entries currently size of unixfs
187-
* directories is limited by the maximum block size. **Note** that this is an
188-
* experimental feature.
189-
* @property {string} [hashAlg='sha2-256'] - Multihash hashing algorithm to use.
190-
* @property {boolean} [onlyHash=false] - If true, will not add blocks to the
191-
* blockstore.
192-
* @property {boolean} [pin=true] - Pin this object when adding.
193-
* @property {(bytes:number, path:string) => void} [progress] - a function that will be called with the number of bytes added as a file is added to ipfs and the path of the file being added
194-
* @property {boolean} [rawLeaves=false] - If true, DAG leaves will contain raw
195-
* file data and not be wrapped in a protobuf.
196-
* @property {number} [shardSplitThreshold=1000] - Directories with more than this
197-
* number of files will be created as HAMT-sharded directories.
198-
* @property {boolean} [trickle=false] - If true will use the
199-
* [trickle DAG](https://godoc.org/github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-unixfs/importer/trickle)
200-
* format for DAG generation.
201-
* @property {boolean} [wrapWithDirectory=false] - Adds a wrapping node around
202-
* the content.
203-
*
204-
* @typedef {import('ipfs-core-utils/src/files/normalise-input/normalise-input').Source} FileStream
205-
* @typedef {import('../../utils').MTime} MTime
206-
* @typedef {import('../../utils').AbortOptions} AbortOptions
207-
* @typedef {import('..').CID} CID
208-
*
209-
* @typedef {Object} ShardingOptions
210-
* @property {boolean} [sharding]
211-
*/

packages/ipfs-core/src/components/add.js

+17-31
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,25 @@
22

33
const last = require('it-last')
44

5+
/**
6+
* @param {Object} config
7+
* @param {import('ipfs-interface/src/root').AddAll} config.addAll
8+
*/
59
module.exports = ({ addAll }) => {
6-
/**
7-
* Import a file or data into IPFS.
8-
*
9-
* @param {Source} source
10-
* @param {AddOptions & AbortOptions} [options]
11-
* @returns {AddResult}
12-
*/
13-
async function add (source, options) { // eslint-disable-line require-await
14-
/** @type {UnixFSEntry} - Could be undefined if empty */
15-
const result = (await last(addAll(source, options)))
10+
/** @type {import('ipfs-interface/src/root').Add} */
11+
async function add (entry, options) {
12+
/** @type {import('ipfs-interface/src/files').ImportSource} */
13+
const source = (entry) //
14+
const result = await last(addAll(source, options))
15+
// Note this should never happen as `addAll` should yield at least one item
16+
// but to satisfy type checker we perfom this check and for good measure
17+
// throw an error in case it does happen.
18+
if (result == null) {
19+
throw Error('Failed to add a file, if you see this please report a bug')
20+
}
21+
1622
return result
1723
}
24+
1825
return add
1926
}
20-
21-
/**
22-
* @typedef {object} AddOptions
23-
* @property {string} [chunker] - chunking algorithm used to build ipfs DAGs (default: `'size-262144'`)
24-
* @property {number} [cidVersion] - the CID version to use when storing the data (default: `0`)
25-
* @property {string} [hashAlg] - multihash hashing algorithm to use (default: `'sha2-256'`)
26-
* @property {boolean} [onlyHash] - If true, will not add blocks to the blockstore (default: `false`)
27-
* @property {boolean} [pin] - pin this object when adding (default: `true`)
28-
* @property {(bytes:number, path:string) => void} [progress] - a function that will be called with the number of bytes added as a file is added to ipfs and the path of the file being added
29-
* @property {boolean} [rawLeaves] - if true, DAG leaves will contain raw file data and not be wrapped in a protobuf (default: `false`)
30-
* @property {boolean} [trickle] - if true will use the [trickle DAG](https://godoc.org/github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-unixfs/importer/trickle) format for DAG generation (default: `false`)
31-
* @property {boolean} [wrapWithDirectory] - Adds a wrapping node around the content (default: `false`)
32-
*
33-
* @typedef {Promise<UnixFSEntry>} AddResult
34-
*
35-
* @typedef {import('ipfs-core-utils/src/files/normalise-input/normalise-input').FileInput} Source
36-
*
37-
* @typedef {import('./add-all').UnixFSEntry} UnixFSEntry
38-
*
39-
* @typedef {import('../utils').AbortOptions} AbortOptions
40-
*/

packages/ipfs-core/src/components/cat.js

+1-20
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
1010
* @param {import('.').Preload} config.preload
1111
*/
1212
module.exports = function ({ ipld, preload }) {
13-
/**
14-
* Returns content of the file addressed by a valid IPFS Path or CID.
15-
*
16-
* @param {CID|string} ipfsPath - An IPFS path or CID to export
17-
* @param {Options} [options]
18-
* @returns {AsyncIterable<Uint8Array>}
19-
*/
13+
/** @type {import('ipfs-interface/src/root').Cat} */
2014
async function * cat (ipfsPath, options = {}) {
2115
ipfsPath = normalizeCidPath(ipfsPath)
2216

@@ -41,16 +35,3 @@ module.exports = function ({ ipld, preload }) {
4135

4236
return withTimeoutOption(cat)
4337
}
44-
45-
/**
46-
* @typedef {CatOptions & AbortOptions} Options
47-
*
48-
* @typedef {Object} CatOptions
49-
* @property {number} [offset] - An offset to start reading the file from
50-
* @property {number} [length] - An optional max length to read from the file
51-
* @property {boolean} [preload]
52-
*
53-
* @typedef {import('../utils').AbortOptions} AbortOptions
54-
*
55-
* @typedef {import('.').CID} CID
56-
*/

packages/ipfs-core/src/components/files/mkdir.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,16 @@ const addEmptyDir = async (context, childName, emptyDir, parent, trail, options)
143143
/**
144144
* @typedef {Object} MkdirOptions
145145
* @property {boolean} [parents=false] - If true, create intermediate directories
146-
* @property {number} [mode] - An integer that represents the file mode
147-
* @property {Mtime|Hrtime|Date} [mtime] - A Date object, an object with `{ secs, nsecs }` properties where secs is the number of seconds since (positive) or before (negative) the Unix Epoch began and nsecs is the number of nanoseconds since the last full second, or the output of `process.hrtime()
146+
* @property {ToMode} [mode] - An integer that represents the file mode
147+
* @property {ToMTime} [mtime] - A Date object, an object with `{ secs, nsecs }` properties where secs is the number of seconds since (positive) or before (negative) the Unix Epoch began and nsecs is the number of nanoseconds since the last full second, or the output of `process.hrtime()
148148
* @property {boolean} [flush] - If true the changes will be immediately flushed to disk
149149
* @property {string} [hashAlg='sha2-256'] - The hash algorithm to use for any updated entries
150-
* @property {0|1} [cidVersion=0] - The CID version to use for any updated entries
150+
* @property {CIDVersion} [cidVersion=0] - The CID version to use for any updated entries
151151
*
152152
* @typedef {import('cids')} CID
153-
* @typedef {import('../../utils').AbortOptions} AbortOptions
154-
* @typedef {import('../../utils').Mtime} Mtime
155-
* @typedef {import('../../utils').Hrtime} Hrtime
153+
* @typedef {import('cids').CIDVersion} CIDVersion
154+
* @typedef {import('ipfs-interface/src/basic').AbortOptions} AbortOptions
155+
* @typedef {import('ipfs-interface/src/files').MTime} Mtime
156+
* @typedef {import('ipfs-interface/src/files').ToMTime} ToMTime
157+
* @typedef {import('ipfs-interface/src/files').ToMode} ToMode
156158
*/

packages/ipfs-core/src/components/files/stat.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ const statters = {
180180
* @property {number} [sizeLocal] - An integer indicating the cumulative size of
181181
* the data present locally.
182182
* @property {number} [mode] - File mode
183-
* @property {import('../add-all').MTime} [mtime] - Modification time
183+
* @property {import('ipfs-interface/src/files').MTime} [mtime] - Modification time
184184
*
185-
* @typedef {import('..').CID} CID
186-
* @typedef {import('../../utils').AbortOptions} AbortOptions
185+
* @typedef {import('cids')} CID
186+
* @typedef {import('ipfs-interface/src/basic').AbortOptions} AbortOptions
187187
*/

packages/ipfs-core/src/components/files/touch.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const mh = require('multihashing-async').multihash
1515
const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
1616

1717
const defaultOptions = {
18-
/** @type {UnixTime|undefined} */
18+
/** @type {ToMTime|undefined} */
1919
mtime: undefined,
2020
flush: true,
2121
shardSplitThreshold: 1000,
@@ -121,14 +121,12 @@ module.exports = (context) => {
121121

122122
/**
123123
* @typedef {Object} TouchOptions
124-
* @property {UnixTime} [mtime] - A Date object, an object with `{ secs, nsecs }` properties where secs is the number of seconds since (positive) or before (negative) the Unix Epoch began and nsecs is the number of nanoseconds since the last full second, or the output of `process.hrtime()`
124+
* @property {ToMTime} [mtime] - A Date object, an object with `{ secs, nsecs }` properties where secs is the number of seconds since (positive) or before (negative) the Unix Epoch began and nsecs is the number of nanoseconds since the last full second, or the output of `process.hrtime()`
125125
* @property {boolean} [flush=false] - If true the changes will be immediately flushed to disk
126126
* @property {string} [hashAlg='sha2-256'] - The hash algorithm to use for any updated entries
127-
* @property {0|1} [cidVersion] - The CID version to use for any updated entries
127+
* @property {import('cids').CIDVersion} [cidVersion] - The CID version to use for any updated entries
128128
*
129129
* @typedef {import('cids')} CID
130-
* @typedef {import('../../utils').AbortOptions} AbortOptions
131-
* @typedef {import('../../utils').Mtime} Mtime
132-
* @typedef {import('../../utils').Hrtime} Hrtime
133-
* @typedef {import('ipfs-core-utils/src/files/normalise-input/normalise-input').UnixTime} UnixTime
130+
* @typedef {import('ipfs-interface/src/basic').AbortOptions} AbortOptions
131+
* @typedef {import('ipfs-interface/src/files').ToMTime} ToMTime
134132
*/

packages/ipfs-core/src/components/files/write.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,11 @@ const countBytesStreamed = async function * (source, notify) {
299299
* @property {boolean} [parents=false] - Create intermediate MFS paths if they do not exist
300300
* @property {boolean} [truncate=false] - Truncate the file at the MFS path if it would have been larger than the passed content
301301
* @property {boolean} [rawLeaves=false] - If true, DAG leaves will contain raw file data and not be wrapped in a protobuf
302-
* @property {number} [mode] - An integer that represents the file mode
303-
* @property {Mtime|Hrtime|Date} [mtime] - A Date object, an object with `{ secs, nsecs }` properties where secs is the number of seconds since (positive) or before (negative) the Unix Epoch began and nsecs is the number of nanoseconds since the last full second, or the output of `process.hrtime()
302+
* @property {import('ipfs-interface/src/files').ToMode} [mode] - An integer that represents the file mode
303+
* @property {import('ipfs-interface/src/files').ToMTime} [mtime] - A Date object, an object with `{ secs, nsecs }` properties where secs is the number of seconds since (positive) or before (negative) the Unix Epoch began and nsecs is the number of nanoseconds since the last full second, or the output of `process.hrtime()
304304
* @property {boolean} [flush] - If true the changes will be immediately flushed to disk
305305
* @property {string} [hashAlg='sha2-256'] - The hash algorithm to use for any updated entries
306306
* @property {0|1} [cidVersion=0] - The CID version to use for any updated entries
307307
*
308308
* @typedef {import('../../utils').AbortOptions} AbortOptions
309-
* @typedef {import('../../utils').Mtime} Mtime
310-
* @typedef {import('../../utils').Hrtime} Hrtime
311309
*/

0 commit comments

Comments
 (0)