Skip to content

Commit be748db

Browse files
authored
Merge branch 'itteco:main' into main
2 parents 64c270a + 16a38ea commit be748db

35 files changed

+484
-384
lines changed

config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
"iframely"
348348
],
349349

350-
KNOWN_VIDEO_SOURCES: /(youtube|youtu|youtube\-nocookie|vimeo|dailymotion|theplatform|jwplatform|jwplayer|cnevids|newsinc|podbean|simplecast|libsyn|wistia|podiant|art19|kaltura|mtvnservices|brightcove|bcove|soundcloud|giphy|flowplayer|vidible|bandzoogle|podigee|smugmug|facebook|vid|ultimedia|mixcloud|vidyard|youplay|streamable|captivate|mdstrm|mediadelivery|hearstapps|rudo|acast|screen9|spotlightr)\.\w+\//i,
350+
KNOWN_VIDEO_SOURCES: /(youtube|youtu|youtube\-nocookie|vimeo|dailymotion|theplatform|jwplatform|jwplayer|cnevids|newsinc|podbean|simplecast|libsyn|wistia|podiant|art19|kaltura|mtvnservices|brightcove|bcove|soundcloud|giphy|flowplayer|vidible|bandzoogle|podigee|smugmug|facebook|vid|ultimedia|mixcloud|vidyard|youplay|streamable|captivate|mdstrm|mediadelivery|hearstapps|rudo|acast|screen9|spotlightr|loom)\.\w+\//i,
351351

352352
OEMBED_RELS_PRIORITY: ["app", "player", "survey", "image", "reader"],
353353
OEMBED_RELS_MEDIA_PRIORITY: ["player", "survey", "image", "reader", "app"],

lib/core.js

Lines changed: 110 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,28 @@
940940
}
941941
}
942942
}
943+
} else if (r.method.name === "getVars") {
944+
for(var key in r.data) {
945+
var v = r.data[key];
946+
947+
if (v !== '' && v !== null && ((typeof v === 'string' && !/^\s+$/.test(v)) || typeof v === 'number')) {
948+
949+
// Check meta plugins order.
950+
allResults.vars._sources = allResults.vars._sources || {};
951+
952+
var prevOrder = null, nextOrder = null, pluginId = allResults.vars._sources[key];
953+
954+
if (pluginId && plugins[pluginId] && plugins[r.method.pluginId]) {
955+
prevOrder = plugins[pluginId].order;
956+
nextOrder = plugins[r.method.pluginId].order;
957+
}
958+
959+
if (!prevOrder || !nextOrder || prevOrder < nextOrder) {
960+
allResults.vars[key] = v;
961+
allResults.vars._sources[key] = r.method.pluginId;
962+
}
963+
}
964+
}
943965
}
944966
}
945967
}
@@ -1072,6 +1094,11 @@
10721094
delete result.h2;
10731095
delete result.allData;
10741096
delete result.meta._sources;
1097+
delete result.vars._sources;
1098+
}
1099+
1100+
if (Object.keys(result.vars).length === 0) {
1101+
delete result.vars;
10751102
}
10761103

10771104
var links = result.links;
@@ -1173,7 +1200,9 @@
11731200
}
11741201

11751202
// Get message from plugin link.
1176-
useMessage(result, link);
1203+
if (!link.error) {
1204+
useMessage(result, link);
1205+
}
11771206
}
11781207

11791208
if (result.meta.medium === 'link' && links.some(
@@ -1217,6 +1246,10 @@
12171246
result.vary = options._usedProviderOptions;
12181247
result.vary.sort && result.vary.sort();
12191248
}
1249+
1250+
if (options.returnFetchUsage) {
1251+
result.fetches = options._fetches;
1252+
}
12201253
}
12211254

12221255
export function sortLinks(links) {
@@ -1411,6 +1444,47 @@
14111444
}
14121445
}
14131446

1447+
function generateRegisterFetchFunc(url, options) {
1448+
if (options.returnFetchUsage) {
1449+
options._fetches = options._fetches || [];
1450+
}
1451+
1452+
options.registerFetch = function(data) {
1453+
if (options.returnFetchUsage) {
1454+
options._fetches.push(data);
1455+
}
1456+
};
1457+
options.registerFetchError = function(data) {
1458+
if (options.returnFetchUsage) {
1459+
1460+
var fetch_data = {...data};
1461+
1462+
if (!data.error && !data.status_code) {
1463+
console.warn('No error param in registerFetchError data');
1464+
} else if (data.error) {
1465+
// Convert runtime `error` to `error_code` and `error_message`.
1466+
if (typeof data.error === 'string') {
1467+
fetch_data.error_code = data.error
1468+
} else {
1469+
if (data.error.code) {
1470+
fetch_data.error_code = data.error.code;
1471+
}
1472+
if (data.error.message) {
1473+
fetch_data.error_message = data.error.message;
1474+
}
1475+
if (!data.error.code && !data.error.message) {
1476+
fetch_data.error_message = '' + data.error;
1477+
}
1478+
}
1479+
}
1480+
1481+
fetch_data.error = true;
1482+
1483+
options._fetches.push(fetch_data);
1484+
}
1485+
};
1486+
}
1487+
14141488
function generateProviderOptionsFunc(url, options) {
14151489
if (options.returnProviderOptionsUsage) {
14161490
options._usedProviderOptions = options._usedProviderOptions || [];
@@ -1436,18 +1510,6 @@
14361510

14371511
const urlOptions = getQueryOptionsFromUrl(url);
14381512

1439-
options.getQueryOptions = function() {
1440-
const requestOptions = getQueryOptionsFromEntries(new URLSearchParams(options.providerOptions || {}));
1441-
var query = !options.getProviderOptions('app.disable_url_options', false)
1442-
? {...requestOptions, ...urlOptions}
1443-
: {...requestOptions}
1444-
1445-
if (query.autoplay && !query.mute && !query.muted) {
1446-
delete query.autoplay;
1447-
}
1448-
return query;
1449-
}
1450-
14511513
options.getProviderOptions = function(path, defaultValue) {
14521514
registerProviderOptionsUsage(path);
14531515

@@ -1505,8 +1567,14 @@
15051567
}
15061568
}
15071569
return value;
1508-
}
1570+
};
15091571

1572+
options.digitize = function(params) {
1573+
return Object.keys(params).reduce((acc, k) => {
1574+
acc[k] = typeof params[k] === 'boolean' ? (params[k] ? 1 : 0) : params[k];
1575+
return acc;
1576+
}, {});
1577+
}
15101578
}
15111579

15121580
function callbackWithErrorCode(code, options, cb) {
@@ -1555,6 +1623,8 @@
15551623

15561624
generateProviderOptionsFunc(uri, options);
15571625

1626+
generateRegisterFetchFunc(uri, options);
1627+
15581628
if (utils.isBlocked(uri, options, cb)) {
15591629
if (options.v === '1.3') {
15601630
cb({
@@ -1641,13 +1711,27 @@
16411711
allResults = {
16421712
meta: {},
16431713
links: [],
1714+
vars: {},
16441715
messages: [],
16451716
allData: []
16461717
},
16471718
usedMethods = {},
16481719

16491720
aborted = false,
16501721

1722+
appendDataParams = function(result) {
1723+
if (options.dataParams) {
1724+
for(var i = 0; i < options.dataParams.length; i++) {
1725+
var param = options.dataParams[i];
1726+
var value = context[param];
1727+
if (value) {
1728+
var e = result.data = allResults.data || {};
1729+
e[param] = value;
1730+
}
1731+
}
1732+
}
1733+
},
1734+
16511735
abortCurrentRequest = function() {
16521736
if (context.htmlparser && context.htmlparser.abortController) {
16531737
context.htmlparser.abortController.abort();
@@ -1676,6 +1760,8 @@
16761760
}
16771761
}
16781762

1763+
appendDataParams(allResults);
1764+
16791765
if (options.whitelist) {
16801766
allResults.whitelist = options.getWhitelistRecord && options.getWhitelistRecord(uri, {disableWildcard: true});
16811767
}
@@ -1717,6 +1803,10 @@
17171803
return;
17181804
}
17191805

1806+
// Gather results.
1807+
// Run before `responseError` to collect data and send in error.
1808+
var hasNewData = useResult(usedMethods, context, pluginsContexts, allResults, result, options, asyncMethodCb);
1809+
17201810
// Abort on error response code.
17211811
var responseError = findResponseError(result, uri);
17221812
if (responseError) {
@@ -1743,16 +1833,19 @@
17431833
error.messages = allResults.messages;
17441834
}
17451835

1836+
appendDataParams(error);
1837+
1838+
if (options.returnFetchUsage) {
1839+
error.fetches = options._fetches;
1840+
}
1841+
17461842
return cb(error);
17471843

17481844
} else {
17491845
return cb(responseError);
17501846
}
17511847
}
17521848

1753-
// Gather results.
1754-
var hasNewData = useResult(usedMethods, context, pluginsContexts, allResults, result, options, asyncMethodCb);
1755-
17561849
if (options.fetchParam && options.fetchParam in context) {
17571850
abortCurrentRequest();
17581851
cb(null, context[options.fetchParam]);

lib/loader/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
"getLinks",
3939
"getData",
4040
"getMeta",
41+
"getVars",
4142
"prepareLink"
4243
];
4344

4445
export const PLUGIN_FIELDS = PLUGIN_METHODS.concat([
4546
"mixins"
46-
]);
47+
]);

lib/plugins/system/htmlparser/htmlparser.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ export default {
2323
reuseCookies: !!options.followHTTPRedirect
2424
}};
2525

26+
options.registerFetch({
27+
source: 'html',
28+
url: url
29+
});
30+
2631
getUrlFunctional(url, options2, {
2732
onError: function(error) {
33+
34+
options.registerFetchError({
35+
source: 'html',
36+
url: url,
37+
error: error
38+
});
39+
2840
if (error.code === 'ENOTFOUND') {
2941
if (!!options.exposeStatusCode) {
3042
cb(null, {
@@ -126,6 +138,13 @@ export default {
126138
}
127139

128140
if (resp.status !== 200) {
141+
142+
options.registerFetchError({
143+
source: 'html',
144+
url: url,
145+
status_code: resp.status
146+
});
147+
129148
if (!!options.exposeStatusCode) {
130149
return cacheAndRespond(null, {
131150
__statusCode: resp.status,
@@ -182,4 +201,4 @@ export default {
182201
}
183202
});
184203
}
185-
};
204+
};

lib/plugins/system/meta/cachedMeta.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,36 @@ export default {
8383
}
8484
}
8585

86+
if (data.error.responseStatusCode !== 200) {
87+
options.registerFetchError({
88+
source: 'cache',
89+
url: url,
90+
status_code: data.error.responseStatusCode
91+
});
92+
}
93+
8694
// If data object has error attribute - return as error (e.g. redirect).
8795
log(' -- Using cached htmlparser error for: ' + url);
8896
cb(data.error);
8997
} else if (data.htmlparser) {
98+
99+
if (data.htmlparser.__statusCode !== 200) {
100+
options.registerFetchError({
101+
source: 'cache',
102+
url: url,
103+
status_code: data.htmlparser.__statusCode
104+
});
105+
}
106+
90107
log(' -- Using cached htmlparser data for: ' + url);
91108
cb(null, data.htmlparser);
92109
} else {
110+
111+
options.registerFetch({
112+
source: 'cache',
113+
url: url
114+
});
115+
93116
log(' -- Using cached meta for: ' + url);
94117
cb(null, {
95118
meta: data,
@@ -109,4 +132,4 @@ export default {
109132
}
110133
}
111134

112-
};
135+
};

lib/plugins/system/oembed/oembedUtils.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ export function findOembedLinks(uri, meta) {
185185
}
186186

187187
if (data && !cachedError) {
188+
189+
options && options.registerFetch({
190+
source: 'cache',
191+
url: uri
192+
});
193+
188194
if (data.response && data.data) {
189195
// Fallback to `request` format. Remove after 10.10.2020.
190196
data = data.data;
@@ -227,9 +233,32 @@ export function findOembedLinks(uri, meta) {
227233
});
228234
}
229235

236+
options && options.registerFetch({
237+
source: 'oembed',
238+
url: uri
239+
});
240+
230241
getUrlFunctional(uri, {}, {
231-
onError: cbWrapper,
242+
onError: function(error) {
243+
244+
options && options.registerFetchError({
245+
source: 'oembed',
246+
url: uri,
247+
error: error
248+
});
249+
250+
cbWrapper(error);
251+
},
232252
onResponse: function(res) {
253+
254+
if (res.status !== 200) {
255+
options && options.registerFetchError({
256+
source: 'oembed',
257+
url: uri,
258+
status_code: res.status
259+
});
260+
}
261+
233262
if (res.status == 200) {
234263
parseResponse(res);
235264
} else if (options && options.parseErrorBody) {
@@ -413,4 +442,4 @@ function jsonStream2oembed(stream, callback) {
413442

414443
callback(null, data);
415444
});
416-
}
445+
}

0 commit comments

Comments
 (0)