2021-05-19 12:28:50 +00:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2021-05-10 10:02:47 +00:00
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
|
|
|
const webidl = window.__bootstrap.webidl;
|
|
|
|
|
2021-05-19 12:28:50 +00:00
|
|
|
const _persistent = Symbol("[[persistent]]");
|
2021-05-10 10:02:47 +00:00
|
|
|
|
|
|
|
class Storage {
|
2021-05-19 12:28:50 +00:00
|
|
|
[_persistent];
|
2021-05-10 10:02:47 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
get length() {
|
|
|
|
webidl.assertBranded(this, Storage);
|
2021-05-19 12:28:50 +00:00
|
|
|
return core.opSync("op_webstorage_length", this[_persistent]);
|
2021-05-10 10:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key(index) {
|
|
|
|
webidl.assertBranded(this, Storage);
|
|
|
|
const prefix = "Failed to execute 'key' on 'Storage'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
index = webidl.converters["unsigned long"](index, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
2021-05-19 12:28:50 +00:00
|
|
|
return core.opSync("op_webstorage_key", index, this[_persistent]);
|
2021-05-10 10:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setItem(key, value) {
|
|
|
|
webidl.assertBranded(this, Storage);
|
|
|
|
const prefix = "Failed to execute 'setItem' on 'Storage'";
|
|
|
|
webidl.requiredArguments(arguments.length, 2, { prefix });
|
|
|
|
key = webidl.converters.DOMString(key, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
value = webidl.converters.DOMString(value, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
|
|
|
|
core.opSync("op_webstorage_set", {
|
|
|
|
keyName: key,
|
|
|
|
keyValue: value,
|
2021-05-19 12:28:50 +00:00
|
|
|
}, this[_persistent]);
|
2021-05-10 10:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getItem(key) {
|
|
|
|
webidl.assertBranded(this, Storage);
|
|
|
|
const prefix = "Failed to execute 'getItem' on 'Storage'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
key = webidl.converters.DOMString(key, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
2021-05-19 12:28:50 +00:00
|
|
|
return core.opSync("op_webstorage_get", key, this[_persistent]);
|
2021-05-10 10:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removeItem(key) {
|
|
|
|
webidl.assertBranded(this, Storage);
|
|
|
|
const prefix = "Failed to execute 'removeItem' on 'Storage'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
key = webidl.converters.DOMString(key, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
2021-05-19 12:28:50 +00:00
|
|
|
core.opSync("op_webstorage_remove", key, this[_persistent]);
|
2021-05-10 10:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
webidl.assertBranded(this, Storage);
|
2021-05-19 12:28:50 +00:00
|
|
|
core.opSync("op_webstorage_clear", this[_persistent]);
|
2021-05-10 10:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createStorage(persistent) {
|
|
|
|
if (persistent) window.location;
|
|
|
|
|
|
|
|
const storage = webidl.createBranded(Storage);
|
2021-05-19 12:28:50 +00:00
|
|
|
storage[_persistent] = persistent;
|
2021-05-10 10:02:47 +00:00
|
|
|
|
|
|
|
const proxy = new Proxy(storage, {
|
|
|
|
deleteProperty(target, key) {
|
|
|
|
if (typeof key == "symbol") {
|
|
|
|
delete target[key];
|
|
|
|
} else {
|
|
|
|
target.removeItem(key);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
defineProperty(target, key, descriptor) {
|
|
|
|
if (typeof key == "symbol") {
|
|
|
|
Object.defineProperty(target, key, descriptor);
|
|
|
|
} else {
|
|
|
|
target.setItem(key, descriptor.value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
get(target, key) {
|
|
|
|
if (typeof key == "symbol") return target[key];
|
|
|
|
if (key in target) {
|
|
|
|
return Reflect.get(...arguments);
|
|
|
|
} else {
|
|
|
|
return target.getItem(key) ?? undefined;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set(target, key, value) {
|
|
|
|
if (typeof key == "symbol") {
|
|
|
|
Object.defineProperty(target, key, {
|
|
|
|
value,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
target.setItem(key, value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
has(target, p) {
|
|
|
|
return (typeof target.getItem(p)) === "string";
|
|
|
|
},
|
|
|
|
ownKeys() {
|
2021-05-19 12:28:50 +00:00
|
|
|
return core.opSync("op_webstorage_iterate_keys", persistent);
|
2021-05-10 10:02:47 +00:00
|
|
|
},
|
|
|
|
getOwnPropertyDescriptor(target, key) {
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (key in target) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const value = target.getItem(key);
|
|
|
|
if (value === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
proxy[Symbol.for("Deno.customInspect")] = function (inspect) {
|
|
|
|
return `${this.constructor.name} ${
|
|
|
|
inspect({
|
|
|
|
length: this.length,
|
|
|
|
...Object.fromEntries(Object.entries(proxy)),
|
|
|
|
})
|
|
|
|
}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
let localStorage;
|
|
|
|
let sessionStorage;
|
|
|
|
|
|
|
|
window.__bootstrap.webStorage = {
|
|
|
|
localStorage() {
|
|
|
|
if (!localStorage) {
|
|
|
|
localStorage = createStorage(true);
|
|
|
|
}
|
|
|
|
return localStorage;
|
|
|
|
},
|
|
|
|
sessionStorage() {
|
|
|
|
if (!sessionStorage) {
|
|
|
|
sessionStorage = createStorage(false);
|
|
|
|
}
|
|
|
|
return sessionStorage;
|
|
|
|
},
|
|
|
|
Storage,
|
|
|
|
};
|
|
|
|
})(this);
|