Skip to content

Commit

Permalink
deps,lib,src: add experimental web storage
Browse files Browse the repository at this point in the history
This commit introduces an experimental implementation of the Web
Storage API using SQLite as the backing data store.
  • Loading branch information
cjihrig committed Oct 12, 2023
1 parent f0e720a commit 4fe81a9
Show file tree
Hide file tree
Showing 99 changed files with 268,189 additions and 0 deletions.
250,808 changes: 250,808 additions & 0 deletions deps/sqlite3/sqlite3.c

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions deps/sqlite3/sqlite3.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
'variables': {
'sqlite3_sources': [
'sqlite3.c',
],
},
'targets': [
{
'target_name': 'sqlite3',
'type': 'static_library',
'include_dirs': ['.'],
'sources': [
'<@(sqlite3_sources)',
],
'direct_dependent_settings': {
'include_dirs': ['.'],
},
},
],
}
13,120 changes: 13,120 additions & 0 deletions deps/sqlite3/sqlite3.h

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,14 @@ added: REPLACEME

Enable experimental [`WebSocket`][] support.

### `--experimental-webstorage`

<!-- YAML
added: REPLACEME
-->

Enable experimental [`Web Storage`][] support.

### `--force-context-aware`

<!-- YAML
Expand Down Expand Up @@ -2768,6 +2776,7 @@ done
[`NODE_OPTIONS`]: #node_optionsoptions
[`NO_COLOR`]: https://no-color.org
[`SlowBuffer`]: buffer.md#class-slowbuffer
[`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
[`WebSocket`]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
[`YoungGenerationSizeFromSemiSpaceSize`]: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.3.129/src/heap/heap.cc#328
[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
Expand Down
45 changes: 45 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,23 @@ changes:
A browser-compatible implementation of {Headers}.

## `localStorage`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental.
A browser-compatible implementation of [`localStorage`][]. Data is stored
unencrypted in the current working directory, with a storage quota of 10 MB.
The filename of the persisted storage is the basename of the main entrypoint,
`process.argv[1]`, followed by a dot (`.`), a hash of the entrypoint's absolute
path and a `.localstorage` extension. If Node.js is run without a file as the
entrypoint, such as when running the REPL, then the executable's basename is
used. Any modification of this data outside of the Web Storage API is not
supported. Enable this API with the [`--experimental-webstorage`][] CLI flag.

## `MessageChannel`

<!-- YAML
Expand Down Expand Up @@ -855,6 +872,19 @@ changes:
A browser-compatible implementation of {Request}.

## `sessionStorage`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental.
A browser-compatible implementation of [`sessionStorage`][]. Data is stored in
memory, with a storage quota of 10 MB. Any modification of this data outside of
the Web Storage API is not supported. Enable this API with the
[`--experimental-webstorage`][] CLI flag.

## `setImmediate(callback[, ...args])`

<!-- YAML
Expand Down Expand Up @@ -885,6 +915,17 @@ added: v0.0.1

[`setTimeout`][] is described in the [timers][] section.

## Class: `Storage`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental.
A browser-compatible implementation of [`Storage`][]. Enable this API with the
[`--experimental-webstorage`][] CLI flag.

## `structuredClone(value[, options])`

<!-- YAML
Expand Down Expand Up @@ -1064,6 +1105,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[Navigator API]: https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
[Web Crypto API]: webcrypto.md
[`--experimental-websocket`]: cli.md#--experimental-websocket
[`--experimental-webstorage`]: cli.md#--experimental-webstorage
[`--no-experimental-global-customevent`]: cli.md#--no-experimental-global-customevent
[`--no-experimental-global-webcrypto`]: cli.md#--no-experimental-global-webcrypto
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
Expand All @@ -1089,6 +1131,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[`ReadableStreamDefaultController`]: webstreams.md#class-readablestreamdefaultcontroller
[`ReadableStreamDefaultReader`]: webstreams.md#class-readablestreamdefaultreader
[`ReadableStream`]: webstreams.md#class-readablestream
[`Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Storage
[`TextDecoderStream`]: webstreams.md#class-textdecoderstream
[`TextDecoder`]: util.md#class-utiltextdecoder
[`TextEncoderStream`]: webstreams.md#class-textencoderstream
Expand All @@ -1113,11 +1156,13 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[`exports`]: modules.md#exports
[`fetch()`]: https://developer.mozilla.org/en-US/docs/Web/API/fetch
[`globalThis`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
[`localStorage`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
[`module`]: modules.md#module
[`perf_hooks.performance`]: perf_hooks.md#perf_hooksperformance
[`process.nextTick()`]: process.md#processnexttickcallback-args
[`process` object]: process.md#process
[`require()`]: modules.md#requireid
[`sessionStorage`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
[`setImmediate`]: timers.md#setimmediatecallback-args
[`setInterval`]: timers.md#setintervalcallback-delay-args
[`setTimeout`]: timers.md#settimeoutcallback-delay-args
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ Enable code coverage in the test runner.
.It Fl -experimental-websocket
Enable experimental support for the WebSocket API.
.
.It Fl -experimental-webstorage
Enable experimental support for the Web Storage API.
.
.It Fl -no-experimental-fetch
Disable experimental support for the Fetch API.
.
Expand Down
15 changes: 15 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function prepareExecution(options) {
setupWarningHandler();
setupUndici();
setupWebCrypto();
setupWebStorage();
setupCustomEvent();
setupCodeCoverage();
setupDebugEnv();
Expand Down Expand Up @@ -371,6 +372,20 @@ function setupWebCrypto() {
}
}

// TODO(cjihrig): Move this to internal/bootstrap/web/* when this is unflagged.
function setupWebStorage() {
if (getEmbedderOptions().noBrowserGlobals ||
!getOptionValue('--experimental-webstorage')) {
return;
}

// https://html.spec.whatwg.org/multipage/webstorage.html#webstorage
exposeLazyInterfaces(globalThis, 'internal/webstorage', ['Storage']);
defineReplaceableLazyAttribute(globalThis, 'internal/webstorage', [
'localStorage', 'sessionStorage',
]);
}

function setupCodeCoverage() {
// Resolve the coverage directory to an absolute path, and
// overwrite process.env so that the original path gets passed
Expand Down
57 changes: 57 additions & 0 deletions lib/internal/webstorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';
const {
MathAbs,
ObjectDefineProperty,
StringPrototypeCharCodeAt,
} = primordials;
const { emitExperimentalWarning } = require('internal/util');
const { kConstructorKey, Storage } = internalBinding('webstorage');
const { basename, join } = require('path');

emitExperimentalWarning('Web storage');

module.exports = { Storage };

let lazyLocalStorage;
let lazySessionStorage;

ObjectDefineProperty(module.exports, 'localStorage', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (lazyLocalStorage === undefined) {
const entry = process.argv[1] ?? process.argv[0];
const base = basename(entry);
const hash = hashCode(entry);
const location = join(process.cwd(), `${base}.${hash}.localstorage`);

lazyLocalStorage = new Storage(kConstructorKey, location);
}

return lazyLocalStorage;
},
});

ObjectDefineProperty(module.exports, 'sessionStorage', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (lazySessionStorage === undefined) {
lazySessionStorage = new Storage(kConstructorKey, ':memory:');
}

return lazySessionStorage;
},
});

function hashCode(s) {
let h = 0;

for (let i = 0; i < s.length; ++i) {
h = (h << 5) - h + StringPrototypeCharCodeAt(s, i) | 0;
}

return MathAbs(h);
}
12 changes: 12 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
'src/node_wasi.cc',
'src/node_wasm_web_api.cc',
'src/node_watchdog.cc',
'src/node_webstorage.cc',
'src/node_worker.cc',
'src/node_zlib.cc',
'src/permission/child_process_permission.cc',
Expand Down Expand Up @@ -261,6 +262,7 @@
'src/node_v8_platform-inl.h',
'src/node_wasi.h',
'src/node_watchdog.h',
'src/node_webstorage.h',
'src/node_worker.h',
'src/permission/child_process_permission.h',
'src/permission/fs_permission.h',
Expand Down Expand Up @@ -520,6 +522,7 @@
'dependencies': [
'deps/histogram/histogram.gyp:histogram',
'deps/uvwasi/uvwasi.gyp:uvwasi',
'deps/sqlite3/sqlite3.gyp:sqlite3',
],

'msvs_settings': {
Expand Down Expand Up @@ -816,6 +819,7 @@
'deps/googletest/googletest.gyp:gtest_prod',
'deps/histogram/histogram.gyp:histogram',
'deps/uvwasi/uvwasi.gyp:uvwasi',
'deps/sqlite3/sqlite3.gyp:sqlite3',
'deps/simdutf/simdutf.gyp:simdutf',
'deps/ada/ada.gyp:ada',
'node_js2c#host',
Expand Down Expand Up @@ -1000,6 +1004,7 @@
'<(node_lib_target_name)',
'deps/histogram/histogram.gyp:histogram',
'deps/uvwasi/uvwasi.gyp:uvwasi',
'deps/sqlite3/sqlite3.gyp:sqlite3',
],
'includes': [
'node.gypi'
Expand All @@ -1011,6 +1016,7 @@
'deps/cares/include',
'deps/uv/include',
'deps/uvwasi/include',
'deps/sqlite3',
'test/cctest',
],
'defines': [
Expand Down Expand Up @@ -1047,6 +1053,7 @@
'deps/googletest/googletest.gyp:gtest_main',
'deps/histogram/histogram.gyp:histogram',
'deps/uvwasi/uvwasi.gyp:uvwasi',
'deps/sqlite3/sqlite3.gyp:sqlite3',
'deps/simdutf/simdutf.gyp:simdutf',
'deps/ada/ada.gyp:ada',
],
Expand All @@ -1062,6 +1069,7 @@
'deps/cares/include',
'deps/uv/include',
'deps/uvwasi/include',
'deps/sqlite3',
'test/cctest',
],

Expand Down Expand Up @@ -1124,6 +1132,7 @@
'<(node_lib_target_name)',
'deps/histogram/histogram.gyp:histogram',
'deps/uvwasi/uvwasi.gyp:uvwasi',
'deps/sqlite3/sqlite3.gyp:sqlite3',
'deps/ada/ada.gyp:ada',
],

Expand All @@ -1138,6 +1147,7 @@
'deps/cares/include',
'deps/uv/include',
'deps/uvwasi/include',
'deps/sqlite3',
'test/embedding',
],

Expand Down Expand Up @@ -1235,6 +1245,7 @@
'<(node_lib_target_name)',
'deps/histogram/histogram.gyp:histogram',
'deps/uvwasi/uvwasi.gyp:uvwasi',
'deps/sqlite3/sqlite3.gyp:sqlite3',
'deps/ada/ada.gyp:ada',
],

Expand All @@ -1249,6 +1260,7 @@
'deps/cares/include',
'deps/uv/include',
'deps/uvwasi/include',
'deps/sqlite3',
],

'defines': [ 'NODE_WANT_INTERNALS=1' ],
Expand Down
1 change: 1 addition & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
V(default_host_defined_options, "default_host_defined_options") \
V(fs_use_promises_symbol, "fs_use_promises_symbol") \
V(async_id_symbol, "async_id_symbol") \
V(constructor_key_symbol, "constructor_key_symbol") \
V(handle_onclose_symbol, "handle_onclose") \
V(no_message_symbol, "no_message_symbol") \
V(messaging_deserialize_symbol, "messaging_deserialize_symbol") \
Expand Down
1 change: 1 addition & 0 deletions src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
V(wasi) \
V(wasm_web_api) \
V(watchdog) \
V(webstorage) \
V(worker) \
V(zlib)

Expand Down
5 changes: 5 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::experimental_websocket,
kAllowedInEnvvar,
true);
AddOption("--experimental-webstorage",
"experimental Web Storage API",
&EnvironmentOptions::experimental_webstorage,
kAllowedInEnvvar,
true);
AddOption("--experimental-global-customevent",
"expose experimental CustomEvent on the global scope",
&EnvironmentOptions::experimental_global_customevent,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class EnvironmentOptions : public Options {
bool enable_source_maps = false;
bool experimental_fetch = true;
bool experimental_websocket = false;
bool experimental_webstorage = false;
bool experimental_global_customevent = true;
bool experimental_global_web_crypto = true;
bool experimental_https_modules = false;
Expand Down
Loading

0 comments on commit 4fe81a9

Please sign in to comment.