-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea17d79
commit 621a885
Showing
6 changed files
with
178 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Impport dev and log helpers | ||
const { log } = require( '../modules/helpers' ) | ||
const debug = false | ||
|
||
/** | ||
* Return a V1 oncall with runtimes. NOTE: v1 appcheck is enforced through code and not config | ||
* @param {Array.<"high_memory"|"long_timeout"|"keep_warm">} [runtimes] - Array of runtime keys to use | ||
* @param {Function} handler - Function to run | ||
* @returns {Function} - Firebase function | ||
*/ | ||
exports.v1_oncall = ( runtimes=[], handler ) => { | ||
|
||
if( debug ) log( `Creating handler with: `, typeof runtimes, runtimes, typeof handler, handler ) | ||
|
||
const functions = require( "firebase-functions" ) | ||
const { v1_runtimes } = require( './runtimes_settings' ) | ||
|
||
// If the first parameter was a function, return the undecorated handler | ||
if( typeof runtimes === 'function' ) { | ||
if( debug ) log( 'v1_oncall: no runtimes specified, returning undecorated handler' ) | ||
return functions.https.onCall( runtimes ) | ||
} | ||
|
||
// Config the runtimes for this function | ||
const runtime = runtimes.reduce( ( acc, runtime_key ) => ( { ...acc, ...v1_runtimes[ runtime_key ] } ), {} ) | ||
if( debug ) log( 'v1_oncall: returning decorated handler with runtime: ', runtime ) | ||
return functions.runWith( runtime ).https.onCall( handler ) | ||
} | ||
|
||
/** | ||
* Return a V2 oncall with runtimes | ||
* @param {Array.<"long_timeout"|"high_memory"|"keep_warm"|"max_concurrency">} [runtimes] - Array of runtime keys to use, the protected runtime is ALWAYS ADDED | ||
* @param {Function} handler - Firebase function handler | ||
* @returns {Function} - Firebase function | ||
*/ | ||
exports.v2_oncall = ( runtimes=[], handler ) => { | ||
|
||
if( debug ) log( `Creating handler with: `, typeof runtimes, runtimes, typeof handler, handler ) | ||
|
||
const { onCall } = require( "firebase-functions/v2/https" ) | ||
const { v2_runtimes } = require( './runtimes_settings' ) | ||
|
||
// If the first parameter was a function, return the handler as 'protected' firebase oncall | ||
if( typeof runtimes === 'function' ) { | ||
if( debug ) log( 'v2_oncall: no runtimes specified, returning undecorated handler' ) | ||
return onCall( { ...v2_runtimes.protected }, runtimes ) | ||
} | ||
|
||
const runtime = runtimes.reduce( ( acc, runtime_key ) => ( { ...acc, ...v2_runtimes[ runtime_key ] } ), { ...v2_runtimes.protected } ) | ||
if( debug ) log( 'v2_oncall: returning decorated handler with runtime: ', runtime ) | ||
return onCall( runtime, handler ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Return a V1 onRequest with runtimes. NOTE: v1 appcheck is enforced through code and not config | ||
* @param {Array.<"high_memory"|"long_timeout"|"keep_warm">} [runtimes] - Array of runtime keys to use | ||
* @param {Function} handler - Function to run | ||
* @returns {Function} - Firebase function | ||
*/ | ||
exports.v1_onrequest = ( runtimes=[], handler ) => { | ||
|
||
const functions = require( "firebase-functions" ) | ||
const { v1_runtimes } = require( './runtimes_settings' ) | ||
|
||
// If the first parameter was a function, return the undecorated handler | ||
if( typeof runtimes === 'function' ) return functions.https.onRequest( runtimes ) | ||
|
||
// Config the runtimes for this function | ||
const runtime = runtimes.reduce( ( acc, runtime_key ) => ( { ...acc, ...v1_runtimes[ runtime_key ] } ), {} ) | ||
return functions.runWith( runtime ).https.onRequest( handler ) | ||
} | ||
|
||
/** | ||
* Return a V2 onRequest with runtimes | ||
* @param {Array.<"long_timeout"|"high_memory"|"keep_warm"|"max_concurrency">} [runtimes] - Array of runtime keys to use, CORS support is ALWAYS ADDED | ||
* @param {Function} handler - Firebase function handler | ||
* @returns {Function} - Firebase function | ||
*/ | ||
exports.v2_onrequest = ( runtimes=[], handler ) => { | ||
|
||
const { onRequest } = require( "firebase-functions/v2/https" ) | ||
const { v2_runtimes } = require( './runtimes_settings' ) | ||
const runtime_basis = { cors: true } | ||
|
||
// If the first parameter was a function, return the handler as 'protected' firebase oncall | ||
if( typeof runtimes === 'function' ) return onRequest( runtime_basis, runtimes ) | ||
|
||
const runtime = runtimes.reduce( ( acc, runtime_key ) => ( { ...acc, ...v2_runtimes[ runtime_key ] } ), runtime_basis ) | ||
return onRequest( runtime, handler ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @typedef {Object} V1runtimes | ||
* @property {string} high_memory - Allocate high memory to function | ||
* @property {string} long_timeout - Set long timeout to function | ||
* @property {string} keep_warm - Keep function warm | ||
*/ | ||
exports.v1_runtimes = { | ||
high_memory: { memory: '4GB' }, | ||
long_timeout: { timeoutSeconds: 540 }, | ||
keep_warm: { minInstances: 1 }, | ||
} | ||
|
||
/** | ||
* @typedef {Object} V2runtimes | ||
* @property {string} protected - Enforce appcheck | ||
* @property {string} long_timeout - Set long timeout to function | ||
* @property {string} high_memory - Allocate high memory to function | ||
* @property {string} keep_warm - Keep function warm with min instances | ||
* @property {string} max_concurrency - Set max concurrency | ||
*/ | ||
exports.v2_runtimes = { | ||
protected: { enforceAppCheck: true }, | ||
long_timeout: { timeoutSeconds: 540 }, | ||
high_memory: { memory: '4GiB' }, // Note: memory also increases compute power | ||
keep_warm: { minInstances: 1 }, | ||
max_concurrency: { concurrency: 1000 } | ||
} |