How to use the serverless-offline/src/functionHelper.createHandler function in serverless-offline

To help you get started, we’ve selected a few serverless-offline examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tradle / serverless-iot-local / index.js View on Github external
select,
            payload: message,
            context: {
              topic: () => topic,
              clientid: () => clientId,
              principal: () => {}
            }
          })

          let handler // The lambda function
          try {
            process.env = _.extend({}, this.service.provider.environment, this.service.functions[name].environment, this.originalEnvironment)
            process.env.SERVERLESS_OFFLINE_PORT = apiGWPort
            process.env.AWS_LAMBDA_FUNCTION_NAME = this.service.service + '-' + this.service.provider.stage
            process.env.AWS_REGION = this.service.provider.region
            handler = functionHelper.createHandler(options, this.options)
          } catch (err) {
            this.log(`Error while loading ${name}: ${err.stack}, ${requestId}`)
            return
          }

          const lambdaContext = createLambdaContext(fn)
          try {
            handler(event, lambdaContext, lambdaContext.done)
          } catch (error) {
            this.log(`Uncaught error in your '${name}' handler: ${error.stack}, ${requestId}`)
          }
        })
      })
github ar90n / serverless-s3-local / index.js View on Github external
const func = (s3Event) => {
        const baseEnvironment = {
          IS_LOCAL: true,
          IS_OFFLINE: true,
        };

        try {
          Object.assign(
            process.env,
            baseEnvironment,
            this.service.provider.environment,
            serviceFunction.environment || {},
          );

          const handler = functionHelper.createHandler(
            funOptions,
            this.options,
          );
          const callback = (error, response) => {
            console.log(`serverless-s3-local: callback is called with ${error} and ${response}`)
          }
          handler(s3Event, lambdaContext, callback);
        } catch (e) {
          console.error('Error while running handler', e);
        }
      };
github CoorpAcademy / serverless-plugins / packages / serverless-offline-kinesis / index.js View on Github external
eventHandler(streamEvent, functionName, shardId, chunk, cb) {
    const streamName = streamEvent.arn.split('/')[1];
    this.serverless.cli.log(`${streamName} (λ: ${functionName})`);

    const {location = '.'} = this.service.custom['serverless-offline'];

    const __function = this.service.getFunction(functionName);
    const servicePath = join(this.serverless.config.servicePath, location);
    const funOptions = getFunctionOptions(__function, functionName, servicePath);
    const handler = createHandler(funOptions, {});

    const lambdaContext = createLambdaContext(__function, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const event = {
      Records: chunk.map(({SequenceNumber, ApproximateArrivalTimestamp, Data, PartitionKey}) => ({
        kinesis: {
          partitionKey: PartitionKey,
          kinesisSchemaVersion: '1.0',
          data: Data.toString('base64'),
          sequenceNumber: SequenceNumber
        },
github CoorpAcademy / serverless-plugins / packages / serverless-offline-dynamodb-streams / index.js View on Github external
eventHandler(functionName, chunk, cb) {
    this.serverless.cli.log(`Kinesis ${JSON.stringify(chunk, null, 4)}`);

    const {location = '.'} = this.service.custom['serverless-offline'];

    const __function = this.service.getFunction(functionName);
    const servicePath = join(this.serverless.config.servicePath, location);
    const funOptions = getFunctionOptions(__function, functionName, servicePath);
    const handler = createHandler(funOptions, {});

    const lambdaContext = createLambdaContext(__function, cb);

    const event = chunk;

    handler(event, lambdaContext, lambdaContext.done);
  }
github msfidelis / serverless-offline-sqs-esmq / index.js View on Github external
eventHandler(queueEvent, functionName, messages, cb) {
    if (!messages) return cb();

    const streamName = this.getQueueName(queueEvent);
    this.serverless.cli.log(`${streamName} (λ: ${functionName})`);

    const {location = '.'} = getConfig(this.service, 'serverless-offline');

    const __function = this.service.getFunction(functionName);
    const servicePath = join(this.serverless.config.servicePath, location);
    const funOptions = getFunctionOptions(__function, functionName, servicePath);
    const handler = createHandler(funOptions, {});

    const lambdaContext = createLambdaContext(__function, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const event = {
      Records: messages.map(
        ({
          MessageId: messageId,
          ReceiptHandle: receiptHandle,
          Body: body,
          Attributes: attributes,
          MessageAttributes: messageAttributes,
github marconi1992 / serverless-offline-lambda / src / index.js View on Github external
handler: (req, reply) => {
          const invocationType = req.headers['x-amz-invocation-type'];
          const { functionName } = req.params;

          const funOptions = funOptionsCache[functionName];

          if (!funOptions) {
            return reply().code(404);
          }

          const handler = functionHelper.createHandler(funOptions, this.options);

          if (!handler) {
            return reply().code(404);
          }
          const { payload } = req;

          let body = '';
          payload.on('data', (chunk) => {
            body += chunk;
          });

          return payload.on('end', () => {
            const event = body ? JSON.parse(body) : {};
            this.serverlessLog(`Invoke (λ: ${functionName})`);
            if (invocationType === 'Event') {
              handler(event, { awsRequestId: this.awsRequestId() }, () => {});
github CoorpAcademy / serverless-plugins / packages / serverless-offline-sqs / src / index.js View on Github external
env,
      get('service.provider.environment', this),
      get('environment', __function)
    ]);
    process.env = functionEnv;

    const serviceRuntime = this.service.provider.runtime;
    const servicePath = join(this.serverless.config.servicePath, location);

    const funOptions = functionHelper.getFunctionOptions(
      __function,
      functionName,
      servicePath,
      serviceRuntime
    );
    const handler = functionHelper.createHandler(funOptions, config);

    const lambdaContext = new LambdaContext(__function, this.service.provider, (err, data) => {
      this.serverless.cli.log(
        `[${err ? figures.cross : figures.tick}] ${functionName} ${JSON.stringify(data) || ''}`
      );
      cb(err, data);
    });

    const awsRegion = config.region || 'us-west-2';
    const awsAccountId = config.accountId || '000000000000';
    const eventSourceARN =
      typeof queueEvent.arn === 'string'
        ? queueEvent.arn
        : `arn:aws:sqs:${awsRegion}:${awsAccountId}:${streamName}`;

    const event = {
github mitipi / serverless-iot-offline / ruleHandler.js View on Github external
payload: message,
            context: {
              topic: (index) => topic(index, topicUrl),
              clientid: () => clientid(topicUrl),
              timestamp: () => timestamp(),
              accountid: () => accountid()
            }
          })

          if (actions && actions.length) {
            applyActions(actions, event, log)
          }

          try {
            process.env = _.extend({}, slsService.provider.environment, slsService.functions[name].environment, process.env)
            handler = functionHelper.createHandler(options, slsOptions)
          } catch (err) {
            log(`Error while loading ${name}: ${err.stack}, ${requestId}`)
            return
          }

          const lambdaContext = createLambdaContext(fn)
          try {
            handler(event, lambdaContext, lambdaContext.done)
          } catch (error) {
            log(`Uncaught error in your '${name}' handler: ${error.stack}, ${requestId}`)
          }
        }
      })
    }

serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project

MIT
Latest version published 13 days ago

Package Health Score

95 / 100
Full package analysis