UNPKG

serverless-offline-edge-lambda

Version:

A plugin for the Serverless Framework that simulates the behavior of AWS CloudFront Edge Lambdas while developing offline.

110 lines 4.8 kB
"use strict"; 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()); }); }; require("./polyfills"); const behavior_router_1 = require("./behavior-router"); class OfflineEdgeLambdaPlugin { constructor(serverless, options) { this.serverless = serverless; this.options = options; this.prepareCustomSection(); this.server = new behavior_router_1.BehaviorRouter(serverless, options); this.log = serverless.cli.log.bind(serverless.cli); this.commands = { offline: { lifecycleEvents: [ 'start' ], commands: { start: { lifecycleEvents: [ 'init', 'end' ], usage: 'Start the offline edge lambda server', options: { port: { usage: 'Specify the port that the server will listen on', default: 8080, type: 'string' }, cloudfrontPort: { usage: '[Deprecated] Specify the port that the server will listen on. Use --port instead', type: 'string' }, disableCache: { usage: 'Disables simulated cache', default: false, type: 'boolean' }, cacheDir: { usage: 'Specify the directory where cache file will be stored', required: false, type: 'string' }, fileDir: { usage: 'Specify the directory where origin requests will draw from', required: false, type: 'string' }, headersFile: { usage: 'Specify the file (JSON) where injected CloudFront headers will be taken from', required: false, type: 'string' } } } } } }; this.hooks = { 'offline:start:init': this.onStart.bind(this), 'offline:start:end': this.onEnd.bind(this), 'webpack:compile:watch:compile': this.onReload.bind(this) }; } onStart() { return __awaiter(this, void 0, void 0, function* () { try { const port = this.options.cloudfrontPort || this.options.port || 8080; this.log(`CloudFront Offline listening on port ${port}`); yield this.server.start(port); } catch (err) { console.error(err); } }); } onEnd() { return __awaiter(this, void 0, void 0, function* () { yield this.server.purgeStorage(); this.log(`CloudFront Offline storage purged`); }); } onReload() { return __awaiter(this, void 0, void 0, function* () { // In the event we have not started the server yet or we are in the process of // restarting the server ignore our changes if (!this.server.hasStarted() || !this.server.isRunning()) { return; } console.log('Restarting server due to function update...'); yield this.server.restart(); }); } prepareCustomSection() { const { service } = this.serverless; service.custom = service.custom || {}; const { custom } = this.serverless.service; custom.offlineEdgeLambda = custom.offlineEdgeLambda || {}; } } module.exports = OfflineEdgeLambdaPlugin; //# sourceMappingURL=index.js.map