@the-polyfills/get-random-values 1.0.1 → 2.0.0

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.
@@ -2,19 +2,12 @@ package com.thepolyfills.getrandomvalues
2
2
 
3
3
  import android.util.Base64
4
4
  import com.facebook.react.bridge.ReactApplicationContext
5
- import com.facebook.react.bridge.ReactContextBaseJavaModule
6
- import com.facebook.react.bridge.ReactMethod
7
5
  import java.security.SecureRandom
8
6
 
9
7
  class GetRandomValuesModule(reactContext: ReactApplicationContext) :
10
- ReactContextBaseJavaModule(reactContext) {
8
+ NativeGetRandomValuesSpec(reactContext) {
11
9
 
12
- override fun getName(): String = NAME
13
-
14
- // Blocking synchronous method so `getRandomBase64` returns the value
15
- // directly, matching the synchronous WebCrypto API.
16
- @ReactMethod(isBlockingSynchronousMethod = true)
17
- fun getRandomBase64(byteLength: Double): String {
10
+ override fun getRandomBase64(byteLength: Double): String {
18
11
  val length = byteLength.toInt()
19
12
 
20
13
  if (length < 0) {
@@ -32,6 +25,6 @@ class GetRandomValuesModule(reactContext: ReactApplicationContext) :
32
25
  }
33
26
 
34
27
  companion object {
35
- const val NAME = "GetRandomValues"
28
+ const val NAME = NativeGetRandomValuesSpec.NAME
36
29
  }
37
30
  }
@@ -1,16 +1,31 @@
1
1
  package com.thepolyfills.getrandomvalues
2
2
 
3
- import com.facebook.react.ReactPackage
3
+ import com.facebook.react.BaseReactPackage
4
4
  import com.facebook.react.bridge.NativeModule
5
5
  import com.facebook.react.bridge.ReactApplicationContext
6
- import com.facebook.react.uimanager.ViewManager
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+ import java.util.HashMap
7
9
 
8
- class GetRandomValuesPackage : ReactPackage {
9
- override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
10
- return listOf(GetRandomValuesModule(reactContext))
10
+ class GetRandomValuesPackage : BaseReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == GetRandomValuesModule.NAME) {
13
+ GetRandomValuesModule(reactContext)
14
+ } else {
15
+ null
16
+ }
11
17
  }
12
18
 
13
- override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
14
- return emptyList()
19
+ override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
20
+ mapOf(
21
+ GetRandomValuesModule.NAME to ReactModuleInfo(
22
+ name = GetRandomValuesModule.NAME,
23
+ className = GetRandomValuesModule.NAME,
24
+ canOverrideExistingModule = false,
25
+ needsEagerInit = false,
26
+ isCxxModule = false,
27
+ isTurboModule = true
28
+ )
29
+ )
15
30
  }
16
31
  }
@@ -1,5 +1,5 @@
1
- #import <React/RCTBridgeModule.h>
1
+ #import <GetRandomValuesSpec/GetRandomValuesSpec.h>
2
2
 
3
- @interface GetRandomValues : NSObject <RCTBridgeModule>
3
+ @interface GetRandomValues : NSObject <NativeGetRandomValuesSpec>
4
4
 
5
5
  @end
@@ -3,13 +3,7 @@
3
3
 
4
4
  @implementation GetRandomValues
5
5
 
6
- RCT_EXPORT_MODULE(GetRandomValues)
7
-
8
- + (BOOL)requiresMainQueueSetup {
9
- return NO;
10
- }
11
-
12
- RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getRandomBase64 : (double)byteLength) {
6
+ - (NSString *)getRandomBase64:(double)byteLength {
13
7
  NSInteger length = (NSInteger)byteLength;
14
8
 
15
9
  if (length < 0) {
@@ -32,4 +26,14 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getRandomBase64 : (double)byteLength) {
32
26
  return [data base64EncodedStringWithOptions:0];
33
27
  }
34
28
 
29
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
30
+ (const facebook::react::ObjCTurboModule::InitParams &)params {
31
+ return std::make_shared<facebook::react::NativeGetRandomValuesSpecJSI>(
32
+ params);
33
+ }
34
+
35
+ + (NSString *)moduleName {
36
+ return @"GetRandomValues";
37
+ }
38
+
35
39
  @end
@@ -1,17 +1,11 @@
1
1
  "use strict";
2
2
 
3
- import { NativeModules } from 'react-native';
4
-
5
- // Old Architecture (Paper) only: the native module is registered via
6
- // RCT_EXPORT_MODULE / ReactPackage and looked up through NativeModules.
7
- // There is intentionally no TurboModuleRegistry usage and no codegen spec
8
- // in this package.
9
-
3
+ import { TurboModuleRegistry } from 'react-native';
4
+ let cachedSpec = null;
10
5
  export function getNativeGetRandomValues() {
11
- const native = NativeModules.GetRandomValues;
12
- if (native == null || typeof native.getRandomBase64 !== 'function') {
13
- throw new Error("Native module 'GetRandomValues' not found. Rebuild the app after installing the package (e.g. `expo prebuild --clean`).");
6
+ if (cachedSpec == null) {
7
+ cachedSpec = TurboModuleRegistry.getEnforcing('GetRandomValues');
14
8
  }
15
- return native;
9
+ return cachedSpec;
16
10
  }
17
11
  //# sourceMappingURL=NativeGetRandomValues.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["NativeModules","getNativeGetRandomValues","native","GetRandomValues","getRandomBase64","Error"],"sourceRoot":"../../src","sources":["NativeGetRandomValues.ts"],"mappings":";;AAAA,SAASA,aAAa,QAAQ,cAAc;;AAE5C;AACA;AACA;AACA;;AAKA,OAAO,SAASC,wBAAwBA,CAAA,EAAS;EAC/C,MAAMC,MAAM,GAAGF,aAAa,CAACG,eAAmC;EAEhE,IAAID,MAAM,IAAI,IAAI,IAAI,OAAOA,MAAM,CAACE,eAAe,KAAK,UAAU,EAAE;IAClE,MAAM,IAAIC,KAAK,CACb,yHACF,CAAC;EACH;EAEA,OAAOH,MAAM;AACf","ignoreList":[]}
1
+ {"version":3,"names":["TurboModuleRegistry","cachedSpec","getNativeGetRandomValues","getEnforcing"],"sourceRoot":"../../src","sources":["NativeGetRandomValues.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;AAEpE,IAAIC,UAAuB,GAAG,IAAI;AAMlC,OAAO,SAASC,wBAAwBA,CAAA,EAAS;EAC/C,IAAID,UAAU,IAAI,IAAI,EAAE;IACtBA,UAAU,GAAGD,mBAAmB,CAACG,YAAY,CAAO,iBAAiB,CAAC;EACxE;EACA,OAAOF,UAAU;AACnB","ignoreList":[]}
@@ -38,7 +38,7 @@ export function getRandomValues(array) {
38
38
  return array;
39
39
  }
40
40
 
41
- // Remote debugging in Chrome can't call sync native methods
41
+ // Old-arch remote debugging in Chrome can't call sync TurboModules
42
42
  // ("Calling synchronous methods on native modules is not supported in
43
43
  // Chrome"), so fall back to Math.random() there.
44
44
  if (isRemoteDebuggingInChrome()) {
@@ -1,4 +1,5 @@
1
- export interface Spec {
1
+ import { type TurboModule } from 'react-native';
2
+ export interface Spec extends TurboModule {
2
3
  getRandomBase64(byteLength: number): string;
3
4
  }
4
5
  export declare function getNativeGetRandomValues(): Spec;
@@ -1 +1 @@
1
- {"version":3,"file":"NativeGetRandomValues.d.ts","sourceRoot":"","sources":["../../../src/NativeGetRandomValues.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,IAAI;IACnB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAA;CAC5C;AAED,wBAAgB,wBAAwB,IAAI,IAAI,CAU/C"}
1
+ {"version":3,"file":"NativeGetRandomValues.d.ts","sourceRoot":"","sources":["../../../src/NativeGetRandomValues.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAA;AAIpE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAA;CAC5C;AAED,wBAAgB,wBAAwB,IAAI,IAAI,CAK/C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@the-polyfills/get-random-values",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "Polyfill for crypto.getRandomValues in React Native",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -54,9 +54,10 @@
54
54
  "@eslint/eslintrc": "^3.3.7",
55
55
  "@eslint/js": "^10.0.1",
56
56
  "@jest/globals": "^30.5.2",
57
- "@react-native/babel-preset": "0.87.1",
58
- "@react-native/eslint-config": "0.87.1",
59
- "@types/react": "~19.0.14",
57
+ "@react-native/babel-preset": "0.86.3",
58
+ "@react-native/eslint-config": "0.86.3",
59
+ "@react-native/jest-preset": "0.86.3",
60
+ "@types/react": "^19.2.0",
60
61
  "del-cli": "^7.0.0",
61
62
  "eslint": "^10.11.0",
62
63
  "eslint-config-prettier": "^10.1.8",
@@ -65,11 +66,11 @@
65
66
  "jest": "^30.5.2",
66
67
  "nodemon": "^3.1.14",
67
68
  "prettier": "^3.9.9",
68
- "react": "19.0.0",
69
- "react-native": "0.79.6",
69
+ "react": "19.2.3",
70
+ "react-native": "0.86.3",
70
71
  "react-native-builder-bob": "^0.43.1",
71
72
  "turbo": "^2.11.3",
72
- "typescript": "~5.8.3"
73
+ "typescript": "^6.0.3"
73
74
  },
74
75
  "peerDependencies": {
75
76
  "react": "*",
@@ -93,8 +94,16 @@
93
94
  ]
94
95
  ]
95
96
  },
97
+ "codegenConfig": {
98
+ "name": "GetRandomValuesSpec",
99
+ "type": "modules",
100
+ "jsSrcsDir": "src",
101
+ "android": {
102
+ "javaPackageName": "com.thepolyfills.getrandomvalues"
103
+ }
104
+ },
96
105
  "jest": {
97
- "preset": "react-native",
106
+ "preset": "@react-native/jest-preset",
98
107
  "testEnvironmentOptions": {
99
108
  "customExportConditions": [
100
109
  "require",
@@ -1,21 +1,14 @@
1
- import { NativeModules } from 'react-native'
1
+ import { TurboModuleRegistry, type TurboModule } from 'react-native'
2
2
 
3
- // Old Architecture (Paper) only: the native module is registered via
4
- // RCT_EXPORT_MODULE / ReactPackage and looked up through NativeModules.
5
- // There is intentionally no TurboModuleRegistry usage and no codegen spec
6
- // in this package.
7
- export interface Spec {
3
+ let cachedSpec: Spec | null = null
4
+
5
+ export interface Spec extends TurboModule {
8
6
  getRandomBase64(byteLength: number): string
9
7
  }
10
8
 
11
9
  export function getNativeGetRandomValues(): Spec {
12
- const native = NativeModules.GetRandomValues as Spec | undefined
13
-
14
- if (native == null || typeof native.getRandomBase64 !== 'function') {
15
- throw new Error(
16
- "Native module 'GetRandomValues' not found. Rebuild the app after installing the package (e.g. `expo prebuild --clean`).",
17
- )
10
+ if (cachedSpec == null) {
11
+ cachedSpec = TurboModuleRegistry.getEnforcing<Spec>('GetRandomValues')
18
12
  }
19
-
20
- return native
13
+ return cachedSpec
21
14
  }
@@ -58,7 +58,7 @@ export function getRandomValues<T extends ArrayBufferView>(array: T): T {
58
58
  return array
59
59
  }
60
60
 
61
- // Remote debugging in Chrome can't call sync native methods
61
+ // Old-arch remote debugging in Chrome can't call sync TurboModules
62
62
  // ("Calling synchronous methods on native modules is not supported in
63
63
  // Chrome"), so fall back to Math.random() there.
64
64
  if (isRemoteDebuggingInChrome()) {