fix: deterministic proto loading

This commit is contained in:
TrèsAbhi committed 2024-09-03 21:47:06 -05:00
1 parent cbb5392525
commit 169f757fb2
11 files changed
+51 -32

No files matched your search

+8 -2
View File
@@ -7,10 +7,16 @@ const withVanillaExtract = createVanillaExtractPlugin({ tests: /\.css$/ });
const nextConfig = {
...withVanillaExtract({
webpack: (config, { isServer }) => {
config.module.rules.push({
config.module.rules.push(
{
test: /\.glsl$/,
type: 'asset/source',
});
},
{
test: /\.proto$/,
type: 'asset/source',
},
);
if (isServer) {
writeFileSync(
File renamed without changes.
+2 -1
View File
@@ -225,7 +225,8 @@ async function postWork() {
{
path: `averages/${latest}.pb`,
content: await encodeToBase64(
'blitzkit/blitzkit.AverageDefinitions',
'average_definitions',
'blitzkit.AverageDefinitions',
averageDefinitions,
),
encoding: 'base64',
+3 -5
View File
@@ -10,7 +10,7 @@ import { asset } from '../src/core/blitzkit/asset';
import { commitAssets } from '../src/core/blitzkit/commitAssets';
import { Reviews, Video } from '../src/core/blitzkit/reviews';
import { decode } from '../src/core/protobuf/decode';
import { encode } from '../src/core/protobuf/encode';
import { encodeToBase64 } from '../src/core/protobuf/encodeToBase64';
import { DATA } from './buildAssets/constants';
import {
BlitzStrings,
@@ -25,7 +25,7 @@ console.log('Finding reviews...');
const currentReviews = await 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)),
);
const auth = await google.auth.getClient({
scopes: ['https://www.googleapis.com/auth/youtube.readonly'],
@@ -124,9 +124,7 @@ for (const tank of tanksSanitized) {
}
}
const content = await encode('reviews/blitzkit.Reviews', reviews).then(
(array) => Buffer.from(array).toString('base64'),
);
const content = await encodeToBase64('reviews', 'blitzkit.Reviews', reviews);
await commitAssets('reviews', [
{ content, encoding: 'base64', path: 'definitions/reviews.pb' },
@@ -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),
),
);
+1 -1
View File
@@ -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)),
);
+7 -4
View File
@@ -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;
}
+9 -5
View File
@@ -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();
}
+7 -2
View File
@@ -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');
}
+8 -11
View File
@@ -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);
}
+4
View File
@@ -0,0 +1,4 @@
declare module '*.proto' {
const value: string;
export default value;
}