nuclei/pkg/js/libs/bytes/buffer.go
HD Moore f26996cb89
Remove singletons from Nuclei engine (continuation of #6210) (#6296)
* introducing execution id

* wip

* .

* adding separate execution context id

* lint

* vet

* fixing pg dialers

* test ignore

* fixing loader FD limit

* test

* fd fix

* wip: remove CloseProcesses() from dev merge

* wip: fix merge issue

* protocolstate: stop memguarding on last dialer delete

* avoid data race in dialers.RawHTTPClient

* use shared logger and avoid race conditions

* use shared logger and avoid race conditions

* go mod

* patch executionId into compiled template cache

* clean up comment in Parse

* go mod update

* bump echarts

* address merge issues

* fix use of gologger

* switch cmd/nuclei to options.Logger

* address merge issues with go.mod

* go vet: address copy of lock with new Copy function

* fixing tests

* disable speed control

* fix nil ExecuterOptions

* removing deprecated code

* fixing result print

* default logger

* cli default logger

* filter warning from results

* fix performance test

* hardcoding path

* disable upload

* refactor(runner): uses `Warning` instead of `Print` for `pdcpUploadErrMsg`

Signed-off-by: Dwi Siswanto <git@dw1.io>

* Revert "disable upload"

This reverts commit 114fbe6663361bf41cf8b2645fd2d57083d53682.

* Revert "hardcoding path"

This reverts commit cf12ca800e0a0e974bd9fd4826a24e51547f7c00.

---------

Signed-off-by: Dwi Siswanto <git@dw1.io>
Co-authored-by: Mzack9999 <mzack9999@protonmail.com>
Co-authored-by: Dwi Siswanto <git@dw1.io>
Co-authored-by: Dwi Siswanto <25837540+dwisiswant0@users.noreply.github.com>
2025-07-10 01:17:26 +05:30

141 lines
3.5 KiB
Go

package bytes
import (
"encoding/hex"
"github.com/Mzack9999/goja"
"github.com/projectdiscovery/nuclei/v3/pkg/js/libs/structs"
"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
)
type (
// Buffer is a bytes/Uint8Array type in javascript
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const bytes = new bytes.Buffer();
// ```
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// // optionally it can accept existing byte/Uint8Array as input
// const bytes = new bytes.Buffer([1, 2, 3]);
// ```
Buffer struct {
buf []byte
}
)
// NewBuffer creates a new buffer from a byte slice.
func NewBuffer(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {
if len(call.Arguments) > 0 {
if arg, ok := call.Argument(0).Export().([]byte); ok {
return utils.LinkConstructor(call, runtime, &Buffer{buf: arg})
} else {
utils.NewNucleiJS(runtime).Throw("Invalid argument type. Expected bytes/Uint8Array as input but got %T", call.Argument(0).Export())
}
}
return utils.LinkConstructor(call, runtime, &Buffer{})
}
// Write appends the given data to the buffer.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.Write([1, 2, 3]);
// ```
func (b *Buffer) Write(data []byte) *Buffer {
b.buf = append(b.buf, data...)
return b
}
// WriteString appends the given string data to the buffer.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.WriteString('hello');
// ```
func (b *Buffer) WriteString(data string) *Buffer {
b.buf = append(b.buf, []byte(data)...)
return b
}
// Bytes returns the byte representation of the buffer.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.WriteString('hello');
// log(buffer.Bytes());
// ```
func (b *Buffer) Bytes() []byte {
return b.buf
}
// String returns the string representation of the buffer.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.WriteString('hello');
// log(buffer.String());
// ```
func (b *Buffer) String() string {
return string(b.buf)
}
// Len returns the length of the buffer.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.WriteString('hello');
// log(buffer.Len());
// ```
func (b *Buffer) Len() int {
return len(b.buf)
}
// Hex returns the hex representation of the buffer.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.WriteString('hello');
// log(buffer.Hex());
// ```
func (b *Buffer) Hex() string {
return hex.EncodeToString(b.buf)
}
// Hexdump returns the hexdump representation of the buffer.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.WriteString('hello');
// log(buffer.Hexdump());
// ```
func (b *Buffer) Hexdump() string {
return hex.Dump(b.buf)
}
// Pack uses structs.Pack and packs given data and appends it to the buffer.
// it packs the data according to the given format.
// @example
// ```javascript
// const bytes = require('nuclei/bytes');
// const buffer = new bytes.Buffer();
// buffer.Pack('I', 123);
// ```
func (b *Buffer) Pack(formatStr string, msg any) error {
bin, err := structs.Pack(formatStr, msg)
if err != nil {
return err
}
b.Write(bin)
return nil
}