Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No crypto subtle #295

Merged
merged 3 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## In progress

- Replace crypto.subtle with sha.js for ID hashing ([#295](https://github.com/ben/foundry-ironsworn/pull/295))

## 1.10.42

- Reload the browser when SF beta flag is changed
Expand Down
103 changes: 103 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"dependencies": {
"@types/lodash": "^4.14.170",
"@types/marked": "^4.0.3",
"buffer": "^6.0.3",
"dataforged": "^0.4",
"gulp": "^4.0.2",
"gulp-less": "^5.0.0",
"lodash": "^4.17.21",
"sha.js": "^2.4.11",
"vue2-editor": "^2.10.3"
},
"devDependencies": {
Expand Down
22 changes: 11 additions & 11 deletions src/module/dataforged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cloneDeep, get, isArray, isObject, max, set } from 'lodash'
import { data as Dataforged, IMove, IOracle, IOracleCategory } from 'dataforged'
import { marked } from 'marked'
import { IronswornItem } from './item/item'
import shajs from 'sha.js'

function getLegacyRank(numericRank) {
switch (numericRank) {
Expand Down Expand Up @@ -43,24 +44,23 @@ export function cleanDollars(obj): any {
}

const HASH_CACHE = {} as { [k: string]: string }
export async function hashLookup(str: string): Promise<string> {
return (HASH_CACHE[str] ||= await hash(str))
export function hashLookup(str: string): string {
HASH_CACHE[str] ||= hash(str)
return HASH_CACHE[str]
}

async function hash(str: string): Promise<string> {
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str))
const hexarr = Array.prototype.map.call(new Uint8Array(buf), (x) => ('00' + x.toString(16)).slice(-2))
return hexarr.join('').substring(48)
function hash(str: string): string {
return shajs('sha256').update(str).digest('hex').substring(48)
}

export async function getFoundryTableByDfId(dfid: string): Promise<RollTable | undefined> {
const pack = game.packs.get('foundry-ironsworn.starforgedoracles')
return (await pack?.getDocument(await hashLookup(dfid))) || undefined
return (await pack?.getDocument(hashLookup(dfid))) || undefined
}

export async function getFoundryMoveByDfId(dfid: string): Promise<IronswornItem | undefined> {
const pack = game.packs.get('foundry-ironsworn.starforgedmoves')
return pack?.get(await hashLookup(dfid)) as any
return (await pack?.getDocument(hashLookup(dfid))) as IronswornItem | undefined
}

export async function getDFMoveByDfId(dfid: string): Promise<IMove | undefined> {
Expand All @@ -72,14 +72,14 @@ export async function getDFMoveByDfId(dfid: string): Promise<IMove | undefined>
return undefined
}

async function generateIdMap(data: typeof Dataforged): Promise<{ [key: string]: string }> {
function generateIdMap(data: typeof Dataforged): { [key: string]: string } {
const ret = {}

const nodeStack = cloneDeep(Object.values(data)) as any[]
while (nodeStack.length) {
const node = nodeStack.pop()
if (node?.$id) {
ret[node.$id] = await hash(node.$id)
ret[node.$id] = hash(node.$id)
}
if (isArray(node)) {
nodeStack.push(...node.reverse())
Expand Down Expand Up @@ -137,7 +137,7 @@ export async function importFromDataforged() {
await Item.deleteDocuments(idsToDelete, { pack: key })
}

const idMap = await generateIdMap(Dataforged)
const idMap = generateIdMap(Dataforged)

// Moves
const movesToCreate = [] as (ItemDataConstructorData & Record<string, unknown>)[]
Expand Down