Skip to content

Commit

Permalink
:octocat: hello world
Browse files Browse the repository at this point in the history
Migrating package form internal repo
  • Loading branch information
frytg committed Jul 24, 2020
0 parents commit 6c8f57f
Show file tree
Hide file tree
Showing 19 changed files with 2,993 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"env": {
"node": true,
"browser": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
}
}
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
open-pull-requests-limit: 10
schedule:
interval: "daily"
time: '08:00'
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
18 changes: 18 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Lint code

on: [push]

jobs:
build:
name: Lint Code Base
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Lint Code Base
uses: docker://github/super-linter:v2.2.0
env:
VALIDATE_ALL_CODEBASE: true
VALIDATE_ANSIBLE: false
42 changes: 42 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Publish packages

on:
release:
types: [created]

jobs:
github:
runs-on: ubuntu-latest
steps:
- name: 👀 Checkout repo
uses: actions/[email protected]
- name: 🛠 Setup Node
uses: actions/[email protected]
with:
node-version: '12.x'
registry-url: 'https://npm.pkg.github.com'
scope: '@swrlab'
- name: 📦 Install Dependencies
run: yarn
- name: 🚀 npm publish
run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

npm:
runs-on: ubuntu-latest
steps:
- name: 👀 Checkout repo
uses: actions/[email protected]
- name: 🛠 Setup Node
uses: actions/[email protected]
with:
node-version: '12.x'
registry-url: 'https://registry.npmjs.org'
scope: '@swrlab'
- name: 📦 Install Dependencies
run: yarn
- name: 🚀 Github publish
run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
62 changes: 62 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# config
config.js
test.js

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# next.js build output
.next
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
package-lock.json
package.json
yarn.lock
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Storage Wrapper for NodeJS

by [**SWR audio lab**](https://lab.swr.de/)

## Changelog

t.b.c.
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright 2020 SWR audio lab

Hippocratic License Version 2.0.

Licensor hereby grants permission by this license ("License"), free of charge, to any person or entity (the "Licensee") obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

* The above copyright notice and this License or a subsequent version published on the Hippocratic License Website (https://firstdonoharm.dev/) shall be included in all copies or substantial portions of the Software. Licensee has the option of following the terms and conditions either of the above numbered version of this License or of any subsequent version published on the Hippocratic License Website.

* Compliance with Human Rights Laws and Human Rights Principles:

1. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any applicable laws, regulations, or rules that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights (the "Human Rights Laws"). Where the Human Rights Laws of more than one jurisdiction are applicable to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.

2. Human Rights Principles. Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights (https://www.un.org/en/universal-declaration-human-rights/) and the United Nations Global Compact (https://www.unglobalcompact.org/what-is-gc/mission/principles) that define recognized principles of international human rights (the "Human Rights Principles"). It is Licensor's express intent that all use of the Software be consistent with Human Rights Principles. If Licensor receives notification or otherwise learns of an alleged violation of any Human Rights Principles relating to Licensee's use of the Software, Licensor may in its discretion and without obligation (i) (a) notify Licensee of such allegation and (b) allow Licensee 90 days from notification under (i)(a) to investigate and respond to Licensor regarding the allegation and (ii) (a) after the earlier of 90 days from notification under (i)(a), or Licensee's response under (i)(b), notify Licensee of License termination and (b) allow Licensee an additional 90 days from notification under (ii)(a) to cease use of the Software.

3. Indemnity. Licensee shall hold harmless and indemnify Licensor against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor's reasonable attorneys' fees, arising out of or relating to Licensee's non-compliance with this License or use of the Software in violation of Human Rights Laws or Human Rights Principles. 

* Enforceability: If any portion or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by applicable law, the court may modify this License to affect the original intent of the parties as closely as possible. The section headings are for convenience only and are not intended to affect the construction or interpretation of this License. Any rule of construction to the effect that ambiguities are to be resolved against the drafting party shall not apply in interpreting this License. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This Hippocratic License is an Ethical Source license (https://ethicalsource.dev).
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Storage Wrapper for NodeJS

This module allows easy unified access to the most common storage operations in the local file storage, AWS S3 and Google Cloud Storage.

Please note: This project is still under development!

## Installation

t.b.c.

## Changelog

A separate Changelog is available in [CHANGELOG.md](CHANGELOG.md)

## License

See [LICENSE.md](LICENSE.md)
1 change: 1 addition & 0 deletions keys/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@swrlab/node-storage-wrapper",
"version": "0.0.1",
"description": "Wrapping AWS S3, GCP GCS, file storage",
"main": "./src/index.js",
"engines": {
"node": ">=12"
},
"repository": {
"type": "git",
"url": "https://github.com/swrlab/node-storage-wrapper"
},
"author": "SWR audio lab <[email protected]>",
"license": "SEE LICENSE in LICENSE.md",
"private": false,
"scripts": {
},
"dependencies": {
"@google-cloud/storage": "5.1.2",
"aws-sdk": "2.719.0",
"node-fetch": "2.6.0",
"uuid": "8.2.0"
},
"devDependencies": {
"@swrlab/swr-prettier-config": "0.0.2",
"eslint": "^7.5.0",
"eslint-plugin-swr": "0.0.3"
},
"prettier": "@swrlab/swr-prettier-config"
}
16 changes: 16 additions & 0 deletions src/createUri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
node-storage-wrapper
This module provides easy access to combine bucket + path to unique URIs
*/

module.exports = {
s3: (bucket, path) => {
return ['s3:/', bucket, path].join('/');
},
gs: (bucket, path) => {
return ['gs:/', bucket, path].join('/');
},
}
66 changes: 66 additions & 0 deletions src/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
node-storage-wrapper
*/

const deleteLocalFile = (filePath) =>
new Promise((resolve, reject) => {
this.sdk.fs.unlink(filePath, (err) => {
if (err) reject(err);
else resolve();
});
});

module.exports = async function (uri) {
try {
let structure, bucket, path;

if (uri.substr(0, 5).toLowerCase() == 's3://') {
// aws s3 file
structure = uri.substr(5).split('/');
bucket = structure.shift();
path = structure.join('/');

// log progress
this.log('log', ['storage.delete.s3 >', uri]);

// delete from aws
await this.sdk.s3
.deleteObject({
Bucket: bucket,
Key: path,
})
.promise();

// return ok
return Promise.resolve();
} else if (uri.substr(0, 5).toLowerCase() == 'gs://') {
// google cloud storage
structure = uri.substr(5).split('/');
bucket = structure.shift();
path = structure.join('/');

// log progress
this.log('log', ['storage.delete.gs >', uri]);

// delete from gcp
await this.sdk.gs.bucket(bucket).file(path).delete(path);

// return ok
return Promise.resolve();
} else {
// log progress
this.log('log', ['storage.delete.local >', uri]);

// delete file
await deleteLocalFile(uri);

// return ok
return Promise.resolve();
}
} catch (err) {
this.log('error', ['storage.delete', JSON.stringify({ uri, message: err.message, stack: err.stack })]);
return Promise.reject(err);
}
};
61 changes: 61 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
node-storage-wrapper
*/

// load node utils
var AWS = require('aws-sdk');
const fs = require('fs');
const { Storage } = require('@google-cloud/storage');

// create wrapper
function StorageWrapper(config) {
// check config
if (!config || !config.gs || !config.s3) {
return Promise.reject('storage config invalid');
}

// enable SDKs
this.sdk = {};

// load node utils
this.sdk.fs = fs;

// load google cloud storage sdk
this.sdk.gs = new Storage(config.gs);

// load aws sdk
AWS.config.loadFromPath(config.s3);
this.sdk.s3 = new AWS.S3();

// enable logging
this.log = config.logging
? (level, message) => {
if (message instanceof Array) {
message = message.join(' ');
}

if (level == 'log') {
console.log(message);
} else if (level == 'warn') {
console.warn(message);
} else if (level == 'error') {
console.error(message);
}
}
: null;

this.log('log', ['storage.index', 'loaded config >', JSON.stringify({ config })]);
}

// enable utils
StorageWrapper.prototype.createUri = require('./createUri');
StorageWrapper.prototype.delete = require('./delete');
StorageWrapper.prototype.list = require('./list');
StorageWrapper.prototype.load = require('./load');
StorageWrapper.prototype.move = require('./move');
StorageWrapper.prototype.save = require('./save');

// export
module.exports = StorageWrapper;
Loading

0 comments on commit 6c8f57f

Please sign in to comment.