ipfs-unixfs-importer
Advanced tools
Comparing version
@@ -6,2 +6,10 @@ # Change Log | ||
## [7.0.1](https://github.com/ipfs/js-ipfs-unixfs/compare/ipfs-unixfs-importer@7.0.0...ipfs-unixfs-importer@7.0.1) (2021-03-19) | ||
**Note:** Version bump only for package ipfs-unixfs-importer | ||
# [7.0.0](https://github.com/ipfs/js-ipfs-unixfs/compare/ipfs-unixfs-importer@6.0.1...ipfs-unixfs-importer@7.0.0) (2021-03-15) | ||
@@ -8,0 +16,0 @@ |
@@ -13,3 +13,3 @@ export = dagBuilder; | ||
type Chunker = (source: AsyncIterable<Uint8Array>, options: import("../types").ImporterOptions) => AsyncIterable<Uint8Array>; | ||
type ChunkValidator = (source: AsyncIterable<string | Uint8Array | ArrayLike<number>>, options: import("../types").ImporterOptions) => AsyncIterable<Uint8Array>; | ||
type ChunkValidator = (source: AsyncIterable<Uint8Array>, options: import("../types").ImporterOptions) => AsyncIterable<Uint8Array>; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -8,7 +8,7 @@ export = validateChunks; | ||
*/ | ||
declare function validateChunks(source: AsyncIterable<string | Uint8Array | ArrayLike<number>>): AsyncIterable<Uint8Array>; | ||
declare function validateChunks(source: AsyncIterable<Uint8Array>): AsyncIterable<Uint8Array>; | ||
declare namespace validateChunks { | ||
export { ChunkValidator }; | ||
} | ||
type ChunkValidator = (source: AsyncIterable<string | Uint8Array | ArrayLike<number>>, options: import("../types").ImporterOptions) => AsyncIterable<Uint8Array>; | ||
type ChunkValidator = (source: AsyncIterable<Uint8Array>, options: import("../types").ImporterOptions) => AsyncIterable<Uint8Array>; | ||
//# sourceMappingURL=validate-chunks.d.ts.map |
@@ -38,3 +38,3 @@ export = DirFlat; | ||
mode?: number | undefined; | ||
mtime?: import("ipfs-unixfs").Mtime | undefined; | ||
mtime?: import("ipfs-unixfs/dist/src/types").Mtime | undefined; | ||
}; | ||
@@ -41,0 +41,0 @@ type ImporterOptions = import("./types").ImporterOptions; |
@@ -39,3 +39,3 @@ export = DirSharded; | ||
mode?: number | undefined; | ||
mtime?: import("ipfs-unixfs").Mtime | undefined; | ||
mtime?: import("ipfs-unixfs/dist/src/types").Mtime | undefined; | ||
}; | ||
@@ -42,0 +42,0 @@ type ImporterOptions = import("./types").ImporterOptions; |
@@ -37,3 +37,3 @@ export = Dir; | ||
mode: number | undefined; | ||
mtime: import("ipfs-unixfs").Mtime | undefined; | ||
mtime: import("ipfs-unixfs/dist/src/types").Mtime | undefined; | ||
/** @type {CID | undefined} */ | ||
@@ -83,5 +83,5 @@ cid: CID | undefined; | ||
mode?: number | undefined; | ||
mtime?: import("ipfs-unixfs").Mtime | undefined; | ||
mtime?: import("ipfs-unixfs/dist/src/types").Mtime | undefined; | ||
}; | ||
type ImporterOptions = import("./types").ImporterOptions; | ||
//# sourceMappingURL=dir.d.ts.map |
@@ -8,2 +8,9 @@ export type BlockAPI = import("./types").BlockAPI; | ||
export type ImportResult = import("./types").ImportResult; | ||
export type Chunker = (source: AsyncIterable<Uint8Array>, options: import("./types").ImporterOptions) => AsyncIterable<Uint8Array>; | ||
export type DAGBuilder = (source: AsyncIterable<import("./types").ImportCandidate> | Iterable<import("./types").ImportCandidate>, block: import("./types").BlockAPI, options: import("./types").ImporterOptions) => AsyncIterable<() => Promise<import("./types").InProgressImportResult>>; | ||
export type TreeBuilder = (source: AsyncIterable<import("./types").InProgressImportResult>, block: import("./types").BlockAPI, options: import("./types").ImporterOptions) => AsyncIterable<import("./types").ImportResult>; | ||
export type BufferImporter = (file: import("./types").File, block: import("./types").BlockAPI, options: import("./types").ImporterOptions) => AsyncIterable<() => Promise<import("./types").InProgressImportResult>>; | ||
export type ChunkValidator = (source: AsyncIterable<Uint8Array>, options: import("./types").ImporterOptions) => AsyncIterable<Uint8Array>; | ||
export type Reducer = (leaves: import("./types").InProgressImportResult[]) => Promise<import("./types").InProgressImportResult>; | ||
export type ProgressHandler = (chunkSize: number, path?: string | undefined) => void; | ||
/** | ||
@@ -17,9 +24,17 @@ * @typedef {import('./types').BlockAPI} BlockAPI | ||
* @typedef {import('./types').ImportResult} ImportResult | ||
* | ||
* @typedef {import('./types').Chunker} Chunker | ||
* @typedef {import('./types').DAGBuilder} DAGBuilder | ||
* @typedef {import('./types').TreeBuilder} TreeBuilder | ||
* @typedef {import('./types').BufferImporter} BufferImporter | ||
* @typedef {import('./types').ChunkValidator} ChunkValidator | ||
* @typedef {import('./types').Reducer} Reducer | ||
* @typedef {import('./types').ProgressHandler} ProgressHandler | ||
*/ | ||
/** | ||
* @param {AsyncIterable<ImportCandidate> | Iterable<ImportCandidate>} source | ||
* @param {AsyncIterable<ImportCandidate> | Iterable<ImportCandidate> | ImportCandidate} source | ||
* @param {BlockAPI} block | ||
* @param {UserImporterOptions} options | ||
*/ | ||
export function importer(source: AsyncIterable<ImportCandidate> | Iterable<ImportCandidate>, block: BlockAPI, options?: UserImporterOptions): AsyncGenerator<{ | ||
export function importer(source: AsyncIterable<ImportCandidate> | Iterable<ImportCandidate> | ImportCandidate, block: BlockAPI, options?: UserImporterOptions): AsyncGenerator<{ | ||
cid: import("cids"); | ||
@@ -26,0 +41,0 @@ path: string | undefined; |
@@ -1,2 +0,2 @@ | ||
import { UnixFS, Mtime, MtimeLike } from 'ipfs-unixfs' | ||
import { UnixFS, Mtime } from 'ipfs-unixfs' | ||
import CID, { CIDVersion } from 'cids' | ||
@@ -9,4 +9,4 @@ import { HashName } from 'multihashes' | ||
path?: string | ||
content?: AsyncIterable<Uint8Array> | Iterable<Uint8Array> | Uint8Array | ArrayLike<number> | string | ||
mtime?: MtimeLike | ||
content?: AsyncIterable<Uint8Array> | ||
mtime?: Mtime | ||
mode?: number | ||
@@ -18,3 +18,3 @@ } | ||
path?: string | ||
mtime?: MtimeLike | ||
mtime?: Mtime | ||
mode?: number | ||
@@ -25,3 +25,3 @@ } | ||
path?: string | ||
mtime?: MtimeLike | ||
mtime?: Mtime | ||
mode?: number | ||
@@ -48,3 +48,3 @@ } | ||
type BufferImporter = (file: File, block: BlockAPI, options: ImporterOptions) => AsyncIterable<() => Promise<InProgressImportResult>> | ||
type ChunkValidator = (source: AsyncIterable<Uint8Array | string | ArrayLike<number>>, options: ImporterOptions) => AsyncIterable<Uint8Array> | ||
type ChunkValidator = (source: AsyncIterable<Uint8Array>, options: ImporterOptions) => AsyncIterable<Uint8Array> | ||
type UnixFSV1DagBuilder<T> = (item: T, block: BlockAPI, options: ImporterOptions) => Promise<InProgressImportResult> | ||
@@ -51,0 +51,0 @@ type Reducer = (leaves: InProgressImportResult[]) => Promise<InProgressImportResult> |
{ | ||
"name": "ipfs-unixfs-importer", | ||
"version": "7.0.0", | ||
"version": "7.0.1", | ||
"description": "JavaScript implementation of the UnixFs importer used by IPFS", | ||
@@ -31,3 +31,3 @@ "leadMaintainer": "Alex Potsides <alex.potsides@protocol.ai>", | ||
"engines": { | ||
"node": ">=15.0.0", | ||
"node": ">=14.0.0", | ||
"npm": ">=7.0.0" | ||
@@ -38,3 +38,3 @@ }, | ||
"@types/mocha": "^8.2.1", | ||
"aegir": "^32.0.0", | ||
"aegir": "^32.1.0", | ||
"assert": "^2.0.0", | ||
@@ -59,3 +59,3 @@ "copy": "^0.3.2", | ||
"hamt-sharding": "^2.0.0", | ||
"ipfs-unixfs": "^4.0.0", | ||
"ipfs-unixfs": "^4.0.1", | ||
"ipld-dag-pb": "^0.21.0", | ||
@@ -79,3 +79,3 @@ "it-all": "^1.0.5", | ||
}, | ||
"gitHead": "172548bf5f5ecf2c6fd6f410be506ccd72804d28" | ||
"gitHead": "5ccac2f40bc582d594ac8ec761696df78d1fd351" | ||
} |
@@ -16,20 +16,42 @@ 'use strict' | ||
/** | ||
* @param {any} item | ||
* @returns {item is ArrayLike<number>} | ||
* @param {any} thing | ||
* @returns {thing is Iterable<any>} | ||
*/ | ||
function isArrayLike (item) { | ||
return ( | ||
Array.isArray(item) || | ||
(Boolean(item) && | ||
typeof item === 'object' && | ||
typeof (item.length) === 'number' && | ||
(item.length === 0 || | ||
(item.length > 0 && | ||
(item.length - 1) in item) | ||
) | ||
) | ||
) | ||
function isIterable (thing) { | ||
return Symbol.iterator in thing | ||
} | ||
/** | ||
* @param {any} thing | ||
* @returns {thing is AsyncIterable<any>} | ||
*/ | ||
function isAsyncIterable (thing) { | ||
return Symbol.asyncIterator in thing | ||
} | ||
/** | ||
* @param {Uint8Array | AsyncIterable<Uint8Array> | Iterable<Uint8Array>} content | ||
* @returns {AsyncIterable<Uint8Array>} | ||
*/ | ||
function contentAsAsyncIterable (content) { | ||
try { | ||
if (content instanceof Uint8Array) { | ||
return (async function * () { | ||
yield content | ||
}()) | ||
} else if (isIterable(content)) { | ||
return (async function * () { | ||
yield * content | ||
}()) | ||
} else if (isAsyncIterable(content)) { | ||
return content | ||
} | ||
} catch { | ||
throw errCode(new Error('Content was invalid'), 'ERR_INVALID_CONTENT') | ||
} | ||
throw errCode(new Error('Content was invalid'), 'ERR_INVALID_CONTENT') | ||
} | ||
/** | ||
* @type {DAGBuilder} | ||
@@ -51,17 +73,2 @@ */ | ||
if (entry.content) { | ||
const source = entry.content | ||
/** @type {AsyncIterable<string | Uint8Array | ArrayLike<number>>} */ | ||
const content = (async function * () { | ||
// wrap in iterator if it is a, string, Uint8Array or array-like | ||
if (typeof source === 'string' || isArrayLike(source)) { | ||
yield source | ||
// @ts-ignore | ||
} else if (source[Symbol.asyncIterator] || source[Symbol.iterator]) { | ||
yield * source | ||
} else { | ||
throw errCode(new Error('Content was invalid'), 'ERR_INVALID_CONTENT') | ||
} | ||
}()) | ||
/** | ||
@@ -96,3 +103,3 @@ * @type {Chunker} | ||
mode: entry.mode, | ||
content: chunker(chunkValidator(content, options), options) | ||
content: chunker(chunkValidator(contentAsAsyncIterable(entry.content), options), options) | ||
} | ||
@@ -99,0 +106,0 @@ |
@@ -14,6 +14,14 @@ 'use strict' | ||
* @typedef {import('./types').ImportResult} ImportResult | ||
* | ||
* @typedef {import('./types').Chunker} Chunker | ||
* @typedef {import('./types').DAGBuilder} DAGBuilder | ||
* @typedef {import('./types').TreeBuilder} TreeBuilder | ||
* @typedef {import('./types').BufferImporter} BufferImporter | ||
* @typedef {import('./types').ChunkValidator} ChunkValidator | ||
* @typedef {import('./types').Reducer} Reducer | ||
* @typedef {import('./types').ProgressHandler} ProgressHandler | ||
*/ | ||
/** | ||
* @param {AsyncIterable<ImportCandidate> | Iterable<ImportCandidate>} source | ||
* @param {AsyncIterable<ImportCandidate> | Iterable<ImportCandidate> | ImportCandidate} source | ||
* @param {BlockAPI} block | ||
@@ -41,3 +49,14 @@ * @param {UserImporterOptions} options | ||
for await (const entry of treeBuilder(parallelBatch(dagBuilder(source, block, opts), opts.fileImportConcurrency), block, opts)) { | ||
/** @type {AsyncIterable<ImportCandidate> | Iterable<ImportCandidate>} */ | ||
let candidates | ||
if (Symbol.asyncIterator in source || Symbol.iterator in source) { | ||
// @ts-ignore | ||
candidates = source | ||
} else { | ||
// @ts-ignore | ||
candidates = [source] | ||
} | ||
for await (const entry of treeBuilder(parallelBatch(dagBuilder(candidates, block, opts), opts.fileImportConcurrency), block, opts)) { | ||
yield { | ||
@@ -44,0 +63,0 @@ cid: entry.cid, |
@@ -1,2 +0,2 @@ | ||
import { UnixFS, Mtime, MtimeLike } from 'ipfs-unixfs' | ||
import { UnixFS, Mtime } from 'ipfs-unixfs' | ||
import CID, { CIDVersion } from 'cids' | ||
@@ -9,4 +9,4 @@ import { HashName } from 'multihashes' | ||
path?: string | ||
content?: AsyncIterable<Uint8Array> | Iterable<Uint8Array> | Uint8Array | ArrayLike<number> | string | ||
mtime?: MtimeLike | ||
content?: AsyncIterable<Uint8Array> | ||
mtime?: Mtime | ||
mode?: number | ||
@@ -18,3 +18,3 @@ } | ||
path?: string | ||
mtime?: MtimeLike | ||
mtime?: Mtime | ||
mode?: number | ||
@@ -25,3 +25,3 @@ } | ||
path?: string | ||
mtime?: MtimeLike | ||
mtime?: Mtime | ||
mode?: number | ||
@@ -48,3 +48,3 @@ } | ||
type BufferImporter = (file: File, block: BlockAPI, options: ImporterOptions) => AsyncIterable<() => Promise<InProgressImportResult>> | ||
type ChunkValidator = (source: AsyncIterable<Uint8Array | string | ArrayLike<number>>, options: ImporterOptions) => AsyncIterable<Uint8Array> | ||
type ChunkValidator = (source: AsyncIterable<Uint8Array>, options: ImporterOptions) => AsyncIterable<Uint8Array> | ||
type UnixFSV1DagBuilder<T> = (item: T, block: BlockAPI, options: ImporterOptions) => Promise<InProgressImportResult> | ||
@@ -51,0 +51,0 @@ type Reducer = (leaves: InProgressImportResult[]) => Promise<InProgressImportResult> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
115305
2.04%1976
2.01%Updated