This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice-detector.go
109 lines (96 loc) · 3.28 KB
/
device-detector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package devicedetector
import (
"io/ioutil"
"log"
"strings"
v8 "rogchap.com/v8go"
)
// TODO: wont work with concurrent reqs design for concurrency
// Parser is a device detector parser.
type Parser struct {
options DeviceDetectorOptions
ctx *v8.Context
Parse func(userAgent string) (result interface{}, err error)
}
// initContext initializes a v8 context from compiled device-detector-js library.
func initContext() (*v8.Context, error) {
file, err := ioutil.ReadFile("build/dist/device-detector.js")
if err != nil {
log.Fatalf("failed to read file: %v", err)
}
src := string(file)
src = strings.TrimPrefix(src, `"use strict";`+"\n")
src = strings.TrimPrefix(src, "(() => {\n")
src = strings.TrimSuffix(src, "\n})();\n")
// Todo: take these from New_ functions nad run with native v8 apis. or export all funcs to global var with regexp replace.
src = src + "\n" + `DeviceDetector = new require_src()`
src = src + "\n" + `BotDetector = new require_bot()`
// src = src + "\n" + `ClientParser_ = new init_client()`
// src = src + "\n" + `DeviceParser = new init_device()`
// src = src + "\n" + `OperatingSystemParser_ = new init_operating_system2()`
// src = src + "\n" + `VendorFragmentParser_ = new init_vendor_fragment()`
ctx := v8.NewContext()
_, e := ctx.RunScript(src+"\n", "h.js")
if e != nil {
return nil, e
}
return ctx, nil
}
// TODO: clear duplicated code.
// TODO: use types.
// NewDeviceDetector creates a new device detector.
func NewDeviceDetector(options DeviceDetectorOptions) (parser Parser, err error) {
ctx, err := initContext()
if err != nil {
return
}
class, _ := ctx.Global().Get("DeviceDetector")
classTmp, _ := class.Object().AsFunction()
instance, err := classTmp.NewInstance(v8.Undefined(v8.NewIsolate()))
return Parser{
options: options,
ctx: ctx,
Parse: func(userAgent string) (result interface{}, err error) {
ctx.Global().Set("userAgent", userAgent)
parse, _ := instance.Object().Get("parse")
res, _ := parse.AsFunction()
ua, _ := ctx.Global().Get("userAgent")
r, _ := res.Call(v8.Undefined(ctx.Isolate()), ua)
return r.Object().MarshalJSON()
},
}, err
}
// NewBotDetector creates a new bot detector with options.
func NewBotDetector(options DeviceDetectorOptions) (parser Parser, err error) {
ctx, err := initContext()
if err != nil {
return
}
class, _ := ctx.Global().Get("BotDetector")
classTmp, _ := class.Object().AsFunction()
instance, err := classTmp.NewInstance(v8.Undefined(v8.NewIsolate()))
return Parser{
options: options,
ctx: ctx,
Parse: func(userAgent string) (result interface{}, err error) {
ctx.Global().Set("userAgent", userAgent)
parse, _ := instance.Object().Get("parse")
res, _ := parse.AsFunction()
ua, _ := ctx.Global().Get("userAgent")
r, _ := res.Call(v8.Undefined(ctx.Isolate()), ua)
return r.Object().MarshalJSON()
},
}, err
}
// NewDeviceParser creates a new device parser with options
func NewDeviceParser(options DeviceDetectorOptions) (parser Parser) {
panic("not implemented")
}
// NewOperatingSystemParser creates a new operating system parser.
func NewOperatingSystemParser() (parser Parser) {
panic("not implemented")
}
// NewVendorFragmentParser creates a new vendor fragment parser.
func NewVendorFragmentParser() (parser Parser) {
panic("not implemented")
}