Skip to content

Commit

Permalink
Merge pull request #1126 from cardstack/virtual-network-part-2
Browse files Browse the repository at this point in the history
Virtual network part 2
  • Loading branch information
ef4 authored Apr 15, 2024
2 parents 96fd60a + 34bfedb commit 76bd4fd
Show file tree
Hide file tree
Showing 26 changed files with 505 additions and 407 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
catalogEntryRef,
CodeRef,
} from '@cardstack/runtime-common';
import { makeResolvedURL } from '@cardstack/runtime-common/loader';

import type { ModuleSyntax } from '@cardstack/runtime-common/module-syntax';

import ModalContainer from '@cardstack/host/components/modal-container';
Expand Down Expand Up @@ -236,8 +236,8 @@ export default class EditFieldModal extends Component<Signature> {
fieldType,
fieldDefinitionType: this.isFieldDef ? 'field' : 'card',
incomingRelativeTo,
outgoingRelativeTo: this.loaderService.loader.reverseResolution(
makeResolvedURL(this.operatorModeStateService.state.codePath!).href,
outgoingRelativeTo: new URL(
this.operatorModeStateService.state.codePath!,
),
outgoingRealmURL: new URL(this.args.file.realmURL),
addFieldAtIndex,
Expand Down
2 changes: 1 addition & 1 deletion packages/host/app/services/loader-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class LoaderService extends Service {
}

let loader = this.virtualNetwork.createLoader();
loader.addURLMapping(
this.virtualNetwork.addURLMapping(
new URL(baseRealm.url),
new URL(config.resolvedBaseRealmURL),
);
Expand Down
9 changes: 5 additions & 4 deletions packages/host/app/services/message-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Service, { service } from '@ember/service';

import { tracked } from '@glimmer/tracking';

import type LoaderService from './loader-service';
Expand All @@ -12,13 +13,13 @@ export default class MessageService extends Service {
}

subscribe(realmURL: string, cb: (ev: MessageEvent) => void): () => void {
let resolvedRealmURL = this.loaderService.loader.resolve(realmURL);
let maybeEventSource = this.subscriptions.get(resolvedRealmURL.href);
let maybeEventSource = this.subscriptions.get(realmURL);

if (!maybeEventSource) {
maybeEventSource = new EventSource(resolvedRealmURL);
maybeEventSource =
this.loaderService.virtualNetwork.createEventSource(realmURL);
maybeEventSource.onerror = () => eventSource.close();
this.subscriptions.set(resolvedRealmURL.href, maybeEventSource);
this.subscriptions.set(realmURL, maybeEventSource);
}

let eventSource = maybeEventSource;
Expand Down
8 changes: 2 additions & 6 deletions packages/host/app/services/operator-mode-state-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ export default class OperatorModeStateService extends Service {
}

get codePathRelativeToRealm() {
if (this.state.codePath && this.resolvedRealmURL) {
let realmPath = new RealmPaths(this.resolvedRealmURL);
if (this.state.codePath && this.realmURL) {
let realmPath = new RealmPaths(this.realmURL);

if (realmPath.inRealm(this.state.codePath)) {
try {
Expand Down Expand Up @@ -488,10 +488,6 @@ export default class OperatorModeStateService extends Service {
return this.cardService.defaultURL;
}

get resolvedRealmURL() {
return this.loaderService.loader.resolve(this.realmURL);
}

subscribeToOpenFileStateChanges(subscriber: OpenFileSubscriber) {
this.openFileSubscribers.push(subscriber);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/host/app/services/recent-files-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class RecentFilesService extends Service {
}
// TODO this wont work when visiting files that come from multiple realms in
// code mode...
let realmURL = this.operatorModeStateService.resolvedRealmURL;
let realmURL = this.operatorModeStateService.realmURL;

if (realmURL) {
let realmPaths = new RealmPaths(new URL(realmURL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ module('Acceptance | code submode | recent files tests', function (hooks) {
],
],
submode: 'code',
codePath: `http://localhost:4201/base/code-ref.gts`,
codePath: `https://cardstack.com/base/date.gts`,
fileView: 'browser',
openDirs: {},
});
Expand Down Expand Up @@ -527,9 +527,8 @@ module('Acceptance | code submode | recent files tests', function (hooks) {
await waitFor('[data-test-file="field-component.gts"]');
await click('[data-test-file="field-component.gts"]');
await waitFor('[data-test-file="field-component.gts"].selected');
assert
.dom('[data-test-recent-file]:nth-child(1)')
.containsText('field-component.gts');

assert.dom('[data-test-recent-file]:nth-child(1)').containsText('date.gts');
assert
.dom('[data-test-recent-file]:nth-child(2)')
.containsText('code-ref.gts');
Expand All @@ -541,6 +540,7 @@ module('Acceptance | code submode | recent files tests', function (hooks) {
JSON.parse(window.localStorage.getItem('recent-files') || '[]'),
[
['https://cardstack.com/base/', 'field-component.gts'],
['https://cardstack.com/base/', 'date.gts'],
['https://cardstack.com/base/', 'code-ref.gts'],
['https://cardstack.com/base/', 'catalog-entry.gts'],
],
Expand Down
6 changes: 5 additions & 1 deletion packages/host/tests/helpers/index.gts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import CardPrerender from '@cardstack/host/components/card-prerender';
import type CardService from '@cardstack/host/services/card-service';
import type { CardSaveSubscriber } from '@cardstack/host/services/card-service';

import type LoaderService from '@cardstack/host/services/loader-service';
import type MessageService from '@cardstack/host/services/message-service';

import {
Expand Down Expand Up @@ -448,7 +449,6 @@ export async function setupIntegrationTestRealm({

export const testRealmSecretSeed = "shhh! it's a secret";
async function setupTestRealm({
loader,
contents,
realmURL,
onFetch,
Expand All @@ -466,6 +466,9 @@ async function setupTestRealm({
permissions?: RealmPermissions;
}) {
let owner = (getContext() as TestContext).owner;
let { loader, virtualNetwork } = owner.lookup(
'service:loader-service',
) as LoaderService;

realmURL = realmURL ?? testRealmURL;

Expand Down Expand Up @@ -548,6 +551,7 @@ async function setupTestRealm({
realmSecretSeed: testRealmSecretSeed,
});

virtualNetwork.mount(realm.maybeHandle);
await realm.ready;
return { realm, adapter };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ module('Integration | text-input-validator', function (hooks) {
loader = (this.owner.lookup('service:loader-service') as LoaderService)
.loader;

loader.addURLMapping(
new URL(baseRealm.url),
new URL('http://localhost:4201/base/'),
);
cardApi = await loader.import(`${baseRealm.url}card-api`);
let bigInteger: typeof import('https://cardstack.com/base/big-integer');
cardApi = await loader.import(`${baseRealm.url}card-api`);
Expand Down
20 changes: 10 additions & 10 deletions packages/host/tests/integration/search-index-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -2643,17 +2643,17 @@ module('Integration | search-index', function (hooks) {
// Exclude synthetic imports that encapsulate scoped CSS
.filter((key) => !key.includes('glimmer-scoped.css')),
[
'http://localhost:4201/base/card-api',
'http://localhost:4201/base/contains-many-component',
'http://localhost:4201/base/field-component',
'http://localhost:4201/base/links-to-editor',
'http://localhost:4201/base/links-to-many-component',
'http://localhost:4201/base/number',
'http://localhost:4201/base/shared-state',
'http://localhost:4201/base/string',
'http://localhost:4201/base/text-input-validator',
'http://localhost:4201/base/watched-array',
'http://localhost:4202/test/person',
'https://cardstack.com/base/card-api',
'https://cardstack.com/base/contains-many-component',
'https://cardstack.com/base/field-component',
'https://cardstack.com/base/links-to-editor',
'https://cardstack.com/base/links-to-many-component',
'https://cardstack.com/base/number',
'https://cardstack.com/base/shared-state',
'https://cardstack.com/base/string',
'https://cardstack.com/base/text-input-validator',
'https://cardstack.com/base/watched-array',
'https://packages/@cardstack/boxel-ui/components',
'https://packages/@cardstack/boxel-ui/helpers',
'https://packages/@cardstack/boxel-ui/icons',
Expand Down
5 changes: 4 additions & 1 deletion packages/host/tests/unit/query-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ module('Unit | query', function (hooks) {
hooks.beforeEach(async function () {
let virtualNetwork = new VirtualNetwork();
loader = virtualNetwork.createLoader();
loader.addURLMapping(new URL(baseRealm.url), new URL(resolvedBaseRealmURL));
virtualNetwork.addURLMapping(
new URL(baseRealm.url),
new URL(resolvedBaseRealmURL),
);
shimExternals(virtualNetwork);

cardApi = await loader.import(`${baseRealm.url}card-api`);
Expand Down
50 changes: 25 additions & 25 deletions packages/realm-server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ let urlMappings = fromUrls.map((fromUrl, i) => [
new URL(String(toUrls[i]), `http://localhost:${port}`),
]);
for (let [from, to] of urlMappings) {
loader.addURLMapping(from, to);
virtualNetwork.addURLMapping(from, to);
}
let hrefs = urlMappings.map(([from, to]) => [from.href, to.href]);
let dist: string | URL;
Expand Down Expand Up @@ -175,33 +175,33 @@ if (distURL) {

let realmPermissions = getRealmPermissions(url);

realms.push(
new Realm(
{
url,
adapter: new NodeAdapter(resolve(String(path))),
loader,
indexRunner: getRunner,
runnerOptsMgr: manager,
getIndexHTML: async () =>
readFileSync(join(distPath, 'index.html')).toString(),
matrix: { url: new URL(matrixURL), username, password },
realmSecretSeed: REALM_SECRET_SEED,
permissions: realmPermissions.users,
},
{
deferStartUp: true,
...(useTestingDomain
? {
useTestingDomain,
}
: {}),
},
),
let realm = new Realm(
{
url,
adapter: new NodeAdapter(resolve(String(path))),
loader,
indexRunner: getRunner,
runnerOptsMgr: manager,
getIndexHTML: async () =>
readFileSync(join(distPath, 'index.html')).toString(),
matrix: { url: new URL(matrixURL), username, password },
realmSecretSeed: REALM_SECRET_SEED,
permissions: realmPermissions.users,
},
{
deferStartUp: true,
...(useTestingDomain
? {
useTestingDomain,
}
: {}),
},
);
realms.push(realm);
virtualNetwork.mount(realm.maybeExternalHandle);
}

let server = new RealmServer(realms, {
let server = new RealmServer(realms, virtualNetwork, {
...(distURL ? { assetsURL: new URL(distURL) } : {}),
});

Expand Down
57 changes: 1 addition & 56 deletions packages/realm-server/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import proxy from 'koa-proxies';
import {
assetsDir,
boxelUIAssetsDir,
logger as getLogger,
type Realm,
} from '@cardstack/runtime-common';
import { logger as getLogger } from '@cardstack/runtime-common';
import type Koa from 'koa';
import basicAuth from 'basic-auth';

Expand Down Expand Up @@ -94,56 +89,6 @@ export function ecsMetadata(ctxt: Koa.Context, next: Koa.Next) {
return next();
}

export function assetRedirect(
assetsURL: URL,
): (ctxt: Koa.Context, next: Koa.Next) => void {
return (ctxt: Koa.Context, next: Koa.Next) => {
if (ctxt.path.startsWith(`/${assetsDir}`)) {
let redirectURL = new URL(
`./${ctxt.path.slice(assetsDir.length + 1)}`,
assetsURL,
).href;

if (redirectURL !== ctxt.href) {
ctxt.redirect(redirectURL);
return;
}
}
if (ctxt.path.startsWith(`/${boxelUIAssetsDir}`)) {
let redirectURL = new URL(`.${ctxt.path}`, assetsURL).href;
ctxt.redirect(redirectURL);
return;
}
return next();
};
}

// requests for the root of the realm without a trailing slash aren't
// technically inside the realm (as the realm includes the trailing '/').
// So issue a redirect in those scenarios.
export function rootRealmRedirect(
realms: Realm[],
): (ctxt: Koa.Context, next: Koa.Next) => void {
return (ctxt: Koa.Context, next: Koa.Next) => {
let url = fullRequestURL(ctxt);

let realmUrlWithoutQueryParams = url.href.split('?')[0];
if (
!realmUrlWithoutQueryParams.endsWith('/') &&
realms.find(
(r) =>
r.loader.reverseResolution(`${realmUrlWithoutQueryParams}/`).href ===
r.url,
)
) {
url.pathname = `${url.pathname}/`;
ctxt.redirect(`${url.href}`); // Adding a trailing slash to the URL one line above will update the href
return;
}
return next();
};
}

export function fullRequestURL(ctxt: Koa.Context): URL {
let protocol =
ctxt.req.headers['x-forwarded-proto'] === 'https' ? 'https' : 'http';
Expand Down
Loading

0 comments on commit 76bd4fd

Please sign in to comment.