-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add typeScript support #3236
Changes from 9 commits
6fccc0b
ac3ee3f
085a5cd
46c7316
27d618e
9bf714b
9e1a6e4
5fd264c
e0f40d1
bef93e2
df5f618
b56d8f7
48e5251
040a82b
2885568
14f7dbb
09d0bbb
f62cef7
2442d96
0d4029c
e9342d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,64 @@ | |
const importer = require('ipfs-unixfs-importer') | ||
const normaliseAddInput = require('ipfs-core-utils/src/files/normalise-input') | ||
const { parseChunkerString } = require('./utils') | ||
const pipe = require('it-pipe') | ||
const { pipe } = require('it-pipe') | ||
const { withTimeoutOption } = require('../../utils') | ||
|
||
/** | ||
* @typedef {Uint8Array | Blob | String | Iterable<Uint8Array|Number> | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>} FileContent | ||
Gozala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @typedef {object} FileObject | ||
* - If no path is specified, then the item will be added to the root level and will be given a name according to it's CID. | ||
* - If no content is passed, then the item is treated as an empty directory. | ||
* - One of path or content must be passed. | ||
* @property {string} [path] - The path you want to the file to be accessible at from the root CID _after_ it has been added | ||
* @property {FileContent} [content] - The contents of the file | ||
* @property {number | string} [mode] - File mode to store the entry with (see https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation) | ||
* @property {UnixTime} [mtime] - The modification time of the entry | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 If you want to be more precise (not asking you to, I have taken same shortcuts myself) type FileObject =
| { path: string, mode?: Mode, mtime?: UnixTime } // Directory
| { path?: string, content: FileContent, mtime?: UnixTime } // File That is to say it must have path or a content or both. |
||
* | ||
* @typedef {FileContent | FileObject} Source | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💭 I think it would be better to call this something like |
||
* @typedef {Iterable<Source> | AsyncIterable<Source> | ReadableStream<Source>} FileStream | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @achingbrain now that I'm looking at it, I find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'm all for making it more straightforward. Input streams are largely called |
||
* | ||
* @typedef {Date | UnixTimeObj | [number, number]} UnixTime - As an array of numbers, it must have two elements, as per the output of [`process.hrtime()`](https://nodejs.org/dist/latest/docs/api/process.html#process_process_hrtime_time). | ||
* | ||
* @typedef {object} UnixTimeObj | ||
* @property {number} secs - the number of seconds since (positive) or before (negative) the Unix Epoch began | ||
* @property {number} [nsecs] - the number of nanoseconds since the last full second. | ||
* | ||
* @typedef {object} UnixFSEntry | ||
* @property {string} path | ||
* @property {import('cids')} cid | ||
* @property {number} mode | ||
* @property {UnixTimeObj} mtime | ||
* @property {number} size | ||
*/ | ||
|
||
module.exports = ({ block, gcLock, preload, pin, options: constructorOptions }) => { | ||
const isShardingEnabled = constructorOptions.EXPERIMENTAL && constructorOptions.EXPERIMENTAL.sharding | ||
|
||
return withTimeoutOption(async function * addAll (source, options) { | ||
/** | ||
* Import multiple files and data into IPFS. | ||
* | ||
* @param {FileStream} source | ||
* | ||
* @param {object} [options] | ||
* @param {string} [options.chunker] - chunking algorithm used to build ipfs DAGs (default: `'size-262144'`) | ||
* @param {Number} [options.cidVersion] - the CID version to use when storing the data (default: `0`) | ||
* @param {boolean} [options.enableShardingExperiment] - allows to create directories with an unlimited number of entries currently size of unixfs directories is limited by the maximum block size. Note that this is an experimental feature (default: `false`) | ||
* @param {String} [options.hashAlg] - multihash hashing algorithm to use (default: `'sha2-256'`) | ||
* @param {boolean} [options.onlyHash] - If true, will not add blocks to the blockstore (default: `false`) | ||
* @param {boolean} [options.pin] - pin this object when adding (default: `true`) | ||
* @param {function} [options.progress] - a function that will be called with the byte length of chunks as a file is added to ipfs (default: `undefined`) | ||
* @param {boolean} [options.rawLeaves] - if true, DAG leaves will contain raw file data and not be wrapped in a protobuf (default: `false`) | ||
* @param {Number} [options.shardSplitThreshold] - Directories with more than this number of files will be created as HAMT-sharded directories (default: `1000`) | ||
* @param {boolean} [options.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`) | ||
* @param {boolean} [options.wrapWithDirectory] - Adds a wrapping node around the content (default: `false`) | ||
* @param {Number} [options.timeout] - A timeout in ms (default: `undefined`) | ||
* @param {AbortSignal} [options.signal] - Can be used to cancel any long running requests started as a result of this call (default: `undefined`) | ||
|
||
* @returns {AsyncIterable<UnixFSEntry>} | ||
*/ | ||
async function * addAll (source, options) { | ||
options = options || {} | ||
|
||
const opts = { | ||
|
@@ -58,7 +109,9 @@ module.exports = ({ block, gcLock, preload, pin, options: constructorOptions }) | |
} finally { | ||
releaseLock() | ||
} | ||
}) | ||
} | ||
|
||
return withTimeoutOption(addAll) | ||
} | ||
|
||
function transformFile (opts) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict' | ||
|
||
const Big = require('bignumber.js') | ||
const Big = require('bignumber.js').default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change necessary ? From their website this is how they suggest to import
So I would much rather keep it as it was, unless absolutely necessary. Otherwise it's fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make the typings work, the export signature in its export default BigNumber;
export declare class BigNumber ... is either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think when namespace and class name do collide in definitions TS does know to not require default, but I could be wrong and it might be behind some config flag. I think I'd still prefer |
||
const CID = require('cids') | ||
const { withTimeoutOption } = require('../../utils') | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why to note add
@return
type here and do it on line 18 instead ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eslint valid-jsdoc requires an
@return
on line 18, and the default return type for@callback
definition isany