serverless-offline-edge-lambda
Version:
A plugin for the Serverless Framework that simulates the behavior of AWS CloudFront Edge Lambdas while developing offline.
90 lines • 4.72 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearModule = void 0;
/*
* <3 Lovingly borrowed from
* https://github.com/dherault/serverless-offline/blob/master/src/lambda/handler-runner/in-process-runner/InProcessRunner.js
*/
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const clearModule = (filePath, opts) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
const options = opts !== null && opts !== void 0 ? opts : {};
if (!require || !require.cache) {
return;
}
if (!require.cache[filePath]) {
const dirname = path.dirname(filePath);
for (const fn of fs.readdirSync(dirname)) {
const fullPath = path.resolve(dirname, fn);
if (fullPath.substr(0, filePath.length + 1) === `${filePath}.` &&
require.cache[fullPath]) {
filePath = fullPath;
break;
}
}
}
if (require.cache[filePath]) {
// Remove file from parent cache
if ((_a = require === null || require === void 0 ? void 0 : require.cache[filePath]) === null || _a === void 0 ? void 0 : _a.parent) {
let i = (_c = (_b = require === null || require === void 0 ? void 0 : require.cache[filePath]) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.children.length;
if (i) {
do {
i -= 1;
if (((_e = (_d = require === null || require === void 0 ? void 0 : require.cache[filePath]) === null || _d === void 0 ? void 0 : _d.parent) === null || _e === void 0 ? void 0 : _e.children[i].id) === filePath) {
(_g = (_f = require === null || require === void 0 ? void 0 : require.cache[filePath]) === null || _f === void 0 ? void 0 : _f.parent) === null || _g === void 0 ? void 0 : _g.children.splice(i, 1);
}
} while (i);
}
}
const cld = (_h = require === null || require === void 0 ? void 0 : require.cache[filePath]) === null || _h === void 0 ? void 0 : _h.children;
delete require.cache[filePath];
for (const c of cld) {
// Unload any non node_modules children
if (!c.filename.match(/node_modules/)) {
(0, exports.clearModule)(c.id, Object.assign(Object.assign({}, options), { cleanup: false }));
}
}
if (opts.cleanup) {
// Cleanup any node_modules that are orphans
let cleanup = false;
do {
cleanup = false;
for (const fn of Object.keys(require.cache)) {
if (((_j = require === null || require === void 0 ? void 0 : require.cache[fn]) === null || _j === void 0 ? void 0 : _j.id) !== '.' &&
((_k = require === null || require === void 0 ? void 0 : require.cache[fn]) === null || _k === void 0 ? void 0 : _k.parent) &&
((_m = (_l = require === null || require === void 0 ? void 0 : require.cache[fn]) === null || _l === void 0 ? void 0 : _l.parent) === null || _m === void 0 ? void 0 : _m.id) !== '.' &&
!require.cache[(_p = (_o = require === null || require === void 0 ? void 0 : require.cache[fn]) === null || _o === void 0 ? void 0 : _o.parent) === null || _p === void 0 ? void 0 : _p.id]) {
delete require.cache[fn];
cleanup = true;
}
}
} while (cleanup);
}
}
};
exports.clearModule = clearModule;
//# sourceMappingURL=clear-module.js.map