Skip to content
Snippets Groups Projects
Unverified Commit cb0fa9a8 authored by woxtu's avatar woxtu Committed by GitHub
Browse files

Use promises API (#11351)

parent 50834580
No related branches found
No related tags found
No related merge requests found
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 {
......
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<number> {
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<string> {
const hash = crypto.createHash('md5').setEncoding('hex');
await pipeline(fs.createReadStream(path), hash);
await stream.pipeline(fs.createReadStream(path), hash);
return hash.read();
}
......
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<string, unknown>;
for (const [k, v] of Object.entries(multipartData.fields)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment