Browse Source

Should be the last of the eslint complaints for the .js files.

dev
Zed A. Shaw 4 months ago
parent
commit
900fa56f8f
  1. 6
      .eslintrc.json
  2. 28
      client/api.js
  3. 1
      client/assert.js
  4. 5
      client/lib/helpers.js
  5. 89
      client/media.js
  6. 2
      client/stores.js
  7. 2
      services/api.js
  8. 2
      services/qserver.js
  9. 27
      services/tracker.js
  10. 4
      socket/chat.js
  11. 3
      tests/models/media.js
  12. 4
      tests/models/payment.js
  13. 5
      tests/models/user.js
  14. 8
      tests/models/user_payment.js
  15. 3
      tests/ui/login.js
  16. 4
      tests/ui/register.js

6
.eslintrc.json

@ -90,7 +90,7 @@
"error",
"unix"
],
"lines-around-comment": "error",
"lines-around-comment": "off",
"lines-around-directive": "error",
"lines-between-class-members": "error",
"max-classes-per-file": "off",
@ -171,7 +171,7 @@
"no-restricted-modules": "error",
"no-restricted-properties": "error",
"no-restricted-syntax": "error",
"no-return-assign": "error",
"no-return-assign": "warn",
"no-return-await": "off",
"no-script-url": "error",
"no-self-compare": "error",
@ -228,7 +228,7 @@
"radix": "error",
"require-atomic-updates": "warn",
"require-await": "off",
"require-jsdoc": "error",
"require-jsdoc": "off",
"require-unicode-regexp": "error",
"rest-spread-spacing": "error",
"semi": "off",

28
client/api.js

@ -2,21 +2,15 @@ const MOCK_ROUTES = {};
import { user } from "./stores.js";
export const logout_user = async () => {
user.update(u => ({authenticated: false}));
let [status, data] = await get('/api/logout');
window.location.replace("/client/#/login");
}
export const mock = (config) => {
for(let route in config) {
MOCK_ROUTES[route] = config[route];
for(let [route, value] of Object.entries(config)) {
MOCK_ROUTES[route] = value;
}
}
export const raw_mock = (url, method, body, unauthed_action) => {
let error;
method = method.toLowerCase();
export const raw_mock = (url, raw_method, body, unauthed_action) => {
let error = undefined;
const method = raw_method.toLowerCase();
const config = MOCK_ROUTES[url];
const data = config[method];
@ -77,6 +71,7 @@ export const raw = async (url, method, body, unauthed_action) => {
return [res.status, JSON.parse(text)];
} catch(error) {
console.error(error, "Failed to parse reply body as JSON. Text is:", text, "error", error, "URL", url);
return [500, {"message": "Exception processing request. See console log."}];
}
}
}
@ -91,6 +86,17 @@ export const del = async (url) => await raw(url, "DELETE");
export const options = async (url) => await raw(url, "OPTIONS");
export const logout_user = async () => {
user.update(() => {
return {authenticated: false}
});
let [status, data] = await get('/api/logout');
if(status !== 200) console.error("Invalid status from logout", status, data);
window.location.replace("/client/#/login");
}
export default {
post, get, put, del, mock, options
}

1
client/assert.js

@ -4,6 +4,7 @@ class AssertionError extends Error {
/* This is from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
* which claims you have to do this weird stuff to make your error actually work.
*/
constructor(foo = 'bar', ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);

5
client/lib/helpers.js

@ -1,6 +1,6 @@
export function inject_remote(document, url, ready_cb=undefined) {
let head = document.getElementsByTagName('head')[0];
export const inject_remote = (document, url, ready_cb=undefined) => {
let head = document.getElementsByTagName('head')[0];
let script = document.createElement('script');
if(ready_cb) script.onload = ready_cb;
script.src=url;
@ -15,4 +15,3 @@ export const random_numbers = (count) => {
let out = new Uint32Array(count);
return window.crypto.getRandomValues(out);
}

89
client/media.js

@ -5,7 +5,23 @@ import { webtorrent, base_host } from '$/client/config.js';
// it's much faster to set it as a script variable in the index.html but that might cause loading problems
// WebTorrent then comes from the public/index.html file, not from an import in here
let LAZY_CLIENT; // this is lazy loaded by getClient
let LAZY_CLIENT = undefined; // this is lazy loaded by getClient
const getClient = () => {
// super hacks!
if(LAZY_CLIENT) {
return LAZY_CLIENT;
} else {
// eslint will complain but webtorrent is loaded by <script>
let client = new WebTorrent({dht: webtorrent.use_dht});
// would need to do some kind of global status for these since it's not per torrent
client.on('error', err => console.error(err));
client.on('listening', () => console.log('LISTENING:'));
LAZY_CLIENT = client;
return LAZY_CLIENT;
}
}
export class WTEvents {
constructor(media) {
@ -17,28 +33,36 @@ export class WTEvents {
setTorrent(torrent) {
// TODO: refine this
this.media.torrent = torrent;
this.torrent = torrent;
this.torrent = torrent;
}
videoReady(file) {
this.media.kind = 'video';
file.renderTo('#' + this.appendId, {autoplay: false, controls: true, muted: false});
file.renderTo(`#${this.appendId}`, {autoplay: false, controls: true, muted: false});
}
audioReady(file) {
this.media.kind = 'audio';
file.renderTo('#' + this.appendId, {autoplay: false, controls: true, muted: true});
file.renderTo(`#${this.appendId}`, {autoplay: false, controls: true, muted: true});
}
downloadReady(file) {
file.renderTo('#' + this.appendId, (err, elem) => {
file.renderTo(`#${this.appendId}`, (err, elem) => {
// TODO: hmmm do we need to do anything here?
// keep this active for 60 second
setTimeout(() => client.remove(torrent.infoHash), seedTimeout);
if(err) {
console.error(err);
} else {
console.log("downloadReady", elem);
// keep this active for 60 second
let client = getClient();
setTimeout(() => client.remove(this.torrent.infoHash), seedTimeout);
}
});
}
done() { }
done() {
console.log("done");
}
infoHash(hash) {
this.media.info_hash = hash;
@ -48,38 +72,42 @@ export class WTEvents {
this.media.metadata = data;
}
ready() {}
warning(msg) { console.log("WT WARNING", msg); }
error(msg) { console.log("WT ERROR", msg); }
ready() {
return true;
}
download(bytes) { }
warning(msg) {
console.log("WT WARNING", msg);
}
upload(bytes) { }
error(msg) {
console.log("WT ERROR", msg);
}
wire(wire) { }
download(bytes) {
return bytes !== undefined;
}
noPeers(announceType) { console.log('noPeers from', announceType); }
upload(bytes) {
return bytes !== undefined;
}
client_error(err) { }
}
wire(wire) {
// this weird idiom is to make eslint shut up for these placeholders
return wire !== undefined;
}
const getClient = () => {
// super hacks!
if(LAZY_CLIENT) {
return LAZY_CLIENT;
} else {
let client = new WebTorrent({dht: webtorrent.use_dht});
noPeers(announceType) {
console.log('noPeers from', announceType);
}
// would need to do some kind of global status for these since it's not per torrent
client.on('error', err => console.log('ERROR: ' + err.message));
client.on('listening', () => console.log('LISTENING:'));
LAZY_CLIENT = client;
return LAZY_CLIENT;
client_error(err) {
console.error(err, "client error");
return true;
}
}
const handle_torrent = (media, torrent, events) => {
torrent.files.forEach((file) => {
media.file_name = file.name;
@ -131,4 +159,3 @@ export const removeMedia = (media) => {
console.error("removeMedia called with a media object without a torrent", media);
}
}

2
client/stores.js

@ -1,6 +1,5 @@
import { writable, readable } from 'svelte/store';
import { io } from 'socket.io-client';
import { push } from 'svelte-spa-router';
export const user = writable({
authenticated: undefined, // yes, this is on purpose
@ -16,4 +15,3 @@ export const reconnect_socket = () => {
raw_socket.disconnect();
raw_socket.connect();
}

2
services/api.js

@ -18,7 +18,7 @@ import fs from "fs";
const app = express();
const http_server = http.createServer(app);
const io = new SocketIO.Server(http_server);
const log = logging.create(import.meta.url);
const log = logging.create("/services/api.js");
const API_BASE = path.resolve("./api");
const SOCKET_BASE = path.resolve("./socket");
const socket_routes = {};

2
services/qserver.js

@ -5,7 +5,7 @@ import path from 'path';
const ACTIVE_QUEUES = {};
const log = logging.create(import.meta.url);
const log = logging.create("/services/qserver.js");
glob("queues/*.js", {}, (err, files) => {
for(let file_name of files) {

27
services/tracker.js

@ -1,56 +1,57 @@
import { Server } from 'bittorrent-tracker';
const port = 9001;
const hostname = '0.0.0.0';
import assert from "assert";
const server = new Server({
udp: false, // enable udp server? [default=true]
http: true, // enable http server? [default=true]
ws: true, // enable websocket server? [default=true]
stats: true, // enable web-based statistics? [default=true]
filter: function (infoHash, params, cb) {
filter: (infoHash, params, cb) => {
console.log("----------------------------------------------");
console.debug(params);
cb(null)
}
})
server.on('error', function (err) {
server.on('error', (err) => {
// fatal server error!
console.log(err.message)
})
server.on('warning', function (err) {
server.on('warning', (err) => {
// client sent bad data. probably not a problem, just a buggy client.
console.log(err.message)
})
server.on('listening', function () {
server.on('listening', () => {
// fired when all requested servers are listening
console.log('listening on port:' + server.http.address().port)
console.log('listening on port:', server.http.address().port)
})
// start tracker server listening! Use 0 to listen on a random free port.
server.listen(port, hostname, function (what) {
console.log("Listening....");
server.listen(port, hostname, (what) => {
console.log("Listening....", what);
})
// listen for individual tracker messages from peers:
server.on('start', function (addr) {
console.log('got start message from ' + addr)
server.on('start', (addr) => {
console.log('got start message from', addr)
})
server.on('complete', function (addr) {
server.on('complete', (addr) => {
console.log('complete', addr);
});
server.on('update', function (addr) {
server.on('update', (addr) => {
console.log('update', addr);
});
server.on('stop', function (addr) {
server.on('stop', (addr) => {
console.log('stop', addr);
});
const onHttpReq = server.onHttpRequest.bind(server);
assert(onHttpReq !== undefined, "Bad call to bind.");

4
socket/chat.js

@ -1,4 +1,3 @@
import * as auth from '../lib/auth.js';
import * as models from '../lib/models.js';
export const message = async (io, socket, data) => {
@ -7,6 +6,3 @@ export const message = async (io, socket, data) => {
}
message.authenticated = true;

3
tests/models/media.js

@ -1,7 +1,4 @@
import test from "ava";
import {sleep} from "../../lib/testing.js";
import faker from "faker";
import {knex} from "../../lib/ormish.js";
import { Media } from "../../lib/models.js";
test('test media model basics work', async (t) => {

4
tests/models/payment.js

@ -1,7 +1,4 @@
import test from "ava";
import {sleep} from "../../lib/testing.js";
import faker from "faker";
import {knex} from "../../lib/ormish.js";
import { Payment } from "../../lib/models.js";
test("quick check the internal uuid generation", (t) => {
@ -56,4 +53,3 @@ test('test payment model basics work', async (t) => {
console.log(error);
}
})

5
tests/models/user.js

@ -1,7 +1,5 @@
import test from "ava";
import {sleep, random_user} from "../../lib/testing.js";
import faker from "faker";
import {knex} from "../../lib/ormish.js";
import {random_user} from "../../lib/testing.js";
import { User, Payment } from "../../lib/models.js";
test('test user model basics work', async (t) => {
@ -46,4 +44,3 @@ test('test user model basics work', async (t) => {
console.log(error);
}
})

8
tests/models/user_payment.js

@ -1,7 +1,5 @@
import test from "ava";
import {sleep, random_user} from "../../lib/testing.js";
import faker from "faker";
import {knex} from "../../lib/ormish.js";
import {random_user} from "../../lib/testing.js";
import { User, Payment, UserPayment } from "../../lib/models.js";
test('test user can have multiple payments', async (t) => {
@ -24,11 +22,15 @@ test('test user can have multiple payments', async (t) => {
payment_id: pay1.id
});
t.not(up1, undefined);
let up2 = await UserPayment.insert({
user_id: buyer.id,
payment_id: pay2.id
});
t.not(up2, undefined);
// test payment has user
// TODO: test payment with multiple users
let pay1_users = await pay1.users();

3
tests/ui/login.js

@ -2,10 +2,13 @@ import test from 'ava';
import {sleep, expect, wait, tid, playstart} from '../../lib/testing.js';
test.before(async t => t.context = await playstart('http://localhost:5001/client/#/login/'));
test.after(async t => await t.context.browser.close());
test('standalone login page works', async (t) => {
const {browser, context, p} = t.context;
t.not(browser, undefined);
t.not(context, undefined);
try {
// login page loads

4
tests/ui/register.js

@ -4,10 +4,12 @@ import faker from 'faker';
import {knex} from '../../lib/ormish.js';
test.before(async t => t.context = await playstart('http://localhost:5001/client/#/register/'));
test.after(async t => await t.context.browser.close());
test('register form error', async (t) => {
const {browser, context, p} = t.context;
t.not(context, undefined);
try {
// confirm we get errors
@ -37,6 +39,8 @@ test('register form error', async (t) => {
await sleep(100);
const res = await knex('user').where({email: user.email}).select();
t.not(res, undefined);
} catch (error) {
console.log(error);
} finally {

Loading…
Cancel
Save