Files: 42ee59ff392dd3ff9454e74b2e3a281ebd9310e6 / datastore.js
986 bytesRaw
1 | const Buffer = require('safe-buffer').Buffer |
2 | const crypto = require('node-webcrypto-shim') |
3 | const DAG = require('ipld-graph-builder/datastore.js') |
4 | const treeNode = require('./treeNode.js') |
5 | const HASH_LEN = 20 |
6 | |
7 | module.exports = class TreeDAG extends DAG { |
8 | async put (val) { |
9 | const encoded = treeNode.encode(val) |
10 | let key = await crypto.subtle.digest({ |
11 | name: 'SHA-256' |
12 | }, encoded) |
13 | |
14 | key = Buffer.from(key.slice(0, HASH_LEN)) |
15 | |
16 | return new Promise((resolve, reject) => { |
17 | this._dag.put(key, encoded.toString('hex'), () => { |
18 | resolve(key) |
19 | }) |
20 | }) |
21 | } |
22 | |
23 | get (link) { |
24 | return new Promise((resolve, reject) => { |
25 | this._dag.get(link, (err, val) => { |
26 | if (err) { |
27 | reject(err) |
28 | } else { |
29 | val = Buffer.from(val, 'hex') |
30 | const decoded = treeNode.decode(val) |
31 | resolve(decoded) |
32 | } |
33 | }) |
34 | }) |
35 | } |
36 | |
37 | isValidLink (link) { |
38 | return Buffer.isBuffer(link) && link.length === HASH_LEN |
39 | } |
40 | } |
41 |
Built with git-ssb-web