This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7941631
async crypto
dignifiedquire a957e1e
ready for next aegir
dignifiedquire 5f2ee96
docs: more badges
dignifiedquire dfa623c
update aegir
dignifiedquire 55f6a43
cr updates
dignifiedquire 4faacbd
ready
dignifiedquire 59ce8b6
chore: update dependencies
dignifiedquire 488d68c
feat: immutable block and cached keys
dignifiedquire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,5 +34,4 @@ node_modules | |
# Optional REPL history | ||
.node_repl_history | ||
|
||
lib | ||
dist |
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 |
---|---|---|
|
@@ -9,6 +9,11 @@ | |
[![Coverage Status](https://coveralls.io/repos/github/ipfs/js-ipfs-block/badge.svg?branch=master)](https://coveralls.io/github/ipfs/js-ipfs-block?branch=master) | ||
[![Dependency Status](https://david-dm.org/ipfs/js-ipfs-block.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-block) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square) | ||
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square) | ||
|
||
[![Sauce Test Status](https://saucelabs.com/browser-matrix/js-ipfs-block.svg)](https://saucelabs.com/u/js-ipf | ||
s-block) | ||
|
||
> [IPFS][ipfs] implementation of the Block data structure in JavaScript. | ||
|
||
|
@@ -25,10 +30,9 @@ | |
- [Browser: `<script>` Tag](#browser-script-tag) | ||
- [API](#api) | ||
- [Block](#block) | ||
- [`new Block(data, [type])`](#new-blockdata-type) | ||
- [`new Block(data)`](#new-blockdata) | ||
- [`block.data`](#blockdata) | ||
- [`block.key`](#blockkey) | ||
- [`block.extension`](#blockextension) | ||
- [`block.key([hashFn,] callback)`](#blockkeyhashfn-callback) | ||
- [Contribute](#contribute) | ||
- [License](#license) | ||
|
||
|
@@ -56,7 +60,7 @@ const Block = require('ipfs-block') | |
// create a block | ||
const block = new Block('hello world') | ||
console.log(block.data) | ||
console.log(block.key) | ||
block.key((err, key) => console.log(err, key)) | ||
``` | ||
|
||
### Browser: Browserify, Webpack, other bundlers | ||
|
@@ -67,7 +71,7 @@ it and use with your favourite bundler without having to adjust asset management | |
process. | ||
|
||
```js | ||
var Block = require('ipfs-block') | ||
const Block = require('ipfs-block') | ||
``` | ||
|
||
### Browser: `<script>` Tag | ||
|
@@ -89,26 +93,24 @@ const Block = require('ipfs-block') | |
|
||
### Block | ||
|
||
#### `new Block(data, [type])` | ||
#### `new Block(data)` | ||
|
||
- `data: Buffer|String` | ||
|
||
Creates a new block with raw data `data`. `type` can be either `'protobuf'` or `'ipld'` | ||
Creates a new block with raw data `data`. | ||
|
||
#### `block.data` | ||
|
||
The raw data of the block. Its format matches whatever was provided in its | ||
constructor. | ||
|
||
#### `block.key` | ||
|
||
The [multihash][multihash] of the block's data, as a buffer. | ||
|
||
#### `block.key([hashFn,] callback)` | ||
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.
|
||
|
||
### `block.extension` | ||
- `hashFn: String`, optional. Default `sha2-256`. | ||
- `callback: Function` | ||
|
||
The extension on how to store the blog, depends on the type: | ||
The callback will be called with the [multihash][multihash] of the block's data, as a buffer. | ||
|
||
- `'protobuf'`: `'data'` | ||
- `'ipld'`: `'ipld'` | ||
|
||
[ipfs]: https://ipfs.io | ||
[multihash]: https://github.com/jbenet/js-multihash | ||
|
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 |
---|---|---|
@@ -1,29 +1,39 @@ | ||
'use strict' | ||
|
||
const multihashing = require('multihashing') | ||
const multihashing = require('multihashing-async') | ||
|
||
function Block (data) { | ||
if (!data) { | ||
throw new Error('Block must be constructed with data') | ||
} | ||
module.exports = Block | ||
|
||
// Immutable block of data | ||
function Block (data) { | ||
if (!(this instanceof Block)) { | ||
return new Block(data) | ||
} | ||
|
||
if (data instanceof Buffer) { | ||
this.data = data | ||
} else { | ||
this.data = new Buffer(data) | ||
if (!data) { | ||
throw new Error('Block must be constructed with data') | ||
} | ||
|
||
this.key = (hashFunc) => { | ||
this.data = ensureBuffer(data) | ||
|
||
this.key = (hashFunc, callback) => { | ||
if (typeof hashFunc === 'function') { | ||
callback = hashFunc | ||
hashFunc = null | ||
} | ||
|
||
if (!hashFunc) { | ||
hashFunc = 'sha2-256' | ||
} | ||
|
||
return multihashing(this.data, hashFunc) | ||
multihashing(this.data, hashFunc, callback) | ||
} | ||
} | ||
|
||
module.exports = Block | ||
function ensureBuffer (data) { | ||
if (Buffer.isBuffer(data)) { | ||
return data | ||
} | ||
|
||
return new Buffer(data) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
s/hashFn/hashAlg