fix server build

This commit is contained in:
2022-05-08 01:02:24 +02:00
parent e731cded1e
commit f3b65987dc
3 changed files with 21 additions and 29 deletions

View File

@@ -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}`);

View File

@@ -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
}