serverless-offline-edge-lambda
Version:
A plugin for the Serverless Framework that simulates the behavior of AWS CloudFront Edge Lambdas while developing offline.
176 lines • 7.24 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;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Origin = void 0;
const fs = __importStar(require("fs-extra"));
const http = __importStar(require("http"));
const https = __importStar(require("https"));
const path = __importStar(require("path"));
const url_1 = require("url");
const utils_1 = require("../utils");
const http_1 = require("../errors/http");
const http_status_codes_1 = require("http-status-codes");
class Origin {
constructor(baseUrl = '') {
this.baseUrl = baseUrl;
this.type = 'http';
if (!baseUrl) {
this.type = 'noop';
}
else if (/^http:\/\//.test(baseUrl)) {
this.type = 'http';
}
else if (/^https:\/\//.test(baseUrl)) {
this.type = 'https';
}
else {
this.baseUrl = path.resolve(baseUrl);
this.type = 'file';
}
}
retrieve(event) {
return __awaiter(this, void 0, void 0, function* () {
const { request } = event.Records[0].cf;
try {
const contents = yield this.getResource(request);
return {
status: '200',
statusDescription: 'OK',
headers: {
'content-type': [
{ key: 'content-type', value: 'application/json' }
]
},
bodyEncoding: 'text',
body: contents
};
}
catch (err) {
// Make sure error gets back to user
const status = err.statusCode || http_status_codes_1.StatusCodes.INTERNAL_SERVER_ERROR;
const reasonPhrase = err.reasonPhrase || 'Internal Server Error';
return {
status: status,
statusDescription: reasonPhrase,
headers: {
'content-type': [
{ key: 'content-type', value: 'application/json' }
]
},
bodyEncoding: 'text',
body: JSON.stringify({
'code': status,
'message': err.message
})
};
}
});
}
getResource(request) {
return __awaiter(this, void 0, void 0, function* () {
const { uri: key } = request;
switch (this.type) {
case 'file': {
return this.getFileResource(key);
}
case 'http':
case 'https': {
return yield this.getHttpResource(request);
}
case 'noop': {
throw new http_1.NotFoundError('Operation given as \'noop\'');
}
default: {
throw new http_1.InternalServerError('Invalid request type (needs to be \'http\', \'https\' or \'file\')');
}
}
});
}
getFileResource(key) {
return __awaiter(this, void 0, void 0, function* () {
const uri = (0, url_1.parse)(key);
const fileName = uri.pathname;
const fileTarget = `${this.baseUrl}/${fileName}`;
// Check for if path given is accessible and is a file before fetching it
try {
yield fs.access(fileTarget);
}
catch (_a) {
throw new http_1.NotFoundError(`File ${fileTarget} does not exist`);
}
const fileState = yield fs.lstat(fileTarget);
if (!fileState.isFile()) {
throw new http_1.NotFoundError(`${fileTarget} is not a file.`);
}
return yield fs.readFile(`${this.baseUrl}/${fileName}`, 'utf-8');
});
}
getHttpResource(request) {
return __awaiter(this, void 0, void 0, function* () {
const httpModule = (this.type === 'https') ? https : http;
const uri = (0, url_1.parse)(request.uri);
const baseUrl = (0, url_1.parse)(this.baseUrl);
const headers = (0, utils_1.toHttpHeaders)(request.headers).reduce((acc, item) => {
acc[item.key] = item.value[0];
return acc;
}, {});
const options = {
method: request.method,
protocol: baseUrl.protocol,
hostname: baseUrl.hostname,
port: baseUrl.port || (baseUrl.protocol === 'https:' ? 443 : 80),
path: uri.path,
headers: Object.assign(Object.assign({}, headers), { Connection: 'Close' })
};
return new Promise((resolve, reject) => {
const req = httpModule.request(options, (res) => {
const chunks = [];
res.on('data', (chunk) => {
chunks.push(chunk);
});
res.on('close', () => {
resolve(Buffer.concat(chunks).toString());
});
res.on('error', (err) => reject(err));
});
if (request.body && request.body.data) {
req.write(request.body.data);
}
req.end();
});
});
}
}
exports.Origin = Origin;
//# sourceMappingURL=origin.service.js.map