From d447c1d1b12f2331aec8519fec67335151b0183a Mon Sep 17 00:00:00 2001 From: "Marc J. Schmidt" Date: Mon, 17 Jun 2024 22:48:43 +0200 Subject: [PATCH] fix(bson): make sure index signature keys use full utf8 encoding --- packages/bson/src/bson-serializer.ts | 4 +-- packages/bson/tests/bson-serialize.spec.ts | 42 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/bson/src/bson-serializer.ts b/packages/bson/src/bson-serializer.ts index a7a5fd0fe..71b407c08 100644 --- a/packages/bson/src/bson-serializer.ts +++ b/packages/bson/src/bson-serializer.ts @@ -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') { @@ -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 { diff --git a/packages/bson/tests/bson-serialize.spec.ts b/packages/bson/tests/bson-serialize.spec.ts index 82f85a442..e6d4637fb 100644 --- a/packages/bson/tests/bson-serialize.spec.ts +++ b/packages/bson/tests/bson-serialize.spec.ts @@ -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 }); + } + } +});