Skip to content
Draft
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
7 changes: 6 additions & 1 deletion docs/ns-builtin-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,12 @@ diagnostic that takes a string and throws
build with `allowRemoteModules` on would fetch `url` under a
`security.remoteModuleAllowlist` of `entries` (`true` for `[]`) and throws
`remoteUrlMatchesAllowlist expects a URL string and an array of strings`
otherwise; release builds omit both. Missing members are simply absent — never
otherwise, and `streamedCompileStats()`, which returns process-wide counts of
how HTTP module bodies large enough for a background compile were compiled
(`finalized`, `discarded` after another walk registered the same key first,
`fellBack` to the JS thread), the background compiles `running` now, and the
isolate teardowns that waited one out (`teardownWaits`); release builds omit
all three. Missing members are simply absent — never
present-but-throwing — so feature checks work. The module is registered in
every build: the security boundary for remote module loading sits at the
network layer (`security.allowRemoteModules` in nativescript.config, enforced
Expand Down
4 changes: 4 additions & 0 deletions test-app/app/src/main/assets/app/esm/mixed/big-entry.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Local root whose only edge is "ns-big-leaf", a bare specifier the spec's
// import map points at a generated ModuleTestServer module above the
// streaming-compile threshold.
export * as leaf from "ns-big-leaf";
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Starts a small and a large HTTP import, queues a microtask that imports
// `probe`, reports "spinning" and spins until terminated: the small import's
// fetch completion and the microtask are still pending when it is.
onmessage = function (msg) {
import(msg.data.small);
import(msg.data.big);
Promise.resolve().then(function () {
import(msg.data.probe);
});
postMessage("spinning");
for (;;) {}
};
10 changes: 10 additions & 0 deletions test-app/app/src/main/assets/app/tests/httpImportSettleWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Starts an HTTP dynamic import, reports "fetching" as soon as it is issued
// and "settled" once it resolves or rejects. A terminated worker never sends
// "settled".
onmessage = function (msg) {
function settled() {
postMessage("settled");
}
import(msg.data).then(settled, settled);
postMessage("fetching");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Starts an HTTP dynamic import and reports as soon as its fetch is in flight,
// without waiting for it to settle.
onmessage = function (msg) {
import(msg.data).catch(function () {});
postMessage("fetching");
};
426 changes: 426 additions & 0 deletions test-app/app/src/main/assets/app/tests/testEsmHttpLoader.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion test-app/app/src/main/assets/app/tests/testNsModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe("ns:module", function () {
var expected = ["configureLoader", "createPumpingRequire", "createRequire",
"getLoadedModuleUrls", "invalidateModules"];
if (typeof nsModule.canonicalizeHttpUrlKey === "function") {
expected.push("canonicalizeHttpUrlKey", "remoteUrlMatchesAllowlist");
expected.push("canonicalizeHttpUrlKey", "remoteUrlMatchesAllowlist",
"streamedCompileStats");
}
expect(Object.keys(nsModule).sort()).toEqual(expected.sort());
});
Expand Down
131 changes: 131 additions & 0 deletions test-app/app/src/main/java/com/tns/tests/ModuleTestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,99 @@ private static void route(Socket socket, String path, String query) throws IOExc
return;
}

// Simulates a dev server answering a cold module transform in
// `delayMs`. The parent re-exports the child, a transitive HTTP edge
// the graph walk discovers after the slow fetch lands.
if (path.startsWith("/esm/slow/")) {
int delayMs = intParam(query, "delayMs=", "/esm/slow/child.mjs".equals(path) ? 0 : 7000);
String key = param(query, "k=");
if (key == null) {
key = "slow";
}
String body;
if ("/esm/slow/parent.mjs".equals(path)) {
body = "export { value } from \"./child.mjs?from=" + key + "\";\n"
+ "export const parent = \"" + key + "\";\n";
} else {
body = "export const value = 42; export const name = \"" + key + "\";\n";
}
if (!sleepFor(delayMs)) {
return;
}
respond(socket, "200 OK", JS_MIME, body.getBytes(UTF8));
return;
}

// Generated modules of a requested size.
// /esm/big.mjs: `lines` real declarations whose evaluation folds into
// `total`, so a value depends on the whole body.
// k module id (also the evaluation counter's key)
// unicode 1 adds non-ASCII string literals (strs/first/last)
// error 1 ends the body with a syntax error at line lines+3
// nest appends an array literal nested `nest` deep
// delayMs answers after a delay
// /esm/big-parent.mjs?k=…&child=<encoded big.mjs query> statically
// imports that big.mjs and re-exports it as `child`.
if ("/esm/big.mjs".equals(path) || "/esm/big-parent.mjs".equals(path)) {
String rawKey = decodedParam(query, "k=");
StringBuilder keyBuilder = new StringBuilder();
for (char c : (rawKey == null ? "x" : rawKey).toCharArray()) {
if (Character.isLetterOrDigit(c) || c == '-') {
keyBuilder.append(c);
}
}
String key = keyBuilder.toString();
int delayMs = intParam(query, "delayMs=", 0);
StringBuilder body = new StringBuilder();
if ("/esm/big-parent.mjs".equals(path)) {
String child = decodedParam(query, "child=");
child = child == null ? "" : child.replace("\"", "");
body.append("export * as child from \"./big.mjs?").append(child).append("\";\n")
.append("export const parent = \"").append(key).append("\";\n");
} else {
int lines = intParam(query, "lines=", 0);
boolean unicode = "1".equals(param(query, "unicode="));
body.append("let acc = 0;\nconst strs = [];\n");
for (int i = 0; i < lines; i++) {
body.append("function f").append(i).append("(x) { return (x * 3 + ").append(i)
.append(") % 1000; } acc += f").append(i).append("(").append(i).append(");");
if (unicode) {
body.append(" strs.push(\"café 日本語 😀 ")
.append(i).append("\");");
}
body.append("\n");
}
int depth = intParam(query, "nest=", 0);
if (depth > 0) {
body.append("export const deep = ");
for (int i = 0; i < depth; i++) {
body.append('[');
}
for (int i = 0; i < depth; i++) {
body.append(']');
}
body.append(";\n");
}
if ("1".equals(param(query, "error="))) {
body.append("export const broken = ;\n");
}
body.append("const evaluations = globalThis.__nsBigModuleEvaluations || (globalThis.__nsBigModuleEvaluations = {});\n")
.append("evaluations[\"").append(key).append("\"] = (evaluations[\"").append(key).append("\"] || 0) + 1;\n")
.append("export const id = \"").append(key).append("\";\n")
.append("export const count = ").append(lines).append(";\n")
.append("export const total = acc;\n")
.append("export const first = strs[0];\n")
.append("export const last = strs[strs.length - 1];\n")
.append("export const joinedLength = strs.join(\"\").length;\n");
body.append("export const bytes = ").append(body.toString().getBytes(UTF8).length).append(";\n");
}
if (!sleepFor(delayMs)) {
return;
}
respond(socket, "200 OK", JS_MIME, body.toString().getBytes(UTF8));
return;
}

if ("/esm/timeout.mjs".equals(path)) {
int delayMs = 12000;
String raw = param(query, "delayMs=");
Expand Down Expand Up @@ -351,6 +444,44 @@ private static String param(String query, String prefix) {
return null;
}

private static String decodedParam(String query, String prefix) {
String raw = param(query, prefix);
if (raw == null) {
return null;
}
try {
return URLDecoder.decode(raw, "UTF-8");
} catch (Exception e) {
return raw;
}
}

private static int intParam(String query, String prefix, int fallback) {
String raw = param(query, prefix);
if (raw == null) {
return fallback;
}
try {
return Integer.parseInt(raw);
} catch (NumberFormatException e) {
return fallback;
}
}

/** False when the connection thread was interrupted (the server is stopping). */
private static boolean sleepFor(int delayMs) {
if (delayMs <= 0) {
return true;
}
try {
Thread.sleep(delayMs);
return true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}

private static String jsStringLiteral(String s) {
return s.replace("\\", "\\\\")
.replace("\"", "\\\"")
Expand Down
Loading
Loading