@whook/gcp-functions 18.1.0 → 19.0.1
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/README.md +16 -54
- package/dist/commands/testGCPFunctionRoute.d.ts +50 -0
- package/dist/commands/{testHTTPFunction.js → testGCPFunctionRoute.js} +43 -44
- package/dist/commands/testGCPFunctionRoute.js.map +1 -0
- package/dist/index.d.ts +7 -6
- package/dist/index.js +49 -52
- package/dist/index.js.map +1 -1
- package/dist/services/_autoload.d.ts +14 -9
- package/dist/services/_autoload.js +49 -62
- package/dist/services/_autoload.js.map +1 -1
- package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.d.ts +19 -0
- package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.js +307 -0
- package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.js.map +1 -0
- package/package.json +10 -8
- package/src/commands/{testHTTPFunction.ts → testGCPFunctionRoute.ts} +68 -69
- package/src/index.ts +85 -87
- package/src/services/_autoload.ts +79 -106
- package/src/types.d.ts +8 -0
- package/src/wrappers/wrapRouteHandlerForGoogleHTTPFunction.ts +571 -0
- package/dist/commands/testHTTPFunction.d.ts +0 -13
- package/dist/commands/testHTTPFunction.js.map +0 -1
- package/dist/services/HANDLER.d.ts +0 -10
- package/dist/services/HANDLER.js +0 -21
- package/dist/services/HANDLER.js.map +0 -1
- package/dist/services/log.d.ts +0 -5
- package/dist/services/log.js +0 -4
- package/dist/services/log.js.map +0 -1
- package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.d.ts +0 -21
- package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.js +0 -270
- package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.js.map +0 -1
- package/src/services/HANDLER.ts +0 -47
- package/src/services/log.ts +0 -7
- package/src/wrappers/wrapHandlerForGoogleHTTPFunction.ts +0 -480
|
@@ -1,81 +1,84 @@
|
|
|
1
1
|
import { loadFunction } from '../libs/utils.js';
|
|
2
|
-
import {
|
|
2
|
+
import { location, autoService } from 'knifecycle';
|
|
3
3
|
import { YError } from 'yerror';
|
|
4
4
|
import stream from 'node:stream';
|
|
5
|
-
import camelCase from 'camelcase';
|
|
6
5
|
import {
|
|
7
|
-
dereferenceOpenAPIOperations,
|
|
8
|
-
getOpenAPIOperations,
|
|
9
6
|
DEFAULT_COMPILER_OPTIONS,
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
PATH_SEPARATOR,
|
|
8
|
+
SEARCH_SEPARATOR,
|
|
9
|
+
type WhookRouteHandlerParameters,
|
|
10
|
+
type WhookCommandHandler,
|
|
12
11
|
type WhookCommandDefinition,
|
|
13
12
|
type WhookCompilerOptions,
|
|
13
|
+
type WhookRoutesDefinitionsService,
|
|
14
14
|
} from '@whook/whook';
|
|
15
15
|
import { type LogService } from 'common-services';
|
|
16
|
-
import { type OpenAPIV3_1 } from 'openapi-types';
|
|
17
16
|
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
properties: {
|
|
29
|
-
name: {
|
|
30
|
-
description: 'Name of the function to run',
|
|
17
|
+
export const definition = {
|
|
18
|
+
name: 'testGCPFunctionRoute',
|
|
19
|
+
description: 'A command for testing a GCP function route',
|
|
20
|
+
example: `whook testGCPFunctionRoute --name getPing`,
|
|
21
|
+
arguments: [
|
|
22
|
+
{
|
|
23
|
+
description: 'Name of the function to run',
|
|
24
|
+
name: 'name',
|
|
25
|
+
required: true,
|
|
26
|
+
schema: {
|
|
31
27
|
type: 'string',
|
|
32
28
|
},
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'type',
|
|
32
|
+
description: 'Type of function to test',
|
|
33
|
+
schema: {
|
|
35
34
|
type: 'string',
|
|
36
35
|
enum: ['main', 'index'],
|
|
37
36
|
default: 'index',
|
|
38
37
|
},
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'contentType',
|
|
41
|
+
description: 'Content type of the payload',
|
|
42
|
+
schema: {
|
|
41
43
|
type: 'string',
|
|
42
44
|
default: 'application/json',
|
|
43
45
|
},
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'parameters',
|
|
49
|
+
description: 'The HTTP call parameters',
|
|
50
|
+
schema: {
|
|
46
51
|
type: 'string',
|
|
47
52
|
default: '{}',
|
|
48
53
|
},
|
|
49
54
|
},
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export default extra(definition, autoService(initTestHTTPFunctionCommand));
|
|
55
|
+
],
|
|
56
|
+
} as const satisfies WhookCommandDefinition;
|
|
54
57
|
|
|
55
|
-
async function
|
|
58
|
+
async function initTestGCPFunctionRouteCommand({
|
|
56
59
|
APP_ENV,
|
|
57
60
|
PROJECT_DIR,
|
|
58
61
|
COMPILER_OPTIONS = DEFAULT_COMPILER_OPTIONS,
|
|
59
|
-
|
|
62
|
+
ROUTES_DEFINITIONS,
|
|
60
63
|
log,
|
|
61
|
-
args,
|
|
62
64
|
}: {
|
|
63
65
|
APP_ENV: string;
|
|
64
66
|
PROJECT_DIR: string;
|
|
65
67
|
COMPILER_OPTIONS?: WhookCompilerOptions;
|
|
66
|
-
|
|
68
|
+
ROUTES_DEFINITIONS: WhookRoutesDefinitionsService;
|
|
67
69
|
log: LogService;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
}): Promise<
|
|
71
|
+
WhookCommandHandler<{
|
|
72
|
+
name: string;
|
|
73
|
+
type: string;
|
|
74
|
+
contentType: string;
|
|
75
|
+
parameters: string;
|
|
76
|
+
}>
|
|
77
|
+
> {
|
|
78
|
+
return async (args) => {
|
|
71
79
|
const {
|
|
72
80
|
namedArguments: { name, type, contentType, parameters: rawParameters },
|
|
73
|
-
} =
|
|
74
|
-
name: string;
|
|
75
|
-
type: string;
|
|
76
|
-
contentType: string;
|
|
77
|
-
parameters: string;
|
|
78
|
-
}>(definition.arguments, args);
|
|
81
|
+
} = args;
|
|
79
82
|
const extension = COMPILER_OPTIONS.format === 'cjs' ? '.cjs' : '.mjs';
|
|
80
83
|
const handler = await loadFunction(
|
|
81
84
|
{ APP_ENV, PROJECT_DIR, log },
|
|
@@ -83,61 +86,52 @@ async function initTestHTTPFunctionCommand({
|
|
|
83
86
|
type,
|
|
84
87
|
extension,
|
|
85
88
|
);
|
|
86
|
-
const
|
|
87
|
-
await dereferenceOpenAPIOperations(API, getOpenAPIOperations(API))
|
|
88
|
-
).find(({ operationId }) => operationId === name);
|
|
89
|
+
const handlerDefinition = ROUTES_DEFINITIONS[name]?.module?.definition;
|
|
89
90
|
|
|
90
|
-
if (!
|
|
91
|
+
if (!handlerDefinition) {
|
|
91
92
|
throw new YError('E_OPERATION_NOT_FOUND');
|
|
92
93
|
}
|
|
93
94
|
|
|
94
|
-
const hasBody = !!
|
|
95
|
-
const parameters = JSON.parse(rawParameters);
|
|
96
|
-
const search = (
|
|
97
|
-
(
|
|
98
|
-
|
|
99
|
-
.filter((p) => p.in === 'query')
|
|
100
|
-
.reduce((accSearch, p) => {
|
|
101
|
-
if (null != parameters[p.name]) {
|
|
95
|
+
const hasBody = !!handlerDefinition.operation.requestBody;
|
|
96
|
+
const parameters = JSON.parse(rawParameters) as WhookRouteHandlerParameters;
|
|
97
|
+
const search = Object.keys(parameters.query || {}).reduce(
|
|
98
|
+
(accSearch, name) => {
|
|
99
|
+
if (null != parameters.query[name]) {
|
|
102
100
|
return (
|
|
103
101
|
accSearch +
|
|
104
102
|
(accSearch ? '&' : '') +
|
|
105
|
-
|
|
103
|
+
name +
|
|
106
104
|
'=' +
|
|
107
|
-
parameters[
|
|
105
|
+
parameters.query[name]
|
|
108
106
|
);
|
|
109
107
|
}
|
|
110
108
|
return accSearch;
|
|
111
|
-
},
|
|
109
|
+
},
|
|
110
|
+
'',
|
|
111
|
+
);
|
|
112
112
|
|
|
113
|
-
const path =
|
|
113
|
+
const path = handlerDefinition.path
|
|
114
114
|
.split(PATH_SEPARATOR)
|
|
115
|
-
|
|
116
115
|
.map((part) => {
|
|
117
116
|
const matches = /^\{([\d\w]+)\}$/i.exec(part);
|
|
118
117
|
|
|
119
118
|
if (matches) {
|
|
120
|
-
return parameters[matches[1]];
|
|
119
|
+
return parameters.path?.[matches[1]];
|
|
121
120
|
}
|
|
122
121
|
return part;
|
|
123
122
|
})
|
|
124
123
|
.join(PATH_SEPARATOR);
|
|
125
124
|
const gcpfRequest = {
|
|
126
|
-
method:
|
|
125
|
+
method: handlerDefinition.method,
|
|
127
126
|
originalUrl: path + (search ? SEARCH_SEPARATOR + search : ''),
|
|
128
|
-
headers:
|
|
129
|
-
.filter((p) => p.in === 'header')
|
|
130
|
-
.reduce((headerParameters, p) => {
|
|
131
|
-
headerParameters[p.name] = parameters[camelCase(p.name)];
|
|
132
|
-
return headerParameters;
|
|
133
|
-
}, {}),
|
|
127
|
+
headers: parameters.header || {},
|
|
134
128
|
rawBody: Buffer.from(
|
|
135
129
|
hasBody
|
|
136
130
|
? contentType === 'application/json'
|
|
137
131
|
? parameters.body
|
|
138
132
|
? JSON.stringify(parameters.body)
|
|
139
133
|
: ''
|
|
140
|
-
: parameters.body || ''
|
|
134
|
+
: (parameters.body as string) || ''
|
|
141
135
|
: '',
|
|
142
136
|
),
|
|
143
137
|
};
|
|
@@ -182,3 +176,8 @@ async function initTestHTTPFunctionCommand({
|
|
|
182
176
|
log('info', 'SUCCESS:', response);
|
|
183
177
|
};
|
|
184
178
|
}
|
|
179
|
+
|
|
180
|
+
export default location(
|
|
181
|
+
autoService(initTestGCPFunctionRouteCommand),
|
|
182
|
+
import.meta.url,
|
|
183
|
+
);
|
package/src/index.ts
CHANGED
|
@@ -20,33 +20,32 @@ import initBuildAutoloader from './services/_autoload.js';
|
|
|
20
20
|
import {
|
|
21
21
|
DEFAULT_BUILD_INITIALIZER_PATH_MAP as BASE_DEFAULT_BUILD_INITIALIZER_PATH_MAP,
|
|
22
22
|
initCompiler,
|
|
23
|
-
|
|
24
|
-
getOpenAPIOperations,
|
|
25
|
-
type WhookOperation,
|
|
23
|
+
type WhookDefinitions,
|
|
26
24
|
type WhookCompilerOptions,
|
|
27
25
|
type WhookCompilerService,
|
|
28
26
|
} from '@whook/whook';
|
|
29
|
-
import { type OpenAPIV3_1 } from 'openapi-types';
|
|
30
27
|
import { type LogService } from 'common-services';
|
|
31
28
|
import { type CprOptions } from 'cpr';
|
|
32
29
|
import { parseArgs } from '@whook/whook/dist/libs/args.js';
|
|
30
|
+
import initWrapRouteHandlerForGoogleHTTPFunction from './wrappers/wrapRouteHandlerForGoogleHTTPFunction.js';
|
|
33
31
|
|
|
34
32
|
export const DEFAULT_BUILD_PARALLELISM = 10;
|
|
35
33
|
export const DEFAULT_BUILD_INITIALIZER_PATH_MAP = {
|
|
36
34
|
...BASE_DEFAULT_BUILD_INITIALIZER_PATH_MAP,
|
|
37
|
-
log: '@whook/
|
|
35
|
+
log: '@whook/whook/dist/services/rawLog.js',
|
|
38
36
|
};
|
|
39
37
|
|
|
40
|
-
export type
|
|
38
|
+
export type * from './wrappers/wrapRouteHandlerForGoogleHTTPFunction.js';
|
|
39
|
+
export { initWrapRouteHandlerForGoogleHTTPFunction };
|
|
40
|
+
|
|
41
|
+
export type WhookGCPFunctionBuildConfig = {
|
|
41
42
|
BUILD_PARALLELISM?: number;
|
|
42
43
|
};
|
|
43
|
-
export type
|
|
44
|
-
type?: 'http';
|
|
45
|
-
sourceOperationId?: string;
|
|
44
|
+
export type WhookGCPFunctionBaseConfig = {
|
|
46
45
|
staticFiles?: string[];
|
|
47
46
|
compilerOptions?: WhookCompilerOptions;
|
|
48
|
-
suffix?: string;
|
|
49
47
|
};
|
|
48
|
+
export type WhookGCPFunctionRouteConfig = WhookGCPFunctionBaseConfig;
|
|
50
49
|
|
|
51
50
|
const cprAsync = promisify(cpr) as (
|
|
52
51
|
source: string,
|
|
@@ -83,15 +82,15 @@ export async function runBuild(
|
|
|
83
82
|
compiler,
|
|
84
83
|
log,
|
|
85
84
|
$autoload,
|
|
86
|
-
|
|
85
|
+
DEFINITIONS,
|
|
87
86
|
buildInitializer,
|
|
88
|
-
}:
|
|
87
|
+
}: WhookGCPFunctionBuildConfig & {
|
|
89
88
|
APP_ENV: string;
|
|
90
89
|
PROJECT_DIR: string;
|
|
91
90
|
compiler: WhookCompilerService;
|
|
92
91
|
log: LogService;
|
|
93
92
|
$autoload: Autoloader<Initializer<Dependencies, Service>>;
|
|
94
|
-
|
|
93
|
+
DEFINITIONS: WhookDefinitions;
|
|
95
94
|
buildInitializer: BuildInitializer;
|
|
96
95
|
} = await $.run([
|
|
97
96
|
'APP_ENV',
|
|
@@ -101,43 +100,30 @@ export async function runBuild(
|
|
|
101
100
|
'compiler',
|
|
102
101
|
'log',
|
|
103
102
|
'$autoload',
|
|
104
|
-
'
|
|
103
|
+
'DEFINITIONS',
|
|
105
104
|
'buildInitializer',
|
|
106
105
|
]);
|
|
107
106
|
|
|
108
107
|
log('info', 'GCP Functions build Environment initialized 🚀🌕');
|
|
109
108
|
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
getOpenAPIOperations<WhookAPIOperationGCPFunctionConfig>(API),
|
|
114
|
-
)
|
|
115
|
-
).filter((operation) => {
|
|
116
|
-
if (handlerName) {
|
|
117
|
-
const sourceOperationId =
|
|
118
|
-
operation['x-whook'] && operation['x-whook'].sourceOperationId;
|
|
119
|
-
|
|
120
|
-
return (
|
|
121
|
-
handlerName === operation.operationId ||
|
|
122
|
-
handlerName === sourceOperationId
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
return true;
|
|
126
|
-
});
|
|
109
|
+
const handlerNames = Object.keys(DEFINITIONS.configs).filter(
|
|
110
|
+
(aHandlerName) => handlerName === aHandlerName || !handlerName,
|
|
111
|
+
);
|
|
127
112
|
|
|
128
|
-
log('warning', `📃 - ${
|
|
113
|
+
log('warning', `📃 - ${handlerNames.length} handlerNames to process.`);
|
|
129
114
|
|
|
130
|
-
await
|
|
115
|
+
await processHandlers(
|
|
131
116
|
{
|
|
132
117
|
APP_ENV,
|
|
133
118
|
BUILD_PARALLELISM: BUILD_PARALLELISM || DEFAULT_BUILD_PARALLELISM,
|
|
134
119
|
PROJECT_DIR,
|
|
120
|
+
DEFINITIONS,
|
|
135
121
|
compiler,
|
|
136
122
|
log,
|
|
137
123
|
$autoload,
|
|
138
124
|
buildInitializer,
|
|
139
125
|
},
|
|
140
|
-
|
|
126
|
+
handlerNames,
|
|
141
127
|
);
|
|
142
128
|
await $.destroy();
|
|
143
129
|
} catch (err) {
|
|
@@ -148,124 +134,128 @@ export async function runBuild(
|
|
|
148
134
|
}
|
|
149
135
|
}
|
|
150
136
|
|
|
151
|
-
async function
|
|
137
|
+
async function processHandlers(
|
|
152
138
|
{
|
|
153
139
|
APP_ENV,
|
|
154
140
|
BUILD_PARALLELISM,
|
|
155
141
|
PROJECT_DIR,
|
|
142
|
+
DEFINITIONS,
|
|
156
143
|
compiler,
|
|
157
144
|
log,
|
|
158
145
|
$autoload,
|
|
159
146
|
buildInitializer,
|
|
160
|
-
}:
|
|
147
|
+
}: WhookGCPFunctionBuildConfig & {
|
|
161
148
|
APP_ENV: string;
|
|
162
149
|
PROJECT_DIR: string;
|
|
150
|
+
DEFINITIONS: WhookDefinitions;
|
|
163
151
|
compiler: WhookCompilerService;
|
|
164
152
|
log: LogService;
|
|
165
153
|
$autoload: Autoloader<Initializer<Dependencies, Service>>;
|
|
166
154
|
buildInitializer: BuildInitializer;
|
|
167
155
|
},
|
|
168
|
-
|
|
156
|
+
handlerNames: string[],
|
|
169
157
|
): Promise<void> {
|
|
170
|
-
const
|
|
158
|
+
const handlerNamesLeft = handlerNames.slice(BUILD_PARALLELISM);
|
|
171
159
|
|
|
172
160
|
await Promise.all(
|
|
173
|
-
|
|
174
|
-
|
|
161
|
+
handlerNames.slice(0, BUILD_PARALLELISM).map((handlerName) =>
|
|
162
|
+
buildHandler(
|
|
175
163
|
{
|
|
176
164
|
APP_ENV,
|
|
177
165
|
PROJECT_DIR,
|
|
166
|
+
DEFINITIONS,
|
|
178
167
|
compiler,
|
|
179
168
|
log,
|
|
180
169
|
buildInitializer,
|
|
181
170
|
},
|
|
182
|
-
|
|
171
|
+
handlerName,
|
|
183
172
|
),
|
|
184
173
|
),
|
|
185
174
|
);
|
|
186
175
|
|
|
187
|
-
if (
|
|
188
|
-
log('info', `📃 - ${
|
|
189
|
-
return
|
|
176
|
+
if (handlerNamesLeft.length) {
|
|
177
|
+
log('info', `📃 - ${handlerNamesLeft.length} handlerNames left.`);
|
|
178
|
+
return processHandlers(
|
|
190
179
|
{
|
|
191
180
|
APP_ENV,
|
|
192
181
|
BUILD_PARALLELISM,
|
|
193
182
|
PROJECT_DIR,
|
|
183
|
+
DEFINITIONS,
|
|
194
184
|
compiler,
|
|
195
185
|
log,
|
|
196
186
|
$autoload,
|
|
197
187
|
buildInitializer,
|
|
198
188
|
},
|
|
199
|
-
|
|
189
|
+
handlerNamesLeft,
|
|
200
190
|
);
|
|
201
191
|
}
|
|
202
|
-
log('info', '🤷 - No more
|
|
192
|
+
log('info', '🤷 - No more handlerNames.');
|
|
203
193
|
}
|
|
204
194
|
|
|
205
|
-
async function
|
|
195
|
+
async function buildHandler(
|
|
206
196
|
{
|
|
207
197
|
APP_ENV,
|
|
208
198
|
PROJECT_DIR,
|
|
199
|
+
DEFINITIONS,
|
|
209
200
|
compiler,
|
|
210
201
|
log,
|
|
211
202
|
buildInitializer,
|
|
212
203
|
}: {
|
|
213
204
|
APP_ENV: string;
|
|
214
205
|
PROJECT_DIR: string;
|
|
206
|
+
DEFINITIONS: WhookDefinitions;
|
|
215
207
|
compiler: WhookCompilerService;
|
|
216
208
|
log: LogService;
|
|
217
209
|
buildInitializer: BuildInitializer;
|
|
218
210
|
},
|
|
219
|
-
|
|
211
|
+
handlerName: string,
|
|
220
212
|
): Promise<void> {
|
|
221
|
-
const { operationId } = operation;
|
|
222
|
-
|
|
223
213
|
try {
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const lambdaPath = join(PROJECT_DIR, 'builds', APP_ENV, finalEntryPoint);
|
|
214
|
+
const definition = DEFINITIONS.configs[handlerName];
|
|
215
|
+
|
|
216
|
+
if (definition.type !== 'route') {
|
|
217
|
+
log('warning', `🚮 - Skipping "${handlerName}"...`);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
log('warning', `🏗 - Building "${handlerName}"...`);
|
|
222
|
+
|
|
223
|
+
const handlerPath = join(PROJECT_DIR, 'builds', APP_ENV, handlerName);
|
|
236
224
|
const srcPath = join(PROJECT_DIR, 'src');
|
|
237
|
-
const srcRelativePath = relative(
|
|
225
|
+
const srcRelativePath = relative(handlerPath, srcPath);
|
|
238
226
|
|
|
239
227
|
const initializerContent = (
|
|
240
|
-
await buildInitializer([`
|
|
228
|
+
await buildInitializer([`MAIN_HANDLER_${handlerName}`])
|
|
241
229
|
).replaceAll(pathToFileURL(srcPath).toString(), srcRelativePath);
|
|
242
|
-
const indexContent = await
|
|
243
|
-
`OPERATION_HANDLER_${finalEntryPoint}`,
|
|
244
|
-
);
|
|
230
|
+
const indexContent = await buildHandlerIndex(`MAIN_HANDLER_${handlerName}`);
|
|
245
231
|
|
|
246
|
-
await mkdirp(
|
|
232
|
+
await mkdirp(handlerPath);
|
|
247
233
|
await Promise.all([
|
|
248
234
|
copyStaticFiles(
|
|
249
235
|
{ PROJECT_DIR, log },
|
|
250
|
-
|
|
251
|
-
|
|
236
|
+
handlerPath,
|
|
237
|
+
definition.config?.staticFiles || [],
|
|
252
238
|
),
|
|
253
239
|
ensureFile(
|
|
254
240
|
{ log },
|
|
255
|
-
join(
|
|
241
|
+
join(handlerPath, 'initialize.js'),
|
|
256
242
|
initializerContent,
|
|
257
243
|
),
|
|
258
|
-
ensureFile({ log }, join(
|
|
244
|
+
ensureFile({ log }, join(handlerPath, 'main.js'), indexContent),
|
|
259
245
|
]);
|
|
260
|
-
await
|
|
246
|
+
await buildFinalHandler(
|
|
247
|
+
{ DEFINITIONS, compiler, log },
|
|
248
|
+
handlerPath,
|
|
249
|
+
handlerName,
|
|
250
|
+
);
|
|
261
251
|
} catch (err) {
|
|
262
|
-
log('error',
|
|
252
|
+
log('error', `💥 - Error building "${handlerName}"...`);
|
|
263
253
|
log('error-stack', printStackTrace(err as Error));
|
|
264
|
-
throw YError.wrap(err as Error, '
|
|
254
|
+
throw YError.wrap(err as Error, 'E_HANDLER_BUILD', handlerName);
|
|
265
255
|
}
|
|
266
256
|
}
|
|
267
257
|
|
|
268
|
-
async function
|
|
258
|
+
async function buildHandlerIndex(handlerName: string): Promise<string> {
|
|
269
259
|
return `// Automatically generated by \`@whook/gcp-functions\`
|
|
270
260
|
import { initialize } from './initialize.js';
|
|
271
261
|
|
|
@@ -273,28 +263,36 @@ const initializationPromise = initialize();
|
|
|
273
263
|
|
|
274
264
|
export default function handler (req, res) {
|
|
275
265
|
return initializationPromise
|
|
276
|
-
.then(services => services['${
|
|
266
|
+
.then(services => services['${handlerName}'](req, res));
|
|
277
267
|
};
|
|
278
268
|
`;
|
|
279
269
|
}
|
|
280
270
|
|
|
281
|
-
async function
|
|
282
|
-
{
|
|
283
|
-
|
|
284
|
-
|
|
271
|
+
async function buildFinalHandler(
|
|
272
|
+
{
|
|
273
|
+
DEFINITIONS,
|
|
274
|
+
compiler,
|
|
275
|
+
log,
|
|
276
|
+
}: {
|
|
277
|
+
DEFINITIONS: WhookDefinitions;
|
|
278
|
+
compiler: WhookCompilerService;
|
|
279
|
+
log: LogService;
|
|
280
|
+
},
|
|
281
|
+
handlerPath: string,
|
|
282
|
+
handlerName: string,
|
|
285
283
|
): Promise<void> {
|
|
286
|
-
const entryPoint = `${
|
|
284
|
+
const entryPoint = `${handlerPath}/main.js`;
|
|
287
285
|
const { extension, contents, mappings } = await compiler(
|
|
288
286
|
entryPoint,
|
|
289
|
-
|
|
287
|
+
DEFINITIONS[handlerName]?.config?.compilerOptions,
|
|
290
288
|
);
|
|
291
289
|
|
|
292
290
|
await Promise.all([
|
|
293
|
-
ensureFile({ log }, join(
|
|
291
|
+
ensureFile({ log }, join(handlerPath, `/index${extension}`), contents),
|
|
294
292
|
mappings
|
|
295
293
|
? ensureFile(
|
|
296
294
|
{ log },
|
|
297
|
-
join(
|
|
295
|
+
join(handlerPath, `/index${extension}.map`),
|
|
298
296
|
mappings,
|
|
299
297
|
)
|
|
300
298
|
: Promise.resolve(),
|
|
@@ -303,7 +301,7 @@ async function buildFinalLambda(
|
|
|
303
301
|
|
|
304
302
|
async function copyStaticFiles(
|
|
305
303
|
{ PROJECT_DIR, log }: { PROJECT_DIR: string; log: LogService },
|
|
306
|
-
|
|
304
|
+
handlerPath: string,
|
|
307
305
|
staticFiles: string[] = [],
|
|
308
306
|
): Promise<void> {
|
|
309
307
|
await Promise.all(
|
|
@@ -312,7 +310,7 @@ async function copyStaticFiles(
|
|
|
312
310
|
await copyFiles(
|
|
313
311
|
{ log },
|
|
314
312
|
join(PROJECT_DIR, 'node_modules', staticFile),
|
|
315
|
-
join(
|
|
313
|
+
join(handlerPath, 'node_modules', staticFile),
|
|
316
314
|
),
|
|
317
315
|
),
|
|
318
316
|
);
|