|
| 1 | +import { FirebaseError } from "../../../error"; |
| 2 | +import * as utils from "../../../utils"; |
| 3 | + |
| 4 | +// N.B. The status "deprecated" and "decommmissioned" is informational only. |
| 5 | +// The deprecationDate and decommmissionDate are the canonical values. |
| 6 | +// Updating the definition to "decommissioned", however, will omit the runtime |
| 7 | +// name from firebaseConfig's json schema. |
| 8 | +export type RuntimeStatus = "experimental" | "beta" | "GA" | "deprecated" | "decommissioned"; |
| 9 | + |
| 10 | +type Day = `${number}-${number}-${number}`; |
| 11 | + |
| 12 | +/** Supported languages. All Runtime are a language + version. */ |
| 13 | +export type Language = "nodejs" | "python"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Helper type that is more friendlier than string interpolation everywhere. |
| 17 | + * Unfortunately, as Runtime has literal numbers and RuntimeOf accepts any |
| 18 | + * number, RuntimeOf<L> and Runtime must be intersected. It might help |
| 19 | + * readability to rename Runtime to KnownRuntime so that it reads better to see |
| 20 | + * KnownRuntime & RuntimeOf<"python">. |
| 21 | + */ |
| 22 | +export type RuntimeOf<T extends Language> = `${T}${number}`; |
| 23 | + |
| 24 | +export interface RuntimeData { |
| 25 | + friendly: string; |
| 26 | + status: RuntimeStatus; |
| 27 | + deprecationDate: Day; |
| 28 | + decommissionDate: Day; |
| 29 | +} |
| 30 | + |
| 31 | +// We can neither use the "satisfies" keyword nor the metaprogramming library |
| 32 | +// in this file to ensure RUNTIMES implements the right interfaces, so we must |
| 33 | +// use the copied assertImplements below. Some day these hacks will go away. |
| 34 | +function runtimes<T extends Record<RuntimeOf<Language>, RuntimeData>>(r: T): T { |
| 35 | + return r; |
| 36 | +} |
| 37 | + |
| 38 | +export const RUNTIMES = runtimes({ |
| 39 | + nodejs6: { |
| 40 | + friendly: "Node.js 6", |
| 41 | + status: "decommissioned", |
| 42 | + deprecationDate: "2019-04-17", |
| 43 | + decommissionDate: "2020-08-01", |
| 44 | + }, |
| 45 | + nodejs8: { |
| 46 | + friendly: "Node.js 8", |
| 47 | + status: "decommissioned", |
| 48 | + deprecationDate: "2020-06-05", |
| 49 | + decommissionDate: "2021-02-01", |
| 50 | + }, |
| 51 | + nodejs10: { |
| 52 | + friendly: "Node.js 10", |
| 53 | + status: "GA", |
| 54 | + deprecationDate: "2024-01-30", |
| 55 | + decommissionDate: "2025-01-30", |
| 56 | + }, |
| 57 | + nodejs12: { |
| 58 | + friendly: "Node.js 12", |
| 59 | + status: "GA", |
| 60 | + deprecationDate: "2024-01-30", |
| 61 | + decommissionDate: "2025-01-30", |
| 62 | + }, |
| 63 | + nodejs14: { |
| 64 | + friendly: "Node.js 14", |
| 65 | + status: "GA", |
| 66 | + deprecationDate: "2024-01-30", |
| 67 | + decommissionDate: "2025-01-30", |
| 68 | + }, |
| 69 | + nodejs16: { |
| 70 | + friendly: "Node.js 16", |
| 71 | + status: "GA", |
| 72 | + deprecationDate: "2024-01-30", |
| 73 | + decommissionDate: "2025-01-30", |
| 74 | + }, |
| 75 | + nodejs18: { |
| 76 | + friendly: "Node.js 18", |
| 77 | + status: "GA", |
| 78 | + deprecationDate: "2025-04-30", |
| 79 | + decommissionDate: "2025-10-31", |
| 80 | + }, |
| 81 | + nodejs20: { |
| 82 | + friendly: "Node.js 20", |
| 83 | + status: "GA", |
| 84 | + deprecationDate: "2026-04-30", |
| 85 | + decommissionDate: "2026-10-31", |
| 86 | + }, |
| 87 | + python310: { |
| 88 | + friendly: "Python 3.10", |
| 89 | + status: "GA", |
| 90 | + deprecationDate: "2026-10-04", |
| 91 | + decommissionDate: "2027-04-30", |
| 92 | + }, |
| 93 | + python311: { |
| 94 | + friendly: "Python 3.11", |
| 95 | + status: "GA", |
| 96 | + deprecationDate: "2027-10-24", |
| 97 | + decommissionDate: "2028-04-30", |
| 98 | + }, |
| 99 | + python312: { |
| 100 | + friendly: "Python 3.12", |
| 101 | + status: "GA", |
| 102 | + deprecationDate: "2028-10-02", |
| 103 | + decommissionDate: "2029-04-30", |
| 104 | + }, |
| 105 | +}); |
| 106 | + |
| 107 | +export type Runtime = keyof typeof RUNTIMES & RuntimeOf<Language>; |
| 108 | + |
| 109 | +export type DecommissionedRuntime = { |
| 110 | + [R in keyof typeof RUNTIMES]: (typeof RUNTIMES)[R] extends { status: "decommissioned" } |
| 111 | + ? R |
| 112 | + : never; |
| 113 | +}[keyof typeof RUNTIMES]; |
| 114 | + |
| 115 | +/** Type deduction helper for a runtime string. */ |
| 116 | +export function isRuntime(maybe: string): maybe is Runtime { |
| 117 | + return maybe in RUNTIMES; |
| 118 | +} |
| 119 | + |
| 120 | +/** Type deduction helper to narrow a runtime to a language. */ |
| 121 | +export function runtimeIsLanguage<L extends Language>( |
| 122 | + runtime: Runtime, |
| 123 | + language: L, |
| 124 | +): runtime is Runtime & RuntimeOf<L> { |
| 125 | + return runtime.startsWith(language); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Find the latest supported Runtime for a Language. |
| 130 | + */ |
| 131 | +export function latest<T extends Language>( |
| 132 | + language: T, |
| 133 | + runtimes: Runtime[] = Object.keys(RUNTIMES) as Runtime[], |
| 134 | +): RuntimeOf<T> & Runtime { |
| 135 | + const sorted = runtimes |
| 136 | + .filter((s) => runtimeIsLanguage(s, language)) |
| 137 | + // node8 is less than node20 |
| 138 | + .sort((left, right) => { |
| 139 | + const leftVersion = +left.substring(language.length); |
| 140 | + const rightVersion = +right.substring(language.length); |
| 141 | + if (isNaN(leftVersion) || isNaN(rightVersion)) { |
| 142 | + throw new FirebaseError("Internal error. Runtime or language names are malformed", { |
| 143 | + exit: 1, |
| 144 | + }); |
| 145 | + } |
| 146 | + return leftVersion - rightVersion; |
| 147 | + }); |
| 148 | + const latest = utils.last(sorted); |
| 149 | + if (!latest) { |
| 150 | + throw new FirebaseError( |
| 151 | + `Internal error trying to find the latest supported runtime for ${language}`, |
| 152 | + { exit: 1 }, |
| 153 | + ); |
| 154 | + } |
| 155 | + return latest as RuntimeOf<T> & Runtime; |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Whether a runtime is decommissioned. |
| 160 | + * Accepts now as a parameter to increase testability |
| 161 | + */ |
| 162 | +export function isDecommissioned(runtime: Runtime, now: Date = new Date()): boolean { |
| 163 | + const cutoff = new Date(RUNTIMES[runtime].decommissionDate); |
| 164 | + return cutoff < now; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Prints a warning if a runtime is in or nearing its deprecation time. Throws |
| 169 | + * an error if the runtime is decommissioned. Accepts time as a parameter to |
| 170 | + * increase testability. |
| 171 | + */ |
| 172 | +export function guardVersionSupport(runtime: Runtime, now: Date = new Date()): void { |
| 173 | + const { deprecationDate, decommissionDate } = RUNTIMES[runtime]; |
| 174 | + |
| 175 | + const decommission = new Date(decommissionDate); |
| 176 | + if (now >= decommission) { |
| 177 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-call |
| 178 | + throw new FirebaseError( |
| 179 | + `Runtime ${RUNTIMES[runtime].friendly} was decommissioned on ${decommissionDate}. To deploy ` + |
| 180 | + "you must first upgrade your runtime version.", |
| 181 | + { exit: 1 }, |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + const deprecation = new Date(deprecationDate); |
| 186 | + if (now >= deprecation) { |
| 187 | + utils.logLabeledWarning( |
| 188 | + "functions", |
| 189 | + `Runtime ${RUNTIMES[runtime].friendly} was deprecated on ${deprecationDate} and will be ` + |
| 190 | + `decommissioned on ${decommissionDate}, after which you will not be able ` + |
| 191 | + "to deploy without upgrading. Consider upgrading now to avoid disruption. See " + |
| 192 | + "https://cloud.google.com/functions/docs/runtime-support for full " + |
| 193 | + "details on the lifecycle policy", |
| 194 | + ); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + const warning = new Date(); |
| 199 | + warning.setDate(deprecation.getDate() - 90); |
| 200 | + if (now >= warning) { |
| 201 | + utils.logLabeledWarning( |
| 202 | + "functions", |
| 203 | + `Runtime ${RUNTIMES[runtime].friendly} will be deprecated on ${deprecationDate} and will be ` + |
| 204 | + `decommissioned on ${decommissionDate}, after which you will not be able ` + |
| 205 | + "to deploy without upgrading. Consider upgrading now to avoid disruption. See " + |
| 206 | + "https://cloud.google.com/functions/docs/runtime-support for full " + |
| 207 | + "details on the lifecycle policy", |
| 208 | + ); |
| 209 | + } |
| 210 | +} |
0 commit comments