@wzyjs/utils 0.3.8 → 0.3.10

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/node.esm.js CHANGED
@@ -9420,7 +9420,20 @@ import * as cheerio from "cheerio";
9420
9420
  import { default as default3 } from "axios";
9421
9421
  import { default as default4 } from "json5";
9422
9422
  import { default as default5 } from "consola";
9423
- import { default as default6, DataStoreOptions } from "nedb";
9423
+
9424
+ // src/common/ai/index.ts
9425
+ var exports_ai = {};
9426
+ __export(exports_ai, {
9427
+ video: () => exports_video,
9428
+ text: () => exports_text,
9429
+ ai302: () => exports_302
9430
+ });
9431
+
9432
+ // src/common/ai/302/index.ts
9433
+ var exports_302 = {};
9434
+ __export(exports_302, {
9435
+ chat: () => chat
9436
+ });
9424
9437
 
9425
9438
  // src/common/ai/302/axios.ts
9426
9439
  import axios_ from "axios";
@@ -9460,12 +9473,25 @@ var chat = async (message, model = "gpt-4.1", isParse = true) => {
9460
9473
  throw new Error(error?.response?.statusText || error?.message || "未知原因");
9461
9474
  }
9462
9475
  };
9476
+ // src/common/ai/video/index.ts
9477
+ var exports_video = {};
9478
+ __export(exports_video, {
9479
+ keling: () => exports_keling
9480
+ });
9463
9481
 
9464
- // src/common/ai/keling/utils.ts
9482
+ // src/common/ai/video/keling/index.ts
9483
+ var exports_keling = {};
9484
+ __export(exports_keling, {
9485
+ setCookie: () => setCookie,
9486
+ getVideoStatus: () => getVideoStatus,
9487
+ generateVideo: () => generateVideo
9488
+ });
9489
+
9490
+ // src/common/ai/video/keling/utils.ts
9465
9491
  import * as url from "url";
9466
9492
  import * as path from "path";
9467
9493
 
9468
- // src/common/ai/keling/axios.ts
9494
+ // src/common/ai/video/keling/axios.ts
9469
9495
  import axios_2 from "axios";
9470
9496
  var axios2 = axios_2.create({
9471
9497
  headers: {
@@ -9474,7 +9500,7 @@ var axios2 = axios_2.create({
9474
9500
  }
9475
9501
  });
9476
9502
 
9477
- // src/common/ai/keling/utils.ts
9503
+ // src/common/ai/video/keling/utils.ts
9478
9504
  var getToken = async (filename) => {
9479
9505
  try {
9480
9506
  const response = await axios2.get("https://klingai.kuaishou.com/api/upload/issue/token", {
@@ -9647,7 +9673,7 @@ var setCookie = (newCookieValue) => {
9647
9673
  axios2.defaults.headers.Cookie = newCookieValue;
9648
9674
  };
9649
9675
 
9650
- // src/common/ai/keling/index.ts
9676
+ // src/common/ai/video/keling/index.ts
9651
9677
  var getVideoStatus = async (taskId) => {
9652
9678
  if (!taskId) {
9653
9679
  return;
@@ -9674,6 +9700,48 @@ var generateVideo = async (imageUrl, prompt) => {
9674
9700
  console.log(666, "任务 id", taskId);
9675
9701
  return taskId;
9676
9702
  };
9703
+ // src/common/ai/text/index.ts
9704
+ var exports_text = {};
9705
+ __export(exports_text, {
9706
+ complete: () => complete
9707
+ });
9708
+ import json52 from "json5";
9709
+ var complete = async (apiUrl, model, token, messages, json = true) => {
9710
+ try {
9711
+ const response = await fetch(apiUrl, {
9712
+ method: "POST",
9713
+ headers: {
9714
+ "Content-Type": "application/json",
9715
+ Authorization: `Bearer ${token}`
9716
+ },
9717
+ body: JSON.stringify({
9718
+ model,
9719
+ messages,
9720
+ stream: false
9721
+ })
9722
+ });
9723
+ if (!response.ok) {
9724
+ const errorText = await response.text();
9725
+ throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`);
9726
+ }
9727
+ const data = await response.json();
9728
+ const content = data?.choices?.[0]?.message?.content || data?.output || "";
9729
+ if (!content) {
9730
+ throw new Error("No content in response");
9731
+ }
9732
+ if (json) {
9733
+ const match = /```(json)?(.*)```/s.exec(content);
9734
+ if (match && match[2]) {
9735
+ return json52.parse(match[2]);
9736
+ }
9737
+ return json52.parse(content);
9738
+ }
9739
+ return content;
9740
+ } catch (error) {
9741
+ console.error("AI completion error:", error);
9742
+ throw error;
9743
+ }
9744
+ };
9677
9745
  // src/common/base/string.ts
9678
9746
  var getChineseByStr = (str) => {
9679
9747
  if (!str) {
@@ -12426,7 +12494,6 @@ import isSameOrAfter from "dayjs/plugin/isSameOrAfter";
12426
12494
  import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
12427
12495
  import utc from "dayjs/plugin/utc";
12428
12496
  import"dayjs/locale/zh-cn";
12429
- import { Dayjs } from "dayjs";
12430
12497
  dayjs.extend(utc);
12431
12498
  dayjs.extend(isBetween);
12432
12499
  dayjs.extend(weekday);
@@ -12454,7 +12521,148 @@ var initChinaDayjs = () => {
12454
12521
  };
12455
12522
  var chinaDayjs = initChinaDayjs();
12456
12523
  var dayjs_default = dayjs;
12457
- // src/node/mail.ts
12524
+ // src/node/oss/index.ts
12525
+ var exports_oss = {};
12526
+ __export(exports_oss, {
12527
+ cf: () => exports_cloudflare,
12528
+ ali: () => exports_ali
12529
+ });
12530
+
12531
+ // src/node/oss/ali.ts
12532
+ var exports_ali = {};
12533
+ __export(exports_ali, {
12534
+ uploadFileToOss: () => uploadFileToOss
12535
+ });
12536
+ import OSS from "ali-oss";
12537
+ var oss = null;
12538
+ var getOssClient = () => {
12539
+ if (!oss) {
12540
+ oss = new OSS({
12541
+ region: process.env.ALI_OSS_REGION,
12542
+ accessKeyId: process.env.ALI_OSS_ACCESS_KEY_ID,
12543
+ accessKeySecret: process.env.ALI_OSS_ACCESS_KEY_SECRET,
12544
+ bucket: process.env.ALI_OSS_BUCKET
12545
+ });
12546
+ }
12547
+ return oss;
12548
+ };
12549
+ var uploadFileToOss = async (params) => {
12550
+ const { buffer, fileName } = params;
12551
+ if (!buffer) {
12552
+ throw new Error("Missing required parameters: buffer");
12553
+ }
12554
+ if (!fileName) {
12555
+ throw new Error("Missing required parameters: fileName");
12556
+ }
12557
+ const client = getOssClient();
12558
+ try {
12559
+ const { url: url2 } = await client.put(decodeURIComponent(fileName), buffer, {
12560
+ headers: {
12561
+ "Cache-Control": "public, max-age=31536000, immutable"
12562
+ }
12563
+ });
12564
+ return {
12565
+ url: url2,
12566
+ objectKey: fileName
12567
+ };
12568
+ } catch (error) {
12569
+ throw new Error(`上传到 OSS 失败: ${error}`);
12570
+ }
12571
+ };
12572
+ // src/node/oss/cloudflare.ts
12573
+ var exports_cloudflare = {};
12574
+ __export(exports_cloudflare, {
12575
+ uploadFile: () => uploadFile,
12576
+ deleteFile: () => deleteFile
12577
+ });
12578
+ import { S3Client, PutObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
12579
+ var s3Client = null;
12580
+ var getS3Client = () => {
12581
+ if (!s3Client) {
12582
+ s3Client = new S3Client({
12583
+ region: "auto",
12584
+ endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
12585
+ credentials: {
12586
+ accessKeyId: process.env.CLOUDFLARE_ACCESS_KEY_ID,
12587
+ secretAccessKey: process.env.CLOUDFLARE_SECRET_ACCESS_KEY
12588
+ }
12589
+ });
12590
+ }
12591
+ return s3Client;
12592
+ };
12593
+ async function uploadFile(params) {
12594
+ const { buffer, mimeType, fileName } = params;
12595
+ if (!buffer) {
12596
+ throw new Error("Missing required parameters: buffer");
12597
+ }
12598
+ if (!fileName) {
12599
+ throw new Error("Missing required parameters: fileName");
12600
+ }
12601
+ if (!mimeType) {
12602
+ throw new Error("Missing required parameters: mimeType");
12603
+ }
12604
+ const client = getS3Client();
12605
+ try {
12606
+ await client.send(new PutObjectCommand({
12607
+ Bucket: process.env.CLOUDFLARE_BUCKET,
12608
+ Key: fileName,
12609
+ Body: buffer,
12610
+ ContentType: mimeType,
12611
+ ContentDisposition: "inline",
12612
+ CacheControl: "public, max-age=31536000, immutable"
12613
+ }));
12614
+ const url2 = `${process.env.CLOUDFLARE_PUBLIC_URL}/${fileName}`;
12615
+ return {
12616
+ url: url2,
12617
+ objectKey: fileName
12618
+ };
12619
+ } catch (error) {
12620
+ throw new Error(`上传到 Cloudflare 失败: ${error}`);
12621
+ }
12622
+ }
12623
+ async function deleteFile(objectKey) {
12624
+ const client = getS3Client();
12625
+ try {
12626
+ await client.send(new DeleteObjectCommand({
12627
+ Bucket: process.env.CLOUDFLARE_BUCKET,
12628
+ Key: objectKey
12629
+ }));
12630
+ } catch (error) {
12631
+ throw new Error(`从 Cloudflare 删除失败: ${error}`);
12632
+ }
12633
+ }
12634
+ // src/node/cron/index.ts
12635
+ var exports_cron = {};
12636
+ __export(exports_cron, {
12637
+ schedule: () => schedule,
12638
+ interval: () => interval
12639
+ });
12640
+ import cron from "node-cron";
12641
+ var interval = (intervalMinutes, immediately, fn) => {
12642
+ console.log(`定时任务已启动,将每${intervalMinutes}分钟执行一次`);
12643
+ if (immediately) {
12644
+ fn();
12645
+ }
12646
+ setInterval(() => {
12647
+ fn();
12648
+ }, intervalMinutes * 60 * 1000);
12649
+ };
12650
+ var schedule = (cronExpression, immediately, fn) => {
12651
+ console.log(`定时任务已启动,cron表达式: ${cronExpression}`);
12652
+ if (immediately) {
12653
+ fn();
12654
+ }
12655
+ cron.schedule(cronExpression, () => {
12656
+ fn();
12657
+ }, {
12658
+ timezone: "Asia/Shanghai"
12659
+ });
12660
+ };
12661
+ // src/node/mail/index.ts
12662
+ var exports_mail = {};
12663
+ __export(exports_mail, {
12664
+ send: () => send
12665
+ });
12458
12666
  import nodemailer from "nodemailer";
12459
12667
  var authMap = {
12460
12668
  163: {
@@ -12472,17 +12680,73 @@ var transporter = nodemailer.createTransport({
12472
12680
  secure: true,
12473
12681
  auth: authMap.gmail
12474
12682
  });
12475
- var sendMail = (config2) => {
12683
+ var send = (config2) => {
12476
12684
  return transporter.sendMail({
12477
12685
  from: authMap.gmail.user,
12478
12686
  ...config2
12479
12687
  });
12480
12688
  };
12481
- // src/node/file.ts
12689
+ // src/node/file/index.ts
12690
+ var exports_file = {};
12691
+ __export(exports_file, {
12692
+ replaceContentInFile: () => replaceContentInFile,
12693
+ processFile: () => processFile,
12694
+ getMimeType: () => getMimeType,
12695
+ downloadFile: () => downloadFile
12696
+ });
12482
12697
  import fs from "fs-extra";
12483
12698
  import axios4 from "axios";
12484
12699
  import url2 from "url";
12485
12700
  import * as path2 from "path";
12701
+ import crypto2 from "crypto";
12702
+ var getMimeType = (fileName) => {
12703
+ const ext = fileName.split(".").pop()?.toLowerCase();
12704
+ switch (ext) {
12705
+ case "png":
12706
+ return "image/png";
12707
+ case "jpg":
12708
+ case "jpeg":
12709
+ return "image/jpeg";
12710
+ case "gif":
12711
+ return "image/gif";
12712
+ case "webp":
12713
+ return "image/webp";
12714
+ case "svg":
12715
+ return "image/svg+xml";
12716
+ case "mp4":
12717
+ return "video/mp4";
12718
+ case "mp3":
12719
+ return "audio/mpeg";
12720
+ case "pdf":
12721
+ return "application/pdf";
12722
+ case "doc":
12723
+ return "application/msword";
12724
+ case "docx":
12725
+ return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
12726
+ case "xls":
12727
+ return "application/vnd.ms-excel";
12728
+ case "xlsx":
12729
+ return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
12730
+ case "ppt":
12731
+ return "application/vnd.ms-powerpoint";
12732
+ case "pptx":
12733
+ return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
12734
+ case "txt":
12735
+ return "text/plain";
12736
+ case "html":
12737
+ return "text/html";
12738
+ case "css":
12739
+ return "text/css";
12740
+ case "js":
12741
+ return "application/javascript";
12742
+ case "json":
12743
+ return "application/json";
12744
+ case "xml":
12745
+ return "application/xml";
12746
+ default:
12747
+ return "application/octet-stream";
12748
+ }
12749
+ };
12486
12750
  var replaceContentInFile = async (filePath, targetContent, replacement) => {
12487
12751
  try {
12488
12752
  const data = await fs.readFile(filePath, "utf8");
@@ -12529,84 +12793,76 @@ var downloadFile = async (httpUrl, outputPath) => {
12529
12793
  throw error;
12530
12794
  }
12531
12795
  };
12532
- // src/node/oss.ts
12533
- import OSS from "ali-oss";
12534
- import axios5 from "axios";
12535
- import fs2 from "fs-extra";
12536
- import * as path3 from "path";
12537
- var oss = new OSS({
12538
- region: process.env.VITE_OSS_REGION,
12539
- accessKeyId: process.env.VITE_OSS_ACCESS_KEY_ID,
12540
- accessKeySecret: process.env.VITE_OSS_ACCESS_KEY_SECRET,
12541
- bucket: process.env.VITE_OSS_BUCKET
12542
- });
12543
- async function uploadFile(file, filename) {
12796
+ var generateUniqueFileName2 = (originalName = "") => {
12797
+ const ext = path2.extname(originalName);
12798
+ const nameWithoutExt = path2.basename(originalName, ext);
12799
+ const finalFileName = `${nameWithoutExt ? `${nameWithoutExt}_` : ""}${Date.now()}_${Math.random().toString(36).slice(2)}${ext}`;
12800
+ return {
12801
+ finalFileName,
12802
+ ext: ext.toLowerCase()
12803
+ };
12804
+ };
12805
+ var processFile = async (params) => {
12806
+ const { file, type } = params;
12544
12807
  let buffer;
12545
- let finalFilename;
12546
- if (typeof file === "string") {
12547
- if (/^https?:\/\//.test(file)) {
12808
+ let mimeType;
12809
+ let originalName;
12810
+ switch (type) {
12811
+ case "url":
12812
+ if (typeof file !== "string") {
12813
+ throw new Error("当类型为 url 时,file 必须是字符串");
12814
+ }
12548
12815
  try {
12549
- const response = await axios5.get(file, { responseType: "arraybuffer" });
12816
+ const response = await axios4.get(file, { responseType: "arraybuffer" });
12550
12817
  buffer = Buffer.from(response.data);
12551
12818
  } catch (error) {
12552
12819
  throw new Error(`无法从 URL 下载文件: ${error}`);
12553
12820
  }
12554
- const originalName = path3.basename(new URL(file).pathname);
12555
- const ext = path3.extname(originalName);
12556
- const nameWithoutExt = path3.basename(originalName, ext);
12557
- finalFilename = `${nameWithoutExt}_${Date.now()}${ext}`;
12558
- } else {
12821
+ originalName = path2.basename(new URL(file).pathname);
12822
+ break;
12823
+ case "path":
12824
+ if (typeof file !== "string") {
12825
+ throw new Error("当类型为 path 时,file 必须是字符串");
12826
+ }
12559
12827
  try {
12560
- buffer = fs2.readFileSync(file);
12828
+ buffer = fs.readFileSync(file);
12829
+ originalName = path2.basename(file);
12561
12830
  } catch (error) {
12562
12831
  throw new Error(`无法读取文件路径: ${error}`);
12563
12832
  }
12564
- const originalName = path3.basename(file);
12565
- const ext = path3.extname(originalName);
12566
- const nameWithoutExt = path3.basename(originalName, ext);
12567
- finalFilename = `${nameWithoutExt}_${Date.now()}${ext}`;
12568
- }
12569
- } else if (Buffer.isBuffer(file)) {
12570
- if (!filename) {
12571
- throw new Error("当文件为 Buffer 时,必须提供文件名");
12572
- }
12573
- buffer = file;
12574
- finalFilename = filename;
12575
- } else {
12576
- throw new Error("不支持的文件类型");
12577
- }
12578
- try {
12579
- const result = await oss.put(decodeURIComponent(finalFilename), buffer, {
12580
- headers: {
12581
- "Cache-Control": "public, max-age=31536000, immutable"
12833
+ break;
12834
+ case "file":
12835
+ if (!(file instanceof File)) {
12836
+ throw new Error("当类型为 file 时,file 必须是 File");
12582
12837
  }
12583
- });
12584
- return result.url;
12585
- } catch (error) {
12586
- throw new Error(`上传到 OSS 失败: ${error}`);
12587
- }
12588
- }
12589
- // src/node/cron.ts
12590
- import cron from "node-cron";
12591
- var interval = (intervalMinutes, immediately, fn) => {
12592
- console.log(`定时任务已启动,将每${intervalMinutes}分钟执行一次`);
12593
- if (immediately) {
12594
- fn();
12838
+ buffer = Buffer.from(await file.arrayBuffer());
12839
+ mimeType = file.type;
12840
+ originalName = file.name || "";
12841
+ break;
12842
+ case "buffer":
12843
+ if (!Buffer.isBuffer(file)) {
12844
+ throw new Error("当类型为 buffer 时,file 必须是 Buffer");
12845
+ }
12846
+ buffer = file;
12847
+ break;
12848
+ default:
12849
+ throw new Error("不支持的文件类型");
12595
12850
  }
12596
- setInterval(() => {
12597
- fn();
12598
- }, intervalMinutes * 60 * 1000);
12599
- };
12600
- var schedule = (cronExpression, immediately, fn) => {
12601
- console.log(`定时任务已启动,cron表达式: ${cronExpression}`);
12602
- if (immediately) {
12603
- fn();
12851
+ const size = buffer.length;
12852
+ const hash = crypto2.createHash("md5").update(buffer).digest("hex");
12853
+ const { finalFileName, ext } = generateUniqueFileName2(originalName);
12854
+ if (!mimeType) {
12855
+ mimeType = getMimeType(finalFileName);
12604
12856
  }
12605
- cron.schedule(cronExpression, () => {
12606
- fn();
12607
- }, {
12608
- timezone: "Asia/Shanghai"
12609
- });
12857
+ return {
12858
+ buffer,
12859
+ finalFileName,
12860
+ originalName,
12861
+ hash,
12862
+ mimeType,
12863
+ size,
12864
+ ext
12865
+ };
12610
12866
  };
12611
12867
  var export__ = import_lodash.default;
12612
12868
 
@@ -12615,7 +12871,6 @@ export {
12615
12871
  watch,
12616
12872
  voidType as void,
12617
12873
  util,
12618
- uploadFile,
12619
12874
  unknownType as unknown,
12620
12875
  unionType as union,
12621
12876
  undefinedType as undefined,
@@ -12627,12 +12882,8 @@ export {
12627
12882
  strictObjectType as strictObject,
12628
12883
  shuffleArray,
12629
12884
  setErrorMap,
12630
- setCookie,
12631
12885
  setType as set,
12632
- sendMail,
12633
- schedule,
12634
12886
  retryPromise,
12635
- replaceContentInFile,
12636
12887
  replaceByVariables,
12637
12888
  replaceByRules,
12638
12889
  replaceAll,
@@ -12645,6 +12896,7 @@ export {
12645
12896
  pipelineType as pipeline,
12646
12897
  performDecimalOperation,
12647
12898
  ostring,
12899
+ exports_oss as oss,
12648
12900
  optionsToEnum,
12649
12901
  optionalType as optional,
12650
12902
  onumber,
@@ -12660,6 +12912,7 @@ export {
12660
12912
  nanType as nan,
12661
12913
  mapType as map,
12662
12914
  makeIssue,
12915
+ exports_mail as mail,
12663
12916
  literalType as literal,
12664
12917
  limitDecimals,
12665
12918
  levenshteinDistance,
@@ -12673,12 +12926,10 @@ export {
12673
12926
  isDirty,
12674
12927
  isAsync,
12675
12928
  isAborted,
12676
- interval,
12677
12929
  intersectionType as intersection,
12678
12930
  instanceOfType as instanceof,
12679
12931
  initChinaDayjs,
12680
12932
  imageToBase64,
12681
- getVideoStatus,
12682
12933
  getType,
12683
12934
  getTotal,
12684
12935
  getStrLength,
@@ -12694,16 +12945,15 @@ export {
12694
12945
  getCookie,
12695
12946
  getChineseByStr,
12696
12947
  getChinese,
12697
- generateVideo,
12698
12948
  generateUniqueFileName,
12699
12949
  generateAlphabetArray,
12700
12950
  functionType as function,
12701
12951
  findItem,
12702
12952
  filterParams,
12953
+ exports_file as file,
12703
12954
  executePromise,
12704
12955
  enumType as enum,
12705
12956
  effectsType as effect,
12706
- downloadFile,
12707
12957
  discriminatedUnionType as discriminatedUnion,
12708
12958
  delay,
12709
12959
  en_default as defaultErrorMap,
@@ -12711,11 +12961,11 @@ export {
12711
12961
  datetimeRegex,
12712
12962
  dateType as date,
12713
12963
  custom,
12964
+ exports_cron as cron,
12714
12965
  default5 as consola,
12715
12966
  coerce,
12716
12967
  chinaDayjs,
12717
12968
  cheerio,
12718
- chat,
12719
12969
  calcJsText,
12720
12970
  booleanType as boolean,
12721
12971
  bigIntType as bigint,
@@ -12723,6 +12973,7 @@ export {
12723
12973
  arrayType as array,
12724
12974
  anyType as any,
12725
12975
  amount,
12976
+ exports_ai as ai,
12726
12977
  addIssueToContext,
12727
12978
  export__ as _,
12728
12979
  ZodVoid,
@@ -12776,9 +13027,6 @@ export {
12776
13027
  INVALID,
12777
13028
  Enum,
12778
13029
  EMPTY_PATH,
12779
- Dayjs,
12780
- DataStoreOptions,
12781
- default6 as DataStore,
12782
13030
  DIRTY,
12783
13031
  BRAND
12784
13032
  };