MCPcopy
hub / github.com/streetwriters/notesnook / readEncrypted

Function readEncrypted

apps/web/src/interfaces/fs.ts:191–227  ·  view source on GitHub ↗
(
  filename: string,
  key: SerializedKey,
  cipherData: FileEncryptionMetadataWithOutputType<TOutputFormat>
)

Source from the content-addressed store, hash-verified

189}
190
191async function readEncrypted<TOutputFormat extends DataFormat>(
192 filename: string,
193 key: SerializedKey,
194 cipherData: FileEncryptionMetadataWithOutputType<TOutputFormat>
195) {
196 const fileHandle = await streamablefs.readFile(filename);
197 if (!fileHandle) {
198 console.error(`File not found. (File hash: ${filename})`);
199 return;
200 }
201 const decryptionStream = await NNCrypto.createDecryptionStream(
202 key,
203 cipherData.iv
204 );
205
206 return (
207 cipherData.outputType === "base64" || cipherData.outputType === "text"
208 ? (
209 await consumeReadableStream(
210 fileHandle.readable
211 .pipeThrough(decryptionStream)
212 .pipeThrough(
213 cipherData.outputType === "text"
214 ? new globalThis.TextDecoderStream()
215 : new Base64DecoderStream()
216 )
217 )
218 ).join("")
219 : new Uint8Array(
220 Buffer.concat(
221 await consumeReadableStream(
222 fileHandle.readable.pipeThrough(decryptionStream)
223 )
224 )
225 )
226 ) as Output<TOutputFormat>;
227}
228
229type RequestOptionsWithSignal = RequestOptions & {
230 signal: AbortSignal;

Callers

nothing calls this directly

Calls 4

consumeReadableStreamFunction · 0.90
readFileMethod · 0.65
errorMethod · 0.45

Tested by

no test coverage detected