From 4ee0166bd2e560cbd8976f208049a5cc31c365c0 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 30 Jul 2023 12:48:45 +0200 Subject: [PATCH] Fixed model loading and accessing when function is deployed --- examples/firebase/firebase.json | 6 +++++- examples/firebase/functions/src/index.ts | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/examples/firebase/firebase.json b/examples/firebase/firebase.json index 444a9a6..21fd26f 100644 --- a/examples/firebase/firebase.json +++ b/examples/firebase/firebase.json @@ -9,7 +9,11 @@ "firebase-debug.log", "firebase-debug.*.log" ], - "predeploy": ["npm --prefix \"$RESOURCE_DIR\" run build"] + "predeploy": [ + "cp -r ../models functions/models", + "npm --prefix \"$RESOURCE_DIR\" run build" + ], + "postdeploy": ["rm -rf functions/models"] } ], "storage": { diff --git a/examples/firebase/functions/src/index.ts b/examples/firebase/functions/src/index.ts index c176bf0..de570ec 100644 --- a/examples/firebase/functions/src/index.ts +++ b/examples/firebase/functions/src/index.ts @@ -47,6 +47,9 @@ declare global { SIGNER_CERT: string; SIGNER_KEY: string; SIGNER_KEY_PASSPHRASE: string; + + // reserved, but we use it to discriminate emulator from deploy + FUNCTIONS_EMULATOR: "true" | "false" | undefined; } } } @@ -60,6 +63,21 @@ const storageRef = getStorage().bucket(); export const pass = functions.https.onRequest( async (request: RequestWithBody, response) => { + let modelBasePath: string; + + if (process.env.FUNCTIONS_EMULATOR === "true") { + modelBasePath = "../../models/"; + } else { + /** + * Models are cloned on deploy through + * the commands in `firebase.json` and + * are uploaded along with our program. + * + * When deployed, root folder is the `functions` folder + */ + modelBasePath = "./models/"; + } + try { if (request.headers["content-type"] !== "application/json") { response.status(400); @@ -91,7 +109,7 @@ export const pass = functions.https.onRequest( * Get relevant pass model from model folder (see passkit-generator/examples/models/) * Path seems to get read like the function is in "firebase/" folder and not in "firebase/functions/" */ - model: `../../models/${request.body.passModel}.pass`, + model: `${modelBasePath}${request.body.passModel}.pass`, certificates: { // Assigning certificates from certs folder (you will need to provide these yourself) wwdr: process.env.WWDR,