@primate/htmx 0.1.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
+ Primate HTMX module
2
+
3
+ Copyright (c) Terrablue <terrablue@proton.me> and contributors.
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
13
+ all 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
18
+ THE 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
21
+ THE SOFTWARE.
package/exports.js ADDED
@@ -0,0 +1 @@
1
+ export {default} from "./src/handler.js";
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@primate/htmx",
3
+ "version": "0.1.0",
4
+ "author": "Terrablue <terrablue@proton.me>",
5
+ "homepage": "https://primatejs.com/modules/htmx",
6
+ "bugs": "https://github.com/primatejs/primate-htmx/issues",
7
+ "repository": "https://github.com/primatejs/primate-htmx",
8
+ "description": "Primate HTMX module",
9
+ "license": "MIT",
10
+ "dependencies": {
11
+ "htmt": "^0.1.0",
12
+ "htmx.org": "^1.8.6",
13
+ "runtime-compat": "^0.12.3"
14
+ },
15
+ "devDependencies": {
16
+ "@babel/core": "^7.21.0",
17
+ "@babel/eslint-parser": "^7.19.1",
18
+ "@babel/plugin-syntax-import-assertions": "^7.20.0",
19
+ "eslint": "^8.36.0",
20
+ "eslint-plugin-json": "^3.1.0"
21
+ },
22
+ "type": "module",
23
+ "exports": "./exports.js",
24
+ "engines": {
25
+ "node": ">=17.9.0"
26
+ }
27
+ }
package/src/handler.js ADDED
@@ -0,0 +1,71 @@
1
+ import {Path, File} from "runtime-compat/filesystem";
2
+ import crypto from "runtime-compat/crypto";
3
+ import {fulfill, flatten} from "htmt";
4
+
5
+ const last = -1;
6
+
7
+ const index_html = "index.html";
8
+ const preset = await new Path(import.meta.url).directory.join(index_html).file
9
+ .read();
10
+
11
+ const getIndex = async env => {
12
+ try {
13
+ return await File.read(`${env.paths.static.join(index_html)}`);
14
+ } catch (error) {
15
+ return preset;
16
+ }
17
+ };
18
+
19
+ const scriptPath = ["node_modules", "htmx.org", "dist", "htmx.js"];
20
+
21
+ const hash = async (string, algorithm = "sha-384") => {
22
+ const buffer = new ArrayBuffer(string.length);
23
+ const view = new Uint8Array(buffer);
24
+ for (let i = 0; i < string.length; i++) {
25
+ view[i] = string.charCodeAt(i);
26
+ }
27
+ const bytes = await crypto.subtle.digest(algorithm, buffer);
28
+ return Buffer.from(bytes).toString("base64");
29
+ };
30
+
31
+ export default (strings, ...keys) => async (env, headers) => {
32
+ // if doesn't exist, copy htmx.js to public
33
+ const htmx = env.paths.public.join("htmx.js");
34
+ if (!await htmx.exists) {
35
+ await new Path(import.meta.url).directory.directory.join(...scriptPath)
36
+ .file.copy(`${htmx}`);
37
+ }
38
+ //console.log(btoa("sha384-" + await hash(file)));
39
+ const src = "/htmx.js";
40
+ const integrity = `sha384-${await hash(await htmx.file.read())}`;
41
+ const script = `<script src="${src}" integrity="${integrity}"></script>`;
42
+ const index = (await getIndex(env)).replace("</head>", `${script}</head>`);
43
+ const {paths: {components: path}} = env;
44
+ const loadFile = async file => [file.base, (await file.read())
45
+ .replaceAll("\n", "")];
46
+ const components = await path.exists
47
+ ? Object.fromEntries(await Promise.all((
48
+ await File.collect(`${path}`, ".html")).map(loadFile)))
49
+ : {};
50
+ const re = strings
51
+ .slice(0, last)
52
+ .map((string, i) => `${string}\${${i}}`)
53
+ .join("") + strings[strings.length + last];
54
+ const html = flatten(await fulfill(re, components, await Promise.all(keys)));
55
+ const body = index.replace("<body>", () => `<body>${html}`);
56
+
57
+ const script_src = `script-src 'self' '${integrity}';`;
58
+ const style_src = "style-src 'unsafe-inline';";
59
+ const csp = `${headers["Content-Security-Policy"]}${script_src}${style_src}`;
60
+ const options = {
61
+ status: 200,
62
+ headers: {
63
+ ...headers,
64
+ "Content-Type": "text/html",
65
+ "Content-Security-Policy": csp,
66
+ },
67
+ };
68
+
69
+ // -> spread into new Response()
70
+ return [body, options];
71
+ };
package/src/index.html ADDED
@@ -0,0 +1,8 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <title>Primate app</title>
5
+ <meta charset="utf-8" />
6
+ </head>
7
+ <body></body>
8
+ </html>