Title: Cannot use reusable encoders/decoders in custom codecs · Issue #195 · msgpack/msgpack-javascript · GitHub
Open Graph Title: Cannot use reusable encoders/decoders in custom codecs · Issue #195 · msgpack/msgpack-javascript
X Title: Cannot use reusable encoders/decoders in custom codecs · Issue #195 · msgpack/msgpack-javascript
Description: When using reusable encoders/decoders, decoding fails when using the reused decode() function in the custom decoding function. The following example works if USE_REUSABLE_CODING is set to false, meaning the whole logic uses the global en...
Open Graph Description: When using reusable encoders/decoders, decoding fails when using the reused decode() function in the custom decoding function. The following example works if USE_REUSABLE_CODING is set to false, me...
X Description: When using reusable encoders/decoders, decoding fails when using the reused decode() function in the custom decoding function. The following example works if USE_REUSABLE_CODING is set to false, me...
Opengraph URL: https://github.com/msgpack/msgpack-javascript/issues/195
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cannot use reusable encoders/decoders in custom codecs","articleBody":"When using reusable encoders/decoders, decoding fails when using the reused `decode()` function in the custom decoding function.\r\n\r\nThe following example works if `USE_REUSABLE_CODING` is set to `false`, meaning the whole logic uses the global `encode`/`decode` function exported by msgpack, but when set to `true`, `encode` and `decode` are reusable instances of `Encode`/`Decore` correspondibly. It fails with `RangeError: Extra 18 of 21 byte(s) found at buffer[3]`.\r\n\r\nDoes reusable encoders/decoders not work with custom codecs?\r\n\r\nExample:\r\n\r\n```ts\r\nimport {\r\n\tEncoder,\r\n\tDecoder,\r\n\tExtensionCodec,\r\n\tencode as _encode,\r\n\tdecode as _decode,\r\n} from \"@msgpack/msgpack\"\r\n\r\n\r\nconst USE_REUSABLE_CODING = true;\r\n\r\nexport class MsgPackContext\r\n{\r\n\treadonly context = this;\r\n\treadonly encode: ( value: unknown ) =\u003e Uint8Array;\r\n\treadonly decode: ( buffer: BufferSource | ArrayLike\u003c number \u003e ) =\u003e unknown;\r\n\treadonly extensionCodec = new ExtensionCodec\u003c MsgPackContext \u003e( );\r\n\r\n\tconstructor( )\r\n\t{\r\n\t\tconst encoder = new Encoder( this.extensionCodec, this );\r\n\t\tconst decoder = new Decoder( this.extensionCodec, this );\r\n\r\n\t\tthis.encode = encoder.encode.bind( encoder );\r\n\t\tthis.decode = decoder.decode.bind( decoder );\r\n\r\n\t\tregisterCodecs( this );\r\n\t}\r\n}\r\n\r\nconst MSGPACK_EXT_TYPE_BIGINT = 0;\r\nconst MSGPACK_EXT_TYPE_MAP = 1;\r\n\r\nexport function registerCodecs( context: MsgPackContext )\r\n{\r\n\tconst { extensionCodec, encode, decode } = context;\r\n\r\n\textensionCodec.register( {\r\n\t\ttype: MSGPACK_EXT_TYPE_BIGINT,\r\n\t\tencode: value =\u003e\r\n\t\t\t( typeof value === 'bigint' || value instanceof BigInt )\r\n\t\t\t?\r\n\t\t\t\tUSE_REUSABLE_CODING\r\n\t\t\t\t? encode( value.toString( ) )\r\n\t\t\t\t: _encode( value.toString( ), context )\r\n\t\t\t: null,\r\n\t\tdecode: data =\u003e\r\n\t\t\tUSE_REUSABLE_CODING\r\n\t\t\t? BigInt( decode( data ) as string )\r\n\t\t\t: BigInt( _decode( data, context ) as string ),\r\n\t} );\r\n\r\n\textensionCodec.register( {\r\n\t\ttype: MSGPACK_EXT_TYPE_MAP,\r\n\t\tencode: value =\u003e\r\n\t\t\tvalue instanceof Map\r\n\t\t\t?\r\n\t\t\t\tUSE_REUSABLE_CODING\r\n\t\t\t\t? encode( [ ...value.entries( ) ] )\r\n\t\t\t\t: _encode( [ ...value.entries( ) ], context )\r\n\t\t\t: null,\r\n\t\tdecode: data =\u003e\r\n\t\t\tUSE_REUSABLE_CODING\r\n\t\t\t? new Map( decode( data ) as Array\u003cany\u003e )\r\n\t\t\t: new Map( _decode( data, context ) as Array\u003cany\u003e ),\r\n\t} );\r\n}\r\n\r\nconst context = new MsgPackContext( );\r\nif ( USE_REUSABLE_CODING )\r\n{\r\n\tconst buf = context.encode( { m: new Map( [ [ 'big', BigInt( 42 ) ] ] ) } );\r\n\tconst data = context.decode( buf );\r\n\tconsole.log( data );\r\n}\r\nelse\r\n{\r\n\tconst buf = _encode( { m: new Map( [ [ 'big', BigInt( 42 ) ] ] ) }, context );\r\n\tconst data = _decode( buf, context );\r\n\tconsole.log( data );\r\n}\r\n```\r\n\r\nJust to be clear, the following is the same code without global `encode`/`decode` usage (`USE_REUSABLE_CODING` being `false`). This code _should_ work, and could be a regression unit test ☺️:\r\n\r\n```ts\r\nimport {\r\n\tEncoder,\r\n\tDecoder,\r\n\tExtensionCodec,\r\n} from \"@msgpack/msgpack\"\r\n\r\n\r\nexport class MsgPackContext\r\n{\r\n\treadonly encode: ( value: unknown ) =\u003e Uint8Array;\r\n\treadonly decode: ( buffer: BufferSource | ArrayLike\u003c number \u003e ) =\u003e unknown;\r\n\treadonly extensionCodec = new ExtensionCodec\u003c MsgPackContext \u003e( );\r\n\r\n\tconstructor( )\r\n\t{\r\n\t\tconst encoder = new Encoder( this.extensionCodec, this );\r\n\t\tconst decoder = new Decoder( this.extensionCodec, this );\r\n\r\n\t\tthis.encode = encoder.encode.bind( encoder );\r\n\t\tthis.decode = decoder.decode.bind( decoder );\r\n\r\n\t\tregisterCodecs( this );\r\n\t}\r\n}\r\n\r\nconst MSGPACK_EXT_TYPE_BIGINT = 0;\r\nconst MSGPACK_EXT_TYPE_MAP = 1;\r\n\r\nexport function registerCodecs( context: MsgPackContext )\r\n{\r\n\tconst { extensionCodec, encode, decode } = context;\r\n\r\n\textensionCodec.register( {\r\n\t\ttype: MSGPACK_EXT_TYPE_BIGINT,\r\n\t\tencode: value =\u003e\r\n\t\t\t( typeof value === 'bigint' || value instanceof BigInt )\r\n\t\t\t? encode( value.toString( ) )\r\n\t\t\t: null,\r\n\t\tdecode: data =\u003e\r\n\t\t\tBigInt( decode( data ) as string ),\r\n\t} );\r\n\r\n\textensionCodec.register( {\r\n\t\ttype: MSGPACK_EXT_TYPE_MAP,\r\n\t\tencode: value =\u003e\r\n\t\t\tvalue instanceof Map\r\n\t\t\t? encode( [ ...value.entries( ) ] )\r\n\t\t\t: null,\r\n\t\tdecode: data =\u003e\r\n\t\t\tnew Map( decode( data ) as Array\u003cany\u003e ),\r\n\t} );\r\n}\r\n\r\nconst context = new MsgPackContext( );\r\nconst buf = context.encode( { m: new Map( [ [ 'big', BigInt( 42 ) ] ] ) } );\r\nconst data = context.decode( buf );\r\nconsole.log( data );\r\n```","author":{"url":"https://github.com/grantila","@type":"Person","name":"grantila"},"datePublished":"2021-10-23T19:09:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/195/msgpack-javascript/issues/195"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:db70ab9c-3c30-a3f6-7b93-f63d1a70c9c5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C06:279AEC:1576BC7:1EFF78A:6A54DDB0 |
| html-safe-nonce | efa1db179c70444234cde0ce9e420b458c80cadc35613a9a18c1e9568bb0cd61 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzA2OjI3OUFFQzoxNTc2QkM3OjFFRkY3OEE6NkE1NEREQjAiLCJ2aXNpdG9yX2lkIjoiMzQwNjQ4MzA4OTg5MTA1NzA3MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1309d723fc313187ebacc1dba584ebe761482be019abe3223f9f3e2406758d71 |
| hovercard-subject-tag | issue:1034234600 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/msgpack/msgpack-javascript/195/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3840e5d5f356fce1d8ec3f16bb3a59dbc4d50dedea718335cb59780f0bbe73e5/msgpack/msgpack-javascript/issues/195 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3840e5d5f356fce1d8ec3f16bb3a59dbc4d50dedea718335cb59780f0bbe73e5/msgpack/msgpack-javascript/issues/195 |
| og:image:alt | When using reusable encoders/decoders, decoding fails when using the reused decode() function in the custom decoding function. The following example works if USE_REUSABLE_CODING is set to false, me... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | grantila |
| hostname | github.com |
| expected-hostname | github.com |
| None | a556215b071af6609619e2bbe6f00bdfbf0812ad723b1ae6c301858a7a829f54 |
| turbo-cache-control | no-preview |
| go-import | github.com/msgpack/msgpack-javascript git https://github.com/msgpack/msgpack-javascript.git |
| octolytics-dimension-user_id | 198264 |
| octolytics-dimension-user_login | msgpack |
| octolytics-dimension-repository_id | 2761854 |
| octolytics-dimension-repository_nwo | msgpack/msgpack-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 2761854 |
| octolytics-dimension-repository_network_root_nwo | msgpack/msgpack-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 00f21e38abd06cd6752db5c227083570f72fdb6c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width