@xyo-network/huri 2.92.7 → 2.92.9
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.
- package/dist/browser/index.cjs +8 -8
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +8 -8
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +8 -8
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +8 -8
- package/dist/node/index.js.map +1 -1
- package/package.json +7 -7
- package/src/Huri.ts +8 -8
package/dist/browser/index.cjs
CHANGED
|
@@ -46,8 +46,8 @@ var Huri = class _Huri {
|
|
|
46
46
|
this.originalHref = huriString;
|
|
47
47
|
const protocol = _Huri.parseProtocol(huriString);
|
|
48
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");
|
|
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
51
|
(0, import_assert.assertEx)((0, import_hex.isHash)(this.hash), () => `Invalid hash [${this.hash}]`);
|
|
52
52
|
if (archivistUri) {
|
|
53
53
|
const archivistUriParts = archivistUri.split("://");
|
|
@@ -116,18 +116,18 @@ var Huri = class _Huri {
|
|
|
116
116
|
}
|
|
117
117
|
parsePath(path, hasProtocol) {
|
|
118
118
|
const pathParts = path.split("/");
|
|
119
|
-
(0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
|
|
119
|
+
(0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
|
|
120
120
|
pathParts[0].length === 0 ? pathParts.shift() : null;
|
|
121
|
-
const hash = (0, import_assert.assertEx)(pathParts.pop(), "No hash specified");
|
|
121
|
+
const hash = (0, import_assert.assertEx)(pathParts.pop(), () => "No hash specified");
|
|
122
122
|
this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
|
|
123
123
|
this.archive = pathParts.pop();
|
|
124
|
-
(0, import_assert.assertEx)(pathParts.length === 0, "Too many path parts");
|
|
124
|
+
(0, import_assert.assertEx)(pathParts.length === 0, () => "Too many path parts");
|
|
125
125
|
return hash;
|
|
126
126
|
}
|
|
127
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");
|
|
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
131
|
}
|
|
132
132
|
};
|
|
133
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":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AACzB,mBAAsB;AACtB,iBAAsC;AACtC,qBAA6B;AA2BtB,IAAMA,OAAN,MAAMA,MAAAA;EA9Bb,OA8BaA;;;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AACjF,UAAMK,aACJX,MAAKO,OAAOE,IAAAA,GAAOG,SAClB,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,cAAA;
|
|
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,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,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,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nB;AACzE,QAAIkB,aAAa;AACf,YAAME,gBAAgBF,aAAaf,MAAM,GAAA;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;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"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -22,8 +22,8 @@ var Huri = class _Huri {
|
|
|
22
22
|
this.originalHref = huriString;
|
|
23
23
|
const protocol = _Huri.parseProtocol(huriString);
|
|
24
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");
|
|
25
|
+
const path = assertEx(_Huri.parsePath(huriString), () => "Missing path");
|
|
26
|
+
this.hash = assertEx(this.parsePath(path, protocol !== void 0), () => "Missing hash");
|
|
27
27
|
assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`);
|
|
28
28
|
if (archivistUri) {
|
|
29
29
|
const archivistUriParts = archivistUri.split("://");
|
|
@@ -92,18 +92,18 @@ var Huri = class _Huri {
|
|
|
92
92
|
}
|
|
93
93
|
parsePath(path, hasProtocol) {
|
|
94
94
|
const pathParts = path.split("/");
|
|
95
|
-
assertEx(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
|
|
95
|
+
assertEx(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
|
|
96
96
|
pathParts[0].length === 0 ? pathParts.shift() : null;
|
|
97
|
-
const hash = assertEx(pathParts.pop(), "No hash specified");
|
|
97
|
+
const hash = assertEx(pathParts.pop(), () => "No hash specified");
|
|
98
98
|
this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
|
|
99
99
|
this.archive = pathParts.pop();
|
|
100
|
-
assertEx(pathParts.length === 0, "Too many path parts");
|
|
100
|
+
assertEx(pathParts.length === 0, () => "Too many path parts");
|
|
101
101
|
return hash;
|
|
102
102
|
}
|
|
103
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");
|
|
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
107
|
}
|
|
108
108
|
};
|
|
109
109
|
export {
|
|
@@ -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":";;;;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,cAAA;
|
|
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,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,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"]}
|
package/dist/node/index.cjs
CHANGED
|
@@ -44,8 +44,8 @@ var _Huri = class _Huri {
|
|
|
44
44
|
this.originalHref = huriString;
|
|
45
45
|
const protocol = _Huri.parseProtocol(huriString);
|
|
46
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");
|
|
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
49
|
(0, import_assert.assertEx)((0, import_hex.isHash)(this.hash), () => `Invalid hash [${this.hash}]`);
|
|
50
50
|
if (archivistUri) {
|
|
51
51
|
const archivistUriParts = archivistUri.split("://");
|
|
@@ -114,19 +114,19 @@ var _Huri = class _Huri {
|
|
|
114
114
|
}
|
|
115
115
|
parsePath(path, hasProtocol) {
|
|
116
116
|
const pathParts = path.split("/");
|
|
117
|
-
(0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
|
|
117
|
+
(0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
|
|
118
118
|
pathParts[0].length === 0 ? pathParts.shift() : null;
|
|
119
|
-
const hash = (0, import_assert.assertEx)(pathParts.pop(), "No hash specified");
|
|
119
|
+
const hash = (0, import_assert.assertEx)(pathParts.pop(), () => "No hash specified");
|
|
120
120
|
this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
|
|
121
121
|
this.archive = pathParts.pop();
|
|
122
|
-
(0, import_assert.assertEx)(pathParts.length === 0, "Too many path parts");
|
|
122
|
+
(0, import_assert.assertEx)(pathParts.length === 0, () => "Too many path parts");
|
|
123
123
|
return hash;
|
|
124
124
|
}
|
|
125
125
|
validateParse() {
|
|
126
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");
|
|
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
130
|
}
|
|
131
131
|
};
|
|
132
132
|
__name(_Huri, "Huri");
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -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":";;;;;;;;;;;;;;;;;;;;;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,cAAA;
|
|
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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;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"]}
|
package/dist/node/index.js
CHANGED
|
@@ -20,8 +20,8 @@ var _Huri = class _Huri {
|
|
|
20
20
|
this.originalHref = huriString;
|
|
21
21
|
const protocol = _Huri.parseProtocol(huriString);
|
|
22
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");
|
|
23
|
+
const path = assertEx(_Huri.parsePath(huriString), () => "Missing path");
|
|
24
|
+
this.hash = assertEx(this.parsePath(path, protocol !== void 0), () => "Missing hash");
|
|
25
25
|
assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`);
|
|
26
26
|
if (archivistUri) {
|
|
27
27
|
const archivistUriParts = archivistUri.split("://");
|
|
@@ -90,19 +90,19 @@ var _Huri = class _Huri {
|
|
|
90
90
|
}
|
|
91
91
|
parsePath(path, hasProtocol) {
|
|
92
92
|
const pathParts = path.split("/");
|
|
93
|
-
assertEx(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
|
|
93
|
+
assertEx(!(hasProtocol && pathParts[0].length === 0), () => "Invalid protocol separator");
|
|
94
94
|
pathParts[0].length === 0 ? pathParts.shift() : null;
|
|
95
|
-
const hash = assertEx(pathParts.pop(), "No hash specified");
|
|
95
|
+
const hash = assertEx(pathParts.pop(), () => "No hash specified");
|
|
96
96
|
this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
|
|
97
97
|
this.archive = pathParts.pop();
|
|
98
|
-
assertEx(pathParts.length === 0, "Too many path parts");
|
|
98
|
+
assertEx(pathParts.length === 0, () => "Too many path parts");
|
|
99
99
|
return hash;
|
|
100
100
|
}
|
|
101
101
|
validateParse() {
|
|
102
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");
|
|
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
106
|
}
|
|
107
107
|
};
|
|
108
108
|
__name(_Huri, "Huri");
|
package/dist/node/index.js.map
CHANGED
|
@@ -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":";;;;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,cAAA;
|
|
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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AAG,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;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
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@xylabs/assert": "^3.0.
|
|
14
|
-
"@xylabs/axios": "^3.0.
|
|
15
|
-
"@xylabs/hex": "^3.0.
|
|
16
|
-
"@xyo-network/account": "~2.92.
|
|
17
|
-
"@xyo-network/payload-model": "~2.92.
|
|
13
|
+
"@xylabs/assert": "^3.0.15",
|
|
14
|
+
"@xylabs/axios": "^3.0.15",
|
|
15
|
+
"@xylabs/hex": "^3.0.15",
|
|
16
|
+
"@xyo-network/account": "~2.92.9",
|
|
17
|
+
"@xyo-network/payload-model": "~2.92.9"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@xylabs/delay": "^3.0.
|
|
20
|
+
"@xylabs/delay": "^3.0.15",
|
|
21
21
|
"@xylabs/ts-scripts-yarn3": "^3.5.2",
|
|
22
22
|
"@xylabs/tsconfig": "^3.5.2",
|
|
23
23
|
"typescript": "^5.4.2"
|
|
@@ -61,6 +61,6 @@
|
|
|
61
61
|
},
|
|
62
62
|
"sideEffects": false,
|
|
63
63
|
"types": "dist/node/index.d.ts",
|
|
64
|
-
"version": "2.92.
|
|
64
|
+
"version": "2.92.9",
|
|
65
65
|
"type": "module"
|
|
66
66
|
}
|
package/src/Huri.ts
CHANGED
|
@@ -49,8 +49,8 @@ export class Huri<T extends Payload = Payload> {
|
|
|
49
49
|
const protocol = Huri.parseProtocol(huriString)
|
|
50
50
|
this.protocol = protocol ?? 'https'
|
|
51
51
|
|
|
52
|
-
const path = assertEx(Huri.parsePath(huriString), 'Missing path')
|
|
53
|
-
this.hash = assertEx(this.parsePath(path, protocol !== undefined), 'Missing hash') as Hash
|
|
52
|
+
const path = assertEx(Huri.parsePath(huriString), () => 'Missing path')
|
|
53
|
+
this.hash = assertEx(this.parsePath(path, protocol !== undefined), () => 'Missing hash') as Hash
|
|
54
54
|
|
|
55
55
|
assertEx(isHash(this.hash), () => `Invalid hash [${this.hash}]`)
|
|
56
56
|
|
|
@@ -130,13 +130,13 @@ export class Huri<T extends Payload = Payload> {
|
|
|
130
130
|
const pathParts = path.split('/')
|
|
131
131
|
|
|
132
132
|
//if the protocol was found, then there is not allowed to be a leading /
|
|
133
|
-
assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')
|
|
133
|
+
assertEx(!(hasProtocol && pathParts[0].length === 0), () => 'Invalid protocol separator')
|
|
134
134
|
|
|
135
135
|
//remove leading '/' if needed
|
|
136
136
|
pathParts[0].length === 0 ? pathParts.shift() : null
|
|
137
137
|
|
|
138
138
|
//hash is assumed to be the last part
|
|
139
|
-
const hash = assertEx(pathParts.pop(), 'No hash specified')
|
|
139
|
+
const hash = assertEx(pathParts.pop(), () => 'No hash specified')
|
|
140
140
|
|
|
141
141
|
//archivist is assumed to be the first part
|
|
142
142
|
this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'
|
|
@@ -145,19 +145,19 @@ export class Huri<T extends Payload = Payload> {
|
|
|
145
145
|
this.archive = pathParts.pop()
|
|
146
146
|
|
|
147
147
|
//after we pull off all the path parts, there should be nothing left
|
|
148
|
-
assertEx(pathParts.length === 0, 'Too many path parts')
|
|
148
|
+
assertEx(pathParts.length === 0, () => 'Too many path parts')
|
|
149
149
|
|
|
150
150
|
return hash
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
private validateParse() {
|
|
154
154
|
//the archivist should not be zero length
|
|
155
|
-
assertEx(this.archivist?.length !== 0, 'Invalid archivist length')
|
|
155
|
+
assertEx(this.archivist?.length !== 0, () => 'Invalid archivist length')
|
|
156
156
|
|
|
157
157
|
//the archivist should not be zero length (can be undefined)
|
|
158
|
-
assertEx(this.archive?.length !== 0, 'Invalid archive length')
|
|
158
|
+
assertEx(this.archive?.length !== 0, () => 'Invalid archive length')
|
|
159
159
|
|
|
160
160
|
//the archive should not be set if the archivist is not set
|
|
161
|
-
assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')
|
|
161
|
+
assertEx(!(this.archive && !this.archivist), () => 'If specifying archive, archivist is also required')
|
|
162
162
|
}
|
|
163
163
|
}
|