From eb88d7d7ce19584632573607267b1d96383d834a Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta Date: Tue, 23 Feb 2021 00:21:20 +0900 Subject: [PATCH 1/3] fix: handle readable ended request --- docs/content/en/configuration.md | 10 +++++++++ packages/content/lib/index.js | 3 ++- packages/content/lib/middleware.js | 32 ++++++++++++++++----------- packages/content/lib/utils.js | 3 ++- packages/content/test/options.test.js | 3 ++- packages/content/types/index.d.ts | 1 + 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/docs/content/en/configuration.md b/docs/content/en/configuration.md index c07695f0a..f44a0b23f 100644 --- a/docs/content/en/configuration.md +++ b/docs/content/en/configuration.md @@ -524,7 +524,17 @@ Your component should implement the following: You should be aware that you get the full markdown file content so this includes the front-matter. You can use `gray-matter` to split and join the markdown and the front-matter. +### `reqBodyKey` +`@nuxt/content` module waits for request body stream as a server middleware. In case another server middleware consume it in advance, you can configure by which key `@nuxt/content` should resolve request body. + +By default, `@nuxt/content` assumes that [`body-parser`](https://www.npmjs.com/package/body-parser) packages is used and `req.body` is populated. + +```js{}[nuxt.config.js] +content: { + reqBodyKey: 'body' +} +``` ## Defaults ```js{}[nuxt.config.js] diff --git a/packages/content/lib/index.js b/packages/content/lib/index.js index fbc50fadd..fccc7921a 100644 --- a/packages/content/lib/index.js +++ b/packages/content/lib/index.js @@ -156,7 +156,8 @@ module.exports = async function (moduleOptions) { ws, database, dir: options.dir, - watch: options.watch + watch: options.watch, + reqBodyKey: options.reqBodyKey }) }) diff --git a/packages/content/lib/middleware.js b/packages/content/lib/middleware.js index b0cb30159..5208714df 100644 --- a/packages/content/lib/middleware.js +++ b/packages/content/lib/middleware.js @@ -3,7 +3,7 @@ const fs = require('graceful-fs').promises const nodeReq = require('node-req') const nodeRes = require('node-res') -module.exports = ({ ws, database, dir, watch }) => async (req, res) => { +module.exports = ({ ws, database, dir, watch, reqBodyKey }) => async (req, res) => { const url = decodeURI(nodeReq.url(req)) // Handle WS @@ -17,18 +17,24 @@ module.exports = ({ ws, database, dir, watch }) => async (req, res) => { // Handle body /* istanbul ignore else */ if (['POST', 'PUT'].includes(req.method)) { - let body = '' - req.on('data', function (data) { - body += data - }) - // Wait for body data - await new Promise(function (resolve, reject) { - req.on('end', resolve) - req.on('error', reject) - }) - // Parse body - if (body) { - params = JSON.parse(body) + // If other server middle ware has already consumed stream, + // there is no longer body data to wait (see #292) + if (req.readableEnded) { + params = req[reqBodyKey] + } else { + let body = '' + req.on('data', function (data) { + body += data + }) + // Wait for body data + await new Promise(function (resolve, reject) { + req.on('end', resolve) + req.on('error', reject) + }) + // Parse body + if (body) { + params = JSON.parse(body) + } } } else if (req.method === 'GET') { params = nodeReq.get(req) diff --git a/packages/content/lib/utils.js b/packages/content/lib/utils.js index ed0200b97..99cf96cea 100644 --- a/packages/content/lib/utils.js +++ b/packages/content/lib/utils.js @@ -31,7 +31,8 @@ const getDefaults = ({ dev = false } = {}) => ({ yaml: {}, csv: {}, xml: {}, - extendParser: {} + extendParser: {}, + reqBodyKey: 'body' }) const processMarkdownTocDepth = (markdown) => { diff --git a/packages/content/test/options.test.js b/packages/content/test/options.test.js index 93424fe15..1022b64e5 100644 --- a/packages/content/test/options.test.js +++ b/packages/content/test/options.test.js @@ -42,7 +42,8 @@ describe('options', () => { }, remarkPlugins: expect.arrayContaining(remarkPlugins.map(name => ({ name, instance: require(name), options: undefined }))), rehypePlugins: expect.arrayContaining(rehypePlugins.map(name => ({ name, instance: require(name), options: undefined }))) - }) + }), + reqBodyKey: 'body' })) }) }) diff --git a/packages/content/types/index.d.ts b/packages/content/types/index.d.ts index 9c435fe09..122ce6666 100644 --- a/packages/content/types/index.d.ts +++ b/packages/content/types/index.d.ts @@ -42,6 +42,7 @@ interface IContentOptions { extendParser?: { [extension: string]: (file: string) => any; }; + reqBodyKey?: string; } // Nuxt 2.9+ From ccdacee305c0f1f3dd682793e2a9f7a25fbddefa Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta Date: Tue, 23 Feb 2021 00:23:46 +0900 Subject: [PATCH 2/3] fix: typo --- packages/content/lib/middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/content/lib/middleware.js b/packages/content/lib/middleware.js index 5208714df..60397743e 100644 --- a/packages/content/lib/middleware.js +++ b/packages/content/lib/middleware.js @@ -17,7 +17,7 @@ module.exports = ({ ws, database, dir, watch, reqBodyKey }) => async (req, res) // Handle body /* istanbul ignore else */ if (['POST', 'PUT'].includes(req.method)) { - // If other server middle ware has already consumed stream, + // If other server middleware has already consumed stream, // there is no longer body data to wait (see #292) if (req.readableEnded) { params = req[reqBodyKey] From c3d78d8cde8853caaa43910ae93dbf6b4f383e4d Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta Date: Tue, 23 Feb 2021 01:11:03 +0900 Subject: [PATCH 3/3] chore: remove req body key option --- docs/content/en/configuration.md | 11 ----------- packages/content/lib/index.js | 3 +-- packages/content/lib/middleware.js | 4 ++-- packages/content/lib/utils.js | 3 +-- packages/content/test/options.test.js | 3 +-- packages/content/types/index.d.ts | 1 - 6 files changed, 5 insertions(+), 20 deletions(-) diff --git a/docs/content/en/configuration.md b/docs/content/en/configuration.md index f44a0b23f..36bb88ccd 100644 --- a/docs/content/en/configuration.md +++ b/docs/content/en/configuration.md @@ -524,17 +524,6 @@ Your component should implement the following: You should be aware that you get the full markdown file content so this includes the front-matter. You can use `gray-matter` to split and join the markdown and the front-matter. -### `reqBodyKey` - -`@nuxt/content` module waits for request body stream as a server middleware. In case another server middleware consume it in advance, you can configure by which key `@nuxt/content` should resolve request body. - -By default, `@nuxt/content` assumes that [`body-parser`](https://www.npmjs.com/package/body-parser) packages is used and `req.body` is populated. - -```js{}[nuxt.config.js] -content: { - reqBodyKey: 'body' -} -``` ## Defaults ```js{}[nuxt.config.js] diff --git a/packages/content/lib/index.js b/packages/content/lib/index.js index fccc7921a..fbc50fadd 100644 --- a/packages/content/lib/index.js +++ b/packages/content/lib/index.js @@ -156,8 +156,7 @@ module.exports = async function (moduleOptions) { ws, database, dir: options.dir, - watch: options.watch, - reqBodyKey: options.reqBodyKey + watch: options.watch }) }) diff --git a/packages/content/lib/middleware.js b/packages/content/lib/middleware.js index 60397743e..2469ff584 100644 --- a/packages/content/lib/middleware.js +++ b/packages/content/lib/middleware.js @@ -3,7 +3,7 @@ const fs = require('graceful-fs').promises const nodeReq = require('node-req') const nodeRes = require('node-res') -module.exports = ({ ws, database, dir, watch, reqBodyKey }) => async (req, res) => { +module.exports = ({ ws, database, dir, watch }) => async (req, res) => { const url = decodeURI(nodeReq.url(req)) // Handle WS @@ -20,7 +20,7 @@ module.exports = ({ ws, database, dir, watch, reqBodyKey }) => async (req, res) // If other server middleware has already consumed stream, // there is no longer body data to wait (see #292) if (req.readableEnded) { - params = req[reqBodyKey] + params = req.body } else { let body = '' req.on('data', function (data) { diff --git a/packages/content/lib/utils.js b/packages/content/lib/utils.js index 99cf96cea..ed0200b97 100644 --- a/packages/content/lib/utils.js +++ b/packages/content/lib/utils.js @@ -31,8 +31,7 @@ const getDefaults = ({ dev = false } = {}) => ({ yaml: {}, csv: {}, xml: {}, - extendParser: {}, - reqBodyKey: 'body' + extendParser: {} }) const processMarkdownTocDepth = (markdown) => { diff --git a/packages/content/test/options.test.js b/packages/content/test/options.test.js index 1022b64e5..93424fe15 100644 --- a/packages/content/test/options.test.js +++ b/packages/content/test/options.test.js @@ -42,8 +42,7 @@ describe('options', () => { }, remarkPlugins: expect.arrayContaining(remarkPlugins.map(name => ({ name, instance: require(name), options: undefined }))), rehypePlugins: expect.arrayContaining(rehypePlugins.map(name => ({ name, instance: require(name), options: undefined }))) - }), - reqBodyKey: 'body' + }) })) }) }) diff --git a/packages/content/types/index.d.ts b/packages/content/types/index.d.ts index 122ce6666..9c435fe09 100644 --- a/packages/content/types/index.d.ts +++ b/packages/content/types/index.d.ts @@ -42,7 +42,6 @@ interface IContentOptions { extendParser?: { [extension: string]: (file: string) => any; }; - reqBodyKey?: string; } // Nuxt 2.9+