lien 3.4.4 → 5.0.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/lib/to-buffer.js CHANGED
@@ -1,61 +1,48 @@
1
- "use strict";
1
+ /**
2
+ * @typedef {object} SerializedContent
3
+ * @property {Buffer} buffer The encoded response body.
4
+ * @property {string} contentType The default Express content type.
5
+ */
2
6
 
3
7
  /**
4
- * ToBuffer
5
- * Converts data to buffer.
8
+ * Serializes response content to a Buffer and selects its default content type.
9
+ * Undefined produces an empty body; other non-string values are serialized as JSON.
6
10
  *
7
- * @name ToBuffer
8
- * @function
9
- * @param {Anything} data The value that will be converted to buffer.
10
- * @return {Object} An object containing the `contentType` and `buffer` fields.
11
+ * @param {*} data The response content.
12
+ * @returns {SerializedContent} The encoded body and content type.
13
+ * @throws {Error} If data is an Error or cannot be serialized.
11
14
  */
12
- var ToBuffer = module.exports = function (data) {
13
-
14
- /**
15
- * createDataObj
16
- *
17
- * @name createDataObj
18
- * @function
19
- * @param {Buffer} buffer The buffer value.
20
- * @param {Object} contentType The response contentType.
21
- * @return {Object} An object containing the `contentType` and `buffer` fields.
22
- */
23
- function createDataObj(buffer, contentType) {
15
+ export default function toBuffer (data) {
16
+ if (data === undefined) {
24
17
  return {
25
- contentType: contentType || "html",
26
- buffer: buffer
18
+ buffer: Buffer.alloc(0),
19
+ contentType: "html"
27
20
  };
28
21
  }
29
22
 
30
- if (data === undefined) {
31
- return createDataObj(new Buffer(0));
23
+ if (Buffer.isBuffer(data)) {
24
+ return {
25
+ buffer: data,
26
+ contentType: "html"
27
+ };
32
28
  }
33
29
 
34
30
  if (typeof data === "string") {
35
- if (/^\<\?xml version\=/.test(data)) {
36
- return createDataObj(new Buffer(data), "xml");
37
- }
38
- return createDataObj(new Buffer(data));
39
- }
40
-
41
- if (data instanceof Buffer) {
42
- return createDataObj(data);
31
+ return {
32
+ buffer: Buffer.from(data),
33
+ contentType: /^<\?xml version=/.test(data) ? "xml" : "html"
34
+ };
43
35
  }
44
36
 
45
37
  if (data instanceof Error) {
46
- return createDataObj(data.stack, "text");
47
- }
48
-
49
- try {
50
- data = JSON.stringify(data);
51
- return createDataObj(data, "json");
52
- } catch (err) {
53
- return createDataObj(new Buffer(0));
38
+ throw data;
54
39
  }
55
-
56
- if (data === undefined) {
57
- return createDataObj(new Buffer(0));
40
+ const json = JSON.stringify(data);
41
+ if (json === undefined) {
42
+ throw new TypeError("Response content is not serializable.");
58
43
  }
59
-
60
- return createDataObj(new Buffer(data));
61
- };
44
+ return {
45
+ buffer: Buffer.from(json),
46
+ contentType: "json"
47
+ };
48
+ }
package/lib/utils.js ADDED
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Property names excluded from session merges and lookups.
3
+ *
4
+ * @private
5
+ * @type {Set<string>}
6
+ */
7
+ const blockedKeys = new Set(["__proto__", "constructor", "prototype"]);
8
+
9
+ /**
10
+ * Merges session records recursively, ignoring undefined values and prototype keys.
11
+ * Returns a new record; arrays and other non-record values replace the previous value.
12
+ *
13
+ * @param {object|null|undefined} base Existing values.
14
+ * @param {object|null|undefined} update New values.
15
+ * @returns {Object<string, *>} The merged record.
16
+ * @private
17
+ */
18
+ export function mergeData (base, update) {
19
+ const result = {};
20
+ for (const source of [base, update]) {
21
+ if (!source || typeof source !== "object") {
22
+ continue;
23
+ }
24
+ for (const [key, value] of Object.entries(source)) {
25
+ if (blockedKeys.has(key) || value === undefined) {
26
+ continue;
27
+ }
28
+ result[key] = isRecord(value) ? mergeData(result[key], value) : value;
29
+ }
30
+ }
31
+ return result;
32
+ }
33
+
34
+ /**
35
+ * Checks for a plain object or an object with a null prototype.
36
+ *
37
+ * @private
38
+ * @param {*} value The value to check.
39
+ * @returns {boolean}
40
+ */
41
+ function isRecord (value) {
42
+ return value !== null && typeof value === "object" && [Object.prototype, null].includes(Object.getPrototypeOf(value));
43
+ }
44
+
45
+ /**
46
+ * Reads an own-property path without traversing prototypes or losing falsy values.
47
+ *
48
+ * @param {object|null|undefined} object The source record.
49
+ * @param {string} field The dot-separated path.
50
+ * @returns {*} The value, or null when absent or blocked.
51
+ * @private
52
+ */
53
+ export function getData (object, field) {
54
+ for (const key of field.replace(/\.$/, "").split(".")) {
55
+ if (blockedKeys.has(key) || object == null || !Object.hasOwn(object, key)) {
56
+ return null;
57
+ }
58
+ object = object[key];
59
+ }
60
+ return object ?? null;
61
+ }
62
+
63
+ /**
64
+ * Keeps Error objects or wraps another failure value as the cause of a new Error.
65
+ *
66
+ * @param {*} value The failure reason.
67
+ * @param {string} [message] The fallback message.
68
+ * @returns {Error} The original or wrapped error.
69
+ * @private
70
+ */
71
+ export function asError (value, message = "Operation failed without an error.") {
72
+ return value instanceof Error ? value : new Error(message, {
73
+ cause: value
74
+ });
75
+ }
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "scripts": {
3
- "test": "node test/index.js"
3
+ "test": "node --test --test-timeout=15000 test/*.test.js",
4
+ "test:coverage": "node --test --test-timeout=15000 --experimental-test-coverage test/*.test.js"
4
5
  },
5
6
  "name": "lien",
6
- "version": "3.4.4",
7
+ "version": "5.0.0",
7
8
  "description": "An easy to use web framework for Node.js.",
8
9
  "main": "lib/index.js",
9
10
  "keywords": [
@@ -20,10 +21,7 @@
20
21
  "test": "test"
21
22
  },
22
23
  "devDependencies": {
23
- "ajs": "^1.2.1",
24
- "connect-mongo": "^4.6.0",
25
- "tester": "^1.3.4",
26
- "tinyreq": "^3.1.2"
24
+ "connect-mongo": "^6.0.0"
27
25
  },
28
26
  "repository": {
29
27
  "type": "git",
@@ -34,21 +32,36 @@
34
32
  },
35
33
  "homepage": "https://github.com/LienJS/Lien",
36
34
  "blah": {
37
- "h_img": "https://raw.githubusercontent.com/LienJS/Resources/master/logo/header.png"
35
+ "h_img": "assets/logo.png"
38
36
  },
39
37
  "dependencies": {
40
- "body-parser": "^1.15.0",
41
- "cookie-parser": "^1.4.3",
42
- "csurf": "^1.9.0",
43
- "express": "^4.13.4",
44
- "express-session": "^1.13.0",
45
- "find-value": "^1.0.1",
46
- "iterate-object": "^1.3.2",
47
- "methods": "^1.1.2",
48
- "noop6": "^1.0.2",
49
- "path-to-regexp": "^6.2.1",
50
- "set-or-get": "^1.2.2",
51
- "transformer": "^1.0.1",
52
- "ul": "^5.2.1"
53
- }
38
+ "@dr.pogodin/csurf": "^1.18.2",
39
+ "cookie-parser": "^1.4.7",
40
+ "express": "^5.3.0",
41
+ "express-session": "^1.19.0",
42
+ "path-to-regexp": "^9.0.0"
43
+ },
44
+ "type": "module",
45
+ "engines": {
46
+ "node": ">=22.12.0"
47
+ },
48
+ "exports": {
49
+ ".": "./lib/index.js",
50
+ "./package.json": "./package.json",
51
+ "./lib/index.js": "./lib/index.js",
52
+ "./lib/hooks.js": "./lib/hooks.js",
53
+ "./lib/to-buffer.js": "./lib/to-buffer.js",
54
+ "./lib": "./lib/index.js",
55
+ "./lib/index": "./lib/index.js",
56
+ "./lib/hooks": "./lib/hooks.js",
57
+ "./lib/to-buffer": "./lib/to-buffer.js"
58
+ },
59
+ "files": [
60
+ "assets/",
61
+ "lib/",
62
+ "example/",
63
+ "README.md",
64
+ "DOCUMENTATION.md",
65
+ "LICENSE"
66
+ ]
54
67
  }
@@ -1,4 +0,0 @@
1
- github: ionicabizau
2
- patreon: ionicabizau
3
- open_collective: ionicabizau
4
- custom: https://www.buymeacoffee.com/h96wwchmy
package/.travis.yml DELETED
@@ -1,3 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - 7
package/CONTRIBUTING.md DELETED
@@ -1,41 +0,0 @@
1
- # Contributing :star: :star2: :dizzy:
2
-
3
- So, you want to contribute to this project? That's awesome! However, before doing so, please read the following simple steps on how to contribute. This will make life easier and avoid wasting time on things that are not requested. ✨
4
-
5
- ## Discuss the changes before making them
6
- To begin, open an issue in the repository using the [bug tracker][1]. Describe the contribution you'd like to make, the bug you've found, or any other ideas you have. This step will help us get you started on the right track.
7
-
8
- If it is relevant, include platform and software information (e.g., operating system, Node.JS version, etc.) and screenshots to help us understand what you're experiencing.
9
-
10
- We recommend waiting for feedback before proceeding to the next steps. However, if the issue is clear, such as a typo, and the fix is simple, you can go ahead and fix it.
11
-
12
- ## Fixing Issues
13
- Begin by forking the project to your own account, and create a branch for your fix, naming it either `some-great-feature` or `some-issue-fix`.
14
-
15
- Commit your changes to that branch, adhering to the [code style][2]. If the project includes tests (usually located in the `test` directory), we encourage you to add a test as well. :memo:
16
-
17
- If the project includes a `package.json` or a `bower.json` file, add yourself to the `contributors` array (or `authors` in the case of `bower.json`). If the array doesn't exist, create it as shown below:
18
-
19
- ```json
20
- {
21
- "contributors": [
22
- "Your Name <your@email.address> (http://your.website)"
23
- ]
24
- }
25
- ```
26
-
27
- ## Creating a Pull Request
28
- Start by opening a pull request and make sure to reference the initial issue in the pull request message (e.g., *fixes #<your-issue-number>*). Provide a clear and descriptive title to help everyone understand what is being fixed or improved.
29
-
30
- If applicable, consider adding screenshots, gifs, or any other visual aids that can make it easier to understand the changes you've made.
31
-
32
- ## Wait for Feedback
33
- Before accepting your contributions, we will review them. You may receive feedback regarding what needs to be addressed in your modified code. If so, simply continue making commits to your branch, and the pull request will be automatically updated.
34
-
35
- ## Everyone Is Happy!
36
- Ultimately, your contributions will be merged, and everyone will be delighted! 😄 Contributions are more than welcome!
37
-
38
- Thanks! :sweat_smile:
39
-
40
- [1]: /issues
41
- [2]: https://github.com/IonicaBizau/code-style
package/test/index.js DELETED
@@ -1,280 +0,0 @@
1
- "use strict";
2
-
3
- var tester = require("tester"),
4
- Lien = require(".."),
5
- request = require("tinyreq"),
6
- fs = require("fs");
7
-
8
- var URL = "http://localhost:9000/";
9
-
10
- tester.describe("lien", function (t) {
11
-
12
- var server = null;
13
-
14
- // Init lien server
15
- t.should("start the server", function (cb) {
16
-
17
- // Start the server
18
- server = new Lien({
19
- host: "localhost",
20
- port: 9000,
21
- public: __dirname + "/public",
22
- views: {
23
- path: __dirname + "/views",
24
- name: "ajs"
25
- }
26
- });
27
-
28
- // Listen for load
29
- server.on("load", cb);
30
-
31
- // Add handler page
32
- server.addPage("/", function (lien) {
33
- lien.end("Hello World");
34
- });
35
-
36
- // Add a dynamic route
37
- server.addPage("/post/:id", function (lien) {
38
- if (lien.fromBeforeHook) {
39
- var res = "1";
40
- if (lien.anotherFromBeforeHook) {
41
- res += "1";
42
- }
43
- return lien.end(res);
44
- }
45
- lien.end("Post id: " + lien.params.id);
46
- });
47
-
48
- // Add a static file
49
- server.addPage("/test", "index.html");
50
- server.errorPages();
51
-
52
- server.on("serverError", function (err) {
53
- console.log(err.stack);
54
- });
55
- });
56
-
57
- t.it("add a static url to test lien fields", function (cb) {
58
- var url = URL + "templates/bar?foo=bar";
59
- server.addPage("/templates/bar", function (lien) {
60
- t.expect(lien.href).toBe(url);
61
- t.expect(lien.path).toBe("/templates/bar");
62
- t.expect(lien.full_path).toBe("/templates/bar?foo=bar");
63
- t.expect(lien.host).toBe("localhost");
64
- t.expect(lien.protocol).toBe("http");
65
- t.expect(lien.domain).toBe("http://localhost:9000");
66
- t.expect(lien.query.foo).toBe("bar");
67
- lien.end();
68
- });
69
- request(url, cb);
70
- });
71
-
72
- t.it("add a static url which renders templates", function () {
73
- server.addPage("/templates/foo", function (lien) {
74
- lien.render("main", { page: "Home" });
75
- });
76
- });
77
-
78
- t.should("static route", function (cb) {
79
- request(URL, function (err, body, res) {
80
- t.expect(body).toBe("Hello World");
81
- t.expect(res.statusCode).toBe(200);
82
- cb();
83
- });
84
- });
85
-
86
- t.should("serve file", function (cb) {
87
- request(URL + "test", function (err, body, res) {
88
- t.expect(body).toBe(fs.readFileSync(__dirname + "/public/index.html", "utf8"));
89
- t.expect(res.statusCode).toBe(200);
90
- cb();
91
- });
92
- });
93
-
94
- t.should("check before adding the hook", function (cb) {
95
- request(URL + "post/12", function (err, body, res) {
96
- t.expect(body).toBe("Post id: 12");
97
- t.expect(res.statusCode).toBe(200);
98
- cb();
99
- });
100
- });
101
-
102
- t.should("add before hook", function () {
103
- server.hook("before", "/post/:id", "get", function (lien) {
104
- lien.fromBeforeHook = true;
105
- });
106
- });
107
-
108
- t.should("check after the before hook", function (cb) {
109
- request(URL + "post/12", function (err, body, res) {
110
- t.expect(body).toBe("1");
111
- t.expect(res.statusCode).toBe(200);
112
- cb();
113
- });
114
- });
115
-
116
- t.should("add before hook", function () {
117
- server.hook("before", "(.*)", "get", function (lien) {
118
- lien.anotherFromBeforeHook = true;
119
- });
120
- });
121
-
122
- t.should("check after the before hook", function (cb) {
123
- request(URL + "post/12", function (err, body, res) {
124
- t.expect(body).toBe("11");
125
- t.expect(res.statusCode).toBe(200);
126
- cb();
127
- });
128
- });
129
-
130
- t.should("add after hook", function () {
131
- server.hook("after", "/post/:id", "get", function (res, next) {
132
- res.content = "hi";
133
- next();
134
- });
135
- });
136
-
137
- t.should("check after hook", function (cb) {
138
- request(URL + "post/12", function (err, body, res) {
139
- t.expect(body).toBe("hi");
140
- t.expect(res.statusCode).toBe(200);
141
- cb();
142
- });
143
- });
144
-
145
- t.should("add different pages", function () {
146
- var articleIds = {
147
- a: "Hello World!",
148
- b: "Hello Mars!"
149
- };
150
- server.addPage("/foo/article/:id", function (lien) {
151
- var content = articleIds[lien.params.id];
152
- if (content) {
153
- lien.end(content);
154
- } else {
155
- lien.next();
156
- }
157
- });
158
- });
159
-
160
- t.it("possible-404 page (foo/article/a)", function (cb) {
161
- request(URL + "foo/article/a", function (err, body, res) {
162
- t.expect(body).toBe("Hello World!");
163
- t.expect(res.statusCode).toBe(200);
164
- cb();
165
- });
166
- });
167
-
168
- t.it("possible-404 page (foo/article/b)", function (cb) {
169
- request(URL + "foo/article/b", function (err, body, res) {
170
- t.expect(body).toBe("Hello Mars!");
171
- t.expect(res.statusCode).toBe(200);
172
- cb();
173
- });
174
- });
175
-
176
- t.it("possible-404 page (foo/article/c)", function (cb) {
177
- request(URL + "foo/article/c", function (err, body, res) {
178
- t.expect(err).toBe(null);
179
- t.expect(body.match("404")).toNotBe(null);
180
- t.expect(res.statusCode).toBe(404);
181
- cb();
182
- });
183
- });
184
-
185
- t.should("add different pages, overriding the public dir", function () {
186
- server.addPage("/foo/bar", function (lien) {
187
- lien.end("foo bar");
188
- });
189
- server.addPage("/foo/:dyn", function (lien) {
190
- lien.end("foo:" + lien.params.dyn);
191
- });
192
- });
193
-
194
- t.it("check foo index before adding the custom route", function (cb) {
195
- request(URL + "foo", function (err, body, res) {
196
- t.expect(body).toBe("html foo\n");
197
- t.expect(res.statusCode).toBe(200);
198
- cb();
199
- });
200
- });
201
-
202
- t.it("load static HTML file", function (cb) {
203
- request(URL + "test.html", function (err, body, res) {
204
- t.expect(body).toBe("test\n");
205
- t.expect(res.statusCode).toBe(200);
206
- cb();
207
- });
208
- });
209
-
210
- t.it("check foo/bar ", function (cb) {
211
- request(URL + "foo/bar", function (err, body, res) {
212
- t.expect(body).toBe("foo bar");
213
- t.expect(res.statusCode).toBe(200);
214
- cb();
215
- });
216
- });
217
-
218
- t.should("override public dir", function () {
219
- server.addPage("/foo", function (lien) {
220
- lien.end("foo index");
221
- });
222
- });
223
-
224
- t.it("check foo index after adding the custom route", function (cb) {
225
- request(URL + "foo", function (err, body, res) {
226
- t.expect(body).toBe("foo index");
227
- t.expect(res.statusCode).toBe(200);
228
- cb();
229
- });
230
- });
231
-
232
- t.it("check foo/:dyn ", function (cb) {
233
- request(URL + "foo/baz", function (err, body, res) {
234
- t.expect(body).toBe("foo:baz");
235
- t.expect(res.statusCode).toBe(200);
236
- cb();
237
- });
238
- });
239
-
240
- t.should("add dynamic page", function () {
241
- server.addPage("/:fooo", function (lien) {
242
- lien.end("dynamic:" + lien.params.fooo);
243
- });
244
- });
245
-
246
- t.it("check /:dyn ", function (cb) {
247
- request(URL + "baz", function (err, body, res) {
248
- t.expect(body).toBe("dynamic:baz");
249
- t.expect(res.statusCode).toBe(200);
250
- cb();
251
- });
252
- });
253
-
254
- t.it("load static HTML file", function (cb) {
255
- server.hook("before", ":foo(.*)", function (lien, cb) {
256
- if (!~lien.path.indexOf(".")) {
257
- return cb();
258
- }
259
- lien.next();
260
- });
261
- request(URL + "test.html", function (err, body, res) {
262
- t.expect(body).toBe("test\n");
263
- t.expect(res.statusCode).toBe(200);
264
- cb();
265
- });
266
- });
267
-
268
- t.it("add a static url which renders templates", function (cb) {
269
- request(URL + "templates/foo", function (err, body, res) {
270
- t.expect(body).toBe("<h1>Page: Home</h1>\n");
271
- t.expect(res.statusCode).toBe(200);
272
- cb();
273
- });
274
- });
275
-
276
- t.should("close the server", function (cb) {
277
- cb();
278
- process.exit();
279
- });
280
- });
@@ -1 +0,0 @@
1
- html foo
@@ -1,12 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <title>Lien</title>
6
- </head>
7
- <body>
8
- <center>
9
- This page is served with Lien framework.
10
- </center>
11
- </body>
12
- </html>
@@ -1 +0,0 @@
1
- test
@@ -1 +0,0 @@
1
- <h1>Page: <%= page %></h1>