@xyo-network/huri 2.84.6 → 2.84.8

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.
@@ -1 +1 @@
1
- {"version":3,"file":"Huri.d.ts","sourceRoot":"","sources":["../../src/Huri.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAElC,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAEpD,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,SAAS,CAAA;AAEhD,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC,CAAA;AAY5E,MAAM,WAAW,WAAW;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO;IACzD,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,OAAO,EAAE,CAAC,CAAA;CACX;AAED,qBAAa,IAAI,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd,OAAO,CAAC,MAAM,CAAO;gBAET,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,MAAM,EAAE,EAAE,YAAY,EAAE,KAAK,EAAE,GAAE,WAAgB;IA0BjF,IAAI,IAAI,WAaP;WAEY,KAAK,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAKnF,MAAM,C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D,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,OAAO,EAAE,CAAC,CAAA;CACX;AAED,qBAAa,IAAI,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd,OAAO,CAAC,MAAM,CAAO;gBAET,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,MAAM,EAAE,EAAE,YAAY,EAAE,KAAK,EAAE,GAAE,WAAgB;IA0BjF,IAAI,IAAI,WAaP;WAEY,KAAK,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAKnF,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO;;;IAM5B,OAAO,CAAC,MAAM,CAAC,SAAS;IAWxB,OAAO,CAAC,MAAM,CAAC,aAAa;IAYtB,KAAK,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAIrC,QAAQ;IAIR,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,aAAa;CAUtB"}
@@ -76,7 +76,6 @@ var Huri = class _Huri {
76
76
  if (typeof value === "object") {
77
77
  return value.isHuri ? value : void 0;
78
78
  }
79
- return void 0;
80
79
  }
81
80
  static parsePath(huri) {
82
81
  const protocolSplit = huri.split("//");
@@ -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 return undefined\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;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAM,OAAN,MAAM,MAAkC;AAAA,EAC7C;AAAA,EACA;AA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AA,EACT;AAAA,EAEQ,gBAAgB;AAEtB,gCAAS,KAAK,WAAW,WAAW,GAAG,0BAA0B;AAGjE,gCAAS,KAAK,SAAS,WAAW,GAAG,wBAAwB;AAG7D,gCAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
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;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAM,OAAN,MAAM,MAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,SAAS;AAAA,EAEjB,YAAY,MAA4B,EAAE,cAAc,MAAM,IAAiB,CAAC,GAAG;AACjF,UAAM,aACJ,MAAK,OAAO,IAAI,GAAG,QAAQ,OAAO,SAAS,WAAY,OAAkB,gBAAgB,cAAc,IAAI,4BAAa,IAAI,EAAE,MAAM,KAAK;AAC3I,SAAK,eAAe;AAEpB,UAAM,WAAW,MAAK,cAAc,UAAU;AAC9C,SAAK,WAAW,YAAY;AAE5B,UAAM,WAAO,wBAAS,MAAK,UAAU,UAAU,GAAG,cAAc;AAChE,SAAK,OAAO,KAAK,UAAU,MAAM,aAAa,MAAS;AAGvD,QAAI,cAAc;AAChB,YAAM,oBAAoB,aAAa,MAAM,KAAK;AAClD,WAAK,WAAW,kBAAkB,CAAC;AACnC,WAAK,YAAY,kBAAkB,CAAC;AAAA,IACtC;AAEA,SAAK,QAAQ;AAEb,SAAK,cAAc;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,OAAO;AACT,UAAM,QAAkB,CAAC;AACzB,QAAI,KAAK,UAAU;AACjB,YAAM,KAAK,GAAG,KAAK,QAAQ,IAAI;AAAA,IACjC;AACA,QAAI,KAAK,SAAS;AAChB,YAAM,KAAK,GAAG,KA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AEA,WAAW;AACT,WAAO,KAAK;AAAA,EACd;AAAA,EAEQ,UAAU,MAAc,aAAsB;AACpD,UAAM,YAAY,KAAK,MAAM,GAAG;AAGhC,gCAAS,EAAE,eAAe,UAAU,CAAC,EAAE,WAAW,IAAI,4BAA4B;AAGlF,cAAU,CAAC,EAAE,WAAW,IAAI,UAAU,MAAM,IAAI;AAGhD,UAAM,WAAO,wBAAS,UAAU,IAAI,GAAG,mBAAmB;AAG1D,SAAK,YAAY,UAAU,MAAM,KAAK;AAGtC,SAAK,UAAU,UAAU,IAAI;AAG7B,gCAAS,UAAU,WAAW,GAAG,qBAAqB;AAEtD,WAAO;AAAA,EACT;AAAA,EAEQ,gBAAgB;AAEtB,gCAAS,KAAK,WAAW,WAAW,GAAG,0BAA0B;AAGjE,gCAAS,KAAK,SAAS,WAAW,GAAG,wBAAwB;AAG7D,gCAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
@@ -50,7 +50,6 @@ var Huri = class _Huri {
50
50
  if (typeof value === "object") {
51
51
  return value.isHuri ? value : void 0;
52
52
  }
53
- return void 0;
54
53
  }
55
54
  static parsePath(huri) {
56
55
  const protocolSplit = huri.split("//");
@@ -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 return undefined\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,SAAS,gBAAgB;AACzB,SAAS,aAAa;AAEtB,SAAS,oBAAoB;AA2BtB,IAAM,OAAN,MAAM,MAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,SAAS;AAAA,EAEjB,YAAY,MAA4B,EAAE,cAAc,MAAM,IAAiB,CAAC,GAAG;AACjF,UAAM,aACJ,MAAK,OAAO,IAAI,GAAG,QAAQ,OAAO,SAAS,WAAY,OAAkB,gBAAgB,cAAc,IAAI,aAAa,IAAI,EAAE,MAAM,KAAK;AAC3I,SAAK,eAAe;AAEpB,UAAM,WAAW,MAAK,cAAc,UA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,CAAC;AAAA,IACxB;AAAA,EACF;AAAA,EAEA,OAAe,cAAc,MAAc;AACzC,UAAM,gBAAgB,KAAK,MAAM,IAAI;AACrC,aAAS,cAAc,UAAU,GAAG,4BAA4B,cAAc,CAAC,CAAC,GAAG;AACnF,UAAM,cAAc,cAAc,WAAW,IAAI,cAAc,MAAM,IAAI;AACzE,QAAI,aAAa;AACf,YAAM,gBAAgB,aAAa,MAAM,GAAG;AAC5C,eAAS,cAAc,WAAW,GAAG,4BAA4B,WAAW,GAAG;AAC/E,eAAS,cAAc,CAAC,EAAE,WAAW,GAAG,qCAAqC,WAAW,GAAG;AAC3F,aAAO,cAAc,MAAM;AAAA,IAC7B;AAAA,EACF;AAAA,EAEA,MAAM,QAAgC;AACpC,WAAO,MAAM,MAAK,MAAS,IAAI;AAAA,EACjC;AAAA,EAEA,WAAW;AACT,WAAO,KAAK;AAAA,EACd;AAAA,EAEQ,UAAU,MAAc,aAAsB;AACpD,UAAM,YAAY,KAAK,MAAM,GAAG;AAGhC,aAAS,EAAE,eAAe,UAAU,CAAC,EAAE,WAAW,IAAI,4BAA4B;AAGlF,cAAU,CAAC,EAAE,WAAW,IAAI,UAAU,MAAM,IAAI;AAGhD,UAAM,OAAO,SAAS,UAAU,IAAI,GAAG,mBAAmB;AAG1D,SAAK,YAAY,UAAU,MAAM,KAAK;AAGtC,SAAK,UAAU,UAAU,IAAI;AAG7B,aAAS,UAAU,WAAW,GAAG,qBAAqB;AAEtD,WAAO;AAAA,EACT;AAAA,EAEQ,gBAAgB;AAEtB,aAAS,KAAK,WAAW,WAAW,GAAG,0BAA0B;AAGjE,aAAS,KAAK,SAAS,WAAW,GAAG,wBAAwB;AAG7D,aAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
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,SAAS,gBAAgB;AACzB,SAAS,aAAa;AAEtB,SAAS,oBAAoB;AA2BtB,IAAM,OAAN,MAAM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;IACrB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO;IACzD,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,OAAO,EAAE,CAAC,CAAA;CACX;AAED,qBAAa,IAAI,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd,OAAO,CAAC,MAAM,CAAO;gBAET,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,MAAM,EAAE,EAAE,YAAY,EAAE,KAAK,EAAE,GAAE,WAAgB;IA0BjF,IAAI,IAAI,WAaP;WAEY,KAAK,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAKnF,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO;;;IAM5B,OAAO,CAAC,MAAM,CAAC,SAAS;IAWxB,OAAO,CAAC,MAAM,CAAC,aAAa;IAYtB,KAAK,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAIrC,QAAQ;IAIR,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,aAAa;CAUtB"}
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Huri: () => Huri
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/Huri.ts
28
+ var import_assert = require("@xylabs/assert");
29
+ var import_axios = require("@xylabs/axios");
30
+ var import_account = require("@xyo-network/account");
31
+ var Huri = class _Huri {
32
+ archive;
33
+ archivist;
34
+ hash;
35
+ originalHref;
36
+ protocol;
37
+ token;
38
+ isHuri = true;
39
+ constructor(huri, { archivistUri, token } = {}) {
40
+ var _a;
41
+ 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
+ this.originalHref = huriString;
43
+ const protocol = _Huri.parseProtocol(huriString);
44
+ this.protocol = protocol ?? "https";
45
+ const path = (0, import_assert.assertEx)(_Huri.parsePath(huriString), "Missing path");
46
+ this.hash = this.parsePath(path, protocol !== void 0);
47
+ if (archivistUri) {
48
+ const archivistUriParts = archivistUri.split("://");
49
+ this.protocol = archivistUriParts[0];
50
+ this.archivist = archivistUriParts[1];
51
+ }
52
+ this.token = token;
53
+ this.validateParse();
54
+ }
55
+ /*
56
+ The full href or the hash
57
+ */
58
+ get href() {
59
+ const parts = [];
60
+ if (this.protocol) {
61
+ parts.push(`${this.protocol}:/`);
62
+ }
63
+ if (this.archive) {
64
+ parts.push(`${this.archive}`);
65
+ }
66
+ if (this.archivist) {
67
+ parts.push(`${this.archivist}`);
68
+ }
69
+ parts.push(this.hash);
70
+ return parts.join("/");
71
+ }
72
+ static async fetch(huri) {
73
+ const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : void 0;
74
+ return (await import_axios.axios.get(huri.href, { headers: AuthHeader })).data;
75
+ }
76
+ static isHuri(value) {
77
+ if (typeof value === "object") {
78
+ return value.isHuri ? value : void 0;
79
+ }
80
+ }
81
+ static parsePath(huri) {
82
+ const protocolSplit = huri.split("//");
83
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid format [${huri}]`);
84
+ if (protocolSplit.length === 1) {
85
+ return huri;
86
+ }
87
+ if (protocolSplit.length === 2) {
88
+ return protocolSplit[1];
89
+ }
90
+ }
91
+ static parseProtocol(huri) {
92
+ const protocolSplit = huri.split("//");
93
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
94
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
95
+ if (rawProtocol) {
96
+ const protocolParts = rawProtocol == null ? void 0 : rawProtocol.split(":");
97
+ (0, import_assert.assertEx)(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
98
+ (0, import_assert.assertEx)(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
99
+ return protocolParts.shift();
100
+ }
101
+ }
102
+ async fetch() {
103
+ return await _Huri.fetch(this);
104
+ }
105
+ toString() {
106
+ return this.href;
107
+ }
108
+ parsePath(path, hasProtocol) {
109
+ const pathParts = path.split("/");
110
+ (0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
111
+ pathParts[0].length === 0 ? pathParts.shift() : null;
112
+ const hash = (0, import_assert.assertEx)(pathParts.pop(), "No hash specified");
113
+ this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
114
+ this.archive = pathParts.pop();
115
+ (0, import_assert.assertEx)(pathParts.length === 0, "Too many path parts");
116
+ return hash;
117
+ }
118
+ validateParse() {
119
+ var _a, _b;
120
+ (0, import_assert.assertEx)(((_a = this.archivist) == null ? void 0 : _a.length) !== 0, "Invalid archivist length");
121
+ (0, import_assert.assertEx)(((_b = this.archive) == null ? void 0 : _b.length) !== 0, "Invalid archive length");
122
+ (0, import_assert.assertEx)(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
123
+ }
124
+ };
125
+ // Annotate the CommonJS export names for ESM import in node:
126
+ 0 && (module.exports = {
127
+ Huri
128
+ });
129
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAM,OAAN,MAAM,MAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,SAAS;AAAA,EAEjB,YAAY,MAA4B,EAAE,cAAc,MAAM,IAAiB,CAAC,GAAG;AAxCrF;AAyCI,UAAM,eACJ,WAAK,OAAO,IAAI,MAAhB,mBAAmB,SAAQ,OAAO,SAAS,WAAY,OAAkB,gBAAgB,cAAc,IAAI,4BAAa,IAAI,EAAE,MAAM,KAAK;AAC3I,SAAK,eAAe;AAEpB,UAAM,WAAW,MAAK,cAAc,UAAU;AAC9C,SAAK,WAAW,YAAY;AAE5B,UAAM,WAAO,wBAAS,MAAK,UAAU,UAAU,GAAG,cAAc;AAChE,SAAK,OAAO,KAAK,UAAU,MAAM,aAAa,MAAS;AAGvD,QAAI,cAAc;AAChB,YAAM,oBAAoB,aAAa,MAAM,KAAK;AAClD,WAAK,WAAW,kBAAkB,CAAC;AACnC,WAAK,YAAY,kBAAkB,CAAC;AAAA,IACtC;AAEA,SAAK,QAAQ;AAEb,SAAK,cAAc;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,OAAO;AACT,UAAM,QAAkB,CAAC;AACzB,QAAI,KAAK,UAAU;AAC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C7B;AAAA,EACF;AAAA,EAEA,MAAM,QAAgC;AACpC,WAAO,MAAM,MAAK,MAAS,IAAI;AAAA,EACjC;AAAA,EAEA,WAAW;AACT,WAAO,KAAK;AAAA,EACd;AAAA,EAEQ,UAAU,MAAc,aAAsB;AACpD,UAAM,YAAY,KAAK,MAAM,GAAG;AAGhC,gCAAS,EAAE,eAAe,UAAU,CAAC,EAAE,WAAW,IAAI,4BAA4B;AAGlF,cAAU,CAAC,EAAE,WAAW,IAAI,UAAU,MAAM,IAAI;AAGhD,UAAM,WAAO,wBAAS,UAAU,IAAI,GAAG,mBAAmB;AAG1D,SAAK,YAAY,UAAU,MAAM,KAAK;AAGtC,SAAK,UAAU,UAAU,IAAI;AAG7B,gCAAS,UAAU,WAAW,GAAG,qBAAqB;AAEtD,WAAO;AAAA,EACT;AAAA,EAEQ,gBAAgB;AAnJ1B;AAqJI,kCAAS,UAAK,cAAL,mBAAgB,YAAW,GAAG,0BAA0B;AAGjE,kCAAS,UAAK,YAAL,mBAAc,YAAW,GAAG,wBAAwB;AAG7D,gCAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
@@ -1,33 +1,7 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- Huri: () => Huri
24
- });
25
- module.exports = __toCommonJS(src_exports);
26
-
27
1
  // src/Huri.ts
28
- var import_assert = require("@xylabs/assert");
29
- var import_axios = require("@xylabs/axios");
30
- var import_account = require("@xyo-network/account");
2
+ import { assertEx } from "@xylabs/assert";
3
+ import { axios } from "@xylabs/axios";
4
+ import { AddressValue } from "@xyo-network/account";
31
5
  var Huri = class _Huri {
32
6
  archive;
33
7
  archivist;
@@ -38,11 +12,11 @@ var Huri = class _Huri {
38
12
  isHuri = true;
39
13
  constructor(huri, { archivistUri, token } = {}) {
40
14
  var _a;
41
- 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;
15
+ const huriString = ((_a = _Huri.isHuri(huri)) == null ? void 0 : _a.href) ?? typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href;
42
16
  this.originalHref = huriString;
43
17
  const protocol = _Huri.parseProtocol(huriString);
44
18
  this.protocol = protocol ?? "https";
45
- const path = (0, import_assert.assertEx)(_Huri.parsePath(huriString), "Missing path");
19
+ const path = assertEx(_Huri.parsePath(huriString), "Missing path");
46
20
  this.hash = this.parsePath(path, protocol !== void 0);
47
21
  if (archivistUri) {
48
22
  const archivistUriParts = archivistUri.split("://");
@@ -71,17 +45,16 @@ var Huri = class _Huri {
71
45
  }
72
46
  static async fetch(huri) {
73
47
  const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : void 0;
74
- return (await import_axios.axios.get(huri.href, { headers: AuthHeader })).data;
48
+ return (await axios.get(huri.href, { headers: AuthHeader })).data;
75
49
  }
76
50
  static isHuri(value) {
77
51
  if (typeof value === "object") {
78
52
  return value.isHuri ? value : void 0;
79
53
  }
80
- return void 0;
81
54
  }
82
55
  static parsePath(huri) {
83
56
  const protocolSplit = huri.split("//");
84
- (0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid format [${huri}]`);
57
+ assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`);
85
58
  if (protocolSplit.length === 1) {
86
59
  return huri;
87
60
  }
@@ -91,12 +64,12 @@ var Huri = class _Huri {
91
64
  }
92
65
  static parseProtocol(huri) {
93
66
  const protocolSplit = huri.split("//");
94
- (0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
67
+ assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
95
68
  const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
96
69
  if (rawProtocol) {
97
70
  const protocolParts = rawProtocol == null ? void 0 : rawProtocol.split(":");
98
- (0, import_assert.assertEx)(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
99
- (0, import_assert.assertEx)(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
71
+ assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
72
+ assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
100
73
  return protocolParts.shift();
101
74
  }
102
75
  }
@@ -108,23 +81,22 @@ var Huri = class _Huri {
108
81
  }
109
82
  parsePath(path, hasProtocol) {
110
83
  const pathParts = path.split("/");
111
- (0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
84
+ assertEx(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
112
85
  pathParts[0].length === 0 ? pathParts.shift() : null;
113
- const hash = (0, import_assert.assertEx)(pathParts.pop(), "No hash specified");
86
+ const hash = assertEx(pathParts.pop(), "No hash specified");
114
87
  this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
115
88
  this.archive = pathParts.pop();
116
- (0, import_assert.assertEx)(pathParts.length === 0, "Too many path parts");
89
+ assertEx(pathParts.length === 0, "Too many path parts");
117
90
  return hash;
118
91
  }
119
92
  validateParse() {
120
93
  var _a, _b;
121
- (0, import_assert.assertEx)(((_a = this.archivist) == null ? void 0 : _a.length) !== 0, "Invalid archivist length");
122
- (0, import_assert.assertEx)(((_b = this.archive) == null ? void 0 : _b.length) !== 0, "Invalid archive length");
123
- (0, import_assert.assertEx)(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
94
+ assertEx(((_a = this.archivist) == null ? void 0 : _a.length) !== 0, "Invalid archivist length");
95
+ assertEx(((_b = this.archive) == null ? void 0 : _b.length) !== 0, "Invalid archive length");
96
+ assertEx(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
124
97
  }
125
98
  };
126
- // Annotate the CommonJS export names for ESM import in node:
127
- 0 && (module.exports = {
99
+ export {
128
100
  Huri
129
- });
101
+ };
130
102
  //# sourceMappingURL=index.js.map
@@ -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 return undefined\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;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAM,OAAN,MAAM,MAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,SAAS;AAAA,EAEjB,YAAY,MAA4B,EAAE,cAAc,MAAM,IAAiB,CAAC,GAAG;AAxCrF;AAyCI,UAAM,eACJ,WAAK,OAAO,IAAI,MAAhB,mBAAmB,SAAQ,OAAO,SAAS,WAAY,OAAkB,gBAAgB,cAAc,IAAI,4BAAa,IAAI,EAAE,MAAM,KAAK;AAC3I,SAAK,eAAe;AAEpB,UAAM,WAAW,MAAK,cAAc,UAAU;AAC9C,SAAK,WAAW,YAAY;AAE5B,UAAM,WAAO,wBAAS,MAAK,UAAU,UAAU,GAAG,cAAc;AAChE,SAAK,OAAO,KAAK,UAAU,MAAM,aAAa,MAAS;AAGvD,QAAI,cAAc;AAChB,YAAM,oBAAoB,aA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M,cAAc,cAAc,WAAW,IAAI,cAAc,MAAM,IAAI;AACzE,QAAI,aAAa;AACf,YAAM,gBAAgB,2CAAa,MAAM;AACzC,kCAAS,cAAc,WAAW,GAAG,4BAA4B,WAAW,GAAG;AAC/E,kCAAS,cAAc,CAAC,EAAE,WAAW,GAAG,qCAAqC,WAAW,GAAG;AAC3F,aAAO,cAAc,MAAM;AAAA,IAC7B;AAAA,EACF;AAAA,EAEA,MAAM,QAAgC;AACpC,WAAO,MAAM,MAAK,MAAS,IAAI;AAAA,EACjC;AAAA,EAEA,WAAW;AACT,WAAO,KAAK;AAAA,EACd;AAAA,EAEQ,UAAU,MAAc,aAAsB;AACpD,UAAM,YAAY,KAAK,MAAM,GAAG;AAGhC,gCAAS,EAAE,eAAe,UAAU,CAAC,EAAE,WAAW,IAAI,4BAA4B;AAGlF,cAAU,CAAC,EAAE,WAAW,IAAI,UAAU,MAAM,IAAI;AAGhD,UAAM,WAAO,wBAAS,UAAU,IAAI,GAAG,mBAAmB;AAG1D,SAAK,YAAY,UAAU,MAAM,KAAK;AAGtC,SAAK,UAAU,UAAU,IAAI;AAG7B,gCAAS,UAAU,WAAW,GAAG,qBAAqB;AAEtD,WAAO;AAAA,EACT;AAAA,EAEQ,gBAAgB;AApJ1B;AAsJI,kCAAS,UAAK,cAAL,mBAAgB,YAAW,GAAG,0BAA0B;AAGjE,kCAAS,UAAK,YAAL,mBAAc,YAAW,GAAG,wBAAwB;AAG7D,gCAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
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,SAAS,gBAAgB;AACzB,SAAS,aAAa;AAEtB,SAAS,oBAAoB;AA2BtB,IAAM,OAAN,MAAM,MAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,SAAS;AAAA,EAEjB,YAAY,MAA4B,EAAE,cAAc,MAAM,IAA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,mBAAc,YAAW,GAAG,wBAAwB;AAG7D,aAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
package/package.json CHANGED
@@ -13,17 +13,16 @@
13
13
  "@xylabs/assert": "^2.13.20",
14
14
  "@xylabs/axios": "^2.13.20",
15
15
  "@xylabs/hex": "^2.13.20",
16
- "@xyo-network/account": "~2.84.6",
17
- "@xyo-network/payload-model": "~2.84.6"
16
+ "@xyo-network/account": "~2.84.8",
17
+ "@xyo-network/payload-model": "~2.84.8"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@xylabs/delay": "^2.13.20",
21
- "@xylabs/ts-scripts-yarn3": "^3.2.19",
22
- "@xylabs/tsconfig": "^3.2.19",
21
+ "@xylabs/ts-scripts-yarn3": "^3.2.24",
22
+ "@xylabs/tsconfig": "^3.2.24",
23
23
  "typescript": "^5.3.3"
24
24
  },
25
25
  "description": "Primary SDK for using XYO Protocol 2.0",
26
- "docs": "dist/docs.json",
27
26
  "exports": {
28
27
  ".": {
29
28
  "browser": {
@@ -38,19 +37,19 @@
38
37
  },
39
38
  "node": {
40
39
  "require": {
41
- "types": "./dist/node/index.d.ts",
42
- "default": "./dist/node/index.js"
40
+ "types": "./dist/node/index.d.cts",
41
+ "default": "./dist/node/index.cjs"
43
42
  },
44
43
  "import": {
45
44
  "types": "./dist/node/index.d.mts",
46
- "default": "./dist/node/index.mjs"
45
+ "default": "./dist/node/index.js"
47
46
  }
48
47
  }
49
48
  },
50
49
  "./package.json": "./package.json"
51
50
  },
52
- "main": "dist/node/index.js",
53
- "module": "dist/node/index.mjs",
51
+ "main": "dist/node/index.cjs",
52
+ "module": "dist/node/index.js",
54
53
  "homepage": "https://xyo.network",
55
54
  "license": "LGPL-3.0-only",
56
55
  "publishConfig": {
@@ -62,5 +61,6 @@
62
61
  },
63
62
  "sideEffects": false,
64
63
  "types": "dist/node/index.d.ts",
65
- "version": "2.84.6"
64
+ "version": "2.84.8",
65
+ "type": "module"
66
66
  }
package/src/Huri.ts CHANGED
@@ -88,7 +88,6 @@ export class Huri<T extends Payload = Payload> {
88
88
  if (typeof value === 'object') {
89
89
  return (value as Huri).isHuri ? (value as Huri) : undefined
90
90
  }
91
- return undefined
92
91
  }
93
92
 
94
93
  private static parsePath(huri: string) {
@@ -1,103 +0,0 @@
1
- // src/Huri.ts
2
- import { assertEx } from "@xylabs/assert";
3
- import { axios } from "@xylabs/axios";
4
- import { AddressValue } from "@xyo-network/account";
5
- var Huri = class _Huri {
6
- archive;
7
- archivist;
8
- hash;
9
- originalHref;
10
- protocol;
11
- token;
12
- isHuri = true;
13
- constructor(huri, { archivistUri, token } = {}) {
14
- var _a;
15
- const huriString = ((_a = _Huri.isHuri(huri)) == null ? void 0 : _a.href) ?? typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href;
16
- this.originalHref = huriString;
17
- const protocol = _Huri.parseProtocol(huriString);
18
- this.protocol = protocol ?? "https";
19
- const path = assertEx(_Huri.parsePath(huriString), "Missing path");
20
- this.hash = this.parsePath(path, protocol !== void 0);
21
- if (archivistUri) {
22
- const archivistUriParts = archivistUri.split("://");
23
- this.protocol = archivistUriParts[0];
24
- this.archivist = archivistUriParts[1];
25
- }
26
- this.token = token;
27
- this.validateParse();
28
- }
29
- /*
30
- The full href or the hash
31
- */
32
- get href() {
33
- const parts = [];
34
- if (this.protocol) {
35
- parts.push(`${this.protocol}:/`);
36
- }
37
- if (this.archive) {
38
- parts.push(`${this.archive}`);
39
- }
40
- if (this.archivist) {
41
- parts.push(`${this.archivist}`);
42
- }
43
- parts.push(this.hash);
44
- return parts.join("/");
45
- }
46
- static async fetch(huri) {
47
- const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : void 0;
48
- return (await axios.get(huri.href, { headers: AuthHeader })).data;
49
- }
50
- static isHuri(value) {
51
- if (typeof value === "object") {
52
- return value.isHuri ? value : void 0;
53
- }
54
- return void 0;
55
- }
56
- static parsePath(huri) {
57
- const protocolSplit = huri.split("//");
58
- assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`);
59
- if (protocolSplit.length === 1) {
60
- return huri;
61
- }
62
- if (protocolSplit.length === 2) {
63
- return protocolSplit[1];
64
- }
65
- }
66
- static parseProtocol(huri) {
67
- const protocolSplit = huri.split("//");
68
- assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
69
- const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
70
- if (rawProtocol) {
71
- const protocolParts = rawProtocol == null ? void 0 : rawProtocol.split(":");
72
- assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
73
- assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
74
- return protocolParts.shift();
75
- }
76
- }
77
- async fetch() {
78
- return await _Huri.fetch(this);
79
- }
80
- toString() {
81
- return this.href;
82
- }
83
- parsePath(path, hasProtocol) {
84
- const pathParts = path.split("/");
85
- assertEx(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
86
- pathParts[0].length === 0 ? pathParts.shift() : null;
87
- const hash = assertEx(pathParts.pop(), "No hash specified");
88
- this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
89
- this.archive = pathParts.pop();
90
- assertEx(pathParts.length === 0, "Too many path parts");
91
- return hash;
92
- }
93
- validateParse() {
94
- var _a, _b;
95
- assertEx(((_a = this.archivist) == null ? void 0 : _a.length) !== 0, "Invalid archivist length");
96
- assertEx(((_b = this.archive) == null ? void 0 : _b.length) !== 0, "Invalid archive length");
97
- assertEx(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
98
- }
99
- };
100
- export {
101
- Huri
102
- };
103
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
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 return undefined\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,SAAS,gBAAgB;AACzB,SAAS,aAAa;AAEtB,SAAS,oBAAoB;AA2BtB,IAAM,OAAN,MAAM,MAAkC;AA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