From 04bb6778e5d0b66053485ded902a3b873ba80410 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 21 Jul 2024 16:49:10 -0400 Subject: [PATCH] src: move `ToNamespacedPath` call of webstorage PR-URL: https://github.com/nodejs/node/pull/53875 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- lib/internal/webstorage.js | 6 ++---- src/node_webstorage.cc | 18 ++++++++++++++---- src/node_webstorage.h | 4 +++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/internal/webstorage.js b/lib/internal/webstorage.js index 1732d880b6bb86..7f58299a1e835f 100644 --- a/lib/internal/webstorage.js +++ b/lib/internal/webstorage.js @@ -6,7 +6,6 @@ const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes; const { getOptionValue } = require('internal/options'); const { emitExperimentalWarning } = require('internal/util'); const { kConstructorKey, Storage } = internalBinding('webstorage'); -const { resolve, toNamespacedPath } = require('path'); const { getValidatedPath } = require('internal/fs/utils'); const kInMemoryPath = ':memory:'; @@ -25,7 +24,7 @@ ObjectDefineProperties(module.exports, { enumerable: true, get() { if (lazyLocalStorage === undefined) { - let location = getOptionValue('--localstorage-file'); + const location = getOptionValue('--localstorage-file'); if (location === '') { throw new ERR_INVALID_ARG_VALUE('--localstorage-file', @@ -33,8 +32,7 @@ ObjectDefineProperties(module.exports, { 'is an invalid localStorage location'); } - location = toNamespacedPath(resolve(getValidatedPath(location))); - lazyLocalStorage = new Storage(kConstructorKey, location); + lazyLocalStorage = new Storage(kConstructorKey, getValidatedPath(location)); } return lazyLocalStorage; diff --git a/src/node_webstorage.cc b/src/node_webstorage.cc index 6c3e19db1dbffc..7c759e94cfb938 100644 --- a/src/node_webstorage.cc +++ b/src/node_webstorage.cc @@ -6,6 +6,7 @@ #include "node.h" #include "node_errors.h" #include "node_mem-inl.h" +#include "path.h" #include "sqlite3.h" #include "util-inl.h" @@ -76,13 +77,14 @@ static void ThrowQuotaExceededException(Local context) { isolate->ThrowException(exception); } -Storage::Storage(Environment* env, Local object, Local location) +Storage::Storage(Environment* env, + Local object, + std::string_view location) : BaseObject(env, object) { MakeWeak(); - Utf8Value utf8_location(env->isolate(), location); symbols_.Reset(env->isolate(), Map::New(env->isolate())); db_ = nullptr; - location_ = utf8_location.ToString(); + location_ = std::string(location); } Storage::~Storage() { @@ -209,7 +211,15 @@ void Storage::New(const FunctionCallbackInfo& args) { CHECK(args.IsConstructCall()); CHECK(args[1]->IsString()); - new Storage(env, args.This(), args[1].As()); + + BufferValue location(env->isolate(), args[1]); + CHECK_NOT_NULL(*location); + // Only call namespaced path if the location is not "in memory". + if (location.ToStringView() != kInMemoryPath) { + ToNamespacedPath(env, &location); + } + + new Storage(env, args.This(), location.ToStringView()); } void Storage::Clear() { diff --git a/src/node_webstorage.h b/src/node_webstorage.h index 2ccc35b10c4d2c..2c0c3ab688cae5 100644 --- a/src/node_webstorage.h +++ b/src/node_webstorage.h @@ -23,11 +23,13 @@ struct stmt_deleter { }; using stmt_unique_ptr = std::unique_ptr; +static constexpr std::string_view kInMemoryPath = ":memory:"; + class Storage : public BaseObject { public: Storage(Environment* env, v8::Local object, - v8::Local location); + std::string_view location); void MemoryInfo(MemoryTracker* tracker) const override; static void New(const v8::FunctionCallbackInfo& args);