Listen to imports or other requests originated from Deno server

69 Views Asked by At

Lets say i have a module qwe.js and his content is following: import('./asd.js')

If i run this module somwhere from the client browser i can catch this import request on server side with Deno.serveHttp and generate some unique response even if file asd.js actually does not exist at all.

However if i run exact same module somewhere from the server im unable to make the same behaviour just becouse import statement goes uncatched with some built-in response logic.

Can i make so that my module with import would work identical both on client and server? Or in other words - can i listen/catch/intercept import requests originated from server to make custom responses for them?

1

There are 1 best solutions below

5
guest271314 On

My question was not about browser requests (i know how to handle them) but about requsts maded from server side, for example if you put some import in the first code block of your answer.

In general you can use inotify.

If the file exists you can use Deno.watchFs().

Some of my recent file system observer experiments https://github.com/guest271314/fs, and some I did a while ago https://github.com/guest271314/captureSystemAudio?tab=readme-ov-file#usage.