react-native-xxhash 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 pioner921227
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # react-native-xxhash
2
+
3
+ react-native-xxhash
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install react-native-xxhash
9
+ ```
10
+
11
+ ## Usage
12
+
13
+
14
+ ```js
15
+ import { hash128, hash64 } from 'react-native-xxhash';
16
+
17
+ // ...
18
+
19
+ //This function provides a fast and deterministic 128-bit hash for a given string input.
20
+ const result_hash_128_bits = hash128("hello world");
21
+
22
+ //This function provides a fast and deterministic 64-bit hash for a given string input.
23
+ const result_hash_64_bits = hash64("hello world");
24
+ ```
25
+
26
+
27
+ ## Contributing
28
+
29
+ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
30
+
31
+ ## License
32
+
33
+ MIT
34
+
35
+ ---
36
+
37
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
@@ -0,0 +1,55 @@
1
+ cmake_minimum_required(VERSION 3.4.1)
2
+ project(react-native-xxhash)
3
+
4
+ set (CMAKE_VERBOSE_MAKEFILE ON)
5
+ set (CMAKE_CXX_STANDARD 17)
6
+
7
+ add_library(react-native-xxhash SHARED
8
+ ../cpp/react-native-xxhash.cpp
9
+ ../cpp/xxhash.c
10
+ cpp-adapter.cpp
11
+ )
12
+
13
+ find_package(ReactAndroid REQUIRED CONFIG)
14
+
15
+ find_package(fbjni REQUIRED CONFIG)
16
+
17
+ target_include_directories(
18
+ react-native-xxhash PRIVATE
19
+ "${NODE_MODULES_DIR}/react-native/React"
20
+ "${NODE_MODULES_DIR}/react-native/React/Base"
21
+ "${NODE_MODULES_DIR}/react-native/ReactCommon"
22
+ "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
23
+ # "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
24
+ # "${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
25
+ )
26
+
27
+ # Specifies a path to native header files.
28
+ include_directories(
29
+ ../cpp
30
+ )
31
+
32
+ if (ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
33
+ target_link_libraries(
34
+ react-native-xxhash
35
+ ReactAndroid::jsi
36
+ log
37
+ ReactAndroid::reactnative
38
+ fbjni::fbjni
39
+ )
40
+ else()
41
+ target_link_libraries(
42
+ react-native-xxhash
43
+ fbjni::fbjni
44
+ android
45
+ ReactAndroid::jsi
46
+ ReactAndroid::turbomodulejsijni
47
+ )
48
+
49
+
50
+ # target_link_libraries(react-native-xxhash
51
+ # ReactAndroid::jsi
52
+ # log
53
+ # ReactAndroid::reactnative
54
+ # fbjni::fbjni
55
+ # )
@@ -0,0 +1,153 @@
1
+ import groovy.json.JsonSlurper
2
+ import org.apache.tools.ant.filters.ReplaceTokens
3
+ import java.nio.file.Paths
4
+
5
+ buildscript {
6
+ // Buildscript is evaluated before everything else so we can't use getExtOrDefault
7
+ def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["Xxhash_kotlinVersion"]
8
+
9
+ repositories {
10
+ google()
11
+ mavenCentral()
12
+ }
13
+
14
+ dependencies {
15
+ classpath "com.android.tools.build:gradle:7.2.1"
16
+ // noinspection DifferentKotlinGradleVersion
17
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
18
+ }
19
+ }
20
+
21
+ static def findNodeModules(baseDir) {
22
+ def basePath = baseDir.toPath().normalize()
23
+ // Node's module resolution algorithm searches up to the root directory,
24
+ // after which the base path will be null
25
+ while (basePath) {
26
+ def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
27
+ def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
28
+ if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
29
+ return nodeModulesPath.toString()
30
+ }
31
+ basePath = basePath.getParent()
32
+ }
33
+ throw new GradleException("react-native-xxhash: Failed to find node_modules/ path!")
34
+ }
35
+
36
+ def nodeModules = findNodeModules(projectDir)
37
+ logger.warn("react-native-xxhash: node_modules/ found at: ${nodeModules}")
38
+
39
+ def reactNativeArchitectures() {
40
+ def value = rootProject.getProperties().get("reactNativeArchitectures")
41
+ return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
42
+ }
43
+
44
+ def isNewArchitectureEnabled() {
45
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
46
+ }
47
+
48
+ apply plugin: "com.android.library"
49
+ apply plugin: "kotlin-android"
50
+
51
+ if (isNewArchitectureEnabled()) {
52
+ apply plugin: "com.facebook.react"
53
+ }
54
+
55
+ def getExtOrDefault(name) {
56
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Xxhash_" + name]
57
+ }
58
+
59
+ def getExtOrIntegerDefault(name) {
60
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Xxhash_" + name]).toInteger()
61
+ }
62
+
63
+ def supportsNamespace() {
64
+ def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
65
+ def major = parsed[0].toInteger()
66
+ def minor = parsed[1].toInteger()
67
+
68
+ // Namespace support was added in 7.3.0
69
+ return (major == 7 && minor >= 3) || major >= 8
70
+ }
71
+
72
+ android {
73
+ if (supportsNamespace()) {
74
+ namespace "com.xxhash"
75
+
76
+ sourceSets {
77
+ main {
78
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
79
+ }
80
+ }
81
+ }
82
+
83
+ ndkVersion getExtOrDefault("ndkVersion")
84
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
85
+
86
+ defaultConfig {
87
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
88
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
89
+
90
+ externalNativeBuild {
91
+ cmake {
92
+ cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
93
+ abiFilters (*reactNativeArchitectures())
94
+ arguments "-DANDROID_STL=c++_shared",
95
+ "-DNODE_MODULES_DIR=${nodeModules}"
96
+ }
97
+ }
98
+ }
99
+
100
+ externalNativeBuild {
101
+ cmake {
102
+ path "CMakeLists.txt"
103
+ }
104
+ }
105
+
106
+ buildTypes {
107
+ release {
108
+ minifyEnabled false
109
+ }
110
+ }
111
+
112
+ buildFeatures {
113
+ buildConfig true
114
+ prefab true
115
+ }
116
+
117
+ packagingOptions {
118
+ excludes = [
119
+ "**/libjsi.so",
120
+ "**/libreactnativejni.so",
121
+ "**/libreact_nativemodule_core.so",
122
+ "**/libturbomodulejsijni.so",
123
+ "**/libc++_shared.so",
124
+ "**/libfbjni.so",
125
+ "**/libreactnative.so",
126
+ ]
127
+ }
128
+
129
+ lintOptions {
130
+ disable "GradleCompatible"
131
+ }
132
+
133
+ compileOptions {
134
+ sourceCompatibility JavaVersion.VERSION_1_8
135
+ targetCompatibility JavaVersion.VERSION_1_8
136
+ }
137
+ }
138
+
139
+ repositories {
140
+ mavenCentral()
141
+ google()
142
+ }
143
+
144
+ def kotlin_version = getExtOrDefault("kotlinVersion")
145
+
146
+ dependencies {
147
+ // For < 0.71, this will be from the local maven repo
148
+ // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
149
+ //noinspection GradleDynamicVersion
150
+ implementation "com.facebook.react:react-native:+"
151
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
152
+ }
153
+
@@ -0,0 +1,38 @@
1
+ #include <jni.h>
2
+ #include "react-native-xxhash.h"
3
+ #include "jsi/jsi.h"
4
+ #include <android/log.h>
5
+ #include <fbjni/fbjni.h>
6
+ #include <fbjni/detail/Registration.h>
7
+ #include <typeinfo>
8
+
9
+ //XXHashBridge
10
+
11
+
12
+ using namespace facebook;
13
+
14
+ struct XXHashBridge : jni::JavaClass<XXHashBridge> {
15
+ public:
16
+ static constexpr auto kJavaDescriptor = "Lcom/xxhash/XxhashModule;";
17
+
18
+ static void registerNatives() {
19
+ javaClassStatic()->registerNatives({
20
+ makeNativeMethod("nativeInstall", XXHashBridge::nativeInstall)
21
+ });
22
+ }
23
+ private:
24
+ static void nativeInstall(
25
+ jni::alias_ref<jni::JObject> thiz,
26
+ jlong jsiRuntimePointer
27
+ ) {
28
+
29
+ // Реализация
30
+ auto jsiRuntime = reinterpret_cast<jsi::Runtime*>(jsiRuntimePointer);
31
+ // Установите JSI
32
+ xxhash::install(jsiRuntime);
33
+ }
34
+ };
35
+
36
+ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
37
+ return jni::initialize(vm, [] { XXHashBridge::registerNatives(); });
38
+ }
@@ -0,0 +1,5 @@
1
+ Xxhash_kotlinVersion=1.7.0
2
+ Xxhash_minSdkVersion=21
3
+ Xxhash_targetSdkVersion=31
4
+ Xxhash_compileSdkVersion=31
5
+ Xxhash_ndkversion=21.4.7075529
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.xxhash">
3
+ </manifest>
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,41 @@
1
+ package com.xxhash
2
+
3
+ import android.util.Log
4
+ import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
6
+ import com.facebook.react.bridge.ReactMethod
7
+ import com.facebook.react.common.annotations.FrameworkAPI
8
+
9
+ class XxhashModule internal constructor(val context: ReactApplicationContext) :
10
+ ReactContextBaseJavaModule(context) {
11
+
12
+ companion object {
13
+ const val NAME = "xxhash"
14
+
15
+ init {
16
+ System.loadLibrary("react-native-xxhash")
17
+ }
18
+
19
+ @OptIn(FrameworkAPI::class)
20
+ @JvmStatic
21
+ external fun nativeInstall(jsiRuntimePointer: Long)
22
+ }
23
+
24
+ private val reactContext = context
25
+
26
+ override fun getName(): String {
27
+ return NAME
28
+ }
29
+
30
+ @OptIn(FrameworkAPI::class)
31
+ @ReactMethod(isBlockingSynchronousMethod = true)
32
+ fun install():Boolean {
33
+ Log.d("xxhash", "install() called")
34
+
35
+ nativeInstall(
36
+ context.javaScriptContextHolder!!.get(),
37
+ )
38
+
39
+ return true
40
+ }
41
+ }
@@ -0,0 +1,17 @@
1
+ package com.xxhash
2
+
3
+ import com.facebook.react.ReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.uimanager.ViewManager
7
+
8
+
9
+ class XxhashPackage : ReactPackage {
10
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
11
+ return listOf(XxhashModule(reactContext))
12
+ }
13
+
14
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
15
+ return emptyList()
16
+ }
17
+ }
@@ -0,0 +1,43 @@
1
+ #include "react-native-xxhash.h"
2
+
3
+ void xxhash::install(jsi::Runtime* rt_ptr) {
4
+ jsi::Runtime& runtime = *rt_ptr;
5
+
6
+ jsi::Function hash128 = jsi::Function::createFromHostFunction(
7
+ runtime, jsi::PropNameID::forAscii(runtime, "hash128"), 1,
8
+ [](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
9
+ size_t count) {
10
+ const jsi::Value& arg = args[0];
11
+
12
+ if (!arg.isString()) [[unlikely]] {
13
+ throw jsi::JSError(rt, "Argument is not a 'string'");
14
+ }
15
+
16
+ std::stringstream ss;
17
+
18
+ xxhash::make_hash<HashSize::BITS_128>(arg.asString(rt).utf8(rt), ss);
19
+
20
+ return jsi::String::createFromUtf8(rt, ss.str());
21
+ });
22
+
23
+ jsi::Function hash64 = jsi::Function::createFromHostFunction(
24
+ runtime, jsi::PropNameID::forAscii(runtime, "hash64"), 1,
25
+ [](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
26
+ size_t count) {
27
+ const jsi::Value& arg = args[0];
28
+
29
+ if (!arg.isString()) [[unlikely]] {
30
+ throw jsi::JSError(rt, "Argument is not a 'string'");
31
+ }
32
+
33
+ std::stringstream ss;
34
+
35
+ xxhash::make_hash<HashSize::BITS_64>(arg.asString(rt).utf8(rt), ss);
36
+
37
+ return jsi::String::createFromUtf8(rt, ss.str());
38
+ });
39
+
40
+ runtime.global().setProperty(runtime, "__xxhash128", std::move(hash128));
41
+
42
+ runtime.global().setProperty(runtime, "__xxhash64", std::move(hash64));
43
+ }
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+ #ifndef XXHASH_H
3
+ #define XXHASH_H
4
+ #include <jsi/jsi.h>
5
+ #include "xxhash.h"
6
+ #include <iomanip>
7
+ #include <sstream>
8
+
9
+ using namespace facebook;
10
+
11
+ namespace xxhash {
12
+ enum class HashSize {
13
+ BITS_64,
14
+ BITS_128,
15
+ };
16
+
17
+ template <HashSize T>
18
+ inline void make_hash(const std::string_view str, std::stringstream& ss) noexcept;
19
+
20
+ template <>
21
+ inline void make_hash<HashSize::BITS_64>(const std::string_view str,
22
+ std::stringstream& ss) noexcept {
23
+ XXH64_hash_t hash = XXH3_64bits(str.data(), str.size());
24
+
25
+ ss << std::hex << std::setfill('0') << std::setw(16) << hash;
26
+ };
27
+
28
+ template <>
29
+ inline void make_hash<HashSize::BITS_128>(const std::string_view str,
30
+ std::stringstream& ss) noexcept {
31
+ XXH128_hash_t hash = XXH3_128bits(str.data(), str.size());
32
+
33
+ ss << std::hex << std::setfill('0') << std::setw(16) << hash.high64
34
+ << std::setw(16) << hash.low64;
35
+ };
36
+
37
+ void install(jsi::Runtime* rt);
38
+ }
39
+
40
+
41
+
42
+
43
+ #endif /* XXHASH_H */
package/cpp/xxhash.c ADDED
@@ -0,0 +1,42 @@
1
+ /*
2
+ * xxHash - Extremely Fast Hash algorithm
3
+ * Copyright (C) 2012-2023 Yann Collet
4
+ *
5
+ * BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are
9
+ * met:
10
+ *
11
+ * * Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ * * Redistributions in binary form must reproduce the above
14
+ * copyright notice, this list of conditions and the following disclaimer
15
+ * in the documentation and/or other materials provided with the
16
+ * distribution.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ * You can contact the author at:
31
+ * - xxHash homepage: https://www.xxhash.com
32
+ * - xxHash source repository: https://github.com/Cyan4973/xxHash
33
+ */
34
+
35
+ /*
36
+ * xxhash.c instantiates functions defined in xxhash.h
37
+ */
38
+
39
+ #define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
40
+ #define XXH_IMPLEMENTATION /* access definitions */
41
+
42
+ #include "xxhash.h"