@tramvai/module-cookie 1.90.2 → 1.91.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/lib/browser.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { Module, Scope } from '@tramvai/core';
3
+ import { ClientHintsModule, USER_AGENT_TOKEN } from '@tramvai/module-client-hints';
3
4
  import { Cookies } from '@tinkoff/browser-cookies';
4
5
  import { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
5
6
  export { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
@@ -26,6 +27,13 @@ const trimSubdomains = (host) => {
26
27
  }
27
28
  return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
28
29
  };
30
+ const prepareCookieOptions = ({ userAgent: { sameSiteNoneCompatible }, defaultHost, secureProtocol }, { sameSite, noSubdomains, ...options }) => ({
31
+ ...options,
32
+ ...(sameSite === 'none' && (!sameSiteNoneCompatible || !secureProtocol) ? {} : { sameSite }),
33
+ ...(secureProtocol && sameSite === 'none' && sameSiteNoneCompatible ? { secure: true } : {}),
34
+ expires: calculateExpires(options.expires),
35
+ domain: noSubdomains ? trimSubdomains(options.domain || defaultHost) : options.domain,
36
+ });
29
37
 
30
38
  const checkCookieEnabled = () => {
31
39
  const testCookieName = 'testcookiesenabled';
@@ -54,29 +62,28 @@ class CookiesFallback {
54
62
  }
55
63
  }
56
64
  class CookieManager {
57
- constructor({ cookieOptions = {} } = {}) {
65
+ constructor({ cookieOptions = {}, userAgent, }) {
58
66
  const isSecure = window.location.protocol === 'https:';
59
67
  this.cookies = checkCookieEnabled()
60
68
  ? new Cookies({
61
- sameSite: isSecure ? 'none' : 'lax',
69
+ sameSite: userAgent.sameSiteNoneCompatible && isSecure ? 'none' : 'lax',
62
70
  secure: isSecure,
63
71
  ...cookieOptions,
64
72
  })
65
73
  : new CookiesFallback();
74
+ this.userAgent = userAgent;
66
75
  }
67
76
  // eslint-disable-next-line class-methods-use-this
68
77
  get(name) {
69
78
  return this.cookies.get(name) || undefined;
70
79
  }
71
80
  // eslint-disable-next-line class-methods-use-this
72
- set({ name, value, noSubdomains, ...options }) {
73
- this.cookies.set(name, value, {
74
- ...options,
75
- expires: calculateExpires(options.expires),
76
- domain: noSubdomains
77
- ? trimSubdomains(options.domain || window.location.hostname)
78
- : options.domain,
79
- });
81
+ set({ name, value, ...options }) {
82
+ this.cookies.set(name, value, prepareCookieOptions({
83
+ userAgent: this.userAgent,
84
+ defaultHost: window.location.hostname,
85
+ secureProtocol: window.location.protocol === 'https:',
86
+ }, options));
80
87
  }
81
88
  // eslint-disable-next-line class-methods-use-this
82
89
  all() {
@@ -92,6 +99,7 @@ let CookieModule = class CookieModule {
92
99
  };
93
100
  CookieModule = __decorate([
94
101
  Module({
102
+ imports: [ClientHintsModule],
95
103
  providers: [
96
104
  {
97
105
  // Управление куками в приложении
@@ -106,6 +114,7 @@ CookieModule = __decorate([
106
114
  token: 'temporary cookieOptions',
107
115
  optional: true,
108
116
  },
117
+ userAgent: USER_AGENT_TOKEN,
109
118
  },
110
119
  },
111
120
  ],
@@ -1,12 +1,15 @@
1
1
  import type { CookieOptions } from '@tinkoff/browser-cookies';
2
+ import type { USER_AGENT_TOKEN } from '@tramvai/module-client-hints';
2
3
  import type { CookieManager as Interface, CookieSetOptions } from './tokens';
3
4
  export declare class CookieManager implements Interface {
4
5
  private cookies;
5
- constructor({ cookieOptions }?: {
6
+ private userAgent;
7
+ constructor({ cookieOptions, userAgent, }: {
6
8
  cookieOptions?: CookieOptions;
9
+ userAgent: typeof USER_AGENT_TOKEN;
7
10
  });
8
11
  get(name: string): string;
9
- set({ name, value, noSubdomains, ...options }: CookieSetOptions): void;
12
+ set({ name, value, ...options }: CookieSetOptions): void;
10
13
  all(): {
11
14
  [key: string]: string;
12
15
  };
@@ -1,15 +1,18 @@
1
1
  import type { REQUEST_MANAGER_TOKEN, RESPONSE_MANAGER_TOKEN } from '@tramvai/tokens-common';
2
+ import type { USER_AGENT_TOKEN } from '@tramvai/module-client-hints';
2
3
  import type { CookieManager as Interface, CookieOptions, CookieSetOptions } from './tokens';
3
4
  export declare class CookieManager implements Interface {
4
5
  private cookies;
5
6
  private requestManager;
6
7
  private responseManager;
7
- constructor({ requestManager, responseManager, }: {
8
+ private userAgent;
9
+ constructor({ requestManager, responseManager, userAgent, }: {
8
10
  requestManager: typeof REQUEST_MANAGER_TOKEN;
9
11
  responseManager: typeof RESPONSE_MANAGER_TOKEN;
12
+ userAgent: typeof USER_AGENT_TOKEN;
10
13
  });
11
14
  get(name: any): string;
12
- set({ name, value, noSubdomains, ...options }: CookieSetOptions): void;
15
+ set({ name, value, ...options }: CookieSetOptions): void;
13
16
  all(): Record<string, string>;
14
17
  remove(name: string, options?: CookieOptions): void;
15
18
  }
package/lib/server.es.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { Module, Scope } from '@tramvai/core';
3
3
  import { REQUEST_MANAGER_TOKEN, RESPONSE_MANAGER_TOKEN } from '@tramvai/tokens-common';
4
+ import { ClientHintsModule, USER_AGENT_TOKEN } from '@tramvai/module-client-hints';
4
5
  import { serialize } from 'cookie';
5
6
  import { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
6
7
  export { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
@@ -27,25 +28,30 @@ const trimSubdomains = (host) => {
27
28
  }
28
29
  return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
29
30
  };
31
+ const prepareCookieOptions = ({ userAgent: { sameSiteNoneCompatible }, defaultHost, secureProtocol }, { sameSite, noSubdomains, ...options }) => ({
32
+ ...options,
33
+ ...(sameSite === 'none' && (!sameSiteNoneCompatible || !secureProtocol) ? {} : { sameSite }),
34
+ ...(secureProtocol && sameSite === 'none' && sameSiteNoneCompatible ? { secure: true } : {}),
35
+ expires: calculateExpires(options.expires),
36
+ domain: noSubdomains ? trimSubdomains(options.domain || defaultHost) : options.domain,
37
+ });
30
38
 
31
39
  class CookieManager {
32
- constructor({ requestManager, responseManager, }) {
40
+ constructor({ requestManager, responseManager, userAgent, }) {
33
41
  this.requestManager = requestManager;
34
42
  this.responseManager = responseManager;
43
+ this.userAgent = userAgent;
35
44
  this.cookies = { ...requestManager.getCookies() };
36
45
  }
37
46
  get(name) {
38
47
  return this.cookies[name];
39
48
  }
40
- set({ name, value, noSubdomains, ...options }) {
41
- this.responseManager.setCookie(name, serialize(name, value, {
42
- path: '/',
43
- ...options,
44
- expires: calculateExpires(options.expires),
45
- domain: noSubdomains
46
- ? trimSubdomains(options.domain || this.requestManager.getHost())
47
- : options.domain,
48
- }));
49
+ set({ name, value, ...options }) {
50
+ this.responseManager.setCookie(name, serialize(name, value, prepareCookieOptions({
51
+ userAgent: this.userAgent,
52
+ defaultHost: this.requestManager.getHost(),
53
+ secureProtocol: this.requestManager.getUrl().startsWith('https:'),
54
+ }, { path: '/', ...options })));
49
55
  // Записываем в cookie request, так как эти данные могут дальше читаться и использоваться
50
56
  this.cookies[name] = value;
51
57
  }
@@ -62,6 +68,7 @@ let CookieModule = class CookieModule {
62
68
  };
63
69
  CookieModule = __decorate([
64
70
  Module({
71
+ imports: [ClientHintsModule],
65
72
  providers: [
66
73
  {
67
74
  // Управление куками в приложении
@@ -71,6 +78,7 @@ CookieModule = __decorate([
71
78
  deps: {
72
79
  requestManager: REQUEST_MANAGER_TOKEN,
73
80
  responseManager: RESPONSE_MANAGER_TOKEN,
81
+ userAgent: USER_AGENT_TOKEN,
74
82
  },
75
83
  },
76
84
  ],
package/lib/server.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var tslib = require('tslib');
6
6
  var core = require('@tramvai/core');
7
7
  var tokensCommon = require('@tramvai/tokens-common');
8
+ var moduleClientHints = require('@tramvai/module-client-hints');
8
9
  var cookie = require('cookie');
9
10
  var tokensCookie = require('@tramvai/tokens-cookie');
10
11
 
@@ -30,25 +31,30 @@ const trimSubdomains = (host) => {
30
31
  }
31
32
  return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
32
33
  };
34
+ const prepareCookieOptions = ({ userAgent: { sameSiteNoneCompatible }, defaultHost, secureProtocol }, { sameSite, noSubdomains, ...options }) => ({
35
+ ...options,
36
+ ...(sameSite === 'none' && (!sameSiteNoneCompatible || !secureProtocol) ? {} : { sameSite }),
37
+ ...(secureProtocol && sameSite === 'none' && sameSiteNoneCompatible ? { secure: true } : {}),
38
+ expires: calculateExpires(options.expires),
39
+ domain: noSubdomains ? trimSubdomains(options.domain || defaultHost) : options.domain,
40
+ });
33
41
 
34
42
  class CookieManager {
35
- constructor({ requestManager, responseManager, }) {
43
+ constructor({ requestManager, responseManager, userAgent, }) {
36
44
  this.requestManager = requestManager;
37
45
  this.responseManager = responseManager;
46
+ this.userAgent = userAgent;
38
47
  this.cookies = { ...requestManager.getCookies() };
39
48
  }
40
49
  get(name) {
41
50
  return this.cookies[name];
42
51
  }
43
- set({ name, value, noSubdomains, ...options }) {
44
- this.responseManager.setCookie(name, cookie.serialize(name, value, {
45
- path: '/',
46
- ...options,
47
- expires: calculateExpires(options.expires),
48
- domain: noSubdomains
49
- ? trimSubdomains(options.domain || this.requestManager.getHost())
50
- : options.domain,
51
- }));
52
+ set({ name, value, ...options }) {
53
+ this.responseManager.setCookie(name, cookie.serialize(name, value, prepareCookieOptions({
54
+ userAgent: this.userAgent,
55
+ defaultHost: this.requestManager.getHost(),
56
+ secureProtocol: this.requestManager.getUrl().startsWith('https:'),
57
+ }, { path: '/', ...options })));
52
58
  // Записываем в cookie request, так как эти данные могут дальше читаться и использоваться
53
59
  this.cookies[name] = value;
54
60
  }
@@ -65,6 +71,7 @@ exports.CookieModule = class CookieModule {
65
71
  };
66
72
  exports.CookieModule = tslib.__decorate([
67
73
  core.Module({
74
+ imports: [moduleClientHints.ClientHintsModule],
68
75
  providers: [
69
76
  {
70
77
  // Управление куками в приложении
@@ -74,6 +81,7 @@ exports.CookieModule = tslib.__decorate([
74
81
  deps: {
75
82
  requestManager: tokensCommon.REQUEST_MANAGER_TOKEN,
76
83
  responseManager: tokensCommon.RESPONSE_MANAGER_TOKEN,
84
+ userAgent: moduleClientHints.USER_AGENT_TOKEN,
77
85
  },
78
86
  },
79
87
  ],
package/lib/utils.d.ts CHANGED
@@ -1,2 +1,18 @@
1
+ import type { USER_AGENT_TOKEN } from '@tramvai/module-client-hints';
2
+ import type { CookieOptions } from './tokens';
1
3
  export declare const calculateExpires: (expires?: number | string | Date) => Date;
2
4
  export declare const trimSubdomains: (host: string) => string;
5
+ declare type PrepareOptions = {
6
+ userAgent: typeof USER_AGENT_TOKEN;
7
+ defaultHost: string;
8
+ secureProtocol: boolean;
9
+ };
10
+ export declare const prepareCookieOptions: ({ userAgent: { sameSiteNoneCompatible }, defaultHost, secureProtocol }: PrepareOptions, { sameSite, noSubdomains, ...options }: CookieOptions) => {
11
+ expires: Date;
12
+ domain: string;
13
+ secure?: boolean;
14
+ sameSite?: boolean | "lax" | "strict" | "none";
15
+ path?: string;
16
+ httpOnly?: boolean;
17
+ };
18
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-cookie",
3
- "version": "1.90.2",
3
+ "version": "1.91.0",
4
4
  "description": "",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -20,14 +20,15 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@tinkoff/browser-cookies": "1.4.2",
23
- "@tramvai/tokens-cookie": "1.90.2",
23
+ "@tramvai/tokens-cookie": "1.91.0",
24
+ "@tramvai/module-client-hints": "1.91.0",
24
25
  "cookie": "^0.4.0"
25
26
  },
26
27
  "peerDependencies": {
27
28
  "@tinkoff/utils": "^2.1.2",
28
- "@tramvai/core": "1.90.2",
29
- "@tramvai/state": "1.90.2",
30
- "@tramvai/tokens-common": "1.90.2",
29
+ "@tramvai/core": "1.91.0",
30
+ "@tramvai/state": "1.91.0",
31
+ "@tramvai/tokens-common": "1.91.0",
31
32
  "@tinkoff/dippy": "0.7.39",
32
33
  "tslib": "^2.0.3"
33
34
  },