2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-05-19 08:28:50 -04:00
|
|
|
|
2021-07-03 05:49:41 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
op_webstorage_clear,
|
|
|
|
op_webstorage_get,
|
|
|
|
op_webstorage_iterate_keys,
|
|
|
|
op_webstorage_key,
|
|
|
|
op_webstorage_length,
|
|
|
|
op_webstorage_remove,
|
|
|
|
op_webstorage_set,
|
|
|
|
} = core.ensureFastOps();
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
ObjectFromEntries,
|
|
|
|
ObjectEntries,
|
2023-11-14 01:01:15 -05:00
|
|
|
ReflectDefineProperty,
|
|
|
|
ReflectDeleteProperty,
|
2023-02-07 14:22:46 -05:00
|
|
|
ReflectGet,
|
|
|
|
ReflectHas,
|
|
|
|
Proxy,
|
|
|
|
} = primordials;
|
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const _persistent = Symbol("[[persistent]]");
|
|
|
|
|
|
|
|
class Storage {
|
|
|
|
[_persistent];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
2021-05-10 06:02:47 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get length() {
|
|
|
|
webidl.assertBranded(this, StoragePrototype);
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_webstorage_length(this[_persistent]);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
key(index) {
|
|
|
|
webidl.assertBranded(this, StoragePrototype);
|
|
|
|
const prefix = "Failed to execute 'key' on 'Storage'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
index = webidl.converters["unsigned long"](index, prefix, "Argument 1");
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_webstorage_key(index, this[_persistent]);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setItem(key, value) {
|
|
|
|
webidl.assertBranded(this, StoragePrototype);
|
|
|
|
const prefix = "Failed to execute 'setItem' on 'Storage'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 2, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
key = webidl.converters.DOMString(key, prefix, "Argument 1");
|
|
|
|
value = webidl.converters.DOMString(value, prefix, "Argument 2");
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
op_webstorage_set(key, value, this[_persistent]);
|
2021-05-10 06:02:47 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
getItem(key) {
|
|
|
|
webidl.assertBranded(this, StoragePrototype);
|
|
|
|
const prefix = "Failed to execute 'getItem' on 'Storage'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
key = webidl.converters.DOMString(key, prefix, "Argument 1");
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_webstorage_get(key, this[_persistent]);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
removeItem(key) {
|
|
|
|
webidl.assertBranded(this, StoragePrototype);
|
|
|
|
const prefix = "Failed to execute 'removeItem' on 'Storage'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
key = webidl.converters.DOMString(key, prefix, "Argument 1");
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
op_webstorage_remove(key, this[_persistent]);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
webidl.assertBranded(this, StoragePrototype);
|
2024-01-10 17:37:25 -05:00
|
|
|
op_webstorage_clear(this[_persistent]);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const StoragePrototype = Storage.prototype;
|
|
|
|
|
|
|
|
function createStorage(persistent) {
|
|
|
|
const storage = webidl.createBranded(Storage);
|
|
|
|
storage[_persistent] = persistent;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const proxy = new Proxy(storage, {
|
|
|
|
deleteProperty(target, key) {
|
2023-11-14 01:01:15 -05:00
|
|
|
if (typeof key === "symbol") {
|
|
|
|
return ReflectDeleteProperty(target, key);
|
2021-05-10 06:02:47 -04:00
|
|
|
}
|
2023-11-14 01:01:15 -05:00
|
|
|
target.removeItem(key);
|
2023-02-07 14:22:46 -05:00
|
|
|
return true;
|
2021-05-10 06:02:47 -04:00
|
|
|
},
|
2023-11-14 01:01:15 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
defineProperty(target, key, descriptor) {
|
2023-11-14 01:01:15 -05:00
|
|
|
if (typeof key === "symbol") {
|
|
|
|
return ReflectDefineProperty(target, key, descriptor);
|
2021-05-10 06:02:47 -04:00
|
|
|
}
|
2023-11-14 01:01:15 -05:00
|
|
|
target.setItem(key, descriptor.value);
|
2023-02-07 14:22:46 -05:00
|
|
|
return true;
|
2021-05-10 06:02:47 -04:00
|
|
|
},
|
2023-11-14 01:01:15 -05:00
|
|
|
|
|
|
|
get(target, key, receiver) {
|
|
|
|
if (typeof key === "symbol") {
|
|
|
|
return target[key];
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (ReflectHas(target, key)) {
|
2023-11-14 01:01:15 -05:00
|
|
|
return ReflectGet(target, key, receiver);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-11-14 01:01:15 -05:00
|
|
|
return target.getItem(key) ?? undefined;
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
2023-11-14 01:01:15 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
set(target, key, value) {
|
2023-11-14 01:01:15 -05:00
|
|
|
if (typeof key === "symbol") {
|
|
|
|
return ReflectDefineProperty(target, key, {
|
2023-02-07 14:22:46 -05:00
|
|
|
value,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
}
|
2023-11-14 01:01:15 -05:00
|
|
|
target.setItem(key, value);
|
2023-02-07 14:22:46 -05:00
|
|
|
return true;
|
|
|
|
},
|
2023-11-14 01:01:15 -05:00
|
|
|
|
|
|
|
has(target, key) {
|
|
|
|
if (ReflectHas(target, key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return typeof key === "string" && typeof target.getItem(key) === "string";
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
2023-11-14 01:01:15 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ownKeys() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_webstorage_iterate_keys(persistent);
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
2023-11-14 01:01:15 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
getOwnPropertyDescriptor(target, key) {
|
|
|
|
if (ReflectHas(target, key)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const value = target.getItem(key);
|
|
|
|
if (value === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-11-19 03:13:38 -05:00
|
|
|
storage[SymbolFor("Deno.privateCustomInspect")] = function (
|
|
|
|
inspect,
|
|
|
|
inspectOptions,
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
return `${this.constructor.name} ${
|
|
|
|
inspect({
|
|
|
|
...ObjectFromEntries(ObjectEntries(proxy)),
|
2023-11-19 03:13:38 -05:00
|
|
|
length: this.length,
|
|
|
|
}, inspectOptions)
|
2023-02-07 14:22:46 -05:00
|
|
|
}`;
|
2021-05-10 06:02:47 -04:00
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
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 };
|