Repository navigation
feat(runtime): ns:url builtin module mirroring node:url, with URL types - #497
Merged
Merged
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 31 minutes. View limit detailsLimit details: You’ve used all 2 included reviews currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (18)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
edusperoni
marked this pull request as ready for review
October 8, 2026 19:33
Adds `ns:url`, exporting the runtime's own URL and URLSearchParams plus the
`fileURLToPath`/`pathToFileURL` converters, so typed code can name the URL
classes by specifier instead of through whatever `globalThis.URL` resolves to
in the program's type environment. Its surface is exactly `node:url`'s: the
standard module now owns the converters, and `node:url` becomes a re-export
shim over it on a distinct, separately frozen module object, the way
`node:util` sits over `ns:util`. `require("node:url")` behaves as before and
additionally exports URL and URLSearchParams, as Node's does. URLPattern stays
a global only; Node's `node:url` has no counterpart and nothing here needs it.
The binding takes both constructors from the templates URLImpl::Init and
URLSearchParamsImpl::Init installed, held per isolate the way Worker's is.
V8 instantiates a template once per context, so the module hands out the very
functions the globals were created with even after an app reassigns them.
types/ns-url.d.ts declares the module and, like @types/node does for its own
globals, merges the module's interfaces into the global URL and
URLSearchParams and declares the global variables through the `onmessage`
probe, so a program with a DOM lib keeps the lib's declarations and one
without gets the module's constructors. Verified with tsc --strict against a
DOM and a non-DOM program: both are clean, and the non-DOM one no longer
reports the two missing-`URL` errors ns-module.d.ts used to produce there.
Experimental, iOS-only until the Android runtime ships the module.
WHATWG URL defines toJSON as the href serialization, so JSON.stringify(url) yields the href string. The runtime's URL lacked it, which also kept a module URL from being assignable to lib.dom's URL in typed code. It is registered next to toString and shares its callback, so both return the same serialization. types/ns-url.d.ts declares it; tsc against the DOM and the non-DOM program stays clean, and the DOM program now assigns pathToFileURL()'s result to the global URL type. The searchParams wrapper that keeps a URL's query in sync forwarded only the name to the native delete, so url.searchParams.delete(name, value) removed every pair with that name. It now forwards the value whenever the caller passed one, by arity, so an explicit undefined still reaches the native coercion.
edusperoni
force-pushed
the
feat/ns-url-module
branch
from
October 8, 2026 19:38
b4cda0c to
aae08b4
Compare
edusperoni
added a commit
to NativeScript/android
that referenced
this pull request
Dec 2, 2026
### Description Two new public builtin modules, ported from the iOS runtime. As there, each one is a commit: `ns:worker_threads` first, then `ns:url`. - `ns:worker_threads` mirrors NativeScript/ios#494. - `ns:url` mirrors NativeScript/ios#497. The `ns:` module owns the implementation. The matching `node:` shim now reads from it instead of from the globals, the way `node:util` sits over `ns:util`. ```js import { Worker } from "ns:worker_threads"; import { URL, fileURLToPath, pathToFileURL } from "ns:url"; const worker = new Worker("./heavy.js", { android: { priority: "lessFavorable" }, ios: { priority: "utility" }, // ignored here; read by the iOS runtime resourceLimits: { maxOldGenerationSizeMb: 64 }, }); pathToFileURL("/app/my file.js").href; // "file:///app/my%20file.js" ``` #### `ns:worker_threads` - Exports `Worker` only, frozen. - The binding builds `Worker` from the template that `CallbackHandlers::InitWorkerConstructor` puts on the global template and keeps in `RuntimeState`. V8 instantiates a template once per context, so the module returns the same function the global was created with, even if the app has reassigned `globalThis.Worker`. - `node:worker_threads` now gets its constructor from `require("ns:worker_threads")` instead of `globalThis.Worker`. #### `ns:url` - Exports `URL`, `URLSearchParams`, `fileURLToPath` and `pathToFileURL`. That is exactly `node:url`'s export set. - The converters moved verbatim from `node-url.js`. `node:url` is now a re-export shim on its own frozen module object, so `require("node:url")` also gains `URL` and `URLSearchParams`, like Node's `node:url`. `URLPattern` stays a global only. - `URLImpl::Init` and `URLSearchParamsImpl::Init` now install the globals and keep their templates, the same way as `Worker`. - It also ports the two runtime fixes from the iOS PR: - `URL` gets `toJSON` (returns `href`). - The `searchParams` wrapper in `blob-url.js` passes `delete`'s value argument through, so `url.searchParams.delete(name, value)` no longer removes every pair with that name. #### Types: Android extends the iOS declarations The package now ships `types/`, with `"types"` in `package.json` and a `copyTypes` step in the npm packaging. The iOS package's `types/` stays the source of truth for the shared `ns:` surface. If two `declare module` blocks both declare `Worker`, TypeScript reports a conflict, so the Android files declare only new names and merge into the iOS ones: - `types/ns-worker-threads-android.d.ts` adds `WorkerAndroidPriority`, `WorkerAndroidOptions`, and the `android` and deprecated `androidPriority` options. They go on both the module's `WorkerOptions` and the global `WorkerOptions`, so `new Worker(path, { android })` type-checks whether you use the module's constructor or the global one. - The file is a script with no top-level import or export. That makes its `declare module` block merge with the iOS one instead of becoming a module augmentation, which would fail to compile without the iOS types. - The rest of iOS's `ns-worker-threads.d.ts` also describes Android as-is: the `resourceLimits` keys (including `jsDispatchTableSizeMb`), and the error event's `error: null` with `stackTrace`, `filename` and `lineno`. - `ns:url` needs no Android declaration: Android's `URL`/`URLSearchParams` match iOS's `types/ns-url.d.ts` member for member, including value-aware `has`/`delete`, `size`, `canParse` and the blob-URL statics. Checked with `tsc --strict` (TypeScript 5.9) against the iOS `origin/main` types. All of these compile clean, and every `@ts-expect-error` (bad priority name, non-object `android`, iOS priority name under `android`, bad name on the global constructor) fires: | program | iOS then Android | Android then iOS | Android only | |---|---|---|---| | no DOM lib (`es2022`) | ✅ | ✅ | ✅ | | DOM lib (`es2022,dom`) | ✅ | ✅ | ✅ | **Known gap:** a program that includes only the Android types sees the Android additions but not `ns:worker_threads`'s `Worker` or any of `ns:url`. It needs `@nativescript/ios`'s types too. Removing that requirement means moving the shared declarations into a package both runtimes depend on. That is a separate change. #### Docs - `docs/ns-builtin-modules.md`: - new `ns:url` and `ns:worker_threads` sections; the converter semantics and the error-message table moved from the `node:url` row into `ns:url`; - updated `node:url` and `node:worker_threads` rows; - the public-row count is now eleven; - the "Android ships no `.d.ts`" note is replaced. - `docs/worker-threads.md`: - new "Worker options" section covering `android.priority` and `resourceLimits`, which were never documented; - the `resourceLimits` export row no longer says the runtime imposes no per-worker limits. ### Related Pull Requests - NativeScript/ios#494 (`ns:worker_threads`) - NativeScript/ios#497 (`ns:url`) ### Does your pull request have unit tests? Yes: - `testNsWorkerThreads.js` and `testNsUrl.js` check: - the frozen export set; - identity with the globals; - the per-realm singleton, through both `require` and `import()`; - that each module is a separate object from its `node:` shim; - through a worker fixture that reassigns the global before its first `require`, that the module still exports the original constructor. - `testNsWorkerThreads.js` also constructs a worker with both `android` and `ios` options plus `resourceLimits`, and checks the resulting thread priority. - `testNsUrl.js` also checks that `node:url` re-exports every member unchanged. - `testNodeUrlModule.js` now expects the four-name export set. - `testURLImpl.js` covers `toJSON`. `testURLSearchParamsImpl.js` covers value-aware `searchParams.delete`. Full suite (`./gradlew runtestsAndVerifyResults`) on an API 36 arm64 emulator: 1975 specs, 0 failures. `npm run lint` is clean. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Added `ns:url` support for URL constructors and file URL/path conversion utilities. * Added `ns:worker_threads` support, including Android worker-priority options and resource limits. * Included TypeScript declarations with the package. * **Bug Fixes** * Improved URL JSON serialization and support for deleting URL search parameters by name and value. * **Documentation** * Added guidance for the URL and worker-thread modules and their options. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #494 (base branch
feat/ns-worker-module), which is stacked on #493; only the last two commits are this PR.What
A new
ns:urlbuiltin whose surface is exactlynode:url's: the runtime's ownURLandURLSearchParamsconstructors plus thefileURLToPath/pathToFileURLconverters. The standard module now owns the converters (moved verbatim fromnode-url.js, same Node error messages), andnode:urlbecomes a thin re-export shim over it on a distinct, separately frozen module object, the waynode:utilsits overns:util.Why a module
URLandURLSearchParamsare globals whose types come from whichever lib a program includes. A specifier gives typed code one fixed place to name them. The binding takes both constructors from the templatesURLImpl::Init/URLSearchParamsImpl::Initinstalled, held per isolate in aCaches::StateForslot exactly likeWorker::Constructorin #494. V8 instantiates a template once per context, so the module hands out the very functions the globals were created with, even after an app reassignsglobalThis.URL.Scope decision
The export set is
URL,URLSearchParams,fileURLToPath,pathToFileURL, identical tonode:url's, so the shim re-exports all four unchanged (nodeUrl.fileURLToPath === nsUrl.fileURLToPath). As a resultrequire("node:url")gainsURLandURLSearchParams, matching Node'snode:url; the converters behave exactly as before.URLPatternstays a global only: Node'snode:urlhas no counterpart, and nothing in the module needs it. It can joinns:urllater the same wayns:utilgrewTextEncoder.Types
types/ns-url.d.tsdeclares the module with standaloneURLandURLSearchParamsinterfaces limited to the members this runtime implements (notoJSON, no staticURL.parse;createObjectURL/revokeObjectURLandsearchParamsare included becauseblob-url.jsinstalls them), plus the two converters. At script level it does what@types/nodedoes for its own globals:interface URL extends NsURL {}/interface URLSearchParams extends NsURLSearchParams {}merges, written inside the module'sglobal {}block with a self-import, since an interface cannot extend animport()type;declare var URL/declare var URLSearchParamsthrough thetypeof globalThis extends { onmessage: any; URL: infer T }probe, so a program with a DOM lib keeps the lib's declarations and one without gets the module's constructors.Verified with
tsc --strictagainst a DOM program and a non-DOM program, each constructing both classes through the module and through the globals, with@ts-expect-errorchecks on wrong argument types: both are clean. The non-DOM program no longer reports the two pre-existingCannot find name 'URL'errors inns-module.d.ts, because the globalURLinterface now exists there.The DOM program also assigns
pathToFileURL()'s result to the globalURLtype, which holds because of thetoJSONthe second commit adds.Runtime gaps closed (second commit)
URLgainstoJSON, registered next totoStringwith the same callback, soJSON.stringify(url)gives the href string as WHATWG requires, and a moduleURLis assignable to lib.dom'sURL.searchParamssync wrapper inblob-url.jsforwarded only the name to the nativedelete, sourl.searchParams.delete(name, value)removed every pair with that name. It now forwards the value whenever one was passed (by arity, so an explicitundefinedstill reaches the native coercion).Both have specs, in
URL.jsandUrlSearchParamsTests.js.Docs and tests
docs/ns-builtin-modules.mdgains thens:urlreference section (marked experimental, iOS-only until Android ships it; it now carries the converter semantics and error-message table), the public-row count goes to nine, and thenode:urlshims row says it re-exports all four members fromns:url, still without legacyurl.parse/format/resolveorURLPattern.NsUrlTests.jspins the frozen export set, identity with the globals,URLPatternstaying out, the per-realm singleton, the converters returning the module's ownURL, the shim being a distinct frozen object whose every member is thens:urlone, and, through a worker fixture that reassignsglobalThis.URLbefore its firstrequire("ns:url"), that the module still exports the original constructor. The existingnode:urlexport-set assertion grows the two names.Full suite on a dedicated iOS 18.5 simulator, with both commits: 1764 test cases, 0 failures.