Skip to content

Instantly share code, notes, and snippets.

@Bastianowicz
Created February 7, 2023 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bastianowicz/6de941369bf4f22534c2744eabe1b042 to your computer and use it in GitHub Desktop.
Save Bastianowicz/6de941369bf4f22534c2744eabe1b042 to your computer and use it in GitHub Desktop.
Emulator Ready Firebase Function Queues (using Nest.js)
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