Skip to content

Commit b537105

Browse files
committed
feat: app file layers and customizers
Make the app file sources a list that plugins can change before the runtime starts. A layer is a closed, immutable descriptor (AppFileLayer): directory(dir), apkAssets(assetBase) or map(path -> file | asset | whiteout), with paths relative to the app root; customizer layers never see internal/ or metadata/. The built-in layers keep the ids "extracted" and "apk" (the latter only with android.loadCodeFromApk). Customizers implement AppFileLayersCustomizer { int priority(); void customize(AppFileLayers.Builder) } and are registered androidx.startup style: <meta-data android:name="com.example.MyCustomizer" android:value="org.nativescript.AppFileLayersCustomizer" /> RuntimeHelper instantiates them by reflection before extraction and runs them in ascending priority. The builder starts from "extracted" above "apk" and exposes the context, isDebuggable, isAppUpdated, loadsCodeFromApk, the layer ids and insertAbove/insertBelow/remove. In debuggable builds "extracted" is pinned back on top afterwards so LiveSync'd files always win; release builds have no LiveSync, so there a customizer may deliberately place a layer above the extracted files. A customizer that cannot be created or that throws (including a LinkageError from a missing dependency) is logged and skipped. The resulting list is handed to native once, as flat arrays. A map entry whose file or asset does not exist falls through to the layers below; only a whiteout hides them. Layering only affects runtime-mediated reads (modules, JSON, source maps); files other code opens by path, such as fonts and images, are not layered. The test app registers a customizer with a map, a directory and an APK asset layer, covered by tests/testAppFileLayers.js.
1 parent a8c1fb5 commit b537105

15 files changed

Lines changed: 749 additions & 53 deletions

File tree

‎test-app/app/src/main/AndroidManifest.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<category android:name="android.intent.category.LAUNCHER" />
2424
</intent-filter>
2525
</activity>
26+
<meta-data
27+
android:name="com.tns.tests.TestAppFileLayersCustomizer"
28+
android:value="org.nativescript.AppFileLayersCustomizer" />
2629
<activity
2730
android:name="com.tns.ErrorReportActivity"
2831
android:label="ErrorActivity">

‎test-app/app/src/main/assets/app/mainpage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ require('./tests/testNodeUrlModule');
116116
require('./tests/testImportMetaResolution');
117117
require('./tests/testWorkerEsmEntry');
118118
require('./tests/testCodeCache');
119+
require('./tests/testAppFileLayers');
119120
// Worker wrapper reachability across GC (strong while running, collectable after)
120121
require('./tests/testWorkerLifetime');
121122
// Java->JS calls route to the runtime that created the object
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports.value = 'target';
2+
module.exports.filename = __filename;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Layers come from com.tns.tests.TestAppFileLayersCustomizer, registered in the
2+
// test app's manifest.
3+
describe("App file layers", function () {
4+
var dir = __dirname + "/appFileLayers/";
5+
6+
it("serves a map entry from an APK asset under the requested path", function () {
7+
var remapped = require("./appFileLayers/remapped.js");
8+
expect(remapped.value).toBe("target");
9+
expect(remapped.filename).toBe(dir + "remapped.js");
10+
expect(com.tns.AppFiles.isFile(dir + "remapped.js")).toBe(true);
11+
});
12+
13+
it("serves files from a customizer's directory layer", function () {
14+
expect(require("./appFileLayers/fromDirectory.js").value).toBe("directory");
15+
});
16+
17+
it("serves ES modules from a customizer's directory layer", function (done) {
18+
import(dir + "fromDirectory.mjs").then(function (ns) {
19+
expect(ns.value).toBe("directory-esm");
20+
done();
21+
}, function (e) {
22+
expect(String(e)).toBe("");
23+
done();
24+
});
25+
});
26+
27+
it("serves files from a customizer's APK asset layer", function () {
28+
expect(require("./appFileLayers/fromApkBase.js").value).toBe("apk-assets");
29+
});
30+
31+
it("hides a whited-out path that a lower layer has", function () {
32+
expect(com.tns.AppFiles.isFile(dir + "whitedOut.js")).toBe(false);
33+
expect(function () {
34+
require("./appFileLayers/whitedOut.js");
35+
}).toThrow();
36+
});
37+
38+
it("never layers the runtime's internal files", function () {
39+
expect(com.tns.AppFiles.isFile(__approot + "/internal/ts_helpers.js")).toBe(true);
40+
expect(com.tns.AppFiles.isFile(__approot + "/internal/tests/appFileLayers/remapped.js")).toBe(false);
41+
});
42+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.value = 'apk-assets';

‎test-app/app/src/main/java/com/tns/RuntimeHelper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public static Runtime initRuntime(Context context) {
7171
} finally {
7272
loadLibraryFrame.close();
7373
}
74-
AppFiles.init(context);
7574

7675
Logger logger = new LogcatLogger(context);
7776

@@ -84,6 +83,7 @@ public static Runtime initRuntime(Context context) {
8483
Thread.setDefaultUncaughtExceptionHandler(exHandler);
8584

8685
DefaultExtractPolicy extractPolicy = new DefaultExtractPolicy(logger);
86+
AppFiles.init(context, extractPolicy.shouldExtract(context));
8787
boolean skipAssetExtraction = Util.runPlugin(logger, context);
8888

8989
String appName = context.getPackageName();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.tns.tests;
2+
3+
import com.tns.AppFileLayer;
4+
import com.tns.AppFileLayers;
5+
import com.tns.AppFileLayersCustomizer;
6+
7+
import java.io.File;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
// Backs tests/testAppFileLayers.js; the layers answer only paths under tests/appFileLayers/.
15+
public class TestAppFileLayersCustomizer implements AppFileLayersCustomizer {
16+
static final String MAP_LAYER = "test-map";
17+
static final String DIRECTORY_LAYER = "test-directory";
18+
static final String ASSETS_LAYER = "test-assets";
19+
20+
@Override
21+
public int priority() {
22+
return 0;
23+
}
24+
25+
@Override
26+
public void customize(AppFileLayers.Builder builder) {
27+
File dir = new File(builder.context().getNoBackupFilesDir(), "appFileLayersTest");
28+
write(new File(dir, "tests/appFileLayers/fromDirectory.js"), "module.exports.value = 'directory';\n");
29+
write(new File(dir, "tests/appFileLayers/fromDirectory.mjs"), "export const value = 'directory-esm';\n");
30+
write(new File(dir, "tests/appFileLayers/whitedOut.js"), "module.exports.value = 'should be hidden';\n");
31+
32+
Map<String, AppFileLayer.Entry> entries = new HashMap<>();
33+
entries.put("tests/appFileLayers/remapped.js", AppFileLayer.Entry.asset("app/tests/appFileLayers/target.js"));
34+
entries.put("tests/appFileLayers/whitedOut.js", AppFileLayer.Entry.whiteout());
35+
36+
builder.insertBelow(AppFileLayers.EXTRACTED, AppFileLayer.map(MAP_LAYER, entries));
37+
builder.insertBelow(MAP_LAYER, AppFileLayer.directory(DIRECTORY_LAYER, dir));
38+
builder.insertBelow(DIRECTORY_LAYER, AppFileLayer.apkAssets(ASSETS_LAYER, "layertest"));
39+
}
40+
41+
private static void write(File file, String text) {
42+
file.getParentFile().mkdirs();
43+
try (FileOutputStream out = new FileOutputStream(file)) {
44+
out.write(text.getBytes(StandardCharsets.UTF_8));
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
}
49+
}

‎test-app/runtime/consumer-rules.pro‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
-keep class com.tns.internal.** { *; }
99

1010
# Preserve annotation metadata so reflection sees them
11-
-keepattributes RuntimeVisibleAnnotations
11+
-keepattributes RuntimeVisibleAnnotations
12+
# Customizers are only named in the manifest and created by reflection
13+
-keep class * implements com.tns.AppFileLayersCustomizer { <init>(); }

0 commit comments

Comments
 (0)