@aloma.io/integration-sdk 3.8.11 → 3.8.12
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/.prettierrc.cjs +8 -0
- package/build/builder/index.d.mts +7 -11
- package/build/builder/index.mjs +14 -15
- package/build/builder/runtime-context.d.mts +1 -1
- package/build/builder/runtime-context.mjs +11 -16
- package/build/cli.mjs +38 -38
- package/build/controller/index.d.mts +58 -2
- package/build/controller/index.mjs +68 -11
- package/build/index.d.mts +2 -2
- package/build/index.mjs +2 -2
- package/build/internal/connector/config.d.mts +2 -2
- package/build/internal/connector/config.mjs +10 -10
- package/build/internal/connector/index.d.mts +2 -2
- package/build/internal/connector/index.mjs +4 -4
- package/build/internal/connector/metrics.mjs +6 -6
- package/build/internal/connector/server/index.d.mts +3 -3
- package/build/internal/connector/server/index.mjs +7 -7
- package/build/internal/connector/server/on-connect/decrypt-config.mjs +3 -3
- package/build/internal/connector/server/on-connect/finish-oauth.d.mts +3 -3
- package/build/internal/connector/server/on-connect/finish-oauth.mjs +17 -23
- package/build/internal/connector/server/on-connect/index.d.mts +2 -2
- package/build/internal/connector/server/on-connect/index.mjs +13 -13
- package/build/internal/connector/server/on-connect/make-oauth.d.mts +3 -3
- package/build/internal/connector/server/on-connect/make-oauth.mjs +17 -25
- package/build/internal/connector/server/on-connect/start-oauth.mjs +6 -13
- package/build/internal/connector/server/on-message.mjs +1 -1
- package/build/internal/dispatcher/index.mjs +34 -45
- package/build/internal/fetcher/fetcher.d.mts +1 -1
- package/build/internal/fetcher/fetcher.mjs +11 -15
- package/build/internal/fetcher/oauth-fetcher.d.mts +1 -1
- package/build/internal/fetcher/oauth-fetcher.mjs +7 -7
- package/build/internal/index.d.mts +1 -1
- package/build/internal/index.mjs +1 -1
- package/build/internal/util/index.mjs +5 -5
- package/build/internal/util/jwe/cli.mjs +3 -3
- package/build/internal/util/jwe/index.d.mts +1 -1
- package/build/internal/util/jwe/index.mjs +10 -10
- package/build/internal/websocket/config.d.mts +1 -1
- package/build/internal/websocket/config.mjs +7 -7
- package/build/internal/websocket/connection/constants.mjs +3 -3
- package/build/internal/websocket/connection/index.mjs +8 -8
- package/build/internal/websocket/connection/registration.mjs +6 -6
- package/build/internal/websocket/index.d.mts +2 -2
- package/build/internal/websocket/index.mjs +3 -3
- package/build/internal/websocket/transport/durable.mjs +6 -6
- package/build/internal/websocket/transport/index.d.mts +2 -2
- package/build/internal/websocket/transport/index.mjs +19 -19
- package/build/internal/websocket/transport/packet.mjs +1 -1
- package/build/internal/websocket/transport/processor.mjs +5 -5
- package/build/transform/index.mjs +26 -26
- package/package.json +1 -1
- package/src/builder/index.mts +66 -66
- package/src/builder/runtime-context.mts +17 -43
- package/src/cli.mts +48 -53
- package/src/controller/index.mts +72 -12
- package/src/index.mts +2 -2
- package/src/internal/connector/config.mts +11 -19
- package/src/internal/connector/index.mts +8 -9
- package/src/internal/connector/metrics.mts +7 -7
- package/src/internal/connector/server/index.mts +10 -10
- package/src/internal/connector/server/on-connect/decrypt-config.mts +4 -4
- package/src/internal/connector/server/on-connect/finish-oauth.mts +22 -35
- package/src/internal/connector/server/on-connect/index.mts +27 -27
- package/src/internal/connector/server/on-connect/make-oauth.mts +22 -33
- package/src/internal/connector/server/on-connect/start-oauth.mts +7 -16
- package/src/internal/connector/server/on-message.mts +1 -1
- package/src/internal/dispatcher/index.mts +49 -70
- package/src/internal/fetcher/fetcher.mts +44 -49
- package/src/internal/fetcher/oauth-fetcher.mts +12 -13
- package/src/internal/index.mts +2 -2
- package/src/internal/util/index.mts +8 -11
- package/src/internal/util/jwe/cli.mts +3 -3
- package/src/internal/util/jwe/index.mts +17 -21
- package/src/internal/websocket/config.mjs +8 -12
- package/src/internal/websocket/connection/constants.mjs +3 -3
- package/src/internal/websocket/connection/index.mjs +14 -14
- package/src/internal/websocket/connection/registration.mjs +10 -10
- package/src/internal/websocket/index.mjs +6 -6
- package/src/internal/websocket/transport/durable.mjs +9 -9
- package/src/internal/websocket/transport/index.mjs +28 -32
- package/src/internal/websocket/transport/packet.mjs +4 -4
- package/src/internal/websocket/transport/processor.mjs +9 -12
- package/src/transform/index.mts +28 -41
@@ -1,4 +1,4 @@
|
|
1
|
-
import { unwrap } from
|
1
|
+
import { unwrap } from '../util/index.mjs';
|
2
2
|
/**
|
3
3
|
* http request fetcher
|
4
4
|
*/
|
@@ -7,7 +7,7 @@ export default class Fetcher {
|
|
7
7
|
baseUrl;
|
8
8
|
onResponse;
|
9
9
|
customize0;
|
10
|
-
constructor({ retry = 5, baseUrl, onResponse, customize } = {}) {
|
10
|
+
constructor({ retry = 5, baseUrl, onResponse, customize, } = {}) {
|
11
11
|
this.retry = retry;
|
12
12
|
this.baseUrl = baseUrl;
|
13
13
|
this.onResponse = onResponse;
|
@@ -47,33 +47,29 @@ export default class Fetcher {
|
|
47
47
|
const options0 = { ...options };
|
48
48
|
if (retries == null)
|
49
49
|
retries = local.retry;
|
50
|
-
let theURL = !baseUrl
|
51
|
-
? url
|
52
|
-
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(/\/\/+/gi, "/");
|
50
|
+
let theURL = !baseUrl ? url : `${baseUrl?.endsWith('/') ? baseUrl : baseUrl + '/'}${url}`.replace(/\/\/+/gi, '/');
|
53
51
|
try {
|
54
52
|
options0.url = url;
|
55
53
|
await local.customize(options0, args);
|
56
54
|
url = options0.url;
|
57
55
|
delete options0.url;
|
58
|
-
theURL = !baseUrl
|
59
|
-
? url
|
60
|
-
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(/\/\/+/gi, "/");
|
56
|
+
theURL = !baseUrl ? url : `${baseUrl?.endsWith('/') ? baseUrl : baseUrl + '/'}${url}`.replace(/\/\/+/gi, '/');
|
61
57
|
if (!options0?.headers || !options0?.headers?.Accept) {
|
62
58
|
options0.headers = {
|
63
59
|
...options0.headers,
|
64
|
-
Accept:
|
60
|
+
Accept: 'application/json',
|
65
61
|
};
|
66
62
|
}
|
67
|
-
if (!options0?.headers || !options0?.headers?.[
|
63
|
+
if (!options0?.headers || !options0?.headers?.['Content-type']) {
|
68
64
|
options0.headers = {
|
69
65
|
...options0.headers,
|
70
|
-
|
66
|
+
'Content-type': 'application/json',
|
71
67
|
};
|
72
68
|
}
|
73
|
-
if (!(options0?.method ===
|
69
|
+
if (!(options0?.method === 'GET' || options0?.method === 'HEAD') &&
|
74
70
|
options0?.body &&
|
75
|
-
!(typeof options0.body ===
|
76
|
-
options0?.headers?.[
|
71
|
+
!(typeof options0.body === 'string') &&
|
72
|
+
options0?.headers?.['Content-type'] === 'application/json') {
|
77
73
|
options0.body = JSON.stringify(options0.body);
|
78
74
|
}
|
79
75
|
const timeout = Math.min(options0?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
|
@@ -84,7 +80,7 @@ export default class Fetcher {
|
|
84
80
|
const status = await ret.status;
|
85
81
|
if (status > 399) {
|
86
82
|
const text = await ret.text();
|
87
|
-
const e = new Error(status +
|
83
|
+
const e = new Error(status + ' ' + text);
|
88
84
|
e.status = status;
|
89
85
|
throw e;
|
90
86
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import Fetcher from
|
1
|
+
import Fetcher from './fetcher.mjs';
|
2
2
|
class OAuthFetcher extends Fetcher {
|
3
3
|
oauth;
|
4
4
|
_getToken;
|
@@ -10,7 +10,7 @@ class OAuthFetcher extends Fetcher {
|
|
10
10
|
}
|
11
11
|
async __healthCheck() {
|
12
12
|
if (this.used && !this.oauth.accessToken())
|
13
|
-
throw new Error(
|
13
|
+
throw new Error('no access token');
|
14
14
|
}
|
15
15
|
async getToken(force) {
|
16
16
|
var local = this, oauth = local.oauth;
|
@@ -22,7 +22,7 @@ class OAuthFetcher extends Fetcher {
|
|
22
22
|
const refreshToken = oauth.refreshToken();
|
23
23
|
try {
|
24
24
|
if (!refreshToken) {
|
25
|
-
throw new Error(
|
25
|
+
throw new Error('have no access_token and no refresh_token');
|
26
26
|
}
|
27
27
|
const ret = await oauth.obtainViaRefreshToken(oauth.refreshToken());
|
28
28
|
if (ret.access_token) {
|
@@ -30,7 +30,7 @@ class OAuthFetcher extends Fetcher {
|
|
30
30
|
return ret.access_token;
|
31
31
|
}
|
32
32
|
else {
|
33
|
-
throw new Error(
|
33
|
+
throw new Error('could not obtain access token via refresh token');
|
34
34
|
}
|
35
35
|
}
|
36
36
|
catch (e) {
|
@@ -55,11 +55,11 @@ class OAuthFetcher extends Fetcher {
|
|
55
55
|
}
|
56
56
|
async periodicRefresh() {
|
57
57
|
const local = this, oauth = local.oauth;
|
58
|
-
console.log(
|
58
|
+
console.log('refreshing oauth token, have token', !!oauth.refreshToken());
|
59
59
|
if (!oauth.refreshToken())
|
60
60
|
return;
|
61
61
|
await local.getToken(true);
|
62
|
-
console.log(
|
62
|
+
console.log('refreshed oauth token');
|
63
63
|
}
|
64
64
|
async customize(options, args = {}) {
|
65
65
|
const local = this;
|
@@ -101,7 +101,7 @@ export class OAuth {
|
|
101
101
|
}
|
102
102
|
async periodicRefresh() {
|
103
103
|
const clients = this.clients;
|
104
|
-
console.log(
|
104
|
+
console.log('refreshing oauth clients', clients.length);
|
105
105
|
for (let i = 0; i < clients.length; ++i) {
|
106
106
|
const client = clients[0];
|
107
107
|
await client.periodicRefresh();
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import { Connector } from
|
1
|
+
import { Connector } from './connector/index.mjs';
|
2
2
|
export { Connector };
|
package/build/internal/index.mjs
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import { Connector } from
|
1
|
+
import { Connector } from './connector/index.mjs';
|
2
2
|
export { Connector };
|
@@ -1,13 +1,13 @@
|
|
1
1
|
export const handlePacketError = (packet, e, transport) => {
|
2
2
|
if (!packet.cb()) {
|
3
|
-
console.dir({ msg:
|
3
|
+
console.dir({ msg: 'packet error', e, packet }, { depth: null });
|
4
4
|
return;
|
5
5
|
}
|
6
|
-
transport.send(transport.newPacket({ c: packet.cb(), a: { error:
|
6
|
+
transport.send(transport.newPacket({ c: packet.cb(), a: { error: '' + e } }));
|
7
7
|
};
|
8
8
|
export const reply = (arg, packet, transport) => {
|
9
9
|
if (!packet.cb()) {
|
10
|
-
console.dir({ msg:
|
10
|
+
console.dir({ msg: 'cannot reply to packet without cb', arg, packet }, { depth: null });
|
11
11
|
return;
|
12
12
|
}
|
13
13
|
transport.send(transport.newPacket({ c: packet.cb(), a: { ...arg } }));
|
@@ -24,7 +24,7 @@ export const unwrap = async (ret, options) => {
|
|
24
24
|
if (options?.text)
|
25
25
|
return unwrap0(ret, await ret.text(), options);
|
26
26
|
if (options?.base64) {
|
27
|
-
const base64 = Buffer.from(await ret.arrayBuffer()).toString(
|
27
|
+
const base64 = Buffer.from(await ret.arrayBuffer()).toString('base64');
|
28
28
|
return unwrap0(ret, base64, options);
|
29
29
|
}
|
30
30
|
if (options?.skipResponseBody) {
|
@@ -35,7 +35,7 @@ export const unwrap = async (ret, options) => {
|
|
35
35
|
return unwrap0(ret, JSON.parse(text), options);
|
36
36
|
}
|
37
37
|
catch (e) {
|
38
|
-
throw e +
|
38
|
+
throw e + ' ' + text;
|
39
39
|
}
|
40
40
|
};
|
41
41
|
export const notEmpty = (what, name) => {
|
@@ -1,9 +1,9 @@
|
|
1
|
-
import JWE from
|
1
|
+
import JWE from './index.mjs';
|
2
2
|
const main = async () => {
|
3
3
|
const jwe = new JWE({});
|
4
4
|
await jwe.newPair();
|
5
|
-
console.log(
|
6
|
-
console.log(
|
5
|
+
console.log('PRIVATE_KEY=' + (await jwe.exportPrivateAsBase64()));
|
6
|
+
console.log('PUBLIC_KEY=' + (await jwe.exportPublicAsBase64()));
|
7
7
|
};
|
8
8
|
setTimeout(() => null, 100);
|
9
9
|
main();
|
@@ -1,10 +1,10 @@
|
|
1
|
-
import * as jose from
|
1
|
+
import * as jose from 'jose';
|
2
2
|
class JWE {
|
3
3
|
issuer;
|
4
4
|
algorithm;
|
5
5
|
pair;
|
6
|
-
constructor({ algorithm =
|
7
|
-
this.issuer =
|
6
|
+
constructor({ algorithm = 'PS256' }) {
|
7
|
+
this.issuer = 'home.aloma.io';
|
8
8
|
this.algorithm = algorithm;
|
9
9
|
}
|
10
10
|
async newPair() {
|
@@ -18,11 +18,11 @@ class JWE {
|
|
18
18
|
}
|
19
19
|
async exportPrivateAsBase64() {
|
20
20
|
const pair = await this.exportPair();
|
21
|
-
return Buffer.from(pair.privateKey).toString(
|
21
|
+
return Buffer.from(pair.privateKey).toString('base64');
|
22
22
|
}
|
23
23
|
async exportPublicAsBase64() {
|
24
24
|
const pair = await this.exportPair();
|
25
|
-
return Buffer.from(pair.publicKey).toString(
|
25
|
+
return Buffer.from(pair.publicKey).toString('base64');
|
26
26
|
}
|
27
27
|
async importPair({ publicKey, privateKey, algorithm }) {
|
28
28
|
this.pair = {
|
@@ -32,18 +32,18 @@ class JWE {
|
|
32
32
|
}
|
33
33
|
async importBase64Pair({ publicKey, privateKey, algorithm }) {
|
34
34
|
this.importPair({
|
35
|
-
publicKey: Buffer.from(publicKey,
|
36
|
-
privateKey: Buffer.from(privateKey,
|
35
|
+
publicKey: Buffer.from(publicKey, 'base64').toString(),
|
36
|
+
privateKey: Buffer.from(privateKey, 'base64').toString(),
|
37
37
|
algorithm,
|
38
38
|
});
|
39
39
|
}
|
40
|
-
async encrypt(what, expiration =
|
40
|
+
async encrypt(what, expiration = '7d', audience, algorithm = 'RSA-OAEP-256') {
|
41
41
|
const item = new jose.EncryptJWT({ _data: { ...what } })
|
42
|
-
.setProtectedHeader({ alg: algorithm, enc:
|
42
|
+
.setProtectedHeader({ alg: algorithm, enc: 'A256GCM' })
|
43
43
|
.setIssuedAt()
|
44
44
|
.setIssuer(this.issuer)
|
45
45
|
.setAudience(audience);
|
46
|
-
if (expiration && expiration !==
|
46
|
+
if (expiration && expiration !== 'none')
|
47
47
|
item.setExpirationTime(expiration);
|
48
48
|
return await item.encrypt(this.pair.publicKey);
|
49
49
|
}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import C from
|
2
|
-
import JWE from
|
1
|
+
import C from './connection/constants.mjs';
|
2
|
+
import JWE from '../util/jwe/index.mjs';
|
3
3
|
class Config {
|
4
4
|
constructor({ registrationToken, version, name, id, endpoint, wsEndpoint, privateKey, publicKey, introspect, configSchema, icon, }) {
|
5
5
|
this._token = null;
|
@@ -17,17 +17,17 @@ class Config {
|
|
17
17
|
this._configSchema = configSchema;
|
18
18
|
this._icon = icon;
|
19
19
|
if (!registrationToken)
|
20
|
-
throw new Error(
|
20
|
+
throw new Error('empty registration token (set env.REGISTRATION_TOKEN)');
|
21
21
|
if (!endpoint)
|
22
|
-
throw new Error(
|
22
|
+
throw new Error('empty endpoint (set env.DEVICE_ENDPOINT)');
|
23
23
|
if (!wsEndpoint)
|
24
|
-
throw new Error(
|
24
|
+
throw new Error('empty registration token (set env.WEBSOCKET_ENDPOINT)');
|
25
25
|
if (!this._id || !this._version)
|
26
|
-
throw new Error(
|
26
|
+
throw new Error('need connector id and version');
|
27
27
|
}
|
28
28
|
async validateKeys(algorithm) {
|
29
29
|
if (!this._privateKey || !this._publicKey)
|
30
|
-
throw new Error(
|
30
|
+
throw new Error('need private and public key');
|
31
31
|
await this._jwe.importBase64Pair({
|
32
32
|
publicKey: this._publicKey,
|
33
33
|
privateKey: this._privateKey,
|
@@ -1,9 +1,9 @@
|
|
1
|
-
const AUTHORIZATION =
|
1
|
+
const AUTHORIZATION = 'Authorization';
|
2
2
|
export default {
|
3
3
|
augmentRequest: (what, config) => {
|
4
4
|
what.headers = {
|
5
5
|
...what.headers,
|
6
|
-
|
6
|
+
'User-Agent': config.id() + '/' + config.version(),
|
7
7
|
};
|
8
8
|
what.headers[AUTHORIZATION] = `Connector ${config.token()}`;
|
9
9
|
return what;
|
@@ -11,7 +11,7 @@ export default {
|
|
11
11
|
augmentRegistration: (what, config) => {
|
12
12
|
what.headers = {
|
13
13
|
...what.headers,
|
14
|
-
|
14
|
+
'User-Agent': config.id() + '/' + config.version(),
|
15
15
|
};
|
16
16
|
what.headers[AUTHORIZATION] = `Connector ${config.registrationToken()}`;
|
17
17
|
return what;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { Registration } from
|
2
|
-
import C from
|
1
|
+
import { Registration } from './registration.mjs';
|
2
|
+
import C from './constants.mjs';
|
3
3
|
class Connection {
|
4
4
|
constructor({ config, onStart }) {
|
5
5
|
this.config = config;
|
@@ -8,10 +8,10 @@ class Connection {
|
|
8
8
|
async start() {
|
9
9
|
var local = this, config = local.config;
|
10
10
|
try {
|
11
|
-
const response = await fetch(config.url() +
|
12
|
-
method:
|
11
|
+
const response = await fetch(config.url() + 'connect', C.augmentRequest({
|
12
|
+
method: 'POST',
|
13
13
|
body: JSON.stringify({}),
|
14
|
-
headers: {
|
14
|
+
headers: { 'Content-Type': 'application/json' },
|
15
15
|
signal: AbortSignal.timeout(60 * 1000),
|
16
16
|
}, config));
|
17
17
|
if (response.status === 401) {
|
@@ -37,10 +37,10 @@ class Connection {
|
|
37
37
|
}
|
38
38
|
async close() {
|
39
39
|
try {
|
40
|
-
await fetch(this.config.url() +
|
41
|
-
method:
|
40
|
+
await fetch(this.config.url() + 'disconnect', C.augmentRequest({
|
41
|
+
method: 'POST',
|
42
42
|
body: JSON.stringify({}),
|
43
|
-
headers: {
|
43
|
+
headers: { 'Content-Type': 'application/json' },
|
44
44
|
signal: AbortSignal.timeout(60 * 1000),
|
45
45
|
}, this.config));
|
46
46
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import C from
|
1
|
+
import C from './constants.mjs';
|
2
2
|
class Registration {
|
3
3
|
constructor(config) {
|
4
4
|
this.config = config;
|
@@ -9,10 +9,10 @@ class Registration {
|
|
9
9
|
const configSchema = config.configSchema();
|
10
10
|
const intro = await config.introspect();
|
11
11
|
const icon = config.icon();
|
12
|
-
const response = await fetch(config.url() +
|
13
|
-
method:
|
12
|
+
const response = await fetch(config.url() + 'register', C.augmentRegistration({
|
13
|
+
method: 'POST',
|
14
14
|
body: JSON.stringify({
|
15
|
-
deployment: process.env.DEPLOYMENT ||
|
15
|
+
deployment: process.env.DEPLOYMENT || '',
|
16
16
|
name: config.name(),
|
17
17
|
version: config.version(),
|
18
18
|
id: config.id(),
|
@@ -20,12 +20,12 @@ class Registration {
|
|
20
20
|
schema: { configSchema, introspect: intro },
|
21
21
|
icon,
|
22
22
|
}),
|
23
|
-
headers: {
|
23
|
+
headers: { 'Content-Type': 'application/json' },
|
24
24
|
signal: AbortSignal.timeout(60 * 1000),
|
25
25
|
}, config));
|
26
26
|
if (response.status === 200)
|
27
27
|
return (await response.json()).key;
|
28
|
-
throw new Error(
|
28
|
+
throw new Error('authentication failed');
|
29
29
|
}
|
30
30
|
}
|
31
31
|
export { Registration };
|
@@ -12,5 +12,5 @@ export class WebsocketConnector {
|
|
12
12
|
close(): Promise<void>;
|
13
13
|
leaving(): Promise<void>;
|
14
14
|
}
|
15
|
-
import { Transport } from
|
16
|
-
import { Connection } from
|
15
|
+
import { Transport } from './transport/index.mjs';
|
16
|
+
import { Connection } from './connection/index.mjs';
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import WebSocket from
|
2
|
-
import { Connection } from
|
3
|
-
import { Transport } from
|
1
|
+
import WebSocket from 'ws';
|
2
|
+
import { Connection } from './connection/index.mjs';
|
3
|
+
import { Transport } from './transport/index.mjs';
|
4
4
|
class WebsocketConnector {
|
5
5
|
constructor({ config, onMessage, onConnect }) {
|
6
6
|
var local = this;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import WebSocket from
|
1
|
+
import WebSocket from 'ws';
|
2
2
|
class DurableWebsocket {
|
3
3
|
constructor({ endpoint, secret, onConnect, onMessage }) {
|
4
4
|
this.endpoint = endpoint;
|
@@ -26,7 +26,7 @@ class DurableWebsocket {
|
|
26
26
|
rejectUnauthorized: false,
|
27
27
|
headers: { Authorization: `Bearer ${local.secret}` },
|
28
28
|
}));
|
29
|
-
ws.on(
|
29
|
+
ws.on('open', () => {
|
30
30
|
local.connecting = false;
|
31
31
|
local.fails = 0;
|
32
32
|
var item;
|
@@ -35,15 +35,15 @@ class DurableWebsocket {
|
|
35
35
|
}
|
36
36
|
local.onConnect(local);
|
37
37
|
});
|
38
|
-
ws.on(
|
38
|
+
ws.on('message', (message) => {
|
39
39
|
setImmediate(() => local.onMessage(JSON.parse(message)));
|
40
40
|
});
|
41
|
-
ws.on(
|
41
|
+
ws.on('error', (message) => {
|
42
42
|
if (local.fails > 50)
|
43
|
-
console.log(
|
43
|
+
console.log('error:', message.message);
|
44
44
|
++local.fails;
|
45
45
|
});
|
46
|
-
ws.on(
|
46
|
+
ws.on('close', (message) => {
|
47
47
|
local.connecting = false;
|
48
48
|
if (!local.closed)
|
49
49
|
setTimeout(() => local.start(), 5000);
|
@@ -33,5 +33,5 @@ export class Transport {
|
|
33
33
|
close(): void;
|
34
34
|
connected: boolean | undefined;
|
35
35
|
}
|
36
|
-
import { DurableWebsocket } from
|
37
|
-
import { Packet } from
|
36
|
+
import { DurableWebsocket } from './durable.mjs';
|
37
|
+
import { Packet } from './packet.mjs';
|
@@ -1,9 +1,9 @@
|
|
1
|
-
import { init } from
|
2
|
-
import C from
|
1
|
+
import { init } from '@paralleldrive/cuid2';
|
2
|
+
import C from '../connection/constants.mjs';
|
3
3
|
const cuid = init({ length: 32 });
|
4
|
-
import WebSocket from
|
5
|
-
import { DurableWebsocket } from
|
6
|
-
import { Callback, Packet } from
|
4
|
+
import WebSocket from 'ws';
|
5
|
+
import { DurableWebsocket } from './durable.mjs';
|
6
|
+
import { Callback, Packet } from './packet.mjs';
|
7
7
|
const cleanInterval = 45 * 1000;
|
8
8
|
const pingInterval = 30 * 1000;
|
9
9
|
class Transport {
|
@@ -36,7 +36,7 @@ class Transport {
|
|
36
36
|
local.ws.send(JSON.stringify({ p: packets }));
|
37
37
|
}
|
38
38
|
catch (e) {
|
39
|
-
console.log(
|
39
|
+
console.log('could not send packets ', e);
|
40
40
|
packets.forEach((packet) => local.packets.unshift(packet));
|
41
41
|
}
|
42
42
|
}
|
@@ -46,30 +46,30 @@ class Transport {
|
|
46
46
|
return;
|
47
47
|
local.close();
|
48
48
|
this.running = true;
|
49
|
-
const ws = (local.ws = new WebSocket(config.wsUrl(), [
|
49
|
+
const ws = (local.ws = new WebSocket(config.wsUrl(), ['connector'], C.augmentRequest({ headers: {} }, config)));
|
50
50
|
ws.onPing = function () {
|
51
51
|
clearTimeout(this.pingTimeout);
|
52
52
|
this.pingTimeout = setTimeout(() => {
|
53
|
-
console.log(
|
53
|
+
console.log('terminating ws');
|
54
54
|
if (local.running)
|
55
55
|
this.terminate();
|
56
56
|
}, 30000 + 15000);
|
57
57
|
};
|
58
|
-
ws.on(
|
59
|
-
console.log(
|
58
|
+
ws.on('open', () => {
|
59
|
+
console.log('transport connected');
|
60
60
|
local.connected = true;
|
61
61
|
ws.onPing();
|
62
62
|
local.pinger = setInterval(() => ws.ping(() => null), pingInterval);
|
63
63
|
local.onConnect(local);
|
64
64
|
});
|
65
|
-
ws.on(
|
65
|
+
ws.on('message', (message) => {
|
66
66
|
setTimeout(() => local.onMessages(JSON.parse(message)), 0);
|
67
67
|
});
|
68
|
-
ws.on(
|
69
|
-
ws.on(
|
70
|
-
console.log(
|
68
|
+
ws.on('ping', () => ws.onPing());
|
69
|
+
ws.on('error', (message) => {
|
70
|
+
console.log('error:', message);
|
71
71
|
});
|
72
|
-
ws.on(
|
72
|
+
ws.on('close', (message) => {
|
73
73
|
local.connected = false;
|
74
74
|
clearInterval(local.pinger);
|
75
75
|
if (local.running)
|
@@ -100,7 +100,7 @@ class Transport {
|
|
100
100
|
this.callbacks[packet.cb()].cb(packet.args());
|
101
101
|
}
|
102
102
|
catch (e) {
|
103
|
-
console.log(
|
103
|
+
console.log('error processing packet', e, packet);
|
104
104
|
}
|
105
105
|
finally {
|
106
106
|
delete this.callbacks[packet.cb()];
|
@@ -125,12 +125,12 @@ class Transport {
|
|
125
125
|
if (!cb)
|
126
126
|
return;
|
127
127
|
if (cb.created < then) {
|
128
|
-
console.log(
|
128
|
+
console.log('callback timeout', key);
|
129
129
|
try {
|
130
|
-
cb.cb({ error:
|
130
|
+
cb.cb({ error: 'timeout' });
|
131
131
|
}
|
132
132
|
catch (e) {
|
133
|
-
console.log(
|
133
|
+
console.log('error while callback', key, cb, e);
|
134
134
|
}
|
135
135
|
delete local.callbacks[key];
|
136
136
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Packet, Callback } from
|
1
|
+
import { Packet, Callback } from './packet.mjs';
|
2
2
|
class Processor {
|
3
3
|
constructor({ transport, processPacket }) {
|
4
4
|
var local = this;
|
@@ -31,24 +31,24 @@ class Processor {
|
|
31
31
|
callbacks[packet.cb()](packet.args());
|
32
32
|
}
|
33
33
|
catch (e) {
|
34
|
-
console.log(
|
34
|
+
console.log('error in callback', callbacks[packet.cb()], packet);
|
35
35
|
}
|
36
36
|
delete local.transport.callbacks[packet.cb()];
|
37
37
|
}
|
38
38
|
else if (packet.event()) {
|
39
|
-
console.log(
|
39
|
+
console.log('handle event packet', packet);
|
40
40
|
}
|
41
41
|
else {
|
42
42
|
try {
|
43
43
|
const result = await local._processPacket(packet);
|
44
44
|
const reply = local.transport.newPacket({});
|
45
|
-
reply.method(
|
45
|
+
reply.method('connector.reply');
|
46
46
|
reply.cb(original.cb());
|
47
47
|
reply.args({ ...result });
|
48
48
|
local.transport.send(reply);
|
49
49
|
}
|
50
50
|
catch (e) {
|
51
|
-
console.log(
|
51
|
+
console.log('error processing packet', e, packet);
|
52
52
|
}
|
53
53
|
}
|
54
54
|
}
|