andykais
andykais
DDeno
Created by andykais on 9/6/2024 in #help
recommended way to get file checksum
Hi, I am creating an application that needs to get checksums for files. The code currently looks like this:
import * as node_crypto from 'node:crypto'

async function get_checksum() {
const hash = node_crypto.createHash('sha256')
using file = await Deno.open(this.#filepath)
for await (const chunk of file.readable) {
hash.update(chunk)
}
return hash.digest('hex')
}
import * as node_crypto from 'node:crypto'

async function get_checksum() {
const hash = node_crypto.createHash('sha256')
using file = await Deno.open(this.#filepath)
for await (const chunk of file.readable) {
hash.update(chunk)
}
return hash.digest('hex')
}
This is fairly though. A ~1GB file will take around 3 minutes to generate a checksum with the code above, whereas the built in linux utility sha256sum can do the same in 2.5 seconds. Parallelizing this isnt straightforward to me though, since the hash update is blocking
3 replies
DDeno
Created by andykais on 11/24/2023 in #help
How to detect when --watch flag has been passed to `deno run` programatically?
I am writing a small cli script, and I have a few bits of code that do not play nicely with deno run --watch <script>. (e.g. Deno.addSignalListener('SIGINT', graceful_shutdown). What I want to do is only set up that listener when I am running my cli app in 'production mode'. E.g. while I am developing the cli app with --watch I do not want to set up shutdown listeners. Is there a way to accomplish this with the Deno global perhaps? My current workaround is to pass some custom flag to my cli script when I am in development, like --mode=development or --no-signal, but this is a bit confusing for users because this flag is only important for development.
3 replies
DDeno
Created by andykais on 4/2/2023 in #help
best practice when spawning commands that require sudo?
I am building a command line utility in deno that leverages data coming from tcpdump. To get any meaningful data out of tcpdump, I need elevated permissions. To keep this command portable, that means running sudo tcpdump inside my deno program. How can I set up deno permissions to run a specific command executed through sudo? A simple permission of --allow-run=sudo would be dangerous. I could just let the user approve each time, but even then, all they know is that they are approving sudo, there is no other knowledge of what the subcommand is:
┌ ⚠️ Deno requests run access to "sudo".
├ Requested by `Deno.Command().spawn()` API.
├ Run again with --allow-run to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all run permissions) > y
┌ ⚠️ Deno requests run access to "sudo".
├ Requested by `Deno.Command().spawn()` API.
├ Run again with --allow-run to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all run permissions) > y
Im not sure if I have a suggestion yet on how we could improve this. Right now I am just curious if anyone else has ran into this. I could leverage roles & permissions on my linux machine to allow this program to run without sudo, but thats not very portable, other users would have to do the same "add user/allow user access to x,y,z/change user to run the command/etc"
2 replies
DDeno
Created by andykais on 3/4/2023 in #help
eval script with stricter permissions in deno
Hi all, I wanted to know if its possible to eval a script in deno with more strict permissions. Something like vm.runInNewContext in node. At best, I think I can use Deno.Command('deno', {args: ['run', 'user-script.ts']}) right now. However, that means allowing any deno process to be ran at any time from the parent script. I am wondering if there is any way to specifically run a script with a subset of the permissions allowed to the parent script
1 replies
DDeno
Created by andykais on 1/30/2023 in #help
op_ffi_cstr_read replacement now that deno.core.ops is gone?
The latest release removed Deno.core.ops, which a library of mine relied on to use op_ffi_cstr_read to read Deno.PointerValue into a string. Is there an alternative?
2 replies
DDeno
Created by andykais on 1/17/2023 in #help
how to hash very large files
We have deprecated the old std/hash code in favor of the crypto module, but I do not know what the standard approach to hashing something piecemeal looks like. The advice around the crypto digest generally looks like this:
async function crypto_hash(filepath: string) {
const file_buffer = await Deno.readFile(filepath)
const hash_buffer = await crypto.subtle.digest('SHA-256', file_buffer)
const hash_array = Array.from(new Uint8Array(hash_buffer))
const hash_hex = hash_array.map((b) => b.toString(16).padStart(2, '0')).join('')
return hash_hex
}
async function crypto_hash(filepath: string) {
const file_buffer = await Deno.readFile(filepath)
const hash_buffer = await crypto.subtle.digest('SHA-256', file_buffer)
const hash_array = Array.from(new Uint8Array(hash_buffer))
const hash_hex = hash_array.map((b) => b.toString(16).padStart(2, '0')).join('')
return hash_hex
}
this works fine on smaller files, but I need to occasionally hash a big file, like 2GB files. This is a lot of data to hold in memory, and if I could leverage readable streams that would be ideal. If I need to do that, should I simply feed the hash_buffer back into itself, concatenating new data as I go?
15 replies
DDeno
Created by andykais on 12/21/2022 in #help
what is import.meta.url on implicit version
I am in an awkward situation where I want to test retrieving the version from the url of my module when no version is specified (e.g. deno.land/x/sqlite_native). Unfortunately, it doesnt appear that there is a good way to test this. I have found the source file in ~/.cache/deno/deps/https/deno.land/, but editing it gives an error that the hash map doesnt match (which I suppose is there for safety). I also cannot test this locally since the import.meta.url will be the local file. I could publish a version to deno.land/x/ just to log out the import.meta.url, but I would like to avoid this. Essentially, I am wondering if deno will redirect the url to the latest version, and that version will appear in the import meta. Of course, if there is a better way to get the module version I am all ears. I am hoping to avoid adding a compile step to ci to edit tags to include a version number.
3 replies
DDeno
Created by andykais on 12/21/2022 in #help
third party module - edit webhook subdirectory
Hi, I just published a module https://deno.land/x/sqlite_native@0.1.0 but the actual library content is under the src/ folder. I did not specify this when first creating the webhook (leading to all imports looking like src/mod.ts. Is it possible to edit the webhook after creation? Its hard to tell if so, because there doesnt appear to be any 'owner' controls on a module after publishing.
2 replies
DDeno
Created by andykais on 12/19/2022 in #help
arch linux deno package might be broken
Installing deno on arch linux appears to 404 on all the mirrors. I just synchronized them all as well so I think theres something else going on... sudo pacman -Sy deno
:: Synchronizing package databases...
core 152.5 KiB 317 KiB/s 00:00 [########################################] 100%
extra 1726.3 KiB 1516 KiB/s 00:01 [########################################] 100%
community 7.2 MiB 1232 KiB/s 00:06 [########################################] 100%
multilib 162.6 KiB 1010 KiB/s 00:00 [########################################] 100%
resolving dependencies...
looking for conflicting packages...

Packages (1) deno-1.29.0-1

Total Download Size: 23.66 MiB
Total Installed Size: 68.19 MiB
Net Upgrade Size: 0.17 MiB

:: Proceed with installation? [Y/n]
:: Retrieving packages...
deno-1.29.0-1-x86_64.pkg.tar.zst failed to download
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from mirror.pit.teraswitch.com : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from ftp.sudhip.com : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from mirrors.mit.edu : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from arch.mirror.constant.com : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from iad.mirrors.misaka.one : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from mirror.ette.biz : The requested URL returned error: 404
warning: failed to retrieve some files
error: failed to commit transaction (failed to retrieve some files)
Errors occurred, no packages were upgraded.
:: Synchronizing package databases...
core 152.5 KiB 317 KiB/s 00:00 [########################################] 100%
extra 1726.3 KiB 1516 KiB/s 00:01 [########################################] 100%
community 7.2 MiB 1232 KiB/s 00:06 [########################################] 100%
multilib 162.6 KiB 1010 KiB/s 00:00 [########################################] 100%
resolving dependencies...
looking for conflicting packages...

Packages (1) deno-1.29.0-1

Total Download Size: 23.66 MiB
Total Installed Size: 68.19 MiB
Net Upgrade Size: 0.17 MiB

:: Proceed with installation? [Y/n]
:: Retrieving packages...
deno-1.29.0-1-x86_64.pkg.tar.zst failed to download
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from mirror.pit.teraswitch.com : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from ftp.sudhip.com : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from mirrors.mit.edu : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from arch.mirror.constant.com : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from iad.mirrors.misaka.one : The requested URL returned error: 404
error: failed retrieving file 'deno-1.29.0-1-x86_64.pkg.tar.zst' from mirror.ette.biz : The requested URL returned error: 404
warning: failed to retrieve some files
error: failed to commit transaction (failed to retrieve some files)
Errors occurred, no packages were upgraded.
3 replies
DDeno
Created by andykais on 11/15/2022 in #help
deno.lock multiple entry point best practices
Hi, I just upgraded to 1.28.0 and realized that lock files are here by default for deno.json config projects. I figure that means its time I adopted lock files. So I wanted to open a forum about best practices. I have read this doc https://deno.land/manual@v1.28.0/basics/modules/integrity_checking, which is a good start. I know at least that a lock file should be checked into git (which is less confusing than w/ node, where lock files change per OS and their advice on version control is pretty fuzzy. Here is my remaining question though, what happens when I have multiple entrypoints? Specifically, I am looking at this repo I own https://github.com/andykais/sqlite-native. I have three main module entrypoints: 1. src/mod.ts (this is what lib users will use) 2. test/*.ts (this is for running tests) 3. deps/compile.ts (this is a build script for building/upgrading the sqlite.so shared lib) should I have three separate lock files?
deno.json
deno.lock
test/
deno.lock
deps/
deno.lock
deno.json
deno.lock
test/
deno.lock
deps/
deno.lock
Interestingly, it seems like deno is smart enough to only add to the lock file if I do not specify manual locations (I would assume different entrypoints would cause the deno.lock file to thrash in version control. What is the best practice here, specify multiple entrypoints, or just let deno manage one lock file? Another, related question, does the lock file do anything as a remote module, or is it only used locally? The docs seem to imply the latter, but I was hoping someone here could confirm.
2 replies
DDeno
Created by andykais on 10/12/2022 in #help
npm specifier import types
so when I do something like
import * as rx from 'npm:rxjs'

console.log(rx)
import * as rx from 'npm:rxjs'

console.log(rx)
I do have the correct code imported, but types are all any. Is this the expected state of npm imports, or is there a bug here? I am running
deno 1.26.1 (release, x86_64-unknown-linux-gnu)
v8 10.7.193.3
typescript 4.8.3
deno 1.26.1 (release, x86_64-unknown-linux-gnu)
v8 10.7.193.3
typescript 4.8.3
14 replies
DDeno
Created by andykais on 10/3/2022 in #help
how to cli run node modules without --compat
So deno run --compat has now been removed in favor of npm: specifiers https://deno.com/blog/v1.26#--compat-mode-removed. I am sure this consolidates a lot of code, but I am unsure how I can gain back certain functionality. For instance, I could run a solidjs template out of the box from deno with compat like so: deno run --compat --unstable --allow-env --allow-read --allow-run --allow-net node_modules/.bin/vite dev Using npm specifiers, I dont seem to have the same capabilities. deno run --compat --unstable --allow-env --allow-read --allow-run npm:vite
error: Uncaught (in promise) Error: Cannot find module 'fs'
Require stack:
- /Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js
- /Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js
at Function.Module._resolveFilename (deno:ext/node/02_require.js:609:17)
at Function.Module._load (deno:ext/node/02_require.js:441:29)
at Module.require (deno:ext/node/02_require.js:652:21)
at require (deno:ext/node/02_require.js:777:18)
at Object.<anonymous> (file:///Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js:1663:10)
at Object.<anonymous> (file:///Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js:2309:4)
at Module._compile (deno:ext/node/02_require.js:713:36)
at Object.Module._extensions..js (deno:ext/node/02_require.js:740:12)
at Module.load (deno:ext/node/02_require.js:630:34)
at Function.Module._load (deno:ext/node/02_require.js:487:14)
error: Uncaught (in promise) Error: Cannot find module 'fs'
Require stack:
- /Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js
- /Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js
at Function.Module._resolveFilename (deno:ext/node/02_require.js:609:17)
at Function.Module._load (deno:ext/node/02_require.js:441:29)
at Module.require (deno:ext/node/02_require.js:652:21)
at require (deno:ext/node/02_require.js:777:18)
at Object.<anonymous> (file:///Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js:1663:10)
at Object.<anonymous> (file:///Users/andrew.kaiser/Library/Caches/deno/npm/registry.npmjs.org/esbuild/0.15.10/lib/main.js:2309:4)
at Module._compile (deno:ext/node/02_require.js:713:36)
at Object.Module._extensions..js (deno:ext/node/02_require.js:740:12)
at Module.load (deno:ext/node/02_require.js:630:34)
at Function.Module._load (deno:ext/node/02_require.js:487:14)
this most likely has to do with a subprocess with esbuild or something similar, but just in general, it seems like we lost some functionality here
4 replies