Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required updates for p2panda-js 0.6.0 and GraphQL API #4

Merged
merged 8 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

* Updates for breaking `p2panda-js` API changes [#4](https://github.com/p2panda/shirokuma/pull/4)

[unreleased]: https://github.com/p2panda/shirokuma/compare/...HEAD
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@
"debug": "^4.3.4",
"graphql": "^16.5.0",
"graphql-request": "^4.3.0",
"p2panda-js": "^0.5.0"
"p2panda-js": "^0.6.0"
}
}
30 changes: 8 additions & 22 deletions src/document/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { Fields } from '../types';

import {
authorFixture,
documentIdFixture,
entryFixture,
encodedEntryFixture,
entryArgsFixture,
Expand Down Expand Up @@ -57,22 +56,13 @@ describe('document', () => {
// These are the fields for an update operation
const fields = entryFixture(2).operation?.fields as Fields;

// This is the document id
const id = documentIdFixture();
const previous = entryFixture(2).operation?.previous as string[];

const previousOperations = entryFixture(2).operation
?.previous_operations as string[];

const entryEncoded = await updateDocument(
id,
previousOperations,
fields,
{
keyPair,
schema: schemaFixture(),
session,
},
);
const entryEncoded = await updateDocument(previous, fields, {
keyPair,
schema: schemaFixture(),
session,
});

expect(entryEncoded).toEqual(encodedEntryFixture(2).entryBytes);
});
Expand All @@ -89,13 +79,9 @@ describe('document', () => {
.mockResolvedValue(entryArgsFixture(4));
jest.spyOn(session, 'getNextArgs').mockImplementation(asyncFunctionMock);

// This is the document id
const id = documentIdFixture();

const previousOperations = entryFixture(4).operation
?.previous_operations as string[];
const previous = entryFixture(4).operation?.previous as string[];

const entryEncoded = await deleteDocument(id, previousOperations, {
const entryEncoded = await deleteDocument(previous, {
keyPair,
schema: schemaFixture(),
session,
Expand Down
21 changes: 9 additions & 12 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ export const createDocument = async (
* Returns the encoded entry that was created.
*/
export const updateDocument = async (
documentId: string,
previousOperations: string[],
previous: string[],
fields: Fields,
{ keyPair, schema, session }: Context,
): Promise<string> => {
log(`Updating document`, {
document: documentId,
previousOperations,
log(`Updating document view`, {
previous,
fields,
});

Expand All @@ -65,7 +63,7 @@ export const updateDocument = async (
const encodedOperation = encodeOperation({
action: 'update',
schemaId: schema,
previousOperations,
previous,
fields: operationFields,
});

Expand All @@ -76,7 +74,7 @@ export const updateDocument = async (
schema,
session,
},
documentId,
previous,
);

return entryEncoded;
Expand All @@ -88,16 +86,15 @@ export const updateDocument = async (
* Returns the encoded entry that was created.
*/
export const deleteDocument = async (
documentId: string,
previousOperations: string[],
previous: string[],
{ keyPair, schema, session }: Context,
): Promise<string> => {
log('Deleting document', { document: documentId, previousOperations });
log('Deleting document with view ', { previous });

const encodedOperation = encodeOperation({
action: 'delete',
schemaId: schema,
previousOperations,
previous,
});

const encodedEntry = await signPublishEntry(
Expand All @@ -107,7 +104,7 @@ export const deleteDocument = async (
schema,
session,
},
documentId,
previous,
);

return encodedEntry;
Expand Down
24 changes: 12 additions & 12 deletions src/entry/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ const log = debug('shirokuma:entry');
* Returns the encoded entry.
*/
export const signPublishEntry = async (
operationEncoded: string,
operation: string,
{ keyPair, session }: Context,
documentId?: string,
viewId?: string[],
): Promise<string> => {
const publicKey = keyPair.publicKey();
const viewIdStr = viewId ? viewId.join('_') : undefined;

log('Signing and publishing entry');
const nextArgs = await session.getNextArgs(publicKey, documentId);
const nextArgs = await session.getNextArgs(publicKey, viewIdStr);

log('Retrieved next args for', {
publicKey,
documentId,
viewId,
nextArgs,
});

const entryEncoded = signAndEncodeEntry(
const entry = signAndEncodeEntry(
{
...nextArgs,
payload: operationEncoded,
operation,
},
keyPair,
);
const entryHash = generateHash(entryEncoded);
const entryHash = generateHash(entry);
log('Signed and encoded entry');

const publishNextArgs = await session.publish(entryEncoded, operationEncoded);
const publishNextArgs = await session.publish(entry, operation);
log('Published entry');

// Cache next entry args for next publish. Use the entry hash as the document
// id for CREATE operations.
session.setNextArgs(publicKey, documentId || entryHash, publishNextArgs);
// Cache next entry args for next publish.
session.setNextArgs(publicKey, entryHash, publishNextArgs);
log('Cached next arguments');

return entryEncoded;
return entry;
};
49 changes: 15 additions & 34 deletions src/session/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
} from '../../test/fixtures';

/* Set up GraphQL server mock. It will respond to:
* - query `nextEntryArgs`: always returns entry args for sequence number 6
* - mutation `publishEntry` always returns a response as if sequence
* - query `nextArgs`: always returns entry args for sequence number 6
* - mutation `publish` always returns a response as if sequence
* number 5 had been published. */
// eslint-disable-next-line @typescript-eslint/no-var-requires
jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox());
Expand All @@ -38,7 +38,7 @@ fetchMock
},
{
data: {
nextEntryArgs: entryArgsFixture(5),
nextArgs: entryArgsFixture(5),
},
},
)
Expand All @@ -50,7 +50,7 @@ fetchMock
},
{
data: {
publishEntry: entryArgsFixture(5),
publish: entryArgsFixture(5),
},
},
);
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('Session', () => {
});
});

describe('get/setNextEntryArgs', () => {
describe('get/setnextArgs', () => {
it('returns next entry args from node', async () => {
const session = new Session('http://localhost:2020');

Expand Down Expand Up @@ -204,12 +204,8 @@ describe('Session', () => {
// These are the fields for an update operation
const fields = entryFixture(2).operation?.fields as Fields;

// This is the document id
const documentId = documentIdFixture();

// These are the previous operations
const previousOperations = entryFixture(2).operation
?.previous_operations as string[];
const previous = entryFixture(2).operation?.previous as string[];

beforeEach(async () => {
session = new Session('http://localhost:2020');
Expand All @@ -219,43 +215,32 @@ describe('Session', () => {

it('handles valid arguments', async () => {
expect(
await session.update(documentId, fields, previousOperations, {
await session.update(fields, previous, {
schema: schemaFixture(),
}),
).resolves;

expect(
await session
.setSchema(schemaFixture())
.update(documentId, fields, previousOperations),
).resolves;
expect(await session.setSchema(schemaFixture()).update(fields, previous))
.resolves;
});

it('throws when missing a required parameter', async () => {
await expect(
// @ts-ignore: We deliberately use the API wrong here
session.update(null, fields, { schema: schemaFixture() }),
session.update(null, { schema: schemaFixture() }),
).rejects.toThrow();
await expect(
// @ts-ignore: We deliberately use the API wrong here
session.update(documentId, null, { schema: schemaFixture() }),
).rejects.toThrow();
await expect(
// @ts-ignore: We deliberately use the API wrong here
session.update(documentId, fields),
session.update(fields),
).rejects.toThrow();
});
});

describe('delete', () => {
let session: Session;

// This is the document id that can be deleted
const documentId = documentIdFixture();

// These are the previous operations
const previousOperations = entryFixture(2).operation
?.previous_operations as string[];
const previous = entryFixture(2).operation?.previous as string[];

beforeEach(async () => {
session = new Session('http://localhost:2020');
Expand All @@ -265,15 +250,11 @@ describe('Session', () => {

it('handles valid arguments', async () => {
expect(
session.delete(documentId, previousOperations, {
session.delete(previous, {
schema: schemaFixture(),
}),
).resolves;
expect(
session
.setSchema(schemaFixture())
.delete(documentId, previousOperations),
).resolves;
expect(session.setSchema(schemaFixture()).delete(previous)).resolves;
});

it('throws when missing a required parameter', async () => {
Expand All @@ -284,7 +265,7 @@ describe('Session', () => {

expect(
// @ts-ignore: We deliberately use the API wrong here
session.delete(documentId),
session.delete(previous),
).rejects.toThrow();
});
});
Expand Down
Loading