MCPcopy Create free account
hub / github.com/FlashpointProject/launcher / validate_socket_message

Function validate_socket_message

src/shared/socket/shared.ts:51–83  ·  view source on GitHub ↗
(data: any)

Source from the content-addressed store, hash-verified

49 * @returns Same object (if valid) or an error.
50 */
51export function validate_socket_message<T>(data: any): [SocketRequestData | SocketResponseData<T> | undefined, Error | undefined] {
52 // Verify shared types
53 const shared_errors = [];
54 if (typeof data !== 'object') {
55 shared_errors.push(`The data object is not of type "object" (it is "${typeof data}").`);
56 }
57 if (typeof data.id !== 'number' && data.id !== undefined) {
58 shared_errors.push(`"id" is not of type "number" (it is "${typeof data.id}").`);
59 }
60
61 if (shared_errors.length > 0) {
62 return [undefined, new Error('Message is incorrectly formatted. ' + shared_errors.join(' '))];
63 }
64
65 // Verify request
66 // @TODO Verify more of the message (perhaps even the argument and result data?)
67 const request_errors: string[] = [];
68
69 if ('args' in data && 'type' in data) {
70 if (!Array.isArray(data.args)) {
71 request_errors.push(`"args" is not an array (it is "${typeof data.args}").`);
72 }
73 if (typeof data.type !== 'number' && typeof data.type !== 'string') {
74 request_errors.push(`"type" is not of type "number" or "string" (it is "${typeof data.type}").`);
75 }
76 }
77
78 if (request_errors.length === 0) {
79 return [data, undefined];
80 } else {
81 return [undefined, new Error('Message is incorrectly formatted. ' + request_errors.join(' '))];
82 }
83}

Callers 2

onMessageMethod · 0.90
onMessageMethod · 0.90

Calls 1

pushMethod · 0.65

Tested by

no test coverage detected