Skip to content

Commit 6eb8bb0

Browse files
authored
Merge pull request #2195 from cardstack/gracefully-handle-null-last-modified
realm can tolerate a null last_modified date in index when serving a module
2 parents 06d59c9 + 09d52a7 commit 6eb8bb0

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

packages/realm-server/tests/realm-endpoints-test.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -3037,7 +3037,7 @@ module(basename(__filename), function () {
30373037
module('various other realm tests', function (hooks) {
30383038
let testRealmHttpServer2: Server;
30393039
let testRealm2: Realm;
3040-
let dbAdapter: PgAdapter;
3040+
let dbAdapter2: PgAdapter;
30413041
let publisher: QueuePublisher;
30423042
let runner: QueueRunner;
30433043
let testRealmDir: string;
@@ -3077,13 +3077,13 @@ module(basename(__filename), function () {
30773077

30783078
setupDB(hooks, {
30793079
beforeEach: async (_dbAdapter, _publisher, _runner) => {
3080-
dbAdapter = _dbAdapter;
3080+
dbAdapter2 = _dbAdapter;
30813081
publisher = _publisher;
30823082
runner = _runner;
30833083
testRealmDir = join(dir.name, 'realm_server_2', 'test');
30843084
ensureDirSync(testRealmDir);
30853085
copySync(join(__dirname, 'cards'), testRealmDir);
3086-
await startRealmServer(dbAdapter, publisher, runner);
3086+
await startRealmServer(dbAdapter2, publisher, runner);
30873087
},
30883088
afterEach: async () => {
30893089
if (seedRealm) {
@@ -3139,6 +3139,18 @@ module(basename(__filename), function () {
31393139
assert.strictEqual(person.firstName, 'Mango', 'card data is correct');
31403140
});
31413141

3142+
test('can load a module when "last_modified" field in index is null', async function (assert) {
3143+
await dbAdapter.execute('update boxel_index set last_modified = null');
3144+
let response = await request
3145+
.get(`/person`)
3146+
.set('Accept', '*/*')
3147+
.set(
3148+
'Authorization',
3149+
`Bearer ${createJWT(testRealm, 'user', ['read', 'write'])}`,
3150+
);
3151+
assert.strictEqual(response.status, 200, 'HTTP 200 status');
3152+
});
3153+
31423154
test('can instantiate a card that uses a code-ref field', async function (assert) {
31433155
let adoptsFrom = {
31443156
module: `${testRealm2Href}code-ref-test`,

packages/runtime-common/index-query-engine.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ interface IndexedModule {
6464
executableCode: string;
6565
source: string;
6666
canonicalURL: string;
67-
lastModified: number;
67+
lastModified: number | null;
6868
resourceCreatedAt: number;
6969
deps: string[] | null;
7070
}
@@ -74,7 +74,7 @@ export interface IndexedInstance {
7474
instance: CardResource;
7575
source: string;
7676
canonicalURL: string;
77-
lastModified: number;
77+
lastModified: number | null;
7878
resourceCreatedAt: number;
7979
isolatedHtml: string | null;
8080
embeddedHtml: { [refURL: string]: string } | null;
@@ -225,7 +225,7 @@ export class IndexQueryEngine {
225225
canonicalURL,
226226
executableCode,
227227
source,
228-
lastModified: parseInt(lastModified),
228+
lastModified: lastModified != null ? parseInt(lastModified) : null,
229229
resourceCreatedAt: parseInt(resourceCreatedAt),
230230
deps: moduleEntry.deps,
231231
};
@@ -311,7 +311,10 @@ export class IndexQueryEngine {
311311
type: 'instance',
312312
instance,
313313
source: instanceEntry.source,
314-
lastModified: parseInt(instanceEntry.last_modified),
314+
lastModified:
315+
instanceEntry.last_modified != null
316+
? parseInt(instanceEntry.last_modified)
317+
: null,
315318
resourceCreatedAt: parseInt(instanceEntry.resource_created_at),
316319
};
317320
}
@@ -1158,7 +1161,7 @@ function assertIndexEntrySource<T>(obj: T): Omit<
11581161
'source' | 'last_modified' | 'resource_created_at'
11591162
> & {
11601163
source: string;
1161-
last_modified: string;
1164+
last_modified: string | null;
11621165
resource_created_at: string;
11631166
} {
11641167
if (!obj || typeof obj !== 'object') {
@@ -1167,7 +1170,7 @@ function assertIndexEntrySource<T>(obj: T): Omit<
11671170
if (!('source' in obj) || typeof obj.source !== 'string') {
11681171
throw new Error(`expected index entry to have "source" string property`);
11691172
}
1170-
if (!('last_modified' in obj) || typeof obj.last_modified !== 'string') {
1173+
if (!('last_modified' in obj)) {
11711174
throw new Error(`expected index entry to have "last_modified" property`);
11721175
}
11731176
if (

packages/runtime-common/realm.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,9 @@ export class Realm {
10401040
body: source,
10411041
init: {
10421042
headers: {
1043-
'last-modified': formatRFC7231(lastModified * 1000),
1043+
...(lastModified != null
1044+
? { 'last-modified': formatRFC7231(lastModified * 1000) }
1045+
: {}),
10441046
},
10451047
},
10461048
requestContext,
@@ -1082,7 +1084,7 @@ export class Realm {
10821084
private async getSourceFromIndex(url: URL): Promise<
10831085
| {
10841086
source: string;
1085-
lastModified: number;
1087+
lastModified: number | null;
10861088
canonicalURL: URL;
10871089
}
10881090
| undefined
@@ -1109,10 +1111,10 @@ export class Realm {
11091111
? module.lastModified
11101112
: instance?.type === 'instance'
11111113
? instance.lastModified
1112-
: undefined;
1113-
if (canonicalURL == null || source == null || lastModified == null) {
1114+
: null;
1115+
if (canonicalURL == null || source == null) {
11141116
throw new Error(
1115-
`missing 'canonicalURL', 'source', and/or 'lastModified' from index entry ${
1117+
`missing 'canonicalURL' and/or 'source' from index entry ${
11161118
url.href
11171119
}, where type is ${
11181120
module?.type === 'module' ? 'module' : 'instance'

0 commit comments

Comments
 (0)