From 5dfd5d9449186eb7b47c9c26bf63f1b07c1cb32d Mon Sep 17 00:00:00 2001 From: krisbitney Date: Thu, 2 Mar 2023 14:28:23 +0530 Subject: [PATCH 1/7] added polywrap uri authority inference for common URI formats to Uri --- packages/js/core/README.md | 2 +- packages/js/core/src/__tests__/Uri.spec.ts | 29 ++++++++- packages/js/core/src/types/Uri.ts | 63 +++++++++++++++---- packages/js/core/src/types/WrapError.ts | 12 +--- packages/js/core/src/utils/RegExpGroups.ts | 5 ++ packages/js/core/src/utils/index.ts | 1 + .../js/plugin/src/utils/getErrorSource.ts | 8 +-- 7 files changed, 89 insertions(+), 31 deletions(-) create mode 100644 packages/js/core/src/utils/RegExpGroups.ts diff --git a/packages/js/core/README.md b/packages/js/core/README.md index f122bcdaa..5f1c4c074 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -340,7 +340,7 @@ export interface UriConfig { /** * A Polywrap URI. Some examples of valid URIs are: * wrap://ipfs/QmHASH - * wrap://ens/sub.dimain.eth + * wrap://ens/sub.domain.eth * wrap://fs/directory/file.txt * wrap://uns/domain.crypto * diff --git a/packages/js/core/src/__tests__/Uri.spec.ts b/packages/js/core/src/__tests__/Uri.spec.ts index f38bfa7f1..e6fe6a3ab 100644 --- a/packages/js/core/src/__tests__/Uri.spec.ts +++ b/packages/js/core/src/__tests__/Uri.spec.ts @@ -15,7 +15,7 @@ describe("Uri", () => { }); it("Fails if an authority is not present", () => { - expect(() => new Uri("wrap://path")).toThrowError(/URI is malformed,/); + expect(() => new Uri("wrap://path")).toThrowError(/URI authority is missing,/); }); it("Fails if a path is not present", () => { @@ -50,4 +50,31 @@ describe("Uri", () => { path: "uri", }); }); + + it("Infers common URI authorities", () => { + let uri = new Uri("https://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://https/https://domain.com/path/to/thing"); + expect(uri.authority).toEqual("https"); + expect(uri.path).toEqual("https://domain.com/path/to/thing"); + + uri = new Uri("http://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://http/http://domain.com/path/to/thing"); + expect(uri.authority).toEqual("http"); + expect(uri.path).toEqual("http://domain.com/path/to/thing"); + + uri = new Uri("ipfs://QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); + expect(uri.uri).toEqual("wrap://ipfs/QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); + expect(uri.authority).toEqual("ipfs"); + expect(uri.path).toEqual("QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); + + uri = new Uri("ens://domain.eth"); + expect(uri.uri).toEqual("wrap://ens/domain.eth"); + expect(uri.authority).toEqual("ens"); + expect(uri.path).toEqual("domain.eth"); + + uri = new Uri("ens://domain.eth:pkg@1.0.0"); + expect(uri.uri).toEqual("wrap://ens/domain.eth:pkg@1.0.0"); + expect(uri.authority).toEqual("ens"); + expect(uri.path).toEqual("domain.eth:pkg@1.0.0"); + }); }); diff --git a/packages/js/core/src/types/Uri.ts b/packages/js/core/src/types/Uri.ts index 755e32a39..08336c99e 100644 --- a/packages/js/core/src/types/Uri.ts +++ b/packages/js/core/src/types/Uri.ts @@ -1,3 +1,5 @@ +import { RegExpGroups } from "../utils/RegExpGroups"; + import { Result, ResultErr, ResultOk } from "@polywrap/result"; // $start: UriConfig @@ -18,7 +20,7 @@ export interface UriConfig { /** * A Polywrap URI. Some examples of valid URIs are: * wrap://ipfs/QmHASH - * wrap://ens/sub.dimain.eth + * wrap://ens/sub.domain.eth * wrap://fs/directory/file.txt * wrap://uns/domain.crypto * @@ -135,17 +137,10 @@ export class Uri { } // Extract the authoriy & path - const result = processed.match(/wrap:\/\/([a-z][a-z0-9-_]+)\/(.*)/); - let uriParts: string[]; - - // Remove all empty strings - if (result) { - uriParts = result.filter((str) => !!str); - } else { - uriParts = []; - } + const re = /^wrap:\/\/((?[a-z][a-z0-9-_]+)\/)?(?.*)$/; + const result: RegExpGroups<"authority" | "path"> = re.exec(processed); - if (uriParts.length !== 3) { + if (!result || !result.groups || !result.groups.path) { return ResultErr( Error( `URI is malformed, here are some examples of valid URIs:\n` + @@ -157,10 +152,30 @@ export class Uri { ); } + let { authority, path } = result.groups; + + if (!authority) { + const inferred = Uri.inferAuthority(path); + if (!inferred) { + return ResultErr( + Error( + `URI authority is missing, here are some examples of valid URIs:\n` + + `wrap://ipfs/QmHASH\n` + + `wrap://ens/domain.eth\n` + + `ens/domain.eth\n\n` + + `Invalid URI Received: ${uri}` + ) + ); + } + authority = inferred.authority; + path = inferred.path; + processed = `wrap://${authority}/${path}`; + } + return ResultOk({ uri: processed, - authority: uriParts[1], - path: uriParts[2], + authority, + path, }); } @@ -194,4 +209,26 @@ export class Uri { public toJSON(): string /* $ */ { return this._config.uri; } + + private static inferAuthority(path: string): UriConfig | undefined { + let authority: string | undefined; + + if (path.startsWith("https://")) { + authority = "https"; + } else if (path.startsWith("http://")) { + authority = "http"; + } else if (path.startsWith("ipfs://")) { + authority = "ipfs"; + path = path.substring(7); + } else if (path.startsWith("ens://")) { + authority = "ens"; + path = path.substring(6); + } + + if (!authority) { + return undefined; + } + + return { authority, path, uri: `wrap://${authority}/${path}` }; + } } diff --git a/packages/js/core/src/types/WrapError.ts b/packages/js/core/src/types/WrapError.ts index 38641858b..af9c82791 100644 --- a/packages/js/core/src/types/WrapError.ts +++ b/packages/js/core/src/types/WrapError.ts @@ -1,4 +1,5 @@ import { CleanResolutionStep } from "../algorithms"; +import { RegExpGroups } from "../utils/RegExpGroups"; export type ErrorSource = Readonly<{ file?: string; @@ -52,12 +53,6 @@ export interface WrapErrorOptions { innerError?: WrapError; } -type RegExpGroups = - | (RegExpExecArray & { - groups?: { [name in T]: string | undefined } | { [key: string]: string }; - }) - | null; - export class WrapError extends Error { readonly name: string = "WrapError"; readonly code: WrapErrorCode; @@ -174,7 +169,7 @@ export class WrapError extends Error { | "resolutionStack" | "cause" > = WrapError.re.exec(error); - if (!result) { + if (!result || !result.groups) { return undefined; } const { @@ -188,8 +183,7 @@ export class WrapError extends Error { col, resolutionStack: resolutionStackStr, cause, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - } = result.groups!; + } = result.groups; const code = parseInt(codeStr as string); diff --git a/packages/js/core/src/utils/RegExpGroups.ts b/packages/js/core/src/utils/RegExpGroups.ts new file mode 100644 index 000000000..35e7fa063 --- /dev/null +++ b/packages/js/core/src/utils/RegExpGroups.ts @@ -0,0 +1,5 @@ +export type RegExpGroups = + | (RegExpExecArray & { + groups?: { [name in T]: string | undefined } | { [key: string]: string }; + }) + | null; diff --git a/packages/js/core/src/utils/index.ts b/packages/js/core/src/utils/index.ts index f52298232..ca4c0fa29 100644 --- a/packages/js/core/src/utils/index.ts +++ b/packages/js/core/src/utils/index.ts @@ -2,3 +2,4 @@ export * from "./combinePaths"; export * from "./getEnvFromUriHistory"; export * from "./is-buffer"; export * from "./typesHandler"; +export * from "./RegExpGroups"; diff --git a/packages/js/plugin/src/utils/getErrorSource.ts b/packages/js/plugin/src/utils/getErrorSource.ts index 8b737f432..f87e497e0 100644 --- a/packages/js/plugin/src/utils/getErrorSource.ts +++ b/packages/js/plugin/src/utils/getErrorSource.ts @@ -1,10 +1,4 @@ -import { ErrorSource } from "@polywrap/core-js"; - -type RegExpGroups = - | (RegExpExecArray & { - groups?: { [name in T]: string | undefined } | { [key: string]: string }; - }) - | null; +import { ErrorSource, RegExpGroups } from "@polywrap/core-js"; const re = /\((?.*):(?\d+):(?\d+)\)$/; From 2683b262f307c62ef32c6f2d8269c03952ad67dc Mon Sep 17 00:00:00 2001 From: krisbitney Date: Fri, 3 Mar 2023 14:09:28 +0530 Subject: [PATCH 2/7] generalized URI authority inference --- packages/js/core/src/__tests__/Uri.spec.ts | 8 ++++---- packages/js/core/src/types/Uri.ts | 21 ++++++--------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/js/core/src/__tests__/Uri.spec.ts b/packages/js/core/src/__tests__/Uri.spec.ts index e6fe6a3ab..778fdde14 100644 --- a/packages/js/core/src/__tests__/Uri.spec.ts +++ b/packages/js/core/src/__tests__/Uri.spec.ts @@ -53,14 +53,14 @@ describe("Uri", () => { it("Infers common URI authorities", () => { let uri = new Uri("https://domain.com/path/to/thing"); - expect(uri.uri).toEqual("wrap://https/https://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://https/domain.com/path/to/thing"); expect(uri.authority).toEqual("https"); - expect(uri.path).toEqual("https://domain.com/path/to/thing"); + expect(uri.path).toEqual("domain.com/path/to/thing"); uri = new Uri("http://domain.com/path/to/thing"); - expect(uri.uri).toEqual("wrap://http/http://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://http/domain.com/path/to/thing"); expect(uri.authority).toEqual("http"); - expect(uri.path).toEqual("http://domain.com/path/to/thing"); + expect(uri.path).toEqual("domain.com/path/to/thing"); uri = new Uri("ipfs://QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); expect(uri.uri).toEqual("wrap://ipfs/QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); diff --git a/packages/js/core/src/types/Uri.ts b/packages/js/core/src/types/Uri.ts index 08336c99e..4af5dbe35 100644 --- a/packages/js/core/src/types/Uri.ts +++ b/packages/js/core/src/types/Uri.ts @@ -210,24 +210,15 @@ export class Uri { return this._config.uri; } - private static inferAuthority(path: string): UriConfig | undefined { - let authority: string | undefined; - - if (path.startsWith("https://")) { - authority = "https"; - } else if (path.startsWith("http://")) { - authority = "http"; - } else if (path.startsWith("ipfs://")) { - authority = "ipfs"; - path = path.substring(7); - } else if (path.startsWith("ens://")) { - authority = "ens"; - path = path.substring(6); - } + private static inferAuthority(_path: string): UriConfig | undefined { + const re = /^(?[a-z][a-z0-9-_]+):\/\/(?.*)$/; + const result: RegExpGroups<"authority" | "path"> = re.exec(_path); - if (!authority) { + if (!result || !result.groups) { return undefined; } + const authority = result.groups.authority as string; + const path = result.groups.path as string; return { authority, path, uri: `wrap://${authority}/${path}` }; } From 56a07f21f1dafa72c48b77cda656c90c19e67625 Mon Sep 17 00:00:00 2001 From: krisbitney Date: Tue, 25 Apr 2023 17:45:56 +0100 Subject: [PATCH 3/7] post-merge fix --- packages/core/src/types/Uri.ts | 2 +- packages/core/src/types/WrapError.ts | 2 +- packages/core/src/utils/RegExpGroups.ts | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 packages/core/src/utils/RegExpGroups.ts diff --git a/packages/core/src/types/Uri.ts b/packages/core/src/types/Uri.ts index 4af5dbe35..b845a7816 100644 --- a/packages/core/src/types/Uri.ts +++ b/packages/core/src/types/Uri.ts @@ -1,4 +1,4 @@ -import { RegExpGroups } from "../utils/RegExpGroups"; +import { RegExpGroups } from "../utils"; import { Result, ResultErr, ResultOk } from "@polywrap/result"; diff --git a/packages/core/src/types/WrapError.ts b/packages/core/src/types/WrapError.ts index af9c82791..f901b7b92 100644 --- a/packages/core/src/types/WrapError.ts +++ b/packages/core/src/types/WrapError.ts @@ -1,5 +1,5 @@ import { CleanResolutionStep } from "../algorithms"; -import { RegExpGroups } from "../utils/RegExpGroups"; +import { RegExpGroups } from "../utils"; export type ErrorSource = Readonly<{ file?: string; diff --git a/packages/core/src/utils/RegExpGroups.ts b/packages/core/src/utils/RegExpGroups.ts new file mode 100644 index 000000000..35e7fa063 --- /dev/null +++ b/packages/core/src/utils/RegExpGroups.ts @@ -0,0 +1,5 @@ +export type RegExpGroups = + | (RegExpExecArray & { + groups?: { [name in T]: string | undefined } | { [key: string]: string }; + }) + | null; From 581522e673cec8b2a1b4bd01bb5d2d7db82b6f09 Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Mon, 26 Jun 2023 20:25:14 -0400 Subject: [PATCH 4/7] chore: update CHANGELOG --- CHANGELOG.md | 7 +++++++ packages/core/src/types/Uri.ts | 14 +++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d98ff945..82800e079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Polywrap Origin (0.11.0) ## Features +**`@polywrap/core-js`:** +* [PR-6](https://github.com/polywrap/javascript-client/pull/6) **Improved URI Inference** + * Non-wrap URI schemes can now be used (ex: `https://domain.com/path`). The non-wrap scheme will be used as the authority, and all other contents will be shifted into the path. + * Examples: + * `https://domain.com/path` into `wrap://https/domain.com/path` + * `ipfs://QmHASH` into `wrap://ipfs/QmHASH` + **`@polywrap/client-config-builder-js`:** * [PR-45](https://github.com/polywrap/javascript-client/pull/45) **Modular Config Bundles** * The `DefaultBundle` has been broken apart into two separate bundles: `sys` and `web3`. diff --git a/packages/core/src/types/Uri.ts b/packages/core/src/types/Uri.ts index b845a7816..d0557f961 100644 --- a/packages/core/src/types/Uri.ts +++ b/packages/core/src/types/Uri.ts @@ -19,15 +19,23 @@ export interface UriConfig { // $start: Uri /** * A Polywrap URI. Some examples of valid URIs are: + * wrap://https/domain.com * wrap://ipfs/QmHASH * wrap://ens/sub.domain.eth - * wrap://fs/directory/file.txt - * wrap://uns/domain.crypto + * wrap://file/directory/file.txt + * + * Some example short-hand URIs (utilizing inference): + * ipfs/QmHASH -> wrap://ipfs/QmHASH + * https://domain.com -> wrap://https/domain.com + * + * URI inference is performed in the following ways: + * 1. If wrap:// is missing, it will be added. + * 2. If non-wrap schema exists, it becomes the authority. * * Breaking down the various parts of the URI, as it applies * to [the URI standard](https://tools.ietf.org/html/rfc3986#section-3): * **wrap://** - URI Scheme: differentiates Polywrap URIs. - * **ipfs/** - URI Authority: allows the Polywrap URI resolution algorithm to determine an authoritative URI resolver. + * **ens/** - URI Authority: allows the Polywrap URI resolution algorithm to determine an authoritative URI resolver. * **sub.domain.eth** - URI Path: tells the Authority where the Wrapper resides. */ export class Uri { From b2ba721f321fbdba8234d6ceb83ad4cc2c98baac Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Mon, 26 Jun 2023 20:27:12 -0400 Subject: [PATCH 5/7] chore: update docs --- packages/core/README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/core/README.md b/packages/core/README.md index 5f1c4c074..8498960e3 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -339,15 +339,23 @@ export interface UriConfig { ```ts /** * A Polywrap URI. Some examples of valid URIs are: + * wrap://https/domain.com * wrap://ipfs/QmHASH * wrap://ens/sub.domain.eth - * wrap://fs/directory/file.txt - * wrap://uns/domain.crypto + * wrap://file/directory/file.txt + * + * Some example short-hand URIs (utilizing inference): + * ipfs/QmHASH -> wrap://ipfs/QmHASH + * https://domain.com -> wrap://https/domain.com + * + * URI inference is performed in the following ways: + * 1. If wrap:// is missing, it will be added. + * 2. If non-wrap schema exists, it becomes the authority. * * Breaking down the various parts of the URI, as it applies * to [the URI standard](https://tools.ietf.org/html/rfc3986#section-3): * **wrap://** - URI Scheme: differentiates Polywrap URIs. - * **ipfs/** - URI Authority: allows the Polywrap URI resolution algorithm to determine an authoritative URI resolver. + * **ens/** - URI Authority: allows the Polywrap URI resolution algorithm to determine an authoritative URI resolver. * **sub.domain.eth** - URI Path: tells the Authority where the Wrapper resides. */ export class Uri { From 2906388c169ad46aa55cdb2cb0e924972e28be51 Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Mon, 26 Jun 2023 21:11:24 -0400 Subject: [PATCH 6/7] chore: simplify URI parsing --- packages/core/src/__tests__/Uri.spec.ts | 15 ++-- packages/core/src/types/Uri.ts | 114 ++++++++++++------------ 2 files changed, 63 insertions(+), 66 deletions(-) diff --git a/packages/core/src/__tests__/Uri.spec.ts b/packages/core/src/__tests__/Uri.spec.ts index 778fdde14..4e832db94 100644 --- a/packages/core/src/__tests__/Uri.spec.ts +++ b/packages/core/src/__tests__/Uri.spec.ts @@ -19,13 +19,7 @@ describe("Uri", () => { }); it("Fails if a path is not present", () => { - expect(() => new Uri("wrap://authority/")).toThrowError(/URI is malformed,/); - }); - - it("Fails if scheme is not at the beginning", () => { - expect(() => new Uri("path/wrap://something")).toThrowError( - /The wrap:\/\/ scheme must/ - ); + expect(() => new Uri("wrap://authority/")).toThrowError(/URI path is missing,/); }); it("Fails with an empty string", () => { @@ -77,4 +71,11 @@ describe("Uri", () => { expect(uri.authority).toEqual("ens"); expect(uri.path).toEqual("domain.eth:pkg@1.0.0"); }); + + it("Handles cases where the scheme delimiter is nested under an authority", () => { + const uri = new Uri("authority/something?uri=wrap://something/something2"); + expect(uri.uri).toEqual("wrap://authority/something?uri=wrap://something/something2"); + expect(uri.authority).toEqual("authority"); + expect(uri.path).toEqual("something?uri=wrap://something/something2"); + }); }); diff --git a/packages/core/src/types/Uri.ts b/packages/core/src/types/Uri.ts index d0557f961..a7b2b5f45 100644 --- a/packages/core/src/types/Uri.ts +++ b/packages/core/src/types/Uri.ts @@ -1,5 +1,3 @@ -import { RegExpGroups } from "../utils"; - import { Result, ResultErr, ResultOk } from "@polywrap/result"; // $start: UriConfig @@ -117,71 +115,82 @@ export class Uri { * @param uri - a string representation of a wrap URI * @returns A Result containing a UriConfig, if successful, or an error */ - public static parseUri(uri: string): Result /* $ */ { - if (!uri) { - return ResultErr(Error("The provided URI is empty")); + public static parseUri(input: string): Result /* $ */ { + const authorityDelimiter = "/"; + const schemeDelimiter = "://"; + const wrapScheme = "wrap://"; + + const validUriExamples = + "wrap://ipfs/QmHASH\n" + + "wrap://ens/domain.eth\n" + + "ipfs/QmHASH\n" + + "ens/domain.eth\n" + + "https://domain.com/path\n\n"; + + if (!input) { + return ResultErr(Error( + "The provided URI is empty, here are some examples of valid URIs:\n" + + validUriExamples + )); } - let processed = uri; + let processedUri = input.trim(); - // Trim preceding '/' characters - while (processed[0] === "/") { - processed = processed.substring(1); + // Remove leading "/" + if (processedUri.startsWith(authorityDelimiter)) { + processedUri = processedUri.substring(1); } - // Check for the wrap:// scheme, add if it isn't there - const wrapSchemeIdx = processed.indexOf("wrap://"); - - // If it's missing the wrap:// scheme, add it - if (wrapSchemeIdx === -1) { - processed = "wrap://" + processed; + // Check if the string starts with a non-wrap URI scheme + if (!processedUri.startsWith(wrapScheme)) { + const schemeIndex = processedUri.indexOf(schemeDelimiter); + const authorityIndex = processedUri.indexOf(authorityDelimiter); + if (schemeIndex !== -1) { + // Make sure the string before the scheme doesn't contain an authority + if (!(authorityIndex !== -1 && schemeIndex > authorityIndex)) { + processedUri = + processedUri.substring(0, schemeIndex) + "/" + + processedUri.substring(schemeIndex + schemeDelimiter.length); + } + } + } else { + processedUri = processedUri.substring(wrapScheme.length); } - // If the wrap:// is not in the beginning, return an error - if (wrapSchemeIdx > -1 && wrapSchemeIdx !== 0) { + // Split the string into parts, using "/" as a delimeter + const parts = processedUri.split(authorityDelimiter); + + if (parts.length < 2) { return ResultErr( - Error("The wrap:// scheme must be at the beginning of the URI string") + Error( + `URI authority is missing, here are some examples of valid URIs:\n` + + validUriExamples + + `Invalid URI Received: ${input}` + ) ); } - // Extract the authoriy & path - const re = /^wrap:\/\/((?[a-z][a-z0-9-_]+)\/)?(?.*)$/; - const result: RegExpGroups<"authority" | "path"> = re.exec(processed); + // Extract the authority and path + const authority = parts[0]; + const path = parts.slice(1).join("/"); - if (!result || !result.groups || !result.groups.path) { + if (!path) { return ResultErr( Error( - `URI is malformed, here are some examples of valid URIs:\n` + - `wrap://ipfs/QmHASH\n` + - `wrap://ens/domain.eth\n` + - `ens/domain.eth\n\n` + - `Invalid URI Received: ${uri}` + `URI path is missing, here are some examples of valid URIs:\n` + + validUriExamples + + `Invalid URI Received: ${input}` ) ); } - let { authority, path } = result.groups; - - if (!authority) { - const inferred = Uri.inferAuthority(path); - if (!inferred) { - return ResultErr( - Error( - `URI authority is missing, here are some examples of valid URIs:\n` + - `wrap://ipfs/QmHASH\n` + - `wrap://ens/domain.eth\n` + - `ens/domain.eth\n\n` + - `Invalid URI Received: ${uri}` - ) - ); - } - authority = inferred.authority; - path = inferred.path; - processed = `wrap://${authority}/${path}`; + // Add "wrap://" if not already present + if (!processedUri.startsWith("wrap://")) { + processedUri = "wrap://" + processedUri; } return ResultOk({ - uri: processed, + uri: processedUri, authority, path, }); @@ -217,17 +226,4 @@ export class Uri { public toJSON(): string /* $ */ { return this._config.uri; } - - private static inferAuthority(_path: string): UriConfig | undefined { - const re = /^(?[a-z][a-z0-9-_]+):\/\/(?.*)$/; - const result: RegExpGroups<"authority" | "path"> = re.exec(_path); - - if (!result || !result.groups) { - return undefined; - } - const authority = result.groups.authority as string; - const path = result.groups.path as string; - - return { authority, path, uri: `wrap://${authority}/${path}` }; - } } From ac7caa29264b810fb14c625809a142017e39cdfe Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Mon, 26 Jun 2023 21:13:49 -0400 Subject: [PATCH 7/7] chore: lint fix --- packages/core/src/types/Uri.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/core/src/types/Uri.ts b/packages/core/src/types/Uri.ts index a7b2b5f45..73d5ccd13 100644 --- a/packages/core/src/types/Uri.ts +++ b/packages/core/src/types/Uri.ts @@ -25,7 +25,7 @@ export interface UriConfig { * Some example short-hand URIs (utilizing inference): * ipfs/QmHASH -> wrap://ipfs/QmHASH * https://domain.com -> wrap://https/domain.com - * + * * URI inference is performed in the following ways: * 1. If wrap:// is missing, it will be added. * 2. If non-wrap schema exists, it becomes the authority. @@ -128,10 +128,12 @@ export class Uri { "https://domain.com/path\n\n"; if (!input) { - return ResultErr(Error( - "The provided URI is empty, here are some examples of valid URIs:\n" + - validUriExamples - )); + return ResultErr( + Error( + "The provided URI is empty, here are some examples of valid URIs:\n" + + validUriExamples + ) + ); } let processedUri = input.trim(); @@ -149,7 +151,8 @@ export class Uri { // Make sure the string before the scheme doesn't contain an authority if (!(authorityIndex !== -1 && schemeIndex > authorityIndex)) { processedUri = - processedUri.substring(0, schemeIndex) + "/" + + processedUri.substring(0, schemeIndex) + + "/" + processedUri.substring(schemeIndex + schemeDelimiter.length); } }