1
- import { assert } from "../deps.ts" ;
2
1
import { Database } from "./database.ts" ;
3
- import { WireProtocol } from "./protocol/mod.ts" ;
4
- import {
5
- ConnectOptions ,
6
- Credential ,
7
- Document ,
8
- ListDatabaseInfo ,
9
- } from "./types.ts" ;
2
+ import { ConnectOptions , Document , ListDatabaseInfo } from "./types.ts" ;
10
3
import { parse } from "./utils/uri.ts" ;
11
- import { AuthContext , ScramAuthPlugin , X509AuthPlugin } from "./auth/mod.ts" ;
12
4
import { MongoError } from "./error.ts" ;
5
+ import { Cluster } from "./cluster.ts" ;
6
+ import { assert } from "../deps.ts" ;
13
7
14
8
const DENO_DRIVER_VERSION = "0.0.1" ;
15
9
16
- export interface DenoConnectOptions {
17
- hostname : string ;
18
- port : number ;
19
- certFile ?: string ;
20
- }
21
-
22
10
export class MongoClient {
23
- #protocol?: WireProtocol ;
24
- #conn?: Deno . Conn ;
11
+ #cluster?: Cluster ;
25
12
26
13
async connect (
27
14
options : ConnectOptions | string ,
28
- serverIndex : number = 0 ,
29
15
) : Promise < Database > {
30
16
try {
31
- if ( typeof options === "string" ) {
32
- options = parse ( options ) ;
33
- }
34
- let conn ;
35
- const denoConnectOps : DenoConnectOptions = {
36
- hostname : options . servers [ serverIndex ] . host ,
37
- port : options . servers [ serverIndex ] . port ,
38
- } ;
39
- if ( options . tls ) {
40
- if ( options . certFile ) {
41
- denoConnectOps . certFile = options . certFile ;
42
- }
43
- if ( options . keyFile ) {
44
- if ( options . keyFilePassword ) {
45
- throw new MongoError (
46
- `Tls keyFilePassword not implemented in Deno driver` ,
47
- ) ;
48
- //TODO, need something like const key = decrypt(options.keyFile) ...
49
- }
50
- throw new MongoError ( `Tls keyFile not implemented in Deno driver` ) ;
51
- //TODO, need Deno.connectTls with something like key or keyFile option.
52
- }
53
- conn = await Deno . connectTls ( denoConnectOps ) ;
54
- } else {
55
- conn = await Deno . connect ( denoConnectOps ) ;
56
- }
57
-
58
- this . #conn = conn ;
59
- this . #protocol = new WireProtocol ( conn ) ;
60
-
61
- if ( ( options as ConnectOptions ) . credential ) {
62
- const authContext = new AuthContext (
63
- this . #protocol,
64
- ( options as ConnectOptions ) . credential ,
65
- options as ConnectOptions ,
66
- ) ;
67
- const mechanism = ( options as ConnectOptions ) . credential ! . mechanism ;
68
- let authPlugin ;
69
- if ( mechanism === "SCRAM-SHA-256" ) {
70
- authPlugin = new ScramAuthPlugin ( "sha256" ) ; //TODO AJUST sha256
71
- } else if ( mechanism === "SCRAM-SHA-1" ) {
72
- authPlugin = new ScramAuthPlugin ( "sha1" ) ;
73
- } else if ( mechanism === "MONGODB-X509" ) {
74
- authPlugin = new X509AuthPlugin ( ) ;
75
- } else {
76
- throw new MongoError (
77
- `Auth mechanism not implemented in Deno driver: ${ mechanism } ` ,
78
- ) ;
79
- }
80
- const request = authPlugin . prepare ( authContext ) ;
81
- authContext . response = await this . #protocol. commandSingle (
82
- "admin" ,
83
- request ,
84
- ) ;
85
- await authPlugin . auth ( authContext ) ;
86
- } else {
87
- await this . #protocol. connect ( ) ;
88
- }
17
+ const parsedOptions = typeof options === "string"
18
+ ? await parse ( options )
19
+ : options ;
20
+ const cluster = new Cluster ( parsedOptions ) ;
21
+ await cluster . connect ( ) ;
22
+ await cluster . authenticate ( ) ;
23
+ await cluster . updateMaster ( ) ;
24
+ this . #cluster = cluster ;
89
25
} catch ( e ) {
90
- if ( serverIndex < ( options as ConnectOptions ) . servers . length - 1 ) {
91
- return await this . connect ( options , serverIndex + 1 ) ;
92
- } else {
93
- throw new MongoError ( `Connection failed: ${ e . message || e } ` ) ;
94
- }
26
+ throw new MongoError ( `Connection failed: ${ e . message || e } ` ) ;
95
27
}
96
28
return this . database ( ( options as ConnectOptions ) . db ) ;
97
29
}
@@ -102,11 +34,11 @@ export class MongoClient {
102
34
authorizedCollections ?: boolean ;
103
35
comment ?: Document ;
104
36
} ) : Promise < ListDatabaseInfo [ ] > {
105
- assert ( this . #protocol) ;
106
37
if ( ! options ) {
107
38
options = { } ;
108
39
}
109
- const { databases } = await this . #protocol. commandSingle ( "admin" , {
40
+ assert ( this . #cluster) ;
41
+ const { databases } = await this . #cluster. protocol . commandSingle ( "admin" , {
110
42
listDatabases : 1 ,
111
43
...options ,
112
44
} ) ;
@@ -115,20 +47,16 @@ export class MongoClient {
115
47
116
48
// TODO: add test cases
117
49
async runCommand < T = any > ( db : string , body : Document ) : Promise < T > {
118
- assert ( this . #protocol ) ;
119
- return await this . #protocol. commandSingle ( db , body ) ;
50
+ assert ( this . #cluster ) ;
51
+ return await this . #cluster . protocol . commandSingle ( db , body ) ;
120
52
}
121
53
122
54
database ( name : string ) : Database {
123
- assert ( this . #protocol ) ;
124
- return new Database ( this . #protocol , name ) ;
55
+ assert ( this . #cluster ) ;
56
+ return new Database ( this . #cluster , name ) ;
125
57
}
126
58
127
59
close ( ) {
128
- if ( this . #conn) {
129
- Deno . close ( this . #conn. rid ) ;
130
- this . #conn = undefined ;
131
- this . #protocol = undefined ;
132
- }
60
+ if ( this . #cluster) this . #cluster. close ( ) ;
133
61
}
134
62
}
0 commit comments