nsp-server-pages 0.2.1 → 0.2.3

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 CHANGED
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.App = void 0;
4
- const mount_js_1 = require("./mount.js");
4
+ const bundle_js_1 = require("./bundle.js");
5
+ const catch_js_1 = require("./catch.js");
6
+ const concat_js_1 = require("./concat.js");
5
7
  const loaders_js_1 = require("./loaders.js");
8
+ const mount_js_1 = require("./mount.js");
6
9
  const jsp_js_1 = require("./parser/jsp.js");
7
- const catch_js_1 = require("./catch.js");
8
- const bundle_js_1 = require("./bundle.js");
10
+ const store_js_1 = require("./store.js");
9
11
  const taglib_js_1 = require("./taglib.js");
10
- const concat_js_1 = require("./concat.js");
11
- const stack_store_js_1 = require("./stack-store.js");
12
12
  class App {
13
13
  constructor(options) {
14
14
  this.loaders = [];
@@ -78,14 +78,14 @@ class App {
78
78
  return loader.load(file);
79
79
  }
80
80
  store(context, key) {
81
- if ("object" !== typeof context && context == null) {
81
+ if ("object" !== typeof context || context == null) {
82
82
  throw new Error("Context must be an object");
83
83
  }
84
84
  const { storeKey } = this.options;
85
85
  const map = (context[storeKey] ??= new Map());
86
86
  let value = map.get(key);
87
87
  if (value == null) {
88
- value = new stack_store_js_1.StackStore();
88
+ value = new store_js_1.Store();
89
89
  map.set(key, value);
90
90
  }
91
91
  return value;
@@ -58,10 +58,10 @@ class Attr {
58
58
  const nextLF = LF + SP;
59
59
  const keys = this.keys();
60
60
  const items = keys.map(key => {
61
+ const value = this.get(key);
61
62
  if (!isSafeKey(key)) {
62
63
  key = JSON.stringify(key);
63
64
  }
64
- const value = this.get(key);
65
65
  return `${key}: ${value}`;
66
66
  });
67
67
  // no arguments
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jspToJS = exports.JSP = void 0;
4
+ const store_js_1 = require("../store.js");
4
5
  const scriptlet_js_1 = require("./scriptlet.js");
5
- const stack_store_js_1 = require("../stack-store.js");
6
6
  const tag_js_1 = require("./tag.js");
7
7
  /**
8
8
  * Parser for JSP document
@@ -44,8 +44,9 @@ const insideRE = `[^"']|${stringRE}`;
44
44
  const tagRegExp = new RegExp(`(</?${nameRE}:(?:${insideRE})*?>)|(<%(?:${insideRE})*?%>)`, "s");
45
45
  const jspToJS = (app, src, option) => {
46
46
  const root = new tag_js_1.Tag(app);
47
- const tree = new stack_store_js_1.StackStore(root);
47
+ const tree = new store_js_1.Store(root);
48
48
  const array = src.split(tagRegExp);
49
+ const { vName } = app.options;
49
50
  for (let i = 0; i < array.length; i++) {
50
51
  const i3 = i % 3;
51
52
  let str = array[i];
@@ -55,7 +56,7 @@ const jspToJS = (app, src, option) => {
55
56
  // close-tag
56
57
  if (tag.isClose()) {
57
58
  const closed = tree.close();
58
- if (!closed) {
59
+ if (!closed?.tagName) {
59
60
  throw new Error(`invalid closing tag: </${tag.tagName}>`);
60
61
  }
61
62
  if (closed.tagName !== tag.tagName) {
@@ -72,7 +73,13 @@ const jspToJS = (app, src, option) => {
72
73
  else if (i3 === 2 && str) {
73
74
  // <% scriptlet %>
74
75
  const item = new scriptlet_js_1.Scriptlet(app, str);
75
- tree.get().append(item);
76
+ const toJS = (option) => {
77
+ const js = item.toJS(option);
78
+ if (/^\/\//.test(js))
79
+ return js; // comment
80
+ return `${vName} => ${js}`; // array function
81
+ };
82
+ tree.get().append({ toJS });
76
83
  }
77
84
  else if (i3 === 0) {
78
85
  // text node
@@ -40,7 +40,7 @@ class Scriptlet {
40
40
  }
41
41
  app.log(`${type} found: ${src?.substring(0, 1000)}`);
42
42
  src = /`|\$\{/.test(src) ? JSON.stringify(src) : "`" + src + "`";
43
- src = `${vName} => ${nspName}.process("${type}", ${src}, ${vName})`;
43
+ src = `${nspName}.process("${type}", ${src}, ${vName})`;
44
44
  return src;
45
45
  }
46
46
  }
@@ -104,7 +104,7 @@ class Tag {
104
104
  const nextLF = LF + SP;
105
105
  const nextOption = { SP, LF: (body ? nextLF : LF) };
106
106
  const type = `parse.tag.${tagName}`;
107
- const def = { app, name: tagName, attr, body, LF: LF, nextLF };
107
+ const def = { app, name: tagName, attr, body, LF, nextLF };
108
108
  const tagJS = app.process(type, def) ?? this.getTagJS(def, nextOption);
109
109
  return commentJS ? commentJS + tagJS : tagJS;
110
110
  }
@@ -52,7 +52,11 @@ const textToJS = (app, src, option) => {
52
52
  value = value.replace(/\s*}$/s, "");
53
53
  const item = new el_js_1.EL(app, value);
54
54
  if (isAsync) {
55
- items.push({ toJS: (option) => `await ${item.toJS(option)}` });
55
+ const toJS = (option) => {
56
+ const js = item.toJS(option);
57
+ return `await ${js}`;
58
+ };
59
+ items.push({ toJS });
56
60
  }
57
61
  else {
58
62
  items.push(item);
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Store = void 0;
4
+ /**
5
+ * The Store class was implemented using a stack {stack: P[]} at first.
6
+ * Now it was changed to a linked list to allow access to the parent.
7
+ */
8
+ class Store {
9
+ constructor(value) {
10
+ this.item = { value };
11
+ }
12
+ open(value) {
13
+ this.item = { parent: this.item, value };
14
+ }
15
+ close() {
16
+ const item = this.item;
17
+ this.item = item.parent;
18
+ return item.value;
19
+ }
20
+ get() {
21
+ return this.item.value;
22
+ }
23
+ set(value) {
24
+ this.item.value = value;
25
+ }
26
+ find(test) {
27
+ let item = this.item;
28
+ while (item) {
29
+ if (test(item.value)) {
30
+ return item.value;
31
+ }
32
+ item = item.parent;
33
+ }
34
+ }
35
+ }
36
+ exports.Store = Store;
package/cjs/src/taglib.js CHANGED
@@ -2,17 +2,45 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.prepareTag = exports.addTagLib = void 0;
4
4
  const to_xml_1 = require("to-xml");
5
+ const isTagCon = (v) => ("function" === typeof v?.prototype?.render);
6
+ const tagConToTagFn = (Tag) => {
7
+ return (tag) => {
8
+ return (context) => {
9
+ const result = new Tag(tag, context).render();
10
+ if (result)
11
+ return result;
12
+ };
13
+ };
14
+ };
5
15
  function addTagLib(tagLibDef) {
6
16
  const { fnMap, tagMap } = this;
7
17
  const { ns, fn, tag } = tagLibDef;
8
18
  if (fn) {
9
19
  for (const name in fn) {
10
- fnMap.set(`${ns}:${name}`, fn[name]);
20
+ const impl = fn[name];
21
+ if (typeof impl === "function") {
22
+ // FnFn is called with App instance as this
23
+ fnMap.set(`${ns}:${name}`, impl.bind(this));
24
+ }
25
+ else if (impl != null) {
26
+ throw new Error(`Invalid taglib implementation: \${${ns}:${name}()}`);
27
+ }
11
28
  }
12
29
  }
13
30
  if (tag) {
14
31
  for (const name in tag) {
15
- tagMap.set(`${ns}:${name}`, tag[name]);
32
+ const impl = tag[name];
33
+ if (isTagCon(impl)) {
34
+ // NSP.TagCon
35
+ tagMap.set(`${ns}:${name}`, tagConToTagFn(impl));
36
+ }
37
+ else if (typeof impl === "function") {
38
+ // NSP.TagFn
39
+ tagMap.set(`${ns}:${name}`, impl);
40
+ }
41
+ else if (impl != null) {
42
+ throw new Error(`Invalid taglib implementation: <${ns}:${name}>`);
43
+ }
16
44
  }
17
45
  }
18
46
  }
package/esm/src/app.js CHANGED
@@ -1,11 +1,11 @@
1
- import { load, mount } from "./mount.js";
1
+ import { bundle } from "./bundle.js";
2
+ import { catchFn } from "./catch.js";
3
+ import { concat } from "./concat.js";
2
4
  import { FileLoader, JsLoader, JspLoader } from "./loaders.js";
5
+ import { load, mount } from "./mount.js";
3
6
  import { JSP } from "./parser/jsp.js";
4
- import { catchFn } from "./catch.js";
5
- import { bundle } from "./bundle.js";
7
+ import { Store } from "./store.js";
6
8
  import { addTagLib, prepareTag } from "./taglib.js";
7
- import { concat } from "./concat.js";
8
- import { StackStore } from "./stack-store.js";
9
9
  export class App {
10
10
  constructor(options) {
11
11
  this.loaders = [];
@@ -75,14 +75,14 @@ export class App {
75
75
  return loader.load(file);
76
76
  }
77
77
  store(context, key) {
78
- if ("object" !== typeof context && context == null) {
78
+ if ("object" !== typeof context || context == null) {
79
79
  throw new Error("Context must be an object");
80
80
  }
81
81
  const { storeKey } = this.options;
82
82
  const map = (context[storeKey] ??= new Map());
83
83
  let value = map.get(key);
84
84
  if (value == null) {
85
- value = new StackStore();
85
+ value = new Store();
86
86
  map.set(key, value);
87
87
  }
88
88
  return value;
@@ -55,10 +55,10 @@ export class Attr {
55
55
  const nextLF = LF + SP;
56
56
  const keys = this.keys();
57
57
  const items = keys.map(key => {
58
+ const value = this.get(key);
58
59
  if (!isSafeKey(key)) {
59
60
  key = JSON.stringify(key);
60
61
  }
61
- const value = this.get(key);
62
62
  return `${key}: ${value}`;
63
63
  });
64
64
  // no arguments
@@ -1,5 +1,5 @@
1
+ import { Store } from "../store.js";
1
2
  import { Scriptlet } from "./scriptlet.js";
2
- import { StackStore } from "../stack-store.js";
3
3
  import { Tag } from "./tag.js";
4
4
  /**
5
5
  * Parser for JSP document
@@ -40,8 +40,9 @@ const insideRE = `[^"']|${stringRE}`;
40
40
  const tagRegExp = new RegExp(`(</?${nameRE}:(?:${insideRE})*?>)|(<%(?:${insideRE})*?%>)`, "s");
41
41
  export const jspToJS = (app, src, option) => {
42
42
  const root = new Tag(app);
43
- const tree = new StackStore(root);
43
+ const tree = new Store(root);
44
44
  const array = src.split(tagRegExp);
45
+ const { vName } = app.options;
45
46
  for (let i = 0; i < array.length; i++) {
46
47
  const i3 = i % 3;
47
48
  let str = array[i];
@@ -51,7 +52,7 @@ export const jspToJS = (app, src, option) => {
51
52
  // close-tag
52
53
  if (tag.isClose()) {
53
54
  const closed = tree.close();
54
- if (!closed) {
55
+ if (!closed?.tagName) {
55
56
  throw new Error(`invalid closing tag: </${tag.tagName}>`);
56
57
  }
57
58
  if (closed.tagName !== tag.tagName) {
@@ -68,7 +69,13 @@ export const jspToJS = (app, src, option) => {
68
69
  else if (i3 === 2 && str) {
69
70
  // <% scriptlet %>
70
71
  const item = new Scriptlet(app, str);
71
- tree.get().append(item);
72
+ const toJS = (option) => {
73
+ const js = item.toJS(option);
74
+ if (/^\/\//.test(js))
75
+ return js; // comment
76
+ return `${vName} => ${js}`; // array function
77
+ };
78
+ tree.get().append({ toJS });
72
79
  }
73
80
  else if (i3 === 0) {
74
81
  // text node
@@ -37,7 +37,7 @@ export class Scriptlet {
37
37
  }
38
38
  app.log(`${type} found: ${src?.substring(0, 1000)}`);
39
39
  src = /`|\$\{/.test(src) ? JSON.stringify(src) : "`" + src + "`";
40
- src = `${vName} => ${nspName}.process("${type}", ${src}, ${vName})`;
40
+ src = `${nspName}.process("${type}", ${src}, ${vName})`;
41
41
  return src;
42
42
  }
43
43
  }
@@ -101,7 +101,7 @@ export class Tag {
101
101
  const nextLF = LF + SP;
102
102
  const nextOption = { SP, LF: (body ? nextLF : LF) };
103
103
  const type = `parse.tag.${tagName}`;
104
- const def = { app, name: tagName, attr, body, LF: LF, nextLF };
104
+ const def = { app, name: tagName, attr, body, LF, nextLF };
105
105
  const tagJS = app.process(type, def) ?? this.getTagJS(def, nextOption);
106
106
  return commentJS ? commentJS + tagJS : tagJS;
107
107
  }
@@ -48,7 +48,11 @@ const textToJS = (app, src, option) => {
48
48
  value = value.replace(/\s*}$/s, "");
49
49
  const item = new EL(app, value);
50
50
  if (isAsync) {
51
- items.push({ toJS: (option) => `await ${item.toJS(option)}` });
51
+ const toJS = (option) => {
52
+ const js = item.toJS(option);
53
+ return `await ${js}`;
54
+ };
55
+ items.push({ toJS });
52
56
  }
53
57
  else {
54
58
  items.push(item);
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The Store class was implemented using a stack {stack: P[]} at first.
3
+ * Now it was changed to a linked list to allow access to the parent.
4
+ */
5
+ export class Store {
6
+ constructor(value) {
7
+ this.item = { value };
8
+ }
9
+ open(value) {
10
+ this.item = { parent: this.item, value };
11
+ }
12
+ close() {
13
+ const item = this.item;
14
+ this.item = item.parent;
15
+ return item.value;
16
+ }
17
+ get() {
18
+ return this.item.value;
19
+ }
20
+ set(value) {
21
+ this.item.value = value;
22
+ }
23
+ find(test) {
24
+ let item = this.item;
25
+ while (item) {
26
+ if (test(item.value)) {
27
+ return item.value;
28
+ }
29
+ item = item.parent;
30
+ }
31
+ }
32
+ }
package/esm/src/taglib.js CHANGED
@@ -1,15 +1,43 @@
1
1
  import { toXML } from "to-xml";
2
+ const isTagCon = (v) => ("function" === typeof v?.prototype?.render);
3
+ const tagConToTagFn = (Tag) => {
4
+ return (tag) => {
5
+ return (context) => {
6
+ const result = new Tag(tag, context).render();
7
+ if (result)
8
+ return result;
9
+ };
10
+ };
11
+ };
2
12
  export function addTagLib(tagLibDef) {
3
13
  const { fnMap, tagMap } = this;
4
14
  const { ns, fn, tag } = tagLibDef;
5
15
  if (fn) {
6
16
  for (const name in fn) {
7
- fnMap.set(`${ns}:${name}`, fn[name]);
17
+ const impl = fn[name];
18
+ if (typeof impl === "function") {
19
+ // FnFn is called with App instance as this
20
+ fnMap.set(`${ns}:${name}`, impl.bind(this));
21
+ }
22
+ else if (impl != null) {
23
+ throw new Error(`Invalid taglib implementation: \${${ns}:${name}()}`);
24
+ }
8
25
  }
9
26
  }
10
27
  if (tag) {
11
28
  for (const name in tag) {
12
- tagMap.set(`${ns}:${name}`, tag[name]);
29
+ const impl = tag[name];
30
+ if (isTagCon(impl)) {
31
+ // NSP.TagCon
32
+ tagMap.set(`${ns}:${name}`, tagConToTagFn(impl));
33
+ }
34
+ else if (typeof impl === "function") {
35
+ // NSP.TagFn
36
+ tagMap.set(`${ns}:${name}`, impl);
37
+ }
38
+ else if (impl != null) {
39
+ throw new Error(`Invalid taglib implementation: <${ns}:${name}>`);
40
+ }
13
41
  }
14
42
  }
15
43
  }
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.2.1",
4
+ "version": "0.2.3",
5
5
  "author": "@kawanet",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kawanet/nsp-server-pages/issues"
@@ -13,8 +13,9 @@
13
13
  "devDependencies": {
14
14
  "@rollup/plugin-node-resolve": "^15.2.1",
15
15
  "@types/mocha": "^10.0.1",
16
- "@types/node": "^20.5.7",
16
+ "@types/node": "^20.5.8",
17
17
  "mocha": "^10.2.0",
18
+ "nyc": "^15.1.0",
18
19
  "typescript": "^5.2.2"
19
20
  },
20
21
  "exports": {
@@ -49,6 +50,7 @@
49
50
  },
50
51
  "scripts": {
51
52
  "build": "make all",
53
+ "coverage": "npx tsc -p tsconfig-cjs.json && npx nyc npx mocha cjs/test",
52
54
  "fixpack": "fixpack",
53
55
  "prepack": "make clean test-title all test",
54
56
  "test": "make test"
package/types/index.d.ts CHANGED
@@ -19,6 +19,8 @@ export declare namespace NSP {
19
19
 
20
20
  type TagFn<A, T = any> = (tag: TagDef<A, T>) => (NodeFn<T> | VoidFn<T>);
21
21
 
22
+ type TagCon<A, T = any, P extends TagClass = TagClass> = { new(tag: NSP.TagDef<A>, context: T): P, prototype: P };
23
+
22
24
  type LoaderFn = (path: string) => Promise<NodeFn<any> | undefined>;
23
25
 
24
26
  type Strings = string | Promise<string> | Strings[];
@@ -166,7 +168,11 @@ export declare namespace NSP {
166
168
  /**
167
169
  * tags
168
170
  */
169
- tag?: { [name: string]: TagFn<any> };
171
+ tag?: { [name: string]: TagFn<any> | TagCon<any> };
172
+ }
173
+
174
+ interface TagClass {
175
+ render(): string | Promise<string> | void | Promise<void>;
170
176
  }
171
177
 
172
178
  interface StackStore<P> {
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StackStore = void 0;
4
- class StackStore {
5
- constructor(value) {
6
- this.stack = [];
7
- if (arguments.length) {
8
- this.set(value);
9
- }
10
- }
11
- open(value) {
12
- this.stack.unshift(value);
13
- }
14
- close() {
15
- return this.stack.shift();
16
- }
17
- get() {
18
- return this.stack[0];
19
- }
20
- set(value) {
21
- this.stack[0] = value;
22
- }
23
- find(test) {
24
- for (const data of this.stack) {
25
- if (test(data)) {
26
- return data;
27
- }
28
- }
29
- }
30
- }
31
- exports.StackStore = StackStore;
@@ -1,27 +0,0 @@
1
- export class StackStore {
2
- constructor(value) {
3
- this.stack = [];
4
- if (arguments.length) {
5
- this.set(value);
6
- }
7
- }
8
- open(value) {
9
- this.stack.unshift(value);
10
- }
11
- close() {
12
- return this.stack.shift();
13
- }
14
- get() {
15
- return this.stack[0];
16
- }
17
- set(value) {
18
- this.stack[0] = value;
19
- }
20
- find(test) {
21
- for (const data of this.stack) {
22
- if (test(data)) {
23
- return data;
24
- }
25
- }
26
- }
27
- }