hx-ext-amz-content-sha256 1.0.3 → 1.0.4

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 (3) hide show
  1. package/main.js +5 -2
  2. package/min.js +1 -1
  3. package/package.json +1 -1
package/main.js CHANGED
@@ -45,9 +45,12 @@ htmx.defineExtension("amz-content-sha256", {
45
45
 
46
46
  // Ensure the target is a valid HTMLFormElement
47
47
  if (form instanceof HTMLFormElement) {
48
- // Create a FormData object from the form, and convert it into a URLSearchParams string
48
+ // Create a FormData object from the form, and serialize using encodeURIComponent
49
+ // to match HTMX's internal encoding (URLSearchParams encodes differently)
49
50
  const formData = new FormData(form);
50
- const data = new URLSearchParams(formData).toString();
51
+ const data = Array.from(formData.entries())
52
+ .map(([k, v]) => encodeURIComponent(k) + '=' + encodeURIComponent(v))
53
+ .join('&');
51
54
 
52
55
  // Calculate the SHA-256 hash of the form data
53
56
  const sha256Hash = await this.calculateSHA256(data);
package/min.js CHANGED
@@ -1 +1 @@
1
- htmx.defineExtension("amz-content-sha256",{init:function(){return this.RequestHashQueue={},this.calculateSHA256=async e=>{const t=(new TextEncoder).encode(e),a=await crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(a)).map((e=>e.toString(16).padStart(2,"0"))).join("")},null},onEvent:async function(e,t){switch(e){case"htmx:confirm":if("post"==t.detail.verb||"put"==t.detail.verb){t.preventDefault();const e=t.target;if(e instanceof HTMLFormElement){const a=new FormData(e),s=new URLSearchParams(a).toString(),i=await this.calculateSHA256(s);this.RequestHashQueue[t.detail.path]?this.RequestHashQueue[t.detail.path].push(i):this.RequestHashQueue[t.detail.path]=[i]}t.detail.issueRequest()}break;case"htmx:configRequest":"post"!=t.detail.verb&&"put"!=t.detail.verb||this.RequestHashQueue[t.detail.path].length>0&&(t.detail.headers["x-amz-content-sha256"]=this.RequestHashQueue[t.detail.path].pop())}}});
1
+ htmx.defineExtension("amz-content-sha256",{init:function(){return this.RequestHashQueue={},this.calculateSHA256=async e=>{const t=(new TextEncoder).encode(e),a=await crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(a)).map((e=>e.toString(16).padStart(2,"0"))).join("")},null},onEvent:async function(e,t){switch(e){case"htmx:confirm":if("post"==t.detail.verb||"put"==t.detail.verb){t.preventDefault();const e=t.target;if(e instanceof HTMLFormElement){const a=new FormData(e),s=Array.from(a.entries()).map((([e,t])=>encodeURIComponent(e)+"="+encodeURIComponent(t))).join("&"),i=await this.calculateSHA256(s);this.RequestHashQueue[t.detail.path]?this.RequestHashQueue[t.detail.path].push(i):this.RequestHashQueue[t.detail.path]=[i]}t.detail.issueRequest()}break;case"htmx:configRequest":"post"!=t.detail.verb&&"put"!=t.detail.verb||this.RequestHashQueue[t.detail.path].length>0&&(t.detail.headers["x-amz-content-sha256"]=this.RequestHashQueue[t.detail.path].pop())}}});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hx-ext-amz-content-sha256",
3
3
  "main": "main.js",
4
- "version": "1.0.3",
4
+ "version": "1.0.4",
5
5
  "scripts": {
6
6
  "lint": "eslint test/ext test",
7
7
  "lint-fix": "eslint test/ext test --fix",