-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathutils.js
61 lines (54 loc) · 1.66 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* global fetch */
import base64url from 'base64url'
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
export function checkNoteIdValid (id) {
const result = id.match(uuidRegex)
return !!(result && result.length === 1)
}
export function encodeNoteId (id) {
// remove dashes in UUID and encode in url-safe base64
const str = id.replace(/-/g, '')
const hexStr = Buffer.from(str, 'hex')
return base64url.encode(hexStr)
}
export function decodeNoteId (encodedId) {
// decode from url-safe base64
const id = base64url.toBuffer(encodedId).toString('hex')
// add dashes between the UUID string parts
const idParts = []
idParts.push(id.substr(0, 8))
idParts.push(id.substr(8, 4))
idParts.push(id.substr(12, 4))
idParts.push(id.substr(16, 4))
idParts.push(id.substr(20, 12))
return idParts.join('-')
}
/**
* sanitize url to prevent XSS
* @see {@link https://github.com/braintree/sanitize-url/issues/52#issue-1593777166}
*
* @param {string} rawUrl
* @returns {string} sanitized url
*/
export function sanitizeUrl (rawUrl) {
try {
const url = new URL(rawUrl)
if (url.protocol === 'http:' || url.protocol === 'https:') {
return url.toString()
}
throw new Error('Invalid protocol')
} catch (error) {
return 'about:blank'
}
}
// Check if URL is a PDF based on Content-Type header
export async function isPdfUrl (url) {
try {
const response = await fetch(url, { method: 'HEAD' })
const contentType = response.headers.get('Content-Type')
return contentType === 'application/pdf'
} catch (error) {
console.warn('Error checking PDF content type:', error)
return false
}
}