@crowdedkingdoms/crowdyjs 18.22.0 → 18.23.1-dev.1
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/MIGRATION.md +29 -3
- package/README.md +86 -64
- package/dist/crowdy-client.d.ts +18 -7
- package/dist/crowdy-client.d.ts.map +1 -1
- package/dist/crowdy-dsh/bridge.d.ts +72 -3
- package/dist/crowdy-dsh/bridge.d.ts.map +1 -1
- package/dist/crowdy-dsh/bridge.js +243 -12
- package/dist/crowdy-dsh/game-paths.d.ts +8 -0
- package/dist/crowdy-dsh/game-paths.d.ts.map +1 -0
- package/dist/crowdy-dsh/game-paths.js +20 -0
- package/dist/crowdy-dsh/pane.d.ts +18 -1
- package/dist/crowdy-dsh/pane.d.ts.map +1 -1
- package/dist/crowdy-dsh/pane.js +86 -2
- package/dist/crowdy-dsh/protocol.d.ts +165 -2
- package/dist/crowdy-dsh/protocol.d.ts.map +1 -1
- package/dist/crowdy-dsh/protocol.js +24 -1
- package/dist/crowdy-studio/controller.d.ts +100 -0
- package/dist/crowdy-studio/controller.d.ts.map +1 -1
- package/dist/crowdy-studio/controller.js +363 -1
- package/dist/crowdy-studio/dom-shell.d.ts +11 -0
- package/dist/crowdy-studio/dom-shell.d.ts.map +1 -1
- package/dist/crowdy-studio/dom-shell.js +182 -0
- package/dist/crowdy-studio/draft/github-draft-store.d.ts +143 -0
- package/dist/crowdy-studio/draft/github-draft-store.d.ts.map +1 -0
- package/dist/crowdy-studio/draft/github-draft-store.js +477 -0
- package/dist/crowdy-studio/draft/hash.d.ts +12 -0
- package/dist/crowdy-studio/draft/hash.d.ts.map +1 -0
- package/dist/crowdy-studio/draft/hash.js +25 -0
- package/dist/crowdy-studio/draft/index.d.ts +6 -0
- package/dist/crowdy-studio/draft/index.d.ts.map +1 -0
- package/dist/crowdy-studio/draft/index.js +5 -0
- package/dist/crowdy-studio/draft/overlay-storage.d.ts +48 -0
- package/dist/crowdy-studio/draft/overlay-storage.d.ts.map +1 -0
- package/dist/crowdy-studio/draft/overlay-storage.js +94 -0
- package/dist/crowdy-studio/draft/paths.d.ts +20 -0
- package/dist/crowdy-studio/draft/paths.d.ts.map +1 -0
- package/dist/crowdy-studio/draft/paths.js +54 -0
- package/dist/crowdy-studio/draft/types.d.ts +137 -0
- package/dist/crowdy-studio/draft/types.d.ts.map +1 -0
- package/dist/crowdy-studio/draft/types.js +29 -0
- package/dist/crowdy-studio/editor.d.ts +2 -0
- package/dist/crowdy-studio/editor.d.ts.map +1 -1
- package/dist/crowdy-studio/embed/panel.d.ts +8 -0
- package/dist/crowdy-studio/embed/panel.d.ts.map +1 -1
- package/dist/crowdy-studio/embed/panel.js +2 -0
- package/dist/crowdy-studio/github/index.d.ts +1 -1
- package/dist/crowdy-studio/github/index.d.ts.map +1 -1
- package/dist/crowdy-studio/github/transport.d.ts +59 -0
- package/dist/crowdy-studio/github/transport.d.ts.map +1 -1
- package/dist/crowdy-studio/github/transport.js +30 -1
- package/dist/crowdy-studio/index.d.ts +1 -0
- package/dist/crowdy-studio/index.d.ts.map +1 -1
- package/dist/crowdy-studio/index.js +1 -0
- package/dist/crowdy-studio/models.d.ts +1 -1
- package/dist/crowdy-studio/models.d.ts.map +1 -1
- package/dist/crowdy-studio/monaco-editor.d.ts.map +1 -1
- package/dist/crowdy-studio/monaco-editor.js +16 -0
- package/dist/crowdy-studio/mount.d.ts +2 -0
- package/dist/crowdy-studio/mount.d.ts.map +1 -1
- package/dist/crowdy-studio/mount.js +2 -0
- package/dist/crowdy-studio/repository-tree.d.ts +14 -0
- package/dist/crowdy-studio/repository-tree.d.ts.map +1 -0
- package/dist/crowdy-studio/repository-tree.js +45 -0
- package/dist/crowdy-studio/styles.d.ts +1 -1
- package/dist/crowdy-studio/styles.d.ts.map +1 -1
- package/dist/crowdy-studio/styles.js +1 -0
- package/dist/crowdy-studio/textarea-editor.d.ts.map +1 -1
- package/dist/crowdy-studio/textarea-editor.js +10 -4
- package/dist/default-origin.d.ts +6 -6
- package/dist/default-origin.d.ts.map +1 -1
- package/dist/default-origin.js +6 -6
- package/dist/domains/auth.d.ts +3 -3
- package/dist/domains/embedded-host.d.ts +19 -9
- package/dist/domains/embedded-host.d.ts.map +1 -1
- package/dist/domains/embedded-host.js +19 -9
- package/dist/domains/hosting.d.ts +12 -3
- package/dist/domains/hosting.d.ts.map +1 -1
- package/dist/domains/hosting.js +12 -3
- package/dist/domains/portal.d.ts +5 -3
- package/dist/domains/portal.d.ts.map +1 -1
- package/dist/domains/portal.js +1 -1
- package/dist/generated/graphql-schema.d.ts +84 -2
- package/dist/generated/graphql-schema.d.ts.map +1 -1
- package/dist/generated/graphql-schema.js +9 -1
- package/dist/hosting/index.d.ts +4 -0
- package/dist/hosting/index.d.ts.map +1 -1
- package/dist/hosting/index.js +4 -0
- package/dist/hosting/publish-directory.d.ts +11 -1
- package/dist/hosting/publish-directory.d.ts.map +1 -1
- package/dist/hosting/publish-directory.js +11 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hosted draft: an overlay over the bound branch, kept in the browser.
|
|
3
|
+
*
|
|
4
|
+
* The base is the project's mirror commit (`githubSha`): its tree comes from
|
|
5
|
+
* `crowdyStudioGitHubTree`, file text from `crowdyStudioGitHubFiles` (100
|
|
6
|
+
* paths per call, cached by blob id). The overlay holds every path the draft
|
|
7
|
+
* changed, created or deleted, with the blob it started from. Committing is
|
|
8
|
+
* one `crowdyStudioGitHubCommitFiles` on top of the base; when the branch has
|
|
9
|
+
* moved, the store refreshes the base, carries every edit that does not
|
|
10
|
+
* collide onto it, retries once, and otherwise reports the colliding paths as
|
|
11
|
+
* conflicts for the player to settle.
|
|
12
|
+
*/
|
|
13
|
+
import { CrowdyGraphQLError } from '../../errors.js';
|
|
14
|
+
import { gitBlobShaOfText, utf8Length } from './hash.js';
|
|
15
|
+
import { MemoryDraftOverlayStorage } from './overlay-storage.js';
|
|
16
|
+
import { isLikelyBinaryPath, normalizeRepoPath } from './paths.js';
|
|
17
|
+
import { DraftError, } from './types.js';
|
|
18
|
+
const READ_BATCH = 100;
|
|
19
|
+
const COMMIT_BATCH = 100;
|
|
20
|
+
const PERSIST_DEBOUNCE_MS = 250;
|
|
21
|
+
function isStale(error) {
|
|
22
|
+
return error instanceof CrowdyGraphQLError && error.code === 'GITHUB_STALE_SHA';
|
|
23
|
+
}
|
|
24
|
+
export class GitHubDraftStore {
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.options = options;
|
|
27
|
+
this.kind = 'github';
|
|
28
|
+
this.base = new Map();
|
|
29
|
+
this.truncated = false;
|
|
30
|
+
this.overlay = new Map();
|
|
31
|
+
/** Blob id -> text, or null when binary or over the cap. */
|
|
32
|
+
this.text = new Map();
|
|
33
|
+
this.conflictList = [];
|
|
34
|
+
this.revision = 0;
|
|
35
|
+
this.loaded = false;
|
|
36
|
+
this.disposed = false;
|
|
37
|
+
this.listeners = new Set();
|
|
38
|
+
this.queue = Promise.resolve();
|
|
39
|
+
this.persistTimer = null;
|
|
40
|
+
this.storage = options.storage ?? new MemoryDraftOverlayStorage();
|
|
41
|
+
this.maxTextBytes = options.maxTextBytes ?? 512 * 1024;
|
|
42
|
+
this.maxEntries = options.maxEntries ?? 5000;
|
|
43
|
+
this.key = `${options.appId}/${options.projectId}`;
|
|
44
|
+
this.baseCommitSha = options.baseCommitSha;
|
|
45
|
+
}
|
|
46
|
+
// ── reads ───────────────────────────────────────────────────────────────────
|
|
47
|
+
load() {
|
|
48
|
+
return this.serial(async () => {
|
|
49
|
+
await this.loadBase(this.baseCommitSha);
|
|
50
|
+
const stored = await this.storage.loadOverlay(this.key);
|
|
51
|
+
this.overlay.clear();
|
|
52
|
+
if (stored) {
|
|
53
|
+
for (const entry of stored.entries) {
|
|
54
|
+
if (normalizeRepoPath(entry.path) === entry.path)
|
|
55
|
+
this.overlay.set(entry.path, entry);
|
|
56
|
+
}
|
|
57
|
+
this.revision = Math.max(this.revision, stored.revision);
|
|
58
|
+
if (stored.baseCommitSha !== this.baseCommitSha)
|
|
59
|
+
this.rebaseOverlay();
|
|
60
|
+
}
|
|
61
|
+
this.loaded = true;
|
|
62
|
+
this.revision += 1;
|
|
63
|
+
this.emit({ changes: [], source: 'reload' });
|
|
64
|
+
return this.snapshot();
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
snapshot() {
|
|
68
|
+
const entries = [];
|
|
69
|
+
const deleted = [];
|
|
70
|
+
const paths = new Set([...this.base.keys(), ...this.overlay.keys()]);
|
|
71
|
+
for (const path of [...paths].sort()) {
|
|
72
|
+
const edit = this.overlay.get(path);
|
|
73
|
+
const baseFile = this.base.get(path);
|
|
74
|
+
if (edit && edit.content === null) {
|
|
75
|
+
if (baseFile)
|
|
76
|
+
deleted.push(path);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (edit && edit.hash) {
|
|
80
|
+
entries.push({
|
|
81
|
+
path,
|
|
82
|
+
hash: edit.hash,
|
|
83
|
+
size: utf8Length(edit.content ?? ''),
|
|
84
|
+
binary: false,
|
|
85
|
+
dirty: edit.hash !== baseFile?.sha,
|
|
86
|
+
});
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (!baseFile)
|
|
90
|
+
continue;
|
|
91
|
+
entries.push({
|
|
92
|
+
path,
|
|
93
|
+
hash: baseFile.sha,
|
|
94
|
+
size: baseFile.size,
|
|
95
|
+
binary: this.isBinaryBase(path, baseFile),
|
|
96
|
+
dirty: false,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
baseCommitSha: this.baseCommitSha,
|
|
101
|
+
revision: this.revision,
|
|
102
|
+
entries,
|
|
103
|
+
deleted,
|
|
104
|
+
truncated: this.truncated,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async read(paths) {
|
|
108
|
+
this.assertLoaded();
|
|
109
|
+
const files = [];
|
|
110
|
+
const missing = [];
|
|
111
|
+
const binary = [];
|
|
112
|
+
const fetch = [];
|
|
113
|
+
for (const raw of paths) {
|
|
114
|
+
const path = normalizeRepoPath(raw);
|
|
115
|
+
if (!path) {
|
|
116
|
+
missing.push(raw);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const edit = this.overlay.get(path);
|
|
120
|
+
if (edit) {
|
|
121
|
+
if (edit.content === null || !edit.hash)
|
|
122
|
+
missing.push(path);
|
|
123
|
+
else
|
|
124
|
+
files.push({ path, hash: edit.hash, content: edit.content });
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const baseFile = this.base.get(path);
|
|
128
|
+
if (!baseFile) {
|
|
129
|
+
missing.push(path);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (baseFile.size > this.maxTextBytes) {
|
|
133
|
+
binary.push(path);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const cached = this.text.has(baseFile.sha) ? this.text.get(baseFile.sha) : await this.storage.loadBlobText(baseFile.sha);
|
|
137
|
+
if (cached === undefined) {
|
|
138
|
+
fetch.push(path);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
this.text.set(baseFile.sha, cached);
|
|
142
|
+
if (cached === null)
|
|
143
|
+
binary.push(path);
|
|
144
|
+
else
|
|
145
|
+
files.push({ path, hash: baseFile.sha, content: cached });
|
|
146
|
+
}
|
|
147
|
+
for (let i = 0; i < fetch.length; i += READ_BATCH) {
|
|
148
|
+
const batch = fetch.slice(i, i + READ_BATCH);
|
|
149
|
+
const got = await this.options.transport.getFiles({
|
|
150
|
+
appId: this.options.appId,
|
|
151
|
+
projectId: this.options.projectId,
|
|
152
|
+
commitSha: this.baseCommitSha,
|
|
153
|
+
paths: batch,
|
|
154
|
+
});
|
|
155
|
+
for (const file of got.files) {
|
|
156
|
+
const text = file.binary || file.content === null ? null : file.content;
|
|
157
|
+
this.text.set(file.sha, text);
|
|
158
|
+
void this.storage.saveBlobText(file.sha, text).catch(() => undefined);
|
|
159
|
+
if (text === null)
|
|
160
|
+
binary.push(file.path);
|
|
161
|
+
else
|
|
162
|
+
files.push({ path: file.path, hash: file.sha, content: text });
|
|
163
|
+
}
|
|
164
|
+
missing.push(...got.missing);
|
|
165
|
+
}
|
|
166
|
+
return { files, missing, binary };
|
|
167
|
+
}
|
|
168
|
+
conflicts() {
|
|
169
|
+
return this.conflictList;
|
|
170
|
+
}
|
|
171
|
+
// ── writes ──────────────────────────────────────────────────────────────────
|
|
172
|
+
write(rawPath, content, options) {
|
|
173
|
+
return this.serial(async () => {
|
|
174
|
+
this.assertLoaded();
|
|
175
|
+
const path = this.writablePath(rawPath);
|
|
176
|
+
if (utf8Length(content) > this.maxTextBytes) {
|
|
177
|
+
throw new DraftError('DRAFT_TOO_LARGE', `${path} is larger than ${this.maxTextBytes} bytes.`);
|
|
178
|
+
}
|
|
179
|
+
const current = this.currentHash(path);
|
|
180
|
+
this.checkPrecondition(path, current, options.precondition);
|
|
181
|
+
const hash = await gitBlobShaOfText(content);
|
|
182
|
+
const baseFile = this.base.get(path);
|
|
183
|
+
const existing = this.overlay.get(path);
|
|
184
|
+
if (baseFile?.sha === hash) {
|
|
185
|
+
this.overlay.delete(path);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
this.overlay.set(path, {
|
|
189
|
+
path,
|
|
190
|
+
content,
|
|
191
|
+
hash,
|
|
192
|
+
baseSha: existing ? existing.baseSha : (baseFile?.sha ?? null),
|
|
193
|
+
updatedAt: Date.now(),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
this.text.set(hash, content);
|
|
197
|
+
this.revision += 1;
|
|
198
|
+
this.persist();
|
|
199
|
+
this.emit({ changes: [{ path, hash }], source: options.source });
|
|
200
|
+
return { path, hash, previousHash: current, revision: this.revision };
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
remove(rawPath, options) {
|
|
204
|
+
return this.serial(async () => {
|
|
205
|
+
this.assertLoaded();
|
|
206
|
+
const path = this.writablePath(rawPath);
|
|
207
|
+
const current = this.currentHash(path);
|
|
208
|
+
if (current === null)
|
|
209
|
+
throw new DraftError('DRAFT_NOT_FOUND', `${path} is not in the draft.`);
|
|
210
|
+
this.checkPrecondition(path, current, options.precondition);
|
|
211
|
+
const baseFile = this.base.get(path);
|
|
212
|
+
const existing = this.overlay.get(path);
|
|
213
|
+
if (baseFile) {
|
|
214
|
+
this.overlay.set(path, {
|
|
215
|
+
path,
|
|
216
|
+
content: null,
|
|
217
|
+
hash: null,
|
|
218
|
+
baseSha: existing ? existing.baseSha : baseFile.sha,
|
|
219
|
+
updatedAt: Date.now(),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
this.overlay.delete(path);
|
|
224
|
+
}
|
|
225
|
+
this.revision += 1;
|
|
226
|
+
this.persist();
|
|
227
|
+
this.emit({ changes: [{ path, hash: null }], source: options.source });
|
|
228
|
+
return { path, previousHash: current, revision: this.revision };
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
discard(paths) {
|
|
232
|
+
return this.serial(async () => {
|
|
233
|
+
this.assertLoaded();
|
|
234
|
+
const targets = paths ? paths.map((p) => normalizeRepoPath(p)).filter((p) => !!p) : [...this.overlay.keys()];
|
|
235
|
+
const changes = [];
|
|
236
|
+
for (const path of targets) {
|
|
237
|
+
if (!this.overlay.delete(path))
|
|
238
|
+
continue;
|
|
239
|
+
changes.push({ path, hash: this.base.get(path)?.sha ?? null });
|
|
240
|
+
}
|
|
241
|
+
this.conflictList = this.conflictList.filter((c) => !targets.includes(c.path));
|
|
242
|
+
if (changes.length === 0)
|
|
243
|
+
return;
|
|
244
|
+
this.revision += 1;
|
|
245
|
+
this.persist();
|
|
246
|
+
this.emit({ changes, source: 'player' });
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
resolveConflict(rawPath, keep) {
|
|
250
|
+
return this.serial(async () => {
|
|
251
|
+
const path = normalizeRepoPath(rawPath);
|
|
252
|
+
const conflict = this.conflictList.find((c) => c.path === path);
|
|
253
|
+
if (!path || !conflict)
|
|
254
|
+
return;
|
|
255
|
+
this.conflictList = this.conflictList.filter((c) => c.path !== path);
|
|
256
|
+
if (keep === 'theirs') {
|
|
257
|
+
this.overlay.delete(path);
|
|
258
|
+
this.revision += 1;
|
|
259
|
+
this.persist();
|
|
260
|
+
this.emit({ changes: [{ path, hash: this.base.get(path)?.sha ?? null }], source: 'player' });
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const edit = this.overlay.get(path);
|
|
264
|
+
if (edit)
|
|
265
|
+
this.overlay.set(path, { ...edit, baseSha: this.base.get(path)?.sha ?? null });
|
|
266
|
+
this.persist();
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
commit(request) {
|
|
270
|
+
return this.serial(async () => {
|
|
271
|
+
this.assertLoaded();
|
|
272
|
+
try {
|
|
273
|
+
return await this.commitOnce(request);
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
if (!isStale(error))
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
// The branch moved: bring the base forward, carry what does not collide, retry once.
|
|
280
|
+
const refreshed = await this.options.transport.refresh({
|
|
281
|
+
appId: this.options.appId,
|
|
282
|
+
projectId: this.options.projectId,
|
|
283
|
+
});
|
|
284
|
+
if (!refreshed.githubSha)
|
|
285
|
+
throw new DraftError('DRAFT_UNAVAILABLE', 'The project is no longer bound to GitHub.');
|
|
286
|
+
await this.loadBase(refreshed.githubSha);
|
|
287
|
+
this.rebaseOverlay();
|
|
288
|
+
this.revision += 1;
|
|
289
|
+
this.persist();
|
|
290
|
+
this.emit({ changes: [], source: 'rebase' });
|
|
291
|
+
const blocking = this.conflictList.filter((c) => !request.paths || request.paths.includes(c.path));
|
|
292
|
+
if (blocking.length > 0) {
|
|
293
|
+
throw new DraftError('DRAFT_CONFLICT', `The branch moved and changed ${blocking.map((c) => c.path).join(', ')} too. Keep the draft or take the branch's version, then commit again.`, blocking);
|
|
294
|
+
}
|
|
295
|
+
return this.commitOnce(request);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
subscribe(listener) {
|
|
299
|
+
this.listeners.add(listener);
|
|
300
|
+
return () => this.listeners.delete(listener);
|
|
301
|
+
}
|
|
302
|
+
dispose() {
|
|
303
|
+
if (this.persistTimer) {
|
|
304
|
+
clearTimeout(this.persistTimer);
|
|
305
|
+
this.persistTimer = null;
|
|
306
|
+
void this.flush();
|
|
307
|
+
}
|
|
308
|
+
this.disposed = true;
|
|
309
|
+
this.listeners.clear();
|
|
310
|
+
}
|
|
311
|
+
// ── internals ───────────────────────────────────────────────────────────────
|
|
312
|
+
async commitOnce(request) {
|
|
313
|
+
const wanted = request.paths?.map((p) => normalizeRepoPath(p)).filter((p) => !!p);
|
|
314
|
+
const dirty = [...this.overlay.values()].filter((edit) => {
|
|
315
|
+
if (wanted && !wanted.includes(edit.path))
|
|
316
|
+
return false;
|
|
317
|
+
return edit.content === null ? this.base.has(edit.path) : edit.hash !== this.base.get(edit.path)?.sha;
|
|
318
|
+
});
|
|
319
|
+
if (dirty.length === 0)
|
|
320
|
+
throw new DraftError('DRAFT_NOT_FOUND', 'The draft has nothing to commit.');
|
|
321
|
+
const conflicted = this.conflictList.filter((c) => dirty.some((d) => d.path === c.path));
|
|
322
|
+
if (conflicted.length > 0) {
|
|
323
|
+
throw new DraftError('DRAFT_CONFLICT', `Settle ${conflicted.map((c) => c.path).join(', ')} before committing.`, conflicted);
|
|
324
|
+
}
|
|
325
|
+
const message = request.message.trim().slice(0, 200) || `Update ${dirty.length} file${dirty.length === 1 ? '' : 's'}`;
|
|
326
|
+
let commitSha = this.baseCommitSha;
|
|
327
|
+
const committed = [];
|
|
328
|
+
// One commit when the draft fits a call; larger drafts stack commits on each other.
|
|
329
|
+
for (let i = 0; i < dirty.length; i += COMMIT_BATCH) {
|
|
330
|
+
const batch = dirty.slice(i, i + COMMIT_BATCH);
|
|
331
|
+
const result = await this.options.transport.commitFiles({
|
|
332
|
+
appId: this.options.appId,
|
|
333
|
+
projectId: this.options.projectId,
|
|
334
|
+
message: dirty.length > COMMIT_BATCH ? `${message} (${i / COMMIT_BATCH + 1})` : message,
|
|
335
|
+
expectedCommitSha: commitSha,
|
|
336
|
+
changes: batch.map((edit) => edit.content === null ? { path: edit.path, delete: true } : { path: edit.path, content: edit.content }),
|
|
337
|
+
});
|
|
338
|
+
commitSha = result.commitSha;
|
|
339
|
+
for (const edit of batch) {
|
|
340
|
+
committed.push(edit.path);
|
|
341
|
+
if (edit.content === null)
|
|
342
|
+
this.base.delete(edit.path);
|
|
343
|
+
else
|
|
344
|
+
this.base.set(edit.path, { sha: edit.hash, size: utf8Length(edit.content) });
|
|
345
|
+
const now = this.overlay.get(edit.path);
|
|
346
|
+
if (now && now.updatedAt === edit.updatedAt)
|
|
347
|
+
this.overlay.delete(edit.path);
|
|
348
|
+
else if (now)
|
|
349
|
+
this.overlay.set(edit.path, { ...now, baseSha: this.base.get(edit.path)?.sha ?? null });
|
|
350
|
+
}
|
|
351
|
+
this.baseCommitSha = commitSha;
|
|
352
|
+
}
|
|
353
|
+
this.revision += 1;
|
|
354
|
+
this.persist();
|
|
355
|
+
this.emit({
|
|
356
|
+
changes: committed.map((path) => ({ path, hash: this.base.get(path)?.sha ?? null })),
|
|
357
|
+
source: 'commit',
|
|
358
|
+
});
|
|
359
|
+
return { commitSha, committed, baseCommitSha: commitSha };
|
|
360
|
+
}
|
|
361
|
+
async loadBase(commitSha) {
|
|
362
|
+
const tree = await this.options.transport.tree({
|
|
363
|
+
appId: this.options.appId,
|
|
364
|
+
projectId: this.options.projectId,
|
|
365
|
+
commitSha,
|
|
366
|
+
});
|
|
367
|
+
const next = new Map();
|
|
368
|
+
let count = 0;
|
|
369
|
+
let dropped = false;
|
|
370
|
+
for (const entry of tree.entries) {
|
|
371
|
+
if (entry.type !== 'blob' || !entry.sha)
|
|
372
|
+
continue;
|
|
373
|
+
if (normalizeRepoPath(entry.path) !== entry.path)
|
|
374
|
+
continue;
|
|
375
|
+
if (count >= this.maxEntries) {
|
|
376
|
+
dropped = true;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
next.set(entry.path, { sha: entry.sha, size: entry.size ?? 0 });
|
|
380
|
+
count += 1;
|
|
381
|
+
}
|
|
382
|
+
this.base = next;
|
|
383
|
+
this.truncated = dropped;
|
|
384
|
+
this.baseCommitSha = tree.commitSha;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* After the base moved: an edit whose starting blob is still the base blob
|
|
388
|
+
* carries over unchanged; one where the branch also changed the path becomes
|
|
389
|
+
* a conflict, unless both sides ended at the same content.
|
|
390
|
+
*/
|
|
391
|
+
rebaseOverlay() {
|
|
392
|
+
const conflicts = [];
|
|
393
|
+
for (const [path, edit] of this.overlay) {
|
|
394
|
+
const theirs = this.base.get(path)?.sha ?? null;
|
|
395
|
+
if (theirs === edit.baseSha)
|
|
396
|
+
continue;
|
|
397
|
+
if (theirs === edit.hash) {
|
|
398
|
+
this.overlay.delete(path);
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
conflicts.push({ path, base: edit.baseSha, theirs, ours: edit.hash });
|
|
402
|
+
}
|
|
403
|
+
this.conflictList = conflicts;
|
|
404
|
+
}
|
|
405
|
+
currentHash(path) {
|
|
406
|
+
const edit = this.overlay.get(path);
|
|
407
|
+
if (edit)
|
|
408
|
+
return edit.content === null ? null : edit.hash;
|
|
409
|
+
return this.base.get(path)?.sha ?? null;
|
|
410
|
+
}
|
|
411
|
+
checkPrecondition(path, current, precondition) {
|
|
412
|
+
if (!precondition)
|
|
413
|
+
return;
|
|
414
|
+
if (precondition.kind === 'createIfAbsent' && current !== null) {
|
|
415
|
+
throw new DraftError('DRAFT_STALE', `${path} already exists in the draft; read it before replacing it.`);
|
|
416
|
+
}
|
|
417
|
+
if (precondition.kind === 'ifHash' && current !== precondition.hash) {
|
|
418
|
+
throw new DraftError('DRAFT_STALE', current === null ? `${path} is no longer in the draft.` : `${path} changed in the draft since it was read; read it again.`);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
writablePath(raw) {
|
|
422
|
+
const path = normalizeRepoPath(raw);
|
|
423
|
+
if (!path)
|
|
424
|
+
throw new DraftError('DRAFT_PATH_REFUSED', `${raw} is not a file the game editor may write.`);
|
|
425
|
+
return path;
|
|
426
|
+
}
|
|
427
|
+
isBinaryBase(path, file) {
|
|
428
|
+
if (file.size > this.maxTextBytes)
|
|
429
|
+
return true;
|
|
430
|
+
const known = this.text.get(file.sha);
|
|
431
|
+
if (known !== undefined)
|
|
432
|
+
return known === null;
|
|
433
|
+
return isLikelyBinaryPath(path);
|
|
434
|
+
}
|
|
435
|
+
assertLoaded() {
|
|
436
|
+
if (this.disposed)
|
|
437
|
+
throw new DraftError('DRAFT_UNAVAILABLE', 'The draft was closed.');
|
|
438
|
+
if (!this.loaded)
|
|
439
|
+
throw new DraftError('DRAFT_UNAVAILABLE', 'The draft is still loading.');
|
|
440
|
+
}
|
|
441
|
+
emit(event) {
|
|
442
|
+
const full = { ...event, revision: this.revision, baseCommitSha: this.baseCommitSha };
|
|
443
|
+
for (const listener of this.listeners) {
|
|
444
|
+
try {
|
|
445
|
+
listener(full);
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
// A listener's failure is its own.
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
persist() {
|
|
453
|
+
if (this.persistTimer)
|
|
454
|
+
clearTimeout(this.persistTimer);
|
|
455
|
+
this.persistTimer = setTimeout(() => {
|
|
456
|
+
this.persistTimer = null;
|
|
457
|
+
void this.flush();
|
|
458
|
+
}, PERSIST_DEBOUNCE_MS);
|
|
459
|
+
}
|
|
460
|
+
async flush() {
|
|
461
|
+
try {
|
|
462
|
+
await this.storage.saveOverlay(this.key, {
|
|
463
|
+
baseCommitSha: this.baseCommitSha,
|
|
464
|
+
revision: this.revision,
|
|
465
|
+
entries: [...this.overlay.values()],
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
catch {
|
|
469
|
+
// The draft stays in memory; the next change tries again.
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
serial(work) {
|
|
473
|
+
const run = this.queue.then(work, work);
|
|
474
|
+
this.queue = run.catch(() => undefined);
|
|
475
|
+
return run;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Git blob ids. A draft file's version is the SHA-1 git computes for it
|
|
3
|
+
* (`blob <bytes>\0<content>`), so an unchanged file in the draft carries the
|
|
4
|
+
* same id as its blob on GitHub, and "dirty" is a string comparison.
|
|
5
|
+
*/
|
|
6
|
+
/** Git blob SHA-1 of raw bytes. */
|
|
7
|
+
export declare function gitBlobSha(bytes: Uint8Array): Promise<string>;
|
|
8
|
+
/** Git blob SHA-1 of UTF-8 text. */
|
|
9
|
+
export declare function gitBlobShaOfText(text: string): Promise<string>;
|
|
10
|
+
/** UTF-8 byte length of text. */
|
|
11
|
+
export declare function utf8Length(text: string): number;
|
|
12
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../../src/crowdy-studio/draft/hash.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAQH,mCAAmC;AACnC,wBAAsB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAMnE;AAED,oCAAoC;AACpC,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAE9D;AAED,iCAAiC;AACjC,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE/C"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Git blob ids. A draft file's version is the SHA-1 git computes for it
|
|
3
|
+
* (`blob <bytes>\0<content>`), so an unchanged file in the draft carries the
|
|
4
|
+
* same id as its blob on GitHub, and "dirty" is a string comparison.
|
|
5
|
+
*/
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
function hex(bytes) {
|
|
8
|
+
return [...new Uint8Array(bytes)].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
9
|
+
}
|
|
10
|
+
/** Git blob SHA-1 of raw bytes. */
|
|
11
|
+
export async function gitBlobSha(bytes) {
|
|
12
|
+
const header = encoder.encode(`blob ${bytes.byteLength}\0`);
|
|
13
|
+
const buffer = new Uint8Array(header.byteLength + bytes.byteLength);
|
|
14
|
+
buffer.set(header, 0);
|
|
15
|
+
buffer.set(bytes, header.byteLength);
|
|
16
|
+
return hex(await globalThis.crypto.subtle.digest('SHA-1', buffer));
|
|
17
|
+
}
|
|
18
|
+
/** Git blob SHA-1 of UTF-8 text. */
|
|
19
|
+
export function gitBlobShaOfText(text) {
|
|
20
|
+
return gitBlobSha(encoder.encode(text));
|
|
21
|
+
}
|
|
22
|
+
/** UTF-8 byte length of text. */
|
|
23
|
+
export function utf8Length(text) {
|
|
24
|
+
return encoder.encode(text).byteLength;
|
|
25
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export { gitBlobSha, gitBlobShaOfText, utf8Length } from './hash.js';
|
|
3
|
+
export { isLikelyBinaryPath, isRepoDir, isRepoPath, normalizeRepoPath } from './paths.js';
|
|
4
|
+
export { IndexedDbDraftOverlayStorage, MemoryDraftOverlayStorage, type DraftOverlayStorage, type StoredOverlay, type StoredOverlayEntry, } from './overlay-storage.js';
|
|
5
|
+
export { GitHubDraftStore, type GitHubDraftStoreOptions, type GitHubDraftTransport } from './github-draft-store.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/crowdy-studio/draft/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC1F,OAAO,EACL,4BAA4B,EAC5B,yBAAyB,EACzB,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,kBAAkB,GACxB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,KAAK,oBAAoB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export { gitBlobSha, gitBlobShaOfText, utf8Length } from './hash.js';
|
|
3
|
+
export { isLikelyBinaryPath, isRepoDir, isRepoPath, normalizeRepoPath } from './paths.js';
|
|
4
|
+
export { IndexedDbDraftOverlayStorage, MemoryDraftOverlayStorage, } from './overlay-storage.js';
|
|
5
|
+
export { GitHubDraftStore } from './github-draft-store.js';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a hosted draft lives between page loads: IndexedDB on the game's own
|
|
3
|
+
* origin (each hosted game is served from an origin of its own, so its
|
|
4
|
+
* storage is nobody else's). The overlay holds only what the draft changed;
|
|
5
|
+
* blob text is cached by blob id, which never changes meaning.
|
|
6
|
+
*/
|
|
7
|
+
export interface StoredOverlayEntry {
|
|
8
|
+
path: string;
|
|
9
|
+
/** Draft content; null for a deletion. */
|
|
10
|
+
content: string | null;
|
|
11
|
+
hash: string | null;
|
|
12
|
+
/** Base blob the edit started from; null when the draft created the path. */
|
|
13
|
+
baseSha: string | null;
|
|
14
|
+
updatedAt: number;
|
|
15
|
+
}
|
|
16
|
+
export interface StoredOverlay {
|
|
17
|
+
baseCommitSha: string;
|
|
18
|
+
revision: number;
|
|
19
|
+
entries: StoredOverlayEntry[];
|
|
20
|
+
}
|
|
21
|
+
export interface DraftOverlayStorage {
|
|
22
|
+
loadOverlay(key: string): Promise<StoredOverlay | null>;
|
|
23
|
+
saveOverlay(key: string, overlay: StoredOverlay): Promise<void>;
|
|
24
|
+
/** Text of a blob id, or null when it is known binary; undefined when not cached. */
|
|
25
|
+
loadBlobText(sha: string): Promise<string | null | undefined>;
|
|
26
|
+
saveBlobText(sha: string, text: string | null): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/** Process memory only (tests, Node, or a browser without IndexedDB). */
|
|
29
|
+
export declare class MemoryDraftOverlayStorage implements DraftOverlayStorage {
|
|
30
|
+
private readonly overlays;
|
|
31
|
+
private readonly blobs;
|
|
32
|
+
loadOverlay(key: string): Promise<StoredOverlay | null>;
|
|
33
|
+
saveOverlay(key: string, overlay: StoredOverlay): Promise<void>;
|
|
34
|
+
loadBlobText(sha: string): Promise<string | null | undefined>;
|
|
35
|
+
saveBlobText(sha: string, text: string | null): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
/** IndexedDB on this origin. Falls back to memory when IndexedDB is unavailable. */
|
|
38
|
+
export declare class IndexedDbDraftOverlayStorage implements DraftOverlayStorage {
|
|
39
|
+
private db;
|
|
40
|
+
private readonly fallback;
|
|
41
|
+
private open;
|
|
42
|
+
private store;
|
|
43
|
+
loadOverlay(key: string): Promise<StoredOverlay | null>;
|
|
44
|
+
saveOverlay(key: string, overlay: StoredOverlay): Promise<void>;
|
|
45
|
+
loadBlobText(sha: string): Promise<string | null | undefined>;
|
|
46
|
+
saveBlobText(sha: string, text: string | null): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=overlay-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"overlay-storage.d.ts","sourceRoot":"","sources":["../../../src/crowdy-studio/draft/overlay-storage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,0CAA0C;IAC1C,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,6EAA6E;IAC7E,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,aAAa;IAC5B,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,kBAAkB,EAAE,CAAC;CAC/B;AAED,MAAM,WAAW,mBAAmB;IAClC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAC;IACxD,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChE,qFAAqF;IACrF,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAC9D,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/D;AAED,yEAAyE;AACzE,qBAAa,yBAA0B,YAAW,mBAAmB;IACnE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoC;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAoC;IAEpD,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAKvD,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/D,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAI7D,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CAGpE;AAYD,oFAAoF;AACpF,qBAAa,4BAA6B,YAAW,mBAAmB;IACtE,OAAO,CAAC,EAAE,CAAqC;IAC/C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmC;IAE5D,OAAO,CAAC,IAAI;YAeE,KAAK;IAUb,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAMvD,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAM/D,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAO7D,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CAKpE"}
|