Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/partialCookie.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
const { BytesValue } = require('./networkTypes')
19
20
/**
21
* Represents a partial cookie used to set cookies.
22
* Described in https://w3c.github.io/webdriver-bidi/#command-storage-setCookie.
23
* @class
24
*/
25
class PartialCookie {
26
#map = new Map()
27
28
/**
29
* Represents a partial cookie.
30
* @class
31
* @param {string} name - The name of the cookie.
32
* @param {BytesValue} value - The value of the cookie as an instance of BytesValue.
33
* @param {string} domain - The domain of the cookie.
34
*/
35
constructor(name, value, domain) {
36
this.#map.set('name', name)
37
if (!(value instanceof BytesValue)) {
38
throw new Error(`Value must be an instance of BytesValue. Received:'${value}'`)
39
}
40
this.#map.set('value', Object.fromEntries(value.asMap()))
41
this.#map.set('domain', domain)
42
}
43
44
/**
45
* Sets the path for the cookie.
46
*
47
* @param {string} path - The path for the cookie.
48
* @returns {PartialCookie} - The updated PartialCookie instance for chaining.
49
*/
50
path(path) {
51
this.#map.set('path', path)
52
return this
53
}
54
55
/**
56
* Sets the size of the cookie.
57
*
58
* @param {number} size - The size of the cookie.
59
* @returns {PartialCookie} - The updated PartialCookie instance for chaining.
60
*/
61
size(size) {
62
this.#map.set('size', size)
63
return this
64
}
65
66
/**
67
* Sets the `httpOnly` flag for the cookie.
68
*
69
* @param {boolean} httpOnly - The value to set for the `httpOnly` flag.
70
* @returns {PartialCookie} - The updated PartialCookie instance for chaining.
71
*/
72
httpOnly(httpOnly) {
73
this.#map.set('httpOnly', httpOnly)
74
return this
75
}
76
77
/**
78
* Sets the secure flag for the cookie.
79
*
80
* @param {boolean} secure - Indicates whether the cookie should only be sent over secure connections.
81
* @returns {PartialCookie} - The updated PartialCookie instance for chaining.
82
*/
83
secure(secure) {
84
this.#map.set('secure', secure)
85
return this
86
}
87
88
/**
89
* Sets the SameSite attribute for the cookie.
90
*
91
* @param {SameSite} sameSite - The SameSite attribute value for the cookie.
92
* @returns {PartialCookie} - The updated PartialCookie instance for chaining.
93
*/
94
sameSite(sameSite) {
95
this.#map.set('sameSite', sameSite)
96
return this
97
}
98
99
/**
100
* Sets the expiry for the cookie.
101
*
102
* @param {number} expiry - The expiry time of the cookie.
103
* @returns {PartialCookie} - The updated PartialCookie instance for chaining.
104
*/
105
expiry(expiry) {
106
this.#map.set('expiry', expiry)
107
return this
108
}
109
110
asMap() {
111
return this.#map
112
}
113
}
114
115
module.exports = { PartialCookie }
116
117