musescore-downloader/src/utils.ts

80 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-11-03 19:13:06 +00:00
2020-11-24 10:02:14 +00:00
import isNodeJs from 'detect-node'
2020-05-18 22:44:45 +00:00
2020-11-24 22:35:44 +00:00
export const escapeFilename = (s: string): string => {
return s.replace(/[\s<>:{}"/\\|?*~.\0\cA-\cZ]+/g, '_')
}
2020-05-17 22:57:28 +00:00
export const getIndexPath = (id: number): string => {
const idStr = String(id)
// 获取最后三位,倒序排列
// x, y, z are the reversed last digits of the score id. Example: id 123456789, x = 9, y = 8, z = 7
// https://developers.musescore.com/#/file-urls
// "5449062" -> ["2", "6", "0"]
const indexN = idStr.split('').reverse().slice(0, 3)
return indexN.join('/')
2019-11-03 19:13:06 +00:00
}
2019-11-29 23:07:43 +00:00
2020-11-24 10:02:14 +00:00
export const getFetch = (): typeof fetch => {
if (!isNodeJs) {
return fetch
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return require('node-fetch')
}
}
2020-10-20 05:39:38 +00:00
export const fetchData = async (url: string, init?: RequestInit): Promise<Uint8Array> => {
const r = await fetch(url, init)
const data = await r.arrayBuffer()
return new Uint8Array(data)
}
2020-11-21 05:38:10 +00:00
export const assertRes = (r: Response): void => {
if (!r.ok) throw new Error(`${r.url} ${r.status} ${r.statusText}`)
}
export const useTimeout = async <T> (promise: T | Promise<T>, ms: number): Promise<T> => {
if (!(promise instanceof Promise)) {
return promise
}
return new Promise((resolve, reject) => {
const i = setTimeout(() => {
reject(new Error('timeout'))
}, ms)
promise.then(resolve, reject).finally(() => clearTimeout(i))
})
}
2020-11-26 02:06:35 +00:00
export const getUnsafeWindow = (): Window => {
// eslint-disable-next-line no-eval
return window.eval('window') as Window
2020-11-13 20:55:41 +00:00
}
2020-11-26 04:57:39 +00:00
export const console: Console = (window || global).console // Object.is(window.console, unsafeWindow.console) == false
2020-11-23 19:37:11 +00:00
2020-11-13 20:55:41 +00:00
export const windowOpen: Window['open'] = (...args): Window | null => {
2020-11-26 02:06:35 +00:00
return window.open(...args) // Object.is(window.open, unsafeWindow.open) == false
}
export const attachShadow = (el: Element): ShadowRoot => {
return Element.prototype.attachShadow.call(el, { mode: 'closed' }) as ShadowRoot
2020-11-13 20:55:41 +00:00
}
2019-11-29 23:07:43 +00:00
export const waitForDocumentLoaded = (): Promise<void> => {
2020-05-17 22:57:28 +00:00
if (document.readyState !== 'complete') {
return new Promise(resolve => {
2020-10-23 14:46:45 +00:00
const cb = () => {
2020-05-17 22:57:28 +00:00
if (document.readyState === 'complete') {
resolve()
2020-10-23 14:46:45 +00:00
document.removeEventListener('readystatechange', cb)
2020-05-17 22:57:28 +00:00
}
2020-10-23 14:46:45 +00:00
}
document.addEventListener('readystatechange', cb)
2020-05-17 22:57:28 +00:00
})
} else {
return Promise.resolve()
}
2019-11-29 23:07:43 +00:00
}