Skip to content

Commit

Permalink
Follow newest HFS change; adjust handler behavior
Browse files Browse the repository at this point in the history
And a 404 page in `bare.tpl`
  • Loading branch information
NaitLee committed Jun 14, 2022
1 parent 1492e90 commit 95520e2
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 63 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.js.map
!bare.tpl
#dist
debug

.directory
thumbs.db
Expand Down
16 changes: 16 additions & 0 deletions dist/bare.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,19 @@ document.addEventListener('DOMContentLoaded', () => {
</table>
</body>
</html>

[not found]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Not Found</title>
<link rel="stylesheet" href="/~style.css" />
</head>
<body>
<p>Not Found</p>
<p><a href="../">&lt; Go Back</a></p>
</body>
</html>

56 changes: 34 additions & 22 deletions dist/tradit-handler.js

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

14 changes: 8 additions & 6 deletions dist/tradit-macros.js

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

62 changes: 41 additions & 21 deletions tradit-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import { Interpreter } from './tradit-interpreter';
import { serialize } from './tradit-serializer';
import { makePathConsistent } from './tradit-misc';

export interface SendListReadable extends Readable {
[Symbol.asyncIterator](): AsyncIterableIterator<SendListEntry<FileEntry>>;
read(size?: number): SendListEntry<FileEntry>;
}

export class ReadableForMacros extends Readable {
ctx: KoaContext;
constructor(options: ReadableOptions & { ctx: KoaContext }) {
super(options);
this.ctx = options.ctx;
}
async put(item: any) {
// Note: this is useless. Will find a better solution
if (!this.push(item)) {
await once(this, 'readable');
}
Expand Down Expand Up @@ -79,39 +85,48 @@ export class Handler {
) return;
let section_name = ctx.path.startsWith(SECTION_URI) ? ctx.path.slice(2) : '';
let id = this.interpreter.getSectionIndex(section_name);
let entry_generator: FileEntryGenerator | APIError | null = null;
entry_generator =
this.interpreter.template.params[id].no_list || section_name
let allow_private_section = false;
let readable_list: SendListReadable | null = null;
readable_list =
(this.interpreter.template.params[id].no_list || section_name !== '')
? null
: await HFS.file_list<FileEntryGenerator>(
: await HFS.file_list<SendListReadable>(
{ path: ctx.path, omit: 'c', sse: true },
ctx
);
if (entry_generator instanceof Error) {
switch (ctx.status = entry_generator.status) {
case 404:
section_name = 'not found';
break;
case 401:
do { // for good code by early break
if (readable_list === null) break;
await once(readable_list, 'readable');
let possible_error = readable_list.read();
if (possible_error === null) break; // TODO: is this possible?
if (possible_error.error === undefined) {
readable_list.unshift(possible_error); // not an error
break;
}
switch (ctx.status = possible_error.error) {
case API.const.UNAUTHORIZED:
section_name = 'unauth';
break;
case 403:
case API.const.FORBIDDEN:
section_name = 'forbidden';
break;
case 400:
// section_name = 'bad request';
section_name = 'not found';
break;
case 418:
// bad request
return;
case 418:
// potential attack, or tea pot
return true;
default:
section_name = 'not found';
}
entry_generator = null;
}
if (!this.interpreter.hasSection(section_name)) {
readable_list = null;
allow_private_section = true;
} while (false);
if (!this.interpreter.hasSection(section_name, allow_private_section)) {
ctx.status = 404;
section_name = 'not found';
}
if (!this.interpreter.hasSection(section_name)) return void(ctx.body = 'not found');
if (!this.interpreter.hasSection(section_name, allow_private_section)) return;
let generator: AsyncGenerator<boolean>;
const step: number = 64;
let readable = new ReadableForMacros({
Expand All @@ -125,8 +140,13 @@ export class Handler {
},
ctx: ctx
});
generator = this.interpreter.getSectionGenerator(section_name, readable, entry_generator) as AsyncGenerator<boolean>;
if (!generator) return void(ctx.body = 'not found');
generator = this.interpreter.getSectionGenerator(
section_name,
readable,
readable_list,
allow_private_section
) as AsyncGenerator<boolean>;
if (!generator) return;
ctx.status = 200;
ctx.type = mimetype(ctx.path);
ctx.body = readable;
Expand Down
10 changes: 5 additions & 5 deletions tradit-interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { Macro, Macros } from "./tradit-macros";
import { ItemRole as ItemRole, NULL_INT, NULL_NUMBER, NULL_STRING, StackType } from "./tradit-constants";
import { ReadableForMacros } from "./tradit-handler";
import { ReadableForMacros, SendListReadable } from "./tradit-handler";
import { groupToString } from "./tradit-misc";
// import { Worker, isMainThread, parentPort, workerData } from "worker_threads";

Expand Down Expand Up @@ -61,7 +61,7 @@ export interface MacroRoutineContext {
/** interpreter instance */
i: Interpreter;
/** file entry generator, if available */
l: FileEntryGenerator | null;
l: SendListReadable | null;
/** koa context */
c: KoaContext;
/** pass-through readable body */
Expand Down Expand Up @@ -428,7 +428,7 @@ export class Interpreter {
r: false, t: this.template, i: this
};
}
newRoutineContext(readable: ReadableForMacros, entry_generator: FileEntryGenerator | null): MacroRoutineContext {
newRoutineContext(readable: ReadableForMacros, entry_generator: SendListReadable | null): MacroRoutineContext {
return {
s: new MacroStack(), l: entry_generator,
vn: Object.setPrototypeOf({}, this.globalVariables.n),
Expand Down Expand Up @@ -483,13 +483,13 @@ export class Interpreter {
ctx.s = old_stack;
return result;
}
getSectionGenerator(name: string, readable: ReadableForMacros, entry_generator: FileEntryGenerator | null, allow_private = false) {
getSectionGenerator(name: string, readable: ReadableForMacros, entry_generator: SendListReadable | null, allow_private = false) {
let index = this.getSectionIndex(name, allow_private);
if (index === -1) return null;
let ctx = this.newRoutineContext(readable, entry_generator);
return this.getGroupGenerator(index, ctx);
}
getSection(name: string, readable: ReadableForMacros, entry_generator: FileEntryGenerator | null, allow_private = false) {
getSection(name: string, readable: ReadableForMacros, entry_generator: SendListReadable | null, allow_private = false) {
let index = this.getSectionIndex(name, allow_private);
if (index === -1) return null;
let ctx = this.newRoutineContext(readable, entry_generator);
Expand Down
13 changes: 7 additions & 6 deletions tradit-macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,14 @@ new Macro('list', c => {
t_folder = await d.i.getSectionAsText('folder', d, true);
t_link = await d.i.getSectionAsText('link', d, true);
let value: string, result: string = '';
for await (let { entry } of d.l) {
for await (let { add } of d.l) {
if (d.c.aborted) break;
value = (entry.s === undefined ? t_folder : t_file)
.replace('%item-url%', d.c.path + entry.n)
.replace('%item-name%', entry.n)
.replace('%item-modified%', entry.m?.toLocaleString() ?? '')
.replace('%item-size%', smartSize(entry.s ?? 0));
if (add === undefined) continue;
value = (add.s === undefined ? t_folder : t_file)
.replace('%item-url%', d.c.path + add.n)
.replace('%item-name%', add.n)
.replace('%item-modified%', add.m?.toLocaleString() ?? '')
.replace('%item-size%', smartSize(add.s ?? 0));
pass ? await d.p.put(value) : result += value;
}
if (!pass) d.s.pushString(result);
Expand Down
13 changes: 10 additions & 3 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ type AssemblizedTemplate = {
* HFS internal API
*/
interface HFS<TypeofFS> {
file_list<T extends FileEntryGenerator | FileEntryList>(
file_list<T>(
args: {
path: string;
omit: 'c';
sse: boolean;
},
ctx: KoaContext
): Promise<T | APIError>;
): Promise<T>;
fs: TypeofFS;
auth?: {
refresh_session({}, ctx: KoaContext): Promise<{
Expand Down Expand Up @@ -125,8 +125,15 @@ interface FileEntry {
s?: number | undefined;
}

interface SendListEntry<T> {
add?: T;
remove?: T;
update?: T;
error?: number; // string | number;
}

type FileEntryGenerator = AsyncGenerator<{ entry: FileEntry }>;
type FileEntryList = { list: FileEntry[] };
type FileEntryList = { list: FileEntry[] } | APIError;

interface APIError extends Error {
status: number;
Expand Down

0 comments on commit 95520e2

Please sign in to comment.