@tramvai/module-cookie 2.70.1 → 2.72.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 +1 -91
- package/lib/cookieManager.browser.browser.js +64 -0
- package/lib/cookieManager.server.es.js +32 -0
- package/lib/cookieManager.server.js +36 -0
- package/lib/server.es.js +1 -59
- package/lib/server.js +2 -60
- package/lib/utils.browser.js +31 -0
- package/lib/utils.es.js +31 -0
- package/lib/utils.js +37 -0
- package/package.json +10 -11
package/lib/browser.js
CHANGED
|
@@ -1,100 +1,10 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
2
|
import { Module, provide, Scope } from '@tramvai/core';
|
|
3
3
|
import { ClientHintsModule, USER_AGENT_TOKEN } from '@tramvai/module-client-hints';
|
|
4
|
-
import {
|
|
4
|
+
import { CookieManager } from './cookieManager.browser.browser.js';
|
|
5
5
|
import { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
|
|
6
6
|
export { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
|
|
7
7
|
|
|
8
|
-
// ipv4 + IPv4-mapped IPv6 addresses and IPv4-translated addresses
|
|
9
|
-
const reIpWithDots = /^(?:::(?:ffff:(0:)?)?)?(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/; // IPv4-Embedded IPv6 Address
|
|
10
|
-
const reIpv4Embedded = /^([0-9a-fA-F]{1,4}:){1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
|
|
11
|
-
const calculateExpires = (expires) => {
|
|
12
|
-
// if expires is number than treat it as seconds
|
|
13
|
-
if (!expires) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
return new Date(typeof expires === 'number' ? +new Date() + expires * 1000 : expires);
|
|
17
|
-
};
|
|
18
|
-
const isIpWithDots = (host) => {
|
|
19
|
-
return reIpWithDots.test(host) || reIpv4Embedded.test(host);
|
|
20
|
-
};
|
|
21
|
-
const trimPort = (host) => {
|
|
22
|
-
return host.replace(/:\d+$/, '');
|
|
23
|
-
};
|
|
24
|
-
const trimSubdomains = (host) => {
|
|
25
|
-
if (isIpWithDots(host)) {
|
|
26
|
-
return host;
|
|
27
|
-
}
|
|
28
|
-
return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
|
|
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
|
-
});
|
|
37
|
-
|
|
38
|
-
const checkCookieEnabled = () => {
|
|
39
|
-
const testCookieName = 'testcookiesenabled';
|
|
40
|
-
document.cookie = testCookieName;
|
|
41
|
-
if (document.cookie.indexOf(testCookieName) > -1) {
|
|
42
|
-
document.cookie = `${testCookieName}; expires=Thu, 01 Jan 1970 00:00:01 GMT'`;
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
};
|
|
47
|
-
class CookiesFallback {
|
|
48
|
-
constructor() {
|
|
49
|
-
this.cache = Object.create(null);
|
|
50
|
-
}
|
|
51
|
-
get(name) {
|
|
52
|
-
return this.cache[name] || undefined;
|
|
53
|
-
}
|
|
54
|
-
set(name, value) {
|
|
55
|
-
this.cache[name] = value;
|
|
56
|
-
}
|
|
57
|
-
erase(name) {
|
|
58
|
-
delete this.cache[name];
|
|
59
|
-
}
|
|
60
|
-
all() {
|
|
61
|
-
return this.cache;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
class CookieManager {
|
|
65
|
-
constructor({ cookieOptions = {}, userAgent, }) {
|
|
66
|
-
const isSecure = window.location.protocol === 'https:';
|
|
67
|
-
this.cookies = checkCookieEnabled()
|
|
68
|
-
? new Cookies({
|
|
69
|
-
sameSite: userAgent.sameSiteNoneCompatible && isSecure ? 'none' : 'lax',
|
|
70
|
-
secure: isSecure,
|
|
71
|
-
...cookieOptions,
|
|
72
|
-
})
|
|
73
|
-
: new CookiesFallback();
|
|
74
|
-
this.userAgent = userAgent;
|
|
75
|
-
}
|
|
76
|
-
// eslint-disable-next-line class-methods-use-this
|
|
77
|
-
get(name) {
|
|
78
|
-
return this.cookies.get(name) || undefined;
|
|
79
|
-
}
|
|
80
|
-
// eslint-disable-next-line class-methods-use-this
|
|
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));
|
|
87
|
-
}
|
|
88
|
-
// eslint-disable-next-line class-methods-use-this
|
|
89
|
-
all() {
|
|
90
|
-
return this.cookies.all();
|
|
91
|
-
}
|
|
92
|
-
// eslint-disable-next-line class-methods-use-this
|
|
93
|
-
remove(name, options) {
|
|
94
|
-
return this.cookies.erase(name, options);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
8
|
let CookieModule = class CookieModule {
|
|
99
9
|
};
|
|
100
10
|
CookieModule = __decorate([
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Cookies } from '@tinkoff/browser-cookies';
|
|
2
|
+
import { prepareCookieOptions } from './utils.browser.js';
|
|
3
|
+
|
|
4
|
+
const checkCookieEnabled = () => {
|
|
5
|
+
const testCookieName = 'testcookiesenabled';
|
|
6
|
+
document.cookie = testCookieName;
|
|
7
|
+
if (document.cookie.indexOf(testCookieName) > -1) {
|
|
8
|
+
document.cookie = `${testCookieName}; expires=Thu, 01 Jan 1970 00:00:01 GMT'`;
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
return false;
|
|
12
|
+
};
|
|
13
|
+
class CookiesFallback {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.cache = Object.create(null);
|
|
16
|
+
}
|
|
17
|
+
get(name) {
|
|
18
|
+
return this.cache[name] || undefined;
|
|
19
|
+
}
|
|
20
|
+
set(name, value) {
|
|
21
|
+
this.cache[name] = value;
|
|
22
|
+
}
|
|
23
|
+
erase(name) {
|
|
24
|
+
delete this.cache[name];
|
|
25
|
+
}
|
|
26
|
+
all() {
|
|
27
|
+
return this.cache;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
class CookieManager {
|
|
31
|
+
constructor({ cookieOptions = {}, userAgent, }) {
|
|
32
|
+
const isSecure = window.location.protocol === 'https:';
|
|
33
|
+
this.cookies = checkCookieEnabled()
|
|
34
|
+
? new Cookies({
|
|
35
|
+
sameSite: userAgent.sameSiteNoneCompatible && isSecure ? 'none' : 'lax',
|
|
36
|
+
secure: isSecure,
|
|
37
|
+
...cookieOptions,
|
|
38
|
+
})
|
|
39
|
+
: new CookiesFallback();
|
|
40
|
+
this.userAgent = userAgent;
|
|
41
|
+
}
|
|
42
|
+
// eslint-disable-next-line class-methods-use-this
|
|
43
|
+
get(name) {
|
|
44
|
+
return this.cookies.get(name) || undefined;
|
|
45
|
+
}
|
|
46
|
+
// eslint-disable-next-line class-methods-use-this
|
|
47
|
+
set({ name, value, ...options }) {
|
|
48
|
+
this.cookies.set(name, value, prepareCookieOptions({
|
|
49
|
+
userAgent: this.userAgent,
|
|
50
|
+
defaultHost: window.location.hostname,
|
|
51
|
+
secureProtocol: window.location.protocol === 'https:',
|
|
52
|
+
}, options));
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line class-methods-use-this
|
|
55
|
+
all() {
|
|
56
|
+
return this.cookies.all();
|
|
57
|
+
}
|
|
58
|
+
// eslint-disable-next-line class-methods-use-this
|
|
59
|
+
remove(name, options) {
|
|
60
|
+
return this.cookies.erase(name, options);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { CookieManager };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { serialize } from 'cookie';
|
|
2
|
+
import { prepareCookieOptions } from './utils.es.js';
|
|
3
|
+
|
|
4
|
+
class CookieManager {
|
|
5
|
+
constructor({ requestManager, responseManager, userAgent, }) {
|
|
6
|
+
this.requestManager = requestManager;
|
|
7
|
+
this.responseManager = responseManager;
|
|
8
|
+
this.userAgent = userAgent;
|
|
9
|
+
this.cookies = { ...requestManager.getCookies() };
|
|
10
|
+
}
|
|
11
|
+
get(name) {
|
|
12
|
+
return this.cookies[name];
|
|
13
|
+
}
|
|
14
|
+
set({ name, value, ...options }) {
|
|
15
|
+
this.responseManager.setCookie(name, serialize(name, value, prepareCookieOptions({
|
|
16
|
+
userAgent: this.userAgent,
|
|
17
|
+
defaultHost: this.requestManager.getHost(),
|
|
18
|
+
secureProtocol: this.requestManager.getUrl().startsWith('https:'),
|
|
19
|
+
}, { path: '/', ...options })));
|
|
20
|
+
// Записываем в cookie request, так как эти данные могут дальше читаться и использоваться
|
|
21
|
+
this.cookies[name] = value;
|
|
22
|
+
}
|
|
23
|
+
all() {
|
|
24
|
+
return this.cookies;
|
|
25
|
+
}
|
|
26
|
+
remove(name, options) {
|
|
27
|
+
this.set({ ...options, name, value: '', expires: new Date(0) });
|
|
28
|
+
delete this.cookies[name];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { CookieManager };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var cookie = require('cookie');
|
|
6
|
+
var utils = require('./utils.js');
|
|
7
|
+
|
|
8
|
+
class CookieManager {
|
|
9
|
+
constructor({ requestManager, responseManager, userAgent, }) {
|
|
10
|
+
this.requestManager = requestManager;
|
|
11
|
+
this.responseManager = responseManager;
|
|
12
|
+
this.userAgent = userAgent;
|
|
13
|
+
this.cookies = { ...requestManager.getCookies() };
|
|
14
|
+
}
|
|
15
|
+
get(name) {
|
|
16
|
+
return this.cookies[name];
|
|
17
|
+
}
|
|
18
|
+
set({ name, value, ...options }) {
|
|
19
|
+
this.responseManager.setCookie(name, cookie.serialize(name, value, utils.prepareCookieOptions({
|
|
20
|
+
userAgent: this.userAgent,
|
|
21
|
+
defaultHost: this.requestManager.getHost(),
|
|
22
|
+
secureProtocol: this.requestManager.getUrl().startsWith('https:'),
|
|
23
|
+
}, { path: '/', ...options })));
|
|
24
|
+
// Записываем в cookie request, так как эти данные могут дальше читаться и использоваться
|
|
25
|
+
this.cookies[name] = value;
|
|
26
|
+
}
|
|
27
|
+
all() {
|
|
28
|
+
return this.cookies;
|
|
29
|
+
}
|
|
30
|
+
remove(name, options) {
|
|
31
|
+
this.set({ ...options, name, value: '', expires: new Date(0) });
|
|
32
|
+
delete this.cookies[name];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.CookieManager = CookieManager;
|
package/lib/server.es.js
CHANGED
|
@@ -2,68 +2,10 @@ import { __decorate } from 'tslib';
|
|
|
2
2
|
import { Module, provide, Scope } from '@tramvai/core';
|
|
3
3
|
import { REQUEST_MANAGER_TOKEN, RESPONSE_MANAGER_TOKEN } from '@tramvai/tokens-common';
|
|
4
4
|
import { ClientHintsModule, USER_AGENT_TOKEN } from '@tramvai/module-client-hints';
|
|
5
|
-
import {
|
|
5
|
+
import { CookieManager } from './cookieManager.server.es.js';
|
|
6
6
|
import { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
|
|
7
7
|
export { COOKIE_MANAGER_TOKEN } from '@tramvai/tokens-cookie';
|
|
8
8
|
|
|
9
|
-
// ipv4 + IPv4-mapped IPv6 addresses and IPv4-translated addresses
|
|
10
|
-
const reIpWithDots = /^(?:::(?:ffff:(0:)?)?)?(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/; // IPv4-Embedded IPv6 Address
|
|
11
|
-
const reIpv4Embedded = /^([0-9a-fA-F]{1,4}:){1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
|
|
12
|
-
const calculateExpires = (expires) => {
|
|
13
|
-
// if expires is number than treat it as seconds
|
|
14
|
-
if (!expires) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
return new Date(typeof expires === 'number' ? +new Date() + expires * 1000 : expires);
|
|
18
|
-
};
|
|
19
|
-
const isIpWithDots = (host) => {
|
|
20
|
-
return reIpWithDots.test(host) || reIpv4Embedded.test(host);
|
|
21
|
-
};
|
|
22
|
-
const trimPort = (host) => {
|
|
23
|
-
return host.replace(/:\d+$/, '');
|
|
24
|
-
};
|
|
25
|
-
const trimSubdomains = (host) => {
|
|
26
|
-
if (isIpWithDots(host)) {
|
|
27
|
-
return host;
|
|
28
|
-
}
|
|
29
|
-
return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
|
|
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
|
-
});
|
|
38
|
-
|
|
39
|
-
class CookieManager {
|
|
40
|
-
constructor({ requestManager, responseManager, userAgent, }) {
|
|
41
|
-
this.requestManager = requestManager;
|
|
42
|
-
this.responseManager = responseManager;
|
|
43
|
-
this.userAgent = userAgent;
|
|
44
|
-
this.cookies = { ...requestManager.getCookies() };
|
|
45
|
-
}
|
|
46
|
-
get(name) {
|
|
47
|
-
return this.cookies[name];
|
|
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 })));
|
|
55
|
-
// Записываем в cookie request, так как эти данные могут дальше читаться и использоваться
|
|
56
|
-
this.cookies[name] = value;
|
|
57
|
-
}
|
|
58
|
-
all() {
|
|
59
|
-
return this.cookies;
|
|
60
|
-
}
|
|
61
|
-
remove(name, options) {
|
|
62
|
-
this.set({ ...options, name, value: '', expires: new Date(0) });
|
|
63
|
-
delete this.cookies[name];
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
9
|
let CookieModule = class CookieModule {
|
|
68
10
|
};
|
|
69
11
|
CookieModule = __decorate([
|
package/lib/server.js
CHANGED
|
@@ -6,67 +6,9 @@ var tslib = require('tslib');
|
|
|
6
6
|
var core = require('@tramvai/core');
|
|
7
7
|
var tokensCommon = require('@tramvai/tokens-common');
|
|
8
8
|
var moduleClientHints = require('@tramvai/module-client-hints');
|
|
9
|
-
var
|
|
9
|
+
var cookieManager_server = require('./cookieManager.server.js');
|
|
10
10
|
var tokensCookie = require('@tramvai/tokens-cookie');
|
|
11
11
|
|
|
12
|
-
// ipv4 + IPv4-mapped IPv6 addresses and IPv4-translated addresses
|
|
13
|
-
const reIpWithDots = /^(?:::(?:ffff:(0:)?)?)?(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/; // IPv4-Embedded IPv6 Address
|
|
14
|
-
const reIpv4Embedded = /^([0-9a-fA-F]{1,4}:){1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
|
|
15
|
-
const calculateExpires = (expires) => {
|
|
16
|
-
// if expires is number than treat it as seconds
|
|
17
|
-
if (!expires) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
return new Date(typeof expires === 'number' ? +new Date() + expires * 1000 : expires);
|
|
21
|
-
};
|
|
22
|
-
const isIpWithDots = (host) => {
|
|
23
|
-
return reIpWithDots.test(host) || reIpv4Embedded.test(host);
|
|
24
|
-
};
|
|
25
|
-
const trimPort = (host) => {
|
|
26
|
-
return host.replace(/:\d+$/, '');
|
|
27
|
-
};
|
|
28
|
-
const trimSubdomains = (host) => {
|
|
29
|
-
if (isIpWithDots(host)) {
|
|
30
|
-
return host;
|
|
31
|
-
}
|
|
32
|
-
return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
|
|
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
|
-
});
|
|
41
|
-
|
|
42
|
-
class CookieManager {
|
|
43
|
-
constructor({ requestManager, responseManager, userAgent, }) {
|
|
44
|
-
this.requestManager = requestManager;
|
|
45
|
-
this.responseManager = responseManager;
|
|
46
|
-
this.userAgent = userAgent;
|
|
47
|
-
this.cookies = { ...requestManager.getCookies() };
|
|
48
|
-
}
|
|
49
|
-
get(name) {
|
|
50
|
-
return this.cookies[name];
|
|
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 })));
|
|
58
|
-
// Записываем в cookie request, так как эти данные могут дальше читаться и использоваться
|
|
59
|
-
this.cookies[name] = value;
|
|
60
|
-
}
|
|
61
|
-
all() {
|
|
62
|
-
return this.cookies;
|
|
63
|
-
}
|
|
64
|
-
remove(name, options) {
|
|
65
|
-
this.set({ ...options, name, value: '', expires: new Date(0) });
|
|
66
|
-
delete this.cookies[name];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
12
|
exports.CookieModule = class CookieModule {
|
|
71
13
|
};
|
|
72
14
|
exports.CookieModule = tslib.__decorate([
|
|
@@ -76,7 +18,7 @@ exports.CookieModule = tslib.__decorate([
|
|
|
76
18
|
core.provide({
|
|
77
19
|
// Управление куками в приложении
|
|
78
20
|
provide: tokensCookie.COOKIE_MANAGER_TOKEN,
|
|
79
|
-
useClass: CookieManager,
|
|
21
|
+
useClass: cookieManager_server.CookieManager,
|
|
80
22
|
scope: core.Scope.REQUEST,
|
|
81
23
|
deps: {
|
|
82
24
|
requestManager: tokensCommon.REQUEST_MANAGER_TOKEN,
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// ipv4 + IPv4-mapped IPv6 addresses and IPv4-translated addresses
|
|
2
|
+
const reIpWithDots = /^(?:::(?:ffff:(0:)?)?)?(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/; // IPv4-Embedded IPv6 Address
|
|
3
|
+
const reIpv4Embedded = /^([0-9a-fA-F]{1,4}:){1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
|
|
4
|
+
const calculateExpires = (expires) => {
|
|
5
|
+
// if expires is number than treat it as seconds
|
|
6
|
+
if (!expires) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
return new Date(typeof expires === 'number' ? +new Date() + expires * 1000 : expires);
|
|
10
|
+
};
|
|
11
|
+
const isIpWithDots = (host) => {
|
|
12
|
+
return reIpWithDots.test(host) || reIpv4Embedded.test(host);
|
|
13
|
+
};
|
|
14
|
+
const trimPort = (host) => {
|
|
15
|
+
return host.replace(/:\d+$/, '');
|
|
16
|
+
};
|
|
17
|
+
const trimSubdomains = (host) => {
|
|
18
|
+
if (isIpWithDots(host)) {
|
|
19
|
+
return host;
|
|
20
|
+
}
|
|
21
|
+
return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
|
|
22
|
+
};
|
|
23
|
+
const prepareCookieOptions = ({ userAgent: { sameSiteNoneCompatible }, defaultHost, secureProtocol }, { sameSite, noSubdomains, ...options }) => ({
|
|
24
|
+
...options,
|
|
25
|
+
...(sameSite === 'none' && (!sameSiteNoneCompatible || !secureProtocol) ? {} : { sameSite }),
|
|
26
|
+
...(secureProtocol && sameSite === 'none' && sameSiteNoneCompatible ? { secure: true } : {}),
|
|
27
|
+
expires: calculateExpires(options.expires),
|
|
28
|
+
domain: noSubdomains ? trimSubdomains(options.domain || defaultHost) : options.domain,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export { calculateExpires, prepareCookieOptions, trimSubdomains };
|
package/lib/utils.es.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// ipv4 + IPv4-mapped IPv6 addresses and IPv4-translated addresses
|
|
2
|
+
const reIpWithDots = /^(?:::(?:ffff:(0:)?)?)?(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/; // IPv4-Embedded IPv6 Address
|
|
3
|
+
const reIpv4Embedded = /^([0-9a-fA-F]{1,4}:){1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
|
|
4
|
+
const calculateExpires = (expires) => {
|
|
5
|
+
// if expires is number than treat it as seconds
|
|
6
|
+
if (!expires) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
return new Date(typeof expires === 'number' ? +new Date() + expires * 1000 : expires);
|
|
10
|
+
};
|
|
11
|
+
const isIpWithDots = (host) => {
|
|
12
|
+
return reIpWithDots.test(host) || reIpv4Embedded.test(host);
|
|
13
|
+
};
|
|
14
|
+
const trimPort = (host) => {
|
|
15
|
+
return host.replace(/:\d+$/, '');
|
|
16
|
+
};
|
|
17
|
+
const trimSubdomains = (host) => {
|
|
18
|
+
if (isIpWithDots(host)) {
|
|
19
|
+
return host;
|
|
20
|
+
}
|
|
21
|
+
return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
|
|
22
|
+
};
|
|
23
|
+
const prepareCookieOptions = ({ userAgent: { sameSiteNoneCompatible }, defaultHost, secureProtocol }, { sameSite, noSubdomains, ...options }) => ({
|
|
24
|
+
...options,
|
|
25
|
+
...(sameSite === 'none' && (!sameSiteNoneCompatible || !secureProtocol) ? {} : { sameSite }),
|
|
26
|
+
...(secureProtocol && sameSite === 'none' && sameSiteNoneCompatible ? { secure: true } : {}),
|
|
27
|
+
expires: calculateExpires(options.expires),
|
|
28
|
+
domain: noSubdomains ? trimSubdomains(options.domain || defaultHost) : options.domain,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export { calculateExpires, prepareCookieOptions, trimSubdomains };
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
// ipv4 + IPv4-mapped IPv6 addresses and IPv4-translated addresses
|
|
6
|
+
const reIpWithDots = /^(?:::(?:ffff:(0:)?)?)?(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/; // IPv4-Embedded IPv6 Address
|
|
7
|
+
const reIpv4Embedded = /^([0-9a-fA-F]{1,4}:){1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
|
|
8
|
+
const calculateExpires = (expires) => {
|
|
9
|
+
// if expires is number than treat it as seconds
|
|
10
|
+
if (!expires) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
return new Date(typeof expires === 'number' ? +new Date() + expires * 1000 : expires);
|
|
14
|
+
};
|
|
15
|
+
const isIpWithDots = (host) => {
|
|
16
|
+
return reIpWithDots.test(host) || reIpv4Embedded.test(host);
|
|
17
|
+
};
|
|
18
|
+
const trimPort = (host) => {
|
|
19
|
+
return host.replace(/:\d+$/, '');
|
|
20
|
+
};
|
|
21
|
+
const trimSubdomains = (host) => {
|
|
22
|
+
if (isIpWithDots(host)) {
|
|
23
|
+
return host;
|
|
24
|
+
}
|
|
25
|
+
return trimPort(host.indexOf('localhost') >= 0 ? host : `.${host.split('.').slice(-2).join('.')}`);
|
|
26
|
+
};
|
|
27
|
+
const prepareCookieOptions = ({ userAgent: { sameSiteNoneCompatible }, defaultHost, secureProtocol }, { sameSite, noSubdomains, ...options }) => ({
|
|
28
|
+
...options,
|
|
29
|
+
...(sameSite === 'none' && (!sameSiteNoneCompatible || !secureProtocol) ? {} : { sameSite }),
|
|
30
|
+
...(secureProtocol && sameSite === 'none' && sameSiteNoneCompatible ? { secure: true } : {}),
|
|
31
|
+
expires: calculateExpires(options.expires),
|
|
32
|
+
domain: noSubdomains ? trimSubdomains(options.domain || defaultHost) : options.domain,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
exports.calculateExpires = calculateExpires;
|
|
36
|
+
exports.prepareCookieOptions = prepareCookieOptions;
|
|
37
|
+
exports.trimSubdomains = trimSubdomains;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-cookie",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.72.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -14,22 +14,21 @@
|
|
|
14
14
|
"url": "git@github.com:Tinkoff/tramvai.git"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
|
-
"build": "tramvai-build --
|
|
18
|
-
"watch": "tsc -w"
|
|
19
|
-
"build-for-publish": "true"
|
|
17
|
+
"build": "tramvai-build --forPublish --preserveModules",
|
|
18
|
+
"watch": "tsc -w"
|
|
20
19
|
},
|
|
21
20
|
"dependencies": {
|
|
22
|
-
"@tinkoff/browser-cookies": "2.0.
|
|
23
|
-
"@tramvai/tokens-cookie": "2.
|
|
24
|
-
"@tramvai/module-client-hints": "2.
|
|
21
|
+
"@tinkoff/browser-cookies": "2.0.6",
|
|
22
|
+
"@tramvai/tokens-cookie": "2.72.0",
|
|
23
|
+
"@tramvai/module-client-hints": "2.72.0",
|
|
25
24
|
"cookie": "^0.4.0"
|
|
26
25
|
},
|
|
27
26
|
"peerDependencies": {
|
|
28
27
|
"@tinkoff/utils": "^2.1.2",
|
|
29
|
-
"@tramvai/core": "2.
|
|
30
|
-
"@tramvai/state": "2.
|
|
31
|
-
"@tramvai/tokens-common": "2.
|
|
32
|
-
"@tinkoff/dippy": "0.8.
|
|
28
|
+
"@tramvai/core": "2.72.0",
|
|
29
|
+
"@tramvai/state": "2.72.0",
|
|
30
|
+
"@tramvai/tokens-common": "2.72.0",
|
|
31
|
+
"@tinkoff/dippy": "0.8.13",
|
|
33
32
|
"tslib": "^2.4.0"
|
|
34
33
|
},
|
|
35
34
|
"devDependencies": {
|