Skip to content

Commit

Permalink
Add document protocol package
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed May 21, 2024
1 parent c401d5e commit f6c609c
Show file tree
Hide file tree
Showing 18 changed files with 371 additions and 37 deletions.
5 changes: 5 additions & 0 deletions packages/document-protocol/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions packages/document-protocol/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 5 additions & 0 deletions packages/document-protocol/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ceramic Document stream protocol

## License

Dual licensed under MIT and Apache 2
58 changes: 58 additions & 0 deletions packages/document-protocol/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@ceramic-sdk/document-protocol",
"version": "0.1.0",
"author": "3Box Labs",
"license": "(Apache-2.0 OR MIT)",
"keywords": ["ceramic", "stream", "document"],
"repository": {
"type": "git",
"url": "https://github.com/ceramicstudio/ceramic-sdk",
"directory": "packages/document-protocol"
},
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": "./dist/index.js"
},
"files": ["dist"],
"engines": {
"node": ">=20"
},
"sideEffects": false,
"scripts": {
"build:clean": "del dist",
"build:js": "swc src -d ./dist --config-file ../../.swcrc --strip-leading-paths",
"build:types": "tsc --project tsconfig.json --emitDeclarationOnly --skipLibCheck",
"build": "pnpm build:clean && pnpm build:types && pnpm build:js",
"lint": "eslint src --fix",
"test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
"test:ci": "pnpm run test --ci --coverage",
"prepare": "pnpm build",
"prepublishOnly": "package-check"
},
"dependencies": {
"@ceramic-sdk/identifiers": "workspace:^",
"@didtools/codecs": "^3.0.0",
"codeco": "^1.2.3",
"object-sizeof": "^2.6.4"
},
"devDependencies": {
"multiformats": "^13.1.0",
"ts-essentials": "^10.0.0"
},
"jest": {
"extensionsToTreatAsEsm": [".ts"],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"transform": {
"^.+\\.(t|j)s$": [
"@swc/jest",
{
"root": "../.."
}
]
}
}
}
18 changes: 18 additions & 0 deletions packages/document-protocol/src/assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sizeof from 'object-sizeof'

import { MAX_DOCUMENT_SIZE } from './constants.js'

/**
* Validate that content does not exceed the maximum allowed
* @param content Content to validate
*/
export function assertValidContentLength(content: unknown) {
if (content != null) {
const contentLength = sizeof(content)
if (contentLength > MAX_DOCUMENT_SIZE) {
throw new Error(
`Content has size of ${contentLength}B which exceeds maximum size of ${MAX_DOCUMENT_SIZE}B`,
)
}
}
}
176 changes: 176 additions & 0 deletions packages/document-protocol/src/codecs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { streamIDAsBytes, streamIDAsString } from '@ceramic-sdk/identifiers'
import {
cid,
didString,
uint8ArrayAsBase64,
uint8array,
} from '@didtools/codecs'
import {
type TypeOf,
array,
boolean,
literal,
optional,
sparse,
strict,
string,
tuple,
union,
unknown,
unknownRecord,
} from 'codeco'
import 'multiformats' // Import needed for TS reference
import 'ts-essentials' // Import needed for TS reference

/**
* JSON patch operations.
*/

export const JSONPatchAddOperation = strict(
{
op: literal('add'),
path: string,
value: unknown,
},
'JSONPatchAddOperation',
)

export const JSONPatchRemoveOperation = strict(
{
op: literal('remove'),
path: string,
},
'JSONPatchRemoveOperation',
)

export const JSONPatchReplaceOperation = strict(
{
op: literal('replace'),
path: string,
value: unknown,
},
'JSONPatchReplaceOperation',
)

export const JSONPatchMoveOperation = strict(
{
op: literal('move'),
path: string,
from: string,
},
'JSONPatchMoveOperation',
)

export const JSONPatchCopyOperation = strict(
{
op: literal('copy'),
path: string,
from: string,
},
'JSONPatchCopyOperation',
)

export const JSONPatchTestOperation = strict(
{
op: literal('test'),
path: string,
value: unknown,
},
'JSONPatchTestOperation',
)

export const JSONPatchOperation = union(
[
JSONPatchAddOperation,
JSONPatchRemoveOperation,
JSONPatchReplaceOperation,
JSONPatchMoveOperation,
JSONPatchCopyOperation,
JSONPatchTestOperation,
],
'JSONPatchOperation',
)
export type JSONPatchOperation = TypeOf<typeof JSONPatchOperation>

/**
* Init event header for a ModelInstanceDocument Stream
*/
export const DocumentInitEventHeader = sparse(
{
controller: tuple([didString]),
model: streamIDAsBytes,
sep: literal('model'),
unique: optional(uint8array),
context: optional(streamIDAsBytes),
shouldIndex: optional(boolean),
},
'DocumentInitEventHeader',
)
export type DocumentInitEventHeader = TypeOf<typeof DocumentInitEventHeader>

/**
* Init event payload for a ModelInstanceDocument Stream
*/
export const DocumentInitEventPayload = sparse(
{
data: unknownRecord,
header: DocumentInitEventHeader,
},
'DocumentInitEventPayload',
)
export type DocumentInitEventPayload = TypeOf<typeof DocumentInitEventPayload>

/**
* Data event header for a ModelInstanceDocument Stream
*/
export const DocumentDataEventHeader = strict(
{
shouldIndex: boolean,
},
'DocumentDataEventHeader',
)
export type DocumentDataEventHeader = TypeOf<typeof DocumentDataEventHeader>

/**
* Data event payload for a ModelInstanceDocument Stream
*/
export const DocumentDataEventPayload = sparse(
{
data: array(JSONPatchOperation),
prev: cid,
id: cid,
header: optional(DocumentDataEventHeader),
},
'DocumentDataEventPayload',
)
export type DocumentDataEventPayload = TypeOf<typeof DocumentDataEventPayload>

/**
* Metadata for a ModelInstanceDocument Stream
*/
export const DocumentMetadata = sparse(
{
/**
* The DID that is allowed to author updates to this ModelInstanceDocument
*/
controller: didString,
/**
* The StreamID of the Model that this ModelInstanceDocument belongs to.
*/
model: streamIDAsString,
/**
* Unique bytes
*/
unique: optional(uint8ArrayAsBase64),
/**
* The "context" StreamID for this ModelInstanceDocument.
*/
context: optional(streamIDAsString),
/**
* Whether the stream should be indexed or not.
*/
shouldIndex: optional(boolean),
},
'DocumentMetadata',
)
export type DocumentMetadata = TypeOf<typeof DocumentMetadata>
1 change: 1 addition & 0 deletions packages/document-protocol/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MAX_DOCUMENT_SIZE = 16_000_000
3 changes: 3 additions & 0 deletions packages/document-protocol/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './assertions.js'
export * from './codecs.js'
export * from './constants.js'
7 changes: 7 additions & 0 deletions packages/document-protocol/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion packages/ethereum-did/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"caip": "^1.1.1",
"did-session": "^3.1.0",
"dids": "^5.0.2",
"viem": "^2.11.0"
"viem": "^2.11.1"
},
"jest": {
"extensionsToTreatAsEsm": [".ts"],
Expand Down
4 changes: 2 additions & 2 deletions packages/events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"@ipld/dag-cbor": "^9.2.0",
"@ipld/dag-json": "^10.2.0",
"cartonne": "^3.0.1",
"codeco": "^1.2.2",
"dag-jose": "^5.0.0",
"codeco": "^1.2.3",
"dag-jose": "^5.1.0",
"multiformats": "^13.1.0",
"multihashes-sync": "^2.0.0",
"uint8arrays": "^5.1.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/identifiers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"dependencies": {
"@ipld/dag-cbor": "^9.2.0",
"codeco": "^1.2.2",
"codeco": "^1.2.3",
"mapmoize": "^1.2.1",
"multiformats": "^13.1.0",
"uint8arrays": "^5.1.0",
Expand Down
1 change: 0 additions & 1 deletion packages/model-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"dependencies": {
"@ceramic-sdk/events": "workspace:^",
"@ceramic-sdk/model-protocol": "workspace:^",
"codeco": "^1.2.2",
"dids": "^5.0.2",
"json-ptr": "^3.1.1",
"lodash.ismatch": "^4.4.0"
Expand Down
5 changes: 2 additions & 3 deletions packages/model-handler/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getModelStreamID,
validateController,
} from '@ceramic-sdk/model-protocol'
import { decode } from 'codeco'
import type { DID } from 'dids'

import {
Expand All @@ -33,9 +32,9 @@ export async function handleInitEvent(
): Promise<ModelState> {
const verified = await verifyInitEvent(context.verifier, event)

const metadata = decode(ModelMetadata, {
const metadata = ModelMetadata.encode({
controller: verified.header.controllers[0],
model: verified.header.model.bytes,
model: verified.header.model,
})
await validateController(metadata.controller, event.cacaoBlock)

Expand Down
3 changes: 2 additions & 1 deletion packages/model-handler/test/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createDID, getAuthenticatedDID } from '@ceramic-sdk/key-did'
import {
MODEL,
MODEL_RESOURCE_URI,
MODEL_STREAM_ID,
type ModelDefinition,
type ModelDefinitionV2,
type ModelInitEventPayload,
Expand Down Expand Up @@ -245,7 +246,7 @@ describe('handleInitEvent()', () => {
expect(state.id).toBe(streamID.toString())
expect(state.content).toEqual(testModelV1)
expect(state.metadata.controller).toBe(authenticatedDID.id)
expect(state.metadata.model.equals(MODEL)).toBe(true)
expect(state.metadata.model).toBe(MODEL_STREAM_ID)
expect(state.log[0]).toBe(event)
})
})
2 changes: 1 addition & 1 deletion packages/model-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@didtools/codecs": "^3.0.0",
"ajv": "^8.13.0",
"ajv-formats": "^3.0.1",
"codeco": "^1.2.2",
"codeco": "^1.2.3",
"json-ptr": "^3.1.1"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit f6c609c

Please sign in to comment.