@delali/narsil-react-native 0.0.0-stage → 0.3.1-next.146

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 (186) hide show
  1. package/LICENSE +201 -0
  2. package/NarsilReactNative.podspec +57 -0
  3. package/README.md +46 -2
  4. package/android/CMakeLists.txt +48 -0
  5. package/android/build.gradle +151 -0
  6. package/android/gradle.properties +5 -0
  7. package/android/src/main/AndroidManifest.xml +2 -0
  8. package/android/src/main/cpp/AndroidEnvironment.cpp +44 -0
  9. package/android/src/main/cpp/AndroidEnvironment.hpp +11 -0
  10. package/android/src/main/cpp/cpp-adapter.cpp +11 -0
  11. package/android/src/main/java/com/margelo/nitro/narsil/NarsilAndroidEnvironment.kt +29 -0
  12. package/android/src/main/java/com/margelo/nitro/narsil/NarsilReactNativePackage.kt +22 -0
  13. package/app.plugin.js +25 -0
  14. package/cpp/AttachedField.cpp +234 -0
  15. package/cpp/AttachedField.hpp +61 -0
  16. package/cpp/Compression.cpp +113 -0
  17. package/cpp/Compression.hpp +16 -0
  18. package/cpp/GraphHeldAlone.cpp +29 -0
  19. package/cpp/GraphHeldAlone.hpp +25 -0
  20. package/cpp/HostMemory.cpp +123 -0
  21. package/cpp/HostMemory.hpp +53 -0
  22. package/cpp/HybridNarsilCoreImpl.cpp +390 -0
  23. package/cpp/HybridNarsilCoreImpl.hpp +68 -0
  24. package/cpp/HybridNarsilFilesImpl.cpp +245 -0
  25. package/cpp/HybridNarsilFilesImpl.hpp +34 -0
  26. package/cpp/HybridNarsilHostImpl.cpp +126 -0
  27. package/cpp/HybridNarsilHostImpl.hpp +42 -0
  28. package/cpp/NativeAtomics.cpp +110 -0
  29. package/cpp/NativeAtomics.hpp +11 -0
  30. package/cpp/ProcessInfo.cpp +83 -0
  31. package/cpp/ProcessInfo.hpp +11 -0
  32. package/cpp/ThreadPriority.cpp +77 -0
  33. package/cpp/ThreadPriority.hpp +8 -0
  34. package/cpp/WorkletRuntimeDispatcher.cpp +61 -0
  35. package/cpp/WorkletRuntimeDispatcher.hpp +9 -0
  36. package/cpp/core/include/narsil_core.h +208 -0
  37. package/cpp/core/src/connect.c +213 -0
  38. package/cpp/core/src/distance.c +103 -0
  39. package/cpp/core/src/files.c +99 -0
  40. package/cpp/core/src/heap.c +84 -0
  41. package/cpp/core/src/internal.h +284 -0
  42. package/cpp/core/src/kernels_code.c +209 -0
  43. package/cpp/core/src/kernels_float.c +197 -0
  44. package/cpp/core/src/kernels_pair.c +133 -0
  45. package/cpp/core/src/last_links.c +145 -0
  46. package/cpp/core/src/links.c +195 -0
  47. package/cpp/core/src/locks.c +168 -0
  48. package/cpp/core/src/nearest_links.c +147 -0
  49. package/cpp/core/src/place.c +215 -0
  50. package/cpp/core/src/quantize.c +226 -0
  51. package/cpp/core/src/records.c +97 -0
  52. package/cpp/core/src/remove.c +205 -0
  53. package/cpp/core/src/vectors.c +139 -0
  54. package/cpp/core/src/walk.c +231 -0
  55. package/cpp/core/src/workspace.c +167 -0
  56. package/ios/PrivacyInfo.xcprivacy +23 -0
  57. package/lib/client.d.ts +19 -0
  58. package/lib/client.js +41 -0
  59. package/lib/constants.d.ts +9 -0
  60. package/lib/constants.js +9 -0
  61. package/lib/engine/byte-files.d.ts +5 -0
  62. package/lib/engine/byte-files.js +97 -0
  63. package/lib/engine/core.d.ts +3 -0
  64. package/lib/engine/core.js +113 -0
  65. package/lib/engine/files.d.ts +3 -0
  66. package/lib/engine/files.js +188 -0
  67. package/lib/engine/host.d.ts +13 -0
  68. package/lib/engine/host.js +102 -0
  69. package/lib/engine/install.d.ts +4 -0
  70. package/lib/engine/install.js +29 -0
  71. package/lib/engine/native-error.d.ts +1 -0
  72. package/lib/engine/native-error.js +12 -0
  73. package/lib/engine/stand-ins/atomics.d.ts +5 -0
  74. package/lib/engine/stand-ins/atomics.js +236 -0
  75. package/lib/engine/stand-ins/crypto.d.ts +2 -0
  76. package/lib/engine/stand-ins/crypto.js +65 -0
  77. package/lib/engine/stand-ins/process.d.ts +2 -0
  78. package/lib/engine/stand-ins/process.js +22 -0
  79. package/lib/engine/stand-ins/streams.d.ts +1 -0
  80. package/lib/engine/stand-ins/streams.js +31 -0
  81. package/lib/engine/stand-ins/structured-clone.d.ts +1 -0
  82. package/lib/engine/stand-ins/structured-clone.js +121 -0
  83. package/lib/engine/streamed.d.ts +6 -0
  84. package/lib/engine/streamed.js +29 -0
  85. package/lib/engine/transfer.d.ts +3 -0
  86. package/lib/engine/transfer.js +77 -0
  87. package/lib/errors.d.ts +25 -0
  88. package/lib/errors.js +59 -0
  89. package/lib/index.d.ts +3 -0
  90. package/lib/index.js +2 -0
  91. package/lib/methods.d.ts +5 -0
  92. package/lib/methods.js +47 -0
  93. package/lib/native-objects.d.ts +2 -0
  94. package/lib/native-objects.js +24 -0
  95. package/lib/specs/core.nitro.d.ts +79 -0
  96. package/lib/specs/core.nitro.js +1 -0
  97. package/lib/specs/files.nitro.d.ts +36 -0
  98. package/lib/specs/files.nitro.js +1 -0
  99. package/lib/specs/host.nitro.d.ts +24 -0
  100. package/lib/specs/host.nitro.js +1 -0
  101. package/lib/transferred.d.ts +15 -0
  102. package/lib/transferred.js +6 -0
  103. package/lib/types.d.ts +136 -0
  104. package/lib/types.js +1 -0
  105. package/lib/worklets/app-turns.d.ts +1 -0
  106. package/lib/worklets/app-turns.js +44 -0
  107. package/lib/worklets/client.d.ts +30 -0
  108. package/lib/worklets/client.js +144 -0
  109. package/lib/worklets/index.d.ts +4 -0
  110. package/lib/worklets/index.js +2 -0
  111. package/lib/worklets/instances.d.ts +15 -0
  112. package/lib/worklets/instances.js +194 -0
  113. package/lib/worklets/json-part.d.ts +1 -0
  114. package/lib/worklets/json-part.js +42 -0
  115. package/lib/worklets/runtime.d.ts +25 -0
  116. package/lib/worklets/runtime.js +80 -0
  117. package/lib/worklets/transfer-queue.d.ts +7 -0
  118. package/lib/worklets/transfer-queue.js +79 -0
  119. package/lib/worklets/types.d.ts +26 -0
  120. package/lib/worklets/types.js +1 -0
  121. package/nitro.json +32 -0
  122. package/nitrogen/generated/.gitattributes +1 -0
  123. package/nitrogen/generated/android/NarsilReactNative+autolinking.cmake +85 -0
  124. package/nitrogen/generated/android/NarsilReactNative+autolinking.gradle +27 -0
  125. package/nitrogen/generated/android/NarsilReactNativeOnLoad.cpp +69 -0
  126. package/nitrogen/generated/android/NarsilReactNativeOnLoad.hpp +34 -0
  127. package/nitrogen/generated/android/kotlin/com/margelo/nitro/narsil/NarsilReactNativeOnLoad.kt +35 -0
  128. package/nitrogen/generated/ios/NarsilReactNative+autolinking.rb +62 -0
  129. package/nitrogen/generated/ios/NarsilReactNative-Swift-Cxx-Bridge.cpp +17 -0
  130. package/nitrogen/generated/ios/NarsilReactNative-Swift-Cxx-Bridge.hpp +27 -0
  131. package/nitrogen/generated/ios/NarsilReactNative-Swift-Cxx-Umbrella.hpp +38 -0
  132. package/nitrogen/generated/ios/NarsilReactNativeAutolinking.mm +55 -0
  133. package/nitrogen/generated/ios/NarsilReactNativeAutolinking.swift +16 -0
  134. package/nitrogen/generated/shared/c++/DirectoryEntry.hpp +91 -0
  135. package/nitrogen/generated/shared/c++/FileStatus.hpp +103 -0
  136. package/nitrogen/generated/shared/c++/GraphMemory.hpp +120 -0
  137. package/nitrogen/generated/shared/c++/HybridNarsilAttachedGraphSpec.cpp +21 -0
  138. package/nitrogen/generated/shared/c++/HybridNarsilAttachedGraphSpec.hpp +62 -0
  139. package/nitrogen/generated/shared/c++/HybridNarsilAttachedStoreSpec.cpp +21 -0
  140. package/nitrogen/generated/shared/c++/HybridNarsilAttachedStoreSpec.hpp +62 -0
  141. package/nitrogen/generated/shared/c++/HybridNarsilCoreSpec.cpp +35 -0
  142. package/nitrogen/generated/shared/c++/HybridNarsilCoreSpec.hpp +100 -0
  143. package/nitrogen/generated/shared/c++/HybridNarsilFilesSpec.cpp +37 -0
  144. package/nitrogen/generated/shared/c++/HybridNarsilFilesSpec.hpp +86 -0
  145. package/nitrogen/generated/shared/c++/HybridNarsilHostSpec.cpp +34 -0
  146. package/nitrogen/generated/shared/c++/HybridNarsilHostSpec.hpp +78 -0
  147. package/nitrogen/generated/shared/c++/MemoryView.hpp +91 -0
  148. package/nitrogen/generated/shared/c++/NativeConnectOutcome.hpp +87 -0
  149. package/nitrogen/generated/shared/c++/NativeScores.hpp +87 -0
  150. package/nitrogen/generated/shared/c++/NativeSearchHits.hpp +91 -0
  151. package/nitrogen/generated/shared/c++/ProcessMemory.hpp +87 -0
  152. package/nitrogen/generated/shared/c++/StoreMemory.hpp +142 -0
  153. package/package.json +87 -3
  154. package/react-native.config.js +8 -0
  155. package/src/client.ts +44 -0
  156. package/src/constants.ts +9 -0
  157. package/src/engine/byte-files.ts +99 -0
  158. package/src/engine/core.ts +162 -0
  159. package/src/engine/files.ts +186 -0
  160. package/src/engine/host.ts +111 -0
  161. package/src/engine/install.ts +31 -0
  162. package/src/engine/native-error.ts +10 -0
  163. package/src/engine/stand-ins/atomics.ts +288 -0
  164. package/src/engine/stand-ins/crypto.ts +81 -0
  165. package/src/engine/stand-ins/process.ts +30 -0
  166. package/src/engine/stand-ins/streams.ts +45 -0
  167. package/src/engine/stand-ins/structured-clone.ts +118 -0
  168. package/src/engine/streamed.ts +40 -0
  169. package/src/engine/transfer.ts +72 -0
  170. package/src/errors.ts +73 -0
  171. package/src/index.ts +3 -0
  172. package/src/methods.ts +52 -0
  173. package/src/native-objects.ts +30 -0
  174. package/src/specs/core.nitro.ts +140 -0
  175. package/src/specs/files.nitro.ts +42 -0
  176. package/src/specs/host.nitro.ts +30 -0
  177. package/src/transferred.ts +18 -0
  178. package/src/types.ts +164 -0
  179. package/src/worklets/app-turns.ts +48 -0
  180. package/src/worklets/client.ts +165 -0
  181. package/src/worklets/index.ts +4 -0
  182. package/src/worklets/instances.ts +231 -0
  183. package/src/worklets/json-part.ts +38 -0
  184. package/src/worklets/runtime.ts +190 -0
  185. package/src/worklets/transfer-queue.ts +98 -0
  186. package/src/worklets/types.ts +37 -0
@@ -0,0 +1,236 @@
1
+ const INTEGER_ARRAY_TYPES = [
2
+ Int8Array,
3
+ Uint8Array,
4
+ Int16Array,
5
+ Uint16Array,
6
+ Int32Array,
7
+ Uint32Array,
8
+ BigInt64Array,
9
+ BigUint64Array,
10
+ ];
11
+ const WAITABLE_ARRAY_TYPES = [Int32Array, BigInt64Array];
12
+ const waitersByBuffer = new WeakMap();
13
+ const NATIVE_LOAD = 0;
14
+ const NATIVE_STORE = 1;
15
+ const NATIVE_ADD = 2;
16
+ const NATIVE_SUB = 3;
17
+ const NATIVE_AND = 4;
18
+ const NATIVE_OR = 5;
19
+ const NATIVE_XOR = 6;
20
+ const NATIVE_EXCHANGE = 7;
21
+ const NATIVE_COMPARE_EXCHANGE = 8;
22
+ let nativeAtomics = null;
23
+ const nativeOwnership = new WeakMap();
24
+ function nativeElementKind(array) {
25
+ if (array instanceof Int8Array)
26
+ return 0;
27
+ if (array instanceof Uint8Array)
28
+ return 1;
29
+ if (array instanceof Int16Array)
30
+ return 2;
31
+ if (array instanceof Uint16Array)
32
+ return 3;
33
+ if (array instanceof Int32Array)
34
+ return 4;
35
+ if (array instanceof Uint32Array)
36
+ return 5;
37
+ return -1;
38
+ }
39
+ function onNativeMemory(array) {
40
+ if (nativeAtomics === null)
41
+ return -1;
42
+ const kind = nativeElementKind(array);
43
+ if (kind < 0)
44
+ return -1;
45
+ let owned = nativeOwnership.get(array.buffer);
46
+ if (owned === undefined) {
47
+ owned = nativeAtomics.ownsMemory(array.buffer);
48
+ nativeOwnership.set(array.buffer, owned);
49
+ }
50
+ return owned ? kind : -1;
51
+ }
52
+ function nativeAtomic(atomics, operation, kind, array, position, value, replacement) {
53
+ const byteIndex = array.byteOffset + position * array.BYTES_PER_ELEMENT;
54
+ return atomics.atomic(operation, kind, array.buffer, byteIndex, Number(value), Number(replacement));
55
+ }
56
+ function isOneOf(array, types) {
57
+ for (const type of types) {
58
+ if (array instanceof type)
59
+ return true;
60
+ }
61
+ return false;
62
+ }
63
+ function requireIndex(array, index) {
64
+ const position = Number(index);
65
+ if (!Number.isInteger(position) || position < 0 || position >= array.length) {
66
+ throw new RangeError(`Atomics index ${String(index)} lies outside the array of ${array.length} elements`);
67
+ }
68
+ return position;
69
+ }
70
+ function requireIntegerArray(array) {
71
+ if (!isOneOf(array, INTEGER_ARRAY_TYPES))
72
+ throw new TypeError('Atomics needs an integer typed array');
73
+ return array;
74
+ }
75
+ function asElement(array, value) {
76
+ if (array instanceof BigInt64Array || array instanceof BigUint64Array)
77
+ return BigInt(value);
78
+ return Number(value);
79
+ }
80
+ function readModifyWrite(array, index, value, operation, combine) {
81
+ const target = requireIntegerArray(array);
82
+ const position = requireIndex(target, index);
83
+ const kind = onNativeMemory(target);
84
+ if (kind >= 0 && nativeAtomics !== null) {
85
+ return nativeAtomic(nativeAtomics, operation, kind, target, position, value, 0);
86
+ }
87
+ const previous = target[position];
88
+ target[position] = combine(previous, asElement(target, value));
89
+ return previous;
90
+ }
91
+ function sameAsStored(array, stored, value) {
92
+ const scratch = new array.constructor(1);
93
+ scratch[0] = asElement(array, value);
94
+ return scratch[0] === stored;
95
+ }
96
+ function waitersAt(array, position) {
97
+ let byIndex = waitersByBuffer.get(array.buffer);
98
+ if (byIndex === undefined) {
99
+ byIndex = new Map();
100
+ waitersByBuffer.set(array.buffer, byIndex);
101
+ }
102
+ const byteIndex = array.byteOffset + position * array.BYTES_PER_ELEMENT;
103
+ let waiters = byIndex.get(byteIndex);
104
+ if (waiters === undefined) {
105
+ waiters = [];
106
+ byIndex.set(byteIndex, waiters);
107
+ }
108
+ return waiters;
109
+ }
110
+ function forgetWaiters(array, position, waiters) {
111
+ if (waiters.length > 0)
112
+ return;
113
+ const byIndex = waitersByBuffer.get(array.buffer);
114
+ if (byIndex === undefined)
115
+ return;
116
+ byIndex.delete(array.byteOffset + position * array.BYTES_PER_ELEMENT);
117
+ if (byIndex.size === 0)
118
+ waitersByBuffer.delete(array.buffer);
119
+ }
120
+ function requireWaitable(array, index) {
121
+ if (!isOneOf(array, WAITABLE_ARRAY_TYPES))
122
+ throw new TypeError('Atomics waits only on an Int32Array or a BigInt64Array');
123
+ const target = array;
124
+ return { target, position: requireIndex(target, index) };
125
+ }
126
+ function waitAsync(array, index, value, timeout) {
127
+ const { target, position } = requireWaitable(array, index);
128
+ if (!sameAsStored(target, target[position], value))
129
+ return { async: false, value: 'not-equal' };
130
+ const limit = timeout === undefined || Number.isNaN(Number(timeout)) ? Infinity : Math.max(0, Number(timeout));
131
+ if (limit === 0)
132
+ return { async: false, value: 'timed-out' };
133
+ const waiters = waitersAt(target, position);
134
+ const settled = new Promise(resolve => {
135
+ let timer;
136
+ const waiter = {
137
+ settle(result) {
138
+ if (timer !== undefined)
139
+ clearTimeout(timer);
140
+ resolve(result);
141
+ },
142
+ };
143
+ waiters.push(waiter);
144
+ if (Number.isFinite(limit)) {
145
+ timer = setTimeout(() => {
146
+ const at = waiters.indexOf(waiter);
147
+ if (at !== -1)
148
+ waiters.splice(at, 1);
149
+ forgetWaiters(target, position, waiters);
150
+ resolve('timed-out');
151
+ }, limit);
152
+ }
153
+ });
154
+ return { async: true, value: settled };
155
+ }
156
+ function notify(array, index, count) {
157
+ const target = requireIntegerArray(array);
158
+ const position = requireIndex(target, index);
159
+ if (!isOneOf(target, WAITABLE_ARRAY_TYPES))
160
+ return 0;
161
+ const byIndex = waitersByBuffer.get(target.buffer);
162
+ const waiters = byIndex?.get(target.byteOffset + position * target.BYTES_PER_ELEMENT);
163
+ if (waiters === undefined)
164
+ return 0;
165
+ const wanted = count === undefined ? waiters.length : Math.max(0, Math.floor(Number(count)));
166
+ const woken = waiters.splice(0, wanted);
167
+ forgetWaiters(target, position, waiters);
168
+ for (const waiter of woken)
169
+ waiter.settle('ok');
170
+ return woken.length;
171
+ }
172
+ function wait(array, index, value) {
173
+ const { target, position } = requireWaitable(array, index);
174
+ if (!sameAsStored(target, target[position], value))
175
+ return 'not-equal';
176
+ throw new TypeError('Atomics.wait would block the only thread of this runtime, so it cannot wait here');
177
+ }
178
+ const standInAtomics = {
179
+ add: (array, index, value) => readModifyWrite(array, index, value, NATIVE_ADD, (a, b) => a + b),
180
+ sub: (array, index, value) => readModifyWrite(array, index, value, NATIVE_SUB, (a, b) => a - b),
181
+ and: (array, index, value) => readModifyWrite(array, index, value, NATIVE_AND, (a, b) => a & b),
182
+ or: (array, index, value) => readModifyWrite(array, index, value, NATIVE_OR, (a, b) => a | b),
183
+ xor: (array, index, value) => readModifyWrite(array, index, value, NATIVE_XOR, (a, b) => a ^ b),
184
+ exchange: (array, index, value) => readModifyWrite(array, index, value, NATIVE_EXCHANGE, (_a, b) => b),
185
+ compareExchange(array, index, expected, replacement) {
186
+ const target = requireIntegerArray(array);
187
+ const position = requireIndex(target, index);
188
+ const kind = onNativeMemory(target);
189
+ if (kind >= 0 && nativeAtomics !== null) {
190
+ return nativeAtomic(nativeAtomics, NATIVE_COMPARE_EXCHANGE, kind, target, position, expected, replacement);
191
+ }
192
+ const previous = target[position];
193
+ if (sameAsStored(target, previous, expected)) {
194
+ ;
195
+ target[position] = asElement(target, replacement);
196
+ }
197
+ return previous;
198
+ },
199
+ load(array, index) {
200
+ const target = requireIntegerArray(array);
201
+ const position = requireIndex(target, index);
202
+ const kind = onNativeMemory(target);
203
+ if (kind >= 0 && nativeAtomics !== null) {
204
+ return nativeAtomic(nativeAtomics, NATIVE_LOAD, kind, target, position, 0, 0);
205
+ }
206
+ return target[position];
207
+ },
208
+ store(array, index, value) {
209
+ const target = requireIntegerArray(array);
210
+ const position = requireIndex(target, index);
211
+ const kind = onNativeMemory(target);
212
+ if (kind >= 0 && nativeAtomics !== null) {
213
+ nativeAtomic(nativeAtomics, NATIVE_STORE, kind, target, position, value, 0);
214
+ return Math.trunc(Number(value));
215
+ }
216
+ const element = asElement(target, value);
217
+ target[position] = element;
218
+ return typeof element === 'bigint' ? element : Math.trunc(element);
219
+ },
220
+ isLockFree: (size) => size === 1 || size === 2 || size === 4 || size === 8,
221
+ wait,
222
+ waitAsync,
223
+ notify,
224
+ pause: () => { },
225
+ };
226
+ export function installAtomics(native) {
227
+ if (typeof globalThis.Atomics === 'object')
228
+ return;
229
+ nativeAtomics = native;
230
+ Object.defineProperty(globalThis, 'Atomics', {
231
+ value: standInAtomics,
232
+ writable: true,
233
+ configurable: true,
234
+ enumerable: false,
235
+ });
236
+ }
@@ -0,0 +1,2 @@
1
+ import type { NarsilHost } from '../../specs/host.nitro';
2
+ export declare function installCrypto(host: NarsilHost): void;
@@ -0,0 +1,65 @@
1
+ const MAX_RANDOM_BYTES_PER_CALL = 65_536;
2
+ const UUID_BYTES = 16;
3
+ const UUID_VERSION_BYTE = 6;
4
+ const UUID_VARIANT_BYTE = 8;
5
+ const UUID_HYPHEN_AFTER_BYTES = new Set([4, 6, 8, 10]);
6
+ function isIntegerArray(view) {
7
+ return (view instanceof Int8Array ||
8
+ view instanceof Uint8Array ||
9
+ view instanceof Uint8ClampedArray ||
10
+ view instanceof Int16Array ||
11
+ view instanceof Uint16Array ||
12
+ view instanceof Int32Array ||
13
+ view instanceof Uint32Array ||
14
+ view instanceof BigInt64Array ||
15
+ view instanceof BigUint64Array);
16
+ }
17
+ function namedError(message, name) {
18
+ const error = new Error(message);
19
+ error.name = name;
20
+ return error;
21
+ }
22
+ export function installCrypto(host) {
23
+ const scope = globalThis;
24
+ const existing = scope.crypto;
25
+ if (typeof existing?.getRandomValues === 'function' && typeof existing.randomUUID === 'function')
26
+ return;
27
+ function getRandomValues(array) {
28
+ if (!ArrayBuffer.isView(array) || !isIntegerArray(array)) {
29
+ throw namedError('getRandomValues fills only an integer typed array', 'TypeMismatchError');
30
+ }
31
+ if (array.byteLength > MAX_RANDOM_BYTES_PER_CALL) {
32
+ throw namedError(`getRandomValues fills at most ${MAX_RANDOM_BYTES_PER_CALL} bytes per call, not ${array.byteLength}`, 'QuotaExceededError');
33
+ }
34
+ host.fillRandom(array.buffer, array.byteOffset, array.byteLength);
35
+ return array;
36
+ }
37
+ function randomUUID() {
38
+ const bytes = new Uint8Array(UUID_BYTES);
39
+ host.fillRandom(bytes.buffer, 0, UUID_BYTES);
40
+ bytes[UUID_VERSION_BYTE] = ((bytes[UUID_VERSION_BYTE] ?? 0) & 0x0f) | 0x40;
41
+ bytes[UUID_VARIANT_BYTE] = ((bytes[UUID_VARIANT_BYTE] ?? 0) & 0x3f) | 0x80;
42
+ let text = '';
43
+ for (let index = 0; index < UUID_BYTES; index++) {
44
+ if (UUID_HYPHEN_AFTER_BYTES.has(index))
45
+ text += '-';
46
+ text += (bytes[index] ?? 0).toString(16).padStart(2, '0');
47
+ }
48
+ return text;
49
+ }
50
+ const crypto = existing ?? {};
51
+ if (typeof crypto.getRandomValues !== 'function') {
52
+ Object.defineProperty(crypto, 'getRandomValues', { value: getRandomValues, writable: true, configurable: true });
53
+ }
54
+ if (typeof crypto.randomUUID !== 'function') {
55
+ Object.defineProperty(crypto, 'randomUUID', { value: randomUUID, writable: true, configurable: true });
56
+ }
57
+ if (existing === undefined) {
58
+ Object.defineProperty(globalThis, 'crypto', {
59
+ value: crypto,
60
+ writable: true,
61
+ configurable: true,
62
+ enumerable: false,
63
+ });
64
+ }
65
+ }
@@ -0,0 +1,2 @@
1
+ import type { NarsilHost } from '../../specs/host.nitro';
2
+ export declare function installProcess(host: NarsilHost): void;
@@ -0,0 +1,22 @@
1
+ const MILLISECONDS_PER_SECOND = 1_000;
2
+ export function installProcess(host) {
3
+ const scope = globalThis;
4
+ const existing = scope.process;
5
+ const processInfo = existing ?? {};
6
+ if (typeof processInfo.pid !== 'number') {
7
+ Object.defineProperty(processInfo, 'pid', { value: host.processId(), writable: true, configurable: true });
8
+ }
9
+ if (typeof processInfo.uptime !== 'function') {
10
+ const startedAtMs = host.processStartedAtMs();
11
+ const uptime = () => Math.max(0, Date.now() - startedAtMs) / MILLISECONDS_PER_SECOND;
12
+ Object.defineProperty(processInfo, 'uptime', { value: uptime, writable: true, configurable: true });
13
+ }
14
+ if (existing === undefined) {
15
+ Object.defineProperty(globalThis, 'process', {
16
+ value: processInfo,
17
+ writable: true,
18
+ configurable: true,
19
+ enumerable: false,
20
+ });
21
+ }
22
+ }
@@ -0,0 +1 @@
1
+ export declare function installStreams(): void;
@@ -0,0 +1,31 @@
1
+ import { ByteLengthQueuingStrategy, CountQueuingStrategy, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, TransformStream, TransformStreamDefaultController, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter, } from 'web-streams-polyfill';
2
+ const STREAM_CLASSES = {
3
+ ByteLengthQueuingStrategy,
4
+ CountQueuingStrategy,
5
+ ReadableByteStreamController,
6
+ ReadableStream,
7
+ ReadableStreamBYOBReader,
8
+ ReadableStreamBYOBRequest,
9
+ ReadableStreamDefaultController,
10
+ ReadableStreamDefaultReader,
11
+ TransformStream,
12
+ TransformStreamDefaultController,
13
+ WritableStream,
14
+ WritableStreamDefaultController,
15
+ WritableStreamDefaultWriter,
16
+ };
17
+ export function installStreams() {
18
+ const scope = globalThis;
19
+ if (typeof scope.ReadableStream === 'function')
20
+ return;
21
+ for (const [name, streamClass] of Object.entries(STREAM_CLASSES)) {
22
+ if (scope[name] !== undefined)
23
+ continue;
24
+ Object.defineProperty(globalThis, name, {
25
+ value: streamClass,
26
+ writable: true,
27
+ configurable: true,
28
+ enumerable: false,
29
+ });
30
+ }
31
+ }
@@ -0,0 +1 @@
1
+ export declare function installStructuredClone(): void;
@@ -0,0 +1,121 @@
1
+ const ERROR_CONSTRUCTORS = {
2
+ Error,
3
+ EvalError,
4
+ RangeError,
5
+ ReferenceError,
6
+ SyntaxError,
7
+ TypeError,
8
+ URIError,
9
+ };
10
+ function dataCloneError(message) {
11
+ const DomException = globalThis.DOMException;
12
+ if (DomException !== undefined)
13
+ return new DomException(message, 'DataCloneError');
14
+ const error = new Error(message);
15
+ error.name = 'DataCloneError';
16
+ return error;
17
+ }
18
+ function typedArrayConstructorOf(view) {
19
+ const viewType = view.constructor;
20
+ if (typeof viewType !== 'function')
21
+ return null;
22
+ const scope = globalThis;
23
+ const name = viewType.name;
24
+ if (typeof name !== 'string' || scope[name] !== viewType)
25
+ return null;
26
+ return viewType;
27
+ }
28
+ function cloneValue(value, memory) {
29
+ if (typeof value === 'symbol')
30
+ throw dataCloneError('A symbol cannot be cloned');
31
+ if (typeof value === 'function')
32
+ throw dataCloneError('A function cannot be cloned');
33
+ if (value === null || typeof value !== 'object')
34
+ return value;
35
+ const remembered = memory.get(value);
36
+ if (remembered !== undefined || memory.has(value))
37
+ return remembered;
38
+ if (value instanceof Date) {
39
+ const copy = new Date(value.getTime());
40
+ memory.set(value, copy);
41
+ return copy;
42
+ }
43
+ if (value instanceof RegExp) {
44
+ const copy = new RegExp(value.source, value.flags);
45
+ memory.set(value, copy);
46
+ return copy;
47
+ }
48
+ if (value instanceof Boolean || value instanceof Number || value instanceof String) {
49
+ const copy = Object(value.valueOf());
50
+ memory.set(value, copy);
51
+ return copy;
52
+ }
53
+ if (typeof BigInt !== 'undefined' && Object.prototype.toString.call(value) === '[object BigInt]') {
54
+ const copy = Object(value.valueOf());
55
+ memory.set(value, copy);
56
+ return copy;
57
+ }
58
+ if (value instanceof ArrayBuffer) {
59
+ const copy = value.slice(0, value.byteLength);
60
+ memory.set(value, copy);
61
+ return copy;
62
+ }
63
+ if (ArrayBuffer.isView(value)) {
64
+ const buffer = cloneValue(value.buffer, memory);
65
+ if (value instanceof DataView) {
66
+ const copy = new DataView(buffer, value.byteOffset, value.byteLength);
67
+ memory.set(value, copy);
68
+ return copy;
69
+ }
70
+ const ViewType = typedArrayConstructorOf(value);
71
+ if (ViewType === null)
72
+ throw dataCloneError('This kind of array buffer view cannot be cloned');
73
+ const length = value.length;
74
+ const copy = new ViewType(buffer, value.byteOffset, length);
75
+ memory.set(value, copy);
76
+ return copy;
77
+ }
78
+ if (value instanceof Map) {
79
+ const copy = new Map();
80
+ memory.set(value, copy);
81
+ for (const [key, entry] of value)
82
+ copy.set(cloneValue(key, memory), cloneValue(entry, memory));
83
+ return copy;
84
+ }
85
+ if (value instanceof Set) {
86
+ const copy = new Set();
87
+ memory.set(value, copy);
88
+ for (const entry of value)
89
+ copy.add(cloneValue(entry, memory));
90
+ return copy;
91
+ }
92
+ if (value instanceof Error) {
93
+ const ErrorType = ERROR_CONSTRUCTORS[value.name] ?? Error;
94
+ const copy = new ErrorType(value.message);
95
+ memory.set(value, copy);
96
+ if (typeof value.stack === 'string')
97
+ copy.stack = value.stack;
98
+ if ('cause' in value)
99
+ copy.cause = cloneValue(value.cause, memory);
100
+ return copy;
101
+ }
102
+ const copy = (Array.isArray(value) ? new Array(value.length) : {});
103
+ memory.set(value, copy);
104
+ for (const key of Object.keys(value)) {
105
+ copy[key] = cloneValue(value[key], memory);
106
+ }
107
+ return copy;
108
+ }
109
+ function structuredCloneStandIn(value) {
110
+ return cloneValue(value, new Map());
111
+ }
112
+ export function installStructuredClone() {
113
+ if (typeof globalThis.structuredClone === 'function')
114
+ return;
115
+ Object.defineProperty(globalThis, 'structuredClone', {
116
+ value: structuredCloneStandIn,
117
+ writable: true,
118
+ configurable: true,
119
+ enumerable: false,
120
+ });
121
+ }
@@ -0,0 +1,6 @@
1
+ import type { DurabilityConfig, Narsil, NarsilConfig, ReactNativeHost } from '@delali/narsil';
2
+ import type { StreamedMethod } from '../methods';
3
+ export declare function withPhoneDurability<T extends {
4
+ durability?: DurabilityConfig | null;
5
+ }>(config: T, durabilitySetElsewhere?: boolean): Omit<T, 'durability'> & Pick<NarsilConfig, 'durability'>;
6
+ export declare function callStreamed(narsil: Narsil, host: ReactNativeHost, method: StreamedMethod, args: readonly unknown[]): Promise<unknown>;
@@ -0,0 +1,29 @@
1
+ import { DEFAULT_DURABILITY_DIRECTORY } from '../constants';
2
+ import { collectStream, fileChunks, resolveAppPath, writeStreamToFile } from './byte-files';
3
+ export function withPhoneDurability(config, durabilitySetElsewhere = false) {
4
+ const { durability, ...rest } = config;
5
+ if (durability === null || durabilitySetElsewhere)
6
+ return rest;
7
+ return { ...rest, durability: { directory: DEFAULT_DURABILITY_DIRECTORY, ...durability } };
8
+ }
9
+ export function callStreamed(narsil, host, method, args) {
10
+ const indexName = args[0];
11
+ switch (method) {
12
+ case 'snapshot':
13
+ return narsil.snapshot(indexName).then(collectStream);
14
+ case 'exportDocuments':
15
+ return narsil.exportDocuments(indexName).then(collectStream);
16
+ case 'snapshotToFile':
17
+ return narsil
18
+ .snapshot(indexName)
19
+ .then(stream => writeStreamToFile(host.files, stream, resolveAppPath(host, args[1])));
20
+ case 'exportDocumentsToFile':
21
+ return narsil
22
+ .exportDocuments(indexName)
23
+ .then(stream => writeStreamToFile(host.files, stream, resolveAppPath(host, args[1])));
24
+ case 'restoreFromFile':
25
+ return narsil.restore(indexName, fileChunks(host.files, resolveAppPath(host, args[1])));
26
+ case 'importDocumentExportFromFile':
27
+ return narsil.importDocumentExport(indexName, fileChunks(host.files, resolveAppPath(host, args[1])));
28
+ }
29
+ }
@@ -0,0 +1,3 @@
1
+ import type { TransferredError } from '../transferred';
2
+ export declare function transferredError(error: unknown, seen?: Map<object, unknown>): TransferredError;
3
+ export declare function transferable(value: unknown, seen?: Map<object, unknown>): unknown;
@@ -0,0 +1,77 @@
1
+ function isPlainObject(value) {
2
+ const prototype = Object.getPrototypeOf(value);
3
+ return prototype === Object.prototype || prototype === null;
4
+ }
5
+ function viewCopy(view) {
6
+ if (view.byteOffset === 0 && view.byteLength === view.buffer.byteLength)
7
+ return view;
8
+ const copy = new Uint8Array(view.byteLength);
9
+ copy.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
10
+ if (view instanceof DataView)
11
+ return new DataView(copy.buffer);
12
+ const Constructor = view.constructor;
13
+ return new Constructor(copy.buffer);
14
+ }
15
+ export function transferredError(error, seen = new Map()) {
16
+ if (!(error instanceof Error)) {
17
+ return { transferredError: true, name: 'Error', message: String(error), code: undefined, details: undefined };
18
+ }
19
+ const coded = error;
20
+ return {
21
+ transferredError: true,
22
+ name: error.name,
23
+ message: error.message,
24
+ code: typeof coded.code === 'string' ? coded.code : undefined,
25
+ details: coded.details === undefined ? undefined : transferable(coded.details, seen),
26
+ };
27
+ }
28
+ export function transferable(value, seen = new Map()) {
29
+ if (value === null || typeof value !== 'object')
30
+ return typeof value === 'function' ? undefined : value;
31
+ const known = seen.get(value);
32
+ if (known !== undefined)
33
+ return known;
34
+ if (ArrayBuffer.isView(value)) {
35
+ const copy = viewCopy(value);
36
+ seen.set(value, copy);
37
+ return copy;
38
+ }
39
+ if (value instanceof Error) {
40
+ const copy = transferredError(value, seen);
41
+ seen.set(value, copy);
42
+ return copy;
43
+ }
44
+ if (Array.isArray(value)) {
45
+ const copy = new Array(value.length);
46
+ seen.set(value, copy);
47
+ for (let index = 0; index < value.length; index++)
48
+ copy[index] = transferable(value[index], seen);
49
+ return copy;
50
+ }
51
+ if (value instanceof Map) {
52
+ const copy = new Map();
53
+ seen.set(value, copy);
54
+ for (const [key, entry] of value)
55
+ copy.set(transferable(key, seen), transferable(entry, seen));
56
+ return copy;
57
+ }
58
+ if (value instanceof Set) {
59
+ const copy = new Set();
60
+ seen.set(value, copy);
61
+ for (const entry of value)
62
+ copy.add(transferable(entry, seen));
63
+ return copy;
64
+ }
65
+ if (value instanceof Date || value instanceof RegExp || value instanceof ArrayBuffer)
66
+ return value;
67
+ if (!isPlainObject(value))
68
+ return value;
69
+ const copy = {};
70
+ seen.set(value, copy);
71
+ for (const key of Object.keys(value)) {
72
+ const entry = transferable(value[key], seen);
73
+ if (entry !== undefined)
74
+ copy[key] = entry;
75
+ }
76
+ return copy;
77
+ }
@@ -0,0 +1,25 @@
1
+ import { type TransferredError } from './transferred';
2
+ /**
3
+ * An error that the engine raised, as it reaches the app's JS thread. `code`
4
+ * holds the engine's error code, such as `INDEX_NOT_FOUND`, and `details`
5
+ * holds the values behind the failure.
6
+ *
7
+ * @public
8
+ */
9
+ export interface PhoneNarsilError extends Error {
10
+ readonly code: string;
11
+ readonly details: Record<string, unknown>;
12
+ }
13
+ /**
14
+ * Reports whether a value is an error that the Narsil engine raised, whether
15
+ * it reached the app from the engine's runtime or from this package.
16
+ *
17
+ * @param value - Anything that a rejected promise or a `catch` block holds.
18
+ * @returns True when the value carries a Narsil error code.
19
+ *
20
+ * @public
21
+ */
22
+ export declare function isNarsilError(value: unknown): value is PhoneNarsilError;
23
+ export declare function nativeDependencyMissing(cause: unknown): PhoneNarsilError;
24
+ export declare function rebuiltError(transferred: TransferredError): Error;
25
+ export declare function rebuiltValue(value: unknown): unknown;