Skip to content

Commit

Permalink
Error system updated and docs fixed. Some tests fail
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Jul 28, 2023
1 parent 44ace80 commit c0616a9
Show file tree
Hide file tree
Showing 351 changed files with 654 additions and 892 deletions.
2 changes: 1 addition & 1 deletion packages/automation-witness-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"declarationDir": "./dist/types",
"typeRoots": []
},

"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}
2 changes: 1 addition & 1 deletion packages/automation-witness-server/tsconfig.typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*"],
"exclude": ["**/spec/*", "**/*.spec.*", "dist", "docs", "packages"],
"extends": "./tsconfig.json"
}

2 changes: 1 addition & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"declarationDir": "./dist/types",
"typeRoots": []
},

"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}
2 changes: 1 addition & 1 deletion packages/cli/tsconfig.typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*", "**/packages/*"],
"exclude": ["**/spec/*", "**/*.spec.*", "**/packages/*", "dist"],
"extends": "./tsconfig.json"
}

2 changes: 1 addition & 1 deletion packages/manifest/packages/model/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"declarationDir": "./dist/types",
"typeRoots": []
},

"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}

2 changes: 1 addition & 1 deletion packages/manifest/packages/model/tsconfig.typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*"],
"exclude": ["**/spec/*", "**/*.spec.*", "dist", "docs", "packages"],
"extends": "./tsconfig.json"
}

2 changes: 1 addition & 1 deletion packages/manifest/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"declarationDir": "./dist/types",
"typeRoots": []
},

"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}

2 changes: 1 addition & 1 deletion packages/manifest/tsconfig.typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*"],
"exclude": ["**/spec/*", "**/*.spec.*", "dist", "docs", "packages"],
"extends": "./tsconfig.json"
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assertEx } from '@xylabs/assert'
import { Account } from '@xyo-network/account'
import {
ArchivistAllQuerySchema,
ArchivistClearQuerySchema,
Expand All @@ -19,9 +18,8 @@ import {
} from '@xyo-network/archivist-model'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness } from '@xyo-network/boundwitness-model'
import { handleErrorAsync } from '@xyo-network/error'
import { AbstractModuleInstance, duplicateModules, ModuleConfig, ModuleErrorBuilder, ModuleQueryResult } from '@xyo-network/module'
import { ModuleError, Payload } from '@xyo-network/payload-model'
import { AbstractModuleInstance, duplicateModules, ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module'
import { Payload } from '@xyo-network/payload-model'
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
import { Promisable, PromisableArray } from '@xyo-network/promise'
import compact from 'lodash/compact'
Expand Down Expand Up @@ -86,7 +84,7 @@ export abstract class AbstractArchivist<
})
}

delete(hashes: string[]): PromisableArray<boolean> {
delete(hashes: string[]): PromisableArray<Payload> {
return this.busy(async () => {
await this.started('throw')
return await this.deleteHandler(hashes)
Expand Down Expand Up @@ -119,7 +117,7 @@ export abstract class AbstractArchivist<
throw Error('Not implemented')
}

protected deleteHandler(_hashes: string[]): PromisableArray<boolean> {
protected deleteHandler(_hashes: string[]): PromisableArray<Payload> {
throw Error('Not implemented')
}

Expand Down Expand Up @@ -173,69 +171,52 @@ export abstract class AbstractArchivist<
query: T,
payloads?: Payload[],
queryConfig?: TConfig,
): Promise<ModuleQueryResult> {
): Promise<ModuleQueryHandlerResult> {
const wrappedQuery = QueryBoundWitnessWrapper.parseQuery<ArchivistQuery>(query, payloads)
const queryPayload = await wrappedQuery.getQuery()
assertEx(this.queryable(query, payloads, queryConfig))
const resultPayloads: Payload[] = []
const errorPayloads: ModuleError[] = []
const queryAccount = this.ephemeralQueryAccountEnabled ? Account.randomSync() : undefined
if (this.config.storeQueries) {
await this.insertHandler([query])
}
try {
switch (queryPayload.schema) {
case ArchivistAllQuerySchema:
resultPayloads.push(...(await this.allHandler()))
break
case ArchivistClearQuerySchema:
await this.clearHandler()
break
case ArchivistCommitQuerySchema:
resultPayloads.push(...(await this.commitHandler()))
break
case ArchivistDeleteQuerySchema:
await this.deleteHandler(wrappedQuery.payloadHashes)
break
case ArchivistGetQuerySchema:
if (queryPayload.hashes?.length) {
resultPayloads.push(...(await this.getHandler(queryPayload.hashes)))
} else {
const head = await this.head()
if (head) resultPayloads.push(head)
}
break
case ArchivistInsertQuerySchema: {
const payloads = await wrappedQuery.getPayloads()
assertEx(await wrappedQuery.getPayloads(), `Missing payloads: ${JSON.stringify(wrappedQuery.payload(), null, 2)}`)
const resolvedPayloads = await PayloadWrapper.filterExclude(payloads, await wrappedQuery.hashAsync())
assertEx(
resolvedPayloads.length === payloads.length,
`Could not find some passed hashes [${resolvedPayloads.length} != ${payloads.length}]`,
)
resultPayloads.push(...(await this.insertHandler(payloads)))
// NOTE: There isn't an exact equivalence between what we get and what we store. Once
// we move to returning only inserted Payloads(/hash) instead of a BoundWitness, we
// can grab the actual last one
this._lastInsertedPayload = resolvedPayloads[resolvedPayloads.length - 1]
break

switch (queryPayload.schema) {
case ArchivistAllQuerySchema:
resultPayloads.push(...(await this.allHandler()))
break
case ArchivistClearQuerySchema:
await this.clearHandler()
break
case ArchivistCommitQuerySchema:
resultPayloads.push(...(await this.commitHandler()))
break
case ArchivistDeleteQuerySchema:
resultPayloads.push(...(await this.deleteHandler(wrappedQuery.payloadHashes)))
break
case ArchivistGetQuerySchema:
if (queryPayload.hashes?.length) {
resultPayloads.push(...(await this.getHandler(queryPayload.hashes)))
} else {
const head = await this.head()
if (head) resultPayloads.push(head)
}
default:
return super.queryHandler(query, payloads)
break
case ArchivistInsertQuerySchema: {
const payloads = await wrappedQuery.getPayloads()
assertEx(await wrappedQuery.getPayloads(), `Missing payloads: ${JSON.stringify(wrappedQuery.payload(), null, 2)}`)
const resolvedPayloads = await PayloadWrapper.filterExclude(payloads, await wrappedQuery.hashAsync())
assertEx(resolvedPayloads.length === payloads.length, `Could not find some passed hashes [${resolvedPayloads.length} != ${payloads.length}]`)
resultPayloads.push(...(await this.insertHandler(payloads)))
// NOTE: There isn't an exact equivalence between what we get and what we store. Once
// we move to returning only inserted Payloads(/hash) instead of a BoundWitness, we
// can grab the actual last one
this._lastInsertedPayload = resolvedPayloads[resolvedPayloads.length - 1]
break
}
} catch (ex) {
await handleErrorAsync(ex, async (error) => {
errorPayloads.push(
new ModuleErrorBuilder()
.sources([await wrappedQuery.hashAsync()])
.name(this.config.name ?? '<Unknown>')
.query(query.schema)
.message(error.message)
.build(),
)
})
default:
return await super.queryHandler(query, payloads)
}
return (await this.bindQueryResult(queryPayload, resultPayloads, queryAccount ? [queryAccount] : [], errorPayloads))[0]
return resultPayloads
}

protected async writeToParent(parent: ArchivistInstance, payloads: Payload[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"declarationDir": "./dist/types",
"typeRoots": []
},
"exclude": ["dist"],
"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*"],
"exclude": ["**/spec/*", "**/*.spec.*", "dist", "docs", "packages"],
"extends": "./tsconfig.json"
}

Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class FilesystemArchivist<TParams extends FilesystemArchivistParams = Fil
return await this.memoryArchivist.commit()
}

protected override deleteHandler(hashes: string[]): PromisableArray<boolean> {
protected override deleteHandler(hashes: string[]): PromisableArray<Payload> {
return this.memoryArchivist.delete(hashes)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"experimentalDecorators": true,
"typeRoots": []
},
"exclude": ["dist"],
"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*"],
"exclude": ["**/spec/*", "**/*.spec.*", "dist", "docs", "packages"],
"extends": "./tsconfig.json"
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
ArchivistModuleEventData,
ArchivistParams,
} from '@xyo-network/archivist-model'
import { BoundWitness } from '@xyo-network/boundwitness-model'
import { PayloadHasher } from '@xyo-network/core'
import { AnyConfigSchema, creatableModule } from '@xyo-network/module'
import { Payload } from '@xyo-network/payload-model'
import { clear, createStore, delMany, entries, getMany, setMany, UseStore } from 'idb-keyval'
import compact from 'lodash/compact'

export type IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'
export const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'
Expand Down Expand Up @@ -84,26 +84,33 @@ export class IndexedDbArchivist<
await clear(this.db)
}

protected override async deleteHandler(hashes: string[]): Promise<boolean[]> {
await delMany(hashes, this.db)
return hashes.map((_) => true)
protected override async deleteHandler(hashes: string[]): Promise<Payload[]> {
const payloadPairs: [string, Payload][] = await Promise.all(
(await this.get(hashes)).map<Promise<[string, Payload]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),
)

const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash)
await delMany(foundHashesToDelete, this.db)

await this.emit('deleted', { hashes: foundHashesToDelete, module: this })
const result = payloadPairs.map(([_, payload]) => payload)
return result
}

protected override async getHandler(hashes: string[]): Promise<Payload[]> {
const result = await getMany<Payload>(hashes, this.db)
return result
}

protected async insertHandler(payloads: Payload[]): Promise<BoundWitness[]> {
protected async insertHandler(payloads: Payload[]): Promise<Payload[]> {
const entries = await Promise.all(
payloads.map<Promise<[string, Payload]>>(async (payload) => {
const hash = await PayloadHasher.hashAsync(payload)
return [hash, payload]
}),
)
await setMany(entries, this.db)
const [result] = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads)
return [result[0]]
return payloads
}

protected override async startHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"experimentalDecorators": true,
"typeRoots": []
},
"exclude": ["dist"],
"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*"],
"exclude": ["**/spec/*", "**/*.spec.*", "dist", "docs", "packages"],
"extends": "./tsconfig.json"
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ArchivistModuleEventData,
} from '@xyo-network/archivist-model'
import { BoundWitness } from '@xyo-network/boundwitness-model'
import { PayloadHasher } from '@xyo-network/core'
import { AnyConfigSchema, creatableModule, ModuleInstance, ModuleParams } from '@xyo-network/module'
import { Payload } from '@xyo-network/payload-model'
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
Expand Down Expand Up @@ -78,7 +79,6 @@ export class MemoryArchivist<
compact(
Object.values((await this.parents()).commit ?? [])?.map(async (parent) => {
const queryPayload: ArchivistInsertQuery = {
payloads: await Promise.all(payloads.map((payload) => PayloadWrapper.hashAsync(payload))),
schema: ArchivistInsertQuerySchema,
}
const query = await this.bindQuery(queryPayload, payloads)
Expand All @@ -90,12 +90,20 @@ export class MemoryArchivist<
return compact(settled.filter(fulfilled).map((result) => result.value))
}

protected override async deleteHandler(hashes: string[]): Promise<boolean[]> {
const found = hashes.map((hash) => {
return this.cache.delete(hash)
})
await this.emit('deleted', { found, hashes, module: this })
return found
protected override async deleteHandler(hashes: string[]): Promise<Payload[]> {
const payloadPairs: [string, Payload][] = await Promise.all(
(await this.get(hashes)).map<Promise<[string, Payload]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),
)
const deletedPairs: [string, Payload][] = compact(
await Promise.all(
payloadPairs.map<[string, Payload] | undefined>(([hash, payload]) => {
return this.cache.delete(hash) ? [hash, payload] : undefined
}),
),
)
await this.emit('deleted', { hashes: deletedPairs.map(([hash, _]) => hash), module: this })
const result = deletedPairs.map(([_, payload]) => payload)
return result
}

protected override async getHandler(hashes: string[]): Promise<Payload[]> {
Expand All @@ -122,12 +130,7 @@ export class MemoryArchivist<
}),
)

const [result] = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads)
const parents = await this.parents()
if (Object.entries(parents.write ?? {}).length) {
// We store the child bw also
await this.writeToParents([result[0], ...payloads])
}
await this.writeToParents(payloads)
await this.emit('inserted', { module: this, payloads })
return payloads
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"experimentalDecorators": true,
"typeRoots": []
},
"exclude": ["dist"],
"exclude": ["dist", "docs"],
"extends": "@xylabs/tsconfig"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/spec/*", "**/*.spec.*"],
"exclude": ["**/spec/*", "**/*.spec.*", "dist", "docs", "packages"],
"extends": "./tsconfig.json"
}

Loading

0 comments on commit c0616a9

Please sign in to comment.