Git Product home page Git Product logo

Comments (12)

CyrusNajmabadi avatar CyrusNajmabadi commented on August 30, 2024 1

I got this all working by moving over to aws-serverless-express. Turns out it works fine as a libary on azure. The code to get it to work was pretty simple, and i can link to it once it's cleaned up.

from azure-function-express.

lworkman avatar lworkman commented on August 30, 2024 1

That's for the tip @CyrusNajmabadi , I found a different library that implemented aws-serverless-express for azure and it worked like a charm (https://github.com/bigx333/azure-aws-serverless-express).

@yvele I do really like this library, and would switch back once this issue gets fixed. To give you more details, I was trying to serve a ReadableStream from azure blob storage through express and kept running into the headers issue.

from azure-function-express.

kfhak-zb avatar kfhak-zb commented on August 30, 2024 1

maybe it's related in "OutgoingMessage.js"

I changed "OutgoingMessage.js" like below

function convertToBody(body, encoding) {

// return Buffer.isBuffer(body) ? body.toString(encoding) : body; //origin code
return Buffer.isBuffer(body) && "binary" != encoding ? body.toString(encoding) : body;

}

and test like this

app.get("/api/getfile", function (req, res) {

const data = fs.readFileSync("sample.pdf");

res.writeHead(200, {

	"Content-Type": "application/pdf",

	"Content-Disposition": "attachment;filename=output.pdf"

});

res.end(data, "binary");

});

It downloaded the file without "TypeError: Cannot read property 'length' of null"

from azure-function-express.

CyrusNajmabadi avatar CyrusNajmabadi commented on August 30, 2024

The crashing line in question appears to be: https://github.com/nodejs/node/blob/1cb5aed5f596a9cb5b68e3a676222c0c35501a5e/lib/_http_outgoing.js#L137

    } else {
      this.output.unshift(this._header);
      this.outputEncodings.unshift('latin1');
      this.outputCallbacks.unshift(null);
      this.outputSize += this._header.length;  //<-- this line.
      if (typeof this._onPendingData === 'function')
        this._onPendingData(this._header.length);
}

from azure-function-express.

CyrusNajmabadi avatar CyrusNajmabadi commented on August 30, 2024

@yvele I've been trying my best to debug through this and figure out what the right thing to do is. but i keep hitting a brick wall. The node http stack is just insanely complicated. I've tried as well on 10.6 but this is broken there as well.

If you could help out here at all it would be very appreciated. Thanks!

from azure-function-express.

CyrusNajmabadi avatar CyrusNajmabadi commented on August 30, 2024

@maktwin Do you have any ideas either? Do either of you two use static file serving in your FunctionApps? If so, do you do anything special to make it work?

from azure-function-express.

yvele avatar yvele commented on August 30, 2024

Diagnostic

I've overwritten the internal writeHead method to setup context.res.headers (specific to Azure Functions) but it's specific to node 6.

/**
* https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers
* Original implementation: https://github.com/nodejs/node/blob/v6.x/lib/_http_server.js#L160
*
* @param {Object} context Azure Function context
* @param {number} statusCode
* @param {string} statusMessage
* @param {Object} headers
* @this {OutgoingMessage}
*/
function writeHead(context, statusCode, statusMessage, headers) {

Node 8~10 doesn't have this internal writeHead method anymore 🤔

PS: By the way this may be an inspiration https://github.com/awslabs/aws-serverless-express

Possible solution

By inheriting native OutgoingMessage we maybe can skip manually implementing writeHead and maybe we can do something like:

constructor(context) {
    super();
    this.context = context; // Azure Function context
}

// Override by adding things specific to Azure Functions runtime
end(data, encoding) {
  super.end(data, encoding);

  // Return raw values to Azure Function runtime
  const { context } = this;
  context.res.headers = ...;
  context.res.status = ...;
  context.res.body = convertToBody(data, encoding);
  context.res.isRaw = true;
  context.done();
}

We also need unit tests for that (covering all supported node versions).

from azure-function-express.

CyrusNajmabadi avatar CyrusNajmabadi commented on August 30, 2024

Interesting info. Note: i've been using aws-serverless-express very successfully on aws itself. I was going to try something today which first seemed odd, but now i think would b ea good idea. Specifically, i was going to just trying using aws-serverless-express on azure. It's just a library which spawns a local http server, then sends an http request to it. It does expect to take an APIGatewayRequest+serverless.Context as arguments, and it will return produce different result values than azure needs. However, i think i can map azure's inputs to the inputs it needs, and i think i can map its outputs to the ones that azure needs as well.

By actually talking to a real node http server, there is no need to try to simulate an OutgoingMessage/ServerResponse. Instead, the true node will be used. That should hopefully make it so all middleware (like express.static) works fine. All that needs to happen is to properly marshal between the azure and aws input/output types, which i think is very doable...

from azure-function-express.

CyrusNajmabadi avatar CyrusNajmabadi commented on August 30, 2024

PR that switches to just using aws-serverless-express: pulumi/pulumi-cloud#584

We're also considering extracting this into a standalone library . Let me know if you'd be interested in that!

from azure-function-express.

franksmule avatar franksmule commented on August 30, 2024

Same problem, checking the encoding like @kfhak-zb does solves the issue

from azure-function-express.

gunzip avatar gunzip commented on August 30, 2024

any update on this ?

from azure-function-express.

wpitallo avatar wpitallo commented on August 30, 2024

Anyone get this working?

from azure-function-express.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.