@whook/gcp-functions 8.3.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.
- package/LICENSE +20 -0
- package/README.md +187 -0
- package/dist/commands/testHTTPFunction.d.ts +12 -0
- package/dist/commands/testHTTPFunction.js +156 -0
- package/dist/commands/testHTTPFunction.js.map +1 -0
- package/dist/commands/testHTTPFunction.mjs +136 -0
- package/dist/commands/testHTTPFunction.mjs.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +299 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +265 -0
- package/dist/index.mjs.map +1 -0
- package/dist/libs/utils.d.ts +5 -0
- package/dist/libs/utils.js +39 -0
- package/dist/libs/utils.js.map +1 -0
- package/dist/libs/utils.mjs +27 -0
- package/dist/libs/utils.mjs.map +1 -0
- package/dist/services/_autoload.d.ts +17 -0
- package/dist/services/_autoload.js +121 -0
- package/dist/services/_autoload.js.map +1 -0
- package/dist/services/_autoload.mjs +107 -0
- package/dist/services/_autoload.mjs.map +1 -0
- package/dist/services/log.d.ts +2 -0
- package/dist/services/log.js +14 -0
- package/dist/services/log.js.map +1 -0
- package/dist/services/log.mjs +4 -0
- package/dist/services/log.mjs.map +1 -0
- package/dist/services/log.test.d.ts +1 -0
- package/dist/services/log.test.js +12 -0
- package/dist/services/log.test.js.map +1 -0
- package/dist/services/log.test.mjs +7 -0
- package/dist/services/log.test.mjs.map +1 -0
- package/dist/wrappers/googleHTTPFunction.d.ts +22 -0
- package/dist/wrappers/googleHTTPFunction.js +310 -0
- package/dist/wrappers/googleHTTPFunction.js.map +1 -0
- package/dist/wrappers/googleHTTPFunction.mjs +289 -0
- package/dist/wrappers/googleHTTPFunction.mjs.map +1 -0
- package/package.json +215 -0
- package/src/commands/testHTTPFunction.ts +181 -0
- package/src/index.ts +443 -0
- package/src/libs/utils.ts +43 -0
- package/src/services/_autoload.ts +161 -0
- package/src/services/log.test.ts +7 -0
- package/src/services/log.ts +4 -0
- package/src/wrappers/googleHTTPFunction.ts +468 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.prepareBuildEnvironment = prepareBuildEnvironment;
|
|
7
|
+
exports.runBuild = runBuild;
|
|
8
|
+
exports.DEFAULT_BUILD_PARALLELISM = void 0;
|
|
9
|
+
|
|
10
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
11
|
+
|
|
12
|
+
var _util = _interopRequireDefault(require("util"));
|
|
13
|
+
|
|
14
|
+
var _path = _interopRequireDefault(require("path"));
|
|
15
|
+
|
|
16
|
+
var _mkdirp = _interopRequireDefault(require("mkdirp"));
|
|
17
|
+
|
|
18
|
+
var _cpr = _interopRequireDefault(require("cpr"));
|
|
19
|
+
|
|
20
|
+
var _yerror = _interopRequireDefault(require("yerror"));
|
|
21
|
+
|
|
22
|
+
var _knifecycle = _interopRequireWildcard(require("knifecycle"));
|
|
23
|
+
|
|
24
|
+
var _whook = require("@whook/whook");
|
|
25
|
+
|
|
26
|
+
var _autoload = _interopRequireDefault(require("./services/_autoload"));
|
|
27
|
+
|
|
28
|
+
var _httpRouter = require("@whook/http-router");
|
|
29
|
+
|
|
30
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
31
|
+
|
|
32
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
33
|
+
|
|
34
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
35
|
+
|
|
36
|
+
/* eslint global-require:0 */
|
|
37
|
+
const DEFAULT_BUILD_PARALLELISM = 10;
|
|
38
|
+
exports.DEFAULT_BUILD_PARALLELISM = DEFAULT_BUILD_PARALLELISM;
|
|
39
|
+
|
|
40
|
+
const readFileAsync = _util.default.promisify(_fs.default.readFile);
|
|
41
|
+
|
|
42
|
+
const writeFileAsync = _util.default.promisify(_fs.default.writeFile);
|
|
43
|
+
|
|
44
|
+
const cprAsync = _util.default.promisify(_cpr.default);
|
|
45
|
+
|
|
46
|
+
const BUILD_DEFINITIONS = {
|
|
47
|
+
http: {
|
|
48
|
+
type: 'HTTP',
|
|
49
|
+
wrapper: {
|
|
50
|
+
name: 'wrapHandlerForGoogleHTTPFunction',
|
|
51
|
+
path: _path.default.join(__dirname, 'wrappers', 'googleHTTPFunction')
|
|
52
|
+
},
|
|
53
|
+
suffix: 'Wrapped'
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
async function prepareBuildEnvironment($ = new _knifecycle.default()) {
|
|
58
|
+
$.register((0, _knifecycle.constant)('INITIALIZER_PATH_MAP', {
|
|
59
|
+
ENV: '@whook/whook/dist/services/ProxyedENV',
|
|
60
|
+
log: __dirname + '/services/log',
|
|
61
|
+
time: 'common-services/dist/time',
|
|
62
|
+
delay: 'common-services/dist/delay'
|
|
63
|
+
}));
|
|
64
|
+
$.register(_knifecycle.initInitializerBuilder);
|
|
65
|
+
$.register(_autoload.default);
|
|
66
|
+
$.register(_whook.initCompiler);
|
|
67
|
+
$.register((0, _knifecycle.constant)('PORT', 1337));
|
|
68
|
+
$.register((0, _knifecycle.constant)('HOST', 'localhost'));
|
|
69
|
+
return $;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function runBuild(aPrepareBuildEnvironment) {
|
|
73
|
+
try {
|
|
74
|
+
const handlerName = process.argv[2];
|
|
75
|
+
const $ = await aPrepareBuildEnvironment();
|
|
76
|
+
const {
|
|
77
|
+
NODE_ENV,
|
|
78
|
+
BUILD_PARALLELISM,
|
|
79
|
+
BUILD_OPTIONS,
|
|
80
|
+
PROJECT_DIR,
|
|
81
|
+
compiler,
|
|
82
|
+
log,
|
|
83
|
+
$autoload,
|
|
84
|
+
API,
|
|
85
|
+
buildInitializer
|
|
86
|
+
} = await $.run(['NODE_ENV', '?BUILD_PARALLELISM', '?BUILD_OPTIONS', 'PROJECT_DIR', 'process', 'compiler', 'log', '$autoload', 'API', 'buildInitializer']);
|
|
87
|
+
log('info', 'Environment initialized 🚀🌕');
|
|
88
|
+
const operations = (await (0, _httpRouter.dereferenceOpenAPIOperations)(API, (0, _httpRouter.getOpenAPIOperations)(API))).filter(operation => {
|
|
89
|
+
if (handlerName) {
|
|
90
|
+
const sourceOperationId = operation['x-whook'] && operation['x-whook'].sourceOperationId;
|
|
91
|
+
return handlerName === operation.operationId || handlerName === sourceOperationId;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return true;
|
|
95
|
+
});
|
|
96
|
+
log('warning', `${operations.length} operations to process.`);
|
|
97
|
+
await processOperations({
|
|
98
|
+
NODE_ENV,
|
|
99
|
+
BUILD_OPTIONS: BUILD_OPTIONS || _whook.DEFAULT_BUILD_OPTIONS,
|
|
100
|
+
BUILD_PARALLELISM: BUILD_PARALLELISM || DEFAULT_BUILD_PARALLELISM,
|
|
101
|
+
PROJECT_DIR,
|
|
102
|
+
compiler,
|
|
103
|
+
log,
|
|
104
|
+
$autoload,
|
|
105
|
+
buildInitializer
|
|
106
|
+
}, operations);
|
|
107
|
+
await $.destroy();
|
|
108
|
+
} catch (err) {
|
|
109
|
+
// eslint-disable-next-line
|
|
110
|
+
console.error('💀 - Cannot launch the build:', err.stack, JSON.stringify(err.params, null, 2));
|
|
111
|
+
process.exit(1);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function processOperations({
|
|
116
|
+
NODE_ENV,
|
|
117
|
+
BUILD_PARALLELISM,
|
|
118
|
+
BUILD_OPTIONS,
|
|
119
|
+
PROJECT_DIR,
|
|
120
|
+
compiler,
|
|
121
|
+
log,
|
|
122
|
+
$autoload,
|
|
123
|
+
buildInitializer
|
|
124
|
+
}, operations) {
|
|
125
|
+
const operationsLeft = operations.slice(BUILD_PARALLELISM);
|
|
126
|
+
await Promise.all(operations.slice(0, BUILD_PARALLELISM).map(operation => buildAnyLambda({
|
|
127
|
+
NODE_ENV,
|
|
128
|
+
PROJECT_DIR,
|
|
129
|
+
BUILD_OPTIONS,
|
|
130
|
+
compiler,
|
|
131
|
+
log,
|
|
132
|
+
$autoload,
|
|
133
|
+
buildInitializer
|
|
134
|
+
}, operation)));
|
|
135
|
+
|
|
136
|
+
if (operationsLeft.length) {
|
|
137
|
+
log('info', operationsLeft.length, ' operations left.');
|
|
138
|
+
return processOperations({
|
|
139
|
+
NODE_ENV,
|
|
140
|
+
BUILD_PARALLELISM,
|
|
141
|
+
BUILD_OPTIONS,
|
|
142
|
+
PROJECT_DIR,
|
|
143
|
+
compiler,
|
|
144
|
+
log,
|
|
145
|
+
$autoload,
|
|
146
|
+
buildInitializer
|
|
147
|
+
}, operationsLeft);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
log('info', 'No more operations.');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function buildAnyLambda({
|
|
154
|
+
NODE_ENV,
|
|
155
|
+
PROJECT_DIR,
|
|
156
|
+
BUILD_OPTIONS,
|
|
157
|
+
compiler,
|
|
158
|
+
log,
|
|
159
|
+
$autoload,
|
|
160
|
+
buildInitializer
|
|
161
|
+
}, operation) {
|
|
162
|
+
const {
|
|
163
|
+
operationId
|
|
164
|
+
} = operation;
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
const whookConfig = operation['x-whook'] || {};
|
|
168
|
+
const operationType = whookConfig.type || 'http';
|
|
169
|
+
const sourceOperationId = whookConfig.sourceOperationId;
|
|
170
|
+
const entryPoint = operationId;
|
|
171
|
+
const finalEntryPoint = (sourceOperationId ? sourceOperationId : operationId) + ((operation['x-whook'] || {}).suffix || '');
|
|
172
|
+
log('warning', `Building ${operationType} "${finalEntryPoint}"...`);
|
|
173
|
+
const buildDefinition = BUILD_DEFINITIONS[operationType]; // eslint-disable-next-line
|
|
174
|
+
|
|
175
|
+
const applyWrapper = require(buildDefinition.wrapper.path).default;
|
|
176
|
+
|
|
177
|
+
const rootNode = await $autoload(entryPoint + (buildDefinition.suffix || ''));
|
|
178
|
+
|
|
179
|
+
const lambdaPath = _path.default.join(PROJECT_DIR, 'builds', NODE_ENV, finalEntryPoint);
|
|
180
|
+
|
|
181
|
+
const finalHandlerInitializer = applyWrapper(rootNode.initializer);
|
|
182
|
+
const initializerContent = await buildInitializer(finalHandlerInitializer[_knifecycle.SPECIAL_PROPS.INJECT].map(name => name === 'OPERATION_API' ? `OPERATION_API>OPERATION_API_${finalEntryPoint}` : name), BUILD_OPTIONS);
|
|
183
|
+
const indexContent = await buildLambdaIndex(rootNode, {
|
|
184
|
+
name: buildDefinition.wrapper.name,
|
|
185
|
+
path: buildDefinition.wrapper.path
|
|
186
|
+
}, BUILD_OPTIONS);
|
|
187
|
+
await (0, _mkdirp.default)(lambdaPath);
|
|
188
|
+
await Promise.all([copyStaticFiles({
|
|
189
|
+
PROJECT_DIR,
|
|
190
|
+
log
|
|
191
|
+
}, lambdaPath, whookConfig.staticFiles || []), ensureFileAsync({
|
|
192
|
+
log
|
|
193
|
+
}, _path.default.join(lambdaPath, 'initialize.js'), initializerContent), ensureFileAsync({
|
|
194
|
+
log
|
|
195
|
+
}, _path.default.join(lambdaPath, 'main.js'), indexContent)]);
|
|
196
|
+
await buildFinalLambda({
|
|
197
|
+
compiler,
|
|
198
|
+
log
|
|
199
|
+
}, lambdaPath, whookConfig);
|
|
200
|
+
} catch (err) {
|
|
201
|
+
log('error', `Error building "${operationId}"...`);
|
|
202
|
+
log('stack', err.stack);
|
|
203
|
+
log('debug', JSON.stringify(err.params, null, 2));
|
|
204
|
+
throw _yerror.default.wrap(err, 'E_LAMBDA_BUILD', operationId);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function buildLambdaIndex(rootNode, buildWrapper, options) {
|
|
209
|
+
return `${options.modules === 'commonjs' ? `const pickModule = (m) => { return m && m.default || m; }
|
|
210
|
+
const initHandler = pickModule(require('${rootNode.path}'));
|
|
211
|
+
const ${buildWrapper.name} = pickModule(require('${buildWrapper.path}'));
|
|
212
|
+
const { initialize } = require('./initialize');` : `import initHandler from '${rootNode.path}';
|
|
213
|
+
import ${buildWrapper.name} from '${buildWrapper.path}';
|
|
214
|
+
import { initialize } from './initialize';`}
|
|
215
|
+
|
|
216
|
+
const handlerInitializer = ${buildWrapper.name}(
|
|
217
|
+
initHandler
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
const handlerPromise = initialize()
|
|
221
|
+
.then(handlerInitializer);
|
|
222
|
+
|
|
223
|
+
${options.modules === 'commonjs' ? 'module.exports = {}; module.exports.default = ' : 'export default '}function handler (req, res) {
|
|
224
|
+
return handlerPromise
|
|
225
|
+
.then(handler => handler(req, res));
|
|
226
|
+
};
|
|
227
|
+
`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function buildFinalLambda({
|
|
231
|
+
compiler,
|
|
232
|
+
log
|
|
233
|
+
}, lambdaPath, whookConfig) {
|
|
234
|
+
const entryPoint = `${lambdaPath}/main.js`;
|
|
235
|
+
const {
|
|
236
|
+
contents,
|
|
237
|
+
mappings
|
|
238
|
+
} = await compiler(entryPoint, whookConfig.compilerOptions);
|
|
239
|
+
await Promise.all([ensureFileAsync({
|
|
240
|
+
log
|
|
241
|
+
}, `${lambdaPath}/index.js`, contents, 'utf-8'), mappings ? ensureFileAsync({
|
|
242
|
+
log
|
|
243
|
+
}, `${lambdaPath}/index.js.map`, mappings, 'utf-8') : Promise.resolve()]);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function copyStaticFiles({
|
|
247
|
+
PROJECT_DIR,
|
|
248
|
+
log
|
|
249
|
+
}, lambdaPath, staticFiles = []) {
|
|
250
|
+
await Promise.all(staticFiles.map(async staticFile => await copyFiles({
|
|
251
|
+
log
|
|
252
|
+
}, _path.default.join(PROJECT_DIR, 'node_modules', staticFile), _path.default.join(lambdaPath, 'node_modules', staticFile))));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function copyFiles({
|
|
256
|
+
log
|
|
257
|
+
}, source, destination) {
|
|
258
|
+
let theError;
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
await (0, _mkdirp.default)(destination);
|
|
262
|
+
const data = await readFileAsync(source, 'utf-8');
|
|
263
|
+
await ensureFileAsync({
|
|
264
|
+
log
|
|
265
|
+
}, destination, data, 'utf-8');
|
|
266
|
+
} catch (err) {
|
|
267
|
+
theError = err;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (theError) {
|
|
271
|
+
if ('EISDIR' !== theError.code) {
|
|
272
|
+
throw theError;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
await cprAsync(source, destination, {
|
|
276
|
+
overwrite: true
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function ensureFileAsync({
|
|
282
|
+
log
|
|
283
|
+
}, path, content, encoding = 'utf-8') {
|
|
284
|
+
try {
|
|
285
|
+
const oldContent = await readFileAsync(path, encoding);
|
|
286
|
+
|
|
287
|
+
if (oldContent === content) {
|
|
288
|
+
log('debug', `Ignore unchanged file: "${path}".`);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
} catch (err) {
|
|
292
|
+
log('debug', `Write new file: "${path}".`);
|
|
293
|
+
return await writeFileAsync(path, content, encoding);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
log('debug', `Write changed file: "${path}".`);
|
|
297
|
+
return await writeFileAsync(path, content, encoding);
|
|
298
|
+
}
|
|
299
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["DEFAULT_BUILD_PARALLELISM","readFileAsync","util","promisify","fs","readFile","writeFileAsync","writeFile","cprAsync","cpr","BUILD_DEFINITIONS","http","type","wrapper","name","path","join","__dirname","suffix","prepareBuildEnvironment","$","Knifecycle","register","ENV","log","time","delay","initInitializerBuilder","initBuildAutoloader","initCompiler","runBuild","aPrepareBuildEnvironment","handlerName","process","argv","NODE_ENV","BUILD_PARALLELISM","BUILD_OPTIONS","PROJECT_DIR","compiler","$autoload","API","buildInitializer","run","operations","filter","operation","sourceOperationId","operationId","length","processOperations","DEFAULT_BUILD_OPTIONS","destroy","err","console","error","stack","JSON","stringify","params","exit","operationsLeft","slice","Promise","all","map","buildAnyLambda","whookConfig","operationType","entryPoint","finalEntryPoint","buildDefinition","applyWrapper","require","default","rootNode","lambdaPath","finalHandlerInitializer","initializer","initializerContent","SPECIAL_PROPS","INJECT","indexContent","buildLambdaIndex","copyStaticFiles","staticFiles","ensureFileAsync","buildFinalLambda","YError","wrap","buildWrapper","options","modules","contents","mappings","compilerOptions","resolve","staticFile","copyFiles","source","destination","theError","data","code","overwrite","content","encoding","oldContent"],"mappings":";;;;;;;;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AAKA;;AACA;;AACA;;;;;;;;AAdA;AA6BO,MAAMA,yBAAyB,GAAG,EAAlC;;;AAcP,MAAMC,aAAa,GAAGC,cAAKC,SAAL,CAAeC,YAAGC,QAAlB,CA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AAK,IAA1C,CAAH;AACA;AACD;AACF,GAPD,CAOE,OAAOsC,GAAP,EAAY;AACZ7B,IAAAA,GAAG,CAAC,OAAD,EAAW,oBAAmBT,IAAK,IAAnC,CAAH;AACA,WAAO,MAAMT,cAAc,CAACS,IAAD,EAAO0F,OAAP,EAAgBC,QAAhB,CAA3B;AACD;;AACDlF,EAAAA,GAAG,CAAC,OAAD,EAAW,wBAAuBT,IAAK,IAAvC,CAAH;AACA,SAAO,MAAMT,cAAc,CAACS,IAAD,EAAO0F,OAAP,EAAgBC,QAAhB,CAA3B;AACD","sourcesContent":["/* eslint global-require:0 */\nimport fs from 'fs';\nimport util from 'util';\nimport path from 'path';\nimport mkdirp from 'mkdirp';\nimport cpr from 'cpr';\nimport YError from 'yerror';\nimport Knifecycle, {\n SPECIAL_PROPS,\n constant,\n initInitializerBuilder,\n} from 'knifecycle';\nimport { DEFAULT_BUILD_OPTIONS, initCompiler } from '@whook/whook';\nimport initBuildAutoloader from './services/_autoload';\nimport {\n dereferenceOpenAPIOperations,\n getOpenAPIOperations,\n} from '@whook/http-router';\nimport type { Autoloader, Dependencies, BuildInitializer } from 'knifecycle';\nimport type {\n WhookOperation,\n WhookCompilerOptions,\n WhookCompilerService,\n} from '@whook/whook';\nimport type { OpenAPIV3 } from 'openapi-types';\nimport type { LogService } from 'common-services';\nimport type { CprOptions } from 'cpr';\nimport type { BuildOptions } from 'knifecycle/dist/build';\n\nexport const DEFAULT_BUILD_PARALLELISM = 10;\n\nexport type WhookBuildConfig = {\n BUILD_OPTIONS?: BuildOptions;\n BUILD_PARALLELISM?: number;\n};\nexport type WhookAPIOperationGCPFunctionConfig = {\n type?: 'http';\n sourceOperationId?: string;\n staticFiles?: string[];\n compilerOptions?: WhookCompilerOptions;\n suffix?: string;\n};\n\nconst readFileAsync = util.promisify(fs.readFile) as (\n path: string,\n encoding: string,\n) => Promise<string>;\nconst writeFileAsync = util.promisify(fs.writeFile) as (\n path: string,\n content: string,\n encoding: string,\n) => Promise<void>;\nconst cprAsync = util.promisify(cpr) as (\n source: string,\n destination: string,\n options: CprOptions,\n) => Promise<string[]>;\n\nconst BUILD_DEFINITIONS: Record<\n WhookAPIOperationGCPFunctionConfig['type'],\n {\n type: string;\n wrapper: { name: string; path: string };\n suffix?: string;\n }\n> = {\n http: {\n type: 'HTTP',\n wrapper: {\n name: 'wrapHandlerForGoogleHTTPFunction',\n path: path.join(__dirname, 'wrappers', 'googleHTTPFunction'),\n },\n suffix: 'Wrapped',\n },\n};\n\nexport async function prepareBuildEnvironment<\n T extends Knifecycle<Dependencies>,\n>($: T = new Knifecycle() as T): Promise<T> {\n $.register(\n constant('INITIALIZER_PATH_MAP', {\n ENV: '@whook/whook/dist/services/ProxyedENV',\n log: __dirname + '/services/log',\n time: 'common-services/dist/time',\n delay: 'common-services/dist/delay',\n }),\n );\n $.register(initInitializerBuilder);\n $.register(initBuildAutoloader);\n $.register(initCompiler);\n $.register(constant('PORT', 1337));\n $.register(constant('HOST', 'localhost'));\n\n return $;\n}\n\nexport async function runBuild(\n aPrepareBuildEnvironment: typeof prepareBuildEnvironment,\n): Promise<void> {\n try {\n const handlerName = process.argv[2];\n const $ = await aPrepareBuildEnvironment();\n const {\n NODE_ENV,\n BUILD_PARALLELISM,\n BUILD_OPTIONS,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n API,\n buildInitializer,\n }: WhookBuildConfig & {\n NODE_ENV: string;\n PROJECT_DIR: string;\n compiler: WhookCompilerService;\n log: LogService;\n $autoload: Autoloader;\n API: OpenAPIV3.Document;\n buildInitializer: BuildInitializer;\n } = await $.run([\n 'NODE_ENV',\n '?BUILD_PARALLELISM',\n '?BUILD_OPTIONS',\n 'PROJECT_DIR',\n 'process',\n 'compiler',\n 'log',\n '$autoload',\n 'API',\n 'buildInitializer',\n ]);\n\n log('info', 'Environment initialized 🚀🌕');\n\n const operations = (\n await dereferenceOpenAPIOperations(\n API,\n getOpenAPIOperations<WhookAPIOperationGCPFunctionConfig>(API),\n )\n ).filter((operation) => {\n if (handlerName) {\n const sourceOperationId =\n operation['x-whook'] && operation['x-whook'].sourceOperationId;\n\n return (\n handlerName === operation.operationId ||\n handlerName === sourceOperationId\n );\n }\n return true;\n });\n\n log('warning', `${operations.length} operations to process.`);\n await processOperations(\n {\n NODE_ENV,\n BUILD_OPTIONS: BUILD_OPTIONS || DEFAULT_BUILD_OPTIONS,\n BUILD_PARALLELISM: BUILD_PARALLELISM || DEFAULT_BUILD_PARALLELISM,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n buildInitializer,\n },\n operations,\n );\n await $.destroy();\n } catch (err) {\n // eslint-disable-next-line\n console.error(\n '💀 - Cannot launch the build:',\n err.stack,\n JSON.stringify(err.params, null, 2),\n );\n process.exit(1);\n }\n}\n\nasync function processOperations(\n {\n NODE_ENV,\n BUILD_PARALLELISM,\n BUILD_OPTIONS,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n buildInitializer,\n }: {\n NODE_ENV: string;\n BUILD_PARALLELISM: number;\n BUILD_OPTIONS: BuildOptions;\n PROJECT_DIR: string;\n compiler: WhookCompilerService;\n log: LogService;\n $autoload: Autoloader;\n buildInitializer: BuildInitializer;\n },\n operations: WhookOperation<WhookAPIOperationGCPFunctionConfig>[],\n): Promise<void> {\n const operationsLeft = operations.slice(BUILD_PARALLELISM);\n\n await Promise.all(\n operations.slice(0, BUILD_PARALLELISM).map((operation) =>\n buildAnyLambda(\n {\n NODE_ENV,\n PROJECT_DIR,\n BUILD_OPTIONS,\n compiler,\n log,\n $autoload,\n buildInitializer,\n },\n operation,\n ),\n ),\n );\n\n if (operationsLeft.length) {\n log('info', operationsLeft.length, ' operations left.');\n return processOperations(\n {\n NODE_ENV,\n BUILD_PARALLELISM,\n BUILD_OPTIONS,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n buildInitializer,\n },\n operationsLeft,\n );\n }\n log('info', 'No more operations.');\n}\n\nasync function buildAnyLambda(\n {\n NODE_ENV,\n PROJECT_DIR,\n BUILD_OPTIONS,\n compiler,\n log,\n $autoload,\n buildInitializer,\n }: {\n NODE_ENV: string;\n PROJECT_DIR: string;\n BUILD_OPTIONS: BuildOptions;\n compiler: WhookCompilerService;\n log: LogService;\n $autoload: Autoloader;\n buildInitializer: BuildInitializer;\n },\n operation: WhookOperation<WhookAPIOperationGCPFunctionConfig>,\n): Promise<void> {\n const { operationId } = operation;\n\n try {\n const whookConfig: WhookAPIOperationGCPFunctionConfig =\n operation['x-whook'] || {};\n const operationType = whookConfig.type || 'http';\n const sourceOperationId = whookConfig.sourceOperationId;\n const entryPoint = operationId;\n const finalEntryPoint =\n (sourceOperationId ? sourceOperationId : operationId) +\n ((operation['x-whook'] || {}).suffix || '');\n log('warning', `Building ${operationType} \"${finalEntryPoint}\"...`);\n const buildDefinition = BUILD_DEFINITIONS[operationType];\n // eslint-disable-next-line\n const applyWrapper = require(buildDefinition.wrapper.path).default;\n const rootNode = await $autoload(\n entryPoint + (buildDefinition.suffix || ''),\n );\n const lambdaPath = path.join(\n PROJECT_DIR,\n 'builds',\n NODE_ENV,\n finalEntryPoint,\n );\n const finalHandlerInitializer = applyWrapper(rootNode.initializer);\n\n const initializerContent = await buildInitializer(\n finalHandlerInitializer[SPECIAL_PROPS.INJECT].map((name) =>\n name === 'OPERATION_API'\n ? `OPERATION_API>OPERATION_API_${finalEntryPoint}`\n : name,\n ),\n BUILD_OPTIONS,\n );\n const indexContent = await buildLambdaIndex(\n rootNode,\n {\n name: buildDefinition.wrapper.name,\n path: buildDefinition.wrapper.path,\n },\n BUILD_OPTIONS,\n );\n\n await mkdirp(lambdaPath);\n await Promise.all([\n copyStaticFiles(\n { PROJECT_DIR, log },\n lambdaPath,\n whookConfig.staticFiles || [],\n ),\n ensureFileAsync(\n { log },\n path.join(lambdaPath, 'initialize.js'),\n initializerContent,\n ),\n ensureFileAsync({ log }, path.join(lambdaPath, 'main.js'), indexContent),\n ]);\n await buildFinalLambda({ compiler, log }, lambdaPath, whookConfig);\n } catch (err) {\n log('error', `Error building \"${operationId}\"...`);\n log('stack', err.stack);\n log('debug', JSON.stringify(err.params, null, 2));\n throw YError.wrap(err, 'E_LAMBDA_BUILD', operationId);\n }\n}\n\nasync function buildLambdaIndex(\n rootNode: { path: string },\n buildWrapper: { name: string; path: string },\n options: BuildOptions,\n): Promise<string> {\n return `${\n options.modules === 'commonjs'\n ? `const pickModule = (m) => { return m && m.default || m; }\nconst initHandler = pickModule(require('${rootNode.path}'));\nconst ${buildWrapper.name} = pickModule(require('${buildWrapper.path}'));\nconst { initialize } = require('./initialize');`\n : `import initHandler from '${rootNode.path}';\nimport ${buildWrapper.name} from '${buildWrapper.path}';\nimport { initialize } from './initialize';`\n }\n\nconst handlerInitializer = ${buildWrapper.name}(\n initHandler\n);\n\nconst handlerPromise = initialize()\n .then(handlerInitializer);\n\n${\n options.modules === 'commonjs'\n ? 'module.exports = {}; module.exports.default = '\n : 'export default '\n}function handler (req, res) {\n return handlerPromise\n .then(handler => handler(req, res));\n};\n`;\n}\n\nasync function buildFinalLambda(\n { compiler, log }: { compiler: WhookCompilerService; log: LogService },\n lambdaPath: string,\n whookConfig: WhookAPIOperationGCPFunctionConfig,\n): Promise<void> {\n const entryPoint = `${lambdaPath}/main.js`;\n const { contents, mappings } = await compiler(\n entryPoint,\n whookConfig.compilerOptions,\n );\n\n await Promise.all([\n ensureFileAsync({ log }, `${lambdaPath}/index.js`, contents, 'utf-8'),\n mappings\n ? ensureFileAsync(\n { log },\n `${lambdaPath}/index.js.map`,\n mappings,\n 'utf-8',\n )\n : Promise.resolve(),\n ]);\n}\n\nasync function copyStaticFiles(\n { PROJECT_DIR, log }: { PROJECT_DIR: string; log: LogService },\n lambdaPath: string,\n staticFiles: string[] = [],\n): Promise<void> {\n await Promise.all(\n staticFiles.map(\n async (staticFile) =>\n await copyFiles(\n { log },\n path.join(PROJECT_DIR, 'node_modules', staticFile),\n path.join(lambdaPath, 'node_modules', staticFile),\n ),\n ),\n );\n}\n\nasync function copyFiles(\n { log }: { log: LogService },\n source: string,\n destination: string,\n): Promise<void> {\n let theError;\n try {\n await mkdirp(destination);\n const data = await readFileAsync(source, 'utf-8');\n await ensureFileAsync({ log }, destination, data, 'utf-8');\n } catch (err) {\n theError = err;\n }\n if (theError) {\n if ('EISDIR' !== theError.code) {\n throw theError;\n }\n await cprAsync(source, destination, {\n overwrite: true,\n });\n }\n}\n\nasync function ensureFileAsync(\n { log }: { log: LogService },\n path: string,\n content: string,\n encoding = 'utf-8',\n): Promise<void> {\n try {\n const oldContent = await readFileAsync(path, encoding);\n\n if (oldContent === content) {\n log('debug', `Ignore unchanged file: \"${path}\".`);\n return;\n }\n } catch (err) {\n log('debug', `Write new file: \"${path}\".`);\n return await writeFileAsync(path, content, encoding);\n }\n log('debug', `Write changed file: \"${path}\".`);\n return await writeFileAsync(path, content, encoding);\n}\n"],"file":"index.js"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/* eslint global-require:0 */
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import util from 'util';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import mkdirp from 'mkdirp';
|
|
6
|
+
import cpr from 'cpr';
|
|
7
|
+
import YError from 'yerror';
|
|
8
|
+
import Knifecycle, { SPECIAL_PROPS, constant, initInitializerBuilder } from 'knifecycle';
|
|
9
|
+
import { DEFAULT_BUILD_OPTIONS, initCompiler } from '@whook/whook';
|
|
10
|
+
import initBuildAutoloader from './services/_autoload';
|
|
11
|
+
import { dereferenceOpenAPIOperations, getOpenAPIOperations } from '@whook/http-router';
|
|
12
|
+
export const DEFAULT_BUILD_PARALLELISM = 10;
|
|
13
|
+
const readFileAsync = util.promisify(fs.readFile);
|
|
14
|
+
const writeFileAsync = util.promisify(fs.writeFile);
|
|
15
|
+
const cprAsync = util.promisify(cpr);
|
|
16
|
+
const BUILD_DEFINITIONS = {
|
|
17
|
+
http: {
|
|
18
|
+
type: 'HTTP',
|
|
19
|
+
wrapper: {
|
|
20
|
+
name: 'wrapHandlerForGoogleHTTPFunction',
|
|
21
|
+
path: path.join(__dirname, 'wrappers', 'googleHTTPFunction')
|
|
22
|
+
},
|
|
23
|
+
suffix: 'Wrapped'
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export async function prepareBuildEnvironment($ = new Knifecycle()) {
|
|
27
|
+
$.register(constant('INITIALIZER_PATH_MAP', {
|
|
28
|
+
ENV: '@whook/whook/dist/services/ProxyedENV',
|
|
29
|
+
log: __dirname + '/services/log',
|
|
30
|
+
time: 'common-services/dist/time',
|
|
31
|
+
delay: 'common-services/dist/delay'
|
|
32
|
+
}));
|
|
33
|
+
$.register(initInitializerBuilder);
|
|
34
|
+
$.register(initBuildAutoloader);
|
|
35
|
+
$.register(initCompiler);
|
|
36
|
+
$.register(constant('PORT', 1337));
|
|
37
|
+
$.register(constant('HOST', 'localhost'));
|
|
38
|
+
return $;
|
|
39
|
+
}
|
|
40
|
+
export async function runBuild(aPrepareBuildEnvironment) {
|
|
41
|
+
try {
|
|
42
|
+
const handlerName = process.argv[2];
|
|
43
|
+
const $ = await aPrepareBuildEnvironment();
|
|
44
|
+
const {
|
|
45
|
+
NODE_ENV,
|
|
46
|
+
BUILD_PARALLELISM,
|
|
47
|
+
BUILD_OPTIONS,
|
|
48
|
+
PROJECT_DIR,
|
|
49
|
+
compiler,
|
|
50
|
+
log,
|
|
51
|
+
$autoload,
|
|
52
|
+
API,
|
|
53
|
+
buildInitializer
|
|
54
|
+
} = await $.run(['NODE_ENV', '?BUILD_PARALLELISM', '?BUILD_OPTIONS', 'PROJECT_DIR', 'process', 'compiler', 'log', '$autoload', 'API', 'buildInitializer']);
|
|
55
|
+
log('info', 'Environment initialized 🚀🌕');
|
|
56
|
+
const operations = (await dereferenceOpenAPIOperations(API, getOpenAPIOperations(API))).filter(operation => {
|
|
57
|
+
if (handlerName) {
|
|
58
|
+
const sourceOperationId = operation['x-whook'] && operation['x-whook'].sourceOperationId;
|
|
59
|
+
return handlerName === operation.operationId || handlerName === sourceOperationId;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return true;
|
|
63
|
+
});
|
|
64
|
+
log('warning', `${operations.length} operations to process.`);
|
|
65
|
+
await processOperations({
|
|
66
|
+
NODE_ENV,
|
|
67
|
+
BUILD_OPTIONS: BUILD_OPTIONS || DEFAULT_BUILD_OPTIONS,
|
|
68
|
+
BUILD_PARALLELISM: BUILD_PARALLELISM || DEFAULT_BUILD_PARALLELISM,
|
|
69
|
+
PROJECT_DIR,
|
|
70
|
+
compiler,
|
|
71
|
+
log,
|
|
72
|
+
$autoload,
|
|
73
|
+
buildInitializer
|
|
74
|
+
}, operations);
|
|
75
|
+
await $.destroy();
|
|
76
|
+
} catch (err) {
|
|
77
|
+
// eslint-disable-next-line
|
|
78
|
+
console.error('💀 - Cannot launch the build:', err.stack, JSON.stringify(err.params, null, 2));
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function processOperations({
|
|
84
|
+
NODE_ENV,
|
|
85
|
+
BUILD_PARALLELISM,
|
|
86
|
+
BUILD_OPTIONS,
|
|
87
|
+
PROJECT_DIR,
|
|
88
|
+
compiler,
|
|
89
|
+
log,
|
|
90
|
+
$autoload,
|
|
91
|
+
buildInitializer
|
|
92
|
+
}, operations) {
|
|
93
|
+
const operationsLeft = operations.slice(BUILD_PARALLELISM);
|
|
94
|
+
await Promise.all(operations.slice(0, BUILD_PARALLELISM).map(operation => buildAnyLambda({
|
|
95
|
+
NODE_ENV,
|
|
96
|
+
PROJECT_DIR,
|
|
97
|
+
BUILD_OPTIONS,
|
|
98
|
+
compiler,
|
|
99
|
+
log,
|
|
100
|
+
$autoload,
|
|
101
|
+
buildInitializer
|
|
102
|
+
}, operation)));
|
|
103
|
+
|
|
104
|
+
if (operationsLeft.length) {
|
|
105
|
+
log('info', operationsLeft.length, ' operations left.');
|
|
106
|
+
return processOperations({
|
|
107
|
+
NODE_ENV,
|
|
108
|
+
BUILD_PARALLELISM,
|
|
109
|
+
BUILD_OPTIONS,
|
|
110
|
+
PROJECT_DIR,
|
|
111
|
+
compiler,
|
|
112
|
+
log,
|
|
113
|
+
$autoload,
|
|
114
|
+
buildInitializer
|
|
115
|
+
}, operationsLeft);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
log('info', 'No more operations.');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function buildAnyLambda({
|
|
122
|
+
NODE_ENV,
|
|
123
|
+
PROJECT_DIR,
|
|
124
|
+
BUILD_OPTIONS,
|
|
125
|
+
compiler,
|
|
126
|
+
log,
|
|
127
|
+
$autoload,
|
|
128
|
+
buildInitializer
|
|
129
|
+
}, operation) {
|
|
130
|
+
const {
|
|
131
|
+
operationId
|
|
132
|
+
} = operation;
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
const whookConfig = operation['x-whook'] || {};
|
|
136
|
+
const operationType = whookConfig.type || 'http';
|
|
137
|
+
const sourceOperationId = whookConfig.sourceOperationId;
|
|
138
|
+
const entryPoint = operationId;
|
|
139
|
+
const finalEntryPoint = (sourceOperationId ? sourceOperationId : operationId) + ((operation['x-whook'] || {}).suffix || '');
|
|
140
|
+
log('warning', `Building ${operationType} "${finalEntryPoint}"...`);
|
|
141
|
+
const buildDefinition = BUILD_DEFINITIONS[operationType]; // eslint-disable-next-line
|
|
142
|
+
|
|
143
|
+
const applyWrapper = require(buildDefinition.wrapper.path).default;
|
|
144
|
+
|
|
145
|
+
const rootNode = await $autoload(entryPoint + (buildDefinition.suffix || ''));
|
|
146
|
+
const lambdaPath = path.join(PROJECT_DIR, 'builds', NODE_ENV, finalEntryPoint);
|
|
147
|
+
const finalHandlerInitializer = applyWrapper(rootNode.initializer);
|
|
148
|
+
const initializerContent = await buildInitializer(finalHandlerInitializer[SPECIAL_PROPS.INJECT].map(name => name === 'OPERATION_API' ? `OPERATION_API>OPERATION_API_${finalEntryPoint}` : name), BUILD_OPTIONS);
|
|
149
|
+
const indexContent = await buildLambdaIndex(rootNode, {
|
|
150
|
+
name: buildDefinition.wrapper.name,
|
|
151
|
+
path: buildDefinition.wrapper.path
|
|
152
|
+
}, BUILD_OPTIONS);
|
|
153
|
+
await mkdirp(lambdaPath);
|
|
154
|
+
await Promise.all([copyStaticFiles({
|
|
155
|
+
PROJECT_DIR,
|
|
156
|
+
log
|
|
157
|
+
}, lambdaPath, whookConfig.staticFiles || []), ensureFileAsync({
|
|
158
|
+
log
|
|
159
|
+
}, path.join(lambdaPath, 'initialize.js'), initializerContent), ensureFileAsync({
|
|
160
|
+
log
|
|
161
|
+
}, path.join(lambdaPath, 'main.js'), indexContent)]);
|
|
162
|
+
await buildFinalLambda({
|
|
163
|
+
compiler,
|
|
164
|
+
log
|
|
165
|
+
}, lambdaPath, whookConfig);
|
|
166
|
+
} catch (err) {
|
|
167
|
+
log('error', `Error building "${operationId}"...`);
|
|
168
|
+
log('stack', err.stack);
|
|
169
|
+
log('debug', JSON.stringify(err.params, null, 2));
|
|
170
|
+
throw YError.wrap(err, 'E_LAMBDA_BUILD', operationId);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function buildLambdaIndex(rootNode, buildWrapper, options) {
|
|
175
|
+
return `${options.modules === 'commonjs' ? `const pickModule = (m) => { return m && m.default || m; }
|
|
176
|
+
const initHandler = pickModule(require('${rootNode.path}'));
|
|
177
|
+
const ${buildWrapper.name} = pickModule(require('${buildWrapper.path}'));
|
|
178
|
+
const { initialize } = require('./initialize');` : `import initHandler from '${rootNode.path}';
|
|
179
|
+
import ${buildWrapper.name} from '${buildWrapper.path}';
|
|
180
|
+
import { initialize } from './initialize';`}
|
|
181
|
+
|
|
182
|
+
const handlerInitializer = ${buildWrapper.name}(
|
|
183
|
+
initHandler
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const handlerPromise = initialize()
|
|
187
|
+
.then(handlerInitializer);
|
|
188
|
+
|
|
189
|
+
${options.modules === 'commonjs' ? 'module.exports = {}; module.exports.default = ' : 'export default '}function handler (req, res) {
|
|
190
|
+
return handlerPromise
|
|
191
|
+
.then(handler => handler(req, res));
|
|
192
|
+
};
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function buildFinalLambda({
|
|
197
|
+
compiler,
|
|
198
|
+
log
|
|
199
|
+
}, lambdaPath, whookConfig) {
|
|
200
|
+
const entryPoint = `${lambdaPath}/main.js`;
|
|
201
|
+
const {
|
|
202
|
+
contents,
|
|
203
|
+
mappings
|
|
204
|
+
} = await compiler(entryPoint, whookConfig.compilerOptions);
|
|
205
|
+
await Promise.all([ensureFileAsync({
|
|
206
|
+
log
|
|
207
|
+
}, `${lambdaPath}/index.js`, contents, 'utf-8'), mappings ? ensureFileAsync({
|
|
208
|
+
log
|
|
209
|
+
}, `${lambdaPath}/index.js.map`, mappings, 'utf-8') : Promise.resolve()]);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function copyStaticFiles({
|
|
213
|
+
PROJECT_DIR,
|
|
214
|
+
log
|
|
215
|
+
}, lambdaPath, staticFiles = []) {
|
|
216
|
+
await Promise.all(staticFiles.map(async staticFile => await copyFiles({
|
|
217
|
+
log
|
|
218
|
+
}, path.join(PROJECT_DIR, 'node_modules', staticFile), path.join(lambdaPath, 'node_modules', staticFile))));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function copyFiles({
|
|
222
|
+
log
|
|
223
|
+
}, source, destination) {
|
|
224
|
+
let theError;
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
await mkdirp(destination);
|
|
228
|
+
const data = await readFileAsync(source, 'utf-8');
|
|
229
|
+
await ensureFileAsync({
|
|
230
|
+
log
|
|
231
|
+
}, destination, data, 'utf-8');
|
|
232
|
+
} catch (err) {
|
|
233
|
+
theError = err;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (theError) {
|
|
237
|
+
if ('EISDIR' !== theError.code) {
|
|
238
|
+
throw theError;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
await cprAsync(source, destination, {
|
|
242
|
+
overwrite: true
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function ensureFileAsync({
|
|
248
|
+
log
|
|
249
|
+
}, path, content, encoding = 'utf-8') {
|
|
250
|
+
try {
|
|
251
|
+
const oldContent = await readFileAsync(path, encoding);
|
|
252
|
+
|
|
253
|
+
if (oldContent === content) {
|
|
254
|
+
log('debug', `Ignore unchanged file: "${path}".`);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
} catch (err) {
|
|
258
|
+
log('debug', `Write new file: "${path}".`);
|
|
259
|
+
return await writeFileAsync(path, content, encoding);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
log('debug', `Write changed file: "${path}".`);
|
|
263
|
+
return await writeFileAsync(path, content, encoding);
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["fs","util","path","mkdirp","cpr","YError","Knifecycle","SPECIAL_PROPS","constant","initInitializerBuilder","DEFAULT_BUILD_OPTIONS","initCompiler","initBuildAutoloader","dereferenceOpenAPIOperations","getOpenAPIOperations","DEFAULT_BUILD_PARALLELISM","readFileAsync","promisify","readFile","writeFileAsync","writeFile","cprAsync","BUILD_DEFINITIONS","http","type","wrapper","name","join","__dirname","suffix","prepareBuildEnvironment","$","register","ENV","log","time","delay","runBuild","aPrepareBuildEnvironment","handlerName","process","argv","NODE_ENV","BUILD_PARALLELISM","BUILD_OPTIONS","PROJECT_DIR","compiler","$autoload","API","buildInitializer","run","operations","filter","operation","sourceOperationId","operationId","length","processOperations","destroy","err","console","error","stack","JSON","stringify","params","exit","operationsLeft","slice","Promise","all","map","buildAnyLambda","whookConfig","operationType","entryPoint","finalEntryPoint","buildDefinition","applyWrapper","require","default","rootNode","lambdaPath","finalHandlerInitializer","initializer","initializerContent","INJECT","indexContent","buildLambdaIndex","copyStaticFiles","staticFiles","ensureFileAsync","buildFinalLambda","wrap","buildWrapper","options","modules","contents","mappings","compilerOptions","resolve","staticFile","copyFiles","source","destination","theError","data","code","overwrite","content","encoding","oldContent"],"mappings":"AAAA;AACA,OAAOA,EAAP,MAAe,IAAf;AACA,OAAOC,IAAP,MAAiB,MAAjB;AACA,OAAOC,IAAP,MAAiB,MAAjB;AACA,OAAOC,MAAP,MAAmB,QAAnB;AACA,OAAOC,GAAP,MAAgB,KAAhB;AACA,OAAOC,MAAP,MAAmB,QAAnB;AACA,OAAOC,UAAP,IACEC,aADF,EAEEC,QAFF,EAGEC,sBAHF,QAIO,YAJP;AAKA,SAASC,qBAAT,EAAgCC,YAAhC,QAAoD,cAApD;AACA,OAAOC,mBAAP,MAAgC,sBAAhC;AACA,SACEC,4BADF,EAEEC,oBAFF,QAGO,oBAHP;AAeA,OAAO,MAAMC,yBAAyB,GAAG,EAAlC;AAcP,MAAMC,aAAa,GAAGf,IAAI,CAACgB,SAAL,CAAejB,EAAE,CAACkB,QAAlB,CAAtB;AAIA,MAAMC,cAAc,GAAGlB,IAAI,CAACgB,SAAL,CAAejB,EAAE,CAACoB,SAAlB,CAAvB;AAKA,MAAMC,QAAQ,GAAGpB,IAAI,CAACgB,SAAL,CAAeb,GAAf,CAAjB;AAMA,MAAMkB,iBAOL,GAAG;AACFC,EAAAA,IAAI,EAAE;AACJC,IAAAA,IAAI,EAAE,MADF;AAEJC,IAAAA,OAAO,EAAE;AACPC,MAAAA,IAAI,EAAE,kCADC;AAEPxB,MAAAA,IAAI,EAAEA,IAAI,CAACyB,IAAL,CAAUC,SAAV,EAAqB,UAArB,EAAiC,oBAAjC;AAFC,KAFL;AAMJC,IAAAA,MAAM,EAAE;AANJ;AADJ,CAPJ;AAkBA,OAAO,eAAeC,uBAAf,CAELC,CAAI,GAAG,IAAIzB,UAAJ,EAFF,EAEqC;AAC1CyB,EAAAA,CAAC,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*/\nimport fs from 'fs';\nimport util from 'util';\nimport path from 'path';\nimport mkdirp from 'mkdirp';\nimport cpr from 'cpr';\nimport YError from 'yerror';\nimport Knifecycle, {\n SPECIAL_PROPS,\n constant,\n initInitializerBuilder,\n} from 'knifecycle';\nimport { DEFAULT_BUILD_OPTIONS, initCompiler } from '@whook/whook';\nimport initBuildAutoloader from './services/_autoload';\nimport {\n dereferenceOpenAPIOperations,\n getOpenAPIOperations,\n} from '@whook/http-router';\nimport type { Autoloader, Dependencies, BuildInitializer } from 'knifecycle';\nimport type {\n WhookOperation,\n WhookCompilerOptions,\n WhookCompilerService,\n} from '@whook/whook';\nimport type { OpenAPIV3 } from 'openapi-types';\nimport type { LogService } from 'common-services';\nimport type { CprOptions } from 'cpr';\nimport type { BuildOptions } from 'knifecycle/dist/build';\n\nexport const DEFAULT_BUILD_PARALLELISM = 10;\n\nexport type WhookBuildConfig = {\n BUILD_OPTIONS?: BuildOptions;\n BUILD_PARALLELISM?: number;\n};\nexport type WhookAPIOperationGCPFunctionConfig = {\n type?: 'http';\n sourceOperationId?: string;\n staticFiles?: string[];\n compilerOptions?: WhookCompilerOptions;\n suffix?: string;\n};\n\nconst readFileAsync = util.promisify(fs.readFile) as (\n path: string,\n encoding: string,\n) => Promise<string>;\nconst writeFileAsync = util.promisify(fs.writeFile) as (\n path: string,\n content: string,\n encoding: string,\n) => Promise<void>;\nconst cprAsync = util.promisify(cpr) as (\n source: string,\n destination: string,\n options: CprOptions,\n) => Promise<string[]>;\n\nconst BUILD_DEFINITIONS: Record<\n WhookAPIOperationGCPFunctionConfig['type'],\n {\n type: string;\n wrapper: { name: string; path: string };\n suffix?: string;\n }\n> = {\n http: {\n type: 'HTTP',\n wrapper: {\n name: 'wrapHandlerForGoogleHTTPFunction',\n path: path.join(__dirname, 'wrappers', 'googleHTTPFunction'),\n },\n suffix: 'Wrapped',\n },\n};\n\nexport async function prepareBuildEnvironment<\n T extends Knifecycle<Dependencies>,\n>($: T = new Knifecycle() as T): Promise<T> {\n $.register(\n constant('INITIALIZER_PATH_MAP', {\n ENV: '@whook/whook/dist/services/ProxyedENV',\n log: __dirname + '/services/log',\n time: 'common-services/dist/time',\n delay: 'common-services/dist/delay',\n }),\n );\n $.register(initInitializerBuilder);\n $.register(initBuildAutoloader);\n $.register(initCompiler);\n $.register(constant('PORT', 1337));\n $.register(constant('HOST', 'localhost'));\n\n return $;\n}\n\nexport async function runBuild(\n aPrepareBuildEnvironment: typeof prepareBuildEnvironment,\n): Promise<void> {\n try {\n const handlerName = process.argv[2];\n const $ = await aPrepareBuildEnvironment();\n const {\n NODE_ENV,\n BUILD_PARALLELISM,\n BUILD_OPTIONS,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n API,\n buildInitializer,\n }: WhookBuildConfig & {\n NODE_ENV: string;\n PROJECT_DIR: string;\n compiler: WhookCompilerService;\n log: LogService;\n $autoload: Autoloader;\n API: OpenAPIV3.Document;\n buildInitializer: BuildInitializer;\n } = await $.run([\n 'NODE_ENV',\n '?BUILD_PARALLELISM',\n '?BUILD_OPTIONS',\n 'PROJECT_DIR',\n 'process',\n 'compiler',\n 'log',\n '$autoload',\n 'API',\n 'buildInitializer',\n ]);\n\n log('info', 'Environment initialized 🚀🌕');\n\n const operations = (\n await dereferenceOpenAPIOperations(\n API,\n getOpenAPIOperations<WhookAPIOperationGCPFunctionConfig>(API),\n )\n ).filter((operation) => {\n if (handlerName) {\n const sourceOperationId =\n operation['x-whook'] && operation['x-whook'].sourceOperationId;\n\n return (\n handlerName === operation.operationId ||\n handlerName === sourceOperationId\n );\n }\n return true;\n });\n\n log('warning', `${operations.length} operations to process.`);\n await processOperations(\n {\n NODE_ENV,\n BUILD_OPTIONS: BUILD_OPTIONS || DEFAULT_BUILD_OPTIONS,\n BUILD_PARALLELISM: BUILD_PARALLELISM || DEFAULT_BUILD_PARALLELISM,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n buildInitializer,\n },\n operations,\n );\n await $.destroy();\n } catch (err) {\n // eslint-disable-next-line\n console.error(\n '💀 - Cannot launch the build:',\n err.stack,\n JSON.stringify(err.params, null, 2),\n );\n process.exit(1);\n }\n}\n\nasync function processOperations(\n {\n NODE_ENV,\n BUILD_PARALLELISM,\n BUILD_OPTIONS,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n buildInitializer,\n }: {\n NODE_ENV: string;\n BUILD_PARALLELISM: number;\n BUILD_OPTIONS: BuildOptions;\n PROJECT_DIR: string;\n compiler: WhookCompilerService;\n log: LogService;\n $autoload: Autoloader;\n buildInitializer: BuildInitializer;\n },\n operations: WhookOperation<WhookAPIOperationGCPFunctionConfig>[],\n): Promise<void> {\n const operationsLeft = operations.slice(BUILD_PARALLELISM);\n\n await Promise.all(\n operations.slice(0, BUILD_PARALLELISM).map((operation) =>\n buildAnyLambda(\n {\n NODE_ENV,\n PROJECT_DIR,\n BUILD_OPTIONS,\n compiler,\n log,\n $autoload,\n buildInitializer,\n },\n operation,\n ),\n ),\n );\n\n if (operationsLeft.length) {\n log('info', operationsLeft.length, ' operations left.');\n return processOperations(\n {\n NODE_ENV,\n BUILD_PARALLELISM,\n BUILD_OPTIONS,\n PROJECT_DIR,\n compiler,\n log,\n $autoload,\n buildInitializer,\n },\n operationsLeft,\n );\n }\n log('info', 'No more operations.');\n}\n\nasync function buildAnyLambda(\n {\n NODE_ENV,\n PROJECT_DIR,\n BUILD_OPTIONS,\n compiler,\n log,\n $autoload,\n buildInitializer,\n }: {\n NODE_ENV: string;\n PROJECT_DIR: string;\n BUILD_OPTIONS: BuildOptions;\n compiler: WhookCompilerService;\n log: LogService;\n $autoload: Autoloader;\n buildInitializer: BuildInitializer;\n },\n operation: WhookOperation<WhookAPIOperationGCPFunctionConfig>,\n): Promise<void> {\n const { operationId } = operation;\n\n try {\n const whookConfig: WhookAPIOperationGCPFunctionConfig =\n operation['x-whook'] || {};\n const operationType = whookConfig.type || 'http';\n const sourceOperationId = whookConfig.sourceOperationId;\n const entryPoint = operationId;\n const finalEntryPoint =\n (sourceOperationId ? sourceOperationId : operationId) +\n ((operation['x-whook'] || {}).suffix || '');\n log('warning', `Building ${operationType} \"${finalEntryPoint}\"...`);\n const buildDefinition = BUILD_DEFINITIONS[operationType];\n // eslint-disable-next-line\n const applyWrapper = require(buildDefinition.wrapper.path).default;\n const rootNode = await $autoload(\n entryPoint + (buildDefinition.suffix || ''),\n );\n const lambdaPath = path.join(\n PROJECT_DIR,\n 'builds',\n NODE_ENV,\n finalEntryPoint,\n );\n const finalHandlerInitializer = applyWrapper(rootNode.initializer);\n\n const initializerContent = await buildInitializer(\n finalHandlerInitializer[SPECIAL_PROPS.INJECT].map((name) =>\n name === 'OPERATION_API'\n ? `OPERATION_API>OPERATION_API_${finalEntryPoint}`\n : name,\n ),\n BUILD_OPTIONS,\n );\n const indexContent = await buildLambdaIndex(\n rootNode,\n {\n name: buildDefinition.wrapper.name,\n path: buildDefinition.wrapper.path,\n },\n BUILD_OPTIONS,\n );\n\n await mkdirp(lambdaPath);\n await Promise.all([\n copyStaticFiles(\n { PROJECT_DIR, log },\n lambdaPath,\n whookConfig.staticFiles || [],\n ),\n ensureFileAsync(\n { log },\n path.join(lambdaPath, 'initialize.js'),\n initializerContent,\n ),\n ensureFileAsync({ log }, path.join(lambdaPath, 'main.js'), indexContent),\n ]);\n await buildFinalLambda({ compiler, log }, lambdaPath, whookConfig);\n } catch (err) {\n log('error', `Error building \"${operationId}\"...`);\n log('stack', err.stack);\n log('debug', JSON.stringify(err.params, null, 2));\n throw YError.wrap(err, 'E_LAMBDA_BUILD', operationId);\n }\n}\n\nasync function buildLambdaIndex(\n rootNode: { path: string },\n buildWrapper: { name: string; path: string },\n options: BuildOptions,\n): Promise<string> {\n return `${\n options.modules === 'commonjs'\n ? `const pickModule = (m) => { return m && m.default || m; }\nconst initHandler = pickModule(require('${rootNode.path}'));\nconst ${buildWrapper.name} = pickModule(require('${buildWrapper.path}'));\nconst { initialize } = require('./initialize');`\n : `import initHandler from '${rootNode.path}';\nimport ${buildWrapper.name} from '${buildWrapper.path}';\nimport { initialize } from './initialize';`\n }\n\nconst handlerInitializer = ${buildWrapper.name}(\n initHandler\n);\n\nconst handlerPromise = initialize()\n .then(handlerInitializer);\n\n${\n options.modules === 'commonjs'\n ? 'module.exports = {}; module.exports.default = '\n : 'export default '\n}function handler (req, res) {\n return handlerPromise\n .then(handler => handler(req, res));\n};\n`;\n}\n\nasync function buildFinalLambda(\n { compiler, log }: { compiler: WhookCompilerService; log: LogService },\n lambdaPath: string,\n whookConfig: WhookAPIOperationGCPFunctionConfig,\n): Promise<void> {\n const entryPoint = `${lambdaPath}/main.js`;\n const { contents, mappings } = await compiler(\n entryPoint,\n whookConfig.compilerOptions,\n );\n\n await Promise.all([\n ensureFileAsync({ log }, `${lambdaPath}/index.js`, contents, 'utf-8'),\n mappings\n ? ensureFileAsync(\n { log },\n `${lambdaPath}/index.js.map`,\n mappings,\n 'utf-8',\n )\n : Promise.resolve(),\n ]);\n}\n\nasync function copyStaticFiles(\n { PROJECT_DIR, log }: { PROJECT_DIR: string; log: LogService },\n lambdaPath: string,\n staticFiles: string[] = [],\n): Promise<void> {\n await Promise.all(\n staticFiles.map(\n async (staticFile) =>\n await copyFiles(\n { log },\n path.join(PROJECT_DIR, 'node_modules', staticFile),\n path.join(lambdaPath, 'node_modules', staticFile),\n ),\n ),\n );\n}\n\nasync function copyFiles(\n { log }: { log: LogService },\n source: string,\n destination: string,\n): Promise<void> {\n let theError;\n try {\n await mkdirp(destination);\n const data = await readFileAsync(source, 'utf-8');\n await ensureFileAsync({ log }, destination, data, 'utf-8');\n } catch (err) {\n theError = err;\n }\n if (theError) {\n if ('EISDIR' !== theError.code) {\n throw theError;\n }\n await cprAsync(source, destination, {\n overwrite: true,\n });\n }\n}\n\nasync function ensureFileAsync(\n { log }: { log: LogService },\n path: string,\n content: string,\n encoding = 'utf-8',\n): Promise<void> {\n try {\n const oldContent = await readFileAsync(path, encoding);\n\n if (oldContent === content) {\n log('debug', `Ignore unchanged file: \"${path}\".`);\n return;\n }\n } catch (err) {\n log('debug', `Write new file: \"${path}\".`);\n return await writeFileAsync(path, content, encoding);\n }\n log('debug', `Write changed file: \"${path}\".`);\n return await writeFileAsync(path, content, encoding);\n}\n"],"file":"index.mjs"}
|