-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.ts
More file actions
258 lines (230 loc) · 7.71 KB
/
Copy pathRuntime.ts
File metadata and controls
258 lines (230 loc) · 7.71 KB
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
import Lane from '../domain/api/Lane.ts';
import type SettlementPreview from '../domain/api/SettlementPreview.ts';
import type SettlementReceipt from '../domain/api/SettlementReceipt.ts';
import { LANE_IDENTITY_FAILURE } from '../domain/api/LaneIdentityFailure.ts';
import {
requireLaneRuntime,
type LaneRuntime,
} from '../domain/api/LaneRuntime.ts';
import type Warp from '../domain/api/Warp.ts';
import type SettlementPlan from '../domain/settlement/SettlementPlan.ts';
import { OPEN_WARP_IDENTITY_FAILURE } from '../domain/api/OpenWarpIdentityFailure.ts';
import { assertTimelineNameIdentity, assertWriterIdentity } from '../domain/api/assertIdentity.ts';
import WarpError from '../domain/errors/WarpError.ts';
import { requireNonEmptyString } from '../domain/utils/scalarValidation.ts';
import GitStorage from './GitStorage.ts';
import RuntimeActivity from './RuntimeActivity.ts';
import { createWorldlineLane } from './RuntimeLaneAdapter.ts';
import RuntimeMutationGate from './RuntimeMutationGate.ts';
import { bindRuntimeStorage } from './RuntimeStorageAccess.ts';
import {
previewRuntimeSettlement,
settleRuntimePlan,
} from './RuntimeSettlement.ts';
import type { RuntimeSettlementOptions } from './RuntimeSettlementOptions.ts';
import { openWarp } from './openWarp.ts';
export type { RuntimeSettlementOptions } from './RuntimeSettlementOptions.ts';
export type RuntimeOpenOptions = {
readonly at: string;
readonly writer: string;
};
export type RuntimeForkOptions = {
readonly name: string;
};
export type RuntimeStrandOptions = {
readonly name: string;
};
const FORK_IDENTITY_FAILURE = Object.freeze({
message: 'Runtime.fork requires a non-empty strand name',
code: 'E_RUNTIME_FORK_IDENTITY',
});
const STRAND_IDENTITY_FAILURE = Object.freeze({
message: 'Runtime.strand requires a non-empty strand name',
code: 'E_RUNTIME_STRAND_IDENTITY',
});
type LaneOwnershipFailure = Readonly<{
readonly message: string;
readonly code: string;
}>;
const FORK_LANE_OWNERSHIP_FAILURE = Object.freeze({
message: 'Runtime.fork requires a Lane owned by this Runtime',
code: 'E_RUNTIME_FORK_FOREIGN_LANE',
});
const STRAND_LANE_OWNERSHIP_FAILURE = Object.freeze({
message: 'Runtime.strand requires a Lane owned by this Runtime',
code: 'E_RUNTIME_STRAND_FOREIGN_LANE',
});
/** Production composition root for one local git-warp runtime. */
export default class Runtime {
readonly #activity: RuntimeActivity;
readonly #laneOwner: object;
readonly #mutations: RuntimeMutationGate;
readonly #storage: GitStorage;
readonly #warp: Warp;
private constructor(warp: Warp, storage: GitStorage) {
this.#warp = warp;
this.#storage = storage;
this.#activity = new RuntimeActivity();
this.#laneOwner = Object.freeze({});
this.#mutations = new RuntimeMutationGate();
bindRuntimeStorage(this, storage);
Object.freeze(this);
}
static async open(options: RuntimeOpenOptions): Promise<Runtime> {
assertRuntimeOpenOptions(options);
const storage = await GitStorage.open({ cwd: options.at });
try {
const warp = await openWarp({ storage, writer: options.writer });
return new Runtime(warp, storage);
} catch (error) {
try {
await storage.close();
} catch (closeError) {
throw new AggregateError(
[error, closeError],
'Runtime failed to open and release local resources',
);
}
throw error;
}
}
get writer(): string {
return this.#warp.writer;
}
async lane(name: string): Promise<Lane> {
assertTimelineNameIdentity(name, 'lane', LANE_IDENTITY_FAILURE);
return await this.#activity.run(async () => {
const timeline = await this.#warp.timeline(name);
return createWorldlineLane(
timeline,
this.#activity,
{ mutations: this.#mutations, owner: this.#laneOwner },
);
});
}
async fork(source: Lane, options: RuntimeForkOptions): Promise<Lane> {
assertForkSource(source);
assertForkOptions(options);
assertTimelineNameIdentity(options.name, 'fork.name', FORK_IDENTITY_FAILURE);
const binding = requireOwnedLaneRuntime(
source,
this.#laneOwner,
FORK_LANE_OWNERSHIP_FAILURE,
);
const fork = requireWorldlineFork(source, binding);
const lease = this.#activity.acquire();
try {
return await fork(options.name);
} finally {
lease.release();
}
}
async strand(parent: Lane, options: RuntimeStrandOptions): Promise<Lane> {
assertStrandParent(parent);
assertStrandOptions(options);
assertTimelineNameIdentity(
options.name,
'strand.name',
STRAND_IDENTITY_FAILURE,
);
const binding = requireOwnedLaneRuntime(
parent,
this.#laneOwner,
STRAND_LANE_OWNERSHIP_FAILURE,
);
const openStrand = requireWorldlineStrand(parent, binding);
const lease = this.#activity.acquire();
try {
return await openStrand(options.name);
} finally {
lease.release();
}
}
async previewSettlement(
options: RuntimeSettlementOptions,
): Promise<SettlementPreview> {
return await previewRuntimeSettlement(options, this.#laneOwner);
}
async settle(plan: SettlementPlan): Promise<SettlementReceipt> {
return await settleRuntimePlan(plan, this.#laneOwner);
}
/** Releases local resources only. */
close(): Promise<void> {
return this.#activity.close(async () => await this.#storage.close());
}
async [Symbol.asyncDispose](): Promise<void> {
await this.close();
}
}
function assertForkSource(source: Lane): void {
if (!(source instanceof Lane)) {
throw new WarpError('Runtime.fork requires a Lane', 'E_RUNTIME_FORK_SOURCE');
}
}
function assertForkOptions(options: RuntimeForkOptions): void {
if (options === null || typeof options !== 'object' || Array.isArray(options)) {
throw new WarpError('Runtime.fork options are required', 'E_RUNTIME_FORK_OPTIONS');
}
}
function assertStrandParent(parent: Lane): void {
if (!(parent instanceof Lane)) {
throw new WarpError(
'Runtime.strand requires a parent Lane',
'E_RUNTIME_STRAND_PARENT',
);
}
}
function assertStrandOptions(options: RuntimeStrandOptions): void {
if (options === null || typeof options !== 'object' || Array.isArray(options)) {
throw new WarpError(
'Runtime.strand options are required',
'E_RUNTIME_STRAND_OPTIONS',
);
}
}
function requireOwnedLaneRuntime(
source: Lane,
owner: object,
failure: LaneOwnershipFailure,
): LaneRuntime {
const binding = requireLaneRuntime(source);
if (binding.owner !== owner) {
throw new WarpError(failure.message, failure.code);
}
return binding;
}
function requireWorldlineFork(
source: Lane,
binding: LaneRuntime,
): NonNullable<LaneRuntime['fork']> {
if (source.kind !== 'worldline' || binding.fork === null) {
throw new WarpError(
'Runtime.fork supports worldline Lane sources only',
'E_RUNTIME_FORK_SOURCE_KIND',
{ context: { kind: source.kind } },
);
}
return binding.fork;
}
function requireWorldlineStrand(
parent: Lane,
binding: LaneRuntime,
): NonNullable<LaneRuntime['openStrand']> {
if (parent.kind !== 'worldline' || binding.openStrand === null) {
throw new WarpError(
'Runtime.strand requires a worldline parent Lane',
'E_RUNTIME_STRAND_PARENT_KIND',
{ context: { kind: parent.kind } },
);
}
return binding.openStrand;
}
function assertRuntimeOpenOptions(
options: RuntimeOpenOptions | null | undefined,
): asserts options is RuntimeOpenOptions {
if (options === null || options === undefined) {
throw new WarpError('Runtime.open options are required', 'E_RUNTIME_OPEN_OPTIONS');
}
requireNonEmptyString(options.at, 'runtime.at');
assertWriterIdentity(options.writer, 'writer', OPEN_WARP_IDENTITY_FAILURE);
}