@powersync/common 0.0.0-dev-20260504100448 → 0.0.0-dev-20260630144038

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 (263) hide show
  1. package/lib/attachments/AttachmentContext.d.ts +9 -8
  2. package/lib/attachments/AttachmentContext.js +8 -3
  3. package/lib/attachments/AttachmentContext.js.map +1 -1
  4. package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
  5. package/lib/attachments/AttachmentQueue.d.ts +85 -36
  6. package/lib/attachments/AttachmentQueue.js +19 -21
  7. package/lib/attachments/AttachmentQueue.js.map +1 -1
  8. package/lib/attachments/AttachmentService.d.ts +3 -3
  9. package/lib/attachments/AttachmentService.js +4 -3
  10. package/lib/attachments/AttachmentService.js.map +1 -1
  11. package/lib/attachments/LocalStorageAdapter.d.ts +14 -8
  12. package/lib/attachments/LocalStorageAdapter.js +3 -0
  13. package/lib/attachments/LocalStorageAdapter.js.map +1 -1
  14. package/lib/attachments/RemoteStorageAdapter.d.ts +4 -4
  15. package/lib/attachments/Schema.d.ts +14 -6
  16. package/lib/attachments/Schema.js +8 -3
  17. package/lib/attachments/Schema.js.map +1 -1
  18. package/lib/attachments/SyncingService.d.ts +2 -2
  19. package/lib/attachments/SyncingService.js +8 -3
  20. package/lib/attachments/SyncingService.js.map +1 -1
  21. package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
  22. package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +112 -238
  23. package/lib/client/CommonPowerSyncDatabase.js +2 -0
  24. package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
  25. package/lib/client/Query.d.ts +9 -0
  26. package/lib/client/SQLOpenFactory.d.ts +44 -10
  27. package/lib/client/SQLOpenFactory.js +1 -19
  28. package/lib/client/SQLOpenFactory.js.map +1 -1
  29. package/lib/client/compilableQueryWatch.d.ts +8 -2
  30. package/lib/client/compilableQueryWatch.js +4 -2
  31. package/lib/client/compilableQueryWatch.js.map +1 -1
  32. package/lib/client/connection/PowerSyncBackendConnector.d.ts +6 -3
  33. package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
  34. package/lib/client/runOnSchemaChange.d.ts +5 -2
  35. package/lib/client/runOnSchemaChange.js +3 -0
  36. package/lib/client/runOnSchemaChange.js.map +1 -1
  37. package/lib/client/sync/bucket/CrudBatch.d.ts +2 -0
  38. package/lib/client/sync/bucket/CrudBatch.js +2 -0
  39. package/lib/client/sync/bucket/CrudBatch.js.map +1 -1
  40. package/lib/client/sync/bucket/CrudEntry.d.ts +9 -31
  41. package/lib/client/sync/bucket/CrudEntry.js +2 -97
  42. package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
  43. package/lib/client/sync/bucket/CrudTransaction.d.ts +3 -0
  44. package/lib/client/sync/bucket/CrudTransaction.js +3 -0
  45. package/lib/client/sync/bucket/CrudTransaction.js.map +1 -1
  46. package/lib/client/sync/options.d.ts +65 -0
  47. package/lib/client/sync/options.js +25 -0
  48. package/lib/client/sync/options.js.map +1 -0
  49. package/lib/client/sync/stream/JsonValue.d.ts +3 -0
  50. package/lib/client/sync/sync-streams.d.ts +23 -8
  51. package/lib/client/triggers/TriggerManager.d.ts +28 -56
  52. package/lib/client/triggers/TriggerManager.js +2 -1
  53. package/lib/client/triggers/TriggerManager.js.map +1 -1
  54. package/lib/client/triggers/sanitizeSQL.d.ts +4 -0
  55. package/lib/client/triggers/sanitizeSQL.js +4 -0
  56. package/lib/client/triggers/sanitizeSQL.js.map +1 -1
  57. package/lib/client/watched/GetAllQuery.d.ts +7 -3
  58. package/lib/client/watched/GetAllQuery.js +3 -1
  59. package/lib/client/watched/GetAllQuery.js.map +1 -1
  60. package/lib/client/watched/WatchedQuery.d.ts +20 -6
  61. package/lib/client/watched/WatchedQuery.js +3 -5
  62. package/lib/client/watched/WatchedQuery.js.map +1 -1
  63. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +15 -45
  64. package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -172
  65. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
  66. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -22
  67. package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
  68. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
  69. package/lib/client/watched/processors/comparators.d.ts +8 -0
  70. package/lib/client/watched/processors/comparators.js +4 -0
  71. package/lib/client/watched/processors/comparators.js.map +1 -1
  72. package/lib/db/DBAdapter.d.ts +90 -113
  73. package/lib/db/DBAdapter.js +70 -89
  74. package/lib/db/DBAdapter.js.map +1 -1
  75. package/lib/db/QueryResult.d.ts +104 -0
  76. package/lib/db/QueryResult.js +96 -0
  77. package/lib/db/QueryResult.js.map +1 -0
  78. package/lib/db/crud/SyncProgress.d.ts +7 -14
  79. package/lib/db/crud/SyncProgress.js +1 -60
  80. package/lib/db/crud/SyncProgress.js.map +1 -1
  81. package/lib/db/crud/SyncStatus.d.ts +54 -91
  82. package/lib/db/crud/SyncStatus.js +1 -245
  83. package/lib/db/crud/SyncStatus.js.map +1 -1
  84. package/lib/db/crud/UploadQueueStatus.d.ts +3 -0
  85. package/lib/db/crud/UploadQueueStatus.js +3 -0
  86. package/lib/db/crud/UploadQueueStatus.js.map +1 -1
  87. package/lib/db/schema/Column.d.ts +22 -1
  88. package/lib/db/schema/Column.js +10 -4
  89. package/lib/db/schema/Column.js.map +1 -1
  90. package/lib/db/schema/Index.d.ts +8 -3
  91. package/lib/db/schema/Index.js +4 -1
  92. package/lib/db/schema/Index.js.map +1 -1
  93. package/lib/db/schema/IndexedColumn.d.ts +8 -3
  94. package/lib/db/schema/IndexedColumn.js +4 -1
  95. package/lib/db/schema/IndexedColumn.js.map +1 -1
  96. package/lib/db/schema/RawTable.d.ts +7 -1
  97. package/lib/db/schema/Schema.d.ts +10 -29
  98. package/lib/db/schema/Schema.js +3 -1
  99. package/lib/db/schema/Schema.js.map +1 -1
  100. package/lib/db/schema/Table.d.ts +69 -91
  101. package/lib/db/schema/Table.js +88 -87
  102. package/lib/db/schema/Table.js.map +1 -1
  103. package/lib/index.d.ts +6 -22
  104. package/lib/index.js +5 -22
  105. package/lib/index.js.map +1 -1
  106. package/lib/types/types.d.ts +6 -0
  107. package/lib/utils/BaseObserver.d.ts +12 -0
  108. package/lib/utils/BaseObserver.js +3 -0
  109. package/lib/utils/BaseObserver.js.map +1 -1
  110. package/lib/utils/Logger.d.ts +60 -22
  111. package/lib/utils/Logger.js +38 -30
  112. package/lib/utils/Logger.js.map +1 -1
  113. package/lib/utils/MetaBaseObserver.d.ts +11 -11
  114. package/lib/utils/MetaBaseObserver.js +1 -50
  115. package/lib/utils/MetaBaseObserver.js.map +1 -1
  116. package/lib/utils/mutex.d.ts +3 -45
  117. package/lib/utils/mutex.js +1 -153
  118. package/lib/utils/mutex.js.map +1 -1
  119. package/package.json +9 -46
  120. package/src/attachments/AttachmentContext.ts +15 -13
  121. package/src/attachments/AttachmentErrorHandler.ts +6 -6
  122. package/src/attachments/AttachmentQueue.ts +99 -42
  123. package/src/attachments/AttachmentService.ts +7 -6
  124. package/src/attachments/LocalStorageAdapter.ts +14 -8
  125. package/src/attachments/README.md +2 -0
  126. package/src/attachments/RemoteStorageAdapter.ts +4 -4
  127. package/src/attachments/Schema.ts +14 -6
  128. package/src/attachments/SyncingService.ts +10 -6
  129. package/src/attachments/WatchedAttachmentItem.ts +3 -1
  130. package/src/client/CommonPowerSyncDatabase.ts +540 -0
  131. package/src/client/Query.ts +9 -0
  132. package/src/client/SQLOpenFactory.ts +47 -19
  133. package/src/client/compilableQueryWatch.ts +9 -4
  134. package/src/client/connection/PowerSyncBackendConnector.ts +6 -3
  135. package/src/client/connection/PowerSyncCredentials.ts +3 -0
  136. package/src/client/runOnSchemaChange.ts +5 -2
  137. package/src/client/sync/bucket/CrudBatch.ts +2 -0
  138. package/src/client/sync/bucket/CrudEntry.ts +10 -101
  139. package/src/client/sync/bucket/CrudTransaction.ts +3 -0
  140. package/src/client/sync/options.ts +77 -0
  141. package/src/client/sync/stream/JsonValue.ts +3 -0
  142. package/src/client/sync/sync-streams.ts +23 -10
  143. package/src/client/triggers/TriggerManager.ts +29 -59
  144. package/src/client/triggers/sanitizeSQL.ts +5 -0
  145. package/src/client/watched/GetAllQuery.ts +8 -4
  146. package/src/client/watched/WatchedQuery.ts +20 -11
  147. package/src/client/watched/processors/DifferentialQueryProcessor.ts +19 -219
  148. package/src/client/watched/processors/OnChangeQueryProcessor.ts +4 -109
  149. package/src/client/watched/processors/comparators.ts +8 -0
  150. package/src/db/DBAdapter.ts +160 -175
  151. package/src/db/QueryResult.ts +195 -0
  152. package/src/db/crud/SyncProgress.ts +8 -43
  153. package/src/db/crud/SyncStatus.ts +66 -216
  154. package/src/db/crud/UploadQueueStatus.ts +3 -0
  155. package/src/db/schema/Column.ts +22 -5
  156. package/src/db/schema/Index.ts +9 -3
  157. package/src/db/schema/IndexedColumn.ts +9 -3
  158. package/src/db/schema/RawTable.ts +7 -1
  159. package/src/db/schema/Schema.ts +12 -7
  160. package/src/db/schema/Table.ts +130 -180
  161. package/src/index.ts +6 -22
  162. package/src/types/types.ts +6 -0
  163. package/src/utils/BaseObserver.ts +12 -0
  164. package/src/utils/Logger.ts +86 -31
  165. package/src/utils/MetaBaseObserver.ts +14 -60
  166. package/src/utils/mutex.ts +4 -189
  167. package/dist/bundle.cjs +0 -14136
  168. package/dist/bundle.cjs.map +0 -1
  169. package/dist/bundle.mjs +0 -14056
  170. package/dist/bundle.mjs.map +0 -1
  171. package/dist/bundle.node.cjs +0 -11613
  172. package/dist/bundle.node.cjs.map +0 -1
  173. package/dist/bundle.node.mjs +0 -11533
  174. package/dist/bundle.node.mjs.map +0 -1
  175. package/dist/index.d.cts +0 -4052
  176. package/legacy/sync_protocol.d.ts +0 -103
  177. package/lib/client/AbstractPowerSyncDatabase.js +0 -987
  178. package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
  179. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -22
  180. package/lib/client/AbstractPowerSyncOpenFactory.js +0 -25
  181. package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
  182. package/lib/client/ConnectionManager.d.ts +0 -112
  183. package/lib/client/ConnectionManager.js +0 -294
  184. package/lib/client/ConnectionManager.js.map +0 -1
  185. package/lib/client/CustomQuery.d.ts +0 -22
  186. package/lib/client/CustomQuery.js +0 -43
  187. package/lib/client/CustomQuery.js.map +0 -1
  188. package/lib/client/constants.d.ts +0 -1
  189. package/lib/client/constants.js +0 -2
  190. package/lib/client/constants.js.map +0 -1
  191. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -40
  192. package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -19
  193. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
  194. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -34
  195. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
  196. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
  197. package/lib/client/sync/stream/AbstractRemote.d.ts +0 -151
  198. package/lib/client/sync/stream/AbstractRemote.js +0 -473
  199. package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
  200. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -218
  201. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -663
  202. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
  203. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
  204. package/lib/client/sync/stream/WebsocketClientTransport.js +0 -61
  205. package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
  206. package/lib/client/sync/stream/core-instruction.d.ts +0 -71
  207. package/lib/client/sync/stream/core-instruction.js +0 -27
  208. package/lib/client/sync/stream/core-instruction.js.map +0 -1
  209. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
  210. package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
  211. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
  212. package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
  213. package/lib/client/triggers/TriggerManagerImpl.js +0 -405
  214. package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
  215. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
  216. package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
  217. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
  218. package/lib/db/ConnectionClosedError.d.ts +0 -10
  219. package/lib/db/ConnectionClosedError.js +0 -21
  220. package/lib/db/ConnectionClosedError.js.map +0 -1
  221. package/lib/db/schema/TableV2.d.ts +0 -9
  222. package/lib/db/schema/TableV2.js +0 -9
  223. package/lib/db/schema/TableV2.js.map +0 -1
  224. package/lib/utils/AbortOperation.d.ts +0 -9
  225. package/lib/utils/AbortOperation.js +0 -19
  226. package/lib/utils/AbortOperation.js.map +0 -1
  227. package/lib/utils/ControlledExecutor.d.ts +0 -25
  228. package/lib/utils/ControlledExecutor.js +0 -51
  229. package/lib/utils/ControlledExecutor.js.map +0 -1
  230. package/lib/utils/async.d.ts +0 -14
  231. package/lib/utils/async.js +0 -46
  232. package/lib/utils/async.js.map +0 -1
  233. package/lib/utils/parseQuery.d.ts +0 -6
  234. package/lib/utils/parseQuery.js +0 -17
  235. package/lib/utils/parseQuery.js.map +0 -1
  236. package/lib/utils/queue.d.ts +0 -16
  237. package/lib/utils/queue.js +0 -42
  238. package/lib/utils/queue.js.map +0 -1
  239. package/lib/utils/stream_transform.d.ts +0 -39
  240. package/lib/utils/stream_transform.js +0 -206
  241. package/lib/utils/stream_transform.js.map +0 -1
  242. package/src/client/AbstractPowerSyncDatabase.ts +0 -1362
  243. package/src/client/AbstractPowerSyncOpenFactory.ts +0 -39
  244. package/src/client/ConnectionManager.ts +0 -402
  245. package/src/client/CustomQuery.ts +0 -56
  246. package/src/client/constants.ts +0 -1
  247. package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -49
  248. package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -199
  249. package/src/client/sync/stream/AbstractRemote.ts +0 -602
  250. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -905
  251. package/src/client/sync/stream/WebsocketClientTransport.ts +0 -80
  252. package/src/client/sync/stream/core-instruction.ts +0 -98
  253. package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
  254. package/src/client/triggers/TriggerManagerImpl.ts +0 -496
  255. package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
  256. package/src/db/ConnectionClosedError.ts +0 -23
  257. package/src/db/schema/TableV2.ts +0 -9
  258. package/src/utils/AbortOperation.ts +0 -17
  259. package/src/utils/ControlledExecutor.ts +0 -72
  260. package/src/utils/async.ts +0 -50
  261. package/src/utils/parseQuery.ts +0 -25
  262. package/src/utils/queue.ts +0 -48
  263. package/src/utils/stream_transform.ts +0 -252
@@ -1,252 +0,0 @@
1
- /**
2
- * An async iterator that can't be cancelled.
3
- *
4
- * To keep data flow simple, we always pass an explicit cancellation token when subscribing to async streams. Once the
5
- * {@link AbortSignal} aborts, iterators are supposed to clean up and then emit a final `{done: true}` event. This means
6
- * that there's no way to distinguish between streams that have completed normally and streams that have been cancelled,
7
- * but that is acceptable for our uses of this.
8
- */
9
- export type SimpleAsyncIterator<T> = Pick<AsyncIterator<T>, 'next'>;
10
-
11
- export const doneResult: IteratorReturnResult<any> = { done: true, value: undefined };
12
-
13
- export function valueResult<T>(value: T) {
14
- return { done: false, value };
15
- }
16
-
17
- /**
18
- * A variant of {@link Array.map} for async iterators.
19
- */
20
- export function map<T1, T2>(source: SimpleAsyncIterator<T1>, map: (source: T1) => T2): SimpleAsyncIterator<T2> {
21
- return {
22
- next: async () => {
23
- const value = await source.next();
24
- if (value.done) {
25
- return value;
26
- } else {
27
- return { value: map(value.value) };
28
- }
29
- }
30
- };
31
- }
32
-
33
- export interface InjectableIterator<T> extends SimpleAsyncIterator<T> {
34
- inject(event: T): void;
35
- }
36
-
37
- /**
38
- * Expands a source async iterator by allowing to inject events asynchronously.
39
- *
40
- * The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
41
- * events are dropped once the main iterator completes, but are otherwise forwarded.
42
- *
43
- * The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
44
- * in response to a `next()` call from downstream if no pending injected events can be dispatched.
45
- */
46
- export function injectable<T>(source: SimpleAsyncIterator<T>): InjectableIterator<T> {
47
- type Waiter = { resolve: (t: IteratorResult<T>) => void; reject: (e: unknown) => void };
48
-
49
- let sourceIsDone = false;
50
- let waiter: Waiter | undefined = undefined; // An active, waiting next() call.
51
- // A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
52
- let pendingSourceEvent: ((w: Waiter) => void) | null = null;
53
-
54
- let pendingInjectedEvents: T[] = [];
55
-
56
- const consumeWaiter = () => {
57
- const pending = waiter;
58
- waiter = undefined;
59
- return pending;
60
- };
61
-
62
- const fetchFromSource = () => {
63
- const resolveWaiter = (propagate: (w: Waiter) => void) => {
64
- const active = consumeWaiter();
65
- if (active) {
66
- propagate(active);
67
- } else {
68
- pendingSourceEvent = propagate;
69
- }
70
- };
71
-
72
- const nextFromSource = source.next();
73
- nextFromSource.then(
74
- (value) => {
75
- sourceIsDone = value.done == true;
76
- resolveWaiter((w) => w.resolve(value));
77
- },
78
- (error) => {
79
- resolveWaiter((w) => w.reject(error));
80
- }
81
- );
82
- };
83
-
84
- return {
85
- next: () => {
86
- return new Promise((resolve, reject) => {
87
- // First priority: Dispatch ready upstream events.
88
- if (sourceIsDone) {
89
- return resolve(doneResult);
90
- }
91
- if (pendingSourceEvent) {
92
- pendingSourceEvent({ resolve, reject });
93
- pendingSourceEvent = null;
94
- return;
95
- }
96
-
97
- // Second priority: Dispatch injected events
98
- if (pendingInjectedEvents.length) {
99
- return resolve(valueResult(pendingInjectedEvents.shift()!));
100
- }
101
-
102
- // Nothing pending? Fetch from source
103
- waiter = { resolve, reject };
104
- return fetchFromSource();
105
- });
106
- },
107
- inject: (event) => {
108
- const pending = consumeWaiter();
109
- if (pending != null) {
110
- pending.resolve(valueResult(event));
111
- } else {
112
- pendingInjectedEvents.push(event);
113
- }
114
- }
115
- };
116
- }
117
-
118
- /**
119
- * Splits a byte stream at line endings, emitting each line as a string.
120
- */
121
- export function extractJsonLines(
122
- source: SimpleAsyncIterator<Uint8Array>,
123
- decoder: TextDecoder
124
- ): SimpleAsyncIterator<string> {
125
- let buffer = '';
126
- const pendingLines: string[] = [];
127
- let isFinalEvent = false;
128
-
129
- return {
130
- next: async () => {
131
- while (true) {
132
- if (isFinalEvent) {
133
- return doneResult;
134
- }
135
-
136
- {
137
- const first = pendingLines.shift();
138
- if (first) {
139
- return { done: false, value: first };
140
- }
141
- }
142
-
143
- const { done, value } = await source.next();
144
- if (done) {
145
- const remaining = buffer.trim();
146
- if (remaining.length != 0) {
147
- isFinalEvent = true;
148
- return { done: false, value: remaining };
149
- }
150
-
151
- return doneResult;
152
- }
153
-
154
- const data = decoder.decode(value, { stream: true });
155
- buffer += data;
156
-
157
- const lines = buffer.split('\n');
158
- for (let i = 0; i < lines.length - 1; i++) {
159
- const l = lines[i].trim();
160
- if (l.length > 0) {
161
- pendingLines.push(l);
162
- }
163
- }
164
-
165
- buffer = lines[lines.length - 1];
166
- }
167
- }
168
- };
169
- }
170
-
171
- /**
172
- * Splits a concatenated stream of BSON objects by emitting individual objects.
173
- */
174
- export function extractBsonObjects(source: SimpleAsyncIterator<Uint8Array>): SimpleAsyncIterator<Uint8Array> {
175
- // Fully read but not emitted yet.
176
- const completedObjects: Uint8Array[] = [];
177
-
178
- // Whether source has returned { done: true }. We do the same once completed objects have been emitted.
179
- let isDone = false;
180
-
181
- const lengthBuffer = new DataView(new ArrayBuffer(4));
182
- let objectBody: Uint8Array | null = null;
183
- // If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
184
- // If we're consuming a document, the bytes remaining.
185
- let remainingLength = 4;
186
-
187
- return {
188
- async next(): Promise<IteratorResult<Uint8Array>> {
189
- while (true) {
190
- // Before fetching new data from upstream, return completed objects.
191
- if (completedObjects.length) {
192
- return valueResult(completedObjects.shift()!);
193
- }
194
- if (isDone) {
195
- return doneResult;
196
- }
197
-
198
- const upstreamEvent = await source.next();
199
- if (upstreamEvent.done) {
200
- isDone = true;
201
- if (objectBody || remainingLength != 4) {
202
- throw new Error('illegal end of stream in BSON object');
203
- }
204
- return doneResult;
205
- }
206
-
207
- const chunk = upstreamEvent.value;
208
- for (let i = 0; i < chunk.length; ) {
209
- const availableInData = chunk.length - i;
210
-
211
- if (objectBody) {
212
- // We're in the middle of reading a BSON document.
213
- const bytesToRead = Math.min(availableInData, remainingLength);
214
- const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
215
- objectBody.set(copySource, objectBody.length - remainingLength);
216
- i += bytesToRead;
217
- remainingLength -= bytesToRead;
218
-
219
- if (remainingLength == 0) {
220
- completedObjects.push(objectBody);
221
-
222
- // Prepare to read another document, starting with its length
223
- objectBody = null;
224
- remainingLength = 4;
225
- }
226
- } else {
227
- // Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
228
- const bytesToRead = Math.min(availableInData, remainingLength);
229
- for (let j = 0; j < bytesToRead; j++) {
230
- lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
231
- }
232
- i += bytesToRead;
233
- remainingLength -= bytesToRead;
234
-
235
- if (remainingLength == 0) {
236
- // Transition from reading length header to reading document. Subtracting 4 because the length of the
237
- // header is included in length.
238
- const length = lengthBuffer.getInt32(0, true /* little endian */);
239
- remainingLength = length - 4;
240
- if (remainingLength < 1) {
241
- throw new Error(`invalid length for bson: ${length}`);
242
- }
243
-
244
- objectBody = new Uint8Array(length);
245
- new DataView(objectBody.buffer).setInt32(0, length, true);
246
- }
247
- }
248
- }
249
- }
250
- }
251
- };
252
- }