Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 10x 10x 10x 10x 10x 33x 33x 33x 33x 33x 33x 25x 33x 8x 8x 8x 4x 8x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 2x 6x 4x 4x 4x 4x 33x 33x 33x 10x 10x 10x 6x 6x 6x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 4x 3x 3x 3x 3x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 1x | export {}
const protobuf = require('protobufjs')
const fs = require('fs')
import { Utils } from '../BaseUtils'
import { DataUtils } from '../data'
import { ErrorUtils } from '../error'
const $data = new DataUtils()
const $error = new ErrorUtils()
type TMessage = any
type TEvent = any
const isLive = !fs.existsSync('./lib')
const protoPath = isLive
? './node_modules/@usssa/core/lib/proto/data.proto'
: './lib/proto/data.proto'
export class TransformUtils extends Utils {
constructor() {
super()
}
parsePubSubMessage = (message: TMessage) => {
try {
const raw_message = message.data.message.data
if (!raw_message) throw new Error('Missing message')
const str_message = Buffer.from(raw_message, 'base64').toString()
if (!JSON.parse(str_message)) throw new Error('Malformed message')
const event = JSON.parse(str_message)
return event
} catch (error) {
throw $error.errorHandler({ error })
}
}
transformDecodedFirestoreEvent = (event: TEvent) => {
function transform(currentObj) {
// Initialize an empty object to hold the transformed data
const transformed = {}
// Iterate over each key in the current object
for (const key in currentObj) {
if (currentObj.hasOwnProperty(key)) {
const value = currentObj[key]
if (typeof value === 'object' && value !== null) {
// Check if the object contains any of the specific type keys
if (value.hasOwnProperty('stringValue')) {
transformed[key] = value.stringValue
} else if (value.hasOwnProperty('integerValue')) {
transformed[key] = Number(value.integerValue)
} else if (value.hasOwnProperty('booleanValue')) {
transformed[key] = value.booleanValue
} else if (
value.hasOwnProperty('mapValue') &&
value.mapValue.hasOwnProperty('fields')
) {
// Recursively transform nested mapValue objects that have fields
transformed[key] = transform(value.mapValue.fields)
} else if (
value.hasOwnProperty('arrayValue') &&
value.arrayValue.hasOwnProperty('values')
) {
// Transform each element in the array
transformed[key] = value.arrayValue.values.map((el) => {
// Each element in the arrayValue is an object with a type key
if (el.hasOwnProperty('stringValue')) {
return el.stringValue
} else if (el.hasOwnProperty('integerValue')) {
return Number(el.integerValue)
} else if (el.hasOwnProperty('booleanValue')) {
return el.booleanValue
} else if (
el.hasOwnProperty('mapValue') &&
el.mapValue.hasOwnProperty('fields')
) {
return transform(el.mapValue.fields)
} else {
return transform(el)
}
})
} else {
// If none of the above, recursively transform the current level
transformed[key] = transform(value)
}
} else {
transformed[key] = value // If it's a primitive value, keep it as is
}
}
}
return transformed
}
return transform(event)
}
decodeFirestoreEvent = async (event: TEvent) => {
try {
const docParts = event.document.split('/')
const collection = docParts[0]
const docId = docParts[1]
const root = await protobuf.load(protoPath)
const DocumentEventData = root.lookupType(
'google.events.cloud.firestore.v1.DocumentEventData'
)
const data = DocumentEventData.decode(event.data)
const result: Record<string, any> = {
collection,
docId,
data,
}
if (data.value?.fields) {
result.transformedValue = this.transformDecodedFirestoreEvent(
data.value.fields
)
}
if (data.oldValue?.fields) {
result.transformedOldValue = this.transformDecodedFirestoreEvent(
data.oldValue.fields
)
}
return result
} catch (error) {
throw $error.errorHandler({ error })
}
}
encodeFirestoreEvent = async (event: TEvent) => {
try {
const root = await protobuf.load(protoPath)
const DocumentEventData = root.lookupType(
'google.events.cloud.firestore.v1.DocumentEventData'
)
const data = DocumentEventData.encode(event.data).finish()
return {
...event,
data,
}
} catch (error) {
throw $error.errorHandler({ error })
}
}
parseUpdateDiff = (before: TEvent, after: TEvent, exclude: string[] = []) => {
try {
return $data.compareObjects(after, before, exclude)
} catch (error) {
throw $error.errorHandler({ error })
}
}
}
|