conode 0.1.22 → 0.1.24

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # CoNode
2
2
 
3
- 0-Deps, simple and fast context library for NodeJS
3
+ 0-deps async context management for Node.js
4
4
 
5
5
  [![Build Status][github-image]][github-url]
6
6
  [![NPM version][npm-image]][npm-url]
@@ -18,9 +18,10 @@
18
18
 
19
19
  ## Features
20
20
 
21
- - Provides useful wrappers of [AsyncLocalStorage](https://nodejs.org/api/async_context.html)
22
- - Supports TypeScript typings
23
- - Supports CommonJS and ES Module systems
21
+ - Zero dependencies
22
+ - Lightweight wrappers around [AsyncLocalStorage](https://nodejs.org/api/async_hooks.html)
23
+ - Full TypeScript support
24
+ - Works in CommonJS and ES Module environments
24
25
 
25
26
  ## Installing
26
27
 
@@ -38,6 +39,22 @@ yarn add conode
38
39
 
39
40
  ## Examples
40
41
 
42
+ Quick start
43
+ ```typescript
44
+ import { createContext } from 'conode';
45
+
46
+ interface RequestContext { userId: string }
47
+
48
+ const reqCtx = createContext<RequestContext>();
49
+
50
+ async function handleRequest() {
51
+ console.log(reqCtx.getContext()?.userId);
52
+ }
53
+
54
+ await reqCtx.contextualize({ userId: 'abc123' }, handleRequest);
55
+ // Logs: 'abc123'
56
+ ```
57
+
41
58
  Functional approach
42
59
  ```typescript
43
60
  import { createContext } from '../index';
@@ -51,7 +68,7 @@ const action = () => {
51
68
  auth.contextualize(jwt, action);
52
69
  ```
53
70
 
54
- Class approach
71
+ Class approach with decorator
55
72
  ```typescript
56
73
  import { contextualize, Context } from '../index';
57
74
 
@@ -83,8 +100,8 @@ Copyright (c) 2022-present Ivan Zakharchanka
83
100
  [npm-url]: https://www.npmjs.com/package/conode
84
101
  [downloads-image]: https://img.shields.io/npm/dw/conode.svg?maxAge=43200
85
102
  [npm-image]: https://img.shields.io/npm/v/conode.svg?maxAge=43200
86
- [github-url]: https://github.com/3axap4eHko/conode/actions/workflows/cicd.yml
87
- [github-image]: https://github.com/3axap4eHko/conode/actions/workflows/cicd.yml/badge.svg
103
+ [github-url]: https://github.com/3axap4eHko/conode/actions/workflows/build.yml
104
+ [github-image]: https://github.com/3axap4eHko/conode/actions/workflows/build.yml/badge.svg
88
105
  [codecov-url]: https://codecov.io/gh/3axap4eHko/conode
89
106
  [codecov-image]: https://codecov.io/gh/3axap4eHko/conode/branch/master/graph/badge.svg?token=JZ8QCGH6PI
90
107
  [codeclimate-url]: https://codeclimate.com/github/3axap4eHko/conode/maintainability
package/build/index.cjs CHANGED
@@ -1,70 +1,60 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
9
  });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.contextualize = exports.Context = exports.createContext = void 0;
13
- const async_hooks_1 = require("async_hooks");
14
- const createContext = () => {
15
- const storage = new async_hooks_1.AsyncLocalStorage();
10
+ }
11
+ _export(exports, {
12
+ Context: function() {
13
+ return Context;
14
+ },
15
+ contextualize: function() {
16
+ return contextualize;
17
+ },
18
+ createContext: function() {
19
+ return createContext;
20
+ }
21
+ });
22
+ const _nodeasync_hooks = require("node:async_hooks");
23
+ const createContext = ()=>{
24
+ const storage = new _nodeasync_hooks.AsyncLocalStorage();
16
25
  return {
17
- contextualize(value, callback) {
18
- return new Promise((resolve, reject) => {
19
- storage.run(value, () => __awaiter(this, void 0, void 0, function* () {
20
- try {
21
- yield callback();
22
- resolve();
23
- }
24
- catch (e) {
25
- reject(e);
26
- }
27
- }));
28
- });
26
+ contextualize (value, callback) {
27
+ return storage.run(value, callback);
29
28
  },
30
- getContext() {
29
+ getContext () {
31
30
  return storage.getStore();
32
31
  }
33
32
  };
34
33
  };
35
- exports.createContext = createContext;
36
- function Context(Base) {
37
- const context = (0, exports.createContext)();
38
- return Base
39
- ? class Contextual extends Base {
40
- static get context() {
41
- return context.getContext();
42
- }
43
- contextualize(callback) {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- return context.contextualize(this, callback);
46
- });
47
- }
34
+ function Context(BaseClass = class {
35
+ }) {
36
+ const context = createContext();
37
+ return class Contextual extends BaseClass {
38
+ static get context() {
39
+ return context.getContext();
48
40
  }
49
- : class Contextual {
50
- static get context() {
51
- return context.getContext();
52
- }
53
- contextualize(callback) {
54
- return __awaiter(this, void 0, void 0, function* () {
55
- return context.contextualize(this, callback);
56
- });
57
- }
58
- };
41
+ async contextualize(callback) {
42
+ return context.contextualize(this, callback);
43
+ }
44
+ };
59
45
  }
60
- exports.Context = Context;
61
46
  function contextualize(target, _, descriptor) {
62
- const method = descriptor.value;
63
- function contextualized() {
64
- return target.contextualize(() => method.apply(this, arguments));
47
+ const property = descriptor.value;
48
+ if (typeof property === 'function') {
49
+ const method = property?.bind(target);
50
+ if (method) {
51
+ function contextualized(...args) {
52
+ return target.contextualize(()=>method.apply(null, args));
53
+ }
54
+ ;
55
+ descriptor.value = contextualized;
56
+ }
65
57
  }
66
- ;
67
- descriptor.value = contextualized;
68
58
  }
69
- exports.contextualize = contextualize;
70
- //# sourceMappingURL=index.cjs.map
59
+
60
+ //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAAgD;AAIzC,MAAM,aAAa,GAAG,GAAM,EAAE;IACnC,MAAM,OAAO,GAAG,IAAI,+BAAiB,EAAK,CAAC;IAE3C,OAAO;QACL,aAAa,CAAC,KAAQ,EAAE,QAAkB;YACxC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBAC3C,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,GAAS,EAAE;oBAC5B,IAAI;wBACF,MAAM,QAAQ,EAAE,CAAC;wBACjB,OAAO,EAAE,CAAC;qBACX;oBAAC,OAAO,CAAC,EAAE;wBACV,MAAM,CAAC,CAAC,CAAC,CAAC;qBACX;gBACH,CAAC,CAAA,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;QACD,UAAU;YACR,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC;QAC5B,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AApBW,QAAA,aAAa,iBAoBxB;AAIF,SAAgB,OAAO,CAAC,IAAkB;IACxC,MAAM,OAAO,GAAG,IAAA,qBAAa,GAAE,CAAC;IAEhC,OAAO,IAAI;QACT,CAAC,CAAC,MAAM,UAAW,SAAQ,IAAI;YAC7B,MAAM,KAAK,OAAO;gBAChB,OAAO,OAAO,CAAC,UAAU,EAAE,CAAC;YAC9B,CAAC;YAEK,aAAa,CAAC,QAAkB;;oBACpC,OAAO,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;gBAC/C,CAAC;aAAA;SACF;QACD,CAAC,CAAC,MAAM,UAAU;YAChB,MAAM,KAAK,OAAO;gBAChB,OAAO,OAAO,CAAC,UAAU,EAAE,CAAC;YAC9B,CAAC;YAEK,aAAa,CAAC,QAAkB;;oBACpC,OAAO,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;gBAC/C,CAAC;aAAA;SACF,CAAC;AACN,CAAC;AAtBD,0BAsBC;AAMD,SAAgB,aAAa,CAA2B,MAAS,EAAE,CAAS,EAAE,UAA6C;IACzH,MAAM,MAAM,GAAG,UAAU,CAAC,KAAM,CAAC;IACjC,SAAS,cAAc;QACrB,OAAO,MAAM,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;IACnE,CAAC;IAAA,CAAC;IACF,UAAU,CAAC,KAAK,GAAG,cAAqB,CAAC;AAC3C,CAAC;AAND,sCAMC"}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<T>();\n\n return {\n contextualize(value: T, callback: Callback) {\n return storage.run(value, callback);\n },\n getContext() {\n return storage.getStore();\n }\n };\n};\n\ntype Constructor = new (...args: unknown[]) => object;\n\nexport function Context(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static get context() {\n return context.getContext();\n }\n\n async contextualize(callback: Callback) {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport interface Contextualized {\n contextualize(callback: Callback): Promise<any>;\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {\n const property = descriptor.value;\n if (typeof property === 'function') {\n const method = property?.bind(target);\n if (method) {\n function contextualized<A extends unknown[]>(...args: A) {\n return target.contextualize(() => method.apply(null, args));\n };\n descriptor.value = contextualized as T[K];\n }\n }\n}\n\n"],"names":["Context","contextualize","createContext","storage","AsyncLocalStorage","value","callback","run","getContext","getStore","BaseClass","context","Contextual","target","_","descriptor","property","method","bind","contextualized","args","apply"],"mappings":";;;;;;;;;;;IAmBgBA,OAAO;eAAPA;;IAiBAC,aAAa;eAAbA;;IAhCHC,aAAa;eAAbA;;;iCAJqB;AAI3B,MAAMA,gBAAgB;IAC3B,MAAMC,UAAU,IAAIC,kCAAiB;IAErC,OAAO;QACLH,eAAcI,KAAQ,EAAEC,QAAkB;YACxC,OAAOH,QAAQI,GAAG,CAACF,OAAOC;QAC5B;QACAE;YACE,OAAOL,QAAQM,QAAQ;QACzB;IACF;AACF;AAIO,SAAST,QAAQU,YAAyB;AAAQ,CAAC;IACxD,MAAMC,UAAUT;IAChB,OAAO,MAAMU,mBAAmBF;QAC9B,WAAWC,UAAU;YACnB,OAAOA,QAAQH,UAAU;QAC3B;QAEA,MAAMP,cAAcK,QAAkB,EAAE;YACtC,OAAOK,QAAQV,aAAa,CAAC,IAAI,EAAEK;QACrC;IACF;AACF;AAMO,SAASL,cAA2DY,MAAS,EAAEC,CAAI,EAAEC,UAA8B;IACxH,MAAMC,WAAWD,WAAWV,KAAK;IACjC,IAAI,OAAOW,aAAa,YAAY;QAClC,MAAMC,SAASD,UAAUE,KAAKL;QAC9B,IAAII,QAAQ;YACV,SAASE,eAAoC,GAAGC,IAAO;gBACrD,OAAOP,OAAOZ,aAAa,CAAC,IAAMgB,OAAOI,KAAK,CAAC,MAAMD;YACvD;;YACAL,WAAWV,KAAK,GAAGc;QACrB;IACF;AACF"}
package/build/index.d.ts CHANGED
@@ -1,17 +1,17 @@
1
- export type Callback = () => any;
1
+ export type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;
2
2
  export declare const createContext: <T>() => {
3
- contextualize(value: T, callback: Callback): Promise<void>;
3
+ contextualize(value: T, callback: Callback): unknown;
4
4
  getContext(): T | undefined;
5
5
  };
6
- type Constructor = new (...args: any[]) => {};
7
- export declare function Context(Base?: Constructor): {
8
- new (...args: any[]): {
9
- contextualize(callback: Callback): Promise<void>;
6
+ type Constructor = new (...args: unknown[]) => object;
7
+ export declare function Context(BaseClass?: Constructor): {
8
+ new (...args: unknown[]): {
9
+ contextualize(callback: Callback): Promise<unknown>;
10
10
  };
11
11
  readonly context: unknown;
12
12
  };
13
13
  export interface Contextualized {
14
14
  contextualize(callback: Callback): Promise<any>;
15
15
  }
16
- export declare function contextualize<T extends Contextualized>(target: T, _: string, descriptor: TypedPropertyDescriptor<Callback>): void;
16
+ export declare function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor): void;
17
17
  export {};
package/build/index.js CHANGED
@@ -1,51 +1,39 @@
1
- import { AsyncLocalStorage } from 'async_hooks';
2
- export const createContext = () => {
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ export const createContext = ()=>{
3
3
  const storage = new AsyncLocalStorage();
4
4
  return {
5
- contextualize(value, callback) {
6
- return new Promise((resolve, reject) => {
7
- storage.run(value, async () => {
8
- try {
9
- await callback();
10
- resolve();
11
- }
12
- catch (e) {
13
- reject(e);
14
- }
15
- });
16
- });
5
+ contextualize (value, callback) {
6
+ return storage.run(value, callback);
17
7
  },
18
- getContext() {
8
+ getContext () {
19
9
  return storage.getStore();
20
10
  }
21
11
  };
22
12
  };
23
- export function Context(Base) {
13
+ export function Context(BaseClass = class {
14
+ }) {
24
15
  const context = createContext();
25
- return Base
26
- ? class Contextual extends Base {
27
- static get context() {
28
- return context.getContext();
29
- }
30
- async contextualize(callback) {
31
- return context.contextualize(this, callback);
32
- }
16
+ return class Contextual extends BaseClass {
17
+ static get context() {
18
+ return context.getContext();
33
19
  }
34
- : class Contextual {
35
- static get context() {
36
- return context.getContext();
37
- }
38
- async contextualize(callback) {
39
- return context.contextualize(this, callback);
40
- }
41
- };
20
+ async contextualize(callback) {
21
+ return context.contextualize(this, callback);
22
+ }
23
+ };
42
24
  }
43
25
  export function contextualize(target, _, descriptor) {
44
- const method = descriptor.value;
45
- function contextualized() {
46
- return target.contextualize(() => method.apply(this, arguments));
26
+ const property = descriptor.value;
27
+ if (typeof property === 'function') {
28
+ const method = property?.bind(target);
29
+ if (method) {
30
+ function contextualized(...args) {
31
+ return target.contextualize(()=>method.apply(null, args));
32
+ }
33
+ ;
34
+ descriptor.value = contextualized;
35
+ }
47
36
  }
48
- ;
49
- descriptor.value = contextualized;
50
37
  }
51
- //# sourceMappingURL=index.js.map
38
+
39
+ //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAIhD,MAAM,CAAC,MAAM,aAAa,GAAG,GAAM,EAAE;IACnC,MAAM,OAAO,GAAG,IAAI,iBAAiB,EAAK,CAAC;IAE3C,OAAO;QACL,aAAa,CAAC,KAAQ,EAAE,QAAkB;YACxC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBAC3C,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE;oBAC5B,IAAI;wBACF,MAAM,QAAQ,EAAE,CAAC;wBACjB,OAAO,EAAE,CAAC;qBACX;oBAAC,OAAO,CAAC,EAAE;wBACV,MAAM,CAAC,CAAC,CAAC,CAAC;qBACX;gBACH,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;QACD,UAAU;YACR,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC;QAC5B,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAIF,MAAM,UAAU,OAAO,CAAC,IAAkB;IACxC,MAAM,OAAO,GAAG,aAAa,EAAE,CAAC;IAEhC,OAAO,IAAI;QACT,CAAC,CAAC,MAAM,UAAW,SAAQ,IAAI;YAC7B,MAAM,KAAK,OAAO;gBAChB,OAAO,OAAO,CAAC,UAAU,EAAE,CAAC;YAC9B,CAAC;YAED,KAAK,CAAC,aAAa,CAAC,QAAkB;gBACpC,OAAO,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YAC/C,CAAC;SACF;QACD,CAAC,CAAC,MAAM,UAAU;YAChB,MAAM,KAAK,OAAO;gBAChB,OAAO,OAAO,CAAC,UAAU,EAAE,CAAC;YAC9B,CAAC;YAED,KAAK,CAAC,aAAa,CAAC,QAAkB;gBACpC,OAAO,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YAC/C,CAAC;SACF,CAAC;AACN,CAAC;AAMD,MAAM,UAAU,aAAa,CAA2B,MAAS,EAAE,CAAS,EAAE,UAA6C;IACzH,MAAM,MAAM,GAAG,UAAU,CAAC,KAAM,CAAC;IACjC,SAAS,cAAc;QACrB,OAAO,MAAM,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;IACnE,CAAC;IAAA,CAAC;IACF,UAAU,CAAC,KAAK,GAAG,cAAqB,CAAC;AAC3C,CAAC"}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<T>();\n\n return {\n contextualize(value: T, callback: Callback) {\n return storage.run(value, callback);\n },\n getContext() {\n return storage.getStore();\n }\n };\n};\n\ntype Constructor = new (...args: unknown[]) => object;\n\nexport function Context(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static get context() {\n return context.getContext();\n }\n\n async contextualize(callback: Callback) {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport interface Contextualized {\n contextualize(callback: Callback): Promise<any>;\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {\n const property = descriptor.value;\n if (typeof property === 'function') {\n const method = property?.bind(target);\n if (method) {\n function contextualized<A extends unknown[]>(...args: A) {\n return target.contextualize(() => method.apply(null, args));\n };\n descriptor.value = contextualized as T[K];\n }\n }\n}\n\n"],"names":["AsyncLocalStorage","createContext","storage","contextualize","value","callback","run","getContext","getStore","Context","BaseClass","context","Contextual","target","_","descriptor","property","method","bind","contextualized","args","apply"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,mBAAmB;AAIrD,OAAO,MAAMC,gBAAgB;IAC3B,MAAMC,UAAU,IAAIF;IAEpB,OAAO;QACLG,eAAcC,KAAQ,EAAEC,QAAkB;YACxC,OAAOH,QAAQI,GAAG,CAACF,OAAOC;QAC5B;QACAE;YACE,OAAOL,QAAQM,QAAQ;QACzB;IACF;AACF,EAAE;AAIF,OAAO,SAASC,QAAQC,YAAyB;AAAQ,CAAC;IACxD,MAAMC,UAAUV;IAChB,OAAO,MAAMW,mBAAmBF;QAC9B,WAAWC,UAAU;YACnB,OAAOA,QAAQJ,UAAU;QAC3B;QAEA,MAAMJ,cAAcE,QAAkB,EAAE;YACtC,OAAOM,QAAQR,aAAa,CAAC,IAAI,EAAEE;QACrC;IACF;AACF;AAMA,OAAO,SAASF,cAA2DU,MAAS,EAAEC,CAAI,EAAEC,UAA8B;IACxH,MAAMC,WAAWD,WAAWX,KAAK;IACjC,IAAI,OAAOY,aAAa,YAAY;QAClC,MAAMC,SAASD,UAAUE,KAAKL;QAC9B,IAAII,QAAQ;YACV,SAASE,eAAoC,GAAGC,IAAO;gBACrD,OAAOP,OAAOV,aAAa,CAAC,IAAMc,OAAOI,KAAK,CAAC,MAAMD;YACvD;;YACAL,WAAWX,KAAK,GAAGe;QACrB;IACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "conode",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "0-Deps, simple and fast context library for NodeJS",
5
5
  "type": "module",
6
6
  "main": "build/index.cjs",
@@ -11,13 +11,6 @@
11
11
  ],
12
12
  "module": "build/index.js",
13
13
  "sideEffects": false,
14
- "scripts": {
15
- "convert-cjs": "node scripts/convert-cjs.js",
16
- "build": "rm -rf build && yarn build:cjs && yarn build:mjs",
17
- "build:cjs": "tsc -p tsconfig.cjs.json && yarn convert-cjs",
18
- "build:mjs": "tsc -p tsconfig.mjs.json --declaration",
19
- "test": "jest"
20
- },
21
14
  "repository": {
22
15
  "type": "git",
23
16
  "url": "git+https://github.com/3axap4eHko/conode.git"
@@ -39,10 +32,15 @@
39
32
  },
40
33
  "homepage": "https://github.com/3axap4eHko/conode#readme",
41
34
  "devDependencies": {
42
- "@types/jest": "^27.5.1",
43
- "@types/node": "^17.0.35",
44
- "jest": "^28.1.0",
45
- "ts-jest": "^28.0.3",
46
- "typescript": "^4.7.2"
35
+ "@swc/jest": "^0.2.38",
36
+ "@types/jest": "^29.5.14",
37
+ "@types/node": "^22.15.18",
38
+ "inop": "^0.7.8",
39
+ "jest": "^29.7.0",
40
+ "typescript": "^5.8.3"
41
+ },
42
+ "scripts": {
43
+ "build": "rm -rf build && inop src build -i __tests__ -i *.tmp.ts && tsc --declaration --emitDeclarationOnly",
44
+ "test": "jest"
47
45
  }
48
- }
46
+ }
package/src/index.ts CHANGED
@@ -1,22 +1,13 @@
1
- import { AsyncLocalStorage } from 'async_hooks';
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
 
3
- export type Callback = () => any;
3
+ export type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;
4
4
 
5
5
  export const createContext = <T>() => {
6
6
  const storage = new AsyncLocalStorage<T>();
7
7
 
8
8
  return {
9
9
  contextualize(value: T, callback: Callback) {
10
- return new Promise<void>((resolve, reject) => {
11
- storage.run(value, async () => {
12
- try {
13
- await callback();
14
- resolve();
15
- } catch (e) {
16
- reject(e);
17
- }
18
- });
19
- });
10
+ return storage.run(value, callback);
20
11
  },
21
12
  getContext() {
22
13
  return storage.getStore();
@@ -24,40 +15,35 @@ export const createContext = <T>() => {
24
15
  };
25
16
  };
26
17
 
27
- type Constructor = new (...args: any[]) => {};
18
+ type Constructor = new (...args: unknown[]) => object;
28
19
 
29
- export function Context(Base?: Constructor) {
20
+ export function Context(BaseClass: Constructor = class { }) {
30
21
  const context = createContext();
22
+ return class Contextual extends BaseClass {
23
+ static get context() {
24
+ return context.getContext();
25
+ }
31
26
 
32
- return Base
33
- ? class Contextual extends Base {
34
- static get context() {
35
- return context.getContext();
36
- }
37
-
38
- async contextualize(callback: Callback) {
39
- return context.contextualize(this, callback);
40
- }
27
+ async contextualize(callback: Callback) {
28
+ return context.contextualize(this, callback);
41
29
  }
42
- : class Contextual {
43
- static get context() {
44
- return context.getContext();
45
- }
46
-
47
- async contextualize(callback: Callback) {
48
- return context.contextualize(this, callback);
49
- }
50
- };
30
+ };
51
31
  }
52
32
 
53
33
  export interface Contextualized {
54
34
  contextualize(callback: Callback): Promise<any>;
55
35
  }
56
36
 
57
- export function contextualize<T extends Contextualized>(target: T, _: string, descriptor: TypedPropertyDescriptor<Callback>) {
58
- const method = descriptor.value!;
59
- function contextualized() {
60
- return target.contextualize(() => method.apply(this, arguments));
61
- };
62
- descriptor.value = contextualized as any;
37
+ export function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {
38
+ const property = descriptor.value;
39
+ if (typeof property === 'function') {
40
+ const method = property?.bind(target);
41
+ if (method) {
42
+ function contextualized<A extends unknown[]>(...args: A) {
43
+ return target.contextualize(() => method.apply(null, args));
44
+ };
45
+ descriptor.value = contextualized as T[K];
46
+ }
47
+ }
63
48
  }
49
+