From 4c14ec823a65684fe53603ad0bb29fa0349d3ed3 Mon Sep 17 00:00:00 2001 From: Hanne Eilertsen Olsen Date: Mon, 9 Oct 2023 13:11:19 +0200 Subject: [PATCH] =?UTF-8?q?Omgj=C3=B8r=20streams=20til=20buffer,=20siden?= =?UTF-8?q?=20vi=20ikke=20utnytter=20noen=20av=20fordelene=20til=20streams?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forh\303\245ndsvisning.test.ts.snap" | 14 +++++++++++++- .../[dokumentId]/forhandsvisning.ts | 9 ++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git "a/src/__tests__/api/dokumenter/__snapshots__/forh\303\245ndsvisning.test.ts.snap" "b/src/__tests__/api/dokumenter/__snapshots__/forh\303\245ndsvisning.test.ts.snap" index 90240504..9c6d61e3 100644 --- "a/src/__tests__/api/dokumenter/__snapshots__/forh\303\245ndsvisning.test.ts.snap" +++ "b/src/__tests__/api/dokumenter/__snapshots__/forh\303\245ndsvisning.test.ts.snap" @@ -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", +} +`; diff --git a/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts b/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts index 44b98699..f1e2dc87 100644 --- a/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts +++ b/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts @@ -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}`; @@ -30,7 +29,7 @@ async function hentDokument( return fetch(endpoint, { headers }); } -export const handleHentDokument: NextApiHandler = async (req, res) => { +export const handleHentDokument: NextApiHandler = async (req, res) => { const session = await getSession(req); if (!session.token) return res.status(401).end(); @@ -56,12 +55,8 @@ export const handleHentDokument: NextApiHandler = 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);