@abtnode/util 1.16.15-beta-9318a201 → 1.16.15-beta-ed0db59e
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/lib/logo-middleware.js +53 -20
- package/package.json +5 -5
package/lib/logo-middleware.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
+
const http = require('http');
|
|
3
|
+
const https = require('https');
|
|
2
4
|
const path = require('path');
|
|
3
5
|
const get = require('lodash/get');
|
|
4
6
|
|
|
5
7
|
const logger = require('@abtnode/logger')('@abtnode/util:logo-middleware');
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param {import('express').Request} req
|
|
10
|
+
* @param {import('express').Request & {blocklet: any, sendOptions: any}} req
|
|
10
11
|
* @param {import('express').Response & {sendFallbackLogo: Function} } res
|
|
11
12
|
* @param {import('express').NextFunction} next
|
|
12
13
|
* @returns
|
|
@@ -23,9 +24,46 @@ const ensureBlockletExist = (req, res, next) => {
|
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
*
|
|
27
27
|
* @param {import('express').Request} req
|
|
28
28
|
* @param {import('express').Response & {sendFallbackLogo: Function} } res
|
|
29
|
+
* @param {string} url
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
const sendCustomRemoteFile = (req, res, url) => {
|
|
33
|
+
const tmp = new URL(url);
|
|
34
|
+
// pass along image filter params
|
|
35
|
+
Object.keys(req.query || {}).forEach((key) => {
|
|
36
|
+
if (tmp.searchParams.get(key)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
tmp.searchParams.set(key, req.query[key]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const opts = Object.assign(new URL(tmp.href), {
|
|
43
|
+
method: req.method,
|
|
44
|
+
headers: req.headers,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const request = opts.protocol === 'https:' ? https.request : http.request;
|
|
48
|
+
|
|
49
|
+
request(opts, (x) => {
|
|
50
|
+
['content-type', 'content-length', 'cache-control', 'date', 'etag', 'last-modified'].forEach((key) => {
|
|
51
|
+
if (x.headers[key]) {
|
|
52
|
+
res.header(key, x.headers[key]);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
x.pipe(res);
|
|
56
|
+
})
|
|
57
|
+
.on('error', (err) => {
|
|
58
|
+
logger.error('failed to get customized logo', { url: req.url, error: err });
|
|
59
|
+
res.sendFallbackLogo();
|
|
60
|
+
})
|
|
61
|
+
.end();
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {import('express').Request & {blocklet: any, sendOptions: any}} req
|
|
66
|
+
* @param {import('express').Response & {sendFallbackLogo: Function, sendLogoFile: Function} } res
|
|
29
67
|
* @param {import('express').NextFunction} next
|
|
30
68
|
* @returns
|
|
31
69
|
*/
|
|
@@ -34,16 +72,15 @@ const ensureCustomSquareLogo = (req, res, next) => {
|
|
|
34
72
|
|
|
35
73
|
/** @type {string} */
|
|
36
74
|
const customLogoSquare = get(blocklet, 'environmentObj.BLOCKLET_APP_LOGO_SQUARE');
|
|
37
|
-
|
|
38
75
|
if (customLogoSquare) {
|
|
39
76
|
if (customLogoSquare.startsWith('http')) {
|
|
40
|
-
res
|
|
77
|
+
sendCustomRemoteFile(req, res, customLogoSquare);
|
|
41
78
|
return;
|
|
42
79
|
}
|
|
43
80
|
|
|
44
81
|
const logoFile = path.join(get(blocklet, 'env.appDir'), customLogoSquare);
|
|
45
82
|
if (fs.existsSync(logoFile)) {
|
|
46
|
-
res.
|
|
83
|
+
res.sendLogoFile(logoFile, sendOptions);
|
|
47
84
|
return;
|
|
48
85
|
}
|
|
49
86
|
}
|
|
@@ -53,8 +90,8 @@ const ensureCustomSquareLogo = (req, res, next) => {
|
|
|
53
90
|
|
|
54
91
|
/**
|
|
55
92
|
*
|
|
56
|
-
* @param {import('express').Request} req
|
|
57
|
-
* @param {import('express').Response & {sendFallbackLogo: Function} } res
|
|
93
|
+
* @param {import('express').Request & {blocklet: any, sendOptions: any}} req
|
|
94
|
+
* @param {import('express').Response & {sendFallbackLogo: Function, sendLogoFile: Function} } res
|
|
58
95
|
* @param {import('express').NextFunction} next
|
|
59
96
|
* @returns
|
|
60
97
|
*/
|
|
@@ -71,7 +108,7 @@ const ensureBundleLogo = (req, res, next) => {
|
|
|
71
108
|
const logoFile = path.join(get(blocklet, 'env.appDir'), blocklet.meta.logo);
|
|
72
109
|
|
|
73
110
|
if (fs.existsSync(logoFile)) {
|
|
74
|
-
res.
|
|
111
|
+
res.sendLogoFile(logoFile, sendOptions);
|
|
75
112
|
return;
|
|
76
113
|
}
|
|
77
114
|
}
|
|
@@ -96,9 +133,8 @@ const cacheError = (err, req, res, next) => {
|
|
|
96
133
|
};
|
|
97
134
|
|
|
98
135
|
/**
|
|
99
|
-
*
|
|
100
|
-
* @param {import('express').
|
|
101
|
-
* @param {import('express').Response & {sendFallbackLogo: Function} } res
|
|
136
|
+
* @param {import('express').Request & {blocklet: any, sendOptions: any}} req
|
|
137
|
+
* @param {import('express').Response & {sendFallbackLogo: Function, sendLogoFile: Function} } res
|
|
102
138
|
* @param {import('express').NextFunction} next
|
|
103
139
|
* @returns
|
|
104
140
|
*/
|
|
@@ -112,8 +148,6 @@ const ensureDefaultLogo = (req, res, next) => {
|
|
|
112
148
|
const { blocklet, sendOptions } = req;
|
|
113
149
|
|
|
114
150
|
if (blocklet && get(blocklet, 'env.dataDir')) {
|
|
115
|
-
get(blocklet, 'env.dataDir');
|
|
116
|
-
|
|
117
151
|
const logoSvgFile = path.join(get(blocklet, 'env.dataDir'), 'logo.svg');
|
|
118
152
|
if (fs.existsSync(logoSvgFile)) {
|
|
119
153
|
res.sendFile(logoSvgFile, sendOptions);
|
|
@@ -122,7 +156,7 @@ const ensureDefaultLogo = (req, res, next) => {
|
|
|
122
156
|
|
|
123
157
|
const logoPngFile = path.join(get(blocklet, 'env.dataDir'), 'logo.png');
|
|
124
158
|
if (fs.existsSync(logoPngFile)) {
|
|
125
|
-
res.
|
|
159
|
+
res.sendLogoFile(logoPngFile, sendOptions);
|
|
126
160
|
return;
|
|
127
161
|
}
|
|
128
162
|
}
|
|
@@ -131,9 +165,8 @@ const ensureDefaultLogo = (req, res, next) => {
|
|
|
131
165
|
};
|
|
132
166
|
|
|
133
167
|
/**
|
|
134
|
-
*
|
|
135
|
-
* @param {import('express').
|
|
136
|
-
* @param {import('express').Response & {sendFallbackLogo: Function} } res
|
|
168
|
+
* @param {import('express').Request & {blocklet: any, sendOptions: any}} req
|
|
169
|
+
* @param {import('express').Response & {sendFallbackLogo: Function, sendLogoFile: Function} } res
|
|
137
170
|
* @param {import('express').NextFunction} next
|
|
138
171
|
* @returns
|
|
139
172
|
*/
|
|
@@ -149,13 +182,13 @@ const ensureCustomFavicon = (req, res, next) => {
|
|
|
149
182
|
const customLogoFavicon = get(blocklet, 'environmentObj.BLOCKLET_APP_LOGO_FAVICON');
|
|
150
183
|
if (customLogoFavicon) {
|
|
151
184
|
if (customLogoFavicon.startsWith('http')) {
|
|
152
|
-
res
|
|
185
|
+
sendCustomRemoteFile(req, res, customLogoFavicon);
|
|
153
186
|
return;
|
|
154
187
|
}
|
|
155
188
|
|
|
156
189
|
const logoFile = path.join(get(blocklet, 'env.appDir'), customLogoFavicon);
|
|
157
190
|
if (fs.existsSync(logoFile)) {
|
|
158
|
-
res.
|
|
191
|
+
res.sendLogoFile(logoFile, sendOptions);
|
|
159
192
|
return;
|
|
160
193
|
}
|
|
161
194
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.15-beta-
|
|
6
|
+
"version": "1.16.15-beta-ed0db59e",
|
|
7
7
|
"description": "ArcBlock's JavaScript utility",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
|
|
19
19
|
"license": "Apache-2.0",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@abtnode/constant": "1.16.15-beta-
|
|
22
|
-
"@abtnode/logger": "1.16.15-beta-
|
|
23
|
-
"@blocklet/constant": "1.16.15-beta-
|
|
21
|
+
"@abtnode/constant": "1.16.15-beta-ed0db59e",
|
|
22
|
+
"@abtnode/logger": "1.16.15-beta-ed0db59e",
|
|
23
|
+
"@blocklet/constant": "1.16.15-beta-ed0db59e",
|
|
24
24
|
"@ocap/client": "1.18.89",
|
|
25
25
|
"@ocap/mcrypto": "1.18.89",
|
|
26
26
|
"@ocap/util": "1.18.89",
|
|
@@ -71,5 +71,5 @@
|
|
|
71
71
|
"fs-extra": "^10.1.0",
|
|
72
72
|
"jest": "^27.5.1"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "647ebe45f6214ea5c284c9234f1319b6ff72f915"
|
|
75
75
|
}
|