@@ -7,7 +7,20 @@ import * as grpc from "@grpc/grpc-js"
77import * as Url from "url-parse"
88import { DgraphClientStub } from "./clientStub"
99
10- const PORT = "443"
10+ const PORT = 443
11+ // Constants for Dgraph client configuration
12+ const cloudPort = "443"
13+ const dgraphScheme = "dgraph"
14+
15+ // Optional parameters for connection string
16+ const cloudAPIKeyParam = "apikey" // Parameter for providing a Dgraph Cloud API key
17+ const bearerTokenParam = "bearertoken" // Parameter for providing an access token
18+
19+ // SSL mode parameters
20+ const sslModeParam = "sslmode" // Parameter for providing a Dgraph SSL mode
21+ const sslModeDisable = "disable" // Disable SSL
22+ const sslModeRequire = "require" // Require SSL without certificate verification
23+ const sslModeVerifyCA = "verify-ca" // Require SSL with certificate verification
1124/**
1225 * @deprecated since v21.3 and will be removed in v21.07 release.
1326 * Please use {@link clientStubFromCloudEndpoint} instead.
@@ -37,3 +50,60 @@ export function clientStubFromCloudEndpoint(graphqlEndpoint: string, apiKey: str
3750 )
3851 return new DgraphClientStub ( backenedURL , credentials )
3952}
53+
54+ export function clientStubFromConnectionString ( connStr : string ) : DgraphClientStub {
55+ const parsedUrl = new URL ( connStr )
56+
57+ if ( parsedUrl . protocol !== dgraphScheme ) {
58+ throw new Error ( "Invalid scheme: must start with dgraph://" )
59+ }
60+
61+ const host = parsedUrl . hostname
62+ const port = parsedUrl . port || "9080"
63+
64+ const queryParams = parsedUrl . searchParams
65+ const bearerToken = queryParams . get ( bearerTokenParam ) || ""
66+ const apiKey = queryParams . get ( cloudAPIKeyParam ) || ""
67+
68+ if ( apiKey !== "" && bearerToken != "" ) {
69+ throw new Error ( `invalid connection string: both apikey and bearertoken cannot be provided` )
70+ }
71+
72+ const sslMode = queryParams . get ( "sslmode" ) || "disable"
73+
74+ // Validate SSL mode
75+ const validSslModes = [ "disable" , "require" , "verify-ca" ]
76+ if ( ! validSslModes . includes ( sslMode ) ) {
77+ throw new Error ( `Invalid SSL mode: ${ sslMode } (must be one of ${ validSslModes . join ( ", " ) } )` )
78+ }
79+
80+ // Set up gRPC credentials based on SSL mode
81+ let credentials : grpc . ChannelCredentials
82+ switch ( sslMode ) {
83+ case "disable" :
84+ credentials = grpc . credentials . createInsecure ( )
85+ break
86+ case "require" :
87+ credentials = grpc . credentials . createSsl ( )
88+ break
89+ case "verify-ca" :
90+ credentials = grpc . credentials . createSsl ( null , null , null , {
91+ checkServerIdentity : ( ) => null ,
92+ } )
93+ break
94+ default :
95+ throw new Error ( `Unsupported SSL mode: ${ sslMode } ` )
96+ }
97+
98+ const username = parsedUrl . username
99+ const password = parsedUrl . password
100+
101+ const stub = new DgraphClientStub ( `${ host } :${ port } ` , credentials )
102+ if ( username && password ) {
103+ stub . login ( username , password ) . catch ( ( err ) => {
104+ throw new Error ( `Failed to sign in user: ${ err . message } ` )
105+ } )
106+ }
107+
108+ return stub
109+ }
0 commit comments