encore.dev 1.44.9 → 1.45.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/api/mod.ts +189 -9
- package/api/stream.ts +68 -0
- package/dist/api/mod.d.ts +95 -0
- package/dist/api/mod.js +106 -0
- package/dist/api/mod.js.map +1 -1
- package/dist/api/stream.d.ts +21 -0
- package/dist/api/stream.js +57 -0
- package/dist/api/stream.js.map +1 -0
- package/dist/internal/appinit/mod.d.ts +17 -1
- package/dist/internal/appinit/mod.js +95 -55
- package/dist/internal/appinit/mod.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +1 -1
- package/dist/internal/runtime/napi/napi.d.cts +1 -0
- package/dist/req_meta.d.ts +2 -0
- package/dist/req_meta.js +4 -3
- package/dist/req_meta.js.map +1 -1
- package/dist/service/mod.d.ts +2 -0
- package/dist/service/mod.js.map +1 -1
- package/dist/storage/sqldb/database.d.ts +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/internal/appinit/mod.ts +162 -59
- package/internal/runtime/napi/napi.cjs +1 -1
- package/internal/runtime/napi/napi.d.cts +1 -0
- package/package.json +1 -1
- package/req_meta.ts +7 -3
- package/service/mod.ts +5 -1
- package/storage/sqldb/database.ts +1 -1
package/api/mod.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
/* eslint-disable */
|
|
2
2
|
|
|
3
3
|
import type { IncomingMessage, ServerResponse } from "http";
|
|
4
|
+
import { RequestMeta, currentRequest } from "../mod";
|
|
5
|
+
import { RawResponse } from "./mod";
|
|
6
|
+
import { RawRequest } from "./mod";
|
|
7
|
+
import { InternalHandlerResponse } from "../internal/appinit/mod";
|
|
8
|
+
import { IterableSocket, IterableStream, Sink } from "./stream";
|
|
4
9
|
export { RawRequest, RawResponse } from "../internal/api/node_http";
|
|
5
10
|
|
|
6
11
|
export type Method =
|
|
@@ -141,21 +146,21 @@ export interface StreamOut<Response> {
|
|
|
141
146
|
|
|
142
147
|
export type StreamInOutHandlerFn<HandshakeData, Request, Response> =
|
|
143
148
|
HandshakeData extends void
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
+
? (stream: StreamInOut<Request, Response>) => Promise<void>
|
|
150
|
+
: (
|
|
151
|
+
data: HandshakeData,
|
|
152
|
+
stream: StreamInOut<Request, Response>
|
|
153
|
+
) => Promise<void>;
|
|
149
154
|
|
|
150
155
|
export type StreamOutHandlerFn<HandshakeData, Response> =
|
|
151
156
|
HandshakeData extends void
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
? (stream: StreamOut<Response>) => Promise<void>
|
|
158
|
+
: (data: HandshakeData, stream: StreamOut<Response>) => Promise<void>;
|
|
154
159
|
|
|
155
160
|
export type StreamInHandlerFn<HandshakeData, Request, Response> =
|
|
156
161
|
HandshakeData extends void
|
|
157
|
-
|
|
158
|
-
|
|
162
|
+
? (stream: StreamIn<Request>) => Promise<Response>
|
|
163
|
+
: (data: HandshakeData, stream: StreamIn<Request>) => Promise<Response>;
|
|
159
164
|
|
|
160
165
|
export type StreamInOut<Request, Response> = StreamIn<Request> &
|
|
161
166
|
StreamOut<Response>;
|
|
@@ -261,5 +266,180 @@ api.static = function staticAssets(options: StaticOptions) {
|
|
|
261
266
|
return new StaticAssets(options);
|
|
262
267
|
};
|
|
263
268
|
|
|
269
|
+
export interface MiddlewareOptions {
|
|
270
|
+
/**
|
|
271
|
+
* Configuration for what endpoints that should be targeted by the middleware
|
|
272
|
+
*/
|
|
273
|
+
target?: {
|
|
274
|
+
/**
|
|
275
|
+
* If set, only run middleware on endpoints that are either exposed or not
|
|
276
|
+
* exposed.
|
|
277
|
+
*/
|
|
278
|
+
expose?: boolean;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* If set, only run middleware on endpoints that either require or not
|
|
282
|
+
* requires auth.
|
|
283
|
+
*/
|
|
284
|
+
auth?: boolean;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* If set, only run middleware on endpoints that are raw endpoints.
|
|
288
|
+
*/
|
|
289
|
+
isRaw?: boolean;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* If set, only run middleware on endpoints that are stream endpoints.
|
|
293
|
+
*/
|
|
294
|
+
isStream?: boolean;
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export class MiddlewareRequest {
|
|
299
|
+
private _reqMeta?: RequestMeta;
|
|
300
|
+
private _stream?: IterableStream | IterableSocket | Sink;
|
|
301
|
+
private _rawReq?: RawRequest;
|
|
302
|
+
private _rawResp?: RawResponse;
|
|
303
|
+
|
|
304
|
+
constructor(
|
|
305
|
+
stream?: IterableStream | IterableSocket | Sink,
|
|
306
|
+
rawReq?: RawRequest,
|
|
307
|
+
rawResp?: RawResponse
|
|
308
|
+
) {
|
|
309
|
+
this._stream = stream;
|
|
310
|
+
this._rawReq = rawReq;
|
|
311
|
+
this._rawResp = rawResp;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* requestMeta is set when the handler is a typed handler or a stream handler.
|
|
316
|
+
* for raw handlers, see rawRequest and rawResponse.
|
|
317
|
+
*/
|
|
318
|
+
public get requestMeta(): RequestMeta | undefined {
|
|
319
|
+
return this._reqMeta || (this._reqMeta = currentRequest());
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* stream is set when the handler is a stream handler.
|
|
324
|
+
*/
|
|
325
|
+
public get stream(): IterableStream | IterableSocket | Sink | undefined {
|
|
326
|
+
return this._stream;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* rawRequest is set when the handler is a raw request handler.
|
|
331
|
+
*/
|
|
332
|
+
public get rawRequest(): RawRequest | undefined {
|
|
333
|
+
return this._rawReq;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* rawResponse is set when the handler is a raw request handler.
|
|
338
|
+
*/
|
|
339
|
+
public get rawResponse(): RawResponse | undefined {
|
|
340
|
+
return this._rawResp;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export class ResponseHeader {
|
|
345
|
+
headers: Record<string, string | string[]>;
|
|
346
|
+
|
|
347
|
+
constructor() {
|
|
348
|
+
this.headers = {};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* set will set a header value for a key, if a previous middleware has
|
|
353
|
+
* already set a value, it will be overridden.
|
|
354
|
+
*/
|
|
355
|
+
public set(key: string, value: string | string[]) {
|
|
356
|
+
this.headers[key] = value;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* add adds a header value to a key, if a previous middleware has
|
|
361
|
+
* already set a value, they will be appended.
|
|
362
|
+
*/
|
|
363
|
+
public add(key: string, value: string | string[]) {
|
|
364
|
+
const prev = this.headers[key];
|
|
365
|
+
|
|
366
|
+
if (prev === undefined) {
|
|
367
|
+
this.headers[key] = value;
|
|
368
|
+
} else {
|
|
369
|
+
this.headers[key] = [prev, value].flat();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export class HandlerResponse {
|
|
375
|
+
/**
|
|
376
|
+
* The payload returned by the handler when the handler is either
|
|
377
|
+
* a typed handler or stream handler.
|
|
378
|
+
*/
|
|
379
|
+
payload: any;
|
|
380
|
+
|
|
381
|
+
private _headers?: ResponseHeader;
|
|
382
|
+
|
|
383
|
+
constructor(payload: any) {
|
|
384
|
+
this.payload = payload;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* header can be used by middlewares to set headers to the
|
|
389
|
+
* response. This only works for typed handler. For raw handlers
|
|
390
|
+
* see MiddlewareRequest.rawResponse.
|
|
391
|
+
*/
|
|
392
|
+
public get header(): ResponseHeader {
|
|
393
|
+
if (this._headers === undefined) {
|
|
394
|
+
this._headers = new ResponseHeader();
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return this._headers;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* __internalToResponse converts a response to the internal representation
|
|
402
|
+
*/
|
|
403
|
+
__internalToResponse(): InternalHandlerResponse {
|
|
404
|
+
return {
|
|
405
|
+
payload: this.payload,
|
|
406
|
+
extraHeaders: this._headers?.headers
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export type Next = (req: MiddlewareRequest) => Promise<HandlerResponse>;
|
|
412
|
+
|
|
413
|
+
export type MiddlewareFn = (
|
|
414
|
+
req: MiddlewareRequest,
|
|
415
|
+
next: Next
|
|
416
|
+
) => Promise<HandlerResponse>;
|
|
417
|
+
|
|
418
|
+
export interface Middleware extends MiddlewareFn {
|
|
419
|
+
options?: MiddlewareOptions;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export function middleware(m: MiddlewareFn): Middleware;
|
|
423
|
+
export function middleware(
|
|
424
|
+
options: MiddlewareOptions,
|
|
425
|
+
fn: MiddlewareFn
|
|
426
|
+
): Middleware;
|
|
427
|
+
|
|
428
|
+
export function middleware(
|
|
429
|
+
a: MiddlewareFn | MiddlewareOptions,
|
|
430
|
+
b?: MiddlewareFn
|
|
431
|
+
): Middleware {
|
|
432
|
+
if (b === undefined) {
|
|
433
|
+
return a as Middleware;
|
|
434
|
+
} else {
|
|
435
|
+
const opts = a as MiddlewareOptions;
|
|
436
|
+
const mw = b as Middleware;
|
|
437
|
+
mw.options = opts;
|
|
438
|
+
|
|
439
|
+
return mw;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
264
443
|
export { APIError, ErrCode } from "./error";
|
|
265
444
|
export { Gateway, type GatewayConfig } from "./gateway";
|
|
445
|
+
export { IterableSocket, IterableStream, Sink } from "./stream";
|
package/api/stream.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as runtime from "../internal/runtime/mod";
|
|
2
|
+
|
|
3
|
+
export class IterableStream {
|
|
4
|
+
private stream: runtime.Stream;
|
|
5
|
+
|
|
6
|
+
constructor(stream: runtime.Stream) {
|
|
7
|
+
this.stream = stream;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
recv(): Promise<Record<string, any>> {
|
|
11
|
+
return this.stream.recv();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async *[Symbol.asyncIterator]() {
|
|
15
|
+
while (true) {
|
|
16
|
+
try {
|
|
17
|
+
yield await this.stream.recv();
|
|
18
|
+
} catch (e) {
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class IterableSocket {
|
|
26
|
+
private socket: runtime.Socket;
|
|
27
|
+
|
|
28
|
+
constructor(socket: runtime.Socket) {
|
|
29
|
+
this.socket = socket;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
send(msg: Record<string, any>): void {
|
|
33
|
+
return this.socket.send(msg);
|
|
34
|
+
}
|
|
35
|
+
recv(): Promise<Record<string, any>> {
|
|
36
|
+
return this.socket.recv();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
close(): void {
|
|
40
|
+
this.socket.close();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async *[Symbol.asyncIterator]() {
|
|
44
|
+
while (true) {
|
|
45
|
+
try {
|
|
46
|
+
yield await this.socket.recv();
|
|
47
|
+
} catch (e) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class Sink {
|
|
55
|
+
private sink: runtime.Sink;
|
|
56
|
+
|
|
57
|
+
constructor(sink: runtime.Sink) {
|
|
58
|
+
this.sink = sink;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
send(msg: Record<string, any>): void {
|
|
62
|
+
return this.sink.send(msg);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
close(): void {
|
|
66
|
+
this.sink.close();
|
|
67
|
+
}
|
|
68
|
+
}
|
package/dist/api/mod.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { IncomingMessage, ServerResponse } from "http";
|
|
3
|
+
import { RequestMeta } from "../mod.js";
|
|
4
|
+
import { RawResponse } from "./mod.js";
|
|
5
|
+
import { RawRequest } from "./mod.js";
|
|
6
|
+
import { InternalHandlerResponse } from "../internal/appinit/mod.js";
|
|
7
|
+
import { IterableSocket, IterableStream, Sink } from "./stream.js";
|
|
3
8
|
export { RawRequest, RawResponse } from "../internal/api/node_http.js";
|
|
4
9
|
export type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "TRACE" | "CONNECT";
|
|
5
10
|
export type Header<TypeOrName extends string | number | boolean | Date = string, Name extends string = ""> = TypeOrName extends string ? string : TypeOrName;
|
|
@@ -146,5 +151,95 @@ export declare class StaticAssets {
|
|
|
146
151
|
readonly options: StaticOptions;
|
|
147
152
|
constructor(options: StaticOptions);
|
|
148
153
|
}
|
|
154
|
+
export interface MiddlewareOptions {
|
|
155
|
+
/**
|
|
156
|
+
* Configuration for what endpoints that should be targeted by the middleware
|
|
157
|
+
*/
|
|
158
|
+
target?: {
|
|
159
|
+
/**
|
|
160
|
+
* If set, only run middleware on endpoints that are either exposed or not
|
|
161
|
+
* exposed.
|
|
162
|
+
*/
|
|
163
|
+
expose?: boolean;
|
|
164
|
+
/**
|
|
165
|
+
* If set, only run middleware on endpoints that either require or not
|
|
166
|
+
* requires auth.
|
|
167
|
+
*/
|
|
168
|
+
auth?: boolean;
|
|
169
|
+
/**
|
|
170
|
+
* If set, only run middleware on endpoints that are raw endpoints.
|
|
171
|
+
*/
|
|
172
|
+
isRaw?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* If set, only run middleware on endpoints that are stream endpoints.
|
|
175
|
+
*/
|
|
176
|
+
isStream?: boolean;
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
export declare class MiddlewareRequest {
|
|
180
|
+
private _reqMeta?;
|
|
181
|
+
private _stream?;
|
|
182
|
+
private _rawReq?;
|
|
183
|
+
private _rawResp?;
|
|
184
|
+
constructor(stream?: IterableStream | IterableSocket | Sink, rawReq?: RawRequest, rawResp?: RawResponse);
|
|
185
|
+
/**
|
|
186
|
+
* requestMeta is set when the handler is a typed handler or a stream handler.
|
|
187
|
+
* for raw handlers, see rawRequest and rawResponse.
|
|
188
|
+
*/
|
|
189
|
+
get requestMeta(): RequestMeta | undefined;
|
|
190
|
+
/**
|
|
191
|
+
* stream is set when the handler is a stream handler.
|
|
192
|
+
*/
|
|
193
|
+
get stream(): IterableStream | IterableSocket | Sink | undefined;
|
|
194
|
+
/**
|
|
195
|
+
* rawRequest is set when the handler is a raw request handler.
|
|
196
|
+
*/
|
|
197
|
+
get rawRequest(): RawRequest | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* rawResponse is set when the handler is a raw request handler.
|
|
200
|
+
*/
|
|
201
|
+
get rawResponse(): RawResponse | undefined;
|
|
202
|
+
}
|
|
203
|
+
export declare class ResponseHeader {
|
|
204
|
+
headers: Record<string, string | string[]>;
|
|
205
|
+
constructor();
|
|
206
|
+
/**
|
|
207
|
+
* set will set a header value for a key, if a previous middleware has
|
|
208
|
+
* already set a value, it will be overridden.
|
|
209
|
+
*/
|
|
210
|
+
set(key: string, value: string | string[]): void;
|
|
211
|
+
/**
|
|
212
|
+
* add adds a header value to a key, if a previous middleware has
|
|
213
|
+
* already set a value, they will be appended.
|
|
214
|
+
*/
|
|
215
|
+
add(key: string, value: string | string[]): void;
|
|
216
|
+
}
|
|
217
|
+
export declare class HandlerResponse {
|
|
218
|
+
/**
|
|
219
|
+
* The payload returned by the handler when the handler is either
|
|
220
|
+
* a typed handler or stream handler.
|
|
221
|
+
*/
|
|
222
|
+
payload: any;
|
|
223
|
+
private _headers?;
|
|
224
|
+
constructor(payload: any);
|
|
225
|
+
/**
|
|
226
|
+
* header can be used by middlewares to set headers to the
|
|
227
|
+
* response. This only works for typed handler. For raw handlers
|
|
228
|
+
* see MiddlewareRequest.rawResponse.
|
|
229
|
+
*/
|
|
230
|
+
get header(): ResponseHeader;
|
|
231
|
+
/**
|
|
232
|
+
* __internalToResponse converts a response to the internal representation
|
|
233
|
+
*/
|
|
234
|
+
__internalToResponse(): InternalHandlerResponse;
|
|
235
|
+
}
|
|
236
|
+
export type Next = (req: MiddlewareRequest) => Promise<HandlerResponse>;
|
|
237
|
+
export type MiddlewareFn = (req: MiddlewareRequest, next: Next) => Promise<HandlerResponse>;
|
|
238
|
+
export interface Middleware extends MiddlewareFn {
|
|
239
|
+
options?: MiddlewareOptions;
|
|
240
|
+
}
|
|
241
|
+
export declare function middleware(m: MiddlewareFn): Middleware;
|
|
242
|
+
export declare function middleware(options: MiddlewareOptions, fn: MiddlewareFn): Middleware;
|
|
149
243
|
export { APIError, ErrCode } from "./error.js";
|
|
150
244
|
export { Gateway, type GatewayConfig } from "./gateway.js";
|
|
245
|
+
export { IterableSocket, IterableStream, Sink } from "./stream.js";
|
package/dist/api/mod.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* eslint-disable */
|
|
2
|
+
import { currentRequest } from "../mod.js";
|
|
2
3
|
export { RawRequest, RawResponse } from "../internal/api/node_http.js";
|
|
3
4
|
export function api(options, fn) {
|
|
4
5
|
return fn;
|
|
@@ -27,6 +28,111 @@ export class StaticAssets {
|
|
|
27
28
|
api.static = function staticAssets(options) {
|
|
28
29
|
return new StaticAssets(options);
|
|
29
30
|
};
|
|
31
|
+
export class MiddlewareRequest {
|
|
32
|
+
_reqMeta;
|
|
33
|
+
_stream;
|
|
34
|
+
_rawReq;
|
|
35
|
+
_rawResp;
|
|
36
|
+
constructor(stream, rawReq, rawResp) {
|
|
37
|
+
this._stream = stream;
|
|
38
|
+
this._rawReq = rawReq;
|
|
39
|
+
this._rawResp = rawResp;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* requestMeta is set when the handler is a typed handler or a stream handler.
|
|
43
|
+
* for raw handlers, see rawRequest and rawResponse.
|
|
44
|
+
*/
|
|
45
|
+
get requestMeta() {
|
|
46
|
+
return this._reqMeta || (this._reqMeta = currentRequest());
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* stream is set when the handler is a stream handler.
|
|
50
|
+
*/
|
|
51
|
+
get stream() {
|
|
52
|
+
return this._stream;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* rawRequest is set when the handler is a raw request handler.
|
|
56
|
+
*/
|
|
57
|
+
get rawRequest() {
|
|
58
|
+
return this._rawReq;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* rawResponse is set when the handler is a raw request handler.
|
|
62
|
+
*/
|
|
63
|
+
get rawResponse() {
|
|
64
|
+
return this._rawResp;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export class ResponseHeader {
|
|
68
|
+
headers;
|
|
69
|
+
constructor() {
|
|
70
|
+
this.headers = {};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* set will set a header value for a key, if a previous middleware has
|
|
74
|
+
* already set a value, it will be overridden.
|
|
75
|
+
*/
|
|
76
|
+
set(key, value) {
|
|
77
|
+
this.headers[key] = value;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* add adds a header value to a key, if a previous middleware has
|
|
81
|
+
* already set a value, they will be appended.
|
|
82
|
+
*/
|
|
83
|
+
add(key, value) {
|
|
84
|
+
const prev = this.headers[key];
|
|
85
|
+
if (prev === undefined) {
|
|
86
|
+
this.headers[key] = value;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.headers[key] = [prev, value].flat();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export class HandlerResponse {
|
|
94
|
+
/**
|
|
95
|
+
* The payload returned by the handler when the handler is either
|
|
96
|
+
* a typed handler or stream handler.
|
|
97
|
+
*/
|
|
98
|
+
payload;
|
|
99
|
+
_headers;
|
|
100
|
+
constructor(payload) {
|
|
101
|
+
this.payload = payload;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* header can be used by middlewares to set headers to the
|
|
105
|
+
* response. This only works for typed handler. For raw handlers
|
|
106
|
+
* see MiddlewareRequest.rawResponse.
|
|
107
|
+
*/
|
|
108
|
+
get header() {
|
|
109
|
+
if (this._headers === undefined) {
|
|
110
|
+
this._headers = new ResponseHeader();
|
|
111
|
+
}
|
|
112
|
+
return this._headers;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* __internalToResponse converts a response to the internal representation
|
|
116
|
+
*/
|
|
117
|
+
__internalToResponse() {
|
|
118
|
+
return {
|
|
119
|
+
payload: this.payload,
|
|
120
|
+
extraHeaders: this._headers?.headers
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
export function middleware(a, b) {
|
|
125
|
+
if (b === undefined) {
|
|
126
|
+
return a;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
const opts = a;
|
|
130
|
+
const mw = b;
|
|
131
|
+
mw.options = opts;
|
|
132
|
+
return mw;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
30
135
|
export { APIError, ErrCode } from "./error.js";
|
|
31
136
|
export { Gateway } from "./gateway.js";
|
|
137
|
+
export { IterableSocket, IterableStream, Sink } from "./stream.js";
|
|
32
138
|
//# sourceMappingURL=mod.js.map
|
package/dist/api/mod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../api/mod.ts"],"names":[],"mappings":"AAAA,oBAAoB;AAGpB,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAmHpE,MAAM,UAAU,GAAG,CAAC,OAAmB,EAAE,EAAO;IAC9C,OAAO,EAAE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AC;QACpC,MAAM,EAAE,GAAG,CAAe,CAAC;QAC3B,EAAE,CAAC,OAAO,GAAG,IAAI,CAAC;QAElB,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAsB,MAAM,WAAW,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as runtime from "../internal/runtime/mod.js";
|
|
2
|
+
export declare class IterableStream {
|
|
3
|
+
private stream;
|
|
4
|
+
constructor(stream: runtime.Stream);
|
|
5
|
+
recv(): Promise<Record<string, any>>;
|
|
6
|
+
[Symbol.asyncIterator](): AsyncGenerator<PVals, void, unknown>;
|
|
7
|
+
}
|
|
8
|
+
export declare class IterableSocket {
|
|
9
|
+
private socket;
|
|
10
|
+
constructor(socket: runtime.Socket);
|
|
11
|
+
send(msg: Record<string, any>): void;
|
|
12
|
+
recv(): Promise<Record<string, any>>;
|
|
13
|
+
close(): void;
|
|
14
|
+
[Symbol.asyncIterator](): AsyncGenerator<PVals, void, unknown>;
|
|
15
|
+
}
|
|
16
|
+
export declare class Sink {
|
|
17
|
+
private sink;
|
|
18
|
+
constructor(sink: runtime.Sink);
|
|
19
|
+
send(msg: Record<string, any>): void;
|
|
20
|
+
close(): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export class IterableStream {
|
|
2
|
+
stream;
|
|
3
|
+
constructor(stream) {
|
|
4
|
+
this.stream = stream;
|
|
5
|
+
}
|
|
6
|
+
recv() {
|
|
7
|
+
return this.stream.recv();
|
|
8
|
+
}
|
|
9
|
+
async *[Symbol.asyncIterator]() {
|
|
10
|
+
while (true) {
|
|
11
|
+
try {
|
|
12
|
+
yield await this.stream.recv();
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class IterableSocket {
|
|
21
|
+
socket;
|
|
22
|
+
constructor(socket) {
|
|
23
|
+
this.socket = socket;
|
|
24
|
+
}
|
|
25
|
+
send(msg) {
|
|
26
|
+
return this.socket.send(msg);
|
|
27
|
+
}
|
|
28
|
+
recv() {
|
|
29
|
+
return this.socket.recv();
|
|
30
|
+
}
|
|
31
|
+
close() {
|
|
32
|
+
this.socket.close();
|
|
33
|
+
}
|
|
34
|
+
async *[Symbol.asyncIterator]() {
|
|
35
|
+
while (true) {
|
|
36
|
+
try {
|
|
37
|
+
yield await this.socket.recv();
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export class Sink {
|
|
46
|
+
sink;
|
|
47
|
+
constructor(sink) {
|
|
48
|
+
this.sink = sink;
|
|
49
|
+
}
|
|
50
|
+
send(msg) {
|
|
51
|
+
return this.sink.send(msg);
|
|
52
|
+
}
|
|
53
|
+
close() {
|
|
54
|
+
this.sink.close();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../api/stream.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,cAAc;IACjB,MAAM,CAAiB;IAE/B,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,IAAI;QACF,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC;QAC3B,OAAO,IAAI,EAAE,CAAC;YACZ,IAAI,CAAC;gBACH,MAAM,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACjC,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED,MAAM,OAAO,cAAc;IACjB,MAAM,CAAiB;IAE/B,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,IAAI,CAAC,GAAwB;QAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;IACD,IAAI;QACF,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC5B,CAAC;IAED,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC;QAC3B,OAAO,IAAI,EAAE,CAAC;YACZ,IAAI,CAAC;gBACH,MAAM,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACjC,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED,MAAM,OAAO,IAAI;IACP,IAAI,CAAe;IAE3B,YAAY,IAAkB;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,CAAC,GAAwB;QAC3B,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED,KAAK;QACH,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC;CACF"}
|
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
import { Gateway } from "../../api/gateway.js";
|
|
2
|
+
import { Middleware } from "../../api/mod.js";
|
|
2
3
|
import * as runtime from "../runtime/mod.js";
|
|
3
|
-
export type Handler =
|
|
4
|
+
export type Handler = {
|
|
5
|
+
apiRoute: runtime.ApiRoute;
|
|
6
|
+
middlewares: Middleware[];
|
|
7
|
+
endpointOptions: EndpointOptions;
|
|
8
|
+
};
|
|
4
9
|
export declare function registerHandlers(handlers: Handler[]): void;
|
|
5
10
|
export declare function registerTestHandler(handler: Handler): void;
|
|
6
11
|
export declare function registerGateways(gateways: Gateway[]): void;
|
|
7
12
|
export declare function run(): Promise<void>;
|
|
13
|
+
interface EndpointOptions {
|
|
14
|
+
expose: boolean;
|
|
15
|
+
auth: boolean;
|
|
16
|
+
isRaw: boolean;
|
|
17
|
+
isStream: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface InternalHandlerResponse {
|
|
20
|
+
payload: any;
|
|
21
|
+
extraHeaders?: Record<string, string | string[]>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|