-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcurrent-run.ts
818 lines (761 loc) · 22.9 KB
/
current-run.ts
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
import ignore, { type Ignore } from 'ignore';
import flatMap from 'lodash/flatMap';
import isEqual from 'lodash/isEqual';
import merge from 'lodash/merge';
import {
Loader,
baseRealm,
logger,
baseCardRef,
LooseCardResource,
isCardResource,
internalKeyFor,
trimExecutableExtension,
hasExecutableExtension,
SupportedMimeType,
Indexer,
Batch,
type CodeRef,
type RealmInfo,
} from '@cardstack/runtime-common';
import {
type SingleCardDocument,
type Relationship,
} from '@cardstack/runtime-common/card-document';
import {
loadCard,
identifyCard,
isBaseDef,
moduleFrom,
} from '@cardstack/runtime-common/code-ref';
import { Deferred } from '@cardstack/runtime-common/deferred';
import {
CardError,
isCardError,
serializableError,
type SerializedError,
} from '@cardstack/runtime-common/error';
import { RealmPaths, LocalPath } from '@cardstack/runtime-common/paths';
import {
isIgnored,
type Reader,
type EntrySetter,
type RunState,
type Stats,
type Module,
type SearchEntryWithErrors,
type ModuleWithErrors,
} from '@cardstack/runtime-common/search-index';
import { URLMap } from '@cardstack/runtime-common/url-map';
import {
CardDef,
type IdentityContext as IdentityContextType,
LoaderType,
} from 'https://cardstack.com/base/card-api';
import type * as CardAPI from 'https://cardstack.com/base/card-api';
import { type RenderCard } from '../services/render-service';
const log = logger('current-run');
type TypesWithErrors =
| { type: 'types'; types: string[] }
| { type: 'error'; error: SerializedError };
export class CurrentRun {
#invalidations: string[] = [];
#instances: URLMap<SearchEntryWithErrors>;
#modules = new Map<string, ModuleWithErrors>();
#moduleWorkingCache = new Map<string, Promise<Module>>();
#typesCache = new WeakMap<typeof CardDef, Promise<TypesWithErrors>>();
#indexingInstances = new Map<string, Promise<void>>();
#reader: Reader;
// TODO make this required after feature flag removed
#indexer: Indexer | undefined;
// TODO make this required after feature flag removed
#batch: Batch | undefined;
#realmPaths: RealmPaths;
#ignoreMap: URLMap<Ignore>;
#ignoreData: Record<string, string>;
#loader: Loader;
#entrySetter: EntrySetter;
#renderCard: RenderCard;
#realmURL: URL;
#realmInfo?: RealmInfo;
#isIndexing = false;
readonly stats: Stats = {
instancesIndexed: 0,
instanceErrors: 0,
moduleErrors: 0,
};
constructor({
realmURL,
reader,
indexer,
instances = new URLMap(),
modules = new Map(),
ignoreMap = new URLMap(),
ignoreData = new Object(null) as Record<string, string>,
loader,
entrySetter,
renderCard,
}: {
realmURL: URL;
reader: Reader;
indexer?: Indexer;
instances?: URLMap<SearchEntryWithErrors>;
modules?: Map<string, ModuleWithErrors>;
ignoreMap?: URLMap<Ignore>;
ignoreData?: Record<string, string>;
loader: Loader;
entrySetter: EntrySetter;
renderCard: RenderCard;
}) {
this.#indexer = indexer;
this.#realmPaths = new RealmPaths(realmURL);
this.#reader = reader;
this.#realmURL = realmURL;
this.#instances = instances;
this.#modules = modules;
this.#ignoreMap = ignoreMap;
this.#ignoreData = ignoreData;
this.#loader = loader;
this.#entrySetter = entrySetter;
this.#renderCard = renderCard;
this.loader.prependURLHandlers([
async (req) => {
if (this.#isIndexing) {
req.headers.set('X-Boxel-Use-WIP-Index', 'true');
}
return null;
},
]);
}
static async fromScratch(current: CurrentRun) {
await current.whileIndexing(async () => {
let start = Date.now();
log.debug(`starting from scratch indexing`);
(globalThis as any).__currentRunLoader = current.loader;
if (isDbIndexerEnabled()) {
current.#batch = await current.indexer.createBatch(current.realmURL);
current.#invalidations = [];
await current.batch.makeNewGeneration();
}
await current.visitDirectory(current.realmURL);
if (isDbIndexerEnabled()) {
await current.batch.done();
}
(globalThis as any).__currentRunLoader = undefined;
log.debug(`completed from scratch indexing in ${Date.now() - start}ms`);
});
return current;
}
static async incremental({
url,
operation,
prev,
reader,
loader,
entrySetter,
renderCard,
indexer,
onInvalidation,
}: {
url: URL;
operation: 'update' | 'delete';
prev: RunState;
reader: Reader;
loader: Loader;
entrySetter: EntrySetter;
renderCard: RenderCard;
indexer?: Indexer;
// TODO remove this after we remove the feature flag. this handler happens
// outside of the index job
onInvalidation?: (invalidatedURLs: URL[]) => void;
}) {
let start = Date.now();
log.debug(`starting from incremental indexing for ${url.href}`);
(globalThis as any).__currentRunLoader = loader;
let instances = new URLMap(prev.instances);
let ignoreMap = new URLMap(prev.ignoreMap);
let ignoreData = { ...prev.ignoreData };
let invalidations: URL[] = [];
let modules = new Map(prev.modules);
if (!isDbIndexerEnabled()) {
instances.remove(new URL(url.href.replace(/\.json$/, '')));
invalidations = flatMap(invalidate(url, modules, instances), (u) =>
// we only ever want to visit our own URL in the update case so we'll do
// that explicitly
u !== url.href && u !== trimExecutableExtension(url).href
? [new URL(u)]
: [],
);
}
let current = new this({
realmURL: prev.realmURL,
reader,
indexer,
instances,
modules,
ignoreMap,
ignoreData,
loader,
entrySetter,
renderCard,
});
if (isDbIndexerEnabled()) {
current.#batch = await current.indexer.createBatch(current.realmURL);
invalidations = (await current.batch.invalidate(url)).map(
(href) => new URL(href),
);
current.#invalidations = [...invalidations].map((url) => url.href);
}
await current.whileIndexing(async () => {
if (operation === 'update') {
await current.tryToVisit(url);
}
for (let invalidation of invalidations) {
await current.tryToVisit(invalidation);
}
if (isDbIndexerEnabled()) {
await current.batch.done();
}
(globalThis as any).__currentRunLoader = undefined;
log.debug(
`completed incremental indexing for ${url.href} in ${
Date.now() - start
}ms`,
);
if (onInvalidation) {
let urls = [...new Set([url, ...invalidations].map((u) => u.href))].map(
(href) => new URL(href.replace(/\.json$/, '')),
);
onInvalidation(urls);
}
});
return current;
}
private async tryToVisit(url: URL) {
try {
await this.visitFile(url);
} catch (err: any) {
if (isCardError(err) && err.status === 404) {
log.info(`tried to visit file ${url.href}, but it no longer exists`);
} else {
throw err;
}
}
}
private async whileIndexing(doIndexing: () => Promise<void>) {
this.#isIndexing = true;
await doIndexing();
this.#isIndexing = false;
}
// TODO we can get rid of this after the feature flag is removed. this is just
// some type sugar so we don't have to check to see if the indexer exists
// since ultimately it will be required.
private get indexer() {
if (!this.#indexer) {
throw new Error(`Indexer is missing`);
}
return this.#indexer;
}
private get batch() {
if (!this.#batch) {
throw new Error('Batch is missing');
}
return this.#batch;
}
get instances() {
return this.#instances;
}
get invalidations() {
return [...this.#invalidations];
}
get modules() {
return this.#modules;
}
get ignoreMap() {
return this.#ignoreMap;
}
get ignoreData() {
return this.#ignoreData;
}
get realmURL() {
return this.#realmURL;
}
public get loader() {
return this.#loader;
}
private async visitDirectory(url: URL): Promise<void> {
let ignorePatterns = await this.#reader.readFileAsText(
this.#realmPaths.local(new URL('.gitignore', url)),
);
if (ignorePatterns && ignorePatterns.content) {
this.#ignoreMap.set(url, ignore().add(ignorePatterns.content));
this.#ignoreData[url.href] = ignorePatterns.content;
}
for await (let { path: innerPath, kind } of this.#reader.readdir(
this.#realmPaths.local(url),
)) {
let innerURL = this.#realmPaths.fileURL(innerPath);
if (isIgnored(this.#realmURL, this.#ignoreMap, innerURL)) {
continue;
}
if (kind === 'file') {
await this.visitFile(innerURL, undefined);
} else {
let directoryURL = this.#realmPaths.directoryURL(innerPath);
await this.visitDirectory(directoryURL);
}
}
}
private async visitFile(
url: URL,
identityContext?: IdentityContextType,
): Promise<void> {
if (isIgnored(this.#realmURL, this.#ignoreMap, url)) {
return;
}
let start = Date.now();
log.debug(`begin visiting file ${url.href}`);
if (
hasExecutableExtension(url.href) ||
// handle modules with no extension too
!url.href.split('/').pop()!.includes('.')
) {
await this.indexCardSource(url);
} else {
let localPath = this.#realmPaths.local(url);
let fileRef = await this.#reader.readFileAsText(localPath, {});
if (!fileRef) {
let error = new CardError(`missing file ${url.href}`, { status: 404 });
error.deps = [url.href];
throw error;
}
if (!identityContext) {
let api = await this.#loader.import<typeof CardAPI>(
`${baseRealm.url}card-api`,
);
let { IdentityContext } = api;
identityContext = new IdentityContext();
}
let { content, lastModified } = fileRef;
if (url.href.endsWith('.json')) {
let resource;
try {
let { data } = JSON.parse(content);
resource = data;
} catch (e) {
log.warn(`unable to parse ${url.href} as card JSON`);
}
if (resource && isCardResource(resource)) {
await this.indexCard(
localPath,
lastModified,
resource,
identityContext,
);
}
}
}
log.debug(`completed visiting file ${url.href} in ${Date.now() - start}ms`);
}
private async indexCardSource(url: URL): Promise<void> {
let module: Record<string, unknown>;
try {
module = await this.loader.import(url.href);
} catch (err: any) {
this.stats.moduleErrors++;
log.warn(
`encountered error loading module "${url.href}": ${err.message}`,
);
let deps = await (
await this.loader.getConsumedModules(url.href)
).filter((u) => u !== url.href);
if (isDbIndexerEnabled()) {
await this.batch.updateEntry(new URL(url), {
type: 'error',
error: {
status: 500,
detail: `encountered error loading module "${url.href}": ${err.message}`,
additionalErrors: null,
deps,
},
});
}
this.#modules.set(url.href, {
type: 'error',
moduleURL: url.href,
error: {
status: 500,
detail: `encountered error loading module "${url.href}": ${err.message}`,
additionalErrors: null,
deps,
},
});
return;
}
let refs = Object.values(module)
.filter((maybeCard) => isBaseDef(maybeCard))
.map((card) => identifyCard(card))
.filter(Boolean) as CodeRef[];
for (let ref of refs) {
if (!('type' in ref)) {
await this.buildModule(ref.module, url);
}
}
}
private async indexCard(
path: LocalPath,
lastModified: number,
resource: LooseCardResource,
identityContext: IdentityContextType,
): Promise<void> {
let fileURL = this.#realmPaths.fileURL(path).href;
let indexingInstance = this.#indexingInstances.get(fileURL);
if (indexingInstance) {
return await indexingInstance;
}
let deferred = new Deferred<void>();
this.#indexingInstances.set(fileURL, deferred.promise);
let instanceURL = new URL(
this.#realmPaths.fileURL(path).href.replace(/\.json$/, ''),
);
let moduleURL = new URL(
moduleFrom(resource.meta.adoptsFrom),
new URL(path, this.#realmURL),
).href;
let typesMaybeError: TypesWithErrors | undefined;
let uncaughtError: Error | undefined;
let doc: SingleCardDocument | undefined;
let searchData: Record<string, any> | undefined;
let cardType: typeof CardDef | undefined;
let html: string | undefined;
try {
let api = await this.#loader.import<typeof CardAPI>(
`${baseRealm.url}card-api`,
);
if (!this.#realmInfo) {
let realmInfoResponse = await this.#loader.fetch(
`${this.realmURL}_info`,
{ headers: { Accept: SupportedMimeType.RealmInfo } },
);
this.#realmInfo = (await realmInfoResponse.json())?.data?.attributes;
}
let res = { ...resource, ...{ id: instanceURL.href } };
//Realm info may be used by a card to render field values.
//Example: catalog-etry-card
merge(res, {
meta: {
realmInfo: this.#realmInfo,
realmURL: this.realmURL,
},
});
let card = await api.createFromSerialized<typeof CardDef>(
res,
{ data: res },
new URL(fileURL),
this.#loader as unknown as LoaderType,
{
identityContext,
},
);
html = await this.#renderCard({
card,
format: 'isolated',
visit: this.visitFile.bind(this),
identityContext,
realmPath: this.#realmPaths,
});
cardType = Reflect.getPrototypeOf(card)?.constructor as typeof CardDef;
let data = api.serializeCard(card, { includeComputeds: true });
// prepare the document for index serialization
Object.values(data.data.relationships ?? {}).forEach(
(rel) => delete (rel as Relationship).data,
);
//Add again realm info and realm URL here
//since we won't get it from serializeCard.
doc = merge(data, {
data: {
id: instanceURL.href,
meta: {
lastModified: lastModified,
realmInfo: this.#realmInfo,
realmURL: this.realmURL.href,
},
},
}) as SingleCardDocument;
searchData = await api.searchDoc(card);
if (!searchData) {
throw new Error(
`bug: could not derive search doc for instance ${instanceURL.href}`,
);
}
// Add a "pseudo field" to the search doc for the card type. We use the
// "_" prefix to make a decent attempt to not pollute the userland
// namespace for cards
if (cardType.displayName === 'Card') {
searchData._cardType = cardType.name;
} else {
searchData._cardType = cardType.displayName;
}
} catch (err: any) {
uncaughtError = err;
}
// if we already encountered an uncaught error then no need to deal with this
if (!uncaughtError && cardType) {
typesMaybeError = await this.getTypes(cardType);
}
if (searchData && doc && typesMaybeError?.type === 'types') {
await this.setInstance(instanceURL, {
type: 'entry',
entry: {
resource: doc.data,
searchData,
html,
types: typesMaybeError.types,
deps: new Set(await this.loader.getConsumedModules(moduleURL)),
},
});
deferred.fulfill();
}
if (uncaughtError || typesMaybeError?.type === 'error') {
let error: SearchEntryWithErrors;
if (uncaughtError) {
error = {
type: 'error',
error:
uncaughtError instanceof CardError
? serializableError(uncaughtError)
: { detail: `${uncaughtError.message}` },
};
error.error.deps = [
moduleURL,
...(uncaughtError instanceof CardError
? uncaughtError.deps ?? []
: []),
];
} else if (typesMaybeError?.type === 'error') {
error = { type: 'error', error: typesMaybeError.error };
} else {
let err = new Error(`bug: should never get here`);
deferred.reject(err);
throw err;
}
log.warn(
`encountered error indexing card instance ${path}: ${error.error.detail}`,
);
await this.setInstance(instanceURL, error);
deferred.fulfill();
}
}
private async setInstance(instanceURL: URL, entry: SearchEntryWithErrors) {
if (isDbIndexerEnabled()) {
await this.batch.updateEntry(assertURLEndsWithJSON(instanceURL), entry);
} else {
this.#instances.set(instanceURL, entry);
this.#entrySetter(instanceURL, entry);
}
if (entry.type === 'entry') {
this.stats.instancesIndexed++;
} else {
this.stats.instanceErrors++;
}
}
public async buildModule(
moduleIdentifier: string,
relativeTo = this.#realmURL,
): Promise<void> {
let url = new URL(moduleIdentifier, relativeTo).href;
let existing = this.#modules.get(url);
if (existing?.type === 'error') {
throw new Error(
`bug: card definition has errors which should never happen since the card already executed successfully: ${url}`,
);
}
if (existing) {
return;
}
let working = this.#moduleWorkingCache.get(url);
if (working) {
await working;
return;
}
let deferred = new Deferred<Module>();
this.#moduleWorkingCache.set(url, deferred.promise);
let m = await this.#loader.import<Record<string, any>>(moduleIdentifier);
if (m) {
for (let exportName of Object.keys(m)) {
m[exportName];
}
}
let consumes = (await this.loader.getConsumedModules(url)).filter(
(u) => u !== url,
);
let module: Module = {
url,
consumes,
};
if (isDbIndexerEnabled()) {
await this.batch.updateEntry(new URL(url), {
type: 'module',
module: {
deps: new Set(
consumes.map((d) => trimExecutableExtension(new URL(d)).href),
),
},
});
}
this.#modules.set(url, { type: 'module', module });
deferred.fulfill(module);
}
private async getTypes(card: typeof CardDef): Promise<TypesWithErrors> {
let cached = this.#typesCache.get(card);
if (cached) {
return await cached;
}
let ref = identifyCard(card);
if (!ref) {
throw new Error(`could not identify card ${card.name}`);
}
let deferred = new Deferred<TypesWithErrors>();
this.#typesCache.set(card, deferred.promise);
let types: string[] = [];
let fullRef: CodeRef = ref;
while (fullRef) {
let loadedCard, loadedCardRef;
try {
loadedCard = await loadCard(fullRef, { loader: this.loader });
loadedCardRef = identifyCard(loadedCard);
if (!loadedCardRef) {
throw new Error(`could not identify card ${loadedCard.name}`);
}
} catch (error) {
return { type: 'error', error: serializableError(error) };
}
types.push(internalKeyFor(loadedCardRef, undefined));
if (!isEqual(loadedCardRef, baseCardRef)) {
fullRef = {
type: 'ancestorOf',
card: loadedCardRef,
};
} else {
break;
}
}
let result: TypesWithErrors = { type: 'types', types };
deferred.fulfill(result);
return result;
}
}
function invalidate(
url: URL,
modules: Map<string, ModuleWithErrors>,
instances: URLMap<SearchEntryWithErrors>,
invalidations: string[] = [],
visited: Set<string> = new Set(),
): string[] {
if (visited.has(url.href)) {
return [];
}
let invalidationSet = new Set(invalidations);
// invalidate any instances whose deps come from the URL or whose error depends on the URL
let invalidatedInstances = [...instances]
.filter(([instanceURL, item]) => {
if (item.type === 'error') {
for (let errorDep of item.error.deps ?? []) {
if (
errorDep === url.href ||
errorDep === trimExecutableExtension(url).href
) {
instances.remove(instanceURL); // note this is a side-effect
return true;
}
}
} else {
if (
item.entry.deps.has(url.href) ||
item.entry.deps.has(trimExecutableExtension(url).href)
) {
instances.remove(instanceURL); // note this is a side-effect
return true;
}
}
return false;
})
.map(([u]) => `${u.href}.json`);
for (let invalidation of invalidatedInstances) {
invalidationSet.add(invalidation);
}
for (let [key, maybeError] of [...modules]) {
if (maybeError.type === 'error') {
// invalidate any errored modules that come from the URL
let errorModule = maybeError.moduleURL;
if (
errorModule === url.href ||
errorModule === trimExecutableExtension(url).href
) {
modules.delete(key);
invalidationSet.add(errorModule);
}
// invalidate any modules in an error state whose errorReference comes
// from the URL
for (let maybeDef of maybeError.error.deps ?? []) {
if (
maybeDef === url.href ||
maybeDef === trimExecutableExtension(url).href
) {
for (let invalidation of invalidate(
new URL(errorModule),
modules,
instances,
[...invalidationSet],
new Set([...visited, url.href]),
)) {
invalidationSet.add(invalidation);
}
// no need to test the other error refs, we have already decided to
// invalidate this URL
break;
}
}
continue;
}
let { module } = maybeError;
// invalidate any modules that come from the URL
if (
module.url === url.href ||
module.url === trimExecutableExtension(url).href
) {
modules.delete(key);
invalidationSet.add(module.url);
}
// invalidate any modules that consume the URL
for (let importURL of module.consumes) {
if (
importURL === url.href ||
importURL === trimExecutableExtension(url).href
) {
for (let invalidation of invalidate(
new URL(module.url),
modules,
instances,
[...invalidationSet],
new Set([...visited, url.href]),
)) {
invalidationSet.add(invalidation);
}
// no need to test the other imports, we have already decided to
// invalidate this URL
break;
}
}
}
return [...invalidationSet];
}
function isDbIndexerEnabled() {
return Boolean((globalThis as any).__enablePgIndexer?.());
}
function assertURLEndsWithJSON(url: URL): URL {
if (!url.href.endsWith('.json')) {
return new URL(`${url}.json`);
}
return url;
}