diff --git a/src/crdt/text.ts b/src/crdt/text.ts index 0dcbc96..e9186a2 100644 --- a/src/crdt/text.ts +++ b/src/crdt/text.ts @@ -1,4 +1,7 @@ -import type { DeleteAtOperation, TextFilePatchOperation } from "../realtime.ts"; +import type { + DeleteAtOperation, + TextFilePatchOperation, +} from "../realtime.types.ts"; import { BinaryIndexedTree } from "./bit.ts"; const shrinkOperations = ( diff --git a/src/realtime.ts b/src/realtime.ts index ba3bfad..2a4d7ad 100644 --- a/src/realtime.ts +++ b/src/realtime.ts @@ -1,8 +1,18 @@ -import { applyReducer, type Operation } from "fast-json-patch"; +import { applyReducer } from "fast-json-patch"; import { BinaryIndexedTree } from "./crdt/bit.ts"; import { apply } from "./crdt/text.ts"; import { type Env } from "./index.ts"; -import { Router, Routes, createRouter } from "./router.ts"; +import { createRouter, Router, Routes } from "./router.ts"; +import { + File, + FilePatchResult, + FileSystemNode, + isJSONFilePatch, + isTextFileSet, + ServerEvent, + VolumePatchRequest, + VolumePatchResponse, +} from "./realtime.types.ts"; export const getObjectFor = (volume: string, ctx: { env: Env }) => { const object = volume.startsWith("ephemeral:") @@ -16,77 +26,6 @@ export const getObjectFor = (volume: string, ctx: { env: Env }) => { const encoder = new TextEncoder(); const decoder = new TextDecoder(); -export interface BaseFilePatch { - path: string; -} -export type TextFilePatchOperation = InsertAtOperation | DeleteAtOperation; - -export interface TextFielPatchOperationBase { - at: number; -} - -export interface InsertAtOperation extends TextFielPatchOperationBase { - text: string; -} - -export interface DeleteAtOperation extends TextFielPatchOperationBase { - length: number; -} -export interface TextFilePatch extends BaseFilePatch { - operations: TextFilePatchOperation[]; - timestamp: number; -} - -export interface TextFileSet extends BaseFilePatch { - content: string | null; -} - -export type FilePatch = JSONFilePatch | TextFilePatch | TextFileSet; - -export const isJSONFilePatch = (patch: FilePatch): patch is JSONFilePatch => { - return (patch as JSONFilePatch).patches !== undefined; -}; - -export const isTextFileSet = (patch: FilePatch): patch is TextFileSet => { - return (patch as TextFileSet).content !== undefined; -}; - -export interface JSONFilePatch extends BaseFilePatch { - patches: Operation[]; -} - -export interface VolumePatchRequest { - messageId?: string; - patches: FilePatch[]; -} - -export interface FilePatchResult { - path: string; - accepted: boolean; - content?: string; - deleted?: boolean; -} - -export interface VolumePatchResponse { - results: FilePatchResult[]; - timestamp: number; -} - -export interface FsEvent { - messageId?: string; - path: string; - deleted?: boolean; - timestamp: number; -} - -export type ServerEvent = FsEvent; - -export interface File { - content: string; -} - -export type FileSystemNode = Record; - export class RealtimeError extends Error { public status: number; diff --git a/src/realtime.types.ts b/src/realtime.types.ts new file mode 100644 index 0000000..eeda90a --- /dev/null +++ b/src/realtime.types.ts @@ -0,0 +1,72 @@ +import { type Operation } from "fast-json-patch"; + +export interface BaseFilePatch { + path: string; +} +export type TextFilePatchOperation = InsertAtOperation | DeleteAtOperation; + +export interface TextFielPatchOperationBase { + at: number; +} + +export interface InsertAtOperation extends TextFielPatchOperationBase { + text: string; +} + +export interface DeleteAtOperation extends TextFielPatchOperationBase { + length: number; +} +export interface TextFilePatch extends BaseFilePatch { + operations: TextFilePatchOperation[]; + timestamp: number; +} + +export interface TextFileSet extends BaseFilePatch { + content: string | null; +} + +export type FilePatch = JSONFilePatch | TextFilePatch | TextFileSet; + +export const isJSONFilePatch = (patch: FilePatch): patch is JSONFilePatch => { + return (patch as JSONFilePatch).patches !== undefined; +}; + +export const isTextFileSet = (patch: FilePatch): patch is TextFileSet => { + return (patch as TextFileSet).content !== undefined; +}; + +export interface JSONFilePatch extends BaseFilePatch { + patches: Operation[]; +} + +export interface VolumePatchRequest { + messageId?: string; + patches: FilePatch[]; +} + +export interface FilePatchResult { + path: string; + accepted: boolean; + content?: string; + deleted?: boolean; +} + +export interface VolumePatchResponse { + results: FilePatchResult[]; + timestamp: number; +} + +export interface FsEvent { + messageId?: string; + path: string; + deleted?: boolean; + timestamp: number; +} + +export type ServerEvent = FsEvent; + +export interface File { + content: string; +} + +export type FileSystemNode = Record;