@primate/htmx 0.3.1 → 0.4.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/exports.js CHANGED
@@ -1 +1 @@
1
- export {default} from "./src/handler.js";
1
+ export {default, partial} from "./src/handler.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/htmx",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "author": "Terrablue <terrablue@proton.me>",
5
5
  "homepage": "https://primatejs.com/modules/htmx",
6
6
  "bugs": "https://github.com/primatejs/primate-htmx/issues",
package/src/handler.js CHANGED
@@ -16,7 +16,8 @@ const getIndex = async env => {
16
16
  }
17
17
  };
18
18
 
19
- const scriptPath = ["node_modules", "htmx.org", "dist", "htmx.min.js"];
19
+ const fileName = "htmx.min.js";
20
+ const scriptPath = ["node_modules", "htmx.org", "dist", fileName];
20
21
 
21
22
  const hash = async (string, algorithm = "sha-384") => {
22
23
  const buffer = new ArrayBuffer(string.length);
@@ -25,36 +26,43 @@ const hash = async (string, algorithm = "sha-384") => {
25
26
  view[i] = string.charCodeAt(i);
26
27
  }
27
28
  const bytes = await crypto.subtle.digest(algorithm, buffer);
28
- return Buffer.from(bytes).toString("base64");
29
+ return btoa(String.fromCharCode(...new Uint8Array(bytes)));
29
30
  };
30
31
 
32
+ const render = async (strings, keys, path) => {
33
+ const loadFile = async file => [file.base, (await file.read())
34
+ .replaceAll("\n", "")];
35
+ const components = await path.exists
36
+ ? Object.fromEntries(await Promise.all((
37
+ await File.collect(`${path}`, ".html")).map(loadFile)))
38
+ : {};
39
+ const re = strings
40
+ .slice(0, last)
41
+ .map((string, i) => `${string}\${${i}}`)
42
+ .join("") + strings[strings.length + last];
43
+ return flatten(await fulfill(re, components, await Promise.all(keys)));
44
+ };
45
+
46
+ export const partial = (strings, ...keys) => async (env, headers) =>
47
+ // -> spread into new Response()
48
+ [await render(strings, keys, env.paths.components), {status: 200, headers}];
49
+
31
50
  export default (strings, ...keys) => async (env, headers) => {
32
- // if doesn't exist, copy htmx.js to public
51
+ // if doesn't exist, copy htmx file to public
33
52
  // if public doesn't exist, create it
34
53
  const _public = env.paths.public;
35
54
  if (!await _public.exists) {
36
55
  await _public.file.create();
37
56
  }
38
- const htmx = _public.join("htmx.js");
57
+ const htmx = _public.join(fileName);
39
58
  if (!await htmx.exists) {
40
59
  await Path.resolve().join(...scriptPath).file.copy(`${htmx}`);
41
60
  }
42
- const src = "/htmx.js";
61
+ const src = `/${fileName}`;
43
62
  const integrity = `sha384-${await hash(await htmx.file.read())}`;
44
63
  const script = `<script src="${src}" integrity="${integrity}"></script>`;
45
64
  const index = (await getIndex(env)).replace("</head>", `${script}</head>`);
46
- const {paths: {components: path}} = env;
47
- const loadFile = async file => [file.base, (await file.read())
48
- .replaceAll("\n", "")];
49
- const components = await path.exists
50
- ? Object.fromEntries(await Promise.all((
51
- await File.collect(`${path}`, ".html")).map(loadFile)))
52
- : {};
53
- const re = strings
54
- .slice(0, last)
55
- .map((string, i) => `${string}\${${i}}`)
56
- .join("") + strings[strings.length + last];
57
- const html = flatten(await fulfill(re, components, await Promise.all(keys)));
65
+ const html = await render(strings, keys, env.paths.components);
58
66
  const body = index.replace("<body>", () => `<body>${html}`);
59
67
 
60
68
  const script_src = `script-src 'self' '${integrity}';`;