Comments (12)
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.
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.
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.
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.
@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.
@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.
Diagnostic
I've overwritten the internal writeHead
method to setup context.res.headers
(specific to Azure Functions) but it's specific to node 6.
azure-function-express/src/OutgoingMessage.js
Lines 35 to 45 in 2bd9041
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.
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.
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.
Same problem, checking the encoding like @kfhak-zb does solves the issue
from azure-function-express.
any update on this ?
from azure-function-express.
Anyone get this working?
from azure-function-express.
Related Issues (20)
- writeHead doesn't work as expected when passing headers as second parameter HOT 6
- Not working with Azure Functions V2 beta HOT 16
- Seems not to work with content type application/json HOT 1
- Content-Type doesn't work correctly HOT 7
- Misleading Readme regarding context
- Package hardcoded to only work on Node <7 HOT 4
- Http body already parsed HOT 4
- Broken in GA Functions 2.0 runtime
- sendFile fails with TypeError: Cannot read property 'length' of null HOT 1
- I can not get the examples to work with v1 and v2
- error, warn, info, and verbose log functions are not bind to the sanitized context HOT 2
- Not able to do a POST request having content-type and body HOT 1
- How to log to console before request
- Node engine version HOT 4
- Is this still under active development? HOT 9
- req.context.invocationId HOT 5
- Changelog HOT 3
- Support for multi-part file uploads HOT 11
- Not getting it to work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-function-express.