@swydo/byol 2.0.13 → 2.0.16

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/CHANGELOG.md CHANGED
@@ -3,6 +3,36 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 2.0.16 (2022-04-06)
7
+
8
+ **Note:** Version bump only for package @swydo/byol
9
+
10
+
11
+
12
+
13
+
14
+ ## 2.0.15 (2022-04-06)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * **byol:** fix cwd not being set to CodeUri from CloudFormation file ([#363](https://github.com/Swydo/byol/issues/363)) ([8c6ce63](https://github.com/Swydo/byol/commit/8c6ce631e5c47ecfcbd3b1c5548cc8f2fcb51805))
20
+
21
+
22
+
23
+
24
+
25
+ ## 2.0.14 (2022-04-04)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * **byol:** add timeout delay to print all the logs ([#356](https://github.com/Swydo/byol/issues/356)) ([937393b](https://github.com/Swydo/byol/commit/937393bed0bd8c88e4776619d27f954e7077cc71))
31
+
32
+
33
+
34
+
35
+
6
36
  ## 2.0.13 (2021-10-25)
7
37
 
8
38
  **Note:** Version bump only for package @swydo/byol
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swydo/byol",
3
- "version": "2.0.13",
3
+ "version": "2.0.16",
4
4
  "description": "Bring Your Own Lambda",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -27,5 +27,5 @@
27
27
  "peerDependencies": {
28
28
  "aws-xray-sdk-core": "^3.0.0"
29
29
  },
30
- "gitHead": "ddbd0d81baf30424ecf45841411e07ef8fe33994"
30
+ "gitHead": "9b5e602f0dd5db4dfcca03c41e8fba61730cf99f"
31
31
  }
@@ -1,3 +1,4 @@
1
+ const path = require('path');
1
2
  const workerpool = require('workerpool');
2
3
  const { generateRequestId } = require('../generateRequestId');
3
4
 
@@ -17,7 +18,7 @@ function hasXRay() {
17
18
  }
18
19
 
19
20
  async function execute(handler, event, awsContext) {
20
- return new Promise(((resolve, reject) => {
21
+ const handlerResult = await new Promise(((resolve, reject) => {
21
22
  const maybePromise = handler(event, awsContext, (err, res) => {
22
23
  if (err) {
23
24
  reject(err);
@@ -36,6 +37,12 @@ async function execute(handler, event, awsContext) {
36
37
  .catch((err) => reject(err));
37
38
  }
38
39
  }));
40
+
41
+ // Timeout delay is required in order to print console logs
42
+ // that are omitted otherwise
43
+ await new Promise((resolve) => setTimeout(resolve, 10));
44
+
45
+ return handlerResult;
39
46
  }
40
47
 
41
48
  async function executeWithXRay(segmentName, handler, event, awsContext) {
@@ -61,13 +68,15 @@ async function executeWithXRay(segmentName, handler, event, awsContext) {
61
68
  }
62
69
 
63
70
  async function callHandler({
64
- absoluteIndexPath,
71
+ indexPath,
65
72
  handlerName,
66
73
  event,
67
74
  environment,
68
75
  }) {
69
76
  process.env = environment;
70
77
 
78
+ const absoluteIndexPath = path.join(process.cwd(), indexPath);
79
+
71
80
  const { [handlerName]: handler } = await import(absoluteIndexPath);
72
81
  const awsContext = {
73
82
  awsRequestId: generateRequestId(),
@@ -3,16 +3,16 @@ const workerpool = require('workerpool');
3
3
 
4
4
  const workerPoolMap = new Map();
5
5
 
6
- function getWorkerPoolKey(absoluteIndexPath, handlerName, requestId) {
6
+ function getWorkerPoolKey(workingDirectory, indexPath, handlerName, requestId) {
7
7
  if (requestId) {
8
- return `${absoluteIndexPath}:${handlerName}:${requestId}`;
8
+ return `${workingDirectory}:${indexPath}:${handlerName}:${requestId}`;
9
9
  }
10
10
 
11
- return `${absoluteIndexPath}:${handlerName}`;
11
+ return `${workingDirectory}:${indexPath}:${handlerName}`;
12
12
  }
13
13
 
14
- function terminateWorkerPool(absoluteIndexPath, handlerName, requestId) {
15
- const poolKey = getWorkerPoolKey(absoluteIndexPath, handlerName, requestId);
14
+ function terminateWorkerPool(workingDirectory, indexPath, handlerName, requestId) {
15
+ const poolKey = getWorkerPoolKey(workingDirectory, indexPath, handlerName, requestId);
16
16
 
17
17
  const { pool } = workerPoolMap.get(poolKey);
18
18
  const terminationPromise = pool.terminate();
@@ -33,15 +33,20 @@ function terminateWorkerPools() {
33
33
  return Promise.all(terminationPromises);
34
34
  }
35
35
 
36
- async function getWorkerPool(absoluteIndexPath, handlerName, environment = {}, requestId) {
37
- const poolKey = getWorkerPoolKey(absoluteIndexPath, handlerName, requestId);
36
+ async function getWorkerPool(workingDirectory, indexPath, handlerName, environment = {}, requestId) {
37
+ const poolKey = getWorkerPoolKey(workingDirectory, indexPath, handlerName, requestId);
38
38
 
39
39
  if (!workerPoolMap.has(poolKey)) {
40
- const pool = workerpool.pool(path.join(__dirname, 'assets', 'callHandlerProcess.js'));
40
+ const pool = workerpool.pool(path.join(__dirname, 'assets', 'callHandlerProcess.js'), {
41
+ workerType: 'process',
42
+ forkOpts: {
43
+ cwd: workingDirectory,
44
+ },
45
+ });
41
46
 
42
47
  workerPoolMap.set(poolKey, {
43
48
  pool,
44
- absoluteIndexPath,
49
+ indexPath,
45
50
  handlerName,
46
51
  environment,
47
52
  requestId,
@@ -53,7 +58,7 @@ async function getWorkerPool(absoluteIndexPath, handlerName, environment = {}, r
53
58
  if (JSON.stringify(poolEnvironment) !== JSON.stringify(environment)) {
54
59
  terminateWorkerPool(poolKey);
55
60
 
56
- return getWorkerPool(absoluteIndexPath, handlerName, environment);
61
+ return getWorkerPool(workingDirectory, indexPath, handlerName, environment);
57
62
  }
58
63
 
59
64
  return pool;
@@ -61,16 +61,17 @@ async function invokeFunction(functionName, event, {
61
61
 
62
62
  const [relativePathWithoutExtension, handlerName] = handler.split('.');
63
63
 
64
- const relativeIndexPath = `${relativePathWithoutExtension}.js`;
65
- const absoluteIndexPath = path.join(templatePath, '..', codeUri, relativeIndexPath);
64
+ const workingDirectory = path.join(templatePath, '..', codeUri);
65
+ const indexPath = `${relativePathWithoutExtension}.js`;
66
66
 
67
67
  const options = {
68
- absoluteIndexPath,
68
+ indexPath,
69
69
  handlerName,
70
70
  environment,
71
71
  event,
72
72
  keepAlive,
73
73
  requestId,
74
+ workingDirectory,
74
75
  };
75
76
 
76
77
  let result;
@@ -7,23 +7,24 @@ function getDebug(requestId) {
7
7
  }
8
8
 
9
9
  async function invokeHandler({
10
- absoluteIndexPath,
10
+ indexPath,
11
11
  handlerName,
12
12
  environment,
13
13
  event,
14
14
  keepAlive = false,
15
15
  requestId,
16
+ workingDirectory,
16
17
  }) {
17
18
  const id = requestId || generateRequestId();
18
19
  const debug = getDebug(id);
19
20
 
20
21
  const poolRequestId = keepAlive ? undefined : id;
21
- const workerPool = await getWorkerPool(absoluteIndexPath, handlerName, environment, poolRequestId);
22
+ const workerPool = await getWorkerPool(workingDirectory, indexPath, handlerName, environment, poolRequestId);
22
23
 
23
24
  try {
24
25
  debug('Start');
25
26
  const result = await workerPool.exec('callHandler', [{
26
- absoluteIndexPath,
27
+ indexPath,
27
28
  handlerName,
28
29
  event,
29
30
  environment: {
@@ -44,7 +45,7 @@ async function invokeHandler({
44
45
  throw e;
45
46
  } finally {
46
47
  if (!keepAlive) {
47
- await terminateWorkerPool(absoluteIndexPath, handlerName, poolRequestId);
48
+ await terminateWorkerPool(workingDirectory, indexPath, handlerName, poolRequestId);
48
49
  }
49
50
  }
50
51
  }
@@ -23,11 +23,10 @@ describe('invokeHandler', function () {
23
23
  });
24
24
 
25
25
  it('invokes an async handler', async function () {
26
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/goodHandler.js');
27
-
28
26
  const result = await invokeHandler({
29
- absoluteIndexPath,
30
27
  handlerName: 'handler',
28
+ indexPath: 'goodHandler.js',
29
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
31
30
  });
32
31
 
33
32
  expect(result).to.be.an('object');
@@ -36,11 +35,10 @@ describe('invokeHandler', function () {
36
35
  });
37
36
 
38
37
  it('invokes a non-async handler', async function () {
39
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/goodCallbackHandler.js');
40
-
41
38
  const result = await invokeHandler({
42
- absoluteIndexPath,
43
39
  handlerName: 'handler',
40
+ indexPath: 'goodCallbackHandler.js',
41
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
44
42
  });
45
43
 
46
44
  expect(result).to.be.an('object');
@@ -49,14 +47,14 @@ describe('invokeHandler', function () {
49
47
  });
50
48
 
51
49
  it('invokes the handler with the given event', async function () {
52
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/goodHandler.js');
53
50
  const event = {
54
51
  foo: 'foo',
55
52
  };
56
53
 
57
54
  const result = await invokeHandler({
58
- absoluteIndexPath,
59
55
  handlerName: 'handler',
56
+ indexPath: 'goodHandler.js',
57
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
60
58
  event,
61
59
  });
62
60
 
@@ -70,14 +68,14 @@ describe('invokeHandler', function () {
70
68
  });
71
69
 
72
70
  it('invokes the handler with the given environment variables', async function () {
73
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/goodHandler.js');
74
71
  const environment = {
75
72
  FOO: 'FOO',
76
73
  };
77
74
 
78
75
  const result = await invokeHandler({
79
- absoluteIndexPath,
80
76
  handlerName: 'handler',
77
+ indexPath: 'goodHandler.js',
78
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
81
79
  environment,
82
80
  });
83
81
 
@@ -90,12 +88,12 @@ describe('invokeHandler', function () {
90
88
  });
91
89
 
92
90
  it('rejects when an error is thrown by an async handler', async function () {
93
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/badHandler.js');
94
91
  const errorMessage = 'FOO';
95
92
 
96
93
  const invokePromise = invokeHandler({
97
- absoluteIndexPath,
98
94
  handlerName: 'handler',
95
+ indexPath: 'badHandler.js',
96
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
99
97
  event: { message: errorMessage },
100
98
  });
101
99
 
@@ -103,12 +101,12 @@ describe('invokeHandler', function () {
103
101
  });
104
102
 
105
103
  it('rejects when an error is returned by an non-async handler', async function () {
106
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/badCallbackHandler.js');
107
104
  const errorMessage = 'FOO';
108
105
 
109
106
  const invokePromise = invokeHandler({
110
- absoluteIndexPath,
111
107
  handlerName: 'handler',
108
+ indexPath: 'badCallbackHandler.js',
109
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
112
110
  event: { message: errorMessage },
113
111
  });
114
112
 
@@ -116,12 +114,12 @@ describe('invokeHandler', function () {
116
114
  });
117
115
 
118
116
  it('rejects when the process dies unexpectedly', async function () {
119
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/brokenHandler.js');
120
117
  const errorMessage = 'FOO';
121
118
 
122
119
  const invokePromise = invokeHandler({
123
- absoluteIndexPath,
124
120
  handlerName: 'handler',
121
+ indexPath: 'brokenHandler.js',
122
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
125
123
  event: { message: errorMessage },
126
124
  });
127
125
 
@@ -129,12 +127,12 @@ describe('invokeHandler', function () {
129
127
  });
130
128
 
131
129
  it('rejects when the handler function can\'t be found', async function () {
132
- const absoluteIndexPath = path.resolve(__dirname, '../tests/assets/goodHandler.js');
133
130
  const errorMessage = 'FOO';
134
131
 
135
132
  const invokePromise = invokeHandler({
136
- absoluteIndexPath,
133
+ indexPath: 'goodHandler.js',
137
134
  handlerName: 'foo',
135
+ workingDirectory: path.resolve(__dirname, '../tests/assets'),
138
136
  event: { message: errorMessage },
139
137
  });
140
138