@coast/service-common 1.0.3 → 1.0.5

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.
@@ -1,5 +1,5 @@
1
- import { Trace } from './Trace';
2
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { Trace } from './Trace';
3
3
  import { TraceId } from './TraceId';
4
4
  export declare class TraceManager {
5
5
  private readonly asyncLocalStorage;
@@ -11,14 +11,11 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  return function (target, key) { decorator(target, key, paramIndex); }
13
13
  };
14
- var __importDefault = (this && this.__importDefault) || function (mod) {
15
- return (mod && mod.__esModule) ? mod : { "default": mod };
16
- };
17
14
  Object.defineProperty(exports, "__esModule", { value: true });
18
15
  exports.TraceManager = void 0;
19
- const uuid_1 = __importDefault(require("uuid"));
20
- const common_1 = require("@nestjs/common");
21
16
  const node_async_hooks_1 = require("node:async_hooks");
17
+ const common_1 = require("@nestjs/common");
18
+ const uuid_1 = require("uuid");
22
19
  const TraceId_1 = require("./TraceId");
23
20
  let TraceManager = class TraceManager {
24
21
  constructor(asyncLocalStorage) {
@@ -29,8 +26,8 @@ let TraceManager = class TraceManager {
29
26
  }
30
27
  withNewTrace(fn, traceId) {
31
28
  const trace = {
32
- traceId: traceId ?? (0, TraceId_1.TraceId)(uuid_1.default.v4()),
33
- subtraceId: (0, TraceId_1.TraceId)(uuid_1.default.v4()),
29
+ traceId: traceId ?? (0, TraceId_1.TraceId)((0, uuid_1.v4)()),
30
+ subtraceId: (0, TraceId_1.TraceId)((0, uuid_1.v4)()),
34
31
  };
35
32
  return this.asyncLocalStorage.run(trace, fn);
36
33
  }
@@ -0,0 +1,5 @@
1
+ export declare class Generators {
2
+ static chunked<T>(iterator: AsyncGenerator<T>, size: number): AsyncGenerator<T[]>;
3
+ static take<T>(iterator: AsyncGenerator<T>, size: number): AsyncGenerator<T>;
4
+ static map<T, Y>(iterator: AsyncGenerator<T>, mapper: (data: T) => Y): AsyncGenerator<Y>;
5
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Generators = void 0;
4
+ class Generators {
5
+ static async *chunked(iterator, size) {
6
+ let chunk = [];
7
+ for await (const item of iterator) {
8
+ chunk.push(item);
9
+ if (chunk.length >= size) {
10
+ yield chunk;
11
+ chunk = [];
12
+ }
13
+ }
14
+ yield chunk;
15
+ }
16
+ static async *take(iterator, size) {
17
+ let count = 0;
18
+ for await (const item of iterator) {
19
+ if (count < size) {
20
+ yield item;
21
+ count++;
22
+ }
23
+ else {
24
+ break;
25
+ }
26
+ }
27
+ }
28
+ static async *map(iterator, mapper) {
29
+ for await (const item of iterator) {
30
+ yield mapper(item);
31
+ }
32
+ }
33
+ }
34
+ exports.Generators = Generators;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Generators_1 = require("../Generators");
4
+ const common_test_1 = require("@paradoxical-io/common-test");
5
+ test('take async', async () => {
6
+ const data = [];
7
+ for await (const i of Generators_1.Generators.take(infinityAsync(), 10)) {
8
+ data.push(i);
9
+ }
10
+ (0, common_test_1.safeExpect)(data).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
11
+ });
12
+ test('chunked async', async () => {
13
+ const data = [];
14
+ const chunked = Generators_1.Generators.chunked(infinityAsync(), 2);
15
+ for await (const i of Generators_1.Generators.take(chunked, 2)) {
16
+ data.push(i);
17
+ }
18
+ (0, common_test_1.safeExpect)(data).toEqual([
19
+ [0, 1],
20
+ [2, 3],
21
+ ]);
22
+ });
23
+ test('chunked async with end', async () => {
24
+ const data = [];
25
+ const chunked = Generators_1.Generators.chunked(Generators_1.Generators.take(infinityAsync(), 10), 4);
26
+ for await (const i of Generators_1.Generators.take(chunked, 3)) {
27
+ data.push(i);
28
+ }
29
+ (0, common_test_1.safeExpect)(data).toEqual([
30
+ [0, 1, 2, 3],
31
+ [4, 5, 6, 7],
32
+ [8, 9],
33
+ ]);
34
+ });
35
+ test('map async', async () => {
36
+ const data = [];
37
+ const mapped = Generators_1.Generators.map(infinityAsync(), (i) => i.toString());
38
+ for await (const i of Generators_1.Generators.take(mapped, 2)) {
39
+ data.push(i);
40
+ }
41
+ (0, common_test_1.safeExpect)(data).toEqual(['0', '1']);
42
+ });
43
+ async function* infinityAsync() {
44
+ let start = 0;
45
+ while (true) {
46
+ yield start;
47
+ start++;
48
+ }
49
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coast/service-common",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Common service package",
5
5
  "main": "./modules",
6
6
  "types": "./modules",
@@ -38,6 +38,7 @@
38
38
  "@nestjs/common": "^10.4.1",
39
39
  "@nestjs/config": "^3.2.3",
40
40
  "@nestjs/schematics": "^10.1.4",
41
+ "@paradoxical-io/common-test": "^1.0.7",
41
42
  "@types/jest": "^29.5.12",
42
43
  "@types/node": "^22.5.0",
43
44
  "@types/supertest": "^6.0.2",