From ca50993ae409808000788d8f6d022bb9f4b584ef Mon Sep 17 00:00:00 2001 From: David Prothero Date: Sun, 25 Jun 2023 16:11:32 -0700 Subject: [PATCH] Add /api/connect endpoint and event callback --- package-lock.json | 58 +++++++++++++++++++++++++++++++++++++++-------- package.json | 3 ++- server.ts | 23 ++++++++++++++++++- 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 74b007f..de9f273 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,15 @@ { "name": "gms-notebook-file-server", - "version": "1.0.0", + "version": "1.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "gms-notebook-file-server", - "version": "1.0.0", + "version": "1.3.0", "license": "MIT", "dependencies": { + "body-parser": "^1.20.2", "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", @@ -1720,12 +1721,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1733,7 +1734,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -2428,6 +2429,43 @@ "node": ">= 0.10.0" } }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -4474,9 +4512,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index e55ae34..1c760d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gms-notebook-file-server", - "version": "1.2.0", + "version": "1.3.0", "description": "Local file server for GMsNotebook.com", "main": "dist/server.js", "type": "module", @@ -16,6 +16,7 @@ "author": "David Prothero", "license": "MIT", "dependencies": { + "body-parser": "^1.20.2", "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", diff --git a/server.ts b/server.ts index e450018..31b8282 100644 --- a/server.ts +++ b/server.ts @@ -1,4 +1,5 @@ import express, { Express, Request, Response } from "express"; +import bodyParser from "body-parser"; import cors from "cors"; import fs from "fs"; import path from "path"; @@ -10,11 +11,17 @@ import { getFileType } from "./filetypes.js"; import { resolveFilePath, pathExists } from "./utils.js"; export type Server = ReturnType; +export type Event = { + type: string; + payload: any; +}; +export type EventCallback = (event: Event) => void; export function startServer( port: number, folder: string, - workFolder: string + workFolder: string, + onEvent?: EventCallback ): Server { const app: Express = express(); const upload = multer({ dest: path.join(workFolder, "uploads") }); @@ -234,6 +241,20 @@ export function startServer( } }); + app.post( + "/api/connect", + bodyParser.json(), + async (req: Request, res: Response) => { + if (onEvent) { + onEvent({ type: "connect", payload: req.body }); + return res.json({ success: true }); + } + res + .status(404) + .json({ success: false, message: "No listener to accept event." }); + } + ); + const server = app.listen(port, () => { console.log(`⚡️[server]: Server is running at http://localhost:${port}`); });