MCPcopy Index your code
hub / github.com/pocketbase/pocketbase / BindCore

Function BindCore

plugins/jsvm/binds.go:298–664  ·  view source on GitHub ↗

BindCore registers common core objects and functions such as sleep, toString, DynamicModel, etc. into the provided runtime.

(vm *goja.Runtime)

Source from the content-addressed store, hash-verified

296// BindCore registers common core objects and functions such as sleep,
297// toString, DynamicModel, etc. into the provided runtime.
298func BindCore(vm *goja.Runtime) {
299 vm.SetFieldNameMapper(FieldMapper{})
300
301 // deprecated: use toString
302 vm.Set("readerToString", func(r io.Reader, maxBytes int) (string, error) {
303 if maxBytes == 0 {
304 maxBytes = router.DefaultMaxMemory
305 }
306
307 limitReader := io.LimitReader(r, int64(maxBytes))
308
309 bodyBytes, readErr := io.ReadAll(limitReader)
310 if readErr != nil {
311 return "", readErr
312 }
313
314 return string(bodyBytes), nil
315 })
316
317 // note: throw only on reader error
318 vm.Set("toBytes", func(raw any, maxReaderBytes int) ([]byte, error) {
319 switch v := raw.(type) {
320 case nil:
321 return []byte{}, nil
322 case string:
323 return []byte(v), nil
324 case []byte:
325 return v, nil
326 case types.JSONRaw:
327 return v, nil
328 case io.Reader:
329 if maxReaderBytes == 0 {
330 maxReaderBytes = router.DefaultMaxMemory
331 }
332
333 limitReader := io.LimitReader(v, int64(maxReaderBytes))
334
335 return io.ReadAll(limitReader)
336 default:
337 b, err := cast.ToUint8SliceE(v)
338 if err == nil {
339 return b, nil
340 }
341
342 str, err := cast.ToStringE(v)
343 if err == nil {
344 return []byte(str), nil
345 }
346
347 // as a last attempt try to json encode the value
348 rawBytes, _ := json.Marshal(raw)
349
350 return rawBytes, nil
351 }
352 })
353
354 // note: throw only on reader error
355 vm.Set("toString", func(raw any, maxReaderBytes int) (string, error) {

Callers 15

TestBindCoreCountFunction · 0.85
TestBindCoreSleepFunction · 0.85
TestBindCoreToStringFunction · 0.85
TestBindCoreToBytesFunction · 0.85
TestBindCoreUnmarshalFunction · 0.85
TestBindCoreContextFunction · 0.85
TestBindCoreCookieFunction · 0.85
TestBindCoreRecordFunction · 0.85
TestBindCoreCollectionFunction · 0.85
TestBindCoreFieldsListFunction · 0.85

Calls 12

UnmarshalJSONMethod · 0.95
NewRecordFunction · 0.92
NewFieldsListFunction · 0.92
NowDateTimeFunction · 0.92
ParseDateTimeFunction · 0.92
newDynamicModelFunction · 0.85
structConstructorFunction · 0.85
GetOrSetMethod · 0.80
SetMethod · 0.65
LoadMethod · 0.45
StringMethod · 0.45

Tested by 15

TestBindCoreCountFunction · 0.68
TestBindCoreSleepFunction · 0.68
TestBindCoreToStringFunction · 0.68
TestBindCoreToBytesFunction · 0.68
TestBindCoreUnmarshalFunction · 0.68
TestBindCoreContextFunction · 0.68
TestBindCoreCookieFunction · 0.68
TestBindCoreRecordFunction · 0.68
TestBindCoreCollectionFunction · 0.68
TestBindCoreFieldsListFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…