toobit-trade-cli 1.0.4 → 1.0.6
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/index.js +45 -893
- package/package.json +1 -1
- package/src/commands/futures.ts +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// ../core/dist/index.js
|
|
2
4
|
import { createHmac } from "crypto";
|
|
3
5
|
import * as fs from "fs";
|
|
4
6
|
import * as path from "path";
|
|
@@ -6,6 +8,7 @@ import * as os from "os";
|
|
|
6
8
|
import { readFileSync as readFileSync2, writeFileSync, mkdirSync, existsSync as existsSync2 } from "fs";
|
|
7
9
|
import { join as join2, dirname } from "path";
|
|
8
10
|
import { homedir as homedir2 } from "os";
|
|
11
|
+
import { parse, stringify } from "smol-toml";
|
|
9
12
|
function getTimestamp() {
|
|
10
13
|
return Date.now();
|
|
11
14
|
}
|
|
@@ -18,12 +21,12 @@ var ToobitMcpError = class extends Error {
|
|
|
18
21
|
suggestion;
|
|
19
22
|
endpoint;
|
|
20
23
|
constructor(type, message, options) {
|
|
21
|
-
super(message,
|
|
24
|
+
super(message, options?.cause ? { cause: options.cause } : void 0);
|
|
22
25
|
this.name = type;
|
|
23
26
|
this.type = type;
|
|
24
|
-
this.code = options
|
|
25
|
-
this.suggestion = options
|
|
26
|
-
this.endpoint = options
|
|
27
|
+
this.code = options?.code;
|
|
28
|
+
this.suggestion = options?.suggestion;
|
|
29
|
+
this.endpoint = options?.endpoint;
|
|
27
30
|
}
|
|
28
31
|
};
|
|
29
32
|
var ConfigError = class extends ToobitMcpError {
|
|
@@ -295,7 +298,7 @@ var ToobitRestClient = class {
|
|
|
295
298
|
if (codeStr === "-1002" || codeStr === "-1022" || codeStr === "-1107" || codeStr === "-2014" || codeStr === "-2015" || codeStr === "-2017") {
|
|
296
299
|
throw new AuthenticationError(
|
|
297
300
|
message,
|
|
298
|
-
|
|
301
|
+
behavior?.suggestion ?? "Check API key, secret key and permissions.",
|
|
299
302
|
endpoint
|
|
300
303
|
);
|
|
301
304
|
}
|
|
@@ -305,13 +308,13 @@ var ToobitRestClient = class {
|
|
|
305
308
|
throw new ToobitApiError(message, {
|
|
306
309
|
code: codeStr,
|
|
307
310
|
endpoint,
|
|
308
|
-
suggestion: behavior
|
|
311
|
+
suggestion: behavior?.suggestion
|
|
309
312
|
});
|
|
310
313
|
}
|
|
311
314
|
if (!response.ok) {
|
|
312
315
|
const rawMsg = responseMsg ?? "Unknown error";
|
|
313
316
|
let suggestion;
|
|
314
|
-
if (/symbol|paramter|parameter/i.test(rawMsg)) {
|
|
317
|
+
if (/symbol.*not valid|invalid.*symbol|paramter.*symbol|parameter.*symbol|symbol.*format/i.test(rawMsg)) {
|
|
315
318
|
const isFuturesPath = /futures|fundingRate|openInterest|markPrice|contract|longShort|insurance|riskLimit/i.test(config.path);
|
|
316
319
|
suggestion = isFuturesPath ? "Futures endpoints require contract symbol format, e.g. BTC-SWAP-USDT instead of BTCUSDT." : "Spot endpoints require symbol format like BTCUSDT.";
|
|
317
320
|
}
|
|
@@ -674,7 +677,7 @@ function registerFuturesTools() {
|
|
|
674
677
|
{
|
|
675
678
|
name: "futures_place_order",
|
|
676
679
|
module: "futures",
|
|
677
|
-
description: "Place a USDT-M futures order. For market orders, set orderType to MARKET \u2014 the handler automatically converts to type=LIMIT + priceType=MARKET as required by Toobit API. [CAUTION] Executes real trades. Private endpoint. Rate limit: 20 req/s.",
|
|
680
|
+
description: "Place a USDT-M futures order. For market orders, set orderType to MARKET \u2014 the handler automatically converts to type=LIMIT + priceType=MARKET as required by Toobit API. Leverage must be set separately via futures_set_leverage before placing orders. [CAUTION] Executes real trades. Private endpoint. Rate limit: 20 req/s.",
|
|
678
681
|
isWrite: true,
|
|
679
682
|
inputSchema: {
|
|
680
683
|
type: "object",
|
|
@@ -684,8 +687,8 @@ function registerFuturesTools() {
|
|
|
684
687
|
orderType: { type: "string", enum: ["LIMIT", "MARKET", "STOP"], description: "LIMIT=limit order, MARKET=market order (auto-converted), STOP=conditional order" },
|
|
685
688
|
quantity: { type: "string", description: "Order quantity (contracts)" },
|
|
686
689
|
price: { type: "string", description: "Required for LIMIT orders with priceType=INPUT" },
|
|
687
|
-
leverage: { type: "string", description: "
|
|
688
|
-
newClientOrderId: { type: "string", description: "Unique client order ID. Auto-generated if omitted." },
|
|
690
|
+
leverage: { type: "string", description: "IGNORED by place-order API. Use futures_set_leverage to set leverage before placing orders." },
|
|
691
|
+
newClientOrderId: { type: "string", description: "Unique client order ID. Auto-generated if omitted. Only [a-zA-Z0-9_\\-.] allowed; other characters are stripped." },
|
|
689
692
|
priceType: { type: "string", enum: ["INPUT", "OPPONENT", "QUEUE", "OVER", "MARKET"], description: "Price type. INPUT=specified price, MARKET=market price" },
|
|
690
693
|
stopPrice: { type: "string", description: "Trigger price for STOP orders" },
|
|
691
694
|
timeInForce: { type: "string", enum: ["GTC", "IOC", "FOK"] }
|
|
@@ -700,7 +703,8 @@ function registerFuturesTools() {
|
|
|
700
703
|
orderType = "LIMIT";
|
|
701
704
|
priceType = "MARKET";
|
|
702
705
|
}
|
|
703
|
-
const
|
|
706
|
+
const rawClientId = readString(args, "newClientOrderId");
|
|
707
|
+
const clientId = rawClientId ? rawClientId.replace(/[^a-zA-Z0-9_\-\.]/g, "") : `mcp_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
704
708
|
const response = await context.client.privatePost(
|
|
705
709
|
"/api/v1/futures/order",
|
|
706
710
|
compactObject({
|
|
@@ -709,7 +713,6 @@ function registerFuturesTools() {
|
|
|
709
713
|
type: orderType,
|
|
710
714
|
quantity: requireString(args, "quantity"),
|
|
711
715
|
price: readString(args, "price"),
|
|
712
|
-
leverage: readString(args, "leverage"),
|
|
713
716
|
newClientOrderId: clientId,
|
|
714
717
|
priceType,
|
|
715
718
|
stopPrice: readString(args, "stopPrice"),
|
|
@@ -822,16 +825,17 @@ function registerFuturesTools() {
|
|
|
822
825
|
{
|
|
823
826
|
name: "futures_amend_order",
|
|
824
827
|
module: "futures",
|
|
825
|
-
description: "Modify a futures order (price/quantity). Private endpoint. Rate limit: 20 req/s.",
|
|
828
|
+
description: "Modify a futures order (price/quantity). Requires the order type. Private endpoint. Rate limit: 20 req/s.",
|
|
826
829
|
isWrite: true,
|
|
827
830
|
inputSchema: {
|
|
828
831
|
type: "object",
|
|
829
832
|
properties: {
|
|
830
833
|
orderId: { type: "string" },
|
|
834
|
+
type: { type: "string", enum: ["LIMIT", "STOP", "STOP_PROFIT_LOSS"], description: "Order type (required by API)" },
|
|
831
835
|
quantity: { type: "string" },
|
|
832
836
|
price: { type: "string" }
|
|
833
837
|
},
|
|
834
|
-
required: ["orderId"]
|
|
838
|
+
required: ["orderId", "type"]
|
|
835
839
|
},
|
|
836
840
|
handler: async (rawArgs, context) => {
|
|
837
841
|
const args = asRecord(rawArgs);
|
|
@@ -839,6 +843,7 @@ function registerFuturesTools() {
|
|
|
839
843
|
"/api/v1/futures/order/update",
|
|
840
844
|
compactObject({
|
|
841
845
|
orderId: requireString(args, "orderId"),
|
|
846
|
+
type: requireString(args, "type"),
|
|
842
847
|
quantity: readString(args, "quantity"),
|
|
843
848
|
price: readString(args, "price")
|
|
844
849
|
}),
|
|
@@ -1068,10 +1073,10 @@ function registerFuturesTools() {
|
|
|
1068
1073
|
inputSchema: {
|
|
1069
1074
|
type: "object",
|
|
1070
1075
|
properties: {
|
|
1071
|
-
symbol: { type: "string" },
|
|
1076
|
+
symbol: { type: "string", description: "Futures contract symbol, e.g. BTC-SWAP-USDT" },
|
|
1072
1077
|
side: { type: "string", enum: ["LONG", "SHORT"] },
|
|
1073
|
-
|
|
1074
|
-
|
|
1078
|
+
takeProfit: { type: "string", description: "Take-profit price" },
|
|
1079
|
+
stopLoss: { type: "string", description: "Stop-loss price" }
|
|
1075
1080
|
},
|
|
1076
1081
|
required: ["symbol", "side"]
|
|
1077
1082
|
},
|
|
@@ -1082,8 +1087,8 @@ function registerFuturesTools() {
|
|
|
1082
1087
|
compactObject({
|
|
1083
1088
|
symbol: requireString(args, "symbol"),
|
|
1084
1089
|
side: requireString(args, "side"),
|
|
1085
|
-
|
|
1086
|
-
|
|
1090
|
+
takeProfit: readString(args, "takeProfit"),
|
|
1091
|
+
stopLoss: readString(args, "stopLoss")
|
|
1087
1092
|
}),
|
|
1088
1093
|
privateRateLimit("futures_set_trading_stop", 10)
|
|
1089
1094
|
);
|
|
@@ -1292,11 +1297,11 @@ function registerFuturesTools() {
|
|
|
1292
1297
|
inputSchema: {
|
|
1293
1298
|
type: "object",
|
|
1294
1299
|
properties: {
|
|
1295
|
-
symbol: { type: "string" },
|
|
1300
|
+
symbol: { type: "string", description: "Futures contract symbol, e.g. BTC-SWAP-USDT" },
|
|
1296
1301
|
side: { type: "string", enum: ["LONG", "SHORT"] },
|
|
1297
|
-
|
|
1302
|
+
enable: { type: "string", enum: ["ON", "OFF"], description: "ON=enable, OFF=disable" }
|
|
1298
1303
|
},
|
|
1299
|
-
required: ["symbol", "side", "
|
|
1304
|
+
required: ["symbol", "side", "enable"]
|
|
1300
1305
|
},
|
|
1301
1306
|
handler: async (rawArgs, context) => {
|
|
1302
1307
|
const args = asRecord(rawArgs);
|
|
@@ -1305,7 +1310,7 @@ function registerFuturesTools() {
|
|
|
1305
1310
|
compactObject({
|
|
1306
1311
|
symbol: requireString(args, "symbol"),
|
|
1307
1312
|
side: requireString(args, "side"),
|
|
1308
|
-
|
|
1313
|
+
enable: requireString(args, "enable")
|
|
1309
1314
|
}),
|
|
1310
1315
|
privateRateLimit("futures_auto_add_margin", 10)
|
|
1311
1316
|
);
|
|
@@ -1827,9 +1832,9 @@ function registerSpotTradeTools() {
|
|
|
1827
1832
|
symbol: { type: "string", description: "e.g. BTCUSDT" },
|
|
1828
1833
|
side: { type: "string", enum: ["BUY", "SELL"] },
|
|
1829
1834
|
type: { type: "string", enum: ["LIMIT", "MARKET", "LIMIT_MAKER"], description: "Order type" },
|
|
1830
|
-
quantity: { type: "string", description: "
|
|
1835
|
+
quantity: { type: "string", description: "For LIMIT/LIMIT_MAKER: base asset quantity (e.g. 0.001 BTC). For MARKET SELL: base asset quantity to sell. For MARKET BUY: quote asset amount to spend (e.g. 50 means spend 50 USDT)." },
|
|
1831
1836
|
price: { type: "string", description: "Order price (required for LIMIT)" },
|
|
1832
|
-
newClientOrderId: { type: "string", description: "Client order ID" },
|
|
1837
|
+
newClientOrderId: { type: "string", description: "Client order ID. Only [a-zA-Z0-9_\\-.] allowed; other characters are stripped." },
|
|
1833
1838
|
timeInForce: { type: "string", enum: ["GTC", "IOC", "FOK"], description: "Default GTC" }
|
|
1834
1839
|
},
|
|
1835
1840
|
required: ["symbol", "side", "type", "quantity"]
|
|
@@ -1844,7 +1849,7 @@ function registerSpotTradeTools() {
|
|
|
1844
1849
|
type: requireString(args, "type"),
|
|
1845
1850
|
quantity: requireString(args, "quantity"),
|
|
1846
1851
|
price: readString(args, "price"),
|
|
1847
|
-
newClientOrderId: readString(args, "newClientOrderId"),
|
|
1852
|
+
newClientOrderId: readString(args, "newClientOrderId")?.replace(/[^a-zA-Z0-9_\-\.]/g, ""),
|
|
1848
1853
|
timeInForce: readString(args, "timeInForce")
|
|
1849
1854
|
}),
|
|
1850
1855
|
privateRateLimit("spot_place_order", 50)
|
|
@@ -2114,817 +2119,6 @@ function createToolRunner(client, config) {
|
|
|
2114
2119
|
return result;
|
|
2115
2120
|
};
|
|
2116
2121
|
}
|
|
2117
|
-
function getLineColFromPtr(string, ptr) {
|
|
2118
|
-
let lines = string.slice(0, ptr).split(/\r\n|\n|\r/g);
|
|
2119
|
-
return [lines.length, lines.pop().length + 1];
|
|
2120
|
-
}
|
|
2121
|
-
function makeCodeBlock(string, line, column) {
|
|
2122
|
-
let lines = string.split(/\r\n|\n|\r/g);
|
|
2123
|
-
let codeblock = "";
|
|
2124
|
-
let numberLen = (Math.log10(line + 1) | 0) + 1;
|
|
2125
|
-
for (let i = line - 1; i <= line + 1; i++) {
|
|
2126
|
-
let l = lines[i - 1];
|
|
2127
|
-
if (!l)
|
|
2128
|
-
continue;
|
|
2129
|
-
codeblock += i.toString().padEnd(numberLen, " ");
|
|
2130
|
-
codeblock += ": ";
|
|
2131
|
-
codeblock += l;
|
|
2132
|
-
codeblock += "\n";
|
|
2133
|
-
if (i === line) {
|
|
2134
|
-
codeblock += " ".repeat(numberLen + column + 2);
|
|
2135
|
-
codeblock += "^\n";
|
|
2136
|
-
}
|
|
2137
|
-
}
|
|
2138
|
-
return codeblock;
|
|
2139
|
-
}
|
|
2140
|
-
var TomlError = class extends Error {
|
|
2141
|
-
line;
|
|
2142
|
-
column;
|
|
2143
|
-
codeblock;
|
|
2144
|
-
constructor(message, options) {
|
|
2145
|
-
const [line, column] = getLineColFromPtr(options.toml, options.ptr);
|
|
2146
|
-
const codeblock = makeCodeBlock(options.toml, line, column);
|
|
2147
|
-
super(`Invalid TOML document: ${message}
|
|
2148
|
-
|
|
2149
|
-
${codeblock}`, options);
|
|
2150
|
-
this.line = line;
|
|
2151
|
-
this.column = column;
|
|
2152
|
-
this.codeblock = codeblock;
|
|
2153
|
-
}
|
|
2154
|
-
};
|
|
2155
|
-
function isEscaped(str, ptr) {
|
|
2156
|
-
let i = 0;
|
|
2157
|
-
while (str[ptr - ++i] === "\\")
|
|
2158
|
-
;
|
|
2159
|
-
return --i && i % 2;
|
|
2160
|
-
}
|
|
2161
|
-
function indexOfNewline(str, start = 0, end = str.length) {
|
|
2162
|
-
let idx = str.indexOf("\n", start);
|
|
2163
|
-
if (str[idx - 1] === "\r")
|
|
2164
|
-
idx--;
|
|
2165
|
-
return idx <= end ? idx : -1;
|
|
2166
|
-
}
|
|
2167
|
-
function skipComment(str, ptr) {
|
|
2168
|
-
for (let i = ptr; i < str.length; i++) {
|
|
2169
|
-
let c = str[i];
|
|
2170
|
-
if (c === "\n")
|
|
2171
|
-
return i;
|
|
2172
|
-
if (c === "\r" && str[i + 1] === "\n")
|
|
2173
|
-
return i + 1;
|
|
2174
|
-
if (c < " " && c !== " " || c === "\x7F") {
|
|
2175
|
-
throw new TomlError("control characters are not allowed in comments", {
|
|
2176
|
-
toml: str,
|
|
2177
|
-
ptr
|
|
2178
|
-
});
|
|
2179
|
-
}
|
|
2180
|
-
}
|
|
2181
|
-
return str.length;
|
|
2182
|
-
}
|
|
2183
|
-
function skipVoid(str, ptr, banNewLines, banComments) {
|
|
2184
|
-
let c;
|
|
2185
|
-
while ((c = str[ptr]) === " " || c === " " || !banNewLines && (c === "\n" || c === "\r" && str[ptr + 1] === "\n"))
|
|
2186
|
-
ptr++;
|
|
2187
|
-
return banComments || c !== "#" ? ptr : skipVoid(str, skipComment(str, ptr), banNewLines);
|
|
2188
|
-
}
|
|
2189
|
-
function skipUntil(str, ptr, sep, end, banNewLines = false) {
|
|
2190
|
-
if (!end) {
|
|
2191
|
-
ptr = indexOfNewline(str, ptr);
|
|
2192
|
-
return ptr < 0 ? str.length : ptr;
|
|
2193
|
-
}
|
|
2194
|
-
for (let i = ptr; i < str.length; i++) {
|
|
2195
|
-
let c = str[i];
|
|
2196
|
-
if (c === "#") {
|
|
2197
|
-
i = indexOfNewline(str, i);
|
|
2198
|
-
} else if (c === sep) {
|
|
2199
|
-
return i + 1;
|
|
2200
|
-
} else if (c === end || banNewLines && (c === "\n" || c === "\r" && str[i + 1] === "\n")) {
|
|
2201
|
-
return i;
|
|
2202
|
-
}
|
|
2203
|
-
}
|
|
2204
|
-
throw new TomlError("cannot find end of structure", {
|
|
2205
|
-
toml: str,
|
|
2206
|
-
ptr
|
|
2207
|
-
});
|
|
2208
|
-
}
|
|
2209
|
-
function getStringEnd(str, seek) {
|
|
2210
|
-
let first = str[seek];
|
|
2211
|
-
let target = first === str[seek + 1] && str[seek + 1] === str[seek + 2] ? str.slice(seek, seek + 3) : first;
|
|
2212
|
-
seek += target.length - 1;
|
|
2213
|
-
do
|
|
2214
|
-
seek = str.indexOf(target, ++seek);
|
|
2215
|
-
while (seek > -1 && first !== "'" && isEscaped(str, seek));
|
|
2216
|
-
if (seek > -1) {
|
|
2217
|
-
seek += target.length;
|
|
2218
|
-
if (target.length > 1) {
|
|
2219
|
-
if (str[seek] === first)
|
|
2220
|
-
seek++;
|
|
2221
|
-
if (str[seek] === first)
|
|
2222
|
-
seek++;
|
|
2223
|
-
}
|
|
2224
|
-
}
|
|
2225
|
-
return seek;
|
|
2226
|
-
}
|
|
2227
|
-
var DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[T ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[-+]\d{2}:\d{2})?$/i;
|
|
2228
|
-
var TomlDate = class _TomlDate extends Date {
|
|
2229
|
-
#hasDate = false;
|
|
2230
|
-
#hasTime = false;
|
|
2231
|
-
#offset = null;
|
|
2232
|
-
constructor(date) {
|
|
2233
|
-
let hasDate = true;
|
|
2234
|
-
let hasTime = true;
|
|
2235
|
-
let offset = "Z";
|
|
2236
|
-
if (typeof date === "string") {
|
|
2237
|
-
let match = date.match(DATE_TIME_RE);
|
|
2238
|
-
if (match) {
|
|
2239
|
-
if (!match[1]) {
|
|
2240
|
-
hasDate = false;
|
|
2241
|
-
date = `0000-01-01T${date}`;
|
|
2242
|
-
}
|
|
2243
|
-
hasTime = !!match[2];
|
|
2244
|
-
hasTime && date[10] === " " && (date = date.replace(" ", "T"));
|
|
2245
|
-
if (match[2] && +match[2] > 23) {
|
|
2246
|
-
date = "";
|
|
2247
|
-
} else {
|
|
2248
|
-
offset = match[3] || null;
|
|
2249
|
-
date = date.toUpperCase();
|
|
2250
|
-
if (!offset && hasTime)
|
|
2251
|
-
date += "Z";
|
|
2252
|
-
}
|
|
2253
|
-
} else {
|
|
2254
|
-
date = "";
|
|
2255
|
-
}
|
|
2256
|
-
}
|
|
2257
|
-
super(date);
|
|
2258
|
-
if (!isNaN(this.getTime())) {
|
|
2259
|
-
this.#hasDate = hasDate;
|
|
2260
|
-
this.#hasTime = hasTime;
|
|
2261
|
-
this.#offset = offset;
|
|
2262
|
-
}
|
|
2263
|
-
}
|
|
2264
|
-
isDateTime() {
|
|
2265
|
-
return this.#hasDate && this.#hasTime;
|
|
2266
|
-
}
|
|
2267
|
-
isLocal() {
|
|
2268
|
-
return !this.#hasDate || !this.#hasTime || !this.#offset;
|
|
2269
|
-
}
|
|
2270
|
-
isDate() {
|
|
2271
|
-
return this.#hasDate && !this.#hasTime;
|
|
2272
|
-
}
|
|
2273
|
-
isTime() {
|
|
2274
|
-
return this.#hasTime && !this.#hasDate;
|
|
2275
|
-
}
|
|
2276
|
-
isValid() {
|
|
2277
|
-
return this.#hasDate || this.#hasTime;
|
|
2278
|
-
}
|
|
2279
|
-
toISOString() {
|
|
2280
|
-
let iso = super.toISOString();
|
|
2281
|
-
if (this.isDate())
|
|
2282
|
-
return iso.slice(0, 10);
|
|
2283
|
-
if (this.isTime())
|
|
2284
|
-
return iso.slice(11, 23);
|
|
2285
|
-
if (this.#offset === null)
|
|
2286
|
-
return iso.slice(0, -1);
|
|
2287
|
-
if (this.#offset === "Z")
|
|
2288
|
-
return iso;
|
|
2289
|
-
let offset = +this.#offset.slice(1, 3) * 60 + +this.#offset.slice(4, 6);
|
|
2290
|
-
offset = this.#offset[0] === "-" ? offset : -offset;
|
|
2291
|
-
let offsetDate = new Date(this.getTime() - offset * 6e4);
|
|
2292
|
-
return offsetDate.toISOString().slice(0, -1) + this.#offset;
|
|
2293
|
-
}
|
|
2294
|
-
static wrapAsOffsetDateTime(jsDate, offset = "Z") {
|
|
2295
|
-
let date = new _TomlDate(jsDate);
|
|
2296
|
-
date.#offset = offset;
|
|
2297
|
-
return date;
|
|
2298
|
-
}
|
|
2299
|
-
static wrapAsLocalDateTime(jsDate) {
|
|
2300
|
-
let date = new _TomlDate(jsDate);
|
|
2301
|
-
date.#offset = null;
|
|
2302
|
-
return date;
|
|
2303
|
-
}
|
|
2304
|
-
static wrapAsLocalDate(jsDate) {
|
|
2305
|
-
let date = new _TomlDate(jsDate);
|
|
2306
|
-
date.#hasTime = false;
|
|
2307
|
-
date.#offset = null;
|
|
2308
|
-
return date;
|
|
2309
|
-
}
|
|
2310
|
-
static wrapAsLocalTime(jsDate) {
|
|
2311
|
-
let date = new _TomlDate(jsDate);
|
|
2312
|
-
date.#hasDate = false;
|
|
2313
|
-
date.#offset = null;
|
|
2314
|
-
return date;
|
|
2315
|
-
}
|
|
2316
|
-
};
|
|
2317
|
-
var INT_REGEX = /^((0x[0-9a-fA-F](_?[0-9a-fA-F])*)|(([+-]|0[ob])?\d(_?\d)*))$/;
|
|
2318
|
-
var FLOAT_REGEX = /^[+-]?\d(_?\d)*(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/;
|
|
2319
|
-
var LEADING_ZERO = /^[+-]?0[0-9_]/;
|
|
2320
|
-
var ESCAPE_REGEX = /^[0-9a-f]{2,8}$/i;
|
|
2321
|
-
var ESC_MAP = {
|
|
2322
|
-
b: "\b",
|
|
2323
|
-
t: " ",
|
|
2324
|
-
n: "\n",
|
|
2325
|
-
f: "\f",
|
|
2326
|
-
r: "\r",
|
|
2327
|
-
e: "\x1B",
|
|
2328
|
-
'"': '"',
|
|
2329
|
-
"\\": "\\"
|
|
2330
|
-
};
|
|
2331
|
-
function parseString(str, ptr = 0, endPtr = str.length) {
|
|
2332
|
-
let isLiteral = str[ptr] === "'";
|
|
2333
|
-
let isMultiline = str[ptr++] === str[ptr] && str[ptr] === str[ptr + 1];
|
|
2334
|
-
if (isMultiline) {
|
|
2335
|
-
endPtr -= 2;
|
|
2336
|
-
if (str[ptr += 2] === "\r")
|
|
2337
|
-
ptr++;
|
|
2338
|
-
if (str[ptr] === "\n")
|
|
2339
|
-
ptr++;
|
|
2340
|
-
}
|
|
2341
|
-
let tmp = 0;
|
|
2342
|
-
let isEscape;
|
|
2343
|
-
let parsed = "";
|
|
2344
|
-
let sliceStart = ptr;
|
|
2345
|
-
while (ptr < endPtr - 1) {
|
|
2346
|
-
let c = str[ptr++];
|
|
2347
|
-
if (c === "\n" || c === "\r" && str[ptr] === "\n") {
|
|
2348
|
-
if (!isMultiline) {
|
|
2349
|
-
throw new TomlError("newlines are not allowed in strings", {
|
|
2350
|
-
toml: str,
|
|
2351
|
-
ptr: ptr - 1
|
|
2352
|
-
});
|
|
2353
|
-
}
|
|
2354
|
-
} else if (c < " " && c !== " " || c === "\x7F") {
|
|
2355
|
-
throw new TomlError("control characters are not allowed in strings", {
|
|
2356
|
-
toml: str,
|
|
2357
|
-
ptr: ptr - 1
|
|
2358
|
-
});
|
|
2359
|
-
}
|
|
2360
|
-
if (isEscape) {
|
|
2361
|
-
isEscape = false;
|
|
2362
|
-
if (c === "x" || c === "u" || c === "U") {
|
|
2363
|
-
let code = str.slice(ptr, ptr += c === "x" ? 2 : c === "u" ? 4 : 8);
|
|
2364
|
-
if (!ESCAPE_REGEX.test(code)) {
|
|
2365
|
-
throw new TomlError("invalid unicode escape", {
|
|
2366
|
-
toml: str,
|
|
2367
|
-
ptr: tmp
|
|
2368
|
-
});
|
|
2369
|
-
}
|
|
2370
|
-
try {
|
|
2371
|
-
parsed += String.fromCodePoint(parseInt(code, 16));
|
|
2372
|
-
} catch {
|
|
2373
|
-
throw new TomlError("invalid unicode escape", {
|
|
2374
|
-
toml: str,
|
|
2375
|
-
ptr: tmp
|
|
2376
|
-
});
|
|
2377
|
-
}
|
|
2378
|
-
} else if (isMultiline && (c === "\n" || c === " " || c === " " || c === "\r")) {
|
|
2379
|
-
ptr = skipVoid(str, ptr - 1, true);
|
|
2380
|
-
if (str[ptr] !== "\n" && str[ptr] !== "\r") {
|
|
2381
|
-
throw new TomlError("invalid escape: only line-ending whitespace may be escaped", {
|
|
2382
|
-
toml: str,
|
|
2383
|
-
ptr: tmp
|
|
2384
|
-
});
|
|
2385
|
-
}
|
|
2386
|
-
ptr = skipVoid(str, ptr);
|
|
2387
|
-
} else if (c in ESC_MAP) {
|
|
2388
|
-
parsed += ESC_MAP[c];
|
|
2389
|
-
} else {
|
|
2390
|
-
throw new TomlError("unrecognized escape sequence", {
|
|
2391
|
-
toml: str,
|
|
2392
|
-
ptr: tmp
|
|
2393
|
-
});
|
|
2394
|
-
}
|
|
2395
|
-
sliceStart = ptr;
|
|
2396
|
-
} else if (!isLiteral && c === "\\") {
|
|
2397
|
-
tmp = ptr - 1;
|
|
2398
|
-
isEscape = true;
|
|
2399
|
-
parsed += str.slice(sliceStart, tmp);
|
|
2400
|
-
}
|
|
2401
|
-
}
|
|
2402
|
-
return parsed + str.slice(sliceStart, endPtr - 1);
|
|
2403
|
-
}
|
|
2404
|
-
function parseValue(value, toml, ptr, integersAsBigInt) {
|
|
2405
|
-
if (value === "true")
|
|
2406
|
-
return true;
|
|
2407
|
-
if (value === "false")
|
|
2408
|
-
return false;
|
|
2409
|
-
if (value === "-inf")
|
|
2410
|
-
return -Infinity;
|
|
2411
|
-
if (value === "inf" || value === "+inf")
|
|
2412
|
-
return Infinity;
|
|
2413
|
-
if (value === "nan" || value === "+nan" || value === "-nan")
|
|
2414
|
-
return NaN;
|
|
2415
|
-
if (value === "-0")
|
|
2416
|
-
return integersAsBigInt ? 0n : 0;
|
|
2417
|
-
let isInt = INT_REGEX.test(value);
|
|
2418
|
-
if (isInt || FLOAT_REGEX.test(value)) {
|
|
2419
|
-
if (LEADING_ZERO.test(value)) {
|
|
2420
|
-
throw new TomlError("leading zeroes are not allowed", {
|
|
2421
|
-
toml,
|
|
2422
|
-
ptr
|
|
2423
|
-
});
|
|
2424
|
-
}
|
|
2425
|
-
value = value.replace(/_/g, "");
|
|
2426
|
-
let numeric = +value;
|
|
2427
|
-
if (isNaN(numeric)) {
|
|
2428
|
-
throw new TomlError("invalid number", {
|
|
2429
|
-
toml,
|
|
2430
|
-
ptr
|
|
2431
|
-
});
|
|
2432
|
-
}
|
|
2433
|
-
if (isInt) {
|
|
2434
|
-
if ((isInt = !Number.isSafeInteger(numeric)) && !integersAsBigInt) {
|
|
2435
|
-
throw new TomlError("integer value cannot be represented losslessly", {
|
|
2436
|
-
toml,
|
|
2437
|
-
ptr
|
|
2438
|
-
});
|
|
2439
|
-
}
|
|
2440
|
-
if (isInt || integersAsBigInt === true)
|
|
2441
|
-
numeric = BigInt(value);
|
|
2442
|
-
}
|
|
2443
|
-
return numeric;
|
|
2444
|
-
}
|
|
2445
|
-
const date = new TomlDate(value);
|
|
2446
|
-
if (!date.isValid()) {
|
|
2447
|
-
throw new TomlError("invalid value", {
|
|
2448
|
-
toml,
|
|
2449
|
-
ptr
|
|
2450
|
-
});
|
|
2451
|
-
}
|
|
2452
|
-
return date;
|
|
2453
|
-
}
|
|
2454
|
-
function sliceAndTrimEndOf(str, startPtr, endPtr) {
|
|
2455
|
-
let value = str.slice(startPtr, endPtr);
|
|
2456
|
-
let commentIdx = value.indexOf("#");
|
|
2457
|
-
if (commentIdx > -1) {
|
|
2458
|
-
skipComment(str, commentIdx);
|
|
2459
|
-
value = value.slice(0, commentIdx);
|
|
2460
|
-
}
|
|
2461
|
-
return [value.trimEnd(), commentIdx];
|
|
2462
|
-
}
|
|
2463
|
-
function extractValue(str, ptr, end, depth, integersAsBigInt) {
|
|
2464
|
-
if (depth === 0) {
|
|
2465
|
-
throw new TomlError("document contains excessively nested structures. aborting.", {
|
|
2466
|
-
toml: str,
|
|
2467
|
-
ptr
|
|
2468
|
-
});
|
|
2469
|
-
}
|
|
2470
|
-
let c = str[ptr];
|
|
2471
|
-
if (c === "[" || c === "{") {
|
|
2472
|
-
let [value, endPtr2] = c === "[" ? parseArray(str, ptr, depth, integersAsBigInt) : parseInlineTable(str, ptr, depth, integersAsBigInt);
|
|
2473
|
-
if (end) {
|
|
2474
|
-
endPtr2 = skipVoid(str, endPtr2);
|
|
2475
|
-
if (str[endPtr2] === ",")
|
|
2476
|
-
endPtr2++;
|
|
2477
|
-
else if (str[endPtr2] !== end) {
|
|
2478
|
-
throw new TomlError("expected comma or end of structure", {
|
|
2479
|
-
toml: str,
|
|
2480
|
-
ptr: endPtr2
|
|
2481
|
-
});
|
|
2482
|
-
}
|
|
2483
|
-
}
|
|
2484
|
-
return [value, endPtr2];
|
|
2485
|
-
}
|
|
2486
|
-
let endPtr;
|
|
2487
|
-
if (c === '"' || c === "'") {
|
|
2488
|
-
endPtr = getStringEnd(str, ptr);
|
|
2489
|
-
let parsed = parseString(str, ptr, endPtr);
|
|
2490
|
-
if (end) {
|
|
2491
|
-
endPtr = skipVoid(str, endPtr);
|
|
2492
|
-
if (str[endPtr] && str[endPtr] !== "," && str[endPtr] !== end && str[endPtr] !== "\n" && str[endPtr] !== "\r") {
|
|
2493
|
-
throw new TomlError("unexpected character encountered", {
|
|
2494
|
-
toml: str,
|
|
2495
|
-
ptr: endPtr
|
|
2496
|
-
});
|
|
2497
|
-
}
|
|
2498
|
-
endPtr += +(str[endPtr] === ",");
|
|
2499
|
-
}
|
|
2500
|
-
return [parsed, endPtr];
|
|
2501
|
-
}
|
|
2502
|
-
endPtr = skipUntil(str, ptr, ",", end);
|
|
2503
|
-
let slice = sliceAndTrimEndOf(str, ptr, endPtr - +(str[endPtr - 1] === ","));
|
|
2504
|
-
if (!slice[0]) {
|
|
2505
|
-
throw new TomlError("incomplete key-value declaration: no value specified", {
|
|
2506
|
-
toml: str,
|
|
2507
|
-
ptr
|
|
2508
|
-
});
|
|
2509
|
-
}
|
|
2510
|
-
if (end && slice[1] > -1) {
|
|
2511
|
-
endPtr = skipVoid(str, ptr + slice[1]);
|
|
2512
|
-
endPtr += +(str[endPtr] === ",");
|
|
2513
|
-
}
|
|
2514
|
-
return [
|
|
2515
|
-
parseValue(slice[0], str, ptr, integersAsBigInt),
|
|
2516
|
-
endPtr
|
|
2517
|
-
];
|
|
2518
|
-
}
|
|
2519
|
-
var KEY_PART_RE = /^[a-zA-Z0-9-_]+[ \t]*$/;
|
|
2520
|
-
function parseKey(str, ptr, end = "=") {
|
|
2521
|
-
let dot = ptr - 1;
|
|
2522
|
-
let parsed = [];
|
|
2523
|
-
let endPtr = str.indexOf(end, ptr);
|
|
2524
|
-
if (endPtr < 0) {
|
|
2525
|
-
throw new TomlError("incomplete key-value: cannot find end of key", {
|
|
2526
|
-
toml: str,
|
|
2527
|
-
ptr
|
|
2528
|
-
});
|
|
2529
|
-
}
|
|
2530
|
-
do {
|
|
2531
|
-
let c = str[ptr = ++dot];
|
|
2532
|
-
if (c !== " " && c !== " ") {
|
|
2533
|
-
if (c === '"' || c === "'") {
|
|
2534
|
-
if (c === str[ptr + 1] && c === str[ptr + 2]) {
|
|
2535
|
-
throw new TomlError("multiline strings are not allowed in keys", {
|
|
2536
|
-
toml: str,
|
|
2537
|
-
ptr
|
|
2538
|
-
});
|
|
2539
|
-
}
|
|
2540
|
-
let eos = getStringEnd(str, ptr);
|
|
2541
|
-
if (eos < 0) {
|
|
2542
|
-
throw new TomlError("unfinished string encountered", {
|
|
2543
|
-
toml: str,
|
|
2544
|
-
ptr
|
|
2545
|
-
});
|
|
2546
|
-
}
|
|
2547
|
-
dot = str.indexOf(".", eos);
|
|
2548
|
-
let strEnd = str.slice(eos, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
2549
|
-
let newLine = indexOfNewline(strEnd);
|
|
2550
|
-
if (newLine > -1) {
|
|
2551
|
-
throw new TomlError("newlines are not allowed in keys", {
|
|
2552
|
-
toml: str,
|
|
2553
|
-
ptr: ptr + dot + newLine
|
|
2554
|
-
});
|
|
2555
|
-
}
|
|
2556
|
-
if (strEnd.trimStart()) {
|
|
2557
|
-
throw new TomlError("found extra tokens after the string part", {
|
|
2558
|
-
toml: str,
|
|
2559
|
-
ptr: eos
|
|
2560
|
-
});
|
|
2561
|
-
}
|
|
2562
|
-
if (endPtr < eos) {
|
|
2563
|
-
endPtr = str.indexOf(end, eos);
|
|
2564
|
-
if (endPtr < 0) {
|
|
2565
|
-
throw new TomlError("incomplete key-value: cannot find end of key", {
|
|
2566
|
-
toml: str,
|
|
2567
|
-
ptr
|
|
2568
|
-
});
|
|
2569
|
-
}
|
|
2570
|
-
}
|
|
2571
|
-
parsed.push(parseString(str, ptr, eos));
|
|
2572
|
-
} else {
|
|
2573
|
-
dot = str.indexOf(".", ptr);
|
|
2574
|
-
let part = str.slice(ptr, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
2575
|
-
if (!KEY_PART_RE.test(part)) {
|
|
2576
|
-
throw new TomlError("only letter, numbers, dashes and underscores are allowed in keys", {
|
|
2577
|
-
toml: str,
|
|
2578
|
-
ptr
|
|
2579
|
-
});
|
|
2580
|
-
}
|
|
2581
|
-
parsed.push(part.trimEnd());
|
|
2582
|
-
}
|
|
2583
|
-
}
|
|
2584
|
-
} while (dot + 1 && dot < endPtr);
|
|
2585
|
-
return [parsed, skipVoid(str, endPtr + 1, true, true)];
|
|
2586
|
-
}
|
|
2587
|
-
function parseInlineTable(str, ptr, depth, integersAsBigInt) {
|
|
2588
|
-
let res = {};
|
|
2589
|
-
let seen = /* @__PURE__ */ new Set();
|
|
2590
|
-
let c;
|
|
2591
|
-
ptr++;
|
|
2592
|
-
while ((c = str[ptr++]) !== "}" && c) {
|
|
2593
|
-
if (c === ",") {
|
|
2594
|
-
throw new TomlError("expected value, found comma", {
|
|
2595
|
-
toml: str,
|
|
2596
|
-
ptr: ptr - 1
|
|
2597
|
-
});
|
|
2598
|
-
} else if (c === "#")
|
|
2599
|
-
ptr = skipComment(str, ptr);
|
|
2600
|
-
else if (c !== " " && c !== " " && c !== "\n" && c !== "\r") {
|
|
2601
|
-
let k;
|
|
2602
|
-
let t = res;
|
|
2603
|
-
let hasOwn = false;
|
|
2604
|
-
let [key, keyEndPtr] = parseKey(str, ptr - 1);
|
|
2605
|
-
for (let i = 0; i < key.length; i++) {
|
|
2606
|
-
if (i)
|
|
2607
|
-
t = hasOwn ? t[k] : t[k] = {};
|
|
2608
|
-
k = key[i];
|
|
2609
|
-
if ((hasOwn = Object.hasOwn(t, k)) && (typeof t[k] !== "object" || seen.has(t[k]))) {
|
|
2610
|
-
throw new TomlError("trying to redefine an already defined value", {
|
|
2611
|
-
toml: str,
|
|
2612
|
-
ptr
|
|
2613
|
-
});
|
|
2614
|
-
}
|
|
2615
|
-
if (!hasOwn && k === "__proto__") {
|
|
2616
|
-
Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
|
|
2617
|
-
}
|
|
2618
|
-
}
|
|
2619
|
-
if (hasOwn) {
|
|
2620
|
-
throw new TomlError("trying to redefine an already defined value", {
|
|
2621
|
-
toml: str,
|
|
2622
|
-
ptr
|
|
2623
|
-
});
|
|
2624
|
-
}
|
|
2625
|
-
let [value, valueEndPtr] = extractValue(str, keyEndPtr, "}", depth - 1, integersAsBigInt);
|
|
2626
|
-
seen.add(value);
|
|
2627
|
-
t[k] = value;
|
|
2628
|
-
ptr = valueEndPtr;
|
|
2629
|
-
}
|
|
2630
|
-
}
|
|
2631
|
-
if (!c) {
|
|
2632
|
-
throw new TomlError("unfinished table encountered", {
|
|
2633
|
-
toml: str,
|
|
2634
|
-
ptr
|
|
2635
|
-
});
|
|
2636
|
-
}
|
|
2637
|
-
return [res, ptr];
|
|
2638
|
-
}
|
|
2639
|
-
function parseArray(str, ptr, depth, integersAsBigInt) {
|
|
2640
|
-
let res = [];
|
|
2641
|
-
let c;
|
|
2642
|
-
ptr++;
|
|
2643
|
-
while ((c = str[ptr++]) !== "]" && c) {
|
|
2644
|
-
if (c === ",") {
|
|
2645
|
-
throw new TomlError("expected value, found comma", {
|
|
2646
|
-
toml: str,
|
|
2647
|
-
ptr: ptr - 1
|
|
2648
|
-
});
|
|
2649
|
-
} else if (c === "#")
|
|
2650
|
-
ptr = skipComment(str, ptr);
|
|
2651
|
-
else if (c !== " " && c !== " " && c !== "\n" && c !== "\r") {
|
|
2652
|
-
let e = extractValue(str, ptr - 1, "]", depth - 1, integersAsBigInt);
|
|
2653
|
-
res.push(e[0]);
|
|
2654
|
-
ptr = e[1];
|
|
2655
|
-
}
|
|
2656
|
-
}
|
|
2657
|
-
if (!c) {
|
|
2658
|
-
throw new TomlError("unfinished array encountered", {
|
|
2659
|
-
toml: str,
|
|
2660
|
-
ptr
|
|
2661
|
-
});
|
|
2662
|
-
}
|
|
2663
|
-
return [res, ptr];
|
|
2664
|
-
}
|
|
2665
|
-
function peekTable(key, table, meta, type) {
|
|
2666
|
-
var _a, _b;
|
|
2667
|
-
let t = table;
|
|
2668
|
-
let m = meta;
|
|
2669
|
-
let k;
|
|
2670
|
-
let hasOwn = false;
|
|
2671
|
-
let state;
|
|
2672
|
-
for (let i = 0; i < key.length; i++) {
|
|
2673
|
-
if (i) {
|
|
2674
|
-
t = hasOwn ? t[k] : t[k] = {};
|
|
2675
|
-
m = (state = m[k]).c;
|
|
2676
|
-
if (type === 0 && (state.t === 1 || state.t === 2)) {
|
|
2677
|
-
return null;
|
|
2678
|
-
}
|
|
2679
|
-
if (state.t === 2) {
|
|
2680
|
-
let l = t.length - 1;
|
|
2681
|
-
t = t[l];
|
|
2682
|
-
m = m[l].c;
|
|
2683
|
-
}
|
|
2684
|
-
}
|
|
2685
|
-
k = key[i];
|
|
2686
|
-
if ((hasOwn = Object.hasOwn(t, k)) && ((_a = m[k]) == null ? void 0 : _a.t) === 0 && ((_b = m[k]) == null ? void 0 : _b.d)) {
|
|
2687
|
-
return null;
|
|
2688
|
-
}
|
|
2689
|
-
if (!hasOwn) {
|
|
2690
|
-
if (k === "__proto__") {
|
|
2691
|
-
Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
|
|
2692
|
-
Object.defineProperty(m, k, { enumerable: true, configurable: true, writable: true });
|
|
2693
|
-
}
|
|
2694
|
-
m[k] = {
|
|
2695
|
-
t: i < key.length - 1 && type === 2 ? 3 : type,
|
|
2696
|
-
d: false,
|
|
2697
|
-
i: 0,
|
|
2698
|
-
c: {}
|
|
2699
|
-
};
|
|
2700
|
-
}
|
|
2701
|
-
}
|
|
2702
|
-
state = m[k];
|
|
2703
|
-
if (state.t !== type && !(type === 1 && state.t === 3)) {
|
|
2704
|
-
return null;
|
|
2705
|
-
}
|
|
2706
|
-
if (type === 2) {
|
|
2707
|
-
if (!state.d) {
|
|
2708
|
-
state.d = true;
|
|
2709
|
-
t[k] = [];
|
|
2710
|
-
}
|
|
2711
|
-
t[k].push(t = {});
|
|
2712
|
-
state.c[state.i++] = state = { t: 1, d: false, i: 0, c: {} };
|
|
2713
|
-
}
|
|
2714
|
-
if (state.d) {
|
|
2715
|
-
return null;
|
|
2716
|
-
}
|
|
2717
|
-
state.d = true;
|
|
2718
|
-
if (type === 1) {
|
|
2719
|
-
t = hasOwn ? t[k] : t[k] = {};
|
|
2720
|
-
} else if (type === 0 && hasOwn) {
|
|
2721
|
-
return null;
|
|
2722
|
-
}
|
|
2723
|
-
return [k, t, state.c];
|
|
2724
|
-
}
|
|
2725
|
-
function parse(toml, { maxDepth = 1e3, integersAsBigInt } = {}) {
|
|
2726
|
-
let res = {};
|
|
2727
|
-
let meta = {};
|
|
2728
|
-
let tbl = res;
|
|
2729
|
-
let m = meta;
|
|
2730
|
-
for (let ptr = skipVoid(toml, 0); ptr < toml.length; ) {
|
|
2731
|
-
if (toml[ptr] === "[") {
|
|
2732
|
-
let isTableArray = toml[++ptr] === "[";
|
|
2733
|
-
let k = parseKey(toml, ptr += +isTableArray, "]");
|
|
2734
|
-
if (isTableArray) {
|
|
2735
|
-
if (toml[k[1] - 1] !== "]") {
|
|
2736
|
-
throw new TomlError("expected end of table declaration", {
|
|
2737
|
-
toml,
|
|
2738
|
-
ptr: k[1] - 1
|
|
2739
|
-
});
|
|
2740
|
-
}
|
|
2741
|
-
k[1]++;
|
|
2742
|
-
}
|
|
2743
|
-
let p = peekTable(
|
|
2744
|
-
k[0],
|
|
2745
|
-
res,
|
|
2746
|
-
meta,
|
|
2747
|
-
isTableArray ? 2 : 1
|
|
2748
|
-
/* Type.EXPLICIT */
|
|
2749
|
-
);
|
|
2750
|
-
if (!p) {
|
|
2751
|
-
throw new TomlError("trying to redefine an already defined table or value", {
|
|
2752
|
-
toml,
|
|
2753
|
-
ptr
|
|
2754
|
-
});
|
|
2755
|
-
}
|
|
2756
|
-
m = p[2];
|
|
2757
|
-
tbl = p[1];
|
|
2758
|
-
ptr = k[1];
|
|
2759
|
-
} else {
|
|
2760
|
-
let k = parseKey(toml, ptr);
|
|
2761
|
-
let p = peekTable(
|
|
2762
|
-
k[0],
|
|
2763
|
-
tbl,
|
|
2764
|
-
m,
|
|
2765
|
-
0
|
|
2766
|
-
/* Type.DOTTED */
|
|
2767
|
-
);
|
|
2768
|
-
if (!p) {
|
|
2769
|
-
throw new TomlError("trying to redefine an already defined table or value", {
|
|
2770
|
-
toml,
|
|
2771
|
-
ptr
|
|
2772
|
-
});
|
|
2773
|
-
}
|
|
2774
|
-
let v = extractValue(toml, k[1], void 0, maxDepth, integersAsBigInt);
|
|
2775
|
-
p[1][p[0]] = v[0];
|
|
2776
|
-
ptr = v[1];
|
|
2777
|
-
}
|
|
2778
|
-
ptr = skipVoid(toml, ptr, true);
|
|
2779
|
-
if (toml[ptr] && toml[ptr] !== "\n" && toml[ptr] !== "\r") {
|
|
2780
|
-
throw new TomlError("each key-value declaration must be followed by an end-of-line", {
|
|
2781
|
-
toml,
|
|
2782
|
-
ptr
|
|
2783
|
-
});
|
|
2784
|
-
}
|
|
2785
|
-
ptr = skipVoid(toml, ptr);
|
|
2786
|
-
}
|
|
2787
|
-
return res;
|
|
2788
|
-
}
|
|
2789
|
-
var BARE_KEY = /^[a-z0-9-_]+$/i;
|
|
2790
|
-
function extendedTypeOf(obj) {
|
|
2791
|
-
let type = typeof obj;
|
|
2792
|
-
if (type === "object") {
|
|
2793
|
-
if (Array.isArray(obj))
|
|
2794
|
-
return "array";
|
|
2795
|
-
if (obj instanceof Date)
|
|
2796
|
-
return "date";
|
|
2797
|
-
}
|
|
2798
|
-
return type;
|
|
2799
|
-
}
|
|
2800
|
-
function isArrayOfTables(obj) {
|
|
2801
|
-
for (let i = 0; i < obj.length; i++) {
|
|
2802
|
-
if (extendedTypeOf(obj[i]) !== "object")
|
|
2803
|
-
return false;
|
|
2804
|
-
}
|
|
2805
|
-
return obj.length != 0;
|
|
2806
|
-
}
|
|
2807
|
-
function formatString(s) {
|
|
2808
|
-
return JSON.stringify(s).replace(/\x7f/g, "\\u007f");
|
|
2809
|
-
}
|
|
2810
|
-
function stringifyValue(val, type, depth, numberAsFloat) {
|
|
2811
|
-
if (depth === 0) {
|
|
2812
|
-
throw new Error("Could not stringify the object: maximum object depth exceeded");
|
|
2813
|
-
}
|
|
2814
|
-
if (type === "number") {
|
|
2815
|
-
if (isNaN(val))
|
|
2816
|
-
return "nan";
|
|
2817
|
-
if (val === Infinity)
|
|
2818
|
-
return "inf";
|
|
2819
|
-
if (val === -Infinity)
|
|
2820
|
-
return "-inf";
|
|
2821
|
-
if (numberAsFloat && Number.isInteger(val))
|
|
2822
|
-
return val.toFixed(1);
|
|
2823
|
-
return val.toString();
|
|
2824
|
-
}
|
|
2825
|
-
if (type === "bigint" || type === "boolean") {
|
|
2826
|
-
return val.toString();
|
|
2827
|
-
}
|
|
2828
|
-
if (type === "string") {
|
|
2829
|
-
return formatString(val);
|
|
2830
|
-
}
|
|
2831
|
-
if (type === "date") {
|
|
2832
|
-
if (isNaN(val.getTime())) {
|
|
2833
|
-
throw new TypeError("cannot serialize invalid date");
|
|
2834
|
-
}
|
|
2835
|
-
return val.toISOString();
|
|
2836
|
-
}
|
|
2837
|
-
if (type === "object") {
|
|
2838
|
-
return stringifyInlineTable(val, depth, numberAsFloat);
|
|
2839
|
-
}
|
|
2840
|
-
if (type === "array") {
|
|
2841
|
-
return stringifyArray(val, depth, numberAsFloat);
|
|
2842
|
-
}
|
|
2843
|
-
}
|
|
2844
|
-
function stringifyInlineTable(obj, depth, numberAsFloat) {
|
|
2845
|
-
let keys = Object.keys(obj);
|
|
2846
|
-
if (keys.length === 0)
|
|
2847
|
-
return "{}";
|
|
2848
|
-
let res = "{ ";
|
|
2849
|
-
for (let i = 0; i < keys.length; i++) {
|
|
2850
|
-
let k = keys[i];
|
|
2851
|
-
if (i)
|
|
2852
|
-
res += ", ";
|
|
2853
|
-
res += BARE_KEY.test(k) ? k : formatString(k);
|
|
2854
|
-
res += " = ";
|
|
2855
|
-
res += stringifyValue(obj[k], extendedTypeOf(obj[k]), depth - 1, numberAsFloat);
|
|
2856
|
-
}
|
|
2857
|
-
return res + " }";
|
|
2858
|
-
}
|
|
2859
|
-
function stringifyArray(array, depth, numberAsFloat) {
|
|
2860
|
-
if (array.length === 0)
|
|
2861
|
-
return "[]";
|
|
2862
|
-
let res = "[ ";
|
|
2863
|
-
for (let i = 0; i < array.length; i++) {
|
|
2864
|
-
if (i)
|
|
2865
|
-
res += ", ";
|
|
2866
|
-
if (array[i] === null || array[i] === void 0) {
|
|
2867
|
-
throw new TypeError("arrays cannot contain null or undefined values");
|
|
2868
|
-
}
|
|
2869
|
-
res += stringifyValue(array[i], extendedTypeOf(array[i]), depth - 1, numberAsFloat);
|
|
2870
|
-
}
|
|
2871
|
-
return res + " ]";
|
|
2872
|
-
}
|
|
2873
|
-
function stringifyArrayTable(array, key, depth, numberAsFloat) {
|
|
2874
|
-
if (depth === 0) {
|
|
2875
|
-
throw new Error("Could not stringify the object: maximum object depth exceeded");
|
|
2876
|
-
}
|
|
2877
|
-
let res = "";
|
|
2878
|
-
for (let i = 0; i < array.length; i++) {
|
|
2879
|
-
res += `${res && "\n"}[[${key}]]
|
|
2880
|
-
`;
|
|
2881
|
-
res += stringifyTable(0, array[i], key, depth, numberAsFloat);
|
|
2882
|
-
}
|
|
2883
|
-
return res;
|
|
2884
|
-
}
|
|
2885
|
-
function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
|
|
2886
|
-
if (depth === 0) {
|
|
2887
|
-
throw new Error("Could not stringify the object: maximum object depth exceeded");
|
|
2888
|
-
}
|
|
2889
|
-
let preamble = "";
|
|
2890
|
-
let tables = "";
|
|
2891
|
-
let keys = Object.keys(obj);
|
|
2892
|
-
for (let i = 0; i < keys.length; i++) {
|
|
2893
|
-
let k = keys[i];
|
|
2894
|
-
if (obj[k] !== null && obj[k] !== void 0) {
|
|
2895
|
-
let type = extendedTypeOf(obj[k]);
|
|
2896
|
-
if (type === "symbol" || type === "function") {
|
|
2897
|
-
throw new TypeError(`cannot serialize values of type '${type}'`);
|
|
2898
|
-
}
|
|
2899
|
-
let key = BARE_KEY.test(k) ? k : formatString(k);
|
|
2900
|
-
if (type === "array" && isArrayOfTables(obj[k])) {
|
|
2901
|
-
tables += (tables && "\n") + stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1, numberAsFloat);
|
|
2902
|
-
} else if (type === "object") {
|
|
2903
|
-
let tblKey = prefix ? `${prefix}.${key}` : key;
|
|
2904
|
-
tables += (tables && "\n") + stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
|
|
2905
|
-
} else {
|
|
2906
|
-
preamble += key;
|
|
2907
|
-
preamble += " = ";
|
|
2908
|
-
preamble += stringifyValue(obj[k], type, depth, numberAsFloat);
|
|
2909
|
-
preamble += "\n";
|
|
2910
|
-
}
|
|
2911
|
-
}
|
|
2912
|
-
}
|
|
2913
|
-
if (tableKey && (preamble || !tables))
|
|
2914
|
-
preamble = preamble ? `[${tableKey}]
|
|
2915
|
-
${preamble}` : `[${tableKey}]`;
|
|
2916
|
-
return preamble && tables ? `${preamble}
|
|
2917
|
-
${tables}` : preamble || tables;
|
|
2918
|
-
}
|
|
2919
|
-
function stringify(obj, { maxDepth = 1e3, numbersAsFloat = false } = {}) {
|
|
2920
|
-
if (extendedTypeOf(obj) !== "object") {
|
|
2921
|
-
throw new TypeError("stringify can only be called with an object");
|
|
2922
|
-
}
|
|
2923
|
-
let str = stringifyTable(0, obj, "", maxDepth, numbersAsFloat);
|
|
2924
|
-
if (str[str.length - 1] !== "\n")
|
|
2925
|
-
return str + "\n";
|
|
2926
|
-
return str;
|
|
2927
|
-
}
|
|
2928
2122
|
function configFilePath() {
|
|
2929
2123
|
return join2(homedir2(), ".toobit", "config.toml");
|
|
2930
2124
|
}
|
|
@@ -2935,10 +2129,9 @@ function readFullConfig() {
|
|
|
2935
2129
|
return parse(raw);
|
|
2936
2130
|
}
|
|
2937
2131
|
function readTomlProfile(profileName) {
|
|
2938
|
-
var _a;
|
|
2939
2132
|
const config = readFullConfig();
|
|
2940
2133
|
const name = profileName ?? config.default_profile ?? "default";
|
|
2941
|
-
return
|
|
2134
|
+
return config.profiles?.[name] ?? {};
|
|
2942
2135
|
}
|
|
2943
2136
|
function writeFullConfig(config) {
|
|
2944
2137
|
const path4 = configFilePath();
|
|
@@ -2973,10 +2166,9 @@ function parseModuleList(rawModules) {
|
|
|
2973
2166
|
return Array.from(deduped);
|
|
2974
2167
|
}
|
|
2975
2168
|
function loadConfig(cli) {
|
|
2976
|
-
var _a, _b, _c;
|
|
2977
2169
|
const toml = readTomlProfile(cli.profile);
|
|
2978
|
-
const apiKey =
|
|
2979
|
-
const secretKey =
|
|
2170
|
+
const apiKey = process.env.TOOBIT_API_KEY?.trim() ?? toml.api_key;
|
|
2171
|
+
const secretKey = process.env.TOOBIT_SECRET_KEY?.trim() ?? toml.secret_key;
|
|
2980
2172
|
const hasAuth = Boolean(apiKey && secretKey);
|
|
2981
2173
|
const partialAuth = Boolean(apiKey) || Boolean(secretKey);
|
|
2982
2174
|
if (partialAuth && !hasAuth) {
|
|
@@ -2985,7 +2177,7 @@ function loadConfig(cli) {
|
|
|
2985
2177
|
"Set TOOBIT_API_KEY and TOOBIT_SECRET_KEY together (env vars or config.toml profile)."
|
|
2986
2178
|
);
|
|
2987
2179
|
}
|
|
2988
|
-
const rawBaseUrl =
|
|
2180
|
+
const rawBaseUrl = process.env.TOOBIT_API_BASE_URL?.trim() ?? toml.base_url ?? TOOBIT_API_BASE_URL;
|
|
2989
2181
|
if (!rawBaseUrl.startsWith("http://") && !rawBaseUrl.startsWith("https://")) {
|
|
2990
2182
|
throw new ConfigError(
|
|
2991
2183
|
`Invalid base URL "${rawBaseUrl}".`,
|
|
@@ -3021,7 +2213,7 @@ var CLIENT_NAMES = {
|
|
|
3021
2213
|
};
|
|
3022
2214
|
var SUPPORTED_CLIENTS = Object.keys(CLIENT_NAMES);
|
|
3023
2215
|
|
|
3024
|
-
//
|
|
2216
|
+
// src/parser.ts
|
|
3025
2217
|
import { parseArgs } from "util";
|
|
3026
2218
|
function parseCli(argv = process.argv.slice(2)) {
|
|
3027
2219
|
const STRING_OPTIONS = /* @__PURE__ */ new Set([
|
|
@@ -3102,7 +2294,7 @@ function parseCli(argv = process.argv.slice(2)) {
|
|
|
3102
2294
|
};
|
|
3103
2295
|
}
|
|
3104
2296
|
|
|
3105
|
-
//
|
|
2297
|
+
// src/formatter.ts
|
|
3106
2298
|
function extractRows(value) {
|
|
3107
2299
|
if (Array.isArray(value)) return value;
|
|
3108
2300
|
if (typeof value === "object" && value !== null) {
|
|
@@ -3157,7 +2349,7 @@ ${separator}
|
|
|
3157
2349
|
${body}`;
|
|
3158
2350
|
}
|
|
3159
2351
|
|
|
3160
|
-
//
|
|
2352
|
+
// src/commands/market.ts
|
|
3161
2353
|
async function handleMarketCommand(cli, run) {
|
|
3162
2354
|
const f = cli.flags;
|
|
3163
2355
|
let result;
|
|
@@ -3235,7 +2427,7 @@ Available: time, info, ticker, ticker-24hr, depth, trades, klines, candles, book
|
|
|
3235
2427
|
process.stdout.write(formatJson(result, cli.json) + "\n");
|
|
3236
2428
|
}
|
|
3237
2429
|
|
|
3238
|
-
//
|
|
2430
|
+
// src/commands/spot.ts
|
|
3239
2431
|
async function handleSpotCommand(cli, run) {
|
|
3240
2432
|
const f = cli.flags;
|
|
3241
2433
|
let result;
|
|
@@ -3285,7 +2477,7 @@ Available: place, cancel, cancel-all, get, orders, open-orders, history, fills
|
|
|
3285
2477
|
process.stdout.write(formatJson(result, cli.json) + "\n");
|
|
3286
2478
|
}
|
|
3287
2479
|
|
|
3288
|
-
//
|
|
2480
|
+
// src/commands/futures.ts
|
|
3289
2481
|
async function handleFuturesCommand(cli, run) {
|
|
3290
2482
|
const f = cli.flags;
|
|
3291
2483
|
let result;
|
|
@@ -3296,8 +2488,7 @@ async function handleFuturesCommand(cli, run) {
|
|
|
3296
2488
|
side: f.side,
|
|
3297
2489
|
orderType: f.orderType ?? f.type ?? "MARKET",
|
|
3298
2490
|
quantity: f.quantity,
|
|
3299
|
-
price: f.price
|
|
3300
|
-
leverage: f.leverage
|
|
2491
|
+
price: f.price
|
|
3301
2492
|
});
|
|
3302
2493
|
break;
|
|
3303
2494
|
case "cancel":
|
|
@@ -3362,7 +2553,7 @@ Available: place, cancel, cancel-all, amend, get, orders, history, positions, hi
|
|
|
3362
2553
|
process.stdout.write(formatJson(result, cli.json) + "\n");
|
|
3363
2554
|
}
|
|
3364
2555
|
|
|
3365
|
-
//
|
|
2556
|
+
// src/commands/account.ts
|
|
3366
2557
|
async function handleAccountCommand(cli, run) {
|
|
3367
2558
|
const f = cli.flags;
|
|
3368
2559
|
let result;
|
|
@@ -3411,7 +2602,7 @@ Available: balance, info, balance-flow, sub-accounts, check-api-key, deposit-add
|
|
|
3411
2602
|
process.stdout.write(formatJson(result, cli.json) + "\n");
|
|
3412
2603
|
}
|
|
3413
2604
|
|
|
3414
|
-
//
|
|
2605
|
+
// src/commands/config.ts
|
|
3415
2606
|
import * as readline from "readline";
|
|
3416
2607
|
function prompt(question) {
|
|
3417
2608
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
@@ -3477,7 +2668,7 @@ Available: init, show, list
|
|
|
3477
2668
|
}
|
|
3478
2669
|
}
|
|
3479
2670
|
|
|
3480
|
-
//
|
|
2671
|
+
// src/index.ts
|
|
3481
2672
|
function printHelp() {
|
|
3482
2673
|
const help = `
|
|
3483
2674
|
Toobit Trade CLI \u2014 Trade from your terminal
|
|
@@ -3552,42 +2743,3 @@ main().catch((error) => {
|
|
|
3552
2743
|
`);
|
|
3553
2744
|
process.exitCode = 1;
|
|
3554
2745
|
});
|
|
3555
|
-
/*! Bundled license information:
|
|
3556
|
-
|
|
3557
|
-
smol-toml/dist/error.js:
|
|
3558
|
-
smol-toml/dist/util.js:
|
|
3559
|
-
smol-toml/dist/date.js:
|
|
3560
|
-
smol-toml/dist/primitive.js:
|
|
3561
|
-
smol-toml/dist/extract.js:
|
|
3562
|
-
smol-toml/dist/struct.js:
|
|
3563
|
-
smol-toml/dist/parse.js:
|
|
3564
|
-
smol-toml/dist/stringify.js:
|
|
3565
|
-
smol-toml/dist/index.js:
|
|
3566
|
-
(*!
|
|
3567
|
-
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
3568
|
-
* SPDX-License-Identifier: BSD-3-Clause
|
|
3569
|
-
*
|
|
3570
|
-
* Redistribution and use in source and binary forms, with or without
|
|
3571
|
-
* modification, are permitted provided that the following conditions are met:
|
|
3572
|
-
*
|
|
3573
|
-
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
3574
|
-
* list of conditions and the following disclaimer.
|
|
3575
|
-
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
3576
|
-
* this list of conditions and the following disclaimer in the
|
|
3577
|
-
* documentation and/or other materials provided with the distribution.
|
|
3578
|
-
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
3579
|
-
* may be used to endorse or promote products derived from this software without
|
|
3580
|
-
* specific prior written permission.
|
|
3581
|
-
*
|
|
3582
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
3583
|
-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
3584
|
-
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
3585
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
3586
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
3587
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
3588
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
3589
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
3590
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
3591
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
3592
|
-
*)
|
|
3593
|
-
*/
|
package/package.json
CHANGED
package/src/commands/futures.ts
CHANGED