Homelab, Linux & ABAP (~˘▾˘)~

[JavaScript] Copy an array of objects to a new array without any object references

The user tim-montague posted a great overview on the different deep copy techniques on stackoverflow:

In my case I had an array of objects which I wanted to copy without reference, therefore I used the option in the middle.

const copy = JSON.parse(JSON.stringify(myArray))

# Another way using ES6 Syntax
const copy = myArray.map(object => ({ ...object }))

[nodejs] Parsing multipart/mixed response (containing a file stream)

Recently I had to consume an API which returned multipart/mixed data. A response looked like this:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename"test.pdf"

1 0 obj

Content-Type: application/json


There are some node packages for parsing multipart responses, but most can only handle multipart/formData and not multipart/mixed. The most recommended package for multipart/mixed is Dicer, but to be honest, I wasn’t sure how to use it properly. Therefore I built my own parser. Luckely the user idbehold provided a function to parse a response string into an json object here. To get it working I just had to change the regex expressions in the split function. The most important step is to convert the data from the arrayBuffer to a String in binary encoding before parsing.

Also I wrote two helper functions. The first one to parse the boundary string from the Content-Type and the second one to parse the filename from the Content-Dispositon Header of your response.

module.exports = new class multipartMixedParser {

    parse(boundary, buffer) {
        const body = buffer.toString('binary') //toString encodes to utf-8 as default, this would lead to corrupted pdf's     
        return body.split(boundary).reduce((parts, part) => {
            if (part && part !== '--\r\n') {
                const [head, body] = part.trim().split(/\r\n\r\n/g)
                console.log({ body })
                    body: body,
                    headers: head.split(/\r\n/g).reduce((headers, header) => {
                        const [key, value] = header.split(/:\s+/)
                        headers[key.toLowerCase()] = value
                        return headers
                    }, {})
            return parts
        }, [])

    getBoundaryFromResponseHeaders(headers) {
        //example: multipart/mixed;boundary=Boundary_0000000000001 -> --Boundary_0000000000001
        const contentType = headers.get('content-type')
        return '--' + contentType.split("=")[1].split(";")[0]

    getFileNameFromContentDisposition(cd) {
        //example: 'attachment; filename="example.pdf"' -> example.pdf
        return cd.slice(
            cd.indexOf('"') + 1,


And that’s how I’m calling the API and using the multipartMixedParser Class to parse the response. The API I was using is expecting a file as formData and is also returning a file (as part of the multipart/mixed response).
It’s important to get the buffer from the response. If you would use response.getText() it would convert the data to an utf-8 encoded string which will lead to corrupted files.

const btoa = require('btoa')
const FormData = require('form-data')
const fetch = require('node-fetch')
const multipartMixedParser = require('./multipartMixedParser') 

function callAPI(file) {

        const form = new FormData()
        form.append('file', file.content, {
            contentType: file.mediaType,
            filename: file.fileName

        const headers = {
            'Authorization': 'Basic ' + btoa(username + ':' + password),

        const url = /my/api/path

        try {
            const response = await fetch(url, {
                method: 'POST',
                headers: headers,
                body: form
            if (!response.ok) throw new Error(response.statusText)

            //parse the response
            const buffer = await response.buffer() 
            const boundary = multipartMixedParser.getBoundaryFromResponseHeaders(response.headers)

            const result = multipartMixedParser.parse(boundary, buffer)

            // in my case I only returned the file content as buffer and filename 
            return {
                fileContent: Buffer.from(result[0].body, 'binary'),
                fileName: multipartMixedParser.getFileNameFromContentDisposition(result[0].headers["content-disposition"])
        } catch (err) {
            console.log("Error message: " + err.message)


[nodejs] iterate through response header

        const response = await fetch("https://example.com/api")

        for (const [key, value] of response.headers) {
            console.log(key, value)

An alternative would be forEach()

        response.headers.forEach((value, key) => {
            console.log(value, key)

Or using the entries() iterator (ES8)

          const headerIterator = response.headers.entries()

To add a new header just use set()

response.set(key, value)

[JavaScript] Download base64 encoded file within a browser

            const sBase64 = "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3....."
            const arrayBuffer = new Uint8Array([...window.atob(sBase64)].map(char => char.charCodeAt(0)))
            const fileLink = document.createElement('a')

            fileLink.href = window.URL.createObjectURL(new Blob([arrayBuffer]))
            fileLink.setAttribute('download', "example.pdf")

Or use the npm package FileSaver.

import { saveAs } from "file-saver";

const sBase64 = "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3....."
const buffer = Buffer.from(sBase64, "base64")
saveAs(new Blob([buffer]), "example.pdf")

[JavaScript] map an array of objects to return an array of objects

Recently I hat to create an array of objects from another array of objects to change some property names and to enrich it with some values. To iterate over the array I used the map() function. But when trying to return the new object using the brackets {}, the map function instead expects me trying to write a function… Therefore you need to wrap the return object in ()

const aPersons = [{
  id: 1,
  name: 'max'
}, {
  id: 2,
  name: 'peter'

const aResult = aPersons.map(person => ({ value: person.id, text: person.name, someboolean: true }))

[Postman] Visualize base64 image

If you have a service which returns a payload like the following (including a base64 endcoded jpeg) you can view it directly in postman.

        "photo": "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsK\r\nCwsND..............",
        "photoId": "192",
        "mimeType": "image/jpeg"

This can be done with a few lines of code. In Postman navigate to the “Tests” tab:

and insert the following lines:

//output to postman console
console.log("PhotoId: " + pm.response.json()["photoId"]);
console.log("Base64: " + pm.response.json()["photo"]);

//output in visualize tab
let template = `<img src='{{img}}'/>`;

pm.visualizer.set(template, { 
    img: `data:image/jpeg;base64,${pm.response.json()["photo"]}`

In the “Visualize” tab you should now find your image