Created
February 7, 2023 19:37
-
-
Save Bastianowicz/6de941369bf4f22534c2744eabe1b042 to your computer and use it in GitHub Desktop.
Emulator Ready Firebase Function Queues (using Nest.js)
This file contains hidden or 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
const adminApp = admin.initializeApp(); | |
@Global() | |
@Module({ | |
providers: [ | |
{ | |
provide: Functions, | |
useFactory: () => { | |
if (process.env.FUNCTIONS_EMULATOR) { | |
// since functions emulator does not know queues or the port is blocked because of this very request | |
// we call our function directly | |
const logger = new Logger("Functions Factory"); | |
logger.debug("Recognized Emulator environment. Stubbing Queues"); | |
Object.assign(TaskQueue.prototype, { | |
enqueue: async function (data: never): Promise<void> { | |
return new Promise((resolve, reject) => { | |
const functionNameSegments = this.functionName.split("/"); | |
const functionName = functionNameSegments.length > 1 | |
? functionNameSegments.pop() | |
: this.functionName; | |
const queue = main[functionName]; // <- this references the main.ts declaring the queues and functions | |
if (queue) { | |
const httpBody = JSON.parse(JSON.stringify(data)); | |
return resolve(queue.run(httpBody)); | |
} else { | |
logger.error("No such queue" + this.functionName); | |
reject(); | |
} | |
}); | |
} | |
}); | |
} | |
return getFunctions(adminApp); | |
} | |
} | |
], | |
exports: [ | |
Functions | |
] | |
}) | |
export class UtilModule { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment