@vandenberghinc/volt 1.2.7 → 1.2.8

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.
@@ -0,0 +1,354 @@
1
+ /*
2
+ * Author: Daan van den Bergh
3
+ * Copyright: © 2022 - 2024 Daan van den Bergh.
4
+ */
5
+
6
+ // ---------------------------------------------------------
7
+ // Imports.
8
+
9
+ const zlib = require('zlib');
10
+
11
+ // ---------------------------------------------------------
12
+ // Response object.
13
+
14
+ /* @docs
15
+ * @nav: Backend
16
+ * @chapter: Stream
17
+ * @title: Response
18
+ * @description: The response object.
19
+ * @deprecated: true
20
+ */
21
+ class Response {
22
+ constructor(res) {
23
+
24
+ // Arguments.
25
+ this.res = res;
26
+
27
+ // Attributes.
28
+ this.cookies = [];
29
+
30
+ // Copy default functions.
31
+ // this.addTrailers = this.res.addTrailers;
32
+ this.cork = this.res.cork.bind(this.res);
33
+ this.end = this.res.end.bind(this.res);
34
+ // this.flushHeaders = this.res.flushHeaders;
35
+ // this.getHeader = this.res.getHeader;
36
+ // this.getHeaderNames = this.res.getHeaderNames;
37
+ // this.getHeaders = this.res.getHeaders;
38
+ // this.hasHeader = this.res.hasHeader;
39
+ // this.removeHeader = this.res.removeHeader;
40
+ // this.setHeader = this.res.setHeader;
41
+ // this.setTimeout = this.res.setTimeout;
42
+ this.uncork = this.res.uncork.bind(this.res);
43
+ this.write = this.res.write.bind(this.res);
44
+ // this.writeContinue = this.res.writeContinue;
45
+ // this.writeEarlyHints = this.res.writeEarlyHints;
46
+ // this.writeHead = this.res.writeHead;
47
+ // this.writeProcessing = this.res.writeProcessing;
48
+
49
+ // Create lowercase functions.
50
+ this.add_trailers = this.res.addTrailers.bind(this.res);
51
+ this.flush_headers = this.res.flushHeaders.bind(this.res);
52
+ this.get_header = this.res.getHeader.bind(this.res);
53
+ this.get_header_names = this.res.getHeaderNames.bind(this.res);
54
+ this.get_headers = this.res.getHeaders.bind(this.res);
55
+ this.has_header = this.res.hasHeader.bind(this.res);
56
+ this.remove_header = this.res.removeHeader.bind(this.res);
57
+ this.set_header = this.res.setHeader.bind(this.res);
58
+ this.set_timeout = this.res.setTimeout;
59
+ // this.write_continue = this.res.writeContinue;
60
+ // this.write_early_hints = this.res.writeEarlyHints;
61
+ // this.write_head = this.res.writeHead;
62
+ // this.write_processing = this.res.writeProcessing;
63
+ }
64
+
65
+ // ---------------------------------------------------------
66
+ // Functions.
67
+
68
+ // Send a response.
69
+ /* @docs:
70
+ * @title: Send
71
+ * @description: Send a response
72
+ * @parameter:
73
+ * @name: status
74
+ * @description: The response status.
75
+ * @type: number
76
+ * @parameter:
77
+ * @name: headers
78
+ * @description: The response headers.
79
+ * @type: object
80
+ * @parameter:
81
+ * @name: data
82
+ * @description: The response data.
83
+ * @type: undefined, string
84
+ * @parameter:
85
+ * @name: compress
86
+ * @description: A boolean indicating if the response data should be compressed.
87
+ * @type: boolean
88
+ * @usage:
89
+ * ...
90
+ * response.send({status: 200, data: "Hello World!"});
91
+ */
92
+ send({status = 200, headers = {}, data = null, compress = false} = {}) {
93
+
94
+ // Set status code.
95
+ this.res.statusCode = status;
96
+
97
+ // Set headers.
98
+ Object.keys(headers).forEach((key) => {
99
+ this.res.setHeader(key, headers[key]);
100
+ });
101
+
102
+ // Set cookies.
103
+ if (this.cookies.length > 0) {
104
+ this.res.setHeader('Set-Cookie', this.cookies);
105
+ }
106
+
107
+ // Convert data.
108
+ let is_json = false;
109
+ if (data != null && typeof data === 'object' && Buffer.isBuffer(data) === false && (data instanceof Uint8Array) === false) {
110
+ is_json = true;
111
+ data = JSON.stringify(data);
112
+ }
113
+
114
+ // @todo compress.
115
+ if (compress) {
116
+ this.res.setHeader("Content-Encoding", "gzip");
117
+ this.res.setHeader("Vary", "Accept-Encoding");
118
+ data = zlib.gzipSync(data, {level: zlib.constants.Z_BEST_COMPRESSION});
119
+ }
120
+
121
+ // Set data.
122
+ if (data != null) {
123
+ if (is_json) {
124
+ this.res.setHeader("Content-Type", "application/json");
125
+ }
126
+ this.res.write(data); // do not use toString() here or it will cause issues with writing binary data.
127
+ }
128
+
129
+ // End.
130
+ this.res.end();
131
+ }
132
+
133
+ // Send a successs response.
134
+ /* @docs:
135
+ * @title: Send Successs
136
+ * @description: Send a response
137
+ * @parameter:
138
+ * @name: status
139
+ * @description: The response status.
140
+ * @type: number
141
+ * @parameter:
142
+ * @name: headers
143
+ * @description: The response headers.
144
+ * @type: object
145
+ * @parameter:
146
+ * @name: data
147
+ * @description: The response data.
148
+ * @type: undefined, string
149
+ * @parameter:
150
+ * @name: compress
151
+ * @description: A boolean indicating if the response data should be compressed.
152
+ * @type: boolean
153
+ * @usage:
154
+ * ...
155
+ * response.success({data: "Hello World!"});
156
+ */
157
+ success({status = 200, headers = {}, data = null, compress = false} = {}) {
158
+ return this.send({status: status, headers: headers, data: data, compress: compress});
159
+ }
160
+
161
+ // Send an error response.
162
+ /* @docs:
163
+ * @title: Send Error
164
+ * @description: Send an error response
165
+ * @parameter:
166
+ * @name: status
167
+ * @description: The response status.
168
+ * @type: number
169
+ * @parameter:
170
+ * @name: headers
171
+ * @description: The response headers.
172
+ * @type: object
173
+ * @parameter:
174
+ * @name: data
175
+ * @description: The response data.
176
+ * @type: undefined, string
177
+ * @parameter:
178
+ * @name: compress
179
+ * @description: A boolean indicating if the response data should be compressed.
180
+ * @type: boolean
181
+ * @usage:
182
+ * ...
183
+ * response.error({data: "Some error occured"});
184
+ */
185
+ error({status = 500, headers = {}, data = null, compress = false} = {}) {
186
+ return this.send({status: status, headers: headers, data: data, compress: compress});
187
+ }
188
+
189
+ // Set headers.
190
+ /* @docs:
191
+ * @title: Set Headers
192
+ * @description: Add new headers to the response data.
193
+ * @parameter:
194
+ * @name: headers
195
+ * @description: The new response headers.
196
+ * @type: object
197
+ * @usage:
198
+ * ...
199
+ * response.set_headers({"Connection": "close"});
200
+ */
201
+ set_headers(headers = {}) {
202
+ if (headers == null) { return null; }
203
+ Object.keys(headers).forEach((key) => {
204
+ this.res.setHeader(key, headers[key]);
205
+ });
206
+ }
207
+
208
+ // Set a cookie.
209
+ /* @docs:
210
+ * @title: Set cookie.
211
+ * @description: Set a cookie that will be sent with the response.
212
+ * @warning: Will only be added to the response when the user uses `send()`, `success()` or `error()`.
213
+ * @parameter:
214
+ * @name: cookie
215
+ * @description: The cookie string.
216
+ * @type: string
217
+ * @usage:
218
+ * ...
219
+ * response.set_cookie("MyCookie=Hello World;");
220
+ */
221
+ set_cookie(cookie) {
222
+ this.cookies.push(cookie);
223
+ }
224
+
225
+ // Set cookies.
226
+ /* @docs:
227
+ * @title: Set Cookies
228
+ * @description: Set a cookie that will be sent with the response.
229
+ * @warning: Will only be added to the response when the user uses `send()`, `success()` or `error()`.
230
+ * @parameter:
231
+ * @name: cookies
232
+ * @description: The cookie strings.
233
+ * @type: ...string
234
+ * @usage:
235
+ * ...
236
+ * response.set_cookies("MyCookie1=Hello World;", "MyCookie2=Hello Universe;");
237
+ */
238
+ set_cookies(cookies) {
239
+ for (let i = 0; i < cookies.length; i++) {
240
+ this.cookies.push(cookies[i]);
241
+ }
242
+ }
243
+
244
+ // ---------------------------------------------------------
245
+ // Defaults.
246
+
247
+ // Create lowercase functions for default getters and setters.
248
+ get headers_sent() {
249
+ return this.res.headersSent;
250
+ }
251
+ set headers_sent(val) {
252
+ this.res.headersSent = val;
253
+ }
254
+ get send_date() {
255
+ return this.res.sendDate;
256
+ }
257
+ set send_date(val) {
258
+ this.res.sendDate = val;
259
+ }
260
+ get status_code() {
261
+ return this.res.statusCode;
262
+ }
263
+ set status_code(val) {
264
+ this.res.statusCode = val;
265
+ }
266
+ get status_message() {
267
+ return this.res.statusMessage;
268
+ }
269
+ set status_message(val) {
270
+ this.res.statusMessage = val;
271
+ }
272
+ get strict_content_length() {
273
+ return this.res.strictContentLength;
274
+ }
275
+ set strict_content_length(val) {
276
+ this.res.strictContentLength = val;
277
+ }
278
+ get writable_ended() {
279
+ return this.res.writableEnded;
280
+ }
281
+ set writable_ended(val) {
282
+ this.res.writableEnded = val;
283
+ }
284
+ get finished() {
285
+ return this.res.finished;
286
+ }
287
+ get writable_finished() {
288
+ return this.res.writableFinished;
289
+ }
290
+ set writable_finished(val) {
291
+ this.res.writableFinished = val;
292
+ }
293
+
294
+ // Copy default functions.
295
+ // get headersSent() {
296
+ // return this.res.headersSent;
297
+ // }
298
+ // set headersSent() {
299
+ // return this.res.headersSent;
300
+ // }
301
+ // get req() {
302
+ // return this.res.req;
303
+ // }
304
+ // set req() {
305
+ // return this.res.req;
306
+ // }
307
+ // get sendDate() {
308
+ // return this.res.sendDate;
309
+ // }
310
+ // set sendDate() {
311
+ // return this.res.sendDate;
312
+ // }
313
+ // get socket() {
314
+ // return this.res.socket;
315
+ // }
316
+ // set socket() {
317
+ // return this.res.socket;
318
+ // }
319
+ // get statusCode() {
320
+ // return this.res.statusCode;
321
+ // }
322
+ // set statusCode() {
323
+ // return this.res.statusCode;
324
+ // }
325
+ // get statusMessage() {
326
+ // return this.res.statusMessage;
327
+ // }
328
+ // set statusMessage() {
329
+ // return this.res.statusMessage;
330
+ // }
331
+ // get strictContentLength() {
332
+ // return this.res.strictContentLength;
333
+ // }
334
+ // set strictContentLength() {
335
+ // return this.res.strictContentLength;
336
+ // }
337
+ // get writableEnded() {
338
+ // return this.res.writableEnded;
339
+ // }
340
+ // set writableEnded() {
341
+ // return this.res.writableEnded;
342
+ // }
343
+ // get writableFinished() {
344
+ // return this.res.writableFinished;
345
+ // }
346
+ // set writableFinished() {
347
+ // return this.res.writableFinished;
348
+ // }
349
+ }
350
+
351
+ // ---------------------------------------------------------
352
+ // Exports.
353
+
354
+ module.exports = Response;
@@ -0,0 +1,58 @@
1
+ // @ts-nocheck
2
+
3
+ // Imports.
4
+ // import * as volt from "@vandenberghinc/volt/frontend";
5
+
6
+ // ---------------------------------------------------------
7
+ // Themes.
8
+
9
+ // Create theme from options.
10
+ const create_theme = (opts: {
11
+ bg: string;
12
+ fg: string;
13
+ primary: string;
14
+ }) => {
15
+ return {
16
+ /**
17
+ * The relative value for font sizes, and the font size used for paragraphs.
18
+ * @warning work with a single fixed font size, and base all other font sizes on this font size using `Theme.multiply("font_size", 1.0)`.
19
+ */
20
+ font_size: 16,
21
+
22
+ /** Code fonts */
23
+ font: "'Mona Sans', sans-serif",
24
+ code_font: "'Menlo', 'Consolas', monospace",
25
+
26
+ /** Colors */
27
+ primary: opts.primary,
28
+ bg: opts.bg,
29
+ bg_0: opts.bg,
30
+ bg_1: new volt.Color(opts.bg).auto_darken_lighten(0.02).rgb(),
31
+ bg_2: new volt.Color(opts.bg).auto_darken_lighten(0.04).rgb(),
32
+ bg_3: new volt.Color(opts.bg).auto_darken_lighten(0.08).rgb(),
33
+ bg_hover: new volt.Color(opts.bg).auto_darken_lighten(0.02).rgb(),
34
+ div_bg: volt.Color.opposite_s(opts.bg).opacity(0.075).rgb(),
35
+ fg: opts.fg,
36
+ fg_0: opts.fg,
37
+ fg_1: new volt.Color(opts.bg).auto_darken_lighten(0.7).rgb(),
38
+ fg_2: new volt.Color(opts.bg).auto_darken_lighten(0.5).rgb(),
39
+
40
+ green: "#60B05B",
41
+ red: "#CA3140",
42
+ yellow: "#F1DA6C", //"#FFFC79",
43
+ }
44
+ }
45
+
46
+ // Initialize the theme.
47
+ export const Theme = new volt.Theme("my_website", {
48
+ dark: create_theme({
49
+ bg: "#0B0E12",
50
+ fg: "#FFFFFF", // #F0F0F0
51
+ primary: `linear-gradient(110deg, #00FDF1, rgb(87, 149, 243) 7.5%, #B96593)`,
52
+ }),
53
+ light: create_theme({
54
+ bg: "#F0F0F0",
55
+ fg: "#0E1D32",
56
+ primary: `linear-gradient(110deg, #00FDF1, rgb(87, 149, 243) 7.5%, #B96593)`,
57
+ }),
58
+ }) as volt.ExtendTheme<ReturnType<typeof create_theme>>;
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Create a single bundle .d.ts file from multiple .d.ts files.
4
+ */
5
+
6
+ import fs from 'fs/promises';
7
+ import { glob } from 'glob';
8
+ import { fileURLToPath } from 'url';
9
+ import * as path from 'path';
10
+ import * as vlib from "@vandenberghinc/vlib"
11
+
12
+ const __filename = fileURLToPath(import.meta.url);
13
+ const __dirname = path.dirname(__filename);
14
+
15
+ async function main() {
16
+
17
+ const target = path.join(__dirname, '..', 'dist/frontend/');
18
+ const patterns = [target + '/**/*.d.ts'];
19
+
20
+ console.log("Target directory:", target)
21
+
22
+ const create_bundle = async (name = "lml_bundle.d.ts", exclude = undefined) => {
23
+ exclude ??= [];
24
+ exclude.push("**/lml_bundle*.d.ts");
25
+ const files = await glob(patterns, { nodir: true, ignore: exclude });
26
+ const bundle = [];
27
+ const out = path.join(target, name);
28
+ console.log("Creating bundle:", out)
29
+ const file_sizes = [];
30
+ for (const file of files) {
31
+ const rel_path = path.relative(target, file);
32
+ if (rel_path === name || (exclude != null && exclude.includes(rel_path))) continue; // Skip output file if re-running
33
+ console.log(" - included", rel_path)
34
+
35
+ // Load.
36
+ let code = await fs.readFile(file, 'utf8');
37
+
38
+ // Process `elements/base.d.ts`
39
+ if (rel_path === 'src/elements/base.d.ts') {
40
+ // only keep everything before the `extend()` function.
41
+ code = code.split("export declare function extend<")[0];
42
+ }
43
+
44
+ // Add to file sizes.
45
+ file_sizes.push({ file: rel_path, size: new vlib.Path(file).size });
46
+
47
+ // Add to file.
48
+ bundle.push(`// ---- ${path.relative(target, file)} ----\n` + code);
49
+ }
50
+
51
+ // Log file sizes
52
+ file_sizes.sort((a, b) => b.size - a.size);
53
+ console.log("Biggest files:");
54
+ for (const fs of file_sizes.slice(0, 10)) {
55
+ console.log(` - ${vlib.System.format_bytes(fs.size).padStart(10)} ${fs.file}`);
56
+ }
57
+
58
+ // Write.
59
+ await fs.writeFile(out, bundle.join('\n\n'), 'utf8');
60
+ console.log(`Bundled ${files.length} .d.ts files into ${out} with a size of ${vlib.System.format_bytes(new vlib.Path(out).size)}.`);
61
+ }
62
+
63
+ await create_bundle("lml_bundle.d.ts");
64
+ await create_bundle("lml_bundle.ui.d.ts", ["src/modules/paddle.d.ts"]);
65
+ }
66
+
67
+ main().catch(err => {
68
+ console.error(err);
69
+ process.exit(1);
70
+ });
71
+