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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
99 additions
and
86 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 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 |
---|---|---|
@@ -1,37 +1,39 @@ | ||
'use strict' | ||
const util = require('./util') | ||
|
||
// Immutable block of data | ||
function Block (data, type) { | ||
if (!data) { | ||
throw new Error('Block must be constructed with data') | ||
} | ||
const multihashing = require('multihashing-async') | ||
|
||
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 = util.hash(this.data) | ||
this.type = type || 'protobuf' | ||
} | ||
this.data = ensureBuffer(data) | ||
|
||
this.key = (hashFunc, callback) => { | ||
if (typeof hashFunc === 'function') { | ||
callback = hashFunc | ||
hashFunc = null | ||
} | ||
|
||
Object.defineProperty(Block.prototype, 'extension', { | ||
get () { | ||
switch (this.type) { | ||
case 'protobuf': | ||
return 'data' | ||
case 'ipld': | ||
return 'ipld' | ||
default: | ||
return this.type | ||
if (!hashFunc) { | ||
hashFunc = 'sha2-256' | ||
} | ||
|
||
multihashing(this.data, hashFunc, callback) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = Block | ||
function ensureBuffer (data) { | ||
if (Buffer.isBuffer(data)) { | ||
return data | ||
} | ||
|
||
return new Buffer(data) | ||
} |
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