poad 2.76.2 → 2.76.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "poad",
3
- "version": "2.76.2",
3
+ "version": "2.76.3",
4
4
  "engine": "0.28.0",
5
5
  "runner": "0.5.0",
6
6
  "main": "index.js",
package/project/source.js CHANGED
@@ -27,6 +27,7 @@
27
27
  // storage-token du plan de contrôle, renouvelé pendant un long suivi) et la
28
28
  // clé anon : jamais affichés. En local : la clé service_role du projet.
29
29
  const flows = require('./flows');
30
+ const { directUrl } = require('../utils/redirectors');
30
31
 
31
32
  const NAME_RE = /^(?:[a-z][a-z0-9-]{0,55}|[a-z_][a-z0-9_]{0,62})$/; // meta, sources.ts : SOURCE_NAME_RE (nom de table, ou d'avant 0.65.0)
32
33
  /** Formats d'une source (meta : sources.ts FORMATS ; ics… lus par l'étape source-read 1.3.0, engine avec meta 1.43.0) */
@@ -140,6 +141,10 @@ const parseLinkArgs = (words) => {
140
141
  if (o.url !== null && o.storage !== null) return { ok: false, message: 'poad source link: --url or --storage, not both.' };
141
142
  for (const k of ['host', 'port', 'database', 'remote', 'credential', 'sslmode']) if (o[k] !== null) return { ok: false, message: `--${k} is for a linked PostgreSQL table, not a file (--url or --storage).` };
142
143
  if (o.url !== null) {
144
+ // Lien de redirection (google.com/url?url=…, Outlook, Facebook…) : l'adresse du fichier qu'il porte
145
+ const d = directUrl(o.url);
146
+ o.url = d.url;
147
+ o.redirect = d.via;
143
148
  let u;
144
149
  try {
145
150
  u = new URL(o.url);
@@ -795,6 +800,7 @@ const main = async (args = {}, raw, deps = {}) => {
795
800
  const t = await ready();
796
801
  if (!t) return;
797
802
  const where = o.storage ? { storage: o.storage } : { url: o.url };
803
+ if (o.redirect && !json) console.log(chalk.grey(` ${o.redirect} redirect link: reading the file it points to, ${remoteLocation(where)}`));
798
804
  const csv = { ...(o.delimiter !== null ? { delimiter: o.delimiter === 'tab' ? '\\t' : o.delimiter } : {}), ...(o.header !== null ? { header: o.header } : {}), ...(o.encoding !== null ? { encoding: o.encoding.toLowerCase() } : {}) };
799
805
  const seen = await guard(() => call(t, 'POST', '/meta/v1/sources/remote/inspect', { ...where, ...(o.format ? { format: o.format } : {}), ...(o.root ? { root: o.root } : {}), ...(Object.keys(csv).length ? { csv } : {}), ...(Object.keys(readerOptions(o)).length ? { options: readerOptions(o) } : {}), name: o.table }));
800
806
  if (!seen) return;
@@ -0,0 +1,23 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert');
3
+ const { directUrl } = require('../utils/redirectors');
4
+ const source = require('../project/source');
5
+
6
+ const google = 'https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://www.data.gouv.fr/api/1/organizations/meteo-france/datasets.csv&ved=2ahUKE&usg=AOvVaw3';
7
+
8
+ test('directUrl : liens de redirection déballés, adresses directes gardées', () => {
9
+ assert.deepStrictEqual(directUrl(google), { url: 'https://www.data.gouv.fr/api/1/organizations/meteo-france/datasets.csv', via: 'Google' });
10
+ assert.deepStrictEqual(directUrl('https://www.google.fr/url?q=https%3A%2F%2Fa.org%2Ff.csv%3Fx%3D1'), { url: 'https://a.org/f.csv?x=1', via: 'Google' });
11
+ assert.deepStrictEqual(directUrl('https://l.facebook.com/l.php?u=https%3A%2F%2Fa.org%2Ff.json&h=x'), { url: 'https://a.org/f.json', via: 'Facebook' });
12
+ assert.deepStrictEqual(directUrl('https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fa.org%2Ff.csv&data=x'), { url: 'https://a.org/f.csv', via: 'Outlook' });
13
+ assert.deepStrictEqual(directUrl('https://a.org/f.csv'), { url: 'https://a.org/f.csv', via: null });
14
+ assert.deepStrictEqual(directUrl('https://www.google.com/search?q=https://a.org'), { url: 'https://www.google.com/search?q=https://a.org', via: null });
15
+ assert.deepStrictEqual(directUrl('https://www.google.com/url?q=javascript:alert(1)').via, null);
16
+ });
17
+
18
+ test('poad source link --url : un lien Google lit le fichier qu’il porte', () => {
19
+ const r = source.parseLinkArgs(['meteo', '--url', google]);
20
+ assert.strictEqual(r.ok, true);
21
+ assert.strictEqual(r.o.url, 'https://www.data.gouv.fr/api/1/organizations/meteo-france/datasets.csv');
22
+ assert.strictEqual(r.o.redirect, 'Google');
23
+ });
@@ -0,0 +1,48 @@
1
+ // Liens de redirection des moteurs de recherche, réseaux sociaux et messageries (google.com/url?url=…,
2
+ // l.facebook.com/l.php?u=…, safelinks d'Outlook…). Ils rendent une page HTML (200) qui redirige en
3
+ // JavaScript : lus tels quels, ils donnent « page web au lieu d'un fichier ». L'adresse du fichier est dans
4
+ // un paramètre : poad source link la reprend. Même liste que l'engine (services/meta/src/source-redirectors.ts,
5
+ // engine 0.73.2), qui déballe aussi de son côté. Pur, sans réseau.
6
+
7
+ const REDIRECTORS = [
8
+ { name: 'Google', host: /^(www\.)?google\.[a-z.]{2,7}$/i, path: /^\/url\/?$/, params: ['url', 'q'] },
9
+ { name: 'Facebook', host: /^(l|lm|m|www)\.facebook\.com$/i, path: /^\/l\.php$/, params: ['u'] },
10
+ { name: 'Instagram', host: /^l\.instagram\.com$/i, path: /^\/?$/, params: ['u'] },
11
+ { name: 'Messenger', host: /^l\.messenger\.com$/i, path: /^\/l\.php$/, params: ['u'] },
12
+ { name: 'YouTube', host: /^(www\.)?youtube\.com$/i, path: /^\/redirect$/, params: ['q'] },
13
+ { name: 'Outlook', host: /^[a-z0-9-]+\.safelinks\.protection\.outlook\.com$/i, path: /^\/?$/, params: ['url'] },
14
+ { name: 'DuckDuckGo', host: /^(www\.)?duckduckgo\.com$/i, path: /^\/l\/?$/, params: ['uddg'] },
15
+ { name: 'LinkedIn', host: /^(www\.)?linkedin\.com$/i, path: /^\/redir\/redirect\/?$/, params: ['url'] },
16
+ { name: 'Slack', host: /^slack-redir\.net$/i, path: /^\/link$/, params: ['url'] },
17
+ { name: 'Steam', host: /^steamcommunity\.com$/i, path: /^\/linkfilter\/?$/, params: ['url', 'u'] },
18
+ ];
19
+
20
+ /**
21
+ * Adresse directe d'un lien de redirection : { url, via } (via = nom du service, null si l'adresse est déjà
22
+ * directe). Plusieurs couches, 5 au plus ; seule une cible http(s) est reprise ; une adresse illisible est rendue telle quelle.
23
+ */
24
+ const directUrl = (raw) => {
25
+ let url = String(raw == null ? '' : raw).trim();
26
+ let via = null;
27
+ for (let i = 0; i < 5; i++) {
28
+ let u;
29
+ try {
30
+ u = new URL(url);
31
+ } catch {
32
+ break;
33
+ }
34
+ const r = REDIRECTORS.find((x) => x.host.test(u.hostname) && x.path.test(u.pathname));
35
+ const target = r ? r.params.map((p) => u.searchParams.get(p)).find((v) => v && /^https?:\/\//i.test(v.trim())) : null;
36
+ if (!r || !target) break;
37
+ try {
38
+ new URL(target.trim());
39
+ } catch {
40
+ break;
41
+ }
42
+ url = target.trim();
43
+ if (via === null) via = r.name;
44
+ }
45
+ return { url, via };
46
+ };
47
+
48
+ module.exports = { directUrl };