-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
serializeClosure.ts
539 lines (472 loc) 路 22.5 KB
/
serializeClosure.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { log } from "../..";
import { Resource } from "../../resource";
import * as closure from "./createClosure";
import * as utils from "./utils";
/**
* SerializeFunctionArgs are arguments used to serialize a JavaScript function
*/
export interface SerializeFunctionArgs {
/**
* The name to export from the module defined by the generated module text. Defaults to 'handler'.
*/
exportName?: string;
/**
* A function to prevent serialization of certain objects captured during the serialization. Primarily used to
* prevent potential cycles.
*/
serialize?: (o: any) => boolean;
/**
* If this is a function which, when invoked, will produce the actual entrypoint function.
* Useful for when serializing a function that has high startup cost that only wants to be
* run once. The signature of this function should be: () => (provider_handler_args...) => provider_result
*
* This will then be emitted as: `exports.[exportName] = serialized_func_name();`
*
* In other words, the function will be invoked (once) and the resulting inner function will
* be what is exported.
*/
isFactoryFunction?: boolean;
/**
* The resource to log any errors we encounter against.
*/
logResource?: Resource;
/**
* If true, allow secrets to be serialized into the function. This should only be set to true if the calling
* code will handle this and propoerly wrap the resulting text in a Secret before passing it into any Resources
* or serializing it to any other output format. If set, the `containsSecrets` property on the returned
* SerializedFunction object will indicate whether secrets were serialized into the function text.
*/
allowSecrets?: boolean;
}
/**
* SerializeFunction is a representation of a serialized JavaScript function.
*/
export interface SerializedFunction {
/**
* The text of a JavaScript module which exports a single name bound to an appropriate value.
* In the case of a normal function, this value will just be serialized function. In the case
* of a factory function this value will be the result of invoking the factory function.
*/
text: string;
/**
* The name of the exported module member.
*/
exportName: string;
/**
* True if the serialized function text includes serialization of secret
*/
containsSecrets: boolean;
}
/**
* serializeFunction serializes a JavaScript function into a text form that can be loaded in another execution context,
* for example as part of a function callback associated with an AWS Lambda. The function serialization captures any
* variables captured by the function body and serializes those values into the generated text along with the function
* body. This process is recursive, so that functions referenced by the body of the serialized function will themselves
* be serialized as well. This process also deeply serializes captured object values, including prototype chains and
* property descriptors, such that the semantics of the function when deserialized should match the original function.
*
* There are several known limitations:
* - If a native function is captured either directly or indirectly, closure serialization will return an error.
* - Captured values will be serialized based on their values at the time that `serializeFunction` is called. Mutations
* to these values after that (but before the deserialized function is used) will not be observed by the deserialized
* function.
*
* @param func The JavaScript function to serialize.
* @param args Arguments to use to control the serialization of the JavaScript function.
*/
export async function serializeFunction(
func: Function,
args: SerializeFunctionArgs = {}): Promise<SerializedFunction> {
const exportName = args.exportName || "handler";
const serialize = args.serialize || (_ => true);
const isFactoryFunction = args.isFactoryFunction === undefined ? false : args.isFactoryFunction;
const closureInfo = await closure.createClosureInfoAsync(func, serialize, args.logResource);
if (!args.allowSecrets && closureInfo.containsSecrets) {
throw new Error("Secret outputs cannot be captured by a closure.");
}
return serializeJavaScriptText(closureInfo, exportName, isFactoryFunction);
}
/**
* @deprecated Please use 'serializeFunction' instead.
*/
export async function serializeFunctionAsync(
func: Function,
serialize?: (o: any) => boolean): Promise<string> {
log.warn("'function serializeFunctionAsync' is deprecated. Please use 'serializeFunction' instead.");
serialize = serialize || (_ => true);
const closureInfo = await closure.createClosureInfoAsync(func, serialize, /*logResource:*/ undefined);
if (closureInfo.containsSecrets) {
throw new Error("Secret outputs cannot be captured by a closure.");
}
return serializeJavaScriptText(closureInfo, "handler", /*isFactoryFunction*/ false).text;
}
/**
* serializeJavaScriptText converts a FunctionInfo object into a string representation of a Node.js module body which
* exposes a single function `exports.handler` representing the serialized function.
*
* @param c The FunctionInfo to be serialized into a module string.
*/
function serializeJavaScriptText(
outerClosure: closure.ClosureInfo,
exportName: string,
isFactoryFunction: boolean): SerializedFunction {
// Now produce a textual representation of the closure and its serialized captured environment.
// State used to build up the environment variables for all the funcs we generate.
// In general, we try to create idiomatic code to make the generated code not too
// hideous. For example, we will try to generate code like:
//
// var __e1 = [1, 2, 3] // or
// var __e2 = { a: 1, b: 2, c: 3 }
//
// However, for non-common cases (i.e. sparse arrays, objects with configured properties,
// etc. etc.) we will spit things out in a much more verbose fashion that eschews
// prettyness for correct semantics.
const envEntryToEnvVar = new Map<closure.Entry, string>();
const envVarNames = new Set<string>();
const functionInfoToEnvVar = new Map<closure.FunctionInfo, string>();
let environmentText = "";
let functionText = "";
const emittedRequires = new Set<string>();
const outerFunctionName = emitFunctionAndGetName(outerClosure.func);
if (environmentText) {
environmentText = "\n" + environmentText;
}
// Export the appropriate value. For a normal function, this will just be exporting the name of
// the module function we created by serializing it. For a factory function this will export
// the function produced by invoking the factory function once.
let text: string;
const exportText = `exports.${exportName} = ${outerFunctionName}${isFactoryFunction ? "()" : ""};`;
if (isFactoryFunction) {
// for a factory function, we need to call the function at the end. That way all the logic
// to set up the environment has run.
text = environmentText + functionText + "\n" + exportText;
}
else {
text = exportText + "\n" + environmentText + functionText;
}
return { text, exportName, containsSecrets: outerClosure.containsSecrets };
function emitFunctionAndGetName(functionInfo: closure.FunctionInfo): string {
// If this is the first time seeing this function, then actually emit the function code for
// it. Otherwise, just return the name of the emitted function for anyone that wants to
// reference it from their own code.
let functionName = functionInfoToEnvVar.get(functionInfo);
if (!functionName) {
functionName = functionInfo.name
? createEnvVarName(functionInfo.name, /*addIndexAtEnd:*/ false)
: createEnvVarName("f", /*addIndexAtEnd:*/ true);
functionInfoToEnvVar.set(functionInfo, functionName);
emitFunctionWorker(functionInfo, functionName);
}
return functionName;
}
function emitFunctionWorker(functionInfo: closure.FunctionInfo, varName: string) {
const capturedValues = envFromEnvObj(functionInfo.capturedValues);
const thisCapture = capturedValues.this;
const argumentsCapture = capturedValues.arguments;
delete capturedValues.this;
delete capturedValues.arguments;
const parameters = [...Array(functionInfo.paramCount)].map((_, index) => `__${index}`).join(", ");
for (const [keyEntry, { entry: valEntry }] of functionInfo.capturedValues) {
if (valEntry.module !== undefined) {
if(!emittedRequires.has(keyEntry.json)) {
emittedRequires.add(keyEntry.json);
functionText += `const ${keyEntry.json} = require("${valEntry.module}");\n`;
}
delete capturedValues[keyEntry.json];
}
}
functionText += "\n" +
"function " + varName + "(" + parameters + ") {\n" +
" return (function() {\n" +
" with(" + envObjToString(capturedValues) + ") {\n\n" +
"return " + functionInfo.code + ";\n\n" +
" }\n" +
" }).apply(" + thisCapture + ", " + argumentsCapture + ").apply(this, arguments);\n" +
"}\n";
// If this function is complex (i.e. non-default __proto__, or has properties, etc.)
// then emit those as well.
emitComplexObjectProperties(varName, varName, functionInfo);
if (functionInfo.proto !== undefined) {
const protoVar = envEntryToString(functionInfo.proto, `${varName}_proto`);
environmentText += `Object.setPrototypeOf(${varName}, ${protoVar});\n`;
}
}
function envFromEnvObj(env: closure.PropertyMap): Record<string, string> {
const envObj: Record<string, string> = {};
for (const [keyEntry, { entry: valEntry }] of env) {
if (typeof keyEntry.json !== "string") {
throw new Error("PropertyMap key was not a string.");
}
const key = keyEntry.json;
const val = envEntryToString(valEntry, key);
envObj[key] = val;
}
return envObj;
}
function envEntryToString(envEntry: closure.Entry, varName: string): string {
const envVar = envEntryToEnvVar.get(envEntry);
if (envVar !== undefined) {
return envVar;
}
// Complex objects may also be referenced from multiple functions. As such, we have to
// create variables for them in the environment so that all references to them unify to the
// same reference to the env variable. Effectively, we need to do this for any object that
// could be compared for reference-identity. Basic types (strings, numbers, etc.) have
// value semantics and this can be emitted directly into the code where they are used as
// there is no way to observe that you are getting a different copy.
if (isObjOrArrayOrRegExp(envEntry)) {
return complexEnvEntryToString(envEntry, varName);
}
else {
// Other values (like strings, bools, etc.) can just be emitted inline.
return simpleEnvEntryToString(envEntry, varName);
}
}
function simpleEnvEntryToString(
envEntry: closure.Entry, varName: string): string {
if (envEntry.hasOwnProperty("json")) {
return JSON.stringify(envEntry.json);
}
else if (envEntry.function !== undefined) {
return emitFunctionAndGetName(envEntry.function);
}
else if (envEntry.module !== undefined) {
return `require("${envEntry.module}")`;
}
else if (envEntry.output !== undefined) {
return envEntryToString(envEntry.output, varName);
}
else if (envEntry.expr) {
// Entry specifies exactly how it should be emitted. So just use whatever
// it wanted.
return envEntry.expr;
}
else if (envEntry.promise) {
return `Promise.resolve(${envEntryToString(envEntry.promise, varName)})`;
}
else {
throw new Error("Malformed: " + JSON.stringify(envEntry));
}
}
function complexEnvEntryToString(
envEntry: closure.Entry, varName: string): string {
// Call all environment variables __e<num> to make them unique. But suffix
// them with the original name of the property to help provide context when
// looking at the source.
const envVar = createEnvVarName(varName, /*addIndexAtEnd:*/ false);
envEntryToEnvVar.set(envEntry, envVar);
if (envEntry.object) {
emitObject(envVar, envEntry.object, varName);
}
else if (envEntry.array) {
emitArray(envVar, envEntry.array, varName);
}
else if (envEntry.regexp) {
const { source, flags } = envEntry.regexp;
const regexVal = `new RegExp(${JSON.stringify(source)}, ${JSON.stringify(flags)})`;
const entryString = `var ${envVar} = ${regexVal};\n`;
environmentText += entryString;
}
return envVar;
}
function createEnvVarName(baseName: string, addIndexAtEnd: boolean): string {
const trimLeadingUnderscoreRegex = /^_*/g;
const legalName = makeLegalJSName(baseName).replace(trimLeadingUnderscoreRegex, "");
let index = 0;
let currentName = addIndexAtEnd
? "__" + legalName + index
: "__" + legalName;
while (envVarNames.has(currentName)) {
currentName = addIndexAtEnd
? "__" + legalName + index
: "__" + index + "_" + legalName;
index++;
}
envVarNames.add(currentName);
return currentName;
}
function emitObject(envVar: string, obj: closure.ObjectInfo, varName: string): void {
const complex = isComplex(obj);
if (complex) {
// we have a complex child. Because of the possibility of recursion in
// the object graph, we have to spit out this variable uninitialized first.
// Then we can walk our children, creating a single assignment per child.
// This way, if the child ends up referencing us, we'll have already emitted
// the **initialized** variable for them to reference.
if (obj.proto) {
const protoVar = envEntryToString(obj.proto, `${varName}_proto`);
environmentText += `var ${envVar} = Object.create(${protoVar});\n`;
}
else {
environmentText += `var ${envVar} = {};\n`;
}
emitComplexObjectProperties(envVar, varName, obj);
}
else {
// All values inside this obj are simple. We can just emit the object
// directly as an object literal with all children embedded in the literal.
const props: string[] = [];
for (const [keyEntry, { entry: valEntry }] of obj.env) {
const keyName = typeof keyEntry.json === "string" ? keyEntry.json : "sym";
const propName = envEntryToString(keyEntry, keyName);
const propVal = simpleEnvEntryToString(valEntry, keyName);
if (typeof keyEntry.json === "string" && utils.isLegalMemberName(keyEntry.json)) {
props.push(`${keyEntry.json}: ${propVal}`);
}
else {
props.push(`[${propName}]: ${propVal}`);
}
}
const allProps = props.join(", ");
const entryString = `var ${envVar} = {${allProps}};\n`;
environmentText += entryString;
}
function isComplex(o: closure.ObjectInfo) {
if (obj.proto !== undefined) {
return true;
}
for (const v of o.env.values()) {
if (entryIsComplex(v)) {
return true;
}
}
return false;
}
function entryIsComplex(v: closure.PropertyInfoAndValue) {
return !isSimplePropertyInfo(v.info) || deepContainsObjOrArrayOrRegExp(v.entry);
}
}
function isSimplePropertyInfo(info: closure.PropertyInfo | undefined): boolean {
if (!info) {
return true;
}
return info.enumerable === true &&
info.writable === true &&
info.configurable === true &&
!info.get && !info.set;
}
function emitComplexObjectProperties(
envVar: string, varName: string, objEntry: closure.ObjectInfo): void {
for (const [keyEntry, { info, entry: valEntry }] of objEntry.env) {
const subName = typeof keyEntry.json === "string" ? keyEntry.json : "sym";
const keyString = envEntryToString(keyEntry, varName + "_" + subName);
const valString = envEntryToString(valEntry, varName + "_" + subName);
if (isSimplePropertyInfo(info)) {
// normal property. Just emit simply as a direct assignment.
if (typeof keyEntry.json === "string" && utils.isLegalMemberName(keyEntry.json)) {
environmentText += `${envVar}.${keyEntry.json} = ${valString};\n`;
}
else {
environmentText += `${envVar}${`[${keyString}]`} = ${valString};\n`;
}
}
else {
// complex property. emit as Object.defineProperty
emitDefineProperty(info!, valString, keyString);
}
}
function emitDefineProperty(
desc: closure.PropertyInfo, entryValue: string, propName: string) {
const copy: any = {};
if (desc.configurable) {
copy.configurable = desc.configurable;
}
if (desc.enumerable) {
copy.enumerable = desc.enumerable;
}
if (desc.writable) {
copy.writable = desc.writable;
}
if (desc.get) {
copy.get = envEntryToString(desc.get, `${varName}_get`);
}
if (desc.set) {
copy.set = envEntryToString(desc.set, `${varName}_set`);
}
if (desc.hasValue) {
copy.value = entryValue;
}
const line = `Object.defineProperty(${envVar}, ${propName}, ${ envObjToString(copy) });\n`;
environmentText += line;
}
}
function emitArray(
envVar: string, arr: closure.Entry[], varName: string): void {
if (arr.some(deepContainsObjOrArrayOrRegExp) || isSparse(arr) || hasNonNumericIndices(arr)) {
// we have a complex child. Because of the possibility of recursion in the object
// graph, we have to spit out this variable initialized (but empty) first. Then we can
// walk our children, knowing we'll be able to find this variable if they reference it.
environmentText += `var ${envVar} = [];\n`;
// Walk the names of the array properties directly. This ensures we work efficiently
// with sparse arrays. i.e. if the array has length 1k, but only has one value in it
// set, we can just set htat value, instead of setting 999 undefineds.
for (const key of Object.getOwnPropertyNames(arr)) {
if (key !== "length") {
const entryString = envEntryToString(arr[<any>key], `${varName}_${key}`);
environmentText += `${envVar}${
isNumeric(key) ? `[${key}]` : `.${key}`} = ${entryString};\n`;
}
}
}
else {
// All values inside this array are simple. We can just emit the array elements in
// place. i.e. we can emit as ``var arr = [1, 2, 3]`` as that's far more preferred than
// having four individual statements to do the same.
const strings: string[] = [];
for (let i = 0, n = arr.length; i < n; i++) {
strings.push(simpleEnvEntryToString(arr[i], `${varName}_${i}`));
}
const entryString = `var ${envVar} = [${strings.join(", ")}];\n`;
environmentText += entryString;
}
}
}
(<any>serializeJavaScriptText).doNotCapture = true;
const makeLegalRegex = /[^0-9a-zA-Z_]/g;
function makeLegalJSName(n: string) {
return n.replace(makeLegalRegex, x => "");
}
function isSparse<T>(arr: Array<T>) {
// getOwnPropertyNames for an array returns all the indices as well as 'length'.
// so we subtract one to get all the real indices. If that's not the same as
// the array length, then we must have missing properties and are thus sparse.
return arr.length !== (Object.getOwnPropertyNames(arr).length - 1);
}
function hasNonNumericIndices<T>(arr: Array<T>) {
return Object.keys(arr).some(k => k !== "length" && !isNumeric(k));
}
function isNumeric(n: string) {
return !isNaN(parseFloat(n)) && isFinite(+n);
}
function isObjOrArrayOrRegExp(env: closure.Entry): boolean {
return env.object !== undefined || env.array !== undefined || env.regexp !== undefined;
}
function deepContainsObjOrArrayOrRegExp(env: closure.Entry): boolean {
return isObjOrArrayOrRegExp(env) ||
(env.output !== undefined && deepContainsObjOrArrayOrRegExp(env.output)) ||
(env.promise !== undefined && deepContainsObjOrArrayOrRegExp(env.promise));
}
/**
* Converts an environment object into a string which can be embedded into a serialized function
* body. Note that this is not JSON serialization, as we may have property values which are
* variable references to other global functions. In other words, there can be free variables in the
* resulting object literal.
*
* @param envObj The environment object to convert to a string.
*/
function envObjToString(envObj: Record<string, string>): string {
return `{ ${Object.keys(envObj).map(k => `${k}: ${envObj[k]}`).join(", ")} }`;
}