Skip to content

Commit daeac37

Browse files
committed
@aws-sdk/client-apigatewaymanagementapi / aws-sdk/clients/apigatewaymanagementapi@aws-lite/apigatewaymanagementapi
1 parent 9a1ad47 commit daeac37

File tree

2 files changed

+119
-91
lines changed

2 files changed

+119
-91
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"author": "Brian LeRoux <b@brian.io>",
2626
"license": "Apache-2.0",
2727
"dependencies": {
28-
"@aws-lite/client": "^0.8.1",
28+
"@aws-lite/apigatewaymanagementapi": "^0.0.2",
29+
"@aws-lite/client": "^0.10.2",
2930
"@aws-lite/dynamodb": "^0.2.1",
3031
"@aws-lite/sns": "^0.0.0",
3132
"@aws-lite/sqs": "^0.0.0",
@@ -42,8 +43,7 @@
4243
"@architect/asap": "^6.0.3",
4344
"@architect/eslint-config": "2.1.1",
4445
"@architect/req-res-fixtures": "git+https://github.com/architect/req-res-fixtures.git",
45-
"@architect/sandbox": "^5.8.2",
46-
"@aws-sdk/client-apigatewaymanagementapi": "^3.319.0",
46+
"@architect/sandbox": "^5.8.3",
4747
"@aws-sdk/client-dynamodb": "^3.319.0",
4848
"@aws-sdk/lib-dynamodb": "^3.319.0",
4949
"@smithy/node-http-handler": "^2.1.7",

src/ws/index.js

Lines changed: 116 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,57 @@
1-
let { isNode18, useAWS } = require('../lib')
2-
let _api, _send, _close, _info
1+
let { getAwsClient, useAWS } = require('../lib')
2+
let client, ApiUrl
33

44
function instantiateAPI () {
5-
if (_api) return
5+
return new Promise((res, rej) => {
6+
if (client) res(client)
67

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
815

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(/^ws/, '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+
}
3627
})
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+
}
5030

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)
6143

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+
})
7249
}
7350

7451
/**
7552
* arc.ws.send
7653
*
77-
* publish web socket events
54+
* Publish WebSocket events
7855
*
7956
* @param {Object} params
8057
* @param {String} params.id - the ws connection id (required)
@@ -83,57 +60,108 @@ function instantiateAPI () {
8360
* @returns {Promise} - returned if no callback is supplied
8461
*/
8562
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+
})
9188
}
9289

9390
/**
9491
* arc.ws.close
9592
*
96-
* publish web socket events
93+
* Terminate a WebSocket client connection
9794
*
9895
* @param {Object} params
9996
* @param {String} params.id - the ws connection id (required)
10097
* @param {Function} callback - a node style errback (optional)
10198
* @returns {Promise} - returned if no callback is supplied
10299
*/
103100
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+
})
108124
}
109125

110126
/**
111127
* arc.ws.info
112128
*
113-
* publish web socket events
129+
* Get info on a WebSocket client connection
114130
*
115131
* @param {Object} params
116132
* @param {String} params.id - the ws connection id (required)
117133
* @param {Function} callback - a node style errback (optional)
118134
* @returns {Promise} - returned if no callback is supplied
119135
*/
120136
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+
})
125160
}
126161

127162
module.exports = {
163+
_api,
128164
send,
129165
close,
130166
info,
131167
}
132-
133-
Object.defineProperty(module.exports, '_api', {
134-
enumerable: true,
135-
get () {
136-
instantiateAPI()
137-
return _api
138-
}
139-
})

0 commit comments

Comments
 (0)