chore (backend): remove promisify

This commit is contained in:
naskya 2024-04-24 05:19:03 +09:00
parent e8b39be387
commit 9ef5350a00
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
3 changed files with 12 additions and 19 deletions

View file

@ -1,5 +1,4 @@
import * as fs from "node:fs";
import * as util from "node:util";
import * as fs from "node:fs/promises";
import Logger from "@/services/logger.js";
import { createTemp } from "./create-temp.js";
import { downloadUrl } from "./download-url.js";
@ -16,7 +15,7 @@ export async function downloadTextFile(url: string): Promise<string> {
// write content at URL to temp file
await downloadUrl(url, path);
const text = await util.promisify(fs.readFile)(path, "utf8");
const text = await fs.readFile(path, "utf-8");
return text;
} finally {

View file

@ -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 got, * as Got from "got";
import { config } from "@/config.js";
import { getAgentByHostname, StatusError } from "./fetch.js";
@ -10,8 +9,6 @@ import IPCIDR from "ip-cidr";
import PrivateIp from "private-ip";
import { isValidUrl } from "./is-valid-url.js";
const pipeline = util.promisify(stream.pipeline);
export async function downloadUrl(url: string, path: string): Promise<void> {
if (!isValidUrl(url)) {
throw new StatusError("Invalid URL", 400);
@ -84,7 +81,7 @@ export async function downloadUrl(url: string, path: string): Promise<void> {
});
try {
await pipeline(req, fs.createWriteStream(path));
await stream.pipeline(req, fs.createWriteStream(path));
} catch (e) {
if (e instanceof Got.HTTPError) {
throw new StatusError(

View file

@ -1,7 +1,7 @@
import * as fs from "node:fs";
import * as fs from "node:fs/promises";
import { createReadStream } from "node:fs";
import * as crypto from "node:crypto";
import * as stream from "node:stream";
import * as util from "node:util";
import * as stream from "node:stream/promises";
import { fileTypeFromFile } from "file-type";
import probeImageSize from "probe-image-size";
import isSvg from "is-svg";
@ -9,8 +9,6 @@ import sharp from "sharp";
import { encode } from "blurhash";
import { inspect } from "node:util";
const pipeline = util.promisify(stream.pipeline);
export type FileInfo = {
size: number;
md5: string;
@ -163,7 +161,7 @@ export async function checkSvg(path: string) {
try {
const size = await getFileSize(path);
if (size > 1 * 1024 * 1024) return false;
return isSvg(fs.readFileSync(path));
return isSvg(await fs.readFile(path, "utf-8"));
} catch {
return false;
}
@ -173,8 +171,7 @@ export async function checkSvg(path: string) {
* Get file size
*/
export async function getFileSize(path: string): Promise<number> {
const getStat = util.promisify(fs.stat);
return (await getStat(path)).size;
return (await fs.stat(path)).size;
}
/**
@ -182,7 +179,7 @@ export async function getFileSize(path: string): Promise<number> {
*/
async function calcHash(path: string): Promise<string> {
const hash = crypto.createHash("md5").setEncoding("hex");
await pipeline(fs.createReadStream(path), hash);
await stream.pipeline(createReadStream(path), hash);
return hash.read();
}
@ -196,7 +193,7 @@ async function detectImageSize(path: string): Promise<{
hUnits: string;
orientation?: number;
}> {
const readable = fs.createReadStream(path);
const readable = createReadStream(path);
const imageSize = await probeImageSize(readable);
readable.destroy();
return imageSize;
@ -214,7 +211,7 @@ function getBlurhash(path: string): Promise<string> {
.toBuffer((err, buffer, { width, height }) => {
if (err) return reject(err);
let hash;
let hash: string;
try {
hash = encode(new Uint8ClampedArray(buffer), width, height, 7, 7);