Auke
Auke
DDeno
Created by Auke on 8/19/2024 in #help
Setup for using npm packages locally + via server.ts
Hi Deno, I have a project in which I use Deno both to execute ts/js files locally to compute features, using a command like: deno run --allow-all --v8-flags=--max-old-space-size=2192 compute.ts This compute.ts script uses .js scripts that are also used in a webpage I serve using a simple server.ts file, which I serve using deno: deno run -A server.ts which contains the following code to serve files:
Deno.serve((req: Request) => {
let pathname = new URL(req.url).pathname;

// fonts endpoint
if (pathname.startsWith("/fonts")) {
return fonts_endpoint(req, pathname);
}

// Canonicalize path
if (pathname === "/" || pathname === "") {
pathname = "/index.html";
}
const filepath = "." + pathname;
console.log(filepath);

const isReadableFile = existsSync(filepath, {
isReadable: true,
isFile: true,
});

if (isReadableFile) {
return serveFile(req, filepath);
}

// Otherwise the requested resource is not available
return new Response("404: Not Found", {
status: 404,
});
});
Deno.serve((req: Request) => {
let pathname = new URL(req.url).pathname;

// fonts endpoint
if (pathname.startsWith("/fonts")) {
return fonts_endpoint(req, pathname);
}

// Canonicalize path
if (pathname === "/" || pathname === "") {
pathname = "/index.html";
}
const filepath = "." + pathname;
console.log(filepath);

const isReadableFile = existsSync(filepath, {
isReadable: true,
isFile: true,
});

if (isReadableFile) {
return serveFile(req, filepath);
}

// Otherwise the requested resource is not available
return new Response("404: Not Found", {
status: 404,
});
});
and serves plain html, .js and .ttf files. I have this setup working for a long time, but it is a mess - I have installed npm libraries using npm, access those javascript files by using direct paths: import * as FloBezier from "./node_modules/flo-bezier3/browser/index.js"; and I do this as a solution to have this javascript file working both locally (using deno run cli) and when accessing the file through the browser (using the server.ts above). Is there a more cannonical approach to doing this? Key requirement is being able to use the same js file both locally and in the browser
8 replies
DDeno
Created by Auke on 5/7/2024 in #help
`deno run` not returning
Hi - I have added the following code to my javascript file, and for some reason the code still executes fine, but it doesn't return anymore, leaving deno running and I have to ctrl-c out of it.
if ("Deno" in window) {
// Import paper_js, as it is not loaded by the index.html script include
const paper_import = await import("npm:paper");
const paper = paper_import.default;
await import("npm:paper-jsdom");
globalThis.paper = paper;
paper.setup();
console.log("setup paper");
}
if ("Deno" in window) {
// Import paper_js, as it is not loaded by the index.html script include
const paper_import = await import("npm:paper");
const paper = paper_import.default;
await import("npm:paper-jsdom");
globalThis.paper = paper;
paper.setup();
console.log("setup paper");
}
This prints out the setup paper. I have added some prints surrounding my entry point main, and those all print as well:
console.log("doing");
try {
await main();
console.log("done!");
} catch (error) {
console.error("An error occursed:", error);
} finally {
// Keep the program running until all asynchronous operations are complete
await new Promise((resolve) => setTimeout(resolve, 0));
console.log("now done");
}
console.log("really done");
console.log("doing");
try {
await main();
console.log("done!");
} catch (error) {
console.error("An error occursed:", error);
} finally {
// Keep the program running until all asynchronous operations are complete
await new Promise((resolve) => setTimeout(resolve, 0));
console.log("now done");
}
console.log("really done");
So what could be keeping the deno instance from returning/finishing?
25 replies