From cb0fa9a8ab168f5e66ff324d730df7d33d8934d1 Mon Sep 17 00:00:00 2001 From: woxtu Date: Thu, 27 Jul 2023 09:04:19 +0900 Subject: [PATCH] Use promises API (#11351) --- packages/backend/src/core/DownloadService.ts | 8 +++----- packages/backend/src/core/FileInfoService.ts | 10 +++------- packages/backend/src/server/api/ApiCallService.ts | 7 ++----- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/packages/backend/src/core/DownloadService.ts b/packages/backend/src/core/DownloadService.ts index 09039a8b57..1ad7c0137b 100644 --- a/packages/backend/src/core/DownloadService.ts +++ b/packages/backend/src/core/DownloadService.ts @@ -1,6 +1,5 @@ import * as fs from 'node:fs'; -import * as stream from 'node:stream'; -import * as util from 'node:util'; +import * as stream from 'node:stream/promises'; import { Inject, Injectable } from '@nestjs/common'; import ipaddr from 'ipaddr.js'; import chalk from 'chalk'; @@ -14,7 +13,6 @@ import { StatusError } from '@/misc/status-error.js'; import { LoggerService } from '@/core/LoggerService.js'; import type Logger from '@/logger.js'; -const pipeline = util.promisify(stream.pipeline); import { bindThis } from '@/decorators.js'; @Injectable() @@ -102,7 +100,7 @@ export class DownloadService { }); try { - await pipeline(req, fs.createWriteStream(path)); + await stream.pipeline(req, fs.createWriteStream(path)); } catch (e) { if (e instanceof Got.HTTPError) { throw new StatusError(`${e.response.statusCode} ${e.response.statusMessage}`, e.response.statusCode, e.response.statusMessage); @@ -129,7 +127,7 @@ export class DownloadService { // write content at URL to temp file await this.downloadUrl(url, path); - const text = await util.promisify(fs.readFile)(path, 'utf8'); + const text = await fs.promises.readFile(path, 'utf8'); return text; } finally { diff --git a/packages/backend/src/core/FileInfoService.ts b/packages/backend/src/core/FileInfoService.ts index d43575b336..1028d3760e 100644 --- a/packages/backend/src/core/FileInfoService.ts +++ b/packages/backend/src/core/FileInfoService.ts @@ -1,8 +1,7 @@ import * as fs from 'node:fs'; import * as crypto from 'node:crypto'; import { join } from 'node:path'; -import * as stream from 'node:stream'; -import * as util from 'node:util'; +import * as stream from 'node:stream/promises'; import { Injectable } from '@nestjs/common'; import { FSWatcher } from 'chokidar'; import * as fileType from 'file-type'; @@ -16,8 +15,6 @@ import { createTempDir } from '@/misc/create-temp.js'; import { AiService } from '@/core/AiService.js'; import { bindThis } from '@/decorators.js'; -const pipeline = util.promisify(stream.pipeline); - export type FileInfo = { size: number; md5: string; @@ -371,8 +368,7 @@ export class FileInfoService { */ @bindThis public async getFileSize(path: string): Promise { - const getStat = util.promisify(fs.stat); - return (await getStat(path)).size; + return (await fs.promises.stat(path)).size; } /** @@ -381,7 +377,7 @@ export class FileInfoService { @bindThis private async calcHash(path: string): Promise { const hash = crypto.createHash('md5').setEncoding('hex'); - await pipeline(fs.createReadStream(path), hash); + await stream.pipeline(fs.createReadStream(path), hash); return hash.read(); } diff --git a/packages/backend/src/server/api/ApiCallService.ts b/packages/backend/src/server/api/ApiCallService.ts index c4c02e7afe..3e8b9fb727 100644 --- a/packages/backend/src/server/api/ApiCallService.ts +++ b/packages/backend/src/server/api/ApiCallService.ts @@ -1,7 +1,6 @@ import { randomUUID } from 'node:crypto'; -import { pipeline } from 'node:stream'; import * as fs from 'node:fs'; -import { promisify } from 'node:util'; +import * as stream from 'node:stream/promises'; import { Inject, Injectable } from '@nestjs/common'; import { DI } from '@/di-symbols.js'; import { getIpHash } from '@/misc/get-ip-hash.js'; @@ -21,8 +20,6 @@ import type { FastifyRequest, FastifyReply } from 'fastify'; import type { OnApplicationShutdown } from '@nestjs/common'; import type { IEndpointMeta, IEndpoint } from './endpoints.js'; -const pump = promisify(pipeline); - const accessDenied = { message: 'Access denied.', code: 'ACCESS_DENIED', @@ -138,7 +135,7 @@ export class ApiCallService implements OnApplicationShutdown { } const [path] = await createTemp(); - await pump(multipartData.file, fs.createWriteStream(path)); + await stream.pipeline(multipartData.file, fs.createWriteStream(path)); const fields = {} as Record; for (const [k, v] of Object.entries(multipartData.fields)) {