nemo-lib v0.0.1
NEMO Library
Quick Start
install
npm i -S @nowtilus/nemo-lib
usage
const {storageAdapter, nemoResources} = require('nemo-lib')
// Working with Nemo Resources
nemoResources.message.create(messageObject)
// Using the storage adapter
const storage = storageAdapter.create({
type: 'azure',
account: 'my-account-name',
key: process.env.KEY
})
storage.list() // lists all files in the blob storage
Use-Cases
- Every NEMO service provides an API to create, read, update and delete resources to simplify the interaction between services the nemo-lib can be used in a unified manner.
- From time to time there are services that need to implement the same functionality. To reduce code dupliocation the nemo-lib is a good place to store those functions.
NEMO Service Resources
The followoing resources are provided via nemo-lib
- messages.create()
Fire and forget
As fire and forget implementation you can simply call the message. Any error or non 2xx response ocuring while making the request wiill be logged to the console.
messages.create(message)
Waiting for the feedback
The methods are promise based and will throw any error or non-2xx respnse.
messages.create(message)
.then(result => console.log(`The created message: ${JSON.stringify(result)}`))
.catch(e => console.error(`An Error ocured: ${JSON.stringify(e)}`))
or async await style
(async () => {
try {
const result = messages.create(message)
console.log(`The created message: ${JSON.stringify(result)}`)
} catch (e) {
console.error(`An Error ocured: ${JSON.stringify(e)}`)
}
})()
Options
To control the bahaviour of the request in more detail you can specify options. Those are as unified as possible. E.g. for every resource.read method you'll find a "limit" and "offset" option.
NEMO Storage Adapter Module
The storage adapter provides a unified streaming api to move files easely between different storages. E.g. Download a file from a url and pipe it to an internal Azure or AWS storage.
Features:
- Internal complexity of using different storages is completely abstracted away
- No need to have enough disc space thanks to streaming
- The library can be used to build CLI tools that support unix pipes (e.g.
my-cli | grep "search-text" | sort - r
)
Supported Storages
Available:
- File System (local hdd or any mounted storage)
- Azure Blob Storage
- HTTP (read only)
Planned:
- AWS S3
Storages have the following actions
- list() // Lists all files in a provided directory
- getDetails() // Provides detailed information related to one file
- writeSourceToStream() // Generates a data stream from a file
- createWriteStream() // Geneate a write stream to a place on the storage
- saveStreamToTarget() // Save a stream to a place at the storage
- delete() // Deletes a file or directory on the storage
Examples
Actions on one storage
Note:
All read processes have the following success response (not all fields are provided by every storage - fields that are not available are set to null)
{
name: 'maxresdefault.jpg',
size: '75477',
modifiedAt: 'Mon, 18 Dec 2017 12:18:44 GMT',
createdAt: 'Mon, 18 Dec 2017 12:18:44 GMT'
}
All streaming and write processes will resolve with an success message or reject with an error
List files of a storage
const localAdapter = require('nemo-lib').storageAdapter.create({
type: 'local',
root: '.'
})
localAdapter
.list('examples')
.then(result => {
console.log(result.map(r => r.name)) // --> [ 'http-to-azure.js', 'local-adapter.js' ]
})
.catch(e => console.error(e))
Get file details
const httpAdapter = require('nemo-lib').storageAdapter.create('http')
httpAdapter
.getDetails('https://www.elastic.co/assets/blt3541c4519daa9d09/maxresdefault.jpg')
.then(result => {
console.log(result)
/*
{
name: 'maxresdefault.jpg',
size: '75477',
modifiedAt: 'Mon, 18 Dec 2017 12:18:44 GMT',
createdAt: 'Mon, 18 Dec 2017 12:18:44 GMT'
}
*/
})
.catch(e => console.error(e))
Delete a blob on an Azure storage
const httpAdapter = require('nemo-lib').storageAdapter.create({
type: 'azure',
account: 'nowtilusingeststaging',
key: process.env.KEY
})
azureAdapter.delete('quality-assurance/nowtilus.png')
.then(result => {
console.log(result) // --> 'Deleted quality-assurance/nowtilus.png'
})
.catch(e => console.error(e))
More examples for single storage actions
- Local storage:
./examples/storage-adapter/local-adapter.js
- Azure storage:
./examples/storage-adapter/azure-adapter.js
Actions between storages
Stream from http to azure storage
const AdapterFactory = require('nemo-lib').storageAdapter
// instantiate an adapter for http
const httpAdapter = AdapterFactory.create('http')
// instantiate an adapter for azure-blob
const azureAdapter = AdapterFactory.create({
type: 'azure',
account: 'myteststorage',
key: process.env.KEY
})
// define the https source
const source = 'https://static.wixstatic.com/media/ef8b8b_910bcc934fa740c4a020b57b54686b35~mv2.png/v1/fill/w_231,h_33,al_c,q_90/nowtilus-sRGB-XL.webp'
// create a write stream to the target
const writeStream = azureAdapter.createWriteStream(
'quality-assurance/nowtilusy.png'
)
// generate a source stream and pipe it to the target stream
httpAdapter
.writeSourceToStream(source, writeStream)
.then(r => console.log(`Stream finished: ${JSON.stringify(r)}`))
.catch(e => console.log(e))
More examples for stream actions
- Copy from HTTP Url to Azure Blob Storage:
./examples/storage-adapter/http-to-azure.js
Contribution Guidance
- crutial to success
- always (close to) 100% test coverage
- stable
5 years ago