@casual-simulation/aux-common
Version:
Common library for AUX projects
197 lines • 7.69 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';
import { memoize } from 'es-toolkit';
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 = memoize(() => z.tuple([z.enum(WebsocketEventTypes), z.number()]).rest(z.any()));
export const websocketUploadRequestEventSchema = memoize(() => z.tuple([z.literal(WebsocketEventTypes.UploadRequest), z.number()]));
export const websocketUploadResponseEventSchema = memoize(() => z.tuple([
z.literal(WebsocketEventTypes.UploadResponse),
z.number(),
z.string(),
z.string(),
z.record(z.string(), z.string()),
]));
export const websocketErrorInfoSchema = memoize(() => z.object({
errorCode: z.string(),
errorMessage: z.string(),
issues: z.array(z.any()).optional(),
reason: z.any().optional(),
}));
export const websocketErrorEventSchema = memoize(() => z.tuple([
z.literal(WebsocketEventTypes.Error),
z.number(),
websocketErrorInfoSchema(),
]));
export const websocketDownloadRequestEventSchema = memoize(() => z.tuple([
z.literal(WebsocketEventTypes.DownloadRequest),
z.number(),
z.string(),
z.string(),
z.record(z.string(), z.string()),
]));
export const loginMessageSchema = memoize(() => z.object({
type: z.literal('login'),
connectionToken: z.string().optional(),
connectionId: z.string().optional(),
}));
export const watchBranchMessageSchema = memoize(() => z.object({
type: z.literal('repo/watch_branch'),
recordName: z.string().nonempty().nullable(),
expires: z.boolean().optional(),
inst: z.string(),
branch: z.string(),
temporary: z.boolean().optional(),
markers: z.array(z.string()).optional(),
}));
export const unwatchBranchMessageSchema = memoize(() => z.object({
type: z.literal('repo/unwatch_branch'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
}));
export const watchBranchDevicesMessageSchema = memoize(() => z.object({
type: z.literal('repo/watch_branch_devices'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
}));
export const unwatchBranchDevicesMessageSchema = memoize(() => z.object({
type: z.literal('repo/unwatch_branch_devices'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
}));
export const addUpdatesMessageSchema = memoize(() => 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 = memoize(() => z.object({
type: z.literal('repo/get_updates'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
}));
export const websocketHttpRequestMessageSchema = memoize(() => z.object({
type: z.literal('http_request'),
request: genericHttpRequestSchema,
id: z.number(),
}));
export const updatesReceivedMessageSchema = memoize(() => 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 = memoize(() => z.object({
type: z.literal('repo/send_action'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
action: remoteActionsSchema,
}));
export const receiveDeviceActionMessageSchema = memoize(() => z.object({
type: z.literal('repo/receive_action'),
recordName: z.string().nonempty().nullable(),
inst: z.string(),
branch: z.string(),
action: deviceActionsSchema,
}));
export const connectedToBranchMessageSchema = memoize(() => z.object({
type: z.literal('repo/connected_to_branch'),
broadcast: z.boolean(),
branch: watchBranchMessageSchema(),
connection: connectionInfoSchema(),
}));
export const disconnectedFromBranchMessageSchema = memoize(() => 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 = memoize(() => 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 = memoize(() => z.object({
type: z.literal('sync/time'),
id: z.number(),
clientRequestTime: z.number(),
}));
export const timeSyncResponseMessageSchema = memoize(() => z.object({
type: z.literal('sync/time/response'),
id: z.number(),
clientRequestTime: z.number(),
serverReceiveTime: z.number(),
serverTransmitTime: z.number(),
}));
export const requestMissingPermissionMessageSchema = memoize(() => 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 = memoize(() => 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 = memoize(() => z.object({
type: z.literal('rate_limit_exceeded'),
retryAfter: z.number(),
totalHits: z.number(),
}));
export const websocketRequestMessageSchema = memoize(() => z.discriminatedUnion('type', [
loginMessageSchema(),
watchBranchMessageSchema(),
unwatchBranchMessageSchema(),
addUpdatesMessageSchema(),
sendActionMessageSchema(),
watchBranchDevicesMessageSchema(),
unwatchBranchDevicesMessageSchema(),
connectionCountMessageSchema(),
timeSyncRequestMessageSchema(),
getUpdatesMessageSchema(),
websocketHttpRequestMessageSchema(),
requestMissingPermissionMessageSchema(),
requestMissingPermissionResponseMessageSchema(),
]));
//# sourceMappingURL=WebsocketEvents.js.map