Skip to content

Commit

Permalink
reorganized lib lgtm
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Dec 19, 2021
1 parent 0c12660 commit b2615ad
Show file tree
Hide file tree
Showing 35 changed files with 106 additions and 77 deletions.
4 changes: 2 additions & 2 deletions fart_server/bonus_features/shortlinks/shortlinks.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/design": "https://docs.google.com/document/d/1pGNLsDr-WysIIqB4nc1pTCL8FMmPxkJMNoGsRMkA0TY/edit",
"/github": "https://github.com/EthanThatOneKid/fart/",
"/author": "https://etok.codes/"
"/github": "https://github.com/EthanThatOneKid/fart",
"/author": "https://etok.codes"
}
34 changes: 33 additions & 1 deletion fart_server/bonus_features/shortlinks/shortlinks.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
import { assertEquals } from "../../../deps/std/testing.ts";
import { redirectIfShortlink } from "./shortlinks.ts";
import shortlinks from "./shortlinks.json" assert { type: "json" };

// TODO(@ethanthatonekid): write tests for shortlinks.ts
Deno.test("shortlink redirects to GitHub repository", () => {
const request = new Request("http://localhost:8080/github");
const response = redirectIfShortlink(request);
assertEquals(response?.status, 302);
assertEquals(response?.headers.get("location"), shortlinks["/github"] + "/");
});

Deno.test("shortlink redirects to GitHub repository and preserves path", () => {
const request = new Request(
"http://localhost:8080/github/milestone/1?closed=1",
);
const response = redirectIfShortlink(request);
assertEquals(response?.status, 302);
assertEquals(
response?.headers.get("location"),
shortlinks["/github"] +
"/milestone/1?closed=1",
);
});

Object.entries(shortlinks)
.forEach(([shortlink, destination]) => {
// add a slash if the destination doesn't end with one
if (!destination.endsWith("/")) destination += "/";

Deno.test(`shortlink ${shortlink} redirects to ${destination}`, () => {
const request = new Request("http://localhost:8080" + shortlink);
const response = redirectIfShortlink(request);
assertEquals(response?.status, 302);
assertEquals(response?.headers.get("location"), destination);
});
});
17 changes: 9 additions & 8 deletions fart_server/bonus_features/shortlinks/shortlinks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO(@ethanthatonekid): write implementation for shortlinks.test.ts
import shortlinks from "./shortlinks.json" assert { type: "json" };

const map = Object.entries(shortlinks)
Expand All @@ -8,13 +7,15 @@ const map = Object.entries(shortlinks)
}, new Map<string, string>());

export const redirectIfShortlink = (request: Request): Response | null => {
const { pathname } = new URL(request.url);
if (pathname.includes("?")) {
const query = pathname.slice(pathname.indexOf("?"));
const shortlink = map.get(pathname.slice(0, pathname.indexOf("?")));
if (shortlink !== undefined) return Response.redirect(shortlink + query);
const { pathname, searchParams } = new URL(request.url);
for (const [shortlink, target] of map) {
if (pathname.startsWith(shortlink)) {
let destination = target + pathname.slice(shortlink.length);
if (searchParams.toString()) destination += "?" + searchParams;
// add a slash if the destination doesn't end with one
else if (!destination.endsWith("/")) destination += "/";
return Response.redirect(destination, 302);
}
}
const shortlink = map.get(pathname.replace(/([^:]\/)\/+/g, "/"));
if (shortlink !== undefined) return Response.redirect(shortlink);
return null;
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Time } from "../../../lib/constants/time.ts";

const deployments = new Map<string, string>();
const projectName = Deno.env.get("DENO_DEPLOY_PROJECT_NAME");
const token = Deno.env.get("DENO_DEPLOY_ACCESS_TOKEN");
const projectName = Deno.env.get("DENO_DEPLOY_PROJECT_NAME") ?? "fart";
const token = Deno.env.get("DENO_DEPLOY_ACCESS_TOKEN") ??
"ddp_10rd56LtEpRv33U37xJQK7Jdl5g0KC3EoB2p";
const refreshRate = 10 * Time.Minute;
let lastFetch = -1;

Expand Down
2 changes: 1 addition & 1 deletion fart_server/bonus_features/versions/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { redirectToDenoDeployPreviewUrl } from "./deno_redirect.ts";
export { redirectToDenoDeployPreviewUrl } from "./deno_deploy_redirect.ts";
11 changes: 6 additions & 5 deletions fart_server/serve.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { redirectToDenoDeployPreviewUrl } from "./bonus_features/versions/mod.ts";
import { redirectIfShortlink } from "./bonus_features/shortlinks/mod.ts";
import { getSize, inject, register } from "./utils.ts";
import { clear, getSize, inject, register } from "./utils.ts";

const middleware = [
// redirect to another server running a different version of the Fart library
redirectToDenoDeployPreviewUrl,
// redirect to an external URL
redirectIfShortlink,
// show how many handlers are registered
(request) => {
(request: Request) => {
if (new URL(request.url).pathname === "/debug/size") {
return new Response(String(getSize()));
}
return null;
},
// show deployment ID if running on Deno Deploy
(request) => {
(request: Request) => {
if (new URL(request.url).pathname === "/debug/deployment") {
return new Response(String(Deno.env.get("DENO_DEPLOYMENT_ID")));
}
Expand All @@ -24,7 +24,7 @@ const middleware = [
];

export const setup = () => {
if (getSize() === middlware.length) return;
if (getSize() === middleware.length) return;
clear();
register(...middleware);
};
Expand All @@ -36,11 +36,12 @@ export const handleRequest = async (event: Deno.RequestEvent) => {

export const serve = async () => {
const port = parseInt(Deno.env.get("PORT") || "8080");
console.log(`Access HTTP webserver at: http://localhost:${port}/`);
console.info(`Access HTTP webserver at: http://localhost:${port}/`);
for await (const connection of Deno.listen({ port })) {
for await (const event of Deno.serveHttp(connection)) {
await handleRequest(event);
}
connection.close();
}
};

Expand Down
10 changes: 0 additions & 10 deletions fart_server/watch_serve.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assertEquals } from "../../deps/std/testing.ts";
import { assertEquals } from "../../../deps/std/testing.ts";
import { Cartridge, CartridgeEvent } from "./cartridge.ts";
import { CodeBlock } from "../code_block/mod.ts";
// import { Lexicon, T } from "../tokenize/mod.ts";

Deno.test("event 'file_start' makes a successful dispatch", async () => {
const cartridge = new Cartridge();
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "../../deps/std/testing.ts";
import { assertEquals } from "../../../deps/std/testing.ts";
import { CodeBlock } from "./code_block.ts";

Deno.test("new code block is empty", () => {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "../../deps/std/testing.ts";
import { assertEquals } from "../../../deps/std/testing.ts";
import { INDENT, Indent } from "./indent.ts";

Deno.test("cache of Indent.Tab1 equals 1 tab", () => {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
bench,
BenchmarkTimer,
runBenchmarks,
} from "../../deps/std/testing.ts";
} from "../../../deps/std/testing.ts";
import { INDENT, Indent } from "./indent.ts";
import { getCachedIndent } from "./utils.ts";

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "../../deps/std/testing.ts";
import { assertEquals } from "../../../deps/std/testing.ts";
import { TextBuilder } from "./text_builder.ts";
import { Cartridge } from "../cartridge/mod.ts";

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "../../deps/std/testing.ts";
import { assertEquals } from "../../../deps/std/testing.ts";
import { T, Token } from "../tokenize/mod.ts";
import { CodeBlock } from "../code_block/mod.ts";
import {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertThrows } from "../../deps/std/testing.ts";
import { assertThrows } from "../../../deps/std/testing.ts";

import { LEXICON } from "./lexicon.ts";

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "../../deps/std/testing.ts";
import { assertEquals } from "../../../deps/std/testing.ts";

import { T } from "./t.ts";

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertEquals } from "../../deps/std/testing.ts";
import { assert, assertEquals } from "../../../deps/std/testing.ts";
import { Token } from "./token.ts";
import { Lexicon } from "./lexicon.ts";

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { T } from "./t.ts";
import { Token } from "./token.ts";
import { tokenize } from "./tokenize.ts";
import { assertEquals } from "../../deps/std/testing.ts";
import { assertEquals } from "../../../deps/std/testing.ts";

Deno.test("yields no tokens given an empty string", () => {
const input = "";
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertEquals } from "../../deps/std/testing.ts";
import { assert, assertEquals } from "../../../deps/std/testing.ts";
import {
checkIsIdentifier,
checkIsInlineComment,
Expand Down
File renamed without changes.
77 changes: 41 additions & 36 deletions lib/transpile/transpile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Token, tokenize } from "../tokenize/mod.ts";
import { Cartridge } from "../cartridge/mod.ts";
import { TextBuilder } from "../text_builder/mod.ts";
import type { FartTokenGenerator } from "../tokenize/mod.ts";
import { Token, tokenize } from "./tokenize/mod.ts";
import { Cartridge } from "./cartridge/mod.ts";
import { TextBuilder } from "./text_builder/mod.ts";
import type { FartTokenGenerator } from "./tokenize/mod.ts";

export interface FartOptions {
targetLanguage: string; // "ts" | "go"
Expand All @@ -11,47 +11,52 @@ export interface FartOptions {
preserveComments: boolean;
}

interface TranspilationContext {
tokenizer: FartTokenGenerator | null;
builder: TextBuilder | null;
prevTokens: Token[];
currentToken: Token | null;
nextToken: () => Token | undefined;
nextStruct: () => Promise<void>;
nextTuple: () => Promise<void>;
}
class TranspilationContext {
constructor(
public tokenizer?: FartTokenGenerator,
public builder?: TextBuilder,
public currentToken: Token | null = null,
public prevTokens: Token[] = [],
) {}

const INITIAL_TRANSPILATION_CONTEXT: TranspilationContext = {
tokenizer: null,
builder: null,
prevTokens: [],
currentToken: null,
nextToken() {
if (this.tokenizer !== null) {
if (this.currentToken !== null) {
public nextToken(): Token | null {
if (this.tokenizer !== undefined) {
if (
this.currentToken !== null &&
this.prevTokens !== undefined &&
this.currentToken !== undefined
) {
this.prevTokens.push(this.currentToken);
}
return this.tokenizer.next().value;
const token = this.tokenizer.next().value;
if (token !== undefined) {
return this.currentToken = token;
}
}
},
async nextStruct() {
// TODO: handle struct
},
async nextTuple() {
// TODO: handle tuple
},
};
return null;
}

const initializeTranspilationContext = () => ({
...INITIAL_TRANSPILATION_CONTEXT,
});
/**
* @todo implement
*/
public async nextStruct(): Promise<void> {
}

/**
* @todo implement
*/
public async nextTuple(): Promise<void> {
}
}

export const transpile = (
code: string,
// options: FartOptions,
): string => {
const ctx = initializeTranspilationContext();
ctx.tokenizer = tokenize(code);

const ctx = new TranspilationContext(
tokenize(code),
new TextBuilder(new Cartridge()),
);
console.log({ ctx });
return "";
};

1 comment on commit b2615ad

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on b2615ad Dec 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

failed to fetch 'https://raw.githubusercontent.com/EthanThatOneKid/fart/b2615adcb69dd0d2b65a84cb713e845ecc29ba0c/fart_server/handle_request.ts': HTTP status client error (404 Not Found) for url (https://raw.githubusercontent.com/EthanThatOneKid/fart/b2615adcb69dd0d2b65a84cb713e845ecc29ba0c/fart_server/handle_request.ts)

Please sign in to comment.