@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,27 +1,24 @@
|
|
1
1
|
export const handlePacketError = (packet, e, transport) => {
|
2
2
|
if (!packet.cb()) {
|
3
|
-
console.dir({
|
3
|
+
console.dir({msg: 'packet error', e, packet}, {depth: null});
|
4
4
|
return;
|
5
5
|
}
|
6
6
|
|
7
|
-
transport.send(transport.newPacket({
|
7
|
+
transport.send(transport.newPacket({c: packet.cb(), a: {error: '' + e}}));
|
8
8
|
};
|
9
9
|
|
10
10
|
export const reply = (arg, packet, transport) => {
|
11
11
|
if (!packet.cb()) {
|
12
|
-
console.dir(
|
13
|
-
{ msg: "cannot reply to packet without cb", arg, packet },
|
14
|
-
{ depth: null },
|
15
|
-
);
|
12
|
+
console.dir({msg: 'cannot reply to packet without cb', arg, packet}, {depth: null});
|
16
13
|
return;
|
17
14
|
}
|
18
15
|
|
19
|
-
transport.send(transport.newPacket({
|
16
|
+
transport.send(transport.newPacket({c: packet.cb(), a: {...arg}}));
|
20
17
|
};
|
21
18
|
|
22
19
|
export const unwrap0 = (ret, body, options) => {
|
23
20
|
if (options?.bodyOnly === false) {
|
24
|
-
return {
|
21
|
+
return {status: ret.status, headers: ret.headers, body};
|
25
22
|
} else {
|
26
23
|
return body;
|
27
24
|
}
|
@@ -30,13 +27,13 @@ export const unwrap0 = (ret, body, options) => {
|
|
30
27
|
export const unwrap = async (ret, options) => {
|
31
28
|
if (options?.text) return unwrap0(ret, await ret.text(), options);
|
32
29
|
if (options?.base64) {
|
33
|
-
const base64 = Buffer.from(await ret.arrayBuffer()).toString(
|
30
|
+
const base64 = Buffer.from(await ret.arrayBuffer()).toString('base64');
|
34
31
|
|
35
32
|
return unwrap0(ret, base64, options);
|
36
33
|
}
|
37
34
|
|
38
35
|
if (options?.skipResponseBody) {
|
39
|
-
return {
|
36
|
+
return {status: ret.status, headers: ret.headers};
|
40
37
|
}
|
41
38
|
|
42
39
|
const text = await ret.text();
|
@@ -44,7 +41,7 @@ export const unwrap = async (ret, options) => {
|
|
44
41
|
try {
|
45
42
|
return unwrap0(ret, JSON.parse(text), options);
|
46
43
|
} catch (e) {
|
47
|
-
throw e +
|
44
|
+
throw e + ' ' + text;
|
48
45
|
}
|
49
46
|
};
|
50
47
|
|
@@ -1,11 +1,11 @@
|
|
1
|
-
import JWE from
|
1
|
+
import JWE from './index.mjs';
|
2
2
|
|
3
3
|
const main = async () => {
|
4
4
|
const jwe = new JWE({});
|
5
5
|
await jwe.newPair();
|
6
6
|
|
7
|
-
console.log(
|
8
|
-
console.log(
|
7
|
+
console.log('PRIVATE_KEY=' + (await jwe.exportPrivateAsBase64()));
|
8
|
+
console.log('PUBLIC_KEY=' + (await jwe.exportPublicAsBase64()));
|
9
9
|
};
|
10
10
|
|
11
11
|
setTimeout(() => null, 100);
|
@@ -1,11 +1,11 @@
|
|
1
|
-
import * as jose from
|
1
|
+
import * as jose from 'jose';
|
2
2
|
|
3
3
|
class JWE {
|
4
4
|
issuer: string;
|
5
5
|
algorithm: string;
|
6
6
|
pair?: jose.GenerateKeyPairResult<jose.KeyLike>;
|
7
|
-
constructor({
|
8
|
-
this.issuer =
|
7
|
+
constructor({algorithm = 'PS256'}) {
|
8
|
+
this.issuer = 'home.aloma.io';
|
9
9
|
this.algorithm = algorithm;
|
10
10
|
}
|
11
11
|
|
@@ -23,51 +23,47 @@ class JWE {
|
|
23
23
|
async exportPrivateAsBase64() {
|
24
24
|
const pair = await this.exportPair();
|
25
25
|
|
26
|
-
return Buffer.from(pair.privateKey).toString(
|
26
|
+
return Buffer.from(pair.privateKey).toString('base64');
|
27
27
|
}
|
28
28
|
|
29
29
|
async exportPublicAsBase64() {
|
30
30
|
const pair = await this.exportPair();
|
31
31
|
|
32
|
-
return Buffer.from(pair.publicKey).toString(
|
32
|
+
return Buffer.from(pair.publicKey).toString('base64');
|
33
33
|
}
|
34
34
|
|
35
|
-
async importPair({
|
35
|
+
async importPair({publicKey, privateKey, algorithm}) {
|
36
36
|
this.pair = {
|
37
37
|
publicKey: await jose.importSPKI(publicKey, algorithm),
|
38
38
|
privateKey: await jose.importPKCS8(privateKey, algorithm),
|
39
39
|
};
|
40
40
|
}
|
41
41
|
|
42
|
-
async importBase64Pair({
|
42
|
+
async importBase64Pair({publicKey, privateKey, algorithm}) {
|
43
43
|
this.importPair({
|
44
|
-
publicKey: Buffer.from(publicKey,
|
45
|
-
privateKey: Buffer.from(privateKey,
|
44
|
+
publicKey: Buffer.from(publicKey, 'base64').toString(),
|
45
|
+
privateKey: Buffer.from(privateKey, 'base64').toString(),
|
46
46
|
algorithm,
|
47
47
|
});
|
48
48
|
}
|
49
49
|
|
50
|
-
async encrypt(what, expiration =
|
51
|
-
const item = new jose.EncryptJWT({
|
52
|
-
.setProtectedHeader({
|
50
|
+
async encrypt(what, expiration = '7d', audience, algorithm = 'RSA-OAEP-256') {
|
51
|
+
const item = new jose.EncryptJWT({_data: {...what}})
|
52
|
+
.setProtectedHeader({alg: algorithm, enc: 'A256GCM'})
|
53
53
|
.setIssuedAt()
|
54
54
|
.setIssuer(this.issuer)
|
55
55
|
.setAudience(audience);
|
56
56
|
|
57
|
-
if (expiration && expiration !==
|
57
|
+
if (expiration && expiration !== 'none') item.setExpirationTime(expiration);
|
58
58
|
|
59
59
|
return await item.encrypt(this.pair!.publicKey);
|
60
60
|
}
|
61
61
|
|
62
62
|
async decrypt(what, audience) {
|
63
|
-
const {
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
issuer: this.issuer,
|
68
|
-
audience,
|
69
|
-
},
|
70
|
-
);
|
63
|
+
const {payload, protectedHeader} = await jose.jwtDecrypt(what, this.pair!.privateKey, {
|
64
|
+
issuer: this.issuer,
|
65
|
+
audience,
|
66
|
+
});
|
71
67
|
|
72
68
|
return payload._data;
|
73
69
|
}
|
@@ -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
|
|
4
4
|
class Config {
|
5
5
|
constructor({
|
@@ -30,19 +30,15 @@ class Config {
|
|
30
30
|
this._configSchema = configSchema;
|
31
31
|
this._icon = icon;
|
32
32
|
|
33
|
-
if (!registrationToken)
|
34
|
-
|
35
|
-
if (!
|
36
|
-
if (!wsEndpoint)
|
37
|
-
throw new Error("empty registration token (set env.WEBSOCKET_ENDPOINT)");
|
33
|
+
if (!registrationToken) throw new Error('empty registration token (set env.REGISTRATION_TOKEN)');
|
34
|
+
if (!endpoint) throw new Error('empty endpoint (set env.DEVICE_ENDPOINT)');
|
35
|
+
if (!wsEndpoint) throw new Error('empty registration token (set env.WEBSOCKET_ENDPOINT)');
|
38
36
|
|
39
|
-
if (!this._id || !this._version)
|
40
|
-
throw new Error("need connector id and version");
|
37
|
+
if (!this._id || !this._version) throw new Error('need connector id and version');
|
41
38
|
}
|
42
39
|
|
43
40
|
async validateKeys(algorithm) {
|
44
|
-
if (!this._privateKey || !this._publicKey)
|
45
|
-
throw new Error("need private and public key");
|
41
|
+
if (!this._privateKey || !this._publicKey) throw new Error('need private and public key');
|
46
42
|
|
47
43
|
await this._jwe.importBase64Pair({
|
48
44
|
publicKey: this._publicKey,
|
@@ -110,4 +106,4 @@ class Config {
|
|
110
106
|
}
|
111
107
|
}
|
112
108
|
|
113
|
-
export {
|
109
|
+
export {Config};
|
@@ -1,10 +1,10 @@
|
|
1
|
-
const AUTHORIZATION =
|
1
|
+
const AUTHORIZATION = 'Authorization';
|
2
2
|
|
3
3
|
export default {
|
4
4
|
augmentRequest: (what, config) => {
|
5
5
|
what.headers = {
|
6
6
|
...what.headers,
|
7
|
-
|
7
|
+
'User-Agent': config.id() + '/' + config.version(),
|
8
8
|
};
|
9
9
|
|
10
10
|
what.headers[AUTHORIZATION] = `Connector ${config.token()}`;
|
@@ -15,7 +15,7 @@ export default {
|
|
15
15
|
augmentRegistration: (what, config) => {
|
16
16
|
what.headers = {
|
17
17
|
...what.headers,
|
18
|
-
|
18
|
+
'User-Agent': config.id() + '/' + config.version(),
|
19
19
|
};
|
20
20
|
|
21
21
|
what.headers[AUTHORIZATION] = `Connector ${config.registrationToken()}`;
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
import C from
|
1
|
+
import {Registration} from './registration.mjs';
|
2
|
+
import C from './constants.mjs';
|
3
3
|
|
4
4
|
class Connection {
|
5
|
-
constructor({
|
5
|
+
constructor({config, onStart}) {
|
6
6
|
this.config = config;
|
7
7
|
this.onStart = onStart;
|
8
8
|
}
|
@@ -13,16 +13,16 @@ class Connection {
|
|
13
13
|
|
14
14
|
try {
|
15
15
|
const response = await fetch(
|
16
|
-
config.url() +
|
16
|
+
config.url() + 'connect',
|
17
17
|
C.augmentRequest(
|
18
18
|
{
|
19
|
-
method:
|
19
|
+
method: 'POST',
|
20
20
|
body: JSON.stringify({}),
|
21
|
-
headers: {
|
21
|
+
headers: {'Content-Type': 'application/json'},
|
22
22
|
signal: AbortSignal.timeout(60 * 1000),
|
23
23
|
},
|
24
|
-
config
|
25
|
-
)
|
24
|
+
config
|
25
|
+
)
|
26
26
|
);
|
27
27
|
|
28
28
|
if (response.status === 401) {
|
@@ -51,16 +51,16 @@ class Connection {
|
|
51
51
|
async close() {
|
52
52
|
try {
|
53
53
|
await fetch(
|
54
|
-
this.config.url() +
|
54
|
+
this.config.url() + 'disconnect',
|
55
55
|
C.augmentRequest(
|
56
56
|
{
|
57
|
-
method:
|
57
|
+
method: 'POST',
|
58
58
|
body: JSON.stringify({}),
|
59
|
-
headers: {
|
59
|
+
headers: {'Content-Type': 'application/json'},
|
60
60
|
signal: AbortSignal.timeout(60 * 1000),
|
61
61
|
},
|
62
|
-
this.config
|
63
|
-
)
|
62
|
+
this.config
|
63
|
+
)
|
64
64
|
);
|
65
65
|
} catch (e) {
|
66
66
|
// blank
|
@@ -68,4 +68,4 @@ class Connection {
|
|
68
68
|
}
|
69
69
|
}
|
70
70
|
|
71
|
-
export {
|
71
|
+
export {Connection};
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import C from
|
1
|
+
import C from './constants.mjs';
|
2
2
|
|
3
3
|
class Registration {
|
4
4
|
constructor(config) {
|
@@ -13,30 +13,30 @@ class Registration {
|
|
13
13
|
const icon = config.icon();
|
14
14
|
|
15
15
|
const response = await fetch(
|
16
|
-
config.url() +
|
16
|
+
config.url() + 'register',
|
17
17
|
C.augmentRegistration(
|
18
18
|
{
|
19
|
-
method:
|
19
|
+
method: 'POST',
|
20
20
|
body: JSON.stringify({
|
21
|
-
deployment: process.env.DEPLOYMENT ||
|
21
|
+
deployment: process.env.DEPLOYMENT || '',
|
22
22
|
name: config.name(),
|
23
23
|
version: config.version(),
|
24
24
|
id: config.id(),
|
25
25
|
publicKey: config.publicKey(),
|
26
|
-
schema: {
|
26
|
+
schema: {configSchema, introspect: intro},
|
27
27
|
icon,
|
28
28
|
}),
|
29
|
-
headers: {
|
29
|
+
headers: {'Content-Type': 'application/json'},
|
30
30
|
signal: AbortSignal.timeout(60 * 1000),
|
31
31
|
},
|
32
|
-
config
|
33
|
-
)
|
32
|
+
config
|
33
|
+
)
|
34
34
|
);
|
35
35
|
|
36
36
|
if (response.status === 200) return (await response.json()).key;
|
37
37
|
|
38
|
-
throw new Error(
|
38
|
+
throw new Error('authentication failed');
|
39
39
|
}
|
40
40
|
}
|
41
41
|
|
42
|
-
export {
|
42
|
+
export {Registration};
|
@@ -1,12 +1,12 @@
|
|
1
|
-
import WebSocket from
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import WebSocket from 'ws';
|
2
|
+
import {Connection} from './connection/index.mjs';
|
3
|
+
import {Transport} from './transport/index.mjs';
|
4
4
|
|
5
5
|
class WebsocketConnector {
|
6
|
-
constructor({
|
6
|
+
constructor({config, onMessage, onConnect}) {
|
7
7
|
var local = this;
|
8
8
|
this.config = config;
|
9
|
-
this.transport = new Transport({
|
9
|
+
this.transport = new Transport({config, onMessage, onConnect});
|
10
10
|
}
|
11
11
|
|
12
12
|
async start() {
|
@@ -43,4 +43,4 @@ class WebsocketConnector {
|
|
43
43
|
}
|
44
44
|
}
|
45
45
|
|
46
|
-
export {
|
46
|
+
export {WebsocketConnector};
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import WebSocket from
|
1
|
+
import WebSocket from 'ws';
|
2
2
|
|
3
3
|
class DurableWebsocket {
|
4
|
-
constructor({
|
4
|
+
constructor({endpoint, secret, onConnect, onMessage}) {
|
5
5
|
this.endpoint = endpoint;
|
6
6
|
this.secret = secret;
|
7
7
|
this.onConnect = onConnect;
|
@@ -27,10 +27,10 @@ class DurableWebsocket {
|
|
27
27
|
|
28
28
|
const ws = (local.ws = new WebSocket(local.endpoint, [], {
|
29
29
|
rejectUnauthorized: false,
|
30
|
-
headers: {
|
30
|
+
headers: {Authorization: `Bearer ${local.secret}`},
|
31
31
|
}));
|
32
32
|
|
33
|
-
ws.on(
|
33
|
+
ws.on('open', () => {
|
34
34
|
local.connecting = false;
|
35
35
|
local.fails = 0;
|
36
36
|
|
@@ -43,17 +43,17 @@ class DurableWebsocket {
|
|
43
43
|
local.onConnect(local);
|
44
44
|
});
|
45
45
|
|
46
|
-
ws.on(
|
46
|
+
ws.on('message', (message) => {
|
47
47
|
setImmediate(() => local.onMessage(JSON.parse(message)));
|
48
48
|
});
|
49
49
|
|
50
|
-
ws.on(
|
51
|
-
if (local.fails > 50) console.log(
|
50
|
+
ws.on('error', (message) => {
|
51
|
+
if (local.fails > 50) console.log('error:', message.message);
|
52
52
|
|
53
53
|
++local.fails;
|
54
54
|
});
|
55
55
|
|
56
|
-
ws.on(
|
56
|
+
ws.on('close', (message) => {
|
57
57
|
local.connecting = false;
|
58
58
|
|
59
59
|
if (!local.closed) setTimeout(() => local.start(), 5000);
|
@@ -68,4 +68,4 @@ class DurableWebsocket {
|
|
68
68
|
}
|
69
69
|
}
|
70
70
|
|
71
|
-
export {
|
71
|
+
export {DurableWebsocket};
|
@@ -1,16 +1,16 @@
|
|
1
|
-
import {
|
2
|
-
import C from
|
3
|
-
const cuid = init({
|
1
|
+
import {init} from '@paralleldrive/cuid2';
|
2
|
+
import C from '../connection/constants.mjs';
|
3
|
+
const cuid = init({length: 32});
|
4
4
|
|
5
|
-
import WebSocket from
|
6
|
-
import {
|
7
|
-
import {
|
5
|
+
import WebSocket from 'ws';
|
6
|
+
import {DurableWebsocket} from './durable.mjs';
|
7
|
+
import {Callback, Packet} from './packet.mjs';
|
8
8
|
|
9
9
|
const cleanInterval = 45 * 1000;
|
10
10
|
const pingInterval = 30 * 1000;
|
11
11
|
|
12
12
|
class Transport {
|
13
|
-
constructor({
|
13
|
+
constructor({config, onMessage, onConnect}) {
|
14
14
|
var local = this;
|
15
15
|
|
16
16
|
this.config = config;
|
@@ -45,9 +45,9 @@ class Transport {
|
|
45
45
|
if (!packets.length) return;
|
46
46
|
|
47
47
|
try {
|
48
|
-
local.ws.send(JSON.stringify({
|
48
|
+
local.ws.send(JSON.stringify({p: packets}));
|
49
49
|
} catch (e) {
|
50
|
-
console.log(
|
50
|
+
console.log('could not send packets ', e);
|
51
51
|
packets.forEach((packet) => local.packets.unshift(packet));
|
52
52
|
}
|
53
53
|
}
|
@@ -62,22 +62,18 @@ class Transport {
|
|
62
62
|
|
63
63
|
this.running = true;
|
64
64
|
|
65
|
-
const ws = (local.ws = new WebSocket(
|
66
|
-
config.wsUrl(),
|
67
|
-
["connector"],
|
68
|
-
C.augmentRequest({ headers: {} }, config),
|
69
|
-
));
|
65
|
+
const ws = (local.ws = new WebSocket(config.wsUrl(), ['connector'], C.augmentRequest({headers: {}}, config)));
|
70
66
|
|
71
67
|
ws.onPing = function () {
|
72
68
|
clearTimeout(this.pingTimeout);
|
73
69
|
this.pingTimeout = setTimeout(() => {
|
74
|
-
console.log(
|
70
|
+
console.log('terminating ws');
|
75
71
|
if (local.running) this.terminate();
|
76
72
|
}, 30000 + 15000);
|
77
73
|
};
|
78
74
|
|
79
|
-
ws.on(
|
80
|
-
console.log(
|
75
|
+
ws.on('open', () => {
|
76
|
+
console.log('transport connected');
|
81
77
|
local.connected = true;
|
82
78
|
ws.onPing();
|
83
79
|
local.pinger = setInterval(() => ws.ping(() => null), pingInterval);
|
@@ -85,17 +81,17 @@ class Transport {
|
|
85
81
|
local.onConnect(local);
|
86
82
|
});
|
87
83
|
|
88
|
-
ws.on(
|
84
|
+
ws.on('message', (message) => {
|
89
85
|
setTimeout(() => local.onMessages(JSON.parse(message)), 0);
|
90
86
|
});
|
91
87
|
|
92
|
-
ws.on(
|
88
|
+
ws.on('ping', () => ws.onPing());
|
93
89
|
|
94
|
-
ws.on(
|
95
|
-
console.log(
|
90
|
+
ws.on('error', (message) => {
|
91
|
+
console.log('error:', message);
|
96
92
|
});
|
97
93
|
|
98
|
-
ws.on(
|
94
|
+
ws.on('close', (message) => {
|
99
95
|
local.connected = false;
|
100
96
|
clearInterval(local.pinger);
|
101
97
|
|
@@ -103,8 +99,8 @@ class Transport {
|
|
103
99
|
});
|
104
100
|
}
|
105
101
|
|
106
|
-
newDurableWebsocket({
|
107
|
-
return new DurableWebsocket({
|
102
|
+
newDurableWebsocket({endpoint, secret, onConnect, onMessage}) {
|
103
|
+
return new DurableWebsocket({endpoint, secret, onConnect, onMessage});
|
108
104
|
}
|
109
105
|
|
110
106
|
send(packet) {
|
@@ -131,7 +127,7 @@ class Transport {
|
|
131
127
|
try {
|
132
128
|
this.callbacks[packet.cb()].cb(packet.args());
|
133
129
|
} catch (e) {
|
134
|
-
console.log(
|
130
|
+
console.log('error processing packet', e, packet);
|
135
131
|
} finally {
|
136
132
|
delete this.callbacks[packet.cb()];
|
137
133
|
}
|
@@ -143,10 +139,10 @@ class Transport {
|
|
143
139
|
}
|
144
140
|
|
145
141
|
newPacket(data, cb, cbKey) {
|
146
|
-
const packet = new Packet({
|
142
|
+
const packet = new Packet({...data});
|
147
143
|
|
148
144
|
if (cb) {
|
149
|
-
this.callbacks[cbKey || packet.id()] = new Callback({
|
145
|
+
this.callbacks[cbKey || packet.id()] = new Callback({cb});
|
150
146
|
packet.cb(cbKey || packet.id());
|
151
147
|
}
|
152
148
|
|
@@ -155,7 +151,7 @@ class Transport {
|
|
155
151
|
|
156
152
|
clean() {
|
157
153
|
var local = this;
|
158
|
-
const cbs = {
|
154
|
+
const cbs = {...this.callbacks},
|
159
155
|
then = Date.now() - 5 * 60 * 1000;
|
160
156
|
|
161
157
|
Object.keys(cbs).forEach((key) => {
|
@@ -163,12 +159,12 @@ class Transport {
|
|
163
159
|
if (!cb) return;
|
164
160
|
|
165
161
|
if (cb.created < then) {
|
166
|
-
console.log(
|
162
|
+
console.log('callback timeout', key);
|
167
163
|
|
168
164
|
try {
|
169
|
-
cb.cb({
|
165
|
+
cb.cb({error: 'timeout'});
|
170
166
|
} catch (e) {
|
171
|
-
console.log(
|
167
|
+
console.log('error while callback', key, cb, e);
|
172
168
|
}
|
173
169
|
|
174
170
|
delete local.callbacks[key];
|
@@ -196,4 +192,4 @@ class Transport {
|
|
196
192
|
}
|
197
193
|
}
|
198
194
|
|
199
|
-
export {
|
195
|
+
export {Transport};
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
const cuid = init({
|
1
|
+
import {init} from '@paralleldrive/cuid2';
|
2
|
+
const cuid = init({length: 32});
|
3
3
|
|
4
4
|
class Packet {
|
5
5
|
constructor(data = {}) {
|
@@ -45,10 +45,10 @@ class Packet {
|
|
45
45
|
}
|
46
46
|
|
47
47
|
class Callback {
|
48
|
-
constructor({
|
48
|
+
constructor({cb}) {
|
49
49
|
this.cb = cb;
|
50
50
|
this.created = Date.now();
|
51
51
|
}
|
52
52
|
}
|
53
53
|
|
54
|
-
export {
|
54
|
+
export {Callback, Packet};
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import {
|
1
|
+
import {Packet, Callback} from './packet.mjs';
|
2
2
|
|
3
3
|
class Processor {
|
4
|
-
constructor({
|
4
|
+
constructor({transport, processPacket}) {
|
5
5
|
var local = this;
|
6
6
|
|
7
7
|
this.transport = transport;
|
@@ -26,10 +26,7 @@ class Processor {
|
|
26
26
|
var local = this;
|
27
27
|
|
28
28
|
if (packet.args().packet) {
|
29
|
-
await local.processPacket0(
|
30
|
-
local.transport.newPacket(packet.args().packet),
|
31
|
-
packet,
|
32
|
-
);
|
29
|
+
await local.processPacket0(local.transport.newPacket(packet.args().packet), packet);
|
33
30
|
} else {
|
34
31
|
await local.processPacket0(packet, packet);
|
35
32
|
}
|
@@ -43,27 +40,27 @@ class Processor {
|
|
43
40
|
try {
|
44
41
|
callbacks[packet.cb()](packet.args());
|
45
42
|
} catch (e) {
|
46
|
-
console.log(
|
43
|
+
console.log('error in callback', callbacks[packet.cb()], packet);
|
47
44
|
}
|
48
45
|
|
49
46
|
delete local.transport.callbacks[packet.cb()];
|
50
47
|
} else if (packet.event()) {
|
51
|
-
console.log(
|
48
|
+
console.log('handle event packet', packet);
|
52
49
|
} else {
|
53
50
|
try {
|
54
51
|
const result = await local._processPacket(packet);
|
55
52
|
const reply = local.transport.newPacket({});
|
56
53
|
|
57
|
-
reply.method(
|
54
|
+
reply.method('connector.reply');
|
58
55
|
reply.cb(original.cb());
|
59
|
-
reply.args({
|
56
|
+
reply.args({...result});
|
60
57
|
|
61
58
|
local.transport.send(reply);
|
62
59
|
} catch (e) {
|
63
|
-
console.log(
|
60
|
+
console.log('error processing packet', e, packet);
|
64
61
|
}
|
65
62
|
}
|
66
63
|
}
|
67
64
|
}
|
68
65
|
|
69
|
-
export {
|
66
|
+
export {Processor};
|