Skip to content

Commit 346fd6f

Browse files
Merge pull request #48 from undefined-hestudio/main
### 1.4.5 1. external.refreshtask: Fix an issue where scheduled tasks have stopped running even though they haven't finished running yet. 2. server.getupdate: Optimize the error-throwing mechanism when checking for updates. 3. server: Upgraded dependency library. 4. server: Modify code structure to optimize startup progress properly. 5. external.refreshtask: Optimize the error-throwing mechanism when running scheduled tasks. 6. server: Optimize the error-throwing mechanism of the listening service.
2 parents 9d9e23b + 47c26c1 commit 346fd6f

File tree

4 files changed

+187
-170
lines changed

4 files changed

+187
-170
lines changed

CHANGELOG

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
### 1.4.5
2+
1. external.refreshtask: Fix an issue where scheduled tasks have stopped running even though they haven't finished running yet.
3+
2. server.getupdate: Optimize the error-throwing mechanism when checking for updates.
4+
3. server: Upgraded dependency library.
5+
4. server: Modify code structure to optimize startup progress properly.
6+
5. external.refreshtask: Optimize the error-throwing mechanism when running scheduled tasks.
7+
6. server: Optimize the error-throwing mechanism of the listening service.
8+
19
### 1.4.4
210
1. server.getupdate: Fix delete `tmp` dir failed when use win32 platform.
311

get.js

+81-92
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
require("dotenv").config();
2-
const VERSION = "1.4.4";
2+
const VERSION = "1.4.5";
33

44
const express = require("express");
55
const schedule = require("node-schedule");
66
const ChildProcess = require("child_process");
77
const fs = require("fs");
8-
const app = express();
98
const dayjs = require("dayjs");
109
const crypto = require("node:crypto");
1110
const bodyParser = require("body-parser");
@@ -97,23 +96,6 @@ else hbwgConfig.header = "x-forwarded-for";
9796
// 定时
9897
const rule = new schedule.RecurrenceRule();
9998

100-
// 允许跨域
101-
app.all("*", function (req, res, next) {
102-
res.header("Access-Control-Allow-Origin", "*");
103-
res.header(
104-
"Access-Control-Allow-Headers",
105-
"Content-Type,Content-Length, Authorization, Accept,X-Requested-With"
106-
);
107-
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
108-
res.header("Access-Control-Allow-Credentials", "true");
109-
if (req.method === "OPTIONS") res.send(200);
110-
else next();
111-
});
112-
113-
// allow read message from post
114-
app.use(bodyParser.urlencoded({ extended: false }));
115-
app.use(bodyParser.json());
116-
11799
// Derived function
118100
module.exports = {
119101
getback,
@@ -181,21 +163,6 @@ else
181163
hbwgConfig.packageurl =
182164
"https://registry.npmjs.com/hestudio-bingwallpaper-get/latest";
183165

184-
if (hbwgConfig.getupdate !== false) {
185-
const requestOptions = {
186-
method: "GET",
187-
redirect: "follow",
188-
};
189-
function AfterGetVersion(src) {
190-
hbwgConfig.version = src.version;
191-
if (hbwgConfig.version !== VERSION)
192-
logwarn(`New Version! ${hbwgConfig.version} is ready.`);
193-
}
194-
fetch(hbwgConfig.packageurl, requestOptions)
195-
.then((response) => response.json())
196-
.then((result) => AfterGetVersion(result));
197-
}
198-
199166
if (
200167
typeof hbwgConfig.external === "object" &&
201168
hbwgConfig.external.refreshtime
@@ -217,70 +184,63 @@ if (
217184
hbwgConfig.refreshtask = hbwgConfig.external.refreshtask;
218185
}
219186

220-
const cacheimg = async () => {
221-
/**
222-
*
223-
* @param {object} bingsrc
224-
*/
225-
const download = async (bingsrc) => {
226-
hbwgConfig.bingsrc = bingsrc;
227-
const url = hbwgConfig.host + bingsrc.images[0].url;
228-
await fetch(url, {
229-
method: "GET",
230-
}).then(async (response) => {
231-
await response.arrayBuffer().then(async (buffer) => {
232-
await (hbwgConfig.image = Buffer.from(buffer));
233-
});
234-
});
235-
hbwgConfig.copyright = String(bingsrc.images[0].copyright);
236-
hbwgConfig.copyrightlink = String(bingsrc.images[0].copyrightlink);
237-
hbwgConfig.title = String(bingsrc.images[0].title);
238-
if (typeof hbwgConfig.refreshtask === "function") {
187+
function cacheimg() {
188+
if (typeof hbwgConfig.refreshtask === "function") {
189+
try {
239190
logwarn("task is running...");
240191
hbwgConfig.refreshtask();
241192
logwarn("task is finish.");
193+
} catch (e) {
194+
logerr(`refreshtask: ${e}`);
242195
}
243-
await logback("Refresh Successfully!");
244-
};
245-
const requestOptions = {
246-
method: "GET",
247-
redirect: "follow",
248-
};
249-
await fetch(hbwgConfig.api, requestOptions)
250-
.then((response) => response.json())
251-
.then((result) => download(result));
252-
};
253-
254-
// eslint-disable-next-line no-unused-vars
255-
const job = schedule.scheduleJob(rule, async function () {
196+
}
256197
if (hbwgConfig.getupdate !== false) {
257198
const requestOptions = {
258199
method: "GET",
259200
redirect: "follow",
260201
};
261202
function AfterGetVersion(src) {
262203
hbwgConfig.version = src.version;
263-
if (hbwgConfig.version !== VERSION) {
204+
if (hbwgConfig.version !== VERSION)
264205
logwarn(`New Version! ${hbwgConfig.version} is ready.`);
265-
}
266206
}
267207
fetch(hbwgConfig.packageurl, requestOptions)
268208
.then((response) => response.json())
269-
.then((result) => AfterGetVersion(result));
209+
.then((result) => AfterGetVersion(result))
210+
.catch((error) => logerr(`getupdate: ${error}`));
270211
}
271-
cacheimg();
272-
});
212+
fetch(hbwgConfig.api, {
213+
method: "GET",
214+
redirect: "follow",
215+
})
216+
.then((response) => response.json())
217+
.then((result) => {
218+
hbwgConfig.bingsrc = result;
219+
const url = hbwgConfig.host + result.images[0].url;
220+
fetch(url, {
221+
method: "GET",
222+
})
223+
.then((response) => {
224+
response.arrayBuffer().then(async (buffer) => {
225+
await (hbwgConfig.image = Buffer.from(buffer));
226+
});
227+
})
228+
.catch((error) => logerr(`api.getimage: ${error}`));
229+
hbwgConfig.copyright = String(result.images[0].copyright);
230+
hbwgConfig.copyrightlink = String(result.images[0].copyrightlink);
231+
hbwgConfig.title = String(result.images[0].title);
232+
})
233+
.catch((error) => logerr(`source.bingsrc: ${error}`));
234+
235+
logback("Refresh Successfully!");
236+
}
273237

274238
cacheimg();
275239

276-
// Load configuration file
277-
if (
278-
typeof hbwgConfig.external === "object" &&
279-
hbwgConfig.external.beforestart
280-
) {
281-
logback("Initialization configuration has been imported.");
282-
hbwgConfig.external.beforestart(app, getback, postback, logback, logerr);
283-
}
240+
// eslint-disable-next-line no-unused-vars
241+
const job = schedule.scheduleJob(rule, async function () {
242+
cacheimg();
243+
});
284244

285245
// api default configuration
286246
hbwgConfig.apiconfig = {
@@ -351,14 +311,6 @@ if (
351311
hbwgConfig.robots = `User-agent: *
352312
Disallow: /`;
353313
}
354-
if (hbwgConfig.robots !== false) {
355-
app.get("/robots.txt", (req, res) => {
356-
res.setHeader("Content-Type", "text/plain");
357-
const ip = req.headers[hbwgConfig.header] || req.connection.remoteAddress;
358-
res.send(hbwgConfig.robots);
359-
getback(ip, "/robots.txt");
360-
});
361-
}
362314

363315
if (
364316
typeof hbwgConfig.external === "object" &&
@@ -369,6 +321,29 @@ if (
369321
}
370322

371323
// 主程序
324+
const app = express();
325+
app.use(bodyParser.urlencoded({ extended: false }));
326+
app.use(bodyParser.json());
327+
app.all("*", function (req, res, next) {
328+
res.header("Access-Control-Allow-Origin", "*");
329+
res.header(
330+
"Access-Control-Allow-Headers",
331+
"Content-Type,Content-Length, Authorization, Accept,X-Requested-With"
332+
);
333+
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
334+
res.header("Access-Control-Allow-Credentials", "true");
335+
if (req.method === "OPTIONS") res.send(200);
336+
else next();
337+
});
338+
339+
if (
340+
typeof hbwgConfig.external === "object" &&
341+
hbwgConfig.external.beforestart
342+
) {
343+
logback("Initialization configuration has been imported.");
344+
hbwgConfig.external.beforestart(app, getback, postback, logback, logerr);
345+
}
346+
372347
app.get("/", (req, res) => {
373348
const ip = req.headers[hbwgConfig.header] || req.connection.remoteAddress;
374349
if (typeof hbwgConfig.rootprogram === "function") {
@@ -380,6 +355,15 @@ app.get("/", (req, res) => {
380355
getback(ip, "/");
381356
});
382357

358+
if (hbwgConfig.robots !== false) {
359+
app.get("/robots.txt", (req, res) => {
360+
res.setHeader("Content-Type", "text/plain");
361+
const ip = req.headers[hbwgConfig.header] || req.connection.remoteAddress;
362+
res.send(hbwgConfig.robots);
363+
getback(ip, "/robots.txt");
364+
});
365+
}
366+
383367
if (hbwgConfig.apiconfig.getimage) {
384368
app.get(hbwgConfig.apiconfig.getimage, (req, res) => {
385369
const ip = req.headers[hbwgConfig.header] || req.connection.remoteAddress;
@@ -574,8 +558,13 @@ if (hbwgConfig.apiconfig.debug.url) {
574558
// delete external cache
575559
hbwgConfig.external = undefined;
576560

577-
app.listen(hbwgConfig.port, () => {
578-
logback(
579-
`heStudio BingWallpaper Get is running on localhost:${hbwgConfig.port}`
580-
);
581-
});
561+
app
562+
.listen(hbwgConfig.port, () => {
563+
logback(
564+
`heStudio BingWallpaper Get is running on localhost:${hbwgConfig.port}`
565+
);
566+
})
567+
.on("error", (err) => {
568+
logerr(`server: ${err}`);
569+
process.exit(1);
570+
});

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hestudio-bingwallpaper-get",
3-
"version": "1.4.4",
3+
"version": "1.4.5",
44
"description": "A Bing wallpaper API interface that can directly image output images.",
55
"main": "get.js",
66
"scripts": {
@@ -45,8 +45,8 @@
4545
"node": ">=18.0.0"
4646
},
4747
"devDependencies": {
48-
"@eslint/js": "^9.16.0",
49-
"eslint": "~9.15.0",
50-
"globals": "^15.13.0"
48+
"@eslint/js": "^9.17.0",
49+
"eslint": "~9.17.0",
50+
"globals": "^15.14.0"
5151
}
5252
}

0 commit comments

Comments
 (0)