From 156e30a3cb2ecb284f9fe47518b84076d08c0ce5 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 9 Feb 2022 12:55:01 -0800 Subject: [PATCH 1/6] docs(usage): Use express server --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1e349f6..3559b2d 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,20 @@ Implementation of in-memory [IPFS Pinning Service API](https://ipfs.github.io/pi ## Install ``` -npm i mock-ipfs-pinning-service +npm i -D mock-ipfs-pinning-service @types/express ``` ## Usage ```js -const http = require("http") const { setup } = require("mock-ipfs-pinning-service") const port = 3000 const main = async () => { - const service = await setup({ token: "secret" }) - const server = http.createServer(http) + /** + * @type {import('express').Application} + */ + const server = await setup({ token: "secret" }) server.listen(port) } ``` From a99b505932caca649201afa251af1c351322be2a Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 9 Feb 2022 12:57:41 -0800 Subject: [PATCH 2/6] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3559b2d..96a87ec 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,9 @@ const main = async () => { * @type {import('express').Application} */ const server = await setup({ token: "secret" }) - server.listen(port) + server.listen(port, () => { + console.log(`server running on port ${port}`); + }) } ``` From 05bdfa23faf9cb2939dd862084260b0b2111aa2c Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 9 Feb 2022 12:58:37 -0800 Subject: [PATCH 3/6] style: remove semicolon --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 96a87ec..47e0e11 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ const main = async () => { */ const server = await setup({ token: "secret" }) server.listen(port, () => { - console.log(`server running on port ${port}`); + console.log(`server running on port ${port}`) }) } ``` From d49114c8e73899777a679328efeebe69f164bb48 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 9 Feb 2022 13:39:31 -0800 Subject: [PATCH 4/6] docs(usage): cleanup express server on exit --- README.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 47e0e11..0c9ecca 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,38 @@ const main = async () => { /** * @type {import('express').Application} */ - const server = await setup({ token: "secret" }) - server.listen(port, () => { + const service = await setup({ token: "secret" }) + const server = service.listen(port, () => { console.log(`server running on port ${port}`) }) + + // Express server cleanup handling. + const cleanup = () => { + server.close((err) => { + if (!err || !err?.code === 'ERR_SERVER_NOT_RUNNING') { + console.log(`server stopped listening on port ${port}`) + } + if (err) { + console.error(err) + } + }) + } + + // And you'll want to make sure you close the server when your process exits + process.on('beforeExit', cleanup) + process.on('SIGTERM', cleanup) + process.on('SIGINT', cleanup) + process.on('SIGHUP', cleanup) + + // To prevent duplicated cleanup, remove the process listeners on server close. + server.on('close', () => { + process.off('beforeExit', cleanup) + process.off('SIGTERM', cleanup) + process.off('SIGINT', cleanup) + process.off('SIGHUP', cleanup) + }) } + ``` State of the pins is exposed via `service.locals.state` which you can monkeypatch or replace. Each new request will be served based on that value. All requests perform that perform state updates do them in immutable style and swap the whole value, in other words references are guaranteed to not mutate. From 611e416090c9daab41eff2e757afd5f75e68fbed Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 9 Feb 2022 13:49:44 -0800 Subject: [PATCH 5/6] docs(usage): fix typo/bug --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c9ecca..25a12ae 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ const main = async () => { // Express server cleanup handling. const cleanup = () => { server.close((err) => { - if (!err || !err?.code === 'ERR_SERVER_NOT_RUNNING') { + if (!err || err?.code !== 'ERR_SERVER_NOT_RUNNING') { console.log(`server stopped listening on port ${port}`) } if (err) { From a9ef70323998d1845b6cf3dc3cbf639079cc74db Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Fri, 25 Feb 2022 11:47:22 -0800 Subject: [PATCH 6/6] docs(chore): code usage appears below CLI usage also make code example more concise --- README.md | 54 ++++++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 25a12ae..e2b807c 100644 --- a/README.md +++ b/README.md @@ -10,44 +10,48 @@ npm i -D mock-ipfs-pinning-service @types/express ## Usage + +### CLI Usage + +You can start the mock server from the command line: + +```sh +npx mock-ipfs-pinning-service --port 3000 --token secret +``` + +If token is not passed it will not preform authentification. + +### Code Usage + ```js -const { setup } = require("mock-ipfs-pinning-service") -const port = 3000 +const { setup } = require('mock-ipfs-pinning-service') +const port = 3005 const main = async () => { /** * @type {import('express').Application} */ - const service = await setup({ token: "secret" }) + const service = await setup({ token: 'secret' }) const server = service.listen(port, () => { console.log(`server running on port ${port}`) }) + const cleanupEvents = ['beforeExit', 'SIGTERM', 'SIGINT', 'SIGHUP'] + // Express server cleanup handling. const cleanup = () => { + // To prevent duplicated cleanup, remove the process listeners on cleanup + cleanupEvents.forEach((event) => process.off(event, cleanup)) server.close((err) => { - if (!err || err?.code !== 'ERR_SERVER_NOT_RUNNING') { - console.log(`server stopped listening on port ${port}`) - } if (err) { console.error(err) + return } + console.log(`server stopped listening on port ${port}`) }) } - - // And you'll want to make sure you close the server when your process exits - process.on('beforeExit', cleanup) - process.on('SIGTERM', cleanup) - process.on('SIGINT', cleanup) - process.on('SIGHUP', cleanup) - - // To prevent duplicated cleanup, remove the process listeners on server close. - server.on('close', () => { - process.off('beforeExit', cleanup) - process.off('SIGTERM', cleanup) - process.off('SIGINT', cleanup) - process.off('SIGHUP', cleanup) - }) + // close the server when your process exits + cleanupEvents.forEach((event) => process.on(event, cleanup)) } ``` @@ -56,16 +60,6 @@ State of the pins is exposed via `service.locals.state` which you can monkeypatc You can see the status of the server and test individual functions from the SwaggerUI by navigating to `http://localhost:${port}/docs/`, switching _Server_ to `/` and then entering access token under _Authorize_ button. -### CLI Usage - -You can start the mock server from the command line: - -```sh -npx mock-ipfs-pinning-service --port 3000 --token secret -``` - -If token is not passed it will not preform authentification. - ### Log levels Pass: