react-native-nitro-storage 0.4.0 → 0.4.3

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 (41) hide show
  1. package/README.md +90 -0
  2. package/android/build.gradle +0 -12
  3. package/android/consumer-rules.pro +26 -4
  4. package/android/src/main/cpp/AndroidStorageAdapterCpp.cpp +7 -10
  5. package/android/src/main/cpp/AndroidStorageAdapterCpp.hpp +0 -4
  6. package/android/src/main/cpp/cpp-adapter.cpp +3 -1
  7. package/android/src/main/java/com/nitrostorage/AndroidStorageAdapter.kt +172 -77
  8. package/cpp/bindings/HybridStorage.cpp +120 -69
  9. package/cpp/bindings/HybridStorage.hpp +4 -0
  10. package/ios/IOSStorageAdapterCpp.hpp +2 -1
  11. package/ios/IOSStorageAdapterCpp.mm +264 -49
  12. package/lib/commonjs/index.js +128 -20
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/index.web.js +169 -41
  15. package/lib/commonjs/index.web.js.map +1 -1
  16. package/lib/commonjs/indexeddb-backend.js +130 -0
  17. package/lib/commonjs/indexeddb-backend.js.map +1 -0
  18. package/lib/commonjs/internal.js +51 -23
  19. package/lib/commonjs/internal.js.map +1 -1
  20. package/lib/module/index.js +121 -20
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/module/index.web.js +162 -41
  23. package/lib/module/index.web.js.map +1 -1
  24. package/lib/module/indexeddb-backend.js +126 -0
  25. package/lib/module/indexeddb-backend.js.map +1 -0
  26. package/lib/module/internal.js +51 -23
  27. package/lib/module/internal.js.map +1 -1
  28. package/lib/typescript/index.d.ts +6 -0
  29. package/lib/typescript/index.d.ts.map +1 -1
  30. package/lib/typescript/index.web.d.ts +7 -1
  31. package/lib/typescript/index.web.d.ts.map +1 -1
  32. package/lib/typescript/indexeddb-backend.d.ts +29 -0
  33. package/lib/typescript/indexeddb-backend.d.ts.map +1 -0
  34. package/lib/typescript/internal.d.ts.map +1 -1
  35. package/nitrogen/generated/android/NitroStorageOnLoad.cpp +22 -17
  36. package/nitrogen/generated/android/NitroStorageOnLoad.hpp +13 -4
  37. package/package.json +7 -3
  38. package/src/index.ts +137 -27
  39. package/src/index.web.ts +182 -49
  40. package/src/indexeddb-backend.ts +143 -0
  41. package/src/internal.ts +51 -23
package/src/internal.ts CHANGED
@@ -4,6 +4,15 @@ export const MIGRATION_VERSION_KEY = "__nitro_storage_migration_version__";
4
4
  export const NATIVE_BATCH_MISSING_SENTINEL =
5
5
  "__nitro_storage_batch_missing__::v1";
6
6
  const PRIMITIVE_FAST_PATH_PREFIX = "__nitro_storage_primitive__:";
7
+ const PRIM_NULL = "__nitro_storage_primitive__:l";
8
+ const PRIM_UNDEFINED = "__nitro_storage_primitive__:u";
9
+ const PRIM_TRUE = "__nitro_storage_primitive__:b:1";
10
+ const PRIM_FALSE = "__nitro_storage_primitive__:b:0";
11
+ const PRIM_STRING_PREFIX = "__nitro_storage_primitive__:s:";
12
+ const PRIM_NUMBER_PREFIX = "__nitro_storage_primitive__:n:";
13
+ const PRIM_INFINITY = "__nitro_storage_primitive__:n:Infinity";
14
+ const PRIM_NEG_INFINITY = "__nitro_storage_primitive__:n:-Infinity";
15
+ const PRIM_NAN = "__nitro_storage_primitive__:n:NaN";
7
16
  const NAMESPACE_SEPARATOR = ":";
8
17
  const VERSION_TOKEN_PREFIX = "__nitro_storage_version__:";
9
18
 
@@ -80,21 +89,30 @@ export function isNamespaced(key: string, namespace: string): boolean {
80
89
 
81
90
  export function serializeWithPrimitiveFastPath<T>(value: T): string {
82
91
  if (value === null) {
83
- return `${PRIMITIVE_FAST_PATH_PREFIX}l`;
92
+ return PRIM_NULL;
84
93
  }
85
94
 
86
95
  switch (typeof value) {
87
96
  case "string":
88
- return `${PRIMITIVE_FAST_PATH_PREFIX}s:${value}`;
97
+ return PRIM_STRING_PREFIX + (value as string);
89
98
  case "number":
90
99
  if (Number.isFinite(value)) {
91
- return `${PRIMITIVE_FAST_PATH_PREFIX}n:${value}`;
100
+ return PRIM_NUMBER_PREFIX + String(value);
101
+ }
102
+ if (Number.isNaN(value as number)) {
103
+ return PRIM_NAN;
104
+ }
105
+ if (value === Infinity) {
106
+ return PRIM_INFINITY;
107
+ }
108
+ if (value === -Infinity) {
109
+ return PRIM_NEG_INFINITY;
92
110
  }
93
111
  break;
94
112
  case "boolean":
95
- return `${PRIMITIVE_FAST_PATH_PREFIX}b:${value ? "1" : "0"}`;
113
+ return value ? PRIM_TRUE : PRIM_FALSE;
96
114
  case "undefined":
97
- return `${PRIMITIVE_FAST_PATH_PREFIX}u`;
115
+ return PRIM_UNDEFINED;
98
116
  default:
99
117
  break;
100
118
  }
@@ -108,31 +126,41 @@ export function serializeWithPrimitiveFastPath<T>(value: T): string {
108
126
  return serialized;
109
127
  }
110
128
 
129
+ // charCode constants for fast tag dispatch
130
+ const CHAR_U = 117; // 'u'
131
+ const CHAR_L = 108; // 'l'
132
+ const CHAR_S = 115; // 's'
133
+ const CHAR_B = 98; // 'b'
134
+ const CHAR_N = 110; // 'n'
135
+
111
136
  export function deserializeWithPrimitiveFastPath<T>(value: string): T {
112
137
  if (value.startsWith(PRIMITIVE_FAST_PATH_PREFIX)) {
113
- const encodedValue = value.slice(PRIMITIVE_FAST_PATH_PREFIX.length);
114
- if (encodedValue === "u") {
138
+ const prefixLen = PRIMITIVE_FAST_PATH_PREFIX.length;
139
+ const tagChar = value.charCodeAt(prefixLen);
140
+
141
+ if (tagChar === CHAR_U) {
115
142
  return undefined as T;
116
143
  }
117
- if (encodedValue === "l") {
144
+ if (tagChar === CHAR_L) {
118
145
  return null as T;
119
146
  }
120
147
 
121
- const separatorIndex = encodedValue.indexOf(":");
122
- if (separatorIndex > 0) {
123
- const tag = encodedValue.slice(0, separatorIndex);
124
- const payload = encodedValue.slice(separatorIndex + 1);
125
- if (tag === "s") {
126
- return payload as T;
127
- }
128
- if (tag === "b") {
129
- return (payload === "1") as T;
130
- }
131
- if (tag === "n") {
132
- const parsed = Number(payload);
133
- if (Number.isFinite(parsed)) {
134
- return parsed as T;
135
- }
148
+ // Tagged values have format: prefix + tag + ':' + payload
149
+ const payload = value.slice(prefixLen + 2);
150
+
151
+ if (tagChar === CHAR_S) {
152
+ return payload as T;
153
+ }
154
+ if (tagChar === CHAR_B) {
155
+ return (payload === "1") as T;
156
+ }
157
+ if (tagChar === CHAR_N) {
158
+ if (payload === "NaN") return NaN as T;
159
+ if (payload === "Infinity") return Infinity as T;
160
+ if (payload === "-Infinity") return -Infinity as T;
161
+ const parsed = Number(payload);
162
+ if (Number.isFinite(parsed)) {
163
+ return parsed as T;
136
164
  }
137
165
  }
138
166
  }