Skip to content

Commit

Permalink
Update deps, get Deno.Reader out of the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed May 18, 2024
1 parent 50297f2 commit 9656f33
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 27 deletions.
17 changes: 5 additions & 12 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
export { parse as parseFlags } from "https://deno.land/[email protected]/flags/mod.ts";
export * as http from "https://deno.land/[email protected]/http/server.ts";
export * as file_server from "https://deno.land/[email protected]/http/file_server.ts";
export {
readableStreamFromIterable,
} from "https://deno.land/[email protected]/streams/readable_stream_from_iterable.ts";
export {
readableStreamFromReader,
} from "https://deno.land/[email protected]/streams/readable_stream_from_reader.ts";
export { parse as parseFlags } from "https://deno.land/[email protected]/flags/mod.ts";
export * as file_server from "https://deno.land/[email protected]/http/file_server.ts";

export * as entities from "https://deno.land/x/[email protected]/lib/xml-entities.js";

export { SubProcess, type SubprocessErrorData } from "https://crux.land/4KsAxM#sub-process";
export { SubProcess, type SubprocessErrorData } from "https://crux.land/76Sb6W#sub-process";
export { filesize } from "https://crux.land/6wZ5Sz#filesize@v1";

// maybe someday we actually use deno_graph's code too
Expand All @@ -23,7 +16,7 @@ export {
trace,
context,
type Context,
} from "https://deno.land/x/observability@v0.4.0/opentelemetry/api.js";
} from "https://deno.land/x/observability@v0.6.1/opentelemetry/api.js";
export {
httpTracer,
} from "https://deno.land/x/observability@v0.4.0/instrumentation/http-server.ts";
} from "https://deno.land/x/observability@v0.6.1/instrumentation/http-server.ts";
11 changes: 4 additions & 7 deletions feat/dependencies-of/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {
entities,
readableStreamFromReader,
readableStreamFromIterable,
SubProcess,
type SubprocessErrorData,
trace,
context,
Context,
} from "../../deps.ts";

import { templateHtml, makeErrorResponse, HtmlHeaders } from '../../lib/request-handling.ts';
Expand Down Expand Up @@ -52,7 +49,7 @@ export async function handleRequest(req: Request, modSlug: string, args: URLSear
}
}

async function serveBufferedOutput(req: Request, computation: Promise<string>, contentType: string) {
async function serveBufferedOutput(_req: Request, computation: Promise<string>, contentType: string) {
return await computation
.then(buffer => new Response(buffer, {
status: 200,
Expand All @@ -62,11 +59,11 @@ async function serveBufferedOutput(req: Request, computation: Promise<string>, c
}), makeErrorResponse);
}

async function serveStreamingOutput(req: Request, computation: Promise<SubProcess>, contentType: string) {
async function serveStreamingOutput(_req: Request, computation: Promise<SubProcess>, contentType: string) {
return await computation
.then(proc => {
proc.status(); // throw this away because not really a way of reporting problems mid-stream
return new Response(readableStreamFromReader(proc.proc.stdout), {
return new Response(proc.proc.stdout, {
status: 200,
headers: {
'content-type': contentType,
Expand Down Expand Up @@ -151,7 +148,7 @@ async function serveHtmlGraphPage(req: Request, modUrl: string, modSlug: string,
}, context.active(), span => renderModuleToHtml(modUrl, args).finally(() => span.end()));

// Return the body in two parts, with a comment in between
return new Response(readableStreamFromIterable((async function*() {
return new Response(ReadableStream.from((async function*() {
const encoder = new TextEncoder();
yield encoder.encode(pageHtml);

Expand Down
2 changes: 1 addition & 1 deletion feat/shields/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function makeCacheSizeShield(map: ModuleMap): Response {
}

async function makeXLatestVersionShield(modId: string): Promise<Response> {
const {latest, versions} = await fetch(`https://cdn.deno.land/${modId}/meta/versions.json`).then(x => x.json()) as {latest: string, versions: string[]};
const {latest} = await fetch(`https://cdn.deno.land/${modId}/meta/versions.json`).then(x => x.json()) as {latest: string, versions: string[]};

return new Response(JSON.stringify({
schemaVersion: 1,
Expand Down
4 changes: 2 additions & 2 deletions lib/request-handling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http, file_server, trace, context } from "../deps.ts";
import { file_server, trace } from "../deps.ts";

export const HtmlHeaders = new Headers({
'content-type': 'text/html; charset=utf-8',
Expand Down Expand Up @@ -43,7 +43,7 @@ async function templateHtmlInner(templatePath: string, replacements: Record<stri
});
}

export async function serveTemplatedHtml(req: Request, templatePath: string, replacements: Record<string,string> = {}) {
export async function serveTemplatedHtml(_req: Request, templatePath: string, replacements: Record<string,string> = {}) {
return await templateHtml(templatePath, replacements)
.then(body => new Response(body, {
headers: HtmlHeaders,
Expand Down
10 changes: 5 additions & 5 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env -S deno run --watch --check --allow-sys=hostname --allow-read --allow-net --allow-run=deno,dot --allow-env

import { http, httpTracer, trace } from "./deps.ts";
import { httpTracer, trace } from "./deps.ts";
import { serveFont, servePublic, serveTemplatedHtml } from './lib/request-handling.ts';

// The different HTTP surfaces we expose
Expand All @@ -16,16 +16,16 @@ try {
}

console.log('Setting up on', { port });
http.serve(httpTracer(async request => {
Deno.serve({
port,
}, httpTracer(async request => {

const resp = await handleReq(request);
return resp ?? new Response('404 Not Found', {
status: 404,
});

}), {
port,
});
}));

async function handleReq(req: Request): Promise<Response | undefined> {
const url = new URL(req.url);
Expand Down

0 comments on commit 9656f33

Please sign in to comment.