@vnejs/helpers.hash-sha256 0.2.1

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.
@@ -0,0 +1 @@
1
+ export declare const hashSha256: (value: unknown, maxLength?: number) => Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ const canonicalize = (value) => {
2
+ if (value === null || typeof value !== "object")
3
+ return value;
4
+ if (Array.isArray(value))
5
+ return value.map(canonicalize);
6
+ return Object.keys(value)
7
+ .sort()
8
+ .reduce((acc, key) => {
9
+ acc[key] = canonicalize(value[key]);
10
+ return acc;
11
+ }, {});
12
+ };
13
+ const toHex = (buffer) => [...new Uint8Array(buffer)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
14
+ export const hashSha256 = async (value, maxLength) => {
15
+ const bytes = new TextEncoder().encode(JSON.stringify(canonicalize(value)));
16
+ const digest = await crypto.subtle.digest("SHA-256", bytes);
17
+ const hex = toHex(digest);
18
+ return maxLength === undefined ? hex : hex.slice(0, maxLength);
19
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@vnejs/helpers.hash-sha256",
3
+ "version": "0.2.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "src",
10
+ "tsconfig.json"
11
+ ],
12
+ "scripts": {
13
+ "test": "npx @vnejs/monorepo test",
14
+ "build": "npx @vnejs/monorepo package",
15
+ "publish:major:plugin": "npm run publish:major",
16
+ "publish:minor:plugin": "npm run publish:minor",
17
+ "publish:patch:plugin": "npm run publish:patch",
18
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
19
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
20
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
21
+ },
22
+ "author": "",
23
+ "license": "ISC",
24
+ "devDependencies": {
25
+ "@vnejs/configs.ts-common": "~0.2.0",
26
+ "@vnejs/configs.vitest": "~0.2.0"
27
+ }
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ const canonicalize = (value: unknown): unknown => {
2
+ if (value === null || typeof value !== "object") return value;
3
+ if (Array.isArray(value)) return value.map(canonicalize);
4
+
5
+ return Object.keys(value as Record<string, unknown>)
6
+ .sort()
7
+ .reduce<Record<string, unknown>>((acc, key) => {
8
+ acc[key] = canonicalize((value as Record<string, unknown>)[key]);
9
+ return acc;
10
+ }, {});
11
+ };
12
+
13
+ const toHex = (buffer: ArrayBuffer) =>
14
+ [...new Uint8Array(buffer)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
15
+
16
+ export const hashSha256 = async (value: unknown, maxLength?: number) => {
17
+ const bytes = new TextEncoder().encode(JSON.stringify(canonicalize(value)));
18
+ const digest = await crypto.subtle.digest("SHA-256", bytes);
19
+ const hex = toHex(digest);
20
+
21
+ return maxLength === undefined ? hex : hex.slice(0, maxLength);
22
+ };
@@ -0,0 +1,24 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { hashSha256 } from "../index.js";
4
+
5
+ describe("hashSha256", () => {
6
+ it("returns the same hash for objects with different key order", async () => {
7
+ const left = await hashSha256({ a: 1, b: 2 });
8
+ const right = await hashSha256({ b: 2, a: 1 });
9
+
10
+ expect(left).toBe(right);
11
+ expect(left).toHaveLength(64);
12
+ });
13
+
14
+ it("returns a different hash for different values", async () => {
15
+ await expect(hashSha256({ a: 1 })).resolves.not.toBe(await hashSha256({ a: 2 }));
16
+ });
17
+
18
+ it("truncates hash to maxLength", async () => {
19
+ const hash = await hashSha256({ a: 1 }, 8);
20
+
21
+ expect(hash).toHaveLength(8);
22
+ expect(await hashSha256({ a: 1 })).toMatch(new RegExp(`^${hash}`));
23
+ });
24
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": [
8
+ "src/**/*.ts"
9
+ ],
10
+ "exclude": [
11
+ "dist",
12
+ "node_modules",
13
+ "src/tests"
14
+ ]
15
+ }