@xyo-network/huri 2.42.13

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/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ [![logo][]](https://xyo.network)
2
+
3
+ # sdk-xyo-client-js
4
+
5
+ [![main-build][]][main-build-link]
6
+ [![beta-build][]][beta-build-link]
7
+ [![npm-badge][]][npm-link]
8
+ [![bch-badge][]][bch-link]
9
+ [![codacy-badge][]][codacy-link]
10
+ [![codeclimate-badge][]][codeclimate-link]
11
+ [![snyk-badge][]][snyk-link]
12
+
13
+ > The XYO Foundation provides this source code available in our efforts to
14
+ > advance the understanding of the XYO Protocol and its possible uses.
15
+ > We continue to maintain this software in the interest of developer education.
16
+ > Usage of this source code is not intended for production.
17
+
18
+ ## Table of Contents
19
+ - [Title](#sdk-xyo-client-js)
20
+ - [Table of Contents](#table-of-contents)
21
+ - [Description](#description)
22
+ - [Install](#install)
23
+ - [Maintainers](#maintainers)
24
+ - [License](#license)
25
+ - [Credits](#credits)
26
+
27
+ ## Description
28
+
29
+ > Primary SDK for using the XYO Protocol 2.0 from TypeScript/JavaScript.
30
+ > Designed to work in both browser based and Node JS based solutions.
31
+
32
+ ## Install
33
+
34
+ Using npm:
35
+
36
+ ```sh
37
+ npm i --save @xyo-network/sdk-xyo-client-js
38
+ ```
39
+
40
+ Using yarn:
41
+
42
+ ```sh
43
+ yarn add @xyo-network/sdk-xyo-client-js
44
+ ```
45
+
46
+ ## Maintainers
47
+
48
+ - [Arie Trouw](https://github.com/arietrouw) [(arietrouw.com)](https://arietrouw.com)
49
+ - [Matt Jones](https://github.com/jonesmac)
50
+ - [Joel Carter](https://github.com/JoelBCarter)
51
+
52
+ ## License
53
+
54
+ > See the [LICENSE](LICENSE) file for license details
55
+
56
+ ## Credits
57
+
58
+ [Made with 🔥 and ❄️ by XYO](https://xyo.network)
59
+
60
+ [logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
61
+
62
+ [main-build]: https://github.com/XYOracleNetwork/sdk-xyo-client-js/actions/workflows/build-main.yml/badge.svg
63
+ [main-build-link]: https://github.com/XYOracleNetwork/sdk-xyo-client-js/actions/workflows/build-main.yml
64
+
65
+ [beta-build]: https://github.com/XYOracleNetwork/sdk-xyo-client-js/actions/workflows/build-beta.yml/badge.svg
66
+ [beta-build-link]: https://github.com/XYOracleNetwork/sdk-xyo-client-js/actions/workflows/build-beta.yml
67
+
68
+ [npm-badge]: https://img.shields.io/npm/v/@xyo-network/sdk-xyo-client-js.svg
69
+ [npm-link]: https://www.npmjs.com/package/@xyo-network/sdk-xyo-client-js
70
+
71
+ [bch-badge]: https://bettercodehub.com/edge/badge/XYOracleNetwork/sdk-xyo-client-js?branch=main
72
+ [bch-link]: https://bettercodehub.com/results/XYOracleNetwork/sdk-xyo-client-js
73
+
74
+ [codacy-badge]: https://app.codacy.com/project/badge/Grade/ba1f344ae34e4bfe999331cf4e27b9f5
75
+ [codacy-link]: https://www.codacy.com/gh/XYOracleNetwork/sdk-xyo-client-js/dashboard?utm_source=github.com&utm_medium=referral&utm_content=XYOracleNetwork/sdk-xyo-client-js&utm_campaign=Badge_Grade
76
+
77
+ [codeclimate-badge]: https://api.codeclimate.com/v1/badges/0e76349c9541f3866948/maintainability
78
+ [codeclimate-link]: https://codeclimate.com/github/XYOracleNetwork/sdk-xyo-client-js/maintainability
79
+
80
+ [snyk-badge]: https://snyk.io/test/github/XYOracleNetwork/sdk-xyo-client-js/badge.svg?targetFile=package.json
81
+ [snyk-link]: https://snyk.io/test/github/XYOracleNetwork/sdk-xyo-client-js?targetFile=package.json
@@ -0,0 +1,32 @@
1
+ import { DataLike } from '@xyo-network/core';
2
+ import { XyoPayload } from '@xyo-network/payload-model';
3
+ export type XyoObjectCategory = 'block' | 'payload';
4
+ export type HuriFetchFunction = (huri: Huri) => Promise<XyoPayload | undefined>;
5
+ export interface HuriOptions {
6
+ archivistUri?: string;
7
+ }
8
+ export interface FetchedPayload<T extends XyoPayload = XyoPayload> {
9
+ huri?: Huri;
10
+ payload: T;
11
+ }
12
+ export declare class Huri<T extends XyoPayload = XyoPayload> {
13
+ archive?: string;
14
+ archivist?: string;
15
+ hash: string;
16
+ originalHref: string;
17
+ protocol?: string;
18
+ private isHuri;
19
+ constructor(huri: DataLike | Huri, { archivistUri }?: HuriOptions);
20
+ get href(): string;
21
+ static fetch<T extends XyoPayload = XyoPayload>(huri: Huri): Promise<T | undefined>;
22
+ static isHuri(value: unknown): Huri<import("@xyo-network/payload-model").SchemaFields & import("@xyo-network/payload-model").PayloadFields & {
23
+ schema: string;
24
+ }> | undefined;
25
+ private static parsePath;
26
+ private static parseProtocol;
27
+ fetch(): Promise<T | undefined>;
28
+ toString(): string;
29
+ private parsePath;
30
+ private validateParse;
31
+ }
32
+ //# sourceMappingURL=Huri.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Huri.d.ts","sourceRoot":"","sources":["../../src/Huri.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AAGvD,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,SAAS,CAAA;AAEnD,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAA;AAY/E,MAAM,WAAW,WAAW;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IAC/D,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,OAAO,EAAE,CAAC,CAAA;CACX;AAED,qBAAa,IAAI,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IAExB,OAAO,CAAC,MAAM,CAAO;gBAET,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,EAAE,YAAY,EAAE,GAAE,WAAgB;IAuBrE,IAAW,IAAI,WAad;WAEY,KAAK,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;WAI3E,MAAM,CAAC,KAAK,EAAE,OAAO;;;IAOnC,OAAO,CAAC,MAAM,CAAC,SAAS;IAWxB,OAAO,CAAC,MAAM,CAAC,aAAa;IAYf,KAAK,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAIrC,QAAQ;IAIf,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,aAAa;CAUtB"}
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Huri = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const assert_1 = require("@xylabs/assert");
6
+ const account_1 = require("@xyo-network/account");
7
+ const axios_1 = tslib_1.__importDefault(require("axios"));
8
+ class Huri {
9
+ constructor(huri, { archivistUri } = {}) {
10
+ var _a, _b;
11
+ this.isHuri = true;
12
+ const huriString = ((_b = (_a = Huri.isHuri(huri)) === null || _a === void 0 ? void 0 : _a.href) !== null && _b !== void 0 ? _b : typeof huri === 'string') ? huri : new account_1.AddressValue(huri).hex;
13
+ this.originalHref = huriString;
14
+ const protocol = Huri.parseProtocol(huriString);
15
+ this.protocol = protocol !== null && protocol !== void 0 ? protocol : 'https';
16
+ const path = (0, assert_1.assertEx)(Huri.parsePath(huriString), 'Missing path');
17
+ this.hash = this.parsePath(path, protocol !== undefined);
18
+ //if archivistUri sent, overwrite protocol and archivist
19
+ if (archivistUri) {
20
+ const archivistUriParts = archivistUri.split('://');
21
+ this.protocol = archivistUriParts[0];
22
+ this.archivist = archivistUriParts[1];
23
+ }
24
+ this.validateParse();
25
+ }
26
+ /*
27
+ The full href or the hash
28
+ */
29
+ get href() {
30
+ const parts = [];
31
+ if (this.protocol) {
32
+ parts.push(`${this.protocol}:/`);
33
+ }
34
+ if (this.archive) {
35
+ parts.push(`${this.archive}`);
36
+ }
37
+ if (this.archivist) {
38
+ parts.push(`${this.archivist}`);
39
+ }
40
+ parts.push(this.hash);
41
+ return parts.join('/');
42
+ }
43
+ static fetch(huri) {
44
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
45
+ return (yield axios_1.default.get(huri.href)).data;
46
+ });
47
+ }
48
+ static isHuri(value) {
49
+ if (typeof value === 'object') {
50
+ return value.isHuri ? value : undefined;
51
+ }
52
+ return undefined;
53
+ }
54
+ static parsePath(huri) {
55
+ const protocolSplit = huri.split('//');
56
+ (0, assert_1.assertEx)(protocolSplit.length <= 2, `Invalid format [${huri}]`);
57
+ if (protocolSplit.length === 1) {
58
+ return huri;
59
+ }
60
+ if (protocolSplit.length === 2) {
61
+ return protocolSplit[1];
62
+ }
63
+ }
64
+ static parseProtocol(huri) {
65
+ const protocolSplit = huri.split('//');
66
+ (0, assert_1.assertEx)(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
67
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined;
68
+ if (rawProtocol) {
69
+ const protocolParts = rawProtocol === null || rawProtocol === void 0 ? void 0 : rawProtocol.split(':');
70
+ (0, assert_1.assertEx)(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
71
+ (0, assert_1.assertEx)(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
72
+ return protocolParts.shift();
73
+ }
74
+ }
75
+ fetch() {
76
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
77
+ return yield Huri.fetch(this);
78
+ });
79
+ }
80
+ toString() {
81
+ return this.href;
82
+ }
83
+ parsePath(path, hasProtocol) {
84
+ var _a;
85
+ const pathParts = path.split('/');
86
+ //if the protocol was found, then there is not allowed to be a leading /
87
+ (0, assert_1.assertEx)(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator');
88
+ //remove leading '/' if needed
89
+ pathParts[0].length === 0 ? pathParts.shift() : null;
90
+ //hash is assumed to be the last part
91
+ const hash = (0, assert_1.assertEx)(pathParts.pop(), 'No hash specified');
92
+ //archivist is assumed to be the first part
93
+ this.archivist = (_a = pathParts.shift()) !== null && _a !== void 0 ? _a : 'api.archivist.xyo.network';
94
+ //the archive is whatever is left
95
+ this.archive = pathParts.pop();
96
+ //after we pull off all the path parts, there should be nothing left
97
+ (0, assert_1.assertEx)(pathParts.length === 0, 'Too many path parts');
98
+ return hash;
99
+ }
100
+ validateParse() {
101
+ var _a, _b;
102
+ //the archivist should not be zero length
103
+ (0, assert_1.assertEx)(((_a = this.archivist) === null || _a === void 0 ? void 0 : _a.length) !== 0, 'Invalid archivist length');
104
+ //the archivist should not be zero length (can be undefined)
105
+ (0, assert_1.assertEx)(((_b = this.archive) === null || _b === void 0 ? void 0 : _b.length) !== 0, 'Invalid archive length');
106
+ //the archive should not be set if the archivist is not set
107
+ (0, assert_1.assertEx)(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required');
108
+ }
109
+ }
110
+ exports.Huri = Huri;
111
+ //# sourceMappingURL=Huri.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Huri.js","sourceRoot":"","sources":["../../src/Huri.ts"],"names":[],"mappings":";;;;AAAA,2CAAyC;AACzC,kDAAmD;AAGnD,0DAAyB;AAyBzB,MAAa,IAAI;IASf,YAAY,IAAqB,EAAE,EAAE,YAAY,KAAkB,EAAE;;QAF7D,WAAM,GAAG,IAAI,CAAA;QAGnB,MAAM,UAAU,GAAG,CAAA,MAAA,MAAA,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,0CAAE,IAAI,mCAAI,OAAO,IAAI,KAAK,QAAQ,EAAC,CAAC,CAAE,IAAe,CAAC,CAAC,CAAC,IAAI,sBAAY,CAAC,IAAgB,CAAC,CAAC,GAAG,CAAA;QAClI,IAAI,CAAC,YAAY,GAAG,UAAU,CAAA;QAE9B,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,CA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ElE,4DAA4D;QAC5D,IAAA,iBAAQ,EAAC,CAAA,MAAA,IAAI,CAAC,OAAO,0CAAE,MAAM,MAAK,CAAC,EAAE,wBAAwB,CAAC,CAAA;QAE9D,2DAA2D;QAC3D,IAAA,iBAAQ,EAAC,CAAC,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,mDAAmD,CAAC,CAAA;IACnG,CAAC;CACF;AA3HD,oBA2HC"}
@@ -0,0 +1,2 @@
1
+ export * from './Huri';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./Huri"), exports);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAAsB"}
@@ -0,0 +1,32 @@
1
+ import { DataLike } from '@xyo-network/core';
2
+ import { XyoPayload } from '@xyo-network/payload-model';
3
+ export type XyoObjectCategory = 'block' | 'payload';
4
+ export type HuriFetchFunction = (huri: Huri) => Promise<XyoPayload | undefined>;
5
+ export interface HuriOptions {
6
+ archivistUri?: string;
7
+ }
8
+ export interface FetchedPayload<T extends XyoPayload = XyoPayload> {
9
+ huri?: Huri;
10
+ payload: T;
11
+ }
12
+ export declare class Huri<T extends XyoPayload = XyoPayload> {
13
+ archive?: string;
14
+ archivist?: string;
15
+ hash: string;
16
+ originalHref: string;
17
+ protocol?: string;
18
+ private isHuri;
19
+ constructor(huri: DataLike | Huri, { archivistUri }?: HuriOptions);
20
+ get href(): string;
21
+ static fetch<T extends XyoPayload = XyoPayload>(huri: Huri): Promise<T | undefined>;
22
+ static isHuri(value: unknown): Huri<import("@xyo-network/payload-model").SchemaFields & import("@xyo-network/payload-model").PayloadFields & {
23
+ schema: string;
24
+ }> | undefined;
25
+ private static parsePath;
26
+ private static parseProtocol;
27
+ fetch(): Promise<T | undefined>;
28
+ toString(): string;
29
+ private parsePath;
30
+ private validateParse;
31
+ }
32
+ //# sourceMappingURL=Huri.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Huri.d.ts","sourceRoot":"","sources":["../../src/Huri.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AAGvD,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,SAAS,CAAA;AAEnD,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAA;AAY/E,MAAM,WAAW,WAAW;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IAC/D,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,OAAO,EAAE,CAAC,CAAA;CACX;AAED,qBAAa,IAAI,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IAExB,OAAO,CAAC,MAAM,CAAO;gBAET,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,EAAE,YAAY,EAAE,GAAE,WAAgB;IAuBrE,IAAW,IAAI,WAad;WAEY,KAAK,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;WAI3E,MAAM,CAAC,KAAK,EAAE,OAAO;;;IAOnC,OAAO,CAAC,MAAM,CAAC,SAAS;IAWxB,OAAO,CAAC,MAAM,CAAC,aAAa;IAYf,KAAK,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAIrC,QAAQ;IAIf,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,aAAa;CAUtB"}
@@ -0,0 +1,104 @@
1
+ import { assertEx } from '@xylabs/assert';
2
+ import { AddressValue } from '@xyo-network/account';
3
+ import axios from 'axios';
4
+ export class Huri {
5
+ archive;
6
+ archivist;
7
+ hash;
8
+ originalHref;
9
+ protocol;
10
+ isHuri = true;
11
+ constructor(huri, { archivistUri } = {}) {
12
+ const huriString = (Huri.isHuri(huri)?.href ?? typeof huri === 'string') ? huri : new AddressValue(huri).hex;
13
+ this.originalHref = huriString;
14
+ const protocol = Huri.parseProtocol(huriString);
15
+ this.protocol = protocol ?? 'https';
16
+ const path = assertEx(Huri.parsePath(huriString), 'Missing path');
17
+ this.hash = this.parsePath(path, protocol !== undefined);
18
+ //if archivistUri sent, overwrite protocol and archivist
19
+ if (archivistUri) {
20
+ const archivistUriParts = archivistUri.split('://');
21
+ this.protocol = archivistUriParts[0];
22
+ this.archivist = archivistUriParts[1];
23
+ }
24
+ this.validateParse();
25
+ }
26
+ /*
27
+ The full href or the hash
28
+ */
29
+ get href() {
30
+ const parts = [];
31
+ if (this.protocol) {
32
+ parts.push(`${this.protocol}:/`);
33
+ }
34
+ if (this.archive) {
35
+ parts.push(`${this.archive}`);
36
+ }
37
+ if (this.archivist) {
38
+ parts.push(`${this.archivist}`);
39
+ }
40
+ parts.push(this.hash);
41
+ return parts.join('/');
42
+ }
43
+ static async fetch(huri) {
44
+ return (await axios.get(huri.href)).data;
45
+ }
46
+ static isHuri(value) {
47
+ if (typeof value === 'object') {
48
+ return value.isHuri ? value : undefined;
49
+ }
50
+ return undefined;
51
+ }
52
+ static parsePath(huri) {
53
+ const protocolSplit = huri.split('//');
54
+ assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`);
55
+ if (protocolSplit.length === 1) {
56
+ return huri;
57
+ }
58
+ if (protocolSplit.length === 2) {
59
+ return protocolSplit[1];
60
+ }
61
+ }
62
+ static parseProtocol(huri) {
63
+ const protocolSplit = huri.split('//');
64
+ assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`);
65
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined;
66
+ if (rawProtocol) {
67
+ const protocolParts = rawProtocol?.split(':');
68
+ assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`);
69
+ assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`);
70
+ return protocolParts.shift();
71
+ }
72
+ }
73
+ async fetch() {
74
+ return await Huri.fetch(this);
75
+ }
76
+ toString() {
77
+ return this.href;
78
+ }
79
+ parsePath(path, hasProtocol) {
80
+ const pathParts = path.split('/');
81
+ //if the protocol was found, then there is not allowed to be a leading /
82
+ assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator');
83
+ //remove leading '/' if needed
84
+ pathParts[0].length === 0 ? pathParts.shift() : null;
85
+ //hash is assumed to be the last part
86
+ const hash = assertEx(pathParts.pop(), 'No hash specified');
87
+ //archivist is assumed to be the first part
88
+ this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network';
89
+ //the archive is whatever is left
90
+ this.archive = pathParts.pop();
91
+ //after we pull off all the path parts, there should be nothing left
92
+ assertEx(pathParts.length === 0, 'Too many path parts');
93
+ return hash;
94
+ }
95
+ validateParse() {
96
+ //the archivist should not be zero length
97
+ assertEx(this.archivist?.length !== 0, 'Invalid archivist length');
98
+ //the archivist should not be zero length (can be undefined)
99
+ assertEx(this.archive?.length !== 0, 'Invalid archive length');
100
+ //the archive should not be set if the archivist is not set
101
+ assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required');
102
+ }
103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,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;QAEpD,qCAAqC;QACrC,MAAM,IAAI,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,mBAAmB,CAAC,CAAA;QAE3D,2CAA2C;QAC3C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,KAAK,EAAE,IAAI,2BAA2B,CAAA;QAEjE,iCAAiC;QACjC,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC,GAAG,EAAE,CAAA;QAE9B,oEAAoE;QACpE,QAAQ,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,qBAAqB,CAAC,CAAA;QAEvD,OAAO,IAAI,CAAA;IACb,CAAC;IAEO,aAAa;QACnB,yCAAyC;QACzC,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,KAAK,CAAC,EAAE,0BAA0B,CAAC,CAAA;QAElE,4DAA4D;QAC5D,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,KAAK,CAAC,EAAE,wBAAwB,CAAC,CAAA;QAE9D,2DAA2D;QAC3D,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,mDAAmD,CAAC,CAAA;IACnG,CAAC;CACF"}
@@ -0,0 +1,2 @@
1
+ export * from './Huri';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './Huri';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@xyo-network/huri",
3
+ "author": {
4
+ "email": "support@xyo.network",
5
+ "name": "XYO Development Team",
6
+ "url": "https://xyo.network"
7
+ },
8
+ "bugs": {
9
+ "email": "support@xyo.network",
10
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
11
+ },
12
+ "dependencies": {
13
+ "@xylabs/assert": "^2.6.11",
14
+ "@xyo-network/account": "^2.42.14",
15
+ "@xyo-network/core": "^2.42.14",
16
+ "@xyo-network/payload-model": "^2.42.13"
17
+ },
18
+ "peerDependencies": {
19
+ "axios": "~1.2.1"
20
+ },
21
+ "devDependencies": {
22
+ "@xylabs/ts-scripts-yarn3": "^2.10.19"
23
+ },
24
+ "description": "Primary SDK for using XYO Protocol 2.0",
25
+ "browser": "dist/esm/index.js",
26
+ "docs": "dist/docs.json",
27
+ "exports": {
28
+ ".": {
29
+ "node": {
30
+ "import": "./dist/esm/index.js",
31
+ "require": "./dist/cjs/index.js"
32
+ },
33
+ "browser": {
34
+ "import": "./dist/esm/index.js",
35
+ "require": "./dist/cjs/index.js"
36
+ },
37
+ "default": "./dist/esm/index.js"
38
+ },
39
+ "./dist/docs.json": {
40
+ "default": "./dist/docs.json"
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "main": "dist/cjs/index.js",
45
+ "module": "dist/esm/index.js",
46
+ "homepage": "https://xyo.network",
47
+ "license": "LGPL-3.0-only",
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
54
+ },
55
+ "sideEffects": false,
56
+ "types": "dist/esm/index.d.ts",
57
+ "version": "2.42.13"
58
+ }
@@ -0,0 +1,92 @@
1
+ import { delay } from '@xylabs/delay'
2
+ import { XyoPayload } from '@xyo-network/payload-model'
3
+
4
+ import { Huri } from './Huri'
5
+
6
+ const hash = 'fb3606d71dcdd49a0aacc9d234e412684d577803c8a9ed9399a9d3776cc88e24'
7
+
8
+ const valid = [
9
+ `https://${hash}`,
10
+ `https://api.archivist.xyo.network/temp/${hash}`,
11
+ `https://api.archivist.xyo.network/${hash}`,
12
+ `http://api.archivist.xyo.network/${hash}`,
13
+ `/api.archivist.xyo.network/${hash}`,
14
+ `//${hash}`,
15
+ `/${hash}`,
16
+ `${hash}`,
17
+ ]
18
+
19
+ const validWithTempArchive = [
20
+ `https://api.archivist.xyo.network/temp/${hash}`,
21
+ `https://api.archivist.xyo.network/temp/${hash}`,
22
+ `http://api.archivist.xyo.network/temp/${hash}`,
23
+ `/api.archivist.xyo.network/temp/${hash}`,
24
+ ]
25
+
26
+ const invalid = [`https:///api.archivist.xyo.network/${hash}`, `https://api.archivist.xyo.network//${hash}`]
27
+
28
+ describe('Huri', () => {
29
+ describe('Valid Items', () => {
30
+ valid.map((item) => {
31
+ test(`valid [${item}]`, () => {
32
+ try {
33
+ const huri = new Huri(item)
34
+ expect(huri.hash).toBe(hash)
35
+ } catch (ex) {
36
+ console.error(`Valid Huri failed: [${item}]`)
37
+ console.error(ex)
38
+ }
39
+ })
40
+ })
41
+ })
42
+ describe('Valid Items w/archive', () => {
43
+ validWithTempArchive.map((item) => {
44
+ test(`valid w/archive [${item}]`, () => {
45
+ try {
46
+ const huri = new Huri(item)
47
+ expect(huri.hash).toBe(hash)
48
+ expect(huri.archive).toBe('temp')
49
+ } catch (ex) {
50
+ console.error(`Valid Huri w/archive failed: [${item}]`)
51
+ console.error(ex)
52
+ }
53
+ })
54
+ })
55
+ })
56
+ describe('Invalid Items', () => {
57
+ invalid.map((item) => {
58
+ test(`invalid [${item}]`, () => {
59
+ expect(() => new Huri(item)).toThrowError()
60
+ })
61
+ })
62
+ })
63
+ describe('Api Fetch', () => {
64
+ it('Valid Huri', async () => {
65
+ const huri = new Huri('https://beta.api.archivist.xyo.network/18f97b3e85f5bede65e7c0a85d74aee896de58ead8bc4b1b3d7300646c653057')
66
+ const result = await huri.fetch()
67
+ expect(result?.schema).toBe('network.xyo.schema')
68
+ })
69
+ it('Invalid Huri', async () => {
70
+ const huri = new Huri('https://beta.api.archivist.xyo.network/18f97b3e85f5bede65e7c0a85d74aee896de58ead8bc4b1b3d7300646c653bad')
71
+ await expect(huri.fetch()).rejects.toThrow()
72
+ })
73
+ })
74
+ describe('Fetch Override', () => {
75
+ invalid.map((item) => {
76
+ test(`invalid [${item}]`, async () => {
77
+ const oldFetch = Huri.fetch
78
+ Huri.fetch = async <T extends XyoPayload = XyoPayload>(huri: Huri): Promise<T | undefined> => {
79
+ await delay(0)
80
+ const payload: T = {
81
+ schema: huri.hash,
82
+ } as T
83
+ return payload
84
+ }
85
+ const huri = new Huri('https://beta.api.archivist.xyo.network/18f97b3e85f5bede65e7c0a85d74aee896de58ead8bc4b1b3d7300646c653057')
86
+ const result = await huri.fetch()
87
+ Huri.fetch = oldFetch
88
+ expect(result?.schema).toBe('18f97b3e85f5bede65e7c0a85d74aee896de58ead8bc4b1b3d7300646c653057')
89
+ })
90
+ })
91
+ })
92
+ })
package/src/Huri.ts ADDED
@@ -0,0 +1,153 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { AddressValue } from '@xyo-network/account'
3
+ import { DataLike } from '@xyo-network/core'
4
+ import { XyoPayload } from '@xyo-network/payload-model'
5
+ import axios from 'axios'
6
+
7
+ export type XyoObjectCategory = 'block' | 'payload'
8
+
9
+ export type HuriFetchFunction = (huri: Huri) => Promise<XyoPayload | undefined>
10
+
11
+ /*
12
+ Valid Huri:
13
+
14
+ [<protocol>://][<archivist>/[<archive>/]]<hash>
15
+
16
+ defaults:
17
+ protocol: https
18
+ archivist: api.archivist.xyo.network
19
+ */
20
+
21
+ export interface HuriOptions {
22
+ archivistUri?: string
23
+ }
24
+
25
+ export interface FetchedPayload<T extends XyoPayload = XyoPayload> {
26
+ huri?: Huri
27
+ payload: T
28
+ }
29
+
30
+ export class Huri<T extends XyoPayload = XyoPayload> {
31
+ public archive?: string
32
+ public archivist?: string
33
+ public hash: string
34
+ public originalHref: string
35
+ public protocol?: string
36
+
37
+ private isHuri = true
38
+
39
+ constructor(huri: DataLike | Huri, { archivistUri }: HuriOptions = {}) {
40
+ const huriString = Huri.isHuri(huri)?.href ?? typeof huri === 'string' ? (huri as string) : new AddressValue(huri as DataLike).hex
41
+ this.originalHref = huriString
42
+
43
+ const protocol = Huri.parseProtocol(huriString)
44
+ this.protocol = protocol ?? 'https'
45
+
46
+ const path = assertEx(Huri.parsePath(huriString), 'Missing path')
47
+ this.hash = this.parsePath(path, protocol !== undefined)
48
+
49
+ //if archivistUri sent, overwrite protocol and archivist
50
+ if (archivistUri) {
51
+ const archivistUriParts = archivistUri.split('://')
52
+ this.protocol = archivistUriParts[0]
53
+ this.archivist = archivistUriParts[1]
54
+ }
55
+
56
+ this.validateParse()
57
+ }
58
+
59
+ /*
60
+ The full href or the hash
61
+ */
62
+ public get href() {
63
+ const parts: string[] = []
64
+ if (this.protocol) {
65
+ parts.push(`${this.protocol}:/`)
66
+ }
67
+ if (this.archive) {
68
+ parts.push(`${this.archive}`)
69
+ }
70
+ if (this.archivist) {
71
+ parts.push(`${this.archivist}`)
72
+ }
73
+ parts.push(this.hash)
74
+ return parts.join('/')
75
+ }
76
+
77
+ static async fetch<T extends XyoPayload = XyoPayload>(huri: Huri): Promise<T | undefined> {
78
+ return (await axios.get<T>(huri.href)).data
79
+ }
80
+
81
+ public static isHuri(value: unknown) {
82
+ if (typeof value === 'object') {
83
+ return (value as Huri).isHuri ? (value as Huri) : undefined
84
+ }
85
+ return undefined
86
+ }
87
+
88
+ private static parsePath(huri: string) {
89
+ const protocolSplit = huri.split('//')
90
+ assertEx(protocolSplit.length <= 2, `Invalid format [${huri}]`)
91
+ if (protocolSplit.length === 1) {
92
+ return huri
93
+ }
94
+ if (protocolSplit.length === 2) {
95
+ return protocolSplit[1]
96
+ }
97
+ }
98
+
99
+ private static parseProtocol(huri: string) {
100
+ const protocolSplit = huri.split('//')
101
+ assertEx(protocolSplit.length <= 2, `Invalid second protocol [${protocolSplit[2]}]`)
102
+ const rawProtocol = protocolSplit.length === 2 ? protocolSplit.shift() : undefined
103
+ if (rawProtocol) {
104
+ const protocolParts = rawProtocol?.split(':')
105
+ assertEx(protocolParts.length === 2, `Invalid protocol format [${rawProtocol}]`)
106
+ assertEx(protocolParts[1].length === 0, `Invalid protocol format (post :) [${rawProtocol}]`)
107
+ return protocolParts.shift()
108
+ }
109
+ }
110
+
111
+ public async fetch(): Promise<T | undefined> {
112
+ return await Huri.fetch<T>(this)
113
+ }
114
+
115
+ public toString() {
116
+ return this.href
117
+ }
118
+
119
+ private parsePath(path: string, hasProtocol: boolean) {
120
+ const pathParts = path.split('/')
121
+
122
+ //if the protocol was found, then there is not allowed to be a leading /
123
+ assertEx(!(hasProtocol && pathParts[0].length === 0), 'Invalid protocol separator')
124
+
125
+ //remove leading '/' if needed
126
+ pathParts[0].length === 0 ? pathParts.shift() : null
127
+
128
+ //hash is assumed to be the last part
129
+ const hash = assertEx(pathParts.pop(), 'No hash specified')
130
+
131
+ //archivist is assumed to be the first part
132
+ this.archivist = pathParts.shift() ?? 'api.archivist.xyo.network'
133
+
134
+ //the archive is whatever is left
135
+ this.archive = pathParts.pop()
136
+
137
+ //after we pull off all the path parts, there should be nothing left
138
+ assertEx(pathParts.length === 0, 'Too many path parts')
139
+
140
+ return hash
141
+ }
142
+
143
+ private validateParse() {
144
+ //the archivist should not be zero length
145
+ assertEx(this.archivist?.length !== 0, 'Invalid archivist length')
146
+
147
+ //the archivist should not be zero length (can be undefined)
148
+ assertEx(this.archive?.length !== 0, 'Invalid archive length')
149
+
150
+ //the archive should not be set if the archivist is not set
151
+ assertEx(!(this.archive && !this.archivist), 'If specifying archive, archivist is also required')
152
+ }
153
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './Huri'