summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/FileWriterStream.ts
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2024-03-02 16:36:49 +0000
committerAmelia Yukii <amelia.yukii@shourai.de>2024-03-02 16:36:49 +0000
commitaf548d05ca821725eabd5a96241d3228b92186f0 (patch)
tree6d23f6739482466abcc71965cd83f9bbfb2c3ae0 /packages/backend/src/misc/FileWriterStream.ts
parentmerge: Fix Images in ReadMe (!445) (diff)
downloadsharkey-af548d05ca821725eabd5a96241d3228b92186f0.tar.gz
sharkey-af548d05ca821725eabd5a96241d3228b92186f0.tar.bz2
sharkey-af548d05ca821725eabd5a96241d3228b92186f0.zip
merge upstream for 2024.2.1
Diffstat (limited to 'packages/backend/src/misc/FileWriterStream.ts')
-rw-r--r--packages/backend/src/misc/FileWriterStream.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/packages/backend/src/misc/FileWriterStream.ts b/packages/backend/src/misc/FileWriterStream.ts
new file mode 100644
index 0000000000..367a8eb560
--- /dev/null
+++ b/packages/backend/src/misc/FileWriterStream.ts
@@ -0,0 +1,36 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and misskey-project
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import * as fs from 'node:fs/promises';
+import type { PathLike } from 'node:fs';
+
+/**
+ * `fs.createWriteStream()`相当のことを行う`WritableStream` (Web標準)
+ */
+export class FileWriterStream extends WritableStream<Uint8Array> {
+ constructor(path: PathLike) {
+ let file: fs.FileHandle | null = null;
+
+ super({
+ start: async () => {
+ file = await fs.open(path, 'a');
+ },
+ write: async (chunk, controller) => {
+ if (file === null) {
+ controller.error();
+ throw new Error();
+ }
+
+ await file.write(chunk);
+ },
+ close: async () => {
+ await file?.close();
+ },
+ abort: async () => {
+ await file?.close();
+ },
+ });
+ }
+}