Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { Octokit } = require('@octokit/core');
const slugify = require('slugify');
const sharp = require('sharp');
const crypto = require('crypto');
import { Octokit } from '@octokit/core';
import crypto from 'node:crypto';
import slugify from 'slugify';

// event.body expected to be:
// Request JSON body expected to be:
// {
// title: "Something",
// imageBlobSha: "sha",
Expand All @@ -19,19 +18,19 @@ const crypto = require('crypto');
// }
// X-Signature: sha256=...

exports.handler = async function (event) {
console.log('Background handler called with: ', event.body);
export default async (request) => {
console.log('Background handler called');

if (!process.env.GITHUB_TOKEN) {
console.error('GitHub Token not loaded');
return;
}

// parse payload and validate signature
const postInfo = JSON.parse(event.body);
const postInfo = await request.json();
const hmac = crypto.createHmac('sha256', process.env.GITHUB_TOKEN);
const signature = hmac.update(JSON.stringify(postInfo)).digest('hex');
if (signature !== event.headers['x-signature']) {
if (signature !== request.headers.get('x-signature')) {
console.error('Invalid signature');
return;
}
Expand Down Expand Up @@ -182,3 +181,7 @@ exports.handler = async function (event) {

console.log('Done!');
};

export const config = {
background: true
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { Octokit } = require('@octokit/core');
const sharp = require('sharp');
const crypto = require('crypto');
import { Octokit } from '@octokit/core';
import sharp from 'sharp';
import crypto from 'node:crypto';

// event.body expected to be:
// Request JSON body expected to be:
// {
// title: "Something",
// image: "base64string=",
Expand Down Expand Up @@ -38,28 +38,22 @@ async function validateImage(imageBase64) {
}
}

exports.handler = async function (event, context) {
console.log('Sync handler called with', event.body);
export default async (request) => {
console.log('Sync handler called');

if (!process.env.GITHUB_TOKEN) {
console.error('GitHub Token not loaded');
return {
statusCode: 500,
body: JSON.stringify({})
};
return Response.json({}, { status: 500 });
}

// parse payload
const postInfo = JSON.parse(event.body);
const postInfo = await request.json();

// validate image and extract its extension
const { error, imageExtension } = await validateImage(postInfo.image);
if (error) {
console.error(error);
return {
statusCode: 400,
body: JSON.stringify({ error })
};
return Response.json({ error }, { status: 400 });
}

// Shared properties
Expand Down Expand Up @@ -99,8 +93,8 @@ exports.handler = async function (event, context) {
imageExtension
};
const signature = hmac.update(JSON.stringify(payload)).digest('hex');
await fetch(
event.rawUrl.replace('submission-sync', 'submission-background'),
const backgroundResponse = await fetch(
new URL('/.netlify/functions/submission-background', request.url),
{
method: 'POST',
body: JSON.stringify(payload),
Expand All @@ -111,10 +105,13 @@ exports.handler = async function (event, context) {
}
);

return {
statusCode: 200,
body: JSON.stringify({
status: 'success'
})
};
if (!backgroundResponse.ok) {
console.error('Background invocation failed:', backgroundResponse.status);
return Response.json(
{ error: 'Could not start submission processing' },
{ status: 502 }
);
}

return Response.json({ status: 'success' });
};
Loading