Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs multipart/x-mixed-replace; boundary=BoundaryString

const webStream = {
    get: function (url, callback) {
        let webClient;

        if (url.startsWith("http://")) {
            webClient = require("http");
        } else if (url.startsWith("https://")) {
            webClient = require("https");
        } else {
            throw "Unsupported protocol.";
        }

        let clientRequest = webClient.get(url, function (response) {
            let context = {
                url: url,
                boundary: "",
                contentType: "",
                contentLength: 0
            };

            let headersCompleted = false;
            let bodyCompleted = false;
            let buffer = null;
            let receivedBodyChunk = 0;

            response.on("data", function (chunk) {
                if (!headersCompleted) {
                    let headers = chunk.toString().split(/
?
/);

                    context.boundary = headers[0].substring(2);
                    context.contentType = headers[1].split(":")[1].trim();
                    context.contentLength = parseInt(headers[2].split(":")[1]);

                    buffer = Buffer.alloc(context.contentLength);

                    headersCompleted = true;
                } else {
                    if (!bodyCompleted) {
                        if (receivedBodyChunk < context.contentLength) {
                            chunk.copy(buffer, receivedBodyChunk, 0, chunk.byteLength);

                            receivedBodyChunk += chunk.byteLength;

                            if (receivedBodyChunk === context.contentLength) {
                                bodyCompleted = true;
                            }
                        }
                    }

                    if (bodyCompleted) {
                        callback(buffer, context);

                        headersCompleted = false;
                        bodyCompleted = false;
                        buffer = null;
                        receivedBodyChunk = 0;
                    }
                }
            });
        });

        return {
            url: url,
            handler: clientRequest,
            on: function (type, listener) {
                clientRequest.on(type, listener);
            },
            abort: function () {
                clientRequest.abort();
            }
        };
    }
};

let stream = webStream.get("http://127.0.0.1:8090/", function (data, context) {
    // data: Received content (Buffer)
    // context: { url, boundary, contentType, contentLength }

    // TODO: Do something here...
});

// stream.abort();

// stream.on("error", function(e) {
//     console.log("Error: " + e.message);
// });
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to set input of time type to current time on initialization 
Javascript :: angular dart router without hash 
Javascript :: jquerybuilder input date 
Javascript :: javascript file access to resources asp.net mvc 
Javascript :: send props from one component to another on button click 
Javascript :: V2271823410017645510 
Javascript :: jquery delete buton 
Javascript :: rangeSlider format price js 
Javascript :: ssh tunnel connect to mongodb in node.js 
Javascript :: cannot create an instance of an abstract class httphandler angular 
Javascript :: javascript Power of a matrix 
Javascript :: site completo com ajax jquery 
Javascript :: grepper add code answer 
Javascript :: ipv6 dual regex angular 
Javascript :: gravity forms vote up or down 
Javascript :: javacript srting 
Javascript :: js run html in blob 
Javascript :: snippets chrome devtools debugging 
Javascript :: why I can not insert image with ajax request 
Javascript :: Merger Douplicate array Object 
Javascript :: javascript loop area 
Javascript :: Decodes a string of data which has been encoded using base-64 encoding - Nodejs 
Javascript :: passar a página atual na url js net core 
Javascript :: cdate ssrs expressions 
Javascript :: rails json exclude nested attribute 
Javascript :: ${product} meaning in react js 
Javascript :: composite key knex 
Javascript :: 7.2. Bracket Notation¶ 
Javascript :: song discord.js 
Javascript :: javascript reassignment 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =