Skip to content

Commit

Permalink
Update ALL the dependencies!
Browse files Browse the repository at this point in the history
  • Loading branch information
pbock committed Dec 13, 2023
1 parent 7efb9d3 commit 3cacab6
Show file tree
Hide file tree
Showing 10 changed files with 2,741 additions and 3,739 deletions.
36 changes: 17 additions & 19 deletions find-orphaned-files.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
'use strict';

import path = require('path');
import _ = require('lodash');
import { resolve } from 'path';
import TalkModel from './models/talks';

const schedulePath = path.resolve(__dirname, 'schedule.json');
const filesBase = path.resolve(__dirname, 'files/');
const schedulePath = resolve(__dirname, 'schedule.json');
const filesBase = resolve(__dirname, 'files/');

const Talk = TalkModel(schedulePath, filesBase, false);

Promise.all([ Talk.all(), Talk._getAllFiles() ])
.then(([ talks, files ]) => {
_(files)
.each((meta, filePath) => {
if (!meta.isDir) return;
// Ignore first-level directories
const nestLevel = filePath.split('/').length - schedulePath.split('/').length;
if (nestLevel < 2) return;
Promise.all([Talk.all(), Talk._getAllFiles()])
.then(([talks, files]) => {
Object.entries(files).forEach(([filePath, meta]) => {
if (!meta.isDir) return;
// Ignore first-level directories
const nestLevel = filePath.split('/').length - schedulePath.split('/').length;
if (nestLevel < 2) return;

const matchingTalk = _(talks).find(t => t.filePath === filePath);
if (!matchingTalk) {
console.warn(filePath);
}
})
})
.then(() => process.exit())
const matchingTalk = talks.find((t) => t.filePath === filePath);
if (!matchingTalk) {
console.warn(filePath);
}
});
})
.then(() => process.exit());
48 changes: 24 additions & 24 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import path = require('path');
import bunyan = require('bunyan');
import express = require('express');
import multer = require('multer');
import i18n = require('i18n');
import URL = require('url');
import moment = require('moment');
import _ = require('lodash');
import archiver = require('archiver');
import * as path from 'path';
import * as bunyan from 'bunyan';
import * as express from 'express';
import * as multer from 'multer';
import * as i18n from 'i18n';
import * as URL from 'url';
import * as moment from 'moment';
import * as _ from 'lodash';
import * as archiver from 'archiver';

// Middleware
import cookieParser = require('cookie-parser');
import helmet = require('helmet');
import basicAuth = require('basic-auth');
import * as cookieParser from 'cookie-parser';
import helmet from 'helmet';
import * as basicAuth from 'basic-auth';

// Models
import TalkModel, { TalkFile } from './models/talks';
Expand Down Expand Up @@ -106,7 +106,7 @@ function checkAuth(
return authorized;
}

app.use(cookieParser());
app.use(cookieParser() as any);
app.use(checkAuth);

app.use((req, res, next) => {
Expand All @@ -128,16 +128,16 @@ app.set('view engine', 'pug');

app.use(
'/vendor/bootstrap',
express.static(path.resolve(__dirname, 'node_modules/bootstrap/dist/'))
express.static(path.resolve(__dirname, 'node_modules/bootstrap/dist/')) as any
);
app.use('/static', express.static(path.resolve(__dirname, 'static/')));
app.use('/static', express.static(path.resolve(__dirname, 'static/')) as any);

app.locals.moment = moment;

app.get('/', (req: PotentiallyAuthenticatedRequest, res) => {
const { isAuthorized } = req;
const scheduleVersion = Talk.getScheduleVersion();
return Talk.allSorted().then(talks =>
return Talk.allSorted().then((talks) =>
res.render('index', { talks, isAuthorized, scheduleVersion })
);
});
Expand All @@ -161,7 +161,7 @@ app.get('/talks/:slug', (req: PotentiallyAuthenticatedRequest, res, next) => {
return (
Talk.findBySlug(req.params.slug)
.then(ensureExistence)
.then(async talk => {
.then(async (talk) => {
if (req.isAuthorized) {
const comments = await talk.getComments();
// FIXME: This destroys the getter
Expand All @@ -183,7 +183,7 @@ app.get('/talks/:slug', (req: PotentiallyAuthenticatedRequest, res, next) => {
.catch(() =>
Talk.findById(req.params.slug)
.then(ensureExistence)
.then(talk => res.redirect(`/talks/${talk.slug}/`))
.then((talk) => res.redirect(`/talks/${talk.slug}/`))
)
.catch(next)
);
Expand All @@ -193,7 +193,7 @@ app.get('/sign-in', forceAuth, (req, res) => {
res.redirect('/');
});

app.post('/talks/:slug/files/', upload.any(), (req, res, next) => {
app.post('/talks/:slug/files/', upload.any() as any, (req, res, next) => {
let requestTalk;
const { body } = req;
const files = req.files as Express.Multer.File[];
Expand All @@ -205,19 +205,19 @@ app.post('/talks/:slug/files/', upload.any(), (req, res, next) => {
log.info({ files, body }, 'Files received');
return Talk.findBySlug(req.params.slug)
.then(ensureExistence)
.then(talk => {
.then((talk) => {
requestTalk = talk;
const tasks = [];
if (files.length) tasks.push(talk.addFiles(files));
if (body.comment) tasks.push(talk.addComment(body.comment));
return Promise.all(tasks).then(() => talk);
})
.then(talk => {
.then((talk) => {
res.redirect(
`/talks/${talk.slug}/?uploadCount=${files.length}&commentCount=${body.comment ? '1' : '0'}`
);
})
.catch(err => {
.catch((err) => {
log.error(err, 'Failed to add files');
next(err);
});
Expand All @@ -226,7 +226,7 @@ app.post('/talks/:slug/files/', upload.any(), (req, res, next) => {
app.get('/talks/:slug/files.zip', forceAuth, (req, res, next) => {
return Talk.findBySlug(req.params.slug)
.then(ensureExistence)
.then(talk => {
.then((talk) => {
const archive = archiver('zip');
archive.directory(talk.filePath, '/');
archive.on('error', next);
Expand All @@ -243,7 +243,7 @@ app.get('/talks/:slug/files.zip', forceAuth, (req, res, next) => {
app.get('/talks/:slug/files/:filename', forceAuth, (req, res, next) => {
return Talk.findBySlug(req.params.slug)
.then(ensureExistence)
.then(talk => {
.then((talk) => {
const file = _.find(talk.files, { name: req.params.filename }) as TalkFile;
if (!file) {
const error = new Error404('File not found');
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/redact-filename.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import redactFilename from '../react-filename';
import redactFilename from '../redact-filename';

describe('redactFilename', () => {
it('only shows the extension and the first and last two characters', () => {
Expand Down
6 changes: 3 additions & 3 deletions lib/react-filename.ts → lib/redact-filename.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path = require('path');
import { extname, basename } from 'path';

export default function redactFilename(filename: string): string {
const extension = path.extname(filename);
const base = path.basename(filename, extension);
const extension = extname(filename);
const base = basename(filename, extension);
if (base.length < 5) return base + extension;
return `${base.substr(0, 2)}[…]${base.substr(-2)}${extension}`;
}
13 changes: 6 additions & 7 deletions lib/stream-hash.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
'use strict';

import crypto = require('crypto');
import fs = require('fs');
import { createHash, type Encoding } from 'crypto';
import { createReadStream } from 'fs';
import { Stream } from 'stream';
import { Utf8AsciiLatin1Encoding } from 'crypto';

export default function streamHash(
firstParam: Stream | string,
algorithm = 'sha1',
encoding: Utf8AsciiLatin1Encoding = 'utf8'
encoding: Encoding = 'utf8'
): Promise<string> {
const stream = typeof firstParam === 'string' ? fs.createReadStream(firstParam) : firstParam;
const hash = crypto.createHash(algorithm);
const stream = typeof firstParam === 'string' ? createReadStream(firstParam) : firstParam;
const hash = createHash(algorithm);
return new Promise((resolve, reject) => {
stream.on('data', chunk => hash.update(chunk, encoding));
stream.on('data', (chunk) => hash.update(chunk, encoding));
stream.on('end', () => resolve(hash.digest('hex')));
stream.on('error', reject);
});
Expand Down
4 changes: 2 additions & 2 deletions lib/wait-promise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function wait(timeout: number) {
return function<T>(arg?: T): Promise<T> {
return new Promise(resolve => setTimeout(() => resolve(arg), timeout));
return function <T>(arg?: T): Promise<T> {
return new Promise((resolve) => setTimeout(() => resolve(arg as T), timeout));
};
}
50 changes: 25 additions & 25 deletions models/talks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';

import chokidar = require('chokidar');
import bunyan = require('bunyan');
import path = require('path');
import fs = require('fs-promise');
import _ = require('lodash');
import { Stats } from 'fs-promise';
import * as chokidar from 'chokidar';
import * as bunyan from 'bunyan';
import * as path from 'path';
import * as fs from 'fs/promises';
import { createReadStream, type Stats, type ReadStream } from 'fs';
import * as _ from 'lodash';

import slugify from '../lib/slugify';
import streamHash from '../lib/stream-hash';
import sortTitle from '../lib/sort-title';
import redactFilename from '../lib/react-filename';
import redactFilename from '../lib/redact-filename';
import wait from '../lib/wait-promise';

const COMMENT_EXTENSION = '.comment.txt';
Expand All @@ -29,7 +29,7 @@ export class TalkFile {
}

read() {
return fs.createReadStream(this.path);
return createReadStream(this.path);
}
}

Expand Down Expand Up @@ -115,10 +115,10 @@ export default function TalkModel(
this.filesCache = _(files)
.map((meta, filePath) => ({ meta, path: filePath }))
.filter(
file =>
(file) =>
!file.meta.isDir && !file.meta.isComment && file.path.indexOf(this.filePath) === 0
)
.map(file => new TalkFile(file.path, file.meta))
.map((file) => new TalkFile(file.path, file.meta))
.value();
this.filesCacheLastUpdated = Date.now();
}
Expand All @@ -128,7 +128,7 @@ export default function TalkModel(
get commentFiles(): TalkFile[] {
if (!this.commentFilesCache || this.commentFilesCacheLastUpdated < filesLastUpdated) {
this.commentFilesCache = _.map(files, (info, filePath) => ({ info, path: filePath }))
.filter(file => file.info.isComment && file.path.indexOf(this.filePath) === 0)
.filter((file) => file.info.isComment && file.path.indexOf(this.filePath) === 0)
.map((file: { info: FileInfo; path: string }) => new TalkFile(file.path, file.info));
this.commentFilesCacheLastUpdated = Date.now();
}
Expand All @@ -138,13 +138,13 @@ export default function TalkModel(
// This is NOT a getter because it is asynchronous
getComments(): Promise<{ body: Buffer; info: FileInfo }[]> {
const commentPromises = _.map(files, (info, filePath) => ({ info, path: filePath }))
.filter(file => file.info.isComment && file.path.indexOf(this.filePath) === 0)
.map(file => fs.readFile(file.path).then(body => ({ body, info: file.info })));
.filter((file) => file.info.isComment && file.path.indexOf(this.filePath) === 0)
.map((file) => fs.readFile(file.path).then((body) => ({ body, info: file.info })));
return Promise.all(commentPromises);
}

readFile(name: string): fs.ReadStream {
return fs.createReadStream(path.resolve(this.filePath, name));
readFile(name: string): ReadStream {
return createReadStream(path.resolve(this.filePath, name));
}

async addComment(comment: string): Promise<this> {
Expand All @@ -154,7 +154,7 @@ export default function TalkModel(

addFiles(files: Express.Multer.File[]) {
return Promise.all(
files.map(file => fs.rename(file.path, path.resolve(this.filePath, file.originalname)))
files.map((file) => fs.rename(file.path, path.resolve(this.filePath, file.originalname)))
)
.then(wait(100)) // HACK: prevent Promise from resolving before watcher fired and file list has been rebuilt
.then(() => this);
Expand Down Expand Up @@ -198,19 +198,19 @@ export default function TalkModel(
const v: string[] = [];

await Promise.all(
scheduleJsonPaths.map(path =>
scheduleJsonPaths.map((path) =>
fs
.readFile(path)
.then(buffer => JSON.parse(buffer.toString()))
.then((buffer) => JSON.parse(buffer.toString()))
.then(({ schedule }) => {
try {
v.push(`${schedule.conference.acronym}: ${schedule.version}`);
} catch (e) {
log.warn(e);
}
_.each(schedule.conference.days, day => {
_.each(day.rooms, talks => {
_.each(talks, talk => {
_.each(schedule.conference.days, (day) => {
_.each(day.rooms, (talks) => {
_.each(talks, (talk) => {
new Talk(talk, day.index);
});
});
Expand All @@ -221,12 +221,12 @@ export default function TalkModel(

versionInformation = v.sort().join('; ');
sortedTalks = _.sortBy(talks, 'sortTitle');
await Promise.all(talks.map(t => fs.ensureDir(t.filePath)));
await Promise.all(talks.map((t) => fs.mkdir(t.filePath, { recursive: true })));
log.info('Done updating talks');
}

let isInitialScan = true;
const filesReady = new Promise(resolve => {
const filesReady = new Promise<void>((resolve) => {
const fileWatcher = chokidar.watch(fileRootPath, {
alwaysStat: true,
ignored: '**/.DS_Store',
Expand All @@ -237,7 +237,7 @@ export default function TalkModel(
.on('unlink', removeFile)
.on('addDir', addDir)
.on('unlinkDir', removeDir)
.on('error', e => {
.on('error', (e) => {
log.error(e);
process.exit(1);
})
Expand All @@ -261,7 +261,7 @@ export default function TalkModel(
files[p] = { stats, isComment, isDir: false, hash: null };
filesLastUpdated = Date.now();
streamHash(p)
.then(hash => {
.then((hash) => {
// In the meantime, the file may have been deleted, in which case
// attempting to write the hash would throw an error.
if (!files[p]) return;
Expand Down
Loading

0 comments on commit 3cacab6

Please sign in to comment.