@mentra/crust 3.2.1 → 3.3.0-beta.942

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 (52) hide show
  1. package/README.md +27 -0
  2. package/android/build.gradle +15 -9
  3. package/android/src/main/AndroidManifest.xml +1 -0
  4. package/android/src/main/java/com/mentra/crust/CrustModule.kt +44 -15
  5. package/android/src/main/java/com/mentra/crust/http/NativeHttpFailure.kt +31 -0
  6. package/android/src/main/java/com/mentra/crust/http/NativeHttpRequests.kt +86 -0
  7. package/android/src/main/java/com/mentra/crust/jsc/JSCPolyfillBridge.kt +1 -25
  8. package/android/src/main/java/com/mentra/crust/jsc/JSCRuntime.kt +65 -8
  9. package/android/src/main/java/com/mentra/crust/jsc/JSDeliveryDiagnostics.kt +44 -0
  10. package/android/src/main/java/com/mentra/crust/navigation/NavigationManager.kt +4 -0
  11. package/android/src/main/java/com/mentra/crust/preview/PixelCopyPreview.kt +76 -0
  12. package/android/src/test/java/com/mentra/crust/http/NativeHttpFailureTest.kt +44 -0
  13. package/android/src/test/java/com/mentra/crust/http/NativeHttpRequestsTest.kt +133 -0
  14. package/android/src/test/java/com/mentra/crust/jsc/JSDeliveryDiagnosticsTest.kt +54 -0
  15. package/android/src/test/java/com/mentra/crust/jsc/QuickJsLifecycleTest.kt +87 -0
  16. package/build/Crust.types.d.ts +25 -11
  17. package/build/Crust.types.d.ts.map +1 -1
  18. package/build/Crust.types.js.map +1 -1
  19. package/build/CrustModule.d.ts +9 -2
  20. package/build/CrustModule.d.ts.map +1 -1
  21. package/build/CrustModule.js.map +1 -1
  22. package/build/CrustModule.web.d.ts +3 -1
  23. package/build/CrustModule.web.d.ts.map +1 -1
  24. package/build/CrustModule.web.js +20 -8
  25. package/build/CrustModule.web.js.map +1 -1
  26. package/build/index.d.ts +1 -0
  27. package/build/index.d.ts.map +1 -1
  28. package/build/index.js +1 -0
  29. package/build/index.js.map +1 -1
  30. package/build/nativeHttpRequest.d.ts +13 -0
  31. package/build/nativeHttpRequest.d.ts.map +1 -0
  32. package/build/nativeHttpRequest.js +51 -0
  33. package/build/nativeHttpRequest.js.map +1 -0
  34. package/build/nativeHttpRequest.test.d.ts +2 -0
  35. package/build/nativeHttpRequest.test.d.ts.map +1 -0
  36. package/build/nativeHttpRequest.test.js +88 -0
  37. package/build/nativeHttpRequest.test.js.map +1 -0
  38. package/ios/Crust.podspec +1 -1
  39. package/ios/CrustModule.swift +18 -7
  40. package/ios/PermissionStatusReceipt.swift +105 -0
  41. package/package.json +2 -2
  42. package/plugin/build/index.d.ts +2 -0
  43. package/plugin/build/index.js +2 -0
  44. package/plugin/build/withAndroid.js +39 -0
  45. package/src/Crust.types.ts +26 -12
  46. package/src/CrustModule.ts +9 -1
  47. package/src/CrustModule.web.ts +18 -7
  48. package/src/index.ts +1 -0
  49. package/src/nativeHttpRequest.test.ts +94 -0
  50. package/src/nativeHttpRequest.ts +74 -0
  51. package/android/src/internal/AndroidManifest.xml +0 -9
  52. package/android/src/internal/java/com/mentra/crust/receivers/CaptionsTesterIncidentReceiver.kt +0 -46
@@ -1,17 +1,29 @@
1
1
  import { registerWebModule, NativeModule } from "expo";
2
2
  class CrustModule extends NativeModule {
3
3
  PI = Math.PI;
4
+ httpRequests = new Map();
4
5
  async setValueAsync(value) {
5
6
  this.emit("onChange", { value });
6
7
  }
7
- async nativeHttpRequest(method, url, headers, body) {
8
- const response = await fetch(url, { method, headers, body });
9
- return {
10
- status: response.status,
11
- statusText: response.statusText,
12
- headers: Object.fromEntries(response.headers.entries()),
13
- body: await response.text(),
14
- };
8
+ async nativeHttpRequest(requestId, method, url, headers, body) {
9
+ const controller = new AbortController();
10
+ this.httpRequests.set(requestId, controller);
11
+ try {
12
+ const response = await fetch(url, { method, headers, body, signal: controller.signal });
13
+ return {
14
+ status: response.status,
15
+ statusText: response.statusText,
16
+ headers: Object.fromEntries(response.headers.entries()),
17
+ body: await response.text(),
18
+ };
19
+ }
20
+ finally {
21
+ this.httpRequests.delete(requestId);
22
+ }
23
+ }
24
+ async cancelNativeHttpRequest(requestId) {
25
+ this.httpRequests.get(requestId)?.abort();
26
+ this.httpRequests.delete(requestId);
15
27
  }
16
28
  hello() {
17
29
  return "Hello world! 👋";
@@ -1 +1 @@
1
- {"version":3,"file":"CrustModule.web.js","sourceRoot":"","sources":["../src/CrustModule.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAE,YAAY,EAAC,MAAM,MAAM,CAAA;AAIpD,MAAM,WAAY,SAAQ,YAA+B;IACvD,EAAE,GAAG,IAAI,CAAC,EAAE,CAAA;IACZ,KAAK,CAAC,aAAa,CAAC,KAAa;QAC/B,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,EAAC,KAAK,EAAC,CAAC,CAAA;IAChC,CAAC;IACD,KAAK,CAAC,iBAAiB,CAAC,MAAc,EAAE,GAAW,EAAE,OAA+B,EAAE,IAAoB;QACxG,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,EAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC,CAAA;QAC1D,OAAO;YACL,MAAM,EAAE,QAAQ,CAAC,MAAM;YACvB,UAAU,EAAE,QAAQ,CAAC,UAAU;YAC/B,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACvD,IAAI,EAAE,MAAM,QAAQ,CAAC,IAAI,EAAE;SAC5B,CAAA;IACH,CAAC;IACD,KAAK;QACH,OAAO,iBAAiB,CAAA;IAC1B,CAAC;IACD,iBAAiB,CAAC,UAA0B,IAAG,CAAC;IAChD,KAAK,CAAC,yBAAyB,CAAC,MAAgB,IAAkB,CAAC;IACnE,KAAK,CAAC,qBAAqB,CAAC,gBAAyB,EAAE,UAAoB,IAAkB,CAAC;IAC9F,KAAK,CAAC,gBAAgB;QACpB,OAAO,EAAE,CAAA;IACX,CAAC;IACD,KAAK,CAAC,gCAAgC;QACpC,OAAO,EAAE,CAAA;IACX,CAAC;IACD,KAAK,CAAC,iCAAiC;QACrC,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,CAAC,2BAA2B;QAC/B,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,CAAC,gCAAgC;QACpC,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,CAAC,WAAW;QACf,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,CAAC,aAAa,CAAC,IAAY,EAAE,SAAiB,EAAE,UAAkB;QACrE,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,CAAC,gBAAgB,CAAC,IAAY,EAAE,IAAY;QAC/C,OAAO,IAAI,CAAA;IACb,CAAC;IACD,KAAK,CAAC,YAAY,CAAC,IAAY;QAC7B,OAAM;IACR,CAAC;IACD,KAAK,CAAC,oBAAoB,CAAC,IAAY,EAAE,IAA6B;QACpE,OAAM;IACR,CAAC;IACD,KAAK,CAAC,mBAAmB,CAAC,IAAY,EAAE,MAAgB;QACtD,OAAM;IACR,CAAC;IACD,qBAAqB;QACnB,OAAO,EAAE,CAAA;IACX,CAAC;IACD,KAAK,CAAC,oBAAoB,CAAC,IAAY;QACrC,OAAO,KAAK,CAAA;IACd,CAAC;IACD,0BAA0B;QACxB,OAAO,EAAE,CAAA;IACX,CAAC;CACF;AAED,eAAe,iBAAiB,CAAC,WAAW,EAAE,aAAa,CAAC,CAAA","sourcesContent":["import {registerWebModule, NativeModule} from \"expo\"\n\nimport {CrustModuleEvents} from \"./Crust.types\"\n\nclass CrustModule extends NativeModule<CrustModuleEvents> {\n PI = Math.PI\n async setValueAsync(value: string): Promise<void> {\n this.emit(\"onChange\", {value})\n }\n async nativeHttpRequest(method: string, url: string, headers: Record<string, string>, body?: string | null) {\n const response = await fetch(url, {method, headers, body})\n return {\n status: response.status,\n statusText: response.statusText,\n headers: Object.fromEntries(response.headers.entries()),\n body: await response.text(),\n }\n }\n hello() {\n return \"Hello world! 👋\"\n }\n showAVRoutePicker(_tintColor?: string | null) {}\n async setDeferredSystemGestures(_edges: string[]): Promise<void> {}\n async setNotificationConfig(_listenerEnabled: boolean, _blocklist: string[]): Promise<void> {}\n async getInstalledApps() {\n return []\n }\n async getInstalledAppsForNotifications() {\n return []\n }\n async hasNotificationListenerPermission() {\n return false\n }\n async refreshNotificationListener() {\n return false\n }\n async openNotificationListenerSettings() {\n return false\n }\n async isBetaBuild() {\n return false\n }\n async mentraJsSpawn(_pkg: string, _polyfill: string, _miniappJs: string) {\n return false\n }\n async mentraJsEvaluate(_pkg: string, _src: string) {\n return null\n }\n async mentraJsKill(_pkg: string) {\n return\n }\n async mentraJsDispatchToJs(_pkg: string, _env: Record<string, unknown>) {\n return\n }\n async mentraJsSetManifest(_pkg: string, _perms: string[]) {\n return\n }\n mentraJsAlivePackages() {\n return []\n }\n async mentraJsDebugForceGC(_pkg: string) {\n return false\n }\n mentraJsLoadPolyfillBundle() {\n return \"\"\n }\n}\n\nexport default registerWebModule(CrustModule, \"CrustModule\")\n"]}
1
+ {"version":3,"file":"CrustModule.web.js","sourceRoot":"","sources":["../src/CrustModule.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAE,YAAY,EAAC,MAAM,MAAM,CAAA;AAIpD,MAAM,WAAY,SAAQ,YAA+B;IACvD,EAAE,GAAG,IAAI,CAAC,EAAE,CAAA;IACK,YAAY,GAAG,IAAI,GAAG,EAA2B,CAAA;IAClE,KAAK,CAAC,aAAa,CAAC,KAAa;QAC/B,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,EAAC,KAAK,EAAC,CAAC,CAAA;IAChC,CAAC;IACD,KAAK,CAAC,iBAAiB,CAAC,SAAiB,EAAE,MAAc,EAAE,GAAW,EAAE,OAA+B,EAAE,IAAoB;QAC3H,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;QACxC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,EAAE,UAAU,CAAC,CAAA;Q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;QACf,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,CAAC,aAAa,CAAC,IAAY,EAAE,SAAiB,EAAE,UAAkB;QACrE,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,CAAC,gBAAgB,CAAC,IAAY,EAAE,IAAY;QAC/C,OAAO,IAAI,CAAA;IACb,CAAC;IACD,KAAK,CAAC,YAAY,CAAC,IAAY;QAC7B,OAAM;IACR,CAAC;IACD,KAAK,CAAC,oBAAoB,CAAC,IAAY,EAAE,IAA6B;QACpE,OAAM;IACR,CAAC;IACD,KAAK,CAAC,mBAAmB,CAAC,IAAY,EAAE,MAAgB;QACtD,OAAM;IACR,CAAC;IACD,qBAAqB;QACnB,OAAO,EAAE,CAAA;IACX,CAAC;IACD,KAAK,CAAC,oBAAoB,CAAC,IAAY;QACrC,OAAO,KAAK,CAAA;IACd,CAAC;IACD,0BAA0B;QACxB,OAAO,EAAE,CAAA;IACX,CAAC;CACF;AAED,eAAe,iBAAiB,CAAC,WAAW,EAAE,aAAa,CAAC,CAAA","sourcesContent":["import {registerWebModule, NativeModule} from \"expo\"\n\nimport {CrustModuleEvents} from \"./Crust.types\"\n\nclass CrustModule extends NativeModule<CrustModuleEvents> {\n PI = Math.PI\n private readonly httpRequests = new Map<string, AbortController>()\n async setValueAsync(value: string): Promise<void> {\n this.emit(\"onChange\", {value})\n }\n async nativeHttpRequest(requestId: string, method: string, url: string, headers: Record<string, string>, body?: string | null) {\n const controller = new AbortController()\n this.httpRequests.set(requestId, controller)\n try {\n const response = await fetch(url, {method, headers, body, signal: controller.signal})\n return {\n status: response.status,\n statusText: response.statusText,\n headers: Object.fromEntries(response.headers.entries()),\n body: await response.text(),\n }\n } finally {\n this.httpRequests.delete(requestId)\n }\n }\n async cancelNativeHttpRequest(requestId: string): Promise<void> {\n this.httpRequests.get(requestId)?.abort()\n this.httpRequests.delete(requestId)\n }\n hello() {\n return \"Hello world! 👋\"\n }\n showAVRoutePicker(_tintColor?: string | null) {}\n async setDeferredSystemGestures(_edges: string[]): Promise<void> {}\n async setNotificationConfig(_listenerEnabled: boolean, _blocklist: string[]): Promise<void> {}\n async getInstalledApps() {\n return []\n }\n async getInstalledAppsForNotifications() {\n return []\n }\n async hasNotificationListenerPermission() {\n return false\n }\n async refreshNotificationListener() {\n return false\n }\n async openNotificationListenerSettings() {\n return false\n }\n async isBetaBuild() {\n return false\n }\n async mentraJsSpawn(_pkg: string, _polyfill: string, _miniappJs: string) {\n return false\n }\n async mentraJsEvaluate(_pkg: string, _src: string) {\n return null\n }\n async mentraJsKill(_pkg: string) {\n return\n }\n async mentraJsDispatchToJs(_pkg: string, _env: Record<string, unknown>) {\n return\n }\n async mentraJsSetManifest(_pkg: string, _perms: string[]) {\n return\n }\n mentraJsAlivePackages() {\n return []\n }\n async mentraJsDebugForceGC(_pkg: string) {\n return false\n }\n mentraJsLoadPolyfillBundle() {\n return \"\"\n }\n}\n\nexport default registerWebModule(CrustModule, \"CrustModule\")\n"]}
package/build/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default } from "./CrustModule";
2
2
  export { default as CrustView } from "./CrustView";
3
3
  export * from "./Crust.types";
4
+ export { nativeHttpRequest } from "./nativeHttpRequest";
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAA;AACrC,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,aAAa,CAAA;AAChD,cAAc,eAAe,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAA;AACrC,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,aAAa,CAAA;AAChD,cAAc,eAAe,CAAA;AAC7B,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAA"}
package/build/index.js CHANGED
@@ -3,4 +3,5 @@
3
3
  export { default } from "./CrustModule";
4
4
  export { default as CrustView } from "./CrustView";
5
5
  export * from "./Crust.types";
6
+ export { nativeHttpRequest } from "./nativeHttpRequest";
6
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,4CAA4C;AAC5C,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAA;AACrC,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,aAAa,CAAA;AAChD,cAAc,eAAe,CAAA","sourcesContent":["// Reexport the native module. On web, it will be resolved to CrustModule.web.ts\n// and on native platforms to CrustModule.ts\nexport {default} from \"./CrustModule\"\nexport {default as CrustView} from \"./CrustView\"\nexport * from \"./Crust.types\"\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,4CAA4C;AAC5C,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAA;AACrC,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,aAAa,CAAA;AAChD,cAAc,eAAe,CAAA;AAC7B,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAA","sourcesContent":["// Reexport the native module. On web, it will be resolved to CrustModule.web.ts\n// and on native platforms to CrustModule.ts\nexport {default} from \"./CrustModule\"\nexport {default as CrustView} from \"./CrustView\"\nexport * from \"./Crust.types\"\nexport {nativeHttpRequest} from \"./nativeHttpRequest\"\n"]}
@@ -0,0 +1,13 @@
1
+ export interface NativeHttpResult {
2
+ status: number;
3
+ statusText: string;
4
+ headers: Record<string, string>;
5
+ body: string;
6
+ }
7
+ export interface NativeHttpTransport {
8
+ nativeHttpRequest(requestId: string, method: string, url: string, headers: Record<string, string>, body: string | null): Promise<NativeHttpResult>;
9
+ cancelNativeHttpRequest(requestId: string): Promise<void>;
10
+ }
11
+ /** Keep Android's background-safe HTTP path while cancelling its actual native request. */
12
+ export declare function nativeHttpRequest(native: NativeHttpTransport, method: string, url: string, headers: Record<string, string>, body: string | null, signal?: AbortSignal): Promise<NativeHttpResult>;
13
+ //# sourceMappingURL=nativeHttpRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nativeHttpRequest.d.ts","sourceRoot":"","sources":["../src/nativeHttpRequest.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B,IAAI,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,mBAAmB;IAClC,iBAAiB,CACf,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,IAAI,EAAE,MAAM,GAAG,IAAI,GAClB,OAAO,CAAC,gBAAgB,CAAC,CAAA;IAC5B,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC1D;AAWD,2FAA2F;AAC3F,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,mBAAmB,EAC3B,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,IAAI,EAAE,MAAM,GAAG,IAAI,EACnB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,gBAAgB,CAAC,CAsC3B"}
@@ -0,0 +1,51 @@
1
+ let requestSequence = 0;
2
+ function abortReason(signal) {
3
+ if (signal.reason !== undefined)
4
+ return signal.reason;
5
+ const error = new Error("Native HTTP request aborted");
6
+ error.name = "AbortError";
7
+ return error;
8
+ }
9
+ /** Keep Android's background-safe HTTP path while cancelling its actual native request. */
10
+ export function nativeHttpRequest(native, method, url, headers, body, signal) {
11
+ if (signal?.aborted)
12
+ return Promise.reject(abortReason(signal));
13
+ const requestId = `http-${Date.now().toString(36)}-${++requestSequence}`;
14
+ // Start first, then subscribe: Expo delivers start and cancel on the same serial queue.
15
+ const request = native.nativeHttpRequest(requestId, method, url, headers, body);
16
+ if (!signal)
17
+ return request;
18
+ return new Promise((resolve, reject) => {
19
+ let settled = false;
20
+ let cancelling = false;
21
+ const finish = (action) => {
22
+ if (settled)
23
+ return;
24
+ settled = true;
25
+ signal.removeEventListener("abort", onAbort);
26
+ action();
27
+ };
28
+ const onAbort = () => {
29
+ if (settled || cancelling)
30
+ return;
31
+ cancelling = true;
32
+ void native.cancelNativeHttpRequest(requestId).then(() => finish(() => reject(abortReason(signal))), (error) => finish(() => reject(error)));
33
+ };
34
+ request.then((result) => {
35
+ if (signal.aborted)
36
+ onAbort();
37
+ else
38
+ finish(() => resolve(result));
39
+ }, (error) => {
40
+ if (signal.aborted)
41
+ onAbort();
42
+ else
43
+ finish(() => reject(error));
44
+ });
45
+ signal.addEventListener("abort", onAbort, { once: true });
46
+ // Handles an abort fired synchronously during native registration.
47
+ if (signal.aborted)
48
+ onAbort();
49
+ });
50
+ }
51
+ //# sourceMappingURL=nativeHttpRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nativeHttpRequest.js","sourceRoot":"","sources":["../src/nativeHttpRequest.ts"],"names":[],"mappings":"AAkBA,IAAI,eAAe,GAAG,CAAC,CAAA;AAEvB,SAAS,WAAW,CAAC,MAAmB;IACtC,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC,MAAM,CAAA;IACrD,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;IACtD,KAAK,CAAC,IAAI,GAAG,YAAY,CAAA;IACzB,OAAO,KAAK,CAAA;AACd,CA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AAK,CAAC,CAAC,CAChD,CAAA;QACH,CAAC,CAAA;QACD,OAAO,CAAC,IAAI,CACV,CAAC,MAAM,EAAE,EAAE;YACT,IAAI,MAAM,CAAC,OAAO;gBAAE,OAAO,EAAE,CAAA;;gBACxB,MAAM,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAA;QACpC,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;YACjB,IAAI,MAAM,CAAC,OAAO;gBAAE,OAAO,EAAE,CAAA;;gBACxB,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;QAClC,CAAC,CACF,CAAA;QACD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAC,IAAI,EAAE,IAAI,EAAC,CAAC,CAAA;QACvD,mEAAmE;QACnE,IAAI,MAAM,CAAC,OAAO;YAAE,OAAO,EAAE,CAAA;IAC/B,CAAC,CAAC,CAAA;AACJ,CAAC","sourcesContent":["export interface NativeHttpResult {\n status: number\n statusText: string\n headers: Record<string, string>\n body: string\n}\n\nexport interface NativeHttpTransport {\n nativeHttpRequest(\n requestId: string,\n method: string,\n url: string,\n headers: Record<string, string>,\n body: string | null,\n ): Promise<NativeHttpResult>\n cancelNativeHttpRequest(requestId: string): Promise<void>\n}\n\nlet requestSequence = 0\n\nfunction abortReason(signal: AbortSignal): unknown {\n if (signal.reason !== undefined) return signal.reason\n const error = new Error(\"Native HTTP request aborted\")\n error.name = \"AbortError\"\n return error\n}\n\n/** Keep Android's background-safe HTTP path while cancelling its actual native request. */\nexport function nativeHttpRequest(\n native: NativeHttpTransport,\n method: string,\n url: string,\n headers: Record<string, string>,\n body: string | null,\n signal?: AbortSignal,\n): Promise<NativeHttpResult> {\n if (signal?.aborted) return Promise.reject(abortReason(signal))\n const requestId = `http-${Date.now().toString(36)}-${++requestSequence}`\n // Start first, then subscribe: Expo delivers start and cancel on the same serial queue.\n const request = native.nativeHttpRequest(requestId, method, url, headers, body)\n if (!signal) return request\n\n return new Promise((resolve, reject) => {\n let settled = false\n let cancelling = false\n const finish = (action: () => void) => {\n if (settled) return\n settled = true\n signal.removeEventListener(\"abort\", onAbort)\n action()\n }\n const onAbort = () => {\n if (settled || cancelling) return\n cancelling = true\n void native.cancelNativeHttpRequest(requestId).then(\n () => finish(() => reject(abortReason(signal))),\n (error: unknown) => finish(() => reject(error)),\n )\n }\n request.then(\n (result) => {\n if (signal.aborted) onAbort()\n else finish(() => resolve(result))\n },\n (error: unknown) => {\n if (signal.aborted) onAbort()\n else finish(() => reject(error))\n },\n )\n signal.addEventListener(\"abort\", onAbort, {once: true})\n // Handles an abort fired synchronously during native registration.\n if (signal.aborted) onAbort()\n })\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=nativeHttpRequest.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nativeHttpRequest.test.d.ts","sourceRoot":"","sources":["../src/nativeHttpRequest.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,88 @@
1
+ import { nativeHttpRequest } from "./nativeHttpRequest";
2
+ const response = { status: 200, statusText: "OK", headers: {}, body: "{}" };
3
+ function deferred() {
4
+ let resolve;
5
+ let reject;
6
+ const promise = new Promise((yes, no) => {
7
+ resolve = yes;
8
+ reject = no;
9
+ });
10
+ return { promise, resolve, reject };
11
+ }
12
+ function transport(request = deferred()) {
13
+ const native = {
14
+ nativeHttpRequest: jest.fn(() => request.promise),
15
+ cancelNativeHttpRequest: jest.fn(async () => { }),
16
+ };
17
+ return { native, request };
18
+ }
19
+ describe("nativeHttpRequest", () => {
20
+ test("pre-aborted requests do not start or cancel native work", async () => {
21
+ const { native } = transport();
22
+ const controller = new AbortController();
23
+ const reason = new Error("deadline");
24
+ controller.abort(reason);
25
+ await expect(nativeHttpRequest(native, "GET", "https://example.com", {}, null, controller.signal)).rejects.toBe(reason);
26
+ expect(native.nativeHttpRequest).not.toHaveBeenCalled();
27
+ expect(native.cancelNativeHttpRequest).not.toHaveBeenCalled();
28
+ });
29
+ test("abort cancels only this request and waits for native cancellation acceptance", async () => {
30
+ const { native, request } = transport();
31
+ const cancellation = deferred();
32
+ native.cancelNativeHttpRequest = jest.fn(() => cancellation.promise);
33
+ const controller = new AbortController();
34
+ const remove = jest.spyOn(controller.signal, "removeEventListener");
35
+ const promise = nativeHttpRequest(native, "POST", "https://example.com", { test: "header" }, "body", controller.signal);
36
+ const rejected = expect(promise).rejects.toMatchObject({ name: "AbortError" });
37
+ controller.abort();
38
+ const id = native.nativeHttpRequest.mock.calls[0][0];
39
+ expect(native.cancelNativeHttpRequest).toHaveBeenCalledWith(id);
40
+ expect(remove).not.toHaveBeenCalled();
41
+ request.reject(new Error("cancelled socket"));
42
+ cancellation.resolve();
43
+ await rejected;
44
+ expect(native.cancelNativeHttpRequest).toHaveBeenCalledTimes(1);
45
+ expect(remove).toHaveBeenCalledWith("abort", expect.any(Function));
46
+ });
47
+ test("an abort during native start is cancelled after start and removes its listener", async () => {
48
+ const { native } = transport();
49
+ const controller = new AbortController();
50
+ const order = [];
51
+ native.nativeHttpRequest = jest.fn(() => {
52
+ order.push("start");
53
+ controller.abort();
54
+ return new Promise(() => { });
55
+ });
56
+ native.cancelNativeHttpRequest = jest.fn(async () => { order.push("cancel"); });
57
+ await expect(nativeHttpRequest(native, "GET", "https://example.com", {}, null, controller.signal)).rejects.toMatchObject({ name: "AbortError" });
58
+ expect(order).toEqual(["start", "cancel"]);
59
+ });
60
+ test.each(["success", "failure"])("%s removes its abort listener and a later abort does not cancel", async (outcome) => {
61
+ const { native, request } = transport();
62
+ const controller = new AbortController();
63
+ const remove = jest.spyOn(controller.signal, "removeEventListener");
64
+ const promise = nativeHttpRequest(native, "GET", "https://example.com", {}, null, controller.signal);
65
+ if (outcome === "success") {
66
+ request.resolve(response);
67
+ await expect(promise).resolves.toBe(response);
68
+ }
69
+ else {
70
+ request.reject(new Error("offline"));
71
+ await expect(promise).rejects.toThrow("offline");
72
+ }
73
+ controller.abort();
74
+ expect(remove).toHaveBeenCalledTimes(1);
75
+ expect(native.cancelNativeHttpRequest).not.toHaveBeenCalled();
76
+ });
77
+ test("independent calls have separate IDs and unsignalled calls keep the native path", async () => {
78
+ const { native, request } = transport();
79
+ const first = nativeHttpRequest(native, "GET", "https://example.com/first", {}, null);
80
+ const second = nativeHttpRequest(native, "GET", "https://example.com/second", {}, null);
81
+ const calls = native.nativeHttpRequest.mock.calls;
82
+ expect(calls[0][0]).not.toBe(calls[1][0]);
83
+ request.resolve(response);
84
+ await Promise.all([first, second]);
85
+ expect(native.cancelNativeHttpRequest).not.toHaveBeenCalled();
86
+ });
87
+ });
88
+ //# sourceMappingURL=nativeHttpRequest.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nativeHttpRequest.test.js","sourceRoot":"","sources":["../src/nativeHttpRequest.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAkD,MAAM,qBAAqB,CAAA;AAEtG,MAAM,QAAQ,GAAqB,EAAC,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAC,CAAA;AAE3F,SAAS,QAAQ;IACf,IAAI,OAA4B,CAAA;IAChC,IAAI,MAAiC,CAAA;IACrC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE;QACzC,OAAO,GAAG,GAAG,CAAA;QACb,MAAM,GAAG,EAAE,CAAA;IACb,CAAC,CAAC,CAAA;IACF,OAAO,EAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAC,CAAA;AACnC,CAAC;AAED,SAAS,SAAS,CAAC,UAAU,QAAQ,EAAoB;IACvD,MAAM,MAAM,GAAwB;QAClC,iBAAiB,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC;QACjD,uBAAuB,EAA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eHttpRequest, type NativeHttpResult, type NativeHttpTransport} from \"./nativeHttpRequest\"\n\nconst response: NativeHttpResult = {status: 200, statusText: \"OK\", headers: {}, body: \"{}\"}\n\nfunction deferred<T>() {\n let resolve!: (value: T) => void\n let reject!: (error: unknown) => void\n const promise = new Promise<T>((yes, no) => {\n resolve = yes\n reject = no\n })\n return {promise, resolve, reject}\n}\n\nfunction transport(request = deferred<NativeHttpResult>()) {\n const native: NativeHttpTransport = {\n nativeHttpRequest: jest.fn(() => request.promise),\n cancelNativeHttpRequest: jest.fn(async () => {}),\n }\n return {native, request}\n}\n\ndescribe(\"nativeHttpRequest\", () => {\n test(\"pre-aborted requests do not start or cancel native work\", async () => {\n const {native} = transport()\n const controller = new AbortController()\n const reason = new Error(\"deadline\")\n controller.abort(reason)\n await expect(nativeHttpRequest(native, \"GET\", \"https://example.com\", {}, null, controller.signal)).rejects.toBe(reason)\n expect(native.nativeHttpRequest).not.toHaveBeenCalled()\n expect(native.cancelNativeHttpRequest).not.toHaveBeenCalled()\n })\n\n test(\"abort cancels only this request and waits for native cancellation acceptance\", async () => {\n const {native, request} = transport()\n const cancellation = deferred<void>()\n native.cancelNativeHttpRequest = jest.fn(() => cancellation.promise)\n const controller = new AbortController()\n const remove = jest.spyOn(controller.signal, \"removeEventListener\")\n const promise = nativeHttpRequest(native, \"POST\", \"https://example.com\", {test: \"header\"}, \"body\", controller.signal)\n const rejected = expect(promise).rejects.toMatchObject({name: \"AbortError\"})\n controller.abort()\n const id = (native.nativeHttpRequest as jest.Mock).mock.calls[0][0]\n expect(native.cancelNativeHttpRequest).toHaveBeenCalledWith(id)\n expect(remove).not.toHaveBeenCalled()\n request.reject(new Error(\"cancelled socket\"))\n cancellation.resolve()\n await rejected\n expect(native.cancelNativeHttpRequest).toHaveBeenCalledTimes(1)\n expect(remove).toHaveBeenCalledWith(\"abort\", expect.any(Function))\n })\n\n test(\"an abort during native start is cancelled after start and removes its listener\", async () => {\n const {native} = transport()\n const controller = new AbortController()\n const order: string[] = []\n native.nativeHttpRequest = jest.fn(() => {\n order.push(\"start\")\n controller.abort()\n return new Promise<NativeHttpResult>(() => {})\n })\n native.cancelNativeHttpRequest = jest.fn(async () => {order.push(\"cancel\")})\n await expect(nativeHttpRequest(native, \"GET\", \"https://example.com\", {}, null, controller.signal)).rejects.toMatchObject({name: \"AbortError\"})\n expect(order).toEqual([\"start\", \"cancel\"])\n })\n\n test.each([\"success\", \"failure\"])(\"%s removes its abort listener and a later abort does not cancel\", async (outcome) => {\n const {native, request} = transport()\n const controller = new AbortController()\n const remove = jest.spyOn(controller.signal, \"removeEventListener\")\n const promise = nativeHttpRequest(native, \"GET\", \"https://example.com\", {}, null, controller.signal)\n if (outcome === \"success\") {\n request.resolve(response)\n await expect(promise).resolves.toBe(response)\n } else {\n request.reject(new Error(\"offline\"))\n await expect(promise).rejects.toThrow(\"offline\")\n }\n controller.abort()\n expect(remove).toHaveBeenCalledTimes(1)\n expect(native.cancelNativeHttpRequest).not.toHaveBeenCalled()\n })\n\n test(\"independent calls have separate IDs and unsignalled calls keep the native path\", async () => {\n const {native, request} = transport()\n const first = nativeHttpRequest(native, \"GET\", \"https://example.com/first\", {}, null)\n const second = nativeHttpRequest(native, \"GET\", \"https://example.com/second\", {}, null)\n const calls = (native.nativeHttpRequest as jest.Mock).mock.calls\n expect(calls[0][0]).not.toBe(calls[1][0])\n request.resolve(response)\n await Promise.all([first, second])\n expect(native.cancelNativeHttpRequest).not.toHaveBeenCalled()\n })\n})\n"]}
package/ios/Crust.podspec CHANGED
@@ -29,7 +29,7 @@ Pod::Spec.new do |s|
29
29
  # is intentionally NO `s.dependency 'MapboxNavigationCore'` here.
30
30
 
31
31
  # iOS frameworks required for media processing + navigation + heading
32
- s.frameworks = 'AVFoundation', 'Photos', 'CoreImage', 'CoreGraphics', 'UIKit', 'CoreLocation', 'CoreMotion'
32
+ s.frameworks = 'AVFoundation', 'Photos', 'CoreImage', 'CoreGraphics', 'UIKit', 'CoreLocation', 'CoreMotion', 'CoreBluetooth'
33
33
 
34
34
  # Swift/Objective-C compatibility + Mapbox SPM module visibility.
35
35
  #
@@ -61,6 +61,13 @@ private struct PhotoLibrarySaveResult {
61
61
  let timedOut: Bool
62
62
  }
63
63
 
64
+ /// MentraJS calls get their own serial queue. Expo runs every module's default-queue
65
+ /// AsyncFunction on one shared serial queue, so a blocking call in another module held every
66
+ /// host→miniapp message behind it: the ~25s offline STT model extraction after login kept
67
+ /// miniapps from spawning and starved running ones of PINGs until the host respawned them.
68
+ /// Serial, so spawn, dispatch and kill still run in the order JS issued them.
69
+ private let mentraJsQueue = DispatchQueue(label: "com.mentra.mentrajs-bridge", qos: .userInitiated)
70
+
64
71
  public class CrustModule: Module {
65
72
  public func definition() -> ModuleDefinition {
66
73
  Name("Crust")
@@ -69,6 +76,11 @@ public class CrustModule: Module {
69
76
  ProcessInfo.processInfo.isiOSAppOnMac
70
77
  }
71
78
 
79
+ AsyncFunction("getPermissionStatus") { (requestId: String?) -> [String: Any] in
80
+ if let requestId { return try await PermissionStatusReceipt.publish(requestId: requestId) }
81
+ return await PermissionStatusReceipt.current()
82
+ }
83
+
72
84
  Constant("PI") {
73
85
  Double.pi
74
86
  }
@@ -77,7 +89,6 @@ public class CrustModule: Module {
77
89
  "onChange",
78
90
  "phone_notification",
79
91
  "phone_notification_dismissed",
80
- "captions_tester_incident",
81
92
  "onNavManeuver",
82
93
  "onNavRerouting",
83
94
  "onNavArrived",
@@ -283,7 +294,7 @@ public class CrustModule: Module {
283
294
  polyfillBundle: polyfillBundle,
284
295
  miniappJs: miniappJs
285
296
  )
286
- }
297
+ }.runOnQueue(mentraJsQueue)
287
298
 
288
299
  /// Evaluate an arbitrary script inside the named context. Returns
289
300
  /// the JS return value bridged to a JSON-friendly Swift type, or
@@ -291,19 +302,19 @@ public class CrustModule: Module {
291
302
  /// + tests; production code paths use mentraJsDispatchToJs.
292
303
  AsyncFunction("mentraJsEvaluate") { (packageName: String, source: String) -> Any? in
293
304
  return JSCRuntime.shared.evaluate(packageName: packageName, source: source)
294
- }
305
+ }.runOnQueue(mentraJsQueue)
295
306
 
296
307
  /// Tear down a JS context. Cancels timers, drops refs, forces GC.
297
308
  AsyncFunction("mentraJsKill") { (packageName: String) -> Void in
298
309
  JSCRuntime.shared.kill(packageName: packageName)
299
- }
310
+ }.runOnQueue(mentraJsQueue)
300
311
 
301
312
  /// Push an event / response envelope into the named context's
302
313
  /// globalThis.__deliver. Used by MentraJSRouter for
303
314
  /// glasses-status broadcasts and request/response correlation.
304
315
  AsyncFunction("mentraJsDispatchToJs") { (packageName: String, envelope: [String: Any]) -> Void in
305
316
  JSCRuntime.shared.dispatchToJs(packageName: packageName, envelope: envelope)
306
- }
317
+ }.runOnQueue(mentraJsQueue)
307
318
 
308
319
  /// Set the installed manifest for a miniapp so the dispatcher's
309
320
  /// permission gate can authorize sensitive `__dispatch` calls.
@@ -312,7 +323,7 @@ public class CrustModule: Module {
312
323
  packageName: packageName,
313
324
  manifest: InstalledMiniappManifest(permissions: Set(permissions))
314
325
  )
315
- }
326
+ }.runOnQueue(mentraJsQueue)
316
327
 
317
328
  /// Diagnostic: list all live packageNames.
318
329
  Function("mentraJsAlivePackages") { () -> [String] in
@@ -324,7 +335,7 @@ public class CrustModule: Module {
324
335
  /// the context is dead.
325
336
  AsyncFunction("mentraJsDebugForceGC") { (packageName: String) -> Bool in
326
337
  return JSCRuntime.shared.debugForceGC(packageName: packageName)
327
- }
338
+ }.runOnQueue(mentraJsQueue)
328
339
 
329
340
  /// Read the bundled MentraJS polyfill (startup.js) from the iOS
330
341
  /// pod's resource bundle. The host calls this once on app boot,
@@ -0,0 +1,105 @@
1
+ import AVFoundation
2
+ import CoreBluetooth
3
+ import CoreLocation
4
+ import Foundation
5
+
6
+ /// Passive app-owned observations. None of these getters requests consent,
7
+ /// starts capture, creates a Bluetooth central, or joins a network.
8
+ enum PermissionStatusReceipt {
9
+ static let relativePath = "MentraDiagnostics/permission-status.json"
10
+
11
+ static func state(_ rawStatus: String, _ status: String) -> [String: String] {
12
+ ["rawStatus": rawStatus, "status": status]
13
+ }
14
+
15
+ static func location(_ value: CLAuthorizationStatus) -> [String: String] {
16
+ switch value {
17
+ case .notDetermined: return state("notDetermined", "denied")
18
+ case .denied: return state("denied", "blocked")
19
+ case .restricted: return state("restricted", "unavailable")
20
+ case .authorizedAlways: return state("authorizedAlways", "granted")
21
+ case .authorizedWhenInUse: return state("authorizedWhenInUse", "granted")
22
+ @unknown default: return state("unknown", "unknown")
23
+ }
24
+ }
25
+
26
+ static func capture(_ value: AVAuthorizationStatus) -> [String: String] {
27
+ switch value {
28
+ case .notDetermined: return state("notDetermined", "denied")
29
+ case .denied: return state("denied", "blocked")
30
+ case .restricted: return state("restricted", "unavailable")
31
+ case .authorized: return state("authorized", "granted")
32
+ @unknown default: return state("unknown", "unknown")
33
+ }
34
+ }
35
+
36
+ static func bluetooth(_ value: CBManagerAuthorization) -> [String: String] {
37
+ switch value {
38
+ case .notDetermined: return state("notDetermined", "denied")
39
+ case .denied: return state("denied", "blocked")
40
+ case .restricted: return state("restricted", "unavailable")
41
+ case .allowedAlways: return state("allowedAlways", "granted")
42
+ @unknown default: return state("unknown", "unknown")
43
+ }
44
+ }
45
+
46
+ static func current() async -> [String: Any] {
47
+ // The global service getter may block; only the manager's authorization
48
+ // observation belongs to the main actor, which owns Core Location objects.
49
+ let servicesEnabled = CLLocationManager.locationServicesEnabled()
50
+ return await MainActor.run {
51
+ let bundle = Bundle.main
52
+ let formatter = ISO8601DateFormatter()
53
+ formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
54
+ return [
55
+ "schemaVersion": 1,
56
+ "observedAt": formatter.string(from: Date()),
57
+ "identity": [
58
+ "pid": ProcessInfo.processInfo.processIdentifier,
59
+ "uid": getuid(),
60
+ "bundleId": bundle.bundleIdentifier ?? "",
61
+ "bundlePath": bundle.bundleURL.path,
62
+ "executablePath": bundle.executableURL?.path ?? "",
63
+ "version": bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "",
64
+ "build": bundle.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "",
65
+ ] as [String: Any],
66
+ "locationServicesEnabled": servicesEnabled,
67
+ "permissions": [
68
+ "location": location(CLLocationManager().authorizationStatus),
69
+ "camera": capture(AVCaptureDevice.authorizationStatus(for: .video)),
70
+ "microphone": capture(AVCaptureDevice.authorizationStatus(for: .audio)),
71
+ "bluetooth": bluetooth(CBManager.authorization),
72
+ // iOS has no public passive Local Network authorization getter.
73
+ "localNetwork": ["rawStatus": NSNull(), "status": "unknown"],
74
+ ] as [String: Any],
75
+ ]
76
+ }
77
+ }
78
+
79
+ static func correlate(_ observation: [String: Any], requestId: String) throws -> [String: Any] {
80
+ guard requestId.range(of: "^[A-Za-z0-9][A-Za-z0-9._:-]{0,159}\\z", options: .regularExpression) != nil else {
81
+ throw NSError(domain: "MentraPermissionStatus", code: 1,
82
+ userInfo: [NSLocalizedDescriptionKey: "One bounded permission request ID is required"])
83
+ }
84
+ return observation.merging(["requestId": requestId]) { _, fresh in fresh }
85
+ }
86
+
87
+ static func write(_ receipt: [String: Any], to path: URL) throws {
88
+ let files = FileManager.default
89
+ try files.createDirectory(at: path.deletingLastPathComponent(), withIntermediateDirectories: true,
90
+ attributes: [.posixPermissions: 0o700])
91
+ try JSONSerialization.data(withJSONObject: receipt, options: [.sortedKeys]).write(to: path, options: .atomic)
92
+ try files.setAttributes([.posixPermissions: 0o600], ofItemAtPath: path.path)
93
+ }
94
+
95
+ static func publish(requestId: String) async throws -> [String: Any] {
96
+ // Validate before observing; the request never controls a filesystem path.
97
+ _ = try correlate([:], requestId: requestId)
98
+ let observation = await current()
99
+ let receipt = try correlate(observation, requestId: requestId)
100
+ let cache = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask,
101
+ appropriateFor: nil, create: true)
102
+ try write(receipt, to: cache.appendingPathComponent(relativePath))
103
+ return receipt
104
+ }
105
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/crust",
3
- "version": "3.2.1",
3
+ "version": "3.3.0-beta.942",
4
4
  "description": "Mentra Native Module",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  "license": "Apache-2.0",
36
36
  "homepage": "https://github.com/Mentra-Community/MentraOS/tree/dev/mobile/modules/crust#readme",
37
37
  "dependencies": {
38
- "@mentra/jspolyfill": "3.2.1"
38
+ "@mentra/jspolyfill": "3.3.0-beta.942"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@expo/config-plugins": ">=8.0.0",
@@ -14,6 +14,8 @@ import { type ConfigPlugin } from "expo/config-plugins";
14
14
  * both fails the release build with duplicate classes)
15
15
  * - core-library desugaring (crust's AAR metadata requires it of embedding
16
16
  * apps — the Nav SDK uses newer core libs)
17
+ * - Kotlin 2.3.0 or newer, with the root Kotlin Gradle plugin pinned to it
18
+ * (crust's QuickJS library, quickjs-kt 1.0.15, is compiled with Kotlin 2.4)
17
19
  * - a generated MainApplication process guard so the notification-listener
18
20
  * process never initializes React Native
19
21
  * - iOS Mapbox Swift Package products and the Crust build-order dependency
@@ -21,6 +21,8 @@ const withMapboxNavIos_1 = __importDefault(require("./withMapboxNavIos"));
21
21
  * both fails the release build with duplicate classes)
22
22
  * - core-library desugaring (crust's AAR metadata requires it of embedding
23
23
  * apps — the Nav SDK uses newer core libs)
24
+ * - Kotlin 2.3.0 or newer, with the root Kotlin Gradle plugin pinned to it
25
+ * (crust's QuickJS library, quickjs-kt 1.0.15, is compiled with Kotlin 2.4)
24
26
  * - a generated MainApplication process guard so the notification-listener
25
27
  * process never initializes React Native
26
28
  * - iOS Mapbox Swift Package products and the Crust build-order dependency
@@ -20,12 +20,50 @@ const MAPBOX_REPO = [
20
20
  " }",
21
21
  " }",
22
22
  ].join("\n");
23
+ // quickjs-kt 1.0.15 is compiled with Kotlin 2.4, and a Kotlin compiler reads
24
+ // metadata one version ahead. 2.3.0 is the newest 2.3 release Expo SDK 55's
25
+ // root plugin maps to a KSP version.
26
+ const MIN_KOTLIN_VERSION = "2.3.0";
27
+ // The template's unversioned classpath entry resolves to the Kotlin Gradle
28
+ // plugin React Native's Gradle plugin depends on (2.1.20 on RN 0.83), whatever
29
+ // `android.kotlinVersion` says; Expo fixes that only in SDK 58
30
+ // (expo/expo#49668). SDK 56 also never copies the property into Expo's catalog,
31
+ // so pin both the compiler and the root `kotlinVersion` to the property itself.
32
+ const UNVERSIONED_KOTLIN_PLUGIN = "classpath('org.jetbrains.kotlin:kotlin-gradle-plugin')";
33
+ const PINNED_KOTLIN_PLUGIN = `classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:\${findProperty('android.kotlinVersion')}")`;
34
+ const KOTLIN_VERSION_MARKER = "kotlinVersion from android.kotlinVersion (injected by @mentra/crust)";
35
+ const EXPO_ROOT_PROJECT = 'apply plugin: "expo-root-project"';
36
+ function isOlderVersion(version, minimum) {
37
+ const actual = version.split(".").map(Number);
38
+ const required = minimum.split(".").map(Number);
39
+ for (let i = 0; i < required.length; i++) {
40
+ if ((actual[i] ?? 0) !== required[i])
41
+ return (actual[i] ?? 0) < required[i];
42
+ }
43
+ return false;
44
+ }
45
+ function withCrustKotlinVersion(config) {
46
+ return (0, config_plugins_1.withGradleProperties)(config, (cfg) => {
47
+ const property = cfg.modResults.find((item) => item.type === "property" && item.key === "android.kotlinVersion");
48
+ if (property?.type !== "property") {
49
+ cfg.modResults.push({ type: "property", key: "android.kotlinVersion", value: MIN_KOTLIN_VERSION });
50
+ }
51
+ else if (isOlderVersion(property.value, MIN_KOTLIN_VERSION)) {
52
+ property.value = MIN_KOTLIN_VERSION;
53
+ }
54
+ return cfg;
55
+ });
56
+ }
23
57
  const PROTOBUF_EXCLUDE = "exclude group: 'com.google.protobuf', module: 'protobuf-javalite'";
24
58
  const NOTIFICATION_PROCESS_GUARD = "crust: skip React Native in notification process";
25
59
  const NOTIFICATION_CONFIG_GUARD = "crust: skip Expo lifecycle in notification process";
26
60
  function withCrustProjectGradle(config) {
27
61
  return (0, config_plugins_1.withProjectBuildGradle)(config, (cfg) => {
28
62
  let gradle = cfg.modResults.contents;
63
+ gradle = gradle.replace(UNVERSIONED_KOTLIN_PLUGIN, PINNED_KOTLIN_PLUGIN);
64
+ if (!gradle.includes(KOTLIN_VERSION_MARKER)) {
65
+ gradle = gradle.replace(EXPO_ROOT_PROJECT, `// ${KOTLIN_VERSION_MARKER}\next.kotlinVersion = findProperty('android.kotlinVersion')\n${EXPO_ROOT_PROJECT}`);
66
+ }
29
67
  // Mapbox Downloads repo, inserted at the top of allprojects.repositories.
30
68
  if (!gradle.includes(MAPBOX_REPO_MARKER)) {
31
69
  const reposMatch = gradle.match(/allprojects\s*\{[\s\S]*?repositories\s*\{/);
@@ -129,6 +167,7 @@ function withNotificationProcessGuard(config) {
129
167
  });
130
168
  }
131
169
  const withCrustAndroidBuildContract = (config) => {
170
+ config = withCrustKotlinVersion(config);
132
171
  config = withCrustProjectGradle(config);
133
172
  config = withCrustAppGradle(config);
134
173
  return withNotificationProcessGuard(config);