2022-02-18 00:34:11 +00:00
|
|
|
import { IncomingMessage } from 'http'
|
|
|
|
import { parse } from 'url'
|
|
|
|
import { ParsedRequest } from './types'
|
2022-01-07 20:07:38 +00:00
|
|
|
|
|
|
|
export function parseRequest(req: IncomingMessage) {
|
2022-02-18 00:34:11 +00:00
|
|
|
console.log('HTTP ' + req.url)
|
|
|
|
const { pathname, query } = parse(req.url || '/', true)
|
2022-01-10 05:50:31 +00:00
|
|
|
const {
|
|
|
|
fontSize,
|
|
|
|
images,
|
|
|
|
widths,
|
|
|
|
heights,
|
|
|
|
theme,
|
|
|
|
md,
|
|
|
|
|
|
|
|
// Attributes for Manifold card:
|
|
|
|
question,
|
|
|
|
probability,
|
2022-08-05 12:56:10 +00:00
|
|
|
numericValue,
|
2022-01-10 05:50:31 +00:00
|
|
|
metadata,
|
|
|
|
creatorName,
|
|
|
|
creatorUsername,
|
|
|
|
creatorAvatarUrl,
|
2022-08-04 21:27:02 +00:00
|
|
|
|
|
|
|
// Challenge attributes:
|
|
|
|
challengerAmount,
|
|
|
|
challengerOutcome,
|
|
|
|
creatorAmount,
|
|
|
|
creatorOutcome,
|
|
|
|
acceptedName,
|
|
|
|
acceptedAvatarUrl,
|
2022-02-18 00:34:11 +00:00
|
|
|
} = query || {}
|
2022-01-07 20:07:38 +00:00
|
|
|
|
|
|
|
if (Array.isArray(fontSize)) {
|
2022-02-18 00:34:11 +00:00
|
|
|
throw new Error('Expected a single fontSize')
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
|
|
|
if (Array.isArray(theme)) {
|
2022-02-18 00:34:11 +00:00
|
|
|
throw new Error('Expected a single theme')
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 00:34:11 +00:00
|
|
|
const arr = (pathname || '/').slice(1).split('.')
|
|
|
|
let extension = ''
|
|
|
|
let text = ''
|
2022-01-07 20:07:38 +00:00
|
|
|
if (arr.length === 0) {
|
2022-02-18 00:34:11 +00:00
|
|
|
text = ''
|
2022-01-07 20:07:38 +00:00
|
|
|
} else if (arr.length === 1) {
|
2022-02-18 00:34:11 +00:00
|
|
|
text = arr[0]
|
2022-01-07 20:07:38 +00:00
|
|
|
} else {
|
2022-02-18 00:34:11 +00:00
|
|
|
extension = arr.pop() as string
|
|
|
|
text = arr.join('.')
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 05:50:31 +00:00
|
|
|
// Take a url query param and return a single string
|
|
|
|
const getString = (stringOrArray: string[] | string | undefined): string => {
|
|
|
|
if (Array.isArray(stringOrArray)) {
|
|
|
|
// If the query param is an array, return the first element
|
2022-02-18 00:34:11 +00:00
|
|
|
return stringOrArray[0]
|
2022-01-10 05:50:31 +00:00
|
|
|
}
|
2022-02-18 00:34:11 +00:00
|
|
|
return stringOrArray || ''
|
|
|
|
}
|
2022-01-10 05:50:31 +00:00
|
|
|
|
2022-01-07 20:07:38 +00:00
|
|
|
const parsedRequest: ParsedRequest = {
|
2022-02-18 00:34:11 +00:00
|
|
|
fileType: extension === 'jpeg' ? extension : 'png',
|
2022-01-07 20:07:38 +00:00
|
|
|
text: decodeURIComponent(text),
|
2022-02-18 00:34:11 +00:00
|
|
|
theme: theme === 'dark' ? 'dark' : 'light',
|
|
|
|
md: md === '1' || md === 'true',
|
|
|
|
fontSize: fontSize || '96px',
|
2022-01-07 20:07:38 +00:00
|
|
|
images: getArray(images),
|
|
|
|
widths: getArray(widths),
|
|
|
|
heights: getArray(heights),
|
2022-01-10 05:50:31 +00:00
|
|
|
|
|
|
|
question:
|
2022-02-18 00:34:11 +00:00
|
|
|
getString(question) || 'Will you create a prediction market on Manifold?',
|
|
|
|
probability: getString(probability),
|
2022-08-05 12:56:10 +00:00
|
|
|
numericValue: getString(numericValue) || '',
|
2022-02-18 00:34:11 +00:00
|
|
|
metadata: getString(metadata) || 'Jan 1 • M$ 123 pool',
|
|
|
|
creatorName: getString(creatorName) || 'Manifold Markets',
|
|
|
|
creatorUsername: getString(creatorUsername) || 'ManifoldMarkets',
|
|
|
|
creatorAvatarUrl: getString(creatorAvatarUrl) || '',
|
2022-08-04 21:27:02 +00:00
|
|
|
challengerAmount: getString(challengerAmount) || '',
|
|
|
|
challengerOutcome: getString(challengerOutcome) || '',
|
|
|
|
creatorAmount: getString(creatorAmount) || '',
|
|
|
|
creatorOutcome: getString(creatorOutcome) || '',
|
|
|
|
acceptedName: getString(acceptedName) || '',
|
|
|
|
acceptedAvatarUrl: getString(acceptedAvatarUrl) || '',
|
2022-02-18 00:34:11 +00:00
|
|
|
}
|
|
|
|
parsedRequest.images = getDefaultImages(parsedRequest.images)
|
|
|
|
return parsedRequest
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getArray(stringOrArray: string[] | string | undefined): string[] {
|
2022-02-18 00:34:11 +00:00
|
|
|
if (typeof stringOrArray === 'undefined') {
|
|
|
|
return []
|
2022-01-07 20:07:38 +00:00
|
|
|
} else if (Array.isArray(stringOrArray)) {
|
2022-02-18 00:34:11 +00:00
|
|
|
return stringOrArray
|
2022-01-07 20:07:38 +00:00
|
|
|
} else {
|
2022-02-18 00:34:11 +00:00
|
|
|
return [stringOrArray]
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultImages(images: string[]): string[] {
|
2022-02-18 00:34:11 +00:00
|
|
|
const defaultImage = 'https://manifold.markets/logo.png'
|
2022-01-07 20:07:38 +00:00
|
|
|
|
|
|
|
if (!images || !images[0]) {
|
2022-02-18 00:34:11 +00:00
|
|
|
return [defaultImage]
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|
2022-02-18 00:34:11 +00:00
|
|
|
return images
|
2022-01-07 20:07:38 +00:00
|
|
|
}
|