Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import {

import {
hasExecutableExtension,
RealmPaths,
ri,
rri,
SupportedMimeType,
} from '@cardstack/runtime-common';

Expand Down Expand Up @@ -987,11 +990,12 @@ export default class Workspace extends Component<Signature> {
this.deleteError = undefined;

try {
let realmPath = new RealmPaths(ri(this.args.realmURL));
let isActiveWorkspace =
this.operatorModeStateService.realmURL === this.args.realmURL ||
this.operatorModeStateService
.getOpenCardIds()
.some((cardId) => cardId.startsWith(this.args.realmURL)) ||
.some((cardId) => realmPath.inRealm(rri(cardId))) ||
this.operatorModeStateService.codePathString?.startsWith(
this.args.realmURL,
);
Expand Down
8 changes: 7 additions & 1 deletion packages/host/app/resources/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {
normalizeQueryForSignature,
buildQueryParamValue,
parseSearchURL,
ri,
rri,
RealmPaths,
runtimeDependencyContextWithSource,
} from '@cardstack/runtime-common';
import type { Query } from '@cardstack/runtime-common/query';
Expand Down Expand Up @@ -299,7 +302,10 @@ export class SearchResource<
get instancesByRealm() {
return this.realmsToSearch
.map((realm) => {
let cards = this.instances.filter((card) => card.id.startsWith(realm));
let realmPath = new RealmPaths(ri(realm));
let cards = this.instances.filter((card) =>
realmPath.inRealm(rri(card.id)),
);
return { realm, cards };
})
.filter((r) => r.cards.length > 0);
Expand Down
2 changes: 1 addition & 1 deletion packages/host/app/services/operator-mode-state-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ export default class OperatorModeStateService extends Service {
if (this._state.codePath && this.realmURL) {
let realmPath = new RealmPaths(new URL(this.realmURL));

if (realmPath.inRealm(rri(this._state.codePath.href))) {
if (realmPath.inRealm(this._state.codePath)) {
try {
return realmPath.local(this._state.codePath!);
} catch (err: any) {
Expand Down
4 changes: 2 additions & 2 deletions packages/host/app/services/recent-files-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { tracked } from '@glimmer/tracking';
import window from 'ember-window-mock';
import { TrackedArray } from 'tracked-built-ins';

import { RealmPaths, rri } from '@cardstack/runtime-common';
import { RealmPaths } from '@cardstack/runtime-common';
import type { LocalPath } from '@cardstack/runtime-common/paths';

import { RecentFiles } from '../utils/local-storage-keys';
Expand Down Expand Up @@ -90,7 +90,7 @@ export default class RecentFilesService extends Service {
let realmPaths = new RealmPaths(new URL(realmURL));
let url = new URL(urlString);

if (realmPaths.inRealm(rri(url.href))) {
if (realmPaths.inRealm(url)) {
this.addRecentFile(realmPaths.local(url));
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/realm-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
DEFAULT_CARD_SIZE_LIMIT_BYTES,
DEFAULT_FILE_SIZE_LIMIT_BYTES,
RealmPaths,
rri,
fetchSessionRoom,
hasExtension,
executableExtensions,
Expand Down Expand Up @@ -621,15 +620,15 @@ export class RealmServer {
let legacy = this.realms.find((candidate) => {
let realmURL = new URL(candidate.url);
realmURL.protocol = requestURL.protocol;
return new RealmPaths(realmURL).inRealm(rri(requestURL.href));
return new RealmPaths(realmURL).inRealm(requestURL);
});
if (legacy) {
return legacy;
}
for (const url of this.reconciler.knownByUrl.keys()) {
let realmURL = new URL(url);
realmURL.protocol = requestURL.protocol;
if (new RealmPaths(realmURL).inRealm(rri(requestURL.href))) {
if (new RealmPaths(realmURL).inRealm(requestURL)) {
return await this.reconciler.lookupOrMount(url);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/runtime-common/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function resolveTargetCodeRef(
codeRef: ResolvedCodeRef,
resolver: ListingPathResolver,
): ResolvedCodeRef {
if (baseRealmPath.inRealm(rri(cardIdToURL(codeRef.module).href))) {
if (baseRealmPath.inRealm(codeRef.module)) {
return codeRef;
} else {
let targetModule = resolver.target(cardIdToURL(codeRef.module).href);
Expand All @@ -220,7 +220,7 @@ export function planModuleInstall(
};
});
let modulesCopy = codeRefs.flatMap((sourceCodeRef: ResolvedCodeRef) => {
if (baseRealmPath.inRealm(rri(cardIdToURL(sourceCodeRef.module).href))) {
if (baseRealmPath.inRealm(sourceCodeRef.module)) {
return [];
}
let targetCodeRef = resolveTargetCodeRef(sourceCodeRef, resolver);
Expand All @@ -242,10 +242,10 @@ export function planInstanceInstall(
for (let instance of instances) {
let sourceCodeRef = resolveAdoptedCodeRef(instance);
let lid = resolver.local(cardIdToURL(instance.id).href);
if (baseRealmPath.inRealm(rri(cardIdToURL(instance.id).href))) {
if (baseRealmPath.inRealm(rri(instance.id))) {
throw new Error('Cannot install instance from base realm');
}
if (!baseRealmPath.inRealm(rri(cardIdToURL(sourceCodeRef.module).href))) {
if (!baseRealmPath.inRealm(sourceCodeRef.module)) {
let targetCodeRef = resolveTargetCodeRef(sourceCodeRef, resolver);
modulesCopy.push({
sourceCodeRef,
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-common/index-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export class Batch {
}

async updateEntry(url: URL, entry: SearchIndexEntry): Promise<void> {
if (!new RealmPaths(this.realmURL).inRealm(rri(url.href))) {
if (!new RealmPaths(this.realmURL).inRealm(url)) {
// TODO this is a workaround for CS-6886. after we have solved that issue we can
// drop this band-aid
return;
Expand Down Expand Up @@ -1210,7 +1210,7 @@ export class Batch {
private copiedRealmURL(fromRealm: URL, file: URL): URL {
let source = new RealmPaths(fromRealm);
let dest = new RealmPaths(this.realmURL);
if (!source.inRealm(rri(file.href))) {
if (!source.inRealm(file)) {
return file;
}
let local = source.local(file);
Expand Down
33 changes: 28 additions & 5 deletions packages/runtime-common/paths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rri } from './card-reference-resolver';
import { cardIdToURL } from './card-reference-resolver';
import type {
RealmIdentifier,
RealmResourceIdentifier,
Expand Down Expand Up @@ -42,7 +42,7 @@ export class RealmPaths {
): LocalPath {
if (input instanceof URL) {
this.assertURLBased('local');
if (!this.inRealm(rri(input.href))) {
if (!this.inRealm(input)) {
let error = new Error(
`realm ${this.url} does not contain ${input.href}`,
);
Expand Down Expand Up @@ -88,17 +88,40 @@ export class RealmPaths {
return new URL(local + '/', this.url);
}

inRealm(input: RealmResourceIdentifier): boolean {
inRealm(input: RealmResourceIdentifier | URL): boolean {
let inputStr = input instanceof URL ? input.href : input;
let decoded: string;
try {
decoded = decodeURI(input);
decoded = decodeURI(inputStr);
} catch {
return false;
}
return (
// Same-form fast path: both sides URL or both prefix.
if (
decoded.startsWith(this.url) ||
// realm root with missing trailing slash, optionally with query string
decoded.split('?')[0] === this.url.replace(/\/$/, '')
) {
return true;
}
// Cross-form: normalize both sides to URL form and re-check.
let realmURL: string;
let inputURL: string;
try {
realmURL = cardIdToURL(this.url).href;
inputURL = cardIdToURL(inputStr).href;
} catch {
return false;
}
let decodedURL: string;
try {
decodedURL = decodeURI(inputURL);
} catch {
return false;
}
return (
decodedURL.startsWith(realmURL) ||
decodedURL.split('?')[0] === realmURL.replace(/\/$/, '')
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-common/realm-index-card.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CardDef } from 'https://cardstack.com/base/card-api';

import { realmURL } from './constants';
import { cardIdToURL, rri } from './card-reference-resolver';
import { cardIdToURL } from './card-reference-resolver';
import { RealmPaths } from './paths';

export function isRealmIndexCardId(
Expand All @@ -17,7 +17,7 @@ export function isRealmIndexCardId(
);
let cardURL = cardIdToURL(cardId);
return (
realmPaths.inRealm(rri(cardURL.href)) &&
realmPaths.inRealm(cardURL) &&
realmPaths.local(cardURL) === 'index'
);
} catch {
Expand Down
3 changes: 1 addition & 2 deletions packages/runtime-common/realm-index-query-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
resolveCardReference,
isRegisteredPrefix,
cardIdToURL,
rri,
unresolveCardReference,
IndexQueryEngine,
codeRefWithAbsoluteIdentifier,
Expand Down Expand Up @@ -1216,7 +1215,7 @@ export class RealmIndexQueryEngine {
// so relationship data.id stays portable across environments.
let relationshipIdStr = unresolveCardReference(relationshipId.href);

let inRealm = realmPath.inRealm(rri(linkURL.href));
let inRealm = realmPath.inRealm(linkURL);
if (inRealm) {
if (expectsCard || (!relationshipType && !expectsFileMeta)) {
inRealmCardURLs.add(linkURL.href);
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-common/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ export class Realm {
requestContext,
});
}
if (!this.paths.inRealm(rri(parsedURL.href))) {
if (!this.paths.inRealm(parsedURL)) {
return badRequest({
message: `URL is not in realm: ${parsedURL.href}`,
requestContext,
Expand Down Expand Up @@ -5759,7 +5759,7 @@ function isGloballyPublicDependency(resourceUrl: string): boolean {
) {
return true;
}
return baseRealm.inRealm(rri(parsed.href));
return baseRealm.inRealm(parsed);
}

function lastModifiedHeader(
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-common/url.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { LooseCardResource, FileMetaResource } from './index';
import { relationshipEntries } from './relationship-utils';
import { RealmPaths } from './paths';
import { rri, unresolveCardReference } from './card-reference-resolver';
import { unresolveCardReference } from './card-reference-resolver';

export function maybeURL(
possibleURL: string,
Expand Down Expand Up @@ -29,8 +29,8 @@ export function relativeURL(
let realmPath = new RealmPaths(realmURL);
// don't return a relative URL for URL that is outside of our realm
if (
realmPath.inRealm(rri(relativeTo.href)) &&
!realmPath.inRealm(rri(url.href))
realmPath.inRealm(relativeTo) &&
!realmPath.inRealm(url)
) {
return undefined;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/runtime-common/virtual-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { RealmPaths, ensureTrailingSlash } from './paths';
import { baseRealm } from './index';
import {
registerCardReferencePrefix,
rri,
type RealmIdentifier,
} from './card-reference-resolver';
import type { ModuleDescriptor } from './package-shim-handler';
Expand Down Expand Up @@ -109,7 +108,7 @@ export class VirtualNetwork {
let sourcePath = new RealmPaths(
new URL(direction === 'virtual-to-real' ? virtual : real),
);
if (sourcePath.inRealm(rri(absoluteURL.href))) {
if (sourcePath.inRealm(absoluteURL)) {
let toPath = new RealmPaths(
new URL(direction === 'virtual-to-real' ? real : virtual),
);
Expand Down Expand Up @@ -286,7 +285,7 @@ function shouldRetryFetch(url: URL) {
return false;
}

if (baseRealm.inRealm(rri(url.href))) {
if (baseRealm.inRealm(url)) {
return true;
}

Expand Down
Loading