@glass-cannon/server 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/{src/request.ts → dist/request.d.ts} +4 -6
- package/dist/request.js +2 -0
- package/dist/request.js.map +1 -0
- package/{src/response.ts → dist/response.d.ts} +5 -8
- package/dist/response.js +2 -0
- package/dist/response.js.map +1 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.js +2 -0
- package/dist/server.js.map +1 -0
- package/dist/stream.d.ts +6 -0
- package/dist/stream.js +20 -0
- package/dist/stream.js.map +1 -0
- package/package.json +2 -1
- package/src/index.ts +0 -4
- package/src/server.ts +0 -21
- package/src/stream.ts +0 -31
- package/tsconfig.json +0 -7
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC"}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type { ReadableStream } from 'node:stream/web';
|
|
2
|
-
|
|
3
2
|
export type RequestBody = ReadableStream<Uint8Array>;
|
|
4
|
-
|
|
5
3
|
export interface Request {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
path: string;
|
|
5
|
+
method: string;
|
|
6
|
+
headers: Headers;
|
|
7
|
+
body: RequestBody;
|
|
10
8
|
}
|
package/dist/request.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":""}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import type { WritableStream } from 'node:stream/web';
|
|
2
|
-
|
|
3
2
|
export type ResponseBody = (stream: WritableStream<Uint8Array>) => Promise<void> | void;
|
|
4
|
-
|
|
5
3
|
export interface Response {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
status: number;
|
|
5
|
+
headers?: Headers;
|
|
6
|
+
body?: ResponseBody;
|
|
9
7
|
}
|
|
10
|
-
|
|
11
8
|
export interface ResponseHeader {
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
status: number;
|
|
10
|
+
headers: Headers;
|
|
14
11
|
}
|
package/dist/response.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":""}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Request } from './request.js';
|
|
2
|
+
import type { Response } from './response.js';
|
|
3
|
+
export interface ListenOptions {
|
|
4
|
+
host?: string;
|
|
5
|
+
port: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Server {
|
|
8
|
+
listen(options: ListenOptions): Promise<RunningServer>;
|
|
9
|
+
handle(req: Request): Promise<Response> | Response;
|
|
10
|
+
}
|
|
11
|
+
export interface RunningServer {
|
|
12
|
+
host: string;
|
|
13
|
+
port: number;
|
|
14
|
+
url: URL;
|
|
15
|
+
stop(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export type Handler = (request: Request) => Promise<Response> | Response;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":""}
|
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { RequestBody } from './request.js';
|
|
2
|
+
import type { ResponseBody } from './response.js';
|
|
3
|
+
export declare function writeText(text: string, encoding?: BufferEncoding): ResponseBody;
|
|
4
|
+
export declare function writeJson(json: unknown, encoding?: BufferEncoding): ResponseBody;
|
|
5
|
+
export declare function readText(body: RequestBody, encoding?: BufferEncoding): Promise<string>;
|
|
6
|
+
export declare function readJson(body: RequestBody, encoding?: BufferEncoding): Promise<unknown>;
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function writeText(text, encoding = 'utf-8') {
|
|
2
|
+
return async (stream) => {
|
|
3
|
+
const writer = stream.getWriter();
|
|
4
|
+
await writer.write(Buffer.from(text, encoding));
|
|
5
|
+
writer.releaseLock();
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export function writeJson(json, encoding = 'utf-8') {
|
|
9
|
+
return writeText(JSON.stringify(json), encoding);
|
|
10
|
+
}
|
|
11
|
+
export async function readText(body, encoding = 'utf-8') {
|
|
12
|
+
const buffer = await new Response(body).arrayBuffer();
|
|
13
|
+
const decoder = new TextDecoder(encoding);
|
|
14
|
+
return decoder.decode(buffer);
|
|
15
|
+
}
|
|
16
|
+
export async function readJson(body, encoding = 'utf-8') {
|
|
17
|
+
const text = await readText(body, encoding);
|
|
18
|
+
return JSON.parse(text);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,WAA2B,OAAO;IACxE,OAAO,KAAK,EAAE,MAAM,EAAE,EAAE;QACtB,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;QAClC,MAAM,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;QAChD,MAAM,CAAC,WAAW,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,IAAa,EAAE,WAA2B,OAAO;IACzE,OAAO,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,CAAC;AACnD,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAiB,EACjB,WAA2B,OAAO;IAElC,MAAM,MAAM,GAAG,MAAM,IAAI,QAAQ,CAAC,IAAiC,CAAC,CAAC,WAAW,EAAE,CAAC;IACnF,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC1C,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAiB,EACjB,WAA2B,OAAO;IAElC,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC5C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glass-cannon/server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"import": "./dist/index.js",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"types": "./dist/middleware/index.d.ts"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
+
"files": ["dist"],
|
|
15
16
|
"devDependencies": {
|
|
16
17
|
"@types/bun": "latest",
|
|
17
18
|
"tsc-alias": "^1.8.12"
|
package/src/index.ts
DELETED
package/src/server.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Request } from './request';
|
|
2
|
-
import type { Response } from './response';
|
|
3
|
-
|
|
4
|
-
export interface ListenOptions {
|
|
5
|
-
host?: string;
|
|
6
|
-
port: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface Server {
|
|
10
|
-
listen(options: ListenOptions): Promise<RunningServer>;
|
|
11
|
-
handle(req: Request): Promise<Response> | Response;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface RunningServer {
|
|
15
|
-
host: string;
|
|
16
|
-
port: number;
|
|
17
|
-
url: URL;
|
|
18
|
-
stop(): Promise<void>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type Handler = (request: Request) => Promise<Response> | Response;
|
package/src/stream.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { RequestBody } from './request';
|
|
2
|
-
import type { ResponseBody } from './response';
|
|
3
|
-
|
|
4
|
-
export function writeText(text: string, encoding: BufferEncoding = 'utf-8'): ResponseBody {
|
|
5
|
-
return async (stream) => {
|
|
6
|
-
const writer = stream.getWriter();
|
|
7
|
-
await writer.write(Buffer.from(text, encoding));
|
|
8
|
-
writer.releaseLock();
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function writeJson(json: unknown, encoding: BufferEncoding = 'utf-8'): ResponseBody {
|
|
13
|
-
return writeText(JSON.stringify(json), encoding);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export async function readText(
|
|
17
|
-
body: RequestBody,
|
|
18
|
-
encoding: BufferEncoding = 'utf-8'
|
|
19
|
-
): Promise<string> {
|
|
20
|
-
const buffer = await new Response(body as unknown as ReadableStream).arrayBuffer();
|
|
21
|
-
const decoder = new TextDecoder(encoding);
|
|
22
|
-
return decoder.decode(buffer);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export async function readJson(
|
|
26
|
-
body: RequestBody,
|
|
27
|
-
encoding: BufferEncoding = 'utf-8'
|
|
28
|
-
): Promise<unknown> {
|
|
29
|
-
const text = await readText(body, encoding);
|
|
30
|
-
return JSON.parse(text);
|
|
31
|
-
}
|