steamutils 1.5.5 → 1.5.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils.js +61 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "steamutils",
3
- "version": "1.5.05",
3
+ "version": "1.5.07",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "alpha-common-utils": "^1.0.6",
package/utils.js CHANGED
@@ -3,6 +3,10 @@ import SteamTotp from "steam-totp";
3
3
  import { EAuthTokenPlatformType } from "./const.js";
4
4
  import fs from "fs";
5
5
  import axios from "axios";
6
+ import readline from "readline";
7
+ import https from "https";
8
+ import { promisify } from "util";
9
+ import { pipeline } from "stream";
6
10
 
7
11
  const isBrowser = typeof window !== "undefined";
8
12
  const g_rgCurrencyData = {
@@ -1048,3 +1052,60 @@ export async function downloadImage(url, filePath) {
1048
1052
  });
1049
1053
  });
1050
1054
  }
1055
+
1056
+ export async function downloadLargeImage(url, filePath) {
1057
+ try {
1058
+ const response = await new Promise((resolve, reject) => {
1059
+ https
1060
+ .get(url, (res) => {
1061
+ if (res.statusCode === 200) {
1062
+ resolve(res);
1063
+ } else {
1064
+ reject(new Error(`Failed to get image, status code: ${res.statusCode}`));
1065
+ }
1066
+ })
1067
+ .on("error", reject);
1068
+ });
1069
+
1070
+ const streamPipeline = promisify(pipeline);
1071
+ await streamPipeline(response, fs.createWriteStream(filePath));
1072
+ console.log("Download large image completed.");
1073
+ return true;
1074
+ } catch (error) {
1075
+ console.error(`Error downloading large image: ${error.message}`);
1076
+ return false;
1077
+ }
1078
+ }
1079
+
1080
+ //from large text file
1081
+ export function readRandomLine(filePath) {
1082
+ return new Promise((resolve, reject) => {
1083
+ const rl = readline.createInterface({
1084
+ input: fs.createReadStream(filePath),
1085
+ crlfDelay: Infinity,
1086
+ });
1087
+
1088
+ let resultLine = null;
1089
+ let lineNumber = 0;
1090
+
1091
+ rl.on("line", (line) => {
1092
+ lineNumber += 1;
1093
+ // Choose this line with probability 1 / lineNumber
1094
+ if (Math.random() < 1 / lineNumber) {
1095
+ resultLine = line;
1096
+ }
1097
+ });
1098
+
1099
+ rl.on("close", () => {
1100
+ if (resultLine !== null) {
1101
+ resolve(resultLine);
1102
+ } else {
1103
+ reject(new Error("File is empty"));
1104
+ }
1105
+ });
1106
+
1107
+ rl.on("error", (err) => {
1108
+ reject(err);
1109
+ });
1110
+ });
1111
+ }