Skip to content

Commit 203c3c8

Browse files
authored
Merge pull request #156 from vim-denops/fix-stdhash
💪 Use web crypto API instead of deprecated std/hash
2 parents 8619dee + b6a7fd1 commit 203c3c8

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

denops_std/helper/load.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Denops } from "https://deno.land/x/denops_core@v3.2.0/mod.ts";
22
import * as fs from "https://deno.land/std@0.160.0/fs/mod.ts";
3-
import * as hash from "https://deno.land/std@0.160.0/hash/mod.ts";
43
import * as path from "https://deno.land/std@0.160.0/path/mod.ts";
54
import { execute } from "./execute.ts";
65

@@ -39,7 +38,7 @@ async function ensureLocalFile(url: URL): Promise<string> {
3938
return path.fromFileUrl(url);
4039
}
4140
const cacheDir = await getOrCreateCacheDir();
42-
const filename = getLocalFilename(url);
41+
const filename = await getLocalFilename(url);
4342
const filepath = path.join(cacheDir, filename);
4443
if (await fs.exists(filepath)) {
4544
return filepath;
@@ -58,11 +57,16 @@ async function ensureLocalFile(url: URL): Promise<string> {
5857
return filepath;
5958
}
6059

61-
function getLocalFilename(url: URL): string {
62-
const h = hash.createHash("sha256");
63-
h.update(url.href);
60+
async function getLocalFilename(url: URL): Promise<string> {
61+
const buf = await crypto.subtle.digest(
62+
"sha-256",
63+
new TextEncoder().encode(url.href),
64+
);
65+
const h = Array.from(new Uint8Array(buf))
66+
.map((b) => b.toString(16).padStart(2, "0"))
67+
.join("");
6468
const basename = path.basename(url.pathname);
65-
return `${h.toString()}-${basename}`;
69+
return `${h}-${basename}`;
6670
}
6771

6872
async function getOrCreateCacheDir(): Promise<string> {

0 commit comments

Comments
 (0)