Skip to content

Commit

Permalink
Merge pull request #12 from cyclic-software/object-function-mode
Browse files Browse the repository at this point in the history
fix: make it work like both function or object
  • Loading branch information
seekayel authored Jan 18, 2023
2 parents 5cdd6ac + 2468c17 commit ea8b8c8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 21 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ Require in the same format as Node.js `fs`, specifying an S3 Bucket:
const fs = require('@cyclic.sh/s3fs/promises')(S3_BUCKET_NAME)
```

- On cyclic.sh
- Alternatively, when using with <a href="https://cyclic.sh" target="_blank">cyclic.sh</a> or if the environment variable `CYCLIC_BUCKET_NAME` is set to an S3 bucket name, initialization can happen without specifying a bucket:
```js
const fs = require('@cyclic.sh/s3fs')
```
or
```js
const fs = require('@cyclic.sh/s3fs/promises')
```

### Authentication

Authenticating the client:
Expand Down
23 changes: 18 additions & 5 deletions src/CyclicS3FSPromises.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,24 @@ function streamToBuffer(stream) {
});
}

class CyclicS3FSPromises{
constructor(bucketName, config={}) {
this.bucket = bucketName
this.config = config
this.s3 = new S3Client({...config});
class CyclicS3FSPromises extends Function{
constructor(bucket, config={}) {
super('...args', 'return this._bound._call(...args)')
this._bound = this.bind(this)
if(process.env.CYCLIC_BUCKET_NAME){
this._bound.bucket = process.env.CYCLIC_BUCKET_NAME
}
if(bucket){
this._bound.bucket = bucket
}
this._bound.s3 = new S3Client({...config});

return this._bound
}

_call(bucketName, config) {
let client = new CyclicS3FSPromises(bucketName, config)
return client
}

async readFile(fileName ,options){
Expand Down
36 changes: 30 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ function makeCallback(cb) {

class CyclicS3FS extends CyclicS3FSPromises {
constructor(bucketName, config={}) {
super(bucketName, config={})
super()
if(process.env.CYCLIC_BUCKET_NAME){
this.bucket = process.env.CYCLIC_BUCKET_NAME
}
this.config = config
if(bucketName){
this.bucket = bucketName
}
}

_call(bucketName, config) {
let client = new CyclicS3FS(bucketName, config)
return client
}


Expand Down Expand Up @@ -186,12 +198,24 @@ class CyclicS3FS extends CyclicS3FSPromises {
}


const client = function(bucketName, config={}){
if(!process.env.AWS_SECRET_ACCESS_KEY){
console.warn('[s3fs] WARNING: AWS credentials are not set. Using local file system')
return fs
var client = new CyclicS3FS()
class FSFallback extends Function{
constructor() {
super('...args', 'return this._bound._call(...args)')
this._bound = this.bind(this)
Object.assign(this._bound,{...fs})
return this._bound
}
_call() {
let client = new FSFallback()
return client
}
return new CyclicS3FS(bucketName, config)
}

if(!process.env.AWS_SECRET_ACCESS_KEY){
console.warn('[s3fs] WARNING: AWS credentials are not set. Using local file system')
client = new FSFallback()
}


module.exports = client
21 changes: 16 additions & 5 deletions src/promises.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
const fs = require('fs/promises')
const CyclicS3FSPromises = require('./CyclicS3FSPromises')
const client = function(bucketName, config={}){
if(!process.env.AWS_SECRET_ACCESS_KEY){
console.warn('[s3fs] WARNING: AWS credentials are not set. Using local file system')
return fs
var client = new CyclicS3FSPromises()
class FSPromisesFallback extends Function{
constructor() {
super('...args', 'return this._bound._call(...args)')
this._bound = this.bind(this)
Object.assign(this._bound,{...fs})
return this._bound
}
_call() {
let client = new FSPromisesFallback()
return client
}
return new CyclicS3FSPromises(bucketName, config)
}

if(!process.env.AWS_SECRET_ACCESS_KEY){
console.warn('[s3fs] WARNING: AWS credentials are not set. Using local file system')
client = new FSPromisesFallback()
}

module.exports = client
1 change: 1 addition & 0 deletions src/sync_interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = {
runSync,
}
const run = async function(bucket, config, method, args){

const fs = new CyclicS3FSPromises(bucket, config)
let result = await fs[method](...args)
if(typeof result !== 'undefined'){
Expand Down
26 changes: 21 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
} = require("@aws-sdk/client-s3");
const s3 = new S3Client({});

const s3fs = require("../src")
const s3fs = require("../src")
const s3fs_promises = require("../src/promises")

afterAll(async () => {
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("Basic smoke tests", () => {
test("readFileSync(jpeg)", async () => {
const d = require("fs").readFileSync('test/_read.jpeg')

const fs = new s3fs(BUCKET)
const fs = s3fs(BUCKET)
const _d = fs.readFileSync('test/_read.jpeg')

expect(Buffer.compare(d, _d)).toEqual(0)
Expand All @@ -127,7 +127,7 @@ describe("Basic smoke tests", () => {
[Date.now()]: Date.now(),
})

const fs = new s3fs(BUCKET)
const fs = s3fs(BUCKET)
fs.writeFileSync('test/_write.json', content)
let x = fs.readFileSync('test/_write.json')

Expand All @@ -137,7 +137,7 @@ describe("Basic smoke tests", () => {
test("writeFileSync(big_text)", async () => {
const big_text = require("fs").readFileSync('test/_read_big.txt')

const fs = new s3fs(BUCKET)
const fs = s3fs(BUCKET)
fs.writeFileSync('test/_write_big.txt', big_text)
let x = fs.readFileSync('test/_write_big.txt')

Expand All @@ -147,7 +147,7 @@ describe("Basic smoke tests", () => {
test("writeFileSync(jpeg)", async () => {
const jpeg = require("fs").readFileSync('test/_read.jpeg')

const fs = new s3fs(BUCKET)
const fs = s3fs(BUCKET)
fs.writeFileSync('test/_write.jpeg', jpeg)
let jpeg_s3 = fs.readFileSync('test/_write.jpeg')

Expand Down Expand Up @@ -509,6 +509,20 @@ describe("Basic smoke tests", () => {

})


test("object mode / function mode", async () => {
let as_object = Object.getOwnPropertyNames(Object.getPrototypeOf(require('../src')))
let as_function = Object.getOwnPropertyNames(Object.getPrototypeOf(require('../src')(BUCKET)))
expect(as_function).toEqual(as_object)
})

test("object mode / function mode - promises", async () => {
let as_object = Object.getOwnPropertyNames(Object.getPrototypeOf(require('../src/promises')))
let as_function = Object.getOwnPropertyNames(Object.getPrototypeOf(require('../src/promises')(BUCKET)))
expect(as_function).toEqual(as_object)
})


test("empty_bucket", async () => {
const fs = s3fs(BUCKET)
let dir_name = `/nested/dir_${Date.now()}`
Expand All @@ -522,6 +536,8 @@ describe("Basic smoke tests", () => {
})






})

0 comments on commit ea8b8c8

Please sign in to comment.