fix(im): use null-prototype maps for file extension lookups in tools

This commit is contained in:
MicroBlock committed 2026-11-18 00:23:53 +08:00
1 parent a008fd19ca
commit b0dcdc2edb
2 files changed
+13 -6

No files matched your search

+6 -6
View File
@@ -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<Record<string, string>> = {
const IMAGE_TYPES: Readonly<Record<string, string>> = Object.assign(Object.create(null) as Record<string, string>, {
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<Record<string, string>> = {
const AUDIO_TYPES: Readonly<Record<string, string>> = Object.assign(Object.create(null) as Record<string, string>, {
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<Record<string, string>> = {
const VIDEO_TYPES: Readonly<Record<string, string>> = Object.assign(Object.create(null) as Record<string, string>, {
mp4: 'video/mp4', mov: 'video/quicktime', webm: 'video/webm',
}
})
/** A `#12` / `12` seq reference of `im_read` and `im_reply`. */
const SEQ = /^#?(\d+)$/
@@ -667,6 +667,13 @@ describe('im_send, im_reply and im_send_file', () => {
const content = mounted.im.sends[0]?.content as { children: { attrs: Record<string, unknown> }[] }
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<string, unknown> }[] }
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 () => {