effortless-aws 0.2.1 → 0.4.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/README.md +152 -37
- package/dist/{chunk-7JVA4742.js → chunk-AHRNISIY.js} +4 -0
- package/dist/cli/index.js +197 -21
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +118 -29
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime/wrap-http.js +19 -4
- package/dist/runtime/wrap-site.js +132 -0
- package/dist/runtime/wrap-table-stream.js +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createHandlerRuntime
|
|
3
|
+
} from "../chunk-AHRNISIY.js";
|
|
4
|
+
|
|
5
|
+
// src/runtime/wrap-site.ts
|
|
6
|
+
import { readFileSync, existsSync } from "fs";
|
|
7
|
+
import { join, extname, resolve } from "path";
|
|
8
|
+
var CONTENT_TYPES = {
|
|
9
|
+
// Text
|
|
10
|
+
".html": "text/html; charset=utf-8",
|
|
11
|
+
".htm": "text/html; charset=utf-8",
|
|
12
|
+
".css": "text/css; charset=utf-8",
|
|
13
|
+
".js": "application/javascript; charset=utf-8",
|
|
14
|
+
".mjs": "application/javascript; charset=utf-8",
|
|
15
|
+
".json": "application/json; charset=utf-8",
|
|
16
|
+
".xml": "application/xml; charset=utf-8",
|
|
17
|
+
".txt": "text/plain; charset=utf-8",
|
|
18
|
+
".csv": "text/csv; charset=utf-8",
|
|
19
|
+
".md": "text/markdown; charset=utf-8",
|
|
20
|
+
".svg": "image/svg+xml; charset=utf-8",
|
|
21
|
+
// Images (binary)
|
|
22
|
+
".png": "image/png",
|
|
23
|
+
".jpg": "image/jpeg",
|
|
24
|
+
".jpeg": "image/jpeg",
|
|
25
|
+
".gif": "image/gif",
|
|
26
|
+
".webp": "image/webp",
|
|
27
|
+
".avif": "image/avif",
|
|
28
|
+
".ico": "image/x-icon",
|
|
29
|
+
".bmp": "image/bmp",
|
|
30
|
+
// Fonts (binary)
|
|
31
|
+
".woff": "font/woff",
|
|
32
|
+
".woff2": "font/woff2",
|
|
33
|
+
".ttf": "font/ttf",
|
|
34
|
+
".otf": "font/otf",
|
|
35
|
+
".eot": "application/vnd.ms-fontobject",
|
|
36
|
+
// Media (binary)
|
|
37
|
+
".mp4": "video/mp4",
|
|
38
|
+
".webm": "video/webm",
|
|
39
|
+
".mp3": "audio/mpeg",
|
|
40
|
+
".ogg": "audio/ogg",
|
|
41
|
+
// Other
|
|
42
|
+
".pdf": "application/pdf",
|
|
43
|
+
".wasm": "application/wasm",
|
|
44
|
+
".map": "application/json",
|
|
45
|
+
".gz": "application/gzip",
|
|
46
|
+
".zip": "application/zip"
|
|
47
|
+
};
|
|
48
|
+
var BINARY_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
49
|
+
".png",
|
|
50
|
+
".jpg",
|
|
51
|
+
".jpeg",
|
|
52
|
+
".gif",
|
|
53
|
+
".webp",
|
|
54
|
+
".avif",
|
|
55
|
+
".ico",
|
|
56
|
+
".bmp",
|
|
57
|
+
".woff",
|
|
58
|
+
".woff2",
|
|
59
|
+
".ttf",
|
|
60
|
+
".otf",
|
|
61
|
+
".eot",
|
|
62
|
+
".mp4",
|
|
63
|
+
".webm",
|
|
64
|
+
".mp3",
|
|
65
|
+
".ogg",
|
|
66
|
+
".pdf",
|
|
67
|
+
".wasm",
|
|
68
|
+
".gz",
|
|
69
|
+
".zip"
|
|
70
|
+
]);
|
|
71
|
+
var wrapSite = (handler) => {
|
|
72
|
+
const { dir, index: indexFile = "index.html", spa = false } = handler.config;
|
|
73
|
+
const rt = createHandlerRuntime({}, "site");
|
|
74
|
+
const baseDir = join(process.cwd(), dir);
|
|
75
|
+
return async (event) => {
|
|
76
|
+
const startTime = Date.now();
|
|
77
|
+
let filePath = event.pathParameters?.["file"] ?? "";
|
|
78
|
+
if (!filePath || filePath.endsWith("/")) {
|
|
79
|
+
filePath = filePath + indexFile;
|
|
80
|
+
}
|
|
81
|
+
const fullPath = resolve(baseDir, filePath);
|
|
82
|
+
if (!fullPath.startsWith(baseDir)) {
|
|
83
|
+
return {
|
|
84
|
+
statusCode: 403,
|
|
85
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
86
|
+
body: "<!DOCTYPE html><html><body><h1>403 Forbidden</h1></body></html>"
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (existsSync(fullPath)) {
|
|
90
|
+
return serveFile(fullPath, filePath, rt, startTime);
|
|
91
|
+
}
|
|
92
|
+
if (spa && !extname(filePath)) {
|
|
93
|
+
const spaPath = join(baseDir, indexFile);
|
|
94
|
+
if (existsSync(spaPath)) {
|
|
95
|
+
return serveFile(spaPath, indexFile, rt, startTime);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
rt.logError(startTime, { path: filePath }, "File not found");
|
|
99
|
+
return {
|
|
100
|
+
statusCode: 404,
|
|
101
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
102
|
+
body: `<!DOCTYPE html><html><body><h1>404 Not Found</h1><p>${filePath}</p></body></html>`
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
function serveFile(fullPath, filePath, rt, startTime) {
|
|
107
|
+
const ext = extname(filePath).toLowerCase();
|
|
108
|
+
const contentType = CONTENT_TYPES[ext] ?? "application/octet-stream";
|
|
109
|
+
const binary = BINARY_EXTENSIONS.has(ext);
|
|
110
|
+
const isHtml = ext === ".html" || ext === ".htm";
|
|
111
|
+
const cacheControl = isHtml ? "public, max-age=0, must-revalidate" : "public, max-age=31536000, immutable";
|
|
112
|
+
if (binary) {
|
|
113
|
+
const body2 = readFileSync(fullPath).toString("base64");
|
|
114
|
+
rt.logExecution(startTime, { path: filePath }, { status: 200, binary: true });
|
|
115
|
+
return {
|
|
116
|
+
statusCode: 200,
|
|
117
|
+
headers: { "Content-Type": contentType, "Cache-Control": cacheControl },
|
|
118
|
+
body: body2,
|
|
119
|
+
isBase64Encoded: true
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const body = readFileSync(fullPath, "utf-8");
|
|
123
|
+
rt.logExecution(startTime, { path: filePath }, { status: 200 });
|
|
124
|
+
return {
|
|
125
|
+
statusCode: 200,
|
|
126
|
+
headers: { "Content-Type": contentType, "Cache-Control": cacheControl },
|
|
127
|
+
body
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export {
|
|
131
|
+
wrapSite
|
|
132
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "effortless-aws",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Code-first AWS Lambda framework",
|
|
@@ -69,6 +69,7 @@
|
|
|
69
69
|
"prebuild": "pnpm run gen",
|
|
70
70
|
"build": "tsup",
|
|
71
71
|
"typecheck": "tsc --noEmit",
|
|
72
|
+
"pretest": "tsup",
|
|
72
73
|
"test": "vitest run",
|
|
73
74
|
"link": "pnpm build && pnpm link --global"
|
|
74
75
|
}
|