@whook/gcp-functions 8.5.1 → 10.0.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.
Files changed (40) hide show
  1. package/README.md +51 -40
  2. package/dist/commands/testHTTPFunction.d.ts +1 -1
  3. package/dist/commands/testHTTPFunction.js +119 -150
  4. package/dist/commands/testHTTPFunction.js.map +1 -1
  5. package/dist/index.d.ts +3 -6
  6. package/dist/index.js +182 -269
  7. package/dist/index.js.map +1 -1
  8. package/dist/libs/utils.js +16 -35
  9. package/dist/libs/utils.js.map +1 -1
  10. package/dist/services/_autoload.d.ts +14 -2
  11. package/dist/services/_autoload.js +85 -105
  12. package/dist/services/_autoload.js.map +1 -1
  13. package/dist/services/log.d.ts +4 -1
  14. package/dist/services/log.js +2 -12
  15. package/dist/services/log.js.map +1 -1
  16. package/dist/services/log.test.js +4 -9
  17. package/dist/services/log.test.js.map +1 -1
  18. package/dist/wrappers/googleHTTPFunction.js +246 -292
  19. package/dist/wrappers/googleHTTPFunction.js.map +1 -1
  20. package/package.json +55 -98
  21. package/src/commands/testHTTPFunction.ts +12 -19
  22. package/src/index.ts +48 -62
  23. package/src/libs/utils.ts +4 -5
  24. package/src/services/_autoload.ts +127 -122
  25. package/src/services/log.test.ts +2 -2
  26. package/src/wrappers/googleHTTPFunction.ts +34 -32
  27. package/dist/commands/testHTTPFunction.mjs +0 -136
  28. package/dist/commands/testHTTPFunction.mjs.map +0 -1
  29. package/dist/index.mjs +0 -265
  30. package/dist/index.mjs.map +0 -1
  31. package/dist/libs/utils.mjs +0 -27
  32. package/dist/libs/utils.mjs.map +0 -1
  33. package/dist/services/_autoload.mjs +0 -107
  34. package/dist/services/_autoload.mjs.map +0 -1
  35. package/dist/services/log.mjs +0 -4
  36. package/dist/services/log.mjs.map +0 -1
  37. package/dist/services/log.test.mjs +0 -7
  38. package/dist/services/log.test.mjs.map +0 -1
  39. package/dist/wrappers/googleHTTPFunction.mjs +0 -288
  40. package/dist/wrappers/googleHTTPFunction.mjs.map +0 -1
package/package.json CHANGED
@@ -1,19 +1,17 @@
1
1
  {
2
2
  "name": "@whook/gcp-functions",
3
- "version": "8.5.1",
3
+ "version": "10.0.0",
4
4
  "description": "Build and deploy to GCP Cloud Functions with Whook.",
5
- "main": "dist/index",
6
- "module": "dist/index.mjs",
5
+ "main": "dist/index.js",
7
6
  "types": "dist/index.d.ts",
8
7
  "metapak": {
9
8
  "configs": [
10
9
  "main",
11
10
  "readme",
11
+ "tsesm",
12
12
  "eslint",
13
- "babel",
14
13
  "jest",
15
- "jsdocs",
16
- "typescript"
14
+ "jsdocs"
17
15
  ],
18
16
  "data": {
19
17
  "childPackage": true,
@@ -50,51 +48,42 @@
50
48
  },
51
49
  "homepage": "https://github.com/nfroidure/whook",
52
50
  "dependencies": {
53
- "@types/bytes": "^3.1.0",
54
- "@whook/cli": "^8.5.1",
55
- "@whook/cors": "^8.5.1",
56
- "@whook/http-router": "^8.5.0",
57
- "@whook/whook": "^8.5.1",
58
- "ajv": "^8.6.3",
59
- "ajv-formats": "^2.1.0",
60
- "camelcase": "^6.2.0",
61
- "common-services": "^9.0.1",
51
+ "@whook/cli": "^10.0.0",
52
+ "@whook/cors": "^10.0.0",
53
+ "@whook/http-router": "^10.0.0",
54
+ "@whook/whook": "^10.0.0",
55
+ "ajv": "^8.11.0",
56
+ "ajv-formats": "^2.1.1",
57
+ "camelcase": "^7.0.0",
58
+ "common-services": "^11.0.1",
62
59
  "cpr": "3.0.1",
63
- "knifecycle": "^11.1.1",
60
+ "knifecycle": "^14.0.0",
64
61
  "mkdirp": "^1.0.4",
65
- "openapi-types": "^9.3.0",
66
- "qs": "^6.10.1",
67
- "strict-qs": "^6.1.4",
68
- "yerror": "^6.0.1"
62
+ "openapi-types": "^12.0.0",
63
+ "qs": "^6.10.3",
64
+ "strict-qs": "^7.0.0",
65
+ "yerror": "^6.1.1"
69
66
  },
70
67
  "devDependencies": {
71
- "@babel/cli": "^7.13.14",
72
- "@babel/core": "^7.13.15",
73
- "@babel/eslint-parser": "^7.13.14",
74
- "@babel/plugin-proposal-class-properties": "^7.13.0",
75
- "@babel/plugin-proposal-object-rest-spread": "^7.13.8",
76
- "@babel/plugin-syntax-dynamic-import": "^7.8.3",
77
- "@babel/plugin-syntax-import-meta": "^7.10.4",
78
- "@babel/preset-env": "^7.13.15",
79
- "@babel/preset-typescript": "^7.13.0",
80
- "@babel/register": "^7.13.14",
81
- "@types/jest": "^26.0.22",
82
- "@typescript-eslint/eslint-plugin": "^4.21.0",
83
- "@typescript-eslint/parser": "^4.21.0",
84
- "babel-plugin-knifecycle": "^5.0.1",
85
- "eslint": "^7.23.0",
86
- "eslint-plugin-prettier": "^3.3.1",
87
- "jest": "^26.6.3",
88
- "jsdoc-to-markdown": "^7.0.1",
89
- "metapak": "^4.0.2",
90
- "metapak-nfroidure": "11.0.7",
91
- "prettier": "^2.2.1",
68
+ "@types/jest": "^28.1.1",
69
+ "@typescript-eslint/eslint-plugin": "^5.27.0",
70
+ "@typescript-eslint/parser": "^5.27.0",
71
+ "esbuild": "^0.14.46",
72
+ "esbuild-jest": "^0.5.0",
73
+ "eslint": "^8.17.0",
74
+ "eslint-config-prettier": "^8.5.0",
75
+ "eslint-plugin-prettier": "^4.0.0",
76
+ "jest": "^28.1.0",
77
+ "jsdoc-to-markdown": "^7.1.1",
78
+ "metapak": "^4.0.4",
79
+ "metapak-nfroidure": "12.1.0",
80
+ "prettier": "^2.6.2",
92
81
  "rimraf": "^3.0.2",
93
- "typescript": "^4.2.4"
82
+ "typescript": "^4.7.3"
94
83
  },
95
84
  "contributors": [],
96
85
  "engines": {
97
- "node": ">=12.19.0"
86
+ "node": ">=16.15.0"
98
87
  },
99
88
  "files": [
100
89
  "dist",
@@ -106,12 +95,13 @@
106
95
  "eslintConfig": {
107
96
  "extends": [
108
97
  "eslint:recommended",
98
+ "plugin:prettier/recommended",
109
99
  "plugin:@typescript-eslint/eslint-recommended",
110
100
  "plugin:@typescript-eslint/recommended"
111
101
  ],
112
102
  "parserOptions": {
113
103
  "ecmaVersion": 2018,
114
- "sourceType": "module",
104
+ "sourceType": "script",
115
105
  "modules": true
116
106
  },
117
107
  "env": {
@@ -138,53 +128,6 @@
138
128
  "trailingComma": "all",
139
129
  "proseWrap": "always"
140
130
  },
141
- "babel": {
142
- "presets": [
143
- "@babel/typescript",
144
- [
145
- "@babel/env",
146
- {
147
- "targets": {
148
- "node": "12.19.0"
149
- }
150
- }
151
- ]
152
- ],
153
- "plugins": [
154
- "@babel/proposal-class-properties",
155
- "@babel/plugin-proposal-object-rest-spread",
156
- "babel-plugin-knifecycle"
157
- ],
158
- "env": {
159
- "cjs": {
160
- "presets": [
161
- [
162
- "@babel/env",
163
- {
164
- "targets": {
165
- "node": "10"
166
- },
167
- "modules": "commonjs"
168
- }
169
- ]
170
- ]
171
- },
172
- "mjs": {
173
- "presets": [
174
- [
175
- "@babel/env",
176
- {
177
- "targets": {
178
- "node": "12"
179
- },
180
- "modules": false
181
- }
182
- ]
183
- ]
184
- }
185
- },
186
- "sourceMaps": true
187
- },
188
131
  "jest": {
189
132
  "coverageReporters": [
190
133
  "lcov",
@@ -195,22 +138,36 @@
195
138
  ],
196
139
  "roots": [
197
140
  "<rootDir>/src"
141
+ ],
142
+ "testEnvironment": "node",
143
+ "transform": {
144
+ "^.+\\.tsx?$": [
145
+ "esbuild-jest",
146
+ {
147
+ "sourcemap": true,
148
+ "format": "esm"
149
+ }
150
+ ]
151
+ },
152
+ "moduleNameMapper": {
153
+ "(.+)\\.js": "$1"
154
+ },
155
+ "extensionsToTreatAsEsm": [
156
+ ".ts"
198
157
  ]
199
158
  },
200
159
  "scripts": {
160
+ "build": "rimraf -f 'dist' && tsc --outDir dist",
201
161
  "cli": "env NODE_ENV=${NODE_ENV:-cli}",
202
- "compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
203
- "compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
204
- "compile:mjs": "babel --env-name=mjs --out-file-extension=.mjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
205
162
  "cover": "npm run jest -- --coverage",
206
163
  "doc": "echo \"# API\" > API.md; jsdoc2md 'dist/**/*.js' >> API.md && git add API.md",
207
164
  "genPackagelock": "npm i --package-lock-only",
208
- "jest": "NODE_ENV=test jest",
165
+ "jest": "NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test jest",
209
166
  "lint": "eslint 'src/**/*.ts'",
210
167
  "metapak": "metapak",
211
168
  "prettier": "prettier --write 'src/**/*.ts'",
212
- "test": "npm run jest",
213
- "types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist"
169
+ "test": "npm run jest"
214
170
  },
215
- "gitHead": "0dcba31dc8a35d209dd0129675b527502dc9895c"
171
+ "type": "module",
172
+ "gitHead": "5f8f3fc82c4a95176a5ce730b416bcee59316678"
216
173
  }
@@ -1,18 +1,14 @@
1
- import { loadLambda } from '../libs/utils';
1
+ import { loadLambda } from '../libs/utils.js';
2
2
  import { extra, autoService } from 'knifecycle';
3
3
  import { readArgs } from '@whook/cli';
4
- import YError from 'yerror';
4
+ import { YError } from 'yerror';
5
5
  import {
6
6
  dereferenceOpenAPIOperations,
7
7
  getOpenAPIOperations,
8
8
  } from '@whook/http-router';
9
9
  import stream from 'stream';
10
10
  import camelCase from 'camelcase';
11
- import type {
12
- WhookCommandArgs,
13
- WhookCommandDefinition,
14
- WhookCommandNamedArgs,
15
- } from '@whook/cli';
11
+ import type { WhookCommandArgs, WhookCommandDefinition } from '@whook/cli';
16
12
  import type { LogService } from 'common-services';
17
13
  import type { OpenAPIV3 } from 'openapi-types';
18
14
 
@@ -68,16 +64,13 @@ async function initTestHTTPLambdaCommand({
68
64
  }) {
69
65
  return async () => {
70
66
  const {
71
- name,
72
- type,
73
- contentType,
74
- parameters: rawParameters,
75
- }: WhookCommandNamedArgs = readArgs(definition.arguments, args) as {
67
+ namedArguments: { name, type, contentType, parameters: rawParameters },
68
+ } = readArgs<{
76
69
  name: string;
77
70
  type: string;
78
71
  contentType: string;
79
72
  parameters: string;
80
- };
73
+ }>(definition.arguments, args);
81
74
  const handler = await loadLambda(
82
75
  { PROJECT_DIR, log },
83
76
  NODE_ENV,
@@ -143,7 +136,7 @@ async function initTestHTTPLambdaCommand({
143
136
  if (hasBody) {
144
137
  gcpfRequest.headers['content-type'] = `${contentType};charset=UTF-8`;
145
138
  }
146
- log('info', 'GCPF_REQUEST:', gcpfRequest);
139
+ log('info', 'GCPF_REQUEST:', gcpfRequest as unknown as string);
147
140
 
148
141
  const response = {
149
142
  status: 0,
@@ -151,18 +144,18 @@ async function initTestHTTPLambdaCommand({
151
144
  data: '',
152
145
  };
153
146
  await new Promise<void>((resolve, reject) => {
154
- const gcpfResponse: any = new stream.PassThrough();
147
+ const gcpfResponse = new stream.PassThrough();
155
148
 
156
- gcpfResponse.set = (name: string, value: string) => {
149
+ (gcpfResponse as any).set = (name: string, value: string): void => {
157
150
  response.headers[name] = value;
158
151
  };
159
- gcpfResponse.status = (code: number) => {
152
+ (gcpfResponse as any).status = (code: number): void => {
160
153
  response.status = code;
161
154
  };
162
155
 
163
156
  handler(gcpfRequest, gcpfResponse).catch(reject);
164
157
 
165
- const chunks = [];
158
+ const chunks = [] as Buffer[];
166
159
 
167
160
  gcpfResponse.once('end', () => {
168
161
  response.data = Buffer.concat(chunks).toString();
@@ -170,7 +163,7 @@ async function initTestHTTPLambdaCommand({
170
163
  });
171
164
  gcpfResponse.once('error', reject);
172
165
  gcpfResponse.on('readable', () => {
173
- let data;
166
+ let data: Buffer;
174
167
  while ((data = gcpfResponse.read())) {
175
168
  chunks.push(data);
176
169
  }
package/src/index.ts CHANGED
@@ -4,19 +4,27 @@ import util from 'util';
4
4
  import path from 'path';
5
5
  import mkdirp from 'mkdirp';
6
6
  import cpr from 'cpr';
7
- import YError from 'yerror';
8
- import Knifecycle, {
7
+ import { YError } from 'yerror';
8
+ import { createRequire } from 'module';
9
+ import {
10
+ Knifecycle,
9
11
  SPECIAL_PROPS,
10
12
  constant,
11
13
  initInitializerBuilder,
12
14
  } from 'knifecycle';
13
- import { DEFAULT_BUILD_OPTIONS, initCompiler } from '@whook/whook';
14
- import initBuildAutoloader from './services/_autoload';
15
+ import { initCompiler } from '@whook/whook';
16
+ import initBuildAutoloader from './services/_autoload.js';
15
17
  import {
16
18
  dereferenceOpenAPIOperations,
17
19
  getOpenAPIOperations,
18
20
  } from '@whook/http-router';
19
- import type { Autoloader, Dependencies, BuildInitializer } from 'knifecycle';
21
+ import type {
22
+ Autoloader,
23
+ Dependencies,
24
+ BuildInitializer,
25
+ Initializer,
26
+ Service,
27
+ } from 'knifecycle';
20
28
  import type {
21
29
  WhookOperation,
22
30
  WhookCompilerOptions,
@@ -25,12 +33,10 @@ import type {
25
33
  import type { OpenAPIV3 } from 'openapi-types';
26
34
  import type { LogService } from 'common-services';
27
35
  import type { CprOptions } from 'cpr';
28
- import type { BuildOptions } from 'knifecycle/dist/build';
29
36
 
30
37
  export const DEFAULT_BUILD_PARALLELISM = 10;
31
38
 
32
- export type WhookBuildConfig = {
33
- BUILD_OPTIONS?: BuildOptions;
39
+ export type WhookGCPBuildConfig = {
34
40
  BUILD_PARALLELISM?: number;
35
41
  };
36
42
  export type WhookAPIOperationGCPFunctionConfig = {
@@ -56,8 +62,11 @@ const cprAsync = util.promisify(cpr) as (
56
62
  options: CprOptions,
57
63
  ) => Promise<string[]>;
58
64
 
65
+ // TODO: Use import.meta when Jest will support it
66
+ const require = createRequire(path.join(process.cwd(), 'src', 'index.ts'));
67
+
59
68
  const BUILD_DEFINITIONS: Record<
60
- WhookAPIOperationGCPFunctionConfig['type'],
69
+ NonNullable<WhookAPIOperationGCPFunctionConfig['type']>,
61
70
  {
62
71
  type: string;
63
72
  wrapper: { name: string; path: string };
@@ -68,19 +77,22 @@ const BUILD_DEFINITIONS: Record<
68
77
  type: 'HTTP',
69
78
  wrapper: {
70
79
  name: 'wrapHandlerForGoogleHTTPFunction',
71
- path: path.join(__dirname, 'wrappers', 'googleHTTPFunction'),
80
+ path: path.join(
81
+ path.dirname(require.resolve('@whook/gcp-function')),
82
+ 'wrappers/googleHTTPFunction.js',
83
+ ),
72
84
  },
73
85
  suffix: 'Wrapped',
74
86
  },
75
87
  };
76
88
 
77
- export async function prepareBuildEnvironment<
78
- T extends Knifecycle<Dependencies>,
79
- >($: T = new Knifecycle() as T): Promise<T> {
89
+ export async function prepareBuildEnvironment<T extends Knifecycle>(
90
+ $: T = new Knifecycle() as T,
91
+ ): Promise<T> {
80
92
  $.register(
81
93
  constant('INITIALIZER_PATH_MAP', {
82
94
  ENV: '@whook/whook/dist/services/ProxyedENV',
83
- log: __dirname + '/services/log',
95
+ log: '@whook/gcp-functions/dist/services/log',
84
96
  time: 'common-services/dist/time',
85
97
  delay: 'common-services/dist/delay',
86
98
  }),
@@ -103,25 +115,23 @@ export async function runBuild(
103
115
  const {
104
116
  NODE_ENV,
105
117
  BUILD_PARALLELISM,
106
- BUILD_OPTIONS,
107
118
  PROJECT_DIR,
108
119
  compiler,
109
120
  log,
110
121
  $autoload,
111
122
  API,
112
123
  buildInitializer,
113
- }: WhookBuildConfig & {
124
+ }: WhookGCPBuildConfig & {
114
125
  NODE_ENV: string;
115
126
  PROJECT_DIR: string;
116
127
  compiler: WhookCompilerService;
117
128
  log: LogService;
118
- $autoload: Autoloader;
129
+ $autoload: Autoloader<Initializer<Dependencies, Service>>;
119
130
  API: OpenAPIV3.Document;
120
131
  buildInitializer: BuildInitializer;
121
132
  } = await $.run([
122
133
  'NODE_ENV',
123
134
  '?BUILD_PARALLELISM',
124
- '?BUILD_OPTIONS',
125
135
  'PROJECT_DIR',
126
136
  'process',
127
137
  'compiler',
@@ -131,7 +141,7 @@ export async function runBuild(
131
141
  'buildInitializer',
132
142
  ]);
133
143
 
134
- log('info', 'Environment initialized 🚀🌕');
144
+ log('info', 'GCP Functions build Environment initialized 🚀🌕');
135
145
 
136
146
  const operations = (
137
147
  await dereferenceOpenAPIOperations(
@@ -155,7 +165,6 @@ export async function runBuild(
155
165
  await processOperations(
156
166
  {
157
167
  NODE_ENV,
158
- BUILD_OPTIONS: BUILD_OPTIONS || DEFAULT_BUILD_OPTIONS,
159
168
  BUILD_PARALLELISM: BUILD_PARALLELISM || DEFAULT_BUILD_PARALLELISM,
160
169
  PROJECT_DIR,
161
170
  compiler,
@@ -170,8 +179,8 @@ export async function runBuild(
170
179
  // eslint-disable-next-line
171
180
  console.error(
172
181
  '💀 - Cannot launch the build:',
173
- err.stack,
174
- JSON.stringify(err.params, null, 2),
182
+ (err as YError).stack,
183
+ JSON.stringify((err as YError).params, null, 2),
175
184
  );
176
185
  process.exit(1);
177
186
  }
@@ -181,20 +190,17 @@ async function processOperations(
181
190
  {
182
191
  NODE_ENV,
183
192
  BUILD_PARALLELISM,
184
- BUILD_OPTIONS,
185
193
  PROJECT_DIR,
186
194
  compiler,
187
195
  log,
188
196
  $autoload,
189
197
  buildInitializer,
190
- }: {
198
+ }: WhookGCPBuildConfig & {
191
199
  NODE_ENV: string;
192
- BUILD_PARALLELISM: number;
193
- BUILD_OPTIONS: BuildOptions;
194
200
  PROJECT_DIR: string;
195
201
  compiler: WhookCompilerService;
196
202
  log: LogService;
197
- $autoload: Autoloader;
203
+ $autoload: Autoloader<Initializer<Dependencies, Service>>;
198
204
  buildInitializer: BuildInitializer;
199
205
  },
200
206
  operations: WhookOperation<WhookAPIOperationGCPFunctionConfig>[],
@@ -207,7 +213,6 @@ async function processOperations(
207
213
  {
208
214
  NODE_ENV,
209
215
  PROJECT_DIR,
210
- BUILD_OPTIONS,
211
216
  compiler,
212
217
  log,
213
218
  $autoload,
@@ -224,7 +229,6 @@ async function processOperations(
224
229
  {
225
230
  NODE_ENV,
226
231
  BUILD_PARALLELISM,
227
- BUILD_OPTIONS,
228
232
  PROJECT_DIR,
229
233
  compiler,
230
234
  log,
@@ -241,7 +245,6 @@ async function buildAnyLambda(
241
245
  {
242
246
  NODE_ENV,
243
247
  PROJECT_DIR,
244
- BUILD_OPTIONS,
245
248
  compiler,
246
249
  log,
247
250
  $autoload,
@@ -249,10 +252,9 @@ async function buildAnyLambda(
249
252
  }: {
250
253
  NODE_ENV: string;
251
254
  PROJECT_DIR: string;
252
- BUILD_OPTIONS: BuildOptions;
253
255
  compiler: WhookCompilerService;
254
256
  log: LogService;
255
- $autoload: Autoloader;
257
+ $autoload: Autoloader<Initializer<Dependencies, Service>>;
256
258
  buildInitializer: BuildInitializer;
257
259
  },
258
260
  operation: WhookOperation<WhookAPIOperationGCPFunctionConfig>,
@@ -260,8 +262,9 @@ async function buildAnyLambda(
260
262
  const { operationId } = operation;
261
263
 
262
264
  try {
263
- const whookConfig: WhookAPIOperationGCPFunctionConfig =
264
- operation['x-whook'] || {};
265
+ const whookConfig: WhookAPIOperationGCPFunctionConfig = operation[
266
+ 'x-whook'
267
+ ] || { type: 'http' };
265
268
  const operationType = whookConfig.type || 'http';
266
269
  const sourceOperationId = whookConfig.sourceOperationId;
267
270
  const entryPoint = operationId;
@@ -271,7 +274,7 @@ async function buildAnyLambda(
271
274
  log('warning', `Building ${operationType} "${finalEntryPoint}"...`);
272
275
  const buildDefinition = BUILD_DEFINITIONS[operationType];
273
276
  // eslint-disable-next-line
274
- const applyWrapper = require(buildDefinition.wrapper.path).default;
277
+ const applyWrapper = (await import(buildDefinition.wrapper.path)).default;
275
278
  const rootNode = await $autoload(
276
279
  entryPoint + (buildDefinition.suffix || ''),
277
280
  );
@@ -289,16 +292,11 @@ async function buildAnyLambda(
289
292
  ? `OPERATION_API>OPERATION_API_${finalEntryPoint}`
290
293
  : name,
291
294
  ),
292
- BUILD_OPTIONS,
293
- );
294
- const indexContent = await buildLambdaIndex(
295
- rootNode,
296
- {
297
- name: buildDefinition.wrapper.name,
298
- path: buildDefinition.wrapper.path,
299
- },
300
- BUILD_OPTIONS,
301
295
  );
296
+ const indexContent = await buildLambdaIndex(rootNode, {
297
+ name: buildDefinition.wrapper.name,
298
+ path: buildDefinition.wrapper.path,
299
+ });
302
300
 
303
301
  await mkdirp(lambdaPath);
304
302
  await Promise.all([
@@ -317,27 +315,19 @@ async function buildAnyLambda(
317
315
  await buildFinalLambda({ compiler, log }, lambdaPath, whookConfig);
318
316
  } catch (err) {
319
317
  log('error', `Error building "${operationId}"...`);
320
- log('stack', err.stack);
321
- log('debug', JSON.stringify(err.params, null, 2));
322
- throw YError.wrap(err, 'E_LAMBDA_BUILD', operationId);
318
+ log('error-stack', (err as Error).stack || 'no_stack_trace');
319
+ log('debug', JSON.stringify((err as YError).params, null, 2));
320
+ throw YError.wrap(err as Error, 'E_LAMBDA_BUILD', operationId);
323
321
  }
324
322
  }
325
323
 
326
324
  async function buildLambdaIndex(
327
325
  rootNode: { path: string },
328
326
  buildWrapper: { name: string; path: string },
329
- options: BuildOptions,
330
327
  ): Promise<string> {
331
- return `${
332
- options.modules === 'commonjs'
333
- ? `const pickModule = (m) => { return m && m.default || m; }
334
- const initHandler = pickModule(require('${rootNode.path}'));
335
- const ${buildWrapper.name} = pickModule(require('${buildWrapper.path}'));
336
- const { initialize } = require('./initialize');`
337
- : `import initHandler from '${rootNode.path}';
328
+ return `import initHandler from '${rootNode.path}';
338
329
  import ${buildWrapper.name} from '${buildWrapper.path}';
339
- import { initialize } from './initialize';`
340
- }
330
+ import { initialize } from './initialize.js';
341
331
 
342
332
  const handlerInitializer = ${buildWrapper.name}(
343
333
  initHandler
@@ -346,11 +336,7 @@ const handlerInitializer = ${buildWrapper.name}(
346
336
  const handlerPromise = initialize()
347
337
  .then(handlerInitializer);
348
338
 
349
- ${
350
- options.modules === 'commonjs'
351
- ? 'module.exports = {}; module.exports.default = '
352
- : 'export default '
353
- }function handler (req, res) {
339
+ export default function handler (req, res) {
354
340
  return handlerPromise
355
341
  .then(handler => handler(req, res));
356
342
  };
package/src/libs/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import YError from 'yerror';
1
+ import { YError } from 'yerror';
2
2
  import path from 'path';
3
3
  import type { LogService } from 'common-services';
4
4
 
@@ -19,14 +19,13 @@ export async function loadLambda(
19
19
  'builds',
20
20
  target,
21
21
  operationId,
22
- type,
22
+ type + '.js',
23
23
  );
24
24
 
25
25
  log('debug', `⛏️ - Loading lambda module at path "${modulePath}".`);
26
26
 
27
27
  try {
28
- // eslint-disable-next-line
29
- const module = require(modulePath);
28
+ const module = await import(modulePath);
30
29
 
31
30
  if (!module) {
32
31
  throw new YError('E_MODULE_NOT_FOUND', module);
@@ -38,6 +37,6 @@ export async function loadLambda(
38
37
 
39
38
  return module.default;
40
39
  } catch (err) {
41
- throw YError.wrap(err, 'E_LAMBDA_LOAD');
40
+ throw YError.wrap(err as Error, 'E_LAMBDA_LOAD');
42
41
  }
43
42
  }