Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rm methods #8

Merged
merged 17 commits into from
Jan 9, 2023
Merged
Prev Previous commit
Next Next commit
fix(dir methods): rm
  • Loading branch information
korostelevm committed Jan 9, 2023
commit 50dfe66d8372c565f87009370c937a6de195b44c
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ Require in the same format as Node.js `fs`, specifying an S3 Bucket:
- [x] promise
- [x] cb
- [x] sync
- [ ] fs.unlink(path, callback)
- [ ] fs.createReadStream(path, [options])
- [ ] fs.createWriteStream(path, [options])
- [x] fs.unlink(path, callback)
- [x] promise
- [x] cb
- [x] sync

## Example Usage
### Authentication
Expand Down
30 changes: 30 additions & 0 deletions src/CyclicS3FSPromises.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,36 @@ class CyclicS3FSPromises{
}
}

async unlink(path){
try{
let f = await Promise.allSettled([
this.stat(path),
this.readdir(path)
])

if(f[0].status == 'rejected' && f[1].status == 'fulfilled'){
throw new Error(`EPERM: operation not permitted, unlink '${path}'`)
}
if(f[0].status == 'rejected' && f[1].status == 'rejected'){
throw f[0].reason
}

}catch(e){
throw e
}
path = util.normalize_path(path)
const cmd = new DeleteObjectCommand({
Bucket: this.bucket,
Key: path
})
try{
await this.s3.send(cmd)
}catch(e){
throw e
}

}

}


Expand Down
18 changes: 18 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ class CyclicS3FS extends CyclicS3FSPromises {
})
}

unlink(path, callback) {
callback = makeCallback(arguments[arguments.length - 1]);
new Promise(async (resolve,reject)=>{
try{
this.stat = super.stat
this.readdir = super.readdir
let res = await super.unlink(...arguments)
return resolve(callback(null,res))
}catch(e){
return resolve(callback(e))
}
})
}

rmdir(path, callback) {
callback = makeCallback(arguments[arguments.length - 1]);
new Promise(async (resolve,reject)=>{
Expand Down Expand Up @@ -161,6 +175,10 @@ class CyclicS3FS extends CyclicS3FSPromises {
return sync_interface.runSync(this,'rm',[path])
}

unlinkSync(path) {
return sync_interface.runSync(this,'unlink',[path])
}

rmdirSync(path) {
return sync_interface.runSync(this,'rmdir',[path])
}
Expand Down
68 changes: 68 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,74 @@ describe("Basic smoke tests", () => {

})

test("unlink() - promises", async () => {
const fs = s3fs_promises(BUCKET)
try{
await fs.rm('test/not_there.json')
}catch(e){
expect(e.message).toContain(`ENOENT: no such file or directory`)
}
await fs.writeFile('test/aaa.txt','asdfsdf')
await fs.unlink('test/aaa.txt')

try{
await fs.stat('test/aaa.txt')
}catch(e){
expect(e.message).toContain(`ENOENT: no such file or directory`)
}
})

test("unlinkSync()", async () => {
const fs = s3fs(BUCKET)
try{
fs.unlinkSync('test/not_there.json')
}catch(e){
expect(e).toContain(`ENOENT: no such file or directory`)
}

fs.writeFileSync('test/aaa.txt','asdfsdf')
fs.unlinkSync('test/aaa.txt')

try{
await fs.statSync('test/aaa.txt')
}catch(e){
expect(e).toContain(`ENOENT: no such file or directory`)
}
})


test("unlink() - callback", async () => {

const fs = s3fs(BUCKET)
await new Promise((resolve,reject)=>{
fs.unlink('test/not_there.txt', (error,data) =>{
expect(error.message).toContain(`ENOENT: no such file or directory`)
resolve()
})
})
fs.writeFileSync('test/aaa.txt','asdfsdf')

await new Promise((resolve,reject)=>{
fs.unlink('test/aaa.txt',(error,data)=>{
expect(error).toEqual(null)
resolve()
})
})
})


test("unlinkSync(directory)", async () => {
const fs = s3fs(BUCKET)
fs.mkdirSync('/dir')

try{
fs.unlinkSync('/dir')
}catch(e){
expect(e).toContain(`EPERM: operation not permitted, unlink`)
}

})



test("rmdir() - promises", async () => {
Expand Down