conode 0.0.1 → 0.1.0

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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2021-present Ivan Zakharchanka
1
+ Copyright (c) 2022-present Ivan Zakharchanka
2
2
 
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -12,16 +12,16 @@
12
12
  ## Table of Contents
13
13
 
14
14
  - [Features](#features)
15
- - [Browser Support](#browser-support)
16
15
  - [Installing](#installing)
17
16
  - [Examples](#examples)
18
17
  - [License](#license)
19
18
 
20
19
  ## Features
21
20
 
22
- - Provides usefull wrappers of [AsyncLocalStorage](https://nodejs.org/api/async_context.html)
23
- - TypeScript support
24
- 0
21
+ - Provides useful wrappers of [AsyncLocalStorage](https://nodejs.org/api/async_context.html)
22
+ - Supports TypeScript typings
23
+ - Supports CommonJS and ES Module systems
24
+
25
25
  ## Installing
26
26
 
27
27
  Using yarn:
@@ -0,0 +1,70 @@
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());
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();
16
+ 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
+ });
29
+ },
30
+ getContext() {
31
+ return storage.getStore();
32
+ }
33
+ };
34
+ };
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
+ }
48
+ }
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
+ };
59
+ }
60
+ exports.Context = Context;
61
+ function contextualize(target, _, descriptor) {
62
+ const method = descriptor.value;
63
+ function contextualized() {
64
+ return target.contextualize(() => method.apply(this, arguments));
65
+ }
66
+ ;
67
+ descriptor.value = contextualized;
68
+ }
69
+ exports.contextualize = contextualize;
70
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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"}
@@ -0,0 +1,17 @@
1
+ export declare type Callback = () => any;
2
+ export declare const createContext: <T>() => {
3
+ contextualize(value: T, callback: Callback): Promise<void>;
4
+ getContext(): T | undefined;
5
+ };
6
+ declare type Constructor = new (...args: any[]) => {};
7
+ export declare function Context(Base?: Constructor): {
8
+ new (...args: any[]): {
9
+ contextualize(callback: Callback): Promise<void>;
10
+ };
11
+ readonly context: unknown;
12
+ };
13
+ export interface Contextualized {
14
+ contextualize(callback: Callback): Promise<any>;
15
+ }
16
+ export declare function contextualize<T extends Contextualized>(target: T, _: string, descriptor: TypedPropertyDescriptor<Callback>): void;
17
+ export {};
package/build/index.js ADDED
@@ -0,0 +1,51 @@
1
+ import { AsyncLocalStorage } from 'async_hooks';
2
+ export const createContext = () => {
3
+ const storage = new AsyncLocalStorage();
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
+ });
17
+ },
18
+ getContext() {
19
+ return storage.getStore();
20
+ }
21
+ };
22
+ };
23
+ export function Context(Base) {
24
+ 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
+ }
33
+ }
34
+ : class Contextual {
35
+ static get context() {
36
+ return context.getContext();
37
+ }
38
+ async contextualize(callback) {
39
+ return context.contextualize(this, callback);
40
+ }
41
+ };
42
+ }
43
+ export function contextualize(target, _, descriptor) {
44
+ const method = descriptor.value;
45
+ function contextualized() {
46
+ return target.contextualize(() => method.apply(this, arguments));
47
+ }
48
+ ;
49
+ descriptor.value = contextualized;
50
+ }
51
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,MA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AS,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"}
package/package.json CHANGED
@@ -1,10 +1,21 @@
1
1
  {
2
2
  "name": "conode",
3
- "version": "0.0.1",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "0.1.0",
4
+ "description": "0-Deps, simple and fast context library for NodeJS",
5
+ "type": "module",
6
+ "main": "build/index.js",
7
+ "types": "build/index.d.ts",
8
+ "files": [
9
+ "build",
10
+ "src/index.ts"
11
+ ],
12
+ "module": "build/index.js",
13
+ "sideEffects": false,
6
14
  "scripts": {
7
- "build": "tsc",
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",
8
19
  "test": "jest"
9
20
  },
10
21
  "repository": {
@@ -33,6 +44,5 @@
33
44
  "jest": "27",
34
45
  "ts-jest": "^27.1.4",
35
46
  "typescript": "^4.6.3"
36
- },
37
- "dependencies": {}
47
+ }
38
48
  }
@@ -1,42 +0,0 @@
1
- name: CI/CD
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- strategy:
9
- matrix:
10
- node-version: [16.x]
11
- steps:
12
- - name: Checkout code
13
- uses: actions/checkout@v2
14
-
15
- - name: Use Node.js ${{ matrix.node-version }}
16
- uses: actions/setup-node@v1
17
- with:
18
- node-version: ${{ matrix.node-version }}
19
- registry-url: https://registry.npmjs.org/
20
-
21
- - name: Environment log
22
- id: env
23
- run: |
24
- node --version
25
- yarn --version
26
-
27
- - name: Install dependecies
28
- run: |
29
- yarn install --frozen-lockfile
30
-
31
- - name: Run build process
32
- run: |
33
- yarn build
34
-
35
- - name: Run testing
36
- run: |
37
- yarn test
38
-
39
- - name: Upload test coverage
40
- uses: codecov/codecov-action@v1
41
- with:
42
- token: ${{ secrets.CODECOV_TOKEN }}
@@ -1,74 +0,0 @@
1
- name: Autoupdate
2
-
3
- on:
4
- schedule:
5
- - cron: 0 0 * * *
6
- workflow_dispatch:
7
-
8
- jobs:
9
- build:
10
- runs-on: ubuntu-latest
11
- strategy:
12
- matrix:
13
- node-version: [14.x]
14
- steps:
15
- - name: Checkout code
16
- uses: actions/checkout@v2
17
-
18
- - name: Use Node.js ${{ matrix.node-version }}
19
- uses: actions/setup-node@v1
20
- with:
21
- node-version: ${{ matrix.node-version }}
22
- registry-url: https://registry.npmjs.org/
23
-
24
- - name: Environment log
25
- id: env
26
- run: |
27
- node --version
28
- yarn --version
29
-
30
- - name: Install dependecies
31
- run: |
32
- yarn install --frozen-lockfile
33
-
34
- - name: Upgrade
35
- id: updates
36
- run: |
37
- PENDING=$(yarn outdated --json|tail -1|jq '.data.body'|jq length)
38
- echo "::set-output name=PENDING::$PENDING"
39
- yarn upgrade
40
- DONE=$(yarn outdated --json|tail -1|jq '.data.body'|jq length)
41
- echo "::set-output name=DONE::$DONE"
42
-
43
- - name: Run build process
44
- if: steps.updates.outputs.PENDING != steps.updates.outputs.DONE
45
- run: |
46
- yarn build
47
-
48
- - name: Run tests
49
- if: steps.updates.outputs.PENDING != steps.updates.outputs.DONE
50
- run: |
51
- yarn test
52
-
53
- - name: Commit updates
54
- if: steps.updates.outputs.PENDING != steps.updates.outputs.DONE
55
- run: |
56
- git config --global user.email "3axap4eHko@github.com"
57
- git config --global user.name "Ivan Zakharchanka"
58
- git add --all
59
- git commit -am "autoupdate"
60
- npm version patch
61
-
62
- - name: Publish
63
- if: steps.updates.outputs.PENDING != steps.updates.outputs.DONE
64
- run: |
65
- echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
66
- npm publish
67
- git push
68
- git push --tags
69
-
70
- - name: Push updates
71
- if: steps.updates.outputs.PENDING != steps.updates.outputs.DONE
72
- run: |
73
- git push
74
- git push --tags
package/jest.config.js DELETED
@@ -1,16 +0,0 @@
1
- module.exports = {
2
- verbose: true,
3
- testEnvironment: 'node',
4
- collectCoverage: !!process.env.CI,
5
- preset: 'ts-jest',
6
- collectCoverageFrom: [
7
- 'src/**/*.ts',
8
- ],
9
- coveragePathIgnorePatterns: [
10
- '/node_modules/',
11
- '__fixtures__',
12
- '__mocks__',
13
- '__tests__',
14
- ],
15
- coverageDirectory: './coverage',
16
- };
@@ -1,77 +0,0 @@
1
- import { createContext, Context, contextualize } from '../index';
2
-
3
- describe('test', () => {
4
- it('Should create a context', async () => {
5
- const context = createContext();
6
- expect(context).toHaveProperty('contextualize');
7
- expect(context).toHaveProperty('getContext');
8
- });
9
-
10
- it('Should execute in the context', async () => {
11
- const context = createContext();
12
- const value1 = { test: 1 };
13
- const value2 = { test: 1 };
14
- const error = new Error();
15
-
16
- const action1 = jest.fn(async () => {
17
- expect(context.getContext()).toStrictEqual(value1);
18
- });
19
- const action2 = jest.fn(async () => {
20
- expect(context.getContext()).toStrictEqual(value2);
21
- });
22
-
23
- const action3 = jest.fn(async () => {
24
- throw error;
25
- });
26
- expect(context.contextualize(value2, action3)).rejects.toStrictEqual(error);
27
- expect(action3).toBeCalled();
28
- expect(action2).not.toBeCalled();
29
- expect(action1).not.toBeCalled();
30
-
31
- await context.contextualize(value2, action2);
32
- expect(action2).toBeCalled();
33
- expect(action1).not.toBeCalled();
34
- await context.contextualize(value1, action1);
35
- expect(action1).toBeCalled();
36
-
37
- });
38
-
39
- it('Should mixin no class context', async () => {
40
- class Test extends Context() { }
41
- const test = new Test();
42
- const action = jest.fn(async () => {
43
- expect(Test.context).toStrictEqual(test);
44
- });
45
- await test.contextualize(action);
46
- expect(action).toBeCalled();
47
- });
48
-
49
- it('Should mixin class context', async () => {
50
- class A {}
51
- class Test extends Context(A) { }
52
- const test = new Test();
53
- const action = jest.fn(async () => {
54
- expect(Test.context).toStrictEqual(test);
55
- });
56
- await test.contextualize(action);
57
- expect(action).toBeCalled();
58
- });
59
-
60
- it('Should decorate method', async () => {
61
- const action = jest.fn(async () => {
62
- expect(Test.context).toStrictEqual(test);
63
- });
64
-
65
- class Test extends Context() {
66
-
67
- @contextualize
68
- async run() {
69
- await action();
70
- }
71
- }
72
-
73
- const test = new Test();
74
- await test.run();
75
- expect(action).toBeCalled();
76
- });
77
- });
package/tsconfig.json DELETED
@@ -1,31 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES6",
4
- "module": "CommonJS",
5
- "moduleResolution": "node",
6
- "noImplicitAny": true,
7
- "removeComments": true,
8
- "preserveConstEnums": true,
9
- "experimentalDecorators": true,
10
- "esModuleInterop": true,
11
- "sourceMap": true,
12
- "declaration": true,
13
- "lib": [
14
- "esnext",
15
- "dom",
16
- "dom.iterable"
17
- ],
18
- "rootDir": "src",
19
- "outDir": "build"
20
- },
21
- "include": [
22
- "src/*.ts",
23
- "src/Request.ts"
24
- ],
25
- "exclude": [
26
- "node_modules",
27
- "src/**/__tests__",
28
- "src/**/__fixtures__",
29
- "src/**/__mocks__"
30
- ]
31
- }