Skip to content

Commit 7b618a4

Browse files
initial commit
0 parents  commit 7b618a4

21 files changed

Lines changed: 2795 additions & 0 deletions

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
pull_request:
8+
schedule:
9+
- cron: '0 0 * * 0'
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
test-bun:
16+
runs-on: ubuntu-latest
17+
timeout-minutes: 10
18+
19+
strategy:
20+
fail-fast: false
21+
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
26+
- name: Install Bun
27+
uses: oven-sh/setup-bun@v2
28+
29+
- name: Install dependencies
30+
run: bun install
31+
32+
- name: Run tests
33+
run: bun test

.github/workflows/publish.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# reference: https://docs.npmjs.com/generating-provenance-statements
2+
3+
name: Publish
4+
5+
on:
6+
push:
7+
tags:
8+
- '*'
9+
10+
jobs:
11+
publish:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
id-token: write
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Install Bun
22+
uses: oven-sh/setup-bun@v2
23+
24+
- name: Install dependencies
25+
run: bun install
26+
27+
- name: Compile project
28+
run: bun run compile
29+
30+
- name: Use Node.js 20
31+
uses: actions/setup-node@v4
32+
with:
33+
node-version: 20
34+
registry-url: 'https://registry.npmjs.org'
35+
36+
- name: Publish package
37+
run: npm publish --provenance --access public
38+
env:
39+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2025-present Guillermo Rauch and Socket.IO contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# @socket.io/bun-engine
2+
3+
## How to use
4+
5+
```js
6+
import { Server as Engine } from "@socket.io/bun-engine";
7+
import { Server } from "socket.io";
8+
9+
const io = new Server();
10+
11+
const engine = new Engine({
12+
path: "/socket.io/",
13+
});
14+
15+
io.bind(engine);
16+
17+
Bun.serve({
18+
...engine.handler(),
19+
port: 3000,
20+
});
21+
22+
io.on("connection", (socket) => {
23+
// ...
24+
});
25+
```
26+
27+
## License
28+
29+
[MIT](/LICENSE)

bun.lock

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cors.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
type OriginOption = boolean | string | RegExp | (string | RegExp)[];
2+
3+
export interface CorsOptions {
4+
origin?: OriginOption;
5+
methods?: string | string[];
6+
allowedHeaders?: string | string[];
7+
exposedHeaders?: string | string[];
8+
credentials?: boolean;
9+
maxAge?: number;
10+
}
11+
12+
export function addCorsHeaders(
13+
headers: Headers,
14+
opts: CorsOptions,
15+
req: Request,
16+
) {
17+
addOrigin(opts, headers, req);
18+
addCredentials(opts, headers);
19+
addExposedHeaders(opts, headers);
20+
21+
if (req.method === "OPTIONS") {
22+
addMethods(opts, headers);
23+
addAllowedHeaders(opts, headers, req);
24+
addMaxAge(opts, headers);
25+
}
26+
}
27+
28+
function join(arg: string | string[]) {
29+
return Array.isArray(arg) ? arg.join(",") : arg;
30+
}
31+
32+
function isOriginAllowed(allowedOrigin: OriginOption, origin: string): boolean {
33+
if (Array.isArray(allowedOrigin)) {
34+
for (let i = 0; i < allowedOrigin.length; i++) {
35+
if (isOriginAllowed(allowedOrigin[i]!, origin)) {
36+
return true;
37+
}
38+
}
39+
return false;
40+
} else if (typeof allowedOrigin === "string") {
41+
return allowedOrigin === origin;
42+
} else if (allowedOrigin instanceof RegExp) {
43+
return allowedOrigin.test(origin);
44+
} else {
45+
return !!allowedOrigin;
46+
}
47+
}
48+
49+
function addOrigin(opts: CorsOptions, headers: Headers, req: Request) {
50+
const origin = req.headers.get("origin")!;
51+
const allowedOrigin = opts.origin;
52+
53+
if (!allowedOrigin || allowedOrigin === "*") {
54+
headers.set("Access-Control-Allow-Origin", "*");
55+
} else if (typeof allowedOrigin === "string") {
56+
headers.set("Access-Control-Allow-Origin", allowedOrigin);
57+
headers.append("Vary", "Origin");
58+
} else {
59+
const isAllowed = isOriginAllowed(allowedOrigin, origin);
60+
headers.set("Access-Control-Allow-Origin", isAllowed ? origin : "false");
61+
headers.append("Vary", "Origin");
62+
}
63+
}
64+
65+
function addMethods(opts: CorsOptions, headers: Headers) {
66+
if (opts.methods) {
67+
headers.set("Access-Control-Allow-Methods", join(opts.methods));
68+
}
69+
}
70+
71+
function addAllowedHeaders(opts: CorsOptions, headers: Headers, req: Request) {
72+
if (opts.allowedHeaders) {
73+
headers.set("Access-Control-Allow-Headers", join(opts.allowedHeaders));
74+
return;
75+
}
76+
const requestedHeaders = req.headers.get("access-control-request-headers");
77+
if (requestedHeaders) {
78+
headers.append("Vary", "Access-Control-Request-Headers");
79+
headers.set("Access-Control-Allow-Headers", requestedHeaders);
80+
}
81+
}
82+
83+
function addExposedHeaders(opts: CorsOptions, headers: Headers) {
84+
if (opts.exposedHeaders) {
85+
headers.set("Access-Control-Expose-Headers", join(opts.exposedHeaders));
86+
}
87+
}
88+
89+
function addCredentials(opts: CorsOptions, headers: Headers) {
90+
if (opts.credentials) {
91+
headers.set("Access-Control-Allow-Credentials", "true");
92+
}
93+
}
94+
95+
function addMaxAge(opts: CorsOptions, headers: Headers) {
96+
if (opts.maxAge) {
97+
headers.set("Access-Control-Max-Age", opts.maxAge.toString());
98+
}
99+
}

0 commit comments

Comments
 (0)