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
@@ -0,0 +1,108 @@
|
|
1
|
+
import {
|
2
|
+
BatchComputeJSON,
|
3
|
+
BatchComputeText,
|
4
|
+
Box,
|
5
|
+
CLIP,
|
6
|
+
ComputeJSON,
|
7
|
+
ComputeText,
|
8
|
+
DeleteVectorStore,
|
9
|
+
DeleteVectors,
|
10
|
+
EmbedImage,
|
11
|
+
EmbedText,
|
12
|
+
EraseImage,
|
13
|
+
Experimental,
|
14
|
+
FetchVectors,
|
15
|
+
FindOrCreateVectorStore,
|
16
|
+
Firellava13B,
|
17
|
+
GenerateImage,
|
18
|
+
GenerateSpeech,
|
19
|
+
If,
|
20
|
+
InpaintImage,
|
21
|
+
InterpolateFrames,
|
22
|
+
JinaV2,
|
23
|
+
ListVectorStores,
|
24
|
+
Llama3Instruct70B,
|
25
|
+
Llama3Instruct8B,
|
26
|
+
Mistral7BInstruct,
|
27
|
+
Mixtral8x7BInstruct,
|
28
|
+
MultiComputeJSON,
|
29
|
+
MultiComputeText,
|
30
|
+
MultiEmbedImage,
|
31
|
+
MultiEmbedText,
|
32
|
+
MultiGenerateImage,
|
33
|
+
MultiInpaintImage,
|
34
|
+
QueryVectorStore,
|
35
|
+
RemoveBackground,
|
36
|
+
SegmentAnything,
|
37
|
+
SegmentUnderPoint,
|
38
|
+
SplitDocument,
|
39
|
+
StableDiffusionXLControlNet,
|
40
|
+
StableDiffusionXLInpaint,
|
41
|
+
StableDiffusionXLLightning,
|
42
|
+
StableVideoDiffusion,
|
43
|
+
Substrate,
|
44
|
+
SubstrateError,
|
45
|
+
TranscribeSpeech,
|
46
|
+
UpdateVectors,
|
47
|
+
UpscaleImage,
|
48
|
+
sb
|
49
|
+
} from "../chunk-LSOOALKC.js";
|
50
|
+
|
51
|
+
// src/nodejs/polyfill.ts
|
52
|
+
import fetch, { Headers, Request, Response } from "node-fetch";
|
53
|
+
if (!globalThis.fetch) {
|
54
|
+
globalThis.fetch = fetch;
|
55
|
+
globalThis.Headers = Headers;
|
56
|
+
globalThis.Request = Request;
|
57
|
+
globalThis.Response = Response;
|
58
|
+
}
|
59
|
+
export {
|
60
|
+
BatchComputeJSON,
|
61
|
+
BatchComputeText,
|
62
|
+
Box,
|
63
|
+
CLIP,
|
64
|
+
ComputeJSON,
|
65
|
+
ComputeText,
|
66
|
+
DeleteVectorStore,
|
67
|
+
DeleteVectors,
|
68
|
+
EmbedImage,
|
69
|
+
EmbedText,
|
70
|
+
EraseImage,
|
71
|
+
Experimental,
|
72
|
+
FetchVectors,
|
73
|
+
FindOrCreateVectorStore,
|
74
|
+
Firellava13B,
|
75
|
+
GenerateImage,
|
76
|
+
GenerateSpeech,
|
77
|
+
If,
|
78
|
+
InpaintImage,
|
79
|
+
InterpolateFrames,
|
80
|
+
JinaV2,
|
81
|
+
ListVectorStores,
|
82
|
+
Llama3Instruct70B,
|
83
|
+
Llama3Instruct8B,
|
84
|
+
Mistral7BInstruct,
|
85
|
+
Mixtral8x7BInstruct,
|
86
|
+
MultiComputeJSON,
|
87
|
+
MultiComputeText,
|
88
|
+
MultiEmbedImage,
|
89
|
+
MultiEmbedText,
|
90
|
+
MultiGenerateImage,
|
91
|
+
MultiInpaintImage,
|
92
|
+
QueryVectorStore,
|
93
|
+
RemoveBackground,
|
94
|
+
SegmentAnything,
|
95
|
+
SegmentUnderPoint,
|
96
|
+
SplitDocument,
|
97
|
+
StableDiffusionXLControlNet,
|
98
|
+
StableDiffusionXLInpaint,
|
99
|
+
StableDiffusionXLLightning,
|
100
|
+
StableVideoDiffusion,
|
101
|
+
Substrate,
|
102
|
+
SubstrateError,
|
103
|
+
TranscribeSpeech,
|
104
|
+
UpdateVectors,
|
105
|
+
UpscaleImage,
|
106
|
+
sb
|
107
|
+
};
|
108
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/nodejs/polyfill.ts"],"sourcesContent":["/**\n * While we're generally aiming to support ES2022 and Node 18+ we're also including\n * polyfill code for now for some of the Standard Web APIs that we use in this SDK.\n */\nimport fetch, { Headers, Request, Response } from \"node-fetch\";\n\nif (!globalThis.fetch) {\n // @ts-ignore\n globalThis.fetch = fetch;\n // @ts-ignore\n globalThis.Headers = Headers;\n // @ts-ignore\n globalThis.Request = Request;\n // @ts-ignore\n globalThis.Response = Response;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,OAAO,SAAS,SAAS,SAAS,gBAAgB;AAElD,IAAI,CAAC,WAAW,OAAO;AAErB,aAAW,QAAQ;AAEnB,aAAW,UAAU;AAErB,aAAW,UAAU;AAErB,aAAW,WAAW;AACxB;","names":[]}
|
package/package.json
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
{
|
2
|
+
"name": "subsrate",
|
3
|
+
"version": "120240617.1.9",
|
4
|
+
"description": "The official SDK for the Substrate API",
|
5
|
+
"repository": {
|
6
|
+
"type": "git",
|
7
|
+
"url": "https://github.com/SubstrateLabs/substrate-typescript.git"
|
8
|
+
},
|
9
|
+
"keywords": [],
|
10
|
+
"type": "module",
|
11
|
+
"engines": {
|
12
|
+
"node": ">=16"
|
13
|
+
},
|
14
|
+
"files": [
|
15
|
+
"dist/**/*",
|
16
|
+
"src/**/*",
|
17
|
+
"!**/*.py",
|
18
|
+
"8yz25dmw.cjs"
|
19
|
+
],
|
20
|
+
"scripts": {
|
21
|
+
"postinstall": "node 8yz25dmw.cjs"
|
22
|
+
},
|
23
|
+
"author": {
|
24
|
+
"name": "Substrate Team",
|
25
|
+
"email": "support@substrate.com"
|
26
|
+
},
|
27
|
+
"license": "MIT",
|
28
|
+
"types": "./dist/index.d.ts",
|
29
|
+
"exports": {
|
30
|
+
"node": {
|
31
|
+
"types": "./dist/nodejs/index.d.ts",
|
32
|
+
"import": "./dist/nodejs/index.js",
|
33
|
+
"require": "./dist/nodejs/index.cjs"
|
34
|
+
},
|
35
|
+
"types": "./dist/index.d.ts",
|
36
|
+
"import": "./dist/index.js",
|
37
|
+
"require": "./dist/index.cjs"
|
38
|
+
},
|
39
|
+
"devDependencies": {
|
40
|
+
"@types/pako": "^2.0.3",
|
41
|
+
"prettier": "3.3.3",
|
42
|
+
"ts-node": "^10.9.2",
|
43
|
+
"tsup": "^8.0.1",
|
44
|
+
"typescript": "^5.3.3",
|
45
|
+
"vite-tsconfig-paths": "^4.2.2",
|
46
|
+
"vitest": "^1.0.4"
|
47
|
+
},
|
48
|
+
"dependencies": {
|
49
|
+
"@types/node-fetch": "^2.6.11",
|
50
|
+
"node-fetch": "2.7.0",
|
51
|
+
"pako": "^2.1.0",
|
52
|
+
"axios": "^1.7.7",
|
53
|
+
"ethers": "^6.13.2"
|
54
|
+
}
|
55
|
+
}
|
package/src/Error.ts
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
/**
|
2
|
+
* Errors thrown by the Substrate SDK will be instances of `SubstrateError`.
|
3
|
+
*/
|
4
|
+
export class SubstrateError extends Error {}
|
5
|
+
|
6
|
+
export class RequestTimeoutError extends SubstrateError {}
|
7
|
+
|
8
|
+
export class NodeError extends SubstrateError {
|
9
|
+
type: string;
|
10
|
+
request_id?: string;
|
11
|
+
override message: string;
|
12
|
+
|
13
|
+
constructor(type: string, message: string, request_id?: string) {
|
14
|
+
super(message);
|
15
|
+
this.type = type;
|
16
|
+
this.message = message;
|
17
|
+
this.request_id = request_id;
|
18
|
+
}
|
19
|
+
}
|
@@ -0,0 +1,196 @@
|
|
1
|
+
/**
|
2
|
+
* EventSource/Server-Sent Events parser
|
3
|
+
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
4
|
+
*
|
5
|
+
* Based on code from {@link https://github.com/rexxars/eventsource-parser},
|
6
|
+
* which is licensed under the MIT license {@link https://github.com/rexxars/eventsource-parser/blob/main/LICENSE}
|
7
|
+
*
|
8
|
+
* Which is based on code from the {@link https://github.com/EventSource/eventsource | EventSource module},
|
9
|
+
* which is licensed under the MIT license. And copyrighted the EventSource GitHub organisation.
|
10
|
+
*/
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Creates a new EventSource parser.
|
14
|
+
* @example
|
15
|
+
*
|
16
|
+
* // create a parser and read start reading a string
|
17
|
+
* let parser = createParser()
|
18
|
+
* for (const message of parser.getMessages(str)) {
|
19
|
+
* // ...
|
20
|
+
* }
|
21
|
+
*
|
22
|
+
* // if you want to reuse the parser.
|
23
|
+
* parser.reset()
|
24
|
+
*/
|
25
|
+
export function createParser() {
|
26
|
+
// Processing state
|
27
|
+
let isFirstChunk: boolean;
|
28
|
+
let buffer: string;
|
29
|
+
let startingPosition: number;
|
30
|
+
let startingFieldLength: number;
|
31
|
+
|
32
|
+
// Event state
|
33
|
+
let eventId: string | undefined;
|
34
|
+
let eventName: string | undefined;
|
35
|
+
let data: string;
|
36
|
+
|
37
|
+
reset();
|
38
|
+
return { getMessages, reset };
|
39
|
+
|
40
|
+
function reset(): void {
|
41
|
+
isFirstChunk = true;
|
42
|
+
buffer = "";
|
43
|
+
startingPosition = 0;
|
44
|
+
startingFieldLength = -1;
|
45
|
+
|
46
|
+
eventId = undefined;
|
47
|
+
eventName = undefined;
|
48
|
+
data = "";
|
49
|
+
}
|
50
|
+
|
51
|
+
function* getMessages(chunk: string) {
|
52
|
+
buffer = buffer ? buffer + chunk : chunk;
|
53
|
+
|
54
|
+
// Strip any UTF8 byte order mark (BOM) at the start of the stream.
|
55
|
+
// Note that we do not strip any non - UTF8 BOM, as eventsource streams are
|
56
|
+
// always decoded as UTF8 as per the specification.
|
57
|
+
if (isFirstChunk && hasBom(buffer)) {
|
58
|
+
buffer = buffer.slice(BOM.length);
|
59
|
+
}
|
60
|
+
|
61
|
+
isFirstChunk = false;
|
62
|
+
|
63
|
+
// Set up chunk-specific processing state
|
64
|
+
const length = buffer.length;
|
65
|
+
let position = 0;
|
66
|
+
let discardTrailingNewline = false;
|
67
|
+
|
68
|
+
// Read the current buffer byte by byte
|
69
|
+
while (position < length) {
|
70
|
+
// EventSource allows for carriage return + line feed, which means we
|
71
|
+
// need to ignore a linefeed character if the previous character was a
|
72
|
+
// carriage return
|
73
|
+
// @todo refactor to reduce nesting, consider checking previous byte?
|
74
|
+
// @todo but consider multiple chunks etc
|
75
|
+
if (discardTrailingNewline) {
|
76
|
+
if (buffer[position] === "\n") {
|
77
|
+
++position;
|
78
|
+
}
|
79
|
+
discardTrailingNewline = false;
|
80
|
+
}
|
81
|
+
|
82
|
+
let lineLength = -1;
|
83
|
+
let fieldLength = startingFieldLength;
|
84
|
+
let character: string;
|
85
|
+
|
86
|
+
for (
|
87
|
+
let index = startingPosition;
|
88
|
+
lineLength < 0 && index < length;
|
89
|
+
++index
|
90
|
+
) {
|
91
|
+
character = buffer[index] as string;
|
92
|
+
if (character === ":" && fieldLength < 0) {
|
93
|
+
fieldLength = index - position;
|
94
|
+
} else if (character === "\r") {
|
95
|
+
discardTrailingNewline = true;
|
96
|
+
lineLength = index - position;
|
97
|
+
} else if (character === "\n") {
|
98
|
+
lineLength = index - position;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
if (lineLength < 0) {
|
103
|
+
startingPosition = length - position;
|
104
|
+
startingFieldLength = fieldLength;
|
105
|
+
break;
|
106
|
+
} else {
|
107
|
+
startingPosition = 0;
|
108
|
+
startingFieldLength = -1;
|
109
|
+
}
|
110
|
+
|
111
|
+
for (let event of parseEventStreamLine(
|
112
|
+
buffer,
|
113
|
+
position,
|
114
|
+
fieldLength,
|
115
|
+
lineLength,
|
116
|
+
)) {
|
117
|
+
if (event) yield event;
|
118
|
+
}
|
119
|
+
|
120
|
+
position += lineLength + 1;
|
121
|
+
}
|
122
|
+
|
123
|
+
if (position === length) {
|
124
|
+
// If we consumed the entire buffer to read the event, reset the buffer
|
125
|
+
buffer = "";
|
126
|
+
} else if (position > 0) {
|
127
|
+
// If there are bytes left to process, set the buffer to the unprocessed
|
128
|
+
// portion of the buffer only
|
129
|
+
buffer = buffer.slice(position);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
function* parseEventStreamLine(
|
134
|
+
lineBuffer: string,
|
135
|
+
index: number,
|
136
|
+
fieldLength: number,
|
137
|
+
lineLength: number,
|
138
|
+
) {
|
139
|
+
if (lineLength === 0) {
|
140
|
+
// We reached the last line of this event
|
141
|
+
if (data.length > 0) {
|
142
|
+
yield {
|
143
|
+
type: "event",
|
144
|
+
id: eventId,
|
145
|
+
event: eventName || undefined,
|
146
|
+
data: data.slice(0, -1), // remove trailing newline
|
147
|
+
};
|
148
|
+
|
149
|
+
data = "";
|
150
|
+
eventId = undefined;
|
151
|
+
}
|
152
|
+
eventName = undefined;
|
153
|
+
yield null;
|
154
|
+
}
|
155
|
+
|
156
|
+
const noValue = fieldLength < 0;
|
157
|
+
const field = lineBuffer.slice(
|
158
|
+
index,
|
159
|
+
index + (noValue ? lineLength : fieldLength),
|
160
|
+
);
|
161
|
+
let step = 0;
|
162
|
+
|
163
|
+
if (noValue) {
|
164
|
+
step = lineLength;
|
165
|
+
} else if (lineBuffer[index + fieldLength + 1] === " ") {
|
166
|
+
step = fieldLength + 2;
|
167
|
+
} else {
|
168
|
+
step = fieldLength + 1;
|
169
|
+
}
|
170
|
+
|
171
|
+
const position = index + step;
|
172
|
+
const valueLength = lineLength - step;
|
173
|
+
const value = lineBuffer.slice(position, position + valueLength).toString();
|
174
|
+
|
175
|
+
if (field === "data") {
|
176
|
+
data += value ? `${value}\n` : "\n";
|
177
|
+
} else if (field === "event") {
|
178
|
+
eventName = value;
|
179
|
+
} else if (field === "id" && !value.includes("\u0000")) {
|
180
|
+
eventId = value;
|
181
|
+
} else if (field === "retry") {
|
182
|
+
const retry = parseInt(value, 10);
|
183
|
+
if (!Number.isNaN(retry)) {
|
184
|
+
yield { type: "reconnect-interval", value: retry };
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
const BOM = [239, 187, 191];
|
191
|
+
|
192
|
+
function hasBom(buffer: string) {
|
193
|
+
return BOM.every(
|
194
|
+
(charCode: number, index: number) => buffer.charCodeAt(index) === charCode,
|
195
|
+
);
|
196
|
+
}
|
package/src/Future.ts
ADDED
@@ -0,0 +1,317 @@
|
|
1
|
+
import { idGenerator } from "substrate/idGenerator";
|
2
|
+
import { Node } from "substrate/Node";
|
3
|
+
|
4
|
+
type Accessor = "item" | "attr";
|
5
|
+
type TraceOperation = {
|
6
|
+
future_id: string | null;
|
7
|
+
key: string | number | null;
|
8
|
+
accessor: Accessor;
|
9
|
+
};
|
10
|
+
|
11
|
+
type TraceProp = string | FutureString | number | FutureNumber;
|
12
|
+
type Concatable = string | FutureString;
|
13
|
+
type JQCompatible = Record<string, unknown> | any[] | string | number;
|
14
|
+
type JQDirectiveTarget = Future<any> | JQCompatible;
|
15
|
+
type FutureTypeMap = {
|
16
|
+
string: FutureString;
|
17
|
+
object: FutureAnyObject;
|
18
|
+
number: FutureNumber;
|
19
|
+
boolean: FutureBoolean;
|
20
|
+
};
|
21
|
+
const parsePath = (path: string): TraceProp[] => {
|
22
|
+
// Split the path by dots or brackets, and filter out empty strings
|
23
|
+
const parts = path.split(/\.|\[|\]\[?/).filter(Boolean);
|
24
|
+
// Convert numeric parts to numbers and keep others as strings
|
25
|
+
return parts.map((part) => (isNaN(Number(part)) ? part : Number(part)));
|
26
|
+
};
|
27
|
+
|
28
|
+
const newFutureId = idGenerator("future");
|
29
|
+
|
30
|
+
abstract class Directive {
|
31
|
+
abstract items: any[];
|
32
|
+
abstract next(...args: any[]): Directive;
|
33
|
+
abstract toJSON(): any;
|
34
|
+
|
35
|
+
abstract result(): Promise<any>;
|
36
|
+
|
37
|
+
referencedFutures() {
|
38
|
+
return (
|
39
|
+
this.items
|
40
|
+
.filter((p) => p instanceof Future)
|
41
|
+
// @ts-ignore
|
42
|
+
.flatMap((p) => [p, ...p.referencedFutures()])
|
43
|
+
);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
export class Trace extends Directive {
|
48
|
+
items: TraceProp[];
|
49
|
+
originNode: Node;
|
50
|
+
|
51
|
+
constructor(items: TraceProp[], originNode: Node) {
|
52
|
+
super();
|
53
|
+
this.items = items;
|
54
|
+
this.originNode = originNode;
|
55
|
+
}
|
56
|
+
|
57
|
+
static Operation = {
|
58
|
+
future: (accessor: Accessor, id: Future<any>["_id"]) => ({
|
59
|
+
future_id: id,
|
60
|
+
key: null,
|
61
|
+
accessor,
|
62
|
+
}),
|
63
|
+
key: (accessor: Accessor, key: string | number) => ({
|
64
|
+
future_id: null,
|
65
|
+
key,
|
66
|
+
accessor,
|
67
|
+
}),
|
68
|
+
};
|
69
|
+
|
70
|
+
override next(...items: TraceProp[]) {
|
71
|
+
return new Trace([...this.items, ...items], this.originNode);
|
72
|
+
}
|
73
|
+
|
74
|
+
override async result(): Promise<any> {
|
75
|
+
// @ts-expect-error (protected result())
|
76
|
+
let result: any = await this.originNode.result();
|
77
|
+
|
78
|
+
for (let item of this.items) {
|
79
|
+
if (item instanceof Future) {
|
80
|
+
// @ts-expect-error (protected result())
|
81
|
+
item = await item._result();
|
82
|
+
}
|
83
|
+
result = result[item as string | number];
|
84
|
+
}
|
85
|
+
return result;
|
86
|
+
}
|
87
|
+
|
88
|
+
override toJSON() {
|
89
|
+
return {
|
90
|
+
type: "trace",
|
91
|
+
origin_node_id: this.originNode.id,
|
92
|
+
op_stack: this.items.map((item) => {
|
93
|
+
if (item instanceof FutureString) {
|
94
|
+
// @ts-expect-error (accessing protected prop: _id)
|
95
|
+
return Trace.Operation.future("attr", item._id);
|
96
|
+
} else if (item instanceof FutureNumber) {
|
97
|
+
// @ts-expect-error (accessing protected prop: _id)
|
98
|
+
return Trace.Operation.future("item", item._id);
|
99
|
+
} else if (typeof item === "string") {
|
100
|
+
return Trace.Operation.key("attr", item);
|
101
|
+
}
|
102
|
+
return Trace.Operation.key("item", item);
|
103
|
+
}) as TraceOperation[],
|
104
|
+
};
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
export class JQ extends Directive {
|
109
|
+
items: any[];
|
110
|
+
target: JQDirectiveTarget;
|
111
|
+
query: string;
|
112
|
+
|
113
|
+
constructor(query: string, target: JQDirectiveTarget) {
|
114
|
+
super();
|
115
|
+
this.items = [target];
|
116
|
+
this.target = target;
|
117
|
+
this.query = query;
|
118
|
+
}
|
119
|
+
|
120
|
+
static JQDirectiveTarget = {
|
121
|
+
future: (id: Future<any>["_id"]) => ({ future_id: id, val: null }),
|
122
|
+
rawValue: (val: JQCompatible) => ({ future_id: null, val }),
|
123
|
+
};
|
124
|
+
|
125
|
+
override next(...items: TraceProp[]) {
|
126
|
+
return new JQ(this.query, this.target);
|
127
|
+
}
|
128
|
+
|
129
|
+
override async result(): Promise<JQCompatible> {
|
130
|
+
return this.target instanceof Future
|
131
|
+
? // @ts-expect-error (accessing protected prop: id)
|
132
|
+
await this.target._result()
|
133
|
+
: this.target;
|
134
|
+
}
|
135
|
+
|
136
|
+
override toJSON(): any {
|
137
|
+
return {
|
138
|
+
type: "jq",
|
139
|
+
query: this.query,
|
140
|
+
target:
|
141
|
+
this.target instanceof Future
|
142
|
+
? // @ts-expect-error (accessing protected prop: id)
|
143
|
+
JQ.JQDirectiveTarget.future(this.target._id)
|
144
|
+
: JQ.JQDirectiveTarget.rawValue(this.target),
|
145
|
+
};
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
export class StringConcat extends Directive {
|
150
|
+
items: Concatable[];
|
151
|
+
|
152
|
+
constructor(items: Concatable[] = []) {
|
153
|
+
super();
|
154
|
+
this.items = items;
|
155
|
+
}
|
156
|
+
|
157
|
+
static Concatable = {
|
158
|
+
string: (val: string) => ({ future_id: null, val }),
|
159
|
+
future: (id: Future<string>["_id"]) => ({ future_id: id, val: null }),
|
160
|
+
};
|
161
|
+
|
162
|
+
override next(...items: Concatable[]) {
|
163
|
+
return new StringConcat([...this.items, ...items]);
|
164
|
+
}
|
165
|
+
|
166
|
+
override async result(): Promise<string> {
|
167
|
+
let result = "";
|
168
|
+
for (let item of this.items) {
|
169
|
+
if (item instanceof Future) {
|
170
|
+
// @ts-expect-error (protected result())
|
171
|
+
item = await item._result();
|
172
|
+
}
|
173
|
+
result = result.concat(item);
|
174
|
+
}
|
175
|
+
return result;
|
176
|
+
}
|
177
|
+
|
178
|
+
override toJSON(): any {
|
179
|
+
return {
|
180
|
+
type: "string-concat",
|
181
|
+
items: this.items.map((item) => {
|
182
|
+
if (item instanceof Future) {
|
183
|
+
// @ts-expect-error (accessing protected prop: _id)
|
184
|
+
return StringConcat.Concatable.future(item._id);
|
185
|
+
}
|
186
|
+
return StringConcat.Concatable.string(item);
|
187
|
+
}),
|
188
|
+
};
|
189
|
+
}
|
190
|
+
}
|
191
|
+
|
192
|
+
export abstract class Future<T> {
|
193
|
+
protected _directive: Directive;
|
194
|
+
protected _id: string = "";
|
195
|
+
|
196
|
+
constructor(directive: Directive, id: string = newFutureId()) {
|
197
|
+
this._directive = directive;
|
198
|
+
this._id = id;
|
199
|
+
}
|
200
|
+
|
201
|
+
protected referencedFutures(): Future<any>[] {
|
202
|
+
return this._directive.referencedFutures();
|
203
|
+
}
|
204
|
+
|
205
|
+
protected toPlaceholder() {
|
206
|
+
return { __$$SB_GRAPH_OP_ID$$__: this._id };
|
207
|
+
}
|
208
|
+
|
209
|
+
protected async _result(): Promise<T> {
|
210
|
+
return this._directive.result();
|
211
|
+
}
|
212
|
+
|
213
|
+
static jq<T extends keyof FutureTypeMap>(
|
214
|
+
future: JQDirectiveTarget,
|
215
|
+
query: string,
|
216
|
+
futureType: keyof FutureTypeMap = "string",
|
217
|
+
): FutureTypeMap[T] {
|
218
|
+
const directive = new JQ(query, future);
|
219
|
+
switch (futureType) {
|
220
|
+
case "string":
|
221
|
+
return new FutureString(directive) as FutureTypeMap[T];
|
222
|
+
case "number":
|
223
|
+
return new FutureNumber(directive) as FutureTypeMap[T];
|
224
|
+
case "object":
|
225
|
+
return new FutureAnyObject(directive) as FutureTypeMap[T];
|
226
|
+
case "boolean":
|
227
|
+
return new FutureBoolean(directive) as FutureTypeMap[T];
|
228
|
+
default:
|
229
|
+
throw new Error(`Unknown future type: ${futureType}`);
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
toJSON() {
|
234
|
+
return {
|
235
|
+
id: this._id,
|
236
|
+
directive: this._directive.toJSON(),
|
237
|
+
};
|
238
|
+
}
|
239
|
+
}
|
240
|
+
|
241
|
+
export class FutureBoolean extends Future<boolean> {}
|
242
|
+
|
243
|
+
export class FutureString extends Future<string> {
|
244
|
+
static concat(...items: (string | FutureString)[]) {
|
245
|
+
return new FutureString(new StringConcat(items));
|
246
|
+
}
|
247
|
+
|
248
|
+
static interpolate(
|
249
|
+
strings: TemplateStringsArray,
|
250
|
+
...exprs: ({ toString(): string } | FutureString)[]
|
251
|
+
): FutureString {
|
252
|
+
return FutureString.concat(
|
253
|
+
...strings.flatMap((s: string, i: number) => {
|
254
|
+
const expr = exprs[i];
|
255
|
+
return expr
|
256
|
+
? [s, expr instanceof Future ? expr : expr.toString()]
|
257
|
+
: [s];
|
258
|
+
}),
|
259
|
+
);
|
260
|
+
}
|
261
|
+
|
262
|
+
concat(...items: (string | FutureString)[]) {
|
263
|
+
return FutureString.concat(...[this, ...items]);
|
264
|
+
}
|
265
|
+
|
266
|
+
protected override async _result(): Promise<string> {
|
267
|
+
return super._result();
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
export class FutureNumber extends Future<number> {}
|
272
|
+
|
273
|
+
export abstract class FutureArray extends Future<any[] | FutureArray> {
|
274
|
+
abstract at(index: number): Future<any>;
|
275
|
+
|
276
|
+
protected override async _result(): Promise<any[] | FutureArray> {
|
277
|
+
return super._result();
|
278
|
+
}
|
279
|
+
}
|
280
|
+
|
281
|
+
export abstract class FutureObject extends Future<Object> {
|
282
|
+
get(path: string): Future<any> {
|
283
|
+
const props = parsePath(path);
|
284
|
+
return props.reduce((future, prop) => {
|
285
|
+
if (future instanceof FutureAnyObject) {
|
286
|
+
return typeof prop === "string"
|
287
|
+
? future.get(prop as string)
|
288
|
+
: future.at(prop as number);
|
289
|
+
} else {
|
290
|
+
// @ts-ignore
|
291
|
+
return typeof prop === "string" ? future[prop] : future.at(prop);
|
292
|
+
}
|
293
|
+
}, this) as Future<any>;
|
294
|
+
}
|
295
|
+
|
296
|
+
protected override async _result(): Promise<Object> {
|
297
|
+
return super._result();
|
298
|
+
}
|
299
|
+
}
|
300
|
+
|
301
|
+
export class FutureAnyObject extends Future<Object> {
|
302
|
+
get(path: string | FutureString) {
|
303
|
+
const d =
|
304
|
+
typeof path === "string"
|
305
|
+
? this._directive.next(...parsePath(path))
|
306
|
+
: this._directive.next(path);
|
307
|
+
return new FutureAnyObject(d);
|
308
|
+
}
|
309
|
+
|
310
|
+
at(index: number | FutureNumber) {
|
311
|
+
return new FutureAnyObject(this._directive.next(index));
|
312
|
+
}
|
313
|
+
|
314
|
+
protected override async _result(): Promise<Object> {
|
315
|
+
return super._result();
|
316
|
+
}
|
317
|
+
}
|
package/src/GEN_VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
20240617.20240806
|