github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/eval/types.go (about)

     1  package eval
     2  
     3  import (
     4  	"github.com/mafredri/cdp/protocol/runtime"
     5  )
     6  
     7  type (
     8  	RemoteType string
     9  
    10  	RemoteObjectType string
    11  
    12  	RemoteClassName string
    13  )
    14  
    15  // List of possible remote types
    16  // "object", "function", "undefined", "string", "number", "boolean", "symbol", "bigint"
    17  const (
    18  	UnknownType   RemoteType = ""
    19  	UndefinedType RemoteType = "undefined"
    20  	StringType    RemoteType = "string"
    21  	NumberType    RemoteType = "number"
    22  	BooleanType   RemoteType = "boolean"
    23  	SymbolType    RemoteType = "symbol"
    24  	BigintType    RemoteType = "bigint"
    25  	ObjectType    RemoteType = "object"
    26  )
    27  
    28  var remoteTypeMap = map[string]RemoteType{
    29  	string(UndefinedType): UndefinedType,
    30  	string(StringType):    StringType,
    31  	string(NumberType):    NumberType,
    32  	string(BooleanType):   BooleanType,
    33  	string(SymbolType):    SymbolType,
    34  	string(BigintType):    BigintType,
    35  	string(ObjectType):    ObjectType,
    36  }
    37  
    38  // List of possible remote object types
    39  const (
    40  	UnknownObjectType     RemoteObjectType = ""
    41  	NullObjectType        RemoteObjectType = "null"
    42  	UndefinedObjectType   RemoteObjectType = "undefined"
    43  	ArrayObjectType       RemoteObjectType = "array"
    44  	NodeObjectType        RemoteObjectType = "node"
    45  	RegexpObjectType      RemoteObjectType = "regexp"
    46  	DateObjectType        RemoteObjectType = "date"
    47  	MapObjectType         RemoteObjectType = "map"
    48  	SetObjectType         RemoteObjectType = "set"
    49  	WeakMapObjectType     RemoteObjectType = "weakmap"
    50  	WeakSetObjectType     RemoteObjectType = "weakset"
    51  	IteratorObjectType    RemoteObjectType = "iterator"
    52  	GeneratorObjectType   RemoteObjectType = "generator"
    53  	ErrorObjectType       RemoteObjectType = "error"
    54  	ProxyObjectType       RemoteObjectType = "proxy"
    55  	PromiseObjectType     RemoteObjectType = "promise"
    56  	TypedArrayObjectType  RemoteObjectType = "typedarray"
    57  	ArrayBufferObjectType RemoteObjectType = "arraybuffer"
    58  	DataViewObjectType    RemoteObjectType = "dataview"
    59  )
    60  
    61  var remoteObjectTypeMap = map[string]RemoteObjectType{
    62  	string(NullObjectType):        NullObjectType,
    63  	string(UndefinedObjectType):   UndefinedObjectType,
    64  	string(ArrayObjectType):       ArrayObjectType,
    65  	string(NodeObjectType):        NodeObjectType,
    66  	string(RegexpObjectType):      RegexpObjectType,
    67  	string(DateObjectType):        DateObjectType,
    68  	string(MapObjectType):         MapObjectType,
    69  	string(SetObjectType):         SetObjectType,
    70  	string(WeakMapObjectType):     WeakMapObjectType,
    71  	string(WeakSetObjectType):     WeakSetObjectType,
    72  	string(IteratorObjectType):    IteratorObjectType,
    73  	string(GeneratorObjectType):   GeneratorObjectType,
    74  	string(ErrorObjectType):       ErrorObjectType,
    75  	string(ProxyObjectType):       ProxyObjectType,
    76  	string(PromiseObjectType):     PromiseObjectType,
    77  	string(TypedArrayObjectType):  TypedArrayObjectType,
    78  	string(ArrayBufferObjectType): ArrayBufferObjectType,
    79  	string(DataViewObjectType):    DataViewObjectType,
    80  }
    81  
    82  // List of supported remote classses
    83  const (
    84  	UnknownClassName  RemoteClassName = ""
    85  	DocumentClassName RemoteClassName = "HTMLDocument"
    86  )
    87  
    88  var remoteClassNameMap = map[string]RemoteClassName{
    89  	string(DocumentClassName): DocumentClassName,
    90  }
    91  
    92  func ToRemoteType(ref runtime.RemoteObject) RemoteType {
    93  	remoteType, found := remoteTypeMap[ref.Type]
    94  
    95  	if found {
    96  		return remoteType
    97  	}
    98  
    99  	return UnknownType
   100  }
   101  
   102  func ToRemoteObjectType(ref runtime.RemoteObject) RemoteObjectType {
   103  	if ref.Subtype != nil {
   104  		remoteObjectType, found := remoteObjectTypeMap[*ref.Subtype]
   105  
   106  		if found {
   107  			return remoteObjectType
   108  		}
   109  	}
   110  
   111  	return UnknownObjectType
   112  }
   113  
   114  func ToRemoteClassName(ref runtime.RemoteObject) RemoteClassName {
   115  	if ref.ClassName != nil {
   116  		remoteClassName, found := remoteClassNameMap[*ref.ClassName]
   117  
   118  		if found {
   119  			return remoteClassName
   120  		}
   121  	}
   122  
   123  	return UnknownClassName
   124  }