-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.ts
49 lines (41 loc) · 1.62 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// eslint-disable-next-line import/no-unresolved, no-unused-vars
import { Context, Callback } from 'aws-lambda';
import { successResponse, runWarm } from './src/utils';
import { createAvatar, output, upload } from './src/initials';
import { getDestinationBucket } from './src/env';
process.env.FONTCONFIG_PATH = '/var/task/fonts';
const mime = require('mime/lite');
interface InitialsLambdaEvent {
seed: string;
filePath: string;
}
const main: Function = async (event: InitialsLambdaEvent,
_context: Context,
// eslint-disable-next-line consistent-return
callback: Callback) => {
// https://stackoverflow.com/q/56188864/2015025
// Lambda is standalone service that doesn't need to be integrated with API
// Gateway.queryStringParameters, body, body mapping templates, all of this is
// specific not to Lambda, but to Lambda - API Gateway integration.
const { seed, filePath } = event;
console.log(`event: ${seed}, filePath: ${filePath}`);
try {
const outputFormat = mime.getExtension(mime.getType(filePath));
const svg = createAvatar(seed, {
chars: 1,
size: 256,
});
const buffer = await output(svg, outputFormat);
await upload(getDestinationBucket(), filePath, buffer);
// successResponse handles wrapping the response in an API Gateway friendly
// format (see other responses, including CORS, in `./utils/lambda-response.js)
return successResponse({
message: filePath,
});
} catch (error) {
callback(error);
}
};
// runWarm function handles pings from the scheduler so you don't
// have to put that boilerplate in your function.
export default runWarm(main);