@mtkruto/node 0.0.910 → 0.0.921

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.
Files changed (51) hide show
  1. package/esm/client/1_client_abstract.d.ts +2 -1
  2. package/esm/client/1_client_abstract.js +9 -3
  3. package/esm/client/3_client.d.ts +4 -2
  4. package/esm/client/3_client.js +150 -76
  5. package/esm/constants.d.ts +1 -1
  6. package/esm/constants.js +1 -1
  7. package/esm/deps.d.ts +1 -2
  8. package/esm/deps.js +0 -1
  9. package/esm/types/!0_file_id.d.ts +6 -6
  10. package/esm/types/!0_file_id.js +11 -58
  11. package/esm/types/0_chat_photo.d.ts +1 -0
  12. package/esm/types/0_chat_photo.js +4 -2
  13. package/esm/types/0_thumbnail.d.ts +9 -0
  14. package/esm/types/0_thumbnail.js +9 -0
  15. package/esm/types/1_photo.d.ts +11 -0
  16. package/esm/types/1_photo.js +49 -0
  17. package/esm/types/3_message.d.ts +2 -0
  18. package/esm/types/3_message.js +8 -3
  19. package/esm/utilities/0_base64.d.ts +2 -0
  20. package/esm/utilities/0_base64.js +9 -0
  21. package/esm/utilities/0_rle.d.ts +2 -0
  22. package/esm/utilities/0_rle.js +49 -0
  23. package/esm/utilities/0_rle_test.d.ts +1 -0
  24. package/package.json +1 -1
  25. package/script/client/1_client_abstract.d.ts +2 -1
  26. package/script/client/1_client_abstract.js +9 -3
  27. package/script/client/3_client.d.ts +4 -2
  28. package/script/client/3_client.js +149 -75
  29. package/script/constants.d.ts +1 -1
  30. package/script/constants.js +1 -1
  31. package/script/deps.d.ts +1 -2
  32. package/script/deps.js +1 -3
  33. package/script/types/!0_file_id.d.ts +6 -6
  34. package/script/types/!0_file_id.js +11 -58
  35. package/script/types/0_chat_photo.d.ts +1 -0
  36. package/script/types/0_chat_photo.js +4 -2
  37. package/script/types/0_thumbnail.d.ts +9 -0
  38. package/script/types/0_thumbnail.js +13 -0
  39. package/script/types/1_photo.d.ts +11 -0
  40. package/script/types/1_photo.js +76 -0
  41. package/script/types/3_message.d.ts +2 -0
  42. package/script/types/3_message.js +8 -3
  43. package/script/utilities/0_base64.d.ts +2 -0
  44. package/script/utilities/0_base64.js +14 -0
  45. package/script/utilities/0_rle.d.ts +2 -0
  46. package/script/utilities/0_rle.js +54 -0
  47. package/script/utilities/0_rle_test.d.ts +1 -0
  48. package/esm/deps/deno.land/x/q@v0.0.1/mod.d.ts +0 -6
  49. package/esm/deps/deno.land/x/q@v0.0.1/mod.js +0 -71
  50. package/script/deps/deno.land/x/q@v0.0.1/mod.d.ts +0 -6
  51. package/script/deps/deno.land/x/q@v0.0.1/mod.js +0 -75
@@ -2,16 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FileUniqueID = exports.FileUniqueType = exports.FileID = exports.ThumbnailSource = exports.FileType = void 0;
4
4
  // Direct port from Pyrogram
5
- const deps_js_1 = require("../deps.js");
5
+ const _0_base64_js_1 = require("../utilities/0_base64.js");
6
6
  const _0_control_js_1 = require("../utilities/0_control.js");
7
+ const _0_rle_js_1 = require("../utilities/0_rle.js");
7
8
  const _0_tl_raw_writer_js_1 = require("../tl/0_tl_raw_writer.js");
8
9
  const _0_tl_raw_reader_js_1 = require("../tl/0_tl_raw_reader.js");
9
- function base64Encode(data) {
10
- return (0, deps_js_1.base64Encode)(data).replace(/=*$/, "");
11
- }
12
- function base64Decode(data) {
13
- return (0, deps_js_1.base64Decode)(data + "=".repeat(-data.length % 4));
14
- }
15
10
  var FileType;
16
11
  (function (FileType) {
17
12
  FileType[FileType["Thumbnail"] = 0] = "Thumbnail";
@@ -45,48 +40,6 @@ var ThumbnailSource;
45
40
  })(ThumbnailSource = exports.ThumbnailSource || (exports.ThumbnailSource = {}));
46
41
  const WEB_LOCATION_FLAG = 1 << 24;
47
42
  const FILE_REFERENCE_FLAG = 1 << 25;
48
- function rleEncode(s) {
49
- const r = new Array();
50
- let n = 0;
51
- for (const b of s) {
52
- if (!b) {
53
- n++;
54
- }
55
- else {
56
- if (n) {
57
- r.push(0);
58
- r.push(n);
59
- n = 0;
60
- }
61
- r.push(b);
62
- }
63
- }
64
- if (n) {
65
- r.push(0);
66
- r.push(n);
67
- }
68
- return new Uint8Array(r);
69
- }
70
- function rleDecode(s) {
71
- const r = new Array();
72
- let z = false;
73
- for (const b of s) {
74
- if (!b) {
75
- z = true;
76
- continue;
77
- }
78
- if (z) {
79
- for (let i = 0; i < b; i++) {
80
- r.push(0);
81
- }
82
- z = false;
83
- }
84
- else {
85
- r.push(b);
86
- }
87
- }
88
- return new Uint8Array(r);
89
- }
90
43
  class FileID {
91
44
  constructor(major = FileID.MAJOR, minor = FileID.MINOR, fileType, dcId, params) {
92
45
  Object.defineProperty(this, "fileType", {
@@ -134,7 +87,7 @@ class FileID {
134
87
  this.params.thumbnailSize ??= "";
135
88
  }
136
89
  static decode(fileId) {
137
- const decoded = rleDecode(base64Decode(fileId));
90
+ const decoded = (0, _0_rle_js_1.rleDecode)((0, _0_base64_js_1.base64DecodeUrlSafe)(fileId));
138
91
  const major = decoded[decoded.length - 1];
139
92
  let minor;
140
93
  let buffer;
@@ -168,7 +121,7 @@ class FileID {
168
121
  const volumeId = reader.readInt64();
169
122
  let thumbnailSource = 0;
170
123
  if (major >= 4) {
171
- thumbnailSource = reader.readInt32();
124
+ thumbnailSource = reader.readInt32(false);
172
125
  }
173
126
  switch (thumbnailSource) {
174
127
  case ThumbnailSource.Legacy: {
@@ -184,10 +137,10 @@ class FileID {
184
137
  }
185
138
  case ThumbnailSource.ChatPhotoSmall:
186
139
  case ThumbnailSource.ChatPhotoBig: {
187
- const chatId = reader.readInt64();
140
+ const chatId = Number(reader.readInt64());
188
141
  const chatAccessHash = reader.readInt64();
189
142
  const localId = reader.readInt32();
190
- return new FileID(major, minor, fileType, dcId, { fileReference, mediaId, accessHash, volumeId, thumbnailSource, chatId, chatAccessHash, localId });
143
+ return new FileID(major, minor, fileType, dcId, { fileReference, mediaId, accessHash, volumeId, thumbnailSource, chatId: Number(chatId), chatAccessHash, localId });
191
144
  }
192
145
  case ThumbnailSource.StickerSetThumbnail: {
193
146
  const stickerSetId = reader.readInt64();
@@ -235,7 +188,7 @@ class FileID {
235
188
  (0, _0_control_js_1.UNREACHABLE)();
236
189
  }
237
190
  writer.writeInt64(this.params.volumeId);
238
- if (major > 4) {
191
+ if (major >= 4) {
239
192
  writer.writeInt32(Number(this.params.thumbnailSource));
240
193
  }
241
194
  switch (this.params.thumbnailSource) {
@@ -259,7 +212,7 @@ class FileID {
259
212
  if (this.params.chatId == undefined || this.params.chatAccessHash == undefined) {
260
213
  (0, _0_control_js_1.UNREACHABLE)();
261
214
  }
262
- writer.writeInt64(this.params.chatId);
215
+ writer.writeInt64(BigInt(this.params.chatId));
263
216
  writer.writeInt64(this.params.chatAccessHash);
264
217
  writer.writeInt32(this.params.localId);
265
218
  break;
@@ -283,7 +236,7 @@ class FileID {
283
236
  (0, _0_control_js_1.UNREACHABLE)();
284
237
  }
285
238
  writer.write(new Uint8Array([minor, major]));
286
- return base64Encode(rleEncode(writer.buffer));
239
+ return (0, _0_base64_js_1.base64EncodeUrlSafe)((0, _0_rle_js_1.rleEncode)(writer.buffer));
287
240
  }
288
241
  }
289
242
  Object.defineProperty(FileID, "MAJOR", {
@@ -324,7 +277,7 @@ class FileUniqueID {
324
277
  });
325
278
  }
326
279
  static decode(fileId) {
327
- const reader = new _0_tl_raw_reader_js_1.TLRawReader(rleDecode(base64Decode(fileId)));
280
+ const reader = new _0_tl_raw_reader_js_1.TLRawReader((0, _0_rle_js_1.rleDecode)((0, _0_base64_js_1.base64DecodeUrlSafe)(fileId)));
328
281
  const fileUniqueType = reader.readInt32();
329
282
  switch (fileUniqueType) {
330
283
  case FileUniqueType.Web: {
@@ -370,7 +323,7 @@ class FileUniqueID {
370
323
  default:
371
324
  (0, _0_control_js_1.UNREACHABLE)();
372
325
  }
373
- return base64Encode(rleEncode(writer.buffer));
326
+ return (0, _0_base64_js_1.base64EncodeUrlSafe)((0, _0_rle_js_1.rleEncode)(writer.buffer));
374
327
  }
375
328
  }
376
329
  exports.FileUniqueID = FileUniqueID;
@@ -5,6 +5,7 @@ export declare namespace ChatPhoto {
5
5
  smallFileUniqueId: string;
6
6
  bigFileId: string;
7
7
  bigFileUniqueId: string;
8
+ hasVideo: boolean;
8
9
  }
9
10
  interface User extends Base {
10
11
  personal: boolean;
@@ -30,7 +30,7 @@ function constructChatPhoto(photo, chatId, chatAccessHash) {
30
30
  const smallFileId = new _0_file_id_js_1.FileID(null, null, _0_file_id_js_1.FileType.ChatPhoto, photo.dcId, {
31
31
  mediaId: photo.photoId,
32
32
  thumbnailSource: _0_file_id_js_1.ThumbnailSource.ChatPhotoSmall,
33
- chatId: BigInt(chatId),
33
+ chatId,
34
34
  chatAccessHash,
35
35
  accessHash: 0n,
36
36
  volumeId: 0n,
@@ -40,7 +40,7 @@ function constructChatPhoto(photo, chatId, chatAccessHash) {
40
40
  const bigFileId = new _0_file_id_js_1.FileID(null, null, _0_file_id_js_1.FileType.ChatPhoto, photo.dcId, {
41
41
  mediaId: photo.photoId,
42
42
  thumbnailSource: _0_file_id_js_1.ThumbnailSource.ChatPhotoBig,
43
- chatId: BigInt(chatId),
43
+ chatId,
44
44
  chatAccessHash,
45
45
  accessHash: 0n,
46
46
  volumeId: 0n,
@@ -53,6 +53,7 @@ function constructChatPhoto(photo, chatId, chatAccessHash) {
53
53
  smallFileUniqueId,
54
54
  bigFileId,
55
55
  bigFileUniqueId,
56
+ hasVideo: photo.hasVideo || false,
56
57
  };
57
58
  }
58
59
  else {
@@ -62,6 +63,7 @@ function constructChatPhoto(photo, chatId, chatAccessHash) {
62
63
  smallFileUniqueId,
63
64
  bigFileId,
64
65
  bigFileUniqueId,
66
+ hasVideo: photo.hasVideo || false,
65
67
  };
66
68
  }
67
69
  }
@@ -0,0 +1,9 @@
1
+ import * as types from "../tl/2_types.js";
2
+ export interface Thumbnail {
3
+ fileId: string;
4
+ fileUniqueId: string;
5
+ width: number;
6
+ height: number;
7
+ fileSize: number;
8
+ }
9
+ export declare function constructThumbnail(size: types.PhotoSize, fileId: string, fileUniqueId: string): Thumbnail;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.constructThumbnail = void 0;
4
+ function constructThumbnail(size, fileId, fileUniqueId) {
5
+ return {
6
+ fileId,
7
+ fileUniqueId,
8
+ width: size.w,
9
+ height: size.h,
10
+ fileSize: size.size,
11
+ };
12
+ }
13
+ exports.constructThumbnail = constructThumbnail;
@@ -0,0 +1,11 @@
1
+ import * as types from "../tl/2_types.js";
2
+ import { Thumbnail } from "./0_thumbnail.js";
3
+ export interface Photo {
4
+ fileId: string;
5
+ fileUniqueId: string;
6
+ width: number;
7
+ height: number;
8
+ fileSize: number;
9
+ thumbnails: Thumbnail[];
10
+ }
11
+ export declare function constructPhoto(photo: types.Photo): Photo;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.constructPhoto = void 0;
27
+ const _1_tl_object_js_1 = require("../tl/1_tl_object.js");
28
+ const types = __importStar(require("../tl/2_types.js"));
29
+ const _0_file_id_js_1 = require("./!0_file_id.js");
30
+ const _0_thumbnail_js_1 = require("./0_thumbnail.js");
31
+ function constructPhoto(photo) {
32
+ const sizes = photo.sizes
33
+ .map((v) => {
34
+ if (v instanceof types.PhotoSizeProgressive) {
35
+ return new types.PhotoSize({ type: v.type, w: v.w, h: v.h, size: Math.max(...v.sizes) });
36
+ }
37
+ else {
38
+ return v;
39
+ }
40
+ })
41
+ .map((v) => v[_1_tl_object_js_1.as](types.PhotoSize))
42
+ .sort((a, b) => a.size - b.size);
43
+ const largest = sizes.slice(-1)[0];
44
+ const { dcId, id: mediaId, accessHash, fileReference } = photo;
45
+ const fileUniqueId = new _0_file_id_js_1.FileUniqueID(_0_file_id_js_1.FileUniqueType.Document, { mediaId: photo.id }).encode();
46
+ return {
47
+ fileId: new _0_file_id_js_1.FileID(null, null, _0_file_id_js_1.FileType.Photo, dcId, {
48
+ mediaId,
49
+ accessHash,
50
+ fileReference,
51
+ thumbnailSource: _0_file_id_js_1.ThumbnailSource.Thumbnail,
52
+ thumbnailFileType: _0_file_id_js_1.FileType.Photo,
53
+ thumbnailSize: largest.type,
54
+ volumeId: 0n,
55
+ localId: 0,
56
+ }).encode(),
57
+ fileUniqueId,
58
+ width: largest.w,
59
+ height: largest.h,
60
+ fileSize: largest.size,
61
+ thumbnails: sizes.slice(0, -1).map((v) => {
62
+ const fileId = new _0_file_id_js_1.FileID(null, null, _0_file_id_js_1.FileType.Photo, dcId, {
63
+ mediaId,
64
+ accessHash,
65
+ fileReference,
66
+ thumbnailSource: _0_file_id_js_1.ThumbnailSource.Thumbnail,
67
+ thumbnailFileType: _0_file_id_js_1.FileType.Photo,
68
+ thumbnailSize: v.type,
69
+ volumeId: 0n,
70
+ localId: 0,
71
+ }).encode();
72
+ return (0, _0_thumbnail_js_1.constructThumbnail)(v, fileId, fileUniqueId);
73
+ }),
74
+ };
75
+ }
76
+ exports.constructPhoto = constructPhoto;
@@ -7,6 +7,7 @@ import { Chat } from "./1_chat.js";
7
7
  import { User } from "./1_user.js";
8
8
  import { InlineKeyboardMarkup } from "./2_inline_keyboard_markup.js";
9
9
  import { ReplyKeyboardMarkup } from "./2_reply_keyboard_markup.js";
10
+ import { Photo } from "./1_photo.js";
10
11
  export interface Message {
11
12
  id: number;
12
13
  threadId?: number;
@@ -35,6 +36,7 @@ export interface Message {
35
36
  hasMediaSpoiler?: boolean;
36
37
  views?: number;
37
38
  replyMarkup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
39
+ photo?: Photo;
38
40
  }
39
41
  export declare function constructMessage(message_: types.Message, getEntity: {
40
42
  (peer: types.PeerUser): MaybePromise<types.User | null>;
@@ -35,6 +35,7 @@ const _1_chat_js_1 = require("./1_chat.js");
35
35
  const _1_user_js_1 = require("./1_user.js");
36
36
  const _2_inline_keyboard_markup_js_1 = require("./2_inline_keyboard_markup.js");
37
37
  const _2_reply_keyboard_markup_js_1 = require("./2_reply_keyboard_markup.js");
38
+ const _1_photo_js_1 = require("./1_photo.js");
38
39
  const d = (0, deps_js_1.debug)("types/Message");
39
40
  async function constructMessage(message_, getEntity, getMessage) {
40
41
  let chat_ = null;
@@ -99,9 +100,6 @@ async function constructMessage(message_, getEntity, getMessage) {
99
100
  (0, _0_control_js_1.UNREACHABLE)();
100
101
  }
101
102
  }
102
- else {
103
- (0, _0_control_js_1.UNREACHABLE)();
104
- }
105
103
  if (message_.message) {
106
104
  if (message_.media == undefined) {
107
105
  message.text = message_.message;
@@ -202,6 +200,13 @@ async function constructMessage(message_, getEntity, getMessage) {
202
200
  (0, _0_control_js_1.UNREACHABLE)();
203
201
  }
204
202
  }
203
+ if (message_.media) {
204
+ if (message_.media instanceof types.MessageMediaPhoto) {
205
+ if (message_.media.photo instanceof types.Photo) {
206
+ message.photo = (0, _1_photo_js_1.constructPhoto)(message_.media.photo);
207
+ }
208
+ }
209
+ }
205
210
  return (0, _0_object_js_1.cleanObject)(message);
206
211
  }
207
212
  exports.constructMessage = constructMessage;
@@ -0,0 +1,2 @@
1
+ export declare function base64EncodeUrlSafe(data: ArrayBuffer | string): string;
2
+ export declare function base64DecodeUrlSafe(data: string): Uint8Array;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.base64DecodeUrlSafe = exports.base64EncodeUrlSafe = void 0;
4
+ const deps_js_1 = require("../deps.js");
5
+ // TODO: test
6
+ function base64EncodeUrlSafe(data) {
7
+ return (0, deps_js_1.base64Encode)(data).replace(/=*$/, "").replaceAll("+", "-").replaceAll("/", "_");
8
+ }
9
+ exports.base64EncodeUrlSafe = base64EncodeUrlSafe;
10
+ function base64DecodeUrlSafe(data) {
11
+ data = data.replaceAll("_", "/").replaceAll("-", "+");
12
+ return (0, deps_js_1.base64Decode)(data + "=".repeat(data.length % 4));
13
+ }
14
+ exports.base64DecodeUrlSafe = base64DecodeUrlSafe;
@@ -0,0 +1,2 @@
1
+ export declare function rleEncode(s: Uint8Array): Uint8Array;
2
+ export declare function rleDecode(s: Uint8Array): Uint8Array;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rleDecode = exports.rleEncode = void 0;
4
+ function rleEncode(s) {
5
+ const r = new Array();
6
+ let n = 0;
7
+ for (const b of s) {
8
+ if (!b) {
9
+ if (n == 255) {
10
+ r.push(0);
11
+ r.push(n);
12
+ n = 1;
13
+ }
14
+ else {
15
+ n++;
16
+ }
17
+ }
18
+ else {
19
+ if (n) {
20
+ r.push(0);
21
+ r.push(n);
22
+ n = 0;
23
+ }
24
+ r.push(b);
25
+ }
26
+ }
27
+ if (n) {
28
+ r.push(0);
29
+ r.push(n);
30
+ }
31
+ return new Uint8Array(r);
32
+ }
33
+ exports.rleEncode = rleEncode;
34
+ function rleDecode(s) {
35
+ const r = new Array();
36
+ let z = false;
37
+ for (const b of s) {
38
+ if (!b) {
39
+ z = true;
40
+ continue;
41
+ }
42
+ if (z) {
43
+ for (let i = 0; i < b; i++) {
44
+ r.push(0);
45
+ }
46
+ z = false;
47
+ }
48
+ else {
49
+ r.push(b);
50
+ }
51
+ }
52
+ return new Uint8Array(r);
53
+ }
54
+ exports.rleDecode = rleDecode;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +0,0 @@
1
- export declare function queue<T, R>(runner: (task: T) => R, concurrency: number): {
2
- push: (task: T | T[]) => void;
3
- drain: (fn?: any) => void | Promise<any>;
4
- onError: (fn: (task: T, error: Error) => any) => (task: T, error: Error) => any;
5
- onDone: (fn: (task: T, result: R) => any) => (task: T, result: R) => any;
6
- };
@@ -1,71 +0,0 @@
1
- export function queue(runner, concurrency) {
2
- if (concurrency <= 0) {
3
- throw new RangeError('concurrency can only be a positive integer');
4
- }
5
- if (!Number.isInteger(concurrency)) {
6
- throw new RangeError('concurrency can only be a positive integer');
7
- }
8
- const tasks = [];
9
- let running = 0;
10
- let drains = [];
11
- let onError = () => { };
12
- let onDone = () => { };
13
- let drainAwaiters = [];
14
- function drained() {
15
- drains.forEach((d) => d());
16
- drains = [];
17
- drainAwaiters.forEach((s) => s());
18
- drainAwaiters = [];
19
- }
20
- async function runNext() {
21
- if (running < concurrency && tasks.length > 0) {
22
- running++;
23
- const task = tasks.shift();
24
- if (task) {
25
- try {
26
- const result = await runner(task);
27
- onDone(task, result);
28
- }
29
- catch (err) {
30
- onError(task, err);
31
- }
32
- }
33
- running--;
34
- if (tasks.length > 0) {
35
- queueMicrotask(runNext); // to prevent "Maximum call stack size exceeded"
36
- }
37
- else if (running === 0) {
38
- drained();
39
- }
40
- }
41
- }
42
- return {
43
- push: (task) => {
44
- let needed = 1;
45
- if (Array.isArray(task)) {
46
- tasks.push(...task);
47
- needed = task.length;
48
- }
49
- else {
50
- tasks.push(task);
51
- }
52
- const free = concurrency - running;
53
- for (let i = 0; i < Math.max(free, needed); i++) {
54
- runNext();
55
- }
56
- },
57
- drain: (fn) => {
58
- if (fn) {
59
- drains.push(fn);
60
- }
61
- else if (tasks.length > 0 || running > 0) {
62
- return new Promise((resolve) => drainAwaiters.push(resolve));
63
- }
64
- else {
65
- return Promise.resolve();
66
- }
67
- },
68
- onError: (fn) => (onError = fn),
69
- onDone: (fn) => (onDone = fn),
70
- };
71
- }
@@ -1,6 +0,0 @@
1
- export declare function queue<T, R>(runner: (task: T) => R, concurrency: number): {
2
- push: (task: T | T[]) => void;
3
- drain: (fn?: any) => void | Promise<any>;
4
- onError: (fn: (task: T, error: Error) => any) => (task: T, error: Error) => any;
5
- onDone: (fn: (task: T, result: R) => any) => (task: T, result: R) => any;
6
- };
@@ -1,75 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.queue = void 0;
4
- function queue(runner, concurrency) {
5
- if (concurrency <= 0) {
6
- throw new RangeError('concurrency can only be a positive integer');
7
- }
8
- if (!Number.isInteger(concurrency)) {
9
- throw new RangeError('concurrency can only be a positive integer');
10
- }
11
- const tasks = [];
12
- let running = 0;
13
- let drains = [];
14
- let onError = () => { };
15
- let onDone = () => { };
16
- let drainAwaiters = [];
17
- function drained() {
18
- drains.forEach((d) => d());
19
- drains = [];
20
- drainAwaiters.forEach((s) => s());
21
- drainAwaiters = [];
22
- }
23
- async function runNext() {
24
- if (running < concurrency && tasks.length > 0) {
25
- running++;
26
- const task = tasks.shift();
27
- if (task) {
28
- try {
29
- const result = await runner(task);
30
- onDone(task, result);
31
- }
32
- catch (err) {
33
- onError(task, err);
34
- }
35
- }
36
- running--;
37
- if (tasks.length > 0) {
38
- queueMicrotask(runNext); // to prevent "Maximum call stack size exceeded"
39
- }
40
- else if (running === 0) {
41
- drained();
42
- }
43
- }
44
- }
45
- return {
46
- push: (task) => {
47
- let needed = 1;
48
- if (Array.isArray(task)) {
49
- tasks.push(...task);
50
- needed = task.length;
51
- }
52
- else {
53
- tasks.push(task);
54
- }
55
- const free = concurrency - running;
56
- for (let i = 0; i < Math.max(free, needed); i++) {
57
- runNext();
58
- }
59
- },
60
- drain: (fn) => {
61
- if (fn) {
62
- drains.push(fn);
63
- }
64
- else if (tasks.length > 0 || running > 0) {
65
- return new Promise((resolve) => drainAwaiters.push(resolve));
66
- }
67
- else {
68
- return Promise.resolve();
69
- }
70
- },
71
- onError: (fn) => (onError = fn),
72
- onDone: (fn) => (onDone = fn),
73
- };
74
- }
75
- exports.queue = queue;