@flowscripter/flowscripter-io-cli 1.0.1 → 1.1.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/bun.lock CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "lockfileVersion": 1,
2
+ "lockfileVersion": 2,
3
3
  "configVersion": 1,
4
4
  "workspaces": {
5
5
  "": {
@@ -8,6 +8,7 @@
8
8
  "@flowscripter/dynamic-cli-framework": "^5.3.4",
9
9
  "@flowscripter/dynamic-cli-framework-api": "^2.3.2",
10
10
  "@flowscripter/dynamic-plugin-framework": "^2.2.4",
11
+ "@flowscripter/flowscripter-io-cli-hash-native": "^1.0.0",
11
12
  "@flowscripter/pluggable-io-framework": "^1.1.0",
12
13
  "@flowscripter/pluggable-io-framework-api": "^1.0.4",
13
14
  },
@@ -30,6 +31,8 @@
30
31
 
31
32
  "@flowscripter/dynamic-plugin-framework": ["@flowscripter/dynamic-plugin-framework@2.2.4", "", { "dependencies": { "semver": "^7.8.5" }, "peerDependencies": { "typescript": "^7.0.2" } }, "sha512-OZ2a4j6OBzU8QiP3aQ8zeUmb5c9JJdRXXTGRmzlhhZEanpDcHsUxYa8mDUJABO3ThG2JoKjzmD8/K2b0xT5RSQ=="],
32
33
 
34
+ "@flowscripter/flowscripter-io-cli-hash-native": ["@flowscripter/flowscripter-io-cli-hash-native@1.0.0", "", { "peerDependencies": { "typescript": "^6.0.3" } }, "sha512-Tx5zaGnqQDHKo209qmaqPGIjXA0pcY7I3o4JnheMoKFF894Gtq0l1nqA8sEQ70lFwIoQN+oD40mGFrtUSwv1wg=="],
35
+
33
36
  "@flowscripter/io-plugin-filesystem": ["@flowscripter/io-plugin-filesystem@1.0.2", "", { "dependencies": { "@flowscripter/pluggable-io-framework-api": "^1.0.1", "zod": "^4.4.3" }, "peerDependencies": { "@flowscripter/dynamic-plugin-framework": "*", "typescript": "^6.0.3" } }, "sha512-xM0ON9vap/GXCjyaf+uYemi+Ly0nwnZv0mocUtwwk0PFZAYPc5o0aHFQT4PWhoB6DhW3wykb4ey8vGoTo3hz2w=="],
34
37
 
35
38
  "@flowscripter/pluggable-io-framework": ["@flowscripter/pluggable-io-framework@1.1.0", "", { "dependencies": { "@flowscripter/dynamic-plugin-framework": "^2.2.0", "@flowscripter/pluggable-io-framework-api": "^1.0.4" }, "peerDependencies": { "typescript": "^6.0.3" } }, "sha512-GzHk94WOZFMaLkxyLjimCuL+lhCpEn/B5LNBFthxS8rfZEYSdlpEEUkJkMPSru0Va7FDKw5UFtoZMWt3ePElFw=="],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowscripter/flowscripter-io-cli",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Example CLI using https://github.com/flowscripter/pluggable-io-framework",
5
5
  "keywords": [
6
6
  "bun",
@@ -24,6 +24,7 @@
24
24
  "@flowscripter/dynamic-cli-framework": "^5.3.4",
25
25
  "@flowscripter/dynamic-cli-framework-api": "^2.3.2",
26
26
  "@flowscripter/dynamic-plugin-framework": "^2.2.4",
27
+ "@flowscripter/flowscripter-io-cli-hash-native": "^1.0.0",
27
28
  "@flowscripter/pluggable-io-framework": "^1.1.0",
28
29
  "@flowscripter/pluggable-io-framework-api": "^1.0.4"
29
30
  },
@@ -7,6 +7,7 @@ import {
7
7
  ValueTypeName,
8
8
  } from "@flowscripter/dynamic-cli-framework";
9
9
  import { ChunkKind, type ChunkRef } from "@flowscripter/pluggable-io-framework-api";
10
+ import { Sha256Hasher } from "@flowscripter/flowscripter-io-cli-hash-native";
10
11
  import { getFilesystemProvider } from "../filesystemProvider.ts";
11
12
 
12
13
  const hash: SubCommand = {
@@ -38,23 +39,57 @@ const hash: SubCommand = {
38
39
  try {
39
40
  const handle = await provider.getReadableStream(path);
40
41
  const reader = (handle.stream as ReadableStream<ChunkRef>).getReader();
41
- const hasher = new Bun.CryptoHasher(
42
- algorithm as ConstructorParameters<typeof Bun.CryptoHasher>[0],
43
- );
44
- for (;;) {
45
- const { done, value } = await reader.read();
46
- if (done) break;
47
- if (value.kind === ChunkKind.Js) {
48
- hasher.update(value.data);
49
- } else {
50
- throw new Error("hash command only supports js-kind chunks in this version");
51
- }
52
- }
53
- await printerService.print(`${hasher.digest("hex")} ${path}\n`);
42
+ const digestHex =
43
+ algorithm === "sha256"
44
+ ? await hashWithNativeSha256(reader)
45
+ : await hashWithBunCryptoHasher(reader, algorithm);
46
+ await printerService.print(`${digestHex} ${path}\n`);
54
47
  } finally {
55
48
  await provider[Symbol.asyncDispose]();
56
49
  }
57
50
  },
58
51
  };
59
52
 
53
+ function toHex(bytes: Uint8Array): string {
54
+ return Array.from(bytes)
55
+ .map((byte) => byte.toString(16).padStart(2, "0"))
56
+ .join("");
57
+ }
58
+
59
+ async function hashWithNativeSha256(
60
+ reader: ReadableStreamDefaultReader<ChunkRef>,
61
+ ): Promise<string> {
62
+ const hasher = new Sha256Hasher();
63
+ for (;;) {
64
+ const { done, value } = await reader.read();
65
+ if (done) break;
66
+ if (value.kind === ChunkKind.Js) {
67
+ hasher.update(value.data);
68
+ } else {
69
+ hasher.updatePointer(value.ptr, value.length);
70
+ value.release();
71
+ }
72
+ }
73
+ return toHex(hasher.final());
74
+ }
75
+
76
+ async function hashWithBunCryptoHasher(
77
+ reader: ReadableStreamDefaultReader<ChunkRef>,
78
+ algorithm: string,
79
+ ): Promise<string> {
80
+ const hasher = new Bun.CryptoHasher(
81
+ algorithm as ConstructorParameters<typeof Bun.CryptoHasher>[0],
82
+ );
83
+ for (;;) {
84
+ const { done, value } = await reader.read();
85
+ if (done) break;
86
+ if (value.kind === ChunkKind.Js) {
87
+ hasher.update(value.data);
88
+ } else {
89
+ throw new Error(`hash command only supports js-kind chunks for algorithm "${algorithm}"`);
90
+ }
91
+ }
92
+ return hasher.digest("hex");
93
+ }
94
+
60
95
  export default hash;
@@ -123,7 +123,7 @@ describe("move", () => {
123
123
  });
124
124
 
125
125
  describe("hash", () => {
126
- test("hashes a file with the default algorithm", async () => {
126
+ test("hashes a file with the default algorithm using the native hasher", async () => {
127
127
  await writeFile(join(root, "a.txt"), "hello");
128
128
  const { lines, context } = createStubContext();
129
129
 
@@ -132,4 +132,14 @@ describe("hash", () => {
132
132
  const expected = new Bun.CryptoHasher("sha256").update("hello").digest("hex");
133
133
  expect(lines[0]).toBe(`${expected} ${join(root, "a.txt")}\n`);
134
134
  });
135
+
136
+ test("hashes a file with a non-sha256 algorithm via the Bun.CryptoHasher fallback", async () => {
137
+ await writeFile(join(root, "a.txt"), "hello");
138
+ const { lines, context } = createStubContext();
139
+
140
+ await hash.execute(context, { path: join(root, "a.txt"), algorithm: "sha1" });
141
+
142
+ const expected = new Bun.CryptoHasher("sha1").update("hello").digest("hex");
143
+ expect(lines[0]).toBe(`${expected} ${join(root, "a.txt")}\n`);
144
+ });
135
145
  });