package bytes12import (3"encoding/hex"45"github.com/Mzack9999/goja"6"github.com/projectdiscovery/nuclei/v3/pkg/js/libs/structs"7"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"8)910type (11// Buffer is a bytes/Uint8Array type in javascript12// @example13// ```javascript14// const bytes = require('nuclei/bytes');15// const bytes = new bytes.Buffer();16// ```17// @example18// ```javascript19// const bytes = require('nuclei/bytes');20// // optionally it can accept existing byte/Uint8Array as input21// const bytes = new bytes.Buffer([1, 2, 3]);22// ```23Buffer struct {24buf []byte25}26)2728// NewBuffer creates a new buffer from a byte slice.29func NewBuffer(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {30if len(call.Arguments) > 0 {31if arg, ok := call.Argument(0).Export().([]byte); ok {32return utils.LinkConstructor(call, runtime, &Buffer{buf: arg})33} else {34utils.NewNucleiJS(runtime).Throw("Invalid argument type. Expected bytes/Uint8Array as input but got %T", call.Argument(0).Export())35}36}37return utils.LinkConstructor(call, runtime, &Buffer{})38}3940// Write appends the given data to the buffer.41// @example42// ```javascript43// const bytes = require('nuclei/bytes');44// const buffer = new bytes.Buffer();45// buffer.Write([1, 2, 3]);46// ```47func (b *Buffer) Write(data []byte) *Buffer {48b.buf = append(b.buf, data...)49return b50}5152// WriteString appends the given string data to the buffer.53// @example54// ```javascript55// const bytes = require('nuclei/bytes');56// const buffer = new bytes.Buffer();57// buffer.WriteString('hello');58// ```59func (b *Buffer) WriteString(data string) *Buffer {60b.buf = append(b.buf, []byte(data)...)61return b62}6364// Bytes returns the byte representation of the buffer.65// @example66// ```javascript67// const bytes = require('nuclei/bytes');68// const buffer = new bytes.Buffer();69// buffer.WriteString('hello');70// log(buffer.Bytes());71// ```72func (b *Buffer) Bytes() []byte {73return b.buf74}7576// String returns the string representation of the buffer.77// @example78// ```javascript79// const bytes = require('nuclei/bytes');80// const buffer = new bytes.Buffer();81// buffer.WriteString('hello');82// log(buffer.String());83// ```84func (b *Buffer) String() string {85return string(b.buf)86}8788// Len returns the length of the buffer.89// @example90// ```javascript91// const bytes = require('nuclei/bytes');92// const buffer = new bytes.Buffer();93// buffer.WriteString('hello');94// log(buffer.Len());95// ```96func (b *Buffer) Len() int {97return len(b.buf)98}99100// Hex returns the hex representation of the buffer.101// @example102// ```javascript103// const bytes = require('nuclei/bytes');104// const buffer = new bytes.Buffer();105// buffer.WriteString('hello');106// log(buffer.Hex());107// ```108func (b *Buffer) Hex() string {109return hex.EncodeToString(b.buf)110}111112// Hexdump returns the hexdump representation of the buffer.113// @example114// ```javascript115// const bytes = require('nuclei/bytes');116// const buffer = new bytes.Buffer();117// buffer.WriteString('hello');118// log(buffer.Hexdump());119// ```120func (b *Buffer) Hexdump() string {121return hex.Dump(b.buf)122}123124// Pack uses structs.Pack and packs given data and appends it to the buffer.125// it packs the data according to the given format.126// @example127// ```javascript128// const bytes = require('nuclei/bytes');129// const buffer = new bytes.Buffer();130// buffer.Pack('I', 123);131// ```132func (b *Buffer) Pack(formatStr string, msg any) error {133bin, err := structs.Pack(formatStr, msg)134if err != nil {135return err136}137b.Write(bin)138return nil139}140141142