Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix: resolve an issue where a new repo wouldnt init properly
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
jacobheun committed Apr 4, 2018
1 parent f4fab92 commit 104d6e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
24 changes: 17 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class S3Datastore {
* @returns {String}
*/
_getFullKey (key /* : Key */) {
return path.join(this.path, key.toString())
// Avoid absolute paths with s3
return path.join('.', this.path, key.toString())
}

/**
Expand Down Expand Up @@ -95,8 +96,11 @@ class S3Datastore {
this.opts.s3.getObject({
Key: this._getFullKey(key)
}, (err, data) => {
if (err) {
return callback(err, null)
if (err && err.statusCode === 404) {
// TODO: Cleanup across repos https://github.com/ipfs/js-ipfs/blob/master/src/core/boot.js#L43
return callback(new Error('not found'))
} else if (err) {
return callback(err)
}

// If a body was returned, ensure it's a Buffer
Expand Down Expand Up @@ -183,7 +187,7 @@ class S3Datastore {

this.opts.s3.listObjectsV2(params, (err, data) => {
if (err) {
return callback(err)
return callback(new Error(err.code))
}

data.Contents.forEach((d) => {
Expand Down Expand Up @@ -320,9 +324,15 @@ class S3Datastore {
* @returns {void}
*/
open (callback /* : Callback<void> */) /* : void */ {
this.opts.s3.headBucket({
Bucket: this.bucket
}, callback)
this.opts.s3.headObject({
Key: this.path
}, (err, data) => {
if (err && err.statusCode === 404) {
return this.put(new Key('/', false), Buffer.from(''), callback)
}

callback(err)
})
}

/**
Expand Down
13 changes: 7 additions & 6 deletions test/utils/s3-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const expect = chai.expect
const standin = require('stand-in')

class S3Error extends Error {
constructor (code) {
super(code)
this.code = code
constructor (message, code) {
super(message)
this.code = message
this.statusCode = code
}
}

Expand All @@ -27,7 +28,7 @@ module.exports = function (s3) {
delete storage[params.Key]
callback(null, {})
} else {
callback(new S3Error('NotFound'), null)
callback(new S3Error('NotFound', 404), null)
}
})

Expand All @@ -36,7 +37,7 @@ module.exports = function (s3) {
if (storage[params.Key]) {
callback(null, { Body: storage[params.Key] })
} else {
callback(new S3Error('NotFound'), null)
callback(new S3Error('NotFound', 404), null)
}
})

Expand All @@ -50,7 +51,7 @@ module.exports = function (s3) {
if (storage[params.Key]) {
callback(null, {})
} else {
callback(new S3Error('NotFound'), null)
callback(new S3Error('NotFound', 404), null)
}
})

Expand Down

0 comments on commit 104d6e9

Please sign in to comment.