-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
0d47877
commit b7d9d16
Showing
3 changed files
with
146 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { TransformStream } from 'node:stream/web'; | ||
|
||
/** | ||
* ストリームに流れてきた各データについて`JSON.stringify()`した上で、それらを一つの配列にまとめる | ||
*/ | ||
export class JsonArrayStream extends TransformStream<unknown, string> { | ||
constructor() { | ||
/** 最初の要素かどうかを変数に記録 */ | ||
let isFirst = true; | ||
|
||
super({ | ||
start(controller) { | ||
controller.enqueue('['); | ||
}, | ||
flush(controller) { | ||
controller.enqueue(']'); | ||
}, | ||
transform(chunk, controller) { | ||
if (isFirst) { | ||
isFirst = false; | ||
} else { | ||
// 妥当なJSON配列にするためには最初以外の要素の前に`,`を挿入しなければならない | ||
controller.enqueue(',\n'); | ||
} | ||
|
||
controller.enqueue(JSON.stringify(chunk)); | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters