Files
speckle-server/packages/server/modules/core/rest/speckleObjectsStream.ts
T
Kristaps Fabians Geikins 4b06f42db7 chore(server): run TS files directly (no compilation) (#5134)
* sort of works

* type fixes

* added option to run old way too
2025-07-23 11:20:40 +02:00

52 lines
1.3 KiB
TypeScript

import { ensureError } from '@speckle/shared'
import type { TransformCallback } from 'stream'
import { Transform } from 'stream'
/**
* A stream that converts database objects stream to "{id}\t{data_json}\n" stream or a json stream of obj.data fields
*/
class SpeckleObjectsStream extends Transform {
simpleText: boolean
isFirstObject: boolean
constructor(simpleText: boolean) {
super({ writableObjectMode: true })
this.simpleText = simpleText
if (!this.simpleText) this.push('[')
this.isFirstObject = true
}
_transform(
dbObj: { dataText: string; id: string; data?: Record<string, unknown> },
_encoding: BufferEncoding,
callback: TransformCallback
) {
let objData = dbObj.dataText
if (objData === undefined) objData = JSON.stringify(dbObj.data)
try {
if (this.simpleText) {
this.push(`${dbObj.id}\t`)
this.push(objData)
this.push('\n')
} else {
// JSON output
if (!this.isFirstObject) this.push(',')
this.push(objData)
this.isFirstObject = false
}
callback()
} catch (e) {
callback(ensureError(e))
}
}
_flush(callback: TransformCallback) {
if (!this.simpleText) this.push(']')
callback()
}
}
export { SpeckleObjectsStream }