/
LazyLoadingBlobStream.ts
155 lines (137 loc) · 4.5 KB
/
LazyLoadingBlobStream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { Readable, ReadableOptions } from "stream";
import { BlobClient, CommonOptions } from "@azure/storage-blob";
import { AbortSignalLike } from "@azure/core-http";
import { createSpan } from "./utils/tracing";
import { SpanStatusCode } from "@azure/core-tracing";
/**
* Options to configure the LazyLoadingBlobStream.
*/
export interface LazyLoadingBlobStreamOptions extends ReadableOptions, CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
interface LazyLoadingBlobStreamDownloadBlockOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* This class generates a readable stream from a blobClient's data.
*/
export class LazyLoadingBlobStream extends Readable {
/**
* BlobClient to make download calls with.
*/
private readonly blobClient: BlobClient;
/**
* The offset within the blob of the next block we will download.
*/
private offset: number;
private readonly blockSize: number;
private lastDownloadBytes: number;
private lastDownloadData?: Buffer;
private blobLength: number;
private options?: LazyLoadingBlobStreamOptions;
/**
* Creates an instance of LazyLoadingBlobStream.
*
* @param byteLength - The total length of data contained in the buffers
*/
constructor(
blobClient: BlobClient,
offset: number,
blockSize: number,
options?: LazyLoadingBlobStreamOptions
) {
super(options);
this.blobClient = blobClient;
this.offset = offset;
this.blockSize = blockSize;
this.lastDownloadBytes = -1;
this.blobLength = -1;
this.options = options;
}
private async downloadBlock(options: LazyLoadingBlobStreamDownloadBlockOptions = {}) {
const { span, updatedOptions } = createSpan("LazyLoadingBlobStream-downloadBlock", options);
try {
const properties = await this.blobClient.getProperties({
abortSignal: options.abortSignal,
tracingOptions: updatedOptions.tracingOptions,
});
this.blobLength = properties.contentLength!;
this.lastDownloadBytes = Math.min(this.blockSize, this.blobLength - this.offset);
if (this.lastDownloadBytes === 0) {
this.lastDownloadData = undefined;
return;
}
this.lastDownloadData = await this.blobClient.downloadToBuffer(
this.offset,
this.lastDownloadBytes,
{
abortSignal: options.abortSignal,
tracingOptions: updatedOptions.tracingOptions,
}
);
this.offset += this.lastDownloadBytes;
} catch (e: any) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message,
});
throw e;
} finally {
span.end();
}
}
/**
* Internal _read() that will be called when the stream wants to pull more data in.
*
* @param size - Optional. The size of data to be read
*/
public async _read(size?: number): Promise<void> {
const { span, updatedOptions } = createSpan("LazyLoadingBlobStream-read", this.options);
try {
if (!size) {
size = this.readableHighWaterMark;
}
let count = 0;
let chunkSize = 0;
const chunksToPush = [];
do {
if (this.lastDownloadData === undefined || this.lastDownloadData?.byteLength === 0) {
await this.downloadBlock({
abortSignal: this.options?.abortSignal,
tracingOptions: updatedOptions?.tracingOptions,
});
}
if (this.lastDownloadData?.byteLength) {
chunkSize = Math.min(size - count, this.lastDownloadData?.byteLength);
chunksToPush.push(this.lastDownloadData.slice(0, chunkSize));
this.lastDownloadData = this.lastDownloadData.slice(chunkSize);
count += chunkSize;
} else {
chunkSize = 0;
}
} while (chunkSize > 0 && count < size);
this.push(Buffer.concat(chunksToPush));
if (count < size) {
this.push(null);
}
} catch (e: any) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message,
});
this.emit("error", e);
} finally {
span.end();
}
}
}