react-native-nitro-storage 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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +447 -0
  3. package/android/CMakeLists.txt +33 -0
  4. package/android/build.gradle +96 -0
  5. package/android/gradle.properties +4 -0
  6. package/android/src/main/cpp/AndroidStorageAdapterCpp.cpp +49 -0
  7. package/android/src/main/cpp/AndroidStorageAdapterCpp.hpp +68 -0
  8. package/android/src/main/cpp/cpp-adapter.cpp +6 -0
  9. package/android/src/main/java/com/nitrostorage/AndroidStorageAdapter.kt +94 -0
  10. package/android/src/main/java/com/nitrostorage/NitroStoragePackage.kt +27 -0
  11. package/app.plugin.js +106 -0
  12. package/cpp/bindings/HybridStorage.cpp +144 -0
  13. package/cpp/bindings/HybridStorage.hpp +52 -0
  14. package/cpp/core/NativeStorageAdapter.hpp +21 -0
  15. package/ios/IOSStorageAdapterCpp.hpp +21 -0
  16. package/ios/IOSStorageAdapterCpp.mm +127 -0
  17. package/lib/commonjs/Storage.nitro.js +13 -0
  18. package/lib/commonjs/Storage.nitro.js.map +1 -0
  19. package/lib/commonjs/index.js +88 -0
  20. package/lib/commonjs/index.js.map +1 -0
  21. package/lib/module/Storage.nitro.js +9 -0
  22. package/lib/module/Storage.nitro.js.map +1 -0
  23. package/lib/module/index.js +77 -0
  24. package/lib/module/index.js.map +1 -0
  25. package/lib/typescript/Storage.nitro.d.ts +16 -0
  26. package/lib/typescript/Storage.nitro.d.ts.map +1 -0
  27. package/lib/typescript/index.d.ts +19 -0
  28. package/lib/typescript/index.d.ts.map +1 -0
  29. package/nitro.json +15 -0
  30. package/nitrogen/generated/.gitattributes +1 -0
  31. package/nitrogen/generated/android/NitroStorage+autolinking.cmake +81 -0
  32. package/nitrogen/generated/android/NitroStorage+autolinking.gradle +27 -0
  33. package/nitrogen/generated/android/NitroStorageOnLoad.cpp +44 -0
  34. package/nitrogen/generated/android/NitroStorageOnLoad.hpp +25 -0
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/com/nitrostorage/NitroStorageOnLoad.kt +35 -0
  36. package/nitrogen/generated/ios/NitroStorage+autolinking.rb +60 -0
  37. package/nitrogen/generated/ios/NitroStorage-Swift-Cxx-Bridge.cpp +17 -0
  38. package/nitrogen/generated/ios/NitroStorage-Swift-Cxx-Bridge.hpp +27 -0
  39. package/nitrogen/generated/ios/NitroStorage-Swift-Cxx-Umbrella.hpp +38 -0
  40. package/nitrogen/generated/ios/NitroStorageAutolinking.mm +35 -0
  41. package/nitrogen/generated/ios/NitroStorageAutolinking.swift +12 -0
  42. package/nitrogen/generated/shared/c++/HybridStorageSpec.cpp +24 -0
  43. package/nitrogen/generated/shared/c++/HybridStorageSpec.hpp +67 -0
  44. package/package.json +117 -0
  45. package/react-native-nitro-storage.podspec +36 -0
  46. package/src/Storage.nitro.ts +17 -0
  47. package/src/index.ts +113 -0
@@ -0,0 +1,12 @@
1
+ ///
2
+ /// NitroStorageAutolinking.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ public final class NitroStorageAutolinking {
9
+ public typealias bridge = margelo.nitro.NitroStorage.bridge.swift
10
+
11
+
12
+ }
@@ -0,0 +1,24 @@
1
+ ///
2
+ /// HybridStorageSpec.cpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #include "HybridStorageSpec.hpp"
9
+
10
+ namespace margelo::nitro::NitroStorage {
11
+
12
+ void HybridStorageSpec::loadHybridMethods() {
13
+ // load base methods/properties
14
+ HybridObject::loadHybridMethods();
15
+ // load custom methods/properties
16
+ registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("set", &HybridStorageSpec::set);
18
+ prototype.registerHybridMethod("get", &HybridStorageSpec::get);
19
+ prototype.registerHybridMethod("remove", &HybridStorageSpec::remove);
20
+ prototype.registerHybridMethod("addOnChange", &HybridStorageSpec::addOnChange);
21
+ });
22
+ }
23
+
24
+ } // namespace margelo::nitro::NitroStorage
@@ -0,0 +1,67 @@
1
+ ///
2
+ /// HybridStorageSpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/HybridObject.hpp>)
11
+ #include <NitroModules/HybridObject.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+
16
+
17
+
18
+ #include <string>
19
+ #include <optional>
20
+ #include <functional>
21
+
22
+ namespace margelo::nitro::NitroStorage {
23
+
24
+ using namespace margelo::nitro;
25
+
26
+ /**
27
+ * An abstract base class for `Storage`
28
+ * Inherit this class to create instances of `HybridStorageSpec` in C++.
29
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
30
+ * @example
31
+ * ```cpp
32
+ * class HybridStorage: public HybridStorageSpec {
33
+ * public:
34
+ * HybridStorage(...): HybridObject(TAG) { ... }
35
+ * // ...
36
+ * };
37
+ * ```
38
+ */
39
+ class HybridStorageSpec: public virtual HybridObject {
40
+ public:
41
+ // Constructor
42
+ explicit HybridStorageSpec(): HybridObject(TAG) { }
43
+
44
+ // Destructor
45
+ ~HybridStorageSpec() override = default;
46
+
47
+ public:
48
+ // Properties
49
+
50
+
51
+ public:
52
+ // Methods
53
+ virtual void set(const std::string& key, const std::string& value, double scope) = 0;
54
+ virtual std::optional<std::string> get(const std::string& key, double scope) = 0;
55
+ virtual void remove(const std::string& key, double scope) = 0;
56
+ virtual std::function<void()> addOnChange(double scope, const std::function<void(const std::string& /* key */, const std::optional<std::string>& /* value */)>& callback) = 0;
57
+
58
+ protected:
59
+ // Hybrid Setup
60
+ void loadHybridMethods() override;
61
+
62
+ protected:
63
+ // Tag for logging
64
+ static constexpr auto TAG = "Storage";
65
+ };
66
+
67
+ } // namespace margelo::nitro::NitroStorage
package/package.json ADDED
@@ -0,0 +1,117 @@
1
+ {
2
+ "name": "react-native-nitro-storage",
3
+ "version": "0.1.0",
4
+ "description": "The fastest, most complete storage solution for React Native. Synchronous Memory, Disk, and Secure storage in one unified API. Built with Nitro Modules.",
5
+ "main": "lib/commonjs/index.js",
6
+ "module": "lib/module/index.js",
7
+ "types": "lib/typescript/index.d.ts",
8
+ "react-native": "src/index.ts",
9
+ "source": "src/index.ts",
10
+ "files": [
11
+ "src",
12
+ "lib",
13
+ "cpp",
14
+ "android",
15
+ "ios",
16
+ "nitrogen",
17
+ "nitro.json",
18
+ "*.podspec",
19
+ "app.plugin.js",
20
+ "!**/__tests__",
21
+ "!**/__fixtures__",
22
+ "!**/__mocks__",
23
+ "!cpp/core/*Test.cpp",
24
+ "!cpp/build",
25
+ "!android/build",
26
+ "!android/.cxx",
27
+ "!scripts"
28
+ ],
29
+ "scripts": {
30
+ "prebuild": "npm run codegen",
31
+ "build": "bob build",
32
+ "clean": "rimraf lib nitrogen/generated",
33
+ "codegen": "nitrogen --logLevel=\"debug\"",
34
+ "typecheck": "tsc --noEmit",
35
+ "test": "jest",
36
+ "test:coverage": "jest --coverage",
37
+ "test:cpp": "node scripts/test-cpp.js",
38
+ "prepublishOnly": "npm run clean && npm run build",
39
+ "prepack": "node -e \"const fs=require('fs'); fs.copyFileSync('../../README.md','./README.md'); try{fs.copyFileSync('../../LICENSE','./LICENSE')}catch(e){}\"",
40
+ "postpack": "node -e \"const fs=require('fs'); ['./README.md','./LICENSE'].forEach(f=>fs.existsSync(f)&&fs.unlinkSync(f))\""
41
+ },
42
+ "keywords": [
43
+ "react-native",
44
+ "storage",
45
+ "nitro",
46
+ "jsi",
47
+ "synchronous",
48
+ "state-management",
49
+ "mmkv",
50
+ "async-storage",
51
+ "secure-store",
52
+ "keychain",
53
+ "persistence",
54
+ "zustand",
55
+ "jotai",
56
+ "encrypted-storage",
57
+ "react-native-storage",
58
+ "native-module",
59
+ "performance",
60
+ "fast-storage"
61
+ ],
62
+ "repository": {
63
+ "type": "git",
64
+ "url": "git+https://github.com/JoaoPauloCMarra/react-native-nitro-storage.git"
65
+ },
66
+ "author": "JoaoPauloCMarra",
67
+ "license": "MIT",
68
+ "bugs": {
69
+ "url": "https://github.com/JoaoPauloCMarra/react-native-nitro-storage/issues"
70
+ },
71
+ "homepage": "https://github.com/JoaoPauloCMarra/react-native-nitro-storage#readme",
72
+ "publishConfig": {
73
+ "registry": "https://registry.npmjs.org/"
74
+ },
75
+ "devDependencies": {
76
+ "@expo/config-plugins": "^54.0.4",
77
+ "@react-native/babel-preset": "^0.83.0",
78
+ "@testing-library/react-hooks": "^8.0.1",
79
+ "@testing-library/react-native": "^13.3.3",
80
+ "@types/jest": "^30.0.0",
81
+ "@types/node": "^20.0.0",
82
+ "@types/react": "^18.0.0",
83
+ "jest": "^29.7.0",
84
+ "nitrogen": "^0.31.10",
85
+ "react": "19.1.0",
86
+ "react-native": "0.81.5",
87
+ "react-native-builder-bob": "^0.31.0",
88
+ "react-native-nitro-modules": "^0.31.10",
89
+ "rimraf": "^6.0.1",
90
+ "ts-jest": "^29.2.5",
91
+ "typescript": "^5.6.3"
92
+ },
93
+ "peerDependencies": {
94
+ "react": "*",
95
+ "react-native": ">=0.75.0",
96
+ "react-native-nitro-modules": ">=0.31.0"
97
+ },
98
+ "react-native-builder-bob": {
99
+ "source": "src",
100
+ "output": "lib",
101
+ "targets": [
102
+ [
103
+ "commonjs",
104
+ {
105
+ "esm": true
106
+ }
107
+ ],
108
+ [
109
+ "module",
110
+ {
111
+ "esm": true
112
+ }
113
+ ],
114
+ "typescript"
115
+ ]
116
+ }
117
+ }
@@ -0,0 +1,36 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "react-native-nitro-storage"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => "13.0" }
14
+ s.source = { :git => "https://github.com/JoaoPauloCMarra/react-native-nitro-storage.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = [
17
+ "ios/**/*.{h,m,mm,swift}",
18
+ "cpp/**/*.{h,hpp,c,cpp}"
19
+ ]
20
+
21
+ s.pod_target_xcconfig = {
22
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
23
+ "CLANG_CXX_LIBRARY" => "libc++",
24
+ "HEADER_SEARCH_PATHS" => [
25
+ "\"$(PODS_TARGET_SRCROOT)/cpp/core\"",
26
+ "\"$(PODS_TARGET_SRCROOT)/cpp/bindings\"",
27
+ "\"$(PODS_TARGET_SRCROOT)/nitrogen/generated/shared/c++\"",
28
+ "\"$(PODS_TARGET_SRCROOT)/nitrogen/generated/ios\""
29
+ ].join(" ")
30
+ }
31
+
32
+ s.dependency "React-Core"
33
+
34
+ load 'nitrogen/generated/ios/NitroStorage+autolinking.rb'
35
+ add_nitrogen_files(s)
36
+ end
@@ -0,0 +1,17 @@
1
+ import type { HybridObject } from "react-native-nitro-modules";
2
+
3
+ export enum StorageScope {
4
+ Memory = 0,
5
+ Disk = 1,
6
+ Secure = 2,
7
+ }
8
+
9
+ export interface Storage extends HybridObject<{ ios: "c++"; android: "c++" }> {
10
+ set(key: string, value: string, scope: number): void;
11
+ get(key: string, scope: number): string | undefined;
12
+ remove(key: string, scope: number): void;
13
+ addOnChange(
14
+ scope: number,
15
+ callback: (key: string, value: string | undefined) => void
16
+ ): () => void;
17
+ }
package/src/index.ts ADDED
@@ -0,0 +1,113 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import { NitroModules } from "react-native-nitro-modules";
3
+ import type { Storage, StorageScope } from "./Storage.nitro";
4
+
5
+ export { StorageScope } from "./Storage.nitro";
6
+ export type { Storage } from "./Storage.nitro";
7
+
8
+ let _storageModule: Storage | null = null;
9
+
10
+ function getStorageModule(): Storage {
11
+ if (!_storageModule) {
12
+ _storageModule = NitroModules.createHybridObject<Storage>("Storage");
13
+ }
14
+ return _storageModule!;
15
+ }
16
+
17
+ export interface StorageItemConfig<T> {
18
+ key: string;
19
+ scope: StorageScope;
20
+ defaultValue?: T;
21
+ serialize?: (value: T) => string;
22
+ deserialize?: (value: string) => T;
23
+ }
24
+
25
+ export interface StorageItem<T> {
26
+ get: () => T;
27
+ set: (value: T) => void;
28
+ delete: () => void;
29
+ subscribe: (callback: () => void) => () => void;
30
+ }
31
+
32
+ function defaultSerialize<T>(value: T): string {
33
+ return JSON.stringify(value);
34
+ }
35
+
36
+ function defaultDeserialize<T>(value: string): T {
37
+ return JSON.parse(value) as T;
38
+ }
39
+
40
+ export function createStorageItem<T = undefined>(
41
+ config: StorageItemConfig<T>
42
+ ): StorageItem<T> {
43
+ const serialize = config.serialize ?? defaultSerialize;
44
+ const deserialize = config.deserialize ?? defaultDeserialize;
45
+
46
+ const listeners = new Set<() => void>();
47
+
48
+ let unsubscribe: (() => void) | null = null;
49
+
50
+ const ensureSubscription = () => {
51
+ if (!unsubscribe) {
52
+ unsubscribe = getStorageModule().addOnChange(config.scope, (key) => {
53
+ if (key === config.key) {
54
+ listeners.forEach((listener) => listener());
55
+ }
56
+ });
57
+ }
58
+ };
59
+
60
+ let lastRaw: string | undefined;
61
+ let lastValue: T | undefined;
62
+
63
+ const get = (): T => {
64
+ const raw = getStorageModule().get(config.key, config.scope);
65
+
66
+ if (raw === lastRaw && lastValue !== undefined) {
67
+ return lastValue;
68
+ }
69
+
70
+ lastRaw = raw;
71
+
72
+ if (raw === undefined) {
73
+ lastValue = config.defaultValue as T;
74
+ } else {
75
+ lastValue = deserialize(raw);
76
+ }
77
+
78
+ return lastValue;
79
+ };
80
+
81
+ const set = (value: T): void => {
82
+ const serialized = serialize(value);
83
+ getStorageModule().set(config.key, serialized, config.scope);
84
+ };
85
+
86
+ const deleteItem = (): void => {
87
+ getStorageModule().remove(config.key, config.scope);
88
+ };
89
+
90
+ const subscribe = (callback: () => void): (() => void) => {
91
+ ensureSubscription();
92
+ listeners.add(callback);
93
+ return () => {
94
+ listeners.delete(callback);
95
+ if (listeners.size === 0 && unsubscribe) {
96
+ unsubscribe();
97
+ unsubscribe = null;
98
+ }
99
+ };
100
+ };
101
+
102
+ return {
103
+ get,
104
+ set,
105
+ delete: deleteItem,
106
+ subscribe,
107
+ };
108
+ }
109
+
110
+ export function useStorage<T>(item: StorageItem<T>): [T, (value: T) => void] {
111
+ const value = useSyncExternalStore(item.subscribe, item.get, item.get);
112
+ return [value, item.set];
113
+ }