Skip to content

Commit

Permalink
Fix edge-case errors and update Deno + libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoBernardino committed Sep 21, 2022
1 parent 1fb432d commit 3aea5bc
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .dvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.22.0
1.25.3
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: v1.22.0
deno-version: v1.25.3
- run: |
make test
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: start
start:
deno run --watch --allow-net --allow-read --allow-env=PORT,USERBASE_APP_ID main.ts
deno run --watch --allow-net --allow-read=public,pages,.env,.env.defaults --allow-env main.ts

.PHONY: format
format:
Expand All @@ -10,4 +10,4 @@ format:
test:
deno fmt --check
deno lint
deno test --allow-net --allow-read --allow-env=PORT,USERBASE_APP_ID --check=all
deno test --allow-net --allow-read=public,pages,.env,.env.defaults --allow-env --check=all
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It's not compatible with Budget Zen v1 (not end-to-end encrypted), which you can

## Requirements

This was tested with `deno@1.22.0`, though it's possible older versions might work.
This was tested with `deno@1.25.3`, though it's possible older versions might work.

There are no other dependencies. **Deno**!

Expand Down
2 changes: 1 addition & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'https://deno.land/std@0.142.0/dotenv/load.ts';
import 'https://deno.land/std@0.156.0/dotenv/load.ts';

import header from '../components/header.ts';
import footer from '../components/footer.ts';
Expand Down
2 changes: 1 addition & 1 deletion lib/utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from 'https://deno.land/std@0.142.0/testing/asserts.ts';
import { assertEquals } from 'https://deno.land/std@0.156.0/testing/asserts.ts';
import { escapeHtml, formatNumber, splitArrayInChunks } from './utils.ts';

Deno.test('that escapeHtml works', () => {
Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serve } from 'https://deno.land/std@0.142.0/http/server.ts';
import { serve } from 'https://deno.land/std@0.156.0/http/server.ts';
import routes, { Route } from './routes.ts';

function handler(request: Request) {
Expand Down
2 changes: 1 addition & 1 deletion main_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from 'https://deno.land/std@0.142.0/testing/asserts.ts';
import { assertEquals } from 'https://deno.land/std@0.156.0/testing/asserts.ts';
import { abortController } from './main.ts';

const baseUrl = 'http://localhost:8000';
Expand Down
6 changes: 4 additions & 2 deletions public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@
await fetchBudgets(expense.date.substring(0, 7))
).find((budget) => budget.name === expense.budget);

const { userbase } = window;

if (!existingBudget) {
const newBudgetId = `${generateId()}`;

Expand All @@ -520,8 +522,6 @@
});
}

const { userbase } = window;

if (expense.id === 'newExpense') {
expense.id = `${generateId()}`;

Expand Down Expand Up @@ -869,6 +869,8 @@
10,
);

const { userbase } = window;

for (const budgetsToAdd of addBudgetChunks) {
await userbase.putTransaction({
databaseName: 'budgets',
Expand Down
26 changes: 15 additions & 11 deletions routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readableStreamFromReader } from 'https://deno.land/std@0.142.0/streams/mod.ts';
import { serveFile } from 'https://deno.land/std@0.156.0/http/file_server.ts';
import { baseUrl, basicLayoutResponse, isRunningLocally, PageContentResult, recordPageView } from './lib/utils.ts';

// NOTE: This won't be necessary once https://github.com/denoland/deploy_feedback/issues/1 is closed
Expand All @@ -18,7 +18,7 @@ export interface Route {
handler: (
request: Request,
match: URLPatternResult,
) => (Response | Promise<Response>);
) => Response | Promise<Response>;
}

interface Routes {
Expand Down Expand Up @@ -104,12 +104,11 @@ const routes: Routes = {
robots: {
pattern: new URLPattern({ pathname: '/robots.txt' }),
handler: async (_request) => {
const file = await Deno.open(`public/robots.txt`, { read: true });
const readableStream = readableStreamFromReader(file);
const fileContents = await Deno.readTextFile(`public/robots.txt`);

const oneDayInSeconds = 24 * 60 * 60;

return new Response(readableStream, {
return new Response(fileContents, {
headers: {
'content-type': 'text/plain',
'cache-control': `max-age=${oneDayInSeconds}, public`,
Expand All @@ -119,12 +118,11 @@ const routes: Routes = {
},
public: {
pattern: new URLPattern({ pathname: '/public/:filePath*' }),
handler: async (_request, match) => {
handler: async (request, match) => {
const { filePath } = match.pathname.groups;

try {
const file = await Deno.open(`public/${filePath}`, { read: true });
const readableStream = readableStreamFromReader(file);
const fullFilePath = `public/${filePath}`;

const oneDayInSeconds = isRunningLocally(match) ? 0 : 24 * 60 * 60;

Expand All @@ -141,15 +139,21 @@ const routes: Routes = {
headers['content-type'] = 'text/css';
} else if (fileExtension === 'jpg') {
headers['content-type'] = 'image/jpeg';
return serveFile(request, fullFilePath);
} else if (fileExtension === 'png') {
headers['content-type'] = 'image/png';
return serveFile(request, fullFilePath);
} else if (fileExtension === 'svg') {
headers['content-type'] = 'image/svg+xml';
} else if (fileExtension === 'json') {
headers['content-type'] = 'text/json';
return serveFile(request, fullFilePath);
} else if (fileExtension === 'ico') {
headers['content-type'] = 'image/x-icon';
return serveFile(request, fullFilePath);
}

return new Response(readableStream, {
const fileContents = await Deno.readTextFile(fullFilePath);

return new Response(fileContents, {
headers,
});
} catch (error) {
Expand Down

0 comments on commit 3aea5bc

Please sign in to comment.