@server/next 0.36.0 → 0.36.2

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.
Files changed (3) hide show
  1. package/index.d.ts +33 -32
  2. package/index.js +17 -13
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -15,6 +15,38 @@ declare class UploadPipeline {
15
15
  }
16
16
  declare function upload(bucket?: Bucket | string | null): UploadPipeline;
17
17
 
18
+ type CookieOptions = string | string[] | Cookie | Cookie[] | null;
19
+ interface ResponseData {
20
+ headers: Headers;
21
+ status?: number;
22
+ }
23
+ declare class Reply$1 {
24
+ res: ResponseData;
25
+ constructor();
26
+ status(status: number): this;
27
+ type(type?: string): this;
28
+ download(name?: string): this;
29
+ headers(key: string | Record<string, string>, value?: string): this;
30
+ cache(value: CacheOption): this;
31
+ cookies(key: string | Record<string, CookieOptions>, value?: CookieOptions): this;
32
+ json(body: unknown): Response;
33
+ redirect(path: string): Response;
34
+ file(path: string): Promise<Response>;
35
+ send(body?: string | Buffer | ReadableStream | any): Response;
36
+ }
37
+ type Params<K extends keyof Reply$1> = Reply$1[K] extends (...args: infer A) => any ? A : never;
38
+ declare const status: (...args: Params<"status">) => Reply$1;
39
+ declare const headers: (...args: Params<"headers">) => Reply$1;
40
+ declare const type: (...args: Params<"type">) => Reply$1;
41
+ declare const cache: (...args: Params<"cache">) => Reply$1;
42
+ declare const download: (...args: Params<"download">) => Reply$1;
43
+ declare const cookies: (...args: Params<"cookies">) => Reply$1;
44
+ declare const send: (...args: Params<"send">) => Response;
45
+ declare const json: (...args: Params<"json">) => Response;
46
+ declare const file: (...args: Params<"file">) => Promise<Response>;
47
+ declare const redirect: (...args: Params<"redirect">) => Response;
48
+
49
+ type Reply = ReturnType<typeof status>;
18
50
  type Method = "get" | "post" | "put" | "patch" | "delete" | "head" | "options" | "socket";
19
51
  type ServerConfig<Session = {}, User = {}> = {
20
52
  Session?: Session;
@@ -266,7 +298,7 @@ type Context<Params extends Record<string, string | undefined> = Record<string,
266
298
  };
267
299
  app: Server;
268
300
  };
269
- type InlineReply = Response | {
301
+ type InlineReply = Response | Reply | {
270
302
  body: string;
271
303
  headers?: Headers;
272
304
  } | SerializableValue | JSX.Element | Buffer | ReadableStream;
@@ -337,37 +369,6 @@ declare class Router<O extends ServerConfig = object> {
337
369
  }
338
370
  declare function router(): Router;
339
371
 
340
- type CookieOptions = string | string[] | Cookie | Cookie[] | null;
341
- interface ResponseData {
342
- headers: Headers;
343
- status?: number;
344
- }
345
- declare class Reply {
346
- res: ResponseData;
347
- constructor();
348
- status(status: number): this;
349
- type(type?: string): this;
350
- download(name?: string): this;
351
- headers(key: string | Record<string, string>, value?: string): this;
352
- cache(value: CacheOption): this;
353
- cookies(key: string | Record<string, CookieOptions>, value?: CookieOptions): this;
354
- json(body: unknown): Response;
355
- redirect(path: string): Response;
356
- file(path: string): Promise<Response>;
357
- send(body?: string | Buffer | ReadableStream | any): Response;
358
- }
359
- type Params<K extends keyof Reply> = Reply[K] extends (...args: infer A) => any ? A : never;
360
- declare const status: (...args: Params<"status">) => Reply;
361
- declare const headers: (...args: Params<"headers">) => Reply;
362
- declare const type: (...args: Params<"type">) => Reply;
363
- declare const cache: (...args: Params<"cache">) => Reply;
364
- declare const download: (...args: Params<"download">) => Reply;
365
- declare const cookies: (...args: Params<"cookies">) => Reply;
366
- declare const send: (...args: Params<"send">) => Response;
367
- declare const json: (...args: Params<"json">) => Response;
368
- declare const file: (...args: Params<"file">) => Promise<Response>;
369
- declare const redirect: (...args: Params<"redirect">) => Response;
370
-
371
372
  declare class Server<O extends ServerConfig = {}> extends Router<O> {
372
373
  settings: Settings;
373
374
  platform: Platform;
package/index.js CHANGED
@@ -1836,6 +1836,9 @@ async function parseResponse(out, ctx) {
1836
1836
  if (typeof out === "function") {
1837
1837
  out = await out(ctx);
1838
1838
  }
1839
+ if (out && typeof out.send === "function" && out.res?.headers instanceof Headers) {
1840
+ out = out.send();
1841
+ }
1839
1842
  if (out instanceof Blob) {
1840
1843
  out = new Response(out, { headers: { "Content-Type": out.type } });
1841
1844
  }
@@ -2064,22 +2067,14 @@ async function hash2(password) {
2064
2067
  });
2065
2068
  }
2066
2069
 
2067
- // src/helpers/iterate.ts
2068
- async function iterate(stream, cb) {
2069
- const reader = stream.getReader();
2070
- while (true) {
2071
- const chunk = await reader.read();
2072
- if (chunk.done || !chunk.value) return;
2073
- cb(chunk.value);
2074
- }
2075
- }
2076
-
2077
2070
  // src/helpers/iteratorAsyncToReadable.ts
2078
2071
  function iteratorAsyncToReadable(asyncGenerator) {
2072
+ let cancelled = false;
2079
2073
  return new ReadableStream({
2080
2074
  async pull(controller) {
2081
2075
  try {
2082
2076
  const { value, done } = await asyncGenerator.next();
2077
+ if (cancelled) return;
2083
2078
  if (done) {
2084
2079
  controller.close();
2085
2080
  return;
@@ -2090,8 +2085,10 @@ function iteratorAsyncToReadable(asyncGenerator) {
2090
2085
  controller.error(err);
2091
2086
  }
2092
2087
  },
2093
- cancel() {
2094
- console.log("Stream cancelled");
2088
+ // Return the generator so its `finally {}` runs and releases resources.
2089
+ async cancel(reason) {
2090
+ cancelled = true;
2091
+ await asyncGenerator.return?.(reason);
2095
2092
  }
2096
2093
  });
2097
2094
  }
@@ -2989,7 +2986,14 @@ var Node = async (app) => {
2989
2986
  response.writeHead(out.status || 200, parseHeaders_default(out.headers));
2990
2987
  try {
2991
2988
  if (out.body instanceof ReadableStream) {
2992
- await iterate(out.body, (chunk) => response.write(chunk));
2989
+ const reader = out.body.getReader();
2990
+ response.on("close", () => reader.cancel().catch(() => {
2991
+ }));
2992
+ while (true) {
2993
+ const { value, done } = await reader.read();
2994
+ if (done) break;
2995
+ response.write(value);
2996
+ }
2993
2997
  } else {
2994
2998
  response.write(out.body || "");
2995
2999
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.36.0",
3
+ "version": "0.36.2",
4
4
  "description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
5
5
  "homepage": "https://server-js.com/",
6
6
  "repository": "github:franciscop/server-next",