diff --git a/src/json-crdt-repo/remote/RemoteHistoryDemoServer.ts b/src/json-crdt-repo/remote/RemoteHistoryDemoServer.ts index 94e6586c27..57314b18c6 100644 --- a/src/json-crdt-repo/remote/RemoteHistoryDemoServer.ts +++ b/src/json-crdt-repo/remote/RemoteHistoryDemoServer.ts @@ -20,7 +20,7 @@ export class RemoteHistoryDemoServer implements RemoteHistory) {} public async create(id: string, patches: RemotePatch[]): Promise { - await this.client.call('blocks.create', { + await this.client.call('block.new', { id, patches: patches.map((patch, seq) => ({ // TODO: seq and created should be set on server. (And returned back?) @@ -36,7 +36,7 @@ export class RemoteHistoryDemoServer implements RemoteHistory { - const {block, patches} = await this.client.call('blocks.get', {id}); + const {block, patches} = await this.client.call('block.get', {id}); return { cursor: block.seq, model: block, @@ -46,7 +46,7 @@ export class RemoteHistoryDemoServer implements RemoteHistory { const limit = 100; - const res = await this.client.call('blocks.history', { + const res = await this.client.call('block.scan', { id, min: cursor, max: cursor + limit, @@ -69,7 +69,7 @@ export class RemoteHistoryDemoServer implements RemoteHistory { - const res = await this.client.call('blocks.edit', { + const res = await this.client.call('block.upd', { id, patches: patches.map((patch, seq) => ({ seq, @@ -84,7 +84,7 @@ export class RemoteHistoryDemoServer implements RemoteHistory { - await this.client.call('blocks.remove', {id}); + await this.client.call('block.del', {id}); } /** diff --git a/src/json-crdt-repo/remote/__tests__/RemoteHistoryServer.spec.ts b/src/json-crdt-repo/remote/__tests__/RemoteHistoryServer.spec.ts index 9cb3e3460c..07c065d4aa 100644 --- a/src/json-crdt-repo/remote/__tests__/RemoteHistoryServer.spec.ts +++ b/src/json-crdt-repo/remote/__tests__/RemoteHistoryServer.spec.ts @@ -28,7 +28,7 @@ describe('.create()', () => { const blob = patch.toBinary(); const id = genId(); await remote.create(id, [{blob}]); - const {data} = await caller.call('blocks.get', {id}, {}); + const {data} = await caller.call('block.get', {id}, {}); // console.log(data.patches); const model2 = Model.fromBinary(data.block.blob); expect(model2.view()).toEqual({foo: 'bar'}); diff --git a/src/server/__tests__/blocks.spec.ts b/src/server/__tests__/block.spec.ts similarity index 83% rename from src/server/__tests__/blocks.spec.ts rename to src/server/__tests__/block.spec.ts index a000e4639e..08f3eb992c 100644 --- a/src/server/__tests__/blocks.spec.ts +++ b/src/server/__tests__/block.spec.ts @@ -3,12 +3,12 @@ import {RpcErrorCodes} from '../../reactive-rpc/common/rpc/caller'; import {setup} from './setup'; import {tick, until} from '../../__tests__/util'; -describe('blocks.*', () => { - describe('blocks.create', () => { +describe('block.*', () => { + describe('block.new', () => { test('can create an empty block', async () => { const {call} = setup(); - await call('blocks.create', {id: 'my-block', patches: []}); - const {block} = await call('blocks.get', {id: 'my-block'}); + await call('block.new', {id: 'my-block', patches: []}); + const {block} = await call('block.get', {id: 'my-block'}); expect(block).toMatchObject({ id: 'my-block', seq: -1, @@ -32,7 +32,7 @@ describe('blocks.*', () => { age: 26, }); const patch2 = model.api.flush(); - await call('blocks.create', { + await call('block.new', { id: '123412341234', patches: [ { @@ -47,7 +47,7 @@ describe('blocks.*', () => { }, ], }); - const {block} = await call('blocks.get', {id: '123412341234'}); + const {block} = await call('block.get', {id: '123412341234'}); expect(block).toMatchObject({ id: '123412341234', seq: 1, @@ -63,15 +63,15 @@ describe('blocks.*', () => { }); }); - describe('blocks.remove', () => { + describe('block.remove', () => { test('can remove an existing block', async () => { const {call} = setup(); - await call('blocks.create', {id: 'my-block', patches: []}); - const {block} = await call('blocks.get', {id: 'my-block'}); + await call('block.new', {id: 'my-block', patches: []}); + const {block} = await call('block.get', {id: 'my-block'}); expect(block.id).toBe('my-block'); - await call('blocks.remove', {id: 'my-block'}); + await call('block.del', {id: 'my-block'}); try { - await call('blocks.get', {id: 'my-block'}); + await call('block.get', {id: 'my-block'}); throw new Error('not this error'); } catch (err: any) { expect(err.errno).toBe(RpcErrorCodes.NOT_FOUND); @@ -79,7 +79,7 @@ describe('blocks.*', () => { }); }); - describe('blocks.edit', () => { + describe('block.upd', () => { test('can edit a document sequentially', async () => { const {call} = setup(); const id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; @@ -88,7 +88,7 @@ describe('blocks.*', () => { text: 'Hell', }); const patch1 = model.api.flush(); - await call('blocks.create', { + await call('block.new', { id, patches: [ { @@ -102,7 +102,7 @@ describe('blocks.*', () => { const patch2 = model.api.flush(); model.api.str(['text']).ins(5, ' World'); const patch3 = model.api.flush(); - await call('blocks.edit', { + await call('block.upd', { id, patches: [ { @@ -117,7 +117,7 @@ describe('blocks.*', () => { }, ], }); - const block2 = await call('blocks.get', {id}); + const block2 = await call('block.get', {id}); expect(Model.fromBinary(block2.block.blob).view()).toStrictEqual({ text: 'Hello World', }); @@ -125,7 +125,7 @@ describe('blocks.*', () => { const patch4 = model.api.flush(); model.api.str(['text']).ins(12, '!'); const patch5 = model.api.flush(); - await call('blocks.edit', { + await call('block.upd', { id, patches: [ { @@ -140,7 +140,7 @@ describe('blocks.*', () => { }, ], }); - const block3 = await call('blocks.get', {id}); + const block3 = await call('block.get', {id}); expect(Model.fromBinary(block3.block.blob).view()).toStrictEqual({ text: 'Hello, World!', }); @@ -156,7 +156,7 @@ describe('blocks.*', () => { text: 'Hell', }); const patch1 = model.api.flush(); - await call('blocks.create', { + await call('block.new', { id, patches: [ { @@ -168,11 +168,11 @@ describe('blocks.*', () => { }); // User 2 - const block2 = await call('blocks.get', {id}); + const block2 = await call('block.get', {id}); const model2 = Model.fromBinary(block2.block.blob).fork(); model2.api.str(['text']).ins(4, ' yeah!'); const patch2User2 = model2.api.flush(); - await call('blocks.edit', { + await call('block.upd', { id, patches: [ { @@ -184,7 +184,7 @@ describe('blocks.*', () => { }); expect(model2.view()).toStrictEqual({text: 'Hell yeah!'}); - const block3 = await call('blocks.get', {id}); + const block3 = await call('block.get', {id}); const model3 = Model.fromBinary(block3.block.blob).fork(); expect(model3.view()).toStrictEqual({text: 'Hell yeah!'}); @@ -193,7 +193,7 @@ describe('blocks.*', () => { const patch2 = model.api.flush(); model.api.str(['text']).ins(5, ' World'); const patch3 = model.api.flush(); - const {patches} = await call('blocks.edit', { + const {patches} = await call('block.upd', { id, patches: [ { @@ -209,7 +209,7 @@ describe('blocks.*', () => { ], }); - const block4 = await call('blocks.get', {id}); + const block4 = await call('block.get', {id}); const model4 = Model.fromBinary(block4.block.blob).fork(); expect(model4.view()).not.toStrictEqual({text: 'Hell yeah!'}); }); @@ -224,7 +224,7 @@ describe('blocks.*', () => { text: 'Hell', }); const patch1 = model.api.flush(); - await call('blocks.create', { + await call('block.new', { id, patches: [ { @@ -236,11 +236,11 @@ describe('blocks.*', () => { }); // User 2 - const block2 = await call('blocks.get', {id}); + const block2 = await call('block.get', {id}); const model2 = Model.fromBinary(block2.block.blob).fork(); model2.api.str(['text']).ins(4, ' yeah!'); const patch2User2 = model2.api.flush(); - await call('blocks.edit', { + await call('block.upd', { id, patches: [ { @@ -256,7 +256,7 @@ describe('blocks.*', () => { const patch2 = model.api.flush(); model.api.str(['text']).ins(5, ' World'); const patch3 = model.api.flush(); - const {patches} = await call('blocks.edit', { + const {patches} = await call('block.upd', { id, patches: [ { @@ -280,13 +280,13 @@ describe('blocks.*', () => { }); }); - describe('blocks.listen', () => { + describe('block.listen', () => { test('can listen for block changes', async () => { const {client} = setup(); - await client.call('blocks.create', {id: 'my-block', patches: []}); + await client.call('block.new', {id: 'my-block', patches: []}); await tick(11); const emits: any[] = []; - client.call$('blocks.listen', {id: 'my-block'}).subscribe((data) => emits.push(data)); + client.call$('block.listen', {id: 'my-block'}).subscribe((data) => emits.push(data)); const model = Model.withLogicalClock(); model.api.root({ text: 'Hell', @@ -294,7 +294,7 @@ describe('blocks.*', () => { const patch1 = model.api.flush(); await tick(12); expect(emits.length).toBe(0); - await client.call('blocks.edit', { + await client.call('block.upd', { id: 'my-block', patches: [{seq: 0, created: Date.now(), blob: patch1.toBinary()}], }); @@ -308,7 +308,7 @@ describe('blocks.*', () => { const patch2 = model.api.flush(); await tick(12); expect(emits.length).toBe(1); - await client.call('blocks.edit', { + await client.call('block.upd', { id: 'my-block', patches: [{seq: 1, created: Date.now(), blob: patch2.toBinary()}], }); @@ -321,7 +321,7 @@ describe('blocks.*', () => { test('can subscribe before block is created', async () => { const {client} = setup(); const emits: any[] = []; - client.call$('blocks.listen', {id: 'my-block'}).subscribe((data) => emits.push(data)); + client.call$('block.listen', {id: 'my-block'}).subscribe((data) => emits.push(data)); const model = Model.withLogicalClock(); model.api.root({ text: 'Hell', @@ -329,7 +329,7 @@ describe('blocks.*', () => { const patch1 = model.api.flush(); await tick(12); expect(emits.length).toBe(0); - await client.call('blocks.create', { + await client.call('block.new', { id: 'my-block', patches: [ { @@ -349,18 +349,18 @@ describe('blocks.*', () => { test('can receive deletion events', async () => { const {client} = setup(); const emits: any[] = []; - client.call$('blocks.listen', {id: 'my-block'}).subscribe((data) => emits.push(data)); - await client.call('blocks.create', {id: 'my-block', patches: []}); + client.call$('block.listen', {id: 'my-block'}).subscribe((data) => emits.push(data)); + await client.call('block.new', {id: 'my-block', patches: []}); await until(() => emits.length === 1); expect(emits[0].block.seq).toBe(-1); await tick(3); - await client.call('blocks.remove', {id: 'my-block'}); + await client.call('block.del', {id: 'my-block'}); await until(() => emits.length === 2); expect(emits[1].deleted).toBe(true); }); }); - describe('blocks.history', () => { + describe('block.history', () => { test('can retrieve change history', async () => { const {client} = setup(); const model = Model.withLogicalClock(); @@ -368,7 +368,7 @@ describe('blocks.*', () => { text: 'Hell', }); const patch1 = model.api.flush(); - await client.call('blocks.create', { + await client.call('block.new', { id: 'my-block', patches: [ { @@ -385,7 +385,7 @@ describe('blocks.*', () => { age: 26, }); const patch3 = model.api.flush(); - await client.call('blocks.edit', { + await client.call('block.upd', { id: 'my-block', patches: [ { @@ -400,7 +400,7 @@ describe('blocks.*', () => { }, ], }); - const history = await client.call('blocks.history', {id: 'my-block', min: 0, max: 2}); + const history = await client.call('block.scan', {id: 'my-block', min: 0, max: 2}); expect(history).toMatchObject({ patches: [ { @@ -423,7 +423,7 @@ describe('blocks.*', () => { }); }); - describe('blocks.get', () => { + describe('block.get', () => { test('returns whole history when block is loaded', async () => { const {client} = setup(); const model = Model.withLogicalClock(); @@ -431,7 +431,7 @@ describe('blocks.*', () => { text: 'Hell', }); const patch1 = model.api.flush(); - await client.call('blocks.create', { + await client.call('block.new', { id: 'my-block', patches: [ { @@ -447,7 +447,7 @@ describe('blocks.*', () => { age: 26, }); const patch3 = model.api.flush(); - await client.call('blocks.edit', { + await client.call('block.upd', { id: 'my-block', patches: [ { @@ -462,7 +462,7 @@ describe('blocks.*', () => { }, ], }); - const result = await client.call('blocks.get', {id: 'my-block'}); + const result = await client.call('block.get', {id: 'my-block'}); expect(result).toMatchObject({ block: expect.any(Object), patches: [ diff --git a/src/server/routes/blocks/index.ts b/src/server/routes/block/index.ts similarity index 68% rename from src/server/routes/blocks/index.ts rename to src/server/routes/block/index.ts index b219a7ce78..9306885880 100644 --- a/src/server/routes/blocks/index.ts +++ b/src/server/routes/block/index.ts @@ -1,13 +1,13 @@ -import {create} from './methods/create'; +import {new_} from './methods/new'; import {get} from './methods/get'; -import {remove} from './methods/remove'; -import {edit} from './methods/edit'; +import {upd} from './methods/upd'; +import {del} from './methods/del'; +import {scan} from './methods/scan'; import {listen} from './methods/listen'; import {Block, BlockId, BlockPatch, BlockSeq} from './schema'; -import {history} from './methods/history'; import type {RouteDeps, Router, RouterBase} from '../types'; -export const blocks = +export const block = (d: RouteDeps) => (r: Router) => { const {system} = d; @@ -19,11 +19,11 @@ export const blocks = // prettier-ignore return ( - ( create(d) + ( new_(d) ( get(d) - ( remove(d) - ( edit(d) + ( upd(d) + ( del(d) ( listen(d) - ( history(d) + ( scan(d) ( r )))))))); }; diff --git a/src/server/routes/blocks/methods/remove.ts b/src/server/routes/block/methods/del.ts similarity index 85% rename from src/server/routes/blocks/methods/remove.ts rename to src/server/routes/block/methods/del.ts index e6a7cda856..f3e28617c8 100644 --- a/src/server/routes/blocks/methods/remove.ts +++ b/src/server/routes/block/methods/del.ts @@ -1,8 +1,7 @@ import type {RouteDeps, Router, RouterBase} from '../../types'; import type {BlockId} from '../schema'; -// TODO: rename to "del". -export const remove = +export const del = ({t, services}: RouteDeps) => (r: Router) => { const Request = t.Object( @@ -20,7 +19,7 @@ export const remove = description: 'Fetches a block by ID.', }); - return r.prop('blocks.remove', Func, async ({id}) => { + return r.prop('block.del', Func, async ({id}) => { await services.blocks.remove(id); return {}; }); diff --git a/src/server/routes/blocks/methods/get.ts b/src/server/routes/block/methods/get.ts similarity index 94% rename from src/server/routes/blocks/methods/get.ts rename to src/server/routes/block/methods/get.ts index ccd496774f..2da94b63a0 100644 --- a/src/server/routes/blocks/methods/get.ts +++ b/src/server/routes/block/methods/get.ts @@ -26,7 +26,7 @@ export const get = description: 'Fetches a block by ID.', }); - return r.prop('blocks.get', Func, async ({id}) => { + return r.prop('block.get', Func, async ({id}) => { const {block, patches} = await services.blocks.get(id); return { block, diff --git a/src/server/routes/blocks/methods/listen.ts b/src/server/routes/block/methods/listen.ts similarity index 96% rename from src/server/routes/blocks/methods/listen.ts rename to src/server/routes/block/methods/listen.ts index f9272228af..7d6f9b1ce7 100644 --- a/src/server/routes/blocks/methods/listen.ts +++ b/src/server/routes/block/methods/listen.ts @@ -34,7 +34,7 @@ export const listen = description: 'Subscribe to a block to receive updates when it changes.', }); - return r.prop('blocks.listen', Func, (req$) => { + return r.prop('block.listen', Func, (req$) => { return req$.pipe(switchMap(({id}) => services.pubsub.listen$(`__block:${id}`))) as any; }); }; diff --git a/src/server/routes/blocks/methods/create.ts b/src/server/routes/block/methods/new.ts similarity index 87% rename from src/server/routes/blocks/methods/create.ts rename to src/server/routes/block/methods/new.ts index 303dbf153a..e0ac20641b 100644 --- a/src/server/routes/blocks/methods/create.ts +++ b/src/server/routes/block/methods/new.ts @@ -1,8 +1,7 @@ import type {RouteDeps, Router, RouterBase} from '../../types'; import type {BlockId, BlockPatch} from '../schema'; -// TODO: Rename to "new", like "block.new"? -export const create = +export const new_ = ({t, services}: RouteDeps) => (r: Router) => { const Request = t.Object( @@ -24,7 +23,7 @@ export const create = description: 'Creates a new block or applies patches to it.', }); - return r.prop('blocks.create', Func, async ({id, patches}) => { + return r.prop('block.new', Func, async ({id, patches}) => { const {block} = await services.blocks.create(id, patches); return {}; }); diff --git a/src/server/routes/blocks/methods/history.ts b/src/server/routes/block/methods/scan.ts similarity index 90% rename from src/server/routes/blocks/methods/history.ts rename to src/server/routes/block/methods/scan.ts index cdab447288..5af677180b 100644 --- a/src/server/routes/blocks/methods/history.ts +++ b/src/server/routes/block/methods/scan.ts @@ -1,8 +1,7 @@ import type {BlockPatch, BlockId} from '../schema'; import type {RouteDeps, Router, RouterBase} from '../../types'; -// TODO: Rename to "scan". -export const history = +export const scan = ({t, services}: RouteDeps) => (r: Router) => { const Request = t.Object( @@ -33,7 +32,7 @@ export const history = description: 'Returns a list of specified change patches for a block.', }); - return r.prop('blocks.history', Func, async ({id, min, max}) => { + return r.prop('block.scan', Func, async ({id, min, max}) => { const {patches} = await services.blocks.history(id, min, max); return {patches}; }); diff --git a/src/server/routes/blocks/methods/edit.ts b/src/server/routes/block/methods/upd.ts similarity index 93% rename from src/server/routes/blocks/methods/edit.ts rename to src/server/routes/block/methods/upd.ts index c4c938a325..987854f5f5 100644 --- a/src/server/routes/blocks/methods/edit.ts +++ b/src/server/routes/block/methods/upd.ts @@ -1,8 +1,7 @@ import type {RouteDeps, Router, RouterBase} from '../../types'; import type {BlockId, BlockPatch} from '../schema'; -// TODO: Rename to "set"? -export const edit = +export const upd = ({t, services}: RouteDeps) => (r: Router) => { const PatchType = t.Ref('BlockPatch'); @@ -39,7 +38,7 @@ export const edit = description: 'Applies patches to an existing document and returns the latest concurrent changes.', }); - return r.prop('blocks.edit', Func, async ({id, patches}) => { + return r.prop('block.upd', Func, async ({id, patches}) => { const res = await services.blocks.edit(id, patches); return { patches: res.patches, diff --git a/src/server/routes/blocks/schema.ts b/src/server/routes/block/schema.ts similarity index 100% rename from src/server/routes/blocks/schema.ts rename to src/server/routes/block/schema.ts diff --git a/src/server/routes/routes.ts b/src/server/routes/routes.ts index 5f2fc47fe8..2d5733147c 100644 --- a/src/server/routes/routes.ts +++ b/src/server/routes/routes.ts @@ -1,7 +1,7 @@ import {util} from './util'; import {pubsub} from './pubsub'; import {presence} from './presence'; -import {blocks} from './blocks'; +import {block} from './block'; import type {RouteDeps} from './types'; import type {ObjectValue} from '../../json-type-value/ObjectValue'; import type {ObjectType} from '../../json-type'; @@ -12,5 +12,5 @@ export const routes = (d: RouteDeps) => >(r: ObjectVal ( pubsub(d) ( presence(d) // TODO: rename "blocks" to "block", in all methods. - ( blocks(d) + ( block(d) ( r )))));