Skip to content

Commit a5cd1e8

Browse files
committed
chore: set JS callbacks by default
1 parent 9c405b8 commit a5cd1e8

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/index.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import bindings = require('bindings');
1+
import { cryptoCallbacks } from './crypto_callbacks';
2+
export { cryptoCallbacks };
23

3-
const mc = bindings('mongocrypt');
4+
import bindings = require('bindings');
5+
const mc: MongoCryptBindings = bindings('mongocrypt');
46

5-
export { cryptoCallbacks } from './crypto_callbacks';
7+
/**
8+
* The value returned by the native bindings
9+
* reference the `Init(Env env, Object exports)` function in the c++
10+
*/
11+
type MongoCryptBindings = {
12+
MongoCrypt: MongoCryptConstructor;
13+
MongoCryptContextCtor: MongoCryptContextCtor;
14+
MongoCryptKMSRequestCtor: MongoCryptKMSRequest;
15+
};
616

717
export interface MongoCryptKMSRequest {
818
addResponse(response: Uint8Array): void;
@@ -32,17 +42,19 @@ export interface MongoCryptContext {
3242
readonly state: number;
3343
}
3444

45+
type MongoCryptConstructorOptions = {
46+
kmsProviders?: Uint8Array;
47+
schemaMap?: Uint8Array;
48+
encryptedFieldsMap?: Uint8Array;
49+
logger?: unknown;
50+
cryptoCallbacks?: Record<string, unknown>;
51+
cryptSharedLibSearchPaths?: string[];
52+
cryptSharedLibPath?: string;
53+
bypassQueryAnalysis?: boolean;
54+
};
55+
3556
export interface MongoCryptConstructor {
36-
new (options: {
37-
kmsProviders?: Uint8Array;
38-
schemaMap?: Uint8Array;
39-
encryptedFieldsMap?: Uint8Array;
40-
logger?: unknown;
41-
cryptoCallbacks?: Record<string, unknown>;
42-
cryptSharedLibSearchPaths?: string[];
43-
cryptSharedLibPath?: string;
44-
bypassQueryAnalysis?: boolean;
45-
}): MongoCrypt;
57+
new (options: MongoCryptConstructorOptions): MongoCrypt;
4658
libmongocryptVersion: string;
4759
}
4860

@@ -91,7 +103,13 @@ export type ExplicitEncryptionContextOptions = NonNullable<
91103
export type DataKeyContextOptions = NonNullable<Parameters<MongoCrypt['makeDataKeyContext']>[1]>;
92104
export type MongoCryptOptions = NonNullable<ConstructorParameters<MongoCryptConstructor>[0]>;
93105

94-
export const MongoCrypt: MongoCryptConstructor = mc.MongoCrypt;
106+
export const MongoCrypt: MongoCryptConstructor = class MongoCrypt extends mc.MongoCrypt {
107+
constructor(options: MongoCryptConstructorOptions) {
108+
// Pass in JS cryptoCallbacks implementation by default.
109+
// If the Node.js openssl version is supported this will be ignored.
110+
super({ cryptoCallbacks, ...options });
111+
}
112+
};
95113

96114
/** exported for testing only. */
97115
interface MongoCryptContextCtor {

0 commit comments

Comments
 (0)