@xyo-network/huri 2.89.0-rc.6 → 2.89.0-rc.7
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 +4 -4
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +4 -4
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +4 -4
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +4 -4
- package/dist/node/index.js.map +1 -1
- package/package.json +3 -3
- package/src/Huri.ts +4 -4
package/dist/browser/index.cjs
CHANGED
|
@@ -87,7 +87,7 @@ var Huri = class _Huri {
|
|
|
87
87
|
}
|
|
88
88
|
static parsePath(huri) {
|
|
89
89
|
const protocolSplit = huri.split("//");
|
|
90
|
-
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid format [${huri}]`);
|
|
90
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
|
|
91
91
|
if (protocolSplit.length === 1) {
|
|
92
92
|
return huri;
|
|
93
93
|
}
|
|
@@ -97,12 +97,12 @@ var Huri = class _Huri {
|
|
|
97
97
|
}
|
|
98
98
|
static parseProtocol(huri) {
|
|
99
99
|
const protocolSplit = huri.split("//");
|
|
100
|
-
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
|
|
100
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
|
|
101
101
|
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
|
|
102
102
|
if (rawProtocol) {
|
|
103
103
|
const protocolParts = 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}]`);
|
|
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
106
|
return protocolParts.shift();
|
|
107
107
|
}
|
|
108
108
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAMA,OAAN,MAAMA,MAAAA;EA9Bb,OA8BaA;;;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AACjF,UAAMK,aACJX,MAAKO,OAAOE,IAAAA,GAAOG,QAAQ,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,4BAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC3I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,WAAOC,wBAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAAIV,cAAc;AAChB,YAAMW,oBAAoBX,aAAaY,MAAM,KAAA;AAC7C,WAAKjB,WAAWgB,kBAAkB,CAAA;AAClC,WAAKnB,YAAYmB,kBAAkB,CAAA;IACrC;AAEA,SAAKf,QAAQA;AAEb,SAAKiB,cAAa;EACpB;;;;EAKA,IAAIX,OAAO;AACT,UAAMY,QAAkB,CAAA;AACxB,QAAI,KAAKnB,UAAU;AACjBmB,YAAMC,KAAK,GAAG,KAAKpB,QAAQ,IAAI;IACjC;AACA,QAAI,KAAKJ,SAAS;AAChBuB,YAAMC,KAAK,GAAG,KAAKxB,OAAO,EAAE;IAC9B;AACA,QAAI,KAAKC,WAAW;AAClBsB,YAAMC,KAAK,GAAG,KAAKvB,SAAS,EAAE;IAChC;AACAsB,UAAMC,KAAK,KAAKtB,IAAI;AACpB,WAAOqB,MAAME,KAAK,GAAA;EACpB;EAEA,aAAaC,MAAmClB,MAAoC;AAClF,UAAMmB,aAAanB,KAAKH,QAAQ;MAAEuB,eAAe,UAAUpB,KAAKH,KAAK;IAAG,IAAIc;AAC5E,YAAQ,MAAMU,mBAAMC,IAAOtB,KAAKG,MAAM;MAAEoB,SAASJ;IAAW,CAAA,GAAIK;EAClE;EAEA,OAAO1B,OAAO2B,OAAgB;AAC5B,QAAI,OAAOA,UAAU,UAAU;AAC7B,aAAQA,MAAe3B,SAAU2B,QAAiBd;IACpD;EACF;EAEA,OAAeD,UAAUV,MAAc;AACrC,UAAM0B,gBAAgB1B,KAAKa,MAAM,IAAA;AACjCJ,gCAASiB,cAAcC,UAAU,GAAG,mBAAmB3B,IAAAA,GAAO;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAMA,OAAN,MAAMA,MAAAA;EA9Bb,OA8BaA;;;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AACjF,UAAMK,aACJX,MAAKO,OAAOE,IAAAA,GAAOG,QAAQ,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,4BAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC3I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,WAAOC,wBAASlB,MAAKmB,UAAUR,UA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c;AACzC,UAAM0B,gBAAgB1B,KAAKa,MAAM,IAAA;AACjCJ,gCAASiB,cAAcC,UAAU,GAAG,MAAM,4BAA4BD,cAAc,CAAA,CAAE,GAAG;AACzF,UAAME,cAAcF,cAAcC,WAAW,IAAID,cAAcG,MAAK,IAAKlB;AACzE,QAAIiB,aAAa;AACf,YAAME,gBAAgBF,aAAaf,MAAM,GAAA;AACzCJ,kCAASqB,cAAcH,WAAW,GAAG,MAAM,4BAA4BC,WAAAA,GAAc;AACrFnB,kCAASqB,cAAc,CAAA,EAAGH,WAAW,GAAG,MAAM,qCAAqCC,WAAAA,GAAc;AACjG,aAAOE,cAAcD,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM3B,MAAK2B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK5B;EACd;EAEQO,UAAUF,MAAcwB,aAAsB;AACpD,UAAMC,YAAYzB,KAAKK,MAAM,GAAA;AAG7BJ,gCAAS,EAAEuB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,WAAOe,wBAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,gCAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAEtBL,gCAAS,KAAKhB,WAAWkC,WAAW,GAAG,0BAAA;AAGvClB,gCAAS,KAAKjB,SAASmC,WAAW,GAAG,wBAAA;AAGrClB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -63,7 +63,7 @@ var Huri = class _Huri {
|
|
|
63
63
|
}
|
|
64
64
|
static parsePath(huri) {
|
|
65
65
|
const protocolSplit = huri.split("//");
|
|
66
|
-
assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`);
|
|
66
|
+
assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
|
|
67
67
|
if (protocolSplit.length === 1) {
|
|
68
68
|
return huri;
|
|
69
69
|
}
|
|
@@ -73,12 +73,12 @@ var Huri = class _Huri {
|
|
|
73
73
|
}
|
|
74
74
|
static parseProtocol(huri) {
|
|
75
75
|
const protocolSplit = huri.split("//");
|
|
76
|
-
assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
|
|
76
|
+
assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
|
|
77
77
|
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
|
|
78
78
|
if (rawProtocol) {
|
|
79
79
|
const protocolParts = rawProtocol?.split(":");
|
|
80
|
-
assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
|
|
81
|
-
assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
80
|
+
assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
|
|
81
|
+
assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
82
82
|
return protocolParts.shift();
|
|
83
83
|
}
|
|
84
84
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,OA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,SAASJ;IAAW,CAAA,GAAIK;EAClE;EAEA,OAAO1B,OAAO2B,OAAgB;AAC5B,QAAI,OAAOA,UAAU,UAAU;AAC7B,aAAQA,MAAe3B,SAAU2B,QAAiBd;IACpD;EACF;EAEA,OAAeD,UAAUV,MAAc;AACrC,UAAM0B,gBAAgB1B,KAAKa,MAAM,IAAA;AACjCJ,aAASiB,cAAcC,UAAU,GAAG,mBAAmB3B,IAAAA,GAAO;
|
|
1
|
+
{"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,OAAN,MAAMA,MAAAA;EA9Bb,OA8BaA;;;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AACjF,UAAMK,aACJX,MAAKO,OAAOE,IAAAA,GAAOG,QAAQ,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,aAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC3I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,OAAOC,SAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAAIV,cAAc;AAChB,YAAMW,oBAAoBX,aAAaY,MAAM,KAAA;AAC7C,WAAKjB,WAAWgB,kBAAkB,CAAA;AAClC,WAAKnB,YAAYmB,kBAAkB,CAAA;IACrC;AAEA,SAAKf,QAAQA;AAEb,SAAKiB,cAAa;EACpB;;;;EAKA,IAAIX,OAAO;AACT,UAAMY,QAAkB,CAAA;AACxB,QAAI,KAAKnB,UAAU;AACjBmB,YAAMC,KAAK,GAAG,KAAKpB,QAAQ,IAAI;IACjC;AACA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,MAAK;IAC5B;EACF;EAEA,MAAMX,QAAgC;AACpC,WAAO,MAAM3B,MAAK2B,MAAS,IAAI;EACjC;EAEAa,WAAW;AACT,WAAO,KAAK5B;EACd;EAEQO,UAAUF,MAAcwB,aAAsB;AACpD,UAAMC,YAAYzB,KAAKK,MAAM,GAAA;AAG7BJ,aAAS,EAAEuB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,OAAOe,SAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,aAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAEtBL,aAAS,KAAKhB,WAAWkC,WAAW,GAAG,0BAAA;AAGvClB,aAAS,KAAKjB,SAASmC,WAAW,GAAG,wBAAA;AAGrClB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;","names":["assertEx","axios","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
|
package/dist/node/index.cjs
CHANGED
|
@@ -85,7 +85,7 @@ var _Huri = class _Huri {
|
|
|
85
85
|
}
|
|
86
86
|
static parsePath(huri) {
|
|
87
87
|
const protocolSplit = huri.split("//");
|
|
88
|
-
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid format [${huri}]`);
|
|
88
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
|
|
89
89
|
if (protocolSplit.length === 1) {
|
|
90
90
|
return huri;
|
|
91
91
|
}
|
|
@@ -95,12 +95,12 @@ var _Huri = class _Huri {
|
|
|
95
95
|
}
|
|
96
96
|
static parseProtocol(huri) {
|
|
97
97
|
const protocolSplit = huri.split("//");
|
|
98
|
-
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
|
|
98
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
|
|
99
99
|
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
|
|
100
100
|
if (rawProtocol) {
|
|
101
101
|
const protocolParts = rawProtocol == null ? void 0 : rawProtocol.split(":");
|
|
102
|
-
(0, import_assert.assertEx)(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
|
|
103
|
-
(0, import_assert.assertEx)(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
102
|
+
(0, import_assert.assertEx)(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
|
|
103
|
+
(0, import_assert.assertEx)(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
104
104
|
return protocolParts.shift();
|
|
105
105
|
}
|
|
106
106
|
}
|
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 { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAMA,QAAN,MAAMA,MAAAA;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AAxCrF;AAyCI,UAAMK,eACJX,WAAKO,OAAOE,IAAAA,MAAZT,mBAAmBY,SAAQ,OAAOH,SAAS,W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;AACjCJ,gCAASiB,cAAcC,UAAU,GAAG,mBAAmB3B,IAAAA,GAAO;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,oBAAyB;AACzB,mBAAsB;AAEtB,qBAA6B;AA2BtB,IAAMA,QAAN,MAAMA,MAAAA;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AAxCrF;AAyCI,UAAMK,eACJX,WAAKO,OAAOE,IAAAA,MAAZT,mBAAmBY,SAAQ,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,4BAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC3I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,WAAOC,wBAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAAIV,cAAc;AAChB,YAAMW,oBAAoBX,aAAaY,MAAM,KAAA;AAC7C,WAAKjB,WAAWgB,kBAAkB,CAAA;AAClC,WAAKnB,YAAYmB,kBAAkB,CAAA;IACrC;AAEA,SAAKf,QAAQA;AAEb,SAAKiB,cAAa;EACpB;;;;EAKA,IAAIX,OAAO;AACT,UAAMY,QAAkB,CAAA;AACxB,QAAI,KAAKnB,UAAU;AACjBmB,YAAMC,KAAK,GAAG,KAAKpB,QAAQ,IAAI;IACjC;AACA,QAAI,KAAKJ,SAAS;AAChBuB,YAAMC,KAAK,GAAG,KAAKxB,OAAO,EAAE;IAC9B;AACA,QAAI,KAAKC,WAAW;AAClBsB,YAAMC,KAAK,GAAG,KAAKvB,SAAS,EAAE;IAChC;AACAsB,UAAMC,K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AYzB,KAAKK,MAAM,GAAA;AAG7BJ,gCAAS,EAAEuB,eAAeC,UAAU,CAAA,EAAGN,WAAW,IAAI,4BAAA;AAGtDM,cAAU,CAAA,EAAGN,WAAW,IAAIM,UAAUJ,MAAK,IAAK;AAGhD,UAAMnC,WAAOe,wBAASwB,UAAUC,IAAG,GAAI,mBAAA;AAGvC,SAAKzC,YAAYwC,UAAUJ,MAAK,KAAM;AAGtC,SAAKrC,UAAUyC,UAAUC,IAAG;AAG5BzB,gCAASwB,UAAUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAnJ1B;AAqJIL,kCAAS,UAAKhB,cAAL,mBAAgBkC,YAAW,GAAG,0BAAA;AAGvClB,kCAAS,UAAKjB,YAAL,mBAAcmC,YAAW,GAAG,wBAAA;AAGrClB,gCAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;AA/HaF;AAAN,IAAMA,OAAN;","names":["Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
|
package/dist/node/index.js
CHANGED
|
@@ -61,7 +61,7 @@ var _Huri = class _Huri {
|
|
|
61
61
|
}
|
|
62
62
|
static parsePath(huri) {
|
|
63
63
|
const protocolSplit = huri.split("//");
|
|
64
|
-
assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`);
|
|
64
|
+
assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`);
|
|
65
65
|
if (protocolSplit.length === 1) {
|
|
66
66
|
return huri;
|
|
67
67
|
}
|
|
@@ -71,12 +71,12 @@ var _Huri = class _Huri {
|
|
|
71
71
|
}
|
|
72
72
|
static parseProtocol(huri) {
|
|
73
73
|
const protocolSplit = huri.split("//");
|
|
74
|
-
assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
|
|
74
|
+
assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`);
|
|
75
75
|
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
|
|
76
76
|
if (rawProtocol) {
|
|
77
77
|
const protocolParts = rawProtocol == null ? void 0 : rawProtocol.split(":");
|
|
78
|
-
assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
|
|
79
|
-
assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
78
|
+
assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`);
|
|
79
|
+
assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
80
80
|
return protocolParts.shift();
|
|
81
81
|
}
|
|
82
82
|
}
|
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 { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,QAAN,MAAMA,MAAAA;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YAAYC,MAA4B,EAAEC,cAAcJ,MAAK,IAAkB,CAAC,GAAG;AAxCrF;AAyCI,UAAMK,eACJX,WAAKO,OAAOE,IAAAA,MAAZT,mBAAmBY,SAAQ,OAAOH,SAAS,WAAYA,OAAkBA,gBAAgBI,cAAc,IAAIC,aAAaL,IAAAA,EAAMM,MAAMN,KAAKG;AAC3I,SAAKR,eAAeO;AAEpB,UAAMN,WAAWL,MAAKgB,cAAcL,UAAAA;AACpC,SAAKN,WAAWA,YAAY;AAE5B,UAAMY,OAAOC,SAASlB,MAAKmB,UAAUR,UAAAA,GAAa,cAAA;AAClD,SAAKR,OAAO,KAAKgB,UAAUF,MAAMZ,aAAae,MAAAA;AAG9C,QAAIV,cAAc;AAChB,YAAMW,oBAAoBX,aAAaY,MAAM,KAAA;AAC7C,WAAKjB,WAAWgB,kBAAkB,CAAA;AAClC,WAAKnB,YAAYmB,kBAAkB,CAAA;IACrC;AAEA,SAAKf,QAAQA;AAEb,SAAKiB,cAAa;EACpB;;;;EAKA,IAAIX,OAAO;AACT,UAAMY,QAAkB,CAAA;AACxB,QAAI,KAAKnB,UAAU;AACjBmB,YAAMC,KAAK,GAAG,KAAKpB,QAAQ,IAAI;IACjC;AACA,QAAI,KAAKJ,SAAS;AAChBuB,YAAMC,KAAK,GAAG,KAAKxB,OAAO,EAAE;IAC9B;AACA,QAAI,KAAKC,WAAW;AAClBsB,YAAMC,KAAK,GAAG,KAAKvB,SAAS,EAAE;IAChC;AACAsB,UAAMC,KAAK,KAAKtB,IAAI;AACpB,WAAOqB,MAAME,KAAK,GAAA;EACpB;EAEA,aAAaC,MAAmClB,MAAoC;AAClF,UAAMmB,aAAanB,KAAKH,QAAQ;MAAEuB,eAAe,UAAUpB,KAAKH,KAAK;IAAG,IAAIc;AAC5E,YAAQ,MAAMU,MAAMC,IAAOtB,KAAKG,MAAM;MAAEoB,SAASJ;IAAW,CAAA,GAAIK;EAClE;EAEA,OAAO1B,OAAO2B,OAAgB;AAC5B,QAAI,OAAOA,UAAU,UAAU;AAC7B,aAAQA,MAAe3B,SAAU2B,QAAiBd;IACpD;EACF;EAEA,OAAeD,UAAUV,MAAc;AACrC,UAAM0B,gBAAgB1B,KAAKa,MAAM,IAAA;AACjCJ,aAASiB,cAAcC,UAAU,GAAG,mBAAmB3B,IAAAA,GAAO;
|
|
1
|
+
{"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { axios } from '@xylabs/axios'\nimport { Hash } from '@xylabs/hex'\nimport { AddressValue } from '@xyo-network/account'\nimport { Payload } from '@xyo-network/payload-model'\n\nexport type ObjectCategory = 'block' | 'payload'\n\nexport type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>\n\n/* \n Valid Huri:\n\n [<protocol>://][<archivist>/[<archive>/]]<hash>\n\n defaults:\n protocol: https\n archivist: api.archivist.xyo.network\n*/\n\nexport interface HuriOptions {\n archivistUri?: string\n token?: string\n}\n\nexport interface FetchedPayload<T extends Payload = Payload> {\n huri?: Huri\n payload: T\n}\n\nexport class Huri<T extends Payload = Payload> {\n archive?: string\n archivist?: string\n hash: string\n originalHref: string\n protocol?: string\n token?: string\n\n private isHuri = true\n\n constructor(huri: Hash | Huri | string, { archivistUri, token }: HuriOptions = {}) {\n const huriString =\n Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : huri instanceof ArrayBuffer ? new AddressValue(huri).hex : huri.href\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,aAAa;AAEtB,SAASC,oBAAoB;AA2BtB,IAAMC,QAAN,MAAMA,MAAAA;EACXC;EACAC;EACAC;EACAC;EACAC;EACAC;EAEQC,SAAS;EAEjBC,YA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AUN,WAAW,GAAG,qBAAA;AAEjC,WAAOjC;EACT;EAEQoB,gBAAgB;AAnJ1B;AAqJIL,eAAS,UAAKhB,cAAL,mBAAgBkC,YAAW,GAAG,0BAAA;AAGvClB,eAAS,UAAKjB,YAAL,mBAAcmC,YAAW,GAAG,wBAAA;AAGrClB,aAAS,EAAE,KAAKjB,WAAW,CAAC,KAAKC,YAAY,mDAAA;EAC/C;AACF;AA/HaF;AAAN,IAAMA,OAAN;","names":["assertEx","axios","AddressValue","Huri","archive","archivist","hash","originalHref","protocol","token","isHuri","constructor","huri","archivistUri","huriString","href","ArrayBuffer","AddressValue","hex","parseProtocol","path","assertEx","parsePath","undefined","archivistUriParts","split","validateParse","parts","push","join","fetch","AuthHeader","Authorization","axios","get","headers","data","value","protocolSplit","length","rawProtocol","shift","protocolParts","toString","hasProtocol","pathParts","pop"]}
|
package/package.json
CHANGED
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"@xylabs/assert": "^2.13.30",
|
|
14
14
|
"@xylabs/axios": "^2.13.30",
|
|
15
15
|
"@xylabs/hex": "^2.13.30",
|
|
16
|
-
"@xyo-network/account": "~2.89.0-rc.
|
|
17
|
-
"@xyo-network/payload-model": "~2.89.0-rc.
|
|
16
|
+
"@xyo-network/account": "~2.89.0-rc.7",
|
|
17
|
+
"@xyo-network/payload-model": "~2.89.0-rc.7"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@xylabs/delay": "^2.13.30",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
},
|
|
62
62
|
"sideEffects": false,
|
|
63
63
|
"types": "dist/node/index.d.ts",
|
|
64
|
-
"version": "2.89.0-rc.
|
|
64
|
+
"version": "2.89.0-rc.7",
|
|
65
65
|
"type": "module",
|
|
66
66
|
"stableVersion": "2.88.3"
|
|
67
67
|
}
|
package/src/Huri.ts
CHANGED
|
@@ -92,7 +92,7 @@ export class Huri<T extends Payload = Payload> {
|
|
|
92
92
|
|
|
93
93
|
private static parsePath(huri: string) {
|
|
94
94
|
const protocolSplit = huri.split('//')
|
|
95
|
-
assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)
|
|
95
|
+
assertEx(protocolSplit.length <= 2, () => `Invalid format [${huri}]`)
|
|
96
96
|
if (protocolSplit.length === 1) {
|
|
97
97
|
return huri
|
|
98
98
|
}
|
|
@@ -103,12 +103,12 @@ export class Huri<T extends Payload = Payload> {
|
|
|
103
103
|
|
|
104
104
|
private static parseProtocol(huri: string) {
|
|
105
105
|
const protocolSplit = huri.split('//')
|
|
106
|
-
assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)
|
|
106
|
+
assertEx(protocolSplit.length <= 2, () => `Invalid second protocol [${protocolSplit[2]}]`)
|
|
107
107
|
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined
|
|
108
108
|
if (rawProtocol) {
|
|
109
109
|
const protocolParts = rawProtocol?.split(':')
|
|
110
|
-
assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)
|
|
111
|
-
assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)
|
|
110
|
+
assertEx(protocolParts.length === 2, () => `Invalid protocol format [${rawProtocol}]`)
|
|
111
|
+
assertEx(protocolParts[1].length === 0, () => `Invalid protocol format (post :) [${rawProtocol}]`)
|
|
112
112
|
return protocolParts.shift()
|
|
113
113
|
}
|
|
114
114
|
}
|