Skip to content

Commit

Permalink
chore: bump aws runtime interface client
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jul 12, 2024
1 parent 31afde0 commit 553cc0f
Show file tree
Hide file tree
Showing 6 changed files with 522 additions and 344 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/coverage
/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js
/src/lambda/handler-runner/in-process-runner/aws-lambda-ric
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.serverless
.serverless-offline
.webpack
coverage
coverage
src/lambda/handler-runner/in-process-runner/aws-lambda-ric
124 changes: 124 additions & 0 deletions src/lambda/handler-runner/in-process-runner/aws-lambda-ric/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// from the AWS Lambda Runtime Interface Client
// https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/9af681895d0cdd2094ef452342a604e94dc82cc3/src/Errors.js

/**
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Defines custom error types throwable by the runtime.
*/

'use strict';

const util = require('util');

function _isError(obj) {
return (
obj &&
obj.name &&
obj.message &&
obj.stack &&
typeof obj.name === 'string' &&
typeof obj.message === 'string' &&
typeof obj.stack === 'string'
);
}

function intoError(err) {
if (err instanceof Error) {
return err;
} else {
return new Error(err);
}
}

module.exports.intoError = intoError;

/**
* Attempt to convert an object into a response object.
* This method accounts for failures when serializing the error object.
*/
function toRapidResponse(error) {
try {
if (util.types.isNativeError(error) || _isError(error)) {
return {
errorType: error.name?.replace(/\x7F/g, '%7F'),
errorMessage: error.message?.replace(/\x7F/g, '%7F'),
trace: error.stack.replace(/\x7F/g, '%7F').split('\n'),
};
} else {
return {
errorType: typeof error,
errorMessage: error.toString(),
trace: [],
};
}
} catch (_err) {
return {
errorType: 'handled',
errorMessage:
'callback called with Error argument, but there was a problem while retrieving one or more of its message, name, and stack',
};
}
}

module.exports.toRapidResponse = toRapidResponse;

/**
* Format an error with the expected properties.
* For compatability, the error string always starts with a tab.
*/
module.exports.toFormatted = (error) => {
try {
return (
'\t' + JSON.stringify(error, (_k, v) => _withEnumerableProperties(v))
);
} catch (err) {
return '\t' + JSON.stringify(toRapidResponse(error));
}
};

/**
* Error name, message, code, and stack are all members of the superclass, which
* means they aren't enumerable and don't normally show up in JSON.stringify.
* This method ensures those interesting properties are available along with any
* user-provided enumerable properties.
*/
function _withEnumerableProperties(error) {
if (error instanceof Error) {
let ret = Object.assign(
{
errorType: error.name,
errorMessage: error.message,
code: error.code,
},
error,
);
if (typeof error.stack == 'string') {
ret.stack = error.stack.split('\n');
}
return ret;
} else {
return error;
}
}

const errorClasses = [
class ImportModuleError extends Error {},
class HandlerNotFound extends Error {},
class MalformedHandlerName extends Error {},
class UserCodeSyntaxError extends Error {},
class MalformedStreamingHandler extends Error {},
class InvalidStreamingOperation extends Error {},
class UnhandledPromiseRejection extends Error {
constructor(reason, promise) {
super(reason);
this.reason = reason;
this.promise = promise;
}
},
];

errorClasses.forEach((e) => {
module.exports[e.name] = e;
e.prototype.name = `Runtime.${e.name}`;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// from the AWS Lambda Runtime Interface Client
// https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/9af681895d0cdd2094ef452342a604e94dc82cc3/src/HttpResponseStream.js

/**
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* HttpResponseStream is NOT used by the runtime.
* It is only exposed in the `awslambda` variable for customers to use.
*/

'use strict';

const METADATA_PRELUDE_CONTENT_TYPE =
'application/vnd.awslambda.http-integration-response';
const DELIMITER_LEN = 8;

// Implements the application/vnd.awslambda.http-integration-response content type.
class HttpResponseStream {
static from(underlyingStream, prelude) {
underlyingStream.setContentType(METADATA_PRELUDE_CONTENT_TYPE);

// JSON.stringify is required. NULL byte is not allowed in metadataPrelude.
const metadataPrelude = JSON.stringify(prelude);

underlyingStream._onBeforeFirstWrite = (write) => {
write(metadataPrelude);

// Write 8 null bytes after the JSON prelude.
write(new Uint8Array(DELIMITER_LEN));
};

return underlyingStream;
}
}

module.exports.HttpResponseStream = HttpResponseStream;
Loading

0 comments on commit 553cc0f

Please sign in to comment.