@whatwg-node/server-plugin-cookies 0.0.2 → 0.0.3

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.
@@ -10,7 +10,8 @@ class CookieStore extends events_1.EventTarget {
10
10
  }
11
11
  constructor(cookieString) {
12
12
  super();
13
- this.cookieString = cookieString;
13
+ this.cookieMap = new Map();
14
+ this.cookieMap = (0, parse_js_1.parse)(cookieString);
14
15
  }
15
16
  async get(init) {
16
17
  if (init == null) {
@@ -64,37 +65,8 @@ class CookieStore extends events_1.EventTarget {
64
65
  if (item.name && item.name.startsWith('__Host')) {
65
66
  item.secure = true;
66
67
  }
67
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
68
- let cookieString = `${item.name}=${encodeURIComponent(item.value)}`;
69
- if (item.domain) {
70
- cookieString += '; Domain=' + item.domain;
71
- }
72
- if (item.path) {
73
- cookieString += '; Path=' + item.path;
74
- }
75
- if (typeof item.expires === 'number') {
76
- cookieString += '; Expires=' + new Date(item.expires).toUTCString();
77
- }
78
- else if (item.expires instanceof Date) {
79
- cookieString += '; Expires=' + item.expires.toUTCString();
80
- }
81
- if ((item.name && item.name.startsWith('__Secure')) || item.secure) {
82
- item.sameSite = 'lax';
83
- cookieString += '; Secure';
84
- }
85
- switch (item.sameSite) {
86
- case 'lax':
87
- cookieString += '; SameSite=Lax';
88
- break;
89
- case 'strict':
90
- cookieString += '; SameSite=Strict';
91
- break;
92
- case 'none':
93
- cookieString += '; SameSite=None';
94
- break;
95
- }
96
68
  const previousCookie = await this.get(item);
97
- this.cookieString = cookieString;
69
+ this.cookieMap.set(item.name || '', item);
98
70
  if (this.onchange) {
99
71
  const changed = [];
100
72
  const deleted = [];
@@ -104,14 +76,12 @@ class CookieStore extends events_1.EventTarget {
104
76
  else {
105
77
  changed.push(item);
106
78
  }
107
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
108
- // @ts-ignore
109
79
  const event = new CookieChangeEvent_js_1.CookieChangeEvent('change', { changed, deleted });
110
80
  this.onchange(event);
111
81
  }
112
82
  }
113
83
  async getAll(init) {
114
- const cookies = (0, parse_js_1.parse)(this.cookieString);
84
+ const cookies = Array.from(this.cookieMap.values());
115
85
  if (init == null || Object.keys(init).length === 0) {
116
86
  return cookies;
117
87
  }
@@ -143,5 +113,38 @@ class CookieStore extends events_1.EventTarget {
143
113
  item.expires = 0;
144
114
  await this.set(item);
145
115
  }
116
+ get cookieString() {
117
+ const cookieStrings = [];
118
+ for (const [, item] of this.cookieMap) {
119
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
120
+ let cookieString = `${item.name}=${encodeURIComponent(item.value)}`;
121
+ if (item.domain) {
122
+ cookieString += '; Domain=' + item.domain;
123
+ }
124
+ if (item.path) {
125
+ cookieString += '; Path=' + item.path;
126
+ }
127
+ if (typeof item.expires === 'number') {
128
+ cookieString += '; Expires=' + new Date(item.expires).toUTCString();
129
+ }
130
+ if ((item.name && item.name.startsWith('__Secure')) || item.secure) {
131
+ item.sameSite = 'lax';
132
+ cookieString += '; Secure';
133
+ }
134
+ switch (item.sameSite) {
135
+ case 'lax':
136
+ cookieString += '; SameSite=Lax';
137
+ break;
138
+ case 'strict':
139
+ cookieString += '; SameSite=Strict';
140
+ break;
141
+ case 'none':
142
+ cookieString += '; SameSite=None';
143
+ break;
144
+ }
145
+ cookieStrings.push(cookieString);
146
+ }
147
+ return cookieStrings.join('; ');
148
+ }
146
149
  }
147
150
  exports.CookieStore = CookieStore;
package/cjs/parse.js CHANGED
@@ -22,7 +22,7 @@ function parse(str, options = {}) {
22
22
  if (typeof str !== 'string') {
23
23
  throw new TypeError('argument str must be a string');
24
24
  }
25
- const obj = [];
25
+ const map = new Map();
26
26
  const opt = options || {};
27
27
  const pairs = str.split(pairSplitRegExp);
28
28
  const dec = opt.decode || decode;
@@ -39,14 +39,11 @@ function parse(str, options = {}) {
39
39
  if (val[0] === '"') {
40
40
  val = val.slice(1, -1);
41
41
  }
42
- // only assign once
43
- if (obj[key] == null) {
44
- obj.push({
45
- name: key,
46
- value: tryDecode(val, dec),
47
- });
42
+ const cookiesPerKey = map.get(key);
43
+ if (!cookiesPerKey) {
44
+ map.set(key, { name: key, value: tryDecode(val, dec) });
48
45
  }
49
46
  }
50
- return obj;
47
+ return map;
51
48
  }
52
49
  exports.parse = parse;
@@ -7,7 +7,8 @@ export class CookieStore extends EventTarget {
7
7
  }
8
8
  constructor(cookieString) {
9
9
  super();
10
- this.cookieString = cookieString;
10
+ this.cookieMap = new Map();
11
+ this.cookieMap = parse(cookieString);
11
12
  }
12
13
  async get(init) {
13
14
  if (init == null) {
@@ -61,37 +62,8 @@ export class CookieStore extends EventTarget {
61
62
  if (item.name && item.name.startsWith('__Host')) {
62
63
  item.secure = true;
63
64
  }
64
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
65
- let cookieString = `${item.name}=${encodeURIComponent(item.value)}`;
66
- if (item.domain) {
67
- cookieString += '; Domain=' + item.domain;
68
- }
69
- if (item.path) {
70
- cookieString += '; Path=' + item.path;
71
- }
72
- if (typeof item.expires === 'number') {
73
- cookieString += '; Expires=' + new Date(item.expires).toUTCString();
74
- }
75
- else if (item.expires instanceof Date) {
76
- cookieString += '; Expires=' + item.expires.toUTCString();
77
- }
78
- if ((item.name && item.name.startsWith('__Secure')) || item.secure) {
79
- item.sameSite = 'lax';
80
- cookieString += '; Secure';
81
- }
82
- switch (item.sameSite) {
83
- case 'lax':
84
- cookieString += '; SameSite=Lax';
85
- break;
86
- case 'strict':
87
- cookieString += '; SameSite=Strict';
88
- break;
89
- case 'none':
90
- cookieString += '; SameSite=None';
91
- break;
92
- }
93
65
  const previousCookie = await this.get(item);
94
- this.cookieString = cookieString;
66
+ this.cookieMap.set(item.name || '', item);
95
67
  if (this.onchange) {
96
68
  const changed = [];
97
69
  const deleted = [];
@@ -101,14 +73,12 @@ export class CookieStore extends EventTarget {
101
73
  else {
102
74
  changed.push(item);
103
75
  }
104
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
105
- // @ts-ignore
106
76
  const event = new CookieChangeEvent('change', { changed, deleted });
107
77
  this.onchange(event);
108
78
  }
109
79
  }
110
80
  async getAll(init) {
111
- const cookies = parse(this.cookieString);
81
+ const cookies = Array.from(this.cookieMap.values());
112
82
  if (init == null || Object.keys(init).length === 0) {
113
83
  return cookies;
114
84
  }
@@ -140,4 +110,37 @@ export class CookieStore extends EventTarget {
140
110
  item.expires = 0;
141
111
  await this.set(item);
142
112
  }
113
+ get cookieString() {
114
+ const cookieStrings = [];
115
+ for (const [, item] of this.cookieMap) {
116
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
117
+ let cookieString = `${item.name}=${encodeURIComponent(item.value)}`;
118
+ if (item.domain) {
119
+ cookieString += '; Domain=' + item.domain;
120
+ }
121
+ if (item.path) {
122
+ cookieString += '; Path=' + item.path;
123
+ }
124
+ if (typeof item.expires === 'number') {
125
+ cookieString += '; Expires=' + new Date(item.expires).toUTCString();
126
+ }
127
+ if ((item.name && item.name.startsWith('__Secure')) || item.secure) {
128
+ item.sameSite = 'lax';
129
+ cookieString += '; Secure';
130
+ }
131
+ switch (item.sameSite) {
132
+ case 'lax':
133
+ cookieString += '; SameSite=Lax';
134
+ break;
135
+ case 'strict':
136
+ cookieString += '; SameSite=Strict';
137
+ break;
138
+ case 'none':
139
+ cookieString += '; SameSite=None';
140
+ break;
141
+ }
142
+ cookieStrings.push(cookieString);
143
+ }
144
+ return cookieStrings.join('; ');
145
+ }
143
146
  }
package/esm/parse.js CHANGED
@@ -19,7 +19,7 @@ export function parse(str, options = {}) {
19
19
  if (typeof str !== 'string') {
20
20
  throw new TypeError('argument str must be a string');
21
21
  }
22
- const obj = [];
22
+ const map = new Map();
23
23
  const opt = options || {};
24
24
  const pairs = str.split(pairSplitRegExp);
25
25
  const dec = opt.decode || decode;
@@ -36,13 +36,10 @@ export function parse(str, options = {}) {
36
36
  if (val[0] === '"') {
37
37
  val = val.slice(1, -1);
38
38
  }
39
- // only assign once
40
- if (obj[key] == null) {
41
- obj.push({
42
- name: key,
43
- value: tryDecode(val, dec),
44
- });
39
+ const cookiesPerKey = map.get(key);
40
+ if (!cookiesPerKey) {
41
+ map.set(key, { name: key, value: tryDecode(val, dec) });
45
42
  }
46
43
  }
47
- return obj;
44
+ return map;
48
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/server-plugin-cookies",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Cookies Plugin",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -2,12 +2,13 @@ import { EventTarget } from '@whatwg-node/events';
2
2
  import { CookieChangeEvent } from './CookieChangeEvent.cjs';
3
3
  import { Cookie, CookieListItem, CookieStoreDeleteOptions, CookieStoreGetOptions } from './types.cjs';
4
4
  export declare class CookieStore extends EventTarget {
5
- cookieString: string;
6
5
  onchange?: (event: CookieChangeEvent) => void;
6
+ private cookieMap;
7
7
  get [Symbol.toStringTag](): 'CookieStore';
8
8
  constructor(cookieString: string);
9
9
  get(init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions): Promise<Cookie | undefined>;
10
10
  set(init: CookieListItem | string, possibleValue?: string): Promise<void>;
11
11
  getAll(init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions): Promise<Cookie[]>;
12
12
  delete(init: CookieStoreDeleteOptions['name'] | CookieStoreDeleteOptions): Promise<void>;
13
+ get cookieString(): string;
13
14
  }
@@ -2,12 +2,13 @@ import { EventTarget } from '@whatwg-node/events';
2
2
  import { CookieChangeEvent } from './CookieChangeEvent.js';
3
3
  import { Cookie, CookieListItem, CookieStoreDeleteOptions, CookieStoreGetOptions } from './types.js';
4
4
  export declare class CookieStore extends EventTarget {
5
- cookieString: string;
6
5
  onchange?: (event: CookieChangeEvent) => void;
6
+ private cookieMap;
7
7
  get [Symbol.toStringTag](): 'CookieStore';
8
8
  constructor(cookieString: string);
9
9
  get(init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions): Promise<Cookie | undefined>;
10
10
  set(init: CookieListItem | string, possibleValue?: string): Promise<void>;
11
11
  getAll(init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions): Promise<Cookie[]>;
12
12
  delete(init: CookieStoreDeleteOptions['name'] | CookieStoreDeleteOptions): Promise<void>;
13
+ get cookieString(): string;
13
14
  }
@@ -8,4 +8,4 @@ export interface ParseOptions {
8
8
  * Parse the given cookie header string into an object
9
9
  * The object has the various cookies as keys(names) => values
10
10
  */
11
- export declare function parse(str: string, options?: ParseOptions): Cookie[];
11
+ export declare function parse(str: string, options?: ParseOptions): Map<string, Cookie>;
@@ -8,4 +8,4 @@ export interface ParseOptions {
8
8
  * Parse the given cookie header string into an object
9
9
  * The object has the various cookies as keys(names) => values
10
10
  */
11
- export declare function parse(str: string, options?: ParseOptions): Cookie[];
11
+ export declare function parse(str: string, options?: ParseOptions): Map<string, Cookie>;