Skip to content

Commit

Permalink
feat: add new arch support
Browse files Browse the repository at this point in the history
  • Loading branch information
dcangulo committed Nov 19, 2024
1 parent 3a0da89 commit a1297e4
Show file tree
Hide file tree
Showing 61 changed files with 5,766 additions and 4,669 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@ android/keystores/debug.keystore
# generated by bob
lib/

# React Native Codegen
ios/generated
android/generated

# tests
.tap
1 change: 0 additions & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ nodeLinker: node-modules
nmHoistingLimits: workspaces

plugins:
- path: scripts/pod-install.cjs
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 David Angulo
Copyright (c) 2024 David Angulo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ Proof Key for Code Exchange (PKCE) challenge generator for React Native.
|Node.js |🟢

## Installation
### New Arch
```bash
yarn add react-native-pkce-challenge
npx pod-install ios # iOS Only
npx pod-install macos # macOS Only
npx pod-install
```

### Old Arch
```bash
yarn add [email protected]
npx pod-install
```

## Usage
Expand Down Expand Up @@ -60,4 +66,8 @@ See [UPGRADING.md](UPGRADING.md)
See [CHANGELOGS.md](CHANGELOGS.md)

## License
Copyright © 2023 David Angulo, released under the MIT license, see [LICENSE](LICENSE).
Copyright © 2024 David Angulo, released under the MIT license, see [LICENSE](LICENSE).

---

Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
2 changes: 1 addition & 1 deletion android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(PkceChallenge)
set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 14)

add_library(cpp
add_library(react-native-pkce-challenge
SHARED
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp"
../cpp/react-native-pkce-challenge.cpp
Expand Down
37 changes: 37 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import java.nio.file.Paths
import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["PkceChallenge_kotlinVersion"]

repositories {
google()
mavenCentral()
}

dependencies {
classpath "com.android.tools.build:gradle:7.2.1"
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Expand All @@ -33,11 +38,17 @@ static def findNodeModules(baseDir) {

def nodeModules = findNodeModules(projectDir)

def reactNativeArchitectures() {
def value = rootProject.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
Expand Down Expand Up @@ -77,11 +88,13 @@ android {
defaultConfig {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

externalNativeBuild {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
arguments "-DNODE_MODULES_DIR=${nodeModules}"
abiFilters (*reactNativeArchitectures())
}
}
}
Expand All @@ -92,6 +105,10 @@ android {
}
}

buildFeatures {
buildConfig true
}

buildTypes {
release {
minifyEnabled false
Expand All @@ -106,18 +123,38 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += [
"generated/java",
"generated/jni"
]
}
}
}
}

repositories {
mavenCentral()
google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
// For < 0.71, this will be from the local maven repo
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "PkceChallenge"
codegenJavaPackageName = "com.pkcechallenge"
}
}
11 changes: 4 additions & 7 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
#include "react-native-pkce-challenge.h"

extern "C"
JNIEXPORT void JNICALL
Java_com_pkcechallenge_PkceChallengeModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsi) {
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);
JNIEXPORT jstring JNICALL
Java_com_pkcechallenge_PkceChallengeModule_nativeGetRandomBase64String(JNIEnv *env, jclass type, jdouble byte_length) {
std::string encoded_data = pkcechallenge::getRandomBase64String(byte_length);

if (runtime)
{
pkcechallenge::install(*runtime);
}
return env->NewStringUTF(encoded_data.c_str());
}
41 changes: 0 additions & 41 deletions android/src/main/java/com/pkcechallenge/PkceChallengeModule.java

This file was deleted.

30 changes: 30 additions & 0 deletions android/src/main/java/com/pkcechallenge/PkceChallengeModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.pkcechallenge

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule

import java.security.SecureRandom
import android.util.Base64

@ReactModule(name = PkceChallengeModule.NAME)
class PkceChallengeModule(reactContext: ReactApplicationContext) :
NativePkceChallengeSpec(reactContext) {

override fun getName(): String {
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
override fun getRandomBase64String(byteLength: Double): String {
val bytes = ByteArray(byteLength.toInt())
val secureRandom = SecureRandom()
secureRandom.nextBytes(bytes)

return Base64.encodeToString(bytes, Base64.NO_WRAP)
}

companion object {
const val NAME = "PkceChallenge"
}
}
28 changes: 0 additions & 28 deletions android/src/main/java/com/pkcechallenge/PkceChallengePackage.java

This file was deleted.

34 changes: 34 additions & 0 deletions android/src/main/java/com/pkcechallenge/PkceChallengePackage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.pkcechallenge

import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider
import java.util.HashMap

class PkceChallengePackage : TurboReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return if (name == PkceChallengeModule.NAME) {
PkceChallengeModule(reactContext)
} else {
null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
moduleInfos[PkceChallengeModule.NAME] = ReactModuleInfo(
PkceChallengeModule.NAME,
PkceChallengeModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
true // isTurboModule
)
moduleInfos
}
}
}
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
presets: [['module:react-native-builder-bob/babel-preset', { modules: 'commonjs' }]],
};
34 changes: 5 additions & 29 deletions cpp/react-native-pkce-challenge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,12 @@
#include <vector>
#include <stdlib.h>

using namespace facebook::jsi;
using namespace std;

namespace pkcechallenge {
void install(Runtime &jsiRuntime)
{
auto getRandomBase64String = Function::createFromHostFunction(jsiRuntime,
PropNameID::forAscii(jsiRuntime,
"getRandomBase64String"),
0,
[](Runtime &runtime,
const Value &thisValue,
const Value *arguments,
size_t count) -> Value
{
int byteLength = arguments[0].getNumber();
vector<uint8_t> buffer(byteLength, 0);
arc4random_buf(&buffer[0],
buffer.size());

string encodedData = base64_encode(&buffer[0],
buffer.size());

return Value(runtime,
String::createFromUtf8(runtime,
encodedData));
});
std::string getRandomBase64String(double byte_length) {
std::vector<uint8_t> buffer(byte_length, 0);
arc4random_buf(&buffer[0], buffer.size());
std::string encoded_data = base64_encode(&buffer[0], buffer.size());

Object RNPkceChallenge = Object(jsiRuntime);
RNPkceChallenge.setProperty(jsiRuntime, "getRandomBase64String", move(getRandomBase64String));
jsiRuntime.global().setProperty(jsiRuntime, "PkceChallenge", move(RNPkceChallenge));
return encoded_data;
}
}
2 changes: 1 addition & 1 deletion cpp/react-native-pkce-challenge.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define PKCECHALLENGE_H

namespace pkcechallenge {
void install(facebook::jsi::Runtime &jsiRuntime);
std::string getRandomBase64String(double byte_length);
}

#endif /* PKCECHALLENGE_H */
6 changes: 4 additions & 2 deletions example/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ source 'https://rubygems.org'
# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
ruby ">= 2.6.10"

gem 'cocoapods', '~> 1.13'
gem 'activesupport', '>= 6.1.7.3', '< 7.1.0'
# Exclude problematic versions of cocoapods and activesupport that causes build failures.
gem 'cocoapods', '>= 1.13', '!= 1.15.0', '!= 1.15.1'
gem 'activesupport', '>= 6.1.7.5', '!= 7.1.0'
gem 'xcodeproj', '< 1.26.0'
Loading

0 comments on commit a1297e4

Please sign in to comment.