Skip to content

feat(runtime): ns:url builtin module mirroring node:url, with URL types - #497

Merged
edusperoni merged 2 commits into
feat/ns-worker-modulefrom
feat/ns-url-module
Oct 9, 2026
Merged

edusperoni merged 2 commits into
feat/ns-worker-modulefrom
feat/ns-url-module

Conversation

@edusperoni

@edusperoni edusperoni commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

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:url builtin whose surface is exactly node:url's: the runtime's own URL and URLSearchParams constructors plus the fileURLToPath/pathToFileURL converters. The standard module now owns the converters (moved verbatim from node-url.js, same Node error messages), and node:url becomes a thin re-export shim over it on a distinct, separately frozen module object, the way node:util sits over ns:util.

import { URL, fileURLToPath, pathToFileURL } from "ns:url";

pathToFileURL("/app/my file.js").href; // "file:///app/my%20file.js"
fileURLToPath(new URL("file:///app/a.js?v=2")); // "/app/a.js"

Why a module

URL and URLSearchParams are 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 templates URLImpl::Init / URLSearchParamsImpl::Init installed, held per isolate in a Caches::StateFor slot exactly like Worker::Constructor in #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 reassigns globalThis.URL.

Scope decision

The export set is URL, URLSearchParams, fileURLToPath, pathToFileURL, identical to node:url's, so the shim re-exports all four unchanged (nodeUrl.fileURLToPath === nsUrl.fileURLToPath). As a result require("node:url") gains URL and URLSearchParams, matching Node's node:url; the converters behave exactly as before. URLPattern stays a global only: Node's node:url has no counterpart, and nothing in the module needs it. It can join ns:url later the same way ns:util grew TextEncoder.

Types

types/ns-url.d.ts declares the module with standalone URL and URLSearchParams interfaces limited to the members this runtime implements (no toJSON, no static URL.parse; createObjectURL/revokeObjectURL and searchParams are included because blob-url.js installs them), plus the two converters. At script level it does what @types/node does for its own globals:

  • interface URL extends NsURL {} / interface URLSearchParams extends NsURLSearchParams {} merges, written inside the module's global {} block with a self-import, since an interface cannot extend an import() type;
  • declare var URL / declare var URLSearchParams through the typeof 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 --strict against a DOM program and a non-DOM program, each constructing both classes through the module and through the globals, with @ts-expect-error checks on wrong argument types: both are clean. The non-DOM program no longer reports the two pre-existing Cannot find name 'URL' errors in ns-module.d.ts, because the global URL interface now exists there.

The DOM program also assigns pathToFileURL()'s result to the global URL type, which holds because of the toJSON the second commit adds.

Runtime gaps closed (second commit)

  • URL gains toJSON, registered next to toString with the same callback, so JSON.stringify(url) gives the href string as WHATWG requires, and a module URL is assignable to lib.dom's URL.
  • The searchParams sync wrapper in blob-url.js 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 one was passed (by arity, so an explicit undefined still reaches the native coercion).

Both have specs, in URL.js and UrlSearchParamsTests.js.

Docs and tests

docs/ns-builtin-modules.md gains the ns:url reference 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 the node:url shims row says it re-exports all four members from ns:url, still without legacy url.parse/format/resolve or URLPattern.

NsUrlTests.js pins the frozen export set, identity with the globals, URLPattern staying out, the per-realm singleton, the converters returning the module's own URL, the shim being a distinct frozen object whose every member is the ns:url one, and, through a worker fixture that reassigns globalThis.URL before its first require("ns:url"), that the module still exports the original constructor. The existing node:url export-set assertion grows the two names.

Full suite on a dedicated iOS 18.5 simulator, with both commits: 1764 test cases, 0 failures.

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 51caa810-4e29-4a80-ad9a-3f8ffb686ac9
📥 Commits

Reviewing files that changed from the base of the PR and between a38ff54 and aae08b4.

📒 Files selected for processing (18)
  • NativeScript/runtime/NsBuiltinModules.cpp
  • NativeScript/runtime/URLImpl.cpp
  • NativeScript/runtime/URLImpl.h
  • NativeScript/runtime/URLSearchParamsImpl.cpp
  • NativeScript/runtime/URLSearchParamsImpl.h
  • NativeScript/runtime/js/blob-url.js
  • NativeScript/runtime/js/node-url.js
  • NativeScript/runtime/js/ns-url.js
  • TestRunner/app/tests/NodeBuiltinsAndOptionalModulesTests.mjs
  • TestRunner/app/tests/NsUrlTests.js
  • TestRunner/app/tests/URL.js
  • TestRunner/app/tests/UrlSearchParamsTests.js
  • TestRunner/app/tests/index.js
  • TestRunner/app/tests/nsUrlIdentityWorker.js
  • docs/ns-builtin-modules.md
  • tools/js2c-inputs.xcfilelist
  • types/index.d.ts
  • types/ns-url.d.ts
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@edusperoni
edusperoni added this pull request to stack #495 October 8, 2026 19:29
@edusperoni
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
edusperoni merged commit 80475b4 into main Oct 9, 2026
10 checks passed
@edusperoni
edusperoni deleted the feat/ns-url-module branch October 9, 2026 19:35
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 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant