Skip to content

Commit

Permalink
Tsify and refactor lib/handlers/source.js (#4662)
Browse files Browse the repository at this point in the history
  • Loading branch information
junlarsen committed Jan 30, 2023
1 parent 7309443 commit f2fbf69
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
35 changes: 21 additions & 14 deletions lib/handlers/source.js → lib/handlers/source.ts
Expand Up @@ -22,33 +22,40 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import express from 'express';
import _ from 'underscore';

import {Source} from '../sources';

// TODO(supergrecko): Maybe provide a more elegant way to do this instead of accessing keys?
const ALLOWED_ACTIONS = new Set(['list', 'load']);

export class SourceHandler {
constructor(fileSources, addStaticHeaders) {
this.allowedActions = new Set(['list', 'load', 'save']);
this.sourceToHandler = _.indexBy(fileSources, 'urlpart');
this.addStaticHeaders = addStaticHeaders;
public constructor(private fileSources: Source[], private addStaticHeaders: (res: express.Response) => void) {}

private getSourceForHandler(handler: string): Source | null {
const records = _.indexBy(this.fileSources, 'urlpart');
return records[handler] || null;
}

handlerForAction(handler, action) {
return this.allowedActions.has(action) ? handler[action] : null;
private getActionForSource(source: Source, action: string): ((...args: unknown[]) => Promise<unknown>) | null {
return ALLOWED_ACTIONS.has(action) ? source[action] : null;
}

handle(req, res, next) {
const bits = req.url.split('/');
const handler = this.sourceToHandler[bits[1]];
if (!handler) {
public handle(req: express.Request, res: express.Response, next: express.NextFunction): void {
// Split URLs with the scheme /source/browser/list into the source and the action to perform
const [_, handler, action, ...rest] = req.url.split('/');
const source = this.getSourceForHandler(handler);
if (source === null) {
next();
return;
}
const handlerAction = this.handlerForAction(handler, bits[2]);
if (!handlerAction) {
const callback = this.getActionForSource(source, action);
if (callback === null) {
next();
return;
}
handlerAction
.apply(handlerAction, bits.slice(3))
callback(rest)
.then(response => {
this.addStaticHeaders(res);
res.send(response);
Expand Down
1 change: 0 additions & 1 deletion lib/sources/browser.ts
Expand Up @@ -28,7 +28,6 @@ import type {Source, SourceEntry} from './index';
export const browser: Source = {
name: 'Browser',
urlpart: 'browser',
save: undefined,
list(): Promise<Omit<SourceEntry, 'path'>[]> {
return Promise.resolve([]);
},
Expand Down
1 change: 0 additions & 1 deletion lib/sources/builtin.ts
Expand Up @@ -69,5 +69,4 @@ export const builtin: Source = {
file: e.file,
}));
},
save: null,
};
2 changes: 0 additions & 2 deletions lib/sources/index.ts
Expand Up @@ -41,8 +41,6 @@ export interface Source {
urlpart: string;
list(): Promise<Omit<SourceEntry, 'path'>[]>;
load(language: string, filename: string): Promise<{file: string}>;
/** Appears to be unused? */
save: unknown;
}

export const sources = [browser, builtin];
1 change: 0 additions & 1 deletion test/handlers/source-tests.js
Expand Up @@ -35,7 +35,6 @@ describe('Sources', () => {
urlpart: 'moose',
list: () => Promise.resolve({moose: 'pig'}),
load: name => Promise.resolve({file: `File called ${name}`}),
save: null,
},
],
res => res.setHeader('Yibble', 'boing'),
Expand Down

0 comments on commit f2fbf69

Please sign in to comment.