Skip to content

Commit 17862d4

Browse files
authored
[BREAKING CHANGE] Use native Promises (#116)
Replace Bluebird Promises with native Promises. Remove loadFromFile helper function as it's insecure.
1 parent 48654fc commit 17862d4

File tree

5 files changed

+578
-3900
lines changed

5 files changed

+578
-3900
lines changed

index.js

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,50 @@
1010
/*
1111
Import modules
1212
*/
13-
const BBPromise = require( 'bluebird' );
1413
const cheerio = require( 'cheerio' );
15-
const fs = BBPromise.promisifyAll( require( 'fs' ) );
1614

1715
const index = require( './lib/index.js' );
1816

1917
/**
2018
* Default exported function that takes a url string or
2119
* request library options dictionary and returns a
22-
* BBPromise for all available metadata
20+
* Promise for all available metadata
2321
*
2422
* @param {Object} urlOrOpts url String or options dictionary
25-
* @return {Object} BBPromise for metadata
23+
* @return {Object} Promise for metadata
2624
*/
2725
exports = module.exports = function ( urlOrOpts ) {
28-
let url, opts;
29-
if ( urlOrOpts instanceof Object ) {
30-
if ( urlOrOpts.uri ) {
31-
url = urlOrOpts.uri;
26+
return new Promise( ( resolve, reject ) => {
27+
let url, opts;
28+
if ( urlOrOpts instanceof Object ) {
29+
if ( urlOrOpts.uri ) {
30+
url = urlOrOpts.uri;
31+
}
32+
opts = urlOrOpts;
33+
} else if ( typeof urlOrOpts === String ) {
34+
url = urlOrOpts;
3235
}
33-
opts = urlOrOpts;
34-
} else if ( typeof urlOrOpts === String ) {
35-
url = urlOrOpts;
36-
}
37-
if ( !url ) {
38-
return BBPromise.reject( 'No uri supplied in argument' );
39-
} else {
40-
return BBPromise.resolve(
41-
// eslint-disable-next-line n/no-unsupported-features/node-builtins
42-
fetch( url, opts ).then(
43-
( response ) => response.text().then(
44-
( body ) => index.parseAll( cheerio.load( body ) )
36+
if ( !url ) {
37+
reject( 'No uri supplied in argument' );
38+
} else {
39+
resolve(
40+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
41+
fetch( url, opts ).then(
42+
( response ) => response.text().then(
43+
( body ) => index.parseAll( cheerio.load( body ) )
44+
)
4545
)
46-
)
47-
);
48-
}
49-
};
50-
51-
/**
52-
* Exported function that takes html file and
53-
* returns a BBPromise for all available metadata
54-
*
55-
* @param {string} path path Path to HTML file
56-
* @param {Object} [opts] opts Additional options such as encoding
57-
* @return {Object} BBPromise for metadata
58-
*/
59-
exports.loadFromFile = function ( path, opts ) {
60-
const defaultEncoding = 'utf-8';
61-
62-
opts = opts || defaultEncoding;
63-
64-
return fs.readFileAsync( path, opts ).then(
65-
( html ) => index.parseAll( cheerio.load( html ) )
66-
);
46+
);
47+
}
48+
} );
6749
};
6850

6951
/**
70-
* c
7152
* Exported function that takes html string and
72-
* returns a BBPromise for all available metadata
53+
* returns a Promise for all available metadata
7354
*
7455
* @param {string} html html String HTML of the page
75-
* @return {Object} BBPromise for metadata
56+
* @return {Object} Promise for metadata
7657
*/
7758
exports.loadFromString = function ( html ) {
7859
return index.parseAll( cheerio.load( html ) );
@@ -83,7 +64,7 @@ exports.loadFromString = function ( html ) {
8364
* using the same keys as in metadataFunctions.
8465
*
8566
* @param {Object} chtml html Cheerio object to parse
86-
* @return {Object} BBPromise for metadata
67+
* @return {Object} Promise for metadata
8768
*/
8869
exports.parseAll = function ( chtml ) {
8970
return index.parseAll( chtml );
@@ -93,7 +74,7 @@ exports.parseAll = function ( chtml ) {
9374
* Scrapes BE Press metadata given html object
9475
*
9576
* @param {Object} chtml html Cheerio object
96-
* @return {Object} BBPromise for metadata
77+
* @return {Object} Promise for metadata
9778
*/
9879
exports.parseBEPress = function ( chtml ) {
9980
return index.parseBEPress( chtml );
@@ -103,7 +84,7 @@ exports.parseBEPress = function ( chtml ) {
10384
* Scrapes embedded COinS data given Cheerio loaded html object
10485
*
10586
* @param {Object} chtml html Cheerio object
106-
* @return {Object} BBPromise for metadata
87+
* @return {Object} Promise for metadata
10788
*/
10889
exports.parseCOinS = function ( chtml ) {
10990
return index.parseCOinS( chtml );
@@ -113,7 +94,7 @@ exports.parseCOinS = function ( chtml ) {
11394
* Parses value of COinS title tag
11495
*
11596
* @param {string} title String corresponding to value of title tag in span element
116-
* @return {Object} BBPromise for metadata
97+
* @return {Object} Promise for metadata
11798
*/
11899
exports.parseCOinSTitle = function ( title ) {
119100
return index.parseCOinSTitle( title );
@@ -123,7 +104,7 @@ exports.parseCOinSTitle = function ( title ) {
123104
* Scrapes Dublin Core data given Cheerio loaded html object
124105
*
125106
* @param {Object} chtml html Cheerio object
126-
* @return {Object} BBPromise for metadata
107+
* @return {Object} Promise for metadata
127108
*/
128109
exports.parseDublinCore = function ( chtml ) {
129110
return index.parseDublinCore( chtml );
@@ -133,7 +114,7 @@ exports.parseDublinCore = function ( chtml ) {
133114
* Scrapes EPrints data given Cheerio loaded html object
134115
*
135116
* @param {Object} chtml html Cheerio object
136-
* @return {Object} BBPromise for metadata
117+
* @return {Object} Promise for metadata
137118
*/
138119
exports.parseEprints = function ( chtml ) {
139120
return index.parseEprints( chtml );
@@ -143,7 +124,7 @@ exports.parseEprints = function ( chtml ) {
143124
* Scrapes general metadata terms given Cheerio loaded html object
144125
*
145126
* @param {Object} chtml html Cheerio object
146-
* @return {Object} BBPromise for metadata
127+
* @return {Object} Promise for metadata
147128
*/
148129
exports.parseGeneral = function ( chtml ) {
149130
return index.parseGeneral( chtml );
@@ -153,7 +134,7 @@ exports.parseGeneral = function ( chtml ) {
153134
* Scrapes Highwire Press metadata given html object
154135
*
155136
* @param {Object} chtml html Cheerio object
156-
* @return {Object} BBPromise for metadata
137+
* @return {Object} Promise for metadata
157138
*/
158139
exports.parseHighwirePress = function ( chtml ) {
159140
return index.parseHighwirePress( chtml );
@@ -163,7 +144,7 @@ exports.parseHighwirePress = function ( chtml ) {
163144
* Retrieves JSON-LD for given html object
164145
*
165146
* @param {Object} chtml html Cheerio object
166-
* @return {Object} BBPromise for JSON-LD
147+
* @return {Object} Promise for JSON-LD
167148
*/
168149
exports.parseJsonLd = function ( chtml ) {
169150
return index.parseJsonLd( chtml );
@@ -173,7 +154,7 @@ exports.parseJsonLd = function ( chtml ) {
173154
* Scrapes OpenGraph data given html object
174155
*
175156
* @param {Object} chtml html Cheerio object
176-
* @return {Object} BBPromise for metadata
157+
* @return {Object} Promise for metadata
177158
*/
178159
exports.parseOpenGraph = function ( chtml ) {
179160
return index.parseOpenGraph( chtml );
@@ -183,7 +164,7 @@ exports.parseOpenGraph = function ( chtml ) {
183164
* Scrapes schema.org microdata given Cheerio loaded html object
184165
*
185166
* @param {Object} chtml html Cheerio object
186-
* @return {Object} BBPromise for metadata
167+
* @return {Object} Promise for metadata
187168
*/
188169
exports.parseSchemaOrgMicrodata = function ( chtml ) {
189170
return index.parseSchemaOrgMicrodata( chtml );
@@ -193,7 +174,7 @@ exports.parseSchemaOrgMicrodata = function ( chtml ) {
193174
* Scrapes Twitter data given html object
194175
*
195176
* @param {Object} chtml html Cheerio object
196-
* @return {Object} BBPromise for metadata
177+
* @return {Object} Promise for metadata
197178
*/
198179
exports.parseTwitter = function ( chtml ) {
199180
return index.parseTwitter( chtml );
@@ -203,7 +184,7 @@ exports.parseTwitter = function ( chtml ) {
203184
* Scrapes PRISM data given html object
204185
*
205186
* @param {Object} chtml html Cheerio object
206-
* @return {Object} BBPromise for metadata
187+
* @return {Object} Promise for metadata
207188
*/
208189
exports.parsePrism = function ( chtml ) {
209190
return index.parsePrism( chtml );

0 commit comments

Comments
 (0)