farmcontrol-ws/src/socket/stringidparser.js
Tom Butcher 1ae6c3f05c
Some checks failed
farmcontrol/farmcontrol-ws/pipeline/head There was a failure building this commit
Enhance database object handling with ObjectId normalization and error handling
- Introduced `refId`, `normalizeFilter`, and `coerceObjectIds` functions in `objectid.js` to standardize ObjectId handling across the database operations.
- Updated `listObjects`, `getObject`, `editObject`, and `newObject` functions to utilize the new normalization methods, improving data integrity and error management.
- Enhanced error handling in socket event handlers to ensure proper feedback on operation failures.
- Refactored `expandObjectIds` and related utility functions to streamline ObjectId processing and improve code clarity.
2026-11-07 18:52:45 +00:00

31 lines
790 B
JavaScript

import { createRequire } from 'node:module';
import { stringifyIds } from '../database/objectid.js';
const require = createRequire(import.meta.url);
const { Encoder, Decoder } = createRequire(require.resolve('socket.io'))(
'socket.io-parser'
);
class StringIdEncoder extends Encoder {
encode(packet) {
if (packet?.data == null) return super.encode(packet);
return super.encode({ ...packet, data: stringifyIds(packet.data) });
}
}
export const stringIdParser = {
Encoder: StringIdEncoder,
Decoder
};
export function useStringIds(socket) {
socket.use((packet, next) => {
for (let index = 1; index < packet.length; index += 1) {
if (typeof packet[index] !== 'function') {
packet[index] = stringifyIds(packet[index]);
}
}
next();
});
}