|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const createNode = require('./create-node') |
| 4 | +const path = require('path') |
| 5 | +const { CID } = require('multiformats/cid') |
| 6 | +const MultihashDigest = require('multiformats/hashes/digest') |
| 7 | +const fs = require('fs').promises |
| 8 | +const uint8ArrayToString = require('uint8arrays/to-string') |
| 9 | +const { convert } = require('ipld-format-to-blockcodec') |
| 10 | +const crypto = require('crypto') |
| 11 | + |
| 12 | +async function main () { |
| 13 | + const ipfs = await createNode({ |
| 14 | + ipld: { |
| 15 | + codecs: [ |
| 16 | + convert(require('ipld-git')) |
| 17 | + ], |
| 18 | + hashers: [{ |
| 19 | + name: 'sha1', |
| 20 | + code: 0x11, |
| 21 | + digest: async (buf) => { |
| 22 | + return MultihashDigest.create(0x11, crypto.createHash('sha1').update(buf).digest()) |
| 23 | + } |
| 24 | + }] |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + console.log('\nStart of the example:') |
| 29 | + |
| 30 | + const gitObjects = [ |
| 31 | + path.join(__dirname, '/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593'), |
| 32 | + path.join(__dirname, '/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe'), |
| 33 | + path.join(__dirname, '/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a'), |
| 34 | + path.join(__dirname, '/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249'), |
| 35 | + path.join(__dirname, '/git-objects/6850c7be7136e6be00976ddbae80671b945c3e9d'), |
| 36 | + path.join(__dirname, '/git-objects/a5095353cd62a178663dd26efc2d61f4f61bccbe'), |
| 37 | + path.join(__dirname, '/git-objects/dc9bd15e8b81b6565d3736f9c308bd1bba60f33a'), |
| 38 | + path.join(__dirname, '/git-objects/e68e6f6e31857877a79fd6b3956898436bb5a76f'), |
| 39 | + path.join(__dirname, '/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c'), |
| 40 | + path.join(__dirname, '/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21') |
| 41 | + ] |
| 42 | + |
| 43 | + await Promise.all(gitObjects.map(async gitObjectsPath => { |
| 44 | + const data = await fs.readFile(gitObjectsPath) |
| 45 | + |
| 46 | + const cid = await ipfs.block.put(data, { |
| 47 | + format: 'git-raw', |
| 48 | + mhtype: 'sha1', |
| 49 | + version: 1 |
| 50 | + }) |
| 51 | + |
| 52 | + console.log(cid.toString()) |
| 53 | + })) |
| 54 | + |
| 55 | + const v1tag = CID.parse('z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue') |
| 56 | + |
| 57 | + async function logResult (fn, comment) { |
| 58 | + const result = await fn() |
| 59 | + |
| 60 | + if (result.value instanceof Uint8Array) { // Blobs (files) are returned as buffer instance |
| 61 | + result.value = uint8ArrayToString(result.value) |
| 62 | + } |
| 63 | + |
| 64 | + console.log('-'.repeat(80)) |
| 65 | + console.log(comment) |
| 66 | + console.log(result.value) |
| 67 | + } |
| 68 | + |
| 69 | + await logResult(() => ipfs.dag.get(v1tag), 'Tag object:') |
| 70 | + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/message' }), 'Tagged commit message:') |
| 71 | + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/message' }), 'Parent of tagged commit:') |
| 72 | + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/tree/src/hash/hello/hash' }), '/src/hello file:') |
| 73 | + await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/tree/src/hash/hello/hash' }), 'previous version of /src/hello file:') |
| 74 | + |
| 75 | + await ipfs.stop() |
| 76 | +} |
| 77 | + |
| 78 | +main() |
| 79 | + .catch(err => { |
| 80 | + console.error(err) |
| 81 | + process.exit(1) |
| 82 | + }) |
0 commit comments