import { Auth } from "firebase-admin/auth"; import * as uca from "unicode-collation-algorithm2"; import * as fw from "./fw"; import * as types from "./types"; import * as crypto from "crypto"; export function eraseUndefined(values: { [index: string]: any }) { Object.keys(values).forEach((key) => { if (values[key] === undefined) { delete values[key]; } }); return values as T; } export function verifyIdToken(idToken: string, auth: Auth) { try { return auth.verifyIdToken(idToken); } catch (error: any) { const errorCode = error.code as string | undefined; // see: https://firebase.google.com/docs/reference/js/firebase.auth.Error // see: https://firebase.google.com/docs/auth/admin/errors if (errorCode != null) { if (errorCode.endsWith("revoked") || errorCode.endsWith("expired")) { throw new types.IdTokenExpiredError(); } } throw new fw.types.Unauthorized(error.message); } } export function getValidAnswerIndex(inputAnswer: string, answers: string[]) { return answers.findIndex((answer) => { return uca.compare(inputAnswer, answer) === 0; }); } export function createRoomId(): string { const seed = "qwertyupasdfghjkxcvbnm1234567890"; const roomIdLength = 8; const roomId = Array.from(crypto.randomBytes(roomIdLength)) .map((n) => seed[n % seed.length]) .join(""); return roomId; } export function createPlayId(roomId: string): string { // 以前はここでsabi加工入っていたけどなくなった return roomId; }