@xyo-network/huri 2.90.21 → 2.90.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,7 @@ var Huri = class _Huri {
41
41
  token;
42
42
  isHuri = true;
43
43
  constructor(huri, { archivistUri, token } = {}) {
44
- const huriString = _Huri.isHuri(huri)?.href ?? typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new import_account.AddressValue(huri).hex : huri.href;
44
+ const huriString = _Huri.isHuri(huri)?.href ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new import_account.AddressValue(huri).hex : huri.href);
45
45
  this.originalHref = huriString;
46
46
  const protocol = _Huri.parseProtocol(huriString);
47
47
  this.protocol = protocol ?? "https";
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAMA,OAAN,MAAMA,MAAAA;EA9Bb,OA8BaA;;;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AACjF,UAAMK,aACJX,MAAKO,OAAOE,IAAAA,GAAOG,QAAQ,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,4BAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC3I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,WAAOC,wBAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAAIV,cAAc;AAChB,YAAMW,oBAAoBX,aAAaY,MAAM,KAAA;AAC7C,WAAKj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WAAW,IAAID,cAAcG,MAAK,IAAKlB;AACzE,QAAIiB,aAAa;AACf,YAAME,gBAAgBF,aAAaf,MAAM,GAAA;AACzCJ,kCAASqB,cAAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFnB,kCAASqB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM3B,MAAK2B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK5B;EACd;EAEQO,UAAUF,MAAcwB,aAAsB;AACpD,UAAMC,YAAYzB,KAAKK,MAAM,GAAA;AAG7BJ,gCAAS,EAAEuB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,WAAOe,wBAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,gCAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAEtBL,gCAAS,KAAKhB,WAAWkC,WAAW,GAAG,0BAAA;AAGvClB,gCAAS,KAAKjB,SAASmC,WAAW,GAAG,wBAAA;AAGrClB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? (typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href)\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 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I,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,WAAOe,wBAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,gCAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAEtBL,gCAAS,KAAKhB,WAAWkC,WAAW,GAAG,0BAAA;AAGvClB,gCAAS,KAAKjB,SAASmC,WAAW,GAAG,wBAAA;AAGrClB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -17,7 +17,7 @@ var Huri = class _Huri {
17
17
  token;
18
18
  isHuri = true;
19
19
  constructor(huri, { archivistUri, token } = {}) {
20
- const huriString = _Huri.isHuri(huri)?.href ?? typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href;
20
+ const huriString = _Huri.isHuri(huri)?.href ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href);
21
21
  this.originalHref = huriString;
22
22
  const protocol = _Huri.parseProtocol(huriString);
23
23
  this.protocol = protocol ?? "https";
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,OAAN,MAAMA,MAAAA;EA9Bb,OA8BaA;;;EACXC;EACAC;E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UAAUC,IAAG;AAG5BzB,aAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAEtBL,aAAS,KAAKhB,WAAWkC,WAAW,GAAG,0BAAA;AAGvClB,aAAS,KAAKjB,SAASmC,WAAW,GAAG,wBAAA;AAGrClB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;","names":["assertEx","axios","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
1
+ {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? (typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href)\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,OAAN,MAAMA,MAAAA;EA9Bb,OA8BaA;;;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AACjF,UAAMK,aACJX,MAAKO,OAAOE,IAAAA,GAAOG,SAAS,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,aAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC5I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,OAAOC,SAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAAIV,cAAc;AAChB,YAAMW,oBAAoBX,aAAaY,MAAM,KAAA;AAC7C,WAAKjB,WAAWgB,kBAAkB,C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,IAAKlB;AACzE,QAAIiB,aAAa;AACf,YAAME,gBAAgBF,aAAaf,MAAM,GAAA;AACzCJ,eAASqB,cAAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFnB,eAASqB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM3B,MAAK2B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK5B;EACd;EAEQO,UAAUF,MAAcwB,aAAsB;AACpD,UAAMC,YAAYzB,KAAKK,MAAM,GAAA;AAG7BJ,aAAS,EAAEuB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,OAAOe,SAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,aAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAEtBL,aAAS,KAAKhB,WAAWkC,WAAW,GAAG,0BAAA;AAGvClB,aAAS,KAAKjB,SAASmC,WAAW,GAAG,wBAAA;AAGrClB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;","names":["assertEx","axios","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -39,7 +39,7 @@ var _Huri = class _Huri {
39
39
  isHuri = true;
40
40
  constructor(huri, { archivistUri, token } = {}) {
41
41
  var _a;
42
- const huriString = ((_a = _Huri.isHuri(huri)) == null ? void 0 : _a.href) ?? typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new import_account.AddressValue(huri).hex : huri.href;
42
+ const huriString = ((_a = _Huri.isHuri(huri)) == null ? void 0 : _a.href) ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new import_account.AddressValue(huri).hex : huri.href);
43
43
  this.originalHref = huriString;
44
44
  const protocol = _Huri.parseProtocol(huriString);
45
45
  this.protocol = protocol ?? "https";
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAMA,QAAN,MAAMA,MAAAA;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AAxCrF;AAyCI,UAAMK,eACJX,WAAKO,OAAOE,IAAAA,MAAZT,mBAAmBY,SAAQ,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,4BAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC3I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,WAAOC,wBAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAAIV,cAAc;AAChB,YAAMW,oBAAoBX,aAAaY,MAAM,KAAA;AAC7C,WAAKjB,WAAWgB,kBAAkB,CAAA;AAClC,WAAKnB,YAAYmB,kBAAkB,CAAA;IACrC;AAEA,SAAKf,QAAQA;AAEb,SAAKiB,cAA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AAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFnB,kCAASqB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM3B,MAAK2B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK5B;EACd;EAEQO,UAAUF,MAAcwB,aAAsB;AACpD,UAAMC,YAAYzB,KAAKK,MAAM,GAAA;AAG7BJ,gCAAS,EAAEuB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,WAAOe,wBAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,gCAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAnJ1B;AAqJIL,kCAAS,UAAKhB,cAAL,mBAAgBkC,YAAW,GAAG,0BAAA;AAGvClB,kCAAS,UAAKjB,YAAL,mBAAcmC,YAAW,GAAG,wBAAA;AAGrClB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;AA/HaF;AAAN,IAAMA,OAAN;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? (typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href)\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;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K,IAAK;AAGhD,UAAMnC,WAAOe,wBAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,gCAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAnJ1B;AAqJIL,kCAAS,UAAKhB,cAAL,mBAAgBkC,YAAW,GAAG,0BAAA;AAGvClB,kCAAS,UAAKjB,YAAL,mBAAcmC,YAAW,GAAG,wBAAA;AAGrClB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;AA/HaF;AAAN,IAAMA,OAAN;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -15,7 +15,7 @@ var _Huri = class _Huri {
15
15
  isHuri = true;
16
16
  constructor(huri, { archivistUri, token } = {}) {
17
17
  var _a;
18
- const huriString = ((_a = _Huri.isHuri(huri)) == null ? void 0 : _a.href) ?? typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href;
18
+ const huriString = ((_a = _Huri.isHuri(huri)) == null ? void 0 : _a.href) ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href);
19
19
  this.originalHref = huriString;
20
20
  const protocol = _Huri.parseProtocol(huriString);
21
21
  this.protocol = protocol ?? "https";
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,QAAN,MAAMA,MAA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,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,aAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAnJ1B;AAqJIL,eAAS,UAAKhB,cAAL,mBAAgBkC,YAAW,GAAG,0BAAA;AAGvClB,eAAS,UAAKjB,YAAL,mBAAcmC,YAAW,GAAG,wBAAA;AAGrClB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;AA/HaF;AAAN,IAAMA,OAAN;","names":["assertEx","axios","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
1
+ {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? (typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href)\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,QAAN,MAAMA,MAAAA;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AAxCrF;AAyCI,UAAMK,eACJX,WAAKO,OAAOE,IAAAA,MAAZT,mBAAmBY,UAAS,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,aAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC5I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,OAAOC,SAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAA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,4BAA4BD,cAAc,CAAA,CAAE,GAAG;AACzF,UAAME,cAAcF,cAAcC,WAAW,IAAID,cAAcG,MAAK,IAAKlB;AACzE,QAAIiB,aAAa;AACf,YAAME,gBAAgBF,2CAAaf,MAAM;AACzCJ,eAASqB,cAAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFnB,eAASqB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM3B,MAAK2B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK5B;EACd;EAEQO,UAAUF,MAAcwB,aAAsB;AACpD,UAAMC,YAAYzB,KAAKK,MAAM,GAAA;AAG7BJ,aAAS,EAAEuB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,OAAOe,SAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,aAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAnJ1B;AAqJIL,eAAS,UAAKhB,cAAL,mBAAgBkC,YAAW,GAAG,0BAAA;AAGvClB,eAAS,UAAKjB,YAAL,mBAAcmC,YAAW,GAAG,wBAAA;AAGrClB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;AA/HaF;AAAN,IAAMA,OAAN;","names":["assertEx","axios","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
package/package.json CHANGED
@@ -10,14 +10,14 @@
10
10
  "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
11
11
  },
12
12
  "dependencies": {
13
- "@xylabs/assert": "^2.14.2",
14
- "@xylabs/axios": "^2.14.2",
15
- "@xylabs/hex": "^2.14.2",
16
- "@xyo-network/account": "~2.90.21",
17
- "@xyo-network/payload-model": "~2.90.21"
13
+ "@xylabs/assert": "^2.14.3",
14
+ "@xylabs/axios": "^2.14.3",
15
+ "@xylabs/hex": "^2.14.3",
16
+ "@xyo-network/account": "~2.90.23",
17
+ "@xyo-network/payload-model": "~2.90.23"
18
18
  },
19
19
  "devDependencies": {
20
- "@xylabs/delay": "^2.14.2",
20
+ "@xylabs/delay": "^2.14.3",
21
21
  "@xylabs/ts-scripts-yarn3": "^3.2.42",
22
22
  "@xylabs/tsconfig": "^3.2.42",
23
23
  "typescript": "^5.3.3"
@@ -61,6 +61,6 @@
61
61
  },
62
62
  "sideEffects": false,
63
63
  "types": "dist/node/index.d.ts",
64
- "version": "2.90.21",
64
+ "version": "2.90.23",
65
65
  "type": "module"
66
66
  }
package/src/Huri.ts CHANGED
@@ -40,7 +40,7 @@ export class Huri<T extends Payload = Payload> {
40
40
 
41
41
  constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {
42
42
  const huriString =
43
- Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href
43
+ Huri.isHuri(huri)?.href ?? (typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href)
44
44
  this.originalHref = huriString
45
45
 
46
46
  const protocol = Huri.parseProtocol(huriString)