| 153 | |
| 154 | // Uses a single allocated buffer for the process, avoiding multiple memory allocations |
| 155 | toBin(): Uint8Array[] { |
| 156 | const buffers = []; |
| 157 | let projection = null; |
| 158 | |
| 159 | // Set up the flags |
| 160 | let flags = 0; |
| 161 | if (this.tailable) { |
| 162 | flags |= OPTS_TAILABLE_CURSOR; |
| 163 | } |
| 164 | |
| 165 | if (this.secondaryOk) { |
| 166 | flags |= OPTS_SECONDARY; |
| 167 | } |
| 168 | |
| 169 | if (this.oplogReplay) { |
| 170 | flags |= OPTS_OPLOG_REPLAY; |
| 171 | } |
| 172 | |
| 173 | if (this.noCursorTimeout) { |
| 174 | flags |= OPTS_NO_CURSOR_TIMEOUT; |
| 175 | } |
| 176 | |
| 177 | if (this.awaitData) { |
| 178 | flags |= OPTS_AWAIT_DATA; |
| 179 | } |
| 180 | |
| 181 | if (this.exhaust) { |
| 182 | flags |= OPTS_EXHAUST; |
| 183 | } |
| 184 | |
| 185 | if (this.partial) { |
| 186 | flags |= OPTS_PARTIAL; |
| 187 | } |
| 188 | |
| 189 | // If batchSize is different to this.numberToReturn |
| 190 | if (this.batchSize !== this.numberToReturn) this.numberToReturn = this.batchSize; |
| 191 | |
| 192 | // Allocate write protocol header buffer |
| 193 | const header = ByteUtils.allocate( |
| 194 | 4 * 4 + // Header |
| 195 | 4 + // Flags |
| 196 | ByteUtils.utf8ByteLength(this.ns) + |
| 197 | 1 + // namespace |
| 198 | 4 + // numberToSkip |
| 199 | 4 // numberToReturn |
| 200 | ); |
| 201 | |
| 202 | // Add header to buffers |
| 203 | buffers.push(header); |
| 204 | |
| 205 | // Serialize the query |
| 206 | const query = BSON.serialize(this.query, { |
| 207 | checkKeys: this.checkKeys, |
| 208 | serializeFunctions: this.serializeFunctions, |
| 209 | ignoreUndefined: this.ignoreUndefined |
| 210 | }); |
| 211 | |
| 212 | // Add query document |