Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f0be63

Browse files
feat(NODE-7043): add support for custom error wrapping functions (#102)
1 parent f2125de commit 7f0be63

File tree

8 files changed

+556
-134
lines changed

8 files changed

+556
-134
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: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
79+
/**
80+
* A function that wraps any errors that are thrown by the bindings in this package
81+
* into a new error type.
82+
*
83+
* Example wrapper function, using the MongoDB driver:
84+
* ```typescript
85+
* (error: Error) => new MongoClientEncryptionError(error.message, { cause: error });
86+
* ```
87+
*/
88+
errorWrapper: (error: Error) => Error;
89+
};
90+
91+
export interface IMongoCrypt {
92+
makeEncryptionContext(ns: string, command: Uint8Array): IMongoCryptContext;
93+
makeExplicitEncryptionContext(
94+
value: Uint8Array,
95+
options?: {
96+
keyId?: Uint8Array;
97+
keyAltName?: Uint8Array;
98+
algorithm?: string;
99+
rangeOptions?: Uint8Array;
100+
textOptions?: Uint8Array;
101+
contentionFactor?: bigint | number;
102+
queryType?: string;
103+
104+
/**
105+
* node-binding specific option
106+
*
107+
* When true, creates a `mongocrypt_ctx_explicit_encrypt_expression` context.
108+
* When false, creates a `mongocrypt_ctx_explicit_encrypt`
109+
*/
110+
expressionMode: boolean;
111+
}
112+
): IMongoCryptContext;
113+
makeDecryptionContext(buffer: Uint8Array): IMongoCryptContext;
114+
makeExplicitDecryptionContext(buffer: Uint8Array): IMongoCryptContext;
115+
makeDataKeyContext(
116+
optionsBuffer: Uint8Array,
117+
options: {
118+
keyAltNames?: Uint8Array[];
119+
keyMaterial?: Uint8Array;
120+
}
121+
): IMongoCryptContext;
122+
makeRewrapManyDataKeyContext(filter: Uint8Array, encryptionKey?: Uint8Array): IMongoCryptContext;
123+
readonly status: MongoCryptStatus;
124+
readonly cryptSharedLibVersionInfo: {
125+
version: bigint;
126+
versionStr: string;
127+
} | null;
128+
readonly cryptoHooksProvider: 'js' | 'native_openssl' | null;
129+
}
130+
131+
export type ExplicitEncryptionContextOptions = NonNullable<
132+
Parameters<IMongoCrypt['makeExplicitEncryptionContext']>[1]
133+
>;
134+
export type DataKeyContextOptions = NonNullable<Parameters<IMongoCrypt['makeDataKeyContext']>[1]>;
135+
export type MongoCryptOptions = MongoCryptConstructorOptions;
136+
export type MongoCryptErrorWrapper = MongoCryptOptions['errorWrapper'];
137+
138+
// export const

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /