-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
01_webstorage.js
193 lines (163 loc) · 4.72 KB
/
01_webstorage.js
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/// <reference path="../../core/internal.d.ts" />
import { primordials } from "ext:core/mod.js";
import {
op_webstorage_clear,
op_webstorage_get,
op_webstorage_iterate_keys,
op_webstorage_key,
op_webstorage_length,
op_webstorage_remove,
op_webstorage_set,
} from "ext:core/ops";
const {
Symbol,
SymbolFor,
ObjectFromEntries,
ObjectEntries,
ReflectDefineProperty,
ReflectDeleteProperty,
ReflectGet,
ReflectHas,
Proxy,
} = primordials;
import * as webidl from "ext:deno_webidl/00_webidl.js";
const _persistent = Symbol("[[persistent]]");
class Storage {
[_persistent];
constructor() {
webidl.illegalConstructor();
}
get length() {
webidl.assertBranded(this, StoragePrototype);
return op_webstorage_length(this[_persistent]);
}
key(index) {
webidl.assertBranded(this, StoragePrototype);
const prefix = "Failed to execute 'key' on 'Storage'";
webidl.requiredArguments(arguments.length, 1, prefix);
index = webidl.converters["unsigned long"](index, prefix, "Argument 1");
return op_webstorage_key(index, this[_persistent]);
}
setItem(key, value) {
webidl.assertBranded(this, StoragePrototype);
const prefix = "Failed to execute 'setItem' on 'Storage'";
webidl.requiredArguments(arguments.length, 2, prefix);
key = webidl.converters.DOMString(key, prefix, "Argument 1");
value = webidl.converters.DOMString(value, prefix, "Argument 2");
op_webstorage_set(key, value, this[_persistent]);
}
getItem(key) {
webidl.assertBranded(this, StoragePrototype);
const prefix = "Failed to execute 'getItem' on 'Storage'";
webidl.requiredArguments(arguments.length, 1, prefix);
key = webidl.converters.DOMString(key, prefix, "Argument 1");
return op_webstorage_get(key, this[_persistent]);
}
removeItem(key) {
webidl.assertBranded(this, StoragePrototype);
const prefix = "Failed to execute 'removeItem' on 'Storage'";
webidl.requiredArguments(arguments.length, 1, prefix);
key = webidl.converters.DOMString(key, prefix, "Argument 1");
op_webstorage_remove(key, this[_persistent]);
}
clear() {
webidl.assertBranded(this, StoragePrototype);
op_webstorage_clear(this[_persistent]);
}
}
const StoragePrototype = Storage.prototype;
function createStorage(persistent) {
const storage = webidl.createBranded(Storage);
storage[_persistent] = persistent;
const proxy = new Proxy(storage, {
deleteProperty(target, key) {
if (typeof key === "symbol") {
return ReflectDeleteProperty(target, key);
}
target.removeItem(key);
return true;
},
defineProperty(target, key, descriptor) {
if (typeof key === "symbol") {
return ReflectDefineProperty(target, key, descriptor);
}
target.setItem(key, descriptor.value);
return true;
},
get(target, key, receiver) {
if (typeof key === "symbol") {
return target[key];
}
if (ReflectHas(target, key)) {
return ReflectGet(target, key, receiver);
}
return target.getItem(key) ?? undefined;
},
set(target, key, value) {
if (typeof key === "symbol") {
return ReflectDefineProperty(target, key, {
__proto__: null,
value,
configurable: true,
});
}
target.setItem(key, value);
return true;
},
has(target, key) {
if (ReflectHas(target, key)) {
return true;
}
return typeof key === "string" && typeof target.getItem(key) === "string";
},
ownKeys() {
return op_webstorage_iterate_keys(persistent);
},
getOwnPropertyDescriptor(target, key) {
if (ReflectHas(target, key)) {
return undefined;
}
if (typeof key === "symbol") {
return undefined;
}
const value = target.getItem(key);
if (value === null) {
return undefined;
}
return {
value,
enumerable: true,
configurable: true,
writable: true,
};
},
});
storage[SymbolFor("Deno.privateCustomInspect")] = function (
inspect,
inspectOptions,
) {
return `${this.constructor.name} ${
inspect({
...ObjectFromEntries(ObjectEntries(proxy)),
length: this.length,
}, inspectOptions)
}`;
};
return proxy;
}
let localStorageStorage;
function localStorage() {
if (!localStorageStorage) {
localStorageStorage = createStorage(true);
}
return localStorageStorage;
}
let sessionStorageStorage;
function sessionStorage() {
if (!sessionStorageStorage) {
sessionStorageStorage = createStorage(false);
}
return sessionStorageStorage;
}
export { localStorage, sessionStorage, Storage };