| 1347 | * @internal |
| 1348 | */ |
| 1349 | export function decorateDecryptionResult( |
| 1350 | decrypted: Document & { [kDecoratedKeys]?: Array<string> }, |
| 1351 | original: Document, |
| 1352 | isTopLevelDecorateCall = true |
| 1353 | ): void { |
| 1354 | if (isTopLevelDecorateCall) { |
| 1355 | // The original value could have been either a JS object or a BSON buffer |
| 1356 | if (ByteUtils.isUint8Array(original)) { |
| 1357 | original = deserialize(original); |
| 1358 | } |
| 1359 | if (ByteUtils.isUint8Array(decrypted)) { |
| 1360 | throw new MongoRuntimeError('Expected result of decryption to be deserialized BSON object'); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | if (!decrypted || typeof decrypted !== 'object') return; |
| 1365 | for (const k of Object.keys(decrypted)) { |
| 1366 | const originalValue = original[k]; |
| 1367 | |
| 1368 | // An object was decrypted by libmongocrypt if and only if it was |
| 1369 | // a BSON Binary object with subtype 6. |
| 1370 | if (originalValue && originalValue._bsontype === 'Binary' && originalValue.sub_type === 6) { |
| 1371 | if (!decrypted[kDecoratedKeys]) { |
| 1372 | Object.defineProperty(decrypted, kDecoratedKeys, { |
| 1373 | value: [], |
| 1374 | configurable: true, |
| 1375 | enumerable: false, |
| 1376 | writable: false |
| 1377 | }); |
| 1378 | } |
| 1379 | // this is defined in the preceding if-statement |
| 1380 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 1381 | decrypted[kDecoratedKeys]!.push(k); |
| 1382 | // Do not recurse into this decrypted value. It could be a sub-document/array, |
| 1383 | // in which case there is no original value associated with its subfields. |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | decorateDecryptionResult(decrypted[k], originalValue, false); |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | /** @internal */ |
| 1392 | export const kDispose: unique symbol = (Symbol.dispose as any) ?? Symbol('dispose'); |