UNPKG

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