@thestarweb/star-framework-app-web 0.1.0
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/browser/index.js +8 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/cli/dev-server.js +64 -0
- package/dist/cli/dev-server.js.map +1 -0
- package/dist/cli/index.js +117 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/types.js +2 -0
- package/dist/cli/types.js.map +1 -0
- package/dist/cli-helper.js +2 -0
- package/dist/cli-helper.js.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/interceptor/index.js +26 -0
- package/dist/interceptor/index.js.map +1 -0
- package/dist/interceptor/middleware-to-interceptor/index.js +32 -0
- package/dist/interceptor/middleware-to-interceptor/index.js.map +1 -0
- package/dist/interceptor/middleware-to-interceptor/mock-res.js +241 -0
- package/dist/interceptor/middleware-to-interceptor/mock-res.js.map +1 -0
- package/dist/interceptor/middleware-to-interceptor/mook-req.js +180 -0
- package/dist/interceptor/middleware-to-interceptor/mook-req.js.map +1 -0
- package/dist/interceptor/types.js +6 -0
- package/dist/interceptor/types.js.map +1 -0
- package/dist/params/index.js +10 -0
- package/dist/params/index.js.map +1 -0
- package/dist/request/index.js +46 -0
- package/dist/request/index.js.map +1 -0
- package/dist/return-types/index.js +2 -0
- package/dist/return-types/index.js.map +1 -0
- package/dist/return-types/response-with-meta.js +25 -0
- package/dist/return-types/response-with-meta.js.map +1 -0
- package/dist/route/index.js +20 -0
- package/dist/route/index.js.map +1 -0
- package/dist/route/types.js +17 -0
- package/dist/route/types.js.map +1 -0
- package/dist/runtime/index.js +84 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/type.js +11 -0
- package/dist/runtime/type.js.map +1 -0
- package/dist/server/body/helper.js +118 -0
- package/dist/server/body/helper.js.map +1 -0
- package/dist/server/body/json.js +211 -0
- package/dist/server/body/json.js.map +1 -0
- package/dist/server/body/types.js +6 -0
- package/dist/server/body/types.js.map +1 -0
- package/dist/server/dump-api-type.js +59 -0
- package/dist/server/dump-api-type.js.map +1 -0
- package/dist/server/index.js +103 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/types.js +8 -0
- package/dist/server/types.js.map +1 -0
- package/dist/server/utils/pass-through-readable.js +13 -0
- package/dist/server/utils/pass-through-readable.js.map +1 -0
- package/dist/view/const.js +2 -0
- package/dist/view/const.js.map +1 -0
- package/dist/view/data-context/conetext.js +70 -0
- package/dist/view/data-context/conetext.js.map +1 -0
- package/dist/view/data-context/index.js +13 -0
- package/dist/view/data-context/index.js.map +1 -0
- package/dist/view/html/create-element.js +20 -0
- package/dist/view/html/create-element.js.map +1 -0
- package/dist/view/html/index.js +4 -0
- package/dist/view/html/index.js.map +1 -0
- package/dist/view/html/parse-html-to-v-dom.js +317 -0
- package/dist/view/html/parse-html-to-v-dom.js.map +1 -0
- package/dist/view/html/render-v-dom-to-string.js +46 -0
- package/dist/view/html/render-v-dom-to-string.js.map +1 -0
- package/dist/view/html/types.js +21 -0
- package/dist/view/html/types.js.map +1 -0
- package/dist/view/index.js +7 -0
- package/dist/view/index.js.map +1 -0
- package/dist/view/layout.js +24 -0
- package/dist/view/layout.js.map +1 -0
- package/dist/view/render/html-render.js +21 -0
- package/dist/view/render/html-render.js.map +1 -0
- package/dist/view/render/index.js +3 -0
- package/dist/view/render/index.js.map +1 -0
- package/dist/view/render/render.js +183 -0
- package/dist/view/render/render.js.map +1 -0
- package/dist/view/render/sf-v-dom-render.js +40 -0
- package/dist/view/render/sf-v-dom-render.js.map +1 -0
- package/dist/view/types.js +10 -0
- package/dist/view/types.js.map +1 -0
- package/dist/view/utils/find-route-dom.js +22 -0
- package/dist/view/utils/find-route-dom.js.map +1 -0
- package/dist/view/utils/get-render/client.js +77 -0
- package/dist/view/utils/get-render/client.js.map +1 -0
- package/dist/view/utils/get-render/common.js +87 -0
- package/dist/view/utils/get-render/common.js.map +1 -0
- package/dist/view/utils/get-render/index.js +4 -0
- package/dist/view/utils/get-render/index.js.map +1 -0
- package/dist/view/utils/get-render/server.js +60 -0
- package/dist/view/utils/get-render/server.js.map +1 -0
- package/dist/view/utils/index.js +3 -0
- package/dist/view/utils/index.js.map +1 -0
- package/dist/view/view.js +10 -0
- package/dist/view/view.js.map +1 -0
- package/package.json +41 -0
- package/resource/default-index.html +11 -0
- package/src/browser/index.ts +7 -0
- package/src/cli/dev-server.ts +66 -0
- package/src/cli/index.ts +122 -0
- package/src/cli/types.ts +7 -0
- package/src/cli-helper.ts +5 -0
- package/src/index.ts +24 -0
- package/src/interceptor/index.ts +26 -0
- package/src/interceptor/middleware-to-interceptor/index.ts +29 -0
- package/src/interceptor/middleware-to-interceptor/mock-res.ts +255 -0
- package/src/interceptor/middleware-to-interceptor/mook-req.ts +218 -0
- package/src/interceptor/types.ts +7 -0
- package/src/params/index.ts +12 -0
- package/src/request/index.ts +65 -0
- package/src/return-types/index.ts +1 -0
- package/src/return-types/response-with-meta.ts +17 -0
- package/src/route/index.ts +43 -0
- package/src/route/types.ts +11 -0
- package/src/server/body/helper.ts +110 -0
- package/src/server/body/json.ts +217 -0
- package/src/server/body/types.ts +6 -0
- package/src/server/dump-api-type.ts +64 -0
- package/src/server/index.ts +113 -0
- package/src/server/types.ts +11 -0
- package/src/server/utils/pass-through-readable.ts +10 -0
- package/src/view/const.ts +1 -0
- package/src/view/data-context/conetext.ts +59 -0
- package/src/view/data-context/index.ts +18 -0
- package/src/view/html/create-element.ts +20 -0
- package/src/view/html/index.ts +3 -0
- package/src/view/html/parse-html-to-v-dom.ts +348 -0
- package/src/view/html/render-v-dom-to-string.ts +79 -0
- package/src/view/html/types.ts +32 -0
- package/src/view/index.ts +6 -0
- package/src/view/layout.ts +20 -0
- package/src/view/render/html-render.ts +21 -0
- package/src/view/render/index.ts +2 -0
- package/src/view/render/render.ts +225 -0
- package/src/view/render/sf-v-dom-render.ts +34 -0
- package/src/view/types.ts +18 -0
- package/src/view/utils/find-route-dom.ts +19 -0
- package/src/view/utils/get-render/client.ts +76 -0
- package/src/view/utils/get-render/common.ts +113 -0
- package/src/view/utils/get-render/index.ts +3 -0
- package/src/view/utils/get-render/server.ts +71 -0
- package/src/view/utils/index.ts +2 -0
- package/src/view/view.ts +7 -0
- package/test/body.test.ts +29 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { IncomingHttpHeaders } from "node:http";
|
|
2
|
+
import {
|
|
3
|
+
Duplex,
|
|
4
|
+
PipeOptions,
|
|
5
|
+
Readable,
|
|
6
|
+
ReadableIteratorOptions,
|
|
7
|
+
ReadableOperatorOptions,
|
|
8
|
+
} from "node:stream";
|
|
9
|
+
import { CallProp } from "../../server/types.js";
|
|
10
|
+
import { Abortable } from "node:events";
|
|
11
|
+
import { ByteReadableStream } from "node:stream/iter";
|
|
12
|
+
import { WritableStream, TransformStream } from "node:stream/web";
|
|
13
|
+
|
|
14
|
+
export class MookReq {
|
|
15
|
+
constructor(req: CallProp) {
|
|
16
|
+
this.headers = req.header;
|
|
17
|
+
this.headersDistinct = req.header;
|
|
18
|
+
this.rawHeaders = [];
|
|
19
|
+
this.url = req.url.pathname + req.url.search;
|
|
20
|
+
this.method = req.method;
|
|
21
|
+
}
|
|
22
|
+
destroy(error?: Error): this {
|
|
23
|
+
throw new Error("Method not implemented.");
|
|
24
|
+
}
|
|
25
|
+
addListener(eventName: unknown, listener: unknown): this {
|
|
26
|
+
throw new Error("Method not implemented.");
|
|
27
|
+
}
|
|
28
|
+
emit(eventName: unknown, ...args: unknown[]): boolean {
|
|
29
|
+
throw new Error("Method not implemented.");
|
|
30
|
+
}
|
|
31
|
+
listenerCount(eventName: unknown, listener?: unknown): number {
|
|
32
|
+
throw new Error("Method not implemented.");
|
|
33
|
+
}
|
|
34
|
+
listeners(eventName: unknown) {
|
|
35
|
+
throw new Error("Method not implemented.");
|
|
36
|
+
}
|
|
37
|
+
off(eventName: unknown, listener: unknown): this {
|
|
38
|
+
throw new Error("Method not implemented.");
|
|
39
|
+
}
|
|
40
|
+
on(eventName: unknown, listener: unknown): this {
|
|
41
|
+
throw new Error("Method not implemented.");
|
|
42
|
+
}
|
|
43
|
+
once(eventName: unknown, listener: unknown): this {
|
|
44
|
+
throw new Error("Method not implemented.");
|
|
45
|
+
}
|
|
46
|
+
prependListener(eventName: unknown, listener: unknown): this {
|
|
47
|
+
throw new Error("Method not implemented.");
|
|
48
|
+
}
|
|
49
|
+
prependOnceListener(eventName: unknown, listener: unknown): this {
|
|
50
|
+
throw new Error("Method not implemented.");
|
|
51
|
+
}
|
|
52
|
+
rawListeners(eventName: unknown) {
|
|
53
|
+
throw new Error("Method not implemented.");
|
|
54
|
+
}
|
|
55
|
+
removeAllListeners(eventName?: unknown): this {
|
|
56
|
+
throw new Error("Method not implemented.");
|
|
57
|
+
}
|
|
58
|
+
removeListener(eventName: unknown, listener: unknown): this {
|
|
59
|
+
throw new Error("Method not implemented.");
|
|
60
|
+
}
|
|
61
|
+
readableAborted = false;
|
|
62
|
+
readable = false;
|
|
63
|
+
readableDidRead = true;
|
|
64
|
+
readableEncoding = null;
|
|
65
|
+
readableEnded = true;
|
|
66
|
+
readableFlowing = false;
|
|
67
|
+
readableHighWaterMark = 16 * 1024;
|
|
68
|
+
readableLength = 0;
|
|
69
|
+
readableObjectMode = false;
|
|
70
|
+
destroyed = false;
|
|
71
|
+
closed = false;
|
|
72
|
+
errored = null;
|
|
73
|
+
_construct?(callback: (error?: Error | null) => void): void {
|
|
74
|
+
throw new Error("Method not implemented.");
|
|
75
|
+
}
|
|
76
|
+
_read(size: number): void {
|
|
77
|
+
throw new Error("Method not implemented.");
|
|
78
|
+
}
|
|
79
|
+
read(size?: number) {
|
|
80
|
+
throw new Error("Method not implemented.");
|
|
81
|
+
}
|
|
82
|
+
setEncoding(encoding: BufferEncoding): this {
|
|
83
|
+
throw new Error("Method not implemented.");
|
|
84
|
+
}
|
|
85
|
+
pause(): this {
|
|
86
|
+
throw new Error("Method not implemented.");
|
|
87
|
+
}
|
|
88
|
+
resume(): this {
|
|
89
|
+
throw new Error("Method not implemented.");
|
|
90
|
+
}
|
|
91
|
+
isPaused(): boolean {
|
|
92
|
+
throw new Error("Method not implemented.");
|
|
93
|
+
}
|
|
94
|
+
unpipe(destination?: NodeJS.WritableStream): this {
|
|
95
|
+
throw new Error("Method not implemented.");
|
|
96
|
+
}
|
|
97
|
+
unshift(chunk: any, encoding?: BufferEncoding): void {
|
|
98
|
+
throw new Error("Method not implemented.");
|
|
99
|
+
}
|
|
100
|
+
wrap(stream: NodeJS.ReadableStream): this {
|
|
101
|
+
throw new Error("Method not implemented.");
|
|
102
|
+
}
|
|
103
|
+
push(chunk: any, encoding?: BufferEncoding): boolean {
|
|
104
|
+
throw new Error("Method not implemented.");
|
|
105
|
+
}
|
|
106
|
+
compose(
|
|
107
|
+
stream:
|
|
108
|
+
| NodeJS.WritableStream
|
|
109
|
+
| WritableStream
|
|
110
|
+
| TransformStream
|
|
111
|
+
| ((source: any) => void),
|
|
112
|
+
options?: Abortable,
|
|
113
|
+
): Duplex {
|
|
114
|
+
throw new Error("Method not implemented.");
|
|
115
|
+
}
|
|
116
|
+
iterator(options?: ReadableIteratorOptions): NodeJS.AsyncIterator<any> {
|
|
117
|
+
throw new Error("Method not implemented.");
|
|
118
|
+
}
|
|
119
|
+
map(
|
|
120
|
+
fn: (data: any, options?: Abortable) => any,
|
|
121
|
+
options?: ReadableOperatorOptions,
|
|
122
|
+
): Readable {
|
|
123
|
+
throw new Error("Method not implemented.");
|
|
124
|
+
}
|
|
125
|
+
filter(
|
|
126
|
+
fn: (data: any, options?: Abortable) => boolean | Promise<boolean>,
|
|
127
|
+
options?: ReadableOperatorOptions,
|
|
128
|
+
): Readable {
|
|
129
|
+
throw new Error("Method not implemented.");
|
|
130
|
+
}
|
|
131
|
+
forEach(
|
|
132
|
+
fn: (data: any, options?: Abortable) => void | Promise<void>,
|
|
133
|
+
options?: Pick<ReadableOperatorOptions, "concurrency" | "signal">,
|
|
134
|
+
): Promise<void> {
|
|
135
|
+
throw new Error("Method not implemented.");
|
|
136
|
+
}
|
|
137
|
+
toArray(options?: Abortable): Promise<any[]> {
|
|
138
|
+
throw new Error("Method not implemented.");
|
|
139
|
+
}
|
|
140
|
+
some(
|
|
141
|
+
fn: (data: any, options?: Abortable) => boolean | Promise<boolean>,
|
|
142
|
+
options?: Pick<ReadableOperatorOptions, "concurrency" | "signal">,
|
|
143
|
+
): Promise<boolean> {
|
|
144
|
+
throw new Error("Method not implemented.");
|
|
145
|
+
}
|
|
146
|
+
find(fn: unknown, options?: unknown) {
|
|
147
|
+
throw new Error("Method not implemented.");
|
|
148
|
+
}
|
|
149
|
+
every(
|
|
150
|
+
fn: (data: any, options?: Abortable) => boolean | Promise<boolean>,
|
|
151
|
+
options?: Pick<ReadableOperatorOptions, "concurrency" | "signal">,
|
|
152
|
+
): Promise<boolean> {
|
|
153
|
+
throw new Error("Method not implemented.");
|
|
154
|
+
}
|
|
155
|
+
flatMap(
|
|
156
|
+
fn: (data: any, options?: Abortable) => any,
|
|
157
|
+
options?: Pick<ReadableOperatorOptions, "concurrency" | "signal">,
|
|
158
|
+
): Readable {
|
|
159
|
+
throw new Error("Method not implemented.");
|
|
160
|
+
}
|
|
161
|
+
drop(limit: number, options?: Abortable): Readable {
|
|
162
|
+
throw new Error("Method not implemented.");
|
|
163
|
+
}
|
|
164
|
+
take(limit: number, options?: Abortable): Readable {
|
|
165
|
+
throw new Error("Method not implemented.");
|
|
166
|
+
}
|
|
167
|
+
reduce(fn: unknown, initial?: unknown, options?: unknown): any {
|
|
168
|
+
throw new Error("Method not implemented.");
|
|
169
|
+
}
|
|
170
|
+
_destroy(
|
|
171
|
+
error: Error | null,
|
|
172
|
+
callback: (error?: Error | null) => void,
|
|
173
|
+
): void {
|
|
174
|
+
throw new Error("Method not implemented.");
|
|
175
|
+
}
|
|
176
|
+
[Symbol.asyncIterator](): NodeJS.AsyncIterator<any> {
|
|
177
|
+
throw new Error("Method not implemented.");
|
|
178
|
+
}
|
|
179
|
+
[Symbol.asyncDispose](): Promise<void> {
|
|
180
|
+
throw new Error("Method not implemented.");
|
|
181
|
+
}
|
|
182
|
+
pipe<T extends NodeJS.WritableStream>(
|
|
183
|
+
destination: T,
|
|
184
|
+
options?: PipeOptions,
|
|
185
|
+
): T {
|
|
186
|
+
throw new Error("Method not implemented.");
|
|
187
|
+
}
|
|
188
|
+
eventNames(): (string | symbol)[] {
|
|
189
|
+
throw new Error("Method not implemented.");
|
|
190
|
+
}
|
|
191
|
+
getMaxListeners(): number {
|
|
192
|
+
throw new Error("Method not implemented.");
|
|
193
|
+
}
|
|
194
|
+
setMaxListeners(n: number): this {
|
|
195
|
+
throw new Error("Method not implemented.");
|
|
196
|
+
}
|
|
197
|
+
aborted = false;
|
|
198
|
+
httpVersion = "1.1";
|
|
199
|
+
httpVersionMajor = 1;
|
|
200
|
+
httpVersionMinor = 1;
|
|
201
|
+
complete = false;
|
|
202
|
+
connection = null as any;
|
|
203
|
+
socket = null as any;
|
|
204
|
+
headers: IncomingHttpHeaders;
|
|
205
|
+
headersDistinct: NodeJS.Dict<string[]>;
|
|
206
|
+
rawHeaders: string[];
|
|
207
|
+
trailers: NodeJS.Dict<string> = {};
|
|
208
|
+
trailersDistinct = {};
|
|
209
|
+
rawTrailers = [];
|
|
210
|
+
setTimeout(msecs: number, callback?: () => void): this {
|
|
211
|
+
throw new Error("Method not implemented.");
|
|
212
|
+
}
|
|
213
|
+
signal: AbortSignal = null as any;
|
|
214
|
+
method?: string | undefined;
|
|
215
|
+
url?: string | undefined;
|
|
216
|
+
statusCode?: number | undefined;
|
|
217
|
+
statusMessage?: string | undefined;
|
|
218
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ResponseWithMeta } from "../return-types/response-with-meta.js";
|
|
2
|
+
import { CallProp } from "../server/types.js";
|
|
3
|
+
|
|
4
|
+
export type Interceptor = (
|
|
5
|
+
req: CallProp,
|
|
6
|
+
next: (req: CallProp) => ResponseWithMeta | Promise<ResponseWithMeta>,
|
|
7
|
+
) => ResponseWithMeta | Promise<ResponseWithMeta>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ParamMetaHelper,
|
|
3
|
+
RootParamType,
|
|
4
|
+
} from "@thestarweb/star-framework-param-helper";
|
|
5
|
+
export const paramMeta = new ParamMetaHelper("http", {
|
|
6
|
+
body: RootParamType.Any,
|
|
7
|
+
header: RootParamType.SingleObject,
|
|
8
|
+
query: RootParamType.Any,
|
|
9
|
+
});
|
|
10
|
+
export const Body = paramMeta.createDecorator("body");
|
|
11
|
+
export const Header = paramMeta.createDecorator("header");
|
|
12
|
+
export const Query = paramMeta.createDecorator("query");
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
interface Prop {
|
|
2
|
+
body?: any;
|
|
3
|
+
query?: any;
|
|
4
|
+
header?: Record<string, string>;
|
|
5
|
+
param?: any;
|
|
6
|
+
}
|
|
7
|
+
type APIType = Record<Method, { [k: string]: { prop: Prop; return: any } }>;
|
|
8
|
+
declare module "@thestarweb/star-framework-app-web" {
|
|
9
|
+
interface ProjectApi extends APIType {}
|
|
10
|
+
}
|
|
11
|
+
import { ProjectApi } from "@thestarweb/star-framework-app-web";
|
|
12
|
+
import { Method } from "../route/types.js";
|
|
13
|
+
import { stringify } from "qs";
|
|
14
|
+
|
|
15
|
+
export class SFWebRequest<API extends APIType = ProjectApi> {
|
|
16
|
+
constructor(public readonly baseUrl: string = "/") {}
|
|
17
|
+
private async _request(
|
|
18
|
+
method: Method,
|
|
19
|
+
path: string,
|
|
20
|
+
prop: Prop,
|
|
21
|
+
): Promise<any> {
|
|
22
|
+
let url = path.startsWith("self:")
|
|
23
|
+
? path.replace(/^self:\//, this.baseUrl)
|
|
24
|
+
: path;
|
|
25
|
+
if (prop.query) {
|
|
26
|
+
url += stringify(prop.query);
|
|
27
|
+
}
|
|
28
|
+
const res = await fetch(url, {
|
|
29
|
+
method,
|
|
30
|
+
body: JSON.stringify(prop.body),
|
|
31
|
+
headers: { "content-type": "application/json", ...prop.header },
|
|
32
|
+
});
|
|
33
|
+
if (!res.ok) {
|
|
34
|
+
throw new Error("request fail");
|
|
35
|
+
}
|
|
36
|
+
if (res.headers.get("content-type") === "application/json") {
|
|
37
|
+
return await res.json();
|
|
38
|
+
}
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
41
|
+
async get<P extends string & keyof API["GET"]>(
|
|
42
|
+
path: `self:${P}`,
|
|
43
|
+
prop: API["GET"][P]["prop"],
|
|
44
|
+
): Promise<API["GET"][P]["return"]> {
|
|
45
|
+
return await this._request("GET", path as string, prop);
|
|
46
|
+
}
|
|
47
|
+
async post<P extends keyof ProjectApi["POST"]>(
|
|
48
|
+
path: `self:${P}`,
|
|
49
|
+
prop: API["POST"][P]["prop"],
|
|
50
|
+
): Promise<API["POST"][P]["return"]> {
|
|
51
|
+
return await this._request("POST", path as string, prop);
|
|
52
|
+
}
|
|
53
|
+
async put<P extends keyof ProjectApi["PUT"]>(
|
|
54
|
+
path: `self:${P}`,
|
|
55
|
+
prop: API["PUT"][P]["prop"],
|
|
56
|
+
): Promise<API["PUT"][P]["return"]> {
|
|
57
|
+
return await this._request("PUT", path as string, prop);
|
|
58
|
+
}
|
|
59
|
+
async delete<P extends keyof ProjectApi["DELETE"]>(
|
|
60
|
+
path: `self:${P}`,
|
|
61
|
+
prop: API["DELETE"][P]["prop"],
|
|
62
|
+
): Promise<API["DELETE"][P]["return"]> {
|
|
63
|
+
return await this._request("PUT", path as string, prop);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./response-with-meta.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class ResponseWithMeta {
|
|
2
|
+
code?: number;
|
|
3
|
+
header?: Record<string, string | string[]>;
|
|
4
|
+
data: any;
|
|
5
|
+
constructor(data: any, meta?: Partial<Omit<ResponseWithMeta, "data">>) {
|
|
6
|
+
Object.assign(this, meta);
|
|
7
|
+
this.data = data;
|
|
8
|
+
this.__ResponseWithMetaFlag = ResponseWithMeta.__ResponseWithMetaFlag;
|
|
9
|
+
Object.freeze(this);
|
|
10
|
+
}
|
|
11
|
+
public readonly __ResponseWithMetaFlag: string;
|
|
12
|
+
private static readonly __ResponseWithMetaFlag = "ResponseWithMeta";
|
|
13
|
+
static isResponseWithMeta(data: any): data is ResponseWithMeta {
|
|
14
|
+
if (!data || typeof data !== "object") return false;
|
|
15
|
+
return data.__ResponseWithMetaFlag === this.__ResponseWithMetaFlag;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RouteMetadata,
|
|
3
|
+
type RouteMetadataRouteType,
|
|
4
|
+
} from "@thestarweb/star-framework-route";
|
|
5
|
+
import {
|
|
6
|
+
CommonRouteOption,
|
|
7
|
+
DeleteRouteOption,
|
|
8
|
+
GetRouteOption,
|
|
9
|
+
Method,
|
|
10
|
+
PostRouteOption,
|
|
11
|
+
PutRouteOption,
|
|
12
|
+
SubRouteOption,
|
|
13
|
+
} from "./types.js";
|
|
14
|
+
|
|
15
|
+
const routeMetadata = new RouteMetadata<
|
|
16
|
+
Method,
|
|
17
|
+
CommonRouteOption,
|
|
18
|
+
{
|
|
19
|
+
POST: PostRouteOption;
|
|
20
|
+
GET: GetRouteOption;
|
|
21
|
+
PUT: PutRouteOption;
|
|
22
|
+
DELETE: DeleteRouteOption;
|
|
23
|
+
},
|
|
24
|
+
SubRouteOption
|
|
25
|
+
>("web");
|
|
26
|
+
export type RouteType<M extends Method> = RouteMetadataRouteType<
|
|
27
|
+
typeof routeMetadata,
|
|
28
|
+
M
|
|
29
|
+
>;
|
|
30
|
+
|
|
31
|
+
export const Get = routeMetadata.route("GET");
|
|
32
|
+
export const Post = routeMetadata.route("POST");
|
|
33
|
+
export const Put = routeMetadata.route("PUT");
|
|
34
|
+
export const Delete = routeMetadata.route("DELETE");
|
|
35
|
+
export const SubRoute = routeMetadata.subRoute.bind(routeMetadata);
|
|
36
|
+
|
|
37
|
+
export function getFlatRoute(obj: object) {
|
|
38
|
+
return routeMetadata.routeFinderFlat(obj);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function parseRoute(obj: object) {
|
|
42
|
+
return routeMetadata.parse(obj);
|
|
43
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ViewInfo } from "../view/index.js";
|
|
2
|
+
|
|
3
|
+
export type Method = "GET" | "POST" | "PUT" | "DELETE";
|
|
4
|
+
export interface CommonRouteOption {}
|
|
5
|
+
export interface GetRouteOption {
|
|
6
|
+
view: ViewInfo;
|
|
7
|
+
}
|
|
8
|
+
export interface PostRouteOption {}
|
|
9
|
+
export interface PutRouteOption {}
|
|
10
|
+
export interface DeleteRouteOption {}
|
|
11
|
+
export interface SubRouteOption {}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export class ReaderHelper {
|
|
2
|
+
reader: ReadableStreamDefaultReader<Uint8Array>;
|
|
3
|
+
constructor(rawStream: ReadableStream<Uint8Array>) {
|
|
4
|
+
this.reader = rawStream.getReader();
|
|
5
|
+
}
|
|
6
|
+
buff: Uint8Array | null = null;
|
|
7
|
+
done = false;
|
|
8
|
+
totalIndex = 0;
|
|
9
|
+
async _readFromRaw(errorOnEOF = true) {
|
|
10
|
+
if (this.buff) {
|
|
11
|
+
const { buff } = this;
|
|
12
|
+
this.buff = null;
|
|
13
|
+
return buff;
|
|
14
|
+
}
|
|
15
|
+
const res = await this.reader.read();
|
|
16
|
+
if (res.done) {
|
|
17
|
+
this.done = true;
|
|
18
|
+
if (errorOnEOF) throw new Error("EOF");
|
|
19
|
+
}
|
|
20
|
+
return res.value!;
|
|
21
|
+
}
|
|
22
|
+
async streamReadUntil(read: (arr: Uint8Array) => number, errorOnEOF = true) {
|
|
23
|
+
while (true) {
|
|
24
|
+
const buff = await this._readFromRaw(errorOnEOF);
|
|
25
|
+
if (!buff) return;
|
|
26
|
+
const index = read(buff);
|
|
27
|
+
if (index === 0) {
|
|
28
|
+
this.buff = buff;
|
|
29
|
+
return;
|
|
30
|
+
} else if (index > 0 && index < buff.length) {
|
|
31
|
+
this.buff = buff.subarray(index);
|
|
32
|
+
return;
|
|
33
|
+
} else if (index === buff.length) {
|
|
34
|
+
this.buff = null;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async streamReadLength(
|
|
40
|
+
length: number,
|
|
41
|
+
callback: (block: Uint8Array) => void,
|
|
42
|
+
) {
|
|
43
|
+
return this.streamReadUntil((block) => {
|
|
44
|
+
if (length <= block.length) {
|
|
45
|
+
callback(block.subarray(0, length));
|
|
46
|
+
return length;
|
|
47
|
+
}
|
|
48
|
+
callback(block);
|
|
49
|
+
length -= block.length;
|
|
50
|
+
return -1;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async readAsABuff(length: number) {
|
|
54
|
+
const res = new Uint8Array(length);
|
|
55
|
+
let offset = 0;
|
|
56
|
+
this.streamReadLength(length, (block) => {
|
|
57
|
+
res.set(block, offset);
|
|
58
|
+
offset += block.length;
|
|
59
|
+
});
|
|
60
|
+
return res;
|
|
61
|
+
}
|
|
62
|
+
async readAsString(length: number, decoder: TextDecoder) {
|
|
63
|
+
const s: string[] = [];
|
|
64
|
+
await this.streamReadLength(length, (block) => {
|
|
65
|
+
s.push(decoder.decode(block, { stream: true }));
|
|
66
|
+
});
|
|
67
|
+
return s.join("");
|
|
68
|
+
}
|
|
69
|
+
async readChar() {
|
|
70
|
+
let res = 0;
|
|
71
|
+
await this.streamReadUntil((arr) => {
|
|
72
|
+
if (arr.length > 0) {
|
|
73
|
+
res = arr[0];
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
return -1;
|
|
77
|
+
});
|
|
78
|
+
return res;
|
|
79
|
+
}
|
|
80
|
+
async viewChar() {
|
|
81
|
+
let res = 0;
|
|
82
|
+
await this.streamReadUntil((arr) => {
|
|
83
|
+
if (arr.length > 0) {
|
|
84
|
+
res = arr[0];
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
return -1;
|
|
88
|
+
});
|
|
89
|
+
return res;
|
|
90
|
+
}
|
|
91
|
+
async skipChar(char: number[]) {
|
|
92
|
+
await this.streamReadUntil((buff) => {
|
|
93
|
+
for (let i = 0; i < buff.length; i++) {
|
|
94
|
+
if (!char.includes(buff[i])) {
|
|
95
|
+
return i;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return -1;
|
|
99
|
+
}, false);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export function mergeUint8Array(data: Uint8Array[]) {
|
|
103
|
+
const ret = new Uint8Array(data.reduce((size, a) => size + a.length, 0));
|
|
104
|
+
let offset = 0;
|
|
105
|
+
for (const arr of data) {
|
|
106
|
+
ret.set(arr, offset);
|
|
107
|
+
offset += arr.length;
|
|
108
|
+
}
|
|
109
|
+
return ret;
|
|
110
|
+
}
|