1- import { assert } from "../deps.ts" ;
21import { 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" ;
103import { parse } from "./utils/uri.ts" ;
11- import { AuthContext , ScramAuthPlugin , X509AuthPlugin } from "./auth/mod.ts" ;
124import { MongoError } from "./error.ts" ;
5+ import { Cluster } from "./cluster.ts" ;
6+ import { assert } from "../deps.ts" ;
137
148const DENO_DRIVER_VERSION = "0.0.1" ;
159
16- export interface DenoConnectOptions {
17- hostname : string ;
18- port : number ;
19- certFile ?: string ;
20- }
21-
2210export class MongoClient {
23- #protocol?: WireProtocol ;
24- #conn?: Deno . Conn ;
11+ #cluster?: Cluster ;
2512
2613 async connect (
2714 options : ConnectOptions | string ,
28- serverIndex : number = 0 ,
2915 ) : Promise < Database > {
3016 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 ;
8925 } 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 } ` ) ;
9527 }
9628 return this . database ( ( options as ConnectOptions ) . db ) ;
9729 }
@@ -102,11 +34,11 @@ export class MongoClient {
10234 authorizedCollections ?: boolean ;
10335 comment ?: Document ;
10436 } ) : Promise < ListDatabaseInfo [ ] > {
105- assert ( this . #protocol) ;
10637 if ( ! options ) {
10738 options = { } ;
10839 }
109- const { databases } = await this . #protocol. commandSingle ( "admin" , {
40+ assert ( this . #cluster) ;
41+ const { databases } = await this . #cluster. protocol . commandSingle ( "admin" , {
11042 listDatabases : 1 ,
11143 ...options ,
11244 } ) ;
@@ -115,20 +47,16 @@ export class MongoClient {
11547
11648 // TODO: add test cases
11749 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 ) ;
12052 }
12153
12254 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 ) ;
12557 }
12658
12759 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 ( ) ;
13361 }
13462}
0 commit comments