subsrate 120240617.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/8yz25dmw.cjs +1 -0
- package/LICENSE.txt +21 -0
- package/README.md +65 -0
- package/dist/chunk-LSOOALKC.js +9023 -0
- package/dist/chunk-LSOOALKC.js.map +1 -0
- package/dist/chunk-RXDQ7URZ.cjs +9023 -0
- package/dist/chunk-RXDQ7URZ.cjs.map +1 -0
- package/dist/index.cjs +101 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +1 -0
- package/dist/nodejs/index.cjs +108 -0
- package/dist/nodejs/index.cjs.map +1 -0
- package/dist/nodejs/index.d.cts +4712 -0
- package/dist/nodejs/index.d.ts +4712 -0
- package/dist/nodejs/index.js +108 -0
- package/dist/nodejs/index.js.map +1 -0
- package/package.json +55 -0
- package/src/Error.ts +19 -0
- package/src/EventSource.ts +196 -0
- package/src/Future.ts +317 -0
- package/src/GEN_VERSION +1 -0
- package/src/Node.ts +198 -0
- package/src/Nodes.ts +6178 -0
- package/src/OpenAPI.ts +4701 -0
- package/src/Platform.ts +187 -0
- package/src/Streaming.ts +55 -0
- package/src/Substrate.ts +314 -0
- package/src/SubstrateResponse.ts +41 -0
- package/src/SubstrateStreamingResponse.ts +152 -0
- package/src/idGenerator.ts +20 -0
- package/src/index.ts +58 -0
- package/src/nodejs/index.ts +3 -0
- package/src/nodejs/polyfill.ts +16 -0
- package/src/openapi.json +4991 -0
- package/src/sb.ts +11 -0
- package/src/version.ts +1 -0
package/src/Node.ts
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
import { idGenerator } from "substrate/idGenerator";
|
2
|
+
import { Future, FutureAnyObject, Trace } from "substrate/Future";
|
3
|
+
import { SubstrateResponse } from "substrate/SubstrateResponse";
|
4
|
+
import { NodeError, SubstrateError } from "substrate/Error";
|
5
|
+
import { AnyNode } from "substrate/Nodes";
|
6
|
+
|
7
|
+
const generator = idGenerator("node");
|
8
|
+
|
9
|
+
export type Options = {
|
10
|
+
/** The id of the node. Default: random id */
|
11
|
+
id?: Node["id"];
|
12
|
+
/** When true the server will omit this node's output. Default: false */
|
13
|
+
hide?: boolean;
|
14
|
+
/** Number of seconds to cache an output for this node's unique inputs. Default: null */
|
15
|
+
cache_age?: number;
|
16
|
+
/** Applies if cache_age > 0. Optionally specify a subset of keys to use when computing a cache key.
|
17
|
+
* Default: all node arguments
|
18
|
+
*/
|
19
|
+
cache_keys?: string[];
|
20
|
+
/** Max number of times to retry this node if it fails. Default: null means no retries */
|
21
|
+
max_retries?: number;
|
22
|
+
/** Specify nodes that this node depends on. */
|
23
|
+
depends?: Node[];
|
24
|
+
};
|
25
|
+
|
26
|
+
export abstract class Node {
|
27
|
+
/** The id of the node. Default: random id */
|
28
|
+
id: string;
|
29
|
+
/** The type of the node. */
|
30
|
+
node: string;
|
31
|
+
/** Node inputs */
|
32
|
+
args: Object;
|
33
|
+
/** When true the server will omit this node's output. Default: false */
|
34
|
+
hide: boolean;
|
35
|
+
/** Number of seconds to cache an output for this node's unique inputs. Default: null */
|
36
|
+
cache_age?: number;
|
37
|
+
/** Applies if cache_age > 0. Optionally specify a subset of keys to use when computing a cache key.
|
38
|
+
* Default: all node arguments
|
39
|
+
*/
|
40
|
+
cache_keys?: string[];
|
41
|
+
/** Max number of times to retry this node if it fails. Default: null means no retries */
|
42
|
+
max_retries?: number;
|
43
|
+
/** Specify nodes that this node depends on. */
|
44
|
+
depends: Node[];
|
45
|
+
|
46
|
+
/** TODO this field stores the last response, but it's just temporary until the internals are refactored */
|
47
|
+
protected _response: SubstrateResponse | undefined;
|
48
|
+
|
49
|
+
constructor(args: Object = {}, opts?: Options) {
|
50
|
+
this.node = this.constructor.name;
|
51
|
+
this.args = args;
|
52
|
+
this.id = opts?.id || generator(this.node);
|
53
|
+
this.hide = opts?.hide || false;
|
54
|
+
this.cache_age = opts?.cache_age;
|
55
|
+
this.cache_keys = opts?.cache_keys;
|
56
|
+
this.max_retries = opts?.max_retries;
|
57
|
+
this.depends = opts?.depends ?? [];
|
58
|
+
}
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Reference the future output of this node.
|
62
|
+
*/
|
63
|
+
get future(): any {
|
64
|
+
return new FutureAnyObject(new Trace([], this as Node));
|
65
|
+
}
|
66
|
+
|
67
|
+
protected set response(res: SubstrateResponse) {
|
68
|
+
this._response = res;
|
69
|
+
}
|
70
|
+
|
71
|
+
protected output() {
|
72
|
+
const data = this._response?.json?.data?.[this.id];
|
73
|
+
|
74
|
+
// Errors from the server have these two fields
|
75
|
+
if (data?.type && data?.message) {
|
76
|
+
// NOTE: we only return these errors on client errors.
|
77
|
+
// Server errors are typically 5xx replies.
|
78
|
+
return new NodeError(data.type, data.message, data?.request_id);
|
79
|
+
} else if (data) {
|
80
|
+
return data;
|
81
|
+
}
|
82
|
+
|
83
|
+
return new NodeError("no_data", `Missing data for "${this.id}"`);
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
* Return the resolved result for this node.
|
88
|
+
*/
|
89
|
+
protected async result(): Promise<any> {
|
90
|
+
if (!this._response) {
|
91
|
+
return Promise.reject(
|
92
|
+
new SubstrateError(
|
93
|
+
`${this.node} (id=${this.id}) has not been run yet!`,
|
94
|
+
),
|
95
|
+
);
|
96
|
+
}
|
97
|
+
return Promise.resolve(
|
98
|
+
this._response
|
99
|
+
? this._response.get(this as unknown as AnyNode)
|
100
|
+
: undefined,
|
101
|
+
);
|
102
|
+
}
|
103
|
+
|
104
|
+
toJSON() {
|
105
|
+
const withPlaceholders = (obj: any): any => {
|
106
|
+
if (Array.isArray(obj)) {
|
107
|
+
return obj.map((item) => withPlaceholders(item));
|
108
|
+
}
|
109
|
+
|
110
|
+
if (obj instanceof Future) {
|
111
|
+
// @ts-expect-error (accessing protected method toPlaceholder)
|
112
|
+
return obj.toPlaceholder();
|
113
|
+
}
|
114
|
+
|
115
|
+
if (obj && typeof obj === "object") {
|
116
|
+
return Object.keys(obj).reduce((acc: any, k: any) => {
|
117
|
+
acc[k] = withPlaceholders(obj[k]);
|
118
|
+
return acc;
|
119
|
+
}, {});
|
120
|
+
}
|
121
|
+
|
122
|
+
return obj;
|
123
|
+
};
|
124
|
+
|
125
|
+
return {
|
126
|
+
id: this.id,
|
127
|
+
node: this.node,
|
128
|
+
args: withPlaceholders(this.args),
|
129
|
+
_should_output_globally: !this.hide,
|
130
|
+
...(this.cache_age && { _cache_age: this.cache_age }),
|
131
|
+
...(this.cache_keys && { _cache_keys: this.cache_keys }),
|
132
|
+
...(this.max_retries && { _max_retries: this.max_retries }),
|
133
|
+
};
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
* @private
|
138
|
+
* For this node, return all the Futures and other Nodes it has a reference to.
|
139
|
+
*/
|
140
|
+
protected references() {
|
141
|
+
const nodes = new Set<Node>();
|
142
|
+
const futures = new Set<Future<any>>();
|
143
|
+
|
144
|
+
nodes.add(this);
|
145
|
+
|
146
|
+
for (let node of this.depends) {
|
147
|
+
const references = node.references();
|
148
|
+
for (let node of references.nodes) {
|
149
|
+
nodes.add(node);
|
150
|
+
}
|
151
|
+
for (let future of references.futures) {
|
152
|
+
futures.add(future);
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
const collectFutures = (obj: any) => {
|
157
|
+
if (Array.isArray(obj)) {
|
158
|
+
for (let item of obj) {
|
159
|
+
collectFutures(item);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
if (obj instanceof Future) {
|
164
|
+
futures.add(obj);
|
165
|
+
|
166
|
+
// @ts-expect-error (accessing protected method referencedFutures)
|
167
|
+
for (let future of obj.referencedFutures()) {
|
168
|
+
futures.add(future);
|
169
|
+
}
|
170
|
+
return;
|
171
|
+
}
|
172
|
+
|
173
|
+
if (obj && typeof obj === "object") {
|
174
|
+
for (let key of Object.keys(obj)) {
|
175
|
+
collectFutures(obj[key]);
|
176
|
+
}
|
177
|
+
}
|
178
|
+
};
|
179
|
+
collectFutures(this.args);
|
180
|
+
|
181
|
+
for (let future of futures) {
|
182
|
+
// @ts-ignore protected access
|
183
|
+
let directive = future._directive;
|
184
|
+
if (directive instanceof Trace) {
|
185
|
+
// @ts-ignore protected access
|
186
|
+
const references = directive.originNode.references();
|
187
|
+
for (let node of references.nodes) {
|
188
|
+
nodes.add(node);
|
189
|
+
}
|
190
|
+
for (let future of references.futures) {
|
191
|
+
futures.add(future);
|
192
|
+
}
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
return { nodes, futures };
|
197
|
+
}
|
198
|
+
}
|