-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure-storage.js
More file actions
206 lines (174 loc) · 5.13 KB
/
Copy pathsecure-storage.js
File metadata and controls
206 lines (174 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import ipc from './ipc.js'
import { Buffer } from './buffer.js'
/**
* @module secure-storage
*/
/**
* @typedef {object} SecureStorageOptions
* @property {string|null} [scope] Origin string identifying the storage namespace.
*/
/**
* @typedef {SecureStorageOptions & { encoding?: 'utf8' | 'base64' | 'hex' }} SetItemOptions
*/
/**
* @typedef {SecureStorageOptions & { encoding?: 'utf8' | 'base64' | 'hex' | 'buffer' }} GetItemOptions
*/
const DEFAULT_SCOPE =
typeof globalThis.location?.origin === 'string'
? globalThis.location.origin
: null
/**
* Normalises a user supplied scope.
* @param {string|null|undefined} scope
* @returns {string|null}
*/
function resolveScope (scope) {
if (scope == null) return DEFAULT_SCOPE
if (typeof scope !== 'string') throw new TypeError('scope must be a string')
return scope.trim()
}
/**
* Ensures the key is a non-empty string.
* @param {string} key
* @returns {string}
*/
function normalizeKey (key) {
if (typeof key !== 'string' || key.trim().length === 0) {
throw new TypeError('key must be a non-empty string')
}
return key
}
/**
* Converts binary inputs into a Buffer instance.
* @param {string|Uint8Array|ArrayBuffer|Buffer} value
* @returns {Buffer}
*/
function toBuffer (value) {
if (typeof value === 'string') return Buffer.from(value, 'utf8')
if (Buffer.isBuffer(value)) return value
if (value instanceof Uint8Array) return Buffer.from(value)
if (value instanceof ArrayBuffer) return Buffer.from(new Uint8Array(value))
throw new TypeError(
'value must be a string, Buffer, Uint8Array, or ArrayBuffer'
)
}
/**
* Stores a value inside secure storage for the given key.
* @param {string} key
* @param {string|Uint8Array|ArrayBuffer|Buffer} value
* @param {SetItemOptions} [options]
* @returns {Promise<void>}
*/
export async function setItem (key, value, options = {}) {
key = normalizeKey(key)
const scope = resolveScope(options.scope)
const params = { key }
if (scope) params.scope = scope
let bytes = null
if (typeof value === 'string') {
const encoding = options.encoding || 'utf8'
if (!['utf8', 'base64', 'hex'].includes(encoding)) {
throw new TypeError("encoding must be 'utf8', 'base64', or 'hex'")
}
params.value = value
params.encoding = encoding
} else {
bytes = toBuffer(value)
}
const result = await ipc.send(
'secureStorage.set',
params,
bytes ? { bytes } : null
)
if (result.err) throw result.err
}
/**
* Retrieves a previously stored value.
* @param {string} key
* @param {GetItemOptions} [options]
* @returns {Promise<string|Uint8Array|null>}
*/
export async function getItem (key, options = {}) {
key = normalizeKey(key)
const scope = resolveScope(options.scope)
const encoding = options.encoding || 'utf8'
const params = { key }
if (scope) params.scope = scope
let nativeEncoding = encoding
let returnBuffer = false
if (encoding === 'buffer') {
nativeEncoding = 'base64'
returnBuffer = true
} else if (!['utf8', 'base64', 'hex'].includes(encoding)) {
throw new TypeError("encoding must be 'utf8', 'base64', 'hex', or 'buffer'")
}
params.encoding = nativeEncoding
const result = await ipc.send('secureStorage.get', params)
if (result.err) {
if (result.err.type === 'NotFoundError') return null
throw result.err
}
const value = result.data?.value
if (typeof value !== 'string') return null
if (returnBuffer) {
const buffer = Buffer.from(value, 'base64')
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
}
return value
}
/**
* Removes a single key from secure storage.
* @param {string} key
* @param {SecureStorageOptions} [options]
* @returns {Promise<void>}
*/
export async function removeItem (key, options = {}) {
key = normalizeKey(key)
const scope = resolveScope(options.scope)
const params = { key }
if (scope) params.scope = scope
const result = await ipc.send('secureStorage.remove', params)
if (result.err) throw result.err
}
/**
* Clears all keys for the provided scope (or default scope).
* @param {SecureStorageOptions} [options]
* @returns {Promise<void>}
*/
export async function clear (options = {}) {
const scope = resolveScope(options.scope)
const params = {}
if (scope) params.scope = scope
const result = await ipc.send('secureStorage.clear', params)
if (result.err) throw result.err
}
/**
* Lists the stored keys for the provided scope.
* @param {SecureStorageOptions} [options]
* @returns {Promise<string[]>}
*/
export async function keys (options = {}) {
const scope = resolveScope(options.scope)
const params = {}
if (scope) params.scope = scope
const result = await ipc.send('secureStorage.keys', params)
if (result.err) throw result.err
return Array.isArray(result.data?.keys) ? result.data.keys : []
}
/**
* @typedef {Object} SecureStorageModule
* @property {typeof setItem} setItem
* @property {typeof getItem} getItem
* @property {typeof removeItem} removeItem
* @property {typeof clear} clear
* @property {typeof keys} keys
*/
/** @type {SecureStorageModule} */
const api = {
setItem,
getItem,
removeItem,
clear,
keys
}
export default api