| 1 | // prettier-ignore |
| 2 | function Env(t, s) { |
| 3 | return new (class { |
| 4 | constructor(t, s) { |
| 5 | this.userIdx = 1; |
| 6 | this.userList = []; |
| 7 | this.userCount = 0; |
| 8 | this.name = t; |
| 9 | this.notifyStr = []; |
| 10 | this.logSeparator = "\n"; |
| 11 | this.startTime = new Date().getTime(); |
| 12 | Object.assign(this, s); |
| 13 | this.log(`\ud83d\udd14${this.name},\u5f00\u59cb!`); |
| 14 | this.bucket = this.bucket || '' |
| 15 | this.fs = require("fs"); |
| 16 | if (this.isNode() && this.bucket) { |
| 17 | try { |
| 18 | if (!this.fs.existsSync(this.bucket)) { |
| 19 | this.fs.writeFileSync(this.bucket, JSON.stringify({}, null, 2)); |
| 20 | this.log(`📁 已创建 bucket 文件: ${this.bucket}`); |
| 21 | } |
| 22 | } catch (e) { |
| 23 | this.log("❌ 初始化 bucket 失败: " + e.message); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | async get(key, def = null) { |
| 28 | if (!this.isNode()) return def; |
| 29 | try { |
| 30 | const data = await this.fs.promises.readFile(this.bucket, "utf-8"); |
| 31 | const json = JSON.parse(data); |
| 32 | return json.hasOwnProperty(key) ? json[key] : def; |
| 33 | } catch (e) { |
| 34 | this.log("❌ 读取bucket失败: " + e.message); |
| 35 | return def; |
| 36 | } |
| 37 | } |
| 38 | async set(key, value) { |
| 39 | if (!this.isNode()) return; |
| 40 | try { |
| 41 | const data = await this.fs.promises.readFile(this.bucket, "utf-8"); |
| 42 | const json = JSON.parse(data); |
| 43 | json[key] = value; |
| 44 | await this.fs.promises.writeFile(this.bucket, JSON.stringify(json, null, 2)); |
| 45 | } catch (e) { |
| 46 | this.log("❌ 写入bucket失败: " + e.message); |
| 47 | } |
| 48 | } |
| 49 | checkEnv(ckName) { |
| 50 | const envSplitor = ["&", "\n"]; |
| 51 | let userCookie = (this.isNode() ? process.env[ckName] : "") || ""; |
| 52 | this.userList = userCookie.split(envSplitor.find((o) => userCookie.includes(o)) || "&").filter((n) => n); |
| 53 | this.userCount = this.userList.length; |
| 54 | this.log(`共找到${this.userCount}个账号`); |
| 55 | } |
| 56 | toStr(v) { |
| 57 | if (v instanceof Error) return v.stack || v.message; |
| 58 | if (v && typeof v == "object") try { return JSON.stringify(v) } catch { return "[Complex Object]" } |
| 59 | return String(v); |
| 60 | } |
nothing calls this directly
no outgoing calls
no test coverage detected