@river-build/sdk
Version:
For more details, visit the following resources:
73 lines • 2.96 kB
JavaScript
import { ChannelMessageSchema, EncryptedDataVersion, ChannelPropertiesSchema, } from '@river-build/proto';
import { fromBinary, fromJsonString } from '@bufbuild/protobuf';
import { checkNever, logNever } from './check';
export function isEncryptedContentKind(kind) {
return kind === 'text' || kind === 'channelMessage' || kind === 'channelProperties';
}
export function toDecryptedContent(kind, dataVersion, cleartext) {
switch (dataVersion) {
case EncryptedDataVersion.ENCRYPTED_DATA_VERSION_0:
if (typeof cleartext !== 'string') {
throw new Error('cleartext is not a string when dataversion is 0');
}
switch (kind) {
case 'text':
return {
kind,
content: cleartext,
};
case 'channelMessage':
return {
kind,
content: fromJsonString(ChannelMessageSchema, cleartext),
};
case 'channelProperties':
return {
kind,
content: fromJsonString(ChannelPropertiesSchema, cleartext),
};
default:
// the client is responsible for this
// we should never have a type we don't know about locally here
checkNever(kind);
return {
kind: 'unsupported',
content: cleartext,
};
}
case EncryptedDataVersion.ENCRYPTED_DATA_VERSION_1:
if (typeof cleartext === 'string') {
throw new Error('cleartext is a string when dataversion is 1');
}
switch (kind) {
case 'text':
return {
kind: 'text',
content: new TextDecoder().decode(cleartext),
};
case 'channelProperties':
return {
kind: 'channelProperties',
content: fromBinary(ChannelPropertiesSchema, cleartext),
};
case 'channelMessage':
return {
kind: 'channelMessage',
content: fromBinary(ChannelMessageSchema, cleartext),
};
default:
checkNever(kind); // local to our codebase, should never happen
return {
kind: 'unsupported',
content: cleartext,
};
}
default:
logNever(dataVersion);
return {
kind: 'unsupported',
content: cleartext,
};
}
}
//# sourceMappingURL=encryptedContentTypes.js.map