Socket
Socket
Sign inDemoInstall

@nodebrick/nodebrick-core

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nodebrick/nodebrick-core - npm Package Compare versions

Comparing version 0.0.27 to 0.0.28

4

package.json
{
"name": "@nodebrick/nodebrick-core",
"version": "0.0.27",
"version": "0.0.28",
"description": "Core of Nodebrick",

@@ -37,3 +37,3 @@ "publishConfig": {

"dependencies": {
"@nodebrick/nodebrick-logger": "0.0.27",
"@nodebrick/nodebrick-logger": "0.0.28",
"@types/cls-hooked": "4.3.0",

@@ -40,0 +40,0 @@ "@types/config": "0.0.36",

@@ -17,3 +17,3 @@ # nodebrick-core

### Module Structure
Have a look at the existing modules, it's self explenatory.
_non exhaustive list_

@@ -28,3 +28,3 @@ Few things to note:

* `<ModuleNameBindings>.ts`
Defines the Inversify bindings for this module are done
Defines the Inversify bindings for this module
* `models`

@@ -91,13 +91,142 @@ Folder storing all our models, i.e our business logic data

```
As you can see you have multiple scope for those
As you can see bindings can have multiples scopes, Transient, Singleton and Request.
Please see [InversifyJS documentation](https://github.com/inversify/InversifyJS/tree/master/wiki).
### Context
### Contexts
_A context can be defined as all the relevant information that a developer needs to complete a task._
Nodebrick Core uses [cls-hooked](https://github.com/jeff-lewis/cls-hooked#readme) internally.
We invite you to read the documentation.
NodebrickCore with the help of [cls-hooked](https://github.com/jeff-lewis/cls-hooked#readme) provides you with tools to create a context
and attach any
* create a context
Here is an excerpt of the documentation explaining the core of this library and how it is helping
> Continuation-local storage works like thread-local storage in threaded programming, but is based on chains of Node-style callbacks instead of threads. The standard Node convention of functions calling functions is very similar to something called "continuation-passing style" in functional programming, and the name comes from the way this module allows you to set and get values that are scoped to the lifetime of these chains of function calls
>
> When you set values in continuation-local storage, those values are accessible until all functions called from the original function – synchronously or asynchronously – have finished executing. This includes callbacks passed to process.nextTick and the timer functions (setImmediate, setTimeout, and setInterval), as well as callbacks passed to asynchronous functions that call native functions (such as those exported from the fs, dns, zlib and crypto modules).
>
> A simple rule of thumb is anywhere where you want to have threaded scoped variables should now use continuation-local storage. This API is designed to allow you extend the scope of a variable across a sequence of function calls, but with values specific to each sequence of calls.
> Values are grouped into namespaces, created with createNamespace(). Sets of function calls are grouped together by calling them within the function passed to .run() on the namespace object. Calls to .run() can be nested, and each nested context this creates has its own copy of the set of values from the parent context. When a function is making multiple asynchronous calls, this allows each child call to get, set, and pass along its own context without overwriting the parent's.
This is a diagram explaining the above
![context diagram](./documentation/assets/contexts_explained.svg)
And this is the cls-hooked documention explaining it with code
>
> A simple, annotated example of how this nesting behaves:
>
> ```javascript
> var createNamespace = require('cls-hooked').createNamespace;
>
> var writer = createNamespace('writer');
> writer.run(function () {
> writer.set('value', 0);
>
> requestHandler();
> });
>
> function requestHandler() {
> writer.run(function(outer) {
> // writer.get('value') returns 0
> // outer.value is 0
> writer.set('value', 1);
> // writer.get('value') returns 1
> // outer.value is 1
> process.nextTick(function() {
> // writer.get('value') returns 1
> // outer.value is 1
> writer.run(function(inner) {
> // writer.get('value') returns 1
> // outer.value is 1
> // inner.value is 1
> writer.set('value', 2);
> // writer.get('value') returns 2
> // outer.value is 1
> // inner.value is 2
> });
> });
> });
>
> setTimeout(function() {
> // runs with the default context, because nested contexts have ended
> console.log(writer.get('value')); // prints 0
> }, 1000);
> }
>
> ```
This also helps segregating the stream of processes (think for instance multiple call to an API, we will create a context when we get a request isolating any models in the context)
#### Usage
Nodebrick Core provides you with tools to:
* create a context
* attach typed value to the context
* retrieve specific values from the context
##### Creating and using context
Please see the following example
```typescript
abstract class ISomeClass {}
class IATypedValue {
public someProperty: string;
}
abstract class IATypedValueContext extends IContext<IATypedValue> {}
class SomeClass extends ISomeClass implements ISomeClass
{
private readonly _applicationContext: IApplicationContextService;
private readonly _logger: INodebrickLoggerService;
// dependency injection
public constructor(
applicationContext: IApplicationContextService
)
{
super();
this._applicationContext = applicationContext;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async someMethod(): Promise<void>
{
// this creates a new context copying the values from the parent (if any)
this._applicationContext.session.run(() =>
{
// set a value in the context
this._applicationContext.set(IATypedValueContext, {someProperty: "it works"});
});
}
}
abstract class ISomeOtherClass {}
class SomeOtherClass extends ISomeOtherClass implements ISomeOtherClass
{
private readonly _applicationContext: IApplicationContextService;
// dependency injection
public constructor(
applicationContext: IApplicationContextService,
logger: INodebrickLoggerService,
)
{
super();
this._applicationContext = applicationContext;
this._logger = logger;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async someMethod(): Promise<void>
{
// this creates a new context copying the values from the parent (if any)
const aTypedValue: IATypedValue = this._applicationContext.get(IATypedValueContext);
// log "it works"
console.log(aTypedValue.someProperty)
}
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc