conode 0.1.24 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -8
- package/build/index.cjs +23 -18
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +12 -11
- package/build/index.js +21 -16
- package/build/index.js.map +1 -1
- package/package.json +8 -6
- package/src/index.ts +27 -24
package/README.md
CHANGED
|
@@ -6,8 +6,6 @@
|
|
|
6
6
|
[![NPM version][npm-image]][npm-url]
|
|
7
7
|
[![Downloads][downloads-image]][npm-url]
|
|
8
8
|
[![Coverage Status][codecov-image]][codecov-url]
|
|
9
|
-
[![Maintainability][codeclimate-image]][codeclimate-url]
|
|
10
|
-
[![Snyk][snyk-image]][snyk-url]
|
|
11
9
|
|
|
12
10
|
## Table of Contents
|
|
13
11
|
|
|
@@ -70,14 +68,14 @@ auth.contextualize(jwt, action);
|
|
|
70
68
|
|
|
71
69
|
Class approach with decorator
|
|
72
70
|
```typescript
|
|
73
|
-
import { contextualize,
|
|
71
|
+
import { contextualize, Contextable } from '../index';
|
|
74
72
|
|
|
75
73
|
const action = () => {
|
|
76
74
|
const service = Service.getContext();
|
|
77
75
|
console.log(service.getSomething());
|
|
78
76
|
};
|
|
79
77
|
|
|
80
|
-
class Service extends
|
|
78
|
+
class Service extends Contextable() {
|
|
81
79
|
@contextualize
|
|
82
80
|
async run() {
|
|
83
81
|
await action();
|
|
@@ -104,8 +102,4 @@ Copyright (c) 2022-present Ivan Zakharchanka
|
|
|
104
102
|
[github-image]: https://github.com/3axap4eHko/conode/actions/workflows/build.yml/badge.svg
|
|
105
103
|
[codecov-url]: https://codecov.io/gh/3axap4eHko/conode
|
|
106
104
|
[codecov-image]: https://codecov.io/gh/3axap4eHko/conode/branch/master/graph/badge.svg?token=JZ8QCGH6PI
|
|
107
|
-
[codeclimate-url]: https://codeclimate.com/github/3axap4eHko/conode/maintainability
|
|
108
|
-
[codeclimate-image]: https://api.codeclimate.com/v1/badges/0ba20f27f6db2b0fec8c/maintainability
|
|
109
|
-
[snyk-url]: https://snyk.io/test/npm/conode/latest
|
|
110
|
-
[snyk-image]: https://img.shields.io/snyk/vulnerabilities/github/3axap4eHko/conode.svg?maxAge=43200
|
|
111
105
|
|
package/build/index.cjs
CHANGED
|
@@ -9,8 +9,8 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
-
|
|
13
|
-
return
|
|
12
|
+
Contextable: function() {
|
|
13
|
+
return Contextable;
|
|
14
14
|
},
|
|
15
15
|
contextualize: function() {
|
|
16
16
|
return contextualize;
|
|
@@ -23,37 +23,42 @@ const _nodeasync_hooks = require("node:async_hooks");
|
|
|
23
23
|
const createContext = ()=>{
|
|
24
24
|
const storage = new _nodeasync_hooks.AsyncLocalStorage();
|
|
25
25
|
return {
|
|
26
|
-
contextualize (
|
|
27
|
-
return storage.run(
|
|
26
|
+
contextualize (context, callback) {
|
|
27
|
+
return storage.run({
|
|
28
|
+
context
|
|
29
|
+
}, callback);
|
|
28
30
|
},
|
|
29
31
|
getContext () {
|
|
30
|
-
|
|
32
|
+
const store = storage.getStore();
|
|
33
|
+
if (!store) {
|
|
34
|
+
throw new Error('Context not initialized');
|
|
35
|
+
}
|
|
36
|
+
return store.context;
|
|
31
37
|
}
|
|
32
38
|
};
|
|
33
39
|
};
|
|
34
|
-
function
|
|
40
|
+
function Contextable(BaseClass = class {
|
|
35
41
|
}) {
|
|
36
42
|
const context = createContext();
|
|
37
43
|
return class Contextual extends BaseClass {
|
|
38
|
-
static
|
|
44
|
+
static getContext() {
|
|
39
45
|
return context.getContext();
|
|
40
46
|
}
|
|
41
|
-
|
|
47
|
+
constructor(...args){
|
|
48
|
+
super(...args);
|
|
49
|
+
}
|
|
50
|
+
contextualize(callback) {
|
|
42
51
|
return context.contextualize(this, callback);
|
|
43
52
|
}
|
|
44
53
|
};
|
|
45
54
|
}
|
|
46
|
-
function contextualize(
|
|
47
|
-
const
|
|
55
|
+
function contextualize(...args) {
|
|
56
|
+
const [, , descriptor] = args;
|
|
57
|
+
const property = descriptor?.value;
|
|
48
58
|
if (typeof property === 'function') {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return target.contextualize(()=>method.apply(null, args));
|
|
53
|
-
}
|
|
54
|
-
;
|
|
55
|
-
descriptor.value = contextualized;
|
|
56
|
-
}
|
|
59
|
+
descriptor.value = function(...args) {
|
|
60
|
+
return this.contextualize(()=>property.apply(this, args));
|
|
61
|
+
};
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
64
|
|
package/build/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Fn<T extends unknown[], R> = (...args: T) => R;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<{ context: T }>();\n return {\n contextualize<R>(context: T, callback: Fn<[], R>): R {\n return storage.run({ context }, callback);\n },\n getContext() {\n const store = storage.getStore();\n if (!store) {\n throw new Error('Context not initialized');\n }\n return store.context;\n }\n };\n};\n\ntype Constructor = new (...args: any) => any;\n\nexport interface Contextualized {\n contextualize<R>(callback: Fn<[], R>): Promise<R>;\n}\n\nexport function Contextable(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static getContext<C extends Constructor>(this: C) {\n return context.getContext() as InstanceType<C>;\n }\n constructor(...args: unknown[]) {\n super(...args);\n }\n\n contextualize<R>(callback: Fn<[], R>): R {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]) {\n const [, , descriptor] = args as [T, K, TypedPropertyDescriptor<T[K]>];\n const property = descriptor?.value;\n if (typeof property === 'function') {\n descriptor.value = function(this: Contextualized, ...args: unknown[]) {\n return this.contextualize(() => property.apply(this, args));\n } as T[K];\n }\n}\n\n"],"names":["Contextable","contextualize","createContext","storage","AsyncLocalStorage","context","callback","run","getContext","store","getStore","Error","BaseClass","Contextual","constructor","args","descriptor","property","value","apply"],"mappings":";;;;;;;;;;;IA0BgBA,WAAW;eAAXA;;IAgBAC,aAAa;eAAbA;;IAtCHC,aAAa;eAAbA;;;iCAJqB;AAI3B,MAAMA,gBAAgB;IAC3B,MAAMC,UAAU,IAAIC,kCAAiB;IACrC,OAAO;QACLH,eAAiBI,OAAU,EAAEC,QAAmB;YAC9C,OAAOH,QAAQI,GAAG,CAAC;gBAAEF;YAAQ,GAAGC;QAClC;QACAE;YACE,MAAMC,QAAQN,QAAQO,QAAQ;YAC9B,IAAI,CAACD,OAAO;gBACV,MAAM,IAAIE,MAAM;YAClB;YACA,OAAOF,MAAMJ,OAAO;QACtB;IACF;AACF;AAQO,SAASL,YAAYY,YAAyB;AAAQ,CAAC;IAC5D,MAAMP,UAAUH;IAChB,OAAO,MAAMW,mBAAmBD;QAC9B,OAAOJ,aAA2C;YAChD,OAAOH,QAAQG,UAAU;QAC3B;QACAM,YAAY,GAAGC,IAAe,CAAE;YAC9B,KAAK,IAAIA;QACX;QAEAd,cAAiBK,QAAmB,EAAK;YACvC,OAAOD,QAAQJ,aAAa,CAAC,IAAI,EAAEK;QACrC;IACF;AACF;AAEO,SAASL,cAA2D,GAAGc,IAAe;IAC3F,MAAM,KAAKC,WAAW,GAAGD;IACzB,MAAME,WAAWD,YAAYE;IAC7B,IAAI,OAAOD,aAAa,YAAY;QAClCD,WAAWE,KAAK,GAAG,SAA+B,GAAGH,IAAe;YAClE,OAAO,IAAI,CAACd,aAAa,CAAC,IAAMgB,SAASE,KAAK,CAAC,IAAI,EAAEJ;QACvD;IACF;AACF"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type Fn<T extends unknown[], R> = (...args: T) => R;
|
|
2
2
|
export declare const createContext: <T>() => {
|
|
3
|
-
contextualize(
|
|
4
|
-
getContext(): T
|
|
3
|
+
contextualize<R>(context: T, callback: Fn<[], R>): R;
|
|
4
|
+
getContext(): T;
|
|
5
5
|
};
|
|
6
|
-
type Constructor = new (...args:
|
|
7
|
-
export
|
|
6
|
+
type Constructor = new (...args: any) => any;
|
|
7
|
+
export interface Contextualized {
|
|
8
|
+
contextualize<R>(callback: Fn<[], R>): Promise<R>;
|
|
9
|
+
}
|
|
10
|
+
export declare function Contextable(BaseClass?: Constructor): {
|
|
8
11
|
new (...args: unknown[]): {
|
|
9
|
-
|
|
12
|
+
[x: string]: any;
|
|
13
|
+
contextualize<R>(callback: Fn<[], R>): R;
|
|
10
14
|
};
|
|
11
|
-
|
|
15
|
+
getContext<C extends Constructor>(this: C): InstanceType<C>;
|
|
12
16
|
};
|
|
13
|
-
export
|
|
14
|
-
contextualize(callback: Callback): Promise<any>;
|
|
15
|
-
}
|
|
16
|
-
export declare function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor): void;
|
|
17
|
+
export declare function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]): void;
|
|
17
18
|
export {};
|
package/build/index.js
CHANGED
|
@@ -2,37 +2,42 @@ import { AsyncLocalStorage } from 'node:async_hooks';
|
|
|
2
2
|
export const createContext = ()=>{
|
|
3
3
|
const storage = new AsyncLocalStorage();
|
|
4
4
|
return {
|
|
5
|
-
contextualize (
|
|
6
|
-
return storage.run(
|
|
5
|
+
contextualize (context, callback) {
|
|
6
|
+
return storage.run({
|
|
7
|
+
context
|
|
8
|
+
}, callback);
|
|
7
9
|
},
|
|
8
10
|
getContext () {
|
|
9
|
-
|
|
11
|
+
const store = storage.getStore();
|
|
12
|
+
if (!store) {
|
|
13
|
+
throw new Error('Context not initialized');
|
|
14
|
+
}
|
|
15
|
+
return store.context;
|
|
10
16
|
}
|
|
11
17
|
};
|
|
12
18
|
};
|
|
13
|
-
export function
|
|
19
|
+
export function Contextable(BaseClass = class {
|
|
14
20
|
}) {
|
|
15
21
|
const context = createContext();
|
|
16
22
|
return class Contextual extends BaseClass {
|
|
17
|
-
static
|
|
23
|
+
static getContext() {
|
|
18
24
|
return context.getContext();
|
|
19
25
|
}
|
|
20
|
-
|
|
26
|
+
constructor(...args){
|
|
27
|
+
super(...args);
|
|
28
|
+
}
|
|
29
|
+
contextualize(callback) {
|
|
21
30
|
return context.contextualize(this, callback);
|
|
22
31
|
}
|
|
23
32
|
};
|
|
24
33
|
}
|
|
25
|
-
export function contextualize(
|
|
26
|
-
const
|
|
34
|
+
export function contextualize(...args) {
|
|
35
|
+
const [, , descriptor] = args;
|
|
36
|
+
const property = descriptor?.value;
|
|
27
37
|
if (typeof property === 'function') {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return target.contextualize(()=>method.apply(null, args));
|
|
32
|
-
}
|
|
33
|
-
;
|
|
34
|
-
descriptor.value = contextualized;
|
|
35
|
-
}
|
|
38
|
+
descriptor.value = function(...args) {
|
|
39
|
+
return this.contextualize(()=>property.apply(this, args));
|
|
40
|
+
};
|
|
36
41
|
}
|
|
37
42
|
}
|
|
38
43
|
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Fn<T extends unknown[], R> = (...args: T) => R;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<{ context: T }>();\n return {\n contextualize<R>(context: T, callback: Fn<[], R>): R {\n return storage.run({ context }, callback);\n },\n getContext() {\n const store = storage.getStore();\n if (!store) {\n throw new Error('Context not initialized');\n }\n return store.context;\n }\n };\n};\n\ntype Constructor = new (...args: any) => any;\n\nexport interface Contextualized {\n contextualize<R>(callback: Fn<[], R>): Promise<R>;\n}\n\nexport function Contextable(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static getContext<C extends Constructor>(this: C) {\n return context.getContext() as InstanceType<C>;\n }\n constructor(...args: unknown[]) {\n super(...args);\n }\n\n contextualize<R>(callback: Fn<[], R>): R {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]) {\n const [, , descriptor] = args as [T, K, TypedPropertyDescriptor<T[K]>];\n const property = descriptor?.value;\n if (typeof property === 'function') {\n descriptor.value = function(this: Contextualized, ...args: unknown[]) {\n return this.contextualize(() => property.apply(this, args));\n } as T[K];\n }\n}\n\n"],"names":["AsyncLocalStorage","createContext","storage","contextualize","context","callback","run","getContext","store","getStore","Error","Contextable","BaseClass","Contextual","constructor","args","descriptor","property","value","apply"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,mBAAmB;AAIrD,OAAO,MAAMC,gBAAgB;IAC3B,MAAMC,UAAU,IAAIF;IACpB,OAAO;QACLG,eAAiBC,OAAU,EAAEC,QAAmB;YAC9C,OAAOH,QAAQI,GAAG,CAAC;gBAAEF;YAAQ,GAAGC;QAClC;QACAE;YACE,MAAMC,QAAQN,QAAQO,QAAQ;YAC9B,IAAI,CAACD,OAAO;gBACV,MAAM,IAAIE,MAAM;YAClB;YACA,OAAOF,MAAMJ,OAAO;QACtB;IACF;AACF,EAAE;AAQF,OAAO,SAASO,YAAYC,YAAyB;AAAQ,CAAC;IAC5D,MAAMR,UAAUH;IAChB,OAAO,MAAMY,mBAAmBD;QAC9B,OAAOL,aAA2C;YAChD,OAAOH,QAAQG,UAAU;QAC3B;QACAO,YAAY,GAAGC,IAAe,CAAE;YAC9B,KAAK,IAAIA;QACX;QAEAZ,cAAiBE,QAAmB,EAAK;YACvC,OAAOD,QAAQD,aAAa,CAAC,IAAI,EAAEE;QACrC;IACF;AACF;AAEA,OAAO,SAASF,cAA2D,GAAGY,IAAe;IAC3F,MAAM,KAAKC,WAAW,GAAGD;IACzB,MAAME,WAAWD,YAAYE;IAC7B,IAAI,OAAOD,aAAa,YAAY;QAClCD,WAAWE,KAAK,GAAG,SAA+B,GAAGH,IAAe;YAClE,OAAO,IAAI,CAACZ,aAAa,CAAC,IAAMc,SAASE,KAAK,CAAC,IAAI,EAAEJ;QACvD;IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "conode",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "0-Deps, simple and fast context library for NodeJS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "build/index.cjs",
|
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
],
|
|
12
12
|
"module": "build/index.js",
|
|
13
13
|
"sideEffects": false,
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "rm -rf build && inop src build -i __tests__ -i *.tmp.ts && tsc --declaration --emitDeclarationOnly",
|
|
16
|
+
"test": "jest",
|
|
17
|
+
"test:build": "node src/__tests__/index.cjs && node src/__tests__/index.mjs"
|
|
18
|
+
},
|
|
14
19
|
"repository": {
|
|
15
20
|
"type": "git",
|
|
16
21
|
"url": "git+https://github.com/3axap4eHko/conode.git"
|
|
@@ -39,8 +44,5 @@
|
|
|
39
44
|
"jest": "^29.7.0",
|
|
40
45
|
"typescript": "^5.8.3"
|
|
41
46
|
},
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
"test": "jest"
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
+
"packageManager": "pnpm@10.11.0"
|
|
48
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,49 +1,52 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type Fn<T extends unknown[], R> = (...args: T) => R;
|
|
4
4
|
|
|
5
5
|
export const createContext = <T>() => {
|
|
6
|
-
const storage = new AsyncLocalStorage<T>();
|
|
7
|
-
|
|
6
|
+
const storage = new AsyncLocalStorage<{ context: T }>();
|
|
8
7
|
return {
|
|
9
|
-
contextualize(
|
|
10
|
-
return storage.run(
|
|
8
|
+
contextualize<R>(context: T, callback: Fn<[], R>): R {
|
|
9
|
+
return storage.run({ context }, callback);
|
|
11
10
|
},
|
|
12
11
|
getContext() {
|
|
13
|
-
|
|
12
|
+
const store = storage.getStore();
|
|
13
|
+
if (!store) {
|
|
14
|
+
throw new Error('Context not initialized');
|
|
15
|
+
}
|
|
16
|
+
return store.context;
|
|
14
17
|
}
|
|
15
18
|
};
|
|
16
19
|
};
|
|
17
20
|
|
|
18
|
-
type Constructor = new (...args:
|
|
21
|
+
type Constructor = new (...args: any) => any;
|
|
19
22
|
|
|
20
|
-
export
|
|
23
|
+
export interface Contextualized {
|
|
24
|
+
contextualize<R>(callback: Fn<[], R>): Promise<R>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function Contextable(BaseClass: Constructor = class { }) {
|
|
21
28
|
const context = createContext();
|
|
22
29
|
return class Contextual extends BaseClass {
|
|
23
|
-
static
|
|
24
|
-
return context.getContext()
|
|
30
|
+
static getContext<C extends Constructor>(this: C) {
|
|
31
|
+
return context.getContext() as InstanceType<C>;
|
|
32
|
+
}
|
|
33
|
+
constructor(...args: unknown[]) {
|
|
34
|
+
super(...args);
|
|
25
35
|
}
|
|
26
36
|
|
|
27
|
-
|
|
37
|
+
contextualize<R>(callback: Fn<[], R>): R {
|
|
28
38
|
return context.contextualize(this, callback);
|
|
29
39
|
}
|
|
30
40
|
};
|
|
31
41
|
}
|
|
32
42
|
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
export function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {
|
|
38
|
-
const property = descriptor.value;
|
|
43
|
+
export function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]) {
|
|
44
|
+
const [, , descriptor] = args as [T, K, TypedPropertyDescriptor<T[K]>];
|
|
45
|
+
const property = descriptor?.value;
|
|
39
46
|
if (typeof property === 'function') {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return target.contextualize(() => method.apply(null, args));
|
|
44
|
-
};
|
|
45
|
-
descriptor.value = contextualized as T[K];
|
|
46
|
-
}
|
|
47
|
+
descriptor.value = function(this: Contextualized, ...args: unknown[]) {
|
|
48
|
+
return this.contextualize(() => property.apply(this, args));
|
|
49
|
+
} as T[K];
|
|
47
50
|
}
|
|
48
51
|
}
|
|
49
52
|
|