File size: 9,047 Bytes
813eca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import os from "os";
import path from "path";
import crypto from "crypto";
import { Readable, Writable } from "stream";

import "colors";
import mime from "mime";
import axios from "axios";
import fs from "fs-extra";
import { v1 as uuid } from "uuid";
import { format as dateFormat } from "date-fns";
import CRC32 from "crc-32";
import randomstring from "randomstring";
import _ from "lodash";
import { CronJob } from "cron";

import HTTP_STATUS_CODE from "./http-status-codes.ts";

const autoIdMap = new Map();

const util = {
  is2DArrays(value: any) {
    return (
      _.isArray(value) &&
      (!value[0] || (_.isArray(value[0]) && _.isArray(value[value.length - 1])))
    );
  },

  uuid: (separator = true) => (separator ? uuid() : uuid().replace(/\-/g, "")),

  autoId: (prefix = "") => {
    let index = autoIdMap.get(prefix);
    if (index > 999999) index = 0; //超过最大数字则重置为0
    autoIdMap.set(prefix, (index || 0) + 1);
    return `${prefix}${index || 1}`;
  },

  ignoreJSONParse(value: string) {
    const result = _.attempt(() => JSON.parse(value));
    if (_.isError(result)) return null;
    return result;
  },

  generateRandomString(options: any): string {
    return randomstring.generate(options);
  },

  getResponseContentType(value: any): string | null {
    return value.headers
      ? value.headers["content-type"] || value.headers["Content-Type"]
      : null;
  },

  mimeToExtension(value: string) {
    let extension = mime.getExtension(value);
    if (extension == "mpga") return "mp3";
    return extension;
  },

  extractURLExtension(value: string) {
    const extname = path.extname(new URL(value).pathname);
    return extname.substring(1).toLowerCase();
  },

  createCronJob(cronPatterns: any, callback?: Function) {
    if (!_.isFunction(callback))
      throw new Error("callback must be an Function");
    return new CronJob(
      cronPatterns,
      () => callback(),
      null,
      false,
      "Asia/Shanghai"
    );
  },

  getDateString(format = "yyyy-MM-dd", date = new Date()) {
    return dateFormat(date, format);
  },

  getIPAddressesByIPv4(): string[] {
    const interfaces = os.networkInterfaces();
    const addresses = [];
    for (let name in interfaces) {
      const networks = interfaces[name];
      const results = networks.filter(
        (network) =>
          network.family === "IPv4" &&
          network.address !== "127.0.0.1" &&
          !network.internal
      );
      if (results[0] && results[0].address) addresses.push(results[0].address);
    }
    return addresses;
  },

  getMACAddressesByIPv4(): string[] {
    const interfaces = os.networkInterfaces();
    const addresses = [];
    for (let name in interfaces) {
      const networks = interfaces[name];
      const results = networks.filter(
        (network) =>
          network.family === "IPv4" &&
          network.address !== "127.0.0.1" &&
          !network.internal
      );
      if (results[0] && results[0].mac) addresses.push(results[0].mac);
    }
    return addresses;
  },

  generateSSEData(event?: string, data?: string, retry?: number) {
    return `event: ${event || "message"}\ndata: ${(data || "")
      .replace(/\n/g, "\\n")
      .replace(/\s/g, "\\s")}\nretry: ${retry || 3000}\n\n`;
  },

  buildDataBASE64(type, ext, buffer) {
    return `data:${type}/${ext.replace("jpg", "jpeg")};base64,${buffer.toString(
      "base64"
    )}`;
  },

  isLinux() {
    return os.platform() !== "win32";
  },

  isIPAddress(value) {
    return (
      _.isString(value) &&
      (/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(
        value
      ) ||
        /\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*/.test(
          value
        ))
    );
  },

  isPort(value) {
    return _.isNumber(value) && value > 0 && value < 65536;
  },

  isReadStream(value): boolean {
    return (
      value &&
      (value instanceof Readable || "readable" in value || value.readable)
    );
  },

  isWriteStream(value): boolean {
    return (
      value &&
      (value instanceof Writable || "writable" in value || value.writable)
    );
  },

  isHttpStatusCode(value) {
    return _.isNumber(value) && Object.values(HTTP_STATUS_CODE).includes(value);
  },

  isURL(value) {
    return !_.isUndefined(value) && /^(http|https)/.test(value);
  },

  isSrc(value) {
    return !_.isUndefined(value) && /^\/.+\.[0-9a-zA-Z]+(\?.+)?$/.test(value);
  },

  isBASE64(value) {
    return !_.isUndefined(value) && /^[a-zA-Z0-9\/\+]+(=?)+$/.test(value);
  },

  isBASE64Data(value) {
    return /^data:/.test(value);
  },

  extractBASE64DataFormat(value): string | null {
    const match = value.trim().match(/^data:(.+);base64,/);
    if (!match) return null;
    return match[1];
  },

  removeBASE64DataHeader(value): string {
    return value.replace(/^data:(.+);base64,/, "");
  },

  isDataString(value): boolean {
    return /^(base64|json):/.test(value);
  },

  isStringNumber(value) {
    return _.isFinite(Number(value));
  },

  isUnixTimestamp(value) {
    return /^[0-9]{10}$/.test(`${value}`);
  },

  isTimestamp(value) {
    return /^[0-9]{13}$/.test(`${value}`);
  },

  isEmail(value) {
    return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(
      value
    );
  },

  isAsyncFunction(value) {
    return Object.prototype.toString.call(value) === "[object AsyncFunction]";
  },

  async isAPNG(filePath) {
    let head;
    const readStream = fs.createReadStream(filePath, { start: 37, end: 40 });
    const readPromise = new Promise((resolve, reject) => {
      readStream.once("end", resolve);
      readStream.once("error", reject);
    });
    readStream.once("data", (data) => (head = data));
    await readPromise;
    return head.compare(Buffer.from([0x61, 0x63, 0x54, 0x4c])) === 0;
  },

  unixTimestamp() {
    return parseInt(`${Date.now() / 1000}`);
  },

  timestamp() {
    return Date.now();
  },

  urlJoin(...values) {
    let url = "";
    for (let i = 0; i < values.length; i++)
      url += `${i > 0 ? "/" : ""}${values[i]
        .replace(/^\/*/, "")
        .replace(/\/*$/, "")}`;
    return url;
  },

  millisecondsToHmss(milliseconds) {
    if (_.isString(milliseconds)) return milliseconds;
    milliseconds = parseInt(milliseconds);
    const sec = Math.floor(milliseconds / 1000);
    const hours = Math.floor(sec / 3600);
    const minutes = Math.floor((sec - hours * 3600) / 60);
    const seconds = sec - hours * 3600 - minutes * 60;
    const ms = (milliseconds % 60000) - seconds * 1000;
    return `${hours > 9 ? hours : "0" + hours}:${
      minutes > 9 ? minutes : "0" + minutes
    }:${seconds > 9 ? seconds : "0" + seconds}.${ms}`;
  },

  millisecondsToTimeString(milliseconds) {
    if (milliseconds < 1000) return `${milliseconds}ms`;
    if (milliseconds < 60000)
      return `${parseFloat((milliseconds / 1000).toFixed(2))}s`;
    return `${Math.floor(milliseconds / 1000 / 60)}m${Math.floor(
      (milliseconds / 1000) % 60
    )}s`;
  },

  rgbToHex(r, g, b): string {
    return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  },

  hexToRgb(hex) {
    const value = parseInt(hex.replace(/^#/, ""), 16);
    return [(value >> 16) & 255, (value >> 8) & 255, value & 255];
  },

  md5(value) {
    return crypto.createHash("md5").update(value).digest("hex");
  },

  crc32(value) {
    return _.isBuffer(value) ? CRC32.buf(value) : CRC32.str(value);
  },

  arrayParse(value): any[] {
    return _.isArray(value) ? value : [value];
  },

  booleanParse(value) {
    return value === "true" || value === true ? true : false;
  },

  encodeBASE64(value) {
    return Buffer.from(value).toString("base64");
  },

  decodeBASE64(value) {
    return Buffer.from(value, "base64").toString();
  },

  async fetchFileBASE64(url: string) {
    const result = await axios.get(url, {
      responseType: "arraybuffer",
    });
    return result.data.toString("base64");
  },
};

export default util;