Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10
12.3.1
26 changes: 11 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable no-console */

const {
import {
access,
constants: { X_OK, R_OK },
constants as fsConstants,
createReadStream,
readFile,
unlink,
} = require('fs');
const { createHash } = require('crypto');
const { parse } = require('url');
const { join } = require('path');
const { spawn } = require('child_process');
const glob = require('glob');
} from 'fs';
import { createHash } from 'crypto';
import { parse } from 'url';
import { join } from 'path';
import { spawn } from 'child_process';
import glob from 'glob';

const env = process.env.NODE_ENV || 'development';
const HOME = env === 'development' ? process.env.HOME : '/home/hosting-user';
Expand Down Expand Up @@ -41,6 +41,7 @@ const executableArgs = [
`--ffmpeg-location=${ffmpegPath}`,
];

const { X_OK, R_OK } = fsConstants;
const allowedProtocols = ['http:', 'https:'];
const forbiddenHosts = ['localhost', '127.0.0.1', '0.0.0.0'];
const destPath = '/tmp/';
Expand All @@ -53,7 +54,7 @@ function isFileExecutable(path) {
});
}

function isFileReadable(path) {
export function isFileReadable(path) {
return new Promise(resolve => {
access(path, R_OK, err =>
err
Expand Down Expand Up @@ -124,7 +125,7 @@ function makeCleanup(event, output) {
};
}

async function spawnYouTubeDL(url, req) {
export async function spawnYouTubeDL(url, req) {
let aborted;
if (req) {
req.on('close', () => {
Expand Down Expand Up @@ -224,8 +225,3 @@ async function spawnYouTubeDL(url, req) {

throw new Error('Could not read audio extract file.');
}

module.exports = {
spawnYouTubeDL,
isFileReadable,
};
68 changes: 24 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"postinstall": "[ -z \"$VHOST\" ] && exit 0; npm run build:prod",
"deploy": "ssh $SOUNDSLICE_DEPLOY deploy default.git"
},
"type": "module",
"author": "Timothée 'Tim' Pillard <[email protected]> (https://twitter.com/tpillard)",
"license": "AGPL-3.0-only",
"repository": {
Expand Down Expand Up @@ -68,7 +69,7 @@
"eslint": "^5.16.0",
"file-loader": "^3.0.1",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.11.0",
"node-sass": "^4.12.0",
"nodemon": "^1.18.10",
"normalize.css": "^8.0.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
Expand Down
10 changes: 4 additions & 6 deletions server/api/deleteSlice.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { getClient, query } = require('../db');
const { isFileReadable } = require('../../lib');
const { unlinkAsync } = require('../utils');
import { getClient, query } from '../db/index.js';
import { isFileReadable } from '../../lib/index.js';
import { unlinkAsync } from '../utils.js';

async function deleteSlice(req, res) {
export default async function deleteSlice(req, res) {
const id = req.params.id;

if (!id) {
Expand Down Expand Up @@ -92,5 +92,3 @@ async function deleteSlice(req, res) {
res.writeHead(204);
res.end();
}

module.exports = deleteSlice;
12 changes: 5 additions & 7 deletions server/api/getSlice.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { createReadStream } = require('fs');
const { encode } = require('punycode');
import { createReadStream } from 'fs';
import { encode } from 'punycode';

const { query } = require('../db');
const { isFileReadable } = require('../../lib');
import { query } from '../db/index.js';
import { isFileReadable } from '../../lib/index.js';

async function getSlice(req, res) {
export default async function getSlice(req, res) {
const id = req.params.id;

if (!id) {
Expand Down Expand Up @@ -65,5 +65,3 @@ async function getSlice(req, res) {
console.info('Streaming slice back to client', name);
fileStream.pipe(res);
}

module.exports = getSlice;
8 changes: 3 additions & 5 deletions server/api/headSlice.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { query } = require('../db');
const { isFileReadable } = require('../../lib');
import { query } from '../db/index.js';
import { isFileReadable } from '../../lib/index.js';

async function headSlice(req, res) {
export default async function headSlice(req, res) {
const id = req.params.id;

if (!id) {
Expand Down Expand Up @@ -57,5 +57,3 @@ async function headSlice(req, res) {
res.writeHead(204, headers);
res.end();
}

module.exports = headSlice;
12 changes: 5 additions & 7 deletions server/api/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module.exports = {
getSlice: require('./getSlice'),
headSlice: require('./headSlice'),
deleteSlice: require('./deleteSlice'),
shareSlice: require('./shareSlice'),
link: require('./link'),
};
export { default as getSlice } from './getSlice.js';
export { default as headSlice } from './headSlice.js';
export { default as deleteSlice } from './deleteSlice.js';
export { default as shareSlice } from './shareSlice.js';
export { default as link } from './link.js';
8 changes: 3 additions & 5 deletions server/api/link.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { encode } = require('punycode');
const { spawnYouTubeDL } = require('../../lib');
import { encode } from 'punycode';
import { spawnYouTubeDL } from '../../lib/index.js';

async function link(req, res) {
export default async function link(req, res) {
if (!req.body) return res.sendStatus(400);

const queue = req.app.locals.queue;
Expand All @@ -25,5 +25,3 @@ async function link(req, res) {
res.sendStatus(500);
}
}

module.exports = link;
16 changes: 7 additions & 9 deletions server/api/shareSlice.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const path = require('path');
const { createHash } = require('crypto');
const get = require('lodash/get');
const multiparty = require('multiparty');
import path from 'path';
import { createHash } from 'crypto';
import { get } from 'lodash';
import multiparty from 'multiparty';

const { renameAsync } = require('../utils');
const { getClient } = require('../db');
import { renameAsync } from '../utils.js';
import { getClient } from '../db/index.js';

const env = process.env.NODE_ENV || 'development';

async function shareSlice(req, res) {
export default async function shareSlice(req, res) {
const finalDir =
res.app.locals.config.uploads.path ||
(env === 'development' ? '/tmp' : '/home/hosting-user/uploads');
Expand Down Expand Up @@ -99,5 +99,3 @@ async function shareSlice(req, res) {

return;
}

module.exports = shareSlice;
14 changes: 6 additions & 8 deletions server/assetPath.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require('fs');
const url = require('url');
const path = require('path');
import fs from 'fs';
import url from 'url';
import path from 'path';

const assetsManifestPath = path.join(__dirname, '..', 'dist/assets.json');
const assets = JSON.parse(fs.readFileSync(assetsManifestPath, 'utf8'));
export default function makeAssetPath(base, publicDir) {
const assetsManifestPath = path.join(__dirname, '..', 'dist/assets.json');
const assets = JSON.parse(fs.readFileSync(assetsManifestPath, 'utf8'));

function makeAssetPath(base, publicDir) {
return function assetPath(asset, absolute = true) {
const pathname = assets[asset] || asset;
if (!absolute) {
Expand All @@ -15,5 +15,3 @@ function makeAssetPath(base, publicDir) {
return url.resolve(base, path.join(publicDir, pathname));
};
}

module.exports = makeAssetPath;
13 changes: 4 additions & 9 deletions server/db/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */

const { Pool } = require('pg');
import pg from 'pg';

// FIXME: configuration file
const config = {
Expand All @@ -11,21 +11,16 @@ const config = {
database: 'soundslice',
};

const pool = new Pool(config);
const pool = new pg.Pool(config);

async function query(text, params) {
export async function query(text, params) {
const start = Date.now();
const res = await pool.query(text, params);
const duration = Date.now() - start;
console.info('Executed query', { text, duration, rows: res.rowCount });
return res;
}

function getClient() {
export function getClient() {
return pool.connect();
}

module.exports = {
query,
getClient,
};
Loading