Skip to content

Commit 2683b26

Browse files
krisbitneydOrgJelli
authored andcommitted
generalized URI authority inference
1 parent 5dfd5d9 commit 2683b26

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

packages/js/core/src/__tests__/Uri.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ describe("Uri", () => {
5353

5454
it("Infers common URI authorities", () => {
5555
let uri = new Uri("https://domain.com/path/to/thing");
56-
expect(uri.uri).toEqual("wrap://https/https://domain.com/path/to/thing");
56+
expect(uri.uri).toEqual("wrap://https/domain.com/path/to/thing");
5757
expect(uri.authority).toEqual("https");
58-
expect(uri.path).toEqual("https://domain.com/path/to/thing");
58+
expect(uri.path).toEqual("domain.com/path/to/thing");
5959

6060
uri = new Uri("http://domain.com/path/to/thing");
61-
expect(uri.uri).toEqual("wrap://http/http://domain.com/path/to/thing");
61+
expect(uri.uri).toEqual("wrap://http/domain.com/path/to/thing");
6262
expect(uri.authority).toEqual("http");
63-
expect(uri.path).toEqual("http://domain.com/path/to/thing");
63+
expect(uri.path).toEqual("domain.com/path/to/thing");
6464

6565
uri = new Uri("ipfs://QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw");
6666
expect(uri.uri).toEqual("wrap://ipfs/QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw");

packages/js/core/src/types/Uri.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,15 @@ export class Uri {
210210
return this._config.uri;
211211
}
212212

213-
private static inferAuthority(path: string): UriConfig | undefined {
214-
let authority: string | undefined;
215-
216-
if (path.startsWith("https://")) {
217-
authority = "https";
218-
} else if (path.startsWith("http://")) {
219-
authority = "http";
220-
} else if (path.startsWith("ipfs://")) {
221-
authority = "ipfs";
222-
path = path.substring(7);
223-
} else if (path.startsWith("ens://")) {
224-
authority = "ens";
225-
path = path.substring(6);
226-
}
213+
private static inferAuthority(_path: string): UriConfig | undefined {
214+
const re = /^(?<authority>[a-z][a-z0-9-_]+):\/\/(?<path>.*)$/;
215+
const result: RegExpGroups<"authority" | "path"> = re.exec(_path);
227216

228-
if (!authority) {
217+
if (!result || !result.groups) {
229218
return undefined;
230219
}
220+
const authority = result.groups.authority as string;
221+
const path = result.groups.path as string;
231222

232223
return { authority, path, uri: `wrap://${authority}/${path}` };
233224
}

0 commit comments

Comments
 (0)