Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function flatten (json, url): Promise {
const opts: FlattenOptions = {}
if (url) {
opts.base = url
}
const expanded = await jsonld.expand(json, opts)
const flattened = await jsonld.flatten(expanded, {})
return flattened[Constants.JsonLd.Graph]
}
if (result.encodingFormat !== 'text/html') {
return next(stream)
}
const [parsed] = await Promise.all([
parseHtml(stream.pipe(new PassThrough()), url),
next(stream.pipe(new PassThrough()))
])
const { twitter, html, icons, dublincore, applinks, sailthru, alternate } = parsed
const [jsonld, rdfa, microdata] = await Promise.all([
parsed.jsonld ? Jsonld.expand(parsed.jsonld, { base: url }).catch(() => undefined) : undefined,
parsed.rdfa ? Jsonld.expand(parsed.rdfa, { base: url }).catch(() => undefined) : undefined,
parsed.microdata ? Jsonld.expand(parsed.microdata, { base: url }).catch(() => undefined) : undefined
])
return Object.assign(result, {
jsonld,
rdfa,
microdata,
twitter,
html,
icons,
dublincore,
applinks,
sailthru,
alternate
})
}
): Promise {
const { url } = result
if (result.encodingFormat !== 'text/html') {
return next(stream)
}
const [parsed] = await Promise.all([
parseHtml(stream.pipe(new PassThrough()), url),
next(stream.pipe(new PassThrough()))
])
const { twitter, html, icons, dublincore, applinks, sailthru, alternate } = parsed
const [jsonld, rdfa, microdata] = await Promise.all([
parsed.jsonld ? Jsonld.expand(parsed.jsonld, { base: url }).catch(() => undefined) : undefined,
parsed.rdfa ? Jsonld.expand(parsed.rdfa, { base: url }).catch(() => undefined) : undefined,
parsed.microdata ? Jsonld.expand(parsed.microdata, { base: url }).catch(() => undefined) : undefined
])
return Object.assign(result, {
jsonld,
rdfa,
microdata,
twitter,
html,
icons,
dublincore,
applinks,
sailthru,
alternate
})
}).then(data => {
const status = data.response.status;
if (status < 200 || status >= 300) {
return promises
.expand(data.body, {
base: getDocumentationUrlFromHeaders(data.response.headers),
})
.then(json => {
return Promise.reject(
new HttpError(
json[0]['http://www.w3.org/ns/hydra/core#description'][0][
'@value'
],
status,
),
);
})
.catch(e => {
if (e.hasOwnProperty('body')) {
return Promise.reject(e);
}).then(data => {
const status = data.response.status;
if (status < 200 || status >= 300) {
return promises
.expand(data.body, {
base: getDocumentationUrlFromHeaders(data.response.headers),
})
.then(json => {
const e = new HttpError(
json[0][
'http://www.w3.org/ns/hydra/core#description'
][0]['@value'],
status,
json[0]
);
return Promise.reject(e);
})
.catch(e => {
if (e instanceof HttpError) {
return Promise.reject(e);
const { url } = result
if (result.encodingFormat !== 'text/html') {
return next(stream)
}
const [parsed] = await Promise.all([
parseHtml(stream.pipe(new PassThrough()), url),
next(stream.pipe(new PassThrough()))
])
const { twitter, html, icons, dublincore, applinks, sailthru, alternate } = parsed
const [jsonld, rdfa, microdata] = await Promise.all([
parsed.jsonld ? Jsonld.expand(parsed.jsonld, { base: url }).catch(() => undefined) : undefined,
parsed.rdfa ? Jsonld.expand(parsed.rdfa, { base: url }).catch(() => undefined) : undefined,
parsed.microdata ? Jsonld.expand(parsed.microdata, { base: url }).catch(() => undefined) : undefined
])
return Object.assign(result, {
jsonld,
rdfa,
microdata,
twitter,
html,
icons,
dublincore,
applinks,
sailthru,
alternate
})
}