( options?: Msgpackr.Options & StreamOptions | undefined )
| 459 | * @category serialization |
| 460 | */ |
| 461 | export const makeMsgPack = ( |
| 462 | options?: Msgpackr.Options & StreamOptions | undefined |
| 463 | ): RpcSerialization["Type"] => { |
| 464 | const { maxBufferSize = defaultMaxBufferSize, ...msgpackOptions } = options ?? {} |
| 465 | return RpcSerialization.of({ |
| 466 | contentType: "application/msgpack", |
| 467 | includesFraming: true, |
| 468 | unsafeMake() { |
| 469 | const unpackr = new Msgpackr.Unpackr(msgpackOptions) |
| 470 | const packr = new Msgpackr.Packr(msgpackOptions) |
| 471 | const encoder = new TextEncoder() |
| 472 | let incomplete: Uint8Array | undefined = undefined |
| 473 | return { |
| 474 | decode(bytes) { |
| 475 | let buf = typeof bytes === "string" ? encoder.encode(bytes) : bytes |
| 476 | if (incomplete !== undefined) { |
| 477 | const prev = buf |
| 478 | bytes = new Uint8Array(incomplete.length + buf.length) |
| 479 | bytes.set(incomplete) |
| 480 | bytes.set(prev, incomplete.length) |
| 481 | buf = bytes |
| 482 | incomplete = undefined |
| 483 | } |
| 484 | try { |
| 485 | return unpackr.unpackMultiple(buf) |
| 486 | } catch (error_) { |
| 487 | const error = error_ as any |
| 488 | if (error.incomplete) { |
| 489 | const nextIncomplete = buf.subarray(error.lastPosition) |
| 490 | const bufferSize = nextIncomplete.length |
| 491 | if (isBufferSizeExceeded(bufferSize, maxBufferSize)) { |
| 492 | throw new RpcSerializationError({ |
| 493 | reason: "BufferSizeExceeded", |
| 494 | maxBufferSize, |
| 495 | bufferSize |
| 496 | }) |
| 497 | } |
| 498 | incomplete = nextIncomplete |
| 499 | return error.values ?? [] |
| 500 | } |
| 501 | throw error_ |
| 502 | } |
| 503 | }, |
| 504 | encode: (response) => packr.pack(response) |
| 505 | } |
| 506 | } |
| 507 | }) |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * @since 1.0.0 |
no test coverage detected