diff --git a/Cargo.lock b/Cargo.lock index c3e84f40ba..81c15fbbd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -914,6 +914,13 @@ dependencies = [ "uuid", ] +[[package]] +name = "deno_cache" +version = "0.1.0" +dependencies = [ + "deno_core", +] + [[package]] name = "deno_console" version = "0.62.0" diff --git a/Cargo.toml b/Cargo.toml index bb88cbaf30..633d18501c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "test_ffi", "test_util", "ext/broadcast_channel", + "ext/cache", "ext/console", "ext/crypto", "ext/fetch", diff --git a/ext/cache/01_cache.js b/ext/cache/01_cache.js new file mode 100644 index 0000000000..0ea93c3c0d --- /dev/null +++ b/ext/cache/01_cache.js @@ -0,0 +1,107 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +/// + +((window) => { + const core = window.Deno.core; + const webidl = window.__bootstrap.webidl; + const { + SafeArrayIterator, + Symbol, + SymbolFor, + ObjectDefineProperty, + ObjectFromEntries, + ObjectEntries, + ReflectGet, + ReflectHas, + Proxy, + } = window.__bootstrap.primordials; + + const _persistent = Symbol("[[persistent]]"); + + class CacheStorage { + #storage; + + constructor() { + webidl.illegalConstructor(); + } + + get length() { + webidl.assertBranded(this, StoragePrototype); + return core.opSync("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, + context: "Argument 1", + }); + + return core.opSync("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, + context: "Argument 1", + }); + value = webidl.converters.DOMString(value, { + prefix, + context: "Argument 2", + }); + + core.opSync("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, + context: "Argument 1", + }); + + return core.opSync("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, + context: "Argument 1", + }); + + core.opSync("op_webstorage_remove", key, this[_persistent]); + } + + clear() { + webidl.assertBranded(this, StoragePrototype); + core.opSync("op_webstorage_clear", this[_persistent]); + } + } + + window.__bootstrap.webStorage = { + localStorage() { + if (!localStorage) { + localStorage = createStorage(true); + } + return localStorage; + }, + sessionStorage() { + if (!sessionStorage) { + sessionStorage = createStorage(false); + } + return sessionStorage; + }, + Storage, + }; +})(this); diff --git a/ext/cache/Cargo.toml b/ext/cache/Cargo.toml new file mode 100644 index 0000000000..14e6efaa0a --- /dev/null +++ b/ext/cache/Cargo.toml @@ -0,0 +1,17 @@ +# Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +[package] +name = "deno_cache" +version = "0.1.0" +authors = ["the Deno authors"] +edition = "2021" +license = "MIT" +readme = "README.md" +repository = "https://github.com/denoland/deno" +description = "Implementation of Cache API for Deno" + +[lib] +path = "lib.rs" + +[dependencies] +deno_core = { version = "0.144.0", path = "../../core" } diff --git a/ext/cache/README.md b/ext/cache/README.md new file mode 100644 index 0000000000..efd45cec0d --- /dev/null +++ b/ext/cache/README.md @@ -0,0 +1,5 @@ +# deno_cache + +This crate implements the Cache API. + +Spec: https://console.spec.whatwg.org/ diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs new file mode 100644 index 0000000000..bd1aacffff --- /dev/null +++ b/ext/cache/lib.rs @@ -0,0 +1,18 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +use deno_core::include_js_files; +use deno_core::Extension; +use std::path::PathBuf; + +pub fn init() -> Extension { + Extension::builder() + .js(include_js_files!( + prefix "deno:ext/cache", + "01_cache.js", + )) + .build() +} + +pub fn get_declaration() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_console.d.ts") +}