@aloma.io/integration-sdk 3.6.6 → 3.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/builder/index.d.mts +1 -1
- package/build/builder/index.mjs +12 -16
- package/build/builder/runtime-context.mjs +3 -2
- package/build/cli.mjs +15 -4
- package/build/internal/dispatcher/index.d.mts +11 -17
- package/build/internal/dispatcher/index.mjs +12 -1
- package/build/internal/fetcher/fetcher.d.mts +15 -0
- package/build/internal/fetcher/fetcher.mjs +97 -0
- package/build/internal/fetcher/index.d.mts +1 -0
- package/build/internal/fetcher/index.mjs +1 -0
- package/build/internal/fetcher/oauth-fetcher.d.mts +32 -0
- package/build/internal/fetcher/oauth-fetcher.mjs +114 -0
- package/build/internal/index.d.mts +8 -7
- package/build/internal/index.mjs +14 -241
- package/build/internal/util/index.d.mts +5 -0
- package/build/internal/util/index.mjs +45 -0
- package/build/internal/util/jwe/index.d.mts +5 -8
- package/build/internal/util/jwe/index.mjs +3 -0
- package/build/transform/index.d.mts +5 -0
- package/build/transform/index.mjs +79 -0
- package/package.json +1 -1
- package/src/builder/index.mts +13 -17
- package/src/builder/runtime-context.mts +4 -1
- package/src/cli.mts +19 -4
- package/src/internal/dispatcher/{index.mjs → index.mts} +14 -2
- package/src/internal/fetcher/fetcher.mts +126 -0
- package/src/internal/fetcher/oauth-fetcher.mts +147 -0
- package/src/internal/{index.mjs → index.mts} +18 -317
- package/src/internal/util/index.mts +57 -0
- package/src/internal/util/jwe/{index.mjs → index.mts} +7 -4
- /package/src/internal/util/jwe/{cli.mjs → cli.mts} +0 -0
- /package/src/{builder/transform → transform}/index.mts +0 -0
@@ -0,0 +1,126 @@
|
|
1
|
+
import { unwrap } from "../util/index.mjs";
|
2
|
+
|
3
|
+
export default class Fetcher {
|
4
|
+
retry: number;
|
5
|
+
baseUrl: any;
|
6
|
+
onResponse: any;
|
7
|
+
customize0: any;
|
8
|
+
constructor({ retry = 5, baseUrl, onResponse, customize }) {
|
9
|
+
this.retry = retry;
|
10
|
+
this.baseUrl = baseUrl;
|
11
|
+
this.onResponse = onResponse;
|
12
|
+
if (customize) this.customize0 = customize;
|
13
|
+
}
|
14
|
+
|
15
|
+
async customize(options = {}, args = {}) {
|
16
|
+
if (this.customize0) await this.customize0(options, args);
|
17
|
+
}
|
18
|
+
|
19
|
+
async onError(e: any, url: string, options: any, retries: number, args: any, rateLimit?) {
|
20
|
+
var local = this;
|
21
|
+
|
22
|
+
return new Promise((resolve, reject) => {
|
23
|
+
setTimeout(
|
24
|
+
async () => {
|
25
|
+
try {
|
26
|
+
resolve(await local.fetch(url, options, retries, args));
|
27
|
+
} catch (e) {
|
28
|
+
reject(e);
|
29
|
+
}
|
30
|
+
},
|
31
|
+
rateLimit ? 10000 : 500,
|
32
|
+
);
|
33
|
+
});
|
34
|
+
}
|
35
|
+
|
36
|
+
async fetch(url: string, options: any = {}, retries: number, args: any = {}) {
|
37
|
+
var local = this,
|
38
|
+
baseUrl = local.baseUrl;
|
39
|
+
|
40
|
+
if (retries == null) retries = local.retry;
|
41
|
+
|
42
|
+
let theURL = !baseUrl
|
43
|
+
? url
|
44
|
+
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(
|
45
|
+
/\/\/+/gi,
|
46
|
+
"/",
|
47
|
+
);
|
48
|
+
|
49
|
+
try {
|
50
|
+
options.url = url;
|
51
|
+
await local.customize(options, args);
|
52
|
+
|
53
|
+
url = options.url;
|
54
|
+
delete options.url;
|
55
|
+
|
56
|
+
theURL = !baseUrl
|
57
|
+
? url
|
58
|
+
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(
|
59
|
+
/\/\/+/gi,
|
60
|
+
"/",
|
61
|
+
);
|
62
|
+
|
63
|
+
if (!options?.headers || !options?.headers?.Accept) {
|
64
|
+
options.headers = {
|
65
|
+
...options.headers,
|
66
|
+
Accept: "application/json",
|
67
|
+
};
|
68
|
+
}
|
69
|
+
|
70
|
+
if (!options?.headers || !options?.headers?.["Content-type"]) {
|
71
|
+
options.headers = {
|
72
|
+
...options.headers,
|
73
|
+
"Content-type": "application/json",
|
74
|
+
};
|
75
|
+
}
|
76
|
+
|
77
|
+
if (
|
78
|
+
!(options?.method === "GET" || options?.method === "HEAD") &&
|
79
|
+
options?.body &&
|
80
|
+
!(typeof options.body === "string") &&
|
81
|
+
options?.headers?.["Content-type"] === "application/json"
|
82
|
+
) {
|
83
|
+
options.body = JSON.stringify(options.body);
|
84
|
+
}
|
85
|
+
|
86
|
+
const timeout = Math.min(options?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
|
87
|
+
const ret = await fetch(theURL, {
|
88
|
+
...options,
|
89
|
+
signal: AbortSignal.timeout(timeout),
|
90
|
+
});
|
91
|
+
const status = await ret.status;
|
92
|
+
|
93
|
+
if (status > 399) {
|
94
|
+
const text = await ret.text();
|
95
|
+
const e: any = new Error(status + " " + text);
|
96
|
+
|
97
|
+
e.status = status;
|
98
|
+
throw e;
|
99
|
+
}
|
100
|
+
|
101
|
+
if (local.onResponse) {
|
102
|
+
await local.onResponse(ret);
|
103
|
+
}
|
104
|
+
|
105
|
+
return unwrap(ret, options);
|
106
|
+
} catch (e: any) {
|
107
|
+
// too many requests
|
108
|
+
if (e.status === 429) {
|
109
|
+
return local.onError(e, url, options, retries, args, true);
|
110
|
+
}
|
111
|
+
|
112
|
+
// bad request
|
113
|
+
if (e.status === 400 || e.status === 422) {
|
114
|
+
throw e;
|
115
|
+
}
|
116
|
+
|
117
|
+
--retries;
|
118
|
+
|
119
|
+
console.log(theURL, e);
|
120
|
+
|
121
|
+
if (retries <= 0) throw e;
|
122
|
+
|
123
|
+
return local.onError(e, url, options, retries, args);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
@@ -0,0 +1,147 @@
|
|
1
|
+
import Fetcher from "./fetcher.mjs";
|
2
|
+
|
3
|
+
class OAuthFetcher extends Fetcher {
|
4
|
+
oauth: any;
|
5
|
+
_getToken: any;
|
6
|
+
constructor({ oauth, retry = 5, getToken, baseUrl, onResponse, customize }) {
|
7
|
+
super({ retry, baseUrl, onResponse, customize });
|
8
|
+
|
9
|
+
this.oauth = oauth;
|
10
|
+
this._getToken = getToken;
|
11
|
+
}
|
12
|
+
|
13
|
+
async getToken(force) {
|
14
|
+
var local = this,
|
15
|
+
oauth = local.oauth;
|
16
|
+
|
17
|
+
if (local._getToken) return local._getToken(force);
|
18
|
+
|
19
|
+
if (!force && oauth.accessToken()) return oauth.accessToken();
|
20
|
+
|
21
|
+
const refreshToken = oauth.refreshToken();
|
22
|
+
|
23
|
+
try {
|
24
|
+
if (!refreshToken) {
|
25
|
+
throw new Error("have no access_token and no refresh_token");
|
26
|
+
}
|
27
|
+
|
28
|
+
const ret = await oauth.obtainViaRefreshToken(oauth.refreshToken());
|
29
|
+
|
30
|
+
if (ret.access_token) {
|
31
|
+
oauth.update(ret.access_token, ret.refresh_token);
|
32
|
+
|
33
|
+
return ret.access_token;
|
34
|
+
} else {
|
35
|
+
throw new Error("could not obtain access token via refresh token");
|
36
|
+
}
|
37
|
+
} catch (e) {
|
38
|
+
oauth.invalidate(e);
|
39
|
+
|
40
|
+
throw e;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
async onError(e, url, options, retries, args, rateLimit) {
|
45
|
+
var local = this;
|
46
|
+
|
47
|
+
return new Promise((resolve, reject) => {
|
48
|
+
setTimeout(
|
49
|
+
async () => {
|
50
|
+
try {
|
51
|
+
resolve(
|
52
|
+
await local.fetch(url, options, retries, {
|
53
|
+
forceTokenRefresh: e.status === 401,
|
54
|
+
}),
|
55
|
+
);
|
56
|
+
} catch (e) {
|
57
|
+
reject(e);
|
58
|
+
}
|
59
|
+
},
|
60
|
+
rateLimit ? 10000 : 500,
|
61
|
+
);
|
62
|
+
});
|
63
|
+
}
|
64
|
+
|
65
|
+
async periodicRefresh() {
|
66
|
+
const local = this,
|
67
|
+
oauth = local.oauth;
|
68
|
+
|
69
|
+
console.log("refreshing oauth token, have token", !!oauth.refreshToken());
|
70
|
+
if (!oauth.refreshToken()) return;
|
71
|
+
|
72
|
+
await local.getToken(true);
|
73
|
+
|
74
|
+
console.log("refreshed oauth token");
|
75
|
+
}
|
76
|
+
|
77
|
+
async customize(options: any, args: any = {}) {
|
78
|
+
const local = this;
|
79
|
+
|
80
|
+
if (this.customize0) await this.customize0(options, args);
|
81
|
+
|
82
|
+
const token = await local.getToken(args.forceTokenRefresh);
|
83
|
+
|
84
|
+
options.headers = {
|
85
|
+
...options.headers,
|
86
|
+
Authorization: `Bearer ${token}`,
|
87
|
+
};
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
export class OAuth {
|
92
|
+
_data: any;
|
93
|
+
saveOAuthResult: any;
|
94
|
+
obtainViaRefreshToken: any;
|
95
|
+
clients: any[];
|
96
|
+
constructor(data, saveOAuthResult, getRefreshToken) {
|
97
|
+
this._data = data || {};
|
98
|
+
this.saveOAuthResult = saveOAuthResult;
|
99
|
+
this.obtainViaRefreshToken = getRefreshToken;
|
100
|
+
this.clients = [];
|
101
|
+
}
|
102
|
+
|
103
|
+
data() {
|
104
|
+
return this._data;
|
105
|
+
}
|
106
|
+
|
107
|
+
accessToken() {
|
108
|
+
return this._data.access_token;
|
109
|
+
}
|
110
|
+
|
111
|
+
refreshToken() {
|
112
|
+
return this._data.refresh_token;
|
113
|
+
}
|
114
|
+
|
115
|
+
async update(accessToken, refreshToken) {
|
116
|
+
this._data.access_token = accessToken;
|
117
|
+
|
118
|
+
if (refreshToken) {
|
119
|
+
this._data.refresh_token = refreshToken;
|
120
|
+
}
|
121
|
+
|
122
|
+
await this.saveOAuthResult(this._data);
|
123
|
+
}
|
124
|
+
|
125
|
+
async periodicRefresh() {
|
126
|
+
const clients = this.clients;
|
127
|
+
|
128
|
+
console.log("refreshing oauth clients", clients.length);
|
129
|
+
|
130
|
+
for (let i = 0; i < clients.length; ++i) {
|
131
|
+
const client = clients[0];
|
132
|
+
|
133
|
+
await client.periodicRefresh();
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
async invalidate(err) {
|
138
|
+
if (true) return;
|
139
|
+
//if (this._data.access_token === "invalid") return;
|
140
|
+
}
|
141
|
+
|
142
|
+
getClient(arg: any = {}) {
|
143
|
+
const client = new OAuthFetcher({ ...arg, oauth: this });
|
144
|
+
this.clients.push(client);
|
145
|
+
return client;
|
146
|
+
}
|
147
|
+
}
|
@@ -2,324 +2,20 @@ import { init } from "@paralleldrive/cuid2";
|
|
2
2
|
import express from "express";
|
3
3
|
import PromClient from "prom-client";
|
4
4
|
import { Dispatcher } from "./dispatcher/index.mjs";
|
5
|
+
import Fetcher from "./fetcher/fetcher.mjs";
|
6
|
+
import { OAuth } from "./fetcher/oauth-fetcher.mjs";
|
7
|
+
import { handlePacketError, reply } from "./util/index.mjs";
|
5
8
|
import JWE from "./util/jwe/index.mjs";
|
6
9
|
import { Config } from "./websocket/config.mjs";
|
7
10
|
import { WebsocketConnector } from "./websocket/index.mjs";
|
8
11
|
const cuid = init({ length: 32 });
|
9
12
|
|
10
|
-
const handlePacketError = (packet, e, transport) => {
|
11
|
-
if (!packet.cb()) {
|
12
|
-
console.dir({ msg: "packet error", e, packet }, { depth: null });
|
13
|
-
return;
|
14
|
-
}
|
15
|
-
|
16
|
-
transport.send(transport.newPacket({ c: packet.cb(), a: { error: "" + e } }));
|
17
|
-
};
|
18
|
-
|
19
|
-
const reply = (arg, packet, transport) => {
|
20
|
-
if (!packet.cb()) {
|
21
|
-
console.dir(
|
22
|
-
{ msg: "cannot reply to packet without cb", arg, packet },
|
23
|
-
{ depth: null },
|
24
|
-
);
|
25
|
-
return;
|
26
|
-
}
|
27
|
-
|
28
|
-
transport.send(transport.newPacket({ c: packet.cb(), a: { ...arg } }));
|
29
|
-
};
|
30
|
-
|
31
|
-
const unwrap0 = (ret, body, options) => {
|
32
|
-
if (options?.bodyOnly === false) {
|
33
|
-
return { status: ret.status, headers: ret.headers, body };
|
34
|
-
} else {
|
35
|
-
return body;
|
36
|
-
}
|
37
|
-
};
|
38
|
-
|
39
|
-
const unwrap = async (ret, options) => {
|
40
|
-
if (options?.text) return unwrap0(ret, await ret.text(), options);
|
41
|
-
if (options?.base64) {
|
42
|
-
const base64 = Buffer.from(await ret.arrayBuffer()).toString("base64");
|
43
|
-
|
44
|
-
return unwrap0(ret, base64, options);
|
45
|
-
}
|
46
|
-
|
47
|
-
if (options?.skipResponseBody) {
|
48
|
-
return { status: ret.status, headers: ret.headers};
|
49
|
-
}
|
50
|
-
|
51
|
-
const text = await ret.text();
|
52
|
-
|
53
|
-
try {
|
54
|
-
return unwrap0(ret, JSON.parse(text), options);
|
55
|
-
} catch (e) {
|
56
|
-
throw e + " " + text;
|
57
|
-
}
|
58
|
-
};
|
59
|
-
|
60
|
-
class Fetcher {
|
61
|
-
constructor({ retry = 5, baseUrl, onResponse, customize }) {
|
62
|
-
this.retry = retry;
|
63
|
-
this.baseUrl = baseUrl;
|
64
|
-
this.onResponse = onResponse;
|
65
|
-
if (customize) this.customize0 = customize;
|
66
|
-
}
|
67
|
-
|
68
|
-
async customize(options = {}, args = {}) {
|
69
|
-
if (this.customize0) await this.customize0(options, args);
|
70
|
-
}
|
71
|
-
|
72
|
-
async onError(e, url, options, retries, args, rateLimit) {
|
73
|
-
var local = this;
|
74
|
-
|
75
|
-
return new Promise((resolve, reject) => {
|
76
|
-
setTimeout(
|
77
|
-
async () => {
|
78
|
-
try {
|
79
|
-
resolve(await local.fetch(url, options, retries, args));
|
80
|
-
} catch (e) {
|
81
|
-
reject(e);
|
82
|
-
}
|
83
|
-
},
|
84
|
-
rateLimit ? 10000 : 500,
|
85
|
-
);
|
86
|
-
});
|
87
|
-
}
|
88
|
-
|
89
|
-
async fetch(url, options = {}, retries, args = {}) {
|
90
|
-
var local = this,
|
91
|
-
baseUrl = local.baseUrl;
|
92
|
-
|
93
|
-
if (retries == null) retries = local.retry;
|
94
|
-
|
95
|
-
let theURL = !baseUrl
|
96
|
-
? url
|
97
|
-
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(
|
98
|
-
/\/\/+/gi,
|
99
|
-
"/",
|
100
|
-
);
|
101
|
-
|
102
|
-
try {
|
103
|
-
options.url = url;
|
104
|
-
await local.customize(options, args);
|
105
|
-
|
106
|
-
url = options.url;
|
107
|
-
delete options.url;
|
108
|
-
|
109
|
-
theURL = !baseUrl
|
110
|
-
? url
|
111
|
-
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(
|
112
|
-
/\/\/+/gi,
|
113
|
-
"/",
|
114
|
-
);
|
115
|
-
|
116
|
-
if (!options?.headers || !options?.headers?.Accept) {
|
117
|
-
options.headers = {
|
118
|
-
...options.headers,
|
119
|
-
Accept: "application/json",
|
120
|
-
};
|
121
|
-
}
|
122
|
-
|
123
|
-
if (!options?.headers || !options?.headers?.["Content-type"]) {
|
124
|
-
options.headers = {
|
125
|
-
...options.headers,
|
126
|
-
"Content-type": "application/json",
|
127
|
-
};
|
128
|
-
}
|
129
|
-
|
130
|
-
if (
|
131
|
-
!(options?.method === "GET" || options?.method === "HEAD") &&
|
132
|
-
options?.body &&
|
133
|
-
!(typeof options.body === "string") &&
|
134
|
-
options?.headers?.["Content-type"] === "application/json"
|
135
|
-
) {
|
136
|
-
options.body = JSON.stringify(options.body);
|
137
|
-
}
|
138
|
-
|
139
|
-
const timeout = Math.min(options?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
|
140
|
-
const ret = await fetch(theURL, {
|
141
|
-
...options,
|
142
|
-
signal: AbortSignal.timeout(timeout),
|
143
|
-
});
|
144
|
-
const status = await ret.status;
|
145
|
-
|
146
|
-
if (status > 399) {
|
147
|
-
const text = await ret.text();
|
148
|
-
const e = new Error(status + " " + text);
|
149
|
-
|
150
|
-
e.status = status;
|
151
|
-
throw e;
|
152
|
-
}
|
153
|
-
|
154
|
-
if (local.onResponse) {
|
155
|
-
await local.onResponse(ret);
|
156
|
-
}
|
157
|
-
|
158
|
-
return unwrap(ret, options);
|
159
|
-
} catch (e) {
|
160
|
-
// too many requests
|
161
|
-
if (e.status === 429) {
|
162
|
-
return local.onError(e, url, options, retries, args, true);
|
163
|
-
}
|
164
|
-
|
165
|
-
// bad request
|
166
|
-
if (e.status === 400 || e.status === 422) {
|
167
|
-
throw e;
|
168
|
-
}
|
169
|
-
|
170
|
-
--retries;
|
171
|
-
|
172
|
-
console.log(theURL, e);
|
173
|
-
|
174
|
-
if (retries <= 0) throw e;
|
175
|
-
|
176
|
-
return local.onError(e, url, options, retries, args);
|
177
|
-
}
|
178
|
-
}
|
179
|
-
}
|
180
|
-
|
181
|
-
class OAuthFetcher extends Fetcher {
|
182
|
-
constructor({ oauth, retry = 5, getToken, baseUrl, onResponse, customize }) {
|
183
|
-
super({ retry, baseUrl, onResponse, customize });
|
184
|
-
|
185
|
-
this.oauth = oauth;
|
186
|
-
this._getToken = getToken;
|
187
|
-
}
|
188
|
-
|
189
|
-
async getToken(force) {
|
190
|
-
var local = this,
|
191
|
-
oauth = local.oauth;
|
192
|
-
|
193
|
-
if (local._getToken) return local._getToken(force);
|
194
|
-
|
195
|
-
if (!force && oauth.accessToken()) return oauth.accessToken();
|
196
|
-
|
197
|
-
const refreshToken = oauth.refreshToken();
|
198
|
-
|
199
|
-
try {
|
200
|
-
if (!refreshToken) {
|
201
|
-
throw new Error("have no access_token and no refresh_token");
|
202
|
-
}
|
203
|
-
|
204
|
-
const ret = await oauth.obtainViaRefreshToken(oauth.refreshToken());
|
205
|
-
|
206
|
-
if (ret.access_token) {
|
207
|
-
oauth.update(ret.access_token, ret.refresh_token);
|
208
|
-
|
209
|
-
return ret.access_token;
|
210
|
-
} else {
|
211
|
-
throw new Error("could not obtain access token via refresh token");
|
212
|
-
}
|
213
|
-
} catch (e) {
|
214
|
-
oauth.invalidate(e);
|
215
|
-
|
216
|
-
throw e;
|
217
|
-
}
|
218
|
-
}
|
219
|
-
|
220
|
-
async onError(e, url, options, retries, args, rateLimit) {
|
221
|
-
var local = this;
|
222
|
-
|
223
|
-
return new Promise((resolve, reject) => {
|
224
|
-
setTimeout(
|
225
|
-
async () => {
|
226
|
-
try {
|
227
|
-
resolve(
|
228
|
-
await local.fetch(url, options, retries, {
|
229
|
-
forceTokenRefresh: e.status === 401,
|
230
|
-
}),
|
231
|
-
);
|
232
|
-
} catch (e) {
|
233
|
-
reject(e);
|
234
|
-
}
|
235
|
-
},
|
236
|
-
rateLimit ? 10000 : 500,
|
237
|
-
);
|
238
|
-
});
|
239
|
-
}
|
240
|
-
|
241
|
-
async periodicRefresh() {
|
242
|
-
const local = this,
|
243
|
-
oauth = local.oauth;
|
244
|
-
|
245
|
-
console.log("refreshing oauth token, have token", !!oauth.refreshToken());
|
246
|
-
if (!oauth.refreshToken()) return;
|
247
|
-
|
248
|
-
await local.getToken(true);
|
249
|
-
|
250
|
-
console.log("refreshed oauth token");
|
251
|
-
}
|
252
|
-
|
253
|
-
async customize(options, args = {}) {
|
254
|
-
const local = this;
|
255
|
-
|
256
|
-
if (this.customize0) await this.customize0(options, args);
|
257
|
-
|
258
|
-
const token = await local.getToken(args.forceTokenRefresh);
|
259
|
-
|
260
|
-
options.headers = {
|
261
|
-
...options.headers,
|
262
|
-
Authorization: `Bearer ${token}`,
|
263
|
-
};
|
264
|
-
}
|
265
|
-
}
|
266
|
-
|
267
|
-
class OAuth {
|
268
|
-
constructor(data, saveOAuthResult, getRefreshToken) {
|
269
|
-
var local = this;
|
270
|
-
this._data = data || {};
|
271
|
-
this.saveOAuthResult = saveOAuthResult;
|
272
|
-
this.obtainViaRefreshToken = getRefreshToken;
|
273
|
-
this.clients = [];
|
274
|
-
}
|
275
|
-
|
276
|
-
data() {
|
277
|
-
return this._data;
|
278
|
-
}
|
279
|
-
|
280
|
-
accessToken() {
|
281
|
-
return this._data.access_token;
|
282
|
-
}
|
283
|
-
|
284
|
-
refreshToken() {
|
285
|
-
return this._data.refresh_token;
|
286
|
-
}
|
287
|
-
|
288
|
-
async update(accessToken, refreshToken) {
|
289
|
-
this._data.access_token = accessToken;
|
290
|
-
|
291
|
-
if (refreshToken) {
|
292
|
-
this._data.refresh_token = refreshToken;
|
293
|
-
}
|
294
|
-
|
295
|
-
await this.saveOAuthResult(this._data);
|
296
|
-
}
|
297
|
-
|
298
|
-
async periodicRefresh() {
|
299
|
-
const clients = this.clients;
|
300
|
-
|
301
|
-
console.log("refreshing oauth clients", clients.length);
|
302
|
-
|
303
|
-
for (let i = 0; i < clients.length; ++i) {
|
304
|
-
const client = clients[0];
|
305
|
-
|
306
|
-
await client.periodicRefresh();
|
307
|
-
}
|
308
|
-
}
|
309
|
-
|
310
|
-
async invalidate(err) {
|
311
|
-
if (true) return;
|
312
|
-
//if (this._data.access_token === "invalid") return;
|
313
|
-
}
|
314
|
-
|
315
|
-
getClient(arg = {}) {
|
316
|
-
const client = new OAuthFetcher({ ...arg, oauth: this });
|
317
|
-
this.clients.push(client);
|
318
|
-
return client;
|
319
|
-
}
|
320
|
-
}
|
321
|
-
|
322
13
|
class Connector {
|
14
|
+
id: any;
|
15
|
+
version: any;
|
16
|
+
name: any;
|
17
|
+
icon: any;
|
18
|
+
dispatcher?: Dispatcher;
|
323
19
|
constructor({ version, id, name, icon }) {
|
324
20
|
this.id = id;
|
325
21
|
this.version = version;
|
@@ -365,7 +61,7 @@ class Connector {
|
|
365
61
|
makeMetricsServer(makeMetrics()).listen(4050, "0.0.0.0");
|
366
62
|
|
367
63
|
const { processPacket, start, introspect, configSchema } =
|
368
|
-
this.dispatcher
|
64
|
+
this.dispatcher!.build();
|
369
65
|
|
370
66
|
const config = new Config({
|
371
67
|
id: this.id,
|
@@ -415,8 +111,9 @@ ${text}
|
|
415
111
|
const server = new WebsocketConnector({
|
416
112
|
config,
|
417
113
|
onConnect: (transport) => {
|
418
|
-
|
419
|
-
|
114
|
+
// @ts-ignore
|
115
|
+
local.dispatcher!.onConfig = async function (secrets) {
|
116
|
+
const decrypted: any = {};
|
420
117
|
const fields = configSchema().fields;
|
421
118
|
|
422
119
|
const keys = Object.keys(secrets);
|
@@ -438,6 +135,7 @@ ${text}
|
|
438
135
|
}
|
439
136
|
}
|
440
137
|
|
138
|
+
// @ts-ignore
|
441
139
|
this.startOAuth = async function (args) {
|
442
140
|
if (!this._oauth) throw new Error("oauth not configured");
|
443
141
|
|
@@ -463,6 +161,7 @@ ${text}
|
|
463
161
|
};
|
464
162
|
};
|
465
163
|
|
164
|
+
// @ts-ignore
|
466
165
|
this.finishOAuth = async function (arg) {
|
467
166
|
var that = this;
|
468
167
|
|
@@ -504,7 +203,7 @@ ${text}
|
|
504
203
|
body.code_verifier = arg.codeVerifier;
|
505
204
|
}
|
506
205
|
|
507
|
-
let headers = {
|
206
|
+
let headers: any = {
|
508
207
|
"Content-Type":
|
509
208
|
"application/x-www-form-urlencoded;charset=UTF-8",
|
510
209
|
Accept: "application/json",
|
@@ -640,9 +339,11 @@ ${text}
|
|
640
339
|
: null;
|
641
340
|
|
642
341
|
if (theOAuth) {
|
342
|
+
// @ts-ignore
|
643
343
|
clearInterval(this._refreshOAuthToken);
|
644
344
|
|
645
345
|
if (!(this._oauth.noPeriodicTokenRefresh === false)) {
|
346
|
+
// @ts-ignore
|
646
347
|
this._refreshOAuthToken = setInterval(
|
647
348
|
async () => {
|
648
349
|
try {
|
@@ -769,7 +470,7 @@ ${text}
|
|
769
470
|
await new Promise((resolve) => {
|
770
471
|
setTimeout(async () => {
|
771
472
|
await server.close();
|
772
|
-
resolve();
|
473
|
+
resolve(null);
|
773
474
|
}, 10000);
|
774
475
|
});
|
775
476
|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
export const handlePacketError = (packet, e, transport) => {
|
3
|
+
if (!packet.cb()) {
|
4
|
+
console.dir({ msg: "packet error", e, packet }, { depth: null });
|
5
|
+
return;
|
6
|
+
}
|
7
|
+
|
8
|
+
transport.send(transport.newPacket({ c: packet.cb(), a: { error: "" + e } }));
|
9
|
+
};
|
10
|
+
|
11
|
+
export const reply = (arg, packet, transport) => {
|
12
|
+
if (!packet.cb()) {
|
13
|
+
console.dir(
|
14
|
+
{ msg: "cannot reply to packet without cb", arg, packet },
|
15
|
+
{ depth: null },
|
16
|
+
);
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
|
20
|
+
transport.send(transport.newPacket({ c: packet.cb(), a: { ...arg } }));
|
21
|
+
};
|
22
|
+
|
23
|
+
export const unwrap0 = (ret, body, options) => {
|
24
|
+
if (options?.bodyOnly === false) {
|
25
|
+
return { status: ret.status, headers: ret.headers, body };
|
26
|
+
} else {
|
27
|
+
return body;
|
28
|
+
}
|
29
|
+
};
|
30
|
+
|
31
|
+
export const unwrap = async (ret, options) => {
|
32
|
+
if (options?.text) return unwrap0(ret, await ret.text(), options);
|
33
|
+
if (options?.base64) {
|
34
|
+
const base64 = Buffer.from(await ret.arrayBuffer()).toString("base64");
|
35
|
+
|
36
|
+
return unwrap0(ret, base64, options);
|
37
|
+
}
|
38
|
+
|
39
|
+
if (options?.skipResponseBody) {
|
40
|
+
return { status: ret.status, headers: ret.headers};
|
41
|
+
}
|
42
|
+
|
43
|
+
const text = await ret.text();
|
44
|
+
|
45
|
+
try {
|
46
|
+
return unwrap0(ret, JSON.parse(text), options);
|
47
|
+
} catch (e) {
|
48
|
+
throw e + " " + text;
|
49
|
+
}
|
50
|
+
};
|
51
|
+
|
52
|
+
|
53
|
+
export const notEmpty = (what, name) => {
|
54
|
+
if (!what?.trim()) throw new Error(`${name} cannot be empty`);
|
55
|
+
|
56
|
+
return what;
|
57
|
+
};
|