@blotch/angular-lynx-native 22.2.21 → 22.2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +4 -0
  2. package/dist/android/build.d.ts +6 -1
  3. package/dist/android/build.d.ts.map +1 -1
  4. package/dist/android/build.js +14 -3
  5. package/dist/android/run.d.ts +10 -1
  6. package/dist/android/run.d.ts.map +1 -1
  7. package/dist/android/run.js +46 -1
  8. package/dist/dev-build.d.ts +16 -0
  9. package/dist/dev-build.d.ts.map +1 -0
  10. package/dist/dev-build.js +44 -0
  11. package/dist/generate.d.ts +24 -5
  12. package/dist/generate.d.ts.map +1 -1
  13. package/dist/generate.js +14 -6
  14. package/dist/index.d.ts +12 -3
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +65 -9
  17. package/dist/ios/build.d.ts +14 -1
  18. package/dist/ios/build.d.ts.map +1 -1
  19. package/dist/ios/build.js +10 -6
  20. package/dist/ios/iphone.d.ts +1 -1
  21. package/dist/ios/iphone.d.ts.map +1 -1
  22. package/dist/ios/iphone.js +4 -1
  23. package/dist/ios/project.d.ts +8 -1
  24. package/dist/ios/project.d.ts.map +1 -1
  25. package/dist/ios/project.js +17 -3
  26. package/dist/ios/run.d.ts +1 -1
  27. package/dist/ios/run.d.ts.map +1 -1
  28. package/dist/ios/run.js +4 -1
  29. package/dist/run.d.ts +42 -2
  30. package/dist/run.d.ts.map +1 -1
  31. package/dist/run.js +16 -1
  32. package/package.json +1 -1
  33. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +159 -0
  34. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServerModules.kt +163 -0
  35. package/templates/android/app/src/debug/res/xml/network_security_config.xml +13 -0
  36. package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +22 -0
  37. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +8 -1
  38. package/templates/android/app/src/main/java/dev/angularlynx/shell/ResourceFetchers.kt +12 -6
  39. package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +24 -0
  40. package/templates/ios/App/AssetsModule.swift +21 -3
  41. package/templates/ios/App/BundleResourceFetcher.swift +46 -2
  42. package/templates/ios/App/DevServer.swift +190 -0
  43. package/templates/ios/App/DevServerModules.swift +223 -0
  44. package/templates/ios/App/LynxViewController.swift +9 -1
package/dist/ios/build.js CHANGED
@@ -57,7 +57,8 @@ export const resolveIosSigning = (app, env = process.env) => {
57
57
  * would be thrown away, and archiving the `.ipa` takes minutes a Personal
58
58
  * Team's run would spend failing. Returns the artifacts' paths.
59
59
  */
60
- export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDir, logFile, log, signing, device, }) => {
60
+ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDir, logFile, log, signing, device, development = false, simulator, }) => {
61
+ const configuration = development ? 'Debug' : 'Release';
61
62
  linkFrameworks(project.dir, frameworksDir);
62
63
  const xcodebuild = (args) => run('xcodebuild', args, { cwd: project.dir, logFile });
63
64
  const common = ['-project', 'App.xcodeproj', '-scheme', 'App'];
@@ -89,7 +90,7 @@ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDi
89
90
  'build',
90
91
  ...common,
91
92
  '-configuration',
92
- 'Release',
93
+ configuration,
93
94
  '-sdk',
94
95
  'iphoneos',
95
96
  // The phone itself rather than generic/platform=iOS, so automatic
@@ -107,7 +108,7 @@ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDi
107
108
  });
108
109
  const deviceApp = path.join(outputDir, `${app.productName}-device.app`);
109
110
  rmSync(deviceApp, { recursive: true, force: true });
110
- cpSync(path.join(buildDir, 'Build/Products/Release-iphoneos', `${app.productName}.app`), deviceApp, { recursive: true, verbatimSymlinks: true });
111
+ cpSync(path.join(buildDir, `Build/Products/${configuration}-iphoneos`, `${app.productName}.app`), deviceApp, { recursive: true, verbatimSymlinks: true });
111
112
  return [deviceApp];
112
113
  }
113
114
  const artifacts = [];
@@ -116,18 +117,21 @@ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDi
116
117
  'build',
117
118
  ...common,
118
119
  '-configuration',
119
- 'Release',
120
+ configuration,
120
121
  '-sdk',
121
122
  'iphonesimulator',
122
123
  '-destination',
123
- 'generic/platform=iOS Simulator',
124
+ simulator ? `id=${simulator.udid}` : 'generic/platform=iOS Simulator',
124
125
  '-derivedDataPath',
125
126
  buildDir,
126
127
  ]);
127
128
  const simulatorApp = path.join(outputDir, `${app.productName}-simulator.app`);
128
129
  rmSync(simulatorApp, { recursive: true, force: true });
129
- cpSync(path.join(buildDir, 'Build/Products/Release-iphonesimulator', `${app.productName}.app`), simulatorApp, { recursive: true, verbatimSymlinks: true });
130
+ cpSync(path.join(buildDir, `Build/Products/${configuration}-iphonesimulator`, `${app.productName}.app`), simulatorApp, { recursive: true, verbatimSymlinks: true });
130
131
  artifacts.push(simulatorApp);
132
+ if (development) {
133
+ return artifacts;
134
+ }
131
135
  if (!signing?.export) {
132
136
  log(signing
133
137
  ? 'Skipping the .ipa: set ios.signing (an App Store Connect API key) or ios.export in angular.json to build one.'
@@ -1,4 +1,4 @@
1
- import type { RunTarget } from '../run.ts';
1
+ import { type RunTarget } from '../run.ts';
2
2
  /**
3
3
  * A device in `xcrun devicectl list devices --json-output -`, as far as it's
4
4
  * read here. Xcode 27's devicectl moved everything into `properties` and
@@ -1 +1 @@
1
- {"version":3,"file":"iphone.d.ts","sourceRoot":"","sources":["../../src/ios/iphone.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,wDAAwD;IACxD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,CAAC,EAAE;QACpB,QAAQ,CAAC,QAAQ,CAAC,EAAE;YAClB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC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BAAuB,GAClC,SAAS,SAAS,eAAe,EAAE,EACnC,MAAM,MAAM,EACZ,QAAQ,MAAM,EACd,SAAS,MAAM,KACd,MA2BF,CAAC"}
@@ -1,6 +1,7 @@
1
1
  import { existsSync, readFileSync, rmSync } from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { NativeBuildError, capture, run } from "../process.js";
4
+ import { iosDevServerArguments } from "../run.js";
4
5
  /**
5
6
  * The real iOS devices in devicectl's list. devicectl lists the simulators
6
7
  * too, which `simctl` already covers, so only physical ones are kept.
@@ -108,7 +109,7 @@ export const startIPhone = ({ phone, logFile, log, }) => {
108
109
  log(`Running the app on ${phone.name}${phone.model ? ` (${phone.model})` : ''}`);
109
110
  return {
110
111
  iPhone: { udid: phone.udid, name: phone.name },
111
- launch: async (binary, app) => {
112
+ launch: async (binary, app, devServer) => {
112
113
  log(`Installing the app on ${phone.name}`);
113
114
  await devicectl(['device', 'install', 'app', '--device', phone.udid, binary], { phone, action: 'install the app', logFile });
114
115
  // Replaces the copy a previous `--run` left open, which would otherwise
@@ -121,6 +122,8 @@ export const startIPhone = ({ phone, logFile, log, }) => {
121
122
  phone.udid,
122
123
  '--terminate-existing',
123
124
  app.id,
125
+ // devicectl hands what follows the app to it as its arguments.
126
+ ...iosDevServerArguments(devServer),
124
127
  ], { phone, action: 'open the app', logFile });
125
128
  log(`Opened ${app.name} on ${phone.name}`);
126
129
  },
@@ -9,5 +9,12 @@ export declare const iosGenerator: PlatformGenerator;
9
9
  export declare const xcconfig: (app: ResolvedApp) => string;
10
10
  /** What iOS shows the first time someone taps Save Image, without `savePhotos`. */
11
11
  export declare const DEFAULT_SAVE_PHOTOS_REASON = "Saves the images you choose to your photo library.";
12
- export declare const infoPlistEntries: (app: ResolvedApp) => string;
12
+ /**
13
+ * What iOS shows the first time the development app connects to the dev
14
+ * server, on a real iPhone. The simulator never asks.
15
+ */
16
+ export declare const LOCAL_NETWORK_REASON = "Loads the app from ng serve on your computer while you develop it.";
17
+ export declare const infoPlistEntries: (app: ResolvedApp, { development }?: {
18
+ readonly development?: boolean;
19
+ }) => string;
13
20
  //# sourceMappingURL=project.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../../src/ios/project.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,KAAK,EAAe,WAAW,EAAE,MAAM,eAAe,CAAC;AAiB9D;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,iBA6B1B,CAAC;AAQF,eAAO,MAAM,QAAQ,GAAI,KAAK,WAAW,KAAG,MAY9B,CAAC;AAEf,mFAAmF;AACnF,eAAO,MAAM,0BAA0B,uDACe,CAAC;AAEvD,eAAO,MAAM,gBAAgB,GAAI,KAAK,WAAW,KAAG,MAsDnD,CAAC"}
1
+ {"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../../src/ios/project.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,KAAK,EAAe,WAAW,EAAE,MAAM,eAAe,CAAC;AAiB9D;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,iBA6B1B,CAAC;AAQF,eAAO,MAAM,QAAQ,GAAI,KAAK,WAAW,KAAG,MAY9B,CAAC;AAEf,mFAAmF;AACnF,eAAO,MAAM,0BAA0B,uDACe,CAAC;AAEvD;;;GAGG;AACH,eAAO,MAAM,oBAAoB,uEACqC,CAAC;AAEvE,eAAO,MAAM,gBAAgB,GAC3B,KAAK,WAAW,EAChB,kBAAyB;IAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAA;CAAO,KAC/D,MAmEF,CAAC"}
@@ -21,10 +21,10 @@ const ORIENTATIONS = {
21
21
  * between the template's `angular-lynx:info` markers.
22
22
  */
23
23
  export const iosGenerator = {
24
- configure: (dir, app, projectRoot) => {
24
+ configure: (dir, app, projectRoot, development = false) => {
25
25
  writeFileSync(path.join(dir, 'Config/App.xcconfig'), xcconfig(app));
26
26
  const plistPath = path.join(dir, 'App/Info.plist');
27
- writeFileSync(plistPath, replaceMarker(readFileSync(plistPath, 'utf8'), 'info', infoPlistEntries(app)));
27
+ writeFileSync(plistPath, replaceMarker(readFileSync(plistPath, 'utf8'), 'info', infoPlistEntries(app, { development })));
28
28
  if (app.icon) {
29
29
  const icon = path.resolve(projectRoot, app.icon);
30
30
  const size = readPngSize(icon);
@@ -54,7 +54,12 @@ export const xcconfig = (app) => [
54
54
  ].join('\n');
55
55
  /** What iOS shows the first time someone taps Save Image, without `savePhotos`. */
56
56
  export const DEFAULT_SAVE_PHOTOS_REASON = 'Saves the images you choose to your photo library.';
57
- export const infoPlistEntries = (app) => {
57
+ /**
58
+ * What iOS shows the first time the development app connects to the dev
59
+ * server, on a real iPhone. The simulator never asks.
60
+ */
61
+ export const LOCAL_NETWORK_REASON = 'Loads the app from ng serve on your computer while you develop it.';
62
+ export const infoPlistEntries = (app, { development = false } = {}) => {
58
63
  const lines = [];
59
64
  const array = (key, values) => {
60
65
  lines.push(`<key>${key}</key>`, '<array>');
@@ -78,6 +83,15 @@ export const infoPlistEntries = (app) => {
78
83
  lines.push(`<key>${key}</key>`, `<string>${escapeXml(reason)}</string>`);
79
84
  }
80
85
  }
86
+ // `ng serve --platform ios`'s development app loads from the dev server at
87
+ // the Mac's address on the local network, which iOS (14 and later) lets an
88
+ // app reach only once the person allows it. iOS asks the first time, with
89
+ // this reason. Only the development app has it: the app for the stores
90
+ // loads its own bundle, and App Review asks about any reason it doesn't
91
+ // need.
92
+ if (development) {
93
+ lines.push('<key>NSLocalNetworkUsageDescription</key>', `<string>${escapeXml(LOCAL_NETWORK_REASON)}</string>`);
94
+ }
81
95
  // The schemes LinkingModule's canOpenURL can ask about: iOS answers false
82
96
  // for any other. UIApplication.open needs none of them.
83
97
  array('LSApplicationQueriesSchemes', app.canOpenURLSchemes);
package/dist/ios/run.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RunTarget } from '../run.ts';
1
+ import { type RunTarget } from '../run.ts';
2
2
  /** What `xcrun simctl list --json devices runtimes` prints, as far as it's read here. */
3
3
  export type SimctlList = {
4
4
  readonly devices: Readonly<Record<string, readonly SimctlDevice[]>>;
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../src/ios/run.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAG3C,yFAAyF;AACzF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,CAAC,CAAC,CAAC;IACpE,QAAQ,CAAC,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAC;CAC7C,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0DAA0D;IAC1D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,KAAK,aAAa,GAAG;IACnB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,uBAAuB;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,oBAAoB;IACpB,QAAQ,CAAC,oBAAoB,CAAC,EAAE,SAAS;QACvC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;Q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;IAC5D,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;IAChC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,GACxB,MAAM,UAAU,EAChB,QAAQ,MAAM,GAAG,SAAS;AAC1B,qEAAqE;AACrE,UAAS,SAAS,MAAM,EAAO,KAC9B,SAsFF,CAAC;AAsBF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAU,2BAIlC;IACD,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC,KAAG,OAAO,CAAC,SAAS,CAcpB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAU,oCAKrC;IACD,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACxC,qEAAqE;IACrE,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC,KAAG,OAAO,CAAC,SAAS,CAgEpB,CAAC"}
package/dist/ios/run.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { NativeBuildError, capture, run } from "../process.js";
4
+ import { iosDevServerArguments } from "../run.js";
4
5
  import { listIPhones, pickIPhone, startIPhone } from "./iphone.js";
5
6
  /**
6
7
  * Chooses the simulator `ng build --platform ios --run` opens the app on.
@@ -149,7 +150,8 @@ export const startIosSimulator = async ({ device, logFile, log, iPhones, }) => {
149
150
  const booted = xcrun(['simctl', 'bootstatus', udid, '-b']);
150
151
  booted.catch(() => { });
151
152
  return {
152
- launch: async (binary, app) => {
153
+ simulator: { udid },
154
+ launch: async (binary, app, devServer) => {
153
155
  await booted;
154
156
  await showSimulator(udid, logFile, log);
155
157
  log(`Installing the app on ${simulator.name}`);
@@ -162,6 +164,7 @@ export const startIosSimulator = async ({ device, logFile, log, iPhones, }) => {
162
164
  '--terminate-running-process',
163
165
  udid,
164
166
  app.id,
167
+ ...iosDevServerArguments(devServer),
165
168
  ]);
166
169
  log(`Opened ${app.name} on ${simulator.name}`);
167
170
  },
package/dist/run.d.ts CHANGED
@@ -9,6 +9,20 @@ export type RunOptions = {
9
9
  */
10
10
  readonly device?: string;
11
11
  };
12
+ /**
13
+ * `ng serve --platform <platform>`'s dev server, which the development app
14
+ * loads its bundle from instead of the copy inside it.
15
+ */
16
+ export type DevServerOptions = {
17
+ /**
18
+ * The Lynx bundle's URL on the dev server, as the bundle itself names the
19
+ * server: rspeedy builds it with this host in its asset URLs and its live
20
+ * reload socket (`dev.assetPrefix`), and the QR code for Lynx Explorer
21
+ * carries it. Usually the Mac's address on the local network, so a phone
22
+ * on the same Wi-Fi reaches it.
23
+ */
24
+ readonly bundleUrl: string;
25
+ };
12
26
  /**
13
27
  * A simulator, emulator or phone picked for `--run`. Picking it and starting
14
28
  * it come before the native build, so a device that doesn't exist fails in
@@ -23,7 +37,33 @@ export type RunTarget = {
23
37
  readonly udid: string;
24
38
  readonly name: string;
25
39
  };
26
- /** Waits for the device to finish starting, then installs and opens the app. */
27
- readonly launch: (binary: string, app: ResolvedApp) => Promise<void>;
40
+ /**
41
+ * The simulator the app runs on, which a development build is compiled for
42
+ * alone, rather than for every simulator.
43
+ */
44
+ readonly simulator?: {
45
+ readonly udid: string;
46
+ };
47
+ /**
48
+ * Waits for the device to finish starting, then installs and opens the
49
+ * app. With `devServer`, the app is told to load its bundle from there.
50
+ */
51
+ readonly launch: (binary: string, app: ResolvedApp, devServer?: DevServerOptions) => Promise<void>;
28
52
  };
53
+ /**
54
+ * The launch argument (iOS) and intent extra (Android) that hand the
55
+ * development app its dev server, and the one that names the host the bundle
56
+ * was built with, when the app reaches the server under another (Android's
57
+ * `localhost`, through `adb reverse`). The shells' DevServer reads them.
58
+ */
59
+ export declare const DEV_SERVER_ARGUMENT = "AngularLynxDevServer";
60
+ export declare const DEV_SERVER_ORIGIN_ARGUMENT = "AngularLynxDevServerOrigin";
61
+ /**
62
+ * The launch arguments that point the development app at the dev server.
63
+ * iOS puts `-Name value` arguments in the app's UserDefaults (its argument
64
+ * domain), where DevServer.swift reads them. A simulator shares the Mac's
65
+ * network, and an iPhone on the same Wi-Fi reaches the Mac's address, so
66
+ * both load the bundle from the URL it was built with.
67
+ */
68
+ export declare const iosDevServerArguments: (devServer: DevServerOptions | undefined) => string[];
29
69
  //# sourceMappingURL=run.d.ts.map
package/dist/run.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,wDAAwD;AACxD,MAAM,MAAM,UAAU,GAAG;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACnE,gFAAgF;IAChF,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACtE,CAAC"}
1
+ {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,wDAAwD;AACxD,MAAM,MAAM,UAAU,GAAG;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACnE;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAC/C;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,CACf,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,WAAW,EAChB,SAAS,CAAC,EAAE,gBAAgB,KACzB,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,yBAAyB,CAAC;AAC1D,eAAO,MAAM,0BAA0B,+BAA+B,CAAC;AAEvE;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,GAChC,WAAW,gBAAgB,GAAG,SAAS,KACtC,MAAM,EAC0D,CAAC"}
package/dist/run.js CHANGED
@@ -1 +1,16 @@
1
- export {};
1
+ /**
2
+ * The launch argument (iOS) and intent extra (Android) that hand the
3
+ * development app its dev server, and the one that names the host the bundle
4
+ * was built with, when the app reaches the server under another (Android's
5
+ * `localhost`, through `adb reverse`). The shells' DevServer reads them.
6
+ */
7
+ export const DEV_SERVER_ARGUMENT = 'AngularLynxDevServer';
8
+ export const DEV_SERVER_ORIGIN_ARGUMENT = 'AngularLynxDevServerOrigin';
9
+ /**
10
+ * The launch arguments that point the development app at the dev server.
11
+ * iOS puts `-Name value` arguments in the app's UserDefaults (its argument
12
+ * domain), where DevServer.swift reads them. A simulator shares the Mac's
13
+ * network, and an iPhone on the same Wi-Fi reaches the Mac's address, so
14
+ * both load the bundle from the URL it was built with.
15
+ */
16
+ export const iosDevServerArguments = (devServer) => devServer ? [`-${DEV_SERVER_ARGUMENT}`, devServer.bundleUrl] : [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotch/angular-lynx-native",
3
- "version": "22.2.21",
3
+ "version": "22.2.22",
4
4
  "description": "Builds AngularLynx apps into iOS and Android binaries",
5
5
  "homepage": "https://github.com/Blotch-Smart-Frames/AngularLynx#readme",
6
6
  "bugs": {
@@ -0,0 +1,159 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import android.app.Activity
4
+ import android.content.Context
5
+ import android.net.Uri
6
+ import android.os.Handler
7
+ import android.os.Looper
8
+ import android.util.TypedValue
9
+ import android.view.Gravity
10
+ import android.view.View
11
+ import android.widget.FrameLayout
12
+ import android.widget.TextView
13
+ import com.lynx.tasm.LynxViewBuilder
14
+
15
+ /**
16
+ * `ng serve --platform android`: the development app loads its bundle from
17
+ * the dev server rather than from its assets (where it has none), and
18
+ * reloads it on every edit, as Lynx Explorer does.
19
+ *
20
+ * This is the debug build type's copy, the one `ng serve --platform android`
21
+ * builds; the release build's (src/release) does nothing.
22
+ *
23
+ * The builder opens the activity with two extras: `AngularLynxDevServer`, the
24
+ * bundle at `http://localhost:<port>/`, which `adb reverse` forwards to the
25
+ * computer whatever network the device is on, and
26
+ * `AngularLynxDevServerOrigin`, the address the bundle was built with (the
27
+ * computer's on the local network), whose URLs are sent to localhost too.
28
+ * They're kept, so opening the app from the launcher later loads from the
29
+ * same server.
30
+ */
31
+ object DevServer {
32
+ private const val EXTRA_URL = "AngularLynxDevServer"
33
+ private const val EXTRA_ORIGIN = "AngularLynxDevServerOrigin"
34
+ private const val PREFERENCES = "angular-lynx-dev-server"
35
+
36
+ private val main = Handler(Looper.getMainLooper())
37
+
38
+ /** The bundle on the dev server, e.g. `http://localhost:3000/main.lynx.bundle`. */
39
+ private var bundle: String? = null
40
+
41
+ /** The folder it's in, which every relative URL the bundle asks for resolves against. */
42
+ private var base: Uri? = null
43
+
44
+ /** The address the bundle was built with, e.g. `http://192.168.1.20:3000`. */
45
+ private var origin: Uri? = null
46
+
47
+ private var reload: (() -> Unit)? = null
48
+ private var status: TextView? = null
49
+ private val retry = Runnable { reload?.invoke() }
50
+
51
+ /** The URL MainActivity renders, or null when it was never given one. */
52
+ fun bundleUrl(activity: Activity): String? {
53
+ val preferences = activity.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE)
54
+ val launched = activity.intent?.getStringExtra(EXTRA_URL)
55
+ if (launched != null) {
56
+ preferences.edit()
57
+ .putString(EXTRA_URL, launched)
58
+ .putString(EXTRA_ORIGIN, activity.intent.getStringExtra(EXTRA_ORIGIN))
59
+ .apply()
60
+ }
61
+ bundle = preferences.getString(EXTRA_URL, null)
62
+ base = bundle?.let { Uri.parse(it.substring(0, it.lastIndexOf('/') + 1)) }
63
+ origin = preferences.getString(EXTRA_ORIGIN, null)?.let(Uri::parse)
64
+ return bundle
65
+ }
66
+
67
+ /**
68
+ * Adds the dev server's modules: the WebSocket rspeedy's live reload
69
+ * listens on, and the reload it asks for (DevServerModules.kt).
70
+ */
71
+ fun register(builder: LynxViewBuilder, reload: () -> Unit) {
72
+ if (bundle == null) return
73
+ this.reload = reload
74
+ builder.registerModule(DevWebSocketModule.NAME, DevWebSocketModule::class.java, null)
75
+ builder.registerModule(DevReloadModule.NAME, DevReloadModule::class.java, null)
76
+ }
77
+
78
+ /** Adds the dev server's status, hidden, over the page. */
79
+ fun attach(container: FrameLayout) {
80
+ val context = container.context
81
+ val view = TextView(context).apply {
82
+ gravity = Gravity.CENTER
83
+ setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
84
+ val padding = (24 * resources.displayMetrics.density).toInt()
85
+ setPadding(padding, padding, padding, padding)
86
+ setBackgroundColor(context.getColor(R.color.window_background))
87
+ visibility = View.GONE
88
+ }
89
+ container.addView(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
90
+ status = view
91
+ if (bundle == null) {
92
+ show(
93
+ "This is the development build of the app, which loads the app from `ng serve`.\n\n" +
94
+ "Run `ng serve --platform android` to open it.",
95
+ )
96
+ }
97
+ }
98
+
99
+ /** Reloads the bundle from the dev server, from any thread. */
100
+ fun reloadBundle() {
101
+ main.post { reload?.invoke() }
102
+ }
103
+
104
+ /**
105
+ * Where a URL the bundle asks for is on the dev server, or null when the
106
+ * app isn't loading from one, or the URL is somewhere else (an API, a
107
+ * CDN, a data: or content: URL).
108
+ */
109
+ fun remoteUrl(url: String): String? {
110
+ val base = base ?: return null
111
+ val uri = Uri.parse(url)
112
+ val scheme = uri.scheme
113
+ if (scheme == "http" || scheme == "https" || scheme == "ws" || scheme == "wss") {
114
+ // The address the bundle was built with: the same server, which
115
+ // this device reaches at localhost.
116
+ if (origin?.encodedAuthority == uri.encodedAuthority) {
117
+ return uri.buildUpon().encodedAuthority(base.encodedAuthority).build().toString()
118
+ }
119
+ return if (uri.encodedAuthority == base.encodedAuthority) url else null
120
+ }
121
+ val path = when {
122
+ url.startsWith("asset:///") -> url.removePrefix("asset:///")
123
+ scheme == null -> url.removePrefix("./").trimStart('/')
124
+ else -> return null
125
+ }
126
+ return base.toString() + path
127
+ }
128
+
129
+ /**
130
+ * Called with the outcome of each load. When the bundle itself didn't
131
+ * load (the server is stopped, or `adb reverse` is gone after the device
132
+ * reconnected), the app says so rather than show nothing, and tries again
133
+ * every two seconds.
134
+ */
135
+ fun loaded(url: String, error: Throwable?) {
136
+ val bundle = bundle ?: return
137
+ if (url != bundle) return
138
+ main.post {
139
+ main.removeCallbacks(retry)
140
+ if (error == null) {
141
+ status?.visibility = View.GONE
142
+ return@post
143
+ }
144
+ show(
145
+ "Can't reach the dev server at ${base?.encodedAuthority}.\n\n${error.message}\n\n" +
146
+ "Is `ng serve --platform android` running? It forwards the port to this device again " +
147
+ "when it starts. Trying again…",
148
+ )
149
+ main.postDelayed(retry, 2000)
150
+ }
151
+ }
152
+
153
+ private fun show(message: String) {
154
+ val status = status ?: return
155
+ status.text = message
156
+ status.visibility = View.VISIBLE
157
+ status.bringToFront()
158
+ }
159
+ }
@@ -0,0 +1,163 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import com.lynx.jsbridge.LynxContextModule
4
+ import com.lynx.jsbridge.LynxMethod
5
+ import com.lynx.jsbridge.LynxModule
6
+ import com.lynx.react.bridge.Callback
7
+ import com.lynx.react.bridge.JavaOnlyArray
8
+ import com.lynx.react.bridge.JavaOnlyMap
9
+ import com.lynx.react.bridge.ReadableArray
10
+ import com.lynx.react.bridge.ReadableMap
11
+ import com.lynx.tasm.behavior.LynxContext
12
+ import okhttp3.OkHttpClient
13
+ import okhttp3.Request
14
+ import okhttp3.Response
15
+ import okhttp3.WebSocket
16
+ import okhttp3.WebSocketListener
17
+ import okio.ByteString
18
+ import org.json.JSONObject
19
+ import java.util.concurrent.ConcurrentHashMap
20
+ import java.util.concurrent.TimeUnit
21
+
22
+ /*
23
+ * The two native modules rspeedy's live reload needs, in the debug build
24
+ * only (see DevServer.kt). Lynx Explorer has them from Lynx DevTool, which
25
+ * the shell doesn't include: the dev server's client, which rspeedy puts in
26
+ * every development bundle, talks to the server over
27
+ * `NativeModules.LynxWebSocketModule` (through @lynx-js/websocket) and
28
+ * reloads the page with `NativeModules.LynxDevToolSetModule.invokeCdp`, a
29
+ * DevTool protocol `Page.reload`. These answer to the same names and calls,
30
+ * so the bundle runs here as it does in Lynx Explorer.
31
+ */
32
+
33
+ /**
34
+ * `NativeModules.LynxWebSocketModule`, as Lynx DevTool's
35
+ * (LynxWebSocket.java): `connect`, `send`, `ping` and `close`, each with the
36
+ * socket's ID, and the `websocketOpen`, `websocketMessage`, `websocketClosed`
37
+ * and `websocketFailed` global events @lynx-js/websocket listens for.
38
+ */
39
+ class DevWebSocketModule : LynxContextModule {
40
+ // Lynx creates a module through whichever of its constructors it finds.
41
+ constructor(context: LynxContext) : super(context)
42
+ constructor(context: LynxContext, param: Any?) : super(context, param)
43
+
44
+ private val sockets = ConcurrentHashMap<Int, WebSocket>()
45
+
46
+ // No read timeout: the dev server only writes when there's a new build.
47
+ private val client = OkHttpClient.Builder()
48
+ .connectTimeout(10, TimeUnit.SECONDS)
49
+ .readTimeout(0, TimeUnit.MILLISECONDS)
50
+ .build()
51
+
52
+ @LynxMethod
53
+ fun connect(url: String, protocols: ReadableArray?, options: ReadableMap?, socketID: Double) {
54
+ val id = socketID.toInt()
55
+ val request = Request.Builder()
56
+ // The address the bundle was built with reaches this device's
57
+ // localhost through adb reverse.
58
+ .url(DevServer.remoteUrl(url) ?: url)
59
+ .apply {
60
+ val names = (0 until (protocols?.size() ?: 0)).mapNotNull { protocols?.getString(it)?.trim() }
61
+ .filter { it.isNotEmpty() && !it.contains(',') }
62
+ if (names.isNotEmpty()) addHeader("Sec-WebSocket-Protocol", names.joinToString(","))
63
+ }
64
+ .build()
65
+ client.newWebSocket(request, object : WebSocketListener() {
66
+ override fun onOpen(webSocket: WebSocket, response: Response) {
67
+ sockets[id] = webSocket
68
+ emit("websocketOpen", "id" to id, "protocol" to (response.header("Sec-WebSocket-Protocol") ?: ""))
69
+ }
70
+
71
+ override fun onMessage(webSocket: WebSocket, text: String) =
72
+ emit("websocketMessage", "id" to id, "type" to "text", "data" to text)
73
+
74
+ override fun onMessage(webSocket: WebSocket, bytes: ByteString) =
75
+ emit("websocketMessage", "id" to id, "type" to "binary", "data" to bytes.base64())
76
+
77
+ override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
78
+ webSocket.close(code, reason)
79
+ }
80
+
81
+ override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
82
+ sockets.remove(id)
83
+ emit("websocketClosed", "id" to id, "code" to code, "reason" to reason)
84
+ }
85
+
86
+ // The connection failed or dropped (the server stopped): the dev
87
+ // server's client then tries again, with a growing delay.
88
+ override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
89
+ sockets.remove(id)
90
+ emit("websocketFailed", "id" to id, "message" to (t.message ?: t.toString()))
91
+ emit("websocketClosed", "id" to id, "code" to 1006, "reason" to (t.message ?: ""))
92
+ }
93
+ })
94
+ }
95
+
96
+ @LynxMethod
97
+ fun send(message: String, socketID: Double) {
98
+ sockets[socketID.toInt()]?.send(message)
99
+ }
100
+
101
+ @LynxMethod
102
+ fun ping(socketID: Double) {
103
+ sockets[socketID.toInt()]?.send(ByteString.EMPTY)
104
+ }
105
+
106
+ @LynxMethod
107
+ fun close(code: Double, reason: String, socketID: Double) {
108
+ sockets.remove(socketID.toInt())?.close(code.toInt(), reason)
109
+ }
110
+
111
+ override fun destroy() {
112
+ sockets.values.forEach { it.close(1001, "The page closed") }
113
+ sockets.clear()
114
+ super.destroy()
115
+ }
116
+
117
+ private fun emit(event: String, vararg params: Pair<String, Any>) {
118
+ val map = JavaOnlyMap()
119
+ for ((key, value) in params) {
120
+ when (value) {
121
+ is Int -> map.putInt(key, value)
122
+ else -> map.putString(key, value.toString())
123
+ }
124
+ }
125
+ mLynxContext.sendGlobalEvent(event, JavaOnlyArray().apply { pushMap(map) })
126
+ }
127
+
128
+ companion object {
129
+ const val NAME = "LynxWebSocketModule"
130
+ }
131
+ }
132
+
133
+ /**
134
+ * `NativeModules.LynxDevToolSetModule`, as far as rspeedy's live reload uses
135
+ * Lynx DevTool's: `invokeCdp` with a `Page.reload`, which renders the bundle
136
+ * from the dev server again, as Lynx DevTool's page reload does. It answers
137
+ * any other DevTool method with an error.
138
+ */
139
+ class DevReloadModule(context: android.content.Context, param: Any?) : LynxModule(context, param) {
140
+ @LynxMethod
141
+ fun invokeCdp(message: String, callback: Callback?) {
142
+ val request = runCatching { JSONObject(message) }.getOrDefault(JSONObject())
143
+ val id = request.opt("id") ?: 0
144
+ val method = request.optString("method")
145
+ val reply = JSONObject().put("id", id)
146
+ if (method == "Page.reload") {
147
+ DevServer.reloadBundle()
148
+ reply.put("result", JSONObject())
149
+ } else {
150
+ reply.put(
151
+ "error",
152
+ JSONObject()
153
+ .put("code", -32601)
154
+ .put("message", "$method needs Lynx DevTool, which this app doesn't have."),
155
+ )
156
+ }
157
+ callback?.invoke(reply.toString())
158
+ }
159
+
160
+ companion object {
161
+ const val NAME = "LynxDevToolSetModule"
162
+ }
163
+ }
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!--
3
+ The debug build's network security config, in place of src/main's: plain
4
+ HTTP to any host. Only `ng serve` builds the debug build type (with the
5
+ platform option set to android). The dev
6
+ server is plain HTTP, and a development bundle names it by the computer's
7
+ address on the local network (rspeedy's dev.assetPrefix), which the app's
8
+ own fetch() of an imported file goes to directly. The release build keeps
9
+ src/main's, which allows plain HTTP to localhost alone.
10
+ -->
11
+ <network-security-config>
12
+ <base-config cleartextTrafficPermitted="true" />
13
+ </network-security-config>