Bairdy
Bairdy
DDeno
Created by Bairdy on 1/14/2024 in #help
Can Deno list all functions within a script without explicit registration or annotation?
. For context, here is how you do it for globalThis:
Object.getOwnPropertyNames(globalThis)
.filter((item: string) => typeof (globalThis as any)[item] === "function")
.forEach((funcName: string) => console.log(funcName));
Object.getOwnPropertyNames(globalThis)
.filter((item: string) => typeof (globalThis as any)[item] === "function")
.forEach((funcName: string) => console.log(funcName));
Output:
Object
Function
Array
Number
parseFloat
parseInt
Boolean
String
Symbol
Date
Promise
RegExp
Error
AggregateError
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
ArrayBuffer
Uint8Array
...
Object
Function
Array
Number
parseFloat
parseInt
Boolean
String
Symbol
Date
Promise
RegExp
Error
AggregateError
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
ArrayBuffer
Uint8Array
...
But in Javascript it does show my functions:
Object.getOwnPropertyNames(window)
.filter(item => typeof window[item] === "function")
.forEach(funcName => console.log(funcName));
Object.getOwnPropertyNames(window)
.filter(item => typeof window[item] === "function")
.forEach(funcName => console.log(funcName));
Output:
...
testFunction1
testFunction2
...
testFunction1
testFunction2
4 replies
DDeno
Created by Bairdy on 10/23/2023 in #help
Can you recommend a mysql driver for deno that isn't a complete dead end?
mysql2 port: never updated. mysql driver: doesn't support strings mysql connector from Oracle: no one cares to port to Deno. ALL, I mean ALL I can think about is going back to superb PHP PDO. What a challenge...
26 replies
DDeno
Created by Bairdy on 10/19/2023 in #help
Deno - Sanitize Filters
Does Deno have anything similar to https://www.php.net/manual/en/filter.filters.sanitize.php for HTTP HTML POST forms? Example:
function filterSanitizeString(input: string): string {
// Remove control characters
let sanitized = input.replace(/[\x00-\x1F\x7F-\x9F]/g, "");

// Remove potential SQL injection code
sanitized = sanitized.replace(/('|--|;)/g, "");

// Remove potential script injection
sanitized = sanitized.replace(/(<script>|<\/script>)/gi, "");

// Escape special HTML characters to prevent basic HTML injection
sanitized = sanitized.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');

return sanitized;
}
function filterSanitizeString(input: string): string {
// Remove control characters
let sanitized = input.replace(/[\x00-\x1F\x7F-\x9F]/g, "");

// Remove potential SQL injection code
sanitized = sanitized.replace(/('|--|;)/g, "");

// Remove potential script injection
sanitized = sanitized.replace(/(<script>|<\/script>)/gi, "");

// Escape special HTML characters to prevent basic HTML injection
sanitized = sanitized.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');

return sanitized;
}
3 replies
DDeno
Created by Bairdy on 10/8/2023 in #help
Why was Preact used for Deno Fresh instead of Superb Vue.js?
I am curious as to why is all.
3 replies
DDeno
Created by Bairdy on 8/8/2023 in #help
Is there a way to show permissions used on start up during runtime?
For example: If I was to run an App with permissions: --allow-net=${hostname}:${port} is there a way to console log this during runtime?
4 replies
DDeno
Created by Bairdy on 8/6/2023 in #help
Deno.serve AbortController onError. How?
When I run my new Deno.serve() updated implementation I get a type error. Perhaps I'm doing it wrong. Maybe it is the way I am trying to use the Abort Controller or maybe the onError handler must always return something?
// You can stop the server with an AbortSignal.
// The abort signal needs to be passed as the signal option in the options bag.
const ac = new AbortController();

const server = Deno.serve({
// The port to listen on.
port: config.http.server.port,
// A literal IP address or host name that can be resolved to an IP address.
hostname: "0.0.0.0",
// An AbortSignal to close the server and all connections.
signal: ac.signal,
// The callback which is called when the server starts listening.
onListen({ port, hostname }) {
// console.log(`Server started at http://${hostname}:${port}`);
console.log(
`%cApplication..... fsrv v2.0 - A Deno HTTP Server
Deno v${Deno.version.deno} : Typescript v${Deno.version.typescript} : V8 v${Deno.version.v8}
Gateway URL..... http://${hostname}:${port}
Server Root..... ${pathJoin(CWD, config.http.server.www)}
Internal Pages.. ${pathJoin(CWD, config.http.server.internal)}
Site Root....... ${pathJoin(CWD, config.http.server.public)}`,
"color: #7986cb",
);
},
// The handler to invoke when route handlers throw an error.
onError(error) {
console.error(error);
log("HTTP 500");

// FATAL ERROR!

// The server aborts when the abort signal is sent to the abort controller.
console.log("Closing server...");
ac.abort();
// To wait for the server to close, await the promise returned from the Deno.serve API.
server.finished.then(() => console.log("Server closed"));

},
}, handler);
// You can stop the server with an AbortSignal.
// The abort signal needs to be passed as the signal option in the options bag.
const ac = new AbortController();

const server = Deno.serve({
// The port to listen on.
port: config.http.server.port,
// A literal IP address or host name that can be resolved to an IP address.
hostname: "0.0.0.0",
// An AbortSignal to close the server and all connections.
signal: ac.signal,
// The callback which is called when the server starts listening.
onListen({ port, hostname }) {
// console.log(`Server started at http://${hostname}:${port}`);
console.log(
`%cApplication..... fsrv v2.0 - A Deno HTTP Server
Deno v${Deno.version.deno} : Typescript v${Deno.version.typescript} : V8 v${Deno.version.v8}
Gateway URL..... http://${hostname}:${port}
Server Root..... ${pathJoin(CWD, config.http.server.www)}
Internal Pages.. ${pathJoin(CWD, config.http.server.internal)}
Site Root....... ${pathJoin(CWD, config.http.server.public)}`,
"color: #7986cb",
);
},
// The handler to invoke when route handlers throw an error.
onError(error) {
console.error(error);
log("HTTP 500");

// FATAL ERROR!

// The server aborts when the abort signal is sent to the abort controller.
console.log("Closing server...");
ac.abort();
// To wait for the server to close, await the promise returned from the Deno.serve API.
server.finished.then(() => console.log("Server closed"));

},
}, handler);
I get the following error:
error: TS2322 [ERROR]: Type '(error: unknown) => void' is not assignable to type '(error: unknown) => Response | Promise<Response>'.
Type 'void' is not assignable to type 'Response | Promise<Response>'.
onError(error) {
error: TS2322 [ERROR]: Type '(error: unknown) => void' is not assignable to type '(error: unknown) => Response | Promise<Response>'.
Type 'void' is not assignable to type 'Response | Promise<Response>'.
onError(error) {
6 replies
DDeno
Created by Bairdy on 6/22/2023 in #help
Is anyone working on LLMs for Deno so I don't have to learn it in disgusting Python?
I'm coming to terms with the reality of having to learn this technology but not on Python.. I mean.. very much hopefully not on Python.
6 replies
DDeno
Created by Bairdy on 5/13/2023 in #help
How do I send a simple text file to the printer?
Hello. I am looking for an example of how to send a simple text file tot eh printer in Deno. Is this possible?
3 replies
DDeno
Created by Bairdy on 4/12/2023 in #help
Parsing Apache mime.types into dictionary Record<string, string>
This code parses the official Apache mime.types from GitHub and creates a dictionary Record<string, string> of all lines that have not been commented out. Perhaps it may help someone learning and using Deno serve so I wanted to share.
/**
* Run command:
* deno run --allow-net --check apache_getmime.ts
*
* Output example:
*
* const MIME_TYPES: Record<string, string> = {
* ".ext1": "type/subtype",
* ".ext2": "type/subtype",
* ".ext3": "type/subtype",
* };
*
*/
// Declare an empty dictionary object called MIME_TYPES
const MIME_TYPES: Record<string, string> = {};

// Define an async function to fetch and parse MIME types from a remote server
async function parseApacheMimeTypes(): Promise<void> {
// Fetch the MIME types file from the remote server
const response = await fetch("https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types");
// Extract the text content of the MIME types file
const text = await response.text();
// Split the text content into an array of individual lines
const lines = text.split("\n");
// Loop through each line in the array
for (const line of lines) {
// Skip lines that are commented out
if (line.startsWith("#")) {
continue;
}
// Extract the media type and extensions from the active line
const [mediaType, ...extensions] = line.trim().split(/\s+/);
// Loop through each extension and add it to the MIME_TYPES dictionary with the corresponding media type
for (const extension of extensions) {
MIME_TYPES["." + extension] = mediaType;
}
}
// Output the completed MIME_TYPES dictionary to the console
console.info(MIME_TYPES);
}

// Call the async function to retrieve and parse the MIME types
await parseApacheMimeTypes();
/**
* Run command:
* deno run --allow-net --check apache_getmime.ts
*
* Output example:
*
* const MIME_TYPES: Record<string, string> = {
* ".ext1": "type/subtype",
* ".ext2": "type/subtype",
* ".ext3": "type/subtype",
* };
*
*/
// Declare an empty dictionary object called MIME_TYPES
const MIME_TYPES: Record<string, string> = {};

// Define an async function to fetch and parse MIME types from a remote server
async function parseApacheMimeTypes(): Promise<void> {
// Fetch the MIME types file from the remote server
const response = await fetch("https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types");
// Extract the text content of the MIME types file
const text = await response.text();
// Split the text content into an array of individual lines
const lines = text.split("\n");
// Loop through each line in the array
for (const line of lines) {
// Skip lines that are commented out
if (line.startsWith("#")) {
continue;
}
// Extract the media type and extensions from the active line
const [mediaType, ...extensions] = line.trim().split(/\s+/);
// Loop through each extension and add it to the MIME_TYPES dictionary with the corresponding media type
for (const extension of extensions) {
MIME_TYPES["." + extension] = mediaType;
}
}
// Output the completed MIME_TYPES dictionary to the console
console.info(MIME_TYPES);
}

// Call the async function to retrieve and parse the MIME types
await parseApacheMimeTypes();
1 replies
DDeno
Created by Bairdy on 3/24/2023 in #help
VScode deno lint blew up again after update.
It seems every time there's an extension update VScode lint blows up and will stop recognizing top level awaits, and all kinds of other things. How can I fix the latest one?
2 replies
DDeno
Created by Bairdy on 3/4/2023 in #help
Feature Suggestion: Dark Mode for `std` library Docs.
Deno's documentation is great. I find myself reading it more and more. I currently use a browser addon called Midnight Lizzard to turn it Dark. It works great but since Deno's site is made in Tailwind I was wondering if the devs could add a simple Light/Dark Toggle button on Top Right of the page where you usually see them. It would be great on the eyes for long reads and overall.
8 replies
DDeno
Created by Bairdy on 3/1/2023 in #help
Deno process watcher. How?
This code when compile, runs a simple http server.
import { serve } from "https://deno.land/std@0.178.0/http/server.ts";
serve((_req) => new Response("Hello, world"));
import { serve } from "https://deno.land/std@0.178.0/http/server.ts";
serve((_req) => new Response("Hello, world"));
Can a separate Deno script be used to watch this process running and if it crashes for whatever reason, it restarts? In Linux you can configure unit file for it but I was wondering if Deno is currently able to watch a different process and restart it if it has exited.
4 replies
DDeno
Created by Bairdy on 2/25/2023 in #help
Remove std/node, it was merged into Deno itself (#3206)
This used to work, but now it doesn't. Where do you show examples of where this was moved to and how to use it from now on?
import { inflate } from "https://deno.land/std/node/zlib.ts";
import { Buffer } from "https://deno.land/std/node/buffer.ts";

const input = "789cb3b1d10fa92c48d5f72bcd4d4a2d72cb2fca4d2cd10fd5482e55482bd1d4775630d477016237178592a2d254fde0600d4d3b3b00bae60fb4";
const buffer = Buffer.from(input, "hex");

inflate(buffer, (err, result) => {
if (err) {
console.error(err);
return;
}
console.log(result.toString());
});
import { inflate } from "https://deno.land/std/node/zlib.ts";
import { Buffer } from "https://deno.land/std/node/buffer.ts";

const input = "789cb3b1d10fa92c48d5f72bcd4d4a2d72cb2fca4d2cd10fd5482e55482bd1d4775630d477016237178592a2d254fde0600d4d3b3b00bae60fb4";
const buffer = Buffer.from(input, "hex");

inflate(buffer, (err, result) => {
if (err) {
console.error(err);
return;
}
console.log(result.toString());
});
22 replies
DDeno
Created by Bairdy on 1/29/2023 in #help
prototype dot functions. How?
How would I go about creating dot function that can be chained at the end of a number in Deno? Example: (2).toUSD() // returns $2.00
function toUSD(amount) {
// RETURN Something..
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(amount);
},
function toUSD(amount) {
// RETURN Something..
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(amount);
},
3 replies