glslh 1.0.0 → 1.0.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/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/src/parser.js +23 -7
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
## 📋 `glslh` Changelog
|
|
2
|
+
|
|
3
|
+
### **v1.0.1** (Latest)
|
|
4
|
+
|
|
5
|
+
- **Strict File Extension Whitelisting**: Added validation checks inside `GLSLHParser` to restrict `#include` directives exclusively to valid shader extensions (`.glsl`, `.glslh`, `.frag`, `.vert`, `.frag.glsl`, `.vert.glsl`), preventing security risks and unauthorized inclusions of non-shader files (e.g., `.py`, `.cpp`, `.txt`).
|
|
6
|
+
- **Compound Header Support**: Enhanced path checking to natively support complex shader extension combinations like `.frag.glslh` and `.vert.glslh`.
|
|
7
|
+
- **Robust Error Messaging**: Improved failure handling and descriptive console warnings/errors when file resolution or extension validation fails.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
### **v1.0.0**
|
|
12
|
+
|
|
13
|
+
- **Recursive C-Style `#include` Preprocessor**: Introduced core parsing logic enabling `.glslh` and `.glsl` files to modularly include other shader headers recursively with built-in circular dependency protection.
|
|
14
|
+
- **Dynamic JavaScript Math Constant Injection**: Added automatic replacement of JS math expressions (such as `Math.PI` and `Math.TAU`) directly into clean GLSL numeric floats.
|
|
15
|
+
- **Metadata Uniforms Block (`@uniforms`)**: Implemented support for JSON-parsed `@uniforms` comment blocks to automatically generate and format Three.js uniform maps.
|
|
16
|
+
- **Zero-Config DOM Canvas Integration (`GLSLHDomCanvas`)**: Released `autoInitGlslCanvases()` to instantly bind canvas elements to Three.js WebGL renderers with automatic shader loading and animation loops.
|
package/package.json
CHANGED
package/src/parser.js
CHANGED
|
@@ -11,18 +11,36 @@ export class GLSLHParser {
|
|
|
11
11
|
return await res.text();
|
|
12
12
|
});
|
|
13
13
|
this.includeCache = new Map();
|
|
14
|
+
|
|
15
|
+
this.allowedExtensions = [
|
|
16
|
+
".glsl",
|
|
17
|
+
".glslh",
|
|
18
|
+
".frag",
|
|
19
|
+
".vert",
|
|
20
|
+
".frag.glsl",
|
|
21
|
+
".vert.glsl",
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
validateExtension(filePath) {
|
|
26
|
+
const lowerPath = filePath.toLowerCase();
|
|
27
|
+
const isValid = this.allowedExtensions.some((ext) =>
|
|
28
|
+
lowerPath.endsWith(ext),
|
|
29
|
+
);
|
|
30
|
+
if (!isValid) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`GLSLH Security Error: Unsupported file extension for include -> '${filePath}'. Allowed extensions: ${this.allowedExtensions.join(", ")}`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
14
35
|
}
|
|
15
36
|
|
|
16
|
-
// Robust path resolution that correctly handles subdirectory nesting
|
|
17
37
|
resolvePath(parentPath, relativePath) {
|
|
18
38
|
if (!parentPath) return relativePath;
|
|
19
39
|
|
|
20
|
-
// Extract directory from parent path (e.g., "./src/examples/shaders/header.glslh" -> "./src/examples/shaders")
|
|
21
40
|
const lastSlash = parentPath.lastIndexOf("/");
|
|
22
41
|
if (lastSlash === -1) return relativePath;
|
|
23
42
|
const parentDir = parentPath.substring(0, lastSlash);
|
|
24
43
|
|
|
25
|
-
// If relative path starts with ./, strip it to join cleanly
|
|
26
44
|
const cleanRelative = relativePath.startsWith("./")
|
|
27
45
|
? relativePath.substring(2)
|
|
28
46
|
: relativePath;
|
|
@@ -36,8 +54,6 @@ export class GLSLHParser {
|
|
|
36
54
|
|
|
37
55
|
async _parseRecursive(sourceCode, currentFilePath, includedFiles) {
|
|
38
56
|
let processed = sourceCode;
|
|
39
|
-
|
|
40
|
-
// 1. Extract and parse the @uniforms metadata block
|
|
41
57
|
const uniformRegex = /\/\*\s*@uniforms([\s\S]*?)\*\//;
|
|
42
58
|
const match = processed.match(uniformRegex);
|
|
43
59
|
let uniformsDefinition = {};
|
|
@@ -54,7 +70,6 @@ export class GLSLHParser {
|
|
|
54
70
|
}
|
|
55
71
|
}
|
|
56
72
|
|
|
57
|
-
// 2. Resolve C-style #include directives recursively
|
|
58
73
|
const includeRegex = /^#include\s+["<](.+)[">]/gm;
|
|
59
74
|
let includeMatch;
|
|
60
75
|
let includes = [];
|
|
@@ -66,6 +81,8 @@ export class GLSLHParser {
|
|
|
66
81
|
for (const inc of includes) {
|
|
67
82
|
const resolvedPath = this.resolvePath(currentFilePath, inc.path);
|
|
68
83
|
|
|
84
|
+
this.validateExtension(resolvedPath);
|
|
85
|
+
|
|
69
86
|
if (includedFiles.has(resolvedPath)) {
|
|
70
87
|
processed = processed.replace(
|
|
71
88
|
inc.fullMatch,
|
|
@@ -93,7 +110,6 @@ export class GLSLHParser {
|
|
|
93
110
|
);
|
|
94
111
|
}
|
|
95
112
|
|
|
96
|
-
// 3. Dynamically inject JS Math constants directly into GLSL syntax
|
|
97
113
|
processed = processed.replace(
|
|
98
114
|
/Math\.([A-Z0-9_]+)/g,
|
|
99
115
|
(match, constantName) => {
|