From f3b65987dcdc7a7c2185152ebb16f6f036ef6bf3 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Sun, 8 May 2022 01:02:24 +0200 Subject: [PATCH] fix server build --- server/src/core/HTTPServer.ts | 16 +++++++------- server/src/utils/{updater.js => updater.ts} | 23 ++++++++------------- tsconfig.json | 11 ++++------ 3 files changed, 21 insertions(+), 29 deletions(-) rename server/src/utils/{updater.js => updater.ts} (81%) diff --git a/server/src/core/HTTPServer.ts b/server/src/core/HTTPServer.ts index 4c0f8e4..cc05960 100644 --- a/server/src/core/HTTPServer.ts +++ b/server/src/core/HTTPServer.ts @@ -1,6 +1,6 @@ -import http from 'http'; -import url from 'url'; -import fs, { open, close } from 'fs'; +import { createServer, Server } from 'http'; +import { parse as urlParse } from 'url'; +import { open, close, readFile, fstat } from 'fs'; import { parse, join } from 'path'; namespace server { @@ -38,10 +38,10 @@ class Jean { * Create a static file server * @returns an instance of a standard NodeJS http.Server */ - public createServer(): http.Server { - return http.createServer((req, res) => { + public createServer(): Server { + return createServer((req, res) => { // parse the current given url - const parsedUrl = url.parse(req.url, false) + const parsedUrl = urlParse(req.url, false) // extract the pathname and guard it with the working dir let pathname = join(this.workingDir, `.${parsedUrl.pathname}`); // extract the file extension @@ -56,7 +56,7 @@ class Jean { return; } // something's gone wrong it's not a file or a directory - fs.fstat(fd, (err, stat) => { + fstat(fd, (err, stat) => { if (err) { res.statusCode = 500; res.end(err); @@ -66,7 +66,7 @@ class Jean { pathname = join(pathname, 'index.html') } // read the file - fs.readFile(pathname, (err, data) => { + readFile(pathname, (err, data) => { if (err) { res.statusCode = 500; res.end(`Error reading the file: ${err}`); diff --git a/server/src/utils/updater.js b/server/src/utils/updater.ts similarity index 81% rename from server/src/utils/updater.js rename to server/src/utils/updater.ts index b028483..f0a5de2 100644 --- a/server/src/utils/updater.js +++ b/server/src/utils/updater.ts @@ -1,7 +1,6 @@ -const https = require('https'); -const fs = require('fs'); -const path = require('path'); -const { Socket } = require('socket.io'); +import { get } from 'https'; +import { rmSync, createWriteStream, chmod } from 'fs'; +import { join } from 'path'; // endpoint to github API const options = { @@ -37,14 +36,14 @@ function buildDonwloadOptions(release) { async function update() { // ensure that the binary has been removed try { - fs.rmSync(path.join(__dirname, '..', 'core', 'yt-dlp')) + rmSync(join(__dirname, '..', 'core', 'yt-dlp')) } catch (e) { console.log('file not found!') } // body buffer let chunks = [] - https.get(options, res => { + get(options, res => { // push the http packets chunks into the buffer res.on('data', chunk => { chunks.push(chunk) @@ -65,16 +64,16 @@ async function update() { * @param {string} url yt-dlp GitHub release url */ function downloadBinary(url) { - https.get(url, res => { + get(url, res => { // if it is a redirect follow the url if (res.statusCode === 301 || res.statusCode === 302) { return downloadBinary(res.headers.location) } - let bin = fs.createWriteStream(path.join(__dirname, '..', 'core', 'yt-dlp')) + let bin = createWriteStream(join(__dirname, '..', 'core', 'yt-dlp')) res.pipe(bin) // once the connection has ended make the file executable res.on('end', () => { - fs.chmod(path.join(__dirname, '..', 'core', 'yt-dlp'), 0o775, err => { + chmod(join(__dirname, '..', 'core', 'yt-dlp'), 0o775, err => { err ? console.error('failed updating!') : console.log('done!') }) }) @@ -84,12 +83,8 @@ function downloadBinary(url) { * Invoke the yt-dlp update procedure * @param {Socket} socket the current connection socket */ -function updateFromFrontend(socket) { +export function ytdlpUpdater(socket) { update().then(() => { socket.emit('updated') }) -} - -module.exports = { - ytdlpUpdater: updateFromFrontend } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 8f84243..49add15 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,16 +1,13 @@ { "compilerOptions": { "outDir": "./dist", - "allowJs": true, "target": "ES2018", - "module": "commonjs", - "esModuleInterop": true, "strict": false, - "noEmit": false + "noEmit": false, + "moduleResolution": "node", + "module": "commonjs", + "skipLibCheck": true }, - "exclude": [ - "node_modules" - ], "include": [ "./server/src/**/*" ]