René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:db70ab9c-3c30-a3f6-7b93-f63d1a70c9c5
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8C06:279AEC:1576BC7:1EFF78A:6A54DDB0
html-safe-nonceefa1db179c70444234cde0ce9e420b458c80cadc35613a9a18c1e9568bb0cd61
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzA2OjI3OUFFQzoxNTc2QkM3OjFFRkY3OEE6NkE1NEREQjAiLCJ2aXNpdG9yX2lkIjoiMzQwNjQ4MzA4OTg5MTA1NzA3MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac1309d723fc313187ebacc1dba584ebe761482be019abe3223f9f3e2406758d71
hovercard-subject-tagissue:1034234600
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/msgpack/msgpack-javascript/195/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3840e5d5f356fce1d8ec3f16bb3a59dbc4d50dedea718335cb59780f0bbe73e5/msgpack/msgpack-javascript/issues/195
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3840e5d5f356fce1d8ec3f16bb3a59dbc4d50dedea718335cb59780f0bbe73e5/msgpack/msgpack-javascript/issues/195
og:image:altWhen 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamegrantila
hostnamegithub.com
expected-hostnamegithub.com
Nonea556215b071af6609619e2bbe6f00bdfbf0812ad723b1ae6c301858a7a829f54
turbo-cache-controlno-preview
go-importgithub.com/msgpack/msgpack-javascript git https://github.com/msgpack/msgpack-javascript.git
octolytics-dimension-user_id198264
octolytics-dimension-user_loginmsgpack
octolytics-dimension-repository_id2761854
octolytics-dimension-repository_nwomsgpack/msgpack-javascript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id2761854
octolytics-dimension-repository_network_root_nwomsgpack/msgpack-javascript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release00f21e38abd06cd6752db5c227083570f72fdb6c
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/msgpack/msgpack-javascript/issues/195#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmsgpack%2Fmsgpack-javascript%2Fissues%2F195
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmsgpack%2Fmsgpack-javascript%2Fissues%2F195
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=msgpack%2Fmsgpack-javascript
Reloadhttps://github.com/msgpack/msgpack-javascript/issues/195
Reloadhttps://github.com/msgpack/msgpack-javascript/issues/195
Reloadhttps://github.com/msgpack/msgpack-javascript/issues/195
Please reload this pagehttps://github.com/msgpack/msgpack-javascript/issues/195
msgpack https://github.com/msgpack
msgpack-javascripthttps://github.com/msgpack/msgpack-javascript
Notifications https://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-javascript
Fork 177 https://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-javascript
Star 1.6k https://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-javascript
Code https://github.com/msgpack/msgpack-javascript
Issues 17 https://github.com/msgpack/msgpack-javascript/issues
Pull requests 5 https://github.com/msgpack/msgpack-javascript/pulls
Discussions https://github.com/msgpack/msgpack-javascript/discussions
Actions https://github.com/msgpack/msgpack-javascript/actions
Security and quality 0 https://github.com/msgpack/msgpack-javascript/security
Insights https://github.com/msgpack/msgpack-javascript/pulse
Code https://github.com/msgpack/msgpack-javascript
Issues https://github.com/msgpack/msgpack-javascript/issues
Pull requests https://github.com/msgpack/msgpack-javascript/pulls
Discussions https://github.com/msgpack/msgpack-javascript/discussions
Actions https://github.com/msgpack/msgpack-javascript/actions
Security and quality https://github.com/msgpack/msgpack-javascript/security
Insights https://github.com/msgpack/msgpack-javascript/pulse
#257https://github.com/msgpack/msgpack-javascript/pull/257
Cannot use reusable encoders/decoders in custom codecshttps://github.com/msgpack/msgpack-javascript/issues/195#top
#257https://github.com/msgpack/msgpack-javascript/pull/257
https://github.com/grantila
grantilahttps://github.com/grantila
on Oct 23, 2021https://github.com/msgpack/msgpack-javascript/issues/195#issue-1034234600
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.