Skip to content

Commit 317f3a7

Browse files
committed
Updates
1 parent dc7aeaf commit 317f3a7

File tree

2 files changed

+12
-64
lines changed

2 files changed

+12
-64
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default tseslint.config(
5656
'unicorn/prefer-modern-math-apis': 'off',
5757
'unicorn/prefer-node-protocol': 'off',
5858
'unicorn/no-unreadable-array-destructuring': 'off',
59+
'unicorn/text-encoding-identifier-case': 'off',
5960
'unicorn/no-abusive-eslint-disable': 'off',
6061
'unicorn/no-array-callback-reference': 'off',
6162
'unicorn/number-literal-case': 'off',

src/remoteFile.ts

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default class RemoteFile implements GenericFilehandle {
9797
position: number,
9898
opts: FilehandleOptions = {},
9999
): Promise<Uint8Array<ArrayBuffer>> {
100-
const { headers = {}, signal, overrides = {}, statusCallback } = opts
100+
const { headers = {}, signal, overrides = {} } = opts
101101
if (length < Infinity) {
102102
headers.range = `bytes=${position}-${position + length}`
103103
} else if (length === Infinity && position !== 0) {
@@ -122,67 +122,15 @@ export default class RemoteFile implements GenericFilehandle {
122122
}
123123

124124
if ((res.status === 200 && position === 0) || res.status === 206) {
125-
// Get the total size for progress reporting
126-
const contentLength = res.headers.get('content-length')
127-
const totalBytes = contentLength ? parseInt(contentLength, 10) : undefined
128-
129-
// Use ReadableStream API for progress reporting if statusCallback is provided
130-
if (statusCallback && res.body) {
131-
const reader = res.body.getReader()
132-
const chunks: Uint8Array[] = []
133-
let receivedBytes = 0
134-
135-
// eslint-disable-next-line no-constant-condition
136-
while (true) {
137-
const { done, value } = await reader.read()
138-
139-
if (done) {
140-
break
141-
}
142-
143-
chunks.push(value)
144-
receivedBytes += value.length
145-
146-
if (statusCallback && totalBytes) {
147-
statusCallback(
148-
`Downloading ${getProgressDisplayStr(receivedBytes, totalBytes)}`,
149-
)
150-
}
151-
}
152-
153-
// Concatenate chunks
154-
const chunksAll = new Uint8Array(receivedBytes)
155-
let position = 0
156-
for (const chunk of chunks) {
157-
chunksAll.set(chunk, position)
158-
position += chunk.length
125+
// try to parse out the size of the remote file
126+
const contentRange = res.headers.get('content-range')
127+
const sizeMatch = /\/(\d+)$/.exec(contentRange || '')
128+
if (sizeMatch?.[1]) {
129+
this._stat = {
130+
size: parseInt(sizeMatch[1], 10),
159131
}
160-
161-
// try to parse out the size of the remote file
162-
const contentRange = res.headers.get('content-range')
163-
const sizeMatch = /\/(\d+)$/.exec(contentRange || '')
164-
if (sizeMatch?.[1]) {
165-
this._stat = {
166-
size: parseInt(sizeMatch[1], 10),
167-
}
168-
}
169-
170-
return chunksAll.slice(0, length)
171-
} else {
172-
// If no statusCallback, use the simpler approach
173-
const resData = await res.arrayBuffer()
174-
175-
// try to parse out the size of the remote file
176-
const contentRange = res.headers.get('content-range')
177-
const sizeMatch = /\/(\d+)$/.exec(contentRange || '')
178-
if (sizeMatch?.[1]) {
179-
this._stat = {
180-
size: parseInt(sizeMatch[1], 10),
181-
}
182-
}
183-
184-
return new Uint8Array(resData.slice(0, length))
185132
}
133+
return new Uint8Array(await res.arrayBuffer())
186134
}
187135

188136
// eslint-disable-next-line unicorn/prefer-ternary
@@ -243,7 +191,7 @@ export default class RemoteFile implements GenericFilehandle {
243191
const chunks: Uint8Array[] = []
244192
let receivedBytes = 0
245193

246-
// eslint-disable-next-line no-constant-condition
194+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
247195
while (true) {
248196
const { done, value } = await reader.read()
249197

@@ -259,7 +207,7 @@ export default class RemoteFile implements GenericFilehandle {
259207
)
260208
}
261209

262-
if (encoding === 'utf8') {
210+
if (encoding === 'utf8' || encoding === 'utf-8') {
263211
const decoder = new TextDecoder('utf-8')
264212
return decoder.decode(concatUint8Array(chunks))
265213
} else if (encoding) {
@@ -268,8 +216,7 @@ export default class RemoteFile implements GenericFilehandle {
268216
return concatUint8Array(chunks)
269217
}
270218
} else {
271-
// If no statusCallback, use the simpler approach
272-
if (encoding === 'utf8') {
219+
if (encoding === 'utf8' || encoding === 'utf-8') {
273220
return res.text()
274221
} else if (encoding) {
275222
throw new Error(`unsupported encoding: ${encoding}`)

0 commit comments

Comments
 (0)