@kontyra/renderer 0.1.0 → 0.1.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.
package/dist/html.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { ComponentVNode } from '@kontyra/core';
2
+ export declare function renderToString(node: ComponentVNode | string | number | null): string;
3
+ export declare function renderToStream(node: ComponentVNode): ReadableStream<Uint8Array>;
4
+ //# sourceMappingURL=html.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,wBAAgB,cAAc,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,MAAM,CA0CpF;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,cAAc,GAAG,cAAc,CAAC,UAAU,CAAC,CAU/E"}
package/dist/html.js ADDED
@@ -0,0 +1,57 @@
1
+ export function renderToString(node) {
2
+ if (node === null || node === undefined) {
3
+ return '';
4
+ }
5
+ if (typeof node === 'string') {
6
+ return escapeHtml(node);
7
+ }
8
+ if (typeof node === 'number') {
9
+ return String(node);
10
+ }
11
+ // Component Function Execution
12
+ if (typeof node.type === 'function') {
13
+ const vnode = node.type({ ...node.props, children: node.children });
14
+ return renderToString(vnode);
15
+ }
16
+ const tag = node.type;
17
+ let attrs = '';
18
+ for (const [key, val] of Object.entries(node.props)) {
19
+ if (key === 'children' || val === false || val === null || val === undefined) {
20
+ continue;
21
+ }
22
+ if (val === true) {
23
+ attrs += ' ' + key;
24
+ }
25
+ else if (key === 'className') {
26
+ attrs += ' class="' + escapeHtml(String(val)) + '"';
27
+ }
28
+ else {
29
+ attrs += ' ' + key + '="' + escapeHtml(String(val)) + '"';
30
+ }
31
+ }
32
+ const isSelfClosing = ['meta', 'link', 'img', 'br', 'hr', 'input'].includes(tag);
33
+ if (isSelfClosing) {
34
+ return '<' + tag + attrs + ' />';
35
+ }
36
+ const innerHtml = node.children.map((c) => renderToString(c)).join('');
37
+ return '<' + tag + attrs + '>' + innerHtml + '</' + tag + '>';
38
+ }
39
+ export function renderToStream(node) {
40
+ const encoder = new TextEncoder();
41
+ const html = renderToString(node);
42
+ return new ReadableStream({
43
+ start(controller) {
44
+ controller.enqueue(encoder.encode(html));
45
+ controller.close();
46
+ },
47
+ });
48
+ }
49
+ function escapeHtml(str) {
50
+ return str
51
+ .replace(/&/g, '&amp;')
52
+ .replace(/</g, '&lt;')
53
+ .replace(/>/g, '&gt;')
54
+ .replace(/"/g, '&quot;')
55
+ .replace(/'/g, '&#39;');
56
+ }
57
+ //# sourceMappingURL=html.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.js","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,cAAc,CAAC,IAA6C;IAC1E,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACxC,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,+BAA+B;IAC/B,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;QACpC,MAAM,KAAK,GAAI,IAAI,CAAC,IAAiB,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAClF,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC;IACtB,IAAI,KAAK,GAAG,EAAE,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,WAAW,EAAE,CAAC;IAClC,MAAM,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IAElC,OAAO,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,UAAU;YACd,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;YACzC,UAAU,CAAC,KAAK,EAAE,CAAC;QACrB,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,SAAS,UAAU,CAAC,GAAW;IAC7B,OAAO,GAAG;SACP,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;SACvB,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './vdom.js';
2
+ export * from './html.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './vdom.js';
2
+ export * from './html.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=renderer.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderer.test.d.ts","sourceRoot":"","sources":["../../src/test/renderer.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,22 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { h, renderToString, renderToStream } from '../index.js';
4
+ test('renderToString produces correct HTML for native components', () => {
5
+ const Card = (props) => {
6
+ return h('div', { className: 'card' }, h('h2', null, props.title), h('p', null, props.children));
7
+ };
8
+ const vnode = h(Card, { title: 'Hello Zenor' }, 'The full-stack framework');
9
+ const html = renderToString(vnode);
10
+ assert.equal(html, '<div class="card"><h2>Hello Zenor</h2><p>The full-stack framework</p></div>');
11
+ });
12
+ test('renderToStream produces readable Uint8Array chunks', async () => {
13
+ const vnode = h('main', { id: 'app' }, h('h1', null, 'Streaming SSR'));
14
+ const stream = renderToStream(vnode);
15
+ const reader = stream.getReader();
16
+ const { value, done } = await reader.read();
17
+ assert.ok(value);
18
+ assert.equal(done, false);
19
+ const text = new TextDecoder().decode(value);
20
+ assert.equal(text, '<main id="app"><h1>Streaming SSR</h1></main>');
21
+ });
22
+ //# sourceMappingURL=renderer.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderer.test.js","sourceRoot":"","sources":["../../src/test/renderer.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,CAAC,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEhE,IAAI,CAAC,4DAA4D,EAAE,GAAG,EAAE;IACtE,MAAM,IAAI,GAAG,CAAC,KAAuC,EAAE,EAAE;QACvD,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,EACnC,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,EAC1B,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,CAC7B,CAAC;IACJ,CAAC,CAAC;IAEF,MAAM,KAAK,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,EAAE,0BAA0B,CAAC,CAAC;IAC5E,MAAM,IAAI,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IAEnC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,6EAA6E,CAAC,CAAC;AACpG,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,oDAAoD,EAAE,KAAK,IAAI,EAAE;IACpE,MAAM,KAAK,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC;IACvE,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IAErC,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;IAClC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;IAE5C,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;IACjB,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC1B,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7C,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,8CAA8C,CAAC,CAAC;AACrE,CAAC,CAAC,CAAC"}
package/dist/vdom.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { ComponentVNode } from '@kontyra/core';
2
+ export type ChildNode = ComponentVNode | string | number | boolean | null | undefined;
3
+ export declare function h(type: string | Function, props: Record<string, unknown> | null, ...children: ChildNode[]): ComponentVNode;
4
+ //# sourceMappingURL=vdom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vdom.d.ts","sourceRoot":"","sources":["../src/vdom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,MAAM,MAAM,SAAS,GAAG,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAEtF,wBAAgB,CAAC,CACf,IAAI,EAAE,MAAM,GAAG,QAAQ,EACvB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,EACrC,GAAG,QAAQ,EAAE,SAAS,EAAE,GACvB,cAAc,CAmBhB"}
package/dist/vdom.js ADDED
@@ -0,0 +1,20 @@
1
+ export function h(type, props, ...children) {
2
+ const flatChildren = [];
3
+ for (const child of children.flat(Infinity)) {
4
+ if (child === null || child === undefined || child === false || child === true) {
5
+ continue;
6
+ }
7
+ if (typeof child === 'string' || typeof child === 'number') {
8
+ flatChildren.push(child);
9
+ }
10
+ else {
11
+ flatChildren.push(child);
12
+ }
13
+ }
14
+ return {
15
+ type,
16
+ props: props || {},
17
+ children: flatChildren,
18
+ };
19
+ }
20
+ //# sourceMappingURL=vdom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vdom.js","sourceRoot":"","sources":["../src/vdom.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,CAAC,CACf,IAAuB,EACvB,KAAqC,EACrC,GAAG,QAAqB;IAExB,MAAM,YAAY,GAAmD,EAAE,CAAC;IAExE,KAAK,MAAM,KAAK,IAAI,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5C,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,KAAK,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YAC/E,SAAS;QACX,CAAC;QACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC3D,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;aAAM,CAAC;YACN,YAAY,CAAC,IAAI,CAAC,KAAuB,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI;QACJ,KAAK,EAAE,KAAK,IAAI,EAAE;QAClB,QAAQ,EAAE,YAAY;KACvB,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kontyra/renderer",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Native component Virtual DOM, CSR, SSR, and streaming HTML engine for Zenor",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -16,11 +16,15 @@
16
16
  "test": "node --test dist/test/*.test.js"
17
17
  },
18
18
  "dependencies": {
19
- "@kontyra/core": "*"
19
+ "@kontyra/core": "^0.1.1"
20
20
  },
21
21
  "publishConfig": {
22
22
  "access": "public"
23
23
  },
24
24
  "license": "Proprietary",
25
- "author": "Kontyra Technologies"
25
+ "author": "Kontyra Technologies",
26
+ "files": [
27
+ "dist",
28
+ "src"
29
+ ]
26
30
  }
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src"
6
- },
7
- "references": [
8
- {
9
- "path": "../core"
10
- }
11
- ],
12
- "include": [
13
- "src/**/*"
14
- ]
15
- }