@shuvi/service 1.0.0 → 1.0.1

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.
@@ -14,24 +14,29 @@ const constants_1 = require("../../constants");
14
14
  const utils_1 = require("../utils");
15
15
  const getAssetMiddleware = (context, isDev = false) => {
16
16
  return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
17
- const fullUrl = new URL(req.url, `http://${req.headers.host}`);
18
- let assetPath = fullUrl.pathname;
19
17
  const candidatePaths = [];
20
- if (context.assetPublicPath.startsWith('/')) {
21
- assetPath = assetPath.replace(context.assetPublicPath, '');
22
- candidatePaths.push(context.resolveBuildFile(constants_1.CLIENT_OUTPUT_DIR, assetPath));
23
- }
24
- else {
25
- fullUrl.search = '';
26
- const urlWithoutQuery = fullUrl.toString();
27
- // when assetPublicPath is http:localhost:3000/xx
28
- if (urlWithoutQuery.startsWith(context.assetPublicPath)) {
29
- assetPath = urlWithoutQuery.replace(context.assetPublicPath, '');
18
+ try {
19
+ const fullUrl = new URL(req.url, `http://${req.headers.host}`);
20
+ let assetPath = fullUrl.pathname;
21
+ if (context.assetPublicPath.startsWith('/')) {
22
+ assetPath = assetPath.replace(context.assetPublicPath, '');
30
23
  candidatePaths.push(context.resolveBuildFile(constants_1.CLIENT_OUTPUT_DIR, assetPath));
31
24
  }
25
+ else {
26
+ fullUrl.search = '';
27
+ const urlWithoutQuery = fullUrl.toString();
28
+ // when assetPublicPath is http:localhost:3000/xx
29
+ if (urlWithoutQuery.startsWith(context.assetPublicPath)) {
30
+ assetPath = urlWithoutQuery.replace(context.assetPublicPath, '');
31
+ candidatePaths.push(context.resolveBuildFile(constants_1.CLIENT_OUTPUT_DIR, assetPath));
32
+ }
33
+ }
34
+ if (isDev) {
35
+ candidatePaths.push(context.resolvePublicFile(assetPath));
36
+ }
32
37
  }
33
- if (isDev) {
34
- candidatePaths.push(context.resolvePublicFile(assetPath));
38
+ catch (err) {
39
+ return next(err);
35
40
  }
36
41
  if (!candidatePaths.length) {
37
42
  return next();
@@ -14,6 +14,7 @@ const string_1 = require("@shuvi/utils/lib/string");
14
14
  const serverMiddleware_1 = require("./serverMiddleware");
15
15
  const http_server_1 = require("./http-server");
16
16
  const plugin_1 = require("./plugin");
17
+ const utils_1 = require("./utils");
17
18
  class ShuviServer {
18
19
  constructor(corePluginContext, options) {
19
20
  this._pluginManager = (0, plugin_1.getManager)();
@@ -28,6 +29,8 @@ class ShuviServer {
28
29
  const fullAssetPath = (0, string_1.joinPath)(this._serverContext.assetPublicPath, assetPath);
29
30
  return fullAssetPath;
30
31
  };
32
+ // Parsing of cookies
33
+ shuviReq.cookies = (0, utils_1.getCookieParser)(req);
31
34
  }
32
35
  _normalizeResp(_resp) {
33
36
  // do nothing
@@ -4,12 +4,14 @@ import { ServerPluginInstance } from './plugin';
4
4
  import { IPlatformContent } from '../core';
5
5
  import { Bunlder } from '../bundler';
6
6
  import { IRequest, IResponse, IRequestHandlerWithNext } from '../server/http-server';
7
+ import type { getCookieParser } from './utils';
7
8
  export interface CustomShuviRequest {
8
9
  }
9
10
  export interface CustomShuviResponse {
10
11
  }
11
12
  export interface ShuviRequest extends IRequest, CustomShuviRequest {
12
13
  getAssetUrl(assetPath: string): string;
14
+ cookies: ReturnType<typeof getCookieParser>;
13
15
  }
14
16
  export interface ShuviResponse extends IResponse, CustomShuviResponse {
15
17
  }
@@ -2,4 +2,11 @@
2
2
  import { IncomingMessage, ServerResponse } from 'http';
3
3
  export declare function isStaticFileExist(path: string): Boolean;
4
4
  export declare function serveStatic(req: IncomingMessage, res: ServerResponse, path: string): Promise<void>;
5
+ /**
6
+ * Parse cookies from `req` header
7
+ * @param req request object
8
+ */
9
+ export declare function getCookieParser(req: IncomingMessage): {
10
+ [key: string]: string;
11
+ };
5
12
  export declare function sendHTML(req: IncomingMessage, res: ServerResponse, html: string): void;
@@ -1,11 +1,35 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.sendHTML = exports.serveStatic = exports.isStaticFileExist = void 0;
29
+ exports.sendHTML = exports.getCookieParser = exports.serveStatic = exports.isStaticFileExist = void 0;
7
30
  const fs_1 = require("fs");
8
31
  const send_1 = __importDefault(require("send"));
32
+ const cookie = __importStar(require("cookie"));
9
33
  function isStaticFileExist(path) {
10
34
  if ((0, fs_1.existsSync)(path)) {
11
35
  return !(0, fs_1.statSync)(path).isDirectory() && (0, fs_1.statSync)(path).isFile();
@@ -31,6 +55,18 @@ function serveStatic(req, res, path) {
31
55
  });
32
56
  }
33
57
  exports.serveStatic = serveStatic;
58
+ /**
59
+ * Parse cookies from `req` header
60
+ * @param req request object
61
+ */
62
+ function getCookieParser(req) {
63
+ const header = req.headers.cookie;
64
+ if (!header) {
65
+ return {};
66
+ }
67
+ return cookie.parse(Array.isArray(header) ? header.join(';') : header);
68
+ }
69
+ exports.getCookieParser = getCookieParser;
34
70
  function sendHTML(req, res, html) {
35
71
  if (res.writableEnded || res.headersSent)
36
72
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/service",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -29,13 +29,13 @@
29
29
  "@babel/generator": "7.14.5",
30
30
  "@babel/parser": "7.14.7",
31
31
  "@babel/traverse": "7.14.7",
32
- "@shuvi/hook": "1.0.0",
33
- "@shuvi/router": "1.0.0",
34
- "@shuvi/runtime": "1.0.0",
35
- "@shuvi/shared": "1.0.0",
36
- "@shuvi/toolpack": "1.0.0",
37
- "@shuvi/utils": "1.0.0",
38
- "@shuvi/error-overlay": "1.0.0",
32
+ "@shuvi/hook": "1.0.1",
33
+ "@shuvi/router": "1.0.1",
34
+ "@shuvi/runtime": "1.0.1",
35
+ "@shuvi/shared": "1.0.1",
36
+ "@shuvi/toolpack": "1.0.1",
37
+ "@shuvi/utils": "1.0.1",
38
+ "@shuvi/error-overlay": "1.0.1",
39
39
  "commander": "5.1.0",
40
40
  "comment-json": "4.2.2",
41
41
  "cross-spawn": "7.0.3",
@@ -43,6 +43,7 @@
43
43
  "fs-extra": "9.0.1",
44
44
  "http-proxy-middleware": "1.0.6",
45
45
  "rimraf": "3.0.2",
46
+ "cookie": "0.4.1",
46
47
  "send": "0.17.1",
47
48
  "ws": "8.2.3",
48
49
  "dotenv": "16.0.3",
@@ -55,6 +56,7 @@
55
56
  "@types/babel__traverse": "^7.14.2",
56
57
  "@types/cross-spawn": "6.0.1",
57
58
  "@types/detect-port": "1.3.0",
59
+ "@types/cookie": "^0.4.1",
58
60
  "@types/ejs": "3.0.4",
59
61
  "@types/ws": "8.2.0"
60
62
  }