From b0dcdc2edbe1bfb5c7583dfd3c9832b23588474a Mon Sep 17 00:00:00 2001 From: MicroBlock <66859419+std-microblock@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:23:53 +0800 Subject: [PATCH] fix(im): use null-prototype maps for file extension lookups in tools --- packages/experimental/im/src/tools.ts | 12 ++++++------ packages/experimental/im/tests/tools.spec.ts | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/experimental/im/src/tools.ts b/packages/experimental/im/src/tools.ts index 47f7a55737..7f66ff7cb0 100644 --- a/packages/experimental/im/src/tools.ts +++ b/packages/experimental/im/src/tools.ts @@ -73,17 +73,17 @@ const MAX_STAGE_NAME_BYTES = 160 const TRUNCATED = '\n… (truncated: the result exceeded the configured read limit)' /** Image formats a file extension publishes as a picture (`im_send_file`). */ -const IMAGE_TYPES: Readonly> = { +const IMAGE_TYPES: Readonly> = Object.assign(Object.create(null) as Record, { png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp', -} +}) /** Audio formats and their media types. */ -const AUDIO_TYPES: Readonly> = { +const AUDIO_TYPES: Readonly> = Object.assign(Object.create(null) as Record, { opus: 'audio/ogg', ogg: 'audio/ogg', mp3: 'audio/mpeg', wav: 'audio/wav', silk: 'audio/silk', m4a: 'audio/mp4', -} +}) /** Video formats and their media types. */ -const VIDEO_TYPES: Readonly> = { +const VIDEO_TYPES: Readonly> = Object.assign(Object.create(null) as Record, { mp4: 'video/mp4', mov: 'video/quicktime', webm: 'video/webm', -} +}) /** A `#12` / `12` seq reference of `im_read` and `im_reply`. */ const SEQ = /^#?(\d+)$/ diff --git a/packages/experimental/im/tests/tools.spec.ts b/packages/experimental/im/tests/tools.spec.ts index 7dc6d81566..4145a01954 100644 --- a/packages/experimental/im/tests/tools.spec.ts +++ b/packages/experimental/im/tests/tools.spec.ts @@ -667,6 +667,13 @@ describe('im_send, im_reply and im_send_file', () => { const content = mounted.im.sends[0]?.content as { children: { attrs: Record }[] } expect(content.children[0]?.attrs).toMatchObject({ name: 'notes.md', size: 3 }) expect(content.children[0]?.attrs.src).toBe('data:application/octet-stream;base64,QUFB') + + // Prototype property names like `constructor` must not match IMAGE_TYPES/AUDIO_TYPES/VIDEO_TYPES. + const protoResult = await mounted.call('im_send_file', { path: 'report.constructor' }) + expect(protoResult.isError).toBe(false) + const protoContent = mounted.im.sends[1]?.content as { children: { type: string; attrs: Record }[] } + expect(protoContent.children[0]?.type).toBe('file') + expect(protoContent.children[0]?.attrs.src).toBe('data:application/octet-stream;base64,QUFB') }) it('refuses a path that is not a regular file and one the filesystem cannot read', async () => {