-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flatten result object formats, use native promises
- Loading branch information
1 parent
ce737a8
commit 4f46001
Showing
20 changed files
with
63,559 additions
and
61,520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './formats' | ||
export * from './options' | ||
export * from './results' | ||
export * from './snippets' | ||
export * from './formats' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
ResultJsonLd, | ||
ResultApplinks, | ||
ResultDublinCore, | ||
ResultHtml, | ||
ResultSailthru, | ||
ResultTwitter, | ||
Icon, | ||
Alternative | ||
} from 'htmlmetaparser' | ||
|
||
import { Headers } from './options' | ||
import { ExifData } from './formats' | ||
|
||
export interface BaseResult { | ||
type?: string | ||
url: string | ||
status: number | ||
headers: Headers | ||
encodingFormat?: string | ||
} | ||
|
||
export interface HtmlResult extends BaseResult { | ||
type: 'html' | ||
jsonld?: ResultJsonLd | ||
rdfa?: ResultJsonLd | ||
microdata?: ResultJsonLd | ||
twitter?: ResultTwitter | ||
html?: ResultHtml | ||
dublincore?: ResultDublinCore | ||
applinks?: ResultApplinks | ||
sailthru?: ResultSailthru | ||
icons: Array<Icon> | ||
alternate: Array<Alternative> | ||
oembed?: any | ||
} | ||
|
||
export interface ImageResult extends BaseResult { | ||
type: 'image' | ||
exifData: ExifData | ||
} | ||
|
||
export interface VideoResult extends BaseResult { | ||
type: 'video' | ||
exifData: ExifData | ||
} | ||
|
||
export interface PdfResult extends BaseResult { | ||
type: 'pdf' | ||
exifData: ExifData | ||
} | ||
|
||
export type Result = HtmlResult | ImageResult | VideoResult | PdfResult | BaseResult |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,50 @@ | ||
import Promise = require('any-promise') | ||
import { Readable } from 'stream' | ||
import { parse } from 'content-type' | ||
import defaultScrapers from './scrapers' | ||
import { Headers, AbortFn, ScrapeResult, ScrapeOptions } from './interfaces' | ||
import { Headers, AbortFn, Result, ScrapeOptions, BaseResult } from './interfaces' | ||
import { makeRequest as defaultMakeRequest } from './support' | ||
|
||
/** | ||
* Scrape metadata from a URL. | ||
*/ | ||
export function scrapeUrl (url: string, options: ScrapeOptions = {}): Promise<ScrapeResult<any>> { | ||
export async function scrapeUrl (url: string, options: ScrapeOptions = {}): Promise<Result> { | ||
const makeRequest = options.makeRequest || defaultMakeRequest | ||
const res = await makeRequest(url) | ||
|
||
return makeRequest(url).then(res => { | ||
return scrapeStream(res.url, res.headers, res.stream, res.abort, options) | ||
}) | ||
return scrapeStream(res.url, res.status, res.headers, res.stream, res.abort, options) | ||
} | ||
|
||
/** | ||
* Scrape metadata from a stream (with headers/URL). | ||
*/ | ||
export function scrapeStream ( | ||
contentUrl: string, | ||
export async function scrapeStream ( | ||
url: string, | ||
status: number, | ||
headers: Headers, | ||
stream: Readable, | ||
abort?: AbortFn, | ||
options: ScrapeOptions = {} | ||
): Promise<ScrapeResult<any>> { | ||
): Promise<Result> { | ||
const encodingFormat = headers['content-type'] ? parse(headers['content-type']).type : undefined | ||
const contentLength = Number(headers['content-length']) | ||
const contentSize = isFinite(contentLength) ? contentLength : undefined | ||
const close = abort || (() => stream.resume()) | ||
const scrapers = options.scrapers || defaultScrapers | ||
|
||
const result: ScrapeResult<null> = { | ||
type: 'link', | ||
content: null, | ||
contentUrl, | ||
encodingFormat, | ||
contentSize | ||
const base: BaseResult = { | ||
url, | ||
status, | ||
headers, | ||
encodingFormat | ||
} | ||
|
||
// Traverse the available scrapers to extract information. | ||
for (const rule of scrapers) { | ||
if (rule.supported(result, headers)) { | ||
return Promise.resolve(rule.handle(result, headers, stream, close, options)) | ||
if (rule.supported(base, headers)) { | ||
return rule.handle(base, stream, close, options) | ||
} | ||
} | ||
|
||
// Abort unhandled types. | ||
close() | ||
|
||
return Promise.resolve(result) | ||
return base | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.