Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Add follow option to entry method (holepunchto#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuKks authored Jul 4, 2023
1 parent 62df017 commit cf0622c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Returns the blob at `path` in the drive. If no blob exists, returns `null`.

It also returns `null` for symbolic links.

#### `const entry = await drive.entry(path)`
#### `const entry = await drive.entry(path, [options])`

Returns the entry at `path` in the drive. It looks like this:
```js
Expand All @@ -143,6 +143,13 @@ Returns the entry at `path` in the drive. It looks like this:
}
```

`options` include:
```js
{
follow: false // Follow symlinks, 16 max or throws an error
}
```

#### `const exists = await drive.exists(path)`

Returns `true` if the entry at `path` does exists, otherwise `false`.
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,19 @@ module.exports = class Hyperdrive extends ReadyResource {
}

async entry (name, opts) {
if (!opts || !opts.follow) return this._entry(name, opts)

for (let i = 0; i < 16; i++) {
const node = await this._entry(name, opts)
if (!node || !node.value.linkname) return node

name = unixPathResolve(node.key, node.value.linkname)
}

throw new Error('Recursive symlink')
}

async _entry (name, opts) {
if (typeof name !== 'string') return name

return this.db.get(std(name, false), { ...opts, keyEncoding })
Expand Down
82 changes: 82 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,88 @@ test('basic compare', async function (t) {
t.is(drive.compare(c, a), 1)
})

test('basic follow entry', async function (t) {
const store = new Corestore(RAM)
const drive = new Hyperdrive(store)

await drive.put('/file.txt', 'hi')
await drive.symlink('/file.shortcut', '/file.txt')

t.is((await drive.entry('/file.shortcut')).value.linkname, '/file.txt')

t.alike(await drive.entry('/file.shortcut', { follow: true }), {
seq: 1,
key: '/file.txt',
value: {
executable: false,
linkname: null,
blob: { byteOffset: 0, blockOffset: 0, blockLength: 1, byteLength: 2 },
metadata: null
}
})

await drive.close()
})

test('multiple follow entry', async function (t) {
const store = new Corestore(RAM)
const drive = new Hyperdrive(store)

await drive.put('/file.txt', 'hi')
await drive.symlink('/file.shortcut', '/file.txt')
await drive.symlink('/file.shortcut.shortcut', '/file.shortcut')

t.is((await drive.entry('/file.shortcut.shortcut')).value.linkname, '/file.shortcut')

t.alike(await drive.entry('/file.shortcut.shortcut', { follow: true }), {
seq: 1,
key: '/file.txt',
value: {
executable: false,
linkname: null,
blob: { byteOffset: 0, blockOffset: 0, blockLength: 1, byteLength: 2 },
metadata: null
}
})

await drive.close()
})

test('max follow entry', async function (t) {
const store = new Corestore(RAM)
const drive = new Hyperdrive(store)

await drive.put('/file.0.txt', 'hi')

for (let i = 1; i <= 17; i++) {
await drive.symlink('/file.' + i + '.txt', '/file.' + (i - 1) + '.txt')
}

t.is((await drive.entry('/file.0.txt')).value.linkname, null)
t.is((await drive.entry('/file.1.txt')).value.linkname, '/file.0.txt')
t.is((await drive.entry('/file.16.txt')).value.linkname, '/file.15.txt')

try {
await drive.entry('/file.16.txt', { follow: true })
t.fail('Should have failed')
} catch {
t.pass()
}

await drive.close()
})

test('non-existing follow entry', async function (t) {
const store = new Corestore(RAM)
const drive = new Hyperdrive(store)

await drive.put('/file.txt', 'hi')

t.is(await drive.entry('/file.random.shortcut', { follow: true }), null)

await drive.close()
})

async function testenv (teardown) {
const corestore = new Corestore(RAM)
await corestore.ready()
Expand Down

0 comments on commit cf0622c

Please sign in to comment.