UNPKG

102 kBTypeScriptView Raw
1/**
2 * Faast.js transforms ordinary JavaScript modules into serverless cloud
3 * functions that can run on AWS Lambda and Google Cloud Functions.
4 *
5 * The main entry point to faast.js is the {@link faast} function, which returns
6 * an object that implements the {@link FaastModule} interface. The most common
7 * options are {@link CommonOptions}. Using faast.js requires writing two
8 * modules, one containing the functions to upload to the cloud, and the other
9 * that invokes faast.js and calls the resulting cloud functions.
10 * @packageDocumentation
11 */
12
13/// <reference types="node" />
14
15import childProcess from 'child_process';
16import { cloudbilling_v1 } from 'googleapis';
17import { cloudfunctions_v1 } from 'googleapis';
18import { CloudWatchLogs } from 'aws-sdk';
19import { ConfigurationOptions } from 'aws-sdk/lib/config-base';
20import { default as debug_2 } from 'debug';
21import { GoogleApis } from 'googleapis';
22import { IAM } from 'aws-sdk';
23import { Lambda } from 'aws-sdk';
24import { Pricing } from 'aws-sdk';
25import { pubsub_v1 } from 'googleapis';
26import { Readable } from 'stream';
27import { S3 } from 'aws-sdk';
28import { SNS } from 'aws-sdk';
29import { SQS } from 'aws-sdk';
30import { STS } from 'aws-sdk';
31import { VError } from 'verror';
32import webpack from 'webpack';
33import { Writable } from 'stream';
34
35declare type AnyFunction = (...args: any[]) => any;
36
37/**
38 * `Async<T>` maps regular values to Promises and Iterators to AsyncIterators,
39 * If `T` is already a Promise or an AsyncIterator, it remains the same. This
40 * type is used to infer the return value of cloud functions from the types of
41 * the functions in the user's input module.
42 * @public
43 */
44export declare type Async<T> = T extends AsyncGenerator<infer R> ? AsyncGenerator<R> : T extends Generator<infer R> ? AsyncGenerator<R> : T extends Promise<infer R> ? Promise<R> : Promise<T>;
45
46/**
47 * `AsyncDetail<T>` is similar to {@link Async} except it maps retun values R to
48 * `Detail<R>`, which is the return value with additional information about each
49 * cloud function invocation.
50 * @public
51 */
52export declare type AsyncDetail<T> = T extends AsyncGenerator<infer R> ? AsyncGenerator<Detail<R>> : T extends Generator<infer R> ? AsyncGenerator<Detail<R>> : T extends Promise<infer R> ? Promise<Detail<R>> : Promise<Detail<T>>;
53
54declare class AsyncIterableQueue<T> extends AsyncQueue<IteratorResult<T>> {
55 push(value: T | Promise<T>): void;
56 done(): void;
57 [Symbol.asyncIterator](): this;
58}
59
60declare class AsyncQueue<T> {
61 protected deferred: Array<Deferred<T>>;
62 protected enqueued: Promise<T>[];
63 enqueue(value: T | Promise<T>): void;
64 next(): Promise<T>;
65 clear(): void;
66}
67
68/**
69 * The return type of {@link faastAws}. See {@link FaastModuleProxy}.
70 * @public
71 */
72export declare type AwsFaastModule<M extends object = object> = FaastModuleProxy<M, AwsOptions, AwsState>;
73
74declare type AwsGcWork = {
75 type: "SetLogRetention";
76 logGroupName: string;
77 retentionInDays: number;
78} | {
79 type: "DeleteResources";
80 resources: AwsResources;
81} | {
82 type: "DeleteLayerVersion";
83 LayerName: string;
84 VersionNumber: number;
85};
86
87declare interface AwsLayerInfo {
88 Version: number;
89 LayerVersionArn: string;
90 LayerName: string;
91}
92
93declare class AwsMetrics {
94 outboundBytes: number;
95 sns64kRequests: number;
96 sqs64kRequests: number;
97}
98
99/**
100 * AWS-specific options for {@link faastAws}.
101 * @public
102 */
103export declare interface AwsOptions extends CommonOptions {
104 /**
105 * The region to create resources in. Garbage collection is also limited to
106 * this region. Default: `"us-west-2"`.
107 */
108 region?: AwsRegion;
109 /**
110 * The role that the lambda function will assume when executing user code.
111 * Default: `"faast-cached-lambda-role"`. Rarely used.
112 * @remarks
113 * When a lambda executes, it first assumes an
114 * {@link https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html | execution role}
115 * to grant access to resources.
116 *
117 * By default, faast.js creates this execution role for you and leaves it
118 * permanently in your account (the role is shared across all lambda
119 * functions created by faast.js). By default, faast.js grants administrator
120 * privileges to this role so your code can perform any AWS operation it
121 * requires.
122 *
123 * You can
124 * {@link https://console.aws.amazon.com/iam/home#/roles | create a custom role}
125 * that specifies more limited permissions if you prefer not to grant
126 * administrator privileges. Any role you assign for faast.js modules needs
127 * at least the following permissions:
128 *
129 * - Execution Role:
130 * ```json
131 * {
132 * "Version": "2012-10-17",
133 * "Statement": [
134 * {
135 * "Effect": "Allow",
136 * "Action": ["logs:*"],
137 * "Resource": "arn:aws:logs:*:*:log-group:faast-*"
138 * },
139 * {
140 * "Effect": "Allow",
141 * "Action": ["sqs:*"],
142 * "Resource": "arn:aws:sqs:*:*:faast-*"
143 * }
144 * ]
145 * }
146 * ```
147 *
148 * - Trust relationship (also known as `AssumeRolePolicyDocument` in the AWS
149 * SDK):
150 * ```json
151 * {
152 * "Version": "2012-10-17",
153 * "Statement": [
154 * {
155 * "Effect": "Allow",
156 * "Principal": {
157 * "Service": "lambda.amazonaws.com"
158 * },
159 * "Action": "sts:AssumeRole"
160 * }
161 * ]
162 * }
163 * ```
164 *
165 */
166 RoleName?: string;
167 /**
168 * Additional options to pass to AWS Lambda creation. See
169 * {@link https://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html | CreateFunction}.
170 * @remarks
171 * If you need specialized options, you can pass them to the AWS Lambda SDK
172 * directly. Note that if you override any settings set by faast.js, you may
173 * cause faast.js to not work:
174 *
175 * ```typescript
176 * const request: aws.Lambda.CreateFunctionRequest = {
177 * FunctionName,
178 * Role,
179 * Runtime: "nodejs14.x",
180 * Handler: "index.trampoline",
181 * Code,
182 * Description: "faast trampoline function",
183 * Timeout,
184 * MemorySize,
185 * ...awsLambdaOptions
186 * };
187 * ```
188 */
189 awsLambdaOptions?: Partial<Lambda.CreateFunctionRequest>;
190 /**
191 * Additional options to pass to all AWS services. See
192 * {@link https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html | AWS.Config}.
193 * @remarks
194 * If you need to specify AWS options such as credentials, you can pass set
195 * these options here. Note that faast.js will override some options even if
196 * they are specified here, specifically the options used to create the
197 * `Lambda` service, to ensure they allow for faast to function correctly.
198 * Options set in {@link CommonOptions} override those set here.
199 *
200 * Example of passing in credentials:
201 *
202 * ```typescript
203 * const credentials = { accessKeyId, secretAccessKey };
204 * const m = await faastAws(funcs, { credentials });
205 * ```
206 */
207 awsConfig?: ConfigurationOptions;
208 /**
209 * Use webpack to pack up the aws-sdk dependency, instead of relying on the
210 * preinstalled version on AWS Lambda. This is useful for using the node18
211 * runtime, because it uses the aws-sdk v3, whereas faast.js currently only
212 * supports v2.
213 */
214 webpackAwsSdk?: boolean;
215 /** @internal */
216 _gcWorker?: (work: AwsGcWork, services: AwsServices) => Promise<void>;
217}
218
219/**
220 * Valid AWS
221 * {@link https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html | regions}.
222 * Not all of these regions have Lambda support.
223 * @public
224 */
225export declare type AwsRegion = "us-east-1" | "us-east-2" | "us-west-1" | "us-west-2" | "ca-central-1" | "eu-central-1" | "eu-west-1" | "eu-west-2" | "eu-west-3" | "ap-northeast-1" | "ap-northeast-2" | "ap-northeast-3" | "ap-southeast-1" | "ap-southeast-2" | "ap-south-1" | "sa-east-1";
226
227declare interface AwsResources {
228 FunctionName: string;
229 RoleName: string;
230 region: AwsRegion;
231 ResponseQueueUrl?: string;
232 ResponseQueueArn?: string;
233 RequestTopicArn?: string;
234 SNSLambdaSubscriptionArn?: string;
235 logGroupName: string;
236 layer?: AwsLayerInfo;
237 Bucket?: string;
238}
239
240declare interface AwsServices {
241 readonly lambda: Lambda;
242 readonly lambda2: Lambda;
243 readonly cloudwatch: CloudWatchLogs;
244 readonly iam: IAM;
245 readonly sqs: SQS;
246 readonly sns: SNS;
247 readonly pricing: Pricing;
248 readonly sts: STS;
249 readonly s3: S3;
250}
251
252/**
253 * @public
254 */
255declare interface AwsState {
256 /** @internal */
257 resources: AwsResources;
258 /** @internal */
259 services: AwsServices;
260 /** @internal */
261 options: Required<AwsOptions>;
262 /** @internal */
263 metrics: AwsMetrics;
264 /** @internal */
265 gcPromise?: Promise<"done" | "skipped">;
266}
267
268declare interface Blob_2 {
269}
270
271declare interface CallId {
272 callId: string;
273}
274
275declare type CallId_2 = string;
276
277declare interface CallingContext {
278 call: FunctionCall;
279 startTime: number;
280 logUrl?: string;
281 executionId?: string;
282 instanceId?: string;
283}
284
285/**
286 * Options that apply to the {@link FaastModule.cleanup} method.
287 * @public
288 */
289export declare interface CleanupOptions {
290 /**
291 * If true, delete provider cloud resources. Default: true.
292 * @remarks
293 * The cleanup operation has two functions: stopping the faast.js runtime
294 * and deleting cloud resources that were instantiated. If `deleteResources`
295 * is false, then only the runtime is stopped and no cloud resources are
296 * deleted. This can be useful for debugging and examining the state of
297 * resources created by faast.js.
298 *
299 * It is supported to call {@link FaastModule.cleanup} twice: once with
300 * `deleteResources` set to `false`, which only stops the runtime, and then
301 * again set to `true` to delete resources. This can be useful for testing.
302 */
303 deleteResources?: boolean;
304 /**
305 * If true, delete cached resources. Default: false.
306 * @remarks
307 * Some resources are cached persistently between calls for performance
308 * reasons. If this option is set to true, these cached resources are
309 * deleted when cleanup occurs, instead of being left behind for future use.
310 * For example, on AWS this includes the Lambda Layers that are created for
311 * {@link CommonOptions.packageJson} dependencies. Note that only the cached
312 * resources created by this instance of FaastModule are deleted, not cached
313 * resources from other FaastModules. This is similar to setting
314 * `useCachedDependencies` to `false` during function construction, except
315 * `deleteCaches` can be set at function cleanup time, and any other
316 * FaastModules created before cleanup may use the cached Layers.
317 */
318 deleteCaches?: boolean;
319 /**
320 * Number of seconds to wait for garbage collection. Default: 10.
321 * @remarks
322 * Garbage collection can still be operating when cleanup is called; this
323 * option limits the amount of time faast waits for the garbage collector.
324 * If set to 0, the wait is unlimited.
325 */
326 gcTimeout?: number;
327}
328
329/**
330 * Options common across all faast.js providers. Used as argument to {@link faast}.
331 * @remarks
332 * There are also more specific options for each provider. See
333 * {@link AwsOptions}, {@link GoogleOptions}, and {@link LocalOptions}.
334 * @public
335 */
336export declare interface CommonOptions {
337 /**
338 * If true, create a child process to isolate user code from faast
339 * scaffolding. Default: true.
340 * @remarks
341 * If a child process is not created, faast runs in the same node instance
342 * as the user code and may not execute in a timely fashion because user
343 * code may
344 * {@link https://nodejs.org/en/docs/guides/dont-block-the-event-loop/ | block the event loop}.
345 * Creating a child process for user code allows faast.js to continue
346 * executing even if user code never yields. This provides better
347 * reliability and functionality:
348 *
349 * - Detect timeout errors more reliably, even if the function doesn't
350 * relinquish the CPU. Not applicable to AWS, which sends separate failure
351 * messages in case of timeout. See {@link CommonOptions.timeout}.
352 *
353 * - CPU metrics used for detecting invocations with high latency, which can
354 * be used for automatically retrying calls to reduce tail latency.
355 *
356 * The cost of creating a child process is mainly in the memory overhead of
357 * creating another node process.
358 */
359 childProcess?: boolean;
360 /**
361 * When childProcess is true, the child process will be spawned with the
362 * value of this property as the setting for --max-old-space-size.
363 * @remarks
364 * This is useful if a function requires the node process to limit its
365 * memory so that another spawned process (e.g. a browser instance) can use
366 * the rest.
367 * @public
368 */
369 childProcessMemoryMb?: number;
370 /**
371 * The maximum number of concurrent invocations to allow. Default: 100,
372 * except for the `local` provider, where the default is 10.
373 * @remarks
374 * The concurrency limit applies to all invocations of all of the faast
375 * functions summed together. It is not a per-function limit. To apply a
376 * per-function limit, use {@link throttle}. A value of 0 is equivalent to
377 * Infinity. A value of 1 ensures mutually exclusive invocations.
378 */
379 concurrency?: number;
380 /**
381 * A user-supplied description for this function, which may make it easier
382 * to track different functions when multiple functions are created.
383 */
384 description?: string;
385 /**
386 * Exclude a subset of files included by {@link CommonOptions.include}.
387 * @remarks
388 * The exclusion can be a directory or glob. Exclusions apply to all included
389 * entries.
390 */
391 exclude?: string[];
392 /**
393 * Rate limit invocations (invocations/sec). Default: no rate limit.
394 * @remarks
395 * Some services cannot handle more than a certain number of requests per
396 * second, and it is easy to overwhelm them with a large number of cloud
397 * functions. Specify a rate limit in invocation/second to restrict how
398 * faast.js issues requests.
399 */
400 rate?: number;
401 /**
402 * Environment variables available during serverless function execution.
403 * Default: \{\}.
404 */
405 env?: {
406 [key: string]: string;
407 };
408 /**
409 * Garbage collector mode. Default: `"auto"`.
410 * @remarks
411 * Garbage collection deletes resources that were created by previous
412 * instantiations of faast that were not cleaned up by
413 * {@link FaastModule.cleanup}, either because it was not called or because
414 * the process terminated and did not execute this cleanup step. In `"auto"`
415 * mode, garbage collection may be throttled to run up to once per hour no
416 * matter how many faast.js instances are created. In `"force"` mode,
417 * garbage collection is run without regard to whether another gc has
418 * already been performed recently. In `"off"` mode, garbage collection is
419 * skipped entirely. This can be useful for performance-sensitive tests, or
420 * for more control over when gc is performed.
421 *
422 * Garbage collection is cloud-specific, but in general garbage collection
423 * should not interfere with the behavior or performance of faast cloud
424 * functions. When {@link FaastModule.cleanup} runs, it waits for garbage
425 * collection to complete. Therefore the cleanup step can in some
426 * circumstances take a significant amount of time even after all
427 * invocations have returned.
428 *
429 * It is generally recommended to leave garbage collection in `"auto"` mode,
430 * otherwise garbage resources may accumulate over time and you will
431 * eventually hit resource limits on your account.
432 *
433 * Also see {@link CommonOptions.retentionInDays}.
434 */
435 gc?: "auto" | "force" | "off";
436 /**
437 * Include files to make available in the remote function. See
438 * {@link IncludeOption}.
439 * @remarks
440 * Each include entry is a directory or glob pattern. Paths can be specified
441 * as relative or absolute paths. Relative paths are resolved relative to
442 * the current working directory, or relative to the `cwd` option.
443 *
444 * If the include entry is a directory `"foo/bar"`, the directory
445 * `"./foo/bar"` will be available in the cloud function. Directories are
446 * recursively added.
447 *
448 * Glob patterns use the syntax of
449 * {@link https://github.com/isaacs/node-glob | node glob}.
450 *
451 * Also see {@link CommonOptions.exclude} for file exclusions.
452 */
453 include?: (string | IncludeOption)[];
454 /**
455 * Maximum number of times that faast will retry each invocation. Default: 2
456 * (invocations can therefore be attemped 3 times in total).
457 * @remarks
458 * Retries are automatically attempted for transient infrastructure-level
459 * failures such as rate limits or netowrk failures. User-level exceptions
460 * are not retried automatically. In addition to retries performed by faast,
461 * some providers automatically attempt retries. These are not controllable
462 * by faast. But as a result, your function may be retried many more times
463 * than this setting suggests.
464 */
465 maxRetries?: number;
466 /**
467 * Memory limit for each function in MB. This setting has an effect on
468 * pricing. Default varies by provider.
469 * @remarks
470 * Each provider has different settings for memory size, and performance
471 * varies depending on the setting. By default faast picks a likely optimal
472 * value for each provider.
473 *
474 * - aws: 1728MB
475 *
476 * - google: 1024MB
477 *
478 * - local: 512MB (however, memory size limits aren't reliable in local mode.)
479 */
480 memorySize?: number;
481 /**
482 * Specify invocation mode. Default: `"auto"`.
483 * @remarks
484 * Modes specify how invocations are triggered. In https mode, the functions
485 * are invoked through an https request or the provider's API. In queue
486 * mode, a provider-specific queue is used to invoke functions. Queue mode
487 * adds additional latency and (usually negligible) cost, but may scale
488 * better for some providers. In auto mode the best default is chosen for
489 * each provider depending on its particular performance characteristics.
490 *
491 * The defaults are:
492 *
493 * - aws: `"auto"` is `"https"`. In https mode, the AWS SDK api
494 * is used to invoke functions. In queue mode, an AWS SNS topic is created
495 * and triggers invocations. The AWS API Gateway service is never used by
496 * faast, as it incurs a higher cost and is not needed to trigger
497 * invocations.
498 *
499 * - google: `"auto"` is `"https"`. In https mode, a PUT request is made to
500 * invoke the cloud function. In queue mode, a PubSub topic is created to
501 * invoke functions.
502 *
503 * - local: The local provider ignores the mode setting and always uses an
504 * internal asynchronous queue to schedule calls.
505 *
506 * Size limits are affected by the choice of mode. On AWS the limit is 256kb
507 * for arguments and return values in `"queue"` mode, and 6MB for `"https"`
508 * mode. For Google the limit is 10MB regardless of mode. In Local mode
509 * messages are sent via node's IPC and are subject to OS IPC limits.
510 *
511 * Note that no matter which mode is selected, faast.js always creates a
512 * queue for sending back intermediate results for bookeeping and
513 * performance monitoring.
514 */
515 mode?: "https" | "queue" | "auto";
516 /**
517 * Specify a package.json file to include with the code package.
518 * @remarks
519 * By default, faast.js will use webpack to bundle dependencies your remote
520 * module imports. In normal usage there is no need to specify a separate
521 * package.json, as webpack will statically analyze your imports and
522 * determine which files to bundle.
523 *
524 * However, there are some use cases where this is not enough. For example,
525 * some dependencies contain native code compiled during installation, and
526 * webpack cannot bundle these native modules. such as dependencies with
527 * native code. or are specifically not designed to work with webpack. In
528 * these cases, you can create a separate `package.json` for these
529 * dependencies and pass the filename as the `packageJson` option. If
530 * `packageJson` is an `object`, it is assumed to be a parsed JSON object
531 * with the same structure as a package.json file (useful for specifying a
532 * synthetic `package.json` directly in code).
533 *
534 * The way the `packageJson` is handled varies by provider:
535 *
536 * - local: Runs `npm install` in a temporary directory it prepares for the
537 * function.
538 *
539 * - google: uses Google Cloud Function's
540 * {@link https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs | native support for package.json}.
541 *
542 * - aws: Recursively calls faast.js to run `npm install` inside a separate
543 * lambda function specifically created for this purpose. Faast.js uses
544 * lambda to install dependencies to ensure that native dependencies are
545 * compiled in an environment that can produce binaries linked against
546 * lambda's
547 * {@link https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/ | execution environment}.
548 * Packages are saved in a Lambda Layer.
549 *
550 * For AWS, if {@link CommonOptions.useDependencyCaching} is `true` (which
551 * is the default), then the Lambda Layer created will be reused in future
552 * function creation requests if the contents of `packageJson` are the same.
553 *
554 * The `FAAST_PACKAGE_DIR` environment variable can be useful for debugging
555 * `packageJson` issues.
556 */
557 packageJson?: string | object;
558 /**
559 * Cache installed dependencies from {@link CommonOptions.packageJson}. Only
560 * applies to AWS. Default: true.
561 * @remarks
562 * If `useDependencyCaching` is `true`, The resulting `node_modules` folder
563 * is cached in a Lambda Layer with the name `faast-${key}`, where `key` is
564 * the SHA1 hash of the `packageJson` contents. These cache entries are
565 * removed by garbage collection, by default after 24h. Using caching
566 * reduces the need to install and upload dependencies every time a function
567 * is created. This is important for AWS because it creates an entirely
568 * separate lambda function to install dependencies remotely, which can
569 * substantially increase function deployment time.
570 *
571 * If `useDependencyCaching` is false, the lambda layer is created with the
572 * same name as the lambda function, and then is deleted when cleanup is
573 * run.
574 */
575 useDependencyCaching?: boolean;
576 /**
577 * Specify how many days to wait before reclaiming cloud garbage. Default:
578 * 1.
579 * @remarks
580 * Garbage collection only deletes resources after they age beyond a certain
581 * number of days. This option specifies how many days old a resource needs
582 * to be before being considered garbage by the collector. Note that this
583 * setting is not recorded when the resources are created. For example,
584 * suppose this is the sequence of events:
585 *
586 * - Day 0: `faast()` is called with `retentionInDays` set to 5. Then, the
587 * function crashes (or omits the call to {@link FaastModule.cleanup}).
588 *
589 * - Day 1: `faast()` is called with `retentionInDays` set to 1.
590 *
591 * In this sequence of events, on Day 0 the garbage collector runs and
592 * removes resources with age older than 5 days. Then the function leaves
593 * new garbage behind because it crashed or did not complete cleanup. On Day
594 * 1, the garbage collector runs and deletes resources at least 1 day old,
595 * which includes garbage left behind from Day 0 (based on the creation
596 * timestamp of the resources). This deletion occurs even though retention
597 * was set to 5 days when resources were created on Day 0.
598 *
599 * On Google, logs are retained according to Google's default expiration
600 * policy (30 days) instead of being deleted by garbage collection.
601 *
602 * Note that if `retentionInDays` is set to 0, garbage collection will
603 * remove all resources, even ones that may be in use by other running faast
604 * instances. Not recommended.
605 *
606 * See {@link CommonOptions.gc}.
607 */
608 retentionInDays?: number;
609 /**
610 * Reduce tail latency by retrying invocations that take substantially
611 * longer than other invocations of the same function. Default: 3.
612 * @remarks
613 * faast.js automatically measures the mean and standard deviation (σ) of
614 * the time taken by invocations of each function. Retries are attempted
615 * when the time for an invocation exceeds the mean time by a certain
616 * threshold. `speculativeRetryThreshold` specifies how many multiples of σ
617 * an invocation needs to exceed the mean for a given function before retry
618 * is attempted.
619 *
620 * The default value of σ is 3. This means a call to a function is retried
621 * when the time to execute exceeds three standard deviations from the mean
622 * of all prior executions of the same function.
623 *
624 * This feature is experimental.
625 * @beta
626 */
627 speculativeRetryThreshold?: number;
628 /**
629 * Execution time limit for each invocation, in seconds. Default: 60.
630 * @remarks
631 * Each provider has a maximum time limit for how long invocations can run
632 * before being automatically terminated (or frozen). The following are the
633 * maximum time limits as of February 2019:
634 *
635 * - aws:
636 * {@link https://docs.aws.amazon.com/lambda/latest/dg/limits.html | 15 minutes}
637 *
638 * - google: {@link https://cloud.google.com/functions/quotas | 9 minutes}
639 *
640 * - local: unlimited
641 *
642 * Faast.js has a proactive timeout detection feature. It automatically
643 * attempts to detect when the time limit is about to be reached and
644 * proactively sends a timeout exception. Faast does this because not all
645 * providers reliably send timely feedback when timeouts occur, leaving
646 * developers to look through cloud logs. In general faast.js' timeout will
647 * be up to 5s earlier than the timeout specified, in order to give time to
648 * allow faast.js to send a timeout message. Proactive timeout detection
649 * only works with {@link CommonOptions.childProcess} set to `true` (the
650 * default).
651 */
652 timeout?: number;
653 /**
654 * Extra webpack options to use to bundle the code package.
655 * @remarks
656 * By default, faast.js uses webpack to bundle the code package. Webpack
657 * automatically handles finding and bundling dependencies, adding source
658 * mappings, etc. If you need specialized bundling, use this option to add
659 * or override the default webpack configuration. The library
660 * {@link https://github.com/survivejs/webpack-merge | webpack-merge} is
661 * used to combine configurations.
662 *
663 * ```typescript
664 * const config: webpack.Configuration = merge({
665 * entry,
666 * mode: "development",
667 * output: {
668 * path: "/",
669 * filename: outputFilename,
670 * libraryTarget: "commonjs2"
671 * },
672 * target: "node",
673 * resolveLoader: { modules: [__dirname, `${__dirname}/dist`] },
674 * node: { global: true, __dirname: false, __filename: false }
675 * },
676 * webpackOptions);
677 * ```
678 *
679 * Take care when setting the values of `entry`, `output`, or
680 * `resolveLoader`. If these options are overwritten, faast.js may fail to
681 * bundle your code. In particular, setting `entry` to an array value will
682 * help `webpack-merge` to concatenate its value instead of replacing the
683 * value that faast.js inserts for you.
684 *
685 * Default:
686 *
687 * - aws: `{ externals: [new RegExp("^aws-sdk/?")] }`. In the lambda
688 * environment `"aws-sdk"` is available in the ambient environment and
689 * does not need to be bundled.
690 *
691 * - other providers: `{}`
692 *
693 * The `FAAST_PACKAGE_DIR` environment variable can be useful for debugging
694 * webpack issues.
695 */
696 webpackOptions?: webpack.Configuration;
697 /**
698 * Check arguments and return values from cloud functions are serializable
699 * without losing information. Default: true.
700 * @remarks
701 * Arguments to cloud functions are automatically serialized with
702 * `JSON.stringify` with a custom replacer that handles built-in JavaScript
703 * types such as `Date` and `Buffer`. Return values go through the same
704 * process. Some JavaScript objects cannot be serialized. By default
705 * `validateSerialization` will verify that every argument and return value
706 * can be serialized and deserialized without losing information. A
707 * `FaastError` will be thrown if faast.js detects a problem according to
708 * the following procedure:
709 *
710 * 1. Serialize arguments and return values with `JSON.stringify` using a
711 * special `replacer` function.
712 *
713 * 2. Deserialize the values with `JSON.parse` with a special `reviver`
714 * function.
715 *
716 * 3. Use
717 * {@link https://nodejs.org/api/assert.html#assert_assert_deepstrictequal_actual_expected_message | assert.deepStringEqual}
718 * to compare the original object with the deserialized object from step
719 * 2.
720 *
721 * There is some overhead to this process because each argument is
722 * serialized and deserialized, which can be costly if arguments or return
723 * values are large.
724 */
725 validateSerialization?: boolean;
726 /**
727 * Debugging output options.
728 * @internal
729 */
730 debugOptions?: {
731 [key: string]: boolean;
732 };
733}
734
735/**
736 * Analyze the cost of a workload across many provider configurations.
737 * @public
738 */
739export declare namespace CostAnalyzer {
740 /**
741 * An input to {@link CostAnalyzer.analyze}, specifying one
742 * configuration of faast.js to run against a workload. See
743 * {@link AwsOptions} and {@link GoogleOptions}.
744 * @public
745 */
746 export type Configuration = {
747 provider: "aws";
748 options: AwsOptions;
749 } | {
750 provider: "google";
751 options: GoogleOptions;
752 };
753 /**
754 * Default AWS cost analyzer configurations include all memory sizes for AWS
755 * Lambda.
756 * @remarks
757 * The default AWS cost analyzer configurations include every memory size
758 * from 128MB to 3008MB in 64MB increments. Each configuration has the
759 * following settings:
760 *
761 * ```typescript
762 * {
763 * provider: "aws",
764 * options: {
765 * mode: "https",
766 * memorySize,
767 * timeout: 300,
768 * gc: "off",
769 * childProcess: true
770 * }
771 * }
772 * ```
773 *
774 * Use `Array.map` to change or `Array.filter` to remove some of these
775 * configurations. For example:
776 *
777 * ```typescript
778 * const configsWithAtLeast1GB = awsConfigurations.filter(c => c.memorySize > 1024)
779 * const shorterTimeout = awsConfigurations.map(c => ({...c, timeout: 60 }));
780 * ```
781 * @public
782 */
783 const awsConfigurations: Configuration[];
784 /**
785 * Default Google Cloud Functions cost analyzer configurations include all
786 * available memory sizes.
787 * @remarks
788 * Each google cost analyzer configuration follows this template:
789 *
790 * ```typescript
791 * {
792 * provider: "google",
793 * options: {
794 * mode: "https",
795 * memorySize,
796 * timeout: 300,
797 * gc: "off",
798 * childProcess: true
799 * }
800 * }
801 * ```
802 *
803 * where `memorySize` is in `[128, 256, 512, 1024, 2048]`.
804 * @public
805 */
806 const googleConfigurations: Configuration[];
807 /**
808 * User-defined custom metrics for a workload. These are automatically
809 * summarized in the output; see {@link CostAnalyzer.Workload}.
810 * @public
811 */
812 export type WorkloadAttribute<A extends string> = {
813 [attr in A]: number;
814 };
815 /**
816 * A user-defined cost analyzer workload for {@link CostAnalyzer.analyze}.
817 * @public
818 * Example:
819 */
820 export interface Workload<T extends object, A extends string> {
821 /**
822 * The imported module that contains the cloud functions to test.
823 */
824 funcs: T;
825 /**
826 * A function that executes cloud functions on
827 * `faastModule.functions.*`. The work function should return `void` if
828 * there are no custom workload attributes. Otherwise, it should return
829 * a {@link CostAnalyzer.WorkloadAttribute} object which maps
830 * user-defined attribute names to numerical values for the workload.
831 * For example, this might measure bandwidth or some other metric not
832 * tracked by faast.js, but are relevant for evaluating the
833 * cost-performance tradeoff of the configurations analyzed by the cost
834 * analyzer.
835 */
836 work: (faastModule: FaastModule<T>) => Promise<WorkloadAttribute<A> | void>;
837 /**
838 * An array of configurations to run the work function against (see
839 * {@link CostAnalyzer.Configuration}). For example, each entry in the
840 * array may specify a provider, memory size, and other options.
841 * Default: {@link CostAnalyzer.awsConfigurations}.
842 */
843 configurations?: Configuration[];
844 /**
845 * Combine {@link CostAnalyzer.WorkloadAttribute} instances returned
846 * from multiple workload executions (caused by value of
847 * {@link CostAnalyzer.Workload.repetitions}). The default is a function
848 * that takes the average of each attribute.
849 */
850 summarize?: (summaries: WorkloadAttribute<A>[]) => WorkloadAttribute<A>;
851 /**
852 * Format an attribute value for console output. This is displayed by
853 * the cost analyzer when all of the repetitions for a configuration
854 * have completed. The default returns
855 * `${attribute}:${value.toFixed(1)}`.
856 */
857 format?: (attr: A, value: number) => string;
858 /**
859 * Format an attribute value for CSV. The default returns
860 * `value.toFixed(1)`.
861 */
862 formatCSV?: (attr: A, value: number) => string;
863 /**
864 * If true, do not output live results to the console. Can be useful for
865 * running the cost analyzer as part of automated tests. Default: false.
866 */
867 silent?: boolean;
868 /**
869 * The number of repetitions to run the workload for each cost analyzer
870 * configuration. Higher repetitions help reduce the jitter in the
871 * results. Repetitions execute in the same FaastModule instance.
872 * Default: 10.
873 */
874 repetitions?: number;
875 /**
876 * The amount of concurrency to allow. Concurrency can arise from
877 * multiple repetitions of the same configuration, or concurrenct
878 * executions of different configurations. This concurrency limit
879 * throttles the total number of concurrent workload executions across
880 * both of these sources of concurrency. Default: 64.
881 */
882 concurrency?: number;
883 }
884 /**
885 * A cost estimate result for a specific cost analyzer configuration.
886 * @public
887 */
888 export interface Estimate<A extends string> {
889 /**
890 * The cost snapshot for the cost analysis of the specific (workload,
891 * configuration) combination. See {@link CostSnapshot}.
892 */
893 costSnapshot: CostSnapshot;
894 /**
895 * The worload configuration that was analyzed. See
896 * {@link CostAnalyzer.Configuration}.
897 */
898 config: Configuration;
899 /**
900 * Additional workload metrics returned from the work function. See
901 * {@link CostAnalyzer.WorkloadAttribute}.
902 */
903 extraMetrics: WorkloadAttribute<A>;
904 }
905 /**
906 * Estimate the cost of a workload using multiple configurations and
907 * providers.
908 * @param userWorkload - a {@link CostAnalyzer.Workload} object specifying
909 * the workload to run and additional parameters.
910 * @returns A promise for a {@link CostAnalyzer.Result}
911 * @public
912 * @remarks
913 * It can be deceptively difficult to set optimal parameters for AWS Lambda
914 * and similar services. On the surface there appears to be only one
915 * parameter: memory size. Choosing more memory also gives more CPU
916 * performance, but it's unclear how much. It's also unclear where single
917 * core performance stops getting better. The workload cost analyzer solves
918 * these problems by making it easy to run cost experiments.
919 * ```text
920 * (AWS)
921 * ┌───────┐
922 * ┌────▶│ 128MB │
923 * │ └───────┘
924 * │ ┌───────┐
925 * ┌─────────────────┐ ├────▶│ 256MB │
926 * ┌──────────────┐ │ │ │ └───────┘
927 * │ workload │───▶│ │ │ ...
928 * └──────────────┘ │ │ │ ┌───────┐
929 * │ cost analyzer │─────┼────▶│3008MB │
930 * ┌──────────────┐ │ │ │ └───────┘
931 * │configurations│───▶│ │ │
932 * └──────────────┘ │ │ │ (Google)
933 * └─────────────────┘ │ ┌───────┐
934 * ├────▶│ 128MB │
935 * │ └───────┘
936 * │ ┌───────┐
937 * └────▶│ 256MB │
938 * └───────┘
939 * ```
940 * `costAnalyzer` is the entry point. It automatically runs this workload
941 * against multiple configurations in parallel. Then it uses faast.js' cost
942 * snapshot mechanism to automatically determine the price of running the
943 * workload with each configuration.
944 *
945 * Example:
946 *
947 * ```typescript
948 * // functions.ts
949 * export function randomNumbers(n: number) {
950 * let sum = 0;
951 * for (let i = 0; i < n; i++) {
952 * sum += Math.random();
953 * }
954 * return sum;
955 * }
956 *
957 * // cost-analyzer-example.ts
958 * import { writeFileSync } from "fs";
959 * import { CostAnalyzer, FaastModule } from "faastjs";
960 * import * as funcs from "./functions";
961 *
962 * async function work(faastModule: FaastModule<typeof funcs>) {
963 * await faastModule.functions.randomNumbers(100000000);
964 * }
965 *
966 * async function main() {
967 * const results = await CostAnalyzer.analyze({ funcs, work });
968 * writeFileSync("cost.csv", results.csv());
969 * }
970 *
971 * main();
972 * ```
973 *
974 * Example output (this is printed to `console.log` unless the
975 * {@link CostAnalyzer.Workload.silent} is `true`):
976 * ```text
977 * ✔ aws 128MB queue 15.385s 0.274σ $0.00003921
978 * ✔ aws 192MB queue 10.024s 0.230σ $0.00003576
979 * ✔ aws 256MB queue 8.077s 0.204σ $0.00003779
980 * ▲ ▲ ▲ ▲ ▲ ▲
981 * │ │ │ │ │ │
982 * provider │ mode │ stdev average
983 * │ │ execution estimated
984 * memory │ time cost
985 * size │
986 * average cloud
987 * execution time
988 * ```
989 *
990 * The output lists the provider, memory size, ({@link CommonOptions.mode}),
991 * average time of a single execution of the workload, the standard
992 * deviation (in seconds) of the execution time, and average estimated cost
993 * for a single run of the workload.
994 *
995 * The "execution time" referenced here is not wall clock time, but rather
996 * execution time in the cloud function. The execution time does not include
997 * any time the workload spends waiting locally. If the workload invokes
998 * multiple cloud functions, their execution times will be summed even if
999 * they happen concurrently. This ensures the execution time and cost are
1000 * aligned.
1001 */
1002 export function analyze<T extends object, A extends string>(userWorkload: Workload<T, A>): Promise<Result<T, A>>;
1003 /**
1004 * Cost analyzer results for each workload and configuration.
1005 * @remarks
1006 * The `estimates` property has the cost estimates for each configuration.
1007 * See {@link CostAnalyzer.Estimate}.
1008 * @public
1009 */
1010 export class Result<T extends object, A extends string> {
1011 /** The workload analyzed. */
1012 readonly workload: Required<Workload<T, A>>;
1013 /**
1014 * Cost estimates for each configuration of the workload. See
1015 * {@link CostAnalyzer.Estimate}.
1016 */
1017 readonly estimates: Estimate<A>[];
1018 /** @internal */
1019 constructor(
1020 /** The workload analyzed. */
1021 workload: Required<Workload<T, A>>,
1022 /**
1023 * Cost estimates for each configuration of the workload. See
1024 * {@link CostAnalyzer.Estimate}.
1025 */
1026 estimates: Estimate<A>[]);
1027 /**
1028 * Comma-separated output of cost analyzer. One line per cost analyzer
1029 * configuration.
1030 * @remarks
1031 * The columns are:
1032 *
1033 * - `memory`: The memory size allocated.
1034 *
1035 * - `cloud`: The cloud provider.
1036 *
1037 * - `mode`: See {@link CommonOptions.mode}.
1038 *
1039 * - `options`: A string summarizing other faast.js options applied to the
1040 * `workload`. See {@link CommonOptions}.
1041 *
1042 * - `completed`: Number of repetitions that successfully completed.
1043 *
1044 * - `errors`: Number of invocations that failed.
1045 *
1046 * - `retries`: Number of retries that were attempted.
1047 *
1048 * - `cost`: The average cost of executing the workload once.
1049 *
1050 * - `executionTime`: the aggregate time spent executing on the provider for
1051 * all cloud function invocations in the workload. This is averaged across
1052 * repetitions.
1053 *
1054 * - `executionTimeStdev`: The standard deviation of `executionTime`.
1055 *
1056 * - `billedTime`: the same as `exectionTime`, except rounded up to the next
1057 * 100ms for each invocation. Usually very close to `executionTime`.
1058 */
1059 csv(): string;
1060 }
1061}
1062
1063/**
1064 * A line item in the cost estimate, including the resource usage metric
1065 * measured and its pricing.
1066 * @public
1067 */
1068export declare class CostMetric {
1069 /** The name of the cost metric, e.g. `functionCallDuration` */
1070 readonly name: string;
1071 /** The price in USD per unit measured. */
1072 readonly pricing: number;
1073 /** The name of the units that pricing is measured in for this metric. */
1074 readonly unit: string;
1075 /** The measured value of the cost metric, in units. */
1076 readonly measured: number;
1077 /**
1078 * The plural form of the unit name. By default the plural form will be the
1079 * name of the unit with "s" appended at the end, unless the last letter is
1080 * capitalized, in which case there is no plural form (e.g. "GB").
1081 */
1082 readonly unitPlural?: string;
1083 /**
1084 * An optional comment, usually providing a link to the provider's pricing
1085 * page and other data.
1086 */
1087 readonly comment?: string;
1088 /**
1089 * True if this cost metric is only for informational purposes (e.g. AWS's
1090 * `logIngestion`) and does not contribute cost.
1091 */
1092 readonly informationalOnly?: boolean;
1093 /** @internal */
1094 constructor(arg: PropertiesExcept<CostMetric, AnyFunction>);
1095 /**
1096 * The cost contribution of this cost metric. Equal to
1097 * {@link CostMetric.pricing} * {@link CostMetric.measured}.
1098 */
1099 cost(): number;
1100 /**
1101 * Return a string with the cost estimate for this metric, omitting
1102 * comments.
1103 */
1104 describeCostOnly(): string;
1105 /** Describe this cost metric, including comments. */
1106 toString(): string;
1107}
1108
1109/**
1110 * A summary of the costs incurred by a faast.js module at a point in time.
1111 * Output of {@link FaastModule.costSnapshot}.
1112 * @remarks
1113 * Cost information provided by faast.js is an estimate. It is derived from
1114 * internal faast.js measurements and not by consulting data provided by your
1115 * cloud provider.
1116 *
1117 * **Faast.js does not guarantee the accuracy of cost estimates.**
1118 *
1119 * **Use at your own risk.**
1120 *
1121 * Example using AWS:
1122 * ```typescript
1123 * const faastModule = await faast("aws", m);
1124 * try {
1125 * // Invoke faastModule.functions.*
1126 * } finally {
1127 * await faastModule.cleanup();
1128 * console.log(`Cost estimate:`);
1129 * console.log(`${await faastModule.costSnapshot()}`);
1130 * }
1131 * ```
1132 *
1133 * AWS example output:
1134 * ```text
1135 * Cost estimate:
1136 * functionCallDuration $0.00002813/second 0.6 second $0.00001688 68.4% [1]
1137 * sqs $0.00000040/request 9 requests $0.00000360 14.6% [2]
1138 * sns $0.00000050/request 5 requests $0.00000250 10.1% [3]
1139 * functionCallRequests $0.00000020/request 5 requests $0.00000100 4.1% [4]
1140 * outboundDataTransfer $0.09000000/GB 0.00000769 GB $0.00000069 2.8% [5]
1141 * logIngestion $0.50000000/GB 0 GB $0 0.0% [6]
1142 * ---------------------------------------------------------------------------------------
1143 * $0.00002467 (USD)
1144 *
1145 * * Estimated using highest pricing tier for each service. Limitations apply.
1146 * ** Does not account for free tier.
1147 * [1]: https://aws.amazon.com/lambda/pricing (rate = 0.00001667/(GB*second) * 1.6875 GB = 0.00002813/second)
1148 * [2]: https://aws.amazon.com/sqs/pricing
1149 * [3]: https://aws.amazon.com/sns/pricing
1150 * [4]: https://aws.amazon.com/lambda/pricing
1151 * [5]: https://aws.amazon.com/ec2/pricing/on-demand/#Data_Transfer
1152 * [6]: https://aws.amazon.com/cloudwatch/pricing/ - Log ingestion costs not currently included.
1153 * ```
1154 *
1155 * A cost snapshot contains several {@link CostMetric} values. Each `CostMetric`
1156 * summarizes one component of the overall cost of executing the functions so
1157 * far. Some cost metrics are common to all faast providers, and other metrics
1158 * are provider-specific. The common metrics are:
1159 *
1160 * - `functionCallDuration`: the estimated billed CPU time (rounded to the next
1161 * 100ms) consumed by completed cloud function calls. This is the metric that
1162 * usually dominates cost.
1163 *
1164 * - `functionCallRequests`: the number of invocation requests made. Most
1165 * providers charge for each invocation.
1166 *
1167 * Provider-specific metrics vary. For example, AWS has the following additional
1168 * metrics:
1169 *
1170 * - `sqs`: AWS Simple Queueing Service. This metric captures the number of
1171 * queue requests made to insert and retrieve queued results (each 64kb chunk
1172 * is counted as an additional request). SQS is used even if
1173 * {@link CommonOptions.mode} is not set to `"queue"`, because it is necessary
1174 * for monitoring cloud function invocations.
1175 *
1176 * - `sns`: AWS Simple Notification Service. SNS is used to invoke Lambda
1177 * functions when {@link CommonOptions.mode} is `"queue"`.
1178 *
1179 * - `outboundDataTransfer`: an estimate of the network data transferred out
1180 * from the cloud provider for this faast.js module. This estimate only counts
1181 * data returned from cloud function invocations and infrastructure that
1182 * faast.js sets up. It does not count any outbound data sent by your cloud
1183 * functions that are not known to faast.js. Note that if you run faast.js on
1184 * EC2 in the same region (see {@link AwsOptions.region}), then the data
1185 * transfer costs will be zero (however, the cost snapshot will not include
1186 * EC2 costs). Also note that if your cloud function transfers data from/to S3
1187 * buckets in the same region, there is no cost as long as that data is not
1188 * returned from the function.
1189 *
1190 * - `logIngestion`: this cost metric is always zero for AWS. It is present to
1191 * remind the user that AWS charges for log data ingested by CloudWatch Logs
1192 * that are not measured by faast.js. Log entries may arrive significantly
1193 * after function execution completes, and there is no way for faast.js to
1194 * know exactly how long to wait, therefore it does not attempt to measure
1195 * this cost. In practice, if your cloud functions do not perform extensive
1196 * logging on all invocations, log ingestion costs from faast.js are likely to
1197 * be low or fall within the free tier.
1198 *
1199 * For Google, extra metrics include `outboundDataTransfer` similar to AWS, and
1200 * `pubsub`, which combines costs that are split into `sns` and `sqs` on AWS.
1201 *
1202 * The Local provider has no extra metrics.
1203 *
1204 * Prices are retrieved dynamically from AWS and Google and cached locally.
1205 * Cached prices expire after 24h. For each cost metric, faast.js uses the
1206 * highest price tier to compute estimated pricing.
1207 *
1208 * Cost estimates do not take free tiers into account.
1209 * @public
1210 */
1211export declare class CostSnapshot {
1212 /** The {@link Provider}, e.g. "aws" or "google" */
1213 readonly provider: string;
1214 /**
1215 * The options used to initialize the faast.js module where this cost
1216 * snapshot was generated.
1217 */
1218 readonly options: CommonOptions | AwsOptions | GoogleOptions;
1219 /** The function statistics that were used to compute prices. */
1220 readonly stats: FunctionStats;
1221 /**
1222 * The cost metric components for this cost snapshot. See
1223 * {@link CostMetric}.
1224 */
1225 readonly costMetrics: CostMetric[];
1226 /** @internal */
1227 constructor(
1228 /** The {@link Provider}, e.g. "aws" or "google" */
1229 provider: string,
1230 /**
1231 * The options used to initialize the faast.js module where this cost
1232 * snapshot was generated.
1233 */
1234 options: CommonOptions | AwsOptions | GoogleOptions, stats: FunctionStats, costMetrics?: CostMetric[]);
1235 /** Sum of cost metrics. */
1236 total(): number;
1237 /** A summary of all cost metrics and prices in this cost snapshot. */
1238 toString(): string;
1239 /**
1240 * Comma separated value output for a cost snapshot.
1241 * @remarks
1242 * The format is "metric,unit,pricing,measured,cost,percentage,comment".
1243 *
1244 * Example output:
1245 * ```text
1246 * metric,unit,pricing,measured,cost,percentage,comment
1247 * functionCallDuration,second,0.00002813,0.60000000,0.00001688,64.1% ,"https://aws.amazon.com/lambda/pricing (rate = 0.00001667/(GB*second) * 1.6875 GB = 0.00002813/second)"
1248 * functionCallRequests,request,0.00000020,5,0.00000100,3.8% ,"https://aws.amazon.com/lambda/pricing"
1249 * outboundDataTransfer,GB,0.09000000,0.00000844,0.00000076,2.9% ,"https://aws.amazon.com/ec2/pricing/on-demand/#Data_Transfer"
1250 * sqs,request,0.00000040,13,0.00000520,19.7% ,"https://aws.amazon.com/sqs/pricing"
1251 * sns,request,0.00000050,5,0.00000250,9.5% ,"https://aws.amazon.com/sns/pricing"
1252 * logIngestion,GB,0.50000000,0,0,0.0% ,"https://aws.amazon.com/cloudwatch/pricing/ - Log ingestion costs not currently included."
1253 * ```
1254 */
1255 csv(): string;
1256 /** @internal */
1257 push(metric: CostMetric): void;
1258 /**
1259 * Find a specific cost metric by name.
1260 * @returns a {@link CostMetric} if found, otherwise `undefined`.
1261 */
1262 find(name: string): CostMetric | undefined;
1263}
1264
1265declare interface CpuMeasurement {
1266 stime: number;
1267 utime: number;
1268 elapsed: number;
1269}
1270
1271declare interface CpuMetricsMessage {
1272 kind: "cpumetrics";
1273 callId: CallId_2;
1274 metrics: CpuMeasurement;
1275}
1276
1277declare class Deferred<T = void> {
1278 promise: Promise<T>;
1279 resolve: (arg: T | PromiseLike<T>) => void;
1280 reject: (err?: any) => void;
1281 constructor();
1282}
1283
1284/**
1285 * A function return value with additional detailed information.
1286 * @public
1287 */
1288export declare interface Detail<R> {
1289 /**
1290 * A Promise for the function's return value.
1291 */
1292 value: R;
1293 /**
1294 * The URL of the logs for the specific execution of this function call.
1295 * @remarks
1296 * This is different from the general logUrl from
1297 * {@link FaastModule.logUrl}, which provides a link to the logs for all
1298 * invocations of all functions within that module. Whereas this logUrl is
1299 * only for this specific invocation.
1300 */
1301 logUrl?: string;
1302 /**
1303 * If available, the provider-specific execution identifier for this
1304 * invocation.
1305 * @remarks
1306 * This ID may be added to the log entries for this invocation by the cloud
1307 * provider.
1308 */
1309 executionId?: string;
1310 /**
1311 * If available, the provider-specific instance identifier for this
1312 * invocation.
1313 * @remarks
1314 * This ID refers to the specific container or VM used to execute this
1315 * function invocation. The instance may be reused across multiple
1316 * invocations.
1317 */
1318 instanceId?: string;
1319}
1320
1321declare type ErrorCallback = (err: Error) => Error;
1322
1323declare interface Executor {
1324 wrapper: Wrapper;
1325 logUrl: string;
1326 logStream?: Writable;
1327}
1328
1329declare type ExtractPropertyNamesExceptType<T, U> = {
1330 [K in keyof T]: T[K] extends U ? never : K;
1331}[keyof T];
1332
1333/**
1334 * The main entry point for faast with any provider and only common options.
1335 * @param provider - One of `"aws"`, `"google"`, or `"local"`. See
1336 * {@link Provider}.
1337 * @param fmodule - A module imported with `import * as X from "Y";`. Using
1338 * `require` also works but loses type information.
1339 * @param options - See {@link CommonOptions}.
1340 * @returns See {@link FaastModule}.
1341 * @remarks
1342 * Example of usage:
1343 * ```typescript
1344 * import { faast } from "faastjs";
1345 * import * as mod from "./path/to/module";
1346 * (async () => {
1347 * const faastModule = await faast("aws", mod);
1348 * try {
1349 * const result = await faastModule.functions.func("arg");
1350 * } finally {
1351 * await faastModule.cleanup();
1352 * }
1353 * })();
1354 * ```
1355 * @public
1356 */
1357export declare function faast<M extends object>(provider: Provider, fmodule: M, options?: CommonOptions): Promise<FaastModule<M>>;
1358
1359/**
1360 * The main entry point for faast with AWS provider.
1361 * @param fmodule - A module imported with `import * as X from "Y";`. Using
1362 * `require` also works but loses type information.
1363 * @param options - Most common options are in {@link CommonOptions}.
1364 * Additional AWS-specific options are in {@link AwsOptions}.
1365 * @public
1366 */
1367export declare function faastAws<M extends object>(fmodule: M, options?: AwsOptions): Promise<AwsFaastModule<M>>;
1368
1369/**
1370 * FaastError is a subclass of VError (https://github.com/joyent/node-verror).
1371 * that is thrown by faast.js APIs and cloud function invocations.
1372 * @remarks
1373 * `FaastError` is a subclass of
1374 * {@link https://github.com/joyent/node-verror | VError}, which provides an API
1375 * for nested error handling. The main API is the same as the standard Error
1376 * class, namely the err.message, err.name, and err.stack properties.
1377 *
1378 * Several static methods on {@link FaastError} are inherited from VError:
1379 *
1380 * FaastError.fullStack(err) - property provides a more detailed stack trace
1381 * that include stack traces of causes in the causal chain.
1382 *
1383 * FaastError.info(err) - returns an object with fields `functionName`, `args`,
1384 * and `logUrl`. The `logUrl` property is a URL pointing to the logs for a
1385 * specific invocation that caused the error.`logUrl` will be surrounded by
1386 * whitespace on both sides to ease parsing as a URL by IDEs.
1387 *
1388 * FaastError.hasCauseWithName(err, cause) - returns true if the FaastError or
1389 * any of its causes includes an error with the given name, otherwise false. All
1390 * of the available names are in the enum {@link FaastErrorNames}. For example,
1391 * to detect if a FaastError was caused by a cloud function timeout:
1392 *
1393 * ```typescript
1394 * FaastError.hasCauseWithName(err, FaastErrorNames.ETIMEOUT)
1395 * ```
1396 *
1397 * FaastError.findCauseByName(err, cause) - like FaastError.hasCauseWithName()
1398 * except it returns the Error in the causal chain with the given name instead
1399 * of a boolean, otherwise null.
1400 *
1401 * @public
1402 */
1403export declare class FaastError extends VError {
1404}
1405
1406/**
1407 * Possible FaastError names. See {@link FaastError}. To test for errors
1408 * matching these names, use the static method
1409 * {@link FaastError}.hasCauseWithName().
1410 * @public
1411 */
1412export declare enum FaastErrorNames {
1413 /** Generic error. See {@link FaastError}. */
1414 EGENERIC = "VError",
1415 /** The arguments passed to the cloud function could not be serialized without losing information. */
1416 ESERIALIZE = "FaastSerializationError",
1417 /** The remote cloud function timed out. */
1418 ETIMEOUT = "FaastTimeoutError",
1419 /** The remote cloud function exceeded memory limits. */
1420 EMEMORY = "FaastOutOfMemoryError",
1421 /** The function invocation was cancelled by user request. */
1422 ECANCEL = "FaastCancelError",
1423 /** The exception was thrown by user's remote code, not by faast.js or the cloud provider. */
1424 EEXCEPTION = "UserException",
1425 /** Could not create the remote cloud function or supporting infrastructure. */
1426 ECREATE = "FaastCreateFunctionError",
1427 /** The remote cloud function failed to execute because of limited concurrency. */
1428 ECONCURRENCY = "FaastConcurrencyError"
1429}
1430
1431/**
1432 * The main entry point for faast with Google provider.
1433 * @param fmodule - A module imported with `import * as X from "Y";`. Using
1434 * `require` also works but loses type information.
1435 * @param options - Most common options are in {@link CommonOptions}.
1436 * Additional Google-specific options are in {@link GoogleOptions}.
1437 * @public
1438 */
1439export declare function faastGoogle<M extends object>(fmodule: M, options?: GoogleOptions): Promise<GoogleFaastModule<M>>;
1440
1441/**
1442 * The main entry point for faast with Local provider.
1443 * @param fmodule - A module imported with `import * as X from "Y";`. Using
1444 * `require` also works but loses type information.
1445 * @param options - Most common options are in {@link CommonOptions}.
1446 * Additional Local-specific options are in {@link LocalOptions}.
1447 * @returns a Promise for {@link LocalFaastModule}.
1448 * @public
1449 */
1450export declare function faastLocal<M extends object>(fmodule: M, options?: LocalOptions): Promise<LocalFaastModule<M>>;
1451
1452/**
1453 * The main interface for invoking, cleaning up, and managing faast.js cloud
1454 * functions. Returned by {@link faast}.
1455 * @public
1456 */
1457export declare interface FaastModule<M extends object> {
1458 /** See {@link Provider}. */
1459 provider: Provider;
1460 /**
1461 * Each call of a cloud function creates a separate remote invocation.
1462 * @remarks
1463 * The module passed into {@link faast} or its provider-specific variants
1464 * ({@link faastAws}, {@link faastGoogle}, and {@link faastLocal}) is mapped
1465 * to a {@link ProxyModule} version of the module, which performs the
1466 * following mapping:
1467 *
1468 * - All function exports that are generators are mapped to async
1469 * generators.
1470 *
1471 * - All function exports that return async generators are preserved as-is.
1472 *
1473 * - All function exports that return promises have their type signatures
1474 * preserved as-is.
1475 *
1476 * - All function exports that return type T, where T is not a Promise,
1477 * Generator, or AsyncGenerator, are mapped to functions that return
1478 * Promise<T>. Argument types are preserved as-is.
1479 *
1480 * - All non-function exports are omitted in the remote module.
1481 *
1482 * Arguments and return values are serialized with `JSON.stringify` when
1483 * cloud functions are called, therefore what is received on the remote side
1484 * might not match what was sent. Faast.js attempts to detect nonsupported
1485 * arguments on a best effort basis.
1486 *
1487 * If the cloud function throws an exception or rejects its promise with an
1488 * instance of `Error`, then the function will reject with
1489 * {@link FaastError} on the local side. If the exception or rejection
1490 * resolves to any value that is not an instance of `Error`, the remote
1491 * function proxy will reject with the value of
1492 * `JSON.parse(JSON.stringify(err))`.
1493 *
1494 * Arguments and return values have size limitations that vary by provider
1495 * and mode:
1496 *
1497 * - AWS: 256KB in queue mode, 6MB arguments and 256KB return values in https mode. See
1498 * {@link https://docs.aws.amazon.com/lambda/latest/dg/limits.html | AWS Lambda Limits}.
1499 *
1500 * - Google: 10MB in https and queue modes. See
1501 * {@link https://cloud.google.com/functions/quotas | Google Cloud Function Quotas}.
1502 *
1503 * - Local: limited only by available memory and the limits of
1504 * {@link https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback | childprocess.send}.
1505 *
1506 * Note that payloads may be base64 encoded for some providers and therefore
1507 * different in size than the original payload. Also, some bookkeeping data
1508 * are passed along with arguments and contribute to the size limit.
1509 */
1510 functions: ProxyModule<M>;
1511 /**
1512 * Similar to {@link FaastModule.functions} except each function returns a
1513 * {@link Detail} object
1514 * @remarks
1515 * Advanced users of faast.js may want more information about each function
1516 * invocation than simply the result of the function call. For example, the
1517 * specific logUrl for each invocation, to help with detailed debugging.
1518 * This interface provides a way to get this detailed information.
1519 */
1520 functionsDetail: ProxyModuleDetail<M>;
1521 /**
1522 * Stop the faast.js runtime for this cloud function and clean up ephemeral
1523 * cloud resources.
1524 * @returns a Promise that resolves when the `FaastModule` runtime stops and
1525 * ephemeral resources have been deleted.
1526 * @remarks
1527 * It is best practice to always call `cleanup` when done with a cloud
1528 * function. A typical way to ensure this in normal execution is to use the
1529 * `finally` construct:
1530 *
1531 * ```typescript
1532 * const faastModule = await faast("aws", m);
1533 * try {
1534 * // Call faastModule.functions.*
1535 * } finally {
1536 * // Note the `await`
1537 * await faastModule.cleanup();
1538 * }
1539 * ```
1540 *
1541 * After the cleanup promise resolves, the cloud function instance can no
1542 * longer invoke new calls on {@link FaastModule.functions}. However, other
1543 * methods on {@link FaastModule} are safe to call, such as
1544 * {@link FaastModule.costSnapshot}.
1545 *
1546 * Cleanup also stops statistics events (See {@link FaastModule.off}).
1547 *
1548 * By default, cleanup will delete all ephemeral cloud resources but leave
1549 * behind cached resources for use by future cloud functions. Deleted
1550 * resources typically include cloud functions, queues, and queue
1551 * subscriptions. Logs are not deleted by cleanup.
1552 *
1553 * Note that `cleanup` leaves behind some provider-specific resources:
1554 *
1555 * - AWS: Cloudwatch logs are preserved until the garbage collector in a
1556 * future cloud function instance deletes them. The default log expiration
1557 * time is 24h (or the value of {@link CommonOptions.retentionInDays}). In
1558 * addition, the AWS Lambda IAM role is not deleted by cleanup. This role
1559 * is shared across cloud function instances. Lambda layers are also not
1560 * cleaned up immediately on AWS when {@link CommonOptions.packageJson} is
1561 * used and {@link CommonOptions.useDependencyCaching} is true. Cached
1562 * layers are cleaned up by garbage collection. Also see
1563 * {@link CleanupOptions.deleteCaches}.
1564 *
1565 * - Google: Google Stackdriver automatically deletes log entries after 30
1566 * days.
1567 *
1568 * - Local: Logs are preserved in a temporary directory on local disk.
1569 * Garbage collection in a future cloud function instance will delete logs
1570 * older than 24h.
1571 */
1572 cleanup(options?: CleanupOptions): Promise<void>;
1573 /**
1574 * The URL of logs generated by this cloud function.
1575 * @remarks
1576 * Logs are not automatically downloaded because they cause outbound data
1577 * transfer, which can be expensive. Also, logs may arrive at the logging
1578 * service well after the cloud functions have completed. This log URL
1579 * specifically filters the logs for this cloud function instance.
1580 * Authentication is required to view cloud provider logs.
1581 *
1582 * The local provider returns a `file://` url pointing to a file for logs.
1583 */
1584 logUrl(): string;
1585 /**
1586 * Register a callback for statistics events.
1587 * @remarks
1588 * The callback is invoked once for each cloud function that was invoked
1589 * within the last 1s interval, with a {@link FunctionStatsEvent}
1590 * summarizing the statistics for each function. Typical usage:
1591 *
1592 * ```typescript
1593 * faastModule.on("stats", console.log);
1594 * ```
1595 */
1596 on(name: "stats", listener: (statsEvent: FunctionStatsEvent) => void): void;
1597 /**
1598 * Deregister a callback for statistics events.
1599 * @remarks
1600 * Stops the callback listener from receiving future function statistics
1601 * events. Calling {@link FaastModule.cleanup} also turns off statistics
1602 * events.
1603 */
1604 off(name: "stats", listener: (statsEvent: FunctionStatsEvent) => void): void;
1605 /**
1606 * Get a near real-time cost estimate of cloud function invocations.
1607 * @returns a Promise for a {@link CostSnapshot}.
1608 * @remarks
1609 * A cost snapshot provides a near real-time estimate of the costs of the
1610 * cloud functions invoked. The cost estimate only includes the cost of
1611 * successfully completed calls. Unsuccessful calls may lack the data
1612 * required to provide cost information. Calls that are still in flight are
1613 * not included in the cost snapshot. For this reason, it is typically a
1614 * good idea to get a cost snapshot after awaiting the result of
1615 * {@link FaastModule.cleanup}.
1616 *
1617 * Code example:
1618 *
1619 * ```typescript
1620 * const faastModule = await faast("aws", m);
1621 * try {
1622 * // invoke cloud functions on faastModule.functions.*
1623 * } finally {
1624 * await faastModule.cleanup();
1625 * const costSnapshot = await faastModule.costSnapshot();
1626 * console.log(costSnapshot);
1627 * }
1628 * ```
1629 */
1630 costSnapshot(): Promise<CostSnapshot>;
1631 /**
1632 * Statistics for a specific function or the entire faast.js module.
1633 *
1634 * @param functionName - The name of the function to retrieve statistics
1635 * for. If the function does not exist or has not been invoked, a new
1636 * instance of {@link FunctionStats} is returned with zero values. If
1637 * `functionName` omitted (undefined), then aggregate statistics are
1638 * returned that summarize all cloud functions within this faast.js module.
1639 * @returns an snapshot of {@link FunctionStats} at a point in time.
1640 */
1641 stats(functionName?: string): FunctionStats;
1642}
1643
1644/**
1645 * Implementation of {@link FaastModule}.
1646 * @remarks
1647 * `FaastModuleProxy` provides a unified developer experience for faast.js
1648 * modules on top of provider-specific runtime APIs. Most users will not create
1649 * `FaastModuleProxy` instances themselves; instead use {@link faast}, or
1650 * {@link faastAws}, {@link faastGoogle}, or {@link faastLocal}.
1651 * `FaastModuleProxy` implements the {@link FaastModule} interface, which is the
1652 * preferred public interface for faast modules. `FaastModuleProxy` can be used
1653 * to access provider-specific details and state, and is useful for deeper
1654 * testing.
1655 * @public
1656 */
1657export declare class FaastModuleProxy<M extends object, O extends CommonOptions, S> implements FaastModule<M> {
1658 private impl;
1659 /** @internal */
1660 readonly state: S;
1661 private fmodule;
1662 private modulePath;
1663 /** The options set for this instance, which includes default values. */
1664 readonly options: Required<CommonOptions>;
1665 /** The {@link Provider}, e.g. "aws" or "google". */
1666 provider: Provider;
1667 /** {@inheritdoc FaastModule.functions} */
1668 functions: ProxyModule<M>;
1669 /** {@inheritdoc FaastModule.functionsDetail} */
1670 functionsDetail: ProxyModuleDetail<M>;
1671 /** @internal */
1672 private _stats;
1673 private _cpuUsage;
1674 private _memoryLeakDetector;
1675 private _funnel;
1676 private _rateLimiter?;
1677 private _skew;
1678 private _statsTimer?;
1679 private _cleanupHooks;
1680 private _initialInvocationTime;
1681 private _callResultsPending;
1682 private _collectorPump;
1683 private _emitter;
1684 /**
1685 * Constructor
1686 * @internal
1687 */
1688 constructor(impl: ProviderImpl<O, S>,
1689 /** @internal */
1690 state: S, fmodule: M, modulePath: string,
1691 /** The options set for this instance, which includes default values. */
1692 options: Required<CommonOptions>);
1693 /** {@inheritdoc FaastModule.cleanup} */
1694 cleanup(userCleanupOptions?: CleanupOptions): Promise<void>;
1695 /** {@inheritdoc FaastModule.logUrl} */
1696 logUrl(): string;
1697 private startStats;
1698 private stopStats;
1699 /** {@inheritdoc FaastModule.on} */
1700 on(name: "stats", listener: (statsEvent: FunctionStatsEvent) => void): void;
1701 /** {@inheritdoc FaastModule.off} */
1702 off(name: "stats", listener: (statsEvent: FunctionStatsEvent) => void): void;
1703 private withCancellation;
1704 private processResponse;
1705 private invoke;
1706 private lookupFname;
1707 private createCallId;
1708 private wrapGenerator;
1709 private clearPending;
1710 private wrapFunction;
1711 /** {@inheritdoc FaastModule.costSnapshot} */
1712 costSnapshot(): Promise<CostSnapshot>;
1713 /** {@inheritdoc FaastModule.stats} */
1714 stats(functionName?: string): FunctionStats;
1715 private resultCollector;
1716 private adjustCollectorConcurrencyLevel;
1717}
1718
1719declare interface FunctionCall extends CallId {
1720 args: string;
1721 modulePath: string;
1722 name: string;
1723 ResponseQueueId: string;
1724}
1725
1726declare interface FunctionStartedMessage {
1727 kind: "functionstarted";
1728 callId: CallId_2;
1729}
1730
1731/**
1732 * Summary statistics for function invocations.
1733 * @remarks
1734 * ```
1735 * localStartLatency remoteStartLatency executionTime
1736 * ◀──────────────────▶◁ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▷◀──────────▶
1737 *
1738 * ┌───────────────────────────────────┬──────────────────────────────────────┐
1739 * │ │ │
1740 * │ Local │ Cloud Provider │
1741 * │ │ │
1742 * │ ┌─────────┐ │ ┌──────────┐ ┌──────────┐ │
1743 * │ │ │ │ │ │ │ │ │
1744 * │ │ local │ │ │ request │ │ │ │
1745 * │ invoke ────────▶│ queue │────┼──▶│ queue ├────────▶│ │ │
1746 * │ │ │ │ │ │ │ │ │
1747 * │ └─────────┘ │ └──────────┘ │ cloud │ │
1748 * │ │ │ function │ │
1749 * │ ┌─────────┐ │ ┌──────────┐ │ │ │
1750 * │ │ │ │ │ │ │ │ │
1751 * │ result ◀────────│ local │◀───┼───│ response │◀────────│ │ │
1752 * │ │ polling │ │ │ queue │ │ │ │
1753 * │ │ │ │ │ │ │ │ │
1754 * │ └─────────┘ │ └──────────┘ └──────────┘ │
1755 * │ │ │
1756 * └───────────────────────────────────┴──────────────────────────────────────┘
1757 *
1758 * ◁ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▷
1759 * returnLatency ◀───────▶
1760 * sendResponseLatency
1761 * ```
1762 *
1763 * `localStartLatency` and `executionTime` are measured on one machine and are
1764 * free of clock skew. `remoteStartLatency` and `returnLatency` are measured as
1765 * time differences between machines and are subject to much more uncertainty,
1766 * and effects like clock skew.
1767 *
1768 * All times are in milliseconds.
1769 *
1770 * @public
1771 */
1772export declare class FunctionStats {
1773 /**
1774 * Statistics for how long invocations stay in the local queue before being
1775 * sent to the cloud provider.
1776 */
1777 localStartLatency: Statistics;
1778 /**
1779 * Statistics for how long requests take to start execution after being sent
1780 * to the cloud provider. This typically includes remote queueing and cold
1781 * start times. Because this measurement requires comparing timestamps from
1782 * different machines, it is subject to clock skew and other effects, and
1783 * should not be considered highly accurate. It can be useful for detecting
1784 * excessively high latency problems. Faast.js attempt to correct for clock
1785 * skew heuristically.
1786 */
1787 remoteStartLatency: Statistics;
1788 /**
1789 * Statistics for function execution time in milliseconds. This is measured
1790 * as wall clock time inside the cloud function, and does not include the
1791 * time taken to send the response to the response queue. Note that most
1792 * cloud providers round up to the next 100ms for pricing.
1793 */
1794 executionTime: Statistics;
1795 /**
1796 * Statistics for how long it takes to send the response to the response
1797 * queue.
1798 */
1799 sendResponseLatency: Statistics;
1800 /**
1801 * Statistics for how long it takes to return a response from the end of
1802 * execution time to the receipt of the response locally. This measurement
1803 * requires comparing timestamps from different machines, and is subject to
1804 * clock skew and other effects. It should not be considered highly
1805 * accurate. It can be useful for detecting excessively high latency
1806 * problems. Faast.js attempts to correct for clock skew heuristically.
1807 */
1808 returnLatency: Statistics;
1809 /**
1810 * Statistics for amount of time billed. This is similar to
1811 * {@link FunctionStats.executionTime} except each sampled time is rounded
1812 * up to the next 100ms.
1813 */
1814 estimatedBilledTime: Statistics;
1815 /**
1816 * The number of invocations attempted. If an invocation is retried, this
1817 * only counts the invocation once.
1818 */
1819 invocations: number;
1820 /**
1821 * The number of invocations that were successfully completed.
1822 */
1823 completed: number;
1824 /**
1825 * The number of invocation retries attempted. This counts retries
1826 * attempted by faast.js to recover from transient errors, but does not
1827 * count retries by the cloud provider.
1828 */
1829 retries: number;
1830 /**
1831 * The number of invocations that resulted in an error. If an invocation is
1832 * retried, an error is only counted once, no matter how many retries were
1833 * attempted.
1834 */
1835 errors: number;
1836 /**
1837 * Summarize the function stats as a string.
1838 * @returns a string showing the value of completed, retries, errors, and
1839 * mean execution time. This string excludes invocations by default because
1840 * it is often fixed.
1841 */
1842 toString(): string;
1843 /** @internal */
1844 clone(): FunctionStats;
1845}
1846
1847/**
1848 * Summarize statistics about cloud function invocations.
1849 * @public
1850 */
1851export declare class FunctionStatsEvent {
1852 /** The name of the cloud function the statistics are about. */
1853 readonly fn: string;
1854 /** See {@link FunctionStats}. */
1855 readonly stats: FunctionStats;
1856 /**
1857 * @internal
1858 */
1859 constructor(
1860 /** The name of the cloud function the statistics are about. */
1861 fn: string,
1862 /** See {@link FunctionStats}. */
1863 stats: FunctionStats);
1864 /**
1865 * Returns a string summarizing the statistics event.
1866 * @remarks
1867 * The string includes number of completed calls, errors, and retries, and
1868 * the mean execution time for the calls that completed within the last time
1869 * interval (1s).
1870 */
1871 toString(): string;
1872}
1873
1874/**
1875 * The return type of {@link faastGoogle}. See {@link FaastModuleProxy}.
1876 * @public
1877 */
1878export declare type GoogleFaastModule<M extends object = object> = FaastModuleProxy<M, GoogleOptions, GoogleState>;
1879
1880declare class GoogleMetrics {
1881 outboundBytes: number;
1882 pubSubBytes: number;
1883}
1884
1885/**
1886 * Google-specific options for {@link faastGoogle}.
1887 * @public
1888 */
1889export declare interface GoogleOptions extends CommonOptions {
1890 /**
1891 * The region to create resources in. Garbage collection is also limited to
1892 * this region. Default: `"us-central1"`.
1893 */
1894 region?: GoogleRegion;
1895 /**
1896 * Additional options to pass to Google Cloud Function creation. See
1897 * {@link https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#CloudFunction | projects.locations.functions}.
1898 * @remarks
1899 * If you need specialized options, you can pass them to the Google Cloud
1900 * Functions API directly. Note that if you override any settings set by
1901 * faast.js, you may cause faast.js to not work:
1902 *
1903 * ```typescript
1904 * const requestBody: CloudFunctions.Schema$CloudFunction = {
1905 * name,
1906 * entryPoint: "trampoline",
1907 * timeout,
1908 * availableMemoryMb,
1909 * sourceUploadUrl,
1910 * runtime: "nodejs14",
1911 * ...googleCloudFunctionOptions
1912 * };
1913 * ```
1914 *
1915 */
1916 googleCloudFunctionOptions?: cloudfunctions_v1.Schema$CloudFunction;
1917 /** @internal */
1918 _gcWorker?: (resources: GoogleResources, services: GoogleServices) => Promise<void>;
1919}
1920
1921/**
1922 * Valid Google Cloud
1923 * {@link https://cloud.google.com/compute/docs/regions-zones/ | regions}. Only
1924 * some of these
1925 * {@link https://cloud.google.com/functions/docs/locations | regions have Cloud Functions}.
1926 * @public
1927 */
1928export declare type GoogleRegion = "asia-east1" | "asia-east2" | "asia-northeast1" | "asia-south1" | "asia-southeast1" | "australia-southeast1" | "europe-north1" | "europe-west1" | "europe-west2" | "europe-west3" | "europe-west4" | "europe-west6" | "northamerica-northeast1" | "southamerica-east1" | "us-central1" | "us-east1" | "us-east4" | "us-west1" | "us-west2";
1929
1930declare interface GoogleResources {
1931 trampoline: string;
1932 requestQueueTopic?: string;
1933 requestSubscription?: string;
1934 responseQueueTopic?: string;
1935 responseSubscription?: string;
1936 region: string;
1937}
1938
1939declare interface GoogleServices {
1940 readonly cloudFunctions: cloudfunctions_v1.Cloudfunctions;
1941 readonly pubsub: pubsub_v1.Pubsub;
1942 readonly google: GoogleApis;
1943 readonly cloudBilling: cloudbilling_v1.Cloudbilling;
1944}
1945
1946/**
1947 * @internal
1948 */
1949declare interface GoogleState {
1950 resources: GoogleResources;
1951 services: GoogleServices;
1952 url?: string;
1953 project: string;
1954 functionName: string;
1955 metrics: GoogleMetrics;
1956 options: Required<GoogleOptions>;
1957 gcPromise?: Promise<void>;
1958}
1959
1960/**
1961 * Options for the {@link CommonOptions.include} option.
1962 * @public
1963 */
1964export declare interface IncludeOption {
1965 /**
1966 * The path to the directory or glob to add to the cloud function.
1967 */
1968 path: string;
1969 /**
1970 * The working directory if `path` is relative. Defaults to `process.cwd()`.
1971 * For example, if `cwd` is `"foo"` and `path` is `"bar"`, then the
1972 * contents of the directory `foo/bar/` will be added to the remote
1973 * function under the path `bar/`.
1974 */
1975 cwd?: string;
1976}
1977
1978declare interface IteratorResponseMessage extends ResponseContext {
1979 kind: "iterator";
1980 sequence: number;
1981}
1982
1983/**
1984 * Specify {@link throttle} limits. These limits shape the way throttle invokes
1985 * the underlying function.
1986 * @public
1987 */
1988export declare interface Limits {
1989 /**
1990 * The maximum number of concurrent executions of the underlying function to
1991 * allow. Must be supplied, there is no default. Specifying `0` or
1992 * `Infinity` is allowed and means there is no concurrency limit.
1993 */
1994 concurrency: number;
1995 /**
1996 * The maximum number of calls per second to allow to the underlying
1997 * function. Default: no rate limit.
1998 */
1999 rate?: number;
2000 /**
2001 * The maximum number of calls to the underlying function to "burst" -- e.g.
2002 * the number that can be issued immediately as long as the rate limit is
2003 * not exceeded. For example, if rate is 5 and burst is 5, and 10 calls are
2004 * made to the throttled function, 5 calls are made immediately and then
2005 * after 1 second, another 5 calls are made immediately. Setting burst to 1
2006 * means calls are issued uniformly every `1/rate` seconds. If `rate` is not
2007 * specified, then `burst` does not apply. Default: 1.
2008 */
2009 burst?: number;
2010 /**
2011 * Retry if the throttled function returns a rejected promise. `retry` can
2012 * be a number or a function. If it is a number `N`, then up to `N`
2013 * additional attempts are made in addition to the initial call. If retry is
2014 * a function, it should return `true` if another retry attempt should be
2015 * made, otherwise `false`. The first argument will be the value of the
2016 * rejected promise from the previous call attempt, and the second argument
2017 * will be the number of previous retry attempts (e.g. the first call will
2018 * have value 0). Default: 0 (no retry attempts).
2019 */
2020 retry?: number | ((err: any, retries: number) => boolean);
2021 /**
2022 * If `memoize` is `true`, then every call to the throttled function will be
2023 * saved as an entry in a map from arguments to return value. If same
2024 * arguments are seen again in a future call, the return value is retrieved
2025 * from the Map rather than calling the function again. This can be useful
2026 * for avoiding redundant calls that are expected to return the same results
2027 * given the same arguments.
2028 *
2029 * The arguments will be captured with `JSON.stringify`, therefore types
2030 * that do not stringify uniquely won't be distinguished from each other.
2031 * Care must be taken when specifying `memoize` to ensure avoid incorrect
2032 * results.
2033 */
2034 memoize?: boolean;
2035 /**
2036 * Similar to `memoize` except the map from function arguments to results is
2037 * stored in a persistent cache on disk. This is useful to prevent redundant
2038 * calls to APIs which are expected to return the same results for the same
2039 * arguments, and which are likely to be called across many faast.js module
2040 * instantiations. This is used internally by faast.js for caching cloud
2041 * prices for AWS and Google, and for saving the last garbage collection
2042 * date for AWS. Persistent cache entries expire after a period of time. See
2043 * {@link PersistentCache}.
2044 */
2045 cache?: PersistentCache;
2046 /**
2047 * A promise that, if resolved, causes cancellation of pending throttled
2048 * invocations. This is typically created using `Deferred`. The idea is to
2049 * use the resolving of the promise as an asynchronous signal that any
2050 * pending invocations in this throttled function should be cleared.
2051 * @internal
2052 */
2053 cancel?: Promise<void>;
2054}
2055
2056/**
2057 * The return type of {@link faastLocal}. See {@link FaastModuleProxy}.
2058 * @public
2059 */
2060export declare type LocalFaastModule<M extends object = object> = FaastModuleProxy<M, LocalOptions, LocalState>;
2061
2062/**
2063 * Local provider options for {@link faastLocal}.
2064 *
2065 * @public
2066 */
2067export declare interface LocalOptions extends CommonOptions {
2068 /** @internal */
2069 _gcWorker?: (tempdir: string) => Promise<void>;
2070}
2071
2072/**
2073 * @public
2074 */
2075declare interface LocalState {
2076 /** @internal */
2077 executors: Executor[];
2078 /** @internal */
2079 getExecutor: () => Promise<Executor>;
2080 /** The temporary directory where the local function is deployed. */
2081 tempDir: string;
2082 /** The file:// URL for the local function log file directory. */
2083 logUrl: string;
2084 /** @internal */
2085 gcPromise?: Promise<void>;
2086 /** @internal */
2087 queue: AsyncQueue<Message>;
2088 /** Options used to initialize the local function. */
2089 options: Required<LocalOptions>;
2090}
2091
2092/**
2093 * Faast.js loggers.
2094 * @remarks
2095 * Unless otherwise specified, each log is disabled by default unless the value
2096 * of the DEBUG environment variable is set to the corresponding value. For
2097 * example:
2098 *
2099 * ```
2100 * $ DEBUG=faast:info,faast:provider <cmd>
2101 * $ DEBUG=faast:* <cmd>
2102 * ```
2103 *
2104 * Logs can also be enabled or disabled programmatically:
2105 * ```typescript
2106 * import { log } from "faastjs"
2107 * log.info.enabled = true;
2108 * log.provider.enabled = true;
2109 * ```
2110 *
2111 * Each log outputs specific information:
2112 *
2113 * `info` - General informational logging.
2114 *
2115 * `minimal` - Outputs only basic information like the function name created in
2116 * the cloud.
2117 *
2118 * `warn` - Warnings. Enabled by default.
2119 *
2120 * `gc` - Garbage collection verbose logging.
2121 *
2122 * `leaks` - Memory leak detector warnings for the cloud function. Enabled by
2123 * default.
2124 *
2125 * `calls` - Verbose logging of each faast.js enabled function invocation.
2126 *
2127 * `webpack` - Verbose logging from webpack and packaging details.
2128 *
2129 * `provider` - Verbose logging of each interaction between faast.js runtime and
2130 * the provider-specific implementation.
2131 *
2132 * `awssdk` - Verbose logging of AWS SDK. This can be useful for identifying
2133 * which API calls are failing, retrying, or encountering rate limits.
2134 *
2135 * @public
2136 */
2137export declare const log: {
2138 info: debug_2.Debugger;
2139 minimal: debug_2.Debugger;
2140 warn: debug_2.Debugger;
2141 gc: debug_2.Debugger;
2142 leaks: debug_2.Debugger;
2143 calls: debug_2.Debugger;
2144 webpack: debug_2.Debugger;
2145 provider: debug_2.Debugger;
2146 awssdk: debug_2.Debugger;
2147};
2148
2149declare type Message = PromiseResponseMessage | IteratorResponseMessage | FunctionStartedMessage | CpuMetricsMessage;
2150
2151declare type MessageCallback = (msg: Message) => Promise<void>;
2152
2153declare interface ModuleType {
2154 [name: string]: any;
2155}
2156
2157/**
2158 * A simple persistent key-value store. Used to implement {@link Limits.cache}
2159 * for {@link throttle}.
2160 * @remarks
2161 * Entries can be expired, but are not actually deleted individually. The entire
2162 * cache can be deleted at once. Hence this cache is useful for storing results
2163 * that are expensive to compute but do not change too often (e.g. the
2164 * node_modules folder from an 'npm install' where 'package.json' is not
2165 * expected to change too often).
2166 *
2167 * By default faast.js will use the directory `~/.faastjs` as a local cache to
2168 * store data such as pricing retrieved from cloud APIs, and garbage collection
2169 * information. This directory can be safely deleted if no faast.js instances
2170 * are running.
2171 * @public
2172 */
2173export declare class PersistentCache {
2174 /**
2175 * The directory under the user's home directory that will be used to
2176 * store cached values. The directory will be created if it doesn't
2177 * exist.
2178 */
2179 readonly dirRelativeToHomeDir: string;
2180 /**
2181 * The age (in ms) after which a cached entry is invalid. Default:
2182 * `24*3600*1000` (1 day).
2183 */
2184 readonly expiration: number;
2185 private initialized;
2186 private initialize;
2187 /**
2188 * The directory on disk where cached values are stored.
2189 */
2190 readonly dir: string;
2191 /**
2192 * Construct a new persistent cache, typically used with {@link Limits} as
2193 * part of the arguments to {@link throttle}.
2194 * @param dirRelativeToHomeDir - The directory under the user's home
2195 * directory that will be used to store cached values. The directory will be
2196 * created if it doesn't exist.
2197 * @param expiration - The age (in ms) after which a cached entry is
2198 * invalid. Default: `24*3600*1000` (1 day).
2199 */
2200 constructor(
2201 /**
2202 * The directory under the user's home directory that will be used to
2203 * store cached values. The directory will be created if it doesn't
2204 * exist.
2205 */
2206 dirRelativeToHomeDir: string,
2207 /**
2208 * The age (in ms) after which a cached entry is invalid. Default:
2209 * `24*3600*1000` (1 day).
2210 */
2211 expiration?: number);
2212 private hash;
2213 /**
2214 * Retrieves the value previously set for the given key, or undefined if the
2215 * key is not found.
2216 */
2217 get(key: string): Promise<Buffer | undefined>;
2218 /**
2219 * Set the cache key to the given value.
2220 * @returns a Promise that resolves when the cache entry has been persisted.
2221 */
2222 set(key: string, value: Buffer | string | Uint8Array | Readable | Blob_2): Promise<void>;
2223 /**
2224 * Retrieve all keys stored in the cache, including expired entries.
2225 */
2226 entries(): Promise<string[]>;
2227 /**
2228 * Deletes all cached entries from disk.
2229 * @param leaveEmptyDir - If true, leave the cache directory in place after
2230 * deleting its contents. If false, the cache directory will be removed.
2231 * Default: `true`.
2232 */
2233 clear({ leaveEmptyDir }?: {
2234 leaveEmptyDir?: boolean | undefined;
2235 }): Promise<void>;
2236}
2237
2238declare interface PollResult {
2239 Messages: Message[];
2240 isFullMessageBatch?: boolean;
2241}
2242
2243declare interface PromiseResponseMessage extends ResponseContext {
2244 kind: "promise";
2245}
2246
2247declare type PropertiesExcept<T, X> = Pick<T, ExtractPropertyNamesExceptType<T, X>>;
2248
2249/**
2250 * The type of all supported cloud providers.
2251 * @public
2252 */
2253export declare type Provider = "aws" | "google" | "local";
2254
2255declare interface ProviderImpl<O extends CommonOptions, S> {
2256 name: Provider;
2257 defaults: Required<O>;
2258 initialize(serverModule: string, nonce: UUID, options: Required<O>): Promise<S>;
2259 costSnapshot(state: S, stats: FunctionStats): Promise<CostSnapshot>;
2260 cleanup(state: S, options: Required<CleanupOptions>): Promise<void>;
2261 logUrl(state: S): string;
2262 invoke(state: S, request: FunctionCall, cancel: Promise<void>): Promise<void>;
2263 poll(state: S, cancel: Promise<void>): Promise<PollResult>;
2264 responseQueueId(state: S): string;
2265}
2266
2267/**
2268 * An array of all available provider.
2269 * @public
2270 */
2271export declare const providers: Provider[];
2272
2273/**
2274 * `ProxyModule<M>` is the type of {@link FaastModule.functions}.
2275 * @remarks
2276 * `ProxyModule<M>` maps an imported module's functions to promise-returning or
2277 * async-iteratable versions of those functions. Non-function exports of the
2278 * module are omitted. When invoked, the functions in a `ProxyModule` invoke a
2279 * remote cloud function.
2280 * @public
2281 */
2282export declare type ProxyModule<M> = {
2283 [K in keyof M]: M[K] extends (...args: infer A) => infer R ? (...args: A) => Async<R> : never;
2284};
2285
2286/**
2287 * Similar to {@link ProxyModule} except each function returns a {@link Detail}
2288 * object.
2289 * @remarks
2290 * See {@link FaastModule.functionsDetail}.
2291 * @public
2292 */
2293export declare type ProxyModuleDetail<M> = {
2294 [K in keyof M]: M[K] extends (...args: infer A) => infer R ? (...args: A) => AsyncDetail<R> : never;
2295};
2296
2297declare interface ResponseContext {
2298 type: "fulfill" | "reject";
2299 value: string;
2300 callId: CallId_2;
2301 isErrorObject?: boolean;
2302 remoteExecutionStartTime?: number;
2303 remoteExecutionEndTime?: number;
2304 logUrl?: string;
2305 instanceId?: string;
2306 executionId?: string;
2307 memoryUsage?: NodeJS.MemoryUsage;
2308 timestamp?: number;
2309}
2310
2311/**
2312 * Incrementally updated statistics on a set of values.
2313 * @public
2314 */
2315export declare class Statistics {
2316 /** The number of decimal places to print in {@link Statistics.toString} */
2317 protected printFixedPrecision: number;
2318 /** Number of values observed. */
2319 samples: number;
2320 /** The maximum value observed. Initialized to `Number.NEGATIVE_INFINITY`. */
2321 max: number;
2322 /** The minimum value observed. Initialized to `Number.POSITIVE_INFINITY`. */
2323 min: number;
2324 /** The variance of the values observed. */
2325 variance: number;
2326 /** The standard deviation of the values observed. */
2327 stdev: number;
2328 /** The mean (average) of the values observed. */
2329 mean: number;
2330 /**
2331 * Incrementally track mean, stdev, min, max, of a sequence of values.
2332 * @param printFixedPrecision - The number of decimal places to print in
2333 * {@link Statistics.toString}.
2334 */
2335 constructor(
2336 /** The number of decimal places to print in {@link Statistics.toString} */
2337 printFixedPrecision?: number);
2338 /** @internal */
2339 clone(): Statistics & this;
2340 /**
2341 * Update statistics with a new value in the sequence.
2342 */
2343 update(value: number | undefined): void;
2344 /**
2345 * Print the mean of the observations seen, with the precision specified in
2346 * the constructor.
2347 */
2348 toString(): string;
2349}
2350
2351/**
2352 * A decorator for rate limiting, concurrency limiting, retry, memoization, and
2353 * on-disk caching. See {@link Limits}.
2354 * @remarks
2355 * When programming against cloud services, databases, and other resources, it
2356 * is often necessary to control the rate of request issuance to avoid
2357 * overwhelming the service provider. In many cases the provider has built-in
2358 * safeguards against abuse, which automatically fail requests if they are
2359 * coming in too fast. Some systems don't have safeguards and precipitously
2360 * degrade their service level or fail outright when faced with excessive load.
2361 *
2362 * With faast.js it becomes very easy to (accidentally) generate requests from
2363 * thousands of cloud functions. The `throttle` function can help manage request
2364 * flow without resorting to setting up a separate service. This is in keeping
2365 * with faast.js' zero-ops philosophy.
2366 *
2367 * Usage is simple:
2368 *
2369 * ```typescript
2370 * async function operation() { ... }
2371 * const throttledOperation = throttle({ concurrency: 10, rate: 5 }, operation);
2372 * for(let i = 0; i < 100; i++) {
2373 * // at most 10 concurrent executions at a rate of 5 invocations per second.
2374 * throttledOperation();
2375 * }
2376 * ```
2377 *
2378 * Note that each invocation to `throttle` creates a separate function with a
2379 * separate limits. Therefore it is likely that you want to use `throttle` in a
2380 * global context, not within a dynamic context:
2381 *
2382 * ```typescript
2383 * async function operation() { ... }
2384 * for(let i = 0; i < 100; i++) {
2385 * // WRONG - each iteration creates a separate throttled function that's only called once.
2386 * const throttledOperation = throttle({ concurrency: 10, rate: 5 }, operation);
2387 * throttledOperation();
2388 * }
2389 * ```
2390 *
2391 * A better way to use throttle avoids creating a named `operation` function
2392 * altogether, ensuring it cannot be accidentally called without throttling:
2393 *
2394 * ```typescript
2395 * const operation = throttle({ concurrency: 10, rate: 5 }, async () => {
2396 * ...
2397 * });
2398 * ```
2399 *
2400 * Throttle supports functions with arguments automatically infers the correct
2401 * type for the returned function:
2402 *
2403 * ```typescript
2404 * // `operation` inferred to have type (str: string) => Promise<string>
2405 * const operation = throttle({ concurrency: 10, rate: 5 }, async (str: string) => {
2406 * return string;
2407 * });
2408 * ```
2409 *
2410 * In addition to limiting concurrency and invocation rate, `throttle` also
2411 * supports retrying failed invocations, memoizing calls, and on-disk caching.
2412 * See {@link Limits} for details.
2413 *
2414 * @param limits - see {@link Limits}.
2415 * @param fn - The function to throttle. It can take any arguments, but must
2416 * return a Promise (which includes `async` functions).
2417 * @returns Returns a throttled function with the same signature as the argument
2418 * `fn`.
2419 * @public
2420 */
2421export declare function throttle<A extends any[], R>(limits: Limits, fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
2422
2423declare type UUID = string;
2424
2425declare class Wrapper {
2426 executing: boolean;
2427 selected: boolean;
2428 protected verbose: boolean;
2429 protected funcs: ModuleType;
2430 protected child?: childProcess.ChildProcess;
2431 protected childPid?: number;
2432 protected log: (msg: string) => void;
2433 protected queue: AsyncIterableQueue<Message>;
2434 readonly options: Required<WrapperOptions>;
2435 protected monitoringTimer?: NodeJS.Timer;
2436 constructor(fModule: ModuleType, options?: WrapperOptions);
2437 protected lookupFunction(request: object): AnyFunction;
2438 protected stopCpuMonitoring(): void;
2439 protected startCpuMonitoring(pid: number, callId: string): void;
2440 stop(): void;
2441 execute(callingContext: CallingContext, { errorCallback, onMessage, measureCpuUsage }: WrapperExecuteOptions): Promise<void>;
2442 protected logLines: (msg: string) => void;
2443 protected setupChildProcess(): childProcess.ChildProcess;
2444}
2445
2446declare interface WrapperExecuteOptions {
2447 errorCallback?: ErrorCallback;
2448 onMessage: MessageCallback;
2449 measureCpuUsage?: boolean;
2450}
2451
2452declare interface WrapperOptions {
2453 /**
2454 * Logging function for console.log/warn/error output. Only available in
2455 * child process mode. This is mainly useful for debugging the "local"
2456 * mode which runs code locally. In real clouds the logs will end up in the
2457 * cloud logging service (e.g. Cloudwatch Logs, or Google Stackdriver logs).
2458 * Defaults to console.log.
2459 */
2460 wrapperLog?: (msg: string) => void;
2461 childProcess?: boolean;
2462 childProcessMemoryLimitMb?: number;
2463 childProcessTimeoutMs?: number;
2464 childProcessEnvironment?: {
2465 [key: string]: string;
2466 };
2467 childDir?: string;
2468 wrapperVerbose?: boolean;
2469 validateSerialization?: boolean;
2470}
2471
2472export { }
2473
\No newline at end of file