jeasx 1.2.0 → 1.2.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/package.json +1 -1
- package/serverless.js +9 -11
- package/serverless.ts +20 -25
package/package.json
CHANGED
package/serverless.js
CHANGED
|
@@ -42,26 +42,27 @@ serverless.addHook("onRequest", async (request, reply) => {
|
|
|
42
42
|
const index = request.url.indexOf("?");
|
|
43
43
|
request.path = index === -1 ? request.url : request.url.slice(0, index);
|
|
44
44
|
});
|
|
45
|
-
const routesCache = {};
|
|
46
45
|
const modulesCache = {};
|
|
47
46
|
const cwd = process.cwd();
|
|
48
47
|
serverless.all("*", async (request, reply) => {
|
|
49
48
|
let response;
|
|
50
49
|
const context = {};
|
|
51
50
|
const path = request.path;
|
|
52
|
-
const
|
|
53
|
-
for (const route of routesCache[path] || generateRoutes(path)) {
|
|
51
|
+
for (const route of generateRoutes(path)) {
|
|
54
52
|
const modulePath = join(cwd, "dist", route);
|
|
55
|
-
|
|
53
|
+
let module = modulesCache[modulePath];
|
|
54
|
+
if (module === null) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (module === void 0) {
|
|
56
58
|
try {
|
|
57
59
|
(await stat(modulePath)).isFile();
|
|
58
60
|
} catch {
|
|
61
|
+
if (!NODE_ENV_IS_DEVELOPMENT) {
|
|
62
|
+
modulesCache[modulePath] = null;
|
|
63
|
+
}
|
|
59
64
|
continue;
|
|
60
65
|
}
|
|
61
|
-
}
|
|
62
|
-
routes.push(route);
|
|
63
|
-
let module = modulesCache[modulePath];
|
|
64
|
-
if (!module) {
|
|
65
66
|
if (NODE_ENV_IS_DEVELOPMENT) {
|
|
66
67
|
module = await import(`file://${modulePath}?${createHash("sha1").update(await readFile(modulePath, "utf-8")).digest("hex")}`);
|
|
67
68
|
} else {
|
|
@@ -88,9 +89,6 @@ serverless.all("*", async (request, reply) => {
|
|
|
88
89
|
break;
|
|
89
90
|
}
|
|
90
91
|
}
|
|
91
|
-
if (!NODE_ENV_IS_DEVELOPMENT && reply.statusCode !== 404) {
|
|
92
|
-
routesCache[path] = routes;
|
|
93
|
-
}
|
|
94
92
|
if (!reply.hasHeader("Content-Type")) {
|
|
95
93
|
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
96
94
|
}
|
package/serverless.ts
CHANGED
|
@@ -63,11 +63,8 @@ serverless.addHook("onRequest", async (request, reply) => {
|
|
|
63
63
|
request.path = index === -1 ? request.url : request.url.slice(0, index);
|
|
64
64
|
});
|
|
65
65
|
|
|
66
|
-
// Cache
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
// Cache loaded modules
|
|
70
|
-
const modulesCache: { [path: string]: { default: Function } } = {};
|
|
66
|
+
// Cache loaded modules, null means not found
|
|
67
|
+
const modulesCache: { [path: string]: { default: Function } | null } = {};
|
|
71
68
|
|
|
72
69
|
// Store current working directory
|
|
73
70
|
const cwd = process.cwd();
|
|
@@ -82,37 +79,40 @@ serverless.all("*", async (request, reply) => {
|
|
|
82
79
|
// Current request path
|
|
83
80
|
const path = request.path;
|
|
84
81
|
|
|
85
|
-
// Effective routes for the current path
|
|
86
|
-
const routes: string[] = [];
|
|
87
|
-
|
|
88
82
|
// Execute route handlers for current request
|
|
89
|
-
for (const route of
|
|
83
|
+
for (const route of generateRoutes(path)) {
|
|
90
84
|
const modulePath = join(cwd, "dist", route);
|
|
91
85
|
|
|
92
|
-
//
|
|
93
|
-
|
|
86
|
+
// Resolve module via cache
|
|
87
|
+
let module = modulesCache[modulePath];
|
|
88
|
+
|
|
89
|
+
// Module was cached as not found?
|
|
90
|
+
if (module === null) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Module was not loaded yet?
|
|
95
|
+
if (module === undefined) {
|
|
96
|
+
// Check file existence of module
|
|
94
97
|
try {
|
|
95
98
|
(await stat(modulePath)).isFile();
|
|
96
99
|
} catch {
|
|
100
|
+
if (!NODE_ENV_IS_DEVELOPMENT) {
|
|
101
|
+
// Cache module as not found
|
|
102
|
+
modulesCache[modulePath] = null;
|
|
103
|
+
}
|
|
97
104
|
continue;
|
|
98
105
|
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// The current route exists, so add it to effective routes
|
|
102
|
-
routes.push(route);
|
|
103
106
|
|
|
104
|
-
// Check if module is already loaded
|
|
105
|
-
let module = modulesCache[modulePath];
|
|
106
|
-
if (!module) {
|
|
107
107
|
if (NODE_ENV_IS_DEVELOPMENT) {
|
|
108
|
-
// Use hash to
|
|
108
|
+
// Use file hash to (re)load modified modules in development
|
|
109
109
|
module = await import(
|
|
110
110
|
`file://${modulePath}?${createHash("sha1")
|
|
111
111
|
.update(await readFile(modulePath, "utf-8"))
|
|
112
112
|
.digest("hex")}`
|
|
113
113
|
);
|
|
114
114
|
} else {
|
|
115
|
-
//
|
|
115
|
+
// Load and cache module for non-development
|
|
116
116
|
module = modulesCache[modulePath] = await import(
|
|
117
117
|
`file://${modulePath}`
|
|
118
118
|
);
|
|
@@ -145,11 +145,6 @@ serverless.all("*", async (request, reply) => {
|
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
// Skip caching of routes in development
|
|
149
|
-
if (!NODE_ENV_IS_DEVELOPMENT && reply.statusCode !== 404) {
|
|
150
|
-
routesCache[path] = routes;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
148
|
// Make sure a Content-Type header is set
|
|
154
149
|
if (!reply.hasHeader("Content-Type")) {
|
|
155
150
|
reply.header("Content-Type", "text/html; charset=utf-8");
|