@mtkruto/node 0.0.832 → 0.0.834
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/esm/client/client.js +24 -8
- package/esm/client/client_abstract.d.ts +1 -1
- package/esm/client/client_abstract.js +3 -8
- package/esm/client/client_plain.js +8 -7
- package/esm/connection/connection.d.ts +1 -0
- package/esm/connection/connection_web_socket.d.ts +1 -0
- package/esm/connection/connection_web_socket.js +3 -0
- package/esm/deps/deno.land/std@0.69.0/fmt/colors.d.ts +77 -0
- package/esm/deps/deno.land/std@0.69.0/fmt/colors.js +224 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/colors.d.ts +3 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/colors.js +71 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/debug.d.ts +22 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/debug.js +112 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/deps.d.ts +2 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/deps.js +2 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/format.d.ts +1 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/format.js +39 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/mod.d.ts +1 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/debug/master/mod.js +1 -0
- package/esm/deps.d.ts +2 -0
- package/esm/deps.js +2 -0
- package/package.json +8 -6
- package/script/client/client.js +23 -7
- package/script/client/client_abstract.d.ts +1 -1
- package/script/client/client_abstract.js +3 -8
- package/script/client/client_plain.js +7 -6
- package/script/connection/connection.d.ts +1 -0
- package/script/connection/connection_web_socket.d.ts +1 -0
- package/script/connection/connection_web_socket.js +3 -0
- package/script/deps/deno.land/std@0.69.0/fmt/colors.d.ts +77 -0
- package/script/deps/deno.land/std@0.69.0/fmt/colors.js +298 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/colors.d.ts +3 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/colors.js +98 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/debug.d.ts +22 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/debug.js +141 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/deps.d.ts +2 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/deps.js +32 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/format.d.ts +1 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/format.js +43 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/mod.d.ts +1 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/debug/master/mod.js +17 -0
- package/script/deps.d.ts +2 -0
- package/script/deps.js +4 -1
package/esm/client/client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { gunzip } from "../deps.js";
|
|
1
|
+
import { debug, gunzip } from "../deps.js";
|
|
2
2
|
import { ackThreshold, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, USERNAME_TTL, ZERO_CHANNEL_ID } from "../constants.js";
|
|
3
3
|
import { bigIntFromBuffer, getRandomBigInt, getRandomId } from "../utilities/0_bigint.js";
|
|
4
4
|
import { parseHtml } from "../utilities/0_html.js";
|
|
@@ -16,6 +16,7 @@ import { ClientPlain } from "./client_plain.js";
|
|
|
16
16
|
import { StorageMemory } from "../storage/storage_memory.js";
|
|
17
17
|
import { sha1 } from "../utilities/0_hash.js";
|
|
18
18
|
import { messageEntityToTlObject } from "../types/0_message_entity.js";
|
|
19
|
+
const d = debug("client");
|
|
19
20
|
export const restartAuth = Symbol();
|
|
20
21
|
export var ParseMode;
|
|
21
22
|
(function (ParseMode) {
|
|
@@ -200,7 +201,7 @@ export class Client extends ClientAbstract {
|
|
|
200
201
|
if (dc == null) {
|
|
201
202
|
await this.storage.setDc(DEFAULT_INITIAL_DC);
|
|
202
203
|
}
|
|
203
|
-
|
|
204
|
+
d("enrypted client connected");
|
|
204
205
|
this.receiveLoop();
|
|
205
206
|
this.pingLoop();
|
|
206
207
|
}
|
|
@@ -228,6 +229,7 @@ export class Client extends ClientAbstract {
|
|
|
228
229
|
if (!this.apiHash) {
|
|
229
230
|
throw new Error("apiHash not set");
|
|
230
231
|
}
|
|
232
|
+
d("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
|
|
231
233
|
await this.invoke(new functions.InitConnection({
|
|
232
234
|
apiId: this.apiId,
|
|
233
235
|
appVersion: this.appVersion,
|
|
@@ -241,6 +243,7 @@ export class Client extends ClientAbstract {
|
|
|
241
243
|
systemLangCode: this.systemLangCode,
|
|
242
244
|
systemVersion: this.systemVersion,
|
|
243
245
|
}));
|
|
246
|
+
d("connection inited");
|
|
244
247
|
const handlePassword = async (err) => {
|
|
245
248
|
params = params;
|
|
246
249
|
if (err instanceof types.RPCError && err.errorMessage == "SESSION_PASSWORD_NEEDED") {
|
|
@@ -251,6 +254,7 @@ export class Client extends ClientAbstract {
|
|
|
251
254
|
const password = typeof params.password === "string" ? params.password : await params.password();
|
|
252
255
|
const input = await checkPassword(password, ap);
|
|
253
256
|
await this.invoke(new functions.AuthCheckPassword({ password: input }));
|
|
257
|
+
d("authorized as user");
|
|
254
258
|
break;
|
|
255
259
|
}
|
|
256
260
|
catch (err) {
|
|
@@ -273,6 +277,7 @@ export class Client extends ClientAbstract {
|
|
|
273
277
|
};
|
|
274
278
|
try {
|
|
275
279
|
await this.invoke(new functions.UpdatesGetState());
|
|
280
|
+
d("already authorized");
|
|
276
281
|
return;
|
|
277
282
|
}
|
|
278
283
|
catch (err) {
|
|
@@ -285,6 +290,7 @@ export class Client extends ClientAbstract {
|
|
|
285
290
|
try {
|
|
286
291
|
if (params instanceof types.AuthExportedAuthorization) {
|
|
287
292
|
await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
|
|
293
|
+
d("authorization imported");
|
|
288
294
|
}
|
|
289
295
|
else if (typeof params == "object") {
|
|
290
296
|
while (true) {
|
|
@@ -300,6 +306,7 @@ export class Client extends ClientAbstract {
|
|
|
300
306
|
phoneNumber,
|
|
301
307
|
settings: new types.CodeSettings(),
|
|
302
308
|
}));
|
|
309
|
+
d("verification code sent");
|
|
303
310
|
if (sentCode instanceof types.AuthSentCode) {
|
|
304
311
|
while (true) {
|
|
305
312
|
const phoneCode = typeof params.code === "string" ? params.code : await params.code();
|
|
@@ -310,6 +317,7 @@ export class Client extends ClientAbstract {
|
|
|
310
317
|
}
|
|
311
318
|
else {
|
|
312
319
|
signedIn = true;
|
|
320
|
+
d("authorized as user");
|
|
313
321
|
break;
|
|
314
322
|
}
|
|
315
323
|
}
|
|
@@ -343,6 +351,7 @@ export class Client extends ClientAbstract {
|
|
|
343
351
|
}
|
|
344
352
|
else {
|
|
345
353
|
await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
|
|
354
|
+
d("authorized as bot");
|
|
346
355
|
}
|
|
347
356
|
}
|
|
348
357
|
catch (err) {
|
|
@@ -354,6 +363,7 @@ export class Client extends ClientAbstract {
|
|
|
354
363
|
newDc += "-test";
|
|
355
364
|
}
|
|
356
365
|
await this.reconnect(newDc);
|
|
366
|
+
d("migrated to DC%s", newDc);
|
|
357
367
|
if (typeof params === "object" && phoneNumber != null) {
|
|
358
368
|
params = Object.assign({}, params);
|
|
359
369
|
params.phone = phoneNumber;
|
|
@@ -394,8 +404,8 @@ export class Client extends ClientAbstract {
|
|
|
394
404
|
try {
|
|
395
405
|
decrypted = await decryptMessage(buffer, this.auth.key, this.auth.id, this.sessionId);
|
|
396
406
|
}
|
|
397
|
-
catch (
|
|
398
|
-
|
|
407
|
+
catch (err) {
|
|
408
|
+
d("failed to decrypt message: %o", err);
|
|
399
409
|
continue;
|
|
400
410
|
}
|
|
401
411
|
const messages = decrypted instanceof MessageContainer ? decrypted.messages : [decrypted];
|
|
@@ -404,7 +414,7 @@ export class Client extends ClientAbstract {
|
|
|
404
414
|
if (body instanceof types.GZIPPacked) {
|
|
405
415
|
body = new TLReader(gunzip(body.packedData)).readObject();
|
|
406
416
|
}
|
|
407
|
-
|
|
417
|
+
d("received %s", body.constructor.name);
|
|
408
418
|
if (body instanceof types.Updates) {
|
|
409
419
|
this.processUpdates(body);
|
|
410
420
|
}
|
|
@@ -413,6 +423,12 @@ export class Client extends ClientAbstract {
|
|
|
413
423
|
if (result instanceof types.GZIPPacked) {
|
|
414
424
|
result = new TLReader(gunzip(result.packedData)).readObject();
|
|
415
425
|
}
|
|
426
|
+
if (result instanceof types.RPCError) {
|
|
427
|
+
d("RPCResult: %d %s", result.errorCode, result.errorMessage);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
d("RPCResult: %s", result.constructor.name);
|
|
431
|
+
}
|
|
416
432
|
const promise = this.promises.get(message.body.messageId);
|
|
417
433
|
if (promise) {
|
|
418
434
|
if (result instanceof types.RPCError) {
|
|
@@ -454,8 +470,8 @@ export class Client extends ClientAbstract {
|
|
|
454
470
|
try {
|
|
455
471
|
await this.invoke(new functions.Ping({ pingId: getRandomBigInt(8, true, false) }));
|
|
456
472
|
}
|
|
457
|
-
catch (
|
|
458
|
-
|
|
473
|
+
catch (err) {
|
|
474
|
+
d("ping loop error: %o", err);
|
|
459
475
|
}
|
|
460
476
|
await new Promise((r) => setTimeout(r, 60 * 1000));
|
|
461
477
|
}
|
|
@@ -471,7 +487,7 @@ export class Client extends ClientAbstract {
|
|
|
471
487
|
}
|
|
472
488
|
const message = new Message(getMessageId(), seqNo, function_);
|
|
473
489
|
await this.transport.send(await encryptMessage(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
|
|
474
|
-
|
|
490
|
+
d("invoked %s", function_.constructor.name);
|
|
475
491
|
if (noWait) {
|
|
476
492
|
return;
|
|
477
493
|
}
|
|
@@ -7,10 +7,10 @@ export declare abstract class ClientAbstract {
|
|
|
7
7
|
protected connection: Connection;
|
|
8
8
|
protected transport: Transport;
|
|
9
9
|
private _dcId;
|
|
10
|
-
protected connected: boolean;
|
|
11
10
|
constructor(transportProvider?: import("../transport/transport_provider.js").TransportProvider);
|
|
12
11
|
get dcId(): number;
|
|
13
12
|
setDc(dc: DC): MaybePromise<void>;
|
|
13
|
+
get connected(): boolean;
|
|
14
14
|
connect(): Promise<void>;
|
|
15
15
|
reconnect(dc?: DC): Promise<void>;
|
|
16
16
|
disconnect(): Promise<void>;
|
|
@@ -27,12 +27,6 @@ export class ClientAbstract {
|
|
|
27
27
|
writable: true,
|
|
28
28
|
value: void 0
|
|
29
29
|
});
|
|
30
|
-
Object.defineProperty(this, "connected", {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
configurable: true,
|
|
33
|
-
writable: true,
|
|
34
|
-
value: false
|
|
35
|
-
});
|
|
36
30
|
const { connection, transport, dcId } = transportProvider({ cdn: false });
|
|
37
31
|
this.connection = connection;
|
|
38
32
|
this.transport = transport;
|
|
@@ -48,11 +42,13 @@ export class ClientAbstract {
|
|
|
48
42
|
this.transport = transport;
|
|
49
43
|
this._dcId = dcId;
|
|
50
44
|
}
|
|
45
|
+
get connected() {
|
|
46
|
+
return this.connection.connected;
|
|
47
|
+
}
|
|
51
48
|
async connect() {
|
|
52
49
|
await initTgCrypto();
|
|
53
50
|
await this.connection.open();
|
|
54
51
|
await this.transport.initialize();
|
|
55
|
-
this.connected = true;
|
|
56
52
|
}
|
|
57
53
|
async reconnect(dc) {
|
|
58
54
|
await this.disconnect();
|
|
@@ -64,6 +60,5 @@ export class ClientAbstract {
|
|
|
64
60
|
async disconnect() {
|
|
65
61
|
await this.transport.deinitialize();
|
|
66
62
|
await this.connection.close();
|
|
67
|
-
this.connected = false;
|
|
68
63
|
}
|
|
69
64
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertEquals, assertInstanceOf, factorize, ige256Decrypt, ige256Encrypt } from "../deps.js";
|
|
1
|
+
import { assertEquals, assertInstanceOf, debug, factorize, ige256Decrypt, ige256Encrypt } from "../deps.js";
|
|
2
2
|
import { publicKeys } from "../constants.js";
|
|
3
3
|
import { bigIntFromBuffer, getRandomBigInt, modExp } from "../utilities/0_bigint.js";
|
|
4
4
|
import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
|
|
@@ -10,6 +10,7 @@ import { ClientDHInnerData, DHGenOK, PQInnerDataDC, ResPQ, ServerDHInnerData, Se
|
|
|
10
10
|
import { ReqDHParams, ReqPQMulti, SetClientDHParams } from "../tl/3_functions.js";
|
|
11
11
|
import { TLReader } from "../tl/3_tl_reader.js";
|
|
12
12
|
import { ClientAbstract } from "./client_abstract.js";
|
|
13
|
+
const d = debug("auth_key_creation");
|
|
13
14
|
export class ClientPlain extends ClientAbstract {
|
|
14
15
|
async invoke(function_) {
|
|
15
16
|
await this.transport.send(packUnencryptedMessage(function_[serialize]()));
|
|
@@ -26,14 +27,14 @@ export class ClientPlain extends ClientAbstract {
|
|
|
26
27
|
}
|
|
27
28
|
async createAuthKey() {
|
|
28
29
|
const nonce = getRandomBigInt(16, false, true);
|
|
29
|
-
|
|
30
|
+
d("auth key creation started");
|
|
30
31
|
const resPq = await this.invoke(new ReqPQMulti({ nonce }));
|
|
31
32
|
assertInstanceOf(resPq, ResPQ);
|
|
32
33
|
assertEquals(resPq.nonce, nonce);
|
|
33
|
-
|
|
34
|
+
d("got res_pq");
|
|
34
35
|
const pq_ = bigIntFromBuffer(resPq.pq, false, false);
|
|
35
36
|
const [p_, q_] = factorize(pq_);
|
|
36
|
-
|
|
37
|
+
d("factorized pq");
|
|
37
38
|
const p = bufferFromBigInt(p_, 4, false, false);
|
|
38
39
|
const q = bufferFromBigInt(q_, 4, false, false);
|
|
39
40
|
let publicKeyFingerprint;
|
|
@@ -71,7 +72,7 @@ export class ClientPlain extends ClientAbstract {
|
|
|
71
72
|
encryptedData,
|
|
72
73
|
}));
|
|
73
74
|
assertInstanceOf(dhParams, ServerDHParamsOK);
|
|
74
|
-
|
|
75
|
+
d("got server_DH_params_ok");
|
|
75
76
|
const newNonce_ = bufferFromBigInt(newNonce, 32, true, true);
|
|
76
77
|
const serverNonce_ = bufferFromBigInt(serverNonce, 16, true, true);
|
|
77
78
|
const tmpAesKey = concat(await sha1(concat(newNonce_, serverNonce_)), (await sha1(concat(serverNonce_, newNonce_))).slice(0, 0 + 12));
|
|
@@ -97,12 +98,12 @@ export class ClientPlain extends ClientAbstract {
|
|
|
97
98
|
encryptedData = ige256Encrypt(dataWithHash, tmpAesKey, tmpAesIv);
|
|
98
99
|
const dhGenOk = await this.invoke(new SetClientDHParams({ nonce, serverNonce, encryptedData }));
|
|
99
100
|
assertInstanceOf(dhGenOk, DHGenOK);
|
|
100
|
-
|
|
101
|
+
d("got dh_gen_ok");
|
|
101
102
|
const serverNonceSlice = serverNonce_.slice(0, 8);
|
|
102
103
|
const salt = newNonce_.slice(0, 0 + 8).map((v, i) => v ^ serverNonceSlice[i]);
|
|
103
104
|
const authKey_ = modExp(gA, b, dhPrime);
|
|
104
105
|
const authKey = bufferFromBigInt(authKey_, 256, false, false);
|
|
105
|
-
|
|
106
|
+
d("auth key created");
|
|
106
107
|
return {
|
|
107
108
|
authKey,
|
|
108
109
|
salt: bigIntFromBuffer(salt, true, false),
|
|
@@ -6,6 +6,7 @@ export declare class ConnectionWebSocket implements Connection {
|
|
|
6
6
|
private buffer;
|
|
7
7
|
private nextResolve;
|
|
8
8
|
constructor(url: string | URL);
|
|
9
|
+
get connected(): boolean;
|
|
9
10
|
open(): Promise<void>;
|
|
10
11
|
read(p: Uint8Array): Promise<void>;
|
|
11
12
|
write(p: Uint8Array): Promise<void>;
|
|
@@ -46,6 +46,9 @@ export class ConnectionWebSocket {
|
|
|
46
46
|
};
|
|
47
47
|
this.webSocket.onerror = console.error;
|
|
48
48
|
}
|
|
49
|
+
get connected() {
|
|
50
|
+
return this.webSocket.readyState == dntShim.WebSocket.OPEN;
|
|
51
|
+
}
|
|
49
52
|
async open() {
|
|
50
53
|
while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
|
|
51
54
|
if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** RGB 8-bits per channel. Each in range `0->255` or `0x00->0xff` */
|
|
2
|
+
interface Rgb {
|
|
3
|
+
r: number;
|
|
4
|
+
g: number;
|
|
5
|
+
b: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function setColorEnabled(value: boolean): void;
|
|
8
|
+
export declare function getColorEnabled(): boolean;
|
|
9
|
+
export declare function reset(str: string): string;
|
|
10
|
+
export declare function bold(str: string): string;
|
|
11
|
+
export declare function dim(str: string): string;
|
|
12
|
+
export declare function italic(str: string): string;
|
|
13
|
+
export declare function underline(str: string): string;
|
|
14
|
+
export declare function inverse(str: string): string;
|
|
15
|
+
export declare function hidden(str: string): string;
|
|
16
|
+
export declare function strikethrough(str: string): string;
|
|
17
|
+
export declare function black(str: string): string;
|
|
18
|
+
export declare function red(str: string): string;
|
|
19
|
+
export declare function green(str: string): string;
|
|
20
|
+
export declare function yellow(str: string): string;
|
|
21
|
+
export declare function blue(str: string): string;
|
|
22
|
+
export declare function magenta(str: string): string;
|
|
23
|
+
export declare function cyan(str: string): string;
|
|
24
|
+
export declare function white(str: string): string;
|
|
25
|
+
export declare function gray(str: string): string;
|
|
26
|
+
export declare function brightBlack(str: string): string;
|
|
27
|
+
export declare function brightRed(str: string): string;
|
|
28
|
+
export declare function brightGreen(str: string): string;
|
|
29
|
+
export declare function brightYellow(str: string): string;
|
|
30
|
+
export declare function brightBlue(str: string): string;
|
|
31
|
+
export declare function brightMagenta(str: string): string;
|
|
32
|
+
export declare function brightCyan(str: string): string;
|
|
33
|
+
export declare function brightWhite(str: string): string;
|
|
34
|
+
export declare function bgBlack(str: string): string;
|
|
35
|
+
export declare function bgRed(str: string): string;
|
|
36
|
+
export declare function bgGreen(str: string): string;
|
|
37
|
+
export declare function bgYellow(str: string): string;
|
|
38
|
+
export declare function bgBlue(str: string): string;
|
|
39
|
+
export declare function bgMagenta(str: string): string;
|
|
40
|
+
export declare function bgCyan(str: string): string;
|
|
41
|
+
export declare function bgWhite(str: string): string;
|
|
42
|
+
export declare function bgBrightBlack(str: string): string;
|
|
43
|
+
export declare function bgBrightRed(str: string): string;
|
|
44
|
+
export declare function bgBrightGreen(str: string): string;
|
|
45
|
+
export declare function bgBrightYellow(str: string): string;
|
|
46
|
+
export declare function bgBrightBlue(str: string): string;
|
|
47
|
+
export declare function bgBrightMagenta(str: string): string;
|
|
48
|
+
export declare function bgBrightCyan(str: string): string;
|
|
49
|
+
export declare function bgBrightWhite(str: string): string;
|
|
50
|
+
/** Set text color using paletted 8bit colors.
|
|
51
|
+
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
|
|
52
|
+
export declare function rgb8(str: string, color: number): string;
|
|
53
|
+
/** Set background color using paletted 8bit colors.
|
|
54
|
+
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
|
|
55
|
+
export declare function bgRgb8(str: string, color: number): string;
|
|
56
|
+
/** Set text color using 24bit rgb.
|
|
57
|
+
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
58
|
+
* an `Rgb`.
|
|
59
|
+
*
|
|
60
|
+
* To produce the color magenta:
|
|
61
|
+
*
|
|
62
|
+
* rgba24("foo", 0xff00ff);
|
|
63
|
+
* rgba24("foo", {r: 255, g: 0, b: 255});
|
|
64
|
+
*/
|
|
65
|
+
export declare function rgb24(str: string, color: number | Rgb): string;
|
|
66
|
+
/** Set background color using 24bit rgb.
|
|
67
|
+
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
68
|
+
* an `Rgb`.
|
|
69
|
+
*
|
|
70
|
+
* To produce the color magenta:
|
|
71
|
+
*
|
|
72
|
+
* bgRgba24("foo", 0xff00ff);
|
|
73
|
+
* bgRgba24("foo", {r: 255, g: 0, b: 255});
|
|
74
|
+
*/
|
|
75
|
+
export declare function bgRgb24(str: string, color: number | Rgb): string;
|
|
76
|
+
export declare function stripColor(string: string): string;
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
/** A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
|
|
3
|
+
* on npm.
|
|
4
|
+
*
|
|
5
|
+
* ```
|
|
6
|
+
* import { bgBlue, red, bold } from "https://deno.land/std/fmt/colors.ts";
|
|
7
|
+
* console.log(bgBlue(red(bold("Hello world!"))));
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* This module supports `NO_COLOR` environmental variable disabling any coloring
|
|
11
|
+
* if `NO_COLOR` is set.
|
|
12
|
+
*
|
|
13
|
+
* This module is browser compatible. */
|
|
14
|
+
import * as dntShim from "../../../../_dnt.shims.js";
|
|
15
|
+
const noColor = dntShim.dntGlobalThis.Deno?.noColor ?? true;
|
|
16
|
+
let enabled = !noColor;
|
|
17
|
+
export function setColorEnabled(value) {
|
|
18
|
+
if (noColor) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
enabled = value;
|
|
22
|
+
}
|
|
23
|
+
export function getColorEnabled() {
|
|
24
|
+
return enabled;
|
|
25
|
+
}
|
|
26
|
+
function code(open, close) {
|
|
27
|
+
return {
|
|
28
|
+
open: `\x1b[${open.join(";")}m`,
|
|
29
|
+
close: `\x1b[${close}m`,
|
|
30
|
+
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function run(str, code) {
|
|
34
|
+
return enabled
|
|
35
|
+
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
|
|
36
|
+
: str;
|
|
37
|
+
}
|
|
38
|
+
export function reset(str) {
|
|
39
|
+
return run(str, code([0], 0));
|
|
40
|
+
}
|
|
41
|
+
export function bold(str) {
|
|
42
|
+
return run(str, code([1], 22));
|
|
43
|
+
}
|
|
44
|
+
export function dim(str) {
|
|
45
|
+
return run(str, code([2], 22));
|
|
46
|
+
}
|
|
47
|
+
export function italic(str) {
|
|
48
|
+
return run(str, code([3], 23));
|
|
49
|
+
}
|
|
50
|
+
export function underline(str) {
|
|
51
|
+
return run(str, code([4], 24));
|
|
52
|
+
}
|
|
53
|
+
export function inverse(str) {
|
|
54
|
+
return run(str, code([7], 27));
|
|
55
|
+
}
|
|
56
|
+
export function hidden(str) {
|
|
57
|
+
return run(str, code([8], 28));
|
|
58
|
+
}
|
|
59
|
+
export function strikethrough(str) {
|
|
60
|
+
return run(str, code([9], 29));
|
|
61
|
+
}
|
|
62
|
+
export function black(str) {
|
|
63
|
+
return run(str, code([30], 39));
|
|
64
|
+
}
|
|
65
|
+
export function red(str) {
|
|
66
|
+
return run(str, code([31], 39));
|
|
67
|
+
}
|
|
68
|
+
export function green(str) {
|
|
69
|
+
return run(str, code([32], 39));
|
|
70
|
+
}
|
|
71
|
+
export function yellow(str) {
|
|
72
|
+
return run(str, code([33], 39));
|
|
73
|
+
}
|
|
74
|
+
export function blue(str) {
|
|
75
|
+
return run(str, code([34], 39));
|
|
76
|
+
}
|
|
77
|
+
export function magenta(str) {
|
|
78
|
+
return run(str, code([35], 39));
|
|
79
|
+
}
|
|
80
|
+
export function cyan(str) {
|
|
81
|
+
return run(str, code([36], 39));
|
|
82
|
+
}
|
|
83
|
+
export function white(str) {
|
|
84
|
+
return run(str, code([37], 39));
|
|
85
|
+
}
|
|
86
|
+
export function gray(str) {
|
|
87
|
+
return brightBlack(str);
|
|
88
|
+
}
|
|
89
|
+
export function brightBlack(str) {
|
|
90
|
+
return run(str, code([90], 39));
|
|
91
|
+
}
|
|
92
|
+
export function brightRed(str) {
|
|
93
|
+
return run(str, code([91], 39));
|
|
94
|
+
}
|
|
95
|
+
export function brightGreen(str) {
|
|
96
|
+
return run(str, code([92], 39));
|
|
97
|
+
}
|
|
98
|
+
export function brightYellow(str) {
|
|
99
|
+
return run(str, code([93], 39));
|
|
100
|
+
}
|
|
101
|
+
export function brightBlue(str) {
|
|
102
|
+
return run(str, code([94], 39));
|
|
103
|
+
}
|
|
104
|
+
export function brightMagenta(str) {
|
|
105
|
+
return run(str, code([95], 39));
|
|
106
|
+
}
|
|
107
|
+
export function brightCyan(str) {
|
|
108
|
+
return run(str, code([96], 39));
|
|
109
|
+
}
|
|
110
|
+
export function brightWhite(str) {
|
|
111
|
+
return run(str, code([97], 39));
|
|
112
|
+
}
|
|
113
|
+
export function bgBlack(str) {
|
|
114
|
+
return run(str, code([40], 49));
|
|
115
|
+
}
|
|
116
|
+
export function bgRed(str) {
|
|
117
|
+
return run(str, code([41], 49));
|
|
118
|
+
}
|
|
119
|
+
export function bgGreen(str) {
|
|
120
|
+
return run(str, code([42], 49));
|
|
121
|
+
}
|
|
122
|
+
export function bgYellow(str) {
|
|
123
|
+
return run(str, code([43], 49));
|
|
124
|
+
}
|
|
125
|
+
export function bgBlue(str) {
|
|
126
|
+
return run(str, code([44], 49));
|
|
127
|
+
}
|
|
128
|
+
export function bgMagenta(str) {
|
|
129
|
+
return run(str, code([45], 49));
|
|
130
|
+
}
|
|
131
|
+
export function bgCyan(str) {
|
|
132
|
+
return run(str, code([46], 49));
|
|
133
|
+
}
|
|
134
|
+
export function bgWhite(str) {
|
|
135
|
+
return run(str, code([47], 49));
|
|
136
|
+
}
|
|
137
|
+
export function bgBrightBlack(str) {
|
|
138
|
+
return run(str, code([100], 49));
|
|
139
|
+
}
|
|
140
|
+
export function bgBrightRed(str) {
|
|
141
|
+
return run(str, code([101], 49));
|
|
142
|
+
}
|
|
143
|
+
export function bgBrightGreen(str) {
|
|
144
|
+
return run(str, code([102], 49));
|
|
145
|
+
}
|
|
146
|
+
export function bgBrightYellow(str) {
|
|
147
|
+
return run(str, code([103], 49));
|
|
148
|
+
}
|
|
149
|
+
export function bgBrightBlue(str) {
|
|
150
|
+
return run(str, code([104], 49));
|
|
151
|
+
}
|
|
152
|
+
export function bgBrightMagenta(str) {
|
|
153
|
+
return run(str, code([105], 49));
|
|
154
|
+
}
|
|
155
|
+
export function bgBrightCyan(str) {
|
|
156
|
+
return run(str, code([106], 49));
|
|
157
|
+
}
|
|
158
|
+
export function bgBrightWhite(str) {
|
|
159
|
+
return run(str, code([107], 49));
|
|
160
|
+
}
|
|
161
|
+
/* Special Color Sequences */
|
|
162
|
+
function clampAndTruncate(n, max = 255, min = 0) {
|
|
163
|
+
return Math.trunc(Math.max(Math.min(n, max), min));
|
|
164
|
+
}
|
|
165
|
+
/** Set text color using paletted 8bit colors.
|
|
166
|
+
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
|
|
167
|
+
export function rgb8(str, color) {
|
|
168
|
+
return run(str, code([38, 5, clampAndTruncate(color)], 39));
|
|
169
|
+
}
|
|
170
|
+
/** Set background color using paletted 8bit colors.
|
|
171
|
+
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
|
|
172
|
+
export function bgRgb8(str, color) {
|
|
173
|
+
return run(str, code([48, 5, clampAndTruncate(color)], 49));
|
|
174
|
+
}
|
|
175
|
+
/** Set text color using 24bit rgb.
|
|
176
|
+
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
177
|
+
* an `Rgb`.
|
|
178
|
+
*
|
|
179
|
+
* To produce the color magenta:
|
|
180
|
+
*
|
|
181
|
+
* rgba24("foo", 0xff00ff);
|
|
182
|
+
* rgba24("foo", {r: 255, g: 0, b: 255});
|
|
183
|
+
*/
|
|
184
|
+
export function rgb24(str, color) {
|
|
185
|
+
if (typeof color === "number") {
|
|
186
|
+
return run(str, code([38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], 39));
|
|
187
|
+
}
|
|
188
|
+
return run(str, code([
|
|
189
|
+
38,
|
|
190
|
+
2,
|
|
191
|
+
clampAndTruncate(color.r),
|
|
192
|
+
clampAndTruncate(color.g),
|
|
193
|
+
clampAndTruncate(color.b),
|
|
194
|
+
], 39));
|
|
195
|
+
}
|
|
196
|
+
/** Set background color using 24bit rgb.
|
|
197
|
+
* `color` can be a number in range `0x000000` to `0xffffff` or
|
|
198
|
+
* an `Rgb`.
|
|
199
|
+
*
|
|
200
|
+
* To produce the color magenta:
|
|
201
|
+
*
|
|
202
|
+
* bgRgba24("foo", 0xff00ff);
|
|
203
|
+
* bgRgba24("foo", {r: 255, g: 0, b: 255});
|
|
204
|
+
*/
|
|
205
|
+
export function bgRgb24(str, color) {
|
|
206
|
+
if (typeof color === "number") {
|
|
207
|
+
return run(str, code([48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], 49));
|
|
208
|
+
}
|
|
209
|
+
return run(str, code([
|
|
210
|
+
48,
|
|
211
|
+
2,
|
|
212
|
+
clampAndTruncate(color.r),
|
|
213
|
+
clampAndTruncate(color.g),
|
|
214
|
+
clampAndTruncate(color.b),
|
|
215
|
+
], 49));
|
|
216
|
+
}
|
|
217
|
+
// https://github.com/chalk/ansi-regex/blob/2b56fb0c7a07108e5b54241e8faec160d393aedb/index.js
|
|
218
|
+
const ANSI_PATTERN = new RegExp([
|
|
219
|
+
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
|
|
220
|
+
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
|
|
221
|
+
].join("|"), "g");
|
|
222
|
+
export function stripColor(string) {
|
|
223
|
+
return string.replace(ANSI_PATTERN, "");
|
|
224
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as dntShim from "../../../../../_dnt.shims.js";
|
|
2
|
+
import { colors } from "./deps.js";
|
|
3
|
+
// @ts-ignore: lib
|
|
4
|
+
const isBrowser = typeof dntShim.Deno === "undefined" &&
|
|
5
|
+
// @ts-ignore: lib
|
|
6
|
+
typeof process === "undefined" && typeof dntShim.dntGlobalThis !== "undefined" &&
|
|
7
|
+
// @ts-ignore: lib
|
|
8
|
+
typeof document !== "undefined";
|
|
9
|
+
export const colorFunctions = [
|
|
10
|
+
(v) => {
|
|
11
|
+
if (isBrowser) {
|
|
12
|
+
return [`%c${v}`, "color: red"];
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return [colors.red(v)];
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
(v) => {
|
|
19
|
+
if (isBrowser) {
|
|
20
|
+
return [`%c${v}`, "color: green"];
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return [colors.green(v)];
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
(v) => {
|
|
27
|
+
if (isBrowser) {
|
|
28
|
+
return [`%c${v}`, "color: yellow"];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return [colors.yellow(v)];
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
(v) => {
|
|
35
|
+
if (isBrowser) {
|
|
36
|
+
return [`%c${v}`, "color: blue"];
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return [colors.blue(v)];
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
(v) => {
|
|
43
|
+
if (isBrowser) {
|
|
44
|
+
return [`%c${v}`, "color: magenta"];
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return [colors.magenta(v)];
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
(v) => {
|
|
51
|
+
if (isBrowser) {
|
|
52
|
+
return [`%c${v}`, "color: cyan"];
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return [colors.cyan(v)];
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
function hashCode(s) {
|
|
60
|
+
let h = 0;
|
|
61
|
+
const l = s.length;
|
|
62
|
+
let i = 0;
|
|
63
|
+
if (l > 0)
|
|
64
|
+
while (i < l)
|
|
65
|
+
h = ((h << 5) - h + s.charCodeAt(i++)) | 0;
|
|
66
|
+
return h;
|
|
67
|
+
}
|
|
68
|
+
export function generateColor(message) {
|
|
69
|
+
const hash = Math.abs(hashCode(message));
|
|
70
|
+
return colorFunctions[hash % colorFunctions.length];
|
|
71
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ColorFunction } from "./colors.js";
|
|
2
|
+
export interface Debug {
|
|
3
|
+
(fmt: string, ...args: unknown[]): void;
|
|
4
|
+
self: Debugger;
|
|
5
|
+
}
|
|
6
|
+
export declare class Debugger {
|
|
7
|
+
manager: DebugManager;
|
|
8
|
+
ns: string;
|
|
9
|
+
color: ColorFunction;
|
|
10
|
+
last: number;
|
|
11
|
+
enabled: boolean;
|
|
12
|
+
constructor(manager: DebugManager, namespace: string);
|
|
13
|
+
log(fmt: string, ...args: unknown[]): void;
|
|
14
|
+
}
|
|
15
|
+
declare class DebugManager {
|
|
16
|
+
debuggers: Map<string, Debugger>;
|
|
17
|
+
enabled: RegExp[];
|
|
18
|
+
constructor(enabled?: RegExp[]);
|
|
19
|
+
}
|
|
20
|
+
export declare function withoutEnv(enabled?: RegExp[] | string): void;
|
|
21
|
+
export declare function debug(namespace: string): Debug;
|
|
22
|
+
export {};
|