From df82bccb86fdab509c9de324ad4adfda93f0ddc1 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Mon, 22 May 2023 13:08:08 -0300 Subject: [PATCH] =?UTF-8?q?chore:=20Use=20Assets=E2=80=99=20async=20API=20?= =?UTF-8?q?(#29311)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/meteor/app/livechat/server/livechat.ts | 2 +- apps/meteor/app/ui-master/server/index.js | 2 +- .../meteor/definition/externals/meteor/meteor.d.ts | 8 ++++++++ apps/meteor/server/lib/dataExport/sendViaEmail.ts | 2 +- apps/meteor/server/lib/getMomentLocale.ts | 6 +++--- apps/meteor/server/routes/i18n.ts | 14 ++++++++------ apps/meteor/server/startup/initialData.js | 2 +- 7 files changed, 23 insertions(+), 13 deletions(-) diff --git a/apps/meteor/app/livechat/server/livechat.ts b/apps/meteor/app/livechat/server/livechat.ts index f5f99835f5c..4a3847fecd9 100644 --- a/apps/meteor/app/livechat/server/livechat.ts +++ b/apps/meteor/app/livechat/server/livechat.ts @@ -5,7 +5,7 @@ import { WebApp } from 'meteor/webapp'; import { settings } from '../../settings/server'; import { addServerUrlToIndex } from '../lib/Assets'; -const indexHtmlWithServerURL = addServerUrlToIndex(Assets.getText('livechat/index.html') || ''); +const indexHtmlWithServerURL = addServerUrlToIndex((await Assets.getTextAsync('livechat/index.html')) || ''); WebApp.connectHandlers.use('/livechat', (req, res, next) => { if (!req.url) { diff --git a/apps/meteor/app/ui-master/server/index.js b/apps/meteor/app/ui-master/server/index.js index 84e402045c2..aba90644c61 100644 --- a/apps/meteor/app/ui-master/server/index.js +++ b/apps/meteor/app/ui-master/server/index.js @@ -161,4 +161,4 @@ injectIntoBody( `, ); -injectIntoBody('icons', Assets.getText('public/icons.svg')); +injectIntoBody('icons', await Assets.getTextAsync('public/icons.svg')); diff --git a/apps/meteor/definition/externals/meteor/meteor.d.ts b/apps/meteor/definition/externals/meteor/meteor.d.ts index 7d97c92e422..1b3ca251fe2 100644 --- a/apps/meteor/definition/externals/meteor/meteor.d.ts +++ b/apps/meteor/definition/externals/meteor/meteor.d.ts @@ -6,6 +6,14 @@ type StringifyBuffers = { [P in keyof T]: T[P] extends Buffer ? string : T[P]; }; +declare global { + namespace Assets { + function getBinaryAsync(assetPath: string): Promise; + + function getTextAsync(assetPath: string): Promise; + } +} + declare module 'meteor/meteor' { namespace Meteor { const Streamer: IStreamerConstructor & IStreamer; diff --git a/apps/meteor/server/lib/dataExport/sendViaEmail.ts b/apps/meteor/server/lib/dataExport/sendViaEmail.ts index a858306ad73..6b91949eb40 100644 --- a/apps/meteor/server/lib/dataExport/sendViaEmail.ts +++ b/apps/meteor/server/lib/dataExport/sendViaEmail.ts @@ -53,7 +53,7 @@ export async function sendViaEmail( const localMoment = moment(); if (lang !== 'en') { - const localeFn = getMomentLocale(lang); + const localeFn = await getMomentLocale(lang); if (localeFn) { Function(localeFn).call({ moment }); localMoment.locale(lang); diff --git a/apps/meteor/server/lib/getMomentLocale.ts b/apps/meteor/server/lib/getMomentLocale.ts index 646a70f2c04..a55f2d601f5 100644 --- a/apps/meteor/server/lib/getMomentLocale.ts +++ b/apps/meteor/server/lib/getMomentLocale.ts @@ -1,13 +1,13 @@ import { Meteor } from 'meteor/meteor'; -export function getMomentLocale(locale: string): string | undefined { +export async function getMomentLocale(locale: string): Promise { const localeLower = locale.toLowerCase(); try { - return Assets.getText(`moment-locales/${localeLower}.js`); + return Assets.getTextAsync(`moment-locales/${localeLower}.js`); } catch (error) { try { - return Assets.getText(`moment-locales/${String(localeLower.split('-').shift())}.js`); + return Assets.getTextAsync(`moment-locales/${String(localeLower.split('-').shift())}.js`); } catch (error) { throw new Meteor.Error('moment-locale-not-found', `Moment locale not found: ${locale}`); } diff --git a/apps/meteor/server/routes/i18n.ts b/apps/meteor/server/routes/i18n.ts index f7c43e3c84d..0e12ad3a4e4 100644 --- a/apps/meteor/server/routes/i18n.ts +++ b/apps/meteor/server/routes/i18n.ts @@ -17,18 +17,20 @@ const i18nHandler = async function (req: IncomingMessage, res: ServerResponse) { const { lng } = match.params; - Assets.getText(`i18n/${lng}.i18n.json`, (err: Error, data: Record) => { - if (err || !data) { - res.writeHead(400); - res.end(); - return; + try { + const data = await Assets.getTextAsync(`i18n/${lng}.i18n.json`); + if (!data) { + throw new Error(); } res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Length', data.length); res.writeHead(200); res.end(data); - }); + } catch (e) { + res.writeHead(400); + res.end(); + } }; WebApp.connectHandlers.use('/i18n/', i18nHandler); diff --git a/apps/meteor/server/startup/initialData.js b/apps/meteor/server/startup/initialData.js index ecb9a790d30..b5c81314921 100644 --- a/apps/meteor/server/startup/initialData.js +++ b/apps/meteor/server/startup/initialData.js @@ -38,7 +38,7 @@ Meteor.startup(async function () { await addUserRolesAsync('rocket.cat', ['bot']); - const buffer = Buffer.from(Assets.getBinary('avatars/rocketcat.png')); + const buffer = Buffer.from(await Assets.getBinaryAsync('avatars/rocketcat.png')); const rs = RocketChatFile.bufferToStream(buffer, 'utf8'); const fileStore = FileUpload.getStore('Avatars');