ultimate-express 2.0.9 → 2.0.11

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/src/view.js CHANGED
@@ -1,126 +1,126 @@
1
- /*
2
- Copyright 2024 dimden.dev
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
16
-
17
- const path = require("path");
18
- const fs = require("fs");
19
- const { NullObject } = require("./utils.js");
20
-
21
- module.exports = class View {
22
- constructor(name, options) {
23
- this.name = name;
24
- this.options = options ? Object.assign({}, options) : new NullObject();
25
- this.defaultEngine = options.defaultEngine;
26
- this.ext = path.extname(name);
27
- this.root = options.root;
28
-
29
- if (!this.ext && !this.defaultEngine) {
30
- throw new Error('No default engine was specified and no extension was provided.');
31
- }
32
-
33
- let fileName = name;
34
- if(!this.ext) {
35
- this.ext = this.defaultEngine[0] !== '.'
36
- ? '.' + this.defaultEngine
37
- : this.defaultEngine;
38
-
39
- fileName += this.ext;
40
- }
41
-
42
- if (!this.options.engines[this.ext]) {
43
- const mod = this.ext.slice(1);
44
-
45
- // default engine export
46
- const fn = require(mod).__express;
47
-
48
- if (typeof fn !== 'function') {
49
- throw new Error('Module "' + mod + '" does not provide a view engine.')
50
- }
51
-
52
- this.options.engines[this.ext] = fn;
53
- }
54
-
55
- this.engine = this.options.engines[this.ext];
56
- if(path.isAbsolute(name)) {
57
- this.path = name;
58
- if(path.extname(name) === '') {
59
- this.path += this.ext;
60
- }
61
- } else {
62
- this.path = this.lookup(fileName);
63
- }
64
- }
65
-
66
- lookup(name) {
67
- let _path;
68
- let roots = [].concat(this.root);
69
- for (let i = 0; i < roots.length && !_path; i++) {
70
- const root = roots[i];
71
-
72
- // resolve the path
73
- const loc = path.resolve(root, name);
74
- const dir = path.dirname(loc);
75
- const file = path.basename(loc);
76
-
77
- // resolve the file
78
- _path = this.resolve(dir, file);
79
- }
80
- return _path;
81
- }
82
-
83
- // ill be real idk what exactly this does but express implements it this way
84
- render(options, callback) {
85
- let sync = true;
86
- this.engine(this.path, options, function onRender() {
87
- if(!sync) {
88
- return callback.apply(this, arguments);
89
- }
90
-
91
- return process.nextTick(() => {
92
- return callback.apply(this, arguments);
93
- });
94
- });
95
-
96
- sync = false;
97
- }
98
-
99
- resolve(dir, file) {
100
- const ext = this.ext;
101
-
102
- // <path>.<ext>
103
- let _path = path.join(dir, file);
104
- let stat = tryStat(_path);
105
-
106
- if(stat && stat.isFile()) {
107
- return _path;
108
- }
109
-
110
- // <path>/index.<ext>
111
- _path = path.join(dir, path.basename(file, ext), 'index' + ext);
112
- stat = tryStat(_path);
113
-
114
- if(stat && stat.isFile()) {
115
- return _path;
116
- }
117
- }
118
- }
119
-
120
- function tryStat(path) {
121
- try {
122
- return fs.statSync(path);
123
- } catch (e) {
124
- return undefined;
125
- }
1
+ /*
2
+ Copyright 2024 dimden.dev
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ const path = require("path");
18
+ const fs = require("fs");
19
+ const { NullObject } = require("./utils.js");
20
+
21
+ module.exports = class View {
22
+ constructor(name, options) {
23
+ this.name = name;
24
+ this.options = options ? Object.assign({}, options) : new NullObject();
25
+ this.defaultEngine = options.defaultEngine;
26
+ this.ext = path.extname(name);
27
+ this.root = options.root;
28
+
29
+ if (!this.ext && !this.defaultEngine) {
30
+ throw new Error('No default engine was specified and no extension was provided.');
31
+ }
32
+
33
+ let fileName = name;
34
+ if(!this.ext) {
35
+ this.ext = this.defaultEngine[0] !== '.'
36
+ ? '.' + this.defaultEngine
37
+ : this.defaultEngine;
38
+
39
+ fileName += this.ext;
40
+ }
41
+
42
+ if (!this.options.engines[this.ext]) {
43
+ const mod = this.ext.slice(1);
44
+
45
+ // default engine export
46
+ const fn = require(mod).__express;
47
+
48
+ if (typeof fn !== 'function') {
49
+ throw new Error('Module "' + mod + '" does not provide a view engine.')
50
+ }
51
+
52
+ this.options.engines[this.ext] = fn;
53
+ }
54
+
55
+ this.engine = this.options.engines[this.ext];
56
+ if(path.isAbsolute(name)) {
57
+ this.path = name;
58
+ if(path.extname(name) === '') {
59
+ this.path += this.ext;
60
+ }
61
+ } else {
62
+ this.path = this.lookup(fileName);
63
+ }
64
+ }
65
+
66
+ lookup(name) {
67
+ let _path;
68
+ let roots = [].concat(this.root);
69
+ for (let i = 0; i < roots.length && !_path; i++) {
70
+ const root = roots[i];
71
+
72
+ // resolve the path
73
+ const loc = path.resolve(root, name);
74
+ const dir = path.dirname(loc);
75
+ const file = path.basename(loc);
76
+
77
+ // resolve the file
78
+ _path = this.resolve(dir, file);
79
+ }
80
+ return _path;
81
+ }
82
+
83
+ // ill be real idk what exactly this does but express implements it this way
84
+ render(options, callback) {
85
+ let sync = true;
86
+ this.engine(this.path, options, function onRender() {
87
+ if(!sync) {
88
+ return callback.apply(this, arguments);
89
+ }
90
+
91
+ return process.nextTick(() => {
92
+ return callback.apply(this, arguments);
93
+ });
94
+ });
95
+
96
+ sync = false;
97
+ }
98
+
99
+ resolve(dir, file) {
100
+ const ext = this.ext;
101
+
102
+ // <path>.<ext>
103
+ let _path = path.join(dir, file);
104
+ let stat = tryStat(_path);
105
+
106
+ if(stat && stat.isFile()) {
107
+ return _path;
108
+ }
109
+
110
+ // <path>/index.<ext>
111
+ _path = path.join(dir, path.basename(file, ext), 'index' + ext);
112
+ stat = tryStat(_path);
113
+
114
+ if(stat && stat.isFile()) {
115
+ return _path;
116
+ }
117
+ }
118
+ }
119
+
120
+ function tryStat(path) {
121
+ try {
122
+ return fs.statSync(path);
123
+ } catch (e) {
124
+ return undefined;
125
+ }
126
126
  }
package/src/worker.js CHANGED
@@ -1,30 +1,30 @@
1
- /*
2
- Copyright 2024 dimden.dev
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
16
-
17
- const fs = require("fs");
18
- const { parentPort } = require("worker_threads");
19
-
20
- parentPort.on('message', (message) => {
21
- if(message.type === 'readFile') {
22
- try {
23
- const data = fs.readFileSync(message.path);
24
- const ab = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
25
- parentPort.postMessage({ key: message.key, data: ab }, [ab]);
26
- } catch(err) {
27
- parentPort.postMessage({ key: message.key, err: String(err) });
28
- }
29
- }
30
- });
1
+ /*
2
+ Copyright 2024 dimden.dev
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ const fs = require("fs");
18
+ const { parentPort } = require("worker_threads");
19
+
20
+ parentPort.on('message', (message) => {
21
+ if(message.type === 'readFile') {
22
+ try {
23
+ const data = fs.readFileSync(message.path);
24
+ const ab = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
25
+ parentPort.postMessage({ key: message.key, data: ab }, [ab]);
26
+ } catch(err) {
27
+ parentPort.postMessage({ key: message.key, err: String(err) });
28
+ }
29
+ }
30
+ });