D
Deno

help

Is inspecting import.meta.url a reliable way to distinguish browser and deno contexts?

Ppatwasalinguist5/9/2023
I have been trying to figure out a good pattern to import JSON that will work whether called in deno or the browser. This is what I was thinking:
let data

if(import.meta.url.startsWith('file')){ // we’re in deno
let jsonstring = await Deno.readTextFile('delete.json')
data = JSON.parse(jsonstring)
} else if(import.meta.url.startsWith('http')){ // we’re in the browser
let response = await fetch('delete.json')
data = await response.json()
}

export {data}
let data

if(import.meta.url.startsWith('file')){ // we’re in deno
let jsonstring = await Deno.readTextFile('delete.json')
data = JSON.parse(jsonstring)
} else if(import.meta.url.startsWith('http')){ // we’re in the browser
let response = await fetch('delete.json')
data = await response.json()
}

export {data}
I would welcome advice as to whether this is a good approach to this problem. thanks 🙏
CcrowlKats5/9/2023
you could just use fetch for both
Ppatwasalinguist5/9/2023
hmm. i get this if i $ deno run --allow-read import-data.js
error: Uncaught TypeError: Invalid URL: './delete.json'
let response = await fetch('./delete.json')
error: Uncaught TypeError: Invalid URL: './delete.json'
let response = await fetch('./delete.json')
in other words, i'm running it as a shell script and not a deno server, if i'm explaining that correctly hmm, this seems to work
let url = new URL('./delete.json', import.meta.url).href
let response = await fetch(url)
let data = await response.json()
let url = new URL('./delete.json', import.meta.url).href
let response = await fetch(url)
let data = await response.json()
IioB5/9/2023
^ I did this at deno-gfm: https://github.com/denoland/deno-gfm/blob/main/example/main.ts basically exactly what you're proposing
Ppatwasalinguist5/9/2023
nice thanks! gotta ❤️ deno

Looking for more? Join the community!

Recommended Posts
Fetching Twitter data with coroutines and puppeteerare there any recent examples of how to fetch Twitter data (like monitoring activity for a keyword ePathname prefix in serve()Hi, I was wondering if it's possible to only handle requests coming from specific prefix within pathembed typescript playground to web pagesome programming language guides like the rust book lets you directly edit and run code in their docUsing deno-lambda for AWS SAMI'm having issues trying to run deno-lambda (https://github.com/hayd/deno-lambda) locally. (I haven'How to monetize an app?I would like to monetize my apps in a certain way. Mabye limit some features. How to approach monetiStandard setup of a monorepo that includes frontend and backend TypeScript code for one appHello friends, I'm new to Deno and have what I feel is a relatively simple use case, but I've struggHow can I import https://github.com/ensdomains/ensjs-v3/tree/main in my denoThis is npm package. I want to use it in my deno project. How can I achive it? I am trying this: `imTesting that advanced types workI have a bunch of pretty advanced types, with generics and inferences all over the place. What woulHow can I reattach a debugger after disconnecting?I want to be able to start deno from a command line, but be able to attach to it from VSCode. I actPass string from TS to dll and return the passed string. (Like echo)Hey I am currently trying to understand the communication between a dll and my TS program. Currentlydeno-deploy limits on file storage?When deploying static files via deployctl, what are the storage limits / costs per GB? Couldn't findDoes anyone have a working OpenCL deno example?How can I use opencl with deno? Any examples?absolute imports in fresh?how do you get this accomplished?PhpStorm not resolving local import with import_map.jsonDeno Version : deno 1.33.0 (release, aarch64-apple-darwin) v8 11.4.183.1 typescript 5.0.3 PhpStorm how to cancel test watchon linux. ran `deno test -A --watch tools/json_tools/examples/*.ts` and `ctrl+c` does not seems to Sentry DenoOsUptime is not a function issueI'm using `npm:@sentry/node@7.49.0` and the following script to test the integration ```TypeScript /Property 'openKv' does not exist on type 'typeof Deno'. VSCodeSuch as the title say, I cant get the LSP to recognize openKv in the Deno namespace... deno 1.32.5+Import from direct URL works fine, but NOT via deps.ts``` // ./lib/foobar1.ts // everything is fine import { z } from "https://deno.land/x/zod@v3.21.4/modSmall Deno Script to upload to my server not working due to not being able to be run on CentOSAll my clients run Ubuntu or Debian, and the script will not run on CentOS due to the problem of a GEmpty output in Deno.CommandI’m trying to run the command `git log —grep='.'` from Deno. However, it always returns an empty out