File size: 23,665 Bytes
31e8457 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | /**
* @since 4.0.0
*/
import * as Arr from "../../Array.ts"
import * as Context from "../../Context.ts"
import * as Data from "../../Data.ts"
import * as Effect from "../../Effect.ts"
import * as Layer from "../../Layer.ts"
import * as Option from "../../Option.ts"
import * as PubSub from "../../PubSub.ts"
import * as RcMap from "../../RcMap.ts"
import * as Redacted from "../../Redacted.ts"
import * as Schema from "../../Schema.ts"
import type * as Scope from "../../Scope.ts"
import * as Semaphore from "../../Semaphore.ts"
import * as Stream from "../../Stream.ts"
import type * as Rpc from "../rpc/Rpc.ts"
import type * as RpcGroup from "../rpc/RpcGroup.ts"
import * as RpcServer from "../rpc/RpcServer.ts"
import type * as Event from "./Event.ts"
import type * as EventGroup from "./EventGroup.ts"
import * as EventJournal from "./EventJournal.ts"
import { Entry, makeEntryIdUnsafe, makeRemoteIdUnsafe, RemoteEntry, type RemoteId } from "./EventJournal.ts"
import * as EventLog from "./EventLog.ts"
import {
ChangesRpc,
type EventLogAuthentication,
EventLogProtocolError,
EventLogRemoteRpcs,
type StoreId,
WriteEntriesUnencrypted
} from "./EventLogMessage.ts"
import * as EventLogServer from "./EventLogServer.ts"
/**
* @since 4.0.0
* @category EventLogServerUnencrypted
*/
export class EventLogServerUnencrypted extends Context.Service<EventLogServerUnencrypted, {
readonly makeWrite: <Groups extends EventGroup.Any>(
schema: EventLog.EventLogSchema<Groups>
) => <
Tag extends EventGroup.Events<Groups>["tag"],
Event extends Event.Any = Event.WithTag<EventGroup.Events<Groups>, Tag>
>(options: {
readonly storeId: StoreId
readonly event: Tag
readonly payload: Event.Payload<Event>
}) => Effect.Effect<
Event.Success<Event>,
EventLogServerStoreError | Event.Error<Event>
>
}>()("effect/eventlog/EventLogServerUnencrypted") {}
/**
* @since 4.0.0
* @category EventLogServerUnencrypted
*/
export const makeWrite = <Groups extends EventGroup.Any>(
schema: EventLog.EventLogSchema<Groups>
): Effect.Effect<
<
Tag extends EventGroup.Events<Groups>["tag"],
Event extends Event.Any = Event.WithTag<EventGroup.Events<Groups>, Tag>
>(options: {
readonly storeId: StoreId
readonly event: Tag
readonly payload: Event.Payload<Event>
}) => Effect.Effect<
Event.Success<Event>,
EventLogServerStoreError | Event.Error<Event>
>,
never,
EventLogServerUnencrypted
> => EventLogServerUnencrypted.useSync((_) => _.makeWrite(schema))
/**
* @since 4.0.0
* @category Layers
*/
export const layerRpcHandlers: Layer.Layer<
Rpc.ToHandler<RpcGroup.Rpcs<typeof EventLogRemoteRpcs>> | EventLogAuthentication,
never,
Storage | StoreMapping | EventLogServerAuthorization | EventLog.Registry
> = Layer.unwrap(Effect.gen(function*() {
const storage = yield* Storage
const mapping = yield* StoreMapping
const auth = yield* EventLogServerAuthorization
const registry = yield* EventLog.Registry
const handler = yield* makeServerHandler
const remoteId = yield* storage.getId
const processEntries = Effect.fnUntraced(function*(options: {
readonly publicKey: string
readonly storeId: StoreId
readonly entries: Arr.NonEmptyReadonlyArray<Entry>
}) {
const entries = Arr.sort(options.entries, Entry.Order)
let history = yield* storage.entriesAfter(options.storeId, entries[0])
const persistedEntries = Arr.empty<Entry>()
for (const entry of entries) {
const [duplicate, conflicts, newHistory] = toConflicts(history, entry)
if (duplicate) continue
history = newHistory
yield* handler({
publicKey: options.publicKey,
storeId: options.storeId,
entry,
conflicts
})
persistedEntries.push(entry)
}
yield* storage.write(options.storeId, persistedEntries)
}, storage.withTransaction)
return EventLogServer.layerRpcHandlers({
remoteId,
getOrCreateSessionAuthBinding: (publicKey, signingPublicKey) =>
storage.getOrCreateSessionAuthBinding(publicKey, signingPublicKey),
onWrite: Effect.fnUntraced(function*(data) {
const request = yield* WriteEntriesUnencrypted.decode(data).pipe(
Effect.mapError((_) =>
new EventLogProtocolError({
requestTag: "WriteEntries",
publicKey: undefined,
code: "InternalServerError",
message: "Decoding failure"
})
)
)
if (!Arr.isReadonlyArrayNonEmpty(request.entries)) return
const resolvedStoreId = yield* mapping.resolve({
publicKey: request.publicKey,
storeId: request.storeId
}).pipe(
Effect.mapError((_) =>
new EventLogProtocolError({
requestTag: "WriteEntries",
publicKey: request.publicKey,
storeId: request.storeId,
code: "Unauthorized",
message: _.message
})
)
)
yield* auth.authorizeWrite({
publicKey: request.publicKey,
storeId: resolvedStoreId,
entries: request.entries
}).pipe(
Effect.mapError((_) =>
new EventLogProtocolError({
requestTag: "WriteEntries",
publicKey: request.publicKey,
storeId: request.storeId,
code: "Unauthorized",
message: _.message
})
)
)
yield* processEntries({
publicKey: request.publicKey,
storeId: resolvedStoreId,
entries: request.entries
}).pipe(
Effect.catchCause((_) =>
Effect.fail(
new EventLogProtocolError({
requestTag: "WriteEntries",
publicKey: request.publicKey,
code: "InternalServerError",
message: "Persistence failure"
})
)
),
Effect.provideService(EventLog.Identity, makeClientIdentity(request.publicKey))
)
}),
changes: Effect.fnUntraced(function*(request) {
const storeId = yield* mapping.resolve({
publicKey: request.publicKey,
storeId: request.storeId
})
yield* auth.authorizeRead({
publicKey: request.publicKey,
storeId
})
return storage.changes({
storeId,
startSequence: request.startSequence,
compactors: registry.compactors
}).pipe(
Stream.mapArrayEffect((entries) => Effect.map(ChangesRpc.encodeUnencrypted(entries), Arr.of))
)
}, Stream.unwrap)
})
}))
/**
* @since 4.0.0
* @category errors
*/
export class EventLogServerStoreError extends Data.TaggedError("EventLogServerStoreError")<{
readonly reason: "NotFound" | "PersistenceFailure"
readonly publicKey?: string | undefined
readonly storeId?: StoreId | undefined
readonly message?: string | undefined
}> {}
/**
* @since 4.0.0
* @category errors
*/
export class EventLogServerAuthError extends Data.TaggedError("EventLogServerAuthError")<{
readonly reason: "Unauthorized" | "Forbidden"
readonly publicKey: string
readonly storeId?: StoreId | undefined
readonly message?: string | undefined
}> {}
/**
* @since 4.0.0
* @category context
*/
export class EventLogServerAuthorization extends Context.Service<EventLogServerAuthorization, {
readonly authorizeWrite: (options: {
readonly publicKey: string
readonly storeId: StoreId
readonly entries: ReadonlyArray<Entry>
}) => Effect.Effect<void, EventLogServerAuthError>
readonly authorizeRead: (options: {
readonly publicKey: string
readonly storeId: StoreId
}) => Effect.Effect<void, EventLogServerAuthError>
readonly authorizeIdentity: (options: {
readonly publicKey: string
}) => Effect.Effect<void, EventLogServerAuthError>
}>()("effect/eventlog/EventLogServerUnencrypted/EventLogServerAuthorization") {}
/**
* @since 4.0.0
* @category context
*/
export class StoreMapping extends Context.Service<StoreMapping, {
readonly resolve: (
options: {
readonly publicKey: string
readonly storeId: StoreId
}
) => Effect.Effect<StoreId, EventLogServerStoreError>
readonly hasStore: (options: {
readonly publicKey: string
readonly storeId: StoreId
}) => Effect.Effect<boolean, EventLogServerStoreError>
}>()("effect/eventlog/EventLogServerUnencrypted/StoreMapping") {}
const toStoreNotFoundError = (options: {
readonly storeId: StoreId
readonly publicKey?: string | undefined
}) =>
new EventLogServerStoreError({
reason: "NotFound",
publicKey: options.publicKey,
storeId: options.storeId,
message: options.publicKey === undefined
? `No provisioned store found for store id: ${options.storeId}`
: `No provisioned store found for public key: ${options.publicKey} and store id: ${options.storeId}`
})
/**
* @since 4.0.0
* @category store
*/
export const layerStoreMappingStatic = (options: {
readonly storeId: StoreId
}): Layer.Layer<StoreMapping> =>
Layer.succeed(StoreMapping, {
resolve(request) {
if (request.storeId === options.storeId) {
return Effect.succeed(options.storeId)
}
return Effect.fail(toStoreNotFoundError(request))
},
hasStore: ({ storeId }) => Effect.succeed(storeId === options.storeId)
})
/**
* @since 4.0.0
* @category storage
*/
export class Storage extends Context.Service<Storage, {
readonly getId: Effect.Effect<RemoteId>
readonly getOrCreateSessionAuthBinding: (
publicKey: string,
signingPublicKey: Uint8Array<ArrayBuffer>
) => Effect.Effect<Uint8Array<ArrayBuffer>>
readonly entriesAfter: (storeId: StoreId, entry: Entry) => Effect.Effect<Array<Entry>>
readonly write: (
storeId: StoreId,
entries: ReadonlyArray<Entry>
) => Effect.Effect<ReadonlyArray<RemoteEntry>>
readonly changes: (options: {
readonly storeId: StoreId
readonly startSequence: number
readonly compactors: ReadonlyMap<string, RegisteredCompactor>
}) => Stream.Stream<RemoteEntry>
readonly withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
}>()("effect/eventlog/EventLogServerUnencrypted/Storage") {}
const makeClientIdentity = (publicKey: string): EventLog.Identity["Service"] => ({
publicKey,
privateKey: constEmptyPrivateKey
})
const constEmptyPrivateKey = Redacted.make(new Uint8Array(32))
const makeServerWriteIdentityPublicKey = (storeId: StoreId): string => `effect-eventlog-server-write:${storeId}`
const entriesAfter = (journal: Array<RemoteEntry>, startSequence: number): ReadonlyArray<RemoteEntry> =>
journal.filter((entry) => entry.remoteSequence >= startSequence)
const toConflicts = (
history: ReadonlyArray<Entry>,
originEntry: Entry
): [duplicate: boolean, conflicts: Array<Entry>, newHistory: Array<Entry>] => {
let duplicate = false
for (let i = 0; i < history.length; i++) {
const entry = history[i]
if (entry.createdAtMillis < originEntry.createdAtMillis) {
continue
} else if (entry.idString === originEntry.idString) {
duplicate = true
continue
}
const newHistory = history.slice(i)
let conflicts: Array<Entry> = []
for (let j = 0; j < newHistory.length; j++) {
const scannedEntry = history[j]!
if (scannedEntry.event === originEntry.event && scannedEntry.primaryKey === originEntry.primaryKey) {
conflicts.push(scannedEntry)
}
}
return [duplicate, conflicts, newHistory]
}
return [duplicate, [], []]
}
type RegisteredCompactor = {
readonly events: ReadonlySet<string>
readonly effect: (options: {
readonly entries: ReadonlyArray<Entry>
readonly write: (entry: Entry) => Effect.Effect<void>
}) => Effect.Effect<void>
}
const representativeSequences = (options: {
readonly remoteEntries: ReadonlyArray<RemoteEntry>
readonly compactedCount: number
}): ReadonlyArray<number> | undefined => {
if (options.compactedCount === 0) {
return []
}
if (options.compactedCount > options.remoteEntries.length) {
return undefined
}
const maxSequence = options.remoteEntries[options.remoteEntries.length - 1]!.remoteSequence
if (options.compactedCount === 1) {
return [maxSequence]
}
const selected = options.remoteEntries
.slice(0, options.compactedCount - 1)
.map((entry) => entry.remoteSequence)
selected.push(maxSequence)
for (let i = 1; i < selected.length; i++) {
if (selected[i]! <= selected[i - 1]!) {
return undefined
}
}
return selected
}
const toCompactedRemoteEntries = (options: {
readonly compacted: ReadonlyArray<Entry>
readonly remoteEntries: ReadonlyArray<RemoteEntry>
}): ReadonlyArray<RemoteEntry> | undefined => {
const sequences = representativeSequences({
remoteEntries: options.remoteEntries,
compactedCount: options.compacted.length
})
if (sequences === undefined) {
return undefined
}
return options.compacted.map((entry, index) =>
new RemoteEntry({
remoteSequence: sequences[index]!,
entry
}, { disableChecks: true })
)
}
/**
* @since 4.0.0
* @category compaction
*/
export const compactBacklog = Effect.fnUntraced(function*(options: {
readonly remoteEntries: ReadonlyArray<RemoteEntry>
readonly compactors: ReadonlyMap<string, RegisteredCompactor>
}) {
if (options.compactors.size === 0 || options.remoteEntries.length === 0) {
return options.remoteEntries
}
const compactedRemoteEntries: Array<RemoteEntry> = []
let index = 0
while (index < options.remoteEntries.length) {
const remoteEntry = options.remoteEntries[index]!
const compactor = options.compactors.get(remoteEntry.entry.event)
if (compactor === undefined) {
compactedRemoteEntries.push(remoteEntry)
index++
continue
}
const entries: Array<Entry> = [remoteEntry.entry]
const remoteGroup: Array<RemoteEntry> = [remoteEntry]
const compacted: Array<Entry> = []
index++
while (index < options.remoteEntries.length) {
const nextRemoteEntry = options.remoteEntries[index]!
const nextCompactor = options.compactors.get(nextRemoteEntry.entry.event)
if (nextCompactor !== compactor) {
break
}
entries.push(nextRemoteEntry.entry)
remoteGroup.push(nextRemoteEntry)
index++
}
yield* compactor.effect({
entries,
write(entry) {
return Effect.sync(() => {
compacted.push(entry)
})
}
}).pipe(Effect.orDie)
const projected = toCompactedRemoteEntries({
compacted,
remoteEntries: remoteGroup
})
if (projected === undefined) {
compactedRemoteEntries.push(...remoteGroup)
continue
}
compactedRemoteEntries.push(...projected)
}
return compactedRemoteEntries
})
/**
* @since 4.0.0
* @category storage
*/
export const makeStorageMemory: Effect.Effect<Storage["Service"], never, Scope.Scope> = Effect.gen(function*() {
const knownIds = new Map<string, Map<string, number>>()
const journals = new Map<string, Array<RemoteEntry>>()
const sessionAuthBindings = new Map<string, Uint8Array<ArrayBuffer>>()
const remoteId = makeRemoteIdUnsafe()
const ensureKnownIds = (storeId: StoreId): Map<string, number> => {
let storeKnownIds = knownIds.get(storeId)
if (storeKnownIds) return storeKnownIds
storeKnownIds = new Map()
knownIds.set(storeId, storeKnownIds)
return storeKnownIds
}
const ensureJournal = (storeId: StoreId): Array<RemoteEntry> => {
let journal = journals.get(storeId)
if (journal) return journal
journal = []
journals.set(storeId, journal)
return journal
}
const pubsubs = yield* RcMap.make({
lookup: (_storeId: string) =>
Effect.acquireRelease(
PubSub.unbounded<RemoteEntry>(),
PubSub.shutdown
),
idleTimeToLive: 60000
})
const write = Effect.fnUntraced(function*(storeId: StoreId, entries: ReadonlyArray<Entry>) {
const sequenceNumbers: Array<number> = []
const committed: Array<RemoteEntry> = []
const storeKnownIds = ensureKnownIds(storeId)
const journal = ensureJournal(storeId)
let lastSequenceNumber = Arr.last(journal).pipe(
Option.map((entry) => entry.remoteSequence),
Option.getOrElse(() => 0)
)
if (entries.some((entry) => storeKnownIds.has(entry.idString))) {
return yield* Effect.die("Duplicate entries")
}
for (const entry of entries) {
const remoteEntry = new RemoteEntry({
remoteSequence: ++lastSequenceNumber,
entry
}, { disableChecks: true })
sequenceNumbers.push(remoteEntry.remoteSequence)
committed.push(remoteEntry)
journal.push(remoteEntry)
storeKnownIds.set(entry.idString, remoteEntry.remoteSequence)
}
const pubsub = yield* RcMap.get(pubsubs, storeId)
yield* PubSub.publishAll(pubsub, committed)
return committed
}, Effect.scoped)
const transactionSemaphore = yield* Semaphore.make(1)
return Storage.of({
getId: Effect.succeed(remoteId),
getOrCreateSessionAuthBinding: (publicKey, signingPublicKey) =>
Effect.sync(() => {
const existing = sessionAuthBindings.get(publicKey)
if (existing) return existing
sessionAuthBindings.set(publicKey, signingPublicKey)
return signingPublicKey
}),
entriesAfter: (storeId, entry) =>
Effect.sync(() => {
const journal = ensureJournal(storeId)
return journal.filter((e) => Entry.Order(e.entry, entry) >= 0).map((e) => e.entry)
}),
write,
changes: Effect.fnUntraced(function*({ storeId, startSequence, compactors }) {
const pubsub = yield* RcMap.get(pubsubs, storeId)
const subscription = yield* PubSub.subscribe(pubsub)
const backlog = yield* compactBacklog({
remoteEntries: entriesAfter(ensureJournal(storeId), startSequence),
compactors
})
const replayedUpTo = backlog.length > 0 ? backlog[backlog.length - 1].remoteSequence : startSequence - 1
return Stream.fromArray(backlog).pipe(
Stream.concat(
Stream.fromSubscription(subscription).pipe(
Stream.filter((entry) => entry.remoteSequence > replayedUpTo)
)
)
)
}, Stream.unwrap),
withTransaction: transactionSemaphore.withPermits(1)
})
})
/**
* @since 4.0.0
* @category storage
*/
export const layerStorageMemory: Layer.Layer<Storage> = Layer.effect(Storage)(makeStorageMemory)
/**
* @since 4.0.0
* @category constructors
*/
export const make = Effect.gen(function*() {
const storage = yield* Storage
const handler = yield* makeServerHandler
return EventLogServerUnencrypted.of({
makeWrite<Groups extends EventGroup.Any>(schema: EventLog.EventLogSchema<Groups>) {
const events = new Map<string, Event.AnyWithProps>()
for (const group of schema.groups as unknown as ReadonlyArray<EventGroup.EventGroup<Event.Any>>) {
for (const [tag, event] of Object.entries(group.events)) {
events.set(tag, event)
}
}
return Effect.fnUntraced(function*(options: {
readonly storeId: StoreId
readonly event: string
readonly payload: unknown
}) {
const publicKey = makeServerWriteIdentityPublicKey(options.storeId)
const schemaEvent = events.get(options.event)
if (schemaEvent === undefined) {
return yield* Effect.die(`Event schema not found for: "${options.event}"`)
}
const entry = new EventJournal.Entry({
id: makeEntryIdUnsafe(),
event: options.event,
primaryKey: schemaEvent.primaryKey(options.payload),
payload: yield* Schema.encodeUnknownEffect(schemaEvent.payloadMsgPack)(options.payload).pipe(
Effect.mapError((_) =>
new EventLogServerStoreError({
reason: "PersistenceFailure",
publicKey: publicKey,
storeId: options.storeId,
message: "Failed to encode event"
})
)
) as Effect.Effect<any, EventLogServerStoreError>
}, { disableChecks: true })
const result = yield* handler({
publicKey,
storeId: options.storeId,
entry,
conflicts: []
}).pipe(
Effect.provideService(EventLog.Identity, makeClientIdentity(publicKey))
)
yield* storage.write(options.storeId, [entry])
return result
}, storage.withTransaction) as any
}
})
})
/**
* @since 4.0.0
* @category layers
*/
export const layerServer: Layer.Layer<
EventLogServerUnencrypted | EventLog.Registry,
never,
Storage
> = Layer.effect(EventLogServerUnencrypted, make).pipe(
Layer.provideMerge(EventLog.layerRegistry)
)
/**
* @since 4.0.0
* @category layers
*/
export const layer = <Groups extends EventGroup.Any, E, R>(
_schema: EventLog.EventLogSchema<Groups>,
layer: Layer.Layer<EventGroup.ToService<Groups>, E, R>
): Layer.Layer<
never,
E,
| Exclude<R, EventLogServerUnencrypted | EventLog.Registry>
| EventLogServerAuthorization
| RpcServer.Protocol
| Storage
| StoreMapping
> =>
RpcServer.layer(EventLogRemoteRpcs).pipe(
Layer.provide(layerRpcHandlers),
Layer.provide(layer),
Layer.provide(layerServer)
)
/**
* @since 4.0.0
* @category layers
*/
export const layerNoRpcServer = <Groups extends EventGroup.Any, E, R>(
_schema: EventLog.EventLogSchema<Groups>,
layer: Layer.Layer<EventGroup.ToService<Groups>, E, R>
): Layer.Layer<
Rpc.ToHandler<RpcGroup.Rpcs<typeof EventLogRemoteRpcs>> | EventLogAuthentication,
E,
| Exclude<R, EventLogServerUnencrypted | EventLog.Registry>
| EventLogServerAuthorization
| Storage
| StoreMapping
> =>
layerRpcHandlers.pipe(
Layer.merge(layer),
Layer.provide(layerServer)
)
const makeServerHandler = Effect.gen(function*() {
const registry = yield* EventLog.Registry
return Effect.fnUntraced(
function*(options: {
readonly publicKey: string
readonly storeId: StoreId
readonly entry: Entry
readonly conflicts: ReadonlyArray<Entry>
readonly payload?: unknown
}): Effect.fn.Return<any, any, EventLog.Identity> {
const handler = registry.handlers.get(options.entry.event)
if (handler === undefined) {
return yield* Effect.logDebug(`Event handler not found for: "${options.entry.event}"`)
}
const decodePayload = Schema.decodeUnknownEffect(handler.event.payloadMsgPack)
const decodedConflicts: Array<{ readonly entry: Entry; readonly payload: unknown }> = []
for (const conflict of options.conflicts) {
decodedConflicts.push({
entry: conflict,
payload: yield* decodePayload(conflict.payload).pipe(
Effect.updateContext((input) => Context.merge(handler.context, input))
) as any
})
}
const payloadEffect = "payload" in options
? Effect.succeed(options.payload)
: decodePayload(options.entry.payload)
return yield* payloadEffect.pipe(
Effect.mapError((_) =>
new EventLogServerStoreError({
reason: "PersistenceFailure",
publicKey: options.publicKey,
storeId: options.storeId,
message: "Failed to decode event"
})
),
Effect.flatMap((payload) =>
handler.handler({
storeId: options.storeId,
payload,
entry: options.entry,
conflicts: decodedConflicts
})
),
Effect.updateContext((input) => Context.merge(handler.context, input))
) as Effect.Effect<any, unknown, EventLog.Identity>
}
)
})
|