Skip to content

Commit

Permalink
feat: add v1 elysia support elysiajs/elysia#513
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Mar 16, 2024
1 parent 8337462 commit 74144a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 66 deletions.
82 changes: 41 additions & 41 deletions package.json
@@ -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"
]
}
48 changes: 23 additions & 25 deletions src/index.ts
@@ -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,
},
});
});
}

0 comments on commit 74144a1

Please sign in to comment.