-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ditsmod): upgrade npm packages and added bun integration. (#9213)
- Loading branch information
1 parent
61ebc14
commit a3ff927
Showing
17 changed files
with
349 additions
and
16 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
Binary file not shown.
17 changes: 17 additions & 0 deletions
17
frameworks/TypeScript/ditsmod/ditsmod-bun-mysql.dockerfile
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM oven/bun:1.1 | ||
|
||
COPY ./ ./ | ||
|
||
RUN bun install | ||
RUN bun run build | ||
|
||
ENV NODE_ENV production | ||
ENV IS_BUN true | ||
ENV DATABASE mysql | ||
ENV MYSQL_HOST tfb-database | ||
ENV MYSQL_USER benchmarkdbuser | ||
ENV MYSQL_PSWD benchmarkdbpass | ||
ENV MYSQL_DBNAME hello_world | ||
|
||
EXPOSE 8080 | ||
CMD rm node_modules/@ditsmod/*/tsconfig.json && bun src/app/bun-integration/spawn.ts |
17 changes: 17 additions & 0 deletions
17
frameworks/TypeScript/ditsmod/ditsmod-bun-postgres.dockerfile
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM oven/bun:1.1 | ||
|
||
COPY ./ ./ | ||
|
||
RUN bun install | ||
RUN bun run build | ||
|
||
ENV NODE_ENV production | ||
ENV IS_BUN true | ||
ENV DATABASE postgres | ||
ENV PG_HOST tfb-database | ||
ENV PG_USER benchmarkdbuser | ||
ENV PG_PSWD benchmarkdbpass | ||
ENV PG_DBNAME hello_world | ||
|
||
EXPOSE 8080 | ||
CMD rm node_modules/@ditsmod/*/tsconfig.json && bun src/app/bun-integration/spawn.ts |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM oven/bun:1.1 | ||
|
||
COPY ./ ./ | ||
|
||
RUN bun install | ||
RUN bun run build | ||
|
||
ENV NODE_ENV production | ||
ENV IS_BUN true | ||
|
||
EXPOSE 8080 | ||
CMD rm node_modules/@ditsmod/*/tsconfig.json && bun src/app/bun-integration/spawn.ts |
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,4 +1,4 @@ | ||
FROM node:18.17.1-slim | ||
FROM node:20.16-slim | ||
|
||
COPY ./ ./ | ||
|
||
|
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,4 +1,4 @@ | ||
FROM node:18.17.1-slim | ||
FROM node:20.16-slim | ||
|
||
COPY ./ ./ | ||
|
||
|
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,4 +1,4 @@ | ||
FROM node:18.17.1-slim | ||
FROM node:20.16-slim | ||
|
||
COPY ./ ./ | ||
|
||
|
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
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,8 +1,13 @@ | ||
import { Providers, rootModule } from '@ditsmod/core'; | ||
import { PreRouter, rootModule } from '@ditsmod/core'; | ||
|
||
import { SimpleModule } from '#routed/simple/simple.module.js'; | ||
import { BunPreRouter } from './bun-integration/pre-router.js'; | ||
import { BunProviders } from './bun-integration/bun-providers.js'; | ||
|
||
@rootModule({ | ||
appends: [SimpleModule], | ||
providersPerApp: [...new Providers().useLogConfig({ level: 'off' })], | ||
providersPerApp: [ | ||
...new BunProviders().useLogConfig({ level: 'off' }).if(process.env.IS_BUN).useClass(PreRouter, BunPreRouter), | ||
], | ||
}) | ||
export class AppModule {} |
17 changes: 17 additions & 0 deletions
17
frameworks/TypeScript/ditsmod/src/app/bun-integration/bun-application.ts
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AnyFn, AppInitializer, Application } from '@ditsmod/core'; | ||
import { Serve, Server } from 'bun'; | ||
|
||
export class BunApplication extends Application { | ||
protected override createServer(requestListener: any): any { | ||
const serveOptions = this.appOptions.serverOptions as Serve; | ||
serveOptions.fetch ??= (req) => requestListener(req); | ||
return Bun.serve(serveOptions); | ||
} | ||
|
||
protected override async createServerAndBindToListening(appInitializer: AppInitializer, resolve: AnyFn) { | ||
this.flushLogs(); | ||
const server = (await this.createServer(appInitializer.requestListener)) as Server; | ||
this.systemLogMediator.serverListen(this, server.hostname, server.port); | ||
resolve({ server }); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
frameworks/TypeScript/ditsmod/src/app/bun-integration/bun-providers.ts
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Providers, Class } from '@ditsmod/core'; | ||
|
||
export class BunProviders extends Providers { | ||
protected setCondition?: boolean; | ||
protected ifCondition?: boolean; | ||
|
||
if(condition: any) { | ||
this.setCondition = true; | ||
this.ifCondition = condition; | ||
return this; | ||
} | ||
|
||
override useClass<A extends Class, B extends A>(token: A, useClass: B, multi?: boolean): this { | ||
if (!this.setCondition || this.ifCondition) { | ||
this.pushProvider({ token, useClass }, multi); | ||
} | ||
this.setCondition = undefined; | ||
this.ifCondition = undefined; | ||
return this; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
frameworks/TypeScript/ditsmod/src/app/bun-integration/node-res.ts
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Writable } from 'node:stream'; | ||
|
||
export class NodeRes extends Writable { | ||
#chunks: Buffer[] = []; | ||
#resolve: (body: string) => void; | ||
status: number = 200; | ||
headers = {} as HeadersInit; | ||
body = new Promise<any>((resolve) => (this.#resolve = resolve)); | ||
headersSent?: boolean; | ||
statusText?: string; | ||
|
||
set statusCode(statusCode: number) { | ||
this.status = statusCode; | ||
} | ||
|
||
getHeader(name: string) { | ||
return this.headers[name as keyof HeadersInit]; | ||
} | ||
|
||
getHeaders() { | ||
return this.headers; | ||
} | ||
|
||
setHeader(name: string, value: number | string | readonly string[]) { | ||
this.headers = { ...this.headers, [name]: value } as HeadersInit; | ||
return this; | ||
} | ||
|
||
writeHead(statusCode: number, headers?: HeadersInit): this; | ||
writeHead(statusCode: number, statusMessage: string, headers?: HeadersInit): this; | ||
writeHead(statusCode: number, statusMsgOrHeaders?: string | HeadersInit, headers?: HeadersInit): this { | ||
this.status = statusCode; | ||
if (typeof statusMsgOrHeaders == 'object') { | ||
this.mergeHeaders(statusMsgOrHeaders); | ||
} else { | ||
this.statusText = statusMsgOrHeaders; | ||
this.mergeHeaders(headers); | ||
} | ||
return this; | ||
} | ||
|
||
override _write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void { | ||
this.#chunks.push(Buffer.from(chunk)); | ||
callback(); | ||
} | ||
|
||
override _final(callback: (error?: Error | null) => void): void { | ||
const finalData = Buffer.concat(this.#chunks); | ||
this.headersSent = true; | ||
this.#resolve(finalData.toString()); | ||
callback(); | ||
} | ||
|
||
protected mergeHeaders(headers: HeadersInit = {}) { | ||
if (Array.isArray(headers)) { | ||
headers.forEach(([key, val]) => ((this.headers as any)[key] = val)); | ||
} else { | ||
this.headers = { ...this.headers, ...headers }; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
frameworks/TypeScript/ditsmod/src/app/bun-integration/pre-router.ts
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { PreRouter } from '@ditsmod/core'; | ||
import { NodeRes } from './node-res.js'; | ||
|
||
export class BunPreRouter extends PreRouter { | ||
override requestListener: any = async (req: Request) => { | ||
const nodeReq = req as any; | ||
const nodeRes = new NodeRes(); | ||
|
||
const url = new URL(req.url); | ||
const uri = url.pathname; | ||
const queryString = url.search.slice(1); | ||
const { handle, params } = this.router.find(req.method as any, uri); | ||
if (!handle) { | ||
this.sendNotImplemented(nodeRes as any); | ||
const body = await nodeRes.body; | ||
return new Response(body, nodeRes); | ||
} | ||
|
||
await handle(nodeReq, nodeRes as any, params!, queryString).catch((err) => { | ||
this.sendInternalServerError(nodeRes as any, err); | ||
}); | ||
|
||
const body = await nodeRes.body; | ||
return new Response(body, nodeRes); | ||
}; | ||
} |
9 changes: 9 additions & 0 deletions
9
frameworks/TypeScript/ditsmod/src/app/bun-integration/spawn.ts
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os from 'node:os'; | ||
|
||
const numCPUs = os.cpus().length; | ||
for (let i = 0; i < numCPUs; i++) { | ||
Bun.spawn(['bun', 'dist/main.bun.js'], { | ||
stdio: ['inherit', 'inherit', 'inherit'], | ||
env: { ...process.env }, | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Serve, Server } from 'bun'; | ||
|
||
import { AppModule } from './app/app.module.js'; | ||
import { BunApplication } from './app/bun-integration/bun-application.js'; | ||
|
||
const reusePort = process.env.NODE_ENV == 'production'; | ||
const serverOptions = { port: 8080, hostname: '0.0.0.0', reusePort } as Serve; | ||
const { server } = await new BunApplication().bootstrap(AppModule, { | ||
serverOptions: serverOptions as any, | ||
}); | ||
|
||
const bunServer = server as unknown as Server; |
Oops, something went wrong.