ghtml 1.5.0 → 1.5.2
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/README.md +18 -2
- package/package.json +2 -2
- package/src/html.js +72 -80
- package/src/includeFile.js +4 -6
package/README.md
CHANGED
|
@@ -86,10 +86,14 @@ import { htmlGenerator as html } from "ghtml";
|
|
|
86
86
|
import { Readable } from "node:stream";
|
|
87
87
|
import http from "node:http";
|
|
88
88
|
|
|
89
|
+
const generator = function* () {
|
|
90
|
+
yield "Hello, World!";
|
|
91
|
+
};
|
|
92
|
+
|
|
89
93
|
http
|
|
90
94
|
.createServer((req, res) => {
|
|
91
95
|
const htmlContent = html`<html>
|
|
92
|
-
<p>${
|
|
96
|
+
<p>${generator()}</p>
|
|
93
97
|
</html>`;
|
|
94
98
|
const readableStream = Readable.from(htmlContent);
|
|
95
99
|
res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
|
|
@@ -102,14 +106,26 @@ http
|
|
|
102
106
|
|
|
103
107
|
```js
|
|
104
108
|
import { htmlAsyncGenerator as html } from "ghtml";
|
|
109
|
+
import { createReadStream } from "node:fs";
|
|
105
110
|
import { readFile } from "node:fs/promises";
|
|
106
111
|
import { Readable } from "node:stream";
|
|
107
112
|
import http from "node:http";
|
|
108
113
|
|
|
114
|
+
const asyncGenerator = async function* () {
|
|
115
|
+
const Hello = await new Promise((resolve) => {
|
|
116
|
+
setTimeout(() => {
|
|
117
|
+
resolve("Hello");
|
|
118
|
+
}, 1000);
|
|
119
|
+
});
|
|
120
|
+
yield `${Hello}!`;
|
|
121
|
+
};
|
|
122
|
+
|
|
109
123
|
http
|
|
110
124
|
.createServer((req, res) => {
|
|
111
125
|
const htmlContent = html`<html>
|
|
112
|
-
<
|
|
126
|
+
<p>${asyncGenerator()}</p>
|
|
127
|
+
<code>${readFile("./README.md", "utf8")}</code>
|
|
128
|
+
<code>${createReadStream("./README.md", "utf8")}</code>
|
|
113
129
|
</html>`;
|
|
114
130
|
const readableStream = Readable.from(htmlContent);
|
|
115
131
|
res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Replace your template engine with fast JavaScript by leveraging the power of tagged templates.",
|
|
4
4
|
"author": "Gürgün Dayıoğlu",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "1.5.
|
|
6
|
+
"version": "1.5.2",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"main": "./src/index.js",
|
|
9
9
|
"exports": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@fastify/pre-commit": "^2.1.0",
|
|
24
24
|
"c8": "^9.1.0",
|
|
25
|
-
"grules": "^0.
|
|
25
|
+
"grules": "^0.16.2",
|
|
26
26
|
"tinybench": "^2.6.0"
|
|
27
27
|
},
|
|
28
28
|
"repository": {
|
package/src/html.js
CHANGED
|
@@ -25,23 +25,24 @@ const html = ({ raw: literals }, ...expressions) => {
|
|
|
25
25
|
let index = 0;
|
|
26
26
|
|
|
27
27
|
for (; index !== expressions.length; ++index) {
|
|
28
|
+
const expression = expressions[index];
|
|
28
29
|
let literal = literals[index];
|
|
29
|
-
let
|
|
30
|
-
|
|
30
|
+
let string =
|
|
31
|
+
expression === undefined || expression === null
|
|
31
32
|
? ""
|
|
32
|
-
: typeof
|
|
33
|
-
?
|
|
34
|
-
: Array.isArray(
|
|
35
|
-
?
|
|
36
|
-
: `${
|
|
33
|
+
: typeof expression === "string"
|
|
34
|
+
? expression
|
|
35
|
+
: Array.isArray(expression)
|
|
36
|
+
? expression.join("")
|
|
37
|
+
: `${expression}`;
|
|
37
38
|
|
|
38
39
|
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
39
40
|
literal = literal.slice(0, -1);
|
|
40
|
-
} else if (
|
|
41
|
-
|
|
41
|
+
} else if (string.length) {
|
|
42
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
accumulator += literal +
|
|
45
|
+
accumulator += literal + string;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
return (accumulator += literals[index]);
|
|
@@ -56,15 +57,16 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
56
57
|
let index = 0;
|
|
57
58
|
|
|
58
59
|
for (; index !== expressions.length; ++index) {
|
|
60
|
+
let expression = expressions[index];
|
|
59
61
|
let literal = literals[index];
|
|
60
|
-
let
|
|
62
|
+
let string;
|
|
61
63
|
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
} else if (typeof
|
|
65
|
-
|
|
64
|
+
if (expression === undefined || expression === null) {
|
|
65
|
+
string = "";
|
|
66
|
+
} else if (typeof expression === "string") {
|
|
67
|
+
string = expression;
|
|
66
68
|
} else {
|
|
67
|
-
if (
|
|
69
|
+
if (expression[Symbol.iterator]) {
|
|
68
70
|
const isRaw =
|
|
69
71
|
literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
|
|
70
72
|
|
|
@@ -76,63 +78,60 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
76
78
|
yield literal;
|
|
77
79
|
}
|
|
78
80
|
|
|
79
|
-
for (
|
|
80
|
-
if (
|
|
81
|
+
for (expression of expression) {
|
|
82
|
+
if (expression === undefined || expression === null) {
|
|
81
83
|
continue;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof expression === "string") {
|
|
87
|
+
string = expression;
|
|
84
88
|
} else {
|
|
85
|
-
if (
|
|
86
|
-
for (
|
|
87
|
-
if (
|
|
89
|
+
if (expression[Symbol.iterator]) {
|
|
90
|
+
for (expression of expression) {
|
|
91
|
+
if (expression === undefined || expression === null) {
|
|
88
92
|
continue;
|
|
89
|
-
} else if (typeof innerValue === "string") {
|
|
90
|
-
expression = innerValue;
|
|
91
|
-
} else {
|
|
92
|
-
expression = `${innerValue}`;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
string = `${expression}`;
|
|
96
|
+
|
|
97
|
+
if (string.length) {
|
|
96
98
|
if (!isRaw) {
|
|
97
|
-
|
|
98
|
-
escapeRegExp,
|
|
99
|
-
escapeFunction,
|
|
100
|
-
);
|
|
99
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
101
100
|
}
|
|
102
101
|
|
|
103
|
-
yield
|
|
102
|
+
yield string;
|
|
104
103
|
}
|
|
105
104
|
}
|
|
106
105
|
|
|
107
106
|
continue;
|
|
108
107
|
}
|
|
109
108
|
|
|
110
|
-
|
|
109
|
+
string = `${expression}`;
|
|
111
110
|
}
|
|
112
111
|
|
|
113
|
-
if (
|
|
112
|
+
if (string.length) {
|
|
114
113
|
if (!isRaw) {
|
|
115
|
-
|
|
114
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
116
115
|
}
|
|
117
116
|
|
|
118
|
-
yield
|
|
117
|
+
yield string;
|
|
119
118
|
}
|
|
120
119
|
}
|
|
121
120
|
|
|
122
121
|
continue;
|
|
123
122
|
}
|
|
124
123
|
|
|
125
|
-
|
|
124
|
+
string = `${expression}`;
|
|
126
125
|
}
|
|
127
126
|
|
|
128
127
|
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
129
128
|
literal = literal.slice(0, -1);
|
|
130
|
-
} else if (
|
|
131
|
-
|
|
129
|
+
} else if (string.length) {
|
|
130
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
132
131
|
}
|
|
133
132
|
|
|
134
|
-
if (literal.length ||
|
|
135
|
-
yield literal +
|
|
133
|
+
if (literal.length || string.length) {
|
|
134
|
+
yield literal + string;
|
|
136
135
|
}
|
|
137
136
|
}
|
|
138
137
|
|
|
@@ -150,20 +149,16 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
150
149
|
let index = 0;
|
|
151
150
|
|
|
152
151
|
for (; index !== expressions.length; ++index) {
|
|
152
|
+
let expression = await expressions[index];
|
|
153
153
|
let literal = literals[index];
|
|
154
|
-
let
|
|
155
|
-
|
|
156
|
-
expressions[index] = await expressions[index];
|
|
154
|
+
let string;
|
|
157
155
|
|
|
158
|
-
if (
|
|
159
|
-
|
|
160
|
-
} else if (typeof
|
|
161
|
-
|
|
156
|
+
if (expression === undefined || expression === null) {
|
|
157
|
+
string = "";
|
|
158
|
+
} else if (typeof expression === "string") {
|
|
159
|
+
string = expression;
|
|
162
160
|
} else {
|
|
163
|
-
if (
|
|
164
|
-
typeof expressions[index][Symbol.iterator] === "function" ||
|
|
165
|
-
typeof expressions[index][Symbol.asyncIterator] === "function"
|
|
166
|
-
) {
|
|
161
|
+
if (expression[Symbol.iterator] || expression[Symbol.asyncIterator]) {
|
|
167
162
|
const isRaw =
|
|
168
163
|
literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
|
|
169
164
|
|
|
@@ -175,66 +170,63 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
175
170
|
yield literal;
|
|
176
171
|
}
|
|
177
172
|
|
|
178
|
-
for await (
|
|
179
|
-
if (
|
|
173
|
+
for await (expression of expression) {
|
|
174
|
+
if (expression === undefined || expression === null) {
|
|
180
175
|
continue;
|
|
181
|
-
}
|
|
182
|
-
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (typeof expression === "string") {
|
|
179
|
+
string = expression;
|
|
183
180
|
} else {
|
|
184
181
|
if (
|
|
185
|
-
|
|
186
|
-
|
|
182
|
+
expression[Symbol.iterator] ||
|
|
183
|
+
expression[Symbol.asyncIterator]
|
|
187
184
|
) {
|
|
188
|
-
for await (
|
|
189
|
-
if (
|
|
185
|
+
for await (expression of expression) {
|
|
186
|
+
if (expression === undefined || expression === null) {
|
|
190
187
|
continue;
|
|
191
|
-
} else if (typeof innerValue === "string") {
|
|
192
|
-
expression = innerValue;
|
|
193
|
-
} else {
|
|
194
|
-
expression = `${innerValue}`;
|
|
195
188
|
}
|
|
196
189
|
|
|
197
|
-
|
|
190
|
+
string = `${expression}`;
|
|
191
|
+
|
|
192
|
+
if (string.length) {
|
|
198
193
|
if (!isRaw) {
|
|
199
|
-
|
|
200
|
-
escapeRegExp,
|
|
201
|
-
escapeFunction,
|
|
202
|
-
);
|
|
194
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
203
195
|
}
|
|
204
196
|
|
|
205
|
-
yield
|
|
197
|
+
yield string;
|
|
206
198
|
}
|
|
207
199
|
}
|
|
208
200
|
|
|
209
201
|
continue;
|
|
210
202
|
}
|
|
211
203
|
|
|
212
|
-
|
|
204
|
+
string = `${expression}`;
|
|
213
205
|
}
|
|
214
206
|
|
|
215
|
-
if (
|
|
207
|
+
if (string.length) {
|
|
216
208
|
if (!isRaw) {
|
|
217
|
-
|
|
209
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
218
210
|
}
|
|
219
211
|
|
|
220
|
-
yield
|
|
212
|
+
yield string;
|
|
221
213
|
}
|
|
222
214
|
}
|
|
223
215
|
|
|
224
216
|
continue;
|
|
225
217
|
}
|
|
226
218
|
|
|
227
|
-
|
|
219
|
+
string = `${expression}`;
|
|
228
220
|
}
|
|
229
221
|
|
|
230
222
|
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
231
223
|
literal = literal.slice(0, -1);
|
|
232
|
-
} else if (
|
|
233
|
-
|
|
224
|
+
} else if (string.length) {
|
|
225
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
234
226
|
}
|
|
235
227
|
|
|
236
|
-
if (literal.length ||
|
|
237
|
-
yield literal +
|
|
228
|
+
if (literal.length || string.length) {
|
|
229
|
+
yield literal + string;
|
|
238
230
|
}
|
|
239
231
|
}
|
|
240
232
|
|
package/src/includeFile.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const fileCache = new Map();
|
|
3
|
+
const cache = new Map();
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* @param {string} path The path to the file to render.
|
|
9
7
|
* @returns {string} The cached content of the file.
|
|
10
8
|
*/
|
|
11
9
|
const includeFile = (path) => {
|
|
12
|
-
let file =
|
|
10
|
+
let file = cache.get(path);
|
|
13
11
|
|
|
14
12
|
if (file === undefined) {
|
|
15
|
-
file = readFileSync(path,
|
|
16
|
-
|
|
13
|
+
file = readFileSync(path, "utf8");
|
|
14
|
+
cache.set(path, file);
|
|
17
15
|
}
|
|
18
16
|
|
|
19
17
|
return file;
|