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

Commit

Permalink
Test readdir filenames with dashes (holepunchto#347)
Browse files Browse the repository at this point in the history
* Test readdir filenames with dashes

* fix tests

---------

Co-authored-by: Mathias Buus <[email protected]>
  • Loading branch information
LuKks and mafintosh authored Jul 4, 2023
1 parent cf0622c commit c8de896
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ module.exports = class Hyperdrive extends ReadyResource {

function shallowReadStream (files, folder, keys) {
let prev = '/'
let prevName = ''

return new Readable({
async read (cb) {
let node = null
Expand All @@ -526,8 +528,15 @@ function shallowReadStream (files, folder, keys) {
const i = suffix.indexOf('/')
const name = i === -1 ? suffix : suffix.slice(0, i)

prev = '/' + name + '0'
prev = '/' + name + (i === -1 ? '' : '0')

// just in case someone does /foo + /foo/bar, but we should prop not even support that
if (name === prevName) {
this._read(cb)
return
}

prevName = name
this.push(keys ? name : node)
cb(null)
}
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,42 @@ test('basic writable option', async function (t) {
}
})

test('readdir filenames with dashes', async function (t) {
t.plan(2)

const store = new Corestore(RAM)
const drive = new Hyperdrive(store)

await drive.put('/one', 'hi')
await drive.put('/one-two', 'hi')

const expected = ['one', 'one-two']

for await (const name of drive.readdir('/')) {
t.is(name, expected.shift())
}

await drive.close()
})

test('readdir filenames with dashes (nested)', async function (t) {
t.plan(2)

const store = new Corestore(RAM)
const drive = new Hyperdrive(store)

await drive.put('/one/two', 'hi')
await drive.put('/one-two', 'hi')

const expected = ['one-two', 'one']

for await (const name of drive.readdir('/')) {
t.is(name, expected.shift())
}

await drive.close()
})

test('basic compare', async function (t) {
const store = new Corestore(RAM)
const drive = new Hyperdrive(store)
Expand Down

0 comments on commit c8de896

Please sign in to comment.