Skip to content

Commit

Permalink
fix: avoid using native crypto
Browse files Browse the repository at this point in the history
fix #25
  • Loading branch information
yyx990803 committed May 25, 2022
1 parent 63b8f22 commit c22e216
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/node": "^16.11.12",
"@vitejs/plugin-vue": "^1.9.0",
"codemirror": "^5.62.3",
"hash-sum": "^2.0.0",
"rimraf": "^3.0.2",
"sucrase": "^3.20.1",
"typescript": "^4.5.4",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
CompilerOptions
} from 'vue/compiler-sfc'
import { transform } from 'sucrase'
// @ts-ignore
import hashId from 'hash-sum'

export const COMP_IDENTIFIER = `__sfc__`

Expand Down Expand Up @@ -48,7 +50,7 @@ export async function compileFile(
return
}

const id = await hashId(filename)
const id = hashId(filename)
const { errors, descriptor } = store.compiler.parse(code, {
filename,
sourceMap: true
Expand Down Expand Up @@ -308,11 +310,3 @@ async function doCompileTemplate(

return code
}

async function hashId(filename: string) {
const msgUint8 = new TextEncoder().encode(filename) // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8) // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)) // convert buffer to byte array
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('') // convert bytes to hex string
return hashHex.slice(0, 8)
}

0 comments on commit c22e216

Please sign in to comment.