@xyo-network/huri 2.107.3 → 2.107.4

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,2 +1,133 @@
1
- "use strict";var a=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var p=(o,t)=>a(o,"name",{value:t,configurable:!0});var P=(o,t)=>{for(var i in t)a(o,i,{get:t[i],enumerable:!0})},I=(o,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of u(t))!m.call(o,r)&&r!==i&&a(o,r,{get:()=>t[r],enumerable:!(s=g(t,r))||s.enumerable});return o};var $=o=>I(a({},"__esModule",{value:!0}),o);var y={};P(y,{Huri:()=>e});module.exports=$(y);var h=require("@xylabs/assert"),l=require("@xylabs/axios"),f=require("@xylabs/hex"),d=require("@xyo-network/account");var e=class o{static{p(this,"Huri")}archive;archivist;hash;originalHref;protocol;token;isHuri=!0;constructor(t,{archivistUri:i,token:s}={}){let r=o.isHuri(t)?.href??(typeof t=="string"?t:t instanceof ArrayBuffer?new d.AddressValue(t).hex:t.href);this.originalHref=r;let n=o.parseProtocol(r);this.protocol=n??"https";let v=(0,h.assertEx)(o.parsePath(r),()=>"Missing path");if(this.hash=(0,h.assertEx)(this.parsePath(v,n!==void 0),()=>"Missing hash"),(0,h.assertEx)((0,f.isHash)(this.hash),()=>`Invalid hash [${this.hash}]`),i){let c=i.split("://");this.protocol=c[0],this.archivist=c[1]}this.token=s,this.validateParse()}get href(){let t=[];return this.protocol&&t.push(`${this.protocol}:/`),this.archive&&t.push(`${this.archive}`),this.archivist&&t.push(`${this.archivist}`),t.push(this.hash),t.join("/")}static async fetch(t){let i=t.token?{Authorization:`Bearer ${t.token}`}:void 0;return(await l.axios.get(t.href,{headers:i})).data}static isHuri(t){if(typeof t=="object")return t.isHuri?t:void 0}static parsePath(t){let i=t.split("//");if((0,h.assertEx)(i.length<=2,()=>`Invalid format [${t}]`),i.length===1)return t;if(i.length===2)return i[1]}static parseProtocol(t){let i=t.split("//");(0,h.assertEx)(i.length<=2,()=>`Invalid second protocol [${i[2]}]`);let s=i.length===2?i.shift():void 0;if(s){let r=s?.split(":");return(0,h.assertEx)(r.length===2,()=>`Invalid protocol format [${s}]`),(0,h.assertEx)(r[1].length===0,()=>`Invalid protocol format (post :) [${s}]`),r.shift()}}async fetch(){return await o.fetch(this)}toString(){return this.href}parsePath(t,i){let s=t.split("/");(0,h.assertEx)(!(i&&s[0].length===0),()=>"Invalid protocol separator"),s[0].length===0&&s.shift();let r=(0,h.assertEx)(s.pop(),()=>"No hash specified");return this.archivist=s.shift()??"api.archivist.xyo.network",this.archive=s.pop(),(0,h.assertEx)(s.length===0,()=>"Too many path parts"),r}validateParse(){(0,h.assertEx)(this.archivist?.length!==0,()=>"Invalid archivist length"),(0,h.assertEx)(this.archive?.length!==0,()=>"Invalid archive length"),(0,h.assertEx)(!(this.archive&&!this.archivist),()=>"If specifying archive, archivist is also required")}};
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ Huri: () => Huri
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/Huri.ts
29
+ var import_assert = require("@xylabs/assert");
30
+ var import_axios = require("@xylabs/axios");
31
+ var import_hex = require("@xylabs/hex");
32
+ var import_account = require("@xyo-network/account");
33
+ var Huri = class _Huri {
34
+ static {
35
+ __name(this, "Huri");
36
+ }
37
+ archive;
38
+ archivist;
39
+ hash;
40
+ originalHref;
41
+ protocol;
42
+ token;
43
+ isHuri = true;
44
+ constructor(huri, { archivistUri, token } = {}) {
45
+ const huriString = _Huri.isHuri(huri)?.href ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new import_account.AddressValue(huri).hex : huri.href);
46
+ this.originalHref = huriString;
47
+ const protocol = _Huri.parseProtocol(huriString);
48
+ this.protocol = protocol ?? "https";
49
+ const path = (0, import_assert.assertEx)(_Huri.parsePath(huriString), () => "Missing path");
50
+ this.hash = (0, import_assert.assertEx)(this.parsePath(path, protocol !== void 0), () => "Missing hash");
51
+ (0, import_assert.assertEx)((0, import_hex.isHash)(this.hash), () => `Invalid hash [${this.hash}]`);
52
+ if (archivistUri) {
53
+ const archivistUriParts = archivistUri.split("://");
54
+ this.protocol = archivistUriParts[0];
55
+ this.archivist = archivistUriParts[1];
56
+ }
57
+ this.token = token;
58
+ this.validateParse();
59
+ }
60
+ /*
61
+ The full href or the hash
62
+ */
63
+ get href() {
64
+ const parts = [];
65
+ if (this.protocol) {
66
+ parts.push(`${this.protocol}:/`);
67
+ }
68
+ if (this.archive) {
69
+ parts.push(`${this.archive}`);
70
+ }
71
+ if (this.archivist) {
72
+ parts.push(`${this.archivist}`);
73
+ }
74
+ parts.push(this.hash);
75
+ return parts.join("/");
76
+ }
77
+ static async fetch(huri) {
78
+ const AuthHeader = huri.token ? {
79
+ Authorization: `Bearer ${huri.token}`
80
+ } : void 0;
81
+ return (await import_axios.axios.get(huri.href, {
82
+ headers: AuthHeader
83
+ })).data;
84
+ }
85
+ static isHuri(value) {
86
+ if (typeof value === "object") {
87
+ return value.isHuri ? value : void 0;
88
+ }
89
+ }
90
+ static parsePath(huri) {
91
+ const protocolSplit = huri.split("//");
92
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
93
+ if (protocolSplit.length === 1) {
94
+ return huri;
95
+ }
96
+ if (protocolSplit.length === 2) {
97
+ return protocolSplit[1];
98
+ }
99
+ }
100
+ static parseProtocol(huri) {
101
+ const protocolSplit = huri.split("//");
102
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
103
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
104
+ if (rawProtocol) {
105
+ const protocolParts = rawProtocol?.split(":");
106
+ (0, import_assert.assertEx)(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
107
+ (0, import_assert.assertEx)(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
108
+ return protocolParts.shift();
109
+ }
110
+ }
111
+ async fetch() {
112
+ return await _Huri.fetch(this);
113
+ }
114
+ toString() {
115
+ return this.href;
116
+ }
117
+ parsePath(path, hasProtocol) {
118
+ const pathParts = path.split("/");
119
+ (0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
120
+ pathParts[0].length === 0 ? pathParts.shift() : null;
121
+ const hash = (0, import_assert.assertEx)(pathParts.pop(), () => "No hash specified");
122
+ this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
123
+ this.archive = pathParts.pop();
124
+ (0, import_assert.assertEx)(pathParts.length === 0, () => "Too many path parts");
125
+ return hash;
126
+ }
127
+ validateParse() {
128
+ (0, import_assert.assertEx)(this.archivist?.length !== 0, () => "Invalid archivist length");
129
+ (0, import_assert.assertEx)(this.archive?.length !== 0, () => "Invalid archive length");
130
+ (0, import_assert.assertEx)(!(this.archive && !this.archivist), () => "If specifying archive, archivist is also required");
131
+ }
132
+ };
2
133
  //# sourceMappingURL=index.cjs.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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAyB,0BACzBC,EAAsB,yBACtBC,EAAsC,uBACtCC,EAA6B,gCA2BtB,IAAMC,EAAN,MAAMA,CAAAA,CA9Bb,MA8BaA,CAAAA,EAAAA,aACXC,QACAC,UACAC,KACAC,aACAC,SACAC,MAEQC,OAAS,GAEjBC,YAAYC,EAA4B,CAAEC,aAAAA,EAAcJ,MAAAA,CAAK,EAAkB,CAAC,EAAG,CACjF,IAAMK,EACJX,EAAKO,OAAOE,CAAAA,GAAOG,OAClB,OAAOH,GAAS,SAAYA,EAC3BA,aAAgBI,YAAc,IAAIC,eAAaL,CAAAA,EAAMM,IACrDN,EAAKG,MACT,KAAKR,aAAeO,EAEpB,IAAMN,EAAWL,EAAKgB,cAAcL,CAAAA,EACpC,KAAKN,SAAWA,GAAY,QAE5B,IAAMY,KAAOC,YAASlB,EAAKmB,UAAUR,CAAAA,EAAa,IAAM,cAAA,EAMxD,GALA,KAAKR,QAAOe,YAAS,KAAKC,UAAUF,EAAMZ,IAAae,MAAAA,EAAY,IAAM,cAAA,KAEzEF,eAASG,UAAO,KAAKlB,IAAI,EAAG,IAAM,iBAAiB,KAAKA,IAAI,GAAG,EAG3DO,EAAc,CAChB,IAAMY,EAAoBZ,EAAaa,MAAM,KAAA,EAC7C,KAAKlB,SAAWiB,EAAkB,CAAA,EAClC,KAAKpB,UAAYoB,EAAkB,CAAA,CACrC,CAEA,KAAKhB,MAAQA,EAEb,KAAKkB,cAAa,CACpB,CAKA,IAAIZ,MAAO,CACT,IAAMa,EAAkB,CAAA,EACxB,OAAI,KAA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,MAAMX,OAAgC,CACpC,OAAO,MAAM5B,EAAK4B,MAAS,IAAI,CACjC,CAEAa,UAAW,CACT,OAAO,KAAK7B,IACd,CAEQO,UAAUF,EAAcyB,EAAsB,CACpD,IAAMC,EAAY1B,EAAKM,MAAM,GAAA,KAG7BL,YAAS,EAAEwB,GAAeC,EAAU,CAAA,EAAGN,SAAW,GAAI,IAAM,4BAAA,EAG5DM,EAAU,CAAA,EAAGN,SAAW,GAAIM,EAAUJ,MAAK,EAG3C,IAAMpC,KAAOe,YAASyB,EAAUC,IAAG,EAAI,IAAM,mBAAA,EAG7C,YAAK1C,UAAYyC,EAAUJ,MAAK,GAAM,4BAGtC,KAAKtC,QAAU0C,EAAUC,IAAG,KAG5B1B,YAASyB,EAAUN,SAAW,EAAG,IAAM,qBAAA,EAEhClC,CACT,CAEQqB,eAAgB,IAEtBN,YAAS,KAAKhB,WAAWmC,SAAW,EAAG,IAAM,0BAAA,KAG7CnB,YAAS,KAAKjB,SAASoC,SAAW,EAAG,IAAM,wBAAA,KAG3CnB,YAAS,EAAE,KAAKjB,SAAW,CAAC,KAAKC,WAAY,IAAM,mDAAA,CACrD,CACF","names":["src_exports","__export","Huri","__toCommonJS","import_assert","import_axios","import_hex","import_account","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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;AACtB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,WAAW,IAAI,MAAM,4BAAA;AAG5DM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMpC,WAAOe,wBAASyB,UAAUC,IAAG,GAAI,MAAM,mBAAA;AAG7C,SAAK1C,YAAYyC,UAAUJ,MAAK,KAAM;AAGtC,SAAKtC,UAAU0C,UAAUC,IAAG;AAG5B1B,gCAASyB,UAAUN,WAAW,GAAG,MAAM,qBAAA;AAEvC,WAAOlC;EACT;EAEQqB,gBAAgB;AAEtBN,gCAAS,KAAKhB,WAAWmC,WAAW,GAAG,MAAM,0BAAA;AAG7CnB,gCAAS,KAAKjB,SAASoC,WAAW,GAAG,MAAM,wBAAA;AAG3CnB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,MAAM,mDAAA;EACrD;AACF;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -1,2 +1,112 @@
1
- var l=Object.defineProperty;var n=(h,t)=>l(h,"name",{value:t,configurable:!0});import{assertEx as r}from"@xylabs/assert";import{axios as f}from"@xylabs/axios";import{isHash as d}from"@xylabs/hex";import{AddressValue as v}from"@xyo-network/account";var c=class h{static{n(this,"Huri")}archive;archivist;hash;originalHref;protocol;token;isHuri=!0;constructor(t,{archivistUri:i,token:s}={}){let o=h.isHuri(t)?.href??(typeof t=="string"?t:t instanceof ArrayBuffer?new v(t).hex:t.href);this.originalHref=o;let a=h.parseProtocol(o);this.protocol=a??"https";let p=r(h.parsePath(o),()=>"Missing path");if(this.hash=r(this.parsePath(p,a!==void 0),()=>"Missing hash"),r(d(this.hash),()=>`Invalid hash [${this.hash}]`),i){let e=i.split("://");this.protocol=e[0],this.archivist=e[1]}this.token=s,this.validateParse()}get href(){let t=[];return this.protocol&&t.push(`${this.protocol}:/`),this.archive&&t.push(`${this.archive}`),this.archivist&&t.push(`${this.archivist}`),t.push(this.hash),t.join("/")}static async fetch(t){let i=t.token?{Authorization:`Bearer ${t.token}`}:void 0;return(await f.get(t.href,{headers:i})).data}static isHuri(t){if(typeof t=="object")return t.isHuri?t:void 0}static parsePath(t){let i=t.split("//");if(r(i.length<=2,()=>`Invalid format [${t}]`),i.length===1)return t;if(i.length===2)return i[1]}static parseProtocol(t){let i=t.split("//");r(i.length<=2,()=>`Invalid second protocol [${i[2]}]`);let s=i.length===2?i.shift():void 0;if(s){let o=s?.split(":");return r(o.length===2,()=>`Invalid protocol format [${s}]`),r(o[1].length===0,()=>`Invalid protocol format (post :) [${s}]`),o.shift()}}async fetch(){return await h.fetch(this)}toString(){return this.href}parsePath(t,i){let s=t.split("/");r(!(i&&s[0].length===0),()=>"Invalid protocol separator"),s[0].length===0&&s.shift();let o=r(s.pop(),()=>"No hash specified");return this.archivist=s.shift()??"api.archivist.xyo.network",this.archive=s.pop(),r(s.length===0,()=>"Too many path parts"),o}validateParse(){r(this.archivist?.length!==0,()=>"Invalid archivist length"),r(this.archive?.length!==0,()=>"Invalid archive length"),r(!(this.archive&&!this.archivist),()=>"If specifying archive, archivist is also required")}};export{c as Huri};
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/Huri.ts
5
+ import { assertEx } from "@xylabs/assert";
6
+ import { axios } from "@xylabs/axios";
7
+ import { isHash } from "@xylabs/hex";
8
+ import { AddressValue } from "@xyo-network/account";
9
+ var Huri = class _Huri {
10
+ static {
11
+ __name(this, "Huri");
12
+ }
13
+ archive;
14
+ archivist;
15
+ hash;
16
+ originalHref;
17
+ protocol;
18
+ token;
19
+ isHuri = true;
20
+ constructor(huri, { archivistUri, token } = {}) {
21
+ const huriString = _Huri.isHuri(huri)?.href ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href);
22
+ this.originalHref = huriString;
23
+ const protocol = _Huri.parseProtocol(huriString);
24
+ this.protocol = protocol ?? "https";
25
+ const path = assertEx(_Huri.parsePath(huriString), () => "Missing path");
26
+ this.hash = assertEx(this.parsePath(path, protocol !== void 0), () => "Missing hash");
27
+ assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`);
28
+ if (archivistUri) {
29
+ const archivistUriParts = archivistUri.split("://");
30
+ this.protocol = archivistUriParts[0];
31
+ this.archivist = archivistUriParts[1];
32
+ }
33
+ this.token = token;
34
+ this.validateParse();
35
+ }
36
+ /*
37
+ The full href or the hash
38
+ */
39
+ get href() {
40
+ const parts = [];
41
+ if (this.protocol) {
42
+ parts.push(`${this.protocol}:/`);
43
+ }
44
+ if (this.archive) {
45
+ parts.push(`${this.archive}`);
46
+ }
47
+ if (this.archivist) {
48
+ parts.push(`${this.archivist}`);
49
+ }
50
+ parts.push(this.hash);
51
+ return parts.join("/");
52
+ }
53
+ static async fetch(huri) {
54
+ const AuthHeader = huri.token ? {
55
+ Authorization: `Bearer ${huri.token}`
56
+ } : void 0;
57
+ return (await axios.get(huri.href, {
58
+ headers: AuthHeader
59
+ })).data;
60
+ }
61
+ static isHuri(value) {
62
+ if (typeof value === "object") {
63
+ return value.isHuri ? value : void 0;
64
+ }
65
+ }
66
+ static parsePath(huri) {
67
+ const protocolSplit = huri.split("//");
68
+ assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
69
+ if (protocolSplit.length === 1) {
70
+ return huri;
71
+ }
72
+ if (protocolSplit.length === 2) {
73
+ return protocolSplit[1];
74
+ }
75
+ }
76
+ static parseProtocol(huri) {
77
+ const protocolSplit = huri.split("//");
78
+ assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
79
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
80
+ if (rawProtocol) {
81
+ const protocolParts = rawProtocol?.split(":");
82
+ assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
83
+ assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
84
+ return protocolParts.shift();
85
+ }
86
+ }
87
+ async fetch() {
88
+ return await _Huri.fetch(this);
89
+ }
90
+ toString() {
91
+ return this.href;
92
+ }
93
+ parsePath(path, hasProtocol) {
94
+ const pathParts = path.split("/");
95
+ assertEx(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
96
+ pathParts[0].length === 0 ? pathParts.shift() : null;
97
+ const hash = assertEx(pathParts.pop(), () => "No hash specified");
98
+ this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
99
+ this.archive = pathParts.pop();
100
+ assertEx(pathParts.length === 0, () => "Too many path parts");
101
+ return hash;
102
+ }
103
+ validateParse() {
104
+ assertEx(this.archivist?.length !== 0, () => "Invalid archivist length");
105
+ assertEx(this.archive?.length !== 0, () => "Invalid archive length");
106
+ assertEx(!(this.archive && !this.archivist), () => "If specifying archive, archivist is also required");
107
+ }
108
+ };
109
+ export {
110
+ Huri
111
+ };
2
112
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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":"+EAAA,OAASA,YAAAA,MAAgB,iBACzB,OAASC,SAAAA,MAAa,gBACtB,OAAwBC,UAAAA,MAAc,cACtC,OAASC,gBAAAA,MAAoB,uBA2BtB,IAAMC,EAAN,MAAMA,CAAAA,CA9Bb,MA8BaA,CAAAA,EAAAA,aACXC,QACAC,UACAC,KACAC,aACAC,SACAC,MAEQC,OAAS,GAEjBC,YAAYC,EAA4B,CAAEC,aAAAA,EAAcJ,MAAAA,CAAK,EAAkB,CAAC,EAAG,CACjF,IAAMK,EACJX,EAAKO,OAAO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AWmC,SAAW,EAAG,IAAM,0BAAA,EAG7CnB,EAAS,KAAKjB,SAASoC,SAAW,EAAG,IAAM,wBAAA,EAG3CnB,EAAS,EAAE,KAAKjB,SAAW,CAAC,KAAKC,WAAY,IAAM,mDAAA,CACrD,CACF","names":["assertEx","axios","isHash","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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;AACtB,SAAwBC,cAAc;AACtC,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,SAClB,OAAOH,SAAS,WAAYA,OAC3BA,gBAAgBI,cAAc,IAAIC,aAAaL,IAAAA,EAAMM,MACrDN,KAAKG;AACT,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,OAAOC,SAASlB,MAAKmB,UAAUR,UAAAA,GAAa,MAAM,cAAA;AACxD,SAAKR,OAAOe,SAAS,KAAKC,UAAUF,MAAMZ,aAAae,MAAAA,GAAY,MAAM,cAAA;AAEzEF,aAASG,OAAO,KAAKlB,IAAI,G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,MAAM,IAAA;AACjCL,aAASkB,cAAcC,UAAU,GAAG,MAAM,4BAA4BD,cAAc,CAAA,CAAE,GAAG;AACzF,UAAME,cAAcF,cAAcC,WAAW,IAAID,cAAcG,MAAK,IAAKnB;AACzE,QAAIkB,aAAa;AACf,YAAME,gBAAgBF,aAAaf,MAAM,GAAA;AACzCL,eAASsB,cAAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFpB,eAASsB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM5B,MAAK4B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK7B;EACd;EAEQO,UAAUF,MAAcyB,aAAsB;AACpD,UAAMC,YAAY1B,KAAKM,MAAM,GAAA;AAG7BL,aAAS,EAAEwB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,MAAM,4BAAA;AAG5DM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMpC,OAAOe,SAASyB,UAAUC,IAAG,GAAI,MAAM,mBAAA;AAG7C,SAAK1C,YAAYyC,UAAUJ,MAAK,KAAM;AAGtC,SAAKtC,UAAU0C,UAAUC,IAAG;AAG5B1B,aAASyB,UAAUN,WAAW,GAAG,MAAM,qBAAA;AAEvC,WAAOlC;EACT;EAEQqB,gBAAgB;AAEtBN,aAAS,KAAKhB,WAAWmC,WAAW,GAAG,MAAM,0BAAA;AAG7CnB,aAAS,KAAKjB,SAASoC,WAAW,GAAG,MAAM,wBAAA;AAG3CnB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,MAAM,mDAAA;EACrD;AACF;","names":["assertEx","axios","isHash","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -1,2 +1,133 @@
1
- "use strict";var a=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var p=(o,t)=>a(o,"name",{value:t,configurable:!0});var P=(o,t)=>{for(var i in t)a(o,i,{get:t[i],enumerable:!0})},I=(o,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of u(t))!m.call(o,r)&&r!==i&&a(o,r,{get:()=>t[r],enumerable:!(s=g(t,r))||s.enumerable});return o};var $=o=>I(a({},"__esModule",{value:!0}),o);var y={};P(y,{Huri:()=>e});module.exports=$(y);var h=require("@xylabs/assert"),l=require("@xylabs/axios"),f=require("@xylabs/hex"),d=require("@xyo-network/account");var e=class o{static{p(this,"Huri")}archive;archivist;hash;originalHref;protocol;token;isHuri=!0;constructor(t,{archivistUri:i,token:s}={}){let r=o.isHuri(t)?.href??(typeof t=="string"?t:t instanceof ArrayBuffer?new d.AddressValue(t).hex:t.href);this.originalHref=r;let n=o.parseProtocol(r);this.protocol=n??"https";let v=(0,h.assertEx)(o.parsePath(r),()=>"Missing path");if(this.hash=(0,h.assertEx)(this.parsePath(v,n!==void 0),()=>"Missing hash"),(0,h.assertEx)((0,f.isHash)(this.hash),()=>`Invalid hash [${this.hash}]`),i){let c=i.split("://");this.protocol=c[0],this.archivist=c[1]}this.token=s,this.validateParse()}get href(){let t=[];return this.protocol&&t.push(`${this.protocol}:/`),this.archive&&t.push(`${this.archive}`),this.archivist&&t.push(`${this.archivist}`),t.push(this.hash),t.join("/")}static async fetch(t){let i=t.token?{Authorization:`Bearer ${t.token}`}:void 0;return(await l.axios.get(t.href,{headers:i})).data}static isHuri(t){if(typeof t=="object")return t.isHuri?t:void 0}static parsePath(t){let i=t.split("//");if((0,h.assertEx)(i.length<=2,()=>`Invalid format [${t}]`),i.length===1)return t;if(i.length===2)return i[1]}static parseProtocol(t){let i=t.split("//");(0,h.assertEx)(i.length<=2,()=>`Invalid second protocol [${i[2]}]`);let s=i.length===2?i.shift():void 0;if(s){let r=s?.split(":");return(0,h.assertEx)(r.length===2,()=>`Invalid protocol format [${s}]`),(0,h.assertEx)(r[1].length===0,()=>`Invalid protocol format (post :) [${s}]`),r.shift()}}async fetch(){return await o.fetch(this)}toString(){return this.href}parsePath(t,i){let s=t.split("/");(0,h.assertEx)(!(i&&s[0].length===0),()=>"Invalid protocol separator"),s[0].length===0&&s.shift();let r=(0,h.assertEx)(s.pop(),()=>"No hash specified");return this.archivist=s.shift()??"api.archivist.xyo.network",this.archive=s.pop(),(0,h.assertEx)(s.length===0,()=>"Too many path parts"),r}validateParse(){(0,h.assertEx)(this.archivist?.length!==0,()=>"Invalid archivist length"),(0,h.assertEx)(this.archive?.length!==0,()=>"Invalid archive length"),(0,h.assertEx)(!(this.archive&&!this.archivist),()=>"If specifying archive, archivist is also required")}};
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ Huri: () => Huri
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/Huri.ts
29
+ var import_assert = require("@xylabs/assert");
30
+ var import_axios = require("@xylabs/axios");
31
+ var import_hex = require("@xylabs/hex");
32
+ var import_account = require("@xyo-network/account");
33
+ var Huri = class _Huri {
34
+ static {
35
+ __name(this, "Huri");
36
+ }
37
+ archive;
38
+ archivist;
39
+ hash;
40
+ originalHref;
41
+ protocol;
42
+ token;
43
+ isHuri = true;
44
+ constructor(huri, { archivistUri, token } = {}) {
45
+ const huriString = _Huri.isHuri(huri)?.href ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new import_account.AddressValue(huri).hex : huri.href);
46
+ this.originalHref = huriString;
47
+ const protocol = _Huri.parseProtocol(huriString);
48
+ this.protocol = protocol ?? "https";
49
+ const path = (0, import_assert.assertEx)(_Huri.parsePath(huriString), () => "Missing path");
50
+ this.hash = (0, import_assert.assertEx)(this.parsePath(path, protocol !== void 0), () => "Missing hash");
51
+ (0, import_assert.assertEx)((0, import_hex.isHash)(this.hash), () => `Invalid hash [${this.hash}]`);
52
+ if (archivistUri) {
53
+ const archivistUriParts = archivistUri.split("://");
54
+ this.protocol = archivistUriParts[0];
55
+ this.archivist = archivistUriParts[1];
56
+ }
57
+ this.token = token;
58
+ this.validateParse();
59
+ }
60
+ /*
61
+ The full href or the hash
62
+ */
63
+ get href() {
64
+ const parts = [];
65
+ if (this.protocol) {
66
+ parts.push(`${this.protocol}:/`);
67
+ }
68
+ if (this.archive) {
69
+ parts.push(`${this.archive}`);
70
+ }
71
+ if (this.archivist) {
72
+ parts.push(`${this.archivist}`);
73
+ }
74
+ parts.push(this.hash);
75
+ return parts.join("/");
76
+ }
77
+ static async fetch(huri) {
78
+ const AuthHeader = huri.token ? {
79
+ Authorization: `Bearer ${huri.token}`
80
+ } : void 0;
81
+ return (await import_axios.axios.get(huri.href, {
82
+ headers: AuthHeader
83
+ })).data;
84
+ }
85
+ static isHuri(value) {
86
+ if (typeof value === "object") {
87
+ return value.isHuri ? value : void 0;
88
+ }
89
+ }
90
+ static parsePath(huri) {
91
+ const protocolSplit = huri.split("//");
92
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
93
+ if (protocolSplit.length === 1) {
94
+ return huri;
95
+ }
96
+ if (protocolSplit.length === 2) {
97
+ return protocolSplit[1];
98
+ }
99
+ }
100
+ static parseProtocol(huri) {
101
+ const protocolSplit = huri.split("//");
102
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
103
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
104
+ if (rawProtocol) {
105
+ const protocolParts = rawProtocol?.split(":");
106
+ (0, import_assert.assertEx)(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
107
+ (0, import_assert.assertEx)(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
108
+ return protocolParts.shift();
109
+ }
110
+ }
111
+ async fetch() {
112
+ return await _Huri.fetch(this);
113
+ }
114
+ toString() {
115
+ return this.href;
116
+ }
117
+ parsePath(path, hasProtocol) {
118
+ const pathParts = path.split("/");
119
+ (0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
120
+ pathParts[0].length === 0 ? pathParts.shift() : null;
121
+ const hash = (0, import_assert.assertEx)(pathParts.pop(), () => "No hash specified");
122
+ this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
123
+ this.archive = pathParts.pop();
124
+ (0, import_assert.assertEx)(pathParts.length === 0, () => "Too many path parts");
125
+ return hash;
126
+ }
127
+ validateParse() {
128
+ (0, import_assert.assertEx)(this.archivist?.length !== 0, () => "Invalid archivist length");
129
+ (0, import_assert.assertEx)(this.archive?.length !== 0, () => "Invalid archive length");
130
+ (0, import_assert.assertEx)(!(this.archive && !this.archivist), () => "If specifying archive, archivist is also required");
131
+ }
132
+ };
2
133
  //# sourceMappingURL=index.cjs.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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAyB,0BACzBC,EAAsB,yBACtBC,EAAsC,uBACtCC,EAA6B,gCA2BtB,IAAMC,EAAN,MAAMA,CAAAA,CA9Bb,MA8BaA,CAAAA,EAAAA,aACXC,QACAC,UACAC,KACAC,aACAC,SACAC,MAEQC,OAAS,GAEjBC,YAAYC,EAA4B,CAAEC,aAAAA,EAAcJ,MAAAA,CAAK,EAAkB,CAAC,EAAG,CACjF,IAAMK,EACJX,EAAKO,OAAOE,CAAAA,GAAOG,OAClB,OAAOH,GAAS,SAAYA,EAC3BA,aAAgBI,YAAc,IAAIC,eAAaL,CAAAA,EAAMM,IACrDN,EAAKG,MACT,KAAKR,aAAeO,EAEpB,IAAMN,EAAWL,EAAKgB,cAAcL,CAAAA,EACpC,KAAKN,SAAWA,GAAY,QAE5B,IAAMY,KAAOC,YAASlB,EAAKmB,UAAUR,CAAAA,EAAa,IAAM,cAAA,EAMxD,GALA,KAAKR,QAAOe,YAAS,KAAKC,UAAUF,EAAMZ,IAAae,MAAAA,EAAY,IAAM,cAAA,KAEzEF,eAASG,UAAO,KAAKlB,IAAI,EAAG,IAAM,iBAAiB,KAAKA,IAAI,GAAG,EAG3DO,EAAc,CAChB,IAAMY,EAAoBZ,EAAaa,MAAM,KAAA,EAC7C,KAAKlB,SAAWiB,EAAkB,CAAA,EAClC,KAAKpB,UAAYoB,EAAkB,CAAA,CACrC,CAEA,KAAKhB,MAAQA,EAEb,KAAKkB,cAAa,CACpB,CAKA,IAAIZ,MAAO,CACT,IAAMa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EAAcD,MAAK,CAC5B,CACF,CAEA,MAAMX,OAAgC,CACpC,OAAO,MAAM5B,EAAK4B,MAAS,IAAI,CACjC,CAEAa,UAAW,CACT,OAAO,KAAK7B,IACd,CAEQO,UAAUF,EAAcyB,EAAsB,CACpD,IAAMC,EAAY1B,EAAKM,MAAM,GAAA,KAG7BL,YAAS,EAAEwB,GAAeC,EAAU,CAAA,EAAGN,SAAW,GAAI,IAAM,4BAAA,EAG5DM,EAAU,CAAA,EAAGN,SAAW,GAAIM,EAAUJ,MAAK,EAG3C,IAAMpC,KAAOe,YAASyB,EAAUC,IAAG,EAAI,IAAM,mBAAA,EAG7C,YAAK1C,UAAYyC,EAAUJ,MAAK,GAAM,4BAGtC,KAAKtC,QAAU0C,EAAUC,IAAG,KAG5B1B,YAASyB,EAAUN,SAAW,EAAG,IAAM,qBAAA,EAEhClC,CACT,CAEQqB,eAAgB,IAEtBN,YAAS,KAAKhB,WAAWmC,SAAW,EAAG,IAAM,0BAAA,KAG7CnB,YAAS,KAAKjB,SAASoC,SAAW,EAAG,IAAM,wBAAA,KAG3CnB,YAAS,EAAE,KAAKjB,SAAW,CAAC,KAAKC,WAAY,IAAM,mDAAA,CACrD,CACF","names":["src_exports","__export","Huri","__toCommonJS","import_assert","import_axios","import_hex","import_account","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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;;;;;;;ACA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EwB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,MAAM,4BAAA;AAG5DM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMpC,WAAOe,wBAASyB,UAAUC,IAAG,GAAI,MAAM,mBAAA;AAG7C,SAAK1C,YAAYyC,UAAUJ,MAAK,KAAM;AAGtC,SAAKtC,UAAU0C,UAAUC,IAAG;AAG5B1B,gCAASyB,UAAUN,WAAW,GAAG,MAAM,qBAAA;AAEvC,WAAOlC;EACT;EAEQqB,gBAAgB;AAEtBN,gCAAS,KAAKhB,WAAWmC,WAAW,GAAG,MAAM,0BAAA;AAG7CnB,gCAAS,KAAKjB,SAASoC,WAAW,GAAG,MAAM,wBAAA;AAG3CnB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,MAAM,mDAAA;EACrD;AACF;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -1,2 +1,112 @@
1
- var l=Object.defineProperty;var n=(h,t)=>l(h,"name",{value:t,configurable:!0});import{assertEx as r}from"@xylabs/assert";import{axios as f}from"@xylabs/axios";import{isHash as d}from"@xylabs/hex";import{AddressValue as v}from"@xyo-network/account";var c=class h{static{n(this,"Huri")}archive;archivist;hash;originalHref;protocol;token;isHuri=!0;constructor(t,{archivistUri:i,token:s}={}){let o=h.isHuri(t)?.href??(typeof t=="string"?t:t instanceof ArrayBuffer?new v(t).hex:t.href);this.originalHref=o;let a=h.parseProtocol(o);this.protocol=a??"https";let p=r(h.parsePath(o),()=>"Missing path");if(this.hash=r(this.parsePath(p,a!==void 0),()=>"Missing hash"),r(d(this.hash),()=>`Invalid hash [${this.hash}]`),i){let e=i.split("://");this.protocol=e[0],this.archivist=e[1]}this.token=s,this.validateParse()}get href(){let t=[];return this.protocol&&t.push(`${this.protocol}:/`),this.archive&&t.push(`${this.archive}`),this.archivist&&t.push(`${this.archivist}`),t.push(this.hash),t.join("/")}static async fetch(t){let i=t.token?{Authorization:`Bearer ${t.token}`}:void 0;return(await f.get(t.href,{headers:i})).data}static isHuri(t){if(typeof t=="object")return t.isHuri?t:void 0}static parsePath(t){let i=t.split("//");if(r(i.length<=2,()=>`Invalid format [${t}]`),i.length===1)return t;if(i.length===2)return i[1]}static parseProtocol(t){let i=t.split("//");r(i.length<=2,()=>`Invalid second protocol [${i[2]}]`);let s=i.length===2?i.shift():void 0;if(s){let o=s?.split(":");return r(o.length===2,()=>`Invalid protocol format [${s}]`),r(o[1].length===0,()=>`Invalid protocol format (post :) [${s}]`),o.shift()}}async fetch(){return await h.fetch(this)}toString(){return this.href}parsePath(t,i){let s=t.split("/");r(!(i&&s[0].length===0),()=>"Invalid protocol separator"),s[0].length===0&&s.shift();let o=r(s.pop(),()=>"No hash specified");return this.archivist=s.shift()??"api.archivist.xyo.network",this.archive=s.pop(),r(s.length===0,()=>"Too many path parts"),o}validateParse(){r(this.archivist?.length!==0,()=>"Invalid archivist length"),r(this.archive?.length!==0,()=>"Invalid archive length"),r(!(this.archive&&!this.archivist),()=>"If specifying archive, archivist is also required")}};export{c as Huri};
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/Huri.ts
5
+ import { assertEx } from "@xylabs/assert";
6
+ import { axios } from "@xylabs/axios";
7
+ import { isHash } from "@xylabs/hex";
8
+ import { AddressValue } from "@xyo-network/account";
9
+ var Huri = class _Huri {
10
+ static {
11
+ __name(this, "Huri");
12
+ }
13
+ archive;
14
+ archivist;
15
+ hash;
16
+ originalHref;
17
+ protocol;
18
+ token;
19
+ isHuri = true;
20
+ constructor(huri, { archivistUri, token } = {}) {
21
+ const huriString = _Huri.isHuri(huri)?.href ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href);
22
+ this.originalHref = huriString;
23
+ const protocol = _Huri.parseProtocol(huriString);
24
+ this.protocol = protocol ?? "https";
25
+ const path = assertEx(_Huri.parsePath(huriString), () => "Missing path");
26
+ this.hash = assertEx(this.parsePath(path, protocol !== void 0), () => "Missing hash");
27
+ assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`);
28
+ if (archivistUri) {
29
+ const archivistUriParts = archivistUri.split("://");
30
+ this.protocol = archivistUriParts[0];
31
+ this.archivist = archivistUriParts[1];
32
+ }
33
+ this.token = token;
34
+ this.validateParse();
35
+ }
36
+ /*
37
+ The full href or the hash
38
+ */
39
+ get href() {
40
+ const parts = [];
41
+ if (this.protocol) {
42
+ parts.push(`${this.protocol}:/`);
43
+ }
44
+ if (this.archive) {
45
+ parts.push(`${this.archive}`);
46
+ }
47
+ if (this.archivist) {
48
+ parts.push(`${this.archivist}`);
49
+ }
50
+ parts.push(this.hash);
51
+ return parts.join("/");
52
+ }
53
+ static async fetch(huri) {
54
+ const AuthHeader = huri.token ? {
55
+ Authorization: `Bearer ${huri.token}`
56
+ } : void 0;
57
+ return (await axios.get(huri.href, {
58
+ headers: AuthHeader
59
+ })).data;
60
+ }
61
+ static isHuri(value) {
62
+ if (typeof value === "object") {
63
+ return value.isHuri ? value : void 0;
64
+ }
65
+ }
66
+ static parsePath(huri) {
67
+ const protocolSplit = huri.split("//");
68
+ assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
69
+ if (protocolSplit.length === 1) {
70
+ return huri;
71
+ }
72
+ if (protocolSplit.length === 2) {
73
+ return protocolSplit[1];
74
+ }
75
+ }
76
+ static parseProtocol(huri) {
77
+ const protocolSplit = huri.split("//");
78
+ assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
79
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
80
+ if (rawProtocol) {
81
+ const protocolParts = rawProtocol?.split(":");
82
+ assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
83
+ assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
84
+ return protocolParts.shift();
85
+ }
86
+ }
87
+ async fetch() {
88
+ return await _Huri.fetch(this);
89
+ }
90
+ toString() {
91
+ return this.href;
92
+ }
93
+ parsePath(path, hasProtocol) {
94
+ const pathParts = path.split("/");
95
+ assertEx(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
96
+ pathParts[0].length === 0 ? pathParts.shift() : null;
97
+ const hash = assertEx(pathParts.pop(), () => "No hash specified");
98
+ this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
99
+ this.archive = pathParts.pop();
100
+ assertEx(pathParts.length === 0, () => "Too many path parts");
101
+ return hash;
102
+ }
103
+ validateParse() {
104
+ assertEx(this.archivist?.length !== 0, () => "Invalid archivist length");
105
+ assertEx(this.archive?.length !== 0, () => "Invalid archive length");
106
+ assertEx(!(this.archive && !this.archivist), () => "If specifying archive, archivist is also required");
107
+ }
108
+ };
109
+ export {
110
+ Huri
111
+ };
2
112
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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":"+EAAA,OAASA,YAAAA,MAAgB,iBACzB,OAASC,SAAAA,MAAa,gBACtB,OAAwBC,UAAAA,MAAc,cACtC,OAASC,gBAAAA,MAAoB,uBA2BtB,IAAMC,EAAN,MAAMA,CAAAA,CA9Bb,MA8BaA,CAAAA,EAAAA,aACXC,QACAC,UACAC,KACAC,aACAC,SACAC,MAEQC,OAAS,GAEjBC,YAAYC,EAA4B,CAAEC,aAAAA,EAAcJ,MAAAA,CAAK,EAAkB,CAAC,EAAG,C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AgB,CAEtBN,EAAS,KAAKhB,WAAWmC,SAAW,EAAG,IAAM,0BAAA,EAG7CnB,EAAS,KAAKjB,SAASoC,SAAW,EAAG,IAAM,wBAAA,EAG3CnB,EAAS,EAAE,KAAKjB,SAAW,CAAC,KAAKC,WAAY,IAAM,mDAAA,CACrD,CACF","names":["assertEx","axios","isHash","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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;AACtB,SAAwBC,cAAc;AACtC,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,SAClB,OAAOH,SAAS,WAAYA,OAC3BA,gBAAgBI,cAAc,IAAIC,aAAaL,IAAAA,EAAMM,MACrDN,KAAKG;AACT,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,OAAOC,SAASlB,MAAKmB,UAAUR,UAAAA,GAAa,MAAM,cAAA;AACxD,SAAKR,OAAOe,SAAS,KAAKC,UAAUF,MAAMZ,aAAae,MAAAA,GAAY,MAAM,cAAA;AAEzE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ACzC,UAAM2B,gBAAgB3B,KAAKc,MAAM,IAAA;AACjCL,aAASkB,cAAcC,UAAU,GAAG,MAAM,4BAA4BD,cAAc,CAAA,CAAE,GAAG;AACzF,UAAME,cAAcF,cAAcC,WAAW,IAAID,cAAcG,MAAK,IAAKnB;AACzE,QAAIkB,aAAa;AACf,YAAME,gBAAgBF,aAAaf,MAAM,GAAA;AACzCL,eAASsB,cAAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFpB,eAASsB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM5B,MAAK4B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK7B;EACd;EAEQO,UAAUF,MAAcyB,aAAsB;AACpD,UAAMC,YAAY1B,KAAKM,MAAM,GAAA;AAG7BL,aAAS,EAAEwB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,MAAM,4BAAA;AAG5DM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMpC,OAAOe,SAASyB,UAAUC,IAAG,GAAI,MAAM,mBAAA;AAG7C,SAAK1C,YAAYyC,UAAUJ,MAAK,KAAM;AAGtC,SAAKtC,UAAU0C,UAAUC,IAAG;AAG5B1B,aAASyB,UAAUN,WAAW,GAAG,MAAM,qBAAA;AAEvC,WAAOlC;EACT;EAEQqB,gBAAgB;AAEtBN,aAAS,KAAKhB,WAAWmC,WAAW,GAAG,MAAM,0BAAA;AAG7CnB,aAAS,KAAKjB,SAASoC,WAAW,GAAG,MAAM,wBAAA;AAG3CnB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,MAAM,mDAAA;EACrD;AACF;","names":["assertEx","axios","isHash","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -1,2 +1,138 @@
1
- "use strict";var e=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var f=(o,t)=>e(o,"name",{value:t,configurable:!0});var $=(o,t)=>{for(var i in t)e(o,i,{get:t[i],enumerable:!0})},y=(o,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of P(t))!I.call(o,r)&&r!==i&&e(o,r,{get:()=>t[r],enumerable:!(s=m(t,r))||s.enumerable});return o};var x=o=>y(e({},"__esModule",{value:!0}),o);var H={};$(H,{Huri:()=>n});module.exports=x(H);var h=require("@xylabs/assert"),d=require("@xylabs/axios"),v=require("@xylabs/hex"),g=require("@xyo-network/account");var a=class a{archive;archivist;hash;originalHref;protocol;token;isHuri=!0;constructor(t,{archivistUri:i,token:s}={}){var p;let r=((p=a.isHuri(t))==null?void 0:p.href)??(typeof t=="string"?t:t instanceof ArrayBuffer?new g.AddressValue(t).hex:t.href);this.originalHref=r;let c=a.parseProtocol(r);this.protocol=c??"https";let u=(0,h.assertEx)(a.parsePath(r),()=>"Missing path");if(this.hash=(0,h.assertEx)(this.parsePath(u,c!==void 0),()=>"Missing hash"),(0,h.assertEx)((0,v.isHash)(this.hash),()=>`Invalid hash [${this.hash}]`),i){let l=i.split("://");this.protocol=l[0],this.archivist=l[1]}this.token=s,this.validateParse()}get href(){let t=[];return this.protocol&&t.push(`${this.protocol}:/`),this.archive&&t.push(`${this.archive}`),this.archivist&&t.push(`${this.archivist}`),t.push(this.hash),t.join("/")}static async fetch(t){let i=t.token?{Authorization:`Bearer ${t.token}`}:void 0;return(await d.axios.get(t.href,{headers:i})).data}static isHuri(t){if(typeof t=="object")return t.isHuri?t:void 0}static parsePath(t){let i=t.split("//");if((0,h.assertEx)(i.length<=2,()=>`Invalid format [${t}]`),i.length===1)return t;if(i.length===2)return i[1]}static parseProtocol(t){let i=t.split("//");(0,h.assertEx)(i.length<=2,()=>`Invalid second protocol [${i[2]}]`);let s=i.length===2?i.shift():void 0;if(s){let r=s==null?void 0:s.split(":");return(0,h.assertEx)(r.length===2,()=>`Invalid protocol format [${s}]`),(0,h.assertEx)(r[1].length===0,()=>`Invalid protocol format (post :) [${s}]`),r.shift()}}async fetch(){return await a.fetch(this)}toString(){return this.href}parsePath(t,i){let s=t.split("/");(0,h.assertEx)(!(i&&s[0].length===0),()=>"Invalid protocol separator"),s[0].length===0&&s.shift();let r=(0,h.assertEx)(s.pop(),()=>"No hash specified");return this.archivist=s.shift()??"api.archivist.xyo.network",this.archive=s.pop(),(0,h.assertEx)(s.length===0,()=>"Too many path parts"),r}validateParse(){var t,i;(0,h.assertEx)(((t=this.archivist)==null?void 0:t.length)!==0,()=>"Invalid archivist length"),(0,h.assertEx)(((i=this.archive)==null?void 0:i.length)!==0,()=>"Invalid archive length"),(0,h.assertEx)(!(this.archive&&!this.archivist),()=>"If specifying archive, archivist is also required")}};f(a,"Huri");var n=a;0&&(module.exports={Huri});
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ Huri: () => Huri
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/Huri.ts
29
+ var import_assert = require("@xylabs/assert");
30
+ var import_axios = require("@xylabs/axios");
31
+ var import_hex = require("@xylabs/hex");
32
+ var import_account = require("@xyo-network/account");
33
+ var _Huri = class _Huri {
34
+ archive;
35
+ archivist;
36
+ hash;
37
+ originalHref;
38
+ protocol;
39
+ token;
40
+ isHuri = true;
41
+ constructor(huri, { archivistUri, token } = {}) {
42
+ var _a;
43
+ 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);
44
+ this.originalHref = huriString;
45
+ const protocol = _Huri.parseProtocol(huriString);
46
+ this.protocol = protocol ?? "https";
47
+ const path = (0, import_assert.assertEx)(_Huri.parsePath(huriString), () => "Missing path");
48
+ this.hash = (0, import_assert.assertEx)(this.parsePath(path, protocol !== void 0), () => "Missing hash");
49
+ (0, import_assert.assertEx)((0, import_hex.isHash)(this.hash), () => `Invalid hash [${this.hash}]`);
50
+ if (archivistUri) {
51
+ const archivistUriParts = archivistUri.split("://");
52
+ this.protocol = archivistUriParts[0];
53
+ this.archivist = archivistUriParts[1];
54
+ }
55
+ this.token = token;
56
+ this.validateParse();
57
+ }
58
+ /*
59
+ The full href or the hash
60
+ */
61
+ get href() {
62
+ const parts = [];
63
+ if (this.protocol) {
64
+ parts.push(`${this.protocol}:/`);
65
+ }
66
+ if (this.archive) {
67
+ parts.push(`${this.archive}`);
68
+ }
69
+ if (this.archivist) {
70
+ parts.push(`${this.archivist}`);
71
+ }
72
+ parts.push(this.hash);
73
+ return parts.join("/");
74
+ }
75
+ static async fetch(huri) {
76
+ const AuthHeader = huri.token ? {
77
+ Authorization: `Bearer ${huri.token}`
78
+ } : void 0;
79
+ return (await import_axios.axios.get(huri.href, {
80
+ headers: AuthHeader
81
+ })).data;
82
+ }
83
+ static isHuri(value) {
84
+ if (typeof value === "object") {
85
+ return value.isHuri ? value : void 0;
86
+ }
87
+ }
88
+ static parsePath(huri) {
89
+ const protocolSplit = huri.split("//");
90
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
91
+ if (protocolSplit.length === 1) {
92
+ return huri;
93
+ }
94
+ if (protocolSplit.length === 2) {
95
+ return protocolSplit[1];
96
+ }
97
+ }
98
+ static parseProtocol(huri) {
99
+ const protocolSplit = huri.split("//");
100
+ (0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
101
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
102
+ if (rawProtocol) {
103
+ const protocolParts = rawProtocol == null ? void 0 : rawProtocol.split(":");
104
+ (0, import_assert.assertEx)(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
105
+ (0, import_assert.assertEx)(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
106
+ return protocolParts.shift();
107
+ }
108
+ }
109
+ async fetch() {
110
+ return await _Huri.fetch(this);
111
+ }
112
+ toString() {
113
+ return this.href;
114
+ }
115
+ parsePath(path, hasProtocol) {
116
+ const pathParts = path.split("/");
117
+ (0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
118
+ pathParts[0].length === 0 ? pathParts.shift() : null;
119
+ const hash = (0, import_assert.assertEx)(pathParts.pop(), () => "No hash specified");
120
+ this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
121
+ this.archive = pathParts.pop();
122
+ (0, import_assert.assertEx)(pathParts.length === 0, () => "Too many path parts");
123
+ return hash;
124
+ }
125
+ validateParse() {
126
+ var _a, _b;
127
+ (0, import_assert.assertEx)(((_a = this.archivist) == null ? void 0 : _a.length) !== 0, () => "Invalid archivist length");
128
+ (0, import_assert.assertEx)(((_b = this.archive) == null ? void 0 : _b.length) !== 0, () => "Invalid archive length");
129
+ (0, import_assert.assertEx)(!(this.archive && !this.archivist), () => "If specifying archive, archivist is also required");
130
+ }
131
+ };
132
+ __name(_Huri, "Huri");
133
+ var Huri = _Huri;
134
+ // Annotate the CommonJS export names for ESM import in node:
135
+ 0 && (module.exports = {
136
+ Huri
137
+ });
2
138
  //# sourceMappingURL=index.cjs.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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAyB,0BACzBC,EAAsB,yBACtBC,EAAsC,uBACtCC,EAA6B,gCA2BtB,IAAMC,EAAN,MAAMA,CAAAA,CACXC,QACAC,UACAC,KACAC,aACAC,SACAC,MAEQC,OAAS,GAEjBC,YAAYC,EAA4B,CAAEC,aAAAA,EAAcJ,MAAAA,CAAK,EAAkB,CAAC,EAAG,CAxCrF,IAAAK,EAyCI,IAAMC,IACJZ,EAAAA,EAAKO,OAAOE,CAAAA,IAAZT,YAAAA,EAAmBa,QAClB,OAAOJ,GAAS,SAAYA,EAC3BA,aAAgBK,YAAc,IAAIC,eAAaN,CAAAA,EAAMO,IACrDP,EAAKI,MACT,KAAKT,aAAeQ,EAEpB,IAAMP,EAAWL,EAAKiB,cAAcL,CAAAA,EACpC,KAAKP,SAAWA,GAAY,QAE5B,IAAMa,KAAOC,YAASnB,EAAKoB,UAAUR,CAAAA,EAAa,IAAM,cAAA,EAMxD,GALA,KAAKT,QAAOgB,YAAS,KAAKC,UAAUF,EAAMb,IAAagB,MAAAA,EAAY,IAAM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BD,EAAc,CAAA,CAAE,GAAG,EACzF,IAAME,EAAcF,EAAcC,SAAW,EAAID,EAAcG,MAAK,EAAKnB,OACzE,GAAIkB,EAAa,CACf,IAAME,EAAgBF,GAAAA,YAAAA,EAAaf,MAAM,KACzCL,qBAASsB,EAAcH,SAAW,EAAG,IAAM,4BAA4BC,CAAAA,GAAc,KACrFpB,YAASsB,EAAc,CAAA,EAAGH,SAAW,EAAG,IAAM,qCAAqCC,CAAAA,GAAc,EAC1FE,EAAcD,MAAK,CAC5B,CACF,CAEA,MAAMX,OAAgC,CACpC,OAAO,MAAM7B,EAAK6B,MAAS,IAAI,CACjC,CAEAa,UAAW,CACT,OAAO,KAAK7B,IACd,CAEQO,UAAUF,EAAcyB,EAAsB,CACpD,IAAMC,EAAY1B,EAAKM,MAAM,GAAA,KAG7BL,YAAS,EAAEwB,GAAeC,EAAU,CAAA,EAAGN,SAAW,GAAI,IAAM,4BAAA,EAG5DM,EAAU,CAAA,EAAGN,SAAW,GAAIM,EAAUJ,MAAK,EAG3C,IAAMrC,KAAOgB,YAASyB,EAAUC,IAAG,EAAI,IAAM,mBAAA,EAG7C,YAAK3C,UAAY0C,EAAUJ,MAAK,GAAM,4BAGtC,KAAKvC,QAAU2C,EAAUC,IAAG,KAG5B1B,YAASyB,EAAUN,SAAW,EAAG,IAAM,qBAAA,EAEhCnC,CACT,CAEQsB,eAAgB,CAxJ1B,IAAAd,EAAAmC,KA0JI3B,cAASR,EAAA,KAAKT,YAAL,YAAAS,EAAgB2B,UAAW,EAAG,IAAM,0BAAA,KAG7CnB,cAAS2B,EAAA,KAAK7C,UAAL,YAAA6C,EAAcR,UAAW,EAAG,IAAM,wBAAA,KAG3CnB,YAAS,EAAE,KAAKlB,SAAW,CAAC,KAAKC,WAAY,IAAM,mDAAA,CACrD,CACF,EApIaF,EAAAA,EAAAA,QAAN,IAAMA,EAAN+C","names":["src_exports","__export","Huri","__toCommonJS","import_assert","import_axios","import_hex","import_account","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","_a","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop","_b","_Huri"]}
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 { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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;AACtB,iBAAsC;AACtC,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,UAClB,OAAOH,SAAS,WAAYA,OAC3BA,gBAAgBI,cAAc,IAAIC,4BAAaL,IAAAA,EAAMM,MACrDN,KAAKG;AACT,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,WAAOC,wBAASlB,MAAKmB,UAAUR,UAAAA,GAAa,MAAM,cAAA;AACxD,SAAKR,WAAOe,wBAAS,KAAKC,UAAUF,MAAMZ,aAAae,MAAAA,GAAY,MAAM,cAAA;AAEzEF,oCAASG,mBAAO,KAAKlB,IAAI,GAAG,MAAM,iBAAiB,KAAKA,IAAI,GAAG;AAG/D,QAAIO,cAAc;AAChB,YAAMY,oBAAoBZ,aAAaa,MAAM,KAAA;AAC7C,WAAKlB,WAAWiB,kBAAkB,CAAA;AAClC,WAAKpB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E,QAAIkB,aAAa;AACf,YAAME,gBAAgBF,2CAAaf,MAAM;AACzCL,kCAASsB,cAAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFpB,kCAASsB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM5B,MAAK4B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK7B;EACd;EAEQO,UAAUF,MAAcyB,aAAsB;AACpD,UAAMC,YAAY1B,KAAKM,MAAM,GAAA;AAG7BL,gCAAS,EAAEwB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,MAAM,4BAAA;AAG5DM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMpC,WAAOe,wBAASyB,UAAUC,IAAG,GAAI,MAAM,mBAAA;AAG7C,SAAK1C,YAAYyC,UAAUJ,MAAK,KAAM;AAGtC,SAAKtC,UAAU0C,UAAUC,IAAG;AAG5B1B,gCAASyB,UAAUN,WAAW,GAAG,MAAM,qBAAA;AAEvC,WAAOlC;EACT;EAEQqB,gBAAgB;AAxJ1B;AA0JIN,kCAAS,UAAKhB,cAAL,mBAAgBmC,YAAW,GAAG,MAAM,0BAAA;AAG7CnB,kCAAS,UAAKjB,YAAL,mBAAcoC,YAAW,GAAG,MAAM,wBAAA;AAG3CnB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,MAAM,mDAAA;EACrD;AACF;AApIaF;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","isHash","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
@@ -1,2 +1,113 @@
1
- var d=Object.defineProperty;var c=(l,t)=>d(l,"name",{value:t,configurable:!0});import{assertEx as r}from"@xylabs/assert";import{axios as v}from"@xylabs/axios";import{isHash as g}from"@xylabs/hex";import{AddressValue as u}from"@xyo-network/account";var o=class o{archive;archivist;hash;originalHref;protocol;token;isHuri=!0;constructor(t,{archivistUri:i,token:s}={}){var e;let h=((e=o.isHuri(t))==null?void 0:e.href)??(typeof t=="string"?t:t instanceof ArrayBuffer?new u(t).hex:t.href);this.originalHref=h;let a=o.parseProtocol(h);this.protocol=a??"https";let f=r(o.parsePath(h),()=>"Missing path");if(this.hash=r(this.parsePath(f,a!==void 0),()=>"Missing hash"),r(g(this.hash),()=>`Invalid hash [${this.hash}]`),i){let n=i.split("://");this.protocol=n[0],this.archivist=n[1]}this.token=s,this.validateParse()}get href(){let t=[];return this.protocol&&t.push(`${this.protocol}:/`),this.archive&&t.push(`${this.archive}`),this.archivist&&t.push(`${this.archivist}`),t.push(this.hash),t.join("/")}static async fetch(t){let i=t.token?{Authorization:`Bearer ${t.token}`}:void 0;return(await v.get(t.href,{headers:i})).data}static isHuri(t){if(typeof t=="object")return t.isHuri?t:void 0}static parsePath(t){let i=t.split("//");if(r(i.length<=2,()=>`Invalid format [${t}]`),i.length===1)return t;if(i.length===2)return i[1]}static parseProtocol(t){let i=t.split("//");r(i.length<=2,()=>`Invalid second protocol [${i[2]}]`);let s=i.length===2?i.shift():void 0;if(s){let h=s==null?void 0:s.split(":");return r(h.length===2,()=>`Invalid protocol format [${s}]`),r(h[1].length===0,()=>`Invalid protocol format (post :) [${s}]`),h.shift()}}async fetch(){return await o.fetch(this)}toString(){return this.href}parsePath(t,i){let s=t.split("/");r(!(i&&s[0].length===0),()=>"Invalid protocol separator"),s[0].length===0&&s.shift();let h=r(s.pop(),()=>"No hash specified");return this.archivist=s.shift()??"api.archivist.xyo.network",this.archive=s.pop(),r(s.length===0,()=>"Too many path parts"),h}validateParse(){var t,i;r(((t=this.archivist)==null?void 0:t.length)!==0,()=>"Invalid archivist length"),r(((i=this.archive)==null?void 0:i.length)!==0,()=>"Invalid archive length"),r(!(this.archive&&!this.archivist),()=>"If specifying archive, archivist is also required")}};c(o,"Huri");var p=o;export{p as Huri};
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/Huri.ts
5
+ import { assertEx } from "@xylabs/assert";
6
+ import { axios } from "@xylabs/axios";
7
+ import { isHash } from "@xylabs/hex";
8
+ import { AddressValue } from "@xyo-network/account";
9
+ var _Huri = class _Huri {
10
+ archive;
11
+ archivist;
12
+ hash;
13
+ originalHref;
14
+ protocol;
15
+ token;
16
+ isHuri = true;
17
+ constructor(huri, { archivistUri, token } = {}) {
18
+ var _a;
19
+ const huriString = ((_a = _Huri.isHuri(huri)) == null ? void 0 : _a.href) ?? (typeof huri === "string" ? huri : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href);
20
+ this.originalHref = huriString;
21
+ const protocol = _Huri.parseProtocol(huriString);
22
+ this.protocol = protocol ?? "https";
23
+ const path = assertEx(_Huri.parsePath(huriString), () => "Missing path");
24
+ this.hash = assertEx(this.parsePath(path, protocol !== void 0), () => "Missing hash");
25
+ assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`);
26
+ if (archivistUri) {
27
+ const archivistUriParts = archivistUri.split("://");
28
+ this.protocol = archivistUriParts[0];
29
+ this.archivist = archivistUriParts[1];
30
+ }
31
+ this.token = token;
32
+ this.validateParse();
33
+ }
34
+ /*
35
+ The full href or the hash
36
+ */
37
+ get href() {
38
+ const parts = [];
39
+ if (this.protocol) {
40
+ parts.push(`${this.protocol}:/`);
41
+ }
42
+ if (this.archive) {
43
+ parts.push(`${this.archive}`);
44
+ }
45
+ if (this.archivist) {
46
+ parts.push(`${this.archivist}`);
47
+ }
48
+ parts.push(this.hash);
49
+ return parts.join("/");
50
+ }
51
+ static async fetch(huri) {
52
+ const AuthHeader = huri.token ? {
53
+ Authorization: `Bearer ${huri.token}`
54
+ } : void 0;
55
+ return (await axios.get(huri.href, {
56
+ headers: AuthHeader
57
+ })).data;
58
+ }
59
+ static isHuri(value) {
60
+ if (typeof value === "object") {
61
+ return value.isHuri ? value : void 0;
62
+ }
63
+ }
64
+ static parsePath(huri) {
65
+ const protocolSplit = huri.split("//");
66
+ assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
67
+ if (protocolSplit.length === 1) {
68
+ return huri;
69
+ }
70
+ if (protocolSplit.length === 2) {
71
+ return protocolSplit[1];
72
+ }
73
+ }
74
+ static parseProtocol(huri) {
75
+ const protocolSplit = huri.split("//");
76
+ assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
77
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
78
+ if (rawProtocol) {
79
+ const protocolParts = rawProtocol == null ? void 0 : rawProtocol.split(":");
80
+ assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
81
+ assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
82
+ return protocolParts.shift();
83
+ }
84
+ }
85
+ async fetch() {
86
+ return await _Huri.fetch(this);
87
+ }
88
+ toString() {
89
+ return this.href;
90
+ }
91
+ parsePath(path, hasProtocol) {
92
+ const pathParts = path.split("/");
93
+ assertEx(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
94
+ pathParts[0].length === 0 ? pathParts.shift() : null;
95
+ const hash = assertEx(pathParts.pop(), () => "No hash specified");
96
+ this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
97
+ this.archive = pathParts.pop();
98
+ assertEx(pathParts.length === 0, () => "Too many path parts");
99
+ return hash;
100
+ }
101
+ validateParse() {
102
+ var _a, _b;
103
+ assertEx(((_a = this.archivist) == null ? void 0 : _a.length) !== 0, () => "Invalid archivist length");
104
+ assertEx(((_b = this.archive) == null ? void 0 : _b.length) !== 0, () => "Invalid archive length");
105
+ assertEx(!(this.archive && !this.archivist), () => "If specifying archive, archivist is also required");
106
+ }
107
+ };
108
+ __name(_Huri, "Huri");
109
+ var Huri = _Huri;
110
+ export {
111
+ Huri
112
+ };
2
113
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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":"+EAAA,OAASA,YAAAA,MAAgB,iBACzB,OAASC,SAAAA,MAAa,gBACtB,OAAwBC,UAAAA,MAAc,cACtC,OAASC,gBAAAA,MAAoB,uBA2BtB,IAAMC,EAAN,MAAMA,CAAAA,CACXC,QACAC,UACAC,KACAC,aACAC,SACAC,MAEQC,OAAS,GAEjBC,YAAYC,EAA4B,CAAEC,aAAAA,EAAcJ,MAAAA,CAAK,EAAkB,CAAC,EAAG,CAxCrF,IAAAK,EAyCI,IAAMC,IACJZ,EAAAA,EAAKO,OAAOE,CAAAA,IAAZT,YAAAA,EAAmBa,QAClB,OAAOJ,GAAS,SAAYA,EAC3BA,aAAgBK,YAAc,IAAIC,EAAaN,CAAAA,EAAMO,IACrDP,EAAKI,MACT,KAAKT,aAAeQ,EAEpB,IAAMP,EAAWL,EAAKiB,cAAcL,CAAAA,EACpC,KAAKP,SAAWA,GAAY,QAE5B,IAAMa,EAAOC,EAASnB,EAAKoB,UAAUR,CAAAA,EAAa,IAAM,cAAA,EAMxD,GALA,KAAKT,KAAOgB,EAAS,KAAKC,UAAUF,EAAMb,IAAagB,MAAAA,EAAY,IAAM,cAAA,EAEzEF,EAASG,EAAO,KAAKnB,IAAI,EAAG,IAAM,iBAAiB,KAAKA,IAAI,GAAG,EAG3DO,EAAc,CAChB,IAAMa,EAAoBb,EAAac,MAAM,KAAA,EAC7C,KAAKnB,SAAWkB,EAAkB,CAAA,EAClC,KAAKrB,UA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EAAG,IAAM,4BAA4BC,CAAAA,GAAc,EACrFpB,EAASsB,EAAc,CAAA,EAAGH,SAAW,EAAG,IAAM,qCAAqCC,CAAAA,GAAc,EAC1FE,EAAcD,MAAK,CAC5B,CACF,CAEA,MAAMX,OAAgC,CACpC,OAAO,MAAM7B,EAAK6B,MAAS,IAAI,CACjC,CAEAa,UAAW,CACT,OAAO,KAAK7B,IACd,CAEQO,UAAUF,EAAcyB,EAAsB,CACpD,IAAMC,EAAY1B,EAAKM,MAAM,GAAA,EAG7BL,EAAS,EAAEwB,GAAeC,EAAU,CAAA,EAAGN,SAAW,GAAI,IAAM,4BAAA,EAG5DM,EAAU,CAAA,EAAGN,SAAW,GAAIM,EAAUJ,MAAK,EAG3C,IAAMrC,EAAOgB,EAASyB,EAAUC,IAAG,EAAI,IAAM,mBAAA,EAG7C,YAAK3C,UAAY0C,EAAUJ,MAAK,GAAM,4BAGtC,KAAKvC,QAAU2C,EAAUC,IAAG,EAG5B1B,EAASyB,EAAUN,SAAW,EAAG,IAAM,qBAAA,EAEhCnC,CACT,CAEQsB,eAAgB,CAxJ1B,IAAAd,EAAAmC,EA0JI3B,IAASR,EAAA,KAAKT,YAAL,YAAAS,EAAgB2B,UAAW,EAAG,IAAM,0BAAA,EAG7CnB,IAAS2B,EAAA,KAAK7C,UAAL,YAAA6C,EAAcR,UAAW,EAAG,IAAM,wBAAA,EAG3CnB,EAAS,EAAE,KAAKlB,SAAW,CAAC,KAAKC,WAAY,IAAM,mDAAA,CACrD,CACF,EApIaF,EAAAA,EAAAA,QAAN,IAAMA,EAAN+C","names":["assertEx","axios","isHash","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","_a","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop","_b","_Huri"]}
1
+ {"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Address, Hash, isHash } 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?: Address | string\n hash: Hash\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 ??\n (typeof huri === 'string' ? (huri as string)\n : huri instanceof ArrayBuffer ? new AddressValue(huri).hex\n : 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 = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash\n\n assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)\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;AACtB,SAAwBC,cAAc;AACtC,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,UAClB,OAAOH,SAAS,WAAYA,OAC3BA,gBAAgBI,cAAc,IAAIC,aAAaL,IAAAA,EAAMM,MACrDN,KAAKG;AACT,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,OAAOC,SAASlB,MAAKmB,UAAUR,UAAAA,GAAa,MAAM,cAAA;AACxD,SAAKR,OAAOe,SAAS,KAAKC,UAAUF,MAAMZ,aAAae,MAAAA,GAAY,MAAM,cAAA;AAEzEF,aAASG,OAAO,KAAKlB,IAAI,GAAG,MAAM,iBAAiB,KAAKA,IAAI,GAAG;AAG/D,QAAIO,cAAc;AAChB,YAAMY,oBAAoBZ,aAAaa,MAAM,KAAA;AAC7C,WAAKlB,WAAWiB,kBAAkB,CAAA;AAClC,WAAKpB,YAAYoB,kBAAkB,CAAA;IACrC;AAEA,SAAKhB,QAAQA;AAEb,SAAKkB,cAAa;EACpB;;;;EAKA,IAAIZ,OAAO;AACT,UAAMa,QAAkB,CAAA;AACxB,QAAI,KAAKpB,UAAU;AACjBoB,YAAMC,KAAK,GAAG,KAAKrB,QAAQ,IAAI;IACjC;AACA,QAAI,KAAKJ,SAAS;AAChBwB,YAAMC,K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WAAO,MAAM5B,MAAK4B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK7B;EACd;EAEQO,UAAUF,MAAcyB,aAAsB;AACpD,UAAMC,YAAY1B,KAAKM,MAAM,GAAA;AAG7BL,aAAS,EAAEwB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,MAAM,4BAAA;AAG5DM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMpC,OAAOe,SAASyB,UAAUC,IAAG,GAAI,MAAM,mBAAA;AAG7C,SAAK1C,YAAYyC,UAAUJ,MAAK,KAAM;AAGtC,SAAKtC,UAAU0C,UAAUC,IAAG;AAG5B1B,aAASyB,UAAUN,WAAW,GAAG,MAAM,qBAAA;AAEvC,WAAOlC;EACT;EAEQqB,gBAAgB;AAxJ1B;AA0JIN,eAAS,UAAKhB,cAAL,mBAAgBmC,YAAW,GAAG,MAAM,0BAAA;AAG7CnB,eAAS,UAAKjB,YAAL,mBAAcoC,YAAW,GAAG,MAAM,wBAAA;AAG3CnB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,MAAM,mDAAA;EACrD;AACF;AApIaF;AAAN,IAAMA,OAAN;","names":["assertEx","axios","isHash","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","isHash","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
@@ -13,13 +13,13 @@
13
13
  "@xylabs/assert": "^3.5.1",
14
14
  "@xylabs/axios": "^3.5.1",
15
15
  "@xylabs/hex": "^3.5.1",
16
- "@xyo-network/account": "~2.107.3",
17
- "@xyo-network/payload-model": "~2.107.3"
16
+ "@xyo-network/account": "~2.107.4",
17
+ "@xyo-network/payload-model": "~2.107.4"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@xylabs/delay": "^3.5.1",
21
- "@xylabs/ts-scripts-yarn3": "^3.11.9",
22
- "@xylabs/tsconfig": "^3.11.9",
21
+ "@xylabs/ts-scripts-yarn3": "^3.11.10",
22
+ "@xylabs/tsconfig": "^3.11.10",
23
23
  "typescript": "^5.5.2"
24
24
  },
25
25
  "description": "Primary SDK for using XYO Protocol 2.0",
@@ -61,6 +61,6 @@
61
61
  },
62
62
  "sideEffects": false,
63
63
  "types": "dist/node/index.d.ts",
64
- "version": "2.107.3",
64
+ "version": "2.107.4",
65
65
  "type": "module"
66
66
  }