elegance-js 1.5.0 → 1.6.1

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/dist/build.d.ts CHANGED
@@ -4,6 +4,7 @@ type CompilationOptions = {
4
4
  environment: "production" | "development";
5
5
  pagesDirectory: string;
6
6
  outputDirectory: string;
7
+ quiet?: boolean;
7
8
  publicDirectory?: {
8
9
  path: string;
9
10
  method: "symlink" | "recursive-copy";
package/dist/build.mjs CHANGED
@@ -103,7 +103,7 @@ async function handleApiRequest(root, pathname, req, res) {
103
103
  const apiSubPath = pathname.slice("/api/".length);
104
104
  const parts = apiSubPath.split("/").filter(Boolean);
105
105
  const routeDir = join(root, pathname);
106
- const routePath = join(routeDir, "route.js");
106
+ const routePath = join(routeDir, "route.mjs");
107
107
  let hasRoute = false;
108
108
  try {
109
109
  await fs.access(routePath);
@@ -131,7 +131,7 @@ async function handleApiRequest(root, pathname, req, res) {
131
131
  }
132
132
  const middlewares = [];
133
133
  for (const dir of middlewareDirs) {
134
- const mwPath = join(dir, "middleware.js");
134
+ const mwPath = join(dir, "middleware.mjs");
135
135
  let mwModule;
136
136
  try {
137
137
  await fs.access(mwPath);
@@ -143,7 +143,7 @@ async function handleApiRequest(root, pathname, req, res) {
143
143
  const mwKeys = Object.keys(mwModule).sort();
144
144
  for (const key of mwKeys) {
145
145
  const f = mwModule[key];
146
- if (typeof f === "function") {
146
+ if (typeof f === "function" && !middlewares.some((existing) => existing === f)) {
147
147
  middlewares.push(f);
148
148
  }
149
149
  }
@@ -163,7 +163,7 @@ async function handleApiRequest(root, pathname, req, res) {
163
163
  function composeMiddlewares(mws, final) {
164
164
  return async function(req, res) {
165
165
  let index = 0;
166
- const dispatch = async (err) => {
166
+ async function dispatch(err) {
167
167
  if (err) {
168
168
  return respondWithJsonError(res, 500, err.message || "Internal Server Error");
169
169
  }
@@ -171,12 +171,24 @@ function composeMiddlewares(mws, final) {
171
171
  return await final(req, res);
172
172
  }
173
173
  const thisMw = mws[index++];
174
+ const next = (e) => dispatch(e);
175
+ const onceNext = (nextFn) => {
176
+ let called = false;
177
+ return async (e) => {
178
+ if (called) {
179
+ console.warn("next() called more than once");
180
+ return;
181
+ }
182
+ called = true;
183
+ await nextFn(e);
184
+ };
185
+ };
174
186
  try {
175
- await thisMw(req, res, dispatch);
187
+ await thisMw(req, res, onceNext(next));
176
188
  } catch (error) {
177
189
  await dispatch(error);
178
190
  }
179
- };
191
+ }
180
192
  await dispatch();
181
193
  };
182
194
  }
@@ -244,6 +256,7 @@ var green = (text) => {
244
256
  return `\x1B[38;2;65;224;108m${text}`;
245
257
  };
246
258
  var log = (...text) => {
259
+ if (options.quiet === true) return;
247
260
  return console.log(text.map((text2) => `${text2}\x1B[0m`).join(""));
248
261
  };
249
262
  var options = process.env.OPTIONS;
@@ -106,7 +106,7 @@ async function handleApiRequest(root, pathname, req, res) {
106
106
  const apiSubPath = pathname.slice("/api/".length);
107
107
  const parts = apiSubPath.split("/").filter(Boolean);
108
108
  const routeDir = join(root, pathname);
109
- const routePath = join(routeDir, "route.js");
109
+ const routePath = join(routeDir, "route.mjs");
110
110
  let hasRoute = false;
111
111
  try {
112
112
  await fs.access(routePath);
@@ -134,7 +134,7 @@ async function handleApiRequest(root, pathname, req, res) {
134
134
  }
135
135
  const middlewares = [];
136
136
  for (const dir of middlewareDirs) {
137
- const mwPath = join(dir, "middleware.js");
137
+ const mwPath = join(dir, "middleware.mjs");
138
138
  let mwModule;
139
139
  try {
140
140
  await fs.access(mwPath);
@@ -146,7 +146,7 @@ async function handleApiRequest(root, pathname, req, res) {
146
146
  const mwKeys = Object.keys(mwModule).sort();
147
147
  for (const key of mwKeys) {
148
148
  const f = mwModule[key];
149
- if (typeof f === "function") {
149
+ if (typeof f === "function" && !middlewares.some((existing) => existing === f)) {
150
150
  middlewares.push(f);
151
151
  }
152
152
  }
@@ -166,7 +166,7 @@ async function handleApiRequest(root, pathname, req, res) {
166
166
  function composeMiddlewares(mws, final) {
167
167
  return async function(req, res) {
168
168
  let index = 0;
169
- const dispatch = async (err) => {
169
+ async function dispatch(err) {
170
170
  if (err) {
171
171
  return respondWithJsonError(res, 500, err.message || "Internal Server Error");
172
172
  }
@@ -174,12 +174,24 @@ function composeMiddlewares(mws, final) {
174
174
  return await final(req, res);
175
175
  }
176
176
  const thisMw = mws[index++];
177
+ const next = (e) => dispatch(e);
178
+ const onceNext = (nextFn) => {
179
+ let called = false;
180
+ return async (e) => {
181
+ if (called) {
182
+ console.warn("next() called more than once");
183
+ return;
184
+ }
185
+ called = true;
186
+ await nextFn(e);
187
+ };
188
+ };
177
189
  try {
178
- await thisMw(req, res, dispatch);
190
+ await thisMw(req, res, onceNext(next));
179
191
  } catch (error) {
180
192
  await dispatch(error);
181
193
  }
182
- };
194
+ }
183
195
  await dispatch();
184
196
  };
185
197
  }
@@ -247,6 +259,7 @@ var green = (text) => {
247
259
  return `\x1B[38;2;65;224;108m${text}`;
248
260
  };
249
261
  var log = (...text) => {
262
+ if (options.quiet === true) return;
250
263
  return console.log(text.map((text2) => `${text2}\x1B[0m`).join(""));
251
264
  };
252
265
  var options = process.env.OPTIONS;
@@ -741,6 +741,7 @@ var build = async () => {
741
741
  bundle: false,
742
742
  outbase: path.join(options.pagesDirectory, "/api"),
743
743
  outdir: path.join(DIST_DIR, "/api"),
744
+ outExtension: { ".js": ".mjs" },
744
745
  loader: {
745
746
  ".js": "js",
746
747
  ".ts": "ts"
@@ -762,6 +763,7 @@ var build = async () => {
762
763
  bundle: false,
763
764
  outbase: path.join(options.pagesDirectory, "/api"),
764
765
  outdir: path.join(DIST_DIR, "/api"),
766
+ outExtension: { ".js": ".mjs" },
765
767
  loader: {
766
768
  ".js": "js",
767
769
  ".ts": "ts"
@@ -96,7 +96,7 @@ async function handleApiRequest(root, pathname, req, res) {
96
96
  const apiSubPath = pathname.slice("/api/".length);
97
97
  const parts = apiSubPath.split("/").filter(Boolean);
98
98
  const routeDir = join(root, pathname);
99
- const routePath = join(routeDir, "route.js");
99
+ const routePath = join(routeDir, "route.mjs");
100
100
  let hasRoute = false;
101
101
  try {
102
102
  await fs.access(routePath);
@@ -124,7 +124,7 @@ async function handleApiRequest(root, pathname, req, res) {
124
124
  }
125
125
  const middlewares = [];
126
126
  for (const dir of middlewareDirs) {
127
- const mwPath = join(dir, "middleware.js");
127
+ const mwPath = join(dir, "middleware.mjs");
128
128
  let mwModule;
129
129
  try {
130
130
  await fs.access(mwPath);
@@ -136,7 +136,7 @@ async function handleApiRequest(root, pathname, req, res) {
136
136
  const mwKeys = Object.keys(mwModule).sort();
137
137
  for (const key of mwKeys) {
138
138
  const f = mwModule[key];
139
- if (typeof f === "function") {
139
+ if (typeof f === "function" && !middlewares.some((existing) => existing === f)) {
140
140
  middlewares.push(f);
141
141
  }
142
142
  }
@@ -156,7 +156,7 @@ async function handleApiRequest(root, pathname, req, res) {
156
156
  function composeMiddlewares(mws, final) {
157
157
  return async function(req, res) {
158
158
  let index = 0;
159
- const dispatch = async (err) => {
159
+ async function dispatch(err) {
160
160
  if (err) {
161
161
  return respondWithJsonError(res, 500, err.message || "Internal Server Error");
162
162
  }
@@ -164,12 +164,24 @@ function composeMiddlewares(mws, final) {
164
164
  return await final(req, res);
165
165
  }
166
166
  const thisMw = mws[index++];
167
+ const next = (e) => dispatch(e);
168
+ const onceNext = (nextFn) => {
169
+ let called = false;
170
+ return async (e) => {
171
+ if (called) {
172
+ console.warn("next() called more than once");
173
+ return;
174
+ }
175
+ called = true;
176
+ await nextFn(e);
177
+ };
178
+ };
167
179
  try {
168
- await thisMw(req, res, dispatch);
180
+ await thisMw(req, res, onceNext(next));
169
181
  } catch (error) {
170
182
  await dispatch(error);
171
183
  }
172
- };
184
+ }
173
185
  await dispatch();
174
186
  };
175
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elegance-js",
3
- "version": "1.5.0",
3
+ "version": "1.6.1",
4
4
  "description": "Web-Framework",
5
5
  "type": "module",
6
6
  "bin": {