Skip to content

Commit

Permalink
Omgjør streams til buffer, siden vi ikke utnytter noen av fordelene t…
Browse files Browse the repository at this point in the history
…il streams
  • Loading branch information
hanneolsen committed Oct 9, 2023
1 parent 510207d commit 4c14ec8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning svarer med en liste dokumenter 1`] = `""`;
exports[`/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning svarer med en liste dokumenter 1`] = `
Object {
"data": Array [
98,
105,
110,
195,
166,
114,
],
"type": "Buffer",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NextApiHandler } from "next";
import { v4 as uuidv4 } from "uuid";
import { withSentry } from "@sentry/nextjs";
import { getSession } from "../../../../../lib/auth.utils";
import { Readable, Stream } from "stream";
import { logger } from "@navikt/next-logger";

const audience = `${process.env.SAF_SELVBETJENING_CLUSTER}:teamdokumenthandtering:${process.env.SAF_SELVBETJENING_SCOPE}`;
Expand Down Expand Up @@ -30,7 +29,7 @@ async function hentDokument(
return fetch(endpoint, { headers });
}

export const handleHentDokument: NextApiHandler<Stream> = async (req, res) => {
export const handleHentDokument: NextApiHandler<Buffer> = async (req, res) => {
const session = await getSession(req);
if (!session.token) return res.status(401).end();

Expand All @@ -56,12 +55,8 @@ export const handleHentDokument: NextApiHandler<Stream> = async (req, res) => {
// Senere kan vi teste ut https://www.npmjs.com/package/readable-web-to-node-stream
const arrayBuffer = await dokumentResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const readable = new Readable();
readable._read = () => {}; // _read is required but you can noop it
readable.push(buffer);
readable.push(null);

readable.pipe(res);
return res.status(dokumentResponse.status).send(buffer);
})
.catch((errors) => {
console.log(errors);
Expand Down

0 comments on commit 4c14ec8

Please sign in to comment.