This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: build & export interop with go-ipfs for small file raw leaves
For files smaller than max chunk size and raw leaves true, go-ipfs will create a single node that is a raw buffer. Prior to this PR, js-ipfs created a unixfs file node who's data was the raw buffer. This resulted in different hashes for the same file. This PR changes the builder to do the same thing as go-ipfs and adds a resolver to the exporter that allows the exporter to export a node that is a single raw buffer (so that you can `ipfs cat [CID w codec raw]` as you can in go-ipfs). License: MIT Signed-off-by: Alan Shaw <[email protected]>
- Loading branch information
Showing
12 changed files
with
164 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
|
||
module.exports = function extractDataFromBlock (block, streamPosition, begin, end) { | ||
const blockLength = block.length | ||
|
||
if (begin >= streamPosition + blockLength) { | ||
// If begin is after the start of the block, return an empty block | ||
// This can happen when internal nodes contain data | ||
return Buffer.alloc(0) | ||
} | ||
|
||
if (end - streamPosition < blockLength) { | ||
// If the end byte is in the current block, truncate the block to the end byte | ||
block = block.slice(0, end - streamPosition) | ||
} | ||
|
||
if (begin > streamPosition && begin < (streamPosition + blockLength)) { | ||
// If the start byte is in the current block, skip to the start byte | ||
block = block.slice(begin - streamPosition) | ||
} | ||
|
||
return block | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
const pull = require('pull-stream') | ||
const extractDataFromBlock = require('./extract-data-from-block') | ||
|
||
// Logic to export a single raw block | ||
module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, depth, offset, length) => { | ||
const accepts = pathRest[0] | ||
|
||
if (accepts !== undefined && accepts !== path) { | ||
return pull.empty() | ||
} | ||
|
||
size = size || node.length | ||
|
||
if (offset < 0) { | ||
return pull.error(new Error('Offset must be greater than 0')) | ||
} | ||
|
||
if (offset > size) { | ||
return pull.error(new Error('Offset must be less than the file size')) | ||
} | ||
|
||
if (length < 0) { | ||
return pull.error(new Error('Length must be greater than or equal to 0')) | ||
} | ||
|
||
if (length === 0) { | ||
return pull.once({ | ||
depth, | ||
content: pull.once(Buffer.alloc(0)), | ||
hash: cid, | ||
name, | ||
path, | ||
size, | ||
type: 'raw' | ||
}) | ||
} | ||
|
||
if (!offset) { | ||
offset = 0 | ||
} | ||
|
||
if (!length || (offset + length > size)) { | ||
length = size - offset | ||
} | ||
|
||
return pull.once({ | ||
depth, | ||
content: pull.once(extractDataFromBlock(node, 0, offset, offset + length)), | ||
hash: cid, | ||
name, | ||
path, | ||
size, | ||
type: 'raw' | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.