change(tracker): configure per message compression

This commit is contained in:
nick-delirium 2023-04-06 14:53:43 +02:00 committed by Delirium
parent 1125d0c15d
commit ab279697a4
6 changed files with 302 additions and 5 deletions

View file

@ -7,7 +7,16 @@ import PrimitiveEncoder from './PrimitiveEncoder.js'
export default class MessageEncoder extends PrimitiveEncoder { export default class MessageEncoder extends PrimitiveEncoder {
encode(msg: Message): boolean { encode(msg: Message, isCompressed: boolean): boolean {
if (isCompressed) {
switch(msg[0]) {
<% $messages.select { |msg| msg.tracker }.each do |msg| %>
case Messages.Type.<%= msg.name %>:
return <% if msg.attributes.size == 0 %> true <% else %> <%= msg.attributes.map.with_index { |attr, index| "this.encodeCompressed(msg[#{index+1}])" }.join " && " %><% end %>
break
<% end %>
}
}
switch(msg[0]) { switch(msg[0]) {
<% $messages.select { |msg| msg.tracker }.each do |msg| %> <% $messages.select { |msg| msg.tracker }.each do |msg| %>
case Messages.Type.<%= msg.name %>: case Messages.Type.<%= msg.name %>:

View file

@ -48,7 +48,8 @@
}, },
"dependencies": { "dependencies": {
"@medv/finder": "^3.0.0", "@medv/finder": "^3.0.0",
"error-stack-parser": "^2.0.6" "error-stack-parser": "^2.0.6",
"fflate": "^0.7.4"
}, },
"engines": { "engines": {
"node": ">=14.0" "node": ">=14.0"

View file

@ -7,7 +7,7 @@ import Sanitizer from './sanitizer.js'
import Ticker from './ticker.js' import Ticker from './ticker.js'
import Logger, { LogLevel } from './logger.js' import Logger, { LogLevel } from './logger.js'
import Session from './session.js' import Session from './session.js'
import { gzip, strToU8 } from 'fflate'
import { deviceMemory, jsHeapSizeLimit } from '../modules/performance.js' import { deviceMemory, jsHeapSizeLimit } from '../modules/performance.js'
import type { Options as ObserverOptions } from './observer/top_observer.js' import type { Options as ObserverOptions } from './observer/top_observer.js'
@ -208,6 +208,13 @@ export default class App {
private _usingOldFetchPlugin = false private _usingOldFetchPlugin = false
send(message: Message, urgent = false): void { send(message: Message, urgent = false): void {
// @ts-ignore
const compressedMessage = message.reduce((acc, curr, index) => {
if (index === 0) return curr
// @ts-ignore
return gzip(strToU8(curr.toString()), (err, data) => data)
}, [])
console.log(message, compressedMessage)
if (this.activityState === ActivityState.NotActive) { if (this.activityState === ActivityState.NotActive) {
return return
} }

View file

@ -1,4 +1,5 @@
// Auto-generated, do not edit // Auto-generated, do not edit
// @ts-nocheck
/* eslint-disable */ /* eslint-disable */
import * as Messages from '../common/messages.gen.js' import * as Messages from '../common/messages.gen.js'
@ -7,7 +8,276 @@ import PrimitiveEncoder from './PrimitiveEncoder.js'
export default class MessageEncoder extends PrimitiveEncoder { export default class MessageEncoder extends PrimitiveEncoder {
encode(msg: Message): boolean { encode(msg: Message, isCompressed?: boolean): boolean {
if (isCompressed) {
switch(msg[0]) {
case Messages.Type.Timestamp:
return this.encodeCompressed(msg[1])
break
case Messages.Type.SetPageLocation:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.SetViewportSize:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.SetViewportScroll:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.CreateDocument:
return true
break
case Messages.Type.CreateElementNode:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5])
break
case Messages.Type.CreateTextNode:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.MoveNode:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.RemoveNode:
return this.encodeCompressed(msg[1])
break
case Messages.Type.SetNodeAttribute:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.RemoveNodeAttribute:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.SetNodeData:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.SetNodeScroll:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.SetInputTarget:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.SetInputValue:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.SetInputChecked:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.MouseMove:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.NetworkRequest:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5]) && this.encodeCompressed(msg[6]) && this.encodeCompressed(msg[7]) && this.encodeCompressed(msg[8])
break
case Messages.Type.ConsoleLog:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.PageLoadTiming:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5]) && this.encodeCompressed(msg[6]) && this.encodeCompressed(msg[7]) && this.encodeCompressed(msg[8]) && this.encodeCompressed(msg[9])
break
case Messages.Type.PageRenderTiming:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.CustomEvent:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.UserID:
return this.encodeCompressed(msg[1])
break
case Messages.Type.UserAnonymousID:
return this.encodeCompressed(msg[1])
break
case Messages.Type.Metadata:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.CSSInsertRule:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.CSSDeleteRule:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.Fetch:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5]) && this.encodeCompressed(msg[6]) && this.encodeCompressed(msg[7])
break
case Messages.Type.Profiler:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.OTable:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.StateAction:
return this.encodeCompressed(msg[1])
break
case Messages.Type.Redux:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.Vuex:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.MobX:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.NgRx:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.GraphQL:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.PerformanceTrack:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.StringDict:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.SetNodeAttributeDict:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.ResourceTimingDeprecated:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5]) && this.encodeCompressed(msg[6]) && this.encodeCompressed(msg[7]) && this.encodeCompressed(msg[8])
break
case Messages.Type.ConnectionInformation:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.SetPageVisibility:
return this.encodeCompressed(msg[1])
break
case Messages.Type.LoadFontFace:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.SetNodeFocus:
return this.encodeCompressed(msg[1])
break
case Messages.Type.LongTask:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5]) && this.encodeCompressed(msg[6]) && this.encodeCompressed(msg[7])
break
case Messages.Type.SetNodeAttributeURLBased:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.SetCSSDataURLBased:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.TechnicalInfo:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.CustomIssue:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.CSSInsertRuleURLBased:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.MouseClick:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.CreateIFrameDocument:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.AdoptedSSReplaceURLBased:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.AdoptedSSInsertRuleURLBased:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.AdoptedSSDeleteRule:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.AdoptedSSAddOwner:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.AdoptedSSRemoveOwner:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.JSException:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4])
break
case Messages.Type.Zustand:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.BatchMetadata:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5])
break
case Messages.Type.PartitionedMessage:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2])
break
case Messages.Type.InputChange:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5]) && this.encodeCompressed(msg[6])
break
case Messages.Type.SelectionChange:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3])
break
case Messages.Type.MouseThrashing:
return this.encodeCompressed(msg[1])
break
case Messages.Type.UnbindNodes:
return this.encodeCompressed(msg[1])
break
case Messages.Type.ResourceTiming:
return this.encodeCompressed(msg[1]) && this.encodeCompressed(msg[2]) && this.encodeCompressed(msg[3]) && this.encodeCompressed(msg[4]) && this.encodeCompressed(msg[5]) && this.encodeCompressed(msg[6]) && this.encodeCompressed(msg[7]) && this.encodeCompressed(msg[8]) && this.encodeCompressed(msg[9]) && this.encodeCompressed(msg[10])
break
}
}
switch(msg[0]) { switch(msg[0]) {
case Messages.Type.Timestamp: case Messages.Type.Timestamp:

View file

@ -114,4 +114,14 @@ export default class PrimitiveEncoder {
this.reset() this.reset()
return data return data
} }
encodeCompressed(message: Uint8Array) {
const length = message.byteLength
if (!this.uint(length) || this.offset + length > this.size) {
return false
}
this.data.set(message, this.offset)
this.offset += length
return true
}
} }

View file

@ -62,7 +62,7 @@ export default class QueueSender {
body: batch, body: batch,
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.token, Authorization: `Bearer ${this.token as string}`,
//"Content-Type": "", //"Content-Type": "",
}, },
keepalive: batch.length < KEEPALIVE_SIZE_LIMIT, keepalive: batch.length < KEEPALIVE_SIZE_LIMIT,