Skip to content

Commit b9cf354

Browse files
authored
Merge pull request #1996 from fturmel/esm-server-functions
Fix broken showcase submission backend
2 parents 4ecd70a + 2ecefbf commit b9cf354

2 files changed

Lines changed: 32 additions & 32 deletions

File tree

netlify/functions/submission-background.js renamed to netlify/functions/submission-background.mjs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const { Octokit } = require('@octokit/core');
2-
const slugify = require('slugify');
3-
const sharp = require('sharp');
4-
const crypto = require('crypto');
1+
import { Octokit } from '@octokit/core';
2+
import crypto from 'node:crypto';
3+
import slugify from 'slugify';
54

6-
// event.body expected to be:
5+
// Request JSON body expected to be:
76
// {
87
// title: "Something",
98
// imageBlobSha: "sha",
@@ -19,19 +18,19 @@ const crypto = require('crypto');
1918
// }
2019
// X-Signature: sha256=...
2120

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

2524
if (!process.env.GITHUB_TOKEN) {
2625
console.error('GitHub Token not loaded');
2726
return;
2827
}
2928

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

183182
console.log('Done!');
184183
};
184+
185+
export const config = {
186+
background: true
187+
};
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { Octokit } = require('@octokit/core');
2-
const sharp = require('sharp');
3-
const crypto = require('crypto');
1+
import { Octokit } from '@octokit/core';
2+
import sharp from 'sharp';
3+
import crypto from 'node:crypto';
44

5-
// event.body expected to be:
5+
// Request JSON body expected to be:
66
// {
77
// title: "Something",
88
// image: "base64string=",
@@ -38,28 +38,22 @@ async function validateImage(imageBase64) {
3838
}
3939
}
4040

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

4444
if (!process.env.GITHUB_TOKEN) {
4545
console.error('GitHub Token not loaded');
46-
return {
47-
statusCode: 500,
48-
body: JSON.stringify({})
49-
};
46+
return Response.json({}, { status: 500 });
5047
}
5148

5249
// parse payload
53-
const postInfo = JSON.parse(event.body);
50+
const postInfo = await request.json();
5451

5552
// validate image and extract its extension
5653
const { error, imageExtension } = await validateImage(postInfo.image);
5754
if (error) {
5855
console.error(error);
59-
return {
60-
statusCode: 400,
61-
body: JSON.stringify({ error })
62-
};
56+
return Response.json({ error }, { status: 400 });
6357
}
6458

6559
// Shared properties
@@ -99,8 +93,8 @@ exports.handler = async function (event, context) {
9993
imageExtension
10094
};
10195
const signature = hmac.update(JSON.stringify(payload)).digest('hex');
102-
await fetch(
103-
event.rawUrl.replace('submission-sync', 'submission-background'),
96+
const backgroundResponse = await fetch(
97+
new URL('/.netlify/functions/submission-background', request.url),
10498
{
10599
method: 'POST',
106100
body: JSON.stringify(payload),
@@ -111,10 +105,13 @@ exports.handler = async function (event, context) {
111105
}
112106
);
113107

114-
return {
115-
statusCode: 200,
116-
body: JSON.stringify({
117-
status: 'success'
118-
})
119-
};
108+
if (!backgroundResponse.ok) {
109+
console.error('Background invocation failed:', backgroundResponse.status);
110+
return Response.json(
111+
{ error: 'Could not start submission processing' },
112+
{ status: 502 }
113+
);
114+
}
115+
116+
return Response.json({ status: 'success' });
120117
};

0 commit comments

Comments
 (0)