@lde/distribution-downloader 0.2.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/README.md +3 -0
- package/dist/download.d.ts +11 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +47 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Distribution } from '@lde/dataset';
|
|
2
|
+
export interface Downloader {
|
|
3
|
+
download(distribution: Distribution, target?: string): Promise<string>;
|
|
4
|
+
}
|
|
5
|
+
export declare class LastModifiedDownloader implements Downloader {
|
|
6
|
+
private readonly path;
|
|
7
|
+
constructor(path?: string);
|
|
8
|
+
download(distribution: Distribution, target?: string): Promise<string>;
|
|
9
|
+
private localFileIsUpToDate;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=download.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"download.d.ts","sourceRoot":"","sources":["../src/download.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAO5C,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,YAAY,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACxE;AAED,qBAAa,sBAAuB,YAAW,UAAU;IAC3C,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,SAAY;IAEhC,QAAQ,CACnB,YAAY,EAAE,YAAY,EAC1B,MAAM,SAAyD,GAC9D,OAAO,CAAC,MAAM,CAAC;YA6BJ,mBAAmB;CAkBlC"}
|
package/dist/download.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import filenamifyUrl from 'filenamify-url';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import { pipeline } from 'node:stream/promises';
|
|
4
|
+
import { createWriteStream } from 'node:fs';
|
|
5
|
+
import { access, stat } from 'node:fs/promises';
|
|
6
|
+
export class LastModifiedDownloader {
|
|
7
|
+
path;
|
|
8
|
+
constructor(path = 'imports') {
|
|
9
|
+
this.path = path;
|
|
10
|
+
}
|
|
11
|
+
async download(distribution, target = join(this.path, filenamifyUrl(distribution.accessUrl))) {
|
|
12
|
+
const downloadUrl = distribution.accessUrl;
|
|
13
|
+
const filePath = resolve(target);
|
|
14
|
+
if (await this.localFileIsUpToDate(filePath, distribution.lastModified)) {
|
|
15
|
+
return filePath;
|
|
16
|
+
}
|
|
17
|
+
const downloadResponse = await fetch(downloadUrl);
|
|
18
|
+
if (!downloadResponse.ok || !downloadResponse.body) {
|
|
19
|
+
throw new Error(`Failed to download ${downloadUrl}: ${downloadResponse.statusText}`);
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
await pipeline(downloadResponse.body, createWriteStream(filePath));
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
throw new Error(`Failed to save ${downloadUrl} to ${filePath}: ${error}`);
|
|
26
|
+
}
|
|
27
|
+
const stats = await stat(filePath);
|
|
28
|
+
if (stats.size <= 1) {
|
|
29
|
+
throw new Error('Distribution download is empty');
|
|
30
|
+
}
|
|
31
|
+
return filePath;
|
|
32
|
+
}
|
|
33
|
+
async localFileIsUpToDate(filePath, lastModified) {
|
|
34
|
+
if (undefined === lastModified) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
await access(filePath);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const stats = await stat(filePath);
|
|
44
|
+
const fileDate = stats.mtime;
|
|
45
|
+
return fileDate >= lastModified;
|
|
46
|
+
}
|
|
47
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './download.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lde/distribution-downloader",
|
|
3
|
+
"version": "0.2.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"development": "./src/index.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"!**/*.tsbuildinfo"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"tslib": "^2.3.0",
|
|
23
|
+
"@lde/dataset": "0.3.1",
|
|
24
|
+
"filenamify-url": "3.1.0"
|
|
25
|
+
}
|
|
26
|
+
}
|