better-ship 0.3.2
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/dist/application.d.ts +127 -0
- package/dist/application.d.ts.map +1 -0
- package/dist/application.js +85 -0
- package/dist/application.js.map +1 -0
- package/dist/cloudflare.d.ts +1 -0
- package/dist/cloudflare.js +0 -0
- package/dist/core-D0asUCvq.js +282 -0
- package/dist/core-D0asUCvq.js.map +1 -0
- package/dist/core.d.ts +2 -0
- package/dist/core.js +3 -0
- package/dist/index-eL4vb02m.d.ts +183 -0
- package/dist/index-eL4vb02m.d.ts.map +1 -0
- package/dist/postgres.d.ts +1 -0
- package/dist/postgres.js +0 -0
- package/dist/tanstack.d.ts +1 -0
- package/dist/tanstack.js +0 -0
- package/dist/ui.d.ts +1 -0
- package/dist/ui.js +0 -0
- package/package.json +49 -0
- package/src/application/handlers.ts +22 -0
- package/src/application/index.ts +13 -0
- package/src/application/message-bus.errors.ts +16 -0
- package/src/application/message-bus.ts +86 -0
- package/src/application/messages.ts +31 -0
- package/src/application/registry.ts +73 -0
- package/src/core/app-error.ts +18 -0
- package/src/core/defects.ts +20 -0
- package/src/core/failure-classification.ts +27 -0
- package/src/core/index.ts +29 -0
- package/src/core/logger.ts +312 -0
- package/src/core/transport-classification.ts +28 -0
- package/src/infrastructure/cloudflare/index.ts +0 -0
- package/src/infrastructure/postgres/index.ts +0 -0
- package/src/infrastructure/tanstack/index.ts +0 -0
- package/src/ui/index.ts +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Alexander Zuev
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { w as AppError } from "./index-eL4vb02m.js";
|
|
2
|
+
//#region src/application/messages.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The identity of a command or event. Receipts and the outbox are keyed by it.
|
|
5
|
+
*
|
|
6
|
+
* Internal messages use UUID v7, so receipts sort by creation. External messages use the
|
|
7
|
+
* provider's own key, `stripe:event:evt_123`, so a redelivered webhook is a duplicate by
|
|
8
|
+
* construction. The app parses and generates ids; this package only carries them.
|
|
9
|
+
*/
|
|
10
|
+
type MessageId = string;
|
|
11
|
+
/** An action to perform. One handler, returns a value. The product adds its payload. */
|
|
12
|
+
type Command<Name extends string = string> = {
|
|
13
|
+
readonly type: 'command';
|
|
14
|
+
readonly name: Name;
|
|
15
|
+
readonly id: MessageId;
|
|
16
|
+
};
|
|
17
|
+
/** A request for data. One handler, returns a value. No id: nothing claims a read. */
|
|
18
|
+
type Query<Name extends string = string> = {
|
|
19
|
+
readonly type: 'query';
|
|
20
|
+
readonly name: Name;
|
|
21
|
+
};
|
|
22
|
+
/** A fact that happened. Zero or more subscribers, returns nothing. The product adds its payload. */
|
|
23
|
+
type Event<Name extends string = string> = {
|
|
24
|
+
readonly type: 'event';
|
|
25
|
+
readonly name: Name;
|
|
26
|
+
readonly id: MessageId;
|
|
27
|
+
};
|
|
28
|
+
/** The routing fields the bus reads. `type` selects the table, `name` selects the slot. */
|
|
29
|
+
type Message = Command | Event | Query;
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/application/message-bus.errors.d.ts
|
|
32
|
+
/**
|
|
33
|
+
* The unit of work throws this when a message's effects already committed.
|
|
34
|
+
* The bus counts an event subscriber that throws it as delivered. From a command it reaches the caller.
|
|
35
|
+
*/
|
|
36
|
+
export declare class DuplicateMessageError extends AppError {
|
|
37
|
+
readonly messageId: MessageId;
|
|
38
|
+
readonly _tag = "DuplicateMessageError";
|
|
39
|
+
readonly classification = "terminal";
|
|
40
|
+
constructor(messageId: MessageId);
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/application/handlers.d.ts
|
|
44
|
+
/**
|
|
45
|
+
* Handles one command with the dependency container and returns the caller's value.
|
|
46
|
+
* A handler that writes inside a transaction opens the unit of work itself. Failures throw.
|
|
47
|
+
*/
|
|
48
|
+
type CommandHandler<TCommand extends Command, Deps, Value> = (command: TCommand, deps: Deps) => Promise<Value>;
|
|
49
|
+
/**
|
|
50
|
+
* Handles one query with the read-only view of the container.
|
|
51
|
+
* The view is a type over the same object, so a query cannot name a write capability.
|
|
52
|
+
*/
|
|
53
|
+
type QueryHandler<TQuery extends Query, QueryDeps, Value> = (query: TQuery, deps: QueryDeps) => Promise<Value>;
|
|
54
|
+
/** Completes one subscriber's work for an event. Failures throw. */
|
|
55
|
+
type EventHandler<TEvent extends Event, Deps> = (event: TEvent, deps: Deps) => Promise<void>;
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/application/registry.d.ts
|
|
58
|
+
/** One slot per command. `unknown` keeps the slot covariant; `MessageResult` recovers the value. */
|
|
59
|
+
type CommandRegistry<TCommand extends Command, Deps> = { readonly [M in TCommand as M['name']]: CommandHandler<M, Deps, unknown>; };
|
|
60
|
+
/** One slot per query, typed with the read-only view of the container. */
|
|
61
|
+
type QueryRegistry<TQuery extends Query, QueryDeps> = { readonly [M in TQuery as M['name']]: QueryHandler<M, QueryDeps, unknown>; };
|
|
62
|
+
/**
|
|
63
|
+
* Zero or more subscribers per event. Annotate the object with this type rather than
|
|
64
|
+
* `satisfies`, so an event with no subscribers is still a message the bus accepts.
|
|
65
|
+
*/
|
|
66
|
+
type EventRegistry<TEvent extends Event, Deps> = { readonly [M in TEvent as M['name']]?: ReadonlyArray<EventHandler<M, Deps>>; };
|
|
67
|
+
/**
|
|
68
|
+
* The app's three tables. `satisfies MessageRegistry<AppMessage, AppDeps, AppQueryDeps>`
|
|
69
|
+
* makes a missing handler a compile error. `Deps extends QueryDeps` is checked here, the one
|
|
70
|
+
* place both types meet: the read view must be a subset of the container.
|
|
71
|
+
*/
|
|
72
|
+
type MessageRegistry<TMessage extends Message, Deps extends QueryDeps, QueryDeps> = {
|
|
73
|
+
readonly commands: CommandRegistry<Extract<TMessage, Command>, Deps>;
|
|
74
|
+
readonly queries: QueryRegistry<Extract<TMessage, Query>, QueryDeps>;
|
|
75
|
+
readonly events: EventRegistry<Extract<TMessage, Event>, Deps>;
|
|
76
|
+
};
|
|
77
|
+
type Slot<Deps> = {
|
|
78
|
+
handle(message: Message, deps: Deps): Promise<unknown>;
|
|
79
|
+
}['handle'];
|
|
80
|
+
type Subscription<Deps> = {
|
|
81
|
+
handle(event: Event, deps: Deps): Promise<void>;
|
|
82
|
+
}['handle'];
|
|
83
|
+
/**
|
|
84
|
+
* The registry as the bus routes it: each table keyed by name. The app's names are unknown
|
|
85
|
+
* inside the package, so this is what a runtime string can index.
|
|
86
|
+
*/
|
|
87
|
+
type Handlers<Deps> = {
|
|
88
|
+
readonly commands: Readonly<Record<string, Slot<Deps>>>;
|
|
89
|
+
readonly queries: Readonly<Record<string, Slot<Deps>>>;
|
|
90
|
+
readonly events: Readonly<Partial<Record<string, ReadonlyArray<Subscription<Deps>>>>>;
|
|
91
|
+
};
|
|
92
|
+
type SlotMessage<TSlots> = Parameters<Extract<TSlots[keyof TSlots], Slot<never>>>[0];
|
|
93
|
+
type SubscriptionMessage<TSlots> = NonNullable<TSlots[keyof TSlots]> extends ReadonlyArray<infer TSubscription> ? Parameters<Extract<TSubscription, Subscription<never>>>[0] : never;
|
|
94
|
+
/** The messages a registry has a slot for, read off each slot's first parameter. */
|
|
95
|
+
type RegistryMessage<TRegistry extends Handlers<never>> = SlotMessage<TRegistry['commands']> | SlotMessage<TRegistry['queries']> | SubscriptionMessage<TRegistry['events']>;
|
|
96
|
+
type SlotValue<TSlots, TName extends string> = TName extends keyof TSlots ? Awaited<ReturnType<Extract<TSlots[TName], Slot<never>>>> : never;
|
|
97
|
+
/** The value `bus.handle(message)` resolves to: the slot's own return type, or void for an event. */
|
|
98
|
+
type MessageResult<TMessage extends Message, TRegistry extends Handlers<never>> = TMessage extends Command ? SlotValue<TRegistry['commands'], TMessage['name']> : TMessage extends Query ? SlotValue<TRegistry['queries'], TMessage['name']> : void;
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/application/message-bus.d.ts
|
|
101
|
+
/** The port an entrypoint dispatches through. `AppDeps` names this as `IMessageBus<typeof registry>`. */
|
|
102
|
+
interface IMessageBus<TRegistry extends Handlers<never>> {
|
|
103
|
+
handle<TMessage extends Message & RegistryMessage<TRegistry>>(message: TMessage): Promise<MessageResult<TMessage, TRegistry>>;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Routes one message to its slot. The bus opens no transaction and keeps no outbox:
|
|
107
|
+
* a handler that writes opens the unit of work itself.
|
|
108
|
+
*/
|
|
109
|
+
export declare class MessageBus<Deps, TRegistry extends Handlers<Deps>> implements IMessageBus<TRegistry> {
|
|
110
|
+
private readonly deps;
|
|
111
|
+
private readonly registry;
|
|
112
|
+
constructor(deps: Deps, registry: TRegistry);
|
|
113
|
+
/** Resolves to the handler's own value for a command or query, and to void for an event. */
|
|
114
|
+
handle<TMessage extends Message & RegistryMessage<TRegistry>>(message: TMessage): Promise<MessageResult<TMessage, TRegistry>>;
|
|
115
|
+
private dispatch;
|
|
116
|
+
private handleCommand;
|
|
117
|
+
private handleQuery;
|
|
118
|
+
/**
|
|
119
|
+
* Every subscriber runs to completion. One failure is rethrown as is. Several are thrown as
|
|
120
|
+
* one `AggregateError`. A `DuplicateMessageError` means that subscriber already committed on
|
|
121
|
+
* an earlier delivery, so it counts as delivered.
|
|
122
|
+
*/
|
|
123
|
+
private handleEvent;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
export type { Command, CommandHandler, CommandRegistry, Event, EventHandler, EventRegistry, Handlers, IMessageBus, Message, MessageId, MessageRegistry, MessageResult, Query, QueryHandler, QueryRegistry, RegistryMessage };
|
|
127
|
+
//# sourceMappingURL=application.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"application.d.ts","names":[],"sources":["../src/application/messages.ts","../src/application/message-bus.errors.ts","../src/application/handlers.ts","../src/application/registry.ts","../src/application/message-bus.ts"],"mappings":";;;;;;;;;KAOY;;KAGA,QAAQ;WACT;WACA,MAAM;WACN,IAAI;;;KAIH,MAAM;WACP;WACA,MAAM;;;KAIL,MAAM;WACP;WACA,MAAM;WACN,IAAI;;;KAIH,UAAU,UAAU,QAAQ;;;;;;;qBCtB3B,8BAA8B;WAIpB,WAAW;WAHvB;WACA;EAET,YAAqB,WAAW;;;;;;;;KCNtB,eAAe,iBAAiB,SAAS,MAAM,UACzD,SAAS,UACT,MAAM,SACH,QAAQ;;;;;KAMD,aAAa,eAAe,OAAO,WAAW,UACxD,OAAO,QACP,MAAM,cACH,QAAQ;;KAGD,aAAa,eAAe,OAAO,SAAS,OAAO,QAAQ,MAAM,SAAS;;;;KCjB1E,gBAAgB,iBAAiB,SAAS,oBAC1C,KAAK,YAAY,YAAY,eAAe,GAAG;;KAI/C,cAAc,eAAe,OAAO,yBACpC,KAAK,UAAU,YAAY,aAAa,GAAG;;;;;KAO3C,cAAc,eAAe,OAAO,oBACpC,KAAK,UAAU,aAAa,cAAc,aAAa,GAAG;;;;;;KAQ1D,gBAAgB,iBAAiB,SAAS,aAAa,WAAW;WACnE,UAAU,gBAAgB,QAAQ,UAAU,UAAU;WACtD,SAAS,cAAc,QAAQ,UAAU,QAAQ;WACjD,QAAQ,cAAc,QAAQ,UAAU,QAAQ;;KAKtD,KAAK;EAAU,OAAO,SAAS,SAAS,MAAM,OAAO;;KACrD,aAAa;EAAU,OAAO,OAAO,OAAO,MAAM,OAAO;;;;;;KAMlD,SAAS;WACV,UAAU,SAAS,eAAe,KAAK;WACvC,SAAS,SAAS,eAAe,KAAK;WACtC,QAAQ,SAAS,QAAQ,eAAe,cAAc,aAAa;;KAGzE,YAAY,UAAU,WAAW,QAAQ,aAAa,SAAS;KAE/D,oBAAoB,UACvB,YAAY,aAAa,iBAAiB,oBAAoB,iBAC1D,WAAW,QAAQ,eAAe;;KAI5B,gBAAgB,kBAAkB,mBAC1C,YAAY,yBACZ,YAAY,wBACZ,oBAAoB;KAEnB,UAAU,QAAQ,wBAAwB,oBAAoB,SAC/D,QAAQ,WAAW,QAAQ,OAAO,QAAQ;;KAIlC,cACV,iBAAiB,SACjB,kBAAkB,mBAChB,iBAAiB,UACjB,UAAU,uBAAuB,oBACjC,iBAAiB,QACf,UAAU,sBAAsB;;;;UC5DrB,YAAY,kBAAkB;EAC7C,OAAO,iBAAiB,UAAU,gBAAgB,YAChD,SAAS,WACR,QAAQ,cAAc,UAAU;;;;;;qBAOxB,WAAW,MAAM,kBAAkB,SAAS,kBAAkB,YAAY;mBAElE;mBACA;EAFnB,YACmB,MAAM,MACN,UAAU;;EAIvB,OAAO,iBAAiB,UAAU,gBAAgB,YACtD,SAAS,WACR,QAAQ,cAAc,UAAU;UAM3B;UASA;UASA;;;;;;UAWM"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { a as createLogger, f as unreachable, p as AppError } from "./core-D0asUCvq.js";
|
|
2
|
+
import { panic } from "better-result";
|
|
3
|
+
|
|
4
|
+
//#region src/application/message-bus.errors.ts
|
|
5
|
+
/**
|
|
6
|
+
* The unit of work throws this when a message's effects already committed.
|
|
7
|
+
* The bus counts an event subscriber that throws it as delivered. From a command it reaches the caller.
|
|
8
|
+
*/
|
|
9
|
+
var DuplicateMessageError = class extends AppError {
|
|
10
|
+
messageId;
|
|
11
|
+
_tag = "DuplicateMessageError";
|
|
12
|
+
classification = "terminal";
|
|
13
|
+
constructor(messageId) {
|
|
14
|
+
super(`message ${messageId} already committed`);
|
|
15
|
+
this.messageId = messageId;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/application/message-bus.ts
|
|
21
|
+
const logger = createLogger("message-bus");
|
|
22
|
+
/**
|
|
23
|
+
* Routes one message to its slot. The bus opens no transaction and keeps no outbox:
|
|
24
|
+
* a handler that writes opens the unit of work itself.
|
|
25
|
+
*/
|
|
26
|
+
var MessageBus = class {
|
|
27
|
+
deps;
|
|
28
|
+
registry;
|
|
29
|
+
constructor(deps, registry) {
|
|
30
|
+
this.deps = deps;
|
|
31
|
+
this.registry = registry;
|
|
32
|
+
}
|
|
33
|
+
/** Resolves to the handler's own value for a command or query, and to void for an event. */
|
|
34
|
+
async handle(message) {
|
|
35
|
+
return await this.dispatch(message);
|
|
36
|
+
}
|
|
37
|
+
dispatch(message) {
|
|
38
|
+
if (message.type === "command") return this.handleCommand(message);
|
|
39
|
+
if (message.type === "query") return this.handleQuery(message);
|
|
40
|
+
if (message.type === "event") return this.handleEvent(message);
|
|
41
|
+
return unreachable(message);
|
|
42
|
+
}
|
|
43
|
+
handleCommand(command) {
|
|
44
|
+
const handler = this.registry.commands[command.name];
|
|
45
|
+
if (handler === void 0) return panic(`No handler for command ${command.name}`);
|
|
46
|
+
logger.debug("handling_command", {
|
|
47
|
+
name: command.name,
|
|
48
|
+
id: command.id
|
|
49
|
+
});
|
|
50
|
+
return handler(command, this.deps);
|
|
51
|
+
}
|
|
52
|
+
handleQuery(query) {
|
|
53
|
+
const handler = this.registry.queries[query.name];
|
|
54
|
+
if (handler === void 0) return panic(`No handler for query ${query.name}`);
|
|
55
|
+
return handler(query, this.deps);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Every subscriber runs to completion. One failure is rethrown as is. Several are thrown as
|
|
59
|
+
* one `AggregateError`. A `DuplicateMessageError` means that subscriber already committed on
|
|
60
|
+
* an earlier delivery, so it counts as delivered.
|
|
61
|
+
*/
|
|
62
|
+
async handleEvent(event) {
|
|
63
|
+
const subscriptions = this.registry.events[event.name] ?? [];
|
|
64
|
+
const outcomes = await Promise.allSettled(subscriptions.map((subscription) => subscription(event, this.deps)));
|
|
65
|
+
const failures = [];
|
|
66
|
+
for (const [subscriber, outcome] of outcomes.entries()) {
|
|
67
|
+
if (outcome.status === "fulfilled") continue;
|
|
68
|
+
if (outcome.reason instanceof DuplicateMessageError) {
|
|
69
|
+
logger.info("duplicate_subscriber_ignored", {
|
|
70
|
+
event: event.name,
|
|
71
|
+
id: event.id,
|
|
72
|
+
subscriber
|
|
73
|
+
});
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
failures.push(outcome.reason);
|
|
77
|
+
}
|
|
78
|
+
if (failures.length === 1) throw failures[0];
|
|
79
|
+
if (failures.length > 1) throw new AggregateError(failures, `${event.name}: ${failures.length} subscribers failed`);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
//#endregion
|
|
84
|
+
export { DuplicateMessageError, MessageBus };
|
|
85
|
+
//# sourceMappingURL=application.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"application.js","names":[],"sources":["../src/application/message-bus.errors.ts","../src/application/message-bus.ts"],"sourcesContent":["import { AppError } from '@/core'\n\nimport type { MessageId } from './messages.ts'\n\n/**\n * The unit of work throws this when a message's effects already committed.\n * The bus counts an event subscriber that throws it as delivered. From a command it reaches the caller.\n */\nexport class DuplicateMessageError extends AppError {\n readonly _tag = 'DuplicateMessageError'\n readonly classification = 'terminal'\n\n constructor(readonly messageId: MessageId) {\n super(`message ${messageId} already committed`)\n }\n}\n","import { panic } from 'better-result'\n\nimport { createLogger, unreachable } from '@/core'\n\nimport { DuplicateMessageError } from './message-bus.errors.ts'\nimport type { Command, Event, Message, Query } from './messages.ts'\nimport type { Handlers, MessageResult, RegistryMessage } from './registry.ts'\n\nconst logger = createLogger('message-bus')\n\n/** The port an entrypoint dispatches through. `AppDeps` names this as `IMessageBus<typeof registry>`. */\nexport interface IMessageBus<TRegistry extends Handlers<never>> {\n handle<TMessage extends Message & RegistryMessage<TRegistry>>(\n message: TMessage,\n ): Promise<MessageResult<TMessage, TRegistry>>\n}\n\n/**\n * Routes one message to its slot. The bus opens no transaction and keeps no outbox:\n * a handler that writes opens the unit of work itself.\n */\nexport class MessageBus<Deps, TRegistry extends Handlers<Deps>> implements IMessageBus<TRegistry> {\n constructor(\n private readonly deps: Deps,\n private readonly registry: TRegistry,\n ) {}\n\n /** Resolves to the handler's own value for a command or query, and to void for an event. */\n async handle<TMessage extends Message & RegistryMessage<TRegistry>>(\n message: TMessage,\n ): Promise<MessageResult<TMessage, TRegistry>> {\n // SAFETY: the slot for this name is what `MessageResult` names, and dispatch returns its value.\n return (await this.dispatch(message)) as MessageResult<TMessage, TRegistry>\n }\n\n // oxlint-disable-next-line anti-slop/no-unknown-returns -- one table holds every slot's value; `handle` names it.\n private dispatch(message: Message): Promise<unknown> {\n if (message.type === 'command') return this.handleCommand(message)\n if (message.type === 'query') return this.handleQuery(message)\n if (message.type === 'event') return this.handleEvent(message)\n // A message parsed at a boundary cannot reach here; one that does must not resolve silently.\n return unreachable(message)\n }\n\n // oxlint-disable-next-line anti-slop/no-unknown-returns -- see dispatch.\n private handleCommand(command: Command): Promise<unknown> {\n // `satisfies` proved the registry complete, so a missing slot is a broken registry object.\n const handler = this.registry.commands[command.name]\n if (handler === undefined) return panic(`No handler for command ${command.name}`)\n logger.debug('handling_command', { name: command.name, id: command.id })\n return handler(command, this.deps)\n }\n\n // oxlint-disable-next-line anti-slop/no-unknown-returns -- see dispatch.\n private handleQuery(query: Query): Promise<unknown> {\n const handler = this.registry.queries[query.name]\n if (handler === undefined) return panic(`No handler for query ${query.name}`)\n return handler(query, this.deps)\n }\n\n /**\n * Every subscriber runs to completion. One failure is rethrown as is. Several are thrown as\n * one `AggregateError`. A `DuplicateMessageError` means that subscriber already committed on\n * an earlier delivery, so it counts as delivered.\n */\n private async handleEvent(event: Event): Promise<void> {\n const subscriptions = this.registry.events[event.name] ?? []\n const outcomes = await Promise.allSettled(\n subscriptions.map((subscription) => subscription(event, this.deps)),\n )\n const failures: unknown[] = []\n for (const [subscriber, outcome] of outcomes.entries()) {\n if (outcome.status === 'fulfilled') continue\n if (outcome.reason instanceof DuplicateMessageError) {\n // The bus swallows this error, so this line is the only record that the skip happened.\n logger.info('duplicate_subscriber_ignored', { event: event.name, id: event.id, subscriber })\n continue\n }\n failures.push(outcome.reason)\n }\n if (failures.length === 1) throw failures[0]\n if (failures.length > 1) {\n throw new AggregateError(failures, `${event.name}: ${failures.length} subscribers failed`)\n }\n }\n}\n"],"mappings":";;;;;;;;AAQA,IAAa,wBAAb,cAA2C,SAAS;CAI7B;CAHrB,AAAS,OAAO;CAChB,AAAS,iBAAiB;CAE1B,YAAY,AAAS,WAAsB;EACzC,MAAM,WAAW,UAAU,mBAAmB;EAD3B;CAErB;AACF;;;;ACPA,MAAM,SAAS,aAAa,aAAa;;;;;AAazC,IAAa,aAAb,MAAkG;CAE7E;CACA;CAFnB,YACE,AAAiB,MACjB,AAAiB,UACjB;EAFiB;EACA;CAChB;;CAGH,MAAM,OACJ,SAC6C;EAE7C,OAAQ,MAAM,KAAK,SAAS,OAAO;CACrC;CAGA,AAAQ,SAAS,SAAoC;EACnD,IAAI,QAAQ,SAAS,WAAW,OAAO,KAAK,cAAc,OAAO;EACjE,IAAI,QAAQ,SAAS,SAAS,OAAO,KAAK,YAAY,OAAO;EAC7D,IAAI,QAAQ,SAAS,SAAS,OAAO,KAAK,YAAY,OAAO;EAE7D,OAAO,YAAY,OAAO;CAC5B;CAGA,AAAQ,cAAc,SAAoC;EAExD,MAAM,UAAU,KAAK,SAAS,SAAS,QAAQ;EAC/C,IAAI,YAAY,QAAW,OAAO,MAAM,0BAA0B,QAAQ,MAAM;EAChF,OAAO,MAAM,oBAAoB;GAAE,MAAM,QAAQ;GAAM,IAAI,QAAQ;EAAG,CAAC;EACvE,OAAO,QAAQ,SAAS,KAAK,IAAI;CACnC;CAGA,AAAQ,YAAY,OAAgC;EAClD,MAAM,UAAU,KAAK,SAAS,QAAQ,MAAM;EAC5C,IAAI,YAAY,QAAW,OAAO,MAAM,wBAAwB,MAAM,MAAM;EAC5E,OAAO,QAAQ,OAAO,KAAK,IAAI;CACjC;;;;;;CAOA,MAAc,YAAY,OAA6B;EACrD,MAAM,gBAAgB,KAAK,SAAS,OAAO,MAAM,SAAS,CAAC;EAC3D,MAAM,WAAW,MAAM,QAAQ,WAC7B,cAAc,KAAK,iBAAiB,aAAa,OAAO,KAAK,IAAI,CAAC,CACpE;EACA,MAAM,WAAsB,CAAC;EAC7B,KAAK,MAAM,CAAC,YAAY,YAAY,SAAS,QAAQ,GAAG;GACtD,IAAI,QAAQ,WAAW,aAAa;GACpC,IAAI,QAAQ,kBAAkB,uBAAuB;IAEnD,OAAO,KAAK,gCAAgC;KAAE,OAAO,MAAM;KAAM,IAAI,MAAM;KAAI;IAAW,CAAC;IAC3F;GACF;GACA,SAAS,KAAK,QAAQ,MAAM;EAC9B;EACA,IAAI,SAAS,WAAW,GAAG,MAAM,SAAS;EAC1C,IAAI,SAAS,SAAS,GACpB,MAAM,IAAI,eAAe,UAAU,GAAG,MAAM,KAAK,IAAI,SAAS,OAAO,oBAAoB;CAE7F;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
File without changes
|