1
- let { isNode18 , useAWS } = require ( '../lib' )
2
- let _api , _send , _close , _info
1
+ let { getAwsClient , useAWS } = require ( '../lib' )
2
+ let client , ApiUrl
3
3
4
4
function instantiateAPI ( ) {
5
- if ( _api ) return
5
+ return new Promise ( ( res , rej ) => {
6
+ if ( client ) res ( client )
6
7
7
- let { ARC_WSS_URL , AWS_REGION , ARC_SANDBOX } = process . env
8
+ getAwsClient ( {
9
+ plugins : [ '@aws-lite/apigatewaymanagementapi' ]
10
+ } , ( err , _client ) => {
11
+ if ( err ) rej ( err )
12
+ else {
13
+ client = _client
14
+ let { ARC_WSS_URL , ARC_SANDBOX } = process . env
8
15
9
- if ( isNode18 ) {
10
- var {
11
- ApiGatewayManagementApi,
12
- PostToConnectionCommand,
13
- DeleteConnectionCommand,
14
- GetConnectionCommand
15
- } = require ( '@aws-sdk/client-apigatewaymanagementapi' )
16
- }
17
- else {
18
- var ApiGatewayManagementApi = require ( 'aws-sdk/clients/apigatewaymanagementapi' )
19
- }
20
-
21
- if ( useAWS ( ) ) {
22
- _api = new ApiGatewayManagementApi ( {
23
- apiVersion : '2018-11-29' ,
24
- endpoint : `${ ARC_WSS_URL . replace ( / ^ w s / , 'http' ) } ` ,
25
- } )
26
- }
27
- else {
28
- let { ports } = JSON . parse ( ARC_SANDBOX )
29
- let port = ports . _arc
30
- if ( ! port )
31
- throw ReferenceError ( 'Architect internal port not found' )
32
- _api = new ApiGatewayManagementApi ( {
33
- apiVersion : '2018-11-29' ,
34
- endpoint : `http://localhost:${ port } /_arc/ws` ,
35
- region : AWS_REGION || 'us-west-2' ,
16
+ if ( useAWS ( ) ) {
17
+ ApiUrl = ARC_WSS_URL
18
+ }
19
+ else {
20
+ let { ports } = JSON . parse ( ARC_SANDBOX )
21
+ let port = ports . _arc
22
+ if ( ! port ) throw ReferenceError ( 'Architect internal port not found' )
23
+ ApiUrl = `http://localhost:${ port } /_arc/ws`
24
+ }
25
+ res ( client )
26
+ }
36
27
} )
37
- }
38
-
39
-
40
- /** idk.. **/
41
- _send = ( params , callback ) => {
42
- if ( isNode18 ) {
43
- let cmd = new PostToConnectionCommand ( params )
44
- return _api . send ( cmd , callback )
45
- }
46
- else {
47
- return callback ? _api . postToConnection ( params , callback ) : _api . postToConnection ( params ) . promise ( )
48
- }
49
- }
28
+ } )
29
+ }
50
30
51
- /** idk.. **/
52
- _close = ( params , callback ) => {
53
- if ( isNode18 ) {
54
- let cmd = new DeleteConnectionCommand ( params )
55
- return _api . send ( cmd , callback )
56
- }
57
- else {
58
- return callback ? _api . deleteConnection ( params , callback ) : _api . deleteConnection ( params ) . promise ( )
59
- }
60
- }
31
+ /**
32
+ * arc.ws._api
33
+ *
34
+ * Get the raw WebSocket client
35
+ *
36
+ * @param {Function } callback - a node style errback (optional)
37
+ * @returns {Promise } - returned if no callback is supplied
38
+ */
39
+ function _api ( callback ) {
40
+ if ( callback ) instantiateAPI ( )
41
+ . then ( client => callback ( null , client ) )
42
+ . catch ( callback )
61
43
62
- /** idk.. **/
63
- _info = ( params , callback ) => {
64
- if ( isNode18 ) {
65
- let cmd = new GetConnectionCommand ( params )
66
- return _api . send ( cmd , callback )
67
- }
68
- else {
69
- return callback ? _api . getConnection ( params , callback ) : _api . getConnection ( params ) . promise ( )
70
- }
71
- }
44
+ else return new Promise ( ( res , rej ) => {
45
+ instantiateAPI ( )
46
+ . then ( client => res ( client ) )
47
+ . catch ( rej )
48
+ } )
72
49
}
73
50
74
51
/**
75
52
* arc.ws.send
76
53
*
77
- * publish web socket events
54
+ * Publish WebSocket events
78
55
*
79
56
* @param {Object } params
80
57
* @param {String } params.id - the ws connection id (required)
@@ -83,57 +60,108 @@ function instantiateAPI () {
83
60
* @returns {Promise } - returned if no callback is supplied
84
61
*/
85
62
function send ( { id, payload } , callback ) {
86
- instantiateAPI ( )
87
- return _send ( {
88
- ConnectionId : id ,
89
- Data : JSON . stringify ( payload )
90
- } , callback )
63
+ if ( callback ) instantiateAPI ( )
64
+ . then ( client => {
65
+ client . ApiGatewayManagementApi . PostToConnection ( {
66
+ ApiUrl,
67
+ ConnectionId : id ,
68
+ Data : payload ,
69
+ } )
70
+ . then ( result => callback ( null , result ) )
71
+ . catch ( callback )
72
+ } )
73
+ . catch ( callback )
74
+
75
+ else return new Promise ( ( res , rej ) => {
76
+ instantiateAPI ( )
77
+ . then ( client => {
78
+ client . ApiGatewayManagementApi . PostToConnection ( {
79
+ ApiUrl,
80
+ ConnectionId : id ,
81
+ Data : payload ,
82
+ } )
83
+ . then ( result => res ( result ) )
84
+ . catch ( rej )
85
+ } )
86
+ . catch ( rej )
87
+ } )
91
88
}
92
89
93
90
/**
94
91
* arc.ws.close
95
92
*
96
- * publish web socket events
93
+ * Terminate a WebSocket client connection
97
94
*
98
95
* @param {Object } params
99
96
* @param {String } params.id - the ws connection id (required)
100
97
* @param {Function } callback - a node style errback (optional)
101
98
* @returns {Promise } - returned if no callback is supplied
102
99
*/
103
100
function close ( { id } , callback ) {
104
- instantiateAPI ( )
105
- return _close ( {
106
- ConnectionId : id ,
107
- } , callback )
101
+ if ( callback ) instantiateAPI ( )
102
+ . then ( client => {
103
+ client . ApiGatewayManagementApi . DeleteConnection ( {
104
+ ApiUrl,
105
+ ConnectionId : id ,
106
+ } )
107
+ . then ( result => callback ( null , result ) )
108
+ . catch ( callback )
109
+ } )
110
+ . catch ( callback )
111
+
112
+ else return new Promise ( ( res , rej ) => {
113
+ instantiateAPI ( )
114
+ . then ( client => {
115
+ client . ApiGatewayManagementApi . DeleteConnection ( {
116
+ ApiUrl,
117
+ ConnectionId : id ,
118
+ } )
119
+ . then ( result => res ( result ) )
120
+ . catch ( rej )
121
+ } )
122
+ . catch ( rej )
123
+ } )
108
124
}
109
125
110
126
/**
111
127
* arc.ws.info
112
128
*
113
- * publish web socket events
129
+ * Get info on a WebSocket client connection
114
130
*
115
131
* @param {Object } params
116
132
* @param {String } params.id - the ws connection id (required)
117
133
* @param {Function } callback - a node style errback (optional)
118
134
* @returns {Promise } - returned if no callback is supplied
119
135
*/
120
136
function info ( { id } , callback ) {
121
- instantiateAPI ( )
122
- return _info ( {
123
- ConnectionId : id ,
124
- } , callback )
137
+ if ( callback ) instantiateAPI ( )
138
+ . then ( client => {
139
+ client . ApiGatewayManagementApi . GetConnection ( {
140
+ ApiUrl,
141
+ ConnectionId : id ,
142
+ } )
143
+ . then ( result => callback ( null , result ) )
144
+ . catch ( callback )
145
+ } )
146
+ . catch ( callback )
147
+
148
+ else return new Promise ( ( res , rej ) => {
149
+ instantiateAPI ( )
150
+ . then ( client => {
151
+ client . ApiGatewayManagementApi . GetConnection ( {
152
+ ApiUrl,
153
+ ConnectionId : id ,
154
+ } )
155
+ . then ( result => res ( result ) )
156
+ . catch ( rej )
157
+ } )
158
+ . catch ( rej )
159
+ } )
125
160
}
126
161
127
162
module . exports = {
163
+ _api,
128
164
send,
129
165
close,
130
166
info,
131
167
}
132
-
133
- Object . defineProperty ( module . exports , '_api' , {
134
- enumerable : true ,
135
- get ( ) {
136
- instantiateAPI ( )
137
- return _api
138
- }
139
- } )
0 commit comments