nsp-server-pages 0.1.0 → 0.1.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.
- package/cjs/src/app.js +5 -5
- package/cjs/src/bundle.js +3 -0
- package/cjs/src/catch.js +6 -6
- package/index.d.ts +16 -18
- package/package.json +2 -2
- package/src/app.js +5 -5
- package/src/bundle.js +3 -0
- package/src/catch.js +6 -6
package/cjs/src/app.js
CHANGED
|
@@ -15,7 +15,7 @@ class App {
|
|
|
15
15
|
this.loaders = [];
|
|
16
16
|
this.tagMap = new Map();
|
|
17
17
|
this.fnMap = new Map();
|
|
18
|
-
this.
|
|
18
|
+
this.hooks = new Map;
|
|
19
19
|
this.options = options = Object.create(options || null);
|
|
20
20
|
if (!options.vName)
|
|
21
21
|
options.vName = "v";
|
|
@@ -25,12 +25,12 @@ class App {
|
|
|
25
25
|
options.storeKey = "#nsp";
|
|
26
26
|
}
|
|
27
27
|
hook(type, fn) {
|
|
28
|
-
this.
|
|
28
|
+
this.hooks.set(type, fn);
|
|
29
29
|
}
|
|
30
|
-
process(type,
|
|
31
|
-
const fn = this.
|
|
30
|
+
process(type, ...args) {
|
|
31
|
+
const fn = this.hooks.get(type);
|
|
32
32
|
if (fn)
|
|
33
|
-
return fn(
|
|
33
|
+
return fn.apply(null, args);
|
|
34
34
|
}
|
|
35
35
|
log(message) {
|
|
36
36
|
const logger = this.options.logger || console;
|
package/cjs/src/bundle.js
CHANGED
|
@@ -6,13 +6,16 @@ const join = (a, b) => (a == null ? b : (b == null ? a : a + b));
|
|
|
6
6
|
const bundle = (array, start, end) => {
|
|
7
7
|
start = +start || 0;
|
|
8
8
|
end = +end || array?.length || 0;
|
|
9
|
+
// empty
|
|
9
10
|
if (end <= start) {
|
|
10
11
|
return () => null;
|
|
11
12
|
}
|
|
13
|
+
// single item
|
|
12
14
|
if (end - 1 === start) {
|
|
13
15
|
const node = array[start];
|
|
14
16
|
return (typeof node === "function") ? node : () => node;
|
|
15
17
|
}
|
|
18
|
+
// multiple items
|
|
16
19
|
return context => {
|
|
17
20
|
let result;
|
|
18
21
|
let promise;
|
package/cjs/src/catch.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.catchFn = void 0;
|
|
4
|
-
const to_xml_1 = require("to-xml");
|
|
5
4
|
const isPromise = (v) => v && (typeof v.catch === "function");
|
|
6
|
-
const escapeError = (e) => (0, to_xml_1.toXML)({ "#": (e?.message || String(e)) });
|
|
7
5
|
const catchFn = (app, fn) => {
|
|
8
6
|
return context => {
|
|
9
7
|
try {
|
|
@@ -26,11 +24,13 @@ const catchFn = (app, fn) => {
|
|
|
26
24
|
throw e;
|
|
27
25
|
data.error = e;
|
|
28
26
|
}
|
|
29
|
-
// call the error
|
|
27
|
+
// call the error hook
|
|
30
28
|
const result = app.process("error", e, context);
|
|
31
|
-
if
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
// if the hook returns nothing, throw the error
|
|
30
|
+
if (result == null)
|
|
31
|
+
throw e;
|
|
32
|
+
// if the hook returns a string, show it
|
|
33
|
+
return result;
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
36
|
};
|
package/index.d.ts
CHANGED
|
@@ -7,19 +7,21 @@
|
|
|
7
7
|
export const createNSP: (options?: NSP.Options) => NSP.App;
|
|
8
8
|
|
|
9
9
|
declare namespace NSP {
|
|
10
|
-
type NodeFn<T> = (context
|
|
10
|
+
type NodeFn<T> = (context: T) => string | Promise<string>;
|
|
11
11
|
|
|
12
12
|
type Node<T> = string | NodeFn<T>;
|
|
13
13
|
|
|
14
|
-
type AttrFn<A, T = any> = (context
|
|
14
|
+
type AttrFn<A, T = any> = (context: T) => A;
|
|
15
15
|
|
|
16
|
-
type
|
|
16
|
+
type VoidFn<T> = (context: T) => void | Promise<void>;
|
|
17
17
|
|
|
18
|
-
type
|
|
18
|
+
type TagFn<A, T = any> = (tag: TagDef<A, T>) => (NodeFn<T> | VoidFn<T>);
|
|
19
19
|
|
|
20
|
-
type
|
|
20
|
+
type LoaderFn = (path: string) => Promise<NodeFn<any> | undefined>;
|
|
21
21
|
|
|
22
|
-
type
|
|
22
|
+
type Strings = string | Promise<string> | Strings[];
|
|
23
|
+
|
|
24
|
+
type Build<T> = (nsp: App) => (context: T) => string | Promise<string>;
|
|
23
25
|
|
|
24
26
|
interface TagDef<A, T = any> {
|
|
25
27
|
app: App;
|
|
@@ -103,18 +105,12 @@ declare namespace NSP {
|
|
|
103
105
|
/**
|
|
104
106
|
* concat strings even if they are Promise<string>
|
|
105
107
|
*/
|
|
106
|
-
concat(...text:
|
|
108
|
+
concat(...text: Strings[]): string | Promise<string>;
|
|
107
109
|
|
|
108
110
|
/**
|
|
109
111
|
* retrieve a result from hook function
|
|
110
112
|
*/
|
|
111
|
-
process<
|
|
112
|
-
|
|
113
|
-
process<T>(type: "directive", src: string, context?: T): string;
|
|
114
|
-
|
|
115
|
-
process<T>(type: "declaration", src: string, context?: T): string;
|
|
116
|
-
|
|
117
|
-
process<T>(type: "scriptlet", src: string, context?: T): string;
|
|
113
|
+
process<R>(type: string, ...args: any[]): R;
|
|
118
114
|
|
|
119
115
|
/**
|
|
120
116
|
* pickup the taglib function
|
|
@@ -148,13 +144,15 @@ declare namespace NSP {
|
|
|
148
144
|
/**
|
|
149
145
|
* register a hook function
|
|
150
146
|
*/
|
|
151
|
-
hook(type: "error", fn:
|
|
147
|
+
hook(type: "error", fn: (e: Error, context?: any) => string | void): void;
|
|
148
|
+
|
|
149
|
+
hook(type: "directive", fn: (src: string, context?: any) => string | void): void;
|
|
152
150
|
|
|
153
|
-
hook(type: "
|
|
151
|
+
hook(type: "declaration", fn: (src: string, context?: any) => string | void): void;
|
|
154
152
|
|
|
155
|
-
hook(type: "
|
|
153
|
+
hook(type: "scriptlet", fn: (src: string, context?: any) => string | void): void;
|
|
156
154
|
|
|
157
|
-
hook(type:
|
|
155
|
+
hook(type: string, fn: (...args: any[]) => any): void;
|
|
158
156
|
|
|
159
157
|
/**
|
|
160
158
|
* parse a JSP document
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nsp-server-pages",
|
|
3
3
|
"description": "NSP JavaScript Server Pages for Node.js",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"author": "@kawanet",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/kawanet/nsp-server-pages/issues"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"@rollup/plugin-node-resolve": "^15.2.1",
|
|
15
15
|
"@types/mocha": "^10.0.1",
|
|
16
|
-
"@types/node": "^20.5.
|
|
16
|
+
"@types/node": "^20.5.4",
|
|
17
17
|
"mocha": "^10.2.0",
|
|
18
18
|
"typescript": "^5.1.6"
|
|
19
19
|
},
|
package/src/app.js
CHANGED
|
@@ -11,7 +11,7 @@ class App {
|
|
|
11
11
|
this.loaders = [];
|
|
12
12
|
this.tagMap = new Map();
|
|
13
13
|
this.fnMap = new Map();
|
|
14
|
-
this.
|
|
14
|
+
this.hooks = new Map;
|
|
15
15
|
this.options = options = Object.create(options || null);
|
|
16
16
|
if (!options.vName)
|
|
17
17
|
options.vName = "v";
|
|
@@ -21,12 +21,12 @@ class App {
|
|
|
21
21
|
options.storeKey = "#nsp";
|
|
22
22
|
}
|
|
23
23
|
hook(type, fn) {
|
|
24
|
-
this.
|
|
24
|
+
this.hooks.set(type, fn);
|
|
25
25
|
}
|
|
26
|
-
process(type,
|
|
27
|
-
const fn = this.
|
|
26
|
+
process(type, ...args) {
|
|
27
|
+
const fn = this.hooks.get(type);
|
|
28
28
|
if (fn)
|
|
29
|
-
return fn(
|
|
29
|
+
return fn.apply(null, args);
|
|
30
30
|
}
|
|
31
31
|
log(message) {
|
|
32
32
|
const logger = this.options.logger || console;
|
package/src/bundle.js
CHANGED
|
@@ -3,13 +3,16 @@ const join = (a, b) => (a == null ? b : (b == null ? a : a + b));
|
|
|
3
3
|
export const bundle = (array, start, end) => {
|
|
4
4
|
start = +start || 0;
|
|
5
5
|
end = +end || array?.length || 0;
|
|
6
|
+
// empty
|
|
6
7
|
if (end <= start) {
|
|
7
8
|
return () => null;
|
|
8
9
|
}
|
|
10
|
+
// single item
|
|
9
11
|
if (end - 1 === start) {
|
|
10
12
|
const node = array[start];
|
|
11
13
|
return (typeof node === "function") ? node : () => node;
|
|
12
14
|
}
|
|
15
|
+
// multiple items
|
|
13
16
|
return context => {
|
|
14
17
|
let result;
|
|
15
18
|
let promise;
|
package/src/catch.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { toXML } from "to-xml";
|
|
2
1
|
const isPromise = (v) => v && (typeof v.catch === "function");
|
|
3
|
-
const escapeError = (e) => toXML({ "#": (e?.message || String(e)) });
|
|
4
2
|
export const catchFn = (app, fn) => {
|
|
5
3
|
return context => {
|
|
6
4
|
try {
|
|
@@ -23,11 +21,13 @@ export const catchFn = (app, fn) => {
|
|
|
23
21
|
throw e;
|
|
24
22
|
data.error = e;
|
|
25
23
|
}
|
|
26
|
-
// call the error
|
|
24
|
+
// call the error hook
|
|
27
25
|
const result = app.process("error", e, context);
|
|
28
|
-
if
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
// if the hook returns nothing, throw the error
|
|
27
|
+
if (result == null)
|
|
28
|
+
throw e;
|
|
29
|
+
// if the hook returns a string, show it
|
|
30
|
+
return result;
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
33
|
};
|