@haibun/web-server-express 1.16.0 → 1.18.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/build/defs.d.ts +4 -3
- package/build/server-express.d.ts +10 -9
- package/build/server-express.js +47 -56
- package/build/server-express.js.map +1 -1
- package/build/server-express.test.d.ts +1 -0
- package/build/server-express.test.js +17 -0
- package/build/server-express.test.js.map +1 -0
- package/build/web-server-stepper.d.ts +5 -8
- package/build/web-server-stepper.js +41 -31
- package/build/web-server-stepper.js.map +1 -1
- package/package.json +7 -5
package/build/defs.d.ts
CHANGED
|
@@ -2,11 +2,12 @@ export declare const WEBSERVER = "webserver";
|
|
|
2
2
|
export declare const CHECK_LISTENER = "CHECK_LISTENER";
|
|
3
3
|
import * as express from 'express';
|
|
4
4
|
export interface IWebServer {
|
|
5
|
-
addStaticFolder(subdir: string
|
|
6
|
-
addKnownStaticFolder(subdir: string, mountAt?: string):
|
|
7
|
-
listen(): Promise<
|
|
5
|
+
addStaticFolder(subdir: string, loc: string): void;
|
|
6
|
+
addKnownStaticFolder(subdir: string, mountAt?: string): void;
|
|
7
|
+
listen(): Promise<unknown>;
|
|
8
8
|
close(): Promise<void>;
|
|
9
9
|
addRoute(type: TRouteTypes, path: string, route: TRequestHandler): void;
|
|
10
|
+
use(middleware: express.RequestHandler): void;
|
|
10
11
|
}
|
|
11
12
|
export type TRouteTypes = 'get' | 'post';
|
|
12
13
|
export type IRequest = typeof express.request;
|
|
@@ -4,24 +4,25 @@ import { RequestHandler } from 'express';
|
|
|
4
4
|
import { IWebServer, TRouteTypes } from './defs.js';
|
|
5
5
|
import { ILogger } from '@haibun/core/build/lib/interfaces/logger.js';
|
|
6
6
|
export declare const DEFAULT_PORT = 8123;
|
|
7
|
-
export declare function shutdown(): Promise<void>;
|
|
8
7
|
export declare class ServerExpress implements IWebServer {
|
|
9
8
|
logger: ILogger;
|
|
10
9
|
static listening: boolean;
|
|
11
10
|
listener?: http.Server;
|
|
12
11
|
app: import("express-serve-static-core").Express;
|
|
13
12
|
static mounted: {
|
|
14
|
-
|
|
13
|
+
get: {};
|
|
14
|
+
post: {};
|
|
15
15
|
};
|
|
16
16
|
base: string;
|
|
17
17
|
port: number;
|
|
18
18
|
constructor(logger: ILogger, base: string, port?: number);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
use(middleware: RequestHandler): void;
|
|
20
|
+
listen(): Promise<unknown>;
|
|
21
|
+
addRoute(type: TRouteTypes, path: string, route: RequestHandler): void;
|
|
22
|
+
private addMounted;
|
|
23
|
+
addStaticFolder(relativeFolder: string, mountAt?: string): void;
|
|
24
|
+
addKnownStaticFolder(folder: string, mountAt?: string): void;
|
|
25
|
+
private doAddStaticFolder;
|
|
26
|
+
checkMountBadOrMounted(type: string, loc: string, what: string): void;
|
|
26
27
|
close(): Promise<void>;
|
|
27
28
|
}
|
package/build/server-express.js
CHANGED
|
@@ -2,15 +2,12 @@ import { statSync, existsSync } from 'fs';
|
|
|
2
2
|
import express from 'express';
|
|
3
3
|
import cookieParser from 'cookie-parser';
|
|
4
4
|
export const DEFAULT_PORT = 8123;
|
|
5
|
-
export async function shutdown() {
|
|
6
|
-
await fetch('//localhost:8123/shutdown', { method: 'POST' });
|
|
7
|
-
}
|
|
8
5
|
export class ServerExpress {
|
|
9
6
|
logger;
|
|
10
7
|
static listening = false;
|
|
11
8
|
listener;
|
|
12
9
|
app = express();
|
|
13
|
-
static mounted = {};
|
|
10
|
+
static mounted = { get: {}, post: {} };
|
|
14
11
|
base;
|
|
15
12
|
port;
|
|
16
13
|
constructor(logger, base, port = DEFAULT_PORT) {
|
|
@@ -20,61 +17,57 @@ export class ServerExpress {
|
|
|
20
17
|
this.app.use(cookieParser());
|
|
21
18
|
this.app.use(express.json({ limit: '50mb' }));
|
|
22
19
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
20
|
+
use(middleware) {
|
|
21
|
+
this.app.use(middleware);
|
|
22
|
+
}
|
|
23
|
+
listen() {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
if (!ServerExpress.listening) {
|
|
26
|
+
try {
|
|
27
|
+
this.listener = this.app.listen(this.port, () => {
|
|
28
|
+
this.logger.log(`Server listening on port: ${this.port}`);
|
|
29
|
+
ServerExpress.listening = true;
|
|
30
|
+
this.logger.log('express listening');
|
|
31
|
+
resolve('started');
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
console.error(e);
|
|
36
|
+
reject(e);
|
|
37
|
+
}
|
|
29
38
|
}
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
else {
|
|
40
|
+
this.logger.log('express already listening');
|
|
41
|
+
resolve('already listening');
|
|
32
42
|
}
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
this.logger.log('express already listening');
|
|
36
|
-
}
|
|
37
|
-
return this;
|
|
43
|
+
});
|
|
38
44
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.logger.debug(`already mounted ${path}`);
|
|
43
|
-
return;
|
|
45
|
+
addRoute(type, path, route) {
|
|
46
|
+
if (type !== 'get' && type !== 'post') {
|
|
47
|
+
throw Error(`invalid route type ${type}`);
|
|
44
48
|
}
|
|
45
|
-
this.
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
this.checkMountBadOrMounted(type, path, route.toString());
|
|
50
|
+
this.logger.log(`adding ${type} route from ${path}`);
|
|
51
|
+
this.app[type](path, route);
|
|
52
|
+
this.addMounted(type, path, route.toString());
|
|
48
53
|
}
|
|
49
|
-
async addMounted(path, what) {
|
|
50
|
-
ServerExpress.mounted[path] = what;
|
|
54
|
+
async addMounted(type, path, what) {
|
|
55
|
+
ServerExpress.mounted[type][path] = what;
|
|
51
56
|
if (!this.listener) {
|
|
52
57
|
await this.listen();
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
// add a static folder restricted to relative paths from files
|
|
56
|
-
|
|
57
|
-
if (relativeFolder !== relativeFolder.replace(/[^a-zA-Z-0-9/-_]/g, '').replace(/^\//g, '')) {
|
|
58
|
-
throw Error(`mount folder ${relativeFolder} has illegal characters`);
|
|
59
|
-
}
|
|
61
|
+
addStaticFolder(relativeFolder, mountAt = '/') {
|
|
60
62
|
const folder = [this.base, relativeFolder].join('/');
|
|
61
|
-
|
|
63
|
+
this.doAddStaticFolder(folder, mountAt);
|
|
62
64
|
}
|
|
63
65
|
// add a static folder at any path
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
addKnownStaticFolder(folder, mountAt = '/') {
|
|
67
|
+
this.doAddStaticFolder(folder, mountAt);
|
|
66
68
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const alreadyMounted = this.checkMountBadOrMounted(mountAt, folder);
|
|
70
|
-
if (alreadyMounted) {
|
|
71
|
-
// FIXME
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
catch (e) {
|
|
76
|
-
return e.message;
|
|
77
|
-
}
|
|
69
|
+
doAddStaticFolder(folder, mountAt = '/') {
|
|
70
|
+
this.checkMountBadOrMounted('get', mountAt, folder);
|
|
78
71
|
if (!existsSync(folder)) {
|
|
79
72
|
throw Error(`"${folder}" doesn't exist`);
|
|
80
73
|
}
|
|
@@ -82,25 +75,23 @@ export class ServerExpress {
|
|
|
82
75
|
if (!stat.isDirectory()) {
|
|
83
76
|
throw Error(`"${folder}" is not a directory`);
|
|
84
77
|
}
|
|
78
|
+
this.app.use(mountAt, express.static(folder));
|
|
79
|
+
this.addMounted('get', mountAt, folder);
|
|
85
80
|
this.logger.info(`serving files from ${folder} at ${mountAt}`);
|
|
86
|
-
await this.app.use(mountAt, express.static(folder));
|
|
87
|
-
await this.addMounted(mountAt, folder);
|
|
88
81
|
return;
|
|
89
82
|
}
|
|
90
|
-
checkMountBadOrMounted(loc, what) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.logger.log(`${alreadyMounted} already mounted at ${loc}`);
|
|
94
|
-
return true;
|
|
83
|
+
checkMountBadOrMounted(type, loc, what) {
|
|
84
|
+
if (loc !== loc.replace(/[^a-zA-Z-0-9/-_]/g, '')) {
|
|
85
|
+
throw Error(`mount folder ${loc} has illegal characters`);
|
|
95
86
|
}
|
|
96
|
-
|
|
97
|
-
|
|
87
|
+
const alreadyMounted = ServerExpress.mounted[type][loc] || Object.keys(ServerExpress.mounted[type]).find((m) => m.startsWith(`${loc}/`));
|
|
88
|
+
if (alreadyMounted) {
|
|
89
|
+
throw Error(`cannot mount ${type} ${what} at ${loc}, ${alreadyMounted} is already mounted}`);
|
|
98
90
|
}
|
|
99
|
-
return false;
|
|
100
91
|
}
|
|
101
92
|
async close() {
|
|
102
93
|
this.logger.info('closing server');
|
|
103
|
-
|
|
94
|
+
this.listener?.close();
|
|
104
95
|
}
|
|
105
96
|
}
|
|
106
97
|
//# sourceMappingURL=server-express.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-express.js","sourceRoot":"","sources":["../src/server-express.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAG1C,OAAO,OAA2B,MAAM,SAAS,CAAC;AAClD,OAAO,YAAY,MAAM,eAAe,CAAC;AAKzC,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC;AAEjC,MAAM,
|
|
1
|
+
{"version":3,"file":"server-express.js","sourceRoot":"","sources":["../src/server-express.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAG1C,OAAO,OAA2B,MAAM,SAAS,CAAC;AAClD,OAAO,YAAY,MAAM,eAAe,CAAC;AAKzC,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC;AAEjC,MAAM,OAAO,aAAa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AAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC;QACjJ,IAAI,cAAc,EAAE;YAClB,MAAM,KAAK,CAAC,gBAAgB,IAAI,IAAI,IAAI,OAAO,GAAG,KAAK,cAAc,sBAAsB,CAAC,CAAC;SAC9F;IACH,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;QACnC,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC;IACzB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ServerExpress } from "./server-express.js";
|
|
2
|
+
import TestLogger from '@haibun/core/build/lib/TestLogger.js';
|
|
3
|
+
describe('mounts', () => {
|
|
4
|
+
it('mounts a route', () => {
|
|
5
|
+
const tl = new TestLogger();
|
|
6
|
+
const se = new ServerExpress(tl, '/', 8999);
|
|
7
|
+
se.listen = async () => 'started';
|
|
8
|
+
expect(() => se.addRoute('get', '/', () => undefined)).not.toThrow();
|
|
9
|
+
});
|
|
10
|
+
it('fails to double mount a route', () => {
|
|
11
|
+
const tl = new TestLogger();
|
|
12
|
+
const se = new ServerExpress(tl, '/', 8999);
|
|
13
|
+
se.listen = async () => 'started';
|
|
14
|
+
expect(() => se.addRoute('get', '/', () => undefined)).toThrow();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
//# sourceMappingURL=server-express.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-express.test.js","sourceRoot":"","sources":["../src/server-express.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,UAAU,MAAM,sCAAsC,CAAC;AAE9D,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACpB,EAAE,CAAC,gBAAgB,EAAE,GAAG,EAAE;QACtB,MAAM,EAAE,GAAG,IAAI,UAAU,EAAE,CAAC;QAC5B,MAAM,EAAE,GAAG,IAAI,aAAa,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QAC5C,EAAE,CAAC,MAAM,GAAG,KAAK,IAAI,EAAE,CAAC,SAAS,CAAC;QAClC,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IACzE,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,+BAA+B,EAAE,GAAG,EAAE;QACrC,MAAM,EAAE,GAAG,IAAI,UAAU,EAAE,CAAC;QAC5B,MAAM,EAAE,GAAG,IAAI,aAAa,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QAC5C,EAAE,CAAC,MAAM,GAAG,KAAK,IAAI,EAAE,CAAC,SAAS,CAAC;QAClC,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IACrE,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { TWorld, TNamed, TOptions, AStepper, TVStep } from '@haibun/core/build/lib/defs.js';
|
|
2
|
-
import { WorkspaceContext } from '@haibun/core/build/lib/contexts.js';
|
|
3
2
|
import { IWebServer } from './defs.js';
|
|
4
3
|
import { ServerExpress } from './server-express.js';
|
|
5
4
|
declare const WebServerStepper: {
|
|
@@ -27,11 +26,6 @@ declare const WebServerStepper: {
|
|
|
27
26
|
webpage: {
|
|
28
27
|
gwta: string;
|
|
29
28
|
action: ({ name, location }: TNamed, vsteps: TVStep) => Promise<import("@haibun/core/build/lib/defs.js").TOKActionResult>;
|
|
30
|
-
build: ({ location }: TNamed, { source }: TVStep, workspace: WorkspaceContext) => Promise<{
|
|
31
|
-
finalize: (workspace: WorkspaceContext) => void;
|
|
32
|
-
ok: true;
|
|
33
|
-
topics?: import("@haibun/core/build/lib/defs.js").TActionResultTopics;
|
|
34
|
-
}>;
|
|
35
29
|
};
|
|
36
30
|
isListening: {
|
|
37
31
|
gwta: string;
|
|
@@ -41,13 +35,16 @@ declare const WebServerStepper: {
|
|
|
41
35
|
gwta: string;
|
|
42
36
|
action: () => Promise<import("@haibun/core/build/lib/defs.js").TOKActionResult>;
|
|
43
37
|
};
|
|
38
|
+
serveFilesAt: {
|
|
39
|
+
gwta: string;
|
|
40
|
+
action: ({ where, loc }: TNamed) => Promise<import("@haibun/core/build/lib/defs.js").TOKActionResult | import("@haibun/core/build/lib/defs.js").TNotOKActionResult>;
|
|
41
|
+
};
|
|
44
42
|
serveFiles: {
|
|
45
43
|
gwta: string;
|
|
46
44
|
action: ({ loc }: TNamed) => Promise<import("@haibun/core/build/lib/defs.js").TOKActionResult | import("@haibun/core/build/lib/defs.js").TNotOKActionResult>;
|
|
47
|
-
build: ({ loc }: TNamed) => Promise<import("@haibun/core/build/lib/defs.js").TOKActionResult>;
|
|
48
45
|
};
|
|
49
46
|
};
|
|
50
|
-
|
|
47
|
+
doServeFiles(where: any, loc: any): import("@haibun/core/build/lib/defs.js").TOKActionResult | import("@haibun/core/build/lib/defs.js").TNotOKActionResult;
|
|
51
48
|
world?: TWorld;
|
|
52
49
|
endFeature?(): void;
|
|
53
50
|
onFailure?(result: import("@haibun/core/build/lib/defs.js").TStepResult): void;
|
|
@@ -2,9 +2,7 @@ import { OK, AStepper } from '@haibun/core/build/lib/defs.js';
|
|
|
2
2
|
import { actionNotOK, getFromRuntime, getStepperOption, intOrError } from '@haibun/core/build/lib/util/index.js';
|
|
3
3
|
import { WEBSERVER, } from './defs.js';
|
|
4
4
|
import { ServerExpress, DEFAULT_PORT } from './server-express.js';
|
|
5
|
-
import { WebPageBuilder } from '@haibun/domain-webpage/build/WebPageBuilder.js';
|
|
6
5
|
import { WEB_PAGE } from '@haibun/domain-webpage/build/domain-webpage.js';
|
|
7
|
-
import { getDomain } from '@haibun/core/build/lib/domain.js';
|
|
8
6
|
const WebServerStepper = class WebServerStepper extends AStepper {
|
|
9
7
|
webserver;
|
|
10
8
|
options = {
|
|
@@ -30,7 +28,7 @@ const WebServerStepper = class WebServerStepper extends AStepper {
|
|
|
30
28
|
action: async ({ where }, vstep) => {
|
|
31
29
|
const page = vstep.source.name;
|
|
32
30
|
const webserver = getFromRuntime(this.getWorld().runtime, WEBSERVER);
|
|
33
|
-
webserver.addStaticFolder(page);
|
|
31
|
+
webserver.addStaticFolder(page, where);
|
|
34
32
|
console.debug('added page', page);
|
|
35
33
|
return OK;
|
|
36
34
|
},
|
|
@@ -44,18 +42,20 @@ const WebServerStepper = class WebServerStepper extends AStepper {
|
|
|
44
42
|
// TODO mount the page
|
|
45
43
|
return OK;
|
|
46
44
|
},
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
45
|
+
/*
|
|
46
|
+
build: async ({ location }: TNamed, { source }: TVStep, workspace: WorkspaceContext) => {
|
|
47
|
+
if (location !== location.replace(/[^a-zA-Z-0-9.]/g, '')) {
|
|
48
|
+
throw Error(`${WEB_PAGE} location ${location} has illegal characters`);
|
|
49
|
+
}
|
|
50
|
+
const subdir = this.getWorld().shared.get('file_location');
|
|
51
|
+
if (!subdir) {
|
|
52
|
+
throw Error(`must declare a file_location`);
|
|
53
|
+
}
|
|
54
|
+
const folder = `files/${subdir}`;
|
|
55
|
+
workspace.addBuilder(new WebPageBuilder(source.name, this.getWorld().logger, location, folder));
|
|
56
|
+
return { ...OK, finalize: this.finalize };
|
|
58
57
|
},
|
|
58
|
+
*/
|
|
59
59
|
},
|
|
60
60
|
isListening: {
|
|
61
61
|
gwta: 'webserver is listening',
|
|
@@ -72,32 +72,42 @@ const WebServerStepper = class WebServerStepper extends AStepper {
|
|
|
72
72
|
return OK;
|
|
73
73
|
},
|
|
74
74
|
},
|
|
75
|
+
serveFilesAt: {
|
|
76
|
+
gwta: 'serve files at {where} from {loc}',
|
|
77
|
+
action: async ({ where, loc }) => {
|
|
78
|
+
return this.doServeFiles(where, loc);
|
|
79
|
+
},
|
|
80
|
+
/*
|
|
81
|
+
build: async ({ loc }: TNamed) => {
|
|
82
|
+
this.getWorld().shared.set('file_location', loc);
|
|
83
|
+
return OK;
|
|
84
|
+
}
|
|
85
|
+
*/
|
|
86
|
+
},
|
|
75
87
|
serveFiles: {
|
|
76
88
|
gwta: 'serve files from {loc}',
|
|
77
89
|
action: async ({ loc }) => {
|
|
78
|
-
|
|
79
|
-
const error = await ws.addStaticFolder(loc);
|
|
80
|
-
this.getWorld().shared.set('file_location', loc);
|
|
81
|
-
return error === undefined ? OK : actionNotOK(error);
|
|
90
|
+
return this.doServeFiles('/', loc);
|
|
82
91
|
},
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
/*
|
|
93
|
+
build: async ({ loc }: TNamed) => {
|
|
94
|
+
this.getWorld().shared.set('file_location', loc);
|
|
95
|
+
return OK;
|
|
86
96
|
}
|
|
97
|
+
*/
|
|
87
98
|
},
|
|
88
99
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
100
|
+
doServeFiles(where, loc) {
|
|
101
|
+
const ws = getFromRuntime(this.getWorld().runtime, WEBSERVER);
|
|
102
|
+
try {
|
|
103
|
+
ws.addStaticFolder(loc, where);
|
|
104
|
+
this.getWorld().shared.set('file_location', loc);
|
|
105
|
+
return OK;
|
|
92
106
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const shared = builder.finalize();
|
|
96
|
-
const domain = getDomain(WEB_PAGE, this.getWorld()).shared.get(builder.name);
|
|
97
|
-
for (const [name, val] of shared) {
|
|
98
|
-
domain.set(name, val);
|
|
107
|
+
catch (error) {
|
|
108
|
+
return actionNotOK(error);
|
|
99
109
|
}
|
|
100
|
-
}
|
|
110
|
+
}
|
|
101
111
|
};
|
|
102
112
|
export default WebServerStepper;
|
|
103
113
|
//# sourceMappingURL=web-server-stepper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-server-stepper.js","sourceRoot":"","sources":["../src/web-server-stepper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,EAAE,EAA4B,QAAQ,EAAuB,MAAM,gCAAgC,CAAC;AAC1H,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,sCAAsC,CAAC;AAEjH,OAAO,EAAc,SAAS,GAAG,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"web-server-stepper.js","sourceRoot":"","sources":["../src/web-server-stepper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,EAAE,EAA4B,QAAQ,EAAuB,MAAM,gCAAgC,CAAC;AAC1H,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,sCAAsC,CAAC;AAEjH,OAAO,EAAc,SAAS,GAAG,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAElE,OAAO,EAAE,QAAQ,EAAE,MAAM,gDAAgD,CAAC;AAG1E,MAAM,gBAAgB,GAAG,MAAM,gBAAiB,SAAQ,QAAQ;IAC9D,SAAS,CAA4B;IAErC,OAAO,GAAG;QACR,IAAI,EAAE;YACJ,IAAI,EAAE,+BAA+B,YAAY,EAAE;YACnD,KAAK,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC;SAC1C;KACF,C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AAe,cAAc,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAC1E,IAAI;YACF,EAAE,CAAC,eAAe,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC/B,IAAI,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;YACjD,OAAO,EAAE,CAAC;SACX;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC;SAC3B;IACH,CAAC;CAiBF,CAAC;AACF,eAAe,gBAAgB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haibun/web-server-express",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.18.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "build/web-server-stepper.js",
|
|
7
7
|
"files": [
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"prepublishOnly": "tsc -b .",
|
|
12
12
|
"build": "tsc -b .",
|
|
13
|
+
"build-watch": "tsc -b . --watch",
|
|
13
14
|
"lint": "eslint -c .eslintrc.json --ext .ts,.js src",
|
|
14
15
|
"preversion": "npm run lint",
|
|
15
16
|
"test": "jest",
|
|
@@ -19,14 +20,15 @@
|
|
|
19
20
|
"author": "",
|
|
20
21
|
"license": "ISC",
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@haibun/core": "file:../core",
|
|
23
|
-
"@haibun/domain-webpage": "file:../domain-webpage",
|
|
23
|
+
"@haibun/core": "file:../haibun-core",
|
|
24
|
+
"@haibun/domain-webpage": "file:../haibun-domain-webpage",
|
|
24
25
|
"cookie-parser": "^1.4.5",
|
|
25
26
|
"express": "^4.17.1"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
|
-
"@haibun/web-http": "file:../web-http",
|
|
29
|
+
"@haibun/web-http": "file:../haibun-web-http",
|
|
29
30
|
"@types/cookie-parser": "^1.4.2",
|
|
30
31
|
"@types/express": "^4.17.15"
|
|
31
|
-
}
|
|
32
|
+
},
|
|
33
|
+
"gitHead": "7cf9680bd922fb622fb59f1e6bf5b65284cb8fd5"
|
|
32
34
|
}
|