UNPKG

@iopipe/iopipe

Version:
112 lines (84 loc) 3.65 kB
'use strict'; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); module.exports = require('@iopipe/core'); function warning(fn) { for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } /*eslint-disable no-console*/ var str = `${fn}(${args.join(',')}) was called from @iopipe/iopipe but this method was not available. You may have called this method outside of an invocation or @iopipe/core needs to be upgraded to satisfy version ^1.11.0`; console.warn(str); return str; /*eslint-enable no-console*/ } function ctx() { var context = typeof module.exports.getContext === 'function' && module.exports.getContext() || {}; return context.iopipe || { mark: { start: warning.bind(null, 'mark.start'), end: warning.bind(null, 'mark.end') }, metric: warning.bind(null, 'metric'), label: warning.bind(null, 'label') }; } module.exports.mark = { start: function start() { var _ctx$mark; return (_ctx$mark = ctx().mark).start.apply(_ctx$mark, arguments); }, end: function end() { var _ctx$mark2; return (_ctx$mark2 = ctx().mark).end.apply(_ctx$mark2, arguments); } }; module.exports.metric = function () { var _ctx; return (_ctx = ctx()).metric.apply(_ctx, arguments); }; module.exports.label = function () { var _ctx2; return (_ctx2 = ctx()).label.apply(_ctx2, arguments); }; var wrappedHandler = void 0; function wrapHandler() { var iopipe = require('@iopipe/core')(); var _process$env = process.env, IOPIPE_HANDLER = _process$env.IOPIPE_HANDLER, _process$env$LAMBDA_T = _process$env.LAMBDA_TASK_ROOT, LAMBDA_TASK_ROOT = _process$env$LAMBDA_T === undefined ? '.' : _process$env$LAMBDA_T; if (!IOPIPE_HANDLER) { throw new Error('No IOPIPE_HANDLER environment variable set.'); } var parts = IOPIPE_HANDLER.split('.'); if (parts.length !== 2) { throw new Error(`Improperly formatted IOPIPE_HANDLER environment variable: ${IOPIPE_HANDLER}`); } var _parts = _slicedToArray(parts, 2), moduleToImport = _parts[0], handlerToWrap = _parts[1]; var importedModule = void 0; try { /*eslint-disable import/no-dynamic-require*/ importedModule = require(`${LAMBDA_TASK_ROOT}/${moduleToImport}`); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') { throw new Error(`Unable to import module '${moduleToImport}'`); } throw e; } var userHandler = importedModule[handlerToWrap]; if (typeof userHandler === 'undefined') { throw new Error(`Handler '${handlerToWrap}' missing on module '${moduleToImport}'`); } if (typeof userHandler !== 'function') { throw new Error(`Handler '${handlerToWrap}' from '${moduleToImport}' is not a function`); } return iopipe(userHandler); } module.exports.handler = function (event, context, callback) { if (!wrappedHandler) { wrappedHandler = wrapHandler(); } return wrappedHandler.call(this, event, context, callback); };