effortless-aws 0.10.1 → 0.12.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.
@@ -183,6 +183,74 @@ var createTableClient = (tableName, options) => {
183
183
  };
184
184
  };
185
185
 
186
+ // src/runtime/bucket-client.ts
187
+ import { S3 } from "@aws-sdk/client-s3";
188
+ var createBucketClient = (bucketName) => {
189
+ let client2 = null;
190
+ const getClient2 = () => client2 ??= new S3({});
191
+ return {
192
+ bucketName,
193
+ async put(key, body, options) {
194
+ await getClient2().putObject({
195
+ Bucket: bucketName,
196
+ Key: key,
197
+ Body: typeof body === "string" ? Buffer.from(body) : body,
198
+ ...options?.contentType ? { ContentType: options.contentType } : {}
199
+ });
200
+ },
201
+ async get(key) {
202
+ try {
203
+ const result = await getClient2().getObject({
204
+ Bucket: bucketName,
205
+ Key: key
206
+ });
207
+ const chunks = [];
208
+ const stream = result.Body;
209
+ for await (const chunk of stream) {
210
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
211
+ }
212
+ return {
213
+ body: Buffer.concat(chunks),
214
+ contentType: result.ContentType
215
+ };
216
+ } catch (error) {
217
+ if (error instanceof Error && (error.name === "NoSuchKey" || error.$metadata?.httpStatusCode === 404)) {
218
+ return void 0;
219
+ }
220
+ throw error;
221
+ }
222
+ },
223
+ async delete(key) {
224
+ await getClient2().deleteObject({
225
+ Bucket: bucketName,
226
+ Key: key
227
+ });
228
+ },
229
+ async list(prefix) {
230
+ const items = [];
231
+ let continuationToken;
232
+ do {
233
+ const result = await getClient2().listObjectsV2({
234
+ Bucket: bucketName,
235
+ ...prefix ? { Prefix: prefix } : {},
236
+ ...continuationToken ? { ContinuationToken: continuationToken } : {}
237
+ });
238
+ for (const obj of result.Contents ?? []) {
239
+ if (obj.Key) {
240
+ items.push({
241
+ key: obj.Key,
242
+ size: obj.Size ?? 0,
243
+ lastModified: obj.LastModified
244
+ });
245
+ }
246
+ }
247
+ continuationToken = result.IsTruncated ? result.NextContinuationToken : void 0;
248
+ } while (continuationToken);
249
+ return items;
250
+ }
251
+ };
252
+ };
253
+
186
254
  // src/runtime/handler-utils.ts
187
255
  import { readFileSync } from "fs";
188
256
  import { join } from "path";
@@ -209,7 +277,7 @@ var getParameters = async (names) => {
209
277
  };
210
278
 
211
279
  // src/runtime/handler-utils.ts
212
- var ENV_TABLE_PREFIX = "EFF_TABLE_";
280
+ var ENV_DEP_PREFIX = "EFF_DEP_";
213
281
  var ENV_PARAM_PREFIX = "EFF_PARAM_";
214
282
  var LOG_RANK = { error: 0, info: 1, debug: 2 };
215
283
  var truncate = (value, maxLength = 4096) => {
@@ -218,17 +286,27 @@ var truncate = (value, maxLength = 4096) => {
218
286
  if (str.length <= maxLength) return value;
219
287
  return str.slice(0, maxLength) + "...[truncated]";
220
288
  };
289
+ var DEP_FACTORIES = {
290
+ table: (name, depHandler) => {
291
+ const tagField = depHandler?.__spec?.tagField;
292
+ return createTableClient(name, tagField ? { tagField } : void 0);
293
+ },
294
+ bucket: (name) => createBucketClient(name)
295
+ };
296
+ var parseDepValue = (raw) => {
297
+ const idx = raw.indexOf(":");
298
+ return { type: raw.slice(0, idx), name: raw.slice(idx + 1) };
299
+ };
221
300
  var buildDeps = (deps) => {
222
301
  if (!deps) return void 0;
223
302
  const result = {};
224
303
  for (const key of Object.keys(deps)) {
225
- const tableName = process.env[`${ENV_TABLE_PREFIX}${key}`];
226
- if (!tableName) {
227
- throw new Error(`Missing environment variable ${ENV_TABLE_PREFIX}${key} for dep "${key}"`);
228
- }
229
- const depHandler = deps[key];
230
- const tagField = depHandler?.__spec?.tagField;
231
- result[key] = createTableClient(tableName, tagField ? { tagField } : void 0);
304
+ const raw = process.env[`${ENV_DEP_PREFIX}${key}`];
305
+ if (!raw) throw new Error(`Missing environment variable ${ENV_DEP_PREFIX}${key} for dep "${key}"`);
306
+ const { type, name } = parseDepValue(raw);
307
+ const factory = DEP_FACTORIES[type];
308
+ if (!factory) throw new Error(`Unknown dep type "${type}" for dep "${key}"`);
309
+ result[key] = factory(name, deps[key]);
232
310
  }
233
311
  return result;
234
312
  };
@@ -274,7 +352,7 @@ var createHandlerRuntime = (handler, handlerType, logLevel = "info", extraSetupA
274
352
  if (params) args.config = params;
275
353
  if (deps) args.deps = deps;
276
354
  if (extraSetupArgs) Object.assign(args, extraSetupArgs());
277
- ctx = Object.keys(args).length > 0 || extraSetupArgs ? await handler.setup(args) : await handler.setup();
355
+ ctx = await handler.setup(args);
278
356
  }
279
357
  return ctx;
280
358
  };
@@ -317,7 +395,7 @@ var createHandlerRuntime = (handler, handlerType, logLevel = "info", extraSetupA
317
395
  };
318
396
  const noop = () => {
319
397
  };
320
- const saved = { log: console.log, info: console.info, debug: console.debug, warn: console.warn, error: console.error };
398
+ const saved = { log: console.log, info: console.info, debug: console.debug };
321
399
  const patchConsole = () => {
322
400
  if (rank < LOG_RANK.debug) console.debug = noop;
323
401
  if (rank < LOG_RANK.info) {
@@ -335,5 +413,6 @@ var createHandlerRuntime = (handler, handlerType, logLevel = "info", extraSetupA
335
413
 
336
414
  export {
337
415
  createTableClient,
416
+ createBucketClient,
338
417
  createHandlerRuntime
339
418
  };