Skip to content

Commit cd497c6

Browse files
committed
readlink should return metadata
1 parent d8e2921 commit cd497c6

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ var drive = hyperdrive(memdb())
1515
var archive = drive.createArchive()
1616

1717
ln.link(archive, 'linkfile', <ARCHIVE KEY>, [meta], err => {}) // create symlink to another archive
18-
ln.readlink(archive, 'linkfile', (err, info) => {}) // get linked archive key
18+
ln.readlink(archive, 'linkfile', (err, link) => {}) // get link data
1919

2020
// assume ln.link(archive, 'path/to/file', <ARCHIVE KEY>)
21-
ln.resolve(archive, 'path/to/file/within/linked/archive', (err, archiveKey, restOfThePath)) // returns (err, <ARCHIVE KEY>, 'within/linked/archive')
21+
ln.resolve(archive, 'path/to/file/within/linked/archive', (err, link, restOfThePath)) // returns (err, {link: <ARCHIVE_KEY>, meta: {...}}, 'within/linked/archive')
2222

2323
// resolve through archives
2424
ln.deepResolve(drive, swarmer, archive, path, (err, result) => {})
@@ -37,21 +37,21 @@ You can pass a `meta` object to store it in the symlink.
3737

3838
#### `ln.readlink(archive, path, cb)`
3939

40-
Get the archiveKey stored inside a symlink
40+
Get the link data stored inside a symlink.
4141

4242
#### `ln.resolve(archive, path, cb)`
4343

4444
Resolve a path. Returns an archive and a path within that archive with `cb(err, linkedArchiveKey, pathWithinLinkedArchive)`
4545

46-
* If there's a symlink encountered in the path. `cb(err, linkKey, pathWithinLinkedArchive)` will be invoked.
47-
* If there's no symlink in the path, `cb(err, archive.key, path)` will be called.
46+
* If there's a symlink encountered in the path. `cb(err, link, pathWithinLinkedArchive)` will be invoked.
47+
* If there's no symlink in the path, `cb(err, {}, path)` will be called.
4848

4949
for example:
5050

5151
```js
5252
ln.link(archive, 'foo/bar', '<LINK_KEY>', (err) => {
5353
ln.resolve(archive, 'foo/bar/baz', (err, link, path) => {
54-
// link === '<LINK_KEY>'
54+
// link === {link: '<LINK_KEY>', meta: {...}}
5555
// path === 'baz'
5656
})
5757
})

index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function resolve (archive, path, cb) {
4545
if (err && err.message === 'not a link') {
4646
if (i === components.length - 1) {
4747
// found the file
48-
return cb(null, archive.key.toString('hex'), partialPath.join('/'))
48+
return cb(null, {}, partialPath.join('/'))
4949
}
5050
return cb(new Error(`unresolvable at ${partialPath.join('/')}`))
5151
}
@@ -75,15 +75,15 @@ function deepResolve (drive, swarmer, archive, path, cb) {
7575

7676
function _resolve (archive, path, cb) {
7777
var swarm = swarmer(archive)
78-
resolve(archive, path, (err, nextArchiveKey, nextPath) => {
78+
resolve(archive, path, (err, linkInfo, nextPath) => {
7979
// must return result: swarm need to be closed
8080
var result = {archive, path, swarm}
8181
if (err) {
8282
return cb(err, result)
8383
}
8484
if (nextPath === path) return cb(null, result)
8585

86-
var nextArchive = drive.createArchive(nextArchiveKey, {sparse: true})
86+
var nextArchive = drive.createArchive(linkInfo.link, {sparse: true})
8787

8888
_resolve(nextArchive, nextPath, (err, resolved) => {
8989
cb(err, {
@@ -112,7 +112,8 @@ function encode (destKey, meta) {
112112
}
113113

114114
function decode (b) {
115+
var data = JSON.parse(b)
115116
var link = JSON.parse(b)['$$hdln$$']
116117
if (!link) throw new Error('not a link')
117-
return link
118+
return {link: link, meta: data.meta}
118119
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
"tape": "^4.6.3"
2222
},
2323
"scripts": {
24-
"test": "node test/*.js"
24+
"test": "tape test/*.js"
2525
}
2626
}

test/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tape('link', function (t) {
1919

2020
ln.readlink(archive, entries[0], (err, info) => {
2121
t.error(err)
22-
t.same(info, 'foo')
22+
t.same(info, {link: 'foo', meta: undefined})
2323
t.end()
2424
})
2525
})
@@ -37,9 +37,9 @@ tape('link with metadata', function (t) {
3737
t.error(err)
3838
t.same(entries[0].name, 'symlink')
3939

40-
ln.readlink(archive, entries[0], (err, key) => {
40+
ln.readlink(archive, entries[0], (err, info) => {
4141
t.error(err)
42-
t.same(key, 'foo')
42+
t.same(info, {link: 'foo', meta: {bar: 'baz'}})
4343

4444
collect(archive.createFileReadStream(entries[0]), (err, data) => {
4545
t.error(err)
@@ -62,7 +62,7 @@ tape('resolve', function (t) {
6262

6363
ln.resolve(archive, '/foo/link/bar/baz.txt', (err, link, nextPath) => {
6464
t.error(err)
65-
t.same(link, linkedArchive.key.toString('hex'))
65+
t.same(link, {link: linkedArchive.key.toString('hex'), meta: undefined})
6666
t.same(nextPath, 'bar/baz.txt')
6767
t.end()
6868
})
@@ -78,7 +78,7 @@ tape('resolve to file without link', function (t) {
7878
function test () {
7979
ln.resolve(archive, '/foo/link', (err, link, nextPath) => {
8080
t.error(err)
81-
t.same(link, archive.key.toString('hex'))
81+
t.same(link, {})
8282
t.same(nextPath, '/foo/link')
8383
t.end()
8484
})

0 commit comments

Comments
 (0)