Firebase Cloud 함수의 콜드 시작 시간을 최소화하려고 할 때 하나의 함수 내에서 사용할 클래스를 가져 오는 방법은 무엇입니까?

DevMike

Firebase Cloud 함수 중 하나에 만 필요한 모듈이있는 경우이 Firebase 자습서 에서는 프로젝트의 다른 모든 함수에 대한 콜드 시작 시간을 최소화하기 위해 필요한 함수 내에서 해당 모듈을 가져올 것을 제안합니다.

이것은 의미가 있지만 함수 내부에 자체 종속성 세트를 포함하는 클래스를 가져올 수도 있습니까?

Bcrypt를 사용할 필요가 있지만 두 가지 기능에서만 사용할 수 있습니다. 따라서 필요하지 않은 다른 모든 클라우드 기능에 대해로드 할 필요가 없습니다.

내 응용 프로그램에는 다음과 같은 가져 오기가 있습니다.

import BcryptTool from './classes/bcrypt'; // <--- only needed in 2 functions

내용은 다음과 같습니다 bcrypt.ts.

import * as bcrypt from 'bcryptjs';
export default class BcryptTool {
 public static hashValue(value: string, rounds: number, callback: (error: Error, hash: string) => void) : void {
      bcrypt.hash(value, rounds, (error:any, hash:any) => {
            callback(error, hash);
      });
 }
 public static compare(value: string, dbHash: string, callback: (error: string | null, match: boolean | null) => void) {
    bcrypt.compare(value, dbHash, (err: Error, match: boolean) => {
        if(match) {
            callback(null, true);
        } else {
            callback('Invalid value match', null);
        }
    });
 }
}

그리고 마지막으로 내 Firebase 클라우드 기능에서 index.ts:

const express = require('express');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({ origin: true });
admin.initializeApp();
const util = express();
const api = express();
...
import BcryptTool from './classes/bcrypt'; // <-- when i import here, calls to its methods within my functions work as expected
...
util.use(cors);
util.post('/verify', async (request: any, response: any) => {

  // according to Doug's answer below i should be able to attempt to import here as a solution using a dynamic import expression like so:

  const BcryptTool = await import('./classes/bcrypt');

  // but the following subsequent call to .compare() fails

  BcryptTool.compare(...)

  // VS Code hinting shows an error: Property 'compare' does not exist on type 'typeof import('FULL/PATH/TO/CLASS/classes/bcrypt')'

});

api.use(cors);
api.post('/endpoint/foo', async (request: any, response: any) => {
  // I do not need Bcrypt here
});
api.post('/endpoint/bar', async (request: any, response: any) => {
  // I do not need Bcrypt here
});

가능하지 않습니까? 내가 잘못하고있는 건가요? *

더그 스티븐슨

물론 TypeScript 코드에서 원하는 곳 어디에서나 비동기 (동적) 가져 오기를 수행 할 수 있습니다. 가져온 심볼은 가져온 범위에 표시되며 다른 위치에는 표시되지 않습니다. 모듈에 포함 된 내용은 중요하지 않습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관