mirror of
https://github.com/denoland/deno.git
synced 2024-12-18 13:22:55 -05:00
1e51b650be
![image](https://github.com/user-attachments/assets/3f86e2fd-9026-4965-8f3b-512423362f1e) Depends on: - https://github.com/denoland/deno_core/pull/970 - https://github.com/denoland/deno_core/pull/976 - https://github.com/denoland/deno_core/pull/980 - https://github.com/denoland/deno_core/pull/981 --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
126 lines
2.9 KiB
JavaScript
126 lines
2.9 KiB
JavaScript
// 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_iterate_keys, Storage } from "ext:core/ops";
|
|
const {
|
|
SymbolFor,
|
|
ObjectFromEntries,
|
|
ObjectEntries,
|
|
ReflectDefineProperty,
|
|
ReflectDeleteProperty,
|
|
FunctionPrototypeBind,
|
|
ReflectHas,
|
|
Proxy,
|
|
} = primordials;
|
|
|
|
function createStorage(persistent) {
|
|
const storage = new Storage(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) {
|
|
if (typeof key === "symbol") {
|
|
return target[key];
|
|
}
|
|
if (ReflectHas(target, key)) {
|
|
const value = target[key];
|
|
if (typeof value === "function") {
|
|
return FunctionPrototypeBind(value, target);
|
|
}
|
|
return value;
|
|
}
|
|
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(storage);
|
|
},
|
|
|
|
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 `Storage ${
|
|
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 };
|