@@ -6,6 +6,7 @@ const queryString = require("querystring");
6
6
const path = require ( "path" ) ;
7
7
const getPort = require ( "get-port" ) ;
8
8
const chokidar = require ( "chokidar" ) ;
9
+ const jwtDecode = require ( "jwt-decode" ) ;
9
10
// const chalk = require("chalk");
10
11
const {
11
12
NETLIFYDEVLOG ,
@@ -83,6 +84,26 @@ function promiseCallback(promise, callback) {
83
84
// return path.join(functionPath, `${path.basename(functionPath)}.js`);
84
85
// }
85
86
87
+ function buildClientContext ( headers ) {
88
+ // inject a client context based on auth header, ported over from netlify-lambda (https://github.com/netlify/netlify-lambda/pull/57)
89
+ if ( ! headers . authorization ) return ;
90
+
91
+ const parts = headers . authorization . split ( " " ) ;
92
+ if ( parts . length !== 2 || parts [ 0 ] !== "Bearer" ) return ;
93
+
94
+ try {
95
+ return {
96
+ identity : {
97
+ url : "NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL" ,
98
+ token : "NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN"
99
+ } ,
100
+ user : jwtDecode ( parts [ 1 ] )
101
+ } ;
102
+ } catch ( _ ) {
103
+ // Ignore errors - bearer token is not a JWT, probably not intended for us
104
+ }
105
+ }
106
+
86
107
function createHandler ( dir ) {
87
108
const functions = { } ;
88
109
fs . readdirSync ( dir ) . forEach ( file => {
@@ -170,7 +191,11 @@ function createHandler(dir) {
170
191
} ;
171
192
172
193
const callback = createCallback ( response ) ;
173
- const promise = handler . handler ( lambdaRequest , { } , callback ) ;
194
+ const promise = handler . handler (
195
+ lambdaRequest ,
196
+ { clientContext : buildClientContext ( request . headers ) || { } } ,
197
+ callback
198
+ ) ;
174
199
promiseCallback ( promise , callback ) ;
175
200
} ;
176
201
}
0 commit comments