Skip to content

Instantly share code, notes, and snippets.

@iceener
Last active January 23, 2023 16:27
Show Gist options
  • Save iceener/4fb0bde0a7f0f068ae15096487c197c2 to your computer and use it in GitHub Desktop.
Save iceener/4fb0bde0a7f0f068ae15096487c197c2 to your computer and use it in GitHub Desktop.
Execute JavaScript snippet (Netlify Function)
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