Repository navigation
Expand file tree
/
Copy pathexample-workspace.ts
More file actions
241 lines (212 loc) · 6.45 KB
/
Copy pathexample-workspace.ts
File metadata and controls
241 lines (212 loc) · 6.45 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
export const exampleWorkspaceVersion = 1
export const exampleEnvironmentNames = [
'client',
'react',
'charts',
'charts-react',
'charts-octane',
] as const
export type ExampleEnvironment = (typeof exampleEnvironmentNames)[number]
export type ExampleWorkspace = {
version: typeof exampleWorkspaceVersion
entry: string
environment?: ExampleEnvironment
files: Record<string, string>
binaryFiles?: Record<string, string>
imports?: Record<string, string>
}
export type ExampleRuntime = {
type: 'webcontainer'
compatibility?: 'tanstack-start-async-context'
install: ExampleRuntimeCommand
start: ExampleRuntimeCommand
}
export type ExampleRuntimeCommand = {
command: string
args: Array<string>
}
export type ExampleDefinition = {
id: string
title: string
description?: string
initialFile?: string
hiddenFiles?: ReadonlyArray<string>
runtime?: ExampleRuntime
workspace: ExampleWorkspace
}
export function createExampleWorkspace({
binaryFiles,
entry,
environment,
files,
imports,
}: {
binaryFiles?: Record<string, string>
entry: string
environment?: ExampleEnvironment
files: Record<string, string>
imports?: Record<string, string>
}): ExampleWorkspace {
const normalizedFiles: Record<string, string> = {}
const normalizedBinaryFiles: Record<string, string> = {}
for (const [path, source] of Object.entries(files)) {
normalizedFiles[normalizeExamplePath(path)] = source
}
for (const [path, source] of Object.entries(binaryFiles ?? {})) {
const normalizedPath = normalizeExamplePath(path)
if (normalizedFiles[normalizedPath] !== undefined) {
throw new Error(`Duplicate example file path: ${normalizedPath}`)
}
normalizedBinaryFiles[normalizedPath] = source
}
return {
version: exampleWorkspaceVersion,
entry: normalizeExamplePath(entry),
...(environment ? { environment } : {}),
files: normalizedFiles,
...(Object.keys(normalizedBinaryFiles).length
? { binaryFiles: normalizedBinaryFiles }
: {}),
...(imports ? { imports: { ...imports } } : {}),
}
}
export function normalizeExamplePath(path: string) {
const segments: Array<string> = []
for (const segment of path.split('/')) {
if (!segment || segment === '.') continue
if (segment === '..') {
segments.pop()
continue
}
segments.push(segment)
}
return `/${segments.join('/')}`
}
export function resolveExampleWorkspaceImport(
specifier: string,
imports: Record<string, string> = {},
) {
const key = Object.hasOwn(imports, specifier)
? specifier
: Object.keys(imports)
.filter((key) => key.endsWith('/') && specifier.startsWith(key))
.sort((left, right) => right.length - left.length)[0]
if (key === undefined) return undefined
const target = imports[key]
if (!target?.startsWith('/') || target.startsWith('//')) return undefined
return normalizeExamplePath(target + specifier.slice(key.length))
}
export function serializeExampleWorkspace(workspace: ExampleWorkspace) {
const files = Object.fromEntries(
Object.entries(workspace.files).sort(([left], [right]) =>
left.localeCompare(right),
),
)
const imports = workspace.imports
? Object.fromEntries(
Object.entries(workspace.imports).sort(([left], [right]) =>
left.localeCompare(right),
),
)
: undefined
const binaryFiles = workspace.binaryFiles
? Object.fromEntries(
Object.entries(workspace.binaryFiles).sort(([left], [right]) =>
left.localeCompare(right),
),
)
: undefined
return JSON.stringify({
version: workspace.version,
entry: workspace.entry,
...(workspace.environment ? { environment: workspace.environment } : {}),
files,
...(binaryFiles ? { binaryFiles } : {}),
...(imports ? { imports } : {}),
})
}
export function parseExampleWorkspace(value: unknown): ExampleWorkspace {
if (
!isRecord(value) ||
!hasOnlyKeys(value, [
'version',
'entry',
'environment',
'files',
'binaryFiles',
'imports',
]) ||
value.version !== exampleWorkspaceVersion
) {
throw new Error('Unsupported example workspace version')
}
if (
typeof value.entry !== 'string' ||
!isCanonicalExamplePath(value.entry) ||
(value.environment !== undefined &&
!isExampleEnvironment(value.environment)) ||
!isStringRecord(value.files) ||
!Object.keys(value.files).every(isCanonicalExamplePath) ||
(value.binaryFiles !== undefined &&
(!isStringRecord(value.binaryFiles) ||
!Object.keys(value.binaryFiles).every(isCanonicalExamplePath) ||
!Object.values(value.binaryFiles).every(isCanonicalBase64))) ||
(value.imports !== undefined && !isStringRecord(value.imports))
) {
throw new Error('Invalid example workspace')
}
const workspace = createExampleWorkspace({
binaryFiles: value.binaryFiles,
entry: value.entry,
environment: value.environment,
files: value.files,
imports: value.imports,
})
if (workspace.files[workspace.entry] === undefined) {
throw new Error(`Entry file not found: ${workspace.entry}`)
}
return workspace
}
export function encodeExampleBinaryFile(bytes: Uint8Array) {
let binary = ''
for (const byte of bytes) binary += String.fromCharCode(byte)
return btoa(binary)
}
export function decodeExampleBinaryFile(source: string) {
const binary = atob(source)
return Uint8Array.from(binary, (character) => character.charCodeAt(0))
}
export function isCanonicalExamplePath(path: string) {
return (
path.startsWith('/') &&
path.length > 1 &&
!path.includes('\\') &&
!path.includes('\0') &&
!path.split('/').includes('..') &&
normalizeExamplePath(path) === path
)
}
export function isExampleEnvironment(
value: unknown,
): value is ExampleEnvironment {
return exampleEnvironmentNames.some((name) => name === value)
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function isStringRecord(value: unknown): value is Record<string, string> {
return (
isRecord(value) &&
Object.values(value).every((source) => typeof source === 'string')
)
}
function isCanonicalBase64(value: string) {
try {
return encodeExampleBinaryFile(decodeExampleBinaryFile(value)) === value
} catch {
return false
}
}
function hasOnlyKeys(value: Record<string, unknown>, keys: Array<string>) {
return Object.keys(value).every((key) => keys.includes(key))
}