@lde/distribution-downloader 0.2.7 → 0.4.0

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.
@@ -1,11 +1,22 @@
1
1
  import { Distribution } from '@lde/dataset';
2
+ export interface Logger {
3
+ fatal(msg: string, ...args: unknown[]): void;
4
+ error(msg: string, ...args: unknown[]): void;
5
+ warn(msg: string, ...args: unknown[]): void;
6
+ info(msg: string, ...args: unknown[]): void;
7
+ debug(msg: string, ...args: unknown[]): void;
8
+ trace(msg: string, ...args: unknown[]): void;
9
+ }
10
+ export interface DownloadOptions {
11
+ logger?: Logger;
12
+ }
2
13
  export interface Downloader {
3
- download(distribution: Distribution, target?: string): Promise<string>;
14
+ download(distribution: Distribution, target?: string, options?: DownloadOptions): Promise<string>;
4
15
  }
5
16
  export declare class LastModifiedDownloader implements Downloader {
6
17
  private readonly path;
7
18
  constructor(path?: string);
8
- download(distribution: Distribution, target?: string): Promise<string>;
19
+ download(distribution: Distribution, target?: string, options?: DownloadOptions): Promise<string>;
9
20
  private localFileIsUpToDate;
10
21
  }
11
22
  //# sourceMappingURL=download.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"download.d.ts","sourceRoot":"","sources":["../src/download.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAM5C,MAAM,WAAW,MAAM;IACrB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAC7C,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAC7C,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAC5C,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAC5C,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAC7C,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;CAC9C;AAaD,MAAM,WAAW,eAAe;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,UAAU;IACzB,QAAQ,CACN,YAAY,EAAE,YAAY,EAC1B,MAAM,CAAC,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,MAAM,CAAC,CAAC;CACpB;AAED,qBAAa,sBAAuB,YAAW,UAAU;IAC3C,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,SAAY;IAEhC,QAAQ,CACnB,YAAY,EAAE,YAAY,EAC1B,MAAM,SAAyD,EAC/D,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,MAAM,CAAC;YAgCJ,mBAAmB;CAyBlC"}
package/dist/download.js CHANGED
@@ -3,15 +3,27 @@ import { join, resolve } from 'node:path';
3
3
  import { pipeline } from 'node:stream/promises';
4
4
  import { createWriteStream } from 'node:fs';
5
5
  import { access, stat } from 'node:fs/promises';
6
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
7
+ const noop = () => { };
8
+ const noopLogger = {
9
+ fatal: noop,
10
+ error: noop,
11
+ warn: noop,
12
+ info: noop,
13
+ debug: noop,
14
+ trace: noop,
15
+ };
6
16
  export class LastModifiedDownloader {
7
17
  path;
8
18
  constructor(path = 'imports') {
9
19
  this.path = path;
10
20
  }
11
- async download(distribution, target = join(this.path, filenamifyUrl(distribution.accessUrl))) {
21
+ async download(distribution, target = join(this.path, filenamifyUrl(distribution.accessUrl)), options) {
22
+ const logger = options?.logger ?? noopLogger;
12
23
  const downloadUrl = distribution.accessUrl;
13
24
  const filePath = resolve(target);
14
- if (await this.localFileIsUpToDate(filePath, distribution.lastModified)) {
25
+ if (await this.localFileIsUpToDate(filePath, distribution)) {
26
+ logger.debug(`File ${filePath} is up to date, skipping download.`);
15
27
  return filePath;
16
28
  }
17
29
  const downloadResponse = await fetch(downloadUrl);
@@ -26,12 +38,13 @@ export class LastModifiedDownloader {
26
38
  }
27
39
  const stats = await stat(filePath);
28
40
  if (stats.size <= 1) {
41
+ logger.debug(`Distribution download ${downloadUrl} is empty`);
29
42
  throw new Error('Distribution download is empty');
30
43
  }
31
44
  return filePath;
32
45
  }
33
- async localFileIsUpToDate(filePath, lastModified) {
34
- if (undefined === lastModified) {
46
+ async localFileIsUpToDate(filePath, distribution) {
47
+ if (undefined === distribution.lastModified) {
35
48
  return false;
36
49
  }
37
50
  try {
@@ -41,7 +54,11 @@ export class LastModifiedDownloader {
41
54
  return false;
42
55
  }
43
56
  const stats = await stat(filePath);
44
- const fileDate = stats.mtime;
45
- return fileDate >= lastModified;
57
+ // Check if file size matches expected size to detect incomplete downloads.
58
+ if (distribution.byteSize !== undefined &&
59
+ stats.size !== distribution.byteSize) {
60
+ return false;
61
+ }
62
+ return stats.mtime >= distribution.lastModified;
46
63
  }
47
64
  }
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@lde/distribution-downloader",
3
- "version": "0.2.7",
3
+ "version": "0.4.0",
4
4
  "repository": {
5
- "url": "https://github.com/ldengine/lde"
5
+ "url": "https://github.com/ldengine/lde",
6
+ "directory": "packages/distribution-downloader"
6
7
  },
7
8
  "type": "module",
8
9
  "exports": {
@@ -22,7 +23,7 @@
22
23
  "!**/*.tsbuildinfo"
23
24
  ],
24
25
  "dependencies": {
25
- "@lde/dataset": "0.4.2",
26
+ "@lde/dataset": "0.6.0",
26
27
  "filenamify-url": "3.1.0",
27
28
  "tslib": "^2.3.0"
28
29
  }