@tramvai/module-common 5.0.2 → 5.1.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.
@@ -1,9 +1,26 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { AsyncLocalStorage } from 'async_hooks';
3
3
  import { Module, provide, Scope } from '@tramvai/core';
4
- import { WEB_FASTIFY_APP_INIT_TOKEN, WEB_FASTIFY_APP_TOKEN } from '@tramvai/tokens-server-private';
4
+ import { WEB_FASTIFY_APP_BEFORE_INIT_TOKEN, WEB_FASTIFY_APP_TOKEN, WEB_FASTIFY_APP_INIT_TOKEN } from '@tramvai/tokens-server-private';
5
5
  import { ASYNC_LOCAL_STORAGE_TOKEN } from '@tramvai/tokens-common';
6
6
 
7
+ function runInAsyncContext({ app, storage, }) {
8
+ app.addHook('onRequest', (req, reply, done) => {
9
+ storage.run({}, done);
10
+ // prevent memory leaks, because async context can be destroyed after response,
11
+ // all stored resources will be accumulated in memory, and peak memory allocation will be high
12
+ // `onResponse` is not used because is not fired when request was aborted by user (https://fastify.dev/docs/latest/Guides/Detecting-When-Clients-Abort/)
13
+ // TODO: can lead to errors because store will be cleared before reply, but maybe it's ok because response will be ignored?
14
+ reply.raw.once('close', () => {
15
+ const store = storage.getStore();
16
+ if (store) {
17
+ for (const key in store) {
18
+ delete store[key];
19
+ }
20
+ }
21
+ });
22
+ });
23
+ }
7
24
  let AsyncLocalStorageModule = class AsyncLocalStorageModule {
8
25
  };
9
26
  AsyncLocalStorageModule = __decorate([
@@ -15,27 +32,27 @@ AsyncLocalStorageModule = __decorate([
15
32
  scope: Scope.SINGLETON,
16
33
  useFactory: () => new AsyncLocalStorage(),
17
34
  }),
35
+ provide({
36
+ provide: WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
37
+ multi: true,
38
+ scope: Scope.SINGLETON,
39
+ useFactory: (deps) => {
40
+ return () => {
41
+ runInAsyncContext(deps);
42
+ };
43
+ },
44
+ deps: {
45
+ app: WEB_FASTIFY_APP_TOKEN,
46
+ storage: ASYNC_LOCAL_STORAGE_TOKEN,
47
+ },
48
+ }),
18
49
  provide({
19
50
  provide: WEB_FASTIFY_APP_INIT_TOKEN,
20
51
  multi: true,
21
52
  scope: Scope.SINGLETON,
22
- useFactory: ({ app, storage }) => {
53
+ useFactory: (deps) => {
23
54
  return () => {
24
- app.addHook('onRequest', (req, reply, done) => {
25
- storage.run({}, done);
26
- // prevent memory leaks, because async context can be destroyed after response,
27
- // all stored resources will be accumulated in memory, and peak memory allocation will be high
28
- // `onResponse` is not used because is not fired when request was aborted by user (https://fastify.dev/docs/latest/Guides/Detecting-When-Clients-Abort/)
29
- // TODO: can lead to errors because store will be cleared before reply, but maybe it's ok because response will be ignored?
30
- reply.raw.once('close', () => {
31
- const store = storage.getStore();
32
- if (store) {
33
- for (const key in store) {
34
- delete store[key];
35
- }
36
- }
37
- });
38
- });
55
+ runInAsyncContext(deps);
39
56
  };
40
57
  },
41
58
  deps: {
@@ -8,6 +8,23 @@ var core = require('@tramvai/core');
8
8
  var tokensServerPrivate = require('@tramvai/tokens-server-private');
9
9
  var tokensCommon = require('@tramvai/tokens-common');
10
10
 
11
+ function runInAsyncContext({ app, storage, }) {
12
+ app.addHook('onRequest', (req, reply, done) => {
13
+ storage.run({}, done);
14
+ // prevent memory leaks, because async context can be destroyed after response,
15
+ // all stored resources will be accumulated in memory, and peak memory allocation will be high
16
+ // `onResponse` is not used because is not fired when request was aborted by user (https://fastify.dev/docs/latest/Guides/Detecting-When-Clients-Abort/)
17
+ // TODO: can lead to errors because store will be cleared before reply, but maybe it's ok because response will be ignored?
18
+ reply.raw.once('close', () => {
19
+ const store = storage.getStore();
20
+ if (store) {
21
+ for (const key in store) {
22
+ delete store[key];
23
+ }
24
+ }
25
+ });
26
+ });
27
+ }
11
28
  exports.AsyncLocalStorageModule = class AsyncLocalStorageModule {
12
29
  };
13
30
  exports.AsyncLocalStorageModule = tslib.__decorate([
@@ -19,27 +36,27 @@ exports.AsyncLocalStorageModule = tslib.__decorate([
19
36
  scope: core.Scope.SINGLETON,
20
37
  useFactory: () => new async_hooks.AsyncLocalStorage(),
21
38
  }),
39
+ core.provide({
40
+ provide: tokensServerPrivate.WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
41
+ multi: true,
42
+ scope: core.Scope.SINGLETON,
43
+ useFactory: (deps) => {
44
+ return () => {
45
+ runInAsyncContext(deps);
46
+ };
47
+ },
48
+ deps: {
49
+ app: tokensServerPrivate.WEB_FASTIFY_APP_TOKEN,
50
+ storage: tokensCommon.ASYNC_LOCAL_STORAGE_TOKEN,
51
+ },
52
+ }),
22
53
  core.provide({
23
54
  provide: tokensServerPrivate.WEB_FASTIFY_APP_INIT_TOKEN,
24
55
  multi: true,
25
56
  scope: core.Scope.SINGLETON,
26
- useFactory: ({ app, storage }) => {
57
+ useFactory: (deps) => {
27
58
  return () => {
28
- app.addHook('onRequest', (req, reply, done) => {
29
- storage.run({}, done);
30
- // prevent memory leaks, because async context can be destroyed after response,
31
- // all stored resources will be accumulated in memory, and peak memory allocation will be high
32
- // `onResponse` is not used because is not fired when request was aborted by user (https://fastify.dev/docs/latest/Guides/Detecting-When-Clients-Abort/)
33
- // TODO: can lead to errors because store will be cleared before reply, but maybe it's ok because response will be ignored?
34
- reply.raw.once('close', () => {
35
- const store = storage.getStore();
36
- if (store) {
37
- for (const key in store) {
38
- delete store[key];
39
- }
40
- }
41
- });
42
- });
59
+ runInAsyncContext(deps);
43
60
  };
44
61
  },
45
62
  deps: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-common",
3
- "version": "5.0.2",
3
+ "version": "5.1.1",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -36,31 +36,31 @@
36
36
  "@tinkoff/lru-cache-nano": "^7.9.0",
37
37
  "@tinkoff/pubsub": "0.8.2",
38
38
  "@tinkoff/url": "0.11.2",
39
- "@tramvai/experiments": "5.0.2",
40
- "@tramvai/module-cookie": "5.0.2",
41
- "@tramvai/module-environment": "5.0.2",
42
- "@tramvai/module-log": "5.0.2",
39
+ "@tramvai/experiments": "5.1.1",
40
+ "@tramvai/module-cookie": "5.1.1",
41
+ "@tramvai/module-environment": "5.1.1",
42
+ "@tramvai/module-log": "5.1.1",
43
43
  "@tramvai/safe-strings": "0.8.2",
44
- "@tramvai/tokens-child-app": "5.0.2",
45
- "@tramvai/tokens-common": "5.0.2",
46
- "@tramvai/tokens-core-private": "5.0.2",
47
- "@tramvai/tokens-metrics": "5.0.2",
48
- "@tramvai/tokens-render": "5.0.2",
49
- "@tramvai/tokens-router": "5.0.2",
50
- "@tramvai/tokens-server-private": "5.0.2",
51
- "@tramvai/types-actions-state-context": "5.0.2",
44
+ "@tramvai/tokens-child-app": "5.1.1",
45
+ "@tramvai/tokens-common": "5.1.1",
46
+ "@tramvai/tokens-core-private": "5.1.1",
47
+ "@tramvai/tokens-metrics": "5.1.1",
48
+ "@tramvai/tokens-render": "5.1.1",
49
+ "@tramvai/tokens-router": "5.1.1",
50
+ "@tramvai/tokens-server-private": "5.1.1",
51
+ "@tramvai/types-actions-state-context": "5.1.1",
52
52
  "hoist-non-react-statics": "^3.3.1",
53
53
  "node-abort-controller": "^3.0.1"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "@tinkoff/dippy": "0.11.2",
57
57
  "@tinkoff/utils": "^2.1.2",
58
- "@tramvai/cli": "5.0.2",
59
- "@tramvai/core": "5.0.2",
60
- "@tramvai/papi": "5.0.2",
61
- "@tramvai/react": "5.0.2",
62
- "@tramvai/state": "5.0.2",
63
- "@tramvai/tokens-server": "5.0.2",
58
+ "@tramvai/cli": "5.1.1",
59
+ "@tramvai/core": "5.1.1",
60
+ "@tramvai/papi": "5.1.1",
61
+ "@tramvai/react": "5.1.1",
62
+ "@tramvai/state": "5.1.1",
63
+ "@tramvai/tokens-server": "5.1.1",
64
64
  "react": ">=16.14.0",
65
65
  "tslib": "^2.4.0"
66
66
  },