Skip to content

Commit

Permalink
Add stream client package
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed Sep 25, 2024
1 parent 5b9b23a commit 1df32e8
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 140 deletions.
5 changes: 3 additions & 2 deletions packages/model-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
},
"dependencies": {
"@ceramic-sdk/events": "workspace:^",
"@ceramic-sdk/http-client": "workspace:^",
"@ceramic-sdk/identifiers": "workspace:^",
"@ceramic-sdk/model-protocol": "workspace:^"
"@ceramic-sdk/model-protocol": "workspace:^",
"@ceramic-sdk/stream-client": "workspace:^"
},
"devDependencies": {
"@ceramic-sdk/http-client": "workspace:^",
"@ceramic-sdk/test-utils": "workspace:^",
"@didtools/key-did": "^1.0.0",
"dids": "^5.0.2"
Expand Down
36 changes: 6 additions & 30 deletions packages/model-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
createSignedInitEvent,
eventToContainer,
} from '@ceramic-sdk/events'
import { type CeramicClient, getCeramicClient } from '@ceramic-sdk/http-client'
import { StreamID } from '@ceramic-sdk/identifiers'
import {
MODEL,
Expand All @@ -14,6 +13,7 @@ import {
getModelStreamID,
validateController,
} from '@ceramic-sdk/model-protocol'
import { StreamClient } from '@ceramic-sdk/stream-client'
import type { DID } from 'dids'

const header: PartialInitEventHeader = { model: MODEL, sep: 'model' }
Expand All @@ -32,35 +32,11 @@ export async function createInitEvent(
return event
}

export type ModelClientParams = {
ceramic: CeramicClient | string
did?: DID
}

export class ModelClient {
#ceramic: CeramicClient
#did?: DID

constructor(params: ModelClientParams) {
this.#ceramic = getCeramicClient(params.ceramic)
this.#did = params.did
}

/** @private */
_getDID(provided?: DID): DID {
if (provided != null) {
return provided
}
if (this.#did != null) {
return this.#did
}
throw new Error('Missing DID')
}

export class ModelClient extends StreamClient {
async getInitEvent(streamID: StreamID | string): Promise<SignedEvent> {
const id =
typeof streamID === 'string' ? StreamID.fromString(streamID) : streamID
return await this.#ceramic.getEventType(SignedEvent, id.cid.toString())
return await this.ceramic.getEventType(SignedEvent, id.cid.toString())
}

async getPayload(
Expand All @@ -69,7 +45,7 @@ export class ModelClient {
): Promise<ModelInitEventPayload> {
const event = await this.getInitEvent(streamID)
const container = await eventToContainer(
this._getDID(verifier),
this.getDID(verifier),
ModelInitEventPayload,
event,
)
Expand All @@ -80,9 +56,9 @@ export class ModelClient {
definition: ModelDefinition,
signer?: DID,
): Promise<StreamID> {
const did = this._getDID(signer)
const did = this.getDID(signer)
const event = await createInitEvent(did, definition)
const cid = await this.#ceramic.postEventType(SignedEvent, event)
const cid = await this.ceramic.postEventType(SignedEvent, event)
return getModelStreamID(cid)
}
}
25 changes: 0 additions & 25 deletions packages/model-client/test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { EthereumDID } from '@ceramic-sdk/test-utils'
import { getAuthenticatedDID } from '@didtools/key-did'
import { jest } from '@jest/globals'
import { DID } from 'dids'

import { ModelClient, createInitEvent } from '../src/index.js'

Expand Down Expand Up @@ -76,30 +75,6 @@ describe('createInitEvent()', () => {
})

describe('ModelClient', () => {
describe('_getDID() method', () => {
test('throws if no DID is provided or set in the constructor', () => {
const client = new ModelClient({ ceramic: 'http://localhost:5101' })
expect(() => client._getDID()).toThrow('Missing DID')
})

test('returns the DID set in the constructor', () => {
const client = new ModelClient({
ceramic: 'http://localhost:5101',
did: authenticatedDID,
})
expect(client._getDID()).toBe(authenticatedDID)
})

test('returns the DID provided as argument', async () => {
const did = new DID()
const client = new ModelClient({
ceramic: 'http://localhost:5101',
did: authenticatedDID,
})
expect(client._getDID(did)).toBe(did)
})
})

describe('getInitEvent() method', () => {
test('gets the model init event', async () => {
const modelEvent = await createInitEvent(authenticatedDID, testModelV1)
Expand Down
3 changes: 2 additions & 1 deletion packages/model-instance-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
},
"dependencies": {
"@ceramic-sdk/events": "workspace:^",
"@ceramic-sdk/http-client": "workspace:^",
"@ceramic-sdk/identifiers": "workspace:^",
"@ceramic-sdk/model-instance-protocol": "workspace:^",
"@ceramic-sdk/stream-client": "workspace:^",
"@didtools/codecs": "^3.0.0",
"fast-json-patch": "^3.1.1"
},
"devDependencies": {
"@ceramic-sdk/http-client": "workspace:^",
"@didtools/key-did": "^1.0.0",
"dids": "^5.0.2",
"uint8arrays": "^5.1.0"
Expand Down
39 changes: 8 additions & 31 deletions packages/model-instance-client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { InitEventPayload, SignedEvent } from '@ceramic-sdk/events'
import { type CeramicClient, getCeramicClient } from '@ceramic-sdk/http-client'
import { CommitID, type StreamID } from '@ceramic-sdk/identifiers'
import {
DocumentEvent,
getStreamID,
} from '@ceramic-sdk/model-instance-protocol'
import { StreamClient } from '@ceramic-sdk/stream-client'
import type { DIDString } from '@didtools/codecs'
import type { DID } from 'dids'

Expand Down Expand Up @@ -35,34 +35,11 @@ export type PostDataParams<T extends UnknownContent = UnknownContent> = Omit<
controller?: DID
}

export type DocumentClientParams = {
ceramic: CeramicClient | string
did?: DID
}

export class DocumentClient {
#ceramic: CeramicClient
#did?: DID

constructor(params: DocumentClientParams) {
this.#ceramic = getCeramicClient(params.ceramic)
this.#did = params.did
}

_getDID(provided?: DID): DID {
if (provided != null) {
return provided
}
if (this.#did != null) {
return this.#did
}
throw new Error('Missing DID')
}

export class DocumentClient extends StreamClient {
async getEvent(commitID: CommitID | string): Promise<DocumentEvent> {
const id =
typeof commitID === 'string' ? CommitID.fromString(commitID) : commitID
return (await this.#ceramic.getEventType(
return (await this.ceramic.getEventType(
DocumentEvent,
id.commit.toString(),
)) as DocumentEvent
Expand All @@ -76,7 +53,7 @@ export class DocumentClient {
params.controller,
params.uniqueValue,
)
const cid = await this.#ceramic.postEventType(InitEventPayload, event)
const cid = await this.ceramic.postEventType(InitEventPayload, event)
return CommitID.fromStream(getStreamID(cid))
}

Expand All @@ -86,9 +63,9 @@ export class DocumentClient {
const { controller, ...rest } = params
const event = await createInitEvent({
...rest,
controller: this._getDID(controller),
controller: this.getDID(controller),
})
const cid = await this.#ceramic.postEventType(SignedEvent, event)
const cid = await this.ceramic.postEventType(SignedEvent, event)
return CommitID.fromStream(getStreamID(cid))
}

Expand All @@ -98,9 +75,9 @@ export class DocumentClient {
const { controller, ...rest } = params
const event = await createDataEvent({
...rest,
controller: this._getDID(controller),
controller: this.getDID(controller),
})
const cid = await this.#ceramic.postEventType(SignedEvent, event)
const cid = await this.ceramic.postEventType(SignedEvent, event)
return CommitID.fromStream(params.currentID.baseID, cid)
}
}
1 change: 0 additions & 1 deletion packages/model-instance-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export {
type DocumentClientParams,
DocumentClient,
type PostDataParams,
type PostDeterministicInitParams,
Expand Down
25 changes: 0 additions & 25 deletions packages/model-instance-client/test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@ceramic-sdk/model-instance-protocol'
import { getAuthenticatedDID } from '@didtools/key-did'
import { jest } from '@jest/globals'
import { DID } from 'dids'
import { equals } from 'uint8arrays'

import {
Expand Down Expand Up @@ -138,30 +137,6 @@ describe('createDataEvent()', () => {
})

describe('DocumentClient', () => {
describe('_getDID() method', () => {
test('throws if no DID is provided or set in the constructor', () => {
const client = new DocumentClient({ ceramic: 'http://localhost:5101' })
expect(() => client._getDID()).toThrow('Missing DID')
})

test('returns the DID set in the constructor', () => {
const client = new DocumentClient({
ceramic: 'http://localhost:5101',
did: authenticatedDID,
})
expect(client._getDID()).toBe(authenticatedDID)
})

test('returns the DID provided as argument', async () => {
const did = new DID()
const client = new DocumentClient({
ceramic: 'http://localhost:5101',
did: authenticatedDID,
})
expect(client._getDID(did)).toBe(did)
})
})

describe('getEvent() method', () => {
test('gets a MID event by commit ID', async () => {
const streamID = randomStreamID()
Expand Down
5 changes: 5 additions & 0 deletions packages/stream-client/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/stream-client/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/stream-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ceramic generic stream client

## License

Dual licensed under MIT and Apache 2
54 changes: 54 additions & 0 deletions packages/stream-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@ceramic-sdk/stream-client",
"version": "0.1.0",
"author": "3Box Labs",
"license": "(Apache-2.0 OR MIT)",
"keywords": ["ceramic", "stream", "client"],
"repository": {
"type": "git",
"url": "https://github.com/ceramicstudio/ceramic-sdk",
"directory": "packages/stream-client"
},
"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/http-client": "workspace:^"
},
"devDependencies": {
"dids": "^5.0.2"
},
"jest": {
"extensionsToTreatAsEsm": [".ts"],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"transform": {
"^.+\\.(t|j)s$": [
"@swc/jest",
{
"root": "../.."
}
]
}
}
}
31 changes: 31 additions & 0 deletions packages/stream-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { type CeramicClient, getCeramicClient } from '@ceramic-sdk/http-client'
import type { DID } from 'dids'

export type StreamClientParams = {
ceramic: CeramicClient | string
did?: DID
}

export class StreamClient {
#ceramic: CeramicClient
#did?: DID

constructor(params: StreamClientParams) {
this.#ceramic = getCeramicClient(params.ceramic)
this.#did = params.did
}

get ceramic(): CeramicClient {
return this.#ceramic
}

getDID(provided?: DID): DID {
if (provided != null) {
return provided
}
if (this.#did != null) {
return this.#did
}
throw new Error('Missing DID')
}
}
Loading

0 comments on commit 1df32e8

Please sign in to comment.