@superblocksteam/sdk 1.6.0 → 1.8.0
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/dist/client.d.ts +3 -0
- package/dist/client.js +13 -5
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +8 -1
- package/dist/socket/handlers.d.ts +4 -0
- package/dist/socket/socket.d.ts +1 -0
- package/dist/socket/socket.js +59 -57
- package/package.json +2 -2
- package/src/client.ts +24 -14
- package/src/errors.ts +7 -0
- package/src/socket/handlers.ts +4 -0
- package/src/socket/socket.ts +76 -79
package/dist/client.d.ts
CHANGED
|
@@ -19,10 +19,12 @@ export interface MultiPageApplicationWrapper {
|
|
|
19
19
|
export type PushApplicationWithCommitConfig = ApplicationWrapper & {
|
|
20
20
|
commitId: string;
|
|
21
21
|
commitMessage: string;
|
|
22
|
+
gitState: LocalGitRepoState;
|
|
22
23
|
};
|
|
23
24
|
export type PushMultiPageApplicationWithCommitConfig = MultiPageApplicationWrapper & {
|
|
24
25
|
commitId: string;
|
|
25
26
|
commitMessage: string;
|
|
27
|
+
gitState: LocalGitRepoState;
|
|
26
28
|
};
|
|
27
29
|
export interface ApiWrapper {
|
|
28
30
|
apiPb: Record<string, any>;
|
|
@@ -32,6 +34,7 @@ export type PushApiWithCommitConfig = {
|
|
|
32
34
|
apiPb: Record<string, any>;
|
|
33
35
|
commitId: string;
|
|
34
36
|
commitMessage: string;
|
|
37
|
+
gitState: LocalGitRepoState;
|
|
35
38
|
};
|
|
36
39
|
export type Branches = {
|
|
37
40
|
branches: Branch[];
|
package/dist/client.js
CHANGED
|
@@ -247,9 +247,17 @@ async function validateGitSetup(cliVersion, resourceId, resourceType, event, loc
|
|
|
247
247
|
`${e}`;
|
|
248
248
|
}
|
|
249
249
|
else {
|
|
250
|
-
message = `${e?.message ? e
|
|
250
|
+
message = `${e?.message ? e.message : e}`;
|
|
251
|
+
}
|
|
252
|
+
const errorMessage = `Could not validate your git setup against Superblocks for the ${resourceType.toLowerCase()} ${resourceName ?? resourceId}${message ? "\n" + message : ""}`;
|
|
253
|
+
// TODO(@taha-au @gpoulios-sb) Replace this check with a more robust one that uses error codes once
|
|
254
|
+
// the backend starts returning them as part of the response body
|
|
255
|
+
if (message.includes("Please initialize git locally")) {
|
|
256
|
+
throw new errors_1.ValidateGitSetupError(errorMessage);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
throw new Error(errorMessage);
|
|
251
260
|
}
|
|
252
|
-
throw new Error(`Could not validate your git setup against Superblocks for the ${resourceType.toLowerCase()} ${resourceName ?? resourceId}${message ? "\n" + message : ""}`);
|
|
253
261
|
}
|
|
254
262
|
}
|
|
255
263
|
exports.validateGitSetup = validateGitSetup;
|
|
@@ -469,6 +477,7 @@ async function pushApplication({ cliVersion, applicationId, token, superblocksBa
|
|
|
469
477
|
commitId: applicationConfig.commitId,
|
|
470
478
|
commitMessage: applicationConfig.commitMessage,
|
|
471
479
|
pages: applicationConfig.pages,
|
|
480
|
+
gitState: applicationConfig.gitState,
|
|
472
481
|
})
|
|
473
482
|
: await socket.call.v1.public.application.pushCommit({
|
|
474
483
|
applicationId,
|
|
@@ -478,6 +487,7 @@ async function pushApplication({ cliVersion, applicationId, token, superblocksBa
|
|
|
478
487
|
commitId: applicationConfig.commitId,
|
|
479
488
|
commitMessage: applicationConfig.commitMessage,
|
|
480
489
|
page: applicationConfig.page,
|
|
490
|
+
gitState: applicationConfig.gitState,
|
|
481
491
|
});
|
|
482
492
|
socket.close();
|
|
483
493
|
handleHttpError(resp.responseMeta.status);
|
|
@@ -557,6 +567,7 @@ async function pushApi({ cliVersion, apiId, token, superblocksBaseUrl, apiConfig
|
|
|
557
567
|
branchName: branch,
|
|
558
568
|
commitId: apiConfig.commitId,
|
|
559
569
|
commitMessage: apiConfig.commitMessage,
|
|
570
|
+
gitState: apiConfig.gitState,
|
|
560
571
|
});
|
|
561
572
|
socket.close();
|
|
562
573
|
handleHttpError(resp.responseMeta.status);
|
|
@@ -604,6 +615,3 @@ async function pushApi({ cliVersion, apiId, token, superblocksBaseUrl, apiConfig
|
|
|
604
615
|
}
|
|
605
616
|
}
|
|
606
617
|
exports.pushApi = pushApi;
|
|
607
|
-
function isMultiPageApplicationWrapper(data) {
|
|
608
|
-
return data.pages !== undefined;
|
|
609
|
-
}
|
package/dist/errors.d.ts
CHANGED
package/dist/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CommitAlreadyExistsError = exports.BranchNotCheckedOutError = void 0;
|
|
3
|
+
exports.ValidateGitSetupError = exports.CommitAlreadyExistsError = exports.BranchNotCheckedOutError = void 0;
|
|
4
4
|
class BranchNotCheckedOutError extends Error {
|
|
5
5
|
constructor(message) {
|
|
6
6
|
super(message);
|
|
@@ -15,3 +15,10 @@ class CommitAlreadyExistsError extends Error {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
exports.CommitAlreadyExistsError = CommitAlreadyExistsError;
|
|
18
|
+
class ValidateGitSetupError extends Error {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "ValidateGitSetupError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.ValidateGitSetupError = ValidateGitSetupError;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LocalGitRepoState } from "@superblocksteam/util";
|
|
1
2
|
import { ApiToSign, ApiToVerify, AppToSign, AppToVerify, MethodHandlers, RemoteCommitDto, Signature } from "../types";
|
|
2
3
|
type MethodSchema<Params, Response> = (params: Params) => Promise<Response>;
|
|
3
4
|
export interface ClientMethods {
|
|
@@ -85,6 +86,7 @@ export interface ServerMethods {
|
|
|
85
86
|
application: Record<string, unknown>;
|
|
86
87
|
page: Record<string, unknown>;
|
|
87
88
|
apis: Record<string, unknown>[];
|
|
89
|
+
gitState: LocalGitRepoState;
|
|
88
90
|
}, RemoteCommitDto>;
|
|
89
91
|
};
|
|
90
92
|
api: {
|
|
@@ -94,6 +96,7 @@ export interface ServerMethods {
|
|
|
94
96
|
commitId: string;
|
|
95
97
|
commitMessage: string;
|
|
96
98
|
apiPb: Record<string, unknown>;
|
|
99
|
+
gitState: LocalGitRepoState;
|
|
97
100
|
}, RemoteCommitDto>;
|
|
98
101
|
};
|
|
99
102
|
};
|
|
@@ -109,6 +112,7 @@ export interface ServerMethods {
|
|
|
109
112
|
application: Record<string, unknown>;
|
|
110
113
|
pages: Record<string, unknown>[];
|
|
111
114
|
apis: Record<string, unknown>[];
|
|
115
|
+
gitState: LocalGitRepoState;
|
|
112
116
|
}, RemoteCommitDto>;
|
|
113
117
|
};
|
|
114
118
|
};
|
package/dist/socket/socket.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export declare class ISocket<ImplementedMethods, CallableMethods, RequestContext
|
|
|
8
8
|
private peerAuthorization?;
|
|
9
9
|
private nxtRequestId;
|
|
10
10
|
constructor(ws: WebSocket, requestHandlers: MethodHandlers<ImplementedMethods, CallableMethods, RequestContext>);
|
|
11
|
+
private handleEvent;
|
|
11
12
|
request<Params, Result>(method: string, params: Params, authorization?: string): Promise<Result>;
|
|
12
13
|
private respond;
|
|
13
14
|
private respondError;
|
package/dist/socket/socket.js
CHANGED
|
@@ -7,71 +7,73 @@ class ISocket {
|
|
|
7
7
|
this.ws = ws;
|
|
8
8
|
this.requestHandlers = requestHandlers;
|
|
9
9
|
this.nxtRequestId = 0;
|
|
10
|
-
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
this.ws.addEventListener("message", async (event) => {
|
|
10
|
+
this.ws.addEventListener("message", (event) => {
|
|
13
11
|
const eventData = JSON.parse(event.data.toString());
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (!Array.isArray(handlers)) {
|
|
29
|
-
return await this.respondError(eventData.request.id, {
|
|
12
|
+
void this.handleEvent(eventData);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async handleEvent(eventData) {
|
|
16
|
+
if (eventData.request) {
|
|
17
|
+
// Split the method string into parts
|
|
18
|
+
const parts = eventData.request.method.split(".");
|
|
19
|
+
let handlers = this.requestHandlers;
|
|
20
|
+
for (const part of parts) {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
handlers = handlers[part];
|
|
24
|
+
if (!handlers) {
|
|
25
|
+
return this.respondError(eventData.request.id, {
|
|
30
26
|
code: 2,
|
|
31
|
-
message:
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
if (eventData.request.setAuthorization) {
|
|
35
|
-
this.peerAuthorization = eventData.request.setAuthorization;
|
|
36
|
-
}
|
|
37
|
-
const middlewareHandlers = handlers.slice(0, -1);
|
|
38
|
-
const handler = handlers[handlers.length - 1];
|
|
39
|
-
const reqCtx = {};
|
|
40
|
-
let response;
|
|
41
|
-
// TODO(george): maybe we should not create a new client for each request
|
|
42
|
-
const client = createISocketClient(this);
|
|
43
|
-
try {
|
|
44
|
-
for (const middlewareHandler of middlewareHandlers) {
|
|
45
|
-
await middlewareHandler(eventData.request.payload, this.peerAuthorization, client, reqCtx);
|
|
46
|
-
}
|
|
47
|
-
response = await handler(eventData.request.payload, client, reqCtx);
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
return await this.respondError(eventData.request.id, {
|
|
51
|
-
code: 3,
|
|
52
|
-
message: error.toString(),
|
|
27
|
+
message: `unknown method ${eventData.request.method}`,
|
|
53
28
|
});
|
|
54
29
|
}
|
|
55
|
-
await this.respond(eventData.request.id, response);
|
|
56
30
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
31
|
+
if (!Array.isArray(handlers)) {
|
|
32
|
+
return this.respondError(eventData.request.id, {
|
|
33
|
+
code: 2,
|
|
34
|
+
message: "unknown method",
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (eventData.request.setAuthorization) {
|
|
38
|
+
this.peerAuthorization = eventData.request.setAuthorization;
|
|
39
|
+
}
|
|
40
|
+
const middlewareHandlers = handlers.slice(0, -1);
|
|
41
|
+
const handler = handlers[handlers.length - 1];
|
|
42
|
+
const reqCtx = {};
|
|
43
|
+
let response;
|
|
44
|
+
// TODO(george): maybe we should not create a new client for each request
|
|
45
|
+
const client = createISocketClient(this);
|
|
46
|
+
try {
|
|
47
|
+
for (const middlewareHandler of middlewareHandlers) {
|
|
48
|
+
await middlewareHandler(eventData.request.payload, this.peerAuthorization, client, reqCtx);
|
|
63
49
|
}
|
|
64
|
-
|
|
65
|
-
this.responseHandler[eventData.response.id].resolve(eventData.response.payload);
|
|
66
|
-
delete this.responseHandler[eventData.response.id];
|
|
50
|
+
response = await handler(eventData.request.payload, client, reqCtx);
|
|
67
51
|
}
|
|
68
|
-
|
|
69
|
-
return
|
|
52
|
+
catch (error) {
|
|
53
|
+
return this.respondError(eventData.request.id, {
|
|
70
54
|
code: 3,
|
|
71
|
-
message:
|
|
55
|
+
message: error.toString(),
|
|
72
56
|
});
|
|
73
57
|
}
|
|
74
|
-
|
|
58
|
+
this.respond(eventData.request.id, response);
|
|
59
|
+
}
|
|
60
|
+
else if (eventData.response && eventData.response.id) {
|
|
61
|
+
if (!this.responseHandler[eventData.response.id]) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (eventData.response.error) {
|
|
65
|
+
this.responseHandler[eventData.response.id].reject(eventData.response.error);
|
|
66
|
+
}
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
this.responseHandler[eventData.response.id].resolve(eventData.response.payload);
|
|
69
|
+
delete this.responseHandler[eventData.response.id];
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
return this.respondError(-1, {
|
|
73
|
+
code: 3,
|
|
74
|
+
message: "unknown request id",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
75
77
|
}
|
|
76
78
|
request(method, params, authorization) {
|
|
77
79
|
return new Promise((resolve, reject) => {
|
|
@@ -91,7 +93,7 @@ class ISocket {
|
|
|
91
93
|
this.ws.send(JSON.stringify(toSend));
|
|
92
94
|
});
|
|
93
95
|
}
|
|
94
|
-
|
|
96
|
+
respond(requestId, result) {
|
|
95
97
|
const toSend = {
|
|
96
98
|
response: {
|
|
97
99
|
payload: result,
|
|
@@ -101,7 +103,7 @@ class ISocket {
|
|
|
101
103
|
};
|
|
102
104
|
return this.ws.send(JSON.stringify(toSend));
|
|
103
105
|
}
|
|
104
|
-
|
|
106
|
+
respondError(requestId, error) {
|
|
105
107
|
const toSend = {
|
|
106
108
|
response: {
|
|
107
109
|
payload: null,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superblocksteam/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Superblocks JS SDK",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"typescript": "^5.1.3"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@superblocksteam/util": "1.
|
|
34
|
+
"@superblocksteam/util": "1.8.0",
|
|
35
35
|
"axios": "^1.3.5",
|
|
36
36
|
"isomorphic-ws": "^5.0.0"
|
|
37
37
|
},
|
package/src/client.ts
CHANGED
|
@@ -13,7 +13,11 @@ import {
|
|
|
13
13
|
import axios, { AxiosError, AxiosRequestConfig } from "axios";
|
|
14
14
|
import FormData from "form-data";
|
|
15
15
|
import { isEmpty } from "lodash";
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
BranchNotCheckedOutError,
|
|
18
|
+
CommitAlreadyExistsError,
|
|
19
|
+
ValidateGitSetupError,
|
|
20
|
+
} from "./errors";
|
|
17
21
|
import { signingEnabled } from "./flag";
|
|
18
22
|
import { connectToISocketRPCServer } from "./socket";
|
|
19
23
|
import {
|
|
@@ -57,12 +61,14 @@ export interface MultiPageApplicationWrapper {
|
|
|
57
61
|
export type PushApplicationWithCommitConfig = ApplicationWrapper & {
|
|
58
62
|
commitId: string;
|
|
59
63
|
commitMessage: string;
|
|
64
|
+
gitState: LocalGitRepoState;
|
|
60
65
|
};
|
|
61
66
|
|
|
62
67
|
export type PushMultiPageApplicationWithCommitConfig =
|
|
63
68
|
MultiPageApplicationWrapper & {
|
|
64
69
|
commitId: string;
|
|
65
70
|
commitMessage: string;
|
|
71
|
+
gitState: LocalGitRepoState;
|
|
66
72
|
};
|
|
67
73
|
|
|
68
74
|
export interface ApiWrapper {
|
|
@@ -74,6 +80,7 @@ export type PushApiWithCommitConfig = {
|
|
|
74
80
|
apiPb: Record<string, any>;
|
|
75
81
|
commitId: string;
|
|
76
82
|
commitMessage: string;
|
|
83
|
+
gitState: LocalGitRepoState;
|
|
77
84
|
};
|
|
78
85
|
|
|
79
86
|
export type Branches = {
|
|
@@ -110,7 +117,6 @@ export async function fetchApplication({
|
|
|
110
117
|
const baseServerUrl = fetchSinglePageApplication
|
|
111
118
|
? BASE_SERVER_PUBLIC_API_URL_V1
|
|
112
119
|
: BASE_SERVER_PUBLIC_API_URL_v2;
|
|
113
|
-
|
|
114
120
|
const serverURL = branch
|
|
115
121
|
? new URL(
|
|
116
122
|
`${baseServerUrl}/applications/${applicationId}/branches/${encodeURIComponent(
|
|
@@ -428,13 +434,20 @@ export async function validateGitSetup(
|
|
|
428
434
|
e?.message ??
|
|
429
435
|
`${e}`;
|
|
430
436
|
} else {
|
|
431
|
-
message = `${e?.message ? e
|
|
437
|
+
message = `${e?.message ? e.message : e}`;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const errorMessage = `Could not validate your git setup against Superblocks for the ${resourceType.toLowerCase()} ${
|
|
441
|
+
resourceName ?? resourceId
|
|
442
|
+
}${message ? "\n" + message : ""}`;
|
|
443
|
+
|
|
444
|
+
// TODO(@taha-au @gpoulios-sb) Replace this check with a more robust one that uses error codes once
|
|
445
|
+
// the backend starts returning them as part of the response body
|
|
446
|
+
if (message.includes("Please initialize git locally")) {
|
|
447
|
+
throw new ValidateGitSetupError(errorMessage);
|
|
448
|
+
} else {
|
|
449
|
+
throw new Error(errorMessage);
|
|
432
450
|
}
|
|
433
|
-
throw new Error(
|
|
434
|
-
`Could not validate your git setup against Superblocks for the ${resourceType.toLowerCase()} ${
|
|
435
|
-
resourceName ?? resourceId
|
|
436
|
-
}${message ? "\n" + message : ""}`
|
|
437
|
-
);
|
|
438
451
|
}
|
|
439
452
|
}
|
|
440
453
|
|
|
@@ -754,6 +767,7 @@ export async function pushApplication({
|
|
|
754
767
|
pages: (
|
|
755
768
|
applicationConfig as unknown as PushMultiPageApplicationWithCommitConfig
|
|
756
769
|
).pages,
|
|
770
|
+
gitState: applicationConfig.gitState,
|
|
757
771
|
})
|
|
758
772
|
: await socket.call.v1.public.application.pushCommit({
|
|
759
773
|
applicationId,
|
|
@@ -765,6 +779,7 @@ export async function pushApplication({
|
|
|
765
779
|
page: (
|
|
766
780
|
applicationConfig as unknown as PushApplicationWithCommitConfig
|
|
767
781
|
).page,
|
|
782
|
+
gitState: applicationConfig.gitState,
|
|
768
783
|
});
|
|
769
784
|
socket.close();
|
|
770
785
|
handleHttpError(resp.responseMeta.status);
|
|
@@ -886,6 +901,7 @@ export async function pushApi({
|
|
|
886
901
|
branchName: branch,
|
|
887
902
|
commitId: apiConfig.commitId,
|
|
888
903
|
commitMessage: apiConfig.commitMessage,
|
|
904
|
+
gitState: apiConfig.gitState,
|
|
889
905
|
});
|
|
890
906
|
socket.close();
|
|
891
907
|
handleHttpError(resp.responseMeta.status);
|
|
@@ -939,9 +955,3 @@ export async function pushApi({
|
|
|
939
955
|
);
|
|
940
956
|
}
|
|
941
957
|
}
|
|
942
|
-
|
|
943
|
-
function isMultiPageApplicationWrapper(
|
|
944
|
-
data: ApplicationWrapper | MultiPageApplicationWrapper
|
|
945
|
-
): data is MultiPageApplicationWrapper {
|
|
946
|
-
return (data as MultiPageApplicationWrapper).pages !== undefined;
|
|
947
|
-
}
|
package/src/errors.ts
CHANGED
|
@@ -11,3 +11,10 @@ export class CommitAlreadyExistsError extends Error {
|
|
|
11
11
|
this.name = "CommitAlreadyExistsError";
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
export class ValidateGitSetupError extends Error {
|
|
16
|
+
constructor(message: string) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "ValidateGitSetupError";
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/socket/handlers.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LocalGitRepoState } from "@superblocksteam/util";
|
|
1
2
|
import {
|
|
2
3
|
ApiToSign,
|
|
3
4
|
ApiToVerify,
|
|
@@ -97,6 +98,7 @@ export interface ServerMethods {
|
|
|
97
98
|
application: Record<string, unknown>;
|
|
98
99
|
page: Record<string, unknown>;
|
|
99
100
|
apis: Record<string, unknown>[];
|
|
101
|
+
gitState: LocalGitRepoState;
|
|
100
102
|
},
|
|
101
103
|
RemoteCommitDto
|
|
102
104
|
>;
|
|
@@ -109,6 +111,7 @@ export interface ServerMethods {
|
|
|
109
111
|
commitId: string;
|
|
110
112
|
commitMessage: string;
|
|
111
113
|
apiPb: Record<string, unknown>;
|
|
114
|
+
gitState: LocalGitRepoState;
|
|
112
115
|
},
|
|
113
116
|
RemoteCommitDto
|
|
114
117
|
>;
|
|
@@ -127,6 +130,7 @@ export interface ServerMethods {
|
|
|
127
130
|
application: Record<string, unknown>;
|
|
128
131
|
pages: Record<string, unknown>[];
|
|
129
132
|
apis: Record<string, unknown>[];
|
|
133
|
+
gitState: LocalGitRepoState;
|
|
130
134
|
},
|
|
131
135
|
RemoteCommitDto
|
|
132
136
|
>;
|
package/src/socket/socket.ts
CHANGED
|
@@ -57,87 +57,90 @@ export class ISocket<ImplementedMethods, CallableMethods, RequestContext> {
|
|
|
57
57
|
this.requestHandlers = requestHandlers;
|
|
58
58
|
this.nxtRequestId = 0;
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
this.ws.addEventListener("message", async (event: MessageEvent) => {
|
|
60
|
+
this.ws.addEventListener("message", (event: WebSocket.MessageEvent) => {
|
|
63
61
|
const eventData: SocketMessage = JSON.parse(event.data.toString());
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
let handlers = this.requestHandlers;
|
|
68
|
-
for (const part of parts) {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
handlers = handlers[part];
|
|
71
|
-
if (!handlers) {
|
|
72
|
-
return await this.respondError(eventData.request.id, {
|
|
73
|
-
code: 2,
|
|
74
|
-
message: `unknown method ${eventData.request.method}`,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
62
|
+
void this.handleEvent(eventData);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
78
65
|
|
|
79
|
-
|
|
80
|
-
|
|
66
|
+
private async handleEvent(eventData: SocketMessage<unknown, unknown>) {
|
|
67
|
+
if (eventData.request) {
|
|
68
|
+
// Split the method string into parts
|
|
69
|
+
const parts = eventData.request.method.split(".");
|
|
70
|
+
let handlers = this.requestHandlers;
|
|
71
|
+
for (const part of parts) {
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
handlers = handlers[part];
|
|
75
|
+
if (!handlers) {
|
|
76
|
+
return this.respondError(eventData.request.id, {
|
|
81
77
|
code: 2,
|
|
82
|
-
message:
|
|
78
|
+
message: `unknown method ${eventData.request.method}`,
|
|
83
79
|
});
|
|
84
80
|
}
|
|
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
|
-
await this.respond(eventData.request.id, response);
|
|
120
|
-
} else if (eventData.response && eventData.response.id) {
|
|
121
|
-
if (!this.responseHandler[eventData.response.id]) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
if (eventData.response.error) {
|
|
125
|
-
this.responseHandler[eventData.response.id].reject(
|
|
126
|
-
eventData.response.error
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!Array.isArray(handlers)) {
|
|
84
|
+
return this.respondError(eventData.request.id, {
|
|
85
|
+
code: 2,
|
|
86
|
+
message: "unknown method",
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (eventData.request.setAuthorization) {
|
|
90
|
+
this.peerAuthorization = eventData.request.setAuthorization;
|
|
91
|
+
}
|
|
92
|
+
const middlewareHandlers = handlers.slice(0, -1) as MiddlewareHandler<
|
|
93
|
+
unknown,
|
|
94
|
+
CallableMethods,
|
|
95
|
+
RequestContext
|
|
96
|
+
>[];
|
|
97
|
+
const handler = handlers[handlers.length - 1] as MethodHandler<
|
|
98
|
+
unknown,
|
|
99
|
+
unknown,
|
|
100
|
+
CallableMethods,
|
|
101
|
+
RequestContext
|
|
102
|
+
>;
|
|
103
|
+
const reqCtx = {} as RequestContext;
|
|
104
|
+
let response: unknown;
|
|
105
|
+
// TODO(george): maybe we should not create a new client for each request
|
|
106
|
+
const client = createISocketClient(this);
|
|
107
|
+
try {
|
|
108
|
+
for (const middlewareHandler of middlewareHandlers) {
|
|
109
|
+
await middlewareHandler(
|
|
110
|
+
eventData.request.payload,
|
|
111
|
+
this.peerAuthorization,
|
|
112
|
+
client,
|
|
113
|
+
reqCtx
|
|
127
114
|
);
|
|
128
115
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
);
|
|
133
|
-
delete this.responseHandler[eventData.response.id];
|
|
134
|
-
} else {
|
|
135
|
-
return await this.respondError(-1, {
|
|
116
|
+
response = await handler(eventData.request.payload, client, reqCtx);
|
|
117
|
+
} catch (error: any) {
|
|
118
|
+
return this.respondError(eventData.request.id, {
|
|
136
119
|
code: 3,
|
|
137
|
-
message:
|
|
120
|
+
message: error.toString(),
|
|
138
121
|
});
|
|
139
122
|
}
|
|
140
|
-
|
|
123
|
+
this.respond(eventData.request.id, response);
|
|
124
|
+
} else if (eventData.response && eventData.response.id) {
|
|
125
|
+
if (!this.responseHandler[eventData.response.id]) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (eventData.response.error) {
|
|
129
|
+
this.responseHandler[eventData.response.id].reject(
|
|
130
|
+
eventData.response.error
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
134
|
+
this.responseHandler[eventData.response.id].resolve(
|
|
135
|
+
eventData.response.payload as any
|
|
136
|
+
);
|
|
137
|
+
delete this.responseHandler[eventData.response.id];
|
|
138
|
+
} else {
|
|
139
|
+
return this.respondError(-1, {
|
|
140
|
+
code: 3,
|
|
141
|
+
message: "unknown request id",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
141
144
|
}
|
|
142
145
|
|
|
143
146
|
public request<Params, Result>(
|
|
@@ -163,10 +166,7 @@ export class ISocket<ImplementedMethods, CallableMethods, RequestContext> {
|
|
|
163
166
|
});
|
|
164
167
|
}
|
|
165
168
|
|
|
166
|
-
private
|
|
167
|
-
requestId: number,
|
|
168
|
-
result: Result
|
|
169
|
-
): Promise<void> {
|
|
169
|
+
private respond<Result>(requestId: number, result: Result): void {
|
|
170
170
|
const toSend: SocketMessage = {
|
|
171
171
|
response: {
|
|
172
172
|
payload: result,
|
|
@@ -177,10 +177,7 @@ export class ISocket<ImplementedMethods, CallableMethods, RequestContext> {
|
|
|
177
177
|
return this.ws.send(JSON.stringify(toSend));
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
private
|
|
181
|
-
requestId: number,
|
|
182
|
-
error: SocketError
|
|
183
|
-
): Promise<void> {
|
|
180
|
+
private respondError(requestId: number, error: SocketError): void {
|
|
184
181
|
const toSend: SocketMessage = {
|
|
185
182
|
response: {
|
|
186
183
|
payload: null,
|