Skip to content

Commit 1394316

Browse files
committed
first commit
0 parents  commit 1394316

File tree

7 files changed

+1743
-0
lines changed

7 files changed

+1743
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Po Chieh
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# hyperdrive-ln
2+
3+
create symbolic link between [hyperdrives](https://github.com/mafintosh/hyperdrive)
4+
5+
## Usage
6+
7+
```js
8+
const tape = require('tape')
9+
const memdb = require('memdb')
10+
const hyperdrive = require('hyperdrive')
11+
const ln = require('.')
12+
13+
var drive = hyperdrive(memdb())
14+
var archive = drive.createArchive()
15+
16+
ln.link(archive, 'linkfile', <ARCHIVE KEY>, cb) // create symlink to another archive
17+
ln.readlink(archive, 'linkfile', cb) // get linked archive key
18+
ln.read(drive, archive, 'linkfile', cb) // returns a hyperdrive archive pointed to linked archive
19+
```

index.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const collect = require('stream-collect')
2+
const Readable = require('stream').Readable
3+
4+
module.exports = {read, readlink, link}
5+
6+
function readlink (archive, entry, cb) {
7+
collect(archive.createFileReadStream(entry), body => {
8+
try {
9+
var l = decode(body)
10+
if (!l) return cb(new Error('no link found'))
11+
cb(null, l)
12+
} catch (e) {
13+
cb(e)
14+
}
15+
})
16+
}
17+
18+
function read (drive, archive, entry, cb) {
19+
readlink(archive, entry, (err, info) => {
20+
if (err) return cb(err)
21+
cb(null, drive.createArchive(info))
22+
})
23+
}
24+
25+
function link (archive, entry, destArchiveKey, cb) {
26+
var s = new Readable()
27+
s.push(encode(destArchiveKey))
28+
s.push(null)
29+
var w = archive.createFileWriteStream(entry)
30+
s.pipe(w).on('finish', cb)
31+
}
32+
33+
function encode (destKey) {
34+
return JSON.stringify({l: destKey})
35+
}
36+
37+
function decode (b) {
38+
return JSON.parse(b).l
39+
}

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "hyperdrive-ln",
3+
"author": "Poga Po<poga.bahamut@gmail.com>",
4+
"version": "1.0.0",
5+
"main": "index.js",
6+
"license": "MIT",
7+
"dependencies": {
8+
"stream-collect": "^1.3.1"
9+
},
10+
"repository": "poga/hyperdrive-ln",
11+
"devDependencies": {
12+
"hyperdrive": "^7.12.1",
13+
"memdb": "^1.3.1",
14+
"tape": "^4.6.3"
15+
},
16+
"scripts": {
17+
"test": "node test.js"
18+
}
19+
}

test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const tape = require('tape')
2+
const memdb = require('memdb')
3+
const hyperdrive = require('hyperdrive')
4+
5+
const ln = require('.')
6+
7+
tape('link', function (t) {
8+
var drive = hyperdrive(memdb())
9+
var archive = drive.createArchive()
10+
11+
ln.link(archive, 'symlink', 'foo', () => {
12+
archive.list((err, entries) => {
13+
t.error(err)
14+
t.same(entries[0].name, 'symlink')
15+
16+
ln.readlink(archive, entries[0], (err, info) => {
17+
t.error(err)
18+
t.same(info, 'foo')
19+
t.end()
20+
})
21+
})
22+
})
23+
})

0 commit comments

Comments
 (0)