{
  "name": "mountable.io file changes",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "mountable-files-changed",
        "responseMode": "onReceived",
        "options": {
          "rawBody": true
        }
      },
      "id": "55555555-5555-4555-8555-555555555555",
      "name": "mountable.io webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "6d0b2f8e-3c41-4a7e-9b52-1f6c8d4e2a90"
    },
    {
      "parameters": {
        "jsCode": "// Paste the secret (whsec_...) that creating the mountable.io webhook returned.\nconst secret = 'REPLACE_WITH_WEBHOOK_SECRET';\nconst crypto = require('crypto');\n\nfunction verify(headers, body) {\n  const id = headers['webhook-id'] ?? '';\n  const timestamp = headers['webhook-timestamp'] ?? '';\n  if (!/^\\d+$/.test(timestamp) || Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;\n  const key = Buffer.from(secret.replace(/^whsec_/, ''), 'base64');\n  const expected = crypto.createHmac('sha256', key).update(`${id}.${timestamp}.${body}`).digest();\n  return (headers['webhook-signature'] ?? '').split(' ').some((sig) => {\n    if (!sig.startsWith('v1,')) return false;\n    const given = Buffer.from(sig.slice(3), 'base64');\n    return given.length === expected.length && crypto.timingSafeEqual(given, expected);\n  });\n}\n\nif (!secret.startsWith('whsec_')) throw new Error('Set the mountable.io webhook secret in this node');\nconst binary = this.helpers ?? helpers;\nconst out = [];\nconst items = $input.all();\nfor (let i = 0; i < items.length; i++) {\n  const headers = items[i].json.headers;\n  const body = (await binary.getBinaryDataBuffer(i, 'data')).toString('utf8');\n  if (!verify(headers, body)) throw new Error('mountable.io webhook signature did not verify');\n  const message = JSON.parse(body);\n  const common = { message: message.type, cursor: message.data.cursor, filesystem_id: message.data.filesystem_id };\n  if (message.type === 'files.resync') {\n    out.push({ json: { ...common, type: 'resync', path: null, file: null } });\n    continue;\n  }\n  for (const event of message.data.events) {\n    out.push({ json: { ...common, type: event.type, path: event.path, file: event.file } });\n  }\n}\nreturn out;\n"
      },
      "id": "66666666-6666-4666-8666-666666666666",
      "name": "Verify and split changes",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {},
      "id": "77777777-7777-4777-8777-777777777777",
      "name": "Handle each change",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        440,
        0
      ]
    }
  ],
  "connections": {
    "mountable.io webhook": {
      "main": [
        [
          {
            "node": "Verify and split changes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify and split changes": {
      "main": [
        [
          {
            "node": "Handle each change",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
