Skip to content

Commit 2575355

Browse files
committed
Node.js programming model changes
1 parent 94c6c54 commit 2575355

File tree

26 files changed

+231
-109
lines changed

26 files changed

+231
-109
lines changed

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
module.exports = function (input, context) {
1+
module.exports = function (context, input) {
22
context.log("Node.js script processed queue message '" + input.prop1 + "'");
3-
input.val1++;
43

4+
input.val1++;
55
input.prop1 = "third";
6+
context.bindings.output = input;
67

7-
context.done(null, input);
8+
context.done();
89
}

sample/BlobTrigger/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function (workItem, context) {
1+
module.exports = function (context, workItem) {
22
console.log('Node.js blob trigger function processed work item ' + workItem.id);
33
context.done();
44
}

sample/HttpTrigger/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function (req, context) {
1+
module.exports = function (context, req) {
22
context.log('Node.js HTTP trigger function processed a request. Name=' + req.query.name);
33

44
var headerValue = req.headers['test-header'];
@@ -7,9 +7,20 @@
77
}
88

99
if (typeof req.query.name == 'undefined') {
10-
context.done(null, "Please pass a name on the query string");
10+
context.res = {
11+
status: 400,
12+
body: "Please pass a name on the query string"
13+
};
1114
}
1215
else {
13-
context.done(null, "Hello " + req.query.name);
16+
context.res = {
17+
status: 200,
18+
body: "Hello " + req.query.name
19+
};
20+
context.res.headers = {
21+
'Content-Type': 'text/plain'
22+
};
1423
}
24+
25+
context.done();
1526
}

sample/ManualTrigger/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function (input, context) {
1+
module.exports = function (context, input) {
22
context.log('Node.js manually triggered function called with input ' + input);
33
context.done();
44
}

sample/QueueTrigger/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
module.exports = function (workItem, context) {
1+
module.exports = function (context, workItem) {
22
context.log('Node.js queue trigger function processed work item ' + workItem.id);
3-
4-
context.done(null, workItem);
3+
context.done(null, {
4+
receipt: workItem
5+
});
56
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
module.exports = function (message, context) {
1+
module.exports = function (context, message) {
22
context.log("Node.js ServiceBus queue trigger function processed message '" + JSON.stringify(message) + "'");
33

4-
var output = null;
4+
var result = null;
55
if (message.count < 1)
66
{
77
// write a message back to the queue that this function is triggered on
88
// ensuring that we only loop on this once
99
message.count += 1;
10-
output = {
10+
result = {
1111
message: JSON.stringify(message)
1212
};
1313
}
14+
context.bindings.output = result;
1415

15-
context.done(null, output);
16+
context.done();
1617
}

sample/TimerTrigger/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
module.exports = function (context) {
1+
module.exports = function (context, timerInfo) {
22
var timeStamp = new Date().toISOString();
33
context.log('Node.js timer trigger function ran! ' + timeStamp);
44

5-
var message = {
5+
console.log('PastDue: ' + timerInfo.isPastDue);
6+
console.log('Last: ' + timerInfo.last);
7+
console.log('Next: ' + timerInfo.next);
8+
9+
context.bindings.message = {
610
id: Math.floor(Math.random() * 10000) + 1,
711
notes: "Generated by Timer function"
812
};
913

10-
context.done(null, message);
14+
context.done();
1115
}

sample/WebHook-Generic/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
module.exports = function (body, context) {
1+
module.exports = function (context, data) {
22
context.log('Webhook was triggered!');
3-
context.done(null, 'WebHook processed successfully! ' + body.a);
3+
context.res = {
4+
body: 'WebHook processed successfully! ' + data.a
5+
};
6+
context.done();
47
}

sample/WebHook-GitHub/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
module.exports = function (body, context) {
2-
context.log('GitHub WebHook triggered! ' + body.comment.body);
3-
context.done(null, 'New GitHub comment: ' + body.comment.body);
1+
module.exports = function (context, data) {
2+
context.log('GitHub WebHook triggered! ' + data.comment.body);
3+
context.res = {
4+
body: 'New GitHub comment: ' + data.comment.body
5+
};
6+
context.done();
47
}

src/WebJobs.Script/Binding/HttpBinding.cs

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Net;
78
using System.Net.Http;
9+
using System.Net.Http.Headers;
810
using System.Reflection.Emit;
911
using System.Threading.Tasks;
1012
using Newtonsoft.Json;
@@ -52,9 +54,14 @@ public override async Task BindAsync(BindingContext context)
5254
// TODO: This logic needs to be made more robust
5355
// E.g. we might decide to use a Regex to determine if
5456
// the json is a response body or not
55-
if (jsonObject["status"] != null && jsonObject["body"] != null)
57+
if (jsonObject["body"] != null)
5658
{
57-
HttpStatusCode statusCode = (HttpStatusCode)jsonObject.Value<int>("status");
59+
HttpStatusCode statusCode = HttpStatusCode.OK;
60+
if (jsonObject["status"] != null)
61+
{
62+
statusCode = (HttpStatusCode)jsonObject.Value<int>("status");
63+
}
64+
5865
string body = jsonObject["body"].ToString();
5966

6067
response = new HttpResponseMessage(statusCode);
@@ -65,10 +72,7 @@ public override async Task BindAsync(BindingContext context)
6572
{
6673
foreach (var header in headers)
6774
{
68-
if (header.Value != null)
69-
{
70-
response.Headers.Add(header.Key, header.Value.ToString());
71-
}
75+
AddResponseHeader(response, header);
7276
}
7377
}
7478
}
@@ -106,5 +110,62 @@ public bool CanProcessResult(object result)
106110
{
107111
return result is HttpResponseMessage;
108112
}
113+
114+
private static void AddResponseHeader(HttpResponseMessage response, KeyValuePair<string, JToken> header)
115+
{
116+
if (header.Value != null)
117+
{
118+
DateTimeOffset dateTimeOffset;
119+
switch (header.Key.ToLowerInvariant())
120+
{
121+
// The following content headers must be added to the response
122+
// content header collection
123+
case "content-type":
124+
response.Content.Headers.ContentType = new MediaTypeHeaderValue(header.Value.ToString());
125+
break;
126+
case "content-length":
127+
long contentLength;
128+
if (long.TryParse(header.Value.ToString(), out contentLength))
129+
{
130+
response.Content.Headers.ContentLength = contentLength;
131+
}
132+
break;
133+
case "content-disposition":
134+
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(header.Value.ToString());
135+
break;
136+
case "content-encoding":
137+
case "content-language":
138+
case "content-range":
139+
response.Content.Headers.Add(header.Key, header.Value.ToString());
140+
break;
141+
case "content-location":
142+
Uri uri;
143+
if (Uri.TryCreate(header.Value.ToString(), UriKind.Absolute, out uri))
144+
{
145+
response.Content.Headers.ContentLocation = uri;
146+
}
147+
break;
148+
case "content-md5":
149+
response.Content.Headers.ContentMD5 = header.Value.Value<byte[]>();
150+
break;
151+
case "expires":
152+
if (DateTimeOffset.TryParse(header.Value.ToString(), out dateTimeOffset))
153+
{
154+
response.Content.Headers.Expires = dateTimeOffset;
155+
}
156+
break;
157+
case "last-modified":
158+
if (DateTimeOffset.TryParse(header.Value.ToString(), out dateTimeOffset))
159+
{
160+
response.Content.Headers.LastModified = dateTimeOffset;
161+
}
162+
break;
163+
default:
164+
// All other headers are added directly to the response
165+
response.Headers.Add(header.Key, header.Value.ToString());
166+
break;
167+
}
168+
}
169+
}
109170
}
110171
}

0 commit comments

Comments
 (0)