Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add v1 elysia support elysiajs/elysia#513
- Loading branch information
1 parent
8337462
commit 74144a1
Showing
2 changed files
with
64 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
{ | ||
"name": "elysia-msgpack", | ||
"version": "0.1.0", | ||
"description": "The library for elysia which allows you to work with MessagePack. To pack/unpack it, we use really fast msgpackr", | ||
"homepage": "https://github.com/kravetsone/elysia-msgpack", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"keywords": [ | ||
"elysia", | ||
"bun", | ||
"msgpack", | ||
"messagepack", | ||
"perfomance", | ||
"serialize", | ||
"deserialize", | ||
"encode", | ||
"decode", | ||
"pack", | ||
"unpack", | ||
"json", | ||
"binary" | ||
], | ||
"scripts": { | ||
"prepublishOnly": "tsc", | ||
"dev": "bun run --watch src/index.ts", | ||
"lint": "bunx @biomejs/biome check ./src", | ||
"lint:fix": "bun lint --apply" | ||
}, | ||
"dependencies": { | ||
"msgpackr": "^1.10.1" | ||
}, | ||
"peerDependencies": { | ||
"elysia": "^0.8.0" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "1.5.3", | ||
"bun-types": "latest", | ||
"elysia": "^0.8.16" | ||
}, | ||
"files": [ | ||
"dist" | ||
] | ||
"name": "elysia-msgpack", | ||
"version": "0.2.0", | ||
"description": "The library for elysia which allows you to work with MessagePack. To pack/unpack it, we use really fast msgpackr", | ||
"homepage": "https://github.com/kravetsone/elysia-msgpack", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"keywords": [ | ||
"elysia", | ||
"bun", | ||
"msgpack", | ||
"messagepack", | ||
"perfomance", | ||
"serialize", | ||
"deserialize", | ||
"encode", | ||
"decode", | ||
"pack", | ||
"unpack", | ||
"json", | ||
"binary" | ||
], | ||
"scripts": { | ||
"prepublishOnly": "tsc", | ||
"dev": "bun run --watch src/index.ts", | ||
"lint": "bunx @biomejs/biome check ./src", | ||
"lint:fix": "bun lint --apply" | ||
}, | ||
"dependencies": { | ||
"msgpackr": "^1.10.1" | ||
}, | ||
"peerDependencies": { | ||
"elysia": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "1.6.1", | ||
"bun-types": "latest", | ||
"elysia": "^1.0.0" | ||
}, | ||
"files": [ | ||
"dist" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
import { Elysia } from "elysia" | ||
import { Options, Packr } from "msgpackr" | ||
import { Elysia } from "elysia"; | ||
import { type Options, Packr } from "msgpackr"; | ||
|
||
const DEFAULT_MIME_TYPE = "application/x-msgpack" | ||
const DEFAULT_MIME_TYPE = "application/x-msgpack"; | ||
|
||
// TODO: worry about onError serialize to msgpack | ||
export function msgpack(options: Options & { mimeType?: string } = {}) { | ||
const packr = new Packr(options) | ||
const packr = new Packr(options); | ||
|
||
return new Elysia({ | ||
name: "elysia-msgpack", | ||
seed: options, | ||
}) | ||
.onParse(async ({ request }, contentType) => { | ||
if (contentType === (options.mimeType ?? DEFAULT_MIME_TYPE)) | ||
return packr.unpack(new Uint8Array(await request.arrayBuffer())) | ||
}) | ||
.mapResponse(({ headers, response }) => { | ||
if ( | ||
headers.accept?.includes( | ||
options.mimeType ?? DEFAULT_MIME_TYPE, | ||
) && | ||
response | ||
) | ||
return new Response(packr.pack(response), { | ||
headers: { | ||
"content-type": options.mimeType ?? DEFAULT_MIME_TYPE, | ||
}, | ||
}) | ||
}) | ||
return new Elysia({ | ||
name: "elysia-msgpack", | ||
seed: options, | ||
}) | ||
.onParse({ as: "global" }, async ({ request }, contentType) => { | ||
if (contentType === (options.mimeType ?? DEFAULT_MIME_TYPE)) | ||
return packr.unpack(new Uint8Array(await request.arrayBuffer())); | ||
}) | ||
.mapResponse({ as: "global" }, ({ headers, response }) => { | ||
if ( | ||
headers.accept?.includes(options.mimeType ?? DEFAULT_MIME_TYPE) && | ||
response | ||
) | ||
return new Response(packr.pack(response), { | ||
headers: { | ||
"content-type": options.mimeType ?? DEFAULT_MIME_TYPE, | ||
}, | ||
}); | ||
}); | ||
} |