hyperpubee
Version:
Self-publishing over the decentralised internet
120 lines (91 loc) • 2.84 kB
JavaScript
const { Pubee } = require('../core/pubee')
const hexlexi = require('../core/hexlexi')
const { CONTENT, STRUCTURE, ROOT, EMBEDDING, METADATA, LINK } = require('../core/constants')
const { getAbsoluteKey } = require('../core/utils')
const { validateEmbedding } = require('../core/embedding')
const { validateLink } = require('../core/link')
class PubeeBuilder {
constructor (bee) {
if (bee.feed.length !== 0) {
throw new Error('Cannot init a pubeebuilder with a non-empty bee')
}
this.
this.
this.
this.
this.
}
async addContent (content) {
if (content.length === 0) {
throw new Error('Cannot add an empty content.')
}
const key = getAbsoluteKey(CONTENT, this.
await this.
return key
}
async
if (children.length === 0) {
throw new Error('Cannot add an empty structure.')
}
await this.
}
if (this.
this.
}
return this.
}
async addStructure (name, children) {
if (!name) {
throw new Error('Cannot add a structure without name.')
}
const generator = this.
const key = getAbsoluteKey(STRUCTURE, name, generator.next().value)
await this.
return key
}
async addEmbedding (embedding) {
validateEmbedding(embedding)
const key = getAbsoluteKey(
EMBEDDING,
this.
)
await this.
return key
}
async addLink (link) {
validateLink(link)
const key = getAbsoluteKey(
LINK,
this.
)
await this.
return key
}
async addRoot (children) {
const key = getAbsoluteKey(STRUCTURE, ROOT)
const existingRootContent = await this.
if (existingRootContent !== null) {
throw new Error('Cannot add the root more than once.')
}
await this.
return key
}
async addMetadata (metadata) {
if ((await this.
throw new Error('Metadata can only be added once')
}
await this.
}
async build () {
const res = new Pubee(this.
await res.ensureIsValid()
return res
}
}
module.exports = PubeeBuilder