@cero-base/core 0.0.5 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +200 -61
- package/package.json +85 -26
- package/src/blobs/index.js +297 -0
- package/src/database/CLAUDE.md +3 -0
- package/src/database/bootstrap.js +76 -0
- package/src/database/dispatch.js +156 -0
- package/src/database/index.js +572 -0
- package/src/identity/CLAUDE.md +3 -0
- package/src/identity/index.js +232 -0
- package/src/index.js +20 -1
- package/src/lib/CLAUDE.md +3 -0
- package/src/lib/constants.js +24 -4
- package/src/lib/errors.js +150 -0
- package/src/lib/schema.js +59 -194
- package/src/lib/spec/index.js +353 -0
- package/src/lib/spec/schema.json +284 -0
- package/src/lib/utils.js +54 -49
- package/src/network/discovery.js +80 -0
- package/src/network/index.js +231 -0
- package/src/pairing/index.js +482 -0
- package/src/pairing/invite.js +199 -0
- package/src/rpc/client.js +45 -0
- package/src/rpc/index.js +141 -0
- package/src/rpc/server.js +45 -0
- package/src/storage/index.js +261 -0
- package/types/blobs/index.d.ts +169 -0
- package/types/database/bootstrap.d.ts +17 -0
- package/types/database/dispatch.d.ts +8 -0
- package/types/database/index.d.ts +329 -0
- package/types/identity/index.d.ts +160 -0
- package/types/index.d.ts +11 -0
- package/types/lib/constants.d.ts +13 -0
- package/types/lib/errors.d.ts +110 -0
- package/types/lib/schema.d.ts +53 -0
- package/types/lib/spec/index.d.ts +95 -0
- package/types/lib/utils.d.ts +39 -0
- package/types/network/discovery.d.ts +44 -0
- package/types/network/index.d.ts +115 -0
- package/types/pairing/index.d.ts +194 -0
- package/types/pairing/invite.d.ts +157 -0
- package/types/rpc/client.d.ts +18 -0
- package/types/rpc/index.d.ts +67 -0
- package/types/rpc/server.d.ts +18 -0
- package/types/storage/index.d.ts +163 -0
- package/src/lib/base.js +0 -98
- package/src/lib/batch.js +0 -95
- package/src/lib/builder.js +0 -24
- package/src/lib/collection.js +0 -208
- package/src/lib/crypto.js +0 -6
- package/src/lib/index.js +0 -6
- package/src/lib/room.js +0 -156
package/src/lib/room.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import ReadyResource from 'ready-resource'
|
|
2
|
-
import safetyCatch from 'safety-catch'
|
|
3
|
-
import HypercoreId from 'hypercore-id-encoding'
|
|
4
|
-
|
|
5
|
-
import { Collection } from './collection.js'
|
|
6
|
-
import { Batch } from './batch.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Room - Constructable ReadyResource for shared collections
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* const room = new Room(db) // create new
|
|
13
|
-
* const room = new Room(db, roomId) // open existing
|
|
14
|
-
* const room = new Room(db, invite) // join via invite
|
|
15
|
-
* await room.ready()
|
|
16
|
-
*
|
|
17
|
-
* const tasks = room.collection('tasks')
|
|
18
|
-
* await tasks.insert({ text: 'Hello', done: false })
|
|
19
|
-
*
|
|
20
|
-
* @extends ReadyResource
|
|
21
|
-
*/
|
|
22
|
-
export class Room extends ReadyResource {
|
|
23
|
-
/**
|
|
24
|
-
* @param {Cero} db - Cero instance
|
|
25
|
-
* @param {string} [arg] - Room ID or invite string (omit to create new)
|
|
26
|
-
* @param {object} [opts] - Options
|
|
27
|
-
* @param {AbortSignal} [opts.signal] - AbortSignal for cancellation
|
|
28
|
-
*/
|
|
29
|
-
constructor(db, arg, opts = {}) {
|
|
30
|
-
super()
|
|
31
|
-
|
|
32
|
-
this.db = db
|
|
33
|
-
this.schema = db.schema
|
|
34
|
-
|
|
35
|
-
this._arg = arg
|
|
36
|
-
this._opts = opts
|
|
37
|
-
|
|
38
|
-
/** @type {import('cero').Room|null} - The underlying cero room */
|
|
39
|
-
this.room = null
|
|
40
|
-
|
|
41
|
-
this.ready().catch(safetyCatch)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** Room ID (z32) */
|
|
45
|
-
get id() {
|
|
46
|
-
return this.room?.id
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Room public key */
|
|
50
|
-
get publicKey() {
|
|
51
|
-
return this.room?.key
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async _open() {
|
|
55
|
-
if (!this.db.opened) await this.db.ready()
|
|
56
|
-
|
|
57
|
-
const type = detectType(this._arg)
|
|
58
|
-
|
|
59
|
-
if (type === 'create') {
|
|
60
|
-
this.room = await this.db.rooms.create(this._opts)
|
|
61
|
-
} else if (type === 'invite') {
|
|
62
|
-
this.room = await this.db.rooms.pair(this._arg, this._opts)
|
|
63
|
-
} else {
|
|
64
|
-
this.room = await this.db.rooms.open(this._arg)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
this.profile = withSub(this.room.profile)
|
|
68
|
-
this.members = withSub(this.room.members)
|
|
69
|
-
this.invites = withSub(this.room.invites)
|
|
70
|
-
|
|
71
|
-
this.room.on('update', () => this.emit('update'))
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async _close() {
|
|
75
|
-
if (!this.room) return
|
|
76
|
-
this.db.rooms.release(this.room)
|
|
77
|
-
this.room = null
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Get a collection handle for shared data
|
|
82
|
-
* @param {string} name - Collection name
|
|
83
|
-
* @returns {Collection}
|
|
84
|
-
*/
|
|
85
|
-
collection(name) {
|
|
86
|
-
this.schema.require(name, 'room')
|
|
87
|
-
return new Collection(this, name)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Create a batch for multiple operations
|
|
92
|
-
* @returns {Batch}
|
|
93
|
-
*/
|
|
94
|
-
batch() {
|
|
95
|
-
return new Batch(this)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Shorthand — create invite and return the string */
|
|
99
|
-
async invite() {
|
|
100
|
-
if (!this.opened) await this.ready()
|
|
101
|
-
const { invite } = await this.room.invites.create()
|
|
102
|
-
return invite
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Dispatch an operation to the room
|
|
107
|
-
* @param {string} op - Operation path
|
|
108
|
-
* @param {object} data - Operation data
|
|
109
|
-
*/
|
|
110
|
-
async dispatch(op, data) {
|
|
111
|
-
if (!this.opened) await this.ready()
|
|
112
|
-
return this.room.dispatch(op, data)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Async factory — creates room + awaits ready
|
|
117
|
-
* @param {Cero} db - Cero instance
|
|
118
|
-
* @param {string} [arg] - Room ID, invite, or null to create
|
|
119
|
-
* @param {object} [opts] - Options
|
|
120
|
-
* @returns {Promise<Room>}
|
|
121
|
-
*/
|
|
122
|
-
static async from(db, arg, opts) {
|
|
123
|
-
const room = new this(db, arg, opts)
|
|
124
|
-
await room.ready()
|
|
125
|
-
return room
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// ==================== Private Helpers ====================
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Detect input type: nothing (create), id, or invite
|
|
133
|
-
* @private
|
|
134
|
-
*/
|
|
135
|
-
function detectType(input) {
|
|
136
|
-
if (!input) return 'create'
|
|
137
|
-
|
|
138
|
-
// z32 is ~52 chars, hex is 64 chars
|
|
139
|
-
if (input.length <= 64) {
|
|
140
|
-
try {
|
|
141
|
-
HypercoreId.decode(input)
|
|
142
|
-
return 'id'
|
|
143
|
-
} catch {
|
|
144
|
-
// Not a valid ID, assume invite
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return 'invite'
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function withSub(obj) {
|
|
152
|
-
if (!obj) return obj
|
|
153
|
-
const wrapped = { ...obj, sub: obj.subscribe.bind(obj) }
|
|
154
|
-
if (obj.list) wrapped.get = obj.list.bind(obj)
|
|
155
|
-
return wrapped
|
|
156
|
-
}
|