Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
354 changes: 279 additions & 75 deletions NativeScript/runtime/ModuleInternalCallbacks.mm

Large diffs are not rendered by default.

588 changes: 588 additions & 0 deletions TestRunner/app/tests/HttpEsmLoaderTests.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions TestRunner/app/tests/esm/http-importer/bare-dynamic.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_bareDynamic = true;
export const name = "bare-dynamic";
2 changes: 2 additions & 0 deletions TestRunner/app/tests/esm/http-importer/bare-static.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_bareStatic = true;
export const name = "bare-static";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/http-importer/file-url.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "file-url";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/http-importer/mapped.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "mapped";
2 changes: 2 additions & 0 deletions TestRunner/app/tests/esm/http-importer/self-mapped-bare.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_selfMappedBare = true;
export const name = "self-mapped-bare";
2 changes: 2 additions & 0 deletions TestRunner/app/tests/esm/http-importer/self-mapped-tilde.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_selfMappedTilde = true;
export const name = "self-mapped-tilde";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/http-importer/self-mapped.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped";
2 changes: 2 additions & 0 deletions TestRunner/app/tests/esm/http-importer/tilde.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_tilde = true;
export const name = "tilde";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/query-map/literal.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "literal";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/query-map/static-importer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "ns-query-map-pkg?x";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/query-map/stripped.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "stripped";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/self-mapped-file/bare-dep.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-file-bare";
4 changes: 4 additions & 0 deletions TestRunner/app/tests/esm/self-mapped-file/local.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const url = import.meta.url;
export function load(spec) {
return import(spec);
}
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/self-mapped-file/sibling.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-file-sibling";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/self-mapped-file/tilde-dep.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-file-tilde";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/self-mapped-prefix/bare-dep.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-prefix-bare";
4 changes: 4 additions & 0 deletions TestRunner/app/tests/esm/self-mapped-prefix/local.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const url = import.meta.url;
export function load(spec) {
return import(spec);
}
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/self-mapped-prefix/sibling.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-prefix-sibling";
1 change: 1 addition & 0 deletions TestRunner/app/tests/esm/self-mapped-prefix/tilde-dep.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-prefix-tilde";
41 changes: 34 additions & 7 deletions TestRunnerTests/TestRunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class TestRunnerTests: XCTestCase {
let path = (environ["PATH_INFO"] as? String) ?? "/"
let query = (environ["QUERY_STRING"] as? String) ?? ""

func jsStringLiteral(_ s: String) -> String {
return s
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "\n", with: "\\n")
.replacingOccurrences(of: "\r", with: "\\r")
}

// Progress beacon from the in-app Jasmine reporter (fire-and-forget):
// records the spec currently running so a hang/timeout can report
// where the JS suite stalled, even though no JUnit report is POSTed.
Expand Down Expand Up @@ -111,13 +119,6 @@ class TestRunnerTests: XCTestCase {
}

if path == "/esm/query.mjs" || path == "/ns/m/query.mjs" {
func jsStringLiteral(_ s: String) -> String {
return s
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "\n", with: "\\n")
.replacingOccurrences(of: "\r", with: "\\r")
}
let nowMs = Int(Date().timeIntervalSince1970 * 1000.0)
let body = """
export const path = \"\(jsStringLiteral(path))\";
Expand All @@ -130,6 +131,32 @@ class TestRunnerTests: XCTestCase {
return
}

// ── Specifier resolution from an HTTP importer ──
// `…/importer.mjs` exports `load(spec)`, an import() issued
// from its own URL; `…/static.mjs` statically imports the
// specifier in its `spec` param; any other path answers with
// the request target it was reached at and its import.meta.url.
if path.hasPrefix("/esm/resolve/") {
let body: String
if path.hasSuffix("/importer.mjs") {
body = "export function load(spec) { return import(spec); }\n"
} else if path.hasSuffix("/static.mjs") {
var spec = ""
if let pair = query.split(separator: "&").first(where: { $0.hasPrefix("spec=") }),
let decoded = String(pair.dropFirst("spec=".count)).removingPercentEncoding {
spec = decoded
}
body = "export * as dep from \"\(jsStringLiteral(spec))\";\n"
} else {
body = "export const path = \"\(jsStringLiteral(path))\";\n" +
"export const query = \"\(jsStringLiteral(query))\";\n" +
"export const url = import.meta.url;\n"
}
startResponse("200 OK", [("Content-Type", "application/javascript; charset=utf-8")])
sendBody(body.data(using: .utf8) ?? Data())
return
}

// ── MIME-gate routes ──────────────────────────────
if path == "/esm/html-fallback.mjs" {
// The SPA-fallback shape: an unknown path answered with the
Expand Down
47 changes: 46 additions & 1 deletion docs/ns-builtin-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ consults the most specific matching scope first (longest prefix wins), then
progressively less specific ones, then `imports` — so a scope can override a
global mapping for one subtree and fall through to it everywhere else. The
synchronous resolver, the graph walk, and `import()` all resolve through the
same cascade.
same cascade. The map rewrites only the specifiers a module imports, never the
importing module's own URL.

The whole map is parsed and validated before any of it is installed: a
rejected map throws a `TypeError` out of `configureLoader` and the previously
Expand Down Expand Up @@ -944,6 +945,50 @@ unmodified where a shim exists:

## Loading ES modules

### Specifier resolution

A module loaded over http(s) resolves the specifiers it imports, with `import`
and `import()` alike, as below. `ns:`/`node:` builtins resolve first, and the
[import map](#import-maps-and-scopes) is consulted next; the table covers a
specifier the map does not match.

| specifier | from an http(s) importer |
|---|---|
| `./x`, `../x` | the importer's URL, query kept |
| `/x` | the importer's origin, query kept |
| `//host/x` | the importer's scheme, query kept |
| `http(s)://…` | that URL |
| `file:///app/x` | the app root, on disk |
| `~/x` | refused |
| bare (`pkg`, `pkg/x`) | refused: it needs an import-map entry |

Only `/`, `./` and `../` prefixes are relative: `.x`, `.` and `..` are bare
specifiers and are refused without an import-map entry. Specifiers follow the
HTML module-specifier rules: the WHATWG URL parser, the one behind
`new URL()`, decides whether a specifier is relative, an absolute URL or bare,
and produces the path, query and fragment, so dot segments and `\` resolve and
the scheme is lowercased as in a browser. The authority (userinfo, host and
port) keeps its spelling from the importer's URL, or from the specifier when it
names one (`//host/x`, `http(s)://…`). A specifier that resolves to an http(s)
URL therefore names the same module as `new URL(specifier, importerUrl).href`
whenever that authority is already spelled the way `new URL()` spells it (a
lowercase host, no default port).

An import-map entry is never refused — it is explicit configuration, so it may
point an HTTP module at a `~/` path — though a relative or root-relative target
still resolves against the importer's URL. A specifier with any other URL
scheme (`data:`, `file:/x`) is never looked up on disk, and `import()` of a
`blob:nativescript/…` URL loads from the blob store. A refused specifier fails
before anything is fetched or read from disk, with an `Error` whose message
names the specifier, the importer and what to use instead;
`createRequire(<file path>)` is the explicit way for an HTTP module to reach
CommonJS or ES modules on disk.

From a file module, a static `import 'pkg?x'` looks `pkg?x` up in the import
map, while `import('pkg?x')` drops the query and looks up `pkg`. An import-map
target should not itself be a key of the map: `import()` may look a target up
a second time.

### The `require()` specifier

Every `require` — the global one and any minted by `createRequire` /
Expand Down
3 changes: 2 additions & 1 deletion types/ns-module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ declare module "ns:module" {
* resolved on demand.
*
* A key ending in `/` maps a whole subtree and its target must end in `/`
* too; longest match wins. The map is validated in full before it is
* too; longest match wins. A module loaded over `http(s)` can use a bare
* specifier only through this map. The map is validated in full before it is
* installed — an invalid map throws a `TypeError` out of
* {@link configureLoader} and leaves the previously installed map in place.
*/
Expand Down
Loading