-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace 'request' with 'node-fetch' (#783)
* Removes request from video embed plugin * Remove request * Remove package * Update drafts * localhost to 127.0.0.1 * localhost * Further tweaks * Back to localhost
- Loading branch information
1 parent
2c33bf7
commit b1c9ad8
Showing
40 changed files
with
680 additions
and
1,530 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,47 @@ | ||
var request = require("request"); | ||
var cheerio = require("cheerio"); | ||
var url = require("url"); | ||
const fetch = require("node-fetch"); | ||
const cheerio = require("cheerio"); | ||
const { parse } = require("url"); | ||
|
||
// we prepend a zero-width char because of a weird fucking | ||
// bug on mobile safari where if the embed is the first child, | ||
// the video player will not show. This causes issues with | ||
// inline elements displaying (adds extra space) solution needed | ||
// that doesn't disrupt page layout... | ||
function template(url, width, height) { | ||
return ( | ||
'<div style="width:0;height:0"> </div><div class="videoContainer bandcamp" style="padding-bottom: ' + | ||
height + | ||
'px"><iframe width="' + | ||
width + | ||
'" height="' + | ||
height + | ||
'" src="' + | ||
encodeURI(url) + // encodeURI should prevent a passed URI from escaping / causing an XSS | ||
'" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>' | ||
); | ||
} | ||
const ERROR_MESSAGE = "Could not retrieve song properties"; | ||
|
||
var FAIL = "Could not retrieve song properties"; | ||
|
||
module.exports = function (href, callback) { | ||
// Before loading anything, check that the path starts with | ||
// /album or /track | ||
var path, paths; | ||
module.exports = async function (href, callback) { | ||
try { | ||
path = url.parse(href).pathname; | ||
} catch (e) { | ||
return callback(new Error(FAIL)); | ||
} | ||
|
||
// Trim trailing slash if applicable | ||
if (path.slice(-1) === "/") path = path.slice(0, -1); | ||
|
||
paths = path.split("/"); | ||
|
||
if (paths.length < 1) { | ||
return callback(new Error(FAIL)); | ||
} | ||
const { hostname, pathname } = parse(href); | ||
const path = pathname.toLowerCase(); | ||
|
||
var category = paths[1]; | ||
if (category !== "track" && category !== "album") { | ||
return callback(new Error(FAIL)); | ||
} | ||
if (!hostname.endsWith("bandcamp.com")) { | ||
return callback(new Error(ERROR_MESSAGE)); | ||
} | ||
|
||
request(href, function (error, response, body) { | ||
if (error) return callback(error); | ||
if (!path || !path.match(/\/(album|track)/)) { | ||
return callback(new Error(ERROR_MESSAGE)); | ||
} | ||
|
||
var $, height, width, html; | ||
const res = await fetch(href); | ||
const body = await res.text(); | ||
const $ = cheerio.load(body); | ||
|
||
try { | ||
$ = cheerio.load(body); | ||
width = Number($('meta[property="og:video:width"]').attr("content")); | ||
height = Number($('meta[property="og:video:height"]').attr("content")); | ||
html = $('meta[property="og:video"]').attr("content"); | ||
} catch (error) { | ||
return callback(new Error(FAIL)); | ||
} | ||
const width = Number($('meta[property="og:video:width"]').attr("content")); | ||
const height = Number( | ||
$('meta[property="og:video:height"]').attr("content") | ||
); | ||
const html = $('meta[property="og:video"]').attr("content"); | ||
|
||
if (!html || isNaN(height) || isNaN(width)) { | ||
return callback(new Error(FAIL)); | ||
return callback(new Error(ERROR_MESSAGE)); | ||
} | ||
|
||
return callback(null, template(html, width, height)); | ||
}); | ||
// we prepend a zero-width char because of a weird | ||
// bug on mobile safari where if the embed is the first child, | ||
// the video player will not show. This causes issues with | ||
// inline elements displaying (adds extra space) solution needed | ||
// that doesn't disrupt page layout... | ||
const embedHTML = `<div style="width:0;height:0"> </div><div class="videoContainer bandcamp" style="padding-bottom: ${height}px"><iframe width="${width}" height="${height}" src="${encodeURI( | ||
href | ||
)}" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>`; | ||
|
||
callback(null, embedHTML); | ||
} catch (error) { | ||
callback(new Error(ERROR_MESSAGE)); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
describe("bandcamp embeds", function () { | ||
const bandcamp = require("../bandcamp"); | ||
|
||
it("handles an empty href", function (done) { | ||
const href = ""; | ||
bandcamp(href, (err, template) => { | ||
expect(err.message).toEqual("Could not retrieve song properties"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles an invalid href", function (done) { | ||
const href = "https://bandcamp.com"; | ||
bandcamp(href, (err, template) => { | ||
expect(err.message).toEqual("Could not retrieve song properties"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles an invalid path", function (done) { | ||
const href = "https://bandcamp.com/invalid"; | ||
bandcamp(href, (err, template) => { | ||
expect(err.message).toEqual("Could not retrieve song properties"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles a valid link to an album on a bandcamp subdomain", function (done) { | ||
const href = "https://oliviachaney.bandcamp.com/album/circus-of-desire"; | ||
bandcamp(href, (err, template) => { | ||
expect(err).toEqual(null); | ||
expect(template).toEqual( | ||
`<div style="width:0;height:0"> </div><div class="videoContainer bandcamp" style="padding-bottom: 120px"><iframe width="400" height="120" src="https://oliviachaney.bandcamp.com/album/circus-of-desire" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>` | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles a valid link to a track on a bandcamp subdomain", function (done) { | ||
const href = "https://cloquet.bandcamp.com/track/new-drugs"; | ||
bandcamp(href, (err, template) => { | ||
expect(err).toEqual(null); | ||
expect(template).toEqual( | ||
`<div style="width:0;height:0"> </div><div class="videoContainer bandcamp" style="padding-bottom: 120px"><iframe width="400" height="120" src="https://cloquet.bandcamp.com/track/new-drugs" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>` | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
describe("video-embed plugin", function () { | ||
const videoEmbed = require("../index"); | ||
const cheerio = require("cheerio"); | ||
|
||
it("embeds videos from youtube, vimeo and music from bandcamp", function (done) { | ||
const $ = cheerio.load( | ||
`<a href="https://www.youtube.com/watch?v=MJ62hh0a9U4">https://www.youtube.com/watch?v=MJ62hh0a9U4</a> | ||
<a href="https://vimeo.com/87952436">https://vimeo.com/87952436</a> | ||
<a href="https://oliviachaney.bandcamp.com/album/circus-of-desire">https://oliviachaney.bandcamp.com/album/circus-of-desire</a> | ||
<a href="https://cloquet.bandcamp.com/track/new-drugs">https://cloquet.bandcamp.com/track/new-drugs</a> | ||
<a href="https://foo.com/87952436">https://foo.com/87952436</a> | ||
` | ||
); | ||
videoEmbed.render($, function (err) { | ||
expect(err).toEqual(null); | ||
expect($("iframe").length).toEqual(4); | ||
done(); | ||
}); | ||
}); | ||
}); |
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,82 @@ | ||
describe("vimeo embeds", function () { | ||
const vimeo = require("../vimeo"); | ||
it("handles an empty href", function (done) { | ||
const href = ""; | ||
vimeo(href, (err, template) => { | ||
expect(err.message).toEqual("Could not retrieve video properties"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles an invalid href", function (done) { | ||
const href = "https://vimeo.com/^*%&^*("; | ||
vimeo(href, (err, template) => { | ||
expect(err.message).toEqual("Could not retrieve video properties"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles an invalid host", function (done) { | ||
const href = "https://viimeo.com/123"; | ||
vimeo(href, (err, template) => { | ||
expect(err.message).toEqual("Could not retrieve video properties"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles an invalid path", function (done) { | ||
const href = "https://vimeo.com/invalid"; | ||
vimeo(href, (err, template) => { | ||
expect(err.message).toEqual("Could not retrieve video properties"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles a valid link", function (done) { | ||
const href = "https://vimeo.com/87952436"; | ||
vimeo(href, (err, template) => { | ||
expect(err).toEqual(null); | ||
expect(template).toEqual( | ||
`<div style="width:0;height:0"> </div><div class="videoContainer vimeo" style="padding-bottom: 56.25%" ><iframe data-thumbnail="https://i.vimeocdn.com/video/466717816-33ad450eea4c71be9149dbe2e0d18673874917cadd5f1af29de3731e4d22a77f-d_640.jpg" src="//player.vimeo.com/video/87952436?badge=0&color=ffffff&byline=0&portrait=0" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>` | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles a valid link with a trailing slash", function (done) { | ||
const href = "https://vimeo.com/87952436/"; | ||
vimeo(href, (err, template) => { | ||
expect(err).toEqual(null); | ||
expect(template).toEqual( | ||
`<div style="width:0;height:0"> </div><div class="videoContainer vimeo" style="padding-bottom: 56.25%" ><iframe data-thumbnail="https://i.vimeocdn.com/video/466717816-33ad450eea4c71be9149dbe2e0d18673874917cadd5f1af29de3731e4d22a77f-d_640.jpg" src="//player.vimeo.com/video/87952436?badge=0&color=ffffff&byline=0&portrait=0" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>` | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles a valid link with a query string", function (done) { | ||
const href = "https://vimeo.com/87952436?query=string"; | ||
vimeo(href, (err, template) => { | ||
expect(err).toEqual(null); | ||
expect(template).toEqual( | ||
`<div style="width:0;height:0"> </div><div class="videoContainer vimeo" style="padding-bottom: 56.25%" ><iframe data-thumbnail="https://i.vimeocdn.com/video/466717816-33ad450eea4c71be9149dbe2e0d18673874917cadd5f1af29de3731e4d22a77f-d_640.jpg" src="//player.vimeo.com/video/87952436?badge=0&color=ffffff&byline=0&portrait=0" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>` | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles a valid link with a hash", function (done) { | ||
const href = "https://vimeo.com/87952436#hash"; | ||
vimeo(href, (err, template) => { | ||
expect(err).toEqual(null); | ||
expect(template).toEqual( | ||
`<div style="width:0;height:0"> </div><div class="videoContainer vimeo" style="padding-bottom: 56.25%" ><iframe data-thumbnail="https://i.vimeocdn.com/video/466717816-33ad450eea4c71be9149dbe2e0d18673874917cadd5f1af29de3731e4d22a77f-d_640.jpg" src="//player.vimeo.com/video/87952436?badge=0&color=ffffff&byline=0&portrait=0" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>` | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); |
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,41 @@ | ||
describe("youtube embeds", function () { | ||
const youtube = require("../youtube"); | ||
|
||
it("handles an empty href", function (done) { | ||
const href = ""; | ||
youtube(href, (err, template) => { | ||
expect(err.message).toEqual("Could not parse youtube video"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles an invalid href", function (done) { | ||
const href = "https://www.youtube.com/watch?v=*(&*^%"; | ||
youtube(href, (err, template) => { | ||
expect(err.message).toEqual("Could not parse youtube video"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles an invalid host", function (done) { | ||
const href = "https://www.youtubee.com/watch?v=123"; | ||
youtube(href, (err, template) => { | ||
expect(err.message).toEqual("Could not parse youtube video"); | ||
expect(template).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("handles a valid link", function (done) { | ||
const href = "https://www.youtube.com/watch?v=YaT_5KoGh1Q"; | ||
youtube(href, (err, template) => { | ||
expect(err).toEqual(null); | ||
expect(template).toEqual( | ||
`<div style="width:0;height:0"> </div><div class="videoContainer" style="padding-bottom:56.25%"><iframe src="https://www.youtube-nocookie.com/embed/YaT_5KoGh1Q?rel=0&wmode=transparent&rel=0&autohide=1&showinfo=0" frameborder="0" allowfullscreen></iframe></div>` | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.