@server/next 0.20.11 → 0.20.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.20.11",
3
+ "version": "0.20.13",
4
4
  "description": "An experimental reimplementation of server.js focused on the DX",
5
5
  "homepage": "https://node-server.com/",
6
6
  "repository": "https://github.com/franciscop/server-next.git",
@@ -1,5 +1,5 @@
1
1
  import auth from "../auth/index.js";
2
- import { define } from "../helpers/index.js";
2
+ import { define, parseHeaders } from "../helpers/index.js";
3
3
  import parseBody from "./parseBody.js";
4
4
  import parseCookies from "./parseCookies.js";
5
5
 
package/src/index.js CHANGED
@@ -31,7 +31,7 @@ const createNodeServer = async (app, options) => {
31
31
  extendWithDefaults(ctx);
32
32
  const out = await handleRequest(app.handlers, ctx);
33
33
 
34
- response.writeHead(out.status || 200, out.headers);
34
+ response.writeHead(out.status || 200, parseHeaders(out.headers));
35
35
  if (out.body instanceof ReadableStream) {
36
36
  await iterate(out.body, (chunk) => response.write(chunk));
37
37
  } else {
@@ -50,6 +50,7 @@ const createNodeServer = async (app, options) => {
50
50
  const validateOptions = (options, env = {}) => {
51
51
  options.port = options.port || env.PORT || 3000;
52
52
  options.secret = options.secret || env.SECRET || "unsafe-" + createId();
53
+ options.cors = options.cors || env.CORS || null;
53
54
 
54
55
  options.views = options.views ? Bucket(options.views) : null;
55
56
  options.public = options.public ? Bucket(options.public) : null;
@@ -3,7 +3,8 @@ import { type } from "../reply.js";
3
3
  export default async function assets(ctx) {
4
4
  try {
5
5
  // TODO: streaming
6
- const asset = await ctx.options.public.read(ctx.url.pathname);
6
+ // Read it as buffer (null)
7
+ const asset = await ctx.options.public.read(ctx.url.pathname, null);
7
8
  if (!asset) return;
8
9
  return type(ctx.url.pathname.split(".").pop()).send(asset);
9
10
  } catch (error) {
@@ -39,6 +39,14 @@ export default async function parseResponse(out, ctx) {
39
39
 
40
40
  // Here it should be a Response
41
41
 
42
+ // If we have CORS, set it up
43
+ if (typeof ctx.options.cors === "string") {
44
+ const origin = ctx.options.cors || ctx.options.domain || "*";
45
+ const methods = "GET,HEAD,POST,PUT,PATCH";
46
+ out.headers.set("Access-Control-Allow-Origin", origin);
47
+ out.headers.set("Access-Control-Allow-Methods", methods);
48
+ }
49
+
42
50
  // If we have a session, we need to persist it into a cookie
43
51
  if (Object.keys(ctx.session || {}).length) {
44
52
  if (!ctx.options.session?.store) {
@@ -1,4 +1,6 @@
1
1
  export default function pathPattern(pattern, path) {
2
+ if (pattern === "*") return {};
3
+
2
4
  pattern = "/" + pattern.replace(/^\//, "");
3
5
  pattern = pattern.replace(/\/$/, "") || "/";
4
6
  path = path.replace(/\/$/, "") || "/";