diff --git a/README.md b/README.md index 61dba1d..4f53459 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ $ export FIREBASE_THUMBNAILS_SIZES="64x64,128x128,256x256" If you want to resize to fit a specific width or height use the following syntax: ```bash -$ export FIREBASE_THUMBNAILS_SIZES="64x0,128x0,256x256" +$ export FIREBASE_THUMBNAILS_SIZES="64,128x0,256x256" ``` The generated file names will have appended `_thumb_{size}` for example: diff --git a/package.json b/package.json index e5a5413..16c8ea8 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "lib" ], "dependencies": { - "firebase-admin": "^9.12.0", + "firebase-admin": "^10.0.2", "mime": "^3.0.0", "parse-cloud-image": "^1.0.3" } diff --git a/src/FirebaseAuthAdapter.ts b/src/FirebaseAuthAdapter.ts index fbc9c34..2f36447 100644 --- a/src/FirebaseAuthAdapter.ts +++ b/src/FirebaseAuthAdapter.ts @@ -1,12 +1,12 @@ -import * as path from "path"; -import * as admin from "firebase-admin"; -import { required } from "./utils"; +import { cert, getApp, initializeApp } from "firebase-admin/app"; +import { getAuth } from "firebase-admin/auth"; +import { credentials } from "./utils"; export default class FirebaseAuthAdapter { constructor() { - admin.initializeApp( + initializeApp( { - credential: admin.credential.cert(this.credentials()), + credential: cert(credentials()), }, "auth" ); @@ -18,10 +18,9 @@ export default class FirebaseAuthAdapter { options: unknown ): Promise { try { - const decodedToken = await admin - .app("auth") - .auth() - .verifyIdToken(authData.access_token); + const decodedToken = await getAuth(getApp("auth")).verifyIdToken( + authData.access_token + ); if (decodedToken && decodedToken.uid === authData.id) { return; } @@ -40,14 +39,4 @@ export default class FirebaseAuthAdapter { validateAppId(): Promise { return Promise.resolve(); } - - private credentials(): admin.ServiceAccount { - const data = required("FIREBASE_SERVICE_ACCOUNT"); - - try { - return JSON.parse(data); - } catch (e) { - return require(path.resolve(".", data)); - } - } } diff --git a/src/FirebaseStorageAdapter.ts b/src/FirebaseStorageAdapter.ts index 8c63154..dbc23e8 100644 --- a/src/FirebaseStorageAdapter.ts +++ b/src/FirebaseStorageAdapter.ts @@ -6,9 +6,11 @@ import { persists, isImage, generateThumbnails, + credentials, } from "./utils"; -import * as admin from "firebase-admin"; import { Bucket } from "@google-cloud/storage"; +import { cert, initializeApp } from "firebase-admin/app"; +import { getStorage } from "firebase-admin/storage"; export interface File { filename: string; @@ -30,16 +32,15 @@ export default class FirebaseStorageAdapter { false ) as boolean; - this.bucket = admin - .initializeApp( + this.bucket = getStorage( + initializeApp( { - credential: admin.credential.cert(this.credentials()), + credential: cert(credentials()), storageBucket: required("FIREBASE_STORAGE_BUCKET"), }, "storage" ) - .storage() - .bucket(); + ).bucket(); } async createFile( @@ -168,16 +169,6 @@ export default class FirebaseStorageAdapter { return null; } - private credentials(): admin.ServiceAccount { - const data = required("FIREBASE_SERVICE_ACCOUNT"); - - try { - return JSON.parse(data); - } catch (e) { - return require(path.resolve(".", data)); - } - } - private async uploadFile(bucket: Bucket, file: File): Promise { const cacheControl = optional( "FIREBASE_STORAGE_CACHE_CONTROL", diff --git a/src/utils.ts b/src/utils.ts index 4d46e92..33efdb2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,7 @@ import * as mime from "mime"; import { Bucket } from "@google-cloud/storage"; import { File } from "./FirebaseStorageAdapter"; import { ParseImage } from "parse-cloud-image"; +import { ServiceAccount } from "firebase-admin/app"; const optional = ( name: string, @@ -40,8 +41,9 @@ const generateThumbnail = async ( }); const pipeline = new ParseImage(image.filename, image.data, contentType); - const width = parseInt(size.split("x")[0]); - const height = parseInt(size.split("x")[1]); + const sizes = size.split("x"); + const width = sizes.length > 0 ? parseInt(sizes[0]) : 0; + const height = sizes.length > 1 ? parseInt(sizes[1]) : 0; const sharp = await pipeline.process((sharp) => sharp.resize( @@ -67,4 +69,21 @@ const generateThumbnails = async ( } }; -export { optional, required, persists, isImage, generateThumbnails }; +const credentials = (): ServiceAccount => { + const data = required("FIREBASE_SERVICE_ACCOUNT"); + + try { + return JSON.parse(data); + } catch (e) { + return require(path.resolve(".", data)); + } +}; + +export { + optional, + required, + persists, + isImage, + generateThumbnails, + credentials, +}; diff --git a/yarn.lock b/yarn.lock index 6b33fb5..fe745a5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -53,11 +53,6 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" -"@firebase/app-types@0.6.3": - version "0.6.3" - resolved "https://registry.yarnpkg.com/@firebase/app-types/-/app-types-0.6.3.tgz#3f10514786aad846d74cd63cb693556309918f4b" - integrity sha512-/M13DPPati7FQHEQ9Minjk1HGLm/4K4gs9bR4rzLCWJg64yGtVC0zNg9gDpkw9yc2cvol/mNFxqTtd4geGrwdw== - "@firebase/app-types@0.7.0": version "0.7.0" resolved "https://registry.yarnpkg.com/@firebase/app-types/-/app-types-0.7.0.tgz#c9e16d1b8bed1a991840b8d2a725fb58d0b5899f" @@ -96,12 +91,13 @@ "@firebase/app-types" "0.7.0" "@firebase/util" "1.4.2" -"@firebase/database-types@^0.7.2": - version "0.7.3" - resolved "https://registry.yarnpkg.com/@firebase/database-types/-/database-types-0.7.3.tgz#819f16dd4c767c864b460004458620f265a3f735" - integrity sha512-dSOJmhKQ0nL8O4EQMRNGpSExWCXeHtH57gGg0BfNAdWcKhC8/4Y+qfKLfWXzyHvrSecpLmO0SmAi/iK2D5fp5A== +"@firebase/database-types@^0.9.3": + version "0.9.6" + resolved "https://registry.yarnpkg.com/@firebase/database-types/-/database-types-0.9.6.tgz#dcea2564ada9afe522d2520d889008f13ad8d2a1" + integrity sha512-E7U28X+FtVtug7EkIkaOXbdP8ghCPno21WWgEiDKsneY28N5WOwccfXqSzHgAAezkR40ht/ZqXlCsUhEpv6JXw== dependencies: - "@firebase/app-types" "0.6.3" + "@firebase/app-types" "0.7.0" + "@firebase/util" "1.5.1" "@firebase/database@0.12.4": version "0.12.4" @@ -129,6 +125,13 @@ dependencies: tslib "^2.1.0" +"@firebase/util@1.5.1": + version "1.5.1" + resolved "https://registry.yarnpkg.com/@firebase/util/-/util-1.5.1.tgz#80ee586c78ed444eb7b96406e00513f279ef5a82" + integrity sha512-ojwPg8sKVcoU/kC1QdTrD+eUDyjQkZyiH9tlouXeZdAeDddCYNvHgIeBQhZt62WIcjlNhy1zro/xdV5nUUU38A== + dependencies: + tslib "^2.1.0" + "@google-cloud/common@^3.8.1": version "3.8.1" resolved "https://registry.yarnpkg.com/@google-cloud/common/-/common-3.8.1.tgz#1313c55bb66df88f69bf7c828135fae25fbd2036" @@ -1704,18 +1707,18 @@ find-up@^5.0.0: locate-path "^6.0.0" path-exists "^4.0.0" -firebase-admin@^9.12.0: - version "9.12.0" - resolved "https://registry.yarnpkg.com/firebase-admin/-/firebase-admin-9.12.0.tgz#d7e889e97c9c31610efbcd131bb6d06a783af757" - integrity sha512-AtA7OH5RbIFGoc0gZOQgaYC6cdjdhZv4w3XgWoupkPKO1HY+0GzixOuXDa75kFeoVyhIyo4PkLg/GAC1dC1P6w== +firebase-admin@^10.0.2: + version "10.0.2" + resolved "https://registry.yarnpkg.com/firebase-admin/-/firebase-admin-10.0.2.tgz#d1142fb40738fa9b62f6625c4e3fc8cbc0ba61c6" + integrity sha512-MLH0SPmC4L0aCHvPjs1KThraru/T84T3hxiPY3uCH7NZEgE/T5n4GwecwU3RcM3X+br75BIBY7qhaR5uCxhdXA== dependencies: "@firebase/database-compat" "^0.1.1" - "@firebase/database-types" "^0.7.2" + "@firebase/database-types" "^0.9.3" "@types/node" ">=12.12.47" dicer "^0.3.0" jsonwebtoken "^8.5.1" jwks-rsa "^2.0.2" - node-forge "^0.10.0" + node-forge "^1.0.0" optionalDependencies: "@google-cloud/firestore" "^4.5.0" "@google-cloud/storage" "^5.3.0" @@ -3055,6 +3058,11 @@ node-forge@^0.10.0: resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3" integrity sha512-PPmu8eEeG9saEUvI97fm4OYxXVB6bFvyNTyiUOBichBpFG8A1Ljw3bY62+5oOjDEMHRnd0Y7HQ+x7uzxOzC6JA== +node-forge@^1.0.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-1.3.1.tgz#be8da2af243b2417d5f646a770663a92b7e9ded3" + integrity sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA== + normalize-package-data@^2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"