@cogeotiff/core 9.3.0 → 9.5.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.
package/src/tiff.ts CHANGED
@@ -18,6 +18,10 @@ export interface TiffCreationOptions {
18
18
  defaultReadSize: number;
19
19
  }
20
20
 
21
+ export interface TiffFetchOptions {
22
+ signal?: AbortSignal;
23
+ }
24
+
21
25
  export class Tiff {
22
26
  /** Read 16KB blocks at a time */
23
27
  static DefaultReadSize = 16 * 1024;
@@ -49,9 +53,9 @@ export class Tiff {
49
53
  /** Create a tiff and initialize it by reading the tiff headers */
50
54
  static create(
51
55
  source: Source,
52
- options: TiffCreationOptions = { defaultReadSize: Tiff.DefaultReadSize },
56
+ options: TiffCreationOptions & TiffFetchOptions = { defaultReadSize: Tiff.DefaultReadSize },
53
57
  ): Promise<Tiff> {
54
- return new Tiff(source, options).init();
58
+ return new Tiff(source, options).init(options);
55
59
  }
56
60
 
57
61
  /**
@@ -60,9 +64,10 @@ export class Tiff {
60
64
  * This is only required if the Tiff was created with the constructor, if you
61
65
  * used {@link create} this will have already been called.
62
66
  */
63
- init(): Promise<Tiff> {
67
+ init(options?: TiffFetchOptions): Promise<Tiff> {
68
+ if (this.isInitialized) return Promise.resolve(this);
64
69
  if (this._initPromise) return this._initPromise;
65
- this._initPromise = this.readHeader();
70
+ this._initPromise = this.readHeader(options);
66
71
  return this._initPromise;
67
72
  }
68
73
 
@@ -92,11 +97,11 @@ export class Tiff {
92
97
  }
93
98
 
94
99
  /** Read the Starting header and all Image headers from the source */
95
- private async readHeader(): Promise<Tiff> {
100
+ private async readHeader(options?: TiffFetchOptions): Promise<Tiff> {
96
101
  if (this.isInitialized) return this;
97
102
  // limit the read to the size of the file if it is known, for small tiffs
98
103
  const bytes = new DataView(
99
- await this.source.fetch(0, getMaxLength(this.source, 0, this.defaultReadSize)),
104
+ await this.source.fetch(0, getMaxLength(this.source, 0, this.defaultReadSize), options),
100
105
  ) as DataViewOffset;
101
106
  if (bytes.byteLength === 0) throw new Error('Unable to read empty tiff');
102
107
 
@@ -144,6 +149,7 @@ export class Tiff {
144
149
  const bytes = await this.source.fetch(
145
150
  nextOffsetIfd,
146
151
  getMaxLength(this.source, nextOffsetIfd, this.defaultReadSize),
152
+ options,
147
153
  );
148
154
  lastView = new DataView(bytes) as DataViewOffset;
149
155
  lastView.sourceOffset = nextOffsetIfd;
@@ -151,7 +157,7 @@ export class Tiff {
151
157
  nextOffsetIfd = this.readIfd(nextOffsetIfd, lastView);
152
158
  }
153
159
 
154
- await Promise.all(this.images.map((i) => i.init()));
160
+ await Promise.all(this.images.map((i) => i.init(true, options)));
155
161
  this.isInitialized = true;
156
162
  return this;
157
163
  }