Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

Commit

Permalink
Migrate to Firebase 10 and allow not specifying height
Browse files Browse the repository at this point in the history
  • Loading branch information
azlekov committed Apr 1, 2022
1 parent 7fc6509 commit 2892ff3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
27 changes: 8 additions & 19 deletions src/FirebaseAuthAdapter.ts
Original file line number Diff line number Diff line change
@@ -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"
);
Expand All @@ -18,10 +18,9 @@ export default class FirebaseAuthAdapter {
options: unknown
): Promise<void> {
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;
}
Expand All @@ -40,14 +39,4 @@ export default class FirebaseAuthAdapter {
validateAppId(): Promise<void> {
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));
}
}
}
23 changes: 7 additions & 16 deletions src/FirebaseStorageAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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<void> {
const cacheControl = optional(
"FIREBASE_STORAGE_CACHE_CONTROL",
Expand Down
25 changes: 22 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T extends string | number | boolean | null | undefined>(
name: string,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
};
40 changes: 24 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2892ff3

Please sign in to comment.