effortless-aws 0.9.0 → 0.10.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/dist/{chunk-B4P7ZKNM.js → chunk-HINWSFWC.js} +105 -20
- package/dist/cli/index.js +193 -63
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +191 -77
- package/dist/index.js.map +1 -1
- package/dist/runtime/wrap-app.js +1 -1
- package/dist/runtime/wrap-fifo-queue.js +1 -1
- package/dist/runtime/wrap-http.js +1 -1
- package/dist/runtime/wrap-middleware.js +83 -0
- package/dist/runtime/wrap-table-stream.js +19 -7
- package/package.json +1 -1
package/dist/runtime/wrap-app.js
CHANGED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/runtime/wrap-middleware.ts
|
|
2
|
+
var parseHeaders = (cfHeaders) => {
|
|
3
|
+
const result = {};
|
|
4
|
+
for (const [key, values] of Object.entries(cfHeaders)) {
|
|
5
|
+
if (values.length > 0) {
|
|
6
|
+
result[key] = values[0].value;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
var parseCookies = (cfHeaders) => {
|
|
12
|
+
const cookies = {};
|
|
13
|
+
const cookieHeaders = cfHeaders.cookie;
|
|
14
|
+
if (!cookieHeaders) return cookies;
|
|
15
|
+
for (const { value } of cookieHeaders) {
|
|
16
|
+
for (const pair of value.split(";")) {
|
|
17
|
+
const eqIdx = pair.indexOf("=");
|
|
18
|
+
if (eqIdx === -1) continue;
|
|
19
|
+
const name = pair.slice(0, eqIdx).trim();
|
|
20
|
+
const val = pair.slice(eqIdx + 1).trim();
|
|
21
|
+
if (name) cookies[name] = val;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return cookies;
|
|
25
|
+
};
|
|
26
|
+
var rewriteUrl = (uri) => {
|
|
27
|
+
if (uri.endsWith("/")) return uri + "index.html";
|
|
28
|
+
if (!uri.includes(".")) return uri + "/index.html";
|
|
29
|
+
return uri;
|
|
30
|
+
};
|
|
31
|
+
var isRedirect = (result) => result != null && "redirect" in result;
|
|
32
|
+
var isDeny = (result) => result != null && "status" in result && result.status === 403;
|
|
33
|
+
var wrapMiddleware = (handler) => {
|
|
34
|
+
const middleware = handler.__spec.middleware;
|
|
35
|
+
return async (event) => {
|
|
36
|
+
const cfRequest = event.Records[0].cf.request;
|
|
37
|
+
const request = {
|
|
38
|
+
uri: cfRequest.uri,
|
|
39
|
+
method: cfRequest.method,
|
|
40
|
+
querystring: cfRequest.querystring,
|
|
41
|
+
headers: parseHeaders(cfRequest.headers),
|
|
42
|
+
cookies: parseCookies(cfRequest.headers)
|
|
43
|
+
};
|
|
44
|
+
try {
|
|
45
|
+
const result = await middleware(request);
|
|
46
|
+
if (isRedirect(result)) {
|
|
47
|
+
const statusCode = result.status ?? 302;
|
|
48
|
+
const descriptions = {
|
|
49
|
+
301: "Moved Permanently",
|
|
50
|
+
302: "Found",
|
|
51
|
+
307: "Temporary Redirect",
|
|
52
|
+
308: "Permanent Redirect"
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
status: String(statusCode),
|
|
56
|
+
statusDescription: descriptions[statusCode] ?? "Found",
|
|
57
|
+
headers: {
|
|
58
|
+
location: [{ key: "Location", value: result.redirect }]
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (isDeny(result)) {
|
|
63
|
+
return {
|
|
64
|
+
status: "403",
|
|
65
|
+
statusDescription: "Forbidden",
|
|
66
|
+
body: result.body ?? "Forbidden"
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
cfRequest.uri = rewriteUrl(cfRequest.uri);
|
|
70
|
+
return cfRequest;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error("Middleware error:", error);
|
|
73
|
+
return {
|
|
74
|
+
status: "500",
|
|
75
|
+
statusDescription: "Internal Server Error",
|
|
76
|
+
body: "Internal Server Error"
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
export {
|
|
82
|
+
wrapMiddleware
|
|
83
|
+
};
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createHandlerRuntime,
|
|
3
3
|
createTableClient
|
|
4
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-HINWSFWC.js";
|
|
5
5
|
|
|
6
6
|
// src/runtime/wrap-table-stream.ts
|
|
7
7
|
import { unmarshall } from "@aws-sdk/util-dynamodb";
|
|
8
|
+
var toTableItem = (raw, decode) => ({
|
|
9
|
+
pk: raw.pk,
|
|
10
|
+
sk: raw.sk,
|
|
11
|
+
tag: raw.tag,
|
|
12
|
+
data: decode(raw.data),
|
|
13
|
+
...raw.ttl !== void 0 ? { ttl: raw.ttl } : {}
|
|
14
|
+
});
|
|
8
15
|
var parseRecords = (rawRecords, schema) => {
|
|
9
16
|
const records = [];
|
|
10
17
|
const sequenceNumbers = /* @__PURE__ */ new Map();
|
|
@@ -13,11 +20,12 @@ var parseRecords = (rawRecords, schema) => {
|
|
|
13
20
|
if (!rawRecord.dynamodb?.Keys) continue;
|
|
14
21
|
const newImage = rawRecord.dynamodb?.NewImage ? unmarshall(rawRecord.dynamodb.NewImage) : void 0;
|
|
15
22
|
const oldImage = rawRecord.dynamodb?.OldImage ? unmarshall(rawRecord.dynamodb.OldImage) : void 0;
|
|
23
|
+
const keys = unmarshall(rawRecord.dynamodb.Keys);
|
|
16
24
|
const record = {
|
|
17
25
|
eventName: rawRecord.eventName,
|
|
18
|
-
new: newImage !== void 0 ?
|
|
19
|
-
old: oldImage !== void 0 ?
|
|
20
|
-
keys
|
|
26
|
+
new: newImage !== void 0 ? toTableItem(newImage, decode) : void 0,
|
|
27
|
+
old: oldImage !== void 0 ? toTableItem(oldImage, decode) : void 0,
|
|
28
|
+
keys,
|
|
21
29
|
sequenceNumber: rawRecord.dynamodb?.SequenceNumber,
|
|
22
30
|
timestamp: rawRecord.dynamodb?.ApproximateCreationDateTime
|
|
23
31
|
};
|
|
@@ -41,16 +49,20 @@ var wrapTableStream = (handler) => {
|
|
|
41
49
|
if (!handler.onRecord && !handler.onBatch) {
|
|
42
50
|
throw new Error("wrapTableStream requires a handler with onRecord or onBatch defined");
|
|
43
51
|
}
|
|
44
|
-
const
|
|
45
|
-
const handleError = handler.onError ?? ((e) => console.error(`[effortless:${rt.handlerName}]`, e));
|
|
52
|
+
const tagField = handler.__spec.tagField ?? "tag";
|
|
46
53
|
let selfClient = null;
|
|
47
54
|
const getSelfClient = () => {
|
|
48
55
|
if (selfClient) return selfClient;
|
|
49
56
|
const tableName = process.env[ENV_TABLE_SELF];
|
|
50
57
|
if (!tableName) return void 0;
|
|
51
|
-
selfClient = createTableClient(tableName);
|
|
58
|
+
selfClient = createTableClient(tableName, { tagField });
|
|
52
59
|
return selfClient;
|
|
53
60
|
};
|
|
61
|
+
const rt = createHandlerRuntime(handler, "table", handler.__spec.logLevel ?? "info", () => {
|
|
62
|
+
const table = getSelfClient();
|
|
63
|
+
return table ? { table } : {};
|
|
64
|
+
});
|
|
65
|
+
const handleError = handler.onError ?? ((e) => console.error(`[effortless:${rt.handlerName}]`, e));
|
|
54
66
|
return async (event) => {
|
|
55
67
|
const startTime = Date.now();
|
|
56
68
|
rt.patchConsole();
|