cannot send buffer over IPC :(
This commit is contained in:
parent
af126ee900
commit
0ba919cc71
2 changed files with 33 additions and 16 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { ChildProcess, fork } from 'child_process';
|
||||
import { EventEmitter } from 'events';
|
||||
import { AddTlvOperation, ITlvStorageOperation, LoadLatestTlvOperation, LoadTlvFileOperation, NewTlvStorageOperation, TlvOperationResponse, TlvStorageSettings } from './tlvFilesStorageProcessor';
|
||||
import { AddTlvOperation, ITlvStorageOperation, LoadLatestTlvOperation, LoadTlvFileOperation, NewTlvStorageOperation, SerializableLatestData, SerializableTlvFile, TlvOperationResponse, TlvStorageSettings } from './tlvFilesStorageProcessor';
|
||||
import { LatestData, TlvFile } from './tlvFilesStorage';
|
||||
|
||||
export type TlvStorageInterface = {
|
||||
|
|
@ -56,20 +56,29 @@ export class TlvStorageFactory extends EventEmitter {
|
|||
|
||||
AddTlv(storageName: string, appId: string, dataName: string, tlv: Uint8Array): Promise<number> {
|
||||
const opId = Math.random().toString()
|
||||
const op: AddTlvOperation = { type: 'addTlv', opId, storageName, appId, dataName, tlv }
|
||||
const op: AddTlvOperation = { type: 'addTlv', opId, storageName, appId, dataName, base64Tlv: Buffer.from(tlv).toString('base64') }
|
||||
return this.handleOp<number>(op)
|
||||
}
|
||||
|
||||
LoadLatest(storageName: string, limit?: number): Promise<LatestData> {
|
||||
async LoadLatest(storageName: string, limit?: number): Promise<LatestData> {
|
||||
const opId = Math.random().toString()
|
||||
const op: LoadLatestTlvOperation = { type: 'loadLatestTlv', opId, storageName, limit }
|
||||
return this.handleOp<LatestData>(op)
|
||||
const latestData = await this.handleOp<SerializableLatestData>(op)
|
||||
const deserializedLatestData: LatestData = {}
|
||||
for (const appId in latestData) {
|
||||
deserializedLatestData[appId] = {}
|
||||
for (const dataName in latestData[appId]) {
|
||||
deserializedLatestData[appId][dataName] = { tlvs: latestData[appId][dataName].base64tlvs.map(tlv => new Uint8Array(Buffer.from(tlv, 'base64'))), current_chunk: latestData[appId][dataName].current_chunk, available_chunks: latestData[appId][dataName].available_chunks }
|
||||
}
|
||||
}
|
||||
return deserializedLatestData
|
||||
}
|
||||
|
||||
LoadFile(storageName: string, appId: string, dataName: string, chunk: number): Promise<TlvFile> {
|
||||
async LoadFile(storageName: string, appId: string, dataName: string, chunk: number): Promise<TlvFile> {
|
||||
const opId = Math.random().toString()
|
||||
const op: LoadTlvFileOperation = { type: 'loadTlvFile', opId, storageName, appId, dataName, chunk }
|
||||
return this.handleOp<TlvFile>(op)
|
||||
const tlvFile = await this.handleOp<SerializableTlvFile>(op)
|
||||
return { fileData: Buffer.from(tlvFile.base64fileData, 'base64'), chunks: tlvFile.chunks }
|
||||
}
|
||||
|
||||
private handleOp<T>(op: ITlvStorageOperation): Promise<T> {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { PubLogger, getLogger } from '../../helpers/logger.js';
|
||||
import { TlvFilesStorage } from './tlvFilesStorage.js';
|
||||
|
||||
export type SerializableLatestData = Record<string, Record<string, { base64tlvs: string[], current_chunk: number, available_chunks: number[] }>>
|
||||
export type SerializableTlvFile = { base64fileData: string, chunks: number[] }
|
||||
export type TlvStorageSettings = {
|
||||
path: string
|
||||
name: string
|
||||
|
|
@ -19,7 +20,7 @@ export type AddTlvOperation = {
|
|||
storageName: string
|
||||
appId: string
|
||||
dataName: string
|
||||
tlv: Uint8Array
|
||||
base64Tlv: string
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +74,6 @@ class TlvFilesStorageProcessor {
|
|||
private async handleOperation(operation: TlvStorageOperation) {
|
||||
try {
|
||||
const opId = operation.opId;
|
||||
if (operation.type === 'addTlv') operation.tlv = new Uint8Array(operation.tlv)
|
||||
if (operation.debug) console.log('handleOperation', operation)
|
||||
switch (operation.type) {
|
||||
case 'newStorage':
|
||||
|
|
@ -132,8 +132,9 @@ class TlvFilesStorageProcessor {
|
|||
})
|
||||
return
|
||||
}
|
||||
this.storages[operation.storageName].AddTlv(operation.appId, operation.dataName, operation.tlv)
|
||||
this.sendResponse({
|
||||
const tlv = new Uint8Array(Buffer.from(operation.base64Tlv, 'base64'))
|
||||
this.storages[operation.storageName].AddTlv(operation.appId, operation.dataName, tlv)
|
||||
this.sendResponse<null>({
|
||||
success: true,
|
||||
type: 'addTlv',
|
||||
data: null,
|
||||
|
|
@ -151,10 +152,17 @@ class TlvFilesStorageProcessor {
|
|||
return
|
||||
}
|
||||
const data = this.storages[operation.storageName].LoadLatest(operation.limit)
|
||||
this.sendResponse({
|
||||
const serializableData: SerializableLatestData = {}
|
||||
for (const appId in data) {
|
||||
serializableData[appId] = {}
|
||||
for (const dataName in data[appId]) {
|
||||
serializableData[appId][dataName] = { base64tlvs: data[appId][dataName].tlvs.map(tlv => Buffer.from(tlv).toString('base64')), current_chunk: data[appId][dataName].current_chunk, available_chunks: data[appId][dataName].available_chunks }
|
||||
}
|
||||
}
|
||||
this.sendResponse<SerializableLatestData>({
|
||||
success: true,
|
||||
type: 'loadLatest',
|
||||
data: data,
|
||||
data: serializableData,
|
||||
opId: operation.opId
|
||||
});
|
||||
}
|
||||
|
|
@ -169,15 +177,15 @@ class TlvFilesStorageProcessor {
|
|||
return
|
||||
}
|
||||
const data = this.storages[operation.storageName].LoadFile(operation.appId, operation.dataName, operation.chunk)
|
||||
this.sendResponse({
|
||||
this.sendResponse<SerializableTlvFile>({
|
||||
success: true,
|
||||
type: 'loadFile',
|
||||
data: data,
|
||||
data: { base64fileData: Buffer.from(data.fileData).toString('base64'), chunks: data.chunks },
|
||||
opId: operation.opId
|
||||
});
|
||||
}
|
||||
|
||||
private sendResponse(response: TlvOperationResponse<any>) {
|
||||
private sendResponse<T>(response: TlvOperationResponse<T>) {
|
||||
if (process.send) {
|
||||
process.send(response);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue