effortless-aws 0.6.0 → 0.7.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.
@@ -99,20 +99,7 @@ var createTableClient = (tableName) => {
99
99
  };
100
100
  };
101
101
 
102
- // src/runtime/platform-types.ts
103
- var ENV_PLATFORM_TABLE = "EFF_PLATFORM_TABLE";
104
- var DEFAULT_TTL_SECONDS = 7 * 24 * 60 * 60;
105
- var truncateForStorage = (value, maxLength = 4096) => {
106
- if (value === void 0 || value === null) return value;
107
- const str = typeof value === "string" ? value : JSON.stringify(value);
108
- if (str.length <= maxLength) return value;
109
- return str.slice(0, maxLength) + "...[truncated]";
110
- };
111
- var dateBucket = (date = /* @__PURE__ */ new Date()) => date.toISOString().slice(0, 10);
112
- var computeTtl = (ttlSeconds = DEFAULT_TTL_SECONDS) => Math.floor(Date.now() / 1e3) + ttlSeconds;
113
-
114
102
  // src/runtime/handler-utils.ts
115
- import { randomUUID } from "crypto";
116
103
  import { readFileSync } from "fs";
117
104
  import { join } from "path";
118
105
 
@@ -137,92 +124,16 @@ var getParameters = async (names) => {
137
124
  return map;
138
125
  };
139
126
 
140
- // src/runtime/platform-client.ts
141
- import { DynamoDB as DynamoDB2 } from "@aws-sdk/client-dynamodb";
142
- import { marshall as marshall2, unmarshall as unmarshall2 } from "@aws-sdk/util-dynamodb";
143
- var createPlatformClient = () => {
144
- const tableName = process.env[ENV_PLATFORM_TABLE];
145
- if (!tableName) return void 0;
146
- let client2 = null;
147
- const getClient2 = () => client2 ??= new DynamoDB2({});
148
- const appendToList = async (handlerName, handlerType, listAttr, entry) => {
149
- const sk = `EXEC#${dateBucket()}`;
150
- try {
151
- await getClient2().updateItem({
152
- TableName: tableName,
153
- Key: marshall2({ pk: `HANDLER#${handlerName}`, sk }),
154
- UpdateExpression: "SET #list = list_append(if_not_exists(#list, :empty), :entry), #type = :type, #hn = :hn, #ht = :ht, #ttl = :ttl",
155
- ExpressionAttributeNames: {
156
- "#list": listAttr,
157
- "#type": "type",
158
- "#hn": "handlerName",
159
- "#ht": "handlerType",
160
- "#ttl": "ttl"
161
- },
162
- ExpressionAttributeValues: marshall2(
163
- {
164
- ":entry": [entry],
165
- ":empty": [],
166
- ":type": "execution-log",
167
- ":hn": handlerName,
168
- ":ht": handlerType,
169
- ":ttl": computeTtl()
170
- },
171
- { removeUndefinedValues: true }
172
- )
173
- });
174
- } catch (err) {
175
- console.error("[effortless] Failed to write platform record:", err);
176
- }
177
- };
178
- return {
179
- tableName,
180
- async appendExecution(handlerName, handlerType, entry) {
181
- await appendToList(handlerName, handlerType, "executions", entry);
182
- },
183
- async appendError(handlerName, handlerType, entry) {
184
- await appendToList(handlerName, handlerType, "errors", entry);
185
- },
186
- async get(pk, sk) {
187
- const result = await getClient2().getItem({
188
- TableName: tableName,
189
- Key: marshall2({ pk, sk })
190
- });
191
- return result.Item ? unmarshall2(result.Item) : void 0;
192
- },
193
- async query(pk, skPrefix) {
194
- const names = { "#pk": "pk" };
195
- const values = { ":pk": pk };
196
- let keyCondition = "#pk = :pk";
197
- if (skPrefix) {
198
- names["#sk"] = "sk";
199
- values[":sk"] = skPrefix;
200
- keyCondition += " AND begins_with(#sk, :sk)";
201
- }
202
- const result = await getClient2().query({
203
- TableName: tableName,
204
- KeyConditionExpression: keyCondition,
205
- ExpressionAttributeNames: names,
206
- ExpressionAttributeValues: marshall2(values, { removeUndefinedValues: true })
207
- });
208
- return (result.Items ?? []).map((item) => unmarshall2(item));
209
- },
210
- async put(entity) {
211
- try {
212
- await getClient2().putItem({
213
- TableName: tableName,
214
- Item: marshall2(entity, { removeUndefinedValues: true })
215
- });
216
- } catch (err) {
217
- console.error("[effortless] Failed to write platform record:", err);
218
- }
219
- }
220
- };
221
- };
222
-
223
127
  // src/runtime/handler-utils.ts
224
128
  var ENV_TABLE_PREFIX = "EFF_TABLE_";
225
129
  var ENV_PARAM_PREFIX = "EFF_PARAM_";
130
+ var LOG_RANK = { error: 0, info: 1, debug: 2 };
131
+ var truncate = (value, maxLength = 4096) => {
132
+ if (value === void 0 || value === null) return value;
133
+ const str = typeof value === "string" ? value : JSON.stringify(value);
134
+ if (str.length <= maxLength) return value;
135
+ return str.slice(0, maxLength) + "...[truncated]";
136
+ };
226
137
  var buildDeps = (deps) => {
227
138
  if (!deps) return void 0;
228
139
  const result = {};
@@ -256,9 +167,9 @@ var buildParams = async (params) => {
256
167
  return result;
257
168
  };
258
169
  var readStatic = (filePath) => readFileSync(join(process.cwd(), filePath), "utf-8");
259
- var createHandlerRuntime = (handler, handlerType) => {
260
- const platform = createPlatformClient();
170
+ var createHandlerRuntime = (handler, handlerType, logLevel = "info") => {
261
171
  const handlerName = process.env.EFF_HANDLER ?? "unknown";
172
+ const rank = LOG_RANK[logLevel];
262
173
  let ctx = null;
263
174
  let resolvedDeps;
264
175
  let resolvedParams = null;
@@ -287,28 +198,51 @@ var createHandlerRuntime = (handler, handlerType) => {
287
198
  return args;
288
199
  };
289
200
  const logExecution = (startTime, input, output) => {
290
- platform?.appendExecution(handlerName, handlerType, {
291
- id: randomUUID(),
292
- ts: (/* @__PURE__ */ new Date()).toISOString(),
293
- ms: Date.now() - startTime,
294
- in: input,
295
- out: truncateForStorage(output)
296
- });
201
+ if (rank < LOG_RANK.info) return;
202
+ const entry = {
203
+ level: "info",
204
+ handler: handlerName,
205
+ type: handlerType,
206
+ ms: Date.now() - startTime
207
+ };
208
+ if (rank >= LOG_RANK.debug) {
209
+ entry.input = truncate(input);
210
+ entry.output = truncate(output);
211
+ }
212
+ console.log(JSON.stringify(entry));
297
213
  };
298
214
  const logError = (startTime, input, error) => {
299
- platform?.appendError(handlerName, handlerType, {
300
- id: randomUUID(),
301
- ts: (/* @__PURE__ */ new Date()).toISOString(),
215
+ const entry = {
216
+ level: "error",
217
+ handler: handlerName,
218
+ type: handlerType,
302
219
  ms: Date.now() - startTime,
303
- in: input,
304
- err: error instanceof Error ? error.message : String(error)
305
- });
220
+ error: error instanceof Error ? error.message : String(error)
221
+ };
222
+ if (rank >= LOG_RANK.debug) {
223
+ entry.input = truncate(input);
224
+ }
225
+ console.error(JSON.stringify(entry));
226
+ };
227
+ const noop = () => {
228
+ };
229
+ const saved = { log: console.log, info: console.info, debug: console.debug, warn: console.warn, error: console.error };
230
+ const patchConsole = () => {
231
+ if (rank < LOG_RANK.debug) console.debug = noop;
232
+ if (rank < LOG_RANK.info) {
233
+ console.log = noop;
234
+ console.info = noop;
235
+ }
236
+ };
237
+ const restoreConsole = () => {
238
+ console.log = saved.log;
239
+ console.info = saved.info;
240
+ console.debug = saved.debug;
306
241
  };
307
- return { commonArgs, logExecution, logError, handlerName };
242
+ return { commonArgs, logExecution, logError, patchConsole, restoreConsole, handlerName };
308
243
  };
309
244
 
310
245
  export {
311
246
  createTableClient,
312
- truncateForStorage,
313
247
  createHandlerRuntime
314
248
  };