@primate/htmx 0.5.0 → 0.6.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/package.json +1 -1
- package/src/module.js +20 -31
package/package.json
CHANGED
package/src/module.js
CHANGED
|
@@ -1,20 +1,6 @@
|
|
|
1
|
-
import {Path} from "runtime-compat/
|
|
2
|
-
import crypto from "runtime-compat/crypto";
|
|
1
|
+
import {Path} from "runtime-compat/fs";
|
|
3
2
|
|
|
4
|
-
const
|
|
5
|
-
const scriptPath = ["node_modules", "htmx.org", "dist", fileName];
|
|
6
|
-
|
|
7
|
-
const hash = async (string, algorithm = "sha-384") => {
|
|
8
|
-
const buffer = new ArrayBuffer(string.length);
|
|
9
|
-
const view = new Uint8Array(buffer);
|
|
10
|
-
for (let i = 0; i < string.length; i++) {
|
|
11
|
-
view[i] = string.charCodeAt(i);
|
|
12
|
-
}
|
|
13
|
-
const bytes = await crypto.subtle.digest(algorithm, buffer);
|
|
14
|
-
return btoa(String.fromCharCode(...new Uint8Array(bytes)));
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const getContent = async (env, name) => {
|
|
3
|
+
const loadComponent = async (env, name) => {
|
|
18
4
|
try {
|
|
19
5
|
return await env.paths.components.join(`${name}.html`).file.read();
|
|
20
6
|
} catch (error) {
|
|
@@ -22,16 +8,19 @@ const getContent = async (env, name) => {
|
|
|
22
8
|
}
|
|
23
9
|
};
|
|
24
10
|
|
|
25
|
-
const
|
|
11
|
+
const getContent = (env, {adhoc, content}) => {
|
|
12
|
+
return adhoc ? content : loadComponent(env, content);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const getBody = async (env, {adhoc, partial, content}) => {
|
|
16
|
+
const body = await getContent(env, {adhoc, content});
|
|
17
|
+
return partial ? body : env.render({body});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const htmx = (content, {status = 200, partial = false, adhoc = false} = {}) =>
|
|
26
21
|
async (env, headers) => {
|
|
27
|
-
const src = `/${fileName}`;
|
|
28
|
-
const htmx = env.paths.public.join(fileName);
|
|
29
|
-
const integrity = `sha384-${await hash(await htmx.file.read())}`;
|
|
30
|
-
const head = `<script src="${src}" integrity="${integrity}"></script>`;
|
|
31
|
-
const body = adhoc ? content : await getContent(env, content);
|
|
32
|
-
const scriptSrc = `script-src 'self' '${integrity}';`;
|
|
33
22
|
const styleSrc = "style-src 'unsafe-inline';";
|
|
34
|
-
const csp = `${headers["Content-Security-Policy"]}${
|
|
23
|
+
const csp = `${headers["Content-Security-Policy"]}${styleSrc}`;
|
|
35
24
|
const options = {
|
|
36
25
|
status,
|
|
37
26
|
headers: {
|
|
@@ -41,20 +30,20 @@ const handler = (content, {status = 200, partial = false, adhoc = false} = {}) =
|
|
|
41
30
|
},
|
|
42
31
|
};
|
|
43
32
|
|
|
44
|
-
return [
|
|
33
|
+
return [await getBody(env, {adhoc, partial, content}), options];
|
|
45
34
|
};
|
|
46
35
|
|
|
47
36
|
export default () => () => {
|
|
48
37
|
return {
|
|
49
38
|
register: async (env, next) => {
|
|
50
|
-
env.register("htmx",
|
|
39
|
+
env.register("htmx", htmx);
|
|
51
40
|
return next(env);
|
|
52
41
|
},
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
42
|
+
publish: async (env, next) => {
|
|
43
|
+
const src = "htmx.min.js";
|
|
44
|
+
const path = ["node_modules", "htmx.org", "dist", src];
|
|
45
|
+
const code = await Path.resolve().join(...path).file.read();
|
|
46
|
+
await env.publish({src, code});
|
|
58
47
|
return next(env);
|
|
59
48
|
},
|
|
60
49
|
};
|