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 { createServer, Server } from 'http';
import url from 'url'; import { parse as urlParse } from 'url';
import fs, { open, close } from 'fs'; import { open, close, readFile, fstat } from 'fs';
import { parse, join } from 'path'; import { parse, join } from 'path';
namespace server { namespace server {
@@ -38,10 +38,10 @@ class Jean {
* Create a static file server * Create a static file server
* @returns an instance of a standard NodeJS http.Server * @returns an instance of a standard NodeJS http.Server
*/ */
public createServer(): http.Server { public createServer(): Server {
return http.createServer((req, res) => { return createServer((req, res) => {
// parse the current given url // 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 // extract the pathname and guard it with the working dir
let pathname = join(this.workingDir, `.${parsedUrl.pathname}`); let pathname = join(this.workingDir, `.${parsedUrl.pathname}`);
// extract the file extension // extract the file extension
@@ -56,7 +56,7 @@ class Jean {
return; return;
} }
// something's gone wrong it's not a file or a directory // something's gone wrong it's not a file or a directory
fs.fstat(fd, (err, stat) => { fstat(fd, (err, stat) => {
if (err) { if (err) {
res.statusCode = 500; res.statusCode = 500;
res.end(err); res.end(err);
@@ -66,7 +66,7 @@ class Jean {
pathname = join(pathname, 'index.html') pathname = join(pathname, 'index.html')
} }
// read the file // read the file
fs.readFile(pathname, (err, data) => { readFile(pathname, (err, data) => {
if (err) { if (err) {
res.statusCode = 500; res.statusCode = 500;
res.end(`Error reading the file: ${err}`); res.end(`Error reading the file: ${err}`);

View File

@@ -1,7 +1,6 @@
const https = require('https'); import { get } from 'https';
const fs = require('fs'); import { rmSync, createWriteStream, chmod } from 'fs';
const path = require('path'); import { join } from 'path';
const { Socket } = require('socket.io');
// endpoint to github API // endpoint to github API
const options = { const options = {
@@ -37,14 +36,14 @@ function buildDonwloadOptions(release) {
async function update() { async function update() {
// ensure that the binary has been removed // ensure that the binary has been removed
try { try {
fs.rmSync(path.join(__dirname, '..', 'core', 'yt-dlp')) rmSync(join(__dirname, '..', 'core', 'yt-dlp'))
} }
catch (e) { catch (e) {
console.log('file not found!') console.log('file not found!')
} }
// body buffer // body buffer
let chunks = [] let chunks = []
https.get(options, res => { get(options, res => {
// push the http packets chunks into the buffer // push the http packets chunks into the buffer
res.on('data', chunk => { res.on('data', chunk => {
chunks.push(chunk) chunks.push(chunk)
@@ -65,16 +64,16 @@ async function update() {
* @param {string} url yt-dlp GitHub release url * @param {string} url yt-dlp GitHub release url
*/ */
function downloadBinary(url) { function downloadBinary(url) {
https.get(url, res => { get(url, res => {
// if it is a redirect follow the url // if it is a redirect follow the url
if (res.statusCode === 301 || res.statusCode === 302) { if (res.statusCode === 301 || res.statusCode === 302) {
return downloadBinary(res.headers.location) 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) res.pipe(bin)
// once the connection has ended make the file executable // once the connection has ended make the file executable
res.on('end', () => { 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!') err ? console.error('failed updating!') : console.log('done!')
}) })
}) })
@@ -84,12 +83,8 @@ function downloadBinary(url) {
* Invoke the yt-dlp update procedure * Invoke the yt-dlp update procedure
* @param {Socket} socket the current connection socket * @param {Socket} socket the current connection socket
*/ */
function updateFromFrontend(socket) { export function ytdlpUpdater(socket) {
update().then(() => { update().then(() => {
socket.emit('updated') socket.emit('updated')
}) })
}
module.exports = {
ytdlpUpdater: updateFromFrontend
} }

View File

@@ -1,16 +1,13 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "./dist", "outDir": "./dist",
"allowJs": true,
"target": "ES2018", "target": "ES2018",
"module": "commonjs",
"esModuleInterop": true,
"strict": false, "strict": false,
"noEmit": false "noEmit": false,
"moduleResolution": "node",
"module": "commonjs",
"skipLibCheck": true
}, },
"exclude": [
"node_modules"
],
"include": [ "include": [
"./server/src/**/*" "./server/src/**/*"
] ]