MCPcopy
hub / github.com/pocketbase/pocketbase / registerHooks

Method registerHooks

plugins/jsvm/jsvm.go:237–350  ·  view source on GitHub ↗

registerHooks registers the JS app hooks loader.

()

Source from the content-addressed store, hash-verified

235
236// registerHooks registers the JS app hooks loader.
237func (p *plugin) registerHooks() error {
238 // fetch all js hooks sorted by their filename
239 files, err := filesContent(p.config.HooksDir, p.config.HooksFilesPattern)
240 if err != nil {
241 return err
242 }
243
244 // prepend the types reference directive
245 //
246 // note: it is loaded during startup to handle conveniently also
247 // the case when the HooksWatch option is enabled and the application
248 // restart on newly created file
249 for name, content := range files {
250 if len(content) != 0 {
251 // skip non-empty files for now to prevent accidental overwrite
252 continue
253 }
254 path := filepath.Join(p.config.HooksDir, name)
255 directive := `/// <reference path="` + p.relativeTypesPath(p.config.HooksDir) + `" />`
256 if err := prependToEmptyFile(path, directive+"\n\n"); err != nil {
257 color.Yellow("Unable to prepend the types reference: %v", err)
258 }
259 }
260
261 // initialize the hooks dir watcher
262 if p.config.HooksWatch {
263 if err := p.watchHooks(); err != nil {
264 color.Yellow("Unable to init hooks watcher: %v", err)
265 }
266 }
267
268 if len(files) == 0 {
269 // no need to register the vms since there are no entrypoint files anyway
270 return nil
271 }
272
273 absHooksDir, err := filepath.Abs(p.config.HooksDir)
274 if err != nil {
275 return err
276 }
277
278 p.app.OnServe().BindFunc(func(e *core.ServeEvent) error {
279 e.Router.BindFunc(p.normalizeServeExceptions)
280
281 return e.Next()
282 })
283
284 // safe to be shared across multiple vms
285 requireRegistry := new(require.Registry)
286 templateRegistry := template.NewRegistry()
287
288 sharedBinds := func(vm *goja.Runtime) {
289 requireRegistry.Enable(vm)
290 console.Enable(vm)
291 process.Enable(vm)
292 buffer.Enable(vm)
293
294 BindCore(vm)

Callers 1

RegisterFunction · 0.95

Calls 15

relativeTypesPathMethod · 0.95
watchHooksMethod · 0.95
NewRegistryFunction · 0.92
filesContentFunction · 0.85
prependToEmptyFileFunction · 0.85
BindCoreFunction · 0.85
BindDbxFunction · 0.85
BindSecurityFunction · 0.85
BindOSFunction · 0.85
BindFilepathFunction · 0.85
BindHTTPFunction · 0.85
BindFilesystemFunction · 0.85

Tested by

no test coverage detected