ActivityPub-federated video streaming platform using P2P directly in your web browser
feat(plugins): add req.rawBody for routes
kontrollanten1b323f4f653f
diff --git a/packages/tests/fixtures/peertube-plugin-test-five/main.js b/packages/tests/fixtures/peertube-plugin-test-five/main.jsindex 07dd186544..14e9ccd3db 100644--- a/packages/tests/fixtures/peertube-plugin-test-five/main.js+++ b/packages/tests/fixtures/peertube-plugin-test-five/main.js@@ -9,6 +9,10 @@ async function register ({router.post('/form/post/mirror', (req, res) => {res.json(req.body)})++ router.post('/form/post/mirror-raw-body', (req, res) => {+ res.json(JSON.parse(req.rawBody))+ })}async function unregister () {diff --git a/packages/tests/src/plugins/plugin-router.ts b/packages/tests/src/plugins/plugin-router.tsindex 6f3571c055..82cf0321ea 100644--- a/packages/tests/src/plugins/plugin-router.ts+++ b/packages/tests/src/plugins/plugin-router.ts@@ -80,6 +80,24 @@ describe('Test plugin helpers', function () {}})+ it('Should mirror the raw post body', async function () {+ const body = {+ torso: 'arms',+ legs: 'feet'+ }++ for (const path of basePaths) {+ const res = await makePostBodyRequest({+ url: server.url,+ path: path + 'form/post/mirror-raw-body',+ fields: body,+ expectedStatus: HttpStatusCode.OK_200+ })++ expect(res.body).to.deep.equal(body)+ }+ })+it('Should remove the plugin and remove the routes', async function () {await server.plugins.uninstall({ npmName: 'peertube-plugin-test-five' })diff --git a/server/core/types/express.d.ts b/server/core/types/express.d.tsindex 4f669ffeed..2cabacc043 100644--- a/server/core/types/express.d.ts+++ b/server/core/types/express.d.ts@@ -54,6 +54,7 @@ declare module 'express' {export interface Request {query: anymethod: HttpMethodType+ rawBody: Buffer // Allow plugin routes to access the raw body}// ---------------------------------------------------------------------------diff --git a/server/server.ts b/server/server.tsindex edb942ce86..23e6cb1e61 100644--- a/server/server.ts+++ b/server/server.ts@@ -211,6 +211,10 @@ app.use(express.json({message: 'Invalid digest'})}++ if (req.originalUrl.startsWith('/plugins/')) {+ req.rawBody = buf+ }}}))diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.mdindex 2ad212a153..3f85c57912 100644--- a/support/doc/plugins/guide.md+++ b/support/doc/plugins/guide.md@@ -247,7 +247,7 @@ function register ({router.get('/ping', (req, res) => res.json({ message: 'pong' }))// Users are automatically authenticated- router.get('/auth', async (res, res) => {+ router.get('/auth', async (req, res) => {const user = await peertubeHelpers.user.getAuthUser(res)const isAdmin = user.role === 0@@ -261,6 +261,15 @@ function register ({isUser})})++ router.post('/webhook', async (req, res) => {+ const rawBody = req.rawBody // Buffer containing the raw body++ handleRawBody(rawBody)++ res.status(204)+ })+}```