fix type definition of converter map

This commit is contained in:
2024-10-07 12:54:49 -05:00
parent 6907cf7595
commit eae4ef7256
+30 -15
View File
@@ -1,6 +1,6 @@
import { OSCType, OSCArg, OSCMessage, OSCTypeConverter } from './models'; import { OSCType, OSCArg, OSCMessage, OSCTypeConverter } from './models';
const oscTypeConverterMap: { [key in OSCType]: OSCTypeConverter } = { const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
s: { s: {
toBuffer: (string) => { toBuffer: (string) => {
if (typeof string === 'string') { if (typeof string === 'string') {
@@ -164,21 +164,36 @@ export function messageFromBuffer(bytes: Buffer): OSCMessage | undefined {
const oscArgs: OSCArg[] = [] const oscArgs: OSCArg[] = []
if(oscTypeConverterMap.s.fromBuffer) { const [address, bytesAfterAddress] = oscTypeConverterMap.s.fromBuffer(bytes);
const [address, bytesAfterAddress] = oscTypeConverterMap.s.fromBuffer(bytes) if(typeof address === 'string'){
if(typeof address === 'string'){ let [typeString, bytesAfterType] = oscTypeConverterMap.s.fromBuffer(bytesAfterAddress);
console.log(`address: ${address}`) if (typeof typeString === 'string') {
console.log(`bytesAfterAddresss: ${bytesAfterAddress}`) if (!typeString.startsWith(',')) {
let [typeString, bytesAfterType] = oscTypeConverterMap.s.fromBuffer(bytesAfterAddress) throw new Error('osc type string must start with a ,');
console.log(`typeString: ${typeString}`) }
console.log(`bytesAfterType: ${bytesAfterType}`) let argsBuffer = bytesAfterType
if(typeof typeString === 'string'){ for (let index = 1; index < typeString.length; index++) {
if(!typeString.startsWith(',')){ const argType = typeString.charAt(index) as OSCType;
throw new Error('osc type string must start with a ,')
const oscTypeConverter = oscTypeConverterMap[argType]
if(oscTypeConverter === undefined){
throw new Error('unknown OSC type')
} }
const [value, remainingBytes] = oscTypeConverter.fromBuffer(argsBuffer)
if(value !== undefined){
const arg: OSCArg = {
type: argType,
value: value
}
oscArgs.push(arg)
}
argsBuffer = remainingBytes;
}
return {
address,
args: oscArgs
} }
} }
} }
}
return
}