diff --git a/npmextra.json b/npmextra.json index 293e39a..4060ced 100644 --- a/npmextra.json +++ b/npmextra.json @@ -11,10 +11,21 @@ "gitrepo": "smartcls", "shortDescription": "continuation-local-storage using native AsyncLocalStorage", "npmPackagename": "@push.rocks/smartcls", - "license": "MIT" + "license": "MIT", + "description": "Provides a wrapper for native AsyncLocalStorage to manage continuation-local storage.", + "keywords": [ + "async local storage", + "continuation-local storage", + "context management", + "async_hooks", + "node.js", + "typescript", + "asynchronous storage", + "thread local storage" + ] } }, - "tsdocs": { + "tsdoc": { "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n" } } \ No newline at end of file diff --git a/package.json b/package.json index 4984972..b7a7735 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@push.rocks/smartcls", "version": "1.0.12", - "description": "continuation-local-storage using native AsyncLocalStorage", + "description": "Provides a wrapper for native AsyncLocalStorage to manage continuation-local storage.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "author": "Lossless GmbH", @@ -34,5 +34,15 @@ "cli.js", "npmextra.json", "readme.md" + ], + "keywords": [ + "async local storage", + "continuation-local storage", + "context management", + "async_hooks", + "node.js", + "typescript", + "asynchronous storage", + "thread local storage" ] } \ No newline at end of file diff --git a/readme.hints.md b/readme.hints.md new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/readme.hints.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/readme.md b/readme.md index 689d384..f002308 100644 --- a/readme.md +++ b/readme.md @@ -1,39 +1,144 @@ -# @pushrocks/smartcls +# @push.rocks/smartcls + continuation-local-storage using native AsyncLocalStorage -## Availabililty and Links -* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartcls) -* [gitlab.com (source)](https://gitlab.com/pushrocks/smartcls) -* [github.com (source mirror)](https://github.com/pushrocks/smartcls) -* [docs (typedoc)](https://pushrocks.gitlab.io/smartcls/) +## Install -## Status for master +To install `@push.rocks/smartcls`, use the following command with npm: -Status Category | Status Badge --- | -- -GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartcls/badges/master/pipeline.svg)](https://lossless.cloud) -GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartcls/badges/master/coverage.svg)](https://lossless.cloud) -npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartcls)](https://lossless.cloud) -Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartcls)](https://lossless.cloud) -TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud) -node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) -Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud) -PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartcls)](https://lossless.cloud) -PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartcls)](https://lossless.cloud) -BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartcls)](https://lossless.cloud) -Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud) +```sh +npm install @push.rocks/smartcls +``` + +or if you prefer using Yarn: + +```sh +yarn add @push.rocks/smartcls +``` ## Usage -Use TypeScript for best in class instellisense. +`@push.rocks/smartcls` simplifies the usage of native AsyncLocalStorage in Node.js, allowing for a more convenient approach to work with continuation-local storage. This can be especially handy in scenarios where context passing through asynchronous calls is essential, such as in web servers or complex application flows that involve async operations. -## Contribution +### Basic Setup and Running Context -We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) +To start using `@push.rocks/smartcls`, you first need to import it and create an instance of `SmartCls`. -For further information read the linked docs at the top of this readme. +```typescript +import { SmartCls } from '@push.rocks/smartcls'; -> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh) -| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) +const mySmartCls = new SmartCls(); +``` -[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com) +Once you have an instance, you can utilize the `run` method to establish a new context. Within this context, you can set and get values that are scoped to the lifetime of the context. + +```typescript +mySmartCls.run(() => { + mySmartCls.set('key', 'value'); + + // later in the async flow + console.log(mySmartCls.get('key')); // Outputs: 'value' +}); +``` + +### Asynchronous Example + +The power of `SmartCls` becomes evident when working with asynchronous operations. Values set in a specific context are accessible throughout the async flow initiated within that context. + +```typescript +import { SmartCls } from '@push.rocks/smartcls'; + +const mySmartCls = new SmartCls(); + +mySmartCls.run(async () => { + mySmartCls.set('asyncKey', 'asyncValue'); + + await someAsyncFunction(); + console.log(mySmartCls.get('asyncKey')); // Outputs: 'asyncValue', even after async operations +}); +``` + +Ensure all asynchronous operations initiated in the context are awaited or properly handled to maintain the context's integrity. + +### Nested Contexts + +`@push.rocks/smartcls` supports nested contexts, allowing you to create sub-contexts within a main context. This can be useful for overriding values or isolating sections of your async flow. + +```typescript +import { SmartCls } from '@push.rocks/smartcls'; + +const mySmartCls = new SmartCls(); + +mySmartCls.run(() => { + mySmartCls.set('level', 'top'); + + mySmartCls.run(() => { + mySmartCls.set('level', 'nested'); + console.log(mySmartCls.get('level')); // Outputs: 'nested' + }); + + console.log(mySmartCls.get('level')); // Outputs: 'top', reverting back to the parent context +}); +``` + +### Working with Express or Similar Frameworks + +In a web server scenario using Express or a similar framework, you can integrate `SmartCls` to track request-specific data across asynchronous operations without explicitly passing the request object. + +```typescript +import express from 'express'; +import { SmartCls } from '@push.rocks/smartcls'; + +const app = express(); +const mySmartCls = new SmartCls(); + +app.use((req, res, next) => { + mySmartCls.run(() => { + mySmartCls.set('requestId', req.header('X-Request-Id') || Math.random().toString()); + next(); + }); +}); + +app.get('/', async (req, res) => { + // Simulate an async operation + await new Promise(resolve => setTimeout(resolve, 100)); + + // Access the requestId set at the beginning of the request + res.send(`Request ID: ${mySmartCls.get('requestId')}`); +}); + +app.listen(3000, () => { + console.log('Server running on port 3000'); +}); +``` + +This example demonstrates how to maintain a unique `requestId` for logging or tracking purposes across asynchronous operations within a single request-response cycle. + +### Important Considerations + +- Ensure that all async operations in a context are awaited to prevent context leakage. +- Be mindful of memory usage when storing large objects in the context, as each context retains its values until completion. +- `SmartCls` leverages Node.js's native `AsyncLocalStorage`, so it's dependent on the Node.js version supporting this feature. + +### Conclusion + +`@push.rocks/smartcls` offers a straightforward and efficient approach to managing continuation-local storage in Node.js applications, simplifying context management across asynchronous operations. + +## License and Legal Information + +This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. + +**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. + +### Trademarks + +This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH. + +### Company Information + +Task Venture Capital GmbH +Registered at District court Bremen HRB 35230 HB, Germany + +For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. + +By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.