-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
128 lines (116 loc) · 2.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
let { getAwsClient, useAWS } = require('../lib')
let client, ApiUrl
function instantiateAPI () {
return new Promise((res, rej) => {
if (client) res(client)
getAwsClient({
plugins: [ import('@aws-lite/apigatewaymanagementapi') ],
}, (err, _client) => {
if (err) rej(err)
else {
client = _client
let { ARC_WSS_URL, ARC_SANDBOX } = process.env
if (useAWS()) {
ApiUrl = ARC_WSS_URL
}
else {
let { ports } = JSON.parse(ARC_SANDBOX)
let port = ports._arc
if (!port) throw ReferenceError('Architect internal port not found')
ApiUrl = `http://localhost:${port}/_arc/ws`
}
res(client)
}
})
})
}
function _api (callback) {
if (callback) instantiateAPI()
.then(client => callback(null, client.ApiGatewayManagementApi))
.catch(callback)
else return new Promise((res, rej) => {
instantiateAPI()
.then(client => res(client.ApiGatewayManagementApi))
.catch(rej)
})
}
function send ({ id, payload }, callback) {
if (callback) instantiateAPI()
.then(client => {
client.ApiGatewayManagementApi.PostToConnection({
ApiUrl,
ConnectionId: id,
Data: payload,
})
.then(result => callback(null, result))
.catch(callback)
})
.catch(callback)
else return new Promise((res, rej) => {
instantiateAPI()
.then(client => {
client.ApiGatewayManagementApi.PostToConnection({
ApiUrl,
ConnectionId: id,
Data: payload,
})
.then(result => res(result))
.catch(rej)
})
.catch(rej)
})
}
function close ({ id }, callback) {
if (callback) instantiateAPI()
.then(client => {
client.ApiGatewayManagementApi.DeleteConnection({
ApiUrl,
ConnectionId: id,
})
.then(result => callback(null, result))
.catch(callback)
})
.catch(callback)
else return new Promise((res, rej) => {
instantiateAPI()
.then(client => {
client.ApiGatewayManagementApi.DeleteConnection({
ApiUrl,
ConnectionId: id,
})
.then(result => res(result))
.catch(rej)
})
.catch(rej)
})
}
function info ({ id }, callback) {
if (callback) instantiateAPI()
.then(client => {
client.ApiGatewayManagementApi.GetConnection({
ApiUrl,
ConnectionId: id,
})
.then(result => callback(null, result))
.catch(callback)
})
.catch(callback)
else return new Promise((res, rej) => {
instantiateAPI()
.then(client => {
client.ApiGatewayManagementApi.GetConnection({
ApiUrl,
ConnectionId: id,
})
.then(result => res(result))
.catch(rej)
})
.catch(rej)
})
}
module.exports = {
_api,
send,
close,
info,
}