@tramvai/module-server 2.74.0 → 2.75.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/README.md CHANGED
@@ -188,7 +188,32 @@ Module uses loggers with identifiers: `server`, `server:static`, `server:webapp`
188
188
 
189
189
  ### Early Hints
190
190
 
191
- Module send the [103 Early Hints](https://developer.chrome.com/blog/early-hints) response to provide better performance, though there are several limitations (look [here](https://chromium.googlesource.com/chromium/src/+/master/docs/early-hints.md#what_s-not-supported) and [here](https://developer.chrome.com/blog/early-hints/#current-limitations)). Currently, module provides hints for next resources:
191
+ Module can send the [103 Early Hints](https://developer.chrome.com/blog/early-hints) response to provide better performance, though there are several limitations (look [here](https://chromium.googlesource.com/chromium/src/+/master/docs/early-hints.md#what_s-not-supported) and [here](https://developer.chrome.com/blog/early-hints/#current-limitations)).
192
+
193
+ If you want to enable Early Hints, provide `EARLY_HINTS_ENABLED` env variable:
194
+
195
+ ```
196
+ EARLY_HINTS_ENABLED: 'true'
197
+ ```
198
+
199
+ Or provide custom `EARLY_HINTS_ENABLED_TOKEN` token (`EARLY_HINTS_ENABLED` will be ignored):
200
+
201
+ ```ts
202
+ import { EARLY_HINTS_ENABLED_TOKEN } from '@tramvai/tokens-server';
203
+
204
+ const provider = {
205
+ provide: EARLY_HINTS_ENABLED_TOKEN,
206
+ useValue: () => true,
207
+ };
208
+ ```
209
+
210
+ :::info
211
+
212
+ You must check that the balancers and proxies in front of your application support Early Hints
213
+
214
+ :::
215
+
216
+ Currently, module provides hints for next resources:
192
217
 
193
218
  - Resources with `preconnectLink` type;
194
219
  - General resources, which have the `preloadLink` type;
@@ -2,6 +2,8 @@ import { __decorate } from 'tslib';
2
2
  import { Module, provide, commandLineListTokens } from '@tramvai/core';
3
3
  import { EARLY_HINTS_MANAGER_TOKEN, FASTIFY_RESPONSE } from '@tramvai/tokens-server-private';
4
4
  import { RESOURCES_REGISTRY, RENDER_FLOW_AFTER_TOKEN } from '@tramvai/tokens-render';
5
+ import { ENV_USED_TOKEN, ENV_MANAGER_TOKEN } from '@tramvai/tokens-common';
6
+ import { EARLY_HINTS_ENABLED_TOKEN } from '@tramvai/tokens-server';
5
7
  import { EarlyHintsManager } from './service.es.js';
6
8
 
7
9
  let EarlyHintsModule = class EarlyHintsModule {
@@ -9,12 +11,32 @@ let EarlyHintsModule = class EarlyHintsModule {
9
11
  EarlyHintsModule = __decorate([
10
12
  Module({
11
13
  providers: [
14
+ provide({
15
+ provide: ENV_USED_TOKEN,
16
+ useValue: [
17
+ {
18
+ key: 'EARLY_HINTS_ENABLED',
19
+ optional: true,
20
+ dehydrate: false,
21
+ },
22
+ ],
23
+ }),
24
+ provide({
25
+ provide: EARLY_HINTS_ENABLED_TOKEN,
26
+ useFactory: ({ envManager }) => {
27
+ return () => envManager.get('EARLY_HINTS_ENABLED') === 'true';
28
+ },
29
+ deps: {
30
+ envManager: ENV_MANAGER_TOKEN,
31
+ },
32
+ }),
12
33
  provide({
13
34
  provide: EARLY_HINTS_MANAGER_TOKEN,
14
35
  useClass: EarlyHintsManager,
15
36
  deps: {
16
37
  response: FASTIFY_RESPONSE,
17
38
  resourcesRegistry: RESOURCES_REGISTRY,
39
+ earlyHintsEnabled: EARLY_HINTS_ENABLED_TOKEN,
18
40
  },
19
41
  }),
20
42
  provide({
@@ -6,6 +6,8 @@ var tslib = require('tslib');
6
6
  var core = require('@tramvai/core');
7
7
  var tokensServerPrivate = require('@tramvai/tokens-server-private');
8
8
  var tokensRender = require('@tramvai/tokens-render');
9
+ var tokensCommon = require('@tramvai/tokens-common');
10
+ var tokensServer = require('@tramvai/tokens-server');
9
11
  var service = require('./service.js');
10
12
 
11
13
  exports.EarlyHintsModule = class EarlyHintsModule {
@@ -13,12 +15,32 @@ exports.EarlyHintsModule = class EarlyHintsModule {
13
15
  exports.EarlyHintsModule = tslib.__decorate([
14
16
  core.Module({
15
17
  providers: [
18
+ core.provide({
19
+ provide: tokensCommon.ENV_USED_TOKEN,
20
+ useValue: [
21
+ {
22
+ key: 'EARLY_HINTS_ENABLED',
23
+ optional: true,
24
+ dehydrate: false,
25
+ },
26
+ ],
27
+ }),
28
+ core.provide({
29
+ provide: tokensServer.EARLY_HINTS_ENABLED_TOKEN,
30
+ useFactory: ({ envManager }) => {
31
+ return () => envManager.get('EARLY_HINTS_ENABLED') === 'true';
32
+ },
33
+ deps: {
34
+ envManager: tokensCommon.ENV_MANAGER_TOKEN,
35
+ },
36
+ }),
16
37
  core.provide({
17
38
  provide: tokensServerPrivate.EARLY_HINTS_MANAGER_TOKEN,
18
39
  useClass: service.EarlyHintsManager,
19
40
  deps: {
20
41
  response: tokensServerPrivate.FASTIFY_RESPONSE,
21
42
  resourcesRegistry: tokensRender.RESOURCES_REGISTRY,
43
+ earlyHintsEnabled: tokensServer.EARLY_HINTS_ENABLED_TOKEN,
22
44
  },
23
45
  }),
24
46
  core.provide({
@@ -1,16 +1,21 @@
1
1
  import type { FASTIFY_RESPONSE, EARLY_HINTS_MANAGER_TOKEN } from '@tramvai/tokens-server-private';
2
2
  import type { RESOURCES_REGISTRY } from '@tramvai/tokens-render';
3
+ import type { ExtractDependencyType } from '@tinkoff/dippy';
4
+ import type { EARLY_HINTS_ENABLED_TOKEN } from '@tramvai/tokens-server';
3
5
  type EarlyHintsInterface = typeof EARLY_HINTS_MANAGER_TOKEN;
4
6
  type Response = typeof FASTIFY_RESPONSE;
5
7
  type ResourcesRegistry = typeof RESOURCES_REGISTRY;
8
+ type EarlyHintsEnabled = ExtractDependencyType<typeof EARLY_HINTS_ENABLED_TOKEN>;
6
9
  interface ConstructorPayload {
7
10
  response: Response;
8
11
  resourcesRegistry: ResourcesRegistry;
12
+ earlyHintsEnabled: EarlyHintsEnabled;
9
13
  }
10
14
  export declare class EarlyHintsManager implements EarlyHintsInterface {
11
15
  private sentHints;
12
16
  private readonly response;
13
17
  private readonly resourcesRegistry;
18
+ private readonly earlyHintsEnabled;
14
19
  constructor(payload: ConstructorPayload);
15
20
  flushHints(): Promise<void>;
16
21
  private getHints;
@@ -3,8 +3,12 @@ class EarlyHintsManager {
3
3
  this.sentHints = new Set();
4
4
  this.response = payload.response;
5
5
  this.resourcesRegistry = payload.resourcesRegistry;
6
+ this.earlyHintsEnabled = payload.earlyHintsEnabled;
6
7
  }
7
8
  async flushHints() {
9
+ if (!this.earlyHintsEnabled()) {
10
+ return;
11
+ }
8
12
  const hints = this.getHints();
9
13
  await this.writeToSocket(hints);
10
14
  }
@@ -7,8 +7,12 @@ class EarlyHintsManager {
7
7
  this.sentHints = new Set();
8
8
  this.response = payload.response;
9
9
  this.resourcesRegistry = payload.resourcesRegistry;
10
+ this.earlyHintsEnabled = payload.earlyHintsEnabled;
10
11
  }
11
12
  async flushHints() {
13
+ if (!this.earlyHintsEnabled()) {
14
+ return;
15
+ }
12
16
  const hints = this.getHints();
13
17
  await this.writeToSocket(hints);
14
18
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-server",
3
- "version": "2.74.0",
3
+ "version": "2.75.0",
4
4
  "description": "",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -21,15 +21,15 @@
21
21
  "watch": "tsc -w"
22
22
  },
23
23
  "dependencies": {
24
- "@tinkoff/errors": "0.3.6",
24
+ "@tinkoff/errors": "0.3.7",
25
25
  "@tinkoff/monkeypatch": "2.0.4",
26
26
  "@tinkoff/terminus": "0.1.7",
27
27
  "@tinkoff/url": "0.8.5",
28
- "@tramvai/module-cache-warmup": "2.74.0",
29
- "@tramvai/module-metrics": "2.74.0",
30
- "@tramvai/papi": "2.74.0",
31
- "@tramvai/tokens-server": "2.74.0",
32
- "@tramvai/tokens-server-private": "2.74.0",
28
+ "@tramvai/module-cache-warmup": "2.75.0",
29
+ "@tramvai/module-metrics": "2.75.0",
30
+ "@tramvai/papi": "2.75.0",
31
+ "@tramvai/tokens-server": "2.75.0",
32
+ "@tramvai/tokens-server-private": "2.75.0",
33
33
  "fastify": "^4.6.0",
34
34
  "@fastify/cookie": "^8.1.0",
35
35
  "@fastify/compress": "^6.1.1",
@@ -40,13 +40,13 @@
40
40
  "peerDependencies": {
41
41
  "@tinkoff/dippy": "0.8.13",
42
42
  "@tinkoff/utils": "^2.1.2",
43
- "@tramvai/cli": "2.74.0",
44
- "@tramvai/core": "2.74.0",
45
- "@tramvai/module-common": "2.74.0",
46
- "@tramvai/module-environment": "2.74.0",
47
- "@tramvai/tokens-common": "2.74.0",
48
- "@tramvai/tokens-core-private": "2.74.0",
49
- "@tramvai/tokens-render": "2.74.0",
43
+ "@tramvai/cli": "2.75.0",
44
+ "@tramvai/core": "2.75.0",
45
+ "@tramvai/module-common": "2.75.0",
46
+ "@tramvai/module-environment": "2.75.0",
47
+ "@tramvai/tokens-common": "2.75.0",
48
+ "@tramvai/tokens-core-private": "2.75.0",
49
+ "@tramvai/tokens-render": "2.75.0",
50
50
  "tslib": "^2.4.0"
51
51
  },
52
52
  "devDependencies": {