mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 11:39:50 +02:00
fix: deterministic proto loading
This commit is contained in:
1 parent
cbb5392525
commit
169f757fb2
11 files changed
+53
-34
No files matched your search
@@ -52,7 +52,8 @@ export const averageDefinitions = fetch(asset('averages/manifest.json'), {
|
||||
.then((response) => response.arrayBuffer())
|
||||
.then((buffer) =>
|
||||
decode<AverageDefinitions>(
|
||||
'blitzkit/blitzkit.AverageDefinitions',
|
||||
'average_definitions',
|
||||
'blitzkit.AverageDefinitions',
|
||||
new Uint8Array(buffer),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -18,5 +18,5 @@ export interface Video {
|
||||
export const reviews = fetch(asset('definitions/reviews.pb'))
|
||||
.then((response) => response.arrayBuffer())
|
||||
.then((buffer) =>
|
||||
decode<Reviews>('reviews/blitzkit.Reviews', new Uint8Array(buffer)),
|
||||
decode<Reviews>('reviews', 'blitzkit.Reviews', new Uint8Array(buffer)),
|
||||
);
|
||||
@@ -1,6 +1,9 @@
|
||||
import { lookup } from './lookup';
|
||||
import { lookup, ProtoSource } from './lookup';
|
||||
|
||||
export async function decode<Type>(proto: string, data: Uint8Array) {
|
||||
const Message = await lookup(proto);
|
||||
return Message.decode(data).toJSON() as Type;
|
||||
export function decode<Type>(
|
||||
source: ProtoSource,
|
||||
type: string,
|
||||
data: Uint8Array,
|
||||
) {
|
||||
return lookup(source, type).decode(data).toJSON() as Type;
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
import { lookup } from './lookup';
|
||||
import { lookup, ProtoSource } from './lookup';
|
||||
|
||||
export async function encode(proto: string, data: any) {
|
||||
const Message = await lookup(proto);
|
||||
const verification = Message.verify(data);
|
||||
export function encode(
|
||||
source: ProtoSource,
|
||||
type: string,
|
||||
data: Record<string, any>,
|
||||
) {
|
||||
const message = lookup(source, type);
|
||||
const verification = message.verify(data);
|
||||
|
||||
if (verification !== null) throw new Error(verification);
|
||||
|
||||
return Message.encode(Message.create(data)).finish();
|
||||
return message.encode(message.create(data)).finish();
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import { encode } from './encode';
|
||||
import { ProtoSource } from './lookup';
|
||||
|
||||
export async function encodeToBase64(proto: string, data: any) {
|
||||
const array = await encode(proto, data);
|
||||
export async function encodeToBase64(
|
||||
source: ProtoSource,
|
||||
type: string,
|
||||
data: any,
|
||||
) {
|
||||
const array = encode(source, type, data);
|
||||
return Buffer.from(array).toString('base64');
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
import { Root } from 'protobufjs';
|
||||
import { context } from '../blitzkit/context';
|
||||
import { parse } from 'protobufjs';
|
||||
import average_definitions from '../../../public/protos/average_definitions.proto';
|
||||
import reviews from '../../../public/protos/reviews.proto';
|
||||
|
||||
export async function lookup(proto: string) {
|
||||
const [file, message] = proto.split('/');
|
||||
const root = await new Root().load(
|
||||
context === 'server'
|
||||
? `public/protos/${file}.proto`
|
||||
: `/protos/${file}.proto`,
|
||||
{ keepCase: true },
|
||||
);
|
||||
export type ProtoSource = keyof typeof protos;
|
||||
|
||||
return root.lookupType(message);
|
||||
const protos = { reviews, average_definitions };
|
||||
|
||||
export function lookup(source: ProtoSource, type: string) {
|
||||
return parse(protos[source]).root.lookupType(type);
|
||||
}
|
||||
Reference in new issue
Block a user