src: move ToNamespacedPath call of webstorage

PR-URL: https://github.com/nodejs/node/pull/53875
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Yagiz Nizipli 2024-07-21 16:49:10 -04:00 committed by GitHub
parent 52ba14405b
commit 1fb23f1897
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 9 deletions

View File

@ -6,7 +6,6 @@ const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes;
const { getOptionValue } = require('internal/options'); const { getOptionValue } = require('internal/options');
const { emitExperimentalWarning } = require('internal/util'); const { emitExperimentalWarning } = require('internal/util');
const { kConstructorKey, Storage } = internalBinding('webstorage'); const { kConstructorKey, Storage } = internalBinding('webstorage');
const { resolve, toNamespacedPath } = require('path');
const { getValidatedPath } = require('internal/fs/utils'); const { getValidatedPath } = require('internal/fs/utils');
const kInMemoryPath = ':memory:'; const kInMemoryPath = ':memory:';
@ -25,7 +24,7 @@ ObjectDefineProperties(module.exports, {
enumerable: true, enumerable: true,
get() { get() {
if (lazyLocalStorage === undefined) { if (lazyLocalStorage === undefined) {
let location = getOptionValue('--localstorage-file'); const location = getOptionValue('--localstorage-file');
if (location === '') { if (location === '') {
throw new ERR_INVALID_ARG_VALUE('--localstorage-file', throw new ERR_INVALID_ARG_VALUE('--localstorage-file',
@ -33,8 +32,7 @@ ObjectDefineProperties(module.exports, {
'is an invalid localStorage location'); 'is an invalid localStorage location');
} }
location = toNamespacedPath(resolve(getValidatedPath(location))); lazyLocalStorage = new Storage(kConstructorKey, getValidatedPath(location));
lazyLocalStorage = new Storage(kConstructorKey, location);
} }
return lazyLocalStorage; return lazyLocalStorage;

View File

@ -6,6 +6,7 @@
#include "node.h" #include "node.h"
#include "node_errors.h" #include "node_errors.h"
#include "node_mem-inl.h" #include "node_mem-inl.h"
#include "path.h"
#include "sqlite3.h" #include "sqlite3.h"
#include "util-inl.h" #include "util-inl.h"
@ -76,13 +77,14 @@ static void ThrowQuotaExceededException(Local<Context> context) {
isolate->ThrowException(exception); isolate->ThrowException(exception);
} }
Storage::Storage(Environment* env, Local<Object> object, Local<String> location) Storage::Storage(Environment* env,
Local<Object> object,
std::string_view location)
: BaseObject(env, object) { : BaseObject(env, object) {
MakeWeak(); MakeWeak();
Utf8Value utf8_location(env->isolate(), location);
symbols_.Reset(env->isolate(), Map::New(env->isolate())); symbols_.Reset(env->isolate(), Map::New(env->isolate()));
db_ = nullptr; db_ = nullptr;
location_ = utf8_location.ToString(); location_ = std::string(location);
} }
Storage::~Storage() { Storage::~Storage() {
@ -209,7 +211,15 @@ void Storage::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args.IsConstructCall()); CHECK(args.IsConstructCall());
CHECK(args[1]->IsString()); CHECK(args[1]->IsString());
new Storage(env, args.This(), args[1].As<String>());
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() { void Storage::Clear() {

View File

@ -23,11 +23,13 @@ struct stmt_deleter {
}; };
using stmt_unique_ptr = std::unique_ptr<sqlite3_stmt, stmt_deleter>; using stmt_unique_ptr = std::unique_ptr<sqlite3_stmt, stmt_deleter>;
static constexpr std::string_view kInMemoryPath = ":memory:";
class Storage : public BaseObject { class Storage : public BaseObject {
public: public:
Storage(Environment* env, Storage(Environment* env,
v8::Local<v8::Object> object, v8::Local<v8::Object> object,
v8::Local<v8::String> location); std::string_view location);
void MemoryInfo(MemoryTracker* tracker) const override; void MemoryInfo(MemoryTracker* tracker) const override;
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); static void New(const v8::FunctionCallbackInfo<v8::Value>& args);