From 9cf12c419d05b583488d645cb32430f70650a03c Mon Sep 17 00:00:00 2001 From: Nattaphong Klinjan Date: Wed, 6 Nov 2024 15:16:40 +0100 Subject: [PATCH] Tester ut readable-web-to-node-stream --- package-lock.json | 29 +++++++++++++++++++ package.json | 1 + .../[dokumentId]/forhandsvisning.ts | 16 ++-------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index e2769798..12f54790 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,6 +37,7 @@ "prop-types": "^15.8.1", "react": "^18.3.1", "react-dom": "^18.3.1", + "readable-web-to-node-stream": "^3.0.2", "swr": "^2.2.5", "unleash-client": "^5.6.1", "uuid": "^10.0.0" @@ -15295,6 +15296,34 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, + "node_modules/readable-web-to-node-stream": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/readable-web-to-node-stream/-/readable-web-to-node-stream-3.0.2.tgz", + "integrity": "sha512-ePeK6cc1EcKLEhJFt/AebMCLL+GgSKhuygrZ/GLaKZYEecIgIECf4UaUuaByiGtzckwR4ain9VzUh95T1exYGw==", + "dependencies": { + "readable-stream": "^3.6.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, + "node_modules/readable-web-to-node-stream/node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/real-require": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/real-require/-/real-require-0.2.0.tgz", diff --git a/package.json b/package.json index f042e701..5f43ab24 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "prop-types": "^15.8.1", "react": "^18.3.1", "react-dom": "^18.3.1", + "readable-web-to-node-stream": "^3.0.2", "swr": "^2.2.5", "unleash-client": "^5.6.1", "uuid": "^10.0.0" diff --git a/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts b/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts index 95a21e52..5f804678 100644 --- a/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts +++ b/src/pages/api/dokumenter/[journalpostId]/[dokumentId]/forhandsvisning.ts @@ -1,7 +1,7 @@ import { logger } from "@navikt/next-logger"; +import { ReadableWebToNodeStream } from "readable-web-to-node-stream"; import { v4 as uuidv4 } from "uuid"; import { getSession } from "../../../../../lib/auth.utils"; -import { Readable } from "stream"; const audience = `${process.env.SAF_SELVBETJENING_CLUSTER}:teamdokumenthandtering:${process.env.SAF_SELVBETJENING_SCOPE}`; @@ -29,18 +29,8 @@ const handleHentDokument = async (req, res) => { try { const response = await fetch(requestUrl, requestHeaders); - - // @ts-ignore // Ignorer respons.body - const stream = Readable.fromWeb(response.body); - - stream.pipe(res); - stream.on("error", (err) => { - res.status(500).send(`Feil ved streaming av SAF dokument: ${err}`); - }); - - stream.on("end", () => { - res.end(); - }); + const readableWebStream = response.body; + return new ReadableWebToNodeStream(readableWebStream); } catch (errors) { logger.error(`Feil fra SAF med call-id ${callId}: ${errors}`); return res.status(500).send(errors);