Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { unpackToFs, unpackStreamToFs } from '../unpack/fs'
import {listFilesInCar, listCidsInCar, listRootsInCar, listFilesAndCidsInCar, hashCar} from './lib'
import {printUnixFsContent} from "./verbose-handler";

import type { CIDVersion } from 'multiformats/cid';

interface Flags {
output?: string,
pack?: string,
Expand All @@ -18,6 +20,7 @@ interface Flags {
listFull?: string
hash?: string
wrapWithDirectory?: boolean
cidVersion?: number
verbose?: boolean
}

Expand Down Expand Up @@ -61,6 +64,11 @@ const options = {
alias: 'w',
default: true
},
cidVersion: {
type: 'number',
alias: 'c',
default: 1
},
verbose: {
type: 'boolean',
alias: 'v',
Expand Down Expand Up @@ -88,6 +96,9 @@ const cli = meow(`
# pack files without wrapping with top-level directory
$ ipfs-car --wrapWithDirectory false --pack path/to/files --output path/to/write/a.car

# pack files with CIDv0
$ ipfs-car --cidVersion 0 --pack path/to/files --output path/to/write/a.car

# pack files and display which one is being packed
$ ipfs-car --pack /path/to/files --verbose

Expand Down Expand Up @@ -132,7 +143,11 @@ const cli = meow(`

async function handleInput ({ flags }: { flags: Flags }) {
if (flags.pack) {
const { root, filename } = await packToFs({input: flags.pack, output: flags.output, wrapWithDirectory: flags.wrapWithDirectory, customStreamSink: flags.verbose ? printUnixFsContent : undefined})
if (flags.cidVersion !== 0 && flags.cidVersion !== 1) {
throw new Error(`Invalid CID version: ${flags.cidVersion}`);
}

const { root, filename } = await packToFs({input: flags.pack, output: flags.output, wrapWithDirectory: flags.wrapWithDirectory, cidVersion: flags.cidVersion, customStreamSink: flags.verbose ? printUnixFsContent : undefined})
// tslint:disable-next-line: no-console
console.log(`root CID: ${root.toString()}`)
// tslint:disable-next-line: no-console
Expand Down
3 changes: 2 additions & 1 deletion src/pack/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface PackToFsProperties extends PackProperties {
output?: string
}

export async function packToFs ({ input, output, blockstore: userBlockstore, hasher, maxChunkSize, maxChildrenPerNode, wrapWithDirectory, rawLeaves, customStreamSink }: PackToFsProperties) {
export async function packToFs ({ input, output, blockstore: userBlockstore, hasher, maxChunkSize, maxChildrenPerNode, wrapWithDirectory, cidVersion, rawLeaves, customStreamSink }: PackToFsProperties) {
const blockstore = userBlockstore ? userBlockstore : new FsBlockStore()
const location = output || `${os.tmpdir()}/${(parseInt(String(Math.random() * 1e9), 10)).toString() + Date.now()}`
const writable = fs.createWriteStream(location)
Expand All @@ -25,6 +25,7 @@ export async function packToFs ({ input, output, blockstore: userBlockstore, has
maxChunkSize,
maxChildrenPerNode,
wrapWithDirectory,
cidVersion,
rawLeaves,
customStreamSink
})
Expand Down
19 changes: 17 additions & 2 deletions src/pack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getNormaliser } from './utils/normalise-input'
import type { ImportCandidateStream, ImportCandidate } from 'ipfs-core-types/src/utils'
import type { MultihashHasher } from 'multiformats/hashes/interface'
export type { ImportCandidateStream }
import type { CIDVersion } from 'multiformats/cid';

import { Blockstore } from '../blockstore'
import { MemoryBlockStore } from '../blockstore/memory'
Expand All @@ -15,6 +16,7 @@ import { unixfsImporterOptionsDefault } from './constants'
export interface PackProperties {
input: ImportCandidateStream | ImportCandidate,
blockstore?: Blockstore,
cidVersion?: CIDVersion,
maxChunkSize?: number,
maxChildrenPerNode?: number,
wrapWithDirectory?: boolean,
Expand All @@ -26,13 +28,25 @@ export interface PackProperties {
rawLeaves?: boolean
}

export async function pack ({ input, blockstore: userBlockstore, hasher, maxChunkSize, maxChildrenPerNode, wrapWithDirectory, rawLeaves }: PackProperties) {
export async function pack ({ input, blockstore: userBlockstore, hasher, maxChunkSize, maxChildrenPerNode, wrapWithDirectory, rawLeaves, cidVersion }: PackProperties) {
if (!input || (Array.isArray(input) && !input.length)) {
throw new Error('missing input file(s)')
}

const blockstore = userBlockstore ? userBlockstore : new MemoryBlockStore()

// Resolve the CID version
const resolvedCidVersion = cidVersion === undefined ? unixfsImporterOptionsDefault.cidVersion : cidVersion

// Resolve the raw leaves option
if (resolvedCidVersion === 0) {
if (rawLeaves === true) {
throw new Error('cannot use raw leaves with CIDv0, must use dag-pb encoding');
}

rawLeaves = false
}

// Consume the source
const rootEntry = await last(pipe(
getNormaliser(input),
Expand All @@ -42,7 +56,8 @@ export async function pack ({ input, blockstore: userBlockstore, hasher, maxChun
maxChunkSize: maxChunkSize || unixfsImporterOptionsDefault.maxChunkSize,
maxChildrenPerNode: maxChildrenPerNode || unixfsImporterOptionsDefault.maxChildrenPerNode,
wrapWithDirectory: wrapWithDirectory === false ? false : unixfsImporterOptionsDefault.wrapWithDirectory,
rawLeaves: rawLeaves == null ? unixfsImporterOptionsDefault.rawLeaves : rawLeaves
rawLeaves: rawLeaves == null ? unixfsImporterOptionsDefault.rawLeaves : rawLeaves,
cidVersion: resolvedCidVersion
})
))

Expand Down
15 changes: 14 additions & 1 deletion src/pack/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ export interface PackToStreamProperties extends PackProperties {
}

// Node version of toCar with Node Stream Writable
export async function packToStream ({ input, writable, blockstore: userBlockstore, hasher, maxChunkSize, maxChildrenPerNode, wrapWithDirectory, rawLeaves, customStreamSink }: PackToStreamProperties) {
export async function packToStream ({ input, writable, blockstore: userBlockstore, hasher, maxChunkSize, maxChildrenPerNode, wrapWithDirectory, cidVersion, rawLeaves, customStreamSink }: PackToStreamProperties) {
if (!input || (Array.isArray(input) && !input.length)) {
throw new Error('given input could not be parsed correctly')
}
input = typeof input === 'string' ? [input] : input

const blockstore = userBlockstore ? userBlockstore : new MemoryBlockStore()

// Resolve the CID version
const resolvedCidVersion = cidVersion === undefined ? unixfsImporterOptionsDefault.cidVersion : cidVersion

// Resolve the raw leaves option
if (resolvedCidVersion === 0) {
if (rawLeaves === true) {
throw new Error('cannot use raw leaves with CIDv0, must use dag-pb encoding');
}

rawLeaves = false
}

// Consume the source
const rootEntry = await last(pipe(
legacyGlobSource(input),
Expand All @@ -39,6 +51,7 @@ export async function packToStream ({ input, writable, blockstore: userBlockstor
maxChunkSize: maxChunkSize || unixfsImporterOptionsDefault.maxChunkSize,
maxChildrenPerNode: maxChildrenPerNode || unixfsImporterOptionsDefault.maxChildrenPerNode,
wrapWithDirectory: wrapWithDirectory === false ? false : unixfsImporterOptionsDefault.wrapWithDirectory,
cidVersion: resolvedCidVersion,
rawLeaves: rawLeaves == null ? unixfsImporterOptionsDefault.rawLeaves : rawLeaves
}),
customStreamSink ? customStreamSink : (sources: AsyncGenerator<ImportResult, void, unknown>) => sources
Expand Down
11 changes: 11 additions & 0 deletions test/pack/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ describe('pack', () => {
expect(() => root.toV0()).to.not.throw()
expect(root.toV0().toString()).to.eql('QmNUCKvjKRFeHZR2wyYM5cPEbEB969hz2zowTYvwGrQXP2')
})

it('can create a v0 CID', async () => {
const { root } = await pack({
input: [new Uint8Array([21, 31])],
blockstore: new Blockstore(),
wrapWithDirectory: false,
cidVersion: 0
})

expect(root.toString()).to.eql('QmNUCKvjKRFeHZR2wyYM5cPEbEB969hz2zowTYvwGrQXP2')
})
})
})
})