-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
371 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "../.." | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MAX_DOCUMENT_SIZE = 16_000_000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.