1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.getLambdaOptionsFromFunction = exports.createZip = exports.createLambda = exports.Lambda = void 0;
|
7 | const assert_1 = __importDefault(require("assert"));
|
8 | const async_sema_1 = __importDefault(require("async-sema"));
|
9 | const yazl_1 = require("yazl");
|
10 | const minimatch_1 = __importDefault(require("minimatch"));
|
11 | const fs_extra_1 = require("fs-extra");
|
12 | const download_1 = require("./fs/download");
|
13 | const stream_to_buffer_1 = __importDefault(require("./fs/stream-to-buffer"));
|
14 | class Lambda {
|
15 | constructor({ zipBuffer, handler, runtime, maxDuration, memory, environment, }) {
|
16 | this.type = 'Lambda';
|
17 | this.zipBuffer = zipBuffer;
|
18 | this.handler = handler;
|
19 | this.runtime = runtime;
|
20 | this.memory = memory;
|
21 | this.maxDuration = maxDuration;
|
22 | this.environment = environment;
|
23 | }
|
24 | }
|
25 | exports.Lambda = Lambda;
|
26 | const sema = new async_sema_1.default(10);
|
27 | const mtime = new Date(1540000000000);
|
28 | async function createLambda({ files, handler, runtime, memory, maxDuration, environment = {}, }) {
|
29 | assert_1.default(typeof files === 'object', '"files" must be an object');
|
30 | assert_1.default(typeof handler === 'string', '"handler" is not a string');
|
31 | assert_1.default(typeof runtime === 'string', '"runtime" is not a string');
|
32 | assert_1.default(typeof environment === 'object', '"environment" is not an object');
|
33 | if (memory !== undefined) {
|
34 | assert_1.default(typeof memory === 'number', '"memory" is not a number');
|
35 | }
|
36 | if (maxDuration !== undefined) {
|
37 | assert_1.default(typeof maxDuration === 'number', '"maxDuration" is not a number');
|
38 | }
|
39 | await sema.acquire();
|
40 | try {
|
41 | const zipBuffer = await createZip(files);
|
42 | return new Lambda({
|
43 | zipBuffer,
|
44 | handler,
|
45 | runtime,
|
46 | memory,
|
47 | maxDuration,
|
48 | environment,
|
49 | });
|
50 | }
|
51 | finally {
|
52 | sema.release();
|
53 | }
|
54 | }
|
55 | exports.createLambda = createLambda;
|
56 | async function createZip(files) {
|
57 | const names = Object.keys(files).sort();
|
58 | const symlinkTargets = new Map();
|
59 | for (const name of names) {
|
60 | const file = files[name];
|
61 | if (file.mode && download_1.isSymbolicLink(file.mode) && file.type === 'FileFsRef') {
|
62 | const symlinkTarget = await fs_extra_1.readlink(file.fsPath);
|
63 | symlinkTargets.set(name, symlinkTarget);
|
64 | }
|
65 | }
|
66 | const zipFile = new yazl_1.ZipFile();
|
67 | const zipBuffer = await new Promise((resolve, reject) => {
|
68 | for (const name of names) {
|
69 | const file = files[name];
|
70 | const opts = { mode: file.mode, mtime };
|
71 | const symlinkTarget = symlinkTargets.get(name);
|
72 | if (typeof symlinkTarget === 'string') {
|
73 | zipFile.addBuffer(Buffer.from(symlinkTarget, 'utf8'), name, opts);
|
74 | }
|
75 | else {
|
76 | const stream = file.toStream();
|
77 | stream.on('error', reject);
|
78 | zipFile.addReadStream(stream, name, opts);
|
79 | }
|
80 | }
|
81 | zipFile.end();
|
82 | stream_to_buffer_1.default(zipFile.outputStream)
|
83 | .then(resolve)
|
84 | .catch(reject);
|
85 | });
|
86 | return zipBuffer;
|
87 | }
|
88 | exports.createZip = createZip;
|
89 | async function getLambdaOptionsFromFunction({ sourceFile, config, }) {
|
90 | if (config && config.functions) {
|
91 | for (const [pattern, fn] of Object.entries(config.functions)) {
|
92 | if (sourceFile === pattern || minimatch_1.default(sourceFile, pattern)) {
|
93 | return {
|
94 | memory: fn.memory,
|
95 | maxDuration: fn.maxDuration,
|
96 | };
|
97 | }
|
98 | }
|
99 | }
|
100 | return {};
|
101 | }
|
102 | exports.getLambdaOptionsFromFunction = getLambdaOptionsFromFunction;
|