generated from beeman/template-typescript-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
55 additions
and
21 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 |
---|---|---|
|
@@ -68,3 +68,5 @@ typings/ | |
.sonarlint/ | ||
|
||
.idea/ | ||
|
||
swh.cache/ |
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
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,11 @@ | ||
import { swh, deleteCacheEntry, processLink } from '..' | ||
import * as log from 'loglevel' | ||
|
||
log.setDefaultLevel('info') | ||
jest.setTimeout(300000) // Allow 5 min to process all links | ||
|
||
test('Process pseudo-link', async () => processLink(0, 'https://github.com/fairmath/SwhSaveNowBatch.git')) | ||
test('Process pseudo-link again', async () => processLink(0, 'https://github.com/fairmath/SwhSaveNowBatch.git')) | ||
test('Delete pseudo-link', async () => deleteCacheEntry(0)) | ||
|
||
test('Get swh list again', async () => swh()) |
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,25 +1,49 @@ | ||
import { Response } from 'node-fetch' | ||
import csv = require('fast-csv') | ||
import CsvParserStream from 'fast-csv/build/src/parser/CsvParserStream' | ||
|
||
import cacach = require('cacache') | ||
import fetch from 'node-fetch' | ||
import log = require('loglevel') | ||
import PQueue from 'p-queue' | ||
|
||
const cachePath = './swh.cache' | ||
|
||
export function processLink(id: number, url: string): Promise<boolean | string> { | ||
log.trace('processing', { id: id, url: url }) | ||
const cacheKey = `${id}` | ||
return cacach | ||
.get(cachePath, cacheKey) | ||
.then((x: any) => { | ||
return x.data | ||
}) | ||
.catch(() => | ||
fetch(`https://archive.softwareheritage.org/api/1/git/url/${url}/`).then((res: Response) => | ||
res.text().then((text) => cacach.put(cachePath, cacheKey, text)), | ||
), | ||
) | ||
} | ||
|
||
function parseCSV(res: Response): Promise<CsvParserStream> { | ||
function parseCSV(res: Response): Promise<PQueue> { | ||
return new Promise((resolve) => { | ||
const stream = res.body.pipe(csv.parse()) | ||
const stream = res.body.pipe(csv.parse({ delimiter: ';', ignoreEmpty: true })) | ||
const queue = new PQueue({ concurrency: 10 }) | ||
let count = 0 | ||
queue.on('active', () => { | ||
log.debug(`Working on item #${++count}. Size: ${queue.size} Pending: ${queue.pending}`) | ||
}) | ||
stream | ||
.on('error', /* istanbul ignore next */ (error: Error) => console.error(error)) | ||
.on('data', (row: string) => console.log(`ROW=${JSON.stringify(row)}`)) | ||
.on('data', (row: [number, string]) => queue.add(() => processLink(row[0], row[1]))) | ||
.on('end', (rowCount: number) => { | ||
console.log(`Parsed ${rowCount} rows`) | ||
resolve(stream) | ||
stream.end() | ||
log.info(`Received ${rowCount} rows from swMATH`) | ||
resolve(queue) | ||
}) | ||
}) | ||
} | ||
|
||
export const greet = (name: string) => `Hello ${name}` | ||
|
||
export const swh = () => | ||
fetch('http://swmath.org/SWH/') | ||
.then(parseCSV) | ||
.then((stream: CsvParserStream) => stream.end()) | ||
.then((q) => q.onIdle().then(() => log.info('Processing finished'))) | ||
|
||
export const deleteCacheEntry = (id: number) => cacach.rm.entry(cachePath, `${id}`) |