|
1 | 1 | import { env } from '../misc/env'
|
| 2 | +import { log } from '../misc/util' |
2 | 3 | import { userMessage as proto } from '../proto'
|
3 | 4 | import { Service } from '../service/Service'
|
4 | 5 |
|
@@ -65,45 +66,50 @@ export class UserMessage extends Service<proto.IMessage, proto.Message> {
|
65 | 66 | * Decode user message received from the network.
|
66 | 67 | */
|
67 | 68 | decodeUserMessage(bytes: Uint8Array, senderId: number): UserDataType | undefined {
|
68 |
| - const { length, type, contentType, full, chunk } = super.decode(bytes) as { |
69 |
| - length: number |
70 |
| - type: proto.Message.Type |
71 |
| - contentType: string |
72 |
| - full: Uint8Array | undefined |
73 |
| - chunk: proto.Message.Chunk |
74 |
| - } |
75 |
| - let result |
76 |
| - switch (contentType) { |
77 |
| - case 'full': { |
78 |
| - result = full |
79 |
| - break |
| 69 | + try { |
| 70 | + const { length, type, contentType, full, chunk } = super.decode(bytes) as { |
| 71 | + length: number |
| 72 | + type: proto.Message.Type |
| 73 | + contentType: string |
| 74 | + full: Uint8Array | undefined |
| 75 | + chunk: proto.Message.Chunk |
80 | 76 | }
|
81 |
| - case 'chunk': { |
82 |
| - let buffer = this.getBuffer(senderId, chunk.id) |
83 |
| - if (buffer === undefined) { |
84 |
| - buffer = new Buffer(length, chunk.content, chunk.nb) |
85 |
| - this.setBuffer(senderId, chunk.id, buffer) |
86 |
| - result = undefined |
87 |
| - } else { |
88 |
| - result = buffer.append(chunk.content, chunk.nb) |
| 77 | + let result |
| 78 | + switch (contentType) { |
| 79 | + case 'full': { |
| 80 | + result = full |
| 81 | + break |
| 82 | + } |
| 83 | + case 'chunk': { |
| 84 | + let buffer = this.getBuffer(senderId, chunk.id) |
| 85 | + if (buffer === undefined) { |
| 86 | + buffer = new Buffer(length, chunk.content, chunk.nb) |
| 87 | + this.setBuffer(senderId, chunk.id, buffer) |
| 88 | + result = undefined |
| 89 | + } else { |
| 90 | + result = buffer.append(chunk.content, chunk.nb) |
| 91 | + } |
| 92 | + break |
| 93 | + } |
| 94 | + default: { |
| 95 | + throw new Error('Unknown message integrity') |
89 | 96 | }
|
90 |
| - break |
91 |
| - } |
92 |
| - default: { |
93 |
| - throw new Error('Unknown message integrity') |
94 | 97 | }
|
95 |
| - } |
96 |
| - if (result !== undefined) { |
97 |
| - switch (type) { |
98 |
| - case proto.Message.Type.U_INT_8_ARRAY: |
99 |
| - return result |
100 |
| - case proto.Message.Type.STRING: |
101 |
| - return textDecoder.decode(result) |
102 |
| - default: |
103 |
| - throw new Error('Unknown message type') |
| 98 | + if (result !== undefined) { |
| 99 | + switch (type) { |
| 100 | + case proto.Message.Type.U_INT_8_ARRAY: |
| 101 | + return result |
| 102 | + case proto.Message.Type.STRING: |
| 103 | + return textDecoder.decode(result) |
| 104 | + default: |
| 105 | + throw new Error('Unknown message type') |
| 106 | + } |
104 | 107 | }
|
| 108 | + return result |
| 109 | + } catch (err) { |
| 110 | + log.warn('Decode user message error: ', err) |
| 111 | + return undefined |
105 | 112 | }
|
106 |
| - return result |
107 | 113 | }
|
108 | 114 |
|
109 | 115 | /**
|
|
0 commit comments