summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/FileWriterStream.ts
diff options
context:
space:
mode:
authorokayurisotto <47853651+okayurisotto@users.noreply.github.com>2024-02-28 15:34:58 +0900
committerGitHub <noreply@github.com>2024-02-28 15:34:58 +0900
commitb7d9d1620161a728e34ab20d8ea99160b3eb4196 (patch)
tree6e59e3cd1a98f8ed96ab943c64223f65b9bb7e14 /packages/backend/src/misc/FileWriterStream.ts
parentenhance(backend): フォロー・フォロワー関連の通知の受信設... (diff)
downloadsharkey-b7d9d1620161a728e34ab20d8ea99160b3eb4196.tar.gz
sharkey-b7d9d1620161a728e34ab20d8ea99160b3eb4196.tar.bz2
sharkey-b7d9d1620161a728e34ab20d8ea99160b3eb4196.zip
refactor(backend): ノートのエクスポート処理でStreams APIを使うように (#13465)
* refactor(backend): ノートのエクスポート処理でStreams APIを使うように * fixup! refactor(backend): ノートのエクスポート処理でStreams APIを使うように `await`忘れにより、ジョブがすぐに完了したことになり削除されてしまっていた。 それによって、`NoteStream`内での`updateProgress`メソッドの呼び出しで、`Missing key for job`のエラーが発生することがあった。 --------- Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
Diffstat (limited to 'packages/backend/src/misc/FileWriterStream.ts')
-rw-r--r--packages/backend/src/misc/FileWriterStream.ts31
1 files changed, 31 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..828851df0e
--- /dev/null
+++ b/packages/backend/src/misc/FileWriterStream.ts
@@ -0,0 +1,31 @@
+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();
+ },
+ });
+ }
+}