Last active
January 23, 2023 16:27
-
-
Save iceener/4fb0bde0a7f0f068ae15096487c197c2 to your computer and use it in GitHub Desktop.
Execute JavaScript snippet (Netlify Function)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { execSync } from 'child_process'; | |
export const handler = async (event) => { | |
if (event.httpMethod === 'POST') { | |
const payload = JSON.parse(event.body); | |
if (payload.secret !== 'SECRET_KEY') { | |
return; | |
} | |
const result = await execSync( | |
`node -e "console.log((() => {${payload.script | |
.replace(/"/g, '\\"') | |
.replace(/\n/g, '')}})())"`, | |
); | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
data: result.toString() | |
}) | |
} | |
} else { | |
return { | |
statusCode: 501, | |
body: JSON.stringify({ message: "Not Implemented" }), | |
headers: { 'content-type': 'application/json' } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment