fetch-opengraph
Advanced tools
Comparing version 1.0.3 to 1.0.5
156
index.js
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -22,92 +11,63 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fetch = void 0; | ||
var axios_1 = require("axios"); | ||
var fetch = function (url) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () { | ||
var response, html, metas, og, _i, metas_1, meta, properties, regex, match, result, error_1; | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 3, , 4]); | ||
return [4 /*yield*/, axios_1.default.get(url)]; | ||
case 1: | ||
response = _b.sent(); | ||
if (response.status >= 400) | ||
return [2 /*return*/, reject(response)]; | ||
return [4 /*yield*/, response.data]; | ||
case 2: | ||
html = _b.sent(); | ||
metas = html.match(/<meta[^>]+>/gim); | ||
og = []; | ||
if (metas) { | ||
for (_i = 0, metas_1 = metas; _i < metas_1.length; _i++) { | ||
meta = metas_1[_i]; | ||
properties = {}; | ||
regex = /((?<key>[^\s]+)=(['"])\s*(?<value>.*?)(?=\3)\3)+/gm; | ||
match = void 0; | ||
do { | ||
match = regex.exec(meta); | ||
if (match) { | ||
if (match.index === regex.lastIndex) | ||
regex.lastIndex++; | ||
properties = __assign(__assign({}, properties), (_a = {}, _a[match[2]] = match[4], _a)); | ||
} | ||
} while (match); | ||
// Filter out valid name and properties | ||
if (properties.name && | ||
properties.name.match(/(description|twitter:card|twitter:title|twitter:description|twitter:image)/)) { | ||
og.push({ name: properties.name, value: properties.content }); | ||
} | ||
else if (properties.property && | ||
properties.property.match(/(og:url|og:type|og:title|og:description|og:image|twitter:domain|twitter:url)/)) { | ||
og.push({ name: properties.property, value: properties.content }); | ||
} | ||
} | ||
} | ||
result = og.reduce(function (chain, meta) { | ||
var _a; | ||
return (__assign(__assign({}, chain), (_a = {}, _a[meta.name] = meta.value, _a))); | ||
}, {}); | ||
result.image = result['og:image'] ? result['og:image'] : result['twitter:image'] ? result['twitter:image'] : null; | ||
result.url = result['og:url'] ? result['og:url'] : result['twitter:url'] ? result['twitter:url'] : url; | ||
return [2 /*return*/, resolve(result)]; | ||
case 3: | ||
error_1 = _b.sent(); | ||
return [2 /*return*/, reject(error_1)]; | ||
case 4: return [2 /*return*/]; | ||
const axios_1 = require("axios"); | ||
const fetch = (url) => __awaiter(void 0, void 0, void 0, function* () { | ||
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
const response = yield axios_1.default.get(url); | ||
if (response.status >= 400) { | ||
throw response; | ||
} | ||
const html = yield response.data; | ||
const metas = html.match(/<meta[^>]+>/gim); | ||
const og = []; | ||
// There is no else statement | ||
/* istanbul ignore else */ | ||
if (metas) { | ||
for (const meta of metas) { | ||
let properties = {}; | ||
// ( Create capture group 1 | ||
// (?<key>[^\s]+) Create capture group 2 called key, capture everything backwards from the = sign upto and not including \s | ||
// = Match a = | ||
// (['"]) Create capture group \3 matching a double or single quote | ||
// \s* 0 or more \s | ||
// (?<value>.*?) Create capture group 4 called value that is not greedy ant takes everything upto the following look forward | ||
// (?=\3) Look forward and make sure there is a corresponding \3 | ||
// \3 Match \3 | ||
// )+ Finish capture group 1 and repeat it | ||
const regex = /((?<key>[^\s]+)=(['"])\s*(?<value>.*?)(?=\3)\3)+/gm; | ||
let match; | ||
do { | ||
match = regex.exec(meta); | ||
if (match) { | ||
// This is to prevent an possible endless loop, | ||
// avoid "If path not taken" from code coverage since you're unable to reproduce | ||
/* istanbul ignore next */ | ||
if (match.index === regex.lastIndex) | ||
regex.lastIndex++; | ||
properties = Object.assign(Object.assign({}, properties), { [match[2]]: match[4] !== 'undefined' ? match[4] : null }); | ||
} | ||
} while (match); | ||
if (properties.name && | ||
properties.name.match(/(title|description|twitter:card|twitter:title|twitter:description|twitter:image)/)) { | ||
og.push({ name: properties.name, value: properties.content }); | ||
} | ||
}); | ||
}); })]; | ||
}); | ||
}); }; | ||
if (properties.property && | ||
properties.property.match(/(og[:]url|og.type|og\:title|og\:description|og\:image|twitter\:domain|twitter\:url)/)) { | ||
og.push({ name: properties.property, value: properties.content }); | ||
} | ||
} | ||
} | ||
const result = og.reduce((chain, meta) => (Object.assign(Object.assign({}, chain), { [meta.name]: meta.value })), {}); | ||
result.image = result['og:image'] ? result['og:image'] : result['twitter:image'] ? result['twitter:image'] : null; | ||
result.url = result['og:url'] ? result['og:url'] : result['twitter:url'] ? result['twitter:url'] : url; | ||
return resolve(result); | ||
} | ||
catch (error) { | ||
return reject(error); | ||
} | ||
})); | ||
}); | ||
exports.fetch = fetch; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "fetch-opengraph", | ||
"version": "1.0.3", | ||
"description": ".fetch() to get opengraph information on a url", | ||
"version": "1.0.5", | ||
"description": "Fetch opengraph information from an url", | ||
"main": "index.js", | ||
@@ -10,9 +10,4 @@ "files": [ | ||
"scripts": { | ||
"test": "jest --config jestconfig.json", | ||
"test": "jest --config jestconfig.json --coverage --no-cache && istanbul-badges-readme", | ||
"build": "tsc", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm test && npm run lint", | ||
"preversion": "npm run lint", | ||
"version": "npm run format && git add -A src", | ||
"postversion": "git push && git push --tags", | ||
"format": "prettier --write \"src/**/*.ts\"", | ||
@@ -32,3 +27,3 @@ "lint": "tslint -p tsconfig.json" | ||
}, | ||
"license": "MIT License", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -40,7 +35,8 @@ "url": "https://github.com/purphoros/fetch-opengraph/issues" | ||
"@types/jest": "^26.0.20", | ||
"axios-mock-adapter": "^1.19.0", | ||
"@types/node": "^14.14.30", | ||
"istanbul-badges-readme": "^1.2.0", | ||
"jest": "^26.6.3", | ||
"msw": "^0.26.2", | ||
"prettier": "^2.2.1", | ||
"ts-jest": "^26.5.1", | ||
"ts-node": "^9.1.1", | ||
"tslint": "^6.1.3", | ||
@@ -47,0 +43,0 @@ "tslint-config-prettier": "^1.18.0", |
# fetch-opengraph | ||
[![npm version](https://badgen.net/npm/v/fetch-opengraph)](https://npm.im/fetch-opengraph) [![CI status](https://github.com/purphoros/fetch-opengraph/workflows/CI/badge.svg)](https://github.com/purphoros/fetch-opengraph/actions) | ||
> Badges from istanbul coverage report | ||
| Statements | Branches | Functions | Lines | | ||
| ----------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ----------------------------------------------------------------------- | | ||
| ![Statements](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | ![Functions](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg) | | ||
Fetch opengraph information from an url. | ||
@@ -4,0 +13,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
68
1
9151
10
71