Skip to content

Commit 2ba775b

Browse files
add support for error wrapping from FLE
1 parent aa61a35 commit 2ba775b

File tree

6 files changed

+543
-144
lines changed

6 files changed

+543
-144
lines changed

.eslintrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
"prettier/prettier": "error",
3939
"no-console": "error",
4040
"valid-typeof": "error",
41+
"@typescript-eslint/no-unused-vars": [
42+
"error",
43+
{
44+
"argsIgnorePattern": "^_",
45+
"caughtErrorsIgnorePattern": "^_",
46+
"destructuredArrayIgnorePattern": "^_",
47+
"varsIgnorePattern": "^_"
48+
}
49+
],
4150
"eqeqeq": [
4251
"error",
4352
"always",

src/bindings.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
function load() {
2+
try {
3+
return require('../build/Release/mongocrypt.node');
4+
} catch {
5+
// Webpack will fail when just returning the require, so we need to wrap
6+
// in a try/catch and rethrow.
7+
/* eslint no-useless-catch: 0 */
8+
try {
9+
return require('../build/Debug/mongocrypt.node');
10+
} catch (error) {
11+
throw error;
12+
}
13+
}
14+
}
15+
16+
export const mc: MongoCryptBindings = load();
17+
18+
export interface MongoCryptConstructor {
19+
new (options: MongoCryptConstructorOptions): IMongoCrypt;
20+
libmongocryptVersion: string;
21+
}
22+
23+
interface MongoCryptContextCtor {
24+
new (): IMongoCryptContext;
25+
}
26+
27+
/**
28+
* The value returned by the native bindings
29+
* reference the `Init(Env env, Object exports)` function in the c++
30+
*/
31+
type MongoCryptBindings = {
32+
MongoCrypt: MongoCryptConstructor;
33+
MongoCryptContextCtor: MongoCryptContextCtor;
34+
MongoCryptKMSRequestCtor: MongoCryptKMSRequest;
35+
};
36+
37+
export interface MongoCryptStatus {
38+
type: number;
39+
code: number;
40+
message?: string;
41+
}
42+
43+
export interface MongoCryptKMSRequest {
44+
addResponse(response: Uint8Array): void;
45+
fail(): boolean;
46+
readonly status: MongoCryptStatus;
47+
readonly bytesNeeded: number;
48+
readonly uSleep: number;
49+
readonly kmsProvider: string;
50+
readonly endpoint: string;
51+
readonly message: Buffer;
52+
}
53+
54+
export interface IMongoCryptContext {
55+
nextMongoOperation(): Buffer;
56+
addMongoOperationResponse(response: Uint8Array): void;
57+
finishMongoOperation(): void;
58+
nextKMSRequest(): MongoCryptKMSRequest | null;
59+
provideKMSProviders(providers: Uint8Array): void;
60+
finishKMSRequests(): void;
61+
finalize(): Buffer;
62+
63+
get status(): MongoCryptStatus;
64+
get state(): number;
65+
}
66+
67+
export type MongoCryptConstructorOptions = {
68+
kmsProviders?: Uint8Array;
69+
schemaMap?: Uint8Array;
70+
encryptedFieldsMap?: Uint8Array;
71+
logger?: unknown;
72+
cryptoCallbacks?: Record<string, unknown>;
73+
cryptSharedLibSearchPaths?: string[];
74+
cryptSharedLibPath?: string;
75+
bypassQueryAnalysis?: boolean;
76+
/** Configure the time to expire the DEK from the cache. */
77+
keyExpirationMS?: number;
78+
/** TODO(NODE-6793): remove this option and have it always set in the next major */
79+
enableMultipleCollinfo?: boolean;
80+
81+
/**
82+
* A function that wraps any errors that are thrown by the bindings in this package
83+
* into a new error type.
84+
*
85+
* Example wrapper function, using the MongoDB driver:
86+
* ```typescript
87+
* (error: Error) => new MongoClientEncryptionError(error.message, { cause: error });
88+
* ```
89+
*/
90+
errorWrapper: (error: Error) => Error;
91+
};
92+
93+
export interface IMongoCrypt {
94+
makeEncryptionContext(ns: string, command: Uint8Array): IMongoCryptContext;
95+
makeExplicitEncryptionContext(
96+
value: Uint8Array,
97+
options?: {
98+
keyId?: Uint8Array;
99+
keyAltName?: Uint8Array;
100+
algorithm?: string;
101+
rangeOptions?: Uint8Array;
102+
textOptions?: Uint8Array;
103+
contentionFactor?: bigint | number;
104+
queryType?: string;
105+
106+
/**
107+
* node-binding specific option
108+
*
109+
* When true, creates a `mongocrypt_ctx_explicit_encrypt_expression` context.
110+
* When false, creates a `mongocrypt_ctx_explicit_encrypt`
111+
*/
112+
expressionMode: boolean;
113+
}
114+
): IMongoCryptContext;
115+
makeDecryptionContext(buffer: Uint8Array): IMongoCryptContext;
116+
makeExplicitDecryptionContext(buffer: Uint8Array): IMongoCryptContext;
117+
makeDataKeyContext(
118+
optionsBuffer: Uint8Array,
119+
options: {
120+
keyAltNames?: Uint8Array[];
121+
keyMaterial?: Uint8Array;
122+
}
123+
): IMongoCryptContext;
124+
makeRewrapManyDataKeyContext(filter: Uint8Array, encryptionKey?: Uint8Array): IMongoCryptContext;
125+
readonly status: MongoCryptStatus;
126+
readonly cryptSharedLibVersionInfo: {
127+
version: bigint;
128+
versionStr: string;
129+
} | null;
130+
readonly cryptoHooksProvider: 'js' | 'native_openssl' | null;
131+
}
132+
133+
export type ExplicitEncryptionContextOptions = NonNullable<
134+
Parameters<IMongoCrypt['makeExplicitEncryptionContext']>[1]
135+
>;
136+
export type DataKeyContextOptions = NonNullable<Parameters<IMongoCrypt['makeDataKeyContext']>[1]>;
137+
export type MongoCryptOptions = MongoCryptConstructorOptions;
138+
export type MongoCryptErrorWrapper = MongoCryptOptions['errorWrapper'];
139+
140+
// export const

0 commit comments

Comments
 (0)