@superblocksteam/sdk 1.10.0 → 1.11.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/LICENSE.txt +87 -0
- package/dist/client.js +19 -22
- package/dist/flag.d.ts +1 -1
- package/dist/flag.js +2 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/sdk.js +3 -3
- package/dist/socket/handlers.js +1 -2
- package/dist/socket/index.js +7 -6
- package/dist/socket/signing.js +2 -3
- package/dist/socket/socket.js +14 -3
- package/dist/types/common.js +5 -0
- package/dist/types/socket.js +2 -0
- package/dist/utils.js +3 -3
- package/package.json +24 -18
- package/src/client.ts +71 -75
- package/src/flag.ts +1 -1
- package/src/index.ts +1 -0
- package/src/sdk.ts +9 -9
- package/src/socket/handlers.ts +4 -4
- package/src/socket/index.ts +8 -8
- package/src/socket/socket.ts +21 -19
- package/src/types/socket.ts +8 -8
- package/src/utils.ts +6 -6
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -0
package/src/socket/socket.ts
CHANGED
|
@@ -25,7 +25,7 @@ interface SocketResponse<Payload = unknown> {
|
|
|
25
25
|
|
|
26
26
|
export interface SocketMessage<
|
|
27
27
|
RequestPayload = unknown,
|
|
28
|
-
ResponsePayload = unknown
|
|
28
|
+
ResponsePayload = unknown,
|
|
29
29
|
> {
|
|
30
30
|
request?: SocketRequest<RequestPayload>;
|
|
31
31
|
response?: SocketResponse<ResponsePayload>;
|
|
@@ -52,7 +52,7 @@ export interface SocketError {
|
|
|
52
52
|
export class ISocket<
|
|
53
53
|
ImplementedMethods,
|
|
54
54
|
CallableMethods,
|
|
55
|
-
RequestContext extends RequestContextBase
|
|
55
|
+
RequestContext extends RequestContextBase,
|
|
56
56
|
> {
|
|
57
57
|
private readonly ws: WebSocket;
|
|
58
58
|
private readonly requestHandlers: MethodHandlers<
|
|
@@ -88,7 +88,7 @@ export class ISocket<
|
|
|
88
88
|
>,
|
|
89
89
|
globalMiddlewares: GenericMiddleware<CallableMethods, RequestContext>[],
|
|
90
90
|
timeouts?: SocketTimeouts,
|
|
91
|
-
logger?: { error: (message?: string) => void }
|
|
91
|
+
logger?: { error: (message?: string) => void },
|
|
92
92
|
) {
|
|
93
93
|
this.ws = ws;
|
|
94
94
|
this.requestHandlers = requestHandlers;
|
|
@@ -103,9 +103,10 @@ export class ISocket<
|
|
|
103
103
|
"message",
|
|
104
104
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
105
105
|
async (event: WebSocket.MessageEvent) => {
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
106
107
|
const eventData: SocketMessage = JSON.parse(event.data.toString());
|
|
107
108
|
return this.handleMessage(eventData);
|
|
108
|
-
}
|
|
109
|
+
},
|
|
109
110
|
);
|
|
110
111
|
}
|
|
111
112
|
|
|
@@ -153,7 +154,7 @@ export class ISocket<
|
|
|
153
154
|
if (alreadyResponded) {
|
|
154
155
|
throw new SocketErrorException(
|
|
155
156
|
4,
|
|
156
|
-
"next() was called after the response was sent"
|
|
157
|
+
"next() was called after the response was sent",
|
|
157
158
|
);
|
|
158
159
|
}
|
|
159
160
|
const handler = (
|
|
@@ -164,13 +165,13 @@ export class ISocket<
|
|
|
164
165
|
if (!handler) {
|
|
165
166
|
throw new SocketErrorException(
|
|
166
167
|
5,
|
|
167
|
-
"cannot call past the last handler in the chain"
|
|
168
|
+
"cannot call past the last handler in the chain",
|
|
168
169
|
);
|
|
169
170
|
}
|
|
170
171
|
if (wasCalled) {
|
|
171
172
|
throw new SocketErrorException(
|
|
172
173
|
6,
|
|
173
|
-
"next() was called multiple times"
|
|
174
|
+
"next() was called multiple times",
|
|
174
175
|
);
|
|
175
176
|
}
|
|
176
177
|
wasCalled = true;
|
|
@@ -179,7 +180,7 @@ export class ISocket<
|
|
|
179
180
|
payload,
|
|
180
181
|
reqCtx,
|
|
181
182
|
client,
|
|
182
|
-
generateNextFn(idx + 1)
|
|
183
|
+
generateNextFn(idx + 1),
|
|
183
184
|
);
|
|
184
185
|
};
|
|
185
186
|
};
|
|
@@ -220,13 +221,13 @@ export class ISocket<
|
|
|
220
221
|
Params,
|
|
221
222
|
Result,
|
|
222
223
|
CallableMethods,
|
|
223
|
-
RequestContext extends RequestContextBase
|
|
224
|
+
RequestContext extends RequestContextBase,
|
|
224
225
|
>(
|
|
225
226
|
handler: MethodHandler<Params, Result, CallableMethods, RequestContext>,
|
|
226
227
|
params: Params,
|
|
227
228
|
ctx: RequestContext,
|
|
228
229
|
client: ISocketClient<CallableMethods>,
|
|
229
|
-
next: () => Promise<Result
|
|
230
|
+
next: () => Promise<Result>,
|
|
230
231
|
): Promise<Result> {
|
|
231
232
|
return handler(params, ctx, client, next);
|
|
232
233
|
}
|
|
@@ -234,12 +235,13 @@ export class ISocket<
|
|
|
234
235
|
public request<Params, Result>(
|
|
235
236
|
method: string,
|
|
236
237
|
params: Params,
|
|
237
|
-
authorization?: string
|
|
238
|
+
authorization?: string,
|
|
238
239
|
): Promise<Result> {
|
|
239
240
|
return new Promise<Result>((resolve, reject) => {
|
|
240
241
|
const requestId = ++this.nxtRequestId;
|
|
241
242
|
this.responseHandler.set(requestId, {
|
|
242
243
|
resolve: (result) => resolve(result as Result),
|
|
244
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
243
245
|
reject: (error: SocketError) => reject(error),
|
|
244
246
|
});
|
|
245
247
|
const toSend: SocketMessage = {
|
|
@@ -287,14 +289,14 @@ export class ISocket<
|
|
|
287
289
|
// Set a new timeout for the next message
|
|
288
290
|
this.connectionTimeout = setTimeout(
|
|
289
291
|
this.handleConnectionTimeout(),
|
|
290
|
-
this.timeouts?.connectionTimeoutInSeconds * 1000
|
|
292
|
+
this.timeouts?.connectionTimeoutInSeconds * 1000,
|
|
291
293
|
);
|
|
292
294
|
}
|
|
293
295
|
|
|
294
296
|
private handleConnectionTimeout() {
|
|
295
297
|
return () => {
|
|
296
298
|
this.logger.error(
|
|
297
|
-
`Connection timed out after ${this.timeouts?.connectionTimeoutInSeconds} seconds
|
|
299
|
+
`Connection timed out after ${this.timeouts?.connectionTimeoutInSeconds} seconds`,
|
|
298
300
|
);
|
|
299
301
|
this.close();
|
|
300
302
|
};
|
|
@@ -316,7 +318,7 @@ export class ISocket<
|
|
|
316
318
|
// Set a new timeout for the next message
|
|
317
319
|
responseHandler.timeout = setTimeout(
|
|
318
320
|
this.handleNoResponseTimeout(reject),
|
|
319
|
-
this.timeouts?.noResponseTimeoutInSeconds * 1000
|
|
321
|
+
this.timeouts?.noResponseTimeoutInSeconds * 1000,
|
|
320
322
|
);
|
|
321
323
|
}
|
|
322
324
|
|
|
@@ -333,7 +335,7 @@ export class ISocket<
|
|
|
333
335
|
this.responseHandler.forEach((handler, key) => {
|
|
334
336
|
clearTimeout(handler.timeout);
|
|
335
337
|
this.logger.error(
|
|
336
|
-
`Rejecting pending requestId ${key} due to connection close
|
|
338
|
+
`Rejecting pending requestId ${key} due to connection close`,
|
|
337
339
|
);
|
|
338
340
|
handler.reject({ code: 8, message: "Connection closed" });
|
|
339
341
|
});
|
|
@@ -348,11 +350,11 @@ const proxyTarget = Object.freeze(() => {
|
|
|
348
350
|
function createIsocketProxy<
|
|
349
351
|
ImplementedMethods,
|
|
350
352
|
CallableMethods,
|
|
351
|
-
RequestContext extends RequestContextBase
|
|
353
|
+
RequestContext extends RequestContextBase,
|
|
352
354
|
>(
|
|
353
355
|
socket: ISocket<ImplementedMethods, CallableMethods, RequestContext>,
|
|
354
356
|
// if path is undefined, it means the current object is the root object
|
|
355
|
-
path: string | undefined
|
|
357
|
+
path: string | undefined,
|
|
356
358
|
): unknown {
|
|
357
359
|
return new Proxy(proxyTarget, {
|
|
358
360
|
get(_target, prop: string) {
|
|
@@ -386,9 +388,9 @@ function createIsocketProxy<
|
|
|
386
388
|
export function createISocketClient<
|
|
387
389
|
CallableMethods,
|
|
388
390
|
ImplementedMethods,
|
|
389
|
-
RequestContext extends RequestContextBase
|
|
391
|
+
RequestContext extends RequestContextBase,
|
|
390
392
|
>(
|
|
391
|
-
socket: ISocket<ImplementedMethods, CallableMethods, RequestContext
|
|
393
|
+
socket: ISocket<ImplementedMethods, CallableMethods, RequestContext>,
|
|
392
394
|
): ISocketClient<CallableMethods> {
|
|
393
395
|
return {
|
|
394
396
|
close: () => socket.close(),
|
package/src/types/socket.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type MethodHandler<Params, Result, PeerMethods, RequestContext> = (
|
|
|
8
8
|
params: Params,
|
|
9
9
|
ctx: RequestContext,
|
|
10
10
|
peer: ISocketClient<PeerMethods>,
|
|
11
|
-
next: () => Promise<Result
|
|
11
|
+
next: () => Promise<Result>,
|
|
12
12
|
) => Promise<Result>;
|
|
13
13
|
|
|
14
14
|
// A generic middleware is a method handler that can be chained with any other method handler, so it uses `any` for the params and result types
|
|
@@ -24,22 +24,22 @@ export type NonEmptyArray<T> = [...T[], T];
|
|
|
24
24
|
|
|
25
25
|
export type MethodHandlers<Methods, PeerMethods, RequestContext> = {
|
|
26
26
|
[Key in keyof Methods]: Methods[Key] extends (
|
|
27
|
-
params: infer Params
|
|
27
|
+
params: infer Params,
|
|
28
28
|
) => Promise<infer Result>
|
|
29
29
|
? NonEmptyArray<MethodHandler<Params, Result, PeerMethods, RequestContext>>
|
|
30
30
|
: Methods[Key] extends Record<string, unknown>
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
? MethodHandlers<Methods[Key], PeerMethods, RequestContext>
|
|
32
|
+
: never;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
type ISocketClientMethodCall<Methods> = {
|
|
36
36
|
[Key in keyof Methods]: Methods[Key] extends (
|
|
37
|
-
params: infer P
|
|
37
|
+
params: infer P,
|
|
38
38
|
) => Promise<infer R>
|
|
39
39
|
? (params: P) => Promise<R>
|
|
40
40
|
: Methods[Key] extends Record<string, unknown>
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
? ISocketClientMethodCall<Methods[Key]>
|
|
42
|
+
: never;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
export type ISocketClient<Methods> = {
|
|
@@ -48,7 +48,7 @@ export type ISocketClient<Methods> = {
|
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
export type MethodSchema<Params, Response> = (
|
|
51
|
-
params: Params
|
|
51
|
+
params: Params,
|
|
52
52
|
) => Promise<Response>;
|
|
53
53
|
|
|
54
54
|
/** The exception type can be thrown to cause the default error handler to write a raw error response to the socket */
|
package/src/utils.ts
CHANGED
|
@@ -12,16 +12,16 @@ export async function getAgentUrl(
|
|
|
12
12
|
agents: Agent[],
|
|
13
13
|
agentType: AgentType,
|
|
14
14
|
profile?: string,
|
|
15
|
-
healthCheck = true
|
|
15
|
+
healthCheck = true,
|
|
16
16
|
): Promise<string> {
|
|
17
17
|
let filtered = agents.filter(
|
|
18
|
-
(a) => a.type === agentType && a.status === AgentStatus.ACTIVE
|
|
18
|
+
(a) => a.type === agentType && a.status === AgentStatus.ACTIVE,
|
|
19
19
|
);
|
|
20
20
|
if (profile) {
|
|
21
21
|
filtered = agents.filter(
|
|
22
22
|
(a) =>
|
|
23
23
|
a.tags.profile &&
|
|
24
|
-
(a.tags.profile.includes("*") || a.tags.profile.includes(profile))
|
|
24
|
+
(a.tags.profile.includes("*") || a.tags.profile.includes(profile)),
|
|
25
25
|
);
|
|
26
26
|
}
|
|
27
27
|
if (healthCheck) {
|
|
@@ -41,7 +41,7 @@ export async function getAgentUrl(
|
|
|
41
41
|
}
|
|
42
42
|
try {
|
|
43
43
|
return await Promise.any(coroutines);
|
|
44
|
-
} catch
|
|
44
|
+
} catch {
|
|
45
45
|
throw new Error("No available agents");
|
|
46
46
|
}
|
|
47
47
|
} else {
|
|
@@ -51,7 +51,7 @@ export async function getAgentUrl(
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export const sanitizeV2RequestBody = (
|
|
54
|
-
apiBody: Record<string, unknown
|
|
54
|
+
apiBody: Record<string, unknown>,
|
|
55
55
|
): Record<string, unknown> => {
|
|
56
56
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
57
|
const traverseJSON = (obj: any) => {
|
|
@@ -72,7 +72,7 @@ export const sanitizeV2RequestBody = (
|
|
|
72
72
|
|
|
73
73
|
export const getSanitizedApi = (api: Api): any => {
|
|
74
74
|
const sanitizedBlocks = (api.blocks ?? [])?.map(
|
|
75
|
-
(block: Record<string, unknown>) => sanitizeV2RequestBody(block)
|
|
75
|
+
(block: Record<string, unknown>) => sanitizeV2RequestBody(block),
|
|
76
76
|
);
|
|
77
77
|
return {
|
|
78
78
|
...api,
|
package/tsconfig.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/client.ts","./src/errors.ts","./src/flag.ts","./src/index.ts","./src/sdk.ts","./src/utils.ts","./src/socket/handlers.ts","./src/socket/index.ts","./src/socket/signing.ts","./src/socket/socket.ts","./src/types/common.ts","./src/types/index.ts","./src/types/plugin.ts","./src/types/signing.ts","./src/types/socket.ts"],"version":"5.7.3"}
|