kilnvm-embed 0.0.0-stage → 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 KilnVM
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,43 @@
1
- # Temporary Holding Version
1
+ # kilnvm-embed
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Zero-dependency JavaScript and TypeScript helper for embedding [KilnVM](https://kilnvm.dev/) live Linux terminal sandboxes in documentation sites, tutorials, and interactive guides.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install kilnvm-embed
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Mount in any DOM container
14
+
15
+ ```js
16
+ import { mountKilnSandbox, runKilnCommand } from "kilnvm-embed";
17
+
18
+ await mountKilnSandbox("#sandbox", {
19
+ project: "proj_your_project_id",
20
+ id: "quickstart",
21
+ theme: "dark",
22
+ cmd: "mycli check config.yaml",
23
+ files: {
24
+ "config.yaml": "mode: strict\n",
25
+ },
26
+ });
27
+
28
+ runKilnCommand("mycli --version", "quickstart");
29
+ ```
30
+
31
+ ### Build an iframe URL (MDX / Mintlify / Nextra)
32
+
33
+ ```js
34
+ import { buildKilnEmbedUrl } from "kilnvm-embed";
35
+
36
+ const src = buildKilnEmbedUrl({
37
+ project: "proj_your_project_id",
38
+ theme: "auto",
39
+ cmd: "mycli --help",
40
+ });
41
+ ```
42
+
43
+ Docs: https://kilnvm.dev/docs
package/index.cjs ADDED
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+
3
+ const DEFAULT_HOST = "https://kilnvm.dev";
4
+ const HEX_FIELDS = ["accent", "bg", "surface", "fg", "border"];
5
+
6
+ function cleanOrigin(rawHost) {
7
+ const base = typeof rawHost === "string" && rawHost.trim() ? rawHost.trim() : DEFAULT_HOST;
8
+ return base.replace(/\/+$/, "");
9
+ }
10
+
11
+ function normalizeHex(raw) {
12
+ if (typeof raw !== "string") return "";
13
+ const cleaned = raw.trim().replace(/^#/, "");
14
+ if (/^[0-9a-fA-F]{3}$/.test(cleaned)) {
15
+ return (
16
+ "#" +
17
+ cleaned[0] +
18
+ cleaned[0] +
19
+ cleaned[1] +
20
+ cleaned[1] +
21
+ cleaned[2] +
22
+ cleaned[2]
23
+ ).toLowerCase();
24
+ }
25
+ if (/^[0-9a-fA-F]{6}$/.test(cleaned)) {
26
+ return "#" + cleaned.toLowerCase();
27
+ }
28
+ return "";
29
+ }
30
+
31
+ function encodeBase64UrlUtf8(raw) {
32
+ const input = String(raw || "");
33
+ if (typeof Buffer !== "undefined") {
34
+ return Buffer.from(input, "utf8")
35
+ .toString("base64")
36
+ .replace(/\+/g, "-")
37
+ .replace(/\//g, "_")
38
+ .replace(/=+$/, "");
39
+ }
40
+ const bytes = new TextEncoder().encode(input);
41
+ let bin = "";
42
+ for (let i = 0; i < bytes.length; i++) {
43
+ bin += String.fromCharCode(bytes[i]);
44
+ }
45
+ return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
46
+ }
47
+
48
+ function buildKilnEmbedUrl(options = {}) {
49
+ const host = cleanOrigin(options.host);
50
+ const params = new URLSearchParams();
51
+ params.set("project", options.project ? String(options.project).trim() : "demo");
52
+ if (options.theme) params.set("theme", String(options.theme).trim().toLowerCase());
53
+ for (const field of HEX_FIELDS) {
54
+ const hex = normalizeHex(options[field]);
55
+ if (hex) params.set(field, hex.slice(1));
56
+ }
57
+ if (options.cmd) params.set("cmd", String(options.cmd).trim());
58
+ if (typeof options.autoboot === "boolean") {
59
+ params.set("autoboot", options.autoboot ? "true" : "false");
60
+ }
61
+ if (options.path) params.set("path", String(options.path).trim());
62
+ if (options.origin) params.set("origin", String(options.origin).trim());
63
+ if (options.files && typeof options.files === "object" && Object.keys(options.files).length > 0) {
64
+ params.set("files", encodeBase64UrlUtf8(JSON.stringify(options.files)));
65
+ }
66
+ return `${host}/embed?${params.toString()}`;
67
+ }
68
+
69
+ function buildKilnMountAttributes(options = {}) {
70
+ const attrs = {
71
+ "data-kilnvm-project": options.project ? String(options.project).trim() : "demo",
72
+ };
73
+ if (options.id) attrs["data-kilnvm-id"] = String(options.id);
74
+ if (options.theme) attrs["data-kilnvm-theme"] = String(options.theme);
75
+ for (const field of HEX_FIELDS) {
76
+ const hex = normalizeHex(options[field]);
77
+ if (hex) attrs[`data-kilnvm-${field}`] = hex;
78
+ }
79
+ if (options.cmd) attrs["data-kilnvm-cmd"] = String(options.cmd);
80
+ if (options.fallbackCmd) attrs["data-kilnvm-fallback-cmd"] = String(options.fallbackCmd);
81
+ if (options.height !== undefined && options.height !== null) {
82
+ attrs["data-kilnvm-height"] = String(options.height);
83
+ }
84
+ if (typeof options.autoboot === "boolean") {
85
+ attrs["data-kilnvm-autoboot"] = options.autoboot ? "true" : "false";
86
+ }
87
+ if (options.files && typeof options.files === "object" && Object.keys(options.files).length > 0) {
88
+ attrs["data-kilnvm-files"] = JSON.stringify(options.files);
89
+ }
90
+ return attrs;
91
+ }
92
+
93
+ function loadKilnEmbedScript(options = {}) {
94
+ if (typeof document === "undefined") {
95
+ return Promise.resolve(null);
96
+ }
97
+ const src = `${cleanOrigin(options.host)}/kilnvm-embed.js`;
98
+ const existing = document.querySelector(`script[src="${src}"]`);
99
+ if (existing) {
100
+ if (typeof window !== "undefined" && window.KilnVMEmbed) {
101
+ return Promise.resolve(window.KilnVMEmbed);
102
+ }
103
+ return new Promise((resolve, reject) => {
104
+ existing.addEventListener("load", () => resolve(window.KilnVMEmbed || null), { once: true });
105
+ existing.addEventListener("error", () => reject(new Error("Failed to load KilnVM embed script")), {
106
+ once: true,
107
+ });
108
+ });
109
+ }
110
+ return new Promise((resolve, reject) => {
111
+ const script = document.createElement("script");
112
+ script.src = src;
113
+ script.defer = true;
114
+ script.addEventListener("load", () => resolve(window.KilnVMEmbed || null), { once: true });
115
+ script.addEventListener("error", () => reject(new Error("Failed to load KilnVM embed script")), {
116
+ once: true,
117
+ });
118
+ (document.head || document.body || document.documentElement).appendChild(script);
119
+ });
120
+ }
121
+
122
+ async function mountKilnSandbox(target, options = {}) {
123
+ if (typeof document === "undefined") return null;
124
+ const el = typeof target === "string" ? document.querySelector(target) : target;
125
+ if (!el) {
126
+ throw new Error("KilnVM mount target element not found");
127
+ }
128
+ const attrs = buildKilnMountAttributes(options);
129
+ for (const [key, val] of Object.entries(attrs)) {
130
+ el.setAttribute(key, val);
131
+ }
132
+ const api = await loadKilnEmbedScript({ host: options.host });
133
+ if (api && typeof api.mountAll === "function") {
134
+ api.mountAll();
135
+ }
136
+ return api;
137
+ }
138
+
139
+ function runKilnCommand(cmd, target) {
140
+ if (typeof window === "undefined" || !window.KilnVMEmbed) return false;
141
+ return Boolean(window.KilnVMEmbed.runCommand(cmd, target));
142
+ }
143
+
144
+ function reconnectKilnSandbox(target) {
145
+ if (typeof window === "undefined" || !window.KilnVMEmbed) return false;
146
+ return Boolean(window.KilnVMEmbed.reconnect(target));
147
+ }
148
+
149
+ function setKilnTheme(theme, overrides, target) {
150
+ if (typeof window === "undefined" || !window.KilnVMEmbed) return false;
151
+ return Boolean(window.KilnVMEmbed.setTheme(theme, overrides, target));
152
+ }
153
+
154
+ module.exports = {
155
+ buildKilnEmbedUrl,
156
+ buildKilnMountAttributes,
157
+ loadKilnEmbedScript,
158
+ mountKilnSandbox,
159
+ runKilnCommand,
160
+ reconnectKilnSandbox,
161
+ setKilnTheme,
162
+ };
package/index.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ export type KilnThemePreset = "dark" | "light" | "midnight" | "emerald" | "auto";
2
+
3
+ export interface KilnThemeOverrides {
4
+ accent?: string;
5
+ bg?: string;
6
+ surface?: string;
7
+ fg?: string;
8
+ border?: string;
9
+ }
10
+
11
+ export interface KilnEmbedUrlOptions extends KilnThemeOverrides {
12
+ project?: string;
13
+ host?: string;
14
+ theme?: KilnThemePreset;
15
+ cmd?: string;
16
+ autoboot?: boolean;
17
+ path?: string;
18
+ origin?: string;
19
+ files?: Record<string, string>;
20
+ }
21
+
22
+ export interface KilnMountOptions extends KilnThemeOverrides {
23
+ id?: string;
24
+ project?: string;
25
+ host?: string;
26
+ theme?: KilnThemePreset;
27
+ cmd?: string;
28
+ fallbackCmd?: string;
29
+ autoboot?: boolean;
30
+ height?: number;
31
+ files?: Record<string, string>;
32
+ }
33
+
34
+ export interface KilnVMEmbedGlobal {
35
+ mountAll(): void;
36
+ runCommand(cmd: string, target?: string): boolean;
37
+ reconnect(target?: string): boolean;
38
+ setTheme(theme: KilnThemePreset | string, overrides?: KilnThemeOverrides, target?: string): boolean;
39
+ getSessionAuth(target?: string): { sessionId: string; wsToken: string; live: boolean };
40
+ setFallbackCommand(cmd: string, target?: string): boolean;
41
+ }
42
+
43
+ export function buildKilnEmbedUrl(options?: KilnEmbedUrlOptions): string;
44
+ export function buildKilnMountAttributes(options?: KilnMountOptions): Record<string, string>;
45
+ export function loadKilnEmbedScript(options?: { host?: string }): Promise<KilnVMEmbedGlobal | null>;
46
+ export function mountKilnSandbox(
47
+ target: Element | string,
48
+ options?: KilnMountOptions
49
+ ): Promise<KilnVMEmbedGlobal | null>;
50
+ export function runKilnCommand(cmd: string, target?: string): boolean;
51
+ export function reconnectKilnSandbox(target?: string): boolean;
52
+ export function setKilnTheme(
53
+ theme: KilnThemePreset | string,
54
+ overrides?: KilnThemeOverrides,
55
+ target?: string
56
+ ): boolean;
package/index.mjs ADDED
@@ -0,0 +1,150 @@
1
+ const DEFAULT_HOST = "https://kilnvm.dev";
2
+ const HEX_FIELDS = ["accent", "bg", "surface", "fg", "border"];
3
+
4
+ function cleanOrigin(rawHost) {
5
+ const base = typeof rawHost === "string" && rawHost.trim() ? rawHost.trim() : DEFAULT_HOST;
6
+ return base.replace(/\/+$/, "");
7
+ }
8
+
9
+ function normalizeHex(raw) {
10
+ if (typeof raw !== "string") return "";
11
+ const cleaned = raw.trim().replace(/^#/, "");
12
+ if (/^[0-9a-fA-F]{3}$/.test(cleaned)) {
13
+ return (
14
+ "#" +
15
+ cleaned[0] +
16
+ cleaned[0] +
17
+ cleaned[1] +
18
+ cleaned[1] +
19
+ cleaned[2] +
20
+ cleaned[2]
21
+ ).toLowerCase();
22
+ }
23
+ if (/^[0-9a-fA-F]{6}$/.test(cleaned)) {
24
+ return "#" + cleaned.toLowerCase();
25
+ }
26
+ return "";
27
+ }
28
+
29
+ function encodeBase64UrlUtf8(raw) {
30
+ const input = String(raw || "");
31
+ if (typeof Buffer !== "undefined") {
32
+ return Buffer.from(input, "utf8")
33
+ .toString("base64")
34
+ .replace(/\+/g, "-")
35
+ .replace(/\//g, "_")
36
+ .replace(/=+$/, "");
37
+ }
38
+ const bytes = new TextEncoder().encode(input);
39
+ let bin = "";
40
+ for (let i = 0; i < bytes.length; i++) {
41
+ bin += String.fromCharCode(bytes[i]);
42
+ }
43
+ return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
44
+ }
45
+
46
+ export function buildKilnEmbedUrl(options = {}) {
47
+ const host = cleanOrigin(options.host);
48
+ const params = new URLSearchParams();
49
+ params.set("project", options.project ? String(options.project).trim() : "demo");
50
+ if (options.theme) params.set("theme", String(options.theme).trim().toLowerCase());
51
+ for (const field of HEX_FIELDS) {
52
+ const hex = normalizeHex(options[field]);
53
+ if (hex) params.set(field, hex.slice(1));
54
+ }
55
+ if (options.cmd) params.set("cmd", String(options.cmd).trim());
56
+ if (typeof options.autoboot === "boolean") {
57
+ params.set("autoboot", options.autoboot ? "true" : "false");
58
+ }
59
+ if (options.path) params.set("path", String(options.path).trim());
60
+ if (options.origin) params.set("origin", String(options.origin).trim());
61
+ if (options.files && typeof options.files === "object" && Object.keys(options.files).length > 0) {
62
+ params.set("files", encodeBase64UrlUtf8(JSON.stringify(options.files)));
63
+ }
64
+ return `${host}/embed?${params.toString()}`;
65
+ }
66
+
67
+ export function buildKilnMountAttributes(options = {}) {
68
+ const attrs = {
69
+ "data-kilnvm-project": options.project ? String(options.project).trim() : "demo",
70
+ };
71
+ if (options.id) attrs["data-kilnvm-id"] = String(options.id);
72
+ if (options.theme) attrs["data-kilnvm-theme"] = String(options.theme);
73
+ for (const field of HEX_FIELDS) {
74
+ const hex = normalizeHex(options[field]);
75
+ if (hex) attrs[`data-kilnvm-${field}`] = hex;
76
+ }
77
+ if (options.cmd) attrs["data-kilnvm-cmd"] = String(options.cmd);
78
+ if (options.fallbackCmd) attrs["data-kilnvm-fallback-cmd"] = String(options.fallbackCmd);
79
+ if (options.height !== undefined && options.height !== null) {
80
+ attrs["data-kilnvm-height"] = String(options.height);
81
+ }
82
+ if (typeof options.autoboot === "boolean") {
83
+ attrs["data-kilnvm-autoboot"] = options.autoboot ? "true" : "false";
84
+ }
85
+ if (options.files && typeof options.files === "object" && Object.keys(options.files).length > 0) {
86
+ attrs["data-kilnvm-files"] = JSON.stringify(options.files);
87
+ }
88
+ return attrs;
89
+ }
90
+
91
+ export function loadKilnEmbedScript(options = {}) {
92
+ if (typeof document === "undefined") {
93
+ return Promise.resolve(null);
94
+ }
95
+ const src = `${cleanOrigin(options.host)}/kilnvm-embed.js`;
96
+ const existing = document.querySelector(`script[src="${src}"]`);
97
+ if (existing) {
98
+ if (typeof window !== "undefined" && window.KilnVMEmbed) {
99
+ return Promise.resolve(window.KilnVMEmbed);
100
+ }
101
+ return new Promise((resolve, reject) => {
102
+ existing.addEventListener("load", () => resolve(window.KilnVMEmbed || null), { once: true });
103
+ existing.addEventListener("error", () => reject(new Error("Failed to load KilnVM embed script")), {
104
+ once: true,
105
+ });
106
+ });
107
+ }
108
+ return new Promise((resolve, reject) => {
109
+ const script = document.createElement("script");
110
+ script.src = src;
111
+ script.defer = true;
112
+ script.addEventListener("load", () => resolve(window.KilnVMEmbed || null), { once: true });
113
+ script.addEventListener("error", () => reject(new Error("Failed to load KilnVM embed script")), {
114
+ once: true,
115
+ });
116
+ (document.head || document.body || document.documentElement).appendChild(script);
117
+ });
118
+ }
119
+
120
+ export async function mountKilnSandbox(target, options = {}) {
121
+ if (typeof document === "undefined") return null;
122
+ const el = typeof target === "string" ? document.querySelector(target) : target;
123
+ if (!el) {
124
+ throw new Error("KilnVM mount target element not found");
125
+ }
126
+ const attrs = buildKilnMountAttributes(options);
127
+ for (const [key, val] of Object.entries(attrs)) {
128
+ el.setAttribute(key, val);
129
+ }
130
+ const api = await loadKilnEmbedScript({ host: options.host });
131
+ if (api && typeof api.mountAll === "function") {
132
+ api.mountAll();
133
+ }
134
+ return api;
135
+ }
136
+
137
+ export function runKilnCommand(cmd, target) {
138
+ if (typeof window === "undefined" || !window.KilnVMEmbed) return false;
139
+ return Boolean(window.KilnVMEmbed.runCommand(cmd, target));
140
+ }
141
+
142
+ export function reconnectKilnSandbox(target) {
143
+ if (typeof window === "undefined" || !window.KilnVMEmbed) return false;
144
+ return Boolean(window.KilnVMEmbed.reconnect(target));
145
+ }
146
+
147
+ export function setKilnTheme(theme, overrides, target) {
148
+ if (typeof window === "undefined" || !window.KilnVMEmbed) return false;
149
+ return Boolean(window.KilnVMEmbed.setTheme(theme, overrides, target));
150
+ }
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "kilnvm-embed",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Zero-dependency JS and TypeScript helper for embedding KilnVM live Linux terminal sandboxes inside documentation and tutorials.",
5
+ "keywords": [
6
+ "kilnvm",
7
+ "terminal",
8
+ "sandbox",
9
+ "microvm",
10
+ "cli",
11
+ "docs",
12
+ "xterm"
13
+ ],
14
+ "homepage": "https://kilnvm.dev/docs",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/kilnvm/sdks.git",
18
+ "directory": "kilnvm-embed"
19
+ },
20
+ "license": "MIT",
21
+ "type": "module",
22
+ "main": "./index.cjs",
23
+ "module": "./index.mjs",
24
+ "types": "./index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./index.d.ts",
28
+ "import": "./index.mjs",
29
+ "require": "./index.cjs"
30
+ }
31
+ },
32
+ "files": [
33
+ "index.mjs",
34
+ "index.cjs",
35
+ "index.d.ts",
36
+ "README.md",
37
+ "LICENSE"
38
+ ],
39
+ "sideEffects": false
40
+ }