Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-link-preview

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-link-preview - npm Package Compare versions

Comparing version 1.3.3 to 1.3.4

22

__tests__/index.spec.js

@@ -27,2 +27,24 @@ const LinkPreview = require('../index.js');

it('should extract link info from a URL with a newline', async () => {
const linkInfo = await LinkPreview.getPreview(`
https://www.youtube.com/watch?v=wuClZjOdT30
`);
// { url: 'https://www.youtube.com/watch?v=wuClZjOdT30',
// title: 'Geography Now! Germany - YouTube',
// description: 'Gluten free vegetarians beware. Watch at your own risk. We now have a Public mailbox! Feel free to send anything via mail! Our public mailbox address is: 190...',
// mediaType: 'video',
// images: [ 'https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg' ],
// videos: undefined }
expect(linkInfo.url).to.be.equal('https://www.youtube.com/watch?v=wuClZjOdT30');
expect(linkInfo.title).to.be.equal('Geography Now! Germany');
expect(linkInfo.description).to.be.ok();
expect(linkInfo.mediaType).to.be.equal('video.other');
expect(linkInfo.images.length).to.be.equal(1);
expect(linkInfo.images[0]).to.be.equal('https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg');
expect(linkInfo.videos.length).to.be.equal(0);
expect(linkInfo.favicons[0]).to.be.equal('https://www.youtube.com/yts/img/favicon_32-vflOogEID.png');
expect(linkInfo.contentType.toLowerCase()).to.be.equal('text/html; charset=utf-8');
});
it('should extract link info from just text with a URL', async () => {

@@ -29,0 +51,0 @@ const linkInfo = await LinkPreview.getPreview('This is some text blah blah https://www.youtube.com/watch?v=wuClZjOdT30 and more text');

109

index.js

@@ -7,15 +7,9 @@ /**

const urlObj = require('url');
const { fetch } = require('cross-fetch');
const fetch = require('cross-fetch').fetch;
require('es6-promise').polyfill();
const {
REGEX_VALID_URL,
REGEX_CONTENT_TYPE_IMAGE,
REGEX_CONTENT_TYPE_AUDIO,
REGEX_CONTENT_TYPE_VIDEO,
REGEX_CONTENT_TYPE_TEXT,
REGEX_CONTENT_TYPE_APPLICATION
} = require('./constants');
const CONSTANTS = require('./constants');
exports.getPreview = function(text, options) {
return new Promise((resolve, reject) => {
return new Promise(function(resolve, reject) {
if (!text) {

@@ -27,6 +21,6 @@ reject({

let detectedUrl = null;
var detectedUrl = null;
text.split(' ').forEach(token => {
if (REGEX_VALID_URL.test(token) && !detectedUrl) {
text.replace(/\n/g, ' ').split(' ').forEach(function(token) {
if (CONSTANTS.REGEX_VALID_URL.test(token) && !detectedUrl) {
detectedUrl = token;

@@ -38,3 +32,3 @@ }

fetch(detectedUrl)
.then(response => {
.then(function(response) {
// get final URL (after any redirects)

@@ -44,3 +38,3 @@ const finalUrl = response.url;

// get content type of response
let contentType = findById(response.headers, 'content-type');
var contentType = findById(response.headers, 'content-type');
if (!contentType) {

@@ -54,14 +48,14 @@ return reject({ error: 'React-Native-Link-Preview: Could not extract content type for URL.' });

// parse response depending on content type
if (contentType && REGEX_CONTENT_TYPE_IMAGE.test(contentType)) {
if (contentType && CONSTANTS.REGEX_CONTENT_TYPE_IMAGE.test(contentType)) {
resolve(parseImageResponse(finalUrl, contentType));
} else if (contentType && REGEX_CONTENT_TYPE_AUDIO.test(contentType)) {
} else if (contentType && CONSTANTS.REGEX_CONTENT_TYPE_AUDIO.test(contentType)) {
resolve(parseAudioResponse(finalUrl, contentType));
} else if (contentType && REGEX_CONTENT_TYPE_VIDEO.test(contentType)) {
} else if (contentType && CONSTANTS.REGEX_CONTENT_TYPE_VIDEO.test(contentType)) {
resolve(parseVideoResponse(finalUrl, contentType));
} else if (contentType && REGEX_CONTENT_TYPE_TEXT.test(contentType)) {
} else if (contentType && CONSTANTS.REGEX_CONTENT_TYPE_TEXT.test(contentType)) {
response.text()
.then(text => {
.then(function(text) {
resolve(parseTextResponse(text, finalUrl, options || {}, contentType));
});
} else if (contentType && REGEX_CONTENT_TYPE_APPLICATION.test(contentType)) {
} else if (contentType && CONSTANTS.REGEX_CONTENT_TYPE_APPLICATION.test(contentType)) {
resolve(parseApplicationResponse(finalUrl, contentType));

@@ -72,3 +66,3 @@ } else {

})
.catch(error => reject({ error }));
.catch(function(error) { reject({ error: error }) });
} else {

@@ -85,5 +79,5 @@ reject({

const findById = function (object, key) {
let value;
var value;
Object.keys(object).some(k => {
Object.keys(object).some(function(k) {
if (k.toLowerCase() === key) {

@@ -104,5 +98,5 @@ value = object[k];

return {
url,
url: url,
mediaType: 'image',
contentType,
contentType: contentType,
favicons: [getDefaultFavicon(url)]

@@ -114,5 +108,5 @@ };

return {
url,
url: url,
mediaType: 'audio',
contentType,
contentType: contentType,
favicons: [getDefaultFavicon(url)]

@@ -124,5 +118,5 @@ };

return {
url,
url: url,
mediaType: 'video',
contentType,
contentType: contentType,
favicons: [getDefaultFavicon(url)]

@@ -134,5 +128,5 @@ };

return {
url,
url: url,
mediaType: 'application',
contentType,
contentType: contentType,
favicons: [getDefaultFavicon(url)]

@@ -146,7 +140,7 @@ };

return {
url,
url: url,
title: getTitle(doc),
description: getDescription(doc),
mediaType: getMediaType(doc) || 'website',
contentType,
contentType: contentType,
images: getImages(doc, url, options.imagesPropertyType),

@@ -159,3 +153,3 @@ videos: getVideos(doc),

const getTitle = function(doc) {
let title = doc("meta[property='og:title']").attr('content');
var title = doc("meta[property='og:title']").attr('content');

@@ -170,3 +164,3 @@ if (!title) {

const getDescription = function(doc) {
let description = doc('meta[name=description]').attr('content');
var description = doc('meta[name=description]').attr('content');

@@ -196,3 +190,3 @@ if (description === undefined) {

const getImages = function(doc, rootUrl, imagesPropertyType) {
let images = [],
var images = [],
nodes,

@@ -202,6 +196,7 @@ src,

nodes = doc(`meta[property='${imagesPropertyType || 'og'}:image']`);
var imagePropertyType = imagesPropertyType || 'og'
nodes = doc('meta[property=\'' + imagePropertyType + ':image\']');
if (nodes.length) {
nodes.each((index, node) => {
nodes.each(function(index, node) {
src = node.attribs.content;

@@ -226,3 +221,3 @@ if (src) {

images = [];
nodes.each((index, node) => {
nodes.each(function(index, node) {
src = node.attribs.src;

@@ -245,13 +240,13 @@ if (src && !dic[src]) {

const videos = [];
let nodeTypes;
let nodeSecureUrls;
let nodeType;
let nodeSecureUrl;
let video;
let videoType;
let videoSecureUrl;
let width;
let height;
let videoObj;
let index;
var nodeTypes;
var nodeSecureUrls;
var nodeType;
var nodeSecureUrl;
var video;
var videoType;
var videoSecureUrl;
var width;
var height;
var videoObj;
var index;

@@ -280,4 +275,4 @@ const nodes = doc("meta[property='og:video']");

type: videoType,
width,
height
width: width,
height: width
};

@@ -297,3 +292,3 @@ if (videoType.indexOf('video/') === 0) {

const getFavicons = function(doc, rootUrl) {
let images = [],
var images = [],
nodes = [],

@@ -304,9 +299,9 @@ src;

relSelectors.forEach((relSelector) => {
relSelectors.forEach(function(relSelector) {
// look for all icon tags
nodes = doc(`link[${relSelector}]`);
nodes = doc('link[' + relSelector + ']');
// collect all images from icon tags
if (nodes.length) {
nodes.each((index, node) => {
nodes.each(function(index, node) {
src = node.attribs.href;

@@ -313,0 +308,0 @@ if (src) {

{
"name": "react-native-link-preview",
"version": "1.3.3",
"version": "1.3.4",
"description": "",

@@ -19,2 +19,3 @@ "main": "index.js",

"cross-fetch": "0.0.8",
"es6-promise": "^4.2.4",
"url": "^0.11.0"

@@ -21,0 +22,0 @@ },

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc