@primate/htmx 0.5.1 → 0.7.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.
Files changed (2) hide show
  1. package/package.json +4 -1
  2. package/src/module.js +26 -39
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/htmx",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "description": "Primate HTMX module",
5
5
  "homepage": "https://primatejs.com/modules/htmx",
6
6
  "bugs": "https://github.com/primatejs/htmx/issues",
@@ -13,6 +13,9 @@
13
13
  "devDependencies": {
14
14
  "maximin": "^0.1.2"
15
15
  },
16
+ "peerDependencies": {
17
+ "primate": "0.14.0"
18
+ },
16
19
  "type": "module",
17
20
  "exports": "./exports.js",
18
21
  "engines": {
package/src/module.js CHANGED
@@ -1,20 +1,6 @@
1
1
  import {Path} from "runtime-compat/fs";
2
- import crypto from "runtime-compat/crypto";
3
2
 
4
- const fileName = "htmx.min.js";
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 handler = (content, {status = 200, partial = false, adhoc = false} = {}) =>
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"]}${scriptSrc}${styleSrc}`;
23
+ const csp = `${headers["Content-Security-Policy"]}${styleSrc}`;
35
24
  const options = {
36
25
  status,
37
26
  headers: {
@@ -41,21 +30,19 @@ const handler = (content, {status = 200, partial = false, adhoc = false} = {}) =
41
30
  },
42
31
  };
43
32
 
44
- return [partial ? body : await env.render({head, body}), options];
33
+ return [await getBody(env, {adhoc, partial, content}), options];
45
34
  };
46
35
 
47
- export default () => () => {
48
- return {
49
- register: async (env, next) => {
50
- env.register("htmx", handler);
51
- return next(env);
52
- },
53
- bundle: async (env, next) => {
54
- const htmx = env.paths.public.join(fileName);
55
- if (!await htmx.exists) {
56
- await Path.resolve().join(...scriptPath).file.copy(`${htmx}`);
57
- }
58
- return next(env);
59
- },
60
- };
61
- };
36
+ export default () => ({
37
+ register(env, next) {
38
+ env.register("htmx", htmx);
39
+ return next(env);
40
+ },
41
+ async publish(env, next) {
42
+ const src = "htmx.min.js";
43
+ const path = ["node_modules", "htmx.org", "dist", src];
44
+ const code = await Path.resolve().join(...path).file.read();
45
+ await env.publish({src, code});
46
+ return next(env);
47
+ },
48
+ });