Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@ghom/handler
Advanced tools
Readme
This package is a file handler for NodeJS. It can load files from a directory, and it can reload the files if they changed. You can handle any file type with this handler, but you need to write a loader function for it.
import { Handler } from "@ghom/handler"
const handler = new Handler(" ... ")
try {
// For load the files, call this function:
await handler.init()
// You can access the loaded files with the "elements" property:
console.log(handler.elements)
} catch(e) {
console.error(e)
}
// For terminate the hot reloading and empty the handler cache, call this function:
handler.destroy()
export const handler = new Handler("dist/files", {
logger: console,
loggerPattern: "loaded new table: $filename",
loader: (path) => import(`file://${path}`),
pattern: /\.js$/,
})
export const handler = new Handler("dist/files", {
pattern: /\.js$/,
hotReload: true,
loader: (path) => import(`file://${path}?update=${Date.now()}`),
onLoad: (path, data) => {
// Do something with the loaded data
},
onChange: (path, data) => {
// Do something with the changed data
},
})
The ?update=${Date.now()}
text part is important if you want to import the changed file with the hot reloading.
const handler = new Handler("dist/files", {
pattern: /\.js$/,
hotReload: true,
loader: (path) => {
delete require.cache[require.resolve(path)]
return require(path)
},
// ...
})
The delete require.cache[require.resolve(path)]
line is important if you want to require the changed file with the hot reloading.
import fs from "fs"
const handler = new Handler("dist/files", {
pattern: /\.txt$/i,
hotReload: true,
loader: async (filepath) => {
return fs.promises.readFile(filepath, "utf8")
},
})
FAQs
File handler with real hot-reload for Node.js
The npm package @ghom/handler receives a total of 39 weekly downloads. As such, @ghom/handler popularity was classified as not popular.
We found that @ghom/handler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.