-
Notifications
You must be signed in to change notification settings - Fork 200
/
tasks.ts
166 lines (146 loc) · 5.46 KB
/
tasks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import * as express from "express";
import { DecodedIdToken } from "firebase-admin/auth";
import * as logger from "../../logger";
import * as https from "./https";
import { Expression } from "../../params";
import { ResetValue } from "../options";
/** How a task should be retried in the event of a non-2xx return. */
export interface RetryConfig {
/**
* Maximum number of times a request should be attempted.
* If left unspecified, will default to 3.
*/
maxAttempts?: number | Expression<number> | ResetValue;
/**
* Maximum amount of time for retrying failed task.
* If left unspecified will retry indefinitely.
*/
maxRetrySeconds?: number | Expression<number> | ResetValue;
/**
* The maximum amount of time to wait between attempts.
* If left unspecified will default to 1hr.
*/
maxBackoffSeconds?: number | Expression<number> | ResetValue;
/**
* The maximum number of times to double the backoff between
* retries. If left unspecified will default to 16.
*/
maxDoublings?: number | Expression<number> | ResetValue;
/**
* The minimum time to wait between attempts. If left unspecified
* will default to 100ms.
*/
minBackoffSeconds?: number | Expression<number> | ResetValue;
}
/** How congestion control should be applied to the function. */
export interface RateLimits {
/**
* The maximum number of requests that can be outstanding at a time.
* If left unspecified, will default to 1000.
*/
maxConcurrentDispatches?: number | Expression<number> | ResetValue;
/**
* The maximum number of requests that can be invoked per second.
* If left unspecified, will default to 500.
*/
maxDispatchesPerSecond?: number | Expression<number> | ResetValue;
}
/** Metadata about the authorization used to invoke a function. */
export interface AuthData {
uid: string;
token: DecodedIdToken;
}
/** Metadata about a call to a Task Queue function. */
export interface TaskContext {
/**
* The result of decoding and verifying an ODIC token.
*/
auth?: AuthData;
}
/**
* The request used to call a Task Queue function.
*/
export interface Request<T = any> {
/**
* The parameters used by a client when calling this function.
*/
data: T;
/**
* The result of decoding and verifying an ODIC token.
*/
auth?: AuthData;
}
type v1TaskHandler = (data: any, context: TaskContext) => void | Promise<void>;
type v2TaskHandler<Req> = (request: Request<Req>) => void | Promise<void>;
/** @internal */
export function onDispatchHandler<Req = any>(
handler: v1TaskHandler | v2TaskHandler<Req>
): (req: https.Request, res: express.Response) => Promise<void> {
return async (req: https.Request, res: express.Response): Promise<void> => {
try {
if (!https.isValidRequest(req)) {
logger.error("Invalid request, unable to process.");
throw new https.HttpsError("invalid-argument", "Bad Request");
}
const context: TaskContext = {};
if (!process.env.FUNCTIONS_EMULATOR) {
const authHeader = req.header("Authorization") || "";
const token = authHeader.match(/^Bearer (.*)$/)?.[1];
// Note: this should never happen since task queue functions are guarded by IAM.
if (!token) {
throw new https.HttpsError("unauthenticated", "Unauthenticated");
}
// We skip authenticating the token since tq functions are guarded by IAM.
const authToken = https.unsafeDecodeIdToken(token);
context.auth = {
uid: authToken.uid,
token: authToken,
};
}
const data: Req = https.decode(req.body.data);
if (handler.length === 2) {
await handler(data, context);
} else {
const arg: Request<Req> = {
...context,
data,
};
// For some reason the type system isn't picking up that the handler
// is a one argument function.
await (handler as v2TaskHandler<Req>)(arg);
}
res.status(204).end();
} catch (err) {
let httpErr: https.HttpsError = err;
if (!(err instanceof https.HttpsError)) {
// This doesn't count as an 'explicit' error.
logger.error("Unhandled error", err);
httpErr = new https.HttpsError("internal", "INTERNAL");
}
const { status } = httpErr.httpErrorCode;
const body = { error: httpErr.toJSON() };
res.status(status).send(body);
}
};
}