@xyo-network/huri 2.74.4 → 2.75.0
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/Huri.cjs +122 -0
- package/dist/browser/Huri.cjs.map +1 -0
- package/dist/browser/Huri.d.mts.map +1 -0
- package/dist/browser/Huri.d.ts.map +1 -0
- package/dist/{index.mjs → browser/Huri.js} +1 -1
- package/dist/browser/Huri.js.map +1 -0
- package/dist/{index.js → browser/index.cjs} +1 -5
- package/dist/browser/index.cjs.map +1 -0
- package/dist/{index.d.mts.map → browser/index.d.mts.map} +1 -1
- package/dist/{index.d.ts.map → browser/index.d.ts.map} +1 -1
- package/dist/browser/index.js +101 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/node/Huri.d.mts +34 -0
- package/dist/node/Huri.d.mts.map +1 -0
- package/dist/node/Huri.d.ts +34 -0
- package/dist/node/Huri.d.ts.map +1 -0
- package/dist/node/Huri.js +124 -0
- package/dist/node/Huri.js.map +1 -0
- package/dist/node/Huri.mjs +100 -0
- package/dist/node/Huri.mjs.map +1 -0
- package/dist/node/index.d.mts +2 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +23 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/index.mjs +2 -0
- package/dist/node/index.mjs.map +1 -0
- package/package.json +29 -26
- package/dist/Huri.d.mts.map +0 -1
- package/dist/Huri.d.ts.map +0 -1
- package/dist/docs.json +0 -1892
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- /package/dist/{Huri.d.mts → browser/Huri.d.mts} +0 -0
- /package/dist/{Huri.d.ts → browser/Huri.d.ts} +0 -0
- /package/dist/{index.d.mts → browser/index.d.mts} +0 -0
- /package/dist/{index.d.ts → browser/index.d.ts} +0 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/Huri.ts
|
|
21
|
+
var Huri_exports = {};
|
|
22
|
+
__export(Huri_exports, {
|
|
23
|
+
Huri: () => Huri
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(Huri_exports);
|
|
26
|
+
var import_assert = require("@xylabs/assert");
|
|
27
|
+
var import_account = require("@xyo-network/account");
|
|
28
|
+
var import_axios = require("@xyo-network/axios");
|
|
29
|
+
var Huri = class _Huri {
|
|
30
|
+
archive;
|
|
31
|
+
archivist;
|
|
32
|
+
hash;
|
|
33
|
+
originalHref;
|
|
34
|
+
protocol;
|
|
35
|
+
token;
|
|
36
|
+
isHuri = true;
|
|
37
|
+
constructor(huri, { archivistUri, token } = {}) {
|
|
38
|
+
const huriString = _Huri.isHuri(huri)?.href ?? typeof huri === "string" ? huri : new import_account.AddressValue(huri).hex;
|
|
39
|
+
this.originalHref = huriString;
|
|
40
|
+
const protocol = _Huri.parseProtocol(huriString);
|
|
41
|
+
this.protocol = protocol ?? "https";
|
|
42
|
+
const path = (0, import_assert.assertEx)(_Huri.parsePath(huriString), "Missing path");
|
|
43
|
+
this.hash = this.parsePath(path, protocol !== void 0);
|
|
44
|
+
if (archivistUri) {
|
|
45
|
+
const archivistUriParts = archivistUri.split("://");
|
|
46
|
+
this.protocol = archivistUriParts[0];
|
|
47
|
+
this.archivist = archivistUriParts[1];
|
|
48
|
+
}
|
|
49
|
+
this.token = token;
|
|
50
|
+
this.validateParse();
|
|
51
|
+
}
|
|
52
|
+
/*
|
|
53
|
+
The full href or the hash
|
|
54
|
+
*/
|
|
55
|
+
get href() {
|
|
56
|
+
const parts = [];
|
|
57
|
+
if (this.protocol) {
|
|
58
|
+
parts.push(`${this.protocol}:/`);
|
|
59
|
+
}
|
|
60
|
+
if (this.archive) {
|
|
61
|
+
parts.push(`${this.archive}`);
|
|
62
|
+
}
|
|
63
|
+
if (this.archivist) {
|
|
64
|
+
parts.push(`${this.archivist}`);
|
|
65
|
+
}
|
|
66
|
+
parts.push(this.hash);
|
|
67
|
+
return parts.join("/");
|
|
68
|
+
}
|
|
69
|
+
static async fetch(huri) {
|
|
70
|
+
const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : void 0;
|
|
71
|
+
return (await import_axios.axios.get(huri.href, { headers: AuthHeader })).data;
|
|
72
|
+
}
|
|
73
|
+
static isHuri(value) {
|
|
74
|
+
if (typeof value === "object") {
|
|
75
|
+
return value.isHuri ? value : void 0;
|
|
76
|
+
}
|
|
77
|
+
return void 0;
|
|
78
|
+
}
|
|
79
|
+
static parsePath(huri) {
|
|
80
|
+
const protocolSplit = huri.split("//");
|
|
81
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid format [${huri}]`);
|
|
82
|
+
if (protocolSplit.length === 1) {
|
|
83
|
+
return huri;
|
|
84
|
+
}
|
|
85
|
+
if (protocolSplit.length === 2) {
|
|
86
|
+
return protocolSplit[1];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
static parseProtocol(huri) {
|
|
90
|
+
const protocolSplit = huri.split("//");
|
|
91
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
|
|
92
|
+
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
|
|
93
|
+
if (rawProtocol) {
|
|
94
|
+
const protocolParts = rawProtocol?.split(":");
|
|
95
|
+
(0, import_assert.assertEx)(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
|
|
96
|
+
(0, import_assert.assertEx)(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
97
|
+
return protocolParts.shift();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async fetch() {
|
|
101
|
+
return await _Huri.fetch(this);
|
|
102
|
+
}
|
|
103
|
+
toString() {
|
|
104
|
+
return this.href;
|
|
105
|
+
}
|
|
106
|
+
parsePath(path, hasProtocol) {
|
|
107
|
+
const pathParts = path.split("/");
|
|
108
|
+
(0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
|
|
109
|
+
pathParts[0].length === 0 ? pathParts.shift() : null;
|
|
110
|
+
const hash = (0, import_assert.assertEx)(pathParts.pop(), "No hash specified");
|
|
111
|
+
this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
|
|
112
|
+
this.archive = pathParts.pop();
|
|
113
|
+
(0, import_assert.assertEx)(pathParts.length === 0, "Too many path parts");
|
|
114
|
+
return hash;
|
|
115
|
+
}
|
|
116
|
+
validateParse() {
|
|
117
|
+
(0, import_assert.assertEx)(this.archivist?.length !== 0, "Invalid archivist length");
|
|
118
|
+
(0, import_assert.assertEx)(this.archive?.length !== 0, "Invalid archive length");
|
|
119
|
+
(0, import_assert.assertEx)(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
//# sourceMappingURL=Huri.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { AddressValue } from '@xyo-network/account'\nimport { axios } from '@xyo-network/axios'\nimport { DataLike } from '@xyo-network/core'\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: DataLike | Huri, { archivistUri, token }: HuriOptions = {}) {\n const huriString = Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : new AddressValue(huri as DataLike).hex\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n return undefined\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;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IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd,OAAO,CAAC,MAAM,CAAO;gBAET,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,EAAE,YAAY,EAAE,KAAK,EAAE,GAAE,WAAgB;IAyB5E,IAAI,IAAI,WAaP;WAEY,KAAK,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAKnF,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO;;;IAO5B,OAAO,CAAC,MAAM,CAAC,SAAS;IAWxB,OAAO,CAAC,MAAM,CAAC,aAAa;IAYtB,KAAK,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAIrC,QAAQ;IAIR,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,aAAa;CAUtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { AddressValue } from '@xyo-network/account'\nimport { axios } from '@xyo-network/axios'\nimport { DataLike } from '@xyo-network/core'\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: DataLike | Huri, { archivistUri, token }: HuriOptions = {}) {\n const huriString = Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : new AddressValue(huri as DataLike).hex\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n return undefined\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,oBAAoB;AAC7B,SAAS,aAAa;AA4Bf,IAAM,OAAN,MAAM,MAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,SAAS;AAAA,EAEjB,YAAY,MAAuB,EAAE,cAAc,MAAM,IAAiB,CAAC,GAAG;AAC5E,UAAM,aAAa,MAAK,OAAO,IAAI,GAAG,QAAQ,OAAO,SAAS,WAAY,OAAkB,IAAI,aAAa,IAAgB,EAAE;AAC/H,SAAK,eAAe;AAEpB,UAAM,WAAW,MAAK,cAAc,UAAU;AAC9C,SAAK,WAAW,YAAY;AAE5B,UAAM,OAAO,SAAS,MAAK,UAAU,UAAU,GAAG,cAAc;AAChE,SAAK,OAAO,KAAK,UAAU,MAAM,aAAa,MAAS;AAGvD,QAAI,cAAc;AAChB,YAAM,oBAAoB,aA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AAc,cAAc,WAAW,IAAI,cAAc,MAAM,IAAI;AACzE,QAAI,aAAa;AACf,YAAM,gBAAgB,aAAa,MAAM,GAAG;AAC5C,eAAS,cAAc,WAAW,GAAG,4BAA4B,WAAW,GAAG;AAC/E,eAAS,cAAc,CAAC,EAAE,WAAW,GAAG,qCAAqC,WAAW,GAAG;AAC3F,aAAO,cAAc,MAAM;AAAA,IAC7B;AAAA,EACF;AAAA,EAEA,MAAM,QAAgC;AACpC,WAAO,MAAM,MAAK,MAAS,IAAI;AAAA,EACjC;AAAA,EAEA,WAAW;AACT,WAAO,KAAK;AAAA,EACd;AAAA,EAEQ,UAAU,MAAc,aAAsB;AACpD,UAAM,YAAY,KAAK,MAAM,GAAG;AAGhC,aAAS,EAAE,eAAe,UAAU,CAAC,EAAE,WAAW,IAAI,4BAA4B;AAGlF,cAAU,CAAC,EAAE,WAAW,IAAI,UAAU,MAAM,IAAI;AAGhD,UAAM,OAAO,SAAS,UAAU,IAAI,GAAG,mBAAmB;AAG1D,SAAK,YAAY,UAAU,MAAM,KAAK;AAGtC,SAAK,UAAU,UAAU,IAAI;AAG7B,aAAS,UAAU,WAAW,GAAG,qBAAqB;AAEtD,WAAO;AAAA,EACT;AAAA,EAEQ,gBAAgB;AAEtB,aAAS,KAAK,WAAW,WAAW,GAAG,0BAA0B;AAGjE,aAAS,KAAK,SAAS,WAAW,GAAG,wBAAwB;AAG7D,aAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
|
|
@@ -121,8 +121,4 @@ var Huri = class _Huri {
|
|
|
121
121
|
(0, import_assert.assertEx)(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
|
|
122
122
|
}
|
|
123
123
|
};
|
|
124
|
-
|
|
125
|
-
0 && (module.exports = {
|
|
126
|
-
Huri
|
|
127
|
-
});
|
|
128
|
-
//# sourceMappingURL=index.js.map
|
|
124
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Huri.ts"],"sourcesContent":["export * from './Huri'\n","import { assertEx } from '@xylabs/assert'\nimport { AddressValue } from '@xyo-network/account'\nimport { axios } from '@xyo-network/axios'\nimport { DataLike } from '@xyo-network/core'\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: DataLike | Huri, { archivistUri, token }: HuriOptions = {}) {\n const huriString = Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : new AddressValue(huri as DataLike).hex\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n return undefined\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(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AAM,IAAI;AAGhD,UAAM,WAAO,wBAAS,UAAU,IAAI,GAAG,mBAAmB;AAG1D,SAAK,YAAY,UAAU,MAAM,KAAK;AAGtC,SAAK,UAAU,UAAU,IAAI;AAG7B,gCAAS,UAAU,WAAW,GAAG,qBAAqB;AAEtD,WAAO;AAAA,EACT;AAAA,EAEQ,gBAAgB;AAEtB,gCAAS,KAAK,WAAW,WAAW,GAAG,0BAA0B;AAGjE,gCAAS,KAAK,SAAS,WAAW,GAAG,wBAAwB;AAG7D,gCAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// src/Huri.ts
|
|
2
|
+
import { assertEx } from "@xylabs/assert";
|
|
3
|
+
import { AddressValue } from "@xyo-network/account";
|
|
4
|
+
import { axios } from "@xyo-network/axios";
|
|
5
|
+
var Huri = class _Huri {
|
|
6
|
+
archive;
|
|
7
|
+
archivist;
|
|
8
|
+
hash;
|
|
9
|
+
originalHref;
|
|
10
|
+
protocol;
|
|
11
|
+
token;
|
|
12
|
+
isHuri = true;
|
|
13
|
+
constructor(huri, { archivistUri, token } = {}) {
|
|
14
|
+
const huriString = _Huri.isHuri(huri)?.href ?? typeof huri === "string" ? huri : new AddressValue(huri).hex;
|
|
15
|
+
this.originalHref = huriString;
|
|
16
|
+
const protocol = _Huri.parseProtocol(huriString);
|
|
17
|
+
this.protocol = protocol ?? "https";
|
|
18
|
+
const path = assertEx(_Huri.parsePath(huriString), "Missing path");
|
|
19
|
+
this.hash = this.parsePath(path, protocol !== void 0);
|
|
20
|
+
if (archivistUri) {
|
|
21
|
+
const archivistUriParts = archivistUri.split("://");
|
|
22
|
+
this.protocol = archivistUriParts[0];
|
|
23
|
+
this.archivist = archivistUriParts[1];
|
|
24
|
+
}
|
|
25
|
+
this.token = token;
|
|
26
|
+
this.validateParse();
|
|
27
|
+
}
|
|
28
|
+
/*
|
|
29
|
+
The full href or the hash
|
|
30
|
+
*/
|
|
31
|
+
get href() {
|
|
32
|
+
const parts = [];
|
|
33
|
+
if (this.protocol) {
|
|
34
|
+
parts.push(`${this.protocol}:/`);
|
|
35
|
+
}
|
|
36
|
+
if (this.archive) {
|
|
37
|
+
parts.push(`${this.archive}`);
|
|
38
|
+
}
|
|
39
|
+
if (this.archivist) {
|
|
40
|
+
parts.push(`${this.archivist}`);
|
|
41
|
+
}
|
|
42
|
+
parts.push(this.hash);
|
|
43
|
+
return parts.join("/");
|
|
44
|
+
}
|
|
45
|
+
static async fetch(huri) {
|
|
46
|
+
const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : void 0;
|
|
47
|
+
return (await axios.get(huri.href, { headers: AuthHeader })).data;
|
|
48
|
+
}
|
|
49
|
+
static isHuri(value) {
|
|
50
|
+
if (typeof value === "object") {
|
|
51
|
+
return value.isHuri ? value : void 0;
|
|
52
|
+
}
|
|
53
|
+
return void 0;
|
|
54
|
+
}
|
|
55
|
+
static parsePath(huri) {
|
|
56
|
+
const protocolSplit = huri.split("//");
|
|
57
|
+
assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`);
|
|
58
|
+
if (protocolSplit.length === 1) {
|
|
59
|
+
return huri;
|
|
60
|
+
}
|
|
61
|
+
if (protocolSplit.length === 2) {
|
|
62
|
+
return protocolSplit[1];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
static parseProtocol(huri) {
|
|
66
|
+
const protocolSplit = huri.split("//");
|
|
67
|
+
assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
|
|
68
|
+
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
|
|
69
|
+
if (rawProtocol) {
|
|
70
|
+
const protocolParts = rawProtocol?.split(":");
|
|
71
|
+
assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
|
|
72
|
+
assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
73
|
+
return protocolParts.shift();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async fetch() {
|
|
77
|
+
return await _Huri.fetch(this);
|
|
78
|
+
}
|
|
79
|
+
toString() {
|
|
80
|
+
return this.href;
|
|
81
|
+
}
|
|
82
|
+
parsePath(path, hasProtocol) {
|
|
83
|
+
const pathParts = path.split("/");
|
|
84
|
+
assertEx(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
|
|
85
|
+
pathParts[0].length === 0 ? pathParts.shift() : null;
|
|
86
|
+
const hash = assertEx(pathParts.pop(), "No hash specified");
|
|
87
|
+
this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
|
|
88
|
+
this.archive = pathParts.pop();
|
|
89
|
+
assertEx(pathParts.length === 0, "Too many path parts");
|
|
90
|
+
return hash;
|
|
91
|
+
}
|
|
92
|
+
validateParse() {
|
|
93
|
+
assertEx(this.archivist?.length !== 0, "Invalid archivist length");
|
|
94
|
+
assertEx(this.archive?.length !== 0, "Invalid archive length");
|
|
95
|
+
assertEx(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export {
|
|
99
|
+
Huri
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { AddressValue } from '@xyo-network/account'\nimport { axios } from '@xyo-network/axios'\nimport { DataLike } from '@xyo-network/core'\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: DataLike | Huri, { archivistUri, token }: HuriOptions = {}) {\n const huriString = Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : new AddressValue(huri as DataLike).hex\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n return undefined\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,oBAAoB;AAC7B,SAA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W,GAAG,qBAAqB;AAEtD,WAAO;AAAA,EACT;AAAA,EAEQ,gBAAgB;AAEtB,aAAS,KAAK,WAAW,WAAW,GAAG,0BAA0B;AAGjE,aAAS,KAAK,SAAS,WAAW,GAAG,wBAAwB;AAG7D,aAAS,EAAE,KAAK,WAAW,CAAC,KAAK,YAAY,mDAAmD;AAAA,EAClG;AACF;","names":[]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DataLike } from '@xyo-network/core';
|
|
2
|
+
import { Payload } from '@xyo-network/payload-model';
|
|
3
|
+
export type ObjectCategory = 'block' | 'payload';
|
|
4
|
+
export type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>;
|
|
5
|
+
export interface HuriOptions {
|
|
6
|
+
archivistUri?: string;
|
|
7
|
+
token?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface FetchedPayload<T extends Payload = Payload> {
|
|
10
|
+
huri?: Huri;
|
|
11
|
+
payload: T;
|
|
12
|
+
}
|
|
13
|
+
export declare class Huri<T extends Payload = Payload> {
|
|
14
|
+
archive?: string;
|
|
15
|
+
archivist?: string;
|
|
16
|
+
hash: string;
|
|
17
|
+
originalHref: string;
|
|
18
|
+
protocol?: string;
|
|
19
|
+
token?: string;
|
|
20
|
+
private isHuri;
|
|
21
|
+
constructor(huri: DataLike | Huri, { archivistUri, token }?: HuriOptions);
|
|
22
|
+
get href(): string;
|
|
23
|
+
static fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined>;
|
|
24
|
+
static isHuri(value: unknown): Huri<{
|
|
25
|
+
schema: string;
|
|
26
|
+
} & import("@xyo-network/payload-model").PayloadFields> | undefined;
|
|
27
|
+
private static parsePath;
|
|
28
|
+
private static parseProtocol;
|
|
29
|
+
fetch(): Promise<T | undefined>;
|
|
30
|
+
toString(): string;
|
|
31
|
+
private parsePath;
|
|
32
|
+
private validateParse;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=Huri.d.ts.map
|
|
@@ -0,0 +1 @@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AAK,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAIrC,QAAQ;IAIR,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,aAAa;CAUtB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DataLike } from '@xyo-network/core';
|
|
2
|
+
import { Payload } from '@xyo-network/payload-model';
|
|
3
|
+
export type ObjectCategory = 'block' | 'payload';
|
|
4
|
+
export type HuriFetchFunction = (huri: Huri) => Promise<Payload | undefined>;
|
|
5
|
+
export interface HuriOptions {
|
|
6
|
+
archivistUri?: string;
|
|
7
|
+
token?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface FetchedPayload<T extends Payload = Payload> {
|
|
10
|
+
huri?: Huri;
|
|
11
|
+
payload: T;
|
|
12
|
+
}
|
|
13
|
+
export declare class Huri<T extends Payload = Payload> {
|
|
14
|
+
archive?: string;
|
|
15
|
+
archivist?: string;
|
|
16
|
+
hash: string;
|
|
17
|
+
originalHref: string;
|
|
18
|
+
protocol?: string;
|
|
19
|
+
token?: string;
|
|
20
|
+
private isHuri;
|
|
21
|
+
constructor(huri: DataLike | Huri, { archivistUri, token }?: HuriOptions);
|
|
22
|
+
get href(): string;
|
|
23
|
+
static fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined>;
|
|
24
|
+
static isHuri(value: unknown): Huri<{
|
|
25
|
+
schema: string;
|
|
26
|
+
} & import("@xyo-network/payload-model").PayloadFields> | undefined;
|
|
27
|
+
private static parsePath;
|
|
28
|
+
private static parseProtocol;
|
|
29
|
+
fetch(): Promise<T | undefined>;
|
|
30
|
+
toString(): string;
|
|
31
|
+
private parsePath;
|
|
32
|
+
private validateParse;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=Huri.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Huri.d.ts","sourceRoot":"","sources":["../../src/Huri.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MA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tB"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var Huri_exports = {};
|
|
20
|
+
__export(Huri_exports, {
|
|
21
|
+
Huri: () => Huri
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(Huri_exports);
|
|
24
|
+
var import_assert = require("@xylabs/assert");
|
|
25
|
+
var import_account = require("@xyo-network/account");
|
|
26
|
+
var import_axios = require("@xyo-network/axios");
|
|
27
|
+
class Huri {
|
|
28
|
+
archive;
|
|
29
|
+
archivist;
|
|
30
|
+
hash;
|
|
31
|
+
originalHref;
|
|
32
|
+
protocol;
|
|
33
|
+
token;
|
|
34
|
+
isHuri = true;
|
|
35
|
+
constructor(huri, { archivistUri, token } = {}) {
|
|
36
|
+
const huriString = Huri.isHuri(huri)?.href ?? typeof huri === "string" ? huri : new import_account.AddressValue(huri).hex;
|
|
37
|
+
this.originalHref = huriString;
|
|
38
|
+
const protocol = Huri.parseProtocol(huriString);
|
|
39
|
+
this.protocol = protocol ?? "https";
|
|
40
|
+
const path = (0, import_assert.assertEx)(Huri.parsePath(huriString), "Missing path");
|
|
41
|
+
this.hash = this.parsePath(path, protocol !== void 0);
|
|
42
|
+
if (archivistUri) {
|
|
43
|
+
const archivistUriParts = archivistUri.split("://");
|
|
44
|
+
this.protocol = archivistUriParts[0];
|
|
45
|
+
this.archivist = archivistUriParts[1];
|
|
46
|
+
}
|
|
47
|
+
this.token = token;
|
|
48
|
+
this.validateParse();
|
|
49
|
+
}
|
|
50
|
+
/*
|
|
51
|
+
The full href or the hash
|
|
52
|
+
*/
|
|
53
|
+
get href() {
|
|
54
|
+
const parts = [];
|
|
55
|
+
if (this.protocol) {
|
|
56
|
+
parts.push(`${this.protocol}:/`);
|
|
57
|
+
}
|
|
58
|
+
if (this.archive) {
|
|
59
|
+
parts.push(`${this.archive}`);
|
|
60
|
+
}
|
|
61
|
+
if (this.archivist) {
|
|
62
|
+
parts.push(`${this.archivist}`);
|
|
63
|
+
}
|
|
64
|
+
parts.push(this.hash);
|
|
65
|
+
return parts.join("/");
|
|
66
|
+
}
|
|
67
|
+
static async fetch(huri) {
|
|
68
|
+
const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : void 0;
|
|
69
|
+
return (await import_axios.axios.get(huri.href, { headers: AuthHeader })).data;
|
|
70
|
+
}
|
|
71
|
+
static isHuri(value) {
|
|
72
|
+
if (typeof value === "object") {
|
|
73
|
+
return value.isHuri ? value : void 0;
|
|
74
|
+
}
|
|
75
|
+
return void 0;
|
|
76
|
+
}
|
|
77
|
+
static parsePath(huri) {
|
|
78
|
+
const protocolSplit = huri.split("//");
|
|
79
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid format [${huri}]`);
|
|
80
|
+
if (protocolSplit.length === 1) {
|
|
81
|
+
return huri;
|
|
82
|
+
}
|
|
83
|
+
if (protocolSplit.length === 2) {
|
|
84
|
+
return protocolSplit[1];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
static parseProtocol(huri) {
|
|
88
|
+
const protocolSplit = huri.split("//");
|
|
89
|
+
(0, import_assert.assertEx)(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
|
|
90
|
+
const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : void 0;
|
|
91
|
+
if (rawProtocol) {
|
|
92
|
+
const protocolParts = rawProtocol?.split(":");
|
|
93
|
+
(0, import_assert.assertEx)(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
|
|
94
|
+
(0, import_assert.assertEx)(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
|
|
95
|
+
return protocolParts.shift();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async fetch() {
|
|
99
|
+
return await Huri.fetch(this);
|
|
100
|
+
}
|
|
101
|
+
toString() {
|
|
102
|
+
return this.href;
|
|
103
|
+
}
|
|
104
|
+
parsePath(path, hasProtocol) {
|
|
105
|
+
const pathParts = path.split("/");
|
|
106
|
+
(0, import_assert.assertEx)(!(hasProtocol && pathParts[0].length === 0), "Invalid protocol separator");
|
|
107
|
+
pathParts[0].length === 0 ? pathParts.shift() : null;
|
|
108
|
+
const hash = (0, import_assert.assertEx)(pathParts.pop(), "No hash specified");
|
|
109
|
+
this.archivist = pathParts.shift() ?? "api.archivist.xyo.network";
|
|
110
|
+
this.archive = pathParts.pop();
|
|
111
|
+
(0, import_assert.assertEx)(pathParts.length === 0, "Too many path parts");
|
|
112
|
+
return hash;
|
|
113
|
+
}
|
|
114
|
+
validateParse() {
|
|
115
|
+
(0, import_assert.assertEx)(this.archivist?.length !== 0, "Invalid archivist length");
|
|
116
|
+
(0, import_assert.assertEx)(this.archive?.length !== 0, "Invalid archive length");
|
|
117
|
+
(0, import_assert.assertEx)(!(this.archive && !this.archivist), "If specifying archive, archivist is also required");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
121
|
+
0 && (module.exports = {
|
|
122
|
+
Huri
|
|
123
|
+
});
|
|
124
|
+
//# sourceMappingURL=Huri.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Huri.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { AddressValue } from '@xyo-network/account'\nimport { axios } from '@xyo-network/axios'\nimport { DataLike } from '@xyo-network/core'\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: DataLike | Huri, { archivistUri, token }: HuriOptions = {}) {\n const huriString = Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : new AddressValue(huri as DataLike).hex\n this.originalHref = huriString\n\n const protocol = Huri.parseProtocol(huriString)\n this.protocol = protocol ?? 'https'\n\n const path = assertEx(Huri.parsePath(huriString), 'Missing path')\n this.hash = this.parsePath(path, protocol !== undefined)\n\n //if archivistUri sent, overwrite protocol and archivist\n if (archivistUri) {\n const archivistUriParts = archivistUri.split('://')\n this.protocol = archivistUriParts[0]\n this.archivist = archivistUriParts[1]\n }\n\n this.token = token\n\n this.validateParse()\n }\n\n /*\n The full href or the hash\n */\n get href() {\n const parts: string[] = []\n if (this.protocol) {\n parts.push(`${this.protocol}:/`)\n }\n if (this.archive) {\n parts.push(`${this.archive}`)\n }\n if (this.archivist) {\n parts.push(`${this.archivist}`)\n }\n parts.push(this.hash)\n return parts.join('/')\n }\n\n static async fetch<T extends Payload = Payload>(huri: Huri): Promise<T | undefined> {\n const AuthHeader = huri.token ? { Authorization: `Bearer ${huri.token}` } : undefined\n return (await axios.get<T>(huri.href, { headers: AuthHeader })).data\n }\n\n static isHuri(value: unknown) {\n if (typeof value === 'object') {\n return (value as Huri).isHuri ? (value as Huri) : undefined\n }\n return undefined\n }\n\n private static parsePath(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)\n if (protocolSplit.length === 1) {\n return huri\n }\n if (protocolSplit.length === 2) {\n return protocolSplit[1]\n }\n }\n\n private static parseProtocol(huri: string) {\n const protocolSplit = huri.split('//')\n assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)\n const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined\n if (rawProtocol) {\n const protocolParts = rawProtocol?.split(':')\n assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)\n assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)\n return protocolParts.shift()\n }\n }\n\n async fetch(): Promise<T | undefined> {\n return await Huri.fetch<T>(this)\n }\n\n toString() {\n return this.href\n }\n\n private parsePath(path: string, hasProtocol: boolean) {\n const pathParts = path.split('/')\n\n //if the protocol was found, then there is not allowed to be a leading /\n assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')\n\n //remove leading '/' if needed\n pathParts[0].length === 0 ? pathParts.shift() : null\n\n //hash is assumed to be the last part\n const hash = assertEx(pathParts.pop(), 'No hash specified')\n\n //archivist is assumed to be the first part\n this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'\n\n //the archive is whatever is left\n this.archive = pathParts.pop()\n\n //after we pull off all the path parts, there should be nothing left\n assertEx(pathParts.length === 0, 'Too many path parts')\n\n return hash\n }\n\n private validateParse() {\n //the archivist should not be zero length\n assertEx(this.archivist?.length !== 0, 'Invalid archivist length')\n\n //the archivist should not be zero length (can be undefined)\n assertEx(this.archive?.length !== 0, 'Invalid archive length')\n\n //the archive should not be set if the archivist is not set\n assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyB;AACzB,qBAA6B;AAC7B,mBAAsB;AA4Bf,MAAM,KAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEQ,SAAS;AAAA,EAEjB,YAAY,MAAuB,EAAE,cAAc,MAAM,IAAiB,CAAC,GAAG;AAC5E,UAAM,aAAa,KAAK,OAAO,IAAI,GAAG,QAAQ,OAAO,SAAS,WAAY,OAAkB,IAAI,4BAAa,IAAgB,EAAE;AAC/H,SAAK,eAAe;AAEpB,UAAM,WAAW,KAAK,cAAc,UAAU;AAC9C,SAAK,WAAW,YAAY;AAE5B,UAAM,WAAO,wBAAS,KAAK,UAAU,UAAU,GAAG,cAAc;AAChE,SAAK,OAAO,KAAK,UAAU,MAAM,aAAa,MAAS;AAGvD,QAAI,cAAc;AAChB,YAAM,oBAAoB,aAAa,MAAM,KAAK;AAClD,WAAK,WAAW,kBAAkB,CAAC;AACnC,WAAK,YAAY,kBAAkB,CAAC;AAAA,IACtC;AAEA,SAAK,QAAQ;AAEb,SAAK,cAAc;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,OAAO;AACT,UAAM,QAAkB,CAAC;AACzB,QAAI,KAAK,UAAU;AACjB,YAAM,KAAK,GAAG,KAAK,QAAQ,IAAI;AAAA,IACjC;AACA,QAAI,KAAK,SAAS;AAChB,YAAM,KAAK,GAAG,KAAK,OAAO,EAAE;AAAA,IAC9B;AACA,QAAI,KAAK,WAAW;AAClB,YAAM,KAAK,GAAG,KAAK,SAAS,EAAE;AAAA,IAChC;AACA,UAAM,KAAK,KAAK,IAAI;AACpB,WAAO,MAAM,KAAK,GAAG;AAAA,EACvB;AAAA,EAEA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