Skip to content

Commit

Permalink
remove functions logic
Browse files Browse the repository at this point in the history
  • Loading branch information
idanasulin2706 committed Nov 26, 2023
1 parent 30b2985 commit 2b0aa8c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 250 deletions.
143 changes: 1 addition & 142 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1044,145 +1044,4 @@ await consumer.destroy();

```js
memphisConnection.isConnected();
```

### Creating a Memphis function
Memphis provides a `createFunction` utility for more easily creating Memphis Functions.

The user-created `eventHandler` will be called for every message in the given batch of events. The user's `eventHandler` will take in a `payload` as a Uint8Array, `headers` as an object, and `inputs` as an object, and should return an object with keys `{ processedMessage, processedHeaders }`. The returned `processedMessage` should be a Uint8Array, and `processedHeaders` should be an object.

The user function should throw an exception if the message processing has failed. If any exception is thrown (deliberately or by a failed operation), the message will be sent to the dead letter station.

If the returned `processedMessage` and `processedHeaders` are `null`, then the message will be skipped and will not be sent to the station or dead letter station.

> Make sure to encode the `processedMessage` Uint8Array object with utf-8 encoding!
This example function takes the Uint8Array object `payload` and decodes it from base64 encoding so that it may be processed.

```javascript
const { memphis } = require("memphis-dev");

export const handler = async (event) => {
return await memphis.createFunction(event, eventHandler);
};

function eventHandler(payload, headers, inputs) {
const decodedPayload = payload.toString('utf-8');
const asJson = JSON.parse(decodedPayload);
asJson.modified = true;

return {
processedMessage: Buffer.from(JSON.stringify(asJson), 'utf-8'),
processedHeaders: headers
};
}
```

A user created `eventHandler` may also be async:

```javascript
const { memphis } = require("memphis-dev");

export const handler = async (event) => {
return await memphis.createFunction(event, eventHandler);
};

async function eventHandler(payload, headers, inputs) {
const decodedPayload = payload.toString('utf-8');
const asJson = JSON.parse(decodedPayload);
asJson.modified = true;

return {
processedMessage: Buffer.from(JSON.stringify(asJson), 'utf-8'),
processedHeaders: headers
};
}
```

If the user wants to have a message that they would like to validate and send to the dead letter station if the validation fails, then the user can throw an exception. In the following example, the field `check` is a boolean. The following function will send any messages that fail the `check` to the dead letter station.

```javascript
const { memphis } = require("memphis-dev");

export const handler = async (event) => {
return await memphis.createFunction(event, eventHandler);
};

async function eventHandler(payload, headers, inputs) {
const decodedPayload = payload.toString('utf-8');
const asJson = JSON.parse(decodedPayload);

if (!asJson.check) {
throw new Error("Validation Failed!");
}

return {
processedMessage: Buffer.from(JSON.stringify(asJson), 'utf-8'),
processedHeaders: headers
};
}
```

If a user would rather just skip the message and not have it be sent to the station or dead letter station, the user could instead return `{ processedMessage: null, processedHeaders: null }`.

```javascript
const { memphis } = require("memphis-dev");

export const handler = async (event) => {
return await memphis.createFunction(event, eventHandler);
};

function eventHandler(payload, headers, inputs) {
const decodedPayload = payload.toString('utf-8');
const asJson = JSON.parse(decodedPayload);

if (!asJson.check) {
return { processedMessage: null, processedHeaders: null };
}

return {
processedMessage: Buffer.from(JSON.stringify(asJson), 'utf-8'),
processedHeaders: headers
};
}
```

LLastly, if the user is using another data format like Protocol Buffers, the user may simply decode the `payload` into that format instead of JSON. The following example will be using the [protobufjs](https://github.com/protobufjs/protobuf.js/) package. Assuming we have a .proto definition like this:

```proto
syntax = "proto3";
package protobuf_example;
message Message{
string data_field = 1;
}
```

We can decode this and get the data_field out like this:

```javascript
const { memphis } = require("memphis-dev");

export const handler = async (event) => {
return await memphis.createFunction(event, eventHandler);
};

function eventHandler(payload, headers, inputs) {
const root = await protobuf.load("./message.proto");
const Message = root.lookupType('protobuf_example.Message');

const my_message = Message.decode(payload)

// data_field gets translated to dataField by the library...
// Arbitrarily changing the data field
my_message.dataField = "My new data"

// .finish() returns a Uint8Array so it may just be returned as the processedMessage
return {
"processedMessage" : Message.encode(my_message).finish(),
"processedHeaders" : headers
}
}


```
```
108 changes: 0 additions & 108 deletions src/functions.js

This file was deleted.

0 comments on commit 2b0aa8c

Please sign in to comment.