-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathvalidate.ts
177 lines (156 loc) · 5.38 KB
/
validate.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import type { Result, Validity } from './types'
import { getDocumentFromString } from './utils'
import { AssetElementMap } from './massPreview/utils'
type HtmlContentValidationResult = {
localP5jsUsed: boolean
titlePresent: boolean
correctTitle: boolean
title: string
externalResourcesNotUsed: boolean
}
type InnerValidity = Pick<
Validity,
| 'canvasSize'
| 'localP5jsUsed'
| 'validTitle'
| 'kodaRendererUsed'
| 'resizerUsed'
| 'usesHashParam'
| 'title'
| 'externalResourcesNotUsed'
>
const constants = {
canvasRegex: /createCanvas\(([^,]+?),\s*([^\s,]+?)(,\s*WEBGL)?\)/g,
getUrlParamsRegex: /\b(const|let|var)\s+(\w+)\s*=\s*getURLParams\(\)\s*/,
urlSearchParamsRegex:
/\b(const|let|var)\s+(\w+)\s*=\s*new URLSearchParams\(window.location.search\)\s*/,
localP5JsRegex: /<script.*src="(?!http)([^"]*p5[^"]*\.js)"/,
titleTagRegex: /<title>(.*?)<\/title>/,
kodaRendererRegex: /kodahash\/render\/completed/,
kodaRendererCalledRegex: /(?<!function\s)postMessageKoda\s*\(/,
resizerRegex: /resizeCanvas\(/,
disallowedTitle: 'KodaHash',
}
const validateCanvasCreation = (
sketchFileContent: string,
): Result<RegExpExecArray> => {
// Create a new regex instance to avoid issues with lastIndex when using the global flag.
const canvasMatch = new RegExp(constants.canvasRegex).exec(sketchFileContent)
if (!canvasMatch) {
return { isSuccess: false, error: 'createCanvas function not found.' }
}
return { isSuccess: true, value: canvasMatch }
}
const validateGetURLParamsUsage = (
sketchFileContent: string,
): Result<RegExpExecArray> => {
const match = constants.getUrlParamsRegex.exec(sketchFileContent)
return match
? { isSuccess: true, value: match }
: { isSuccess: false, error: 'getURLParams() usage not found.' }
}
const validateURLSearchParamsUsage = (
sketchFileContent: string,
): Result<RegExpExecArray> => {
const match = constants.urlSearchParamsRegex.exec(sketchFileContent)
if (match) {
// Check if the 'hash' parameter is accessed using the captured variable name.
const hashAccessMatch = new RegExp(
`${match[2]}.get\\(['"\`]hash['"\`]\\)`,
).test(sketchFileContent)
return hashAccessMatch
? { isSuccess: true, value: match }
: {
isSuccess: false,
error: 'URLSearchParams used but \'hash\' parameter not accessed.',
}
}
return { isSuccess: false, error: 'URLSearchParams usage not found.' }
}
const validateURLParamsUsage = (
sketchFileContent: string,
): Result<RegExpExecArray> => {
const result = validateGetURLParamsUsage(sketchFileContent)
return result.isSuccess
? result
: validateURLSearchParamsUsage(sketchFileContent)
}
const doesNotUseExternalResources = (htmlFileContent: string): boolean => {
const doc = getDocumentFromString(htmlFileContent)
const extenal: boolean[] = []
Object.keys(AssetElementMap).forEach((asset) => {
doc.querySelectorAll(AssetElementMap[asset].tag).forEach((tag) => {
extenal.push(
!tag[AssetElementMap[asset].src].includes(window.location.hostname),
)
})
})
return !extenal.some(Boolean)
}
const validateHtmlContent = (
htmlFileContent: string,
): HtmlContentValidationResult => {
const localP5jsUsed = constants.localP5JsRegex.test(htmlFileContent)
const titleTagMatch = constants.titleTagRegex.exec(htmlFileContent)
const titlePresent = Boolean(titleTagMatch)
const title = titleTagMatch ? titleTagMatch[1].trim() : '-'
const correctTitle = title !== '-' && title !== constants.disallowedTitle
const externalResourcesNotUsed = doesNotUseExternalResources(htmlFileContent)
return {
localP5jsUsed,
titlePresent,
correctTitle,
title,
externalResourcesNotUsed,
}
}
const validateSketchContent = (
sketchFileContent: string,
canvasMatch: RegExpExecArray,
): Pick<
Validity,
| 'canvasSize'
| 'localP5jsUsed'
| 'validTitle'
| 'kodaRendererUsed'
| 'resizerUsed'
| 'usesHashParam'
> => {
const width = canvasMatch[1].trim()
const height = canvasMatch[2].trim()
const isNumericWidth = /^\d+$/.test(width)
const isNumericHeight = /^\d+$/.test(height)
const canvasSize
= isNumericWidth && isNumericHeight ? `${width} X ${height}` : 'Dynamic'
return {
canvasSize,
localP5jsUsed: false, // This will be set based on HTML content checks
validTitle: false, // This will be updated after HTML content checks
kodaRendererUsed: constants.kodaRendererRegex.test(sketchFileContent) && constants.kodaRendererCalledRegex.test(sketchFileContent),
resizerUsed: constants.resizerRegex.test(sketchFileContent),
usesHashParam: validateURLParamsUsage(sketchFileContent).isSuccess,
}
}
export const validate = (
htmlFileContent: string,
sketchFileContent: string,
): Result<InnerValidity> => {
const canvasResult = validateCanvasCreation(sketchFileContent)
if (!canvasResult.isSuccess) {
return canvasResult
}
const htmlValidationResult = validateHtmlContent(htmlFileContent)
const partialValidity = validateSketchContent(
sketchFileContent,
canvasResult.value,
)
const validity: InnerValidity = {
...partialValidity,
localP5jsUsed: htmlValidationResult.localP5jsUsed,
validTitle:
htmlValidationResult.titlePresent && htmlValidationResult.correctTitle,
title: htmlValidationResult.title,
externalResourcesNotUsed: htmlValidationResult.externalResourcesNotUsed,
}
return { isSuccess: true, value: validity }
}