url: add ToObject method to native URL class#12056
url: add ToObject method to native URL class#12056jasnell wants to merge 2 commits intonodejs:masterfrom
Conversation
Provides a factory method to convert a native URL class
into a JS URL object.
```c++
Environment* env = ...
URL url("http://example.org/a/b/c?query#fragment");
MaybeLocal<Value> val = url.ToObject(env);
```
lib/internal/url.js
Outdated
| const url = new NativeURL(ctx); | ||
| if (!url[searchParams]) { // invoked from URL constructor | ||
| url[searchParams] = new URLSearchParams(); | ||
| url[searchParams][context] = this; |
There was a problem hiding this comment.
Additionally, since this function is never "invoked from URL constructor" but !url[searchParams] is always true, you might want to remove the conditional part.
There was a problem hiding this comment.
heh... yeah, just missed changing this one ;-)
|
|
||
| _process.setupRawDebug(); | ||
|
|
||
| NativeModule.require('internal/url'); |
There was a problem hiding this comment.
You could add a comment that mentions the side effect you’re using this for, e.g.
// Make sure setURLConstructor() is called before the native
// URL::ToObject() method is used.
NativeModule.require('internal/url');
lib/internal/url.js
Outdated
| function NativeURL(ctx) { | ||
| this[context] = ctx; | ||
| } | ||
| util.inherits(NativeURL, URL); |
There was a problem hiding this comment.
I think this would lead to observable differences between URLs constructed with URL vs NativeURL. How about a plain NativeURL.prototype = URL.prototype?
src/node_url.cc
Outdated
| FatalException(isolate, try_catch); | ||
| } | ||
|
|
||
| return ret; |
There was a problem hiding this comment.
If you’re crashing for empty MaybeLocals anyway, you might as well make this method return Local<Value> and pass ret.ToLocalChecked()?
There was a problem hiding this comment.
I wasn't entirely sure what to do here, to be honest. I wanted to get it out so that @bmeck could play with it then tweak it from there. Your suggestion works for me tho :-)
src/node_url.h
Outdated
| return ret; | ||
| } | ||
|
|
||
| MaybeLocal<Value> ToObject(Environment* env); |
src/node_url.cc
Outdated
| if (context_.port > -1) | ||
| argv[ARG_PORT] = Integer::New(isolate, context_.port); | ||
| if (context_.flags & URL_FLAGS_HAS_PATH) | ||
| argv[ARG_PATH] = Copy(env, context_.path); |
There was a problem hiding this comment.
Can this series of C++-to-JS conversion be somehow consolidated with the code in Parse?
There was a problem hiding this comment.
A common inline utility method could likely be used for both. I was already considering it :-)
src/node_url.cc
Outdated
|
|
||
| const Local<Value> undef = Undefined(isolate); | ||
| if (context_.flags & URL_FLAGS_FAILED) | ||
| return undef; |
There was a problem hiding this comment.
Either return a Local<Value> as @addaleax suggested, or make this return MaybeLocal<Value>().
|
|
||
| _process.setupRawDebug(); | ||
|
|
||
| NativeModule.require('internal/url'); |
There was a problem hiding this comment.
I'm a bit afraid of the increase in memory usage. Is there a need to make some requires in internal/url lazy?
There was a problem hiding this comment.
There shouldn't be. The requires that are there are minimal (util will have already been loaded, os is small, etc). I'm not convinced this should be a concern.
| using v8::Value; | ||
|
|
||
|
|
||
| #define BIT_AT(a, i) \ |
There was a problem hiding this comment.
Aside from this PR, maybe we would want to move these private macros and inlined functions into the .cc file itself?
There was a problem hiding this comment.
Likely. It's been on my todo list but just haven't gotten to it yet.
lib/internal/url.js
Outdated
| const url = new NativeURL(ctx); | ||
| if (!url[searchParams]) { // invoked from URL constructor | ||
| url[searchParams] = new URLSearchParams(); | ||
| url[searchParams][context] = this; |
There was a problem hiding this comment.
Additionally, since this function is never "invoked from URL constructor" but !url[searchParams] is always true, you might want to remove the conditional part.
|
Updated! PTAL! I do not consider this to be ready to land yet, btw. I'd like feedback from @bmeck on whether this will work for what he needs before landing. Marking as in progress so it's not landed prematurely. |
addaleax
left a comment
There was a problem hiding this comment.
This PR LGTM in its current state
| } | ||
|
|
||
| MaybeLocal<Value> ToObject(Environment* env); | ||
| const Local<Value> ToObject(Environment* env) const; |
There was a problem hiding this comment.
Specifying const on a return type dosn’t really do anything, btw ;)
There was a problem hiding this comment.
Bad habits die hard. I appreciate the reminder. Eventually I'll remember ;)
Provides a factory method to convert a native URL class
into a JS URL object.
```c++
Environment* env = ...
URL url("http://example.org/a/b/c?query#fragment");
MaybeLocal<Value> val = url.ToObject(env);
```
PR-URL: #12056
Reviewed-By: Anna Henningsen <anna@addaleax.net>
|
Landed in 7139b93 |
|
cc @jasnell |
|
Removing semver-minor since this API is not exposed to users. |
Provides a factory method to convert a native URL class into a JS URL object.
(Including a test for this is a bit difficult at the current time.)
/cc @bmeck
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
url