From 4d0206b5f01051a03e1380aac3c020832df47f8a Mon Sep 17 00:00:00 2001 From: Moritz Raho Date: Tue, 20 Aug 2019 16:49:20 +0200 Subject: [PATCH] initial commit - template --- .eslintignore | 2 + .eslintrc | 7 ++ .github/CONTRIBUTING.md | 47 ++++++++ .github/ISSUE_TEMPLATE.md | 16 +++ .github/PULL_REQUEST_TEMPLATE.md | 45 +++++++ .gitignore | 30 +++++ .travis.yml | 17 +++ CODE_OF_CONDUCT.md | 74 ++++++++++++ COPYRIGHT | 5 + LICENSE | 201 +++++++++++++++++++++++++++++++ README.md | 102 +++++++++++++++- index.js | 113 +++++++++++++++++ jest.config.js | 22 ++++ jsconfig.json | 7 ++ lib/azure/CosmosKVS.js | 2 + lib/types.jsdoc.js | 16 +++ package.json | 50 ++++++++ test/index.test.js | 162 +++++++++++++++++++++++++ test/jest.setup.js | 59 +++++++++ 19 files changed, 976 insertions(+), 1 deletion(-) create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .github/CONTRIBUTING.md create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 CODE_OF_CONDUCT.md create mode 100644 COPYRIGHT create mode 100644 LICENSE create mode 100644 index.js create mode 100644 jest.config.js create mode 100644 jsconfig.json create mode 100644 lib/azure/CosmosKVS.js create mode 100644 lib/types.jsdoc.js create mode 100644 package.json create mode 100644 test/index.test.js create mode 100644 test/jest.setup.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..b947077 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +node_modules/ +dist/ diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..e0a1851 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,7 @@ +{ + "globals": { + "NodeJS": true + }, + "plugins": ["jest"], + "extends": ["standard", "plugin:jest/recommended", "plugin:jsdoc/recommended"] +} diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..c4940cf --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,47 @@ +# Contributing + +Thanks for choosing to contribute! + +The following are a set of guidelines to follow when contributing to this project. + +## Code Of Conduct + +This project adheres to the Adobe [code of conduct](../CODE_OF_CONDUCT.md). By participating, +you are expected to uphold this code. Please report unacceptable behavior to +[Grp-opensourceoffice@adobe.com](mailto:Grp-opensourceoffice@adobe.com). + +## Have A Question? + +Start by filing an issue. The existing committers on this project work to reach +consensus around project direction and issue solutions within issue threads +(when appropriate). + +## Contributor License Agreement + +All third-party contributions to this project must be accompanied by a signed contributor +license agreement. This gives Adobe permission to redistribute your contributions +as part of the project. [Sign our CLA](http://opensource.adobe.com/cla.html). You +only need to submit an Adobe CLA one time, so if you have submitted one previously, +you are good to go! + +## Code Reviews + +All submissions should come in the form of pull requests and need to be reviewed +by project committers. Read [GitHub's pull request documentation](https://help.github.com/articles/about-pull-requests/) +for more information on sending pull requests. + +Lastly, please follow the [pull request template](PULL_REQUEST_TEMPLATE.md) when +submitting a pull request! + +## From Contributor To Committer + +We love contributions from our community! If you'd like to go a step beyond contributor +and become a committer with full write access and a say in the project, you must +be invited to the project. The existing committers employ an internal nomination +process that must reach lazy consensus (silence is approval) before invitations +are issued. If you feel you are qualified and want to get more deeply involved, +feel free to reach out to existing committers to have a conversation about that. + +## Security Issues + +Security issues shouldn't be reported on this issue tracker. Instead, [file an issue to our security experts](https://helpx.adobe.com/security/alertus.html) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..389679c --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,16 @@ + + + +### Expected Behaviour + +### Actual Behaviour + +### Reproduce Scenario (including but not limited to) + +#### Steps to Reproduce + +#### Platform and Version + +#### Sample Code that illustrates the problem + +#### Logs taken while reproducing problem diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..9529d71 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,45 @@ + + +## Description + + + +## Related Issue + + + + + + +## Motivation and Context + + + +## How Has This Been Tested? + + + + + +## Screenshots (if appropriate): + +## Types of changes + + + +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to change) + +## Checklist: + + + + +- [ ] I have signed the [Adobe Open Source CLA](http://opensource.adobe.com/cla.html). +- [ ] My code follows the code style of this project. +- [ ] My change requires a change to the documentation. +- [ ] I have updated the documentation accordingly. +- [ ] I have read the **CONTRIBUTING** document. +- [ ] I have added tests to cover my changes. +- [ ] All new and existing tests passed. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0b75d5c --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +# package directories +node_modules +jspm_packages + +# Serverless directories +.serverless + +# build +dist +.manifest-dist.yml + +# Config +config.json +.env + +# IDE & Temp +.cache +.idea +.nyc_output +.vscode +coverage +.aws.tmp.creds.json + +# OSX +.DS_Store + +#custom +playground + +.tvmCache \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3006ebb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: node_js +os: + - windows + - linux +node_js: + - "10" + - "12" +env: + global: + - CXX=g++-4.8 +install: + - npm version + - npm ci +script: + - npm test +after_success: + - ./node_modules/.bin/codecov diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..7ba0d6a --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,74 @@ +# Adobe Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at Grp-opensourceoffice@adobe.com. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [https://contributor-covenant.org/version/1/4][version] + +[homepage]: https://contributor-covenant.org +[version]: https://contributor-covenant.org/version/1/4/ diff --git a/COPYRIGHT b/COPYRIGHT new file mode 100644 index 0000000..24c564b --- /dev/null +++ b/COPYRIGHT @@ -0,0 +1,5 @@ +© Copyright 2015-2019 Adobe. All rights reserved. + +Adobe holds the copyright for all the files found in this repository. + +See the LICENSE file for licensing information. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e3af604 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2019 Adobe + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index 7a09c81..77ead77 100644 --- a/README.md +++ b/README.md @@ -1 +1,101 @@ -# adobeio-cna-kv-store \ No newline at end of file + + +[![Version](https://img.shields.io/npm/v/@adobe/adobeio-cna-cloud-storage.svg)](https://npmjs.org/package/@adobe/adobeio-cna-cloud-storage) +[![Downloads/week](https://img.shields.io/npm/dw/@adobe/adobeio-cna-cloud-storage.svg)](https://npmjs.org/package/@adobe/adobeio-cna-cloud-storage) +[![Build Status](https://travis-ci.com/adobe/adobeio-cna-cloud-storage.svg?branch=master)](https://travis-ci.com/adobe/adobeio-cna-cloud-storage) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) +[![Codecov Coverage](https://img.shields.io/codecov/c/github/adobe/adobeio-cna-cloud-storage/master.svg?style=flat-square)](https://codecov.io/gh/adobe/adobeio-cna-cloud-storage/) + +# Adobe I/O CNA Storage SDK + +A JavaScript abstraction on top of cloud blob storages exposing a file-system like API. + +You can initialize the SDK with your Adobe I/O Runtime (a.k.a OpenWhisk) +credentials. + +Alternatively, you can bring your own cloud storage keys. Note however, that as +of now we only support Azure Blob Storage. AWS S3 is the next on the todo list +and will soon be available. + +## Install + +```bash +npm install @adobe/adobeio-cna-cloud-storage +``` + +## Use + +```js + const storageSDK = require('@adobe/adobeio-cna-cloud-storage') + + // init + // init sdk using OpenWhisk credentials + const storage = await storageSDK.init({ ow: { namespace, auth } }) + // init when env vars __OW_AUTH and __OW_NAMESPACE are set (e.g. when running in an OpenWhisk action) + const storage = await storageSDK.init() + // or if you want to use your own storage account + const storage = await storageSDK.init({ azure: { storageAccount, storageAccessKey, containerName } }) + + // write private file + await storage.write('mydir/myfile.txt', 'some private content') + + // write publicly accessible file + await storage.write('public/index.html', '

Hello World!

') + + // get file url + const props = await storage.getProperties('public/index.html') + props.url + + // list all files + await storage.list('/') // ['mydir/myfile.txt', 'public/index.html'] + + // read + const buffer = await storage.read('mydir/myfile.txt') + buffer.toString() // 'some private content' + + // pipe read stream to local file + const rdStream = await storage.createReadStream('mydir/myfile.txt') + const stream = rdStream.pipe(fs.createWriteStream('my-local-file.txt')) + stream.on('finish', () => console.log('done!')) + + // write read stream to remote file + const rdStream = fs.createReadStream('my-local-file.txt') + await storage.write('my/remote/file.txt', rdStream) + + // delete files in 'my/remote/' dir + await storage.delete('my/remote/') + // delete all public files + await storage.delete('public/') + // delete all files including public + await storage.delete('/') + + // copy + // upload local directory + await storage.copy('my-static-app/', 'public/', { localSrc: true }) + // download to local directory + await storage.copy('public/my-static-app/', 'my-static-app-copy', { localDest: true }) + // copy files around cloud storage + await storage.copy('public/my-static-app/', 'my/private/folder') +``` + +## Explore + +`goto` [API](doc/api.md) + +## Contributing + +Contributions are welcomed! Read the [Contributing Guide](./.github/CONTRIBUTING.md) for more information. + +## Licensing + +This project is licensed under the Apache V2 License. See [LICENSE](LICENSE) for more information. diff --git a/index.js b/index.js new file mode 100644 index 0000000..f8ca796 --- /dev/null +++ b/index.js @@ -0,0 +1,113 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ +const joi = require('@hapi/joi') + +const { CosmosKVS } = require('./lib/azure/CosmosKVS') +const { KVSError } = require('./lib/KVSError') +const { KVS } = require('./lib/KVS') +const TvmClient = require('@adobe/adobeio-cna-tvm-client') + +/** + * Initializes and returns the key-value-store SDK. + * + * To use the SDK you must either provide provide your + * [OpenWhisk credentials]{@link module:types~OpenWhiskCredentials} in + * `credentials.ow` or your own + * [Azure storage credentials]{@link module:types~AzureCosmosMasterCredentials} in `credentials.azure`. + * + * OpenWhisk credentials can also be read from environment variables (`OW_NAMESPACE` or `__OW_NAMESPACE` and `OW_AUTH` or `__OW_AUTH`). + * + * @param {object} credentials used to init the sdk + * + * @param {module:types~OpenWhiskCredentials} [credentials.ow] + * {@link module:types~OpenWhiskCredentials}. Set those if you want + * to use ootb credentials to access our storage infrastructure. OpenWhisk + * namespace and auth can also be passed through environment variables: + * `OW_NAMESPACE` or `__OW_NAMESPACE` and `OW_AUTH` or `__OW_AUTH` + * + * @param {module:types~AzureCosmosMasterCredentials|module:types~AzureCosmosResourceCredentials} [credentials.azure] + * bring your own [Azure SAS credentials]{@link module:types~AzureCosmosResourceCredentials} or + * [Azure storage account credentials]{@link module:types~AzureCosmosMasterCredentials} + * + * @param {object} [options={}] options + * @param {string} [options.tvmApiUrl] alternative tvm api url. Only makes + * sense in the context of OpenWhisk credentials. + * @param {string} [options.tvmCacheFile] alternative tvm cache file, defaults + * to `/.tvmCache`. Set to `false` to disable caching. Only makes + * sense in the context of OpenWhisk credentials. + * @returns {Promise} A storage instance + * @throws {KVSError} + */ +async function init (credentials, options = {}) { + // todo in tvm client? + // include ow environment vars to credentials + const namespace = process.env['__OW_NAMESPACE'] || process.env['OW_NAMESPACE'] + const auth = process.env['__OW_AUTH'] || process.env['OW_AUTH'] + if (namespace || auth) { + if (typeof credentials !== 'object') { + credentials = {} + } + if (typeof credentials.ow !== 'object') { + credentials.ow = {} + } + credentials.ow.namespace = credentials.ow.namespace || namespace + credentials.ow.auth = credentials.ow.auth || auth + } + + return _init(credentials, options) +} + +// eslint-disable-next-line jsdoc/require-jsdoc +async function _init (credentials, options) { + const validation = joi.validate(credentials, joi.object().label('credentials').keys({ + azure: joi.object().keys({ + // either + cosmosClientArgs: joi.object().label('cosmosClientArgs').keys({ + endpoint: joi.string().required(), + resourceTokens: joi.object().required() + }), + // or + cosmosMasterKey: joi.string(), + cosmosAccount: joi.string(), + // always + databaseId: joi.string().required(), + containerId: joi.string().required() + }).unknown().and('cosmosMasterKey', 'cosmosAccount').xor('cosmosMasterKey', 'cosmosClientArgs'), + ow: joi.object().keys({ + namespace: joi.string().required(), + auth: joi.string().required() + }) + }).unknown().xor('ow', 'azure').required()) + if (validation.error) throw new KVSError(validation.error.message, KVSError.codes.BadArgument) + + // 1. set provider + const provider = 'azure' // only azure is supported for now + + // 2. instantiate tvm if ow credentials + let tvm + if (credentials.ow && !credentials.azure) { + // default tvm url + const tvmArgs = { ow: credentials.ow, apiUrl: options.tvmApiUrl || _defaultTvmApiUrl } + if (options.tvmCacheFile) tvmArgs.cacheFile = options.tvmCacheFile + tvm = new TvmClient(tvmArgs) + } + + // 3. return storage based on provider + switch (provider) { + case 'azure': + return CosmosKVS.init(credentials.azure || await tvm.getAzureCosmosCredentials()) + // default: + // throw new KVSError(`provider '${provider}' is not supported.`, KVSError.codes.BadArgument) + } +} + +module.exports = { init, _defaultTvmApiUrl } diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..6dabe5d --- /dev/null +++ b/jest.config.js @@ -0,0 +1,22 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +module.exports = { + testEnvironment: 'node', + verbose: true, + setupFilesAfterEnv: ['./test/jest.setup.js'], + collectCoverage: true, + collectCoverageFrom: [ + 'index.js', + 'lib/**/*.js' + ] +} diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..38c21a9 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,7 @@ +{ + "typeAcquisition": { + "include": [ + "jest" + ] + } +} diff --git a/lib/azure/CosmosKVS.js b/lib/azure/CosmosKVS.js new file mode 100644 index 0000000..8043383 --- /dev/null +++ b/lib/azure/CosmosKVS.js @@ -0,0 +1,2 @@ +const cosmos = require('@azure/cosmos') + diff --git a/lib/types.jsdoc.js b/lib/types.jsdoc.js new file mode 100644 index 0000000..3a5eb0e --- /dev/null +++ b/lib/types.jsdoc.js @@ -0,0 +1,16 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +/* istanbul ignore file */ + +/** @module types */ + diff --git a/package.json b/package.json new file mode 100644 index 0000000..2ed9111 --- /dev/null +++ b/package.json @@ -0,0 +1,50 @@ +{ + "name": "@adobe/adobeio-cna-kv-store", + "version": "0.1.2", + "description": "An Abstraction on top of Key Value Stores", + "main": "index.js", + "directories": { + "lib": "lib" + }, + "scripts": { + "lint": "eslint index.js lib/**/*.js test/**/*.js", + "beautify": "npm run lint -- --fix", + "test": "jest -c ./jest.config.js && npm run lint", + "generate-jsdoc": "jsdoc2md -f index.js 'lib/**/*.js' > doc/api.md" + }, + "author": "Adobe Inc.", + "license": "Apache-2.0", + "engines": { + "node": ">=10.0.0" + }, + "repository": "adobe/adobeio-cna-kv-store", + "keywords": [ + "sdk", + "openwhisk", + "key-value-store", + "persistence", + "cloud-native", + "adobe-io", + "abstraction" + ], + "devDependencies": { + "@types/hapi__joi": "^15.0.3", + "@types/jest": "^24.0.17", + "codecov": "^3.5.0", + "eslint": "^5.16.0", + "eslint-config-standard": "^12.0.0", + "eslint-plugin-import": "^2.17.3", + "eslint-plugin-jest": "^22.6.4", + "eslint-plugin-jsdoc": "^15.3.2", + "eslint-plugin-node": "^9.1.0", + "eslint-plugin-promise": "^4.1.1", + "eslint-plugin-standard": "^4.0.0", + "jest": "^24.8.0", + "jsdoc-to-markdown": "^5.0.0" + }, + "dependencies": { + "@adobe/adobeio-cna-tvm-client": "^0.1.0", + "@azure/cosmos": "^10.3.0", + "@hapi/joi": "^15.1.0" + } +} diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..13ae733 --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,162 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +const kvsSDK = require('../index') + +// beforeEach(async () => { +// expect.hasAssertions() +// jest.restoreAllMocks() +// }) + +// describe('init', () => { +// /* Common setup for init tests */ +// const maxDate = new Date(8640000000000000).toISOString() +// let fakeAzureSASCredentials +// let fakeAzureUserCredentials +// let fakeAzureTVMResponse +// let fakeOWCreds +// beforeEach(async () => { +// AzureStorage.mockRestore() +// AzureStorage.init = jest.fn() +// fakeAzureSASCredentials = { +// sasURLPrivate: 'https://fake.com/private', +// sasURLPublic: 'https://fake.com/public' +// } +// fakeAzureUserCredentials = { +// containerName: 'fake', +// storageAccessKey: 'fakeKey', +// storageAccount: 'fakeAccount' +// } +// fakeAzureTVMResponse = { +// expiration: maxDate, +// ...fakeAzureSASCredentials +// } +// fakeOWCreds = { +// auth: 'fake', +// namespace: 'fake' +// } +// }) + +// describe('with bad arguments', () => { +// test('when empty credentials', async () => { +// await expect(storageSDK.init).toThrowBadArgWithMessageContaining(['credentials', 'required']) +// }) +// test('when credentials with empty ow object', async () => { +// await expect(storageSDK.init.bind(null, { ow: {} })).toThrowBadArgWithMessageContaining(['ow']) +// }) +// test('when credentials with empty azure object', async () => { +// await expect(storageSDK.init.bind(null, { azure: {} })).toThrowBadArgWithMessageContaining(['azure']) +// }) +// test('when credentials with both ow and azure credentials set', async () => { +// await expect(storageSDK.init.bind(null, { azure: fakeAzureUserCredentials, ow: fakeOWCreds })).toThrowBadArgWithMessageContaining(['azure', 'ow']) +// }) +// }) + +// describe('with user provided azure credentials', () => { +// test('when storage account credentials are specified', async () => { +// await storageSDK.init({ azure: fakeAzureUserCredentials }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureUserCredentials) +// expect(TvmClient).toHaveBeenCalledTimes(0) +// }) + +// test('when SAS credentials are specified', async () => { +// await storageSDK.init({ azure: fakeAzureSASCredentials }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureSASCredentials) +// expect(TvmClient).toHaveBeenCalledTimes(0) +// }) +// }) + +// describe('with OpenWhisk credentials', () => { +// const fakeTvmApiUrl = 'http://fakeApiUrl' +// const fakeTvmCacheFile = 'fake-cache.file' +// beforeEach(async () => { +// TvmClient.mockRestore() +// TvmClient.prototype.getAzureBlobCredentials.mockRestore() +// TvmClient.prototype.getAzureBlobCredentials.mockResolvedValue(fakeAzureTVMResponse) +// delete process.env['__OW_AUTH'] +// delete process.env['__OW_NAMESPACE'] +// delete process.env['OW_AUTH'] +// delete process.env['OW_NAMESPACE'] +// }) +// test('when tvm url is not specified', async () => { +// await storageSDK.init({ ow: fakeOWCreds }) +// expect(TvmClient.prototype.getAzureBlobCredentials).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledWith({ ow: fakeOWCreds, apiUrl: storageSDK._defaultTvmApiUrl }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureTVMResponse) +// }) +// test('when tvm url is specified', async () => { +// await storageSDK.init({ ow: fakeOWCreds }, { tvmApiUrl: fakeTvmApiUrl }) +// expect(TvmClient.prototype.getAzureBlobCredentials).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledWith({ ow: fakeOWCreds, apiUrl: fakeTvmApiUrl }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureTVMResponse) +// }) +// test('when tvm cache file is specified', async () => { +// await storageSDK.init({ ow: fakeOWCreds }, { tvmCacheFile: fakeTvmCacheFile }) +// expect(TvmClient.prototype.getAzureBlobCredentials).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledWith({ ow: fakeOWCreds, apiUrl: storageSDK._defaultTvmApiUrl, cacheFile: fakeTvmCacheFile }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureTVMResponse) +// }) +// test('when credentials are passed through env vars OW_XXXX', async () => { +// process.env['OW_AUTH'] = fakeOWCreds.auth +// process.env['OW_NAMESPACE'] = fakeOWCreds.namespace +// await storageSDK.init() +// expect(TvmClient.prototype.getAzureBlobCredentials).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledWith({ ow: fakeOWCreds, apiUrl: storageSDK._defaultTvmApiUrl }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureTVMResponse) +// }) +// test('when credentials are passed through env vars __OW_XXXX', async () => { +// process.env['__OW_AUTH'] = fakeOWCreds.auth +// process.env['__OW_NAMESPACE'] = fakeOWCreds.namespace +// await storageSDK.init() +// expect(TvmClient.prototype.getAzureBlobCredentials).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledWith({ ow: fakeOWCreds, apiUrl: storageSDK._defaultTvmApiUrl }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureTVMResponse) +// }) +// test('when __OW_AUTH is passed through env var and namespace through arg', async () => { +// process.env['__OW_AUTH'] = fakeOWCreds.auth +// await storageSDK.init({ ow: { namespace: fakeOWCreds.namespace } }) +// expect(TvmClient.prototype.getAzureBlobCredentials).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledTimes(1) +// expect(TvmClient).toHaveBeenCalledWith({ ow: fakeOWCreds, apiUrl: storageSDK._defaultTvmApiUrl }) +// expect(AzureStorage.init).toHaveBeenCalledTimes(1) +// expect(AzureStorage.init).toHaveBeenCalledWith(fakeAzureTVMResponse) +// }) +// test('when tvm rejects with a 401', async () => { +// TvmClient.prototype.getAzureBlobCredentials.mockRejectedValue({ statusCode: 401 }) +// await expect(storageSDK.init.bind(storageSDK, { ow: fakeOWCreds })).toThrowForbidden() +// }) +// test('when tvm rejects with a 403', async () => { +// TvmClient.prototype.getAzureBlobCredentials.mockRejectedValue({ statusCode: 403 }) +// await expect(storageSDK.init.bind(storageSDK, { ow: fakeOWCreds })).toThrowForbidden() +// }) +// test('when tvm rejects with an unhandled status code', async () => { +// TvmClient.prototype.getAzureBlobCredentials.mockRejectedValue({ statusCode: 444 }) +// await expect(storageSDK.init.bind(storageSDK, { ow: fakeOWCreds })).toThrowInternalWithStatus(444) +// }) +// test('when tvm rejects with no status code', async () => { +// TvmClient.prototype.getAzureBlobCredentials.mockRejectedValue(true) +// await expect(storageSDK.init.bind(storageSDK, { ow: fakeOWCreds })).toThrowInternal() +// }) +// }) +// }) diff --git a/test/jest.setup.js b/test/jest.setup.js new file mode 100644 index 0000000..ee48adf --- /dev/null +++ b/test/jest.setup.js @@ -0,0 +1,59 @@ +/* +Copyright 2019 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +governing permissions and limitations under the License. +*/ + +/* eslint-disable jsdoc/require-jsdoc */ +const { KVSError } = require('../lib/KeyValueStoreError') + +process.on('unhandledRejection', error => { + throw error +}) + +async function toThrowWithCodeAndMessageContains (received, code, words, checkErrorType = true) { + function checkErrorCode (e, code) { + if (!(e instanceof KVSError)) { + return { message: () => `expected error to be instanceof "KVSError", instead received "${e.constructor.name}" with message: "${e.message}"`, pass: false } + } + if (e.code !== code) { + return { message: () => `expected error code to be "${code}", instead received "${e.code}" with message: "${e.message}"`, pass: false } + } + } + function checkErrorMessageContains (message, words) { + message = message.toLowerCase() + if (typeof words === 'string') words = [words] + for (let i = 0; i < words.length; ++i) { + let a = words[i].toLowerCase() + if (message.indexOf(a) < 0) { + return { message: () => `expected error message "${message}" to contain "${a}"`, pass: false } + } + } + } + try { + await received() + } catch (e) { + if (checkErrorType) { + const res = checkErrorCode(e, code) + if (res) return res + } + const res = checkErrorMessageContains(e.message, words) + if (res) return res + return { pass: true } + } + return { message: () => 'function should have thrown', pass: false } +} +expect.extend({ + toThrowWithCodeAndMessageContains, + toThrowBadArgWithMessageContaining: (received, words, checkErrorType = true) => toThrowWithCodeAndMessageContains(received, KVSError.codes.BadArgument, words, checkErrorType), + toThrowForbidden: (received) => toThrowWithCodeAndMessageContains(received, KVSError.codes.Forbidden, ['forbidden', 'credentials']), + toThrowInternalWithStatus: (received, status) => toThrowWithCodeAndMessageContains(received, KVSError.codes.Internal, ['' + status, 'unknown']), + toThrowInternal: (received) => toThrowWithCodeAndMessageContains(received, KVSError.codes.Internal, ['unknown']), + toThrowNotImplemented: (received, methodName) => toThrowWithCodeAndMessageContains(received, KVSError.codes.NotImplemented, ['not implemented', methodName]) +})