@seip/bluebird-js 0.0.1 → 0.1.0
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/.env_example +53 -53
- package/AGENTS.md +426 -403
- package/LICENSE +1 -1
- package/README.md +292 -285
- package/app/routes/api.js +50 -50
- package/app/routes/web.js +55 -53
- package/app/views/about.js +63 -0
- package/app/views/dashboard.js +147 -0
- package/app/views/index.js +80 -0
- package/app/views/layouts/main/view.js +81 -0
- package/app/views/legal/terms.html +19 -0
- package/app/views/login.js +80 -0
- package/core/app.d.ts +18 -18
- package/core/app.js +684 -644
- package/core/auth.d.ts +13 -13
- package/core/auth.js +326 -326
- package/core/cache.d.ts +17 -17
- package/core/cache.js +475 -475
- package/core/cli/doctor.js +287 -287
- package/core/cli/ecosystem.js +162 -162
- package/core/cli/init.js +779 -779
- package/core/cli/migrate.js +541 -541
- package/core/cli/model.js +66 -66
- package/core/cli/nginx.js +199 -199
- package/core/cli/route.js +120 -120
- package/core/cli/view.js +55 -36
- package/core/config.d.ts +3 -3
- package/core/config.js +111 -111
- package/core/csrf.d.ts +3 -3
- package/core/csrf.js +114 -114
- package/core/database.d.ts +15 -15
- package/core/database.js +636 -636
- package/core/debug.d.ts +3 -3
- package/core/debug.js +249 -249
- package/core/hash.d.ts +3 -3
- package/core/hash.js +194 -194
- package/core/html.d.ts +42 -0
- package/core/html.js +217 -0
- package/core/index.d.ts +1176 -1071
- package/core/logger.d.ts +3 -3
- package/core/logger.js +127 -124
- package/core/middleware.d.ts +3 -3
- package/core/middleware.js +28 -28
- package/core/model.d.ts +19 -19
- package/core/model.js +600 -600
- package/core/queue.d.ts +13 -13
- package/core/queue.js +172 -172
- package/core/ratelimit.d.ts +3 -3
- package/core/ratelimit.js +145 -145
- package/core/redis.d.ts +21 -21
- package/core/redis.js +230 -230
- package/core/render-cache.js +212 -0
- package/core/render.d.ts +3 -3
- package/core/render.js +199 -555
- package/core/router.d.ts +3 -3
- package/core/router.js +171 -171
- package/core/seo.d.ts +3 -3
- package/core/seo.js +106 -106
- package/core/swagger.d.ts +3 -3
- package/core/swagger.js +60 -60
- package/core/upload.d.ts +13 -13
- package/core/upload.js +102 -102
- package/core/util/colors.js +82 -82
- package/core/validate.d.ts +15 -15
- package/core/validate.js +364 -364
- package/core/view-loader.js +162 -0
- package/core/vite.d.ts +3 -3
- package/core/vite.js +145 -145
- package/core/ws.d.ts +13 -13
- package/core/ws.js +245 -245
- package/index.js +18 -18
- package/jsconfig.json +15 -15
- package/package.json +85 -86
- package/production.md +273 -273
- package/public/css/bluebird.min.css +1 -1
- package/public/js/bluebird-client.js +1780 -1662
- package/public/js/bluebird.min.js +1072 -1072
- package/app/views/about.html +0 -161
- package/app/views/dashboard.html +0 -140
- package/app/views/index.html +0 -150
- package/app/views/login.html +0 -144
package/core/hash.js
CHANGED
|
@@ -1,194 +1,194 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
|
|
3
|
-
let bcryptModule = null;
|
|
4
|
-
try {
|
|
5
|
-
bcryptModule = (await import("bcrypt")).default || (await import("bcrypt"));
|
|
6
|
-
} catch {}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* High-performance Password Hashing class.
|
|
10
|
-
* Uses native node:crypto scrypt by default (zero npm dependencies, NIST recommended),
|
|
11
|
-
* with seamless support for bcrypt when installed or verifying bcrypt hashes.
|
|
12
|
-
*/
|
|
13
|
-
class Hash {
|
|
14
|
-
/**
|
|
15
|
-
* Hashes a plain text password using scrypt (default) or bcrypt.
|
|
16
|
-
* @param {string} password - The plain text password.
|
|
17
|
-
* @param {Object} [options={}] - Hashing options.
|
|
18
|
-
* @param {string} [options.driver="scrypt"] - Hashing driver ('scrypt' or 'bcrypt').
|
|
19
|
-
* @param {number} [options.rounds=10] - Salt rounds for bcrypt (if driver is 'bcrypt').
|
|
20
|
-
* @param {number} [options.N=16384] - CPU/memory cost parameter for scrypt.
|
|
21
|
-
* @param {number} [options.r=8] - Block size for scrypt.
|
|
22
|
-
* @param {number} [options.p=1] - Parallelization parameter for scrypt.
|
|
23
|
-
* @returns {Promise<string>} Formatted password hash string.
|
|
24
|
-
* @example
|
|
25
|
-
* const hash = await Hash.make("mySecretPassword");
|
|
26
|
-
* const bcryptHash = await Hash.make("mySecretPassword", { driver: "bcrypt" });
|
|
27
|
-
*/
|
|
28
|
-
static async make(password, options = {}) {
|
|
29
|
-
if (typeof password !== "string" || !password) {
|
|
30
|
-
throw new Error("[HASH ERROR] Password must be a non-empty string.");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const driver = (options.driver || "scrypt").toLowerCase();
|
|
34
|
-
|
|
35
|
-
if (driver === "bcrypt") {
|
|
36
|
-
if (!bcryptModule) {
|
|
37
|
-
throw new Error(
|
|
38
|
-
"[HASH ERROR] 'bcrypt' package is not installed. Run 'npm install bcrypt' or 'npx blue-bird add bcrypt', or use the default scrypt driver.",
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
const rounds = options.rounds || 10;
|
|
42
|
-
return bcryptModule.hash(password, rounds);
|
|
43
|
-
}
|
|
44
|
-
const N = options.N || 16384;
|
|
45
|
-
const r = options.r || 8;
|
|
46
|
-
const p = options.p || 1;
|
|
47
|
-
const keylen = 64;
|
|
48
|
-
const salt = crypto.randomBytes(16);
|
|
49
|
-
|
|
50
|
-
return new Promise((resolve, reject) => {
|
|
51
|
-
crypto.scrypt(
|
|
52
|
-
password,
|
|
53
|
-
salt,
|
|
54
|
-
keylen,
|
|
55
|
-
{ N, r, p, maxmem: 32 * 1024 * 1024 },
|
|
56
|
-
(err, derivedKey) => {
|
|
57
|
-
if (err) return reject(err);
|
|
58
|
-
const hashString = `$scrypt$N=${N},r=${r},p=${p}$${salt.toString("hex")}$${derivedKey.toString("hex")}`;
|
|
59
|
-
resolve(hashString);
|
|
60
|
-
},
|
|
61
|
-
);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Alias for make().
|
|
67
|
-
* @param {string} password - The plain text password.
|
|
68
|
-
* @param {Object} [options={}] - Options.
|
|
69
|
-
* @returns {Promise<string>}
|
|
70
|
-
*/
|
|
71
|
-
static async hash(password, options = {}) {
|
|
72
|
-
return this.make(password, options);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Verifies a plain text password against a hash string.
|
|
77
|
-
* Automatically detects scrypt or bcrypt hash formats.
|
|
78
|
-
* Uses timing-safe comparison to protect against side-channel attacks.
|
|
79
|
-
*
|
|
80
|
-
* @param {string} password - The plain text password to check.
|
|
81
|
-
* @param {string} hash - The stored hash string.
|
|
82
|
-
* @returns {Promise<boolean>} True if password matches, false otherwise.
|
|
83
|
-
* @example
|
|
84
|
-
* const isValid = await Hash.verify("mySecretPassword", storedHash);
|
|
85
|
-
*/
|
|
86
|
-
static async verify(password, hash) {
|
|
87
|
-
if (
|
|
88
|
-
typeof password !== "string" ||
|
|
89
|
-
!password ||
|
|
90
|
-
typeof hash !== "string" ||
|
|
91
|
-
!hash
|
|
92
|
-
) {
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (/^\$2[aby]\$\d{2}\$/.test(hash)) {
|
|
97
|
-
if (!bcryptModule) {
|
|
98
|
-
console.error(
|
|
99
|
-
"[HASH ERROR] A bcrypt hash was detected, but the 'bcrypt' package is not installed. Run 'npm install bcrypt' or 'npx blue-bird add bcrypt'.",
|
|
100
|
-
);
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
try {
|
|
104
|
-
return await bcryptModule.compare(password, hash);
|
|
105
|
-
} catch {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (hash.startsWith("$scrypt$")) {
|
|
111
|
-
const parts = hash.split("$");
|
|
112
|
-
if (parts.length !== 5) return false;
|
|
113
|
-
|
|
114
|
-
const paramsStr = parts[2];
|
|
115
|
-
const saltHex = parts[3];
|
|
116
|
-
const originalHashHex = parts[4];
|
|
117
|
-
|
|
118
|
-
if (!paramsStr || !saltHex || !originalHashHex) return false;
|
|
119
|
-
|
|
120
|
-
let N = 16384,
|
|
121
|
-
r = 8,
|
|
122
|
-
p = 1;
|
|
123
|
-
paramsStr.split(",").forEach((param) => {
|
|
124
|
-
const [k, v] = param.split("=");
|
|
125
|
-
if (k === "N") N = parseInt(v, 10) || N;
|
|
126
|
-
if (k === "r") r = parseInt(v, 10) || r;
|
|
127
|
-
if (k === "p") p = parseInt(v, 10) || p;
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const salt = Buffer.from(saltHex, "hex");
|
|
131
|
-
const originalHash = Buffer.from(originalHashHex, "hex");
|
|
132
|
-
|
|
133
|
-
return new Promise((resolve) => {
|
|
134
|
-
crypto.scrypt(
|
|
135
|
-
password,
|
|
136
|
-
salt,
|
|
137
|
-
originalHash.length,
|
|
138
|
-
{ N, r, p, maxmem: 32 * 1024 * 1024 },
|
|
139
|
-
(err, derivedKey) => {
|
|
140
|
-
if (err) return resolve(false);
|
|
141
|
-
try {
|
|
142
|
-
const matches = crypto.timingSafeEqual(originalHash, derivedKey);
|
|
143
|
-
resolve(matches);
|
|
144
|
-
} catch {
|
|
145
|
-
resolve(false);
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Alias for verify().
|
|
157
|
-
* @param {string} password - The plain text password.
|
|
158
|
-
* @param {string} hash - The stored hash string.
|
|
159
|
-
* @returns {Promise<boolean>}
|
|
160
|
-
*/
|
|
161
|
-
static async check(password, hash) {
|
|
162
|
-
return this.verify(password, hash);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Checks if a given hash needs to be rehashed to match updated security parameters.
|
|
167
|
-
* @param {string} hash - The stored hash string.
|
|
168
|
-
* @param {Object} [options={}] - Target options.
|
|
169
|
-
* @returns {boolean} True if the hash should be regenerated.
|
|
170
|
-
*/
|
|
171
|
-
static needsRehash(hash, options = {}) {
|
|
172
|
-
if (!hash || typeof hash !== "string") return true;
|
|
173
|
-
const targetDriver = (options.driver || "scrypt").toLowerCase();
|
|
174
|
-
|
|
175
|
-
if (targetDriver === "bcrypt") {
|
|
176
|
-
return !/^\$2[aby]\$\d{2}\$/.test(hash);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (!hash.startsWith("$scrypt$")) return true;
|
|
180
|
-
|
|
181
|
-
const parts = hash.split("$");
|
|
182
|
-
if (parts.length !== 5) return true;
|
|
183
|
-
|
|
184
|
-
const paramsStr = parts[2];
|
|
185
|
-
const targetN = options.N || 16384;
|
|
186
|
-
const targetR = options.r || 8;
|
|
187
|
-
const targetP = options.p || 1;
|
|
188
|
-
|
|
189
|
-
return !paramsStr.includes(`N=${targetN},r=${targetR},p=${targetP}`);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export { Hash };
|
|
194
|
-
export default Hash;
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
|
|
3
|
+
let bcryptModule = null;
|
|
4
|
+
try {
|
|
5
|
+
bcryptModule = (await import("bcrypt")).default || (await import("bcrypt"));
|
|
6
|
+
} catch {}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* High-performance Password Hashing class.
|
|
10
|
+
* Uses native node:crypto scrypt by default (zero npm dependencies, NIST recommended),
|
|
11
|
+
* with seamless support for bcrypt when installed or verifying bcrypt hashes.
|
|
12
|
+
*/
|
|
13
|
+
class Hash {
|
|
14
|
+
/**
|
|
15
|
+
* Hashes a plain text password using scrypt (default) or bcrypt.
|
|
16
|
+
* @param {string} password - The plain text password.
|
|
17
|
+
* @param {Object} [options={}] - Hashing options.
|
|
18
|
+
* @param {string} [options.driver="scrypt"] - Hashing driver ('scrypt' or 'bcrypt').
|
|
19
|
+
* @param {number} [options.rounds=10] - Salt rounds for bcrypt (if driver is 'bcrypt').
|
|
20
|
+
* @param {number} [options.N=16384] - CPU/memory cost parameter for scrypt.
|
|
21
|
+
* @param {number} [options.r=8] - Block size for scrypt.
|
|
22
|
+
* @param {number} [options.p=1] - Parallelization parameter for scrypt.
|
|
23
|
+
* @returns {Promise<string>} Formatted password hash string.
|
|
24
|
+
* @example
|
|
25
|
+
* const hash = await Hash.make("mySecretPassword");
|
|
26
|
+
* const bcryptHash = await Hash.make("mySecretPassword", { driver: "bcrypt" });
|
|
27
|
+
*/
|
|
28
|
+
static async make(password, options = {}) {
|
|
29
|
+
if (typeof password !== "string" || !password) {
|
|
30
|
+
throw new Error("[HASH ERROR] Password must be a non-empty string.");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const driver = (options.driver || "scrypt").toLowerCase();
|
|
34
|
+
|
|
35
|
+
if (driver === "bcrypt") {
|
|
36
|
+
if (!bcryptModule) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
"[HASH ERROR] 'bcrypt' package is not installed. Run 'npm install bcrypt' or 'npx blue-bird add bcrypt', or use the default scrypt driver.",
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const rounds = options.rounds || 10;
|
|
42
|
+
return bcryptModule.hash(password, rounds);
|
|
43
|
+
}
|
|
44
|
+
const N = options.N || 16384;
|
|
45
|
+
const r = options.r || 8;
|
|
46
|
+
const p = options.p || 1;
|
|
47
|
+
const keylen = 64;
|
|
48
|
+
const salt = crypto.randomBytes(16);
|
|
49
|
+
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
crypto.scrypt(
|
|
52
|
+
password,
|
|
53
|
+
salt,
|
|
54
|
+
keylen,
|
|
55
|
+
{ N, r, p, maxmem: 32 * 1024 * 1024 },
|
|
56
|
+
(err, derivedKey) => {
|
|
57
|
+
if (err) return reject(err);
|
|
58
|
+
const hashString = `$scrypt$N=${N},r=${r},p=${p}$${salt.toString("hex")}$${derivedKey.toString("hex")}`;
|
|
59
|
+
resolve(hashString);
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Alias for make().
|
|
67
|
+
* @param {string} password - The plain text password.
|
|
68
|
+
* @param {Object} [options={}] - Options.
|
|
69
|
+
* @returns {Promise<string>}
|
|
70
|
+
*/
|
|
71
|
+
static async hash(password, options = {}) {
|
|
72
|
+
return this.make(password, options);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Verifies a plain text password against a hash string.
|
|
77
|
+
* Automatically detects scrypt or bcrypt hash formats.
|
|
78
|
+
* Uses timing-safe comparison to protect against side-channel attacks.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} password - The plain text password to check.
|
|
81
|
+
* @param {string} hash - The stored hash string.
|
|
82
|
+
* @returns {Promise<boolean>} True if password matches, false otherwise.
|
|
83
|
+
* @example
|
|
84
|
+
* const isValid = await Hash.verify("mySecretPassword", storedHash);
|
|
85
|
+
*/
|
|
86
|
+
static async verify(password, hash) {
|
|
87
|
+
if (
|
|
88
|
+
typeof password !== "string" ||
|
|
89
|
+
!password ||
|
|
90
|
+
typeof hash !== "string" ||
|
|
91
|
+
!hash
|
|
92
|
+
) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (/^\$2[aby]\$\d{2}\$/.test(hash)) {
|
|
97
|
+
if (!bcryptModule) {
|
|
98
|
+
console.error(
|
|
99
|
+
"[HASH ERROR] A bcrypt hash was detected, but the 'bcrypt' package is not installed. Run 'npm install bcrypt' or 'npx blue-bird add bcrypt'.",
|
|
100
|
+
);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
return await bcryptModule.compare(password, hash);
|
|
105
|
+
} catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (hash.startsWith("$scrypt$")) {
|
|
111
|
+
const parts = hash.split("$");
|
|
112
|
+
if (parts.length !== 5) return false;
|
|
113
|
+
|
|
114
|
+
const paramsStr = parts[2];
|
|
115
|
+
const saltHex = parts[3];
|
|
116
|
+
const originalHashHex = parts[4];
|
|
117
|
+
|
|
118
|
+
if (!paramsStr || !saltHex || !originalHashHex) return false;
|
|
119
|
+
|
|
120
|
+
let N = 16384,
|
|
121
|
+
r = 8,
|
|
122
|
+
p = 1;
|
|
123
|
+
paramsStr.split(",").forEach((param) => {
|
|
124
|
+
const [k, v] = param.split("=");
|
|
125
|
+
if (k === "N") N = parseInt(v, 10) || N;
|
|
126
|
+
if (k === "r") r = parseInt(v, 10) || r;
|
|
127
|
+
if (k === "p") p = parseInt(v, 10) || p;
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const salt = Buffer.from(saltHex, "hex");
|
|
131
|
+
const originalHash = Buffer.from(originalHashHex, "hex");
|
|
132
|
+
|
|
133
|
+
return new Promise((resolve) => {
|
|
134
|
+
crypto.scrypt(
|
|
135
|
+
password,
|
|
136
|
+
salt,
|
|
137
|
+
originalHash.length,
|
|
138
|
+
{ N, r, p, maxmem: 32 * 1024 * 1024 },
|
|
139
|
+
(err, derivedKey) => {
|
|
140
|
+
if (err) return resolve(false);
|
|
141
|
+
try {
|
|
142
|
+
const matches = crypto.timingSafeEqual(originalHash, derivedKey);
|
|
143
|
+
resolve(matches);
|
|
144
|
+
} catch {
|
|
145
|
+
resolve(false);
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Alias for verify().
|
|
157
|
+
* @param {string} password - The plain text password.
|
|
158
|
+
* @param {string} hash - The stored hash string.
|
|
159
|
+
* @returns {Promise<boolean>}
|
|
160
|
+
*/
|
|
161
|
+
static async check(password, hash) {
|
|
162
|
+
return this.verify(password, hash);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Checks if a given hash needs to be rehashed to match updated security parameters.
|
|
167
|
+
* @param {string} hash - The stored hash string.
|
|
168
|
+
* @param {Object} [options={}] - Target options.
|
|
169
|
+
* @returns {boolean} True if the hash should be regenerated.
|
|
170
|
+
*/
|
|
171
|
+
static needsRehash(hash, options = {}) {
|
|
172
|
+
if (!hash || typeof hash !== "string") return true;
|
|
173
|
+
const targetDriver = (options.driver || "scrypt").toLowerCase();
|
|
174
|
+
|
|
175
|
+
if (targetDriver === "bcrypt") {
|
|
176
|
+
return !/^\$2[aby]\$\d{2}\$/.test(hash);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!hash.startsWith("$scrypt$")) return true;
|
|
180
|
+
|
|
181
|
+
const parts = hash.split("$");
|
|
182
|
+
if (parts.length !== 5) return true;
|
|
183
|
+
|
|
184
|
+
const paramsStr = parts[2];
|
|
185
|
+
const targetN = options.N || 16384;
|
|
186
|
+
const targetR = options.r || 8;
|
|
187
|
+
const targetP = options.p || 1;
|
|
188
|
+
|
|
189
|
+
return !paramsStr.includes(`N=${targetN},r=${targetR},p=${targetP}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { Hash };
|
|
194
|
+
export default Hash;
|
package/core/html.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export class SafeHtml {
|
|
2
|
+
value: string;
|
|
3
|
+
constructor(value: string);
|
|
4
|
+
toString(): string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function isSafeHtml(val: any): val is SafeHtml;
|
|
8
|
+
|
|
9
|
+
export function escapeHtml(value: any): string;
|
|
10
|
+
|
|
11
|
+
export function safeJson(data: any): string;
|
|
12
|
+
|
|
13
|
+
export interface HtmlTag {
|
|
14
|
+
(strings: TemplateStringsArray, ...values: any[]): SafeHtml;
|
|
15
|
+
raw(str: string): SafeHtml;
|
|
16
|
+
json(obj: any): SafeHtml;
|
|
17
|
+
script: typeof script;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const html: HtmlTag;
|
|
21
|
+
|
|
22
|
+
export interface ScriptTag {
|
|
23
|
+
(strings: TemplateStringsArray, ...values: any[]): SafeHtml;
|
|
24
|
+
(src: string): SafeHtml;
|
|
25
|
+
(options: { src: string; defer?: boolean; module?: boolean }): SafeHtml;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const script: ScriptTag;
|
|
29
|
+
|
|
30
|
+
export function cached(
|
|
31
|
+
key: string,
|
|
32
|
+
options: number | { ttl?: number; vary?: string[] },
|
|
33
|
+
renderFn: () => SafeHtml | string | Promise<SafeHtml | string>
|
|
34
|
+
): Promise<SafeHtml>;
|
|
35
|
+
|
|
36
|
+
export function defer<T>(
|
|
37
|
+
promise: Promise<T>,
|
|
38
|
+
fallback: SafeHtml | string,
|
|
39
|
+
renderer: (data: T) => SafeHtml | string | Promise<SafeHtml | string>
|
|
40
|
+
): { __bluebird_deferred__: true; promise: Promise<T>; fallback: string; renderer: Function };
|
|
41
|
+
|
|
42
|
+
export default html;
|
package/core/html.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import RenderCache from "./render-cache.js";
|
|
2
|
+
|
|
3
|
+
const SAFE_HTML_KEY = Symbol.for("Bluebird.SafeHtml");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wrapper class representing a trusted HTML string that bypasses escaping.
|
|
7
|
+
*/
|
|
8
|
+
export class SafeHtml {
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} value
|
|
11
|
+
*/
|
|
12
|
+
constructor(value) {
|
|
13
|
+
this.value = typeof value === "string" ? value : String(value ?? "");
|
|
14
|
+
this[SAFE_HTML_KEY] = true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns the raw inner HTML string.
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
toString() {
|
|
22
|
+
return this.value;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether a given value is an instance of SafeHtml.
|
|
28
|
+
* @param {any} val
|
|
29
|
+
* @returns {boolean}
|
|
30
|
+
*/
|
|
31
|
+
export function isSafeHtml(val) {
|
|
32
|
+
return Boolean(val && (val instanceof SafeHtml || val[SAFE_HTML_KEY] === true));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Escapes HTML special characters to prevent XSS.
|
|
37
|
+
* @param {any} value
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
export function escapeHtml(value) {
|
|
41
|
+
if (value === null || value === undefined) return "";
|
|
42
|
+
const str = typeof value === "string" ? value : String(value);
|
|
43
|
+
return str
|
|
44
|
+
.replace(/&/g, "&")
|
|
45
|
+
.replace(/</g, "<")
|
|
46
|
+
.replace(/>/g, ">")
|
|
47
|
+
.replace(/"/g, """)
|
|
48
|
+
.replace(/'/g, "'");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Serializes data into a secure JSON string suitable for script tags and HTML attributes.
|
|
53
|
+
* Prevents HTML/script closing tag injection.
|
|
54
|
+
* @param {any} data
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
export function safeJson(data) {
|
|
58
|
+
return JSON.stringify(data)
|
|
59
|
+
.replace(/</g, "\\u003c")
|
|
60
|
+
.replace(/>/g, "\\u003e")
|
|
61
|
+
.replace(/&/g, "\\u0026")
|
|
62
|
+
.replace(/\u2028/g, "\\u2028")
|
|
63
|
+
.replace(/\u2029/g, "\\u2029");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Flattens and stringifies a template literal value.
|
|
68
|
+
* @param {any} val
|
|
69
|
+
* @returns {string}
|
|
70
|
+
*/
|
|
71
|
+
function resolveValue(val) {
|
|
72
|
+
if (val === null || val === undefined || val === false) {
|
|
73
|
+
return "";
|
|
74
|
+
}
|
|
75
|
+
if (val === true) {
|
|
76
|
+
return "";
|
|
77
|
+
}
|
|
78
|
+
if (isSafeHtml(val)) {
|
|
79
|
+
return val.toString();
|
|
80
|
+
}
|
|
81
|
+
if (Array.isArray(val)) {
|
|
82
|
+
return val.map(resolveValue).join("");
|
|
83
|
+
}
|
|
84
|
+
if (typeof val === "function") {
|
|
85
|
+
throw new TypeError(
|
|
86
|
+
"[BLUEBIRD HTML] Functions cannot be interpolated directly in html`...`. Did you mean to call it like ${myFunc()}?"
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return escapeHtml(val);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Tagged template literal for creating secure HTML strings.
|
|
94
|
+
* All interpolated values are escaped by default unless wrapped in SafeHtml or html.raw().
|
|
95
|
+
*
|
|
96
|
+
* @param {TemplateStringsArray} strings
|
|
97
|
+
* @param {...any} values
|
|
98
|
+
* @returns {SafeHtml}
|
|
99
|
+
*/
|
|
100
|
+
export function html(strings, ...values) {
|
|
101
|
+
let result = "";
|
|
102
|
+
const len = strings.length;
|
|
103
|
+
|
|
104
|
+
for (let i = 0; i < len; i++) {
|
|
105
|
+
result += strings[i];
|
|
106
|
+
if (i < values.length) {
|
|
107
|
+
result += resolveValue(values[i]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return new SafeHtml(result);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Marks a string as explicitly safe, bypassing HTML escaping.
|
|
116
|
+
* @param {string} str
|
|
117
|
+
* @returns {SafeHtml}
|
|
118
|
+
*/
|
|
119
|
+
html.raw = function raw(str) {
|
|
120
|
+
return new SafeHtml(str);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Generates an escaped JSON string for embedding inside HTML attributes or scripts.
|
|
125
|
+
* @param {any} obj
|
|
126
|
+
* @returns {SafeHtml}
|
|
127
|
+
*/
|
|
128
|
+
html.json = function json(obj) {
|
|
129
|
+
return new SafeHtml(safeJson(obj));
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Tagged template literal and helper for creating inline client scripts safely.
|
|
134
|
+
* Interpolated values are safely converted to JSON literals to prevent code injection.
|
|
135
|
+
* Escapes closing </script> tags automatically.
|
|
136
|
+
*
|
|
137
|
+
* @param {TemplateStringsArray|string|Object} first
|
|
138
|
+
* @param {...any} values
|
|
139
|
+
* @returns {SafeHtml}
|
|
140
|
+
*/
|
|
141
|
+
export function script(first, ...values) {
|
|
142
|
+
if (Array.isArray(first) && "raw" in first) {
|
|
143
|
+
let code = "";
|
|
144
|
+
for (let i = 0; i < first.length; i++) {
|
|
145
|
+
code += first[i];
|
|
146
|
+
if (i < values.length) {
|
|
147
|
+
const val = values[i];
|
|
148
|
+
if (isSafeHtml(val)) {
|
|
149
|
+
code += val.toString();
|
|
150
|
+
} else {
|
|
151
|
+
code += safeJson(val);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const safeCode = code.replace(/<\/script/gi, "<\\/script");
|
|
156
|
+
return new SafeHtml(`<script>${safeCode}</script>`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (typeof first === "string") {
|
|
160
|
+
const src = first.replace(/"/g, """);
|
|
161
|
+
return new SafeHtml(`<script type="module" src="${src}"></script>`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (first && typeof first === "object" && first.src) {
|
|
165
|
+
const src = String(first.src).replace(/"/g, """);
|
|
166
|
+
const defer = first.defer ? " defer" : "";
|
|
167
|
+
const type = first.module ? ' type="module"' : "";
|
|
168
|
+
return new SafeHtml(`<script${type} src="${src}"${defer}></script>`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return new SafeHtml("");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
html.script = script;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Cached fragment helper. Evaluates renderFn only on cache miss.
|
|
178
|
+
*
|
|
179
|
+
* @param {string} key - Cache key identifier.
|
|
180
|
+
* @param {number|{ttl?: number, vary?: string[]}} options - TTL in seconds or options object.
|
|
181
|
+
* @param {Function} renderFn - Callback returning SafeHtml or string.
|
|
182
|
+
* @returns {Promise<SafeHtml>}
|
|
183
|
+
*/
|
|
184
|
+
export async function cached(key, options, renderFn) {
|
|
185
|
+
const ttl = typeof options === "number" ? options : options?.ttl || 300;
|
|
186
|
+
const cacheKey = `fragment:${key}`;
|
|
187
|
+
|
|
188
|
+
const existing = await RenderCache.lookup(cacheKey);
|
|
189
|
+
if (existing !== null) {
|
|
190
|
+
return new SafeHtml(existing);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const output = await renderFn();
|
|
194
|
+
const renderedString = output instanceof SafeHtml ? output.toString() : String(output ?? "");
|
|
195
|
+
await RenderCache.store(cacheKey, renderedString, ttl);
|
|
196
|
+
|
|
197
|
+
return new SafeHtml(renderedString);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Deferred streaming token for progressive chunked rendering.
|
|
202
|
+
*
|
|
203
|
+
* @param {Promise<any>} promise - Asynchronous data promise.
|
|
204
|
+
* @param {SafeHtml|string} fallback - Initial placeholder HTML chunk.
|
|
205
|
+
* @param {Function} renderer - Callback taking resolved data and returning SafeHtml or string.
|
|
206
|
+
* @returns {Object}
|
|
207
|
+
*/
|
|
208
|
+
export function defer(promise, fallback, renderer) {
|
|
209
|
+
return {
|
|
210
|
+
__bluebird_deferred__: true,
|
|
211
|
+
promise,
|
|
212
|
+
fallback: fallback instanceof SafeHtml ? fallback.toString() : String(fallback ?? ""),
|
|
213
|
+
renderer,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export default html;
|