mirror of
https://github.com/serverless/serverless.git
synced 2026-02-01 16:07:28 +00:00
Fix invoke local when using callback in nodejs
This commit is contained in:
parent
806e07c6d6
commit
5ffd7f4ae8
@ -22,6 +22,10 @@ module.exports.withMessageByCallback = (event, context, callback) => {
|
|||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.withMessageAndDelayByCallback = (event, context, callback) => {
|
||||||
|
setTimeout(() => callback(null, 'Succeed'), 1);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.withMessageByLambdaProxy = () =>
|
module.exports.withMessageByLambdaProxy = () =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
|
|||||||
@ -312,60 +312,63 @@ class AwsInvokeLocal {
|
|||||||
this.serverless.cli.consoleLog(JSON.stringify(result, null, 4));
|
this.serverless.cli.consoleLog(JSON.stringify(result, null, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
const callback = (err, result) => {
|
return new Promise((resolve) => {
|
||||||
if (!hasResponded) {
|
const callback = (err, result) => {
|
||||||
hasResponded = true;
|
if (!hasResponded) {
|
||||||
if (err) {
|
hasResponded = true;
|
||||||
handleError.call(this, err);
|
if (err) {
|
||||||
} else if (result) {
|
handleError.call(this, err);
|
||||||
handleResult.call(this, result);
|
} else if (result) {
|
||||||
|
handleResult.call(this, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
|
||||||
|
const startTime = new Date();
|
||||||
|
const timeout = Number(this.options.functionObj.timeout)
|
||||||
|
|| Number(this.serverless.service.provider.timeout)
|
||||||
|
|| 6;
|
||||||
|
let context = {
|
||||||
|
awsRequestId: 'id',
|
||||||
|
invokeid: 'id',
|
||||||
|
logGroupName: this.provider.naming.getLogGroupName(this.options.functionObj.name),
|
||||||
|
logStreamName: '2015/09/22/[HEAD]13370a84ca4ed8b77c427af260',
|
||||||
|
functionVersion: 'HEAD',
|
||||||
|
isDefaultFunctionVersion: true,
|
||||||
|
|
||||||
|
functionName: this.options.functionObj.name,
|
||||||
|
memoryLimitInMB: '1024',
|
||||||
|
|
||||||
|
succeed(result) {
|
||||||
|
return callback(null, result);
|
||||||
|
},
|
||||||
|
fail(error) {
|
||||||
|
return callback(error);
|
||||||
|
},
|
||||||
|
done(error, result) {
|
||||||
|
return callback(error, result);
|
||||||
|
},
|
||||||
|
getRemainingTimeInMillis() {
|
||||||
|
return Math.max((timeout * 1000) - ((new Date()).valueOf() - startTime.valueOf()), 0);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if (customContext) {
|
||||||
|
context = customContext;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
const startTime = new Date();
|
const maybeThennable = lambda(event, context, callback);
|
||||||
const timeout = Number(this.options.functionObj.timeout)
|
if (!_.isUndefined(maybeThennable) && _.isFunction(maybeThennable.then)) {
|
||||||
|| Number(this.serverless.service.provider.timeout)
|
return maybeThennable
|
||||||
|| 6;
|
.then(
|
||||||
let context = {
|
callback.bind(this, null),
|
||||||
awsRequestId: 'id',
|
callback.bind(this)
|
||||||
invokeid: 'id',
|
);
|
||||||
logGroupName: this.provider.naming.getLogGroupName(this.options.functionObj.name),
|
}
|
||||||
logStreamName: '2015/09/22/[HEAD]13370a84ca4ed8b77c427af260',
|
|
||||||
functionVersion: 'HEAD',
|
|
||||||
isDefaultFunctionVersion: true,
|
|
||||||
|
|
||||||
functionName: this.options.functionObj.name,
|
return maybeThennable;
|
||||||
memoryLimitInMB: '1024',
|
});
|
||||||
|
|
||||||
succeed(result) {
|
|
||||||
return callback(null, result);
|
|
||||||
},
|
|
||||||
fail(error) {
|
|
||||||
return callback(error);
|
|
||||||
},
|
|
||||||
done(error, result) {
|
|
||||||
return callback(error, result);
|
|
||||||
},
|
|
||||||
getRemainingTimeInMillis() {
|
|
||||||
return Math.max((timeout * 1000) - ((new Date()).valueOf() - startTime.valueOf()), 0);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if (customContext) {
|
|
||||||
context = customContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
const maybeThennable = lambda(event, context, callback);
|
|
||||||
if (!_.isUndefined(maybeThennable) && _.isFunction(maybeThennable.then)) {
|
|
||||||
return maybeThennable
|
|
||||||
.then(
|
|
||||||
callback.bind(this, null),
|
|
||||||
callback.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return maybeThennable;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -625,6 +625,24 @@ describe('AwsInvokeLocal', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("by callback method even if callback isn't called syncronously", () => {
|
||||||
|
it('should succeed once if succeed if by callback', () => {
|
||||||
|
awsInvokeLocal.serverless.config.servicePath = __dirname;
|
||||||
|
|
||||||
|
return awsInvokeLocal.invokeLocalNodeJs(
|
||||||
|
'fixture/asyncHandlerWithSuccess',
|
||||||
|
'withMessageAndDelayByCallback'
|
||||||
|
)
|
||||||
|
.then(() => {
|
||||||
|
expect(serverless.cli.consoleLog.lastCall.args[0]).to.contain('"Succeed"');
|
||||||
|
const calls = serverless.cli.consoleLog.getCalls().reduce((acc, call) => (
|
||||||
|
_.includes(call.args[0], 'Succeed') ? [call].concat(acc) : acc
|
||||||
|
), []);
|
||||||
|
expect(calls.length).to.equal(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('with Lambda Proxy with application/json response', () => {
|
describe('with Lambda Proxy with application/json response', () => {
|
||||||
it('should succeed if succeed', () => {
|
it('should succeed if succeed', () => {
|
||||||
awsInvokeLocal.serverless.config.servicePath = __dirname;
|
awsInvokeLocal.serverless.config.servicePath = __dirname;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user