Skip to content

Commit

Permalink
fix(bson): make sure index signature keys use full utf8 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Jun 17, 2024
1 parent 2332134 commit d447c1d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/bson/src/bson-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ function handleObjectLiteral(
if (target === 'serialization') {
nameWriter = `
state.writer.prepareWriteType();
state.writer.writeAsciiString(${i});
state.writer.writeString(${i});
state.writer.writeByte(0);
`;
} else if (target === 'sizer') {
Expand Down Expand Up @@ -865,7 +865,7 @@ function propertyNameWrite(propertyName?: string | RuntimeCode) {
if (propertyName) {
if (propertyName instanceof RuntimeCode) {
return `
state.writer.writeAsciiString(${propertyName.code});
state.writer.writeString(${propertyName.code});
state.writer.writeByte(0);
`;
} else {
Expand Down
42 changes: 42 additions & 0 deletions packages/bson/tests/bson-serialize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,3 +1537,45 @@ test('NaN roundtrip to 0', () => {
expect(back.v).toBe(0);
}
});

test('utf8', () => {
const messages = {
'— feel free to": "— それまでご自由に': '— feel free to": "— それまでご自由に',
'Schoolismの1年間のサブスクリプションを勝つチャンスを得るために、ツアーを必ず完全に終了してください! 体験は約10分で完了します': 'Schoolismの1年間のサブスクリプションを勝つチャンスを得るために、ツアーを必ず完全に終了してください! 体験は約10分で完了します',
}

for (const [_, msg] of Object.entries(messages)) {
{
const bson = serialize({ msg });
const back = deserialize(bson);
expect(back.msg).toBe(msg);
}

{
const bson = serialize({ msg });
const back = deserializeBSONWithoutOptimiser(bson);
expect(back.msg).toBe(msg);
}

{
const bson = getBSONSerializer<{ msg: string }>()({ msg });
const back = deserializeBSONWithoutOptimiser(bson);
expect(back.msg).toBe(msg);
}
{
const bson = getBSONSerializer<{ msg: string }>()({ msg });
const back = getBSONDeserializer<{ msg: string }>()(bson);
expect(back.msg).toBe(msg);
}
{
const bson = getBSONSerializer<[string, string]>()([_, msg]);
const back = getBSONDeserializer<[string, string]>()(bson);
expect(back).toEqual([_, msg]);
}
{
const bson = getBSONSerializer<{ [name: string]: string }>()({ [_]: msg });
const back = getBSONDeserializer<{ [name: string]: string }>()(bson);
expect(back).toEqual({ [_]: msg });
}
}
});

0 comments on commit d447c1d

Please sign in to comment.