@casual-simulation/aux-common
Version:
Common library for AUX projects
199 lines • 7.14 kB
JavaScript
import { connectionInfoSchema } from '../common/ConnectionInfo';
import { remoteActionsSchema, deviceActionsSchema, } from '../common/RemoteActions';
import { z } from 'zod';
import { genericHttpRequestSchema } from '../http/GenericHttpInterface';
import { ACTION_KINDS_VALIDATION, RESOURCE_KIND_VALIDATION, SUBJECT_TYPE_VALIDATION, } from '../common';
export var WebsocketEventTypes;
(function (WebsocketEventTypes) {
WebsocketEventTypes[WebsocketEventTypes["Message"] = 1] = "Message";
WebsocketEventTypes[WebsocketEventTypes["UploadRequest"] = 2] = "UploadRequest";
WebsocketEventTypes[WebsocketEventTypes["UploadResponse"] = 3] = "UploadResponse";
WebsocketEventTypes[WebsocketEventTypes["DownloadRequest"] = 4] = "DownloadRequest";
WebsocketEventTypes[WebsocketEventTypes["Error"] = 5] = "Error";
})(WebsocketEventTypes || (WebsocketEventTypes = {}));
export const websocketEventSchema = z
.tuple([z.nativeEnum(WebsocketEventTypes), z.number()])
.rest(z.any());
export const websocketUploadRequestEventSchema = z.tuple([
z.literal(WebsocketEventTypes.UploadRequest),
z.number(),
]);
export const websocketUploadResponseEventSchema = z.tuple([
z.literal(WebsocketEventTypes.UploadResponse),
z.number(),
z.string(),
z.string(),
z.record(z.string()),
]);
export const websocketErrorInfoSchema = z.object({
errorCode: z.string(),
errorMessage: z.string(),
issues: z.array(z.any()).optional(),
reason: z.any().optional(),
});
export const websocketErrorEventSchema = z.tuple([
z.literal(WebsocketEventTypes.Error),
z.number(),
websocketErrorInfoSchema,
]);
export const websocketDownloadRequestEventSchema = z.tuple([
z.literal(WebsocketEventTypes.DownloadRequest),
z.number(),
z.string(),
z.string(),
z.record(z.string()),
]);
export const loginMessageSchema = z.object({
type: z.literal('login'),
connectionToken: z.string().optional(),
connectionId: z.string().optional(),
});
export const watchBranchMessageSchema = z.object({
type: z.literal('repo/watch_branch'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
temporary: z.boolean().optional(),
});
export const unwatchBranchMessageSchema = z.object({
type: z.literal('repo/unwatch_branch'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
});
export const watchBranchDevicesMessageSchema = z.object({
type: z.literal('repo/watch_branch_devices'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
});
export const unwatchBranchDevicesMessageSchema = z.object({
type: z.literal('repo/unwatch_branch_devices'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
});
export const addUpdatesMessageSchema = z.object({
type: z.literal('repo/add_updates'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
updates: z.array(z.string()),
updateId: z.number().optional(),
initial: z.boolean().optional(),
timestamps: z.array(z.number()).optional(),
});
export const getUpdatesMessageSchema = z.object({
type: z.literal('repo/get_updates'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
});
export const websocketHttpRequestMessageSchema = z.object({
type: z.literal('http_request'),
request: genericHttpRequestSchema,
id: z.number(),
});
export const updatesReceivedMessageSchema = z.object({
type: z.literal('repo/updates_received'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
updateId: z.number(),
errorCode: z
.enum(['max_size_reached', 'record_not_found', 'inst_not_found'])
.optional(),
maxBranchSizeInBytes: z.number().optional(),
neededBranchSizeInBytes: z.number().optional(),
});
export const sendActionMessageSchema = z.object({
type: z.literal('repo/send_action'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
action: remoteActionsSchema,
});
export const receiveDeviceActionMessageSchema = z.object({
type: z.literal('repo/receive_action'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
action: deviceActionsSchema,
});
export const connectedToBranchMessageSchema = z.object({
type: z.literal('repo/connected_to_branch'),
broadcast: z.boolean(),
branch: watchBranchMessageSchema,
connection: connectionInfoSchema,
});
export const disconnectedFromBranchMessageSchema = z.object({
type: z.literal('repo/disconnected_from_branch'),
broadcast: z.boolean(),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
connection: connectionInfoSchema,
});
export const connectionCountMessageSchema = z.object({
type: z.literal('repo/connection_count'),
recordName: z.string().nonempty().nullable(),
inst: z.string().nullable(),
branch: z.string().nullable(),
count: z.number().optional(),
});
export const timeSyncRequestMessageSchema = z.object({
type: z.literal('sync/time'),
id: z.number(),
clientRequestTime: z.number(),
});
export const timeSyncResponseMessageSchema = z.object({
type: z.literal('sync/time/response'),
id: z.number(),
clientRequestTime: z.number(),
serverReceiveTime: z.number(),
serverTransmitTime: z.number(),
});
export const requestMissingPermissionMessageSchema = z.object({
type: z.literal('permission/request/missing'),
reason: z.object({
type: z.literal('missing_permission'),
recordName: z.string(),
resourceKind: RESOURCE_KIND_VALIDATION,
resourceId: z.string(),
action: ACTION_KINDS_VALIDATION,
subjectType: SUBJECT_TYPE_VALIDATION,
subjectId: z.string(),
}),
});
export const requestMissingPermissionResponseMessageSchema = z.object({
type: z.literal('permission/request/missing/response'),
success: z.boolean(),
recordName: z.string(),
resourceKind: RESOURCE_KIND_VALIDATION,
resourceId: z.string(),
subjectType: SUBJECT_TYPE_VALIDATION,
subjectId: z.string(),
errorCode: z.string().optional(),
errorMessage: z.string().optional(),
});
export const rateLimitExceededMessageSchema = z.object({
type: z.literal('rate_limit_exceeded'),
retryAfter: z.number(),
totalHits: z.number(),
});
export const websocketRequestMessageSchema = z.discriminatedUnion('type', [
loginMessageSchema,
watchBranchMessageSchema,
unwatchBranchMessageSchema,
addUpdatesMessageSchema,
sendActionMessageSchema,
watchBranchDevicesMessageSchema,
unwatchBranchDevicesMessageSchema,
connectionCountMessageSchema,
timeSyncRequestMessageSchema,
getUpdatesMessageSchema,
websocketHttpRequestMessageSchema,
requestMissingPermissionMessageSchema,
requestMissingPermissionResponseMessageSchema,
]);
//# sourceMappingURL=WebsocketEvents.js.map