@milaboratories/pl-drivers 1.10.9 → 1.10.11

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.
Files changed (35) hide show
  1. package/dist/clients/download.cjs +1 -0
  2. package/dist/clients/download.cjs.map +1 -1
  3. package/dist/clients/download.d.ts +4 -3
  4. package/dist/clients/download.d.ts.map +1 -1
  5. package/dist/clients/download.js +1 -0
  6. package/dist/clients/download.js.map +1 -1
  7. package/dist/drivers/download_blob/download_blob.cjs +38 -12
  8. package/dist/drivers/download_blob/download_blob.cjs.map +1 -1
  9. package/dist/drivers/download_blob/download_blob.d.ts +8 -1
  10. package/dist/drivers/download_blob/download_blob.d.ts.map +1 -1
  11. package/dist/drivers/download_blob/download_blob.js +39 -13
  12. package/dist/drivers/download_blob/download_blob.js.map +1 -1
  13. package/dist/drivers/download_blob/sparse_cache/ranges.d.ts.map +1 -1
  14. package/dist/drivers/helpers/helpers.cjs.map +1 -1
  15. package/dist/drivers/helpers/helpers.d.ts.map +1 -1
  16. package/dist/drivers/helpers/helpers.js.map +1 -1
  17. package/dist/drivers/helpers/read_file.cjs +19 -11
  18. package/dist/drivers/helpers/read_file.cjs.map +1 -1
  19. package/dist/drivers/helpers/read_file.d.ts +6 -1
  20. package/dist/drivers/helpers/read_file.d.ts.map +1 -1
  21. package/dist/drivers/helpers/read_file.js +18 -11
  22. package/dist/drivers/helpers/read_file.js.map +1 -1
  23. package/dist/helpers/download.cjs +3 -0
  24. package/dist/helpers/download.cjs.map +1 -1
  25. package/dist/helpers/download.d.ts +2 -6
  26. package/dist/helpers/download.d.ts.map +1 -1
  27. package/dist/helpers/download.js +3 -0
  28. package/dist/helpers/download.js.map +1 -1
  29. package/package.json +8 -8
  30. package/src/clients/download.ts +5 -4
  31. package/src/drivers/download_blob/download_blob.test.ts +4 -4
  32. package/src/drivers/download_blob/download_blob.ts +68 -19
  33. package/src/drivers/helpers/helpers.ts +0 -9
  34. package/src/drivers/helpers/read_file.ts +29 -11
  35. package/src/helpers/download.ts +7 -7
@@ -1 +1 @@
1
- {"version":3,"file":"download_blob.js","sources":["../../../src/drivers/download_blob/download_blob.ts"],"sourcesContent":["import type {\n ComputableCtx,\n ComputableStableDefined,\n Watcher,\n} from '@milaboratories/computable';\nimport {\n ChangeSource,\n Computable,\n} from '@milaboratories/computable';\nimport type { ResourceId, ResourceType } from '@milaboratories/pl-client';\nimport { resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';\nimport type {\n AnyLogHandle,\n BlobDriver,\n LocalBlobHandle,\n LocalBlobHandleAndSize,\n ReadyLogHandle,\n RemoteBlobHandle,\n RemoteBlobHandleAndSize,\n StreamingApiResponse,\n} from '@milaboratories/pl-model-common';\nimport { type RangeBytes, validateRangeBytes } from '@milaboratories/pl-model-common';\nimport type {\n PlTreeEntry,\n ResourceInfo,\n ResourceSnapshot\n} from '@milaboratories/pl-tree';\nimport {\n isPlTreeEntry,\n makeResourceSnapshot,\n treeEntryToResourceInfo,\n} from '@milaboratories/pl-tree';\nimport type { MiLogger, Signer } from '@milaboratories/ts-helpers';\nimport { CallersCounter, mapGet, TaskProcessor } from '@milaboratories/ts-helpers';\nimport Denque from 'denque';\nimport * as fs from 'fs';\nimport { randomUUID } from 'node:crypto';\nimport * as fsp from 'node:fs/promises';\nimport * as os from 'node:os';\nimport * as path from 'node:path';\nimport * as readline from 'node:readline/promises';\nimport { Writable } from 'node:stream';\nimport { buffer } from 'node:stream/consumers';\nimport type { ClientDownload } from '../../clients/download';\nimport type { ClientLogs } from '../../clients/logs';\nimport { readFileContent } from '../helpers/read_file';\nimport {\n isLocalBlobHandle,\n newLocalHandle,\n parseLocalHandle,\n} from '../helpers/download_local_handle';\nimport {\n isRemoteBlobHandle,\n newRemoteHandle,\n parseRemoteHandle,\n} from '../helpers/download_remote_handle';\nimport { Updater, WrongResourceTypeError } from '../helpers/helpers';\nimport { getResourceInfoFromLogHandle, newLogHandle } from '../helpers/logs_handle';\nimport { getSize, OnDemandBlobResourceSnapshot } from '../types';\nimport { blobKey, pathToKey } from './blob_key';\nimport { DownloadBlobTask, nonRecoverableError } from './download_blob_task';\nimport { FilesCache } from '../helpers/files_cache';\nimport { SparseCache, SparseCacheFsFile, SparseCacheFsRanges } from './sparse_cache/cache';\n\nexport type DownloadDriverOps = {\n /**\n * A soft limit of the amount of blob storage, in bytes.\n * Once exceeded, the download driver will start deleting blobs one by one\n * when they become unneeded.\n * */\n cacheSoftSizeBytes: number;\n\n /**\n * A hard limit of the amount of sparse cache, in bytes.\n * Once exceeded, the download driver will start deleting blobs one by one.\n *\n * The sparse cache is used to store ranges of blobs.\n * */\n rangesCacheMaxSizeBytes: number;\n\n /**\n * Max number of concurrent downloads while calculating computable states\n * derived from this driver\n * */\n nConcurrentDownloads: number;\n};\n\n/** DownloadDriver holds a queue of downloading tasks,\n * and notifies every watcher when a file were downloaded. */\nexport class DownloadDriver implements BlobDriver {\n /** Represents a unique key to the path of a blob as a map. */\n private keyToDownload: Map<string, DownloadBlobTask> = new Map();\n\n /** Writes and removes files to a hard drive and holds a counter for every\n * file that should be kept. */\n private cache: FilesCache<DownloadBlobTask>;\n private rangesCache: SparseCache;\n\n /** Downloads files and writes them to the local dir. */\n private downloadQueue: TaskProcessor;\n\n private keyToOnDemand: Map<string, OnDemandBlobHolder> = new Map();\n\n private idToLastLines: Map<string, LastLinesGetter> = new Map();\n private idToProgressLog: Map<string, LastLinesGetter> = new Map();\n\n private readonly saveDir: string;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly clientDownload: ClientDownload,\n private readonly clientLogs: ClientLogs,\n saveDir: string,\n private readonly rangesCacheDir: string,\n private readonly signer: Signer,\n private readonly ops: DownloadDriverOps,\n ) {\n this.cache = new FilesCache(this.ops.cacheSoftSizeBytes);\n\n const fsRanges = new SparseCacheFsRanges(this.logger, this.rangesCacheDir);\n const fsStorage = new SparseCacheFsFile(this.logger, this.rangesCacheDir);\n this.rangesCache = new SparseCache(this.logger, this.ops.rangesCacheMaxSizeBytes, fsRanges, fsStorage);\n\n this.downloadQueue = new TaskProcessor(this.logger, ops.nConcurrentDownloads);\n\n this.saveDir = path.resolve(saveDir);\n }\n\n static async init(\n logger: MiLogger,\n clientDownload: ClientDownload,\n clientLogs: ClientLogs,\n saveDir: string,\n rangesCacheDir: string,\n signer: Signer,\n ops: DownloadDriverOps,\n ): Promise<DownloadDriver> {\n const driver = new DownloadDriver(logger, clientDownload, clientLogs, saveDir, rangesCacheDir, signer, ops);\n await driver.rangesCache.reset();\n\n return driver;\n }\n\n /** Gets a blob or part of the blob by its resource id or downloads a blob and sets it in a cache. */\n public getDownloadedBlob(\n res: ResourceInfo | PlTreeEntry,\n ctx: ComputableCtx,\n ): LocalBlobHandleAndSize | undefined;\n public getDownloadedBlob(\n res: ResourceInfo | PlTreeEntry,\n ): ComputableStableDefined<LocalBlobHandleAndSize>;\n public getDownloadedBlob(\n res: ResourceInfo | PlTreeEntry,\n ctx?: ComputableCtx,\n ): Computable<LocalBlobHandleAndSize | undefined> | LocalBlobHandleAndSize | undefined {\n if (ctx === undefined) {\n return Computable.make((ctx) => this.getDownloadedBlob(res, ctx));\n }\n\n const rInfo = treeEntryToResourceInfo(res, ctx);\n\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseBlob(rInfo, callerId));\n\n const result = this.getDownloadedBlobNoCtx(ctx.watcher, rInfo as ResourceSnapshot, callerId);\n if (result == undefined) {\n ctx.markUnstable('download blob is still undefined');\n }\n\n return result;\n }\n\n private getDownloadedBlobNoCtx(\n w: Watcher,\n rInfo: ResourceSnapshot,\n callerId: string,\n ): LocalBlobHandleAndSize | undefined {\n validateDownloadableResourceType('getDownloadedBlob', rInfo.type);\n\n // We don't need to request files with wider limits,\n // PFrame's engine does it disk-optimally by itself.\n\n const task = this.getOrSetNewTask(rInfo, callerId);\n task.attach(w, callerId);\n\n const result = task.getBlob();\n if (!result.done) {\n return undefined;\n }\n if (result.result.ok) {\n return result.result.value;\n }\n throw result.result.error;\n }\n\n private getOrSetNewTask(\n rInfo: ResourceSnapshot,\n callerId: string,\n ): DownloadBlobTask {\n const key = blobKey(rInfo.id);\n\n const inMemoryTask = this.keyToDownload.get(key);\n if (inMemoryTask) {\n return inMemoryTask;\n }\n\n // schedule the blob downloading, then it'll be added to the cache.\n const fPath = path.resolve(this.saveDir, key);\n\n const newTask = new DownloadBlobTask(\n this.logger,\n this.clientDownload,\n rInfo,\n newLocalHandle(fPath, this.signer),\n fPath,\n );\n this.keyToDownload.set(key, newTask);\n\n this.downloadQueue.push({\n fn: () => this.downloadBlob(newTask, callerId),\n recoverableErrorPredicate: (e) => !nonRecoverableError(e),\n });\n\n return newTask;\n }\n\n private async downloadBlob(task: DownloadBlobTask, callerId: string) {\n await task.download();\n const blob = task.getBlob();\n if (blob.done && blob.result.ok) {\n this.cache.addCache(task, callerId);\n }\n }\n\n /** Gets on demand blob. */\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ): Computable<RemoteBlobHandleAndSize>;\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ctx?: undefined,\n fromBytes?: number,\n toBytes?: number,\n ): Computable<RemoteBlobHandleAndSize>;\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ctx: ComputableCtx,\n fromBytes?: number,\n toBytes?: number,\n ): RemoteBlobHandleAndSize;\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ctx?: ComputableCtx,\n ): ComputableStableDefined<RemoteBlobHandleAndSize> | RemoteBlobHandleAndSize | undefined {\n if (ctx === undefined) return Computable.make((ctx) => this.getOnDemandBlob(res, ctx));\n\n const rInfo: OnDemandBlobResourceSnapshot = isPlTreeEntry(res)\n ? makeResourceSnapshot(res, OnDemandBlobResourceSnapshot, ctx)\n : res;\n\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseOnDemandBlob(rInfo.id, callerId));\n\n // note that the watcher is not needed,\n // the handler never changes.\n const result = this.getOnDemandBlobNoCtx(rInfo, callerId);\n\n return result;\n }\n\n private getOnDemandBlobNoCtx(\n info: OnDemandBlobResourceSnapshot,\n callerId: string,\n ): RemoteBlobHandleAndSize {\n validateDownloadableResourceType('getOnDemandBlob', info.type);\n\n let blob = this.keyToOnDemand.get(blobKey(info.id));\n\n if (blob === undefined) {\n blob = new OnDemandBlobHolder(getSize(info), newRemoteHandle(info, this.signer));\n this.keyToOnDemand.set(blobKey(info.id), blob);\n }\n\n blob.attach(callerId);\n\n return blob.getHandle();\n }\n\n /** Gets a path from a handle. */\n public getLocalPath(handle: LocalBlobHandle): string {\n const { path } = parseLocalHandle(handle, this.signer);\n return path;\n }\n\n /** Gets a content of a blob by a handle. */\n public async getContent(handle: LocalBlobHandle | RemoteBlobHandle, range?: RangeBytes): Promise<Uint8Array> {\n if (range) {\n validateRangeBytes(range, `getContent`);\n }\n\n if (isLocalBlobHandle(handle)) {\n return await readFileContent(this.getLocalPath(handle), range);\n }\n\n if (isRemoteBlobHandle(handle)) {\n const result = parseRemoteHandle(handle, this.signer);\n\n const key = blobKey(result.info.id);\n const filePath = await this.rangesCache.get(key, range ?? { from: 0, to: result.size });\n if (filePath) {\n return await readFileContent(filePath, range);\n }\n\n const data = await this.clientDownload.withBlobContent(\n { id: result.info.id, type: result.info.type },\n undefined,\n { range },\n async (content) => await buffer(content)\n );\n await this.rangesCache.set(key, range ?? { from: 0, to: result.size }, data);\n\n return data;\n }\n\n throw new Error('Malformed remote handle');\n }\n\n /**\n * Creates computable that will return blob content once it is downloaded.\n * Uses downloaded blob handle under the hood, so stores corresponding blob in file system.\n */\n public getComputableContent(\n res: ResourceInfo | PlTreeEntry,\n range?: RangeBytes,\n ): ComputableStableDefined<Uint8Array> {\n if (range) {\n validateRangeBytes(range, `getComputableContent`);\n }\n\n return Computable.make((ctx) =>\n this.getDownloadedBlob(res, ctx), {\n postprocessValue: (v) => v ? this.getContent(v.handle, range) : undefined\n }\n ).withStableType()\n }\n\n /** Returns all logs and schedules a job that reads remain logs.\n * Notifies when a new portion of the log appeared. */\n public getLastLogs(\n res: ResourceInfo | PlTreeEntry,\n lines: number\n ): Computable<string | undefined>;\n public getLastLogs(\n res: ResourceInfo | PlTreeEntry,\n lines: number,\n ctx: ComputableCtx\n ): Computable<string | undefined>;\n public getLastLogs(\n res: ResourceInfo | PlTreeEntry,\n lines: number,\n ctx?: ComputableCtx,\n ): Computable<string | undefined> | string | undefined {\n if (ctx == undefined) return Computable.make((ctx) => this.getLastLogs(res, lines, ctx));\n\n const r = treeEntryToResourceInfo(res, ctx);\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseBlob(r, callerId));\n\n const result = this.getLastLogsNoCtx(ctx.watcher, r as ResourceSnapshot, lines, callerId);\n if (result == undefined)\n ctx.markUnstable('either a file was not downloaded or logs was not read');\n\n return result;\n }\n\n private getLastLogsNoCtx(\n w: Watcher,\n rInfo: ResourceSnapshot,\n lines: number,\n callerId: string,\n ): string | undefined {\n validateDownloadableResourceType('getLastLogs', rInfo.type);\n const blob = this.getDownloadedBlobNoCtx(w, rInfo, callerId);\n if (blob == undefined) return undefined;\n\n const { path } = parseLocalHandle(blob.handle, this.signer);\n\n let logGetter = this.idToLastLines.get(blobKey(rInfo.id));\n\n if (logGetter == undefined) {\n const newLogGetter = new LastLinesGetter(path, lines);\n this.idToLastLines.set(blobKey(rInfo.id), newLogGetter);\n logGetter = newLogGetter;\n }\n\n const result = logGetter.getOrSchedule(w);\n if (result.error) throw result.error;\n\n return result.log;\n }\n\n /** Returns a last line that has patternToSearch.\n * Notifies when a new line appeared or EOF reached. */\n public getProgressLog(\n res: ResourceInfo | PlTreeEntry,\n patternToSearch: string\n ): Computable<string | undefined>;\n public getProgressLog(\n res: ResourceInfo | PlTreeEntry,\n patternToSearch: string,\n ctx: ComputableCtx\n ): string | undefined;\n public getProgressLog(\n res: ResourceInfo | PlTreeEntry,\n patternToSearch: string,\n ctx?: ComputableCtx,\n ): Computable<string | undefined> | string | undefined {\n if (ctx == undefined)\n return Computable.make((ctx) => this.getProgressLog(res, patternToSearch, ctx));\n\n const r = treeEntryToResourceInfo(res, ctx);\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseBlob(r, callerId));\n\n const result = this.getProgressLogNoCtx(\n ctx.watcher,\n r as ResourceSnapshot,\n patternToSearch,\n callerId,\n );\n if (result === undefined)\n ctx.markUnstable('either a file was not downloaded or a progress log was not read');\n\n return result;\n }\n\n private getProgressLogNoCtx(\n w: Watcher,\n rInfo: ResourceSnapshot,\n patternToSearch: string,\n callerId: string,\n ): string | undefined {\n validateDownloadableResourceType('getProgressLog', rInfo.type);\n\n const blob = this.getDownloadedBlobNoCtx(w, rInfo, callerId);\n if (blob == undefined) return undefined;\n const { path } = parseLocalHandle(blob.handle, this.signer);\n\n let logGetter = this.idToProgressLog.get(blobKey(rInfo.id));\n\n if (logGetter == undefined) {\n const newLogGetter = new LastLinesGetter(path, 1, patternToSearch);\n this.idToProgressLog.set(blobKey(rInfo.id), newLogGetter);\n\n logGetter = newLogGetter;\n }\n\n const result = logGetter.getOrSchedule(w);\n if (result.error) throw result.error;\n\n return result.log;\n }\n\n /** Returns an Id of a smart object, that can read logs directly from\n * the platform. */\n public getLogHandle(res: ResourceInfo | PlTreeEntry): Computable<AnyLogHandle>;\n public getLogHandle(res: ResourceInfo | PlTreeEntry, ctx: ComputableCtx): AnyLogHandle;\n public getLogHandle(\n res: ResourceInfo | PlTreeEntry,\n ctx?: ComputableCtx,\n ): Computable<AnyLogHandle> | AnyLogHandle {\n if (ctx == undefined) return Computable.make((ctx) => this.getLogHandle(res, ctx));\n\n const r = treeEntryToResourceInfo(res, ctx);\n\n return this.getLogHandleNoCtx(r as ResourceSnapshot);\n }\n\n private getLogHandleNoCtx(rInfo: ResourceSnapshot): AnyLogHandle {\n validateDownloadableResourceType('getLogHandle', rInfo.type);\n return newLogHandle(false, rInfo);\n }\n\n public async lastLines(\n handle: ReadyLogHandle,\n lineCount: number,\n offsetBytes?: number, // if 0n, then start from the end.\n searchStr?: string,\n ): Promise<StreamingApiResponse> {\n const resp = await this.clientLogs.lastLines(\n getResourceInfoFromLogHandle(handle),\n lineCount,\n BigInt(offsetBytes ?? 0),\n searchStr,\n );\n\n return {\n live: false,\n shouldUpdateHandle: false,\n data: resp.data,\n size: Number(resp.size),\n newOffset: Number(resp.newOffset),\n };\n }\n\n public async readText(\n handle: ReadyLogHandle,\n lineCount: number,\n offsetBytes?: number,\n searchStr?: string,\n ): Promise<StreamingApiResponse> {\n const resp = await this.clientLogs.readText(\n getResourceInfoFromLogHandle(handle),\n lineCount,\n BigInt(offsetBytes ?? 0),\n searchStr,\n );\n\n return {\n live: false,\n shouldUpdateHandle: false,\n data: resp.data,\n size: Number(resp.size),\n newOffset: Number(resp.newOffset),\n };\n }\n\n private async releaseBlob(rInfo: ResourceInfo, callerId: string) {\n const task = this.keyToDownload.get(blobKey(rInfo.id));\n if (task == undefined) {\n return;\n }\n\n if (this.cache.existsFile(blobKey(rInfo.id))) {\n const toDelete = this.cache.removeFile(blobKey(rInfo.id), callerId);\n\n await Promise.all(\n toDelete.map(async (cachedFile) => {\n await fsp.rm(cachedFile.path);\n\n this.cache.removeCache(cachedFile);\n\n this.removeTask(\n mapGet(this.keyToDownload, pathToKey(cachedFile.path)),\n `the task ${stringifyWithResourceId(cachedFile)} was removed`\n + `from cache along with ${stringifyWithResourceId(toDelete.map((d) => d.path))}`,\n );\n }),\n );\n } else {\n // The task is still in a downloading queue.\n const deleted = task.counter.dec(callerId);\n if (deleted) {\n this.removeTask(\n task,\n `the task ${stringifyWithResourceId(task.info())} was removed from cache`,\n );\n }\n }\n }\n\n private removeTask(task: DownloadBlobTask, reason: string) {\n task.abort(reason);\n task.change.markChanged(`download task for ${task.path} removed: ${reason}`);\n this.keyToDownload.delete(pathToKey(task.path));\n this.idToLastLines.delete(blobKey(task.rInfo.id));\n this.idToProgressLog.delete(blobKey(task.rInfo.id));\n }\n\n private async releaseOnDemandBlob(blobId: ResourceId, callerId: string) {\n const deleted = this.keyToOnDemand.get(blobKey(blobId))?.release(callerId) ?? false;\n if (deleted) this.keyToOnDemand.delete(blobKey(blobId));\n }\n\n /** Removes all files from a hard drive. */\n async releaseAll() {\n this.downloadQueue.stop();\n\n this.keyToDownload.forEach((task, key) => {\n this.keyToDownload.delete(key);\n task.change.markChanged(`task ${resourceIdToString(task.rInfo.id)} released`);\n });\n }\n}\n\n/** Keeps a counter to the on demand handle. */\nclass OnDemandBlobHolder {\n private readonly counter = new CallersCounter();\n\n constructor(\n private readonly size: number,\n private readonly handle: RemoteBlobHandle,\n ) {}\n\n public getHandle(): RemoteBlobHandleAndSize {\n return { handle: this.handle, size: this.size };\n }\n\n public attach(callerId: string) {\n this.counter.inc(callerId);\n }\n\n public release(callerId: string): boolean {\n return this.counter.dec(callerId);\n }\n}\n\nclass LastLinesGetter {\n private updater: Updater;\n private log: string | undefined;\n private readonly change: ChangeSource = new ChangeSource();\n private error: any | undefined = undefined;\n\n constructor(\n private readonly path: string,\n private readonly lines: number,\n private readonly patternToSearch?: string,\n ) {\n this.updater = new Updater(async () => this.update());\n }\n\n getOrSchedule(w: Watcher): {\n log: string | undefined;\n error?: any | undefined;\n } {\n this.change.attachWatcher(w);\n\n this.updater.schedule();\n\n return {\n log: this.log,\n error: this.error,\n };\n }\n\n async update(): Promise<void> {\n try {\n const newLogs = await getLastLines(this.path, this.lines, this.patternToSearch);\n\n if (this.log != newLogs) this.change.markChanged(`logs for ${this.path} updated`);\n this.log = newLogs;\n } catch (e: any) {\n if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {\n // No resource\n this.log = '';\n this.error = e;\n this.change.markChanged(`log update for ${this.path} failed, resource not found`);\n return;\n }\n\n throw e;\n }\n }\n}\n\n/** Gets last lines from a file by reading the file from the top and keeping\n * last N lines in a window queue. */\nasync function getLastLines(fPath: string, nLines: number, patternToSearch?: string): Promise<string> {\n let inStream: fs.ReadStream | undefined;\n let rl: readline.Interface | undefined;\n\n try {\n inStream = fs.createReadStream(fPath);\n rl = readline.createInterface({ input: inStream, crlfDelay: Infinity });\n\n const lines = new Denque();\n\n for await (const line of rl) {\n if (patternToSearch != undefined && !line.includes(patternToSearch)) continue;\n\n lines.push(line);\n if (lines.length > nLines) {\n lines.shift();\n }\n }\n\n // last EOL is for keeping backward compat with platforma implementation.\n return lines.toArray().join(os.EOL) + os.EOL;\n } finally {\n // Cleanup resources in finally block to ensure they're always cleaned up\n try {\n if (rl) {\n rl.close();\n }\n } catch (cleanupError) {\n console.error('Error closing readline interface:', cleanupError);\n }\n\n try {\n if (inStream && !inStream.destroyed) {\n inStream.destroy();\n }\n } catch (cleanupError) {\n console.error('Error destroying read stream:', cleanupError);\n }\n }\n}\n\nfunction validateDownloadableResourceType(methodName: string, rType: ResourceType) {\n if (!rType.name.startsWith('Blob/')) {\n let message = `${methodName}: wrong resource type: ${rType.name}, expected: a resource of type that starts with 'Blob/'.`;\n if (rType.name == 'Blob')\n message += ` If it's called from workflow, should a file be exported with 'file.exportFile' function?`;\n\n throw new WrongResourceTypeError(message);\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAuFA;AAC6D;MAChD,cAAc,CAAA;AAoBN,IAAA,MAAA;AACA,IAAA,cAAA;AACA,IAAA,UAAA;AAEA,IAAA,cAAA;AACA,IAAA,MAAA;AACA,IAAA,GAAA;;AAxBX,IAAA,aAAa,GAAkC,IAAI,GAAG,EAAE;AAEhE;AAC+B;AACvB,IAAA,KAAK;AACL,IAAA,WAAW;;AAGX,IAAA,aAAa;AAEb,IAAA,aAAa,GAAoC,IAAI,GAAG,EAAE;AAE1D,IAAA,aAAa,GAAiC,IAAI,GAAG,EAAE;AACvD,IAAA,eAAe,GAAiC,IAAI,GAAG,EAAE;AAEhD,IAAA,OAAO;AAExB,IAAA,WAAA,CACmB,MAAgB,EAChB,cAA8B,EAC9B,UAAsB,EACvC,OAAe,EACE,cAAsB,EACtB,MAAc,EACd,GAA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ErE,YAAA,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;AAChB,YAAA,IAAI,KAAK,CAAC,MAAM,GAAG,MAAM,EAAE;gBACzB,KAAK,CAAC,KAAK,EAAE;YACf;QACF;;AAGA,QAAA,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG;IAC9C;YAAU;;AAER,QAAA,IAAI;YACF,IAAI,EAAE,EAAE;gBACN,EAAE,CAAC,KAAK,EAAE;YACZ;QACF;QAAE,OAAO,YAAY,EAAE;AACrB,YAAA,OAAO,CAAC,KAAK,CAAC,mCAAmC,EAAE,YAAY,CAAC;QAClE;AAEA,QAAA,IAAI;AACF,YAAA,IAAI,QAAQ,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE;gBACnC,QAAQ,CAAC,OAAO,EAAE;YACpB;QACF;QAAE,OAAO,YAAY,EAAE;AACrB,YAAA,OAAO,CAAC,KAAK,CAAC,+BAA+B,EAAE,YAAY,CAAC;QAC9D;IACF;AACF;AAEA,SAAS,gCAAgC,CAAC,UAAkB,EAAE,KAAmB,EAAA;IAC/E,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;QACnC,IAAI,OAAO,GAAG,CAAA,EAAG,UAAU,0BAA0B,KAAK,CAAC,IAAI,CAAA,wDAAA,CAA0D;AACzH,QAAA,IAAI,KAAK,CAAC,IAAI,IAAI,MAAM;YACtB,OAAO,IAAI,2FAA2F;AAExG,QAAA,MAAM,IAAI,sBAAsB,CAAC,OAAO,CAAC;IAC3C;AACF;;;;"}
1
+ {"version":3,"file":"download_blob.js","sources":["../../../src/drivers/download_blob/download_blob.ts"],"sourcesContent":["import type {\n ComputableCtx,\n ComputableStableDefined,\n Watcher,\n} from '@milaboratories/computable';\nimport {\n ChangeSource,\n Computable,\n} from '@milaboratories/computable';\nimport type { ResourceId, ResourceType } from '@milaboratories/pl-client';\nimport { resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';\nimport type {\n AnyLogHandle,\n BlobDriver,\n ContentHandler,\n GetContentOptions,\n LocalBlobHandle,\n LocalBlobHandleAndSize,\n ReadyLogHandle,\n RemoteBlobHandle,\n RemoteBlobHandleAndSize,\n StreamingApiResponse,\n} from '@milaboratories/pl-model-common';\nimport { type RangeBytes, validateRangeBytes } from '@milaboratories/pl-model-common';\nimport type {\n PlTreeEntry,\n ResourceInfo,\n ResourceSnapshot\n} from '@milaboratories/pl-tree';\nimport {\n isPlTreeEntry,\n makeResourceSnapshot,\n treeEntryToResourceInfo,\n} from '@milaboratories/pl-tree';\nimport type { MiLogger, Signer } from '@milaboratories/ts-helpers';\nimport { CallersCounter, mapGet, TaskProcessor } from '@milaboratories/ts-helpers';\nimport Denque from 'denque';\nimport * as fs from 'fs';\nimport { randomUUID } from 'node:crypto';\nimport * as fsp from 'node:fs/promises';\nimport * as os from 'node:os';\nimport * as path from 'node:path';\nimport * as readline from 'node:readline/promises';\nimport { buffer } from 'node:stream/consumers';\nimport { Readable } from 'node:stream';\nimport type { ClientDownload } from '../../clients/download';\nimport type { ClientLogs } from '../../clients/logs';\nimport { withFileContent } from '../helpers/read_file';\nimport {\n isLocalBlobHandle,\n newLocalHandle,\n parseLocalHandle,\n} from '../helpers/download_local_handle';\nimport {\n isRemoteBlobHandle,\n newRemoteHandle,\n parseRemoteHandle,\n} from '../helpers/download_remote_handle';\nimport { Updater, WrongResourceTypeError } from '../helpers/helpers';\nimport { getResourceInfoFromLogHandle, newLogHandle } from '../helpers/logs_handle';\nimport { getSize, OnDemandBlobResourceSnapshot } from '../types';\nimport { blobKey, pathToKey } from './blob_key';\nimport { DownloadBlobTask, nonRecoverableError } from './download_blob_task';\nimport { FilesCache } from '../helpers/files_cache';\nimport { SparseCache, SparseCacheFsFile, SparseCacheFsRanges } from './sparse_cache/cache';\n\nexport type DownloadDriverOps = {\n /**\n * A soft limit of the amount of blob storage, in bytes.\n * Once exceeded, the download driver will start deleting blobs one by one\n * when they become unneeded.\n * */\n cacheSoftSizeBytes: number;\n\n /**\n * A hard limit of the amount of sparse cache, in bytes.\n * Once exceeded, the download driver will start deleting blobs one by one.\n *\n * The sparse cache is used to store ranges of blobs.\n * */\n rangesCacheMaxSizeBytes: number;\n\n /**\n * Max number of concurrent downloads while calculating computable states\n * derived from this driver\n * */\n nConcurrentDownloads: number;\n};\n\n/** DownloadDriver holds a queue of downloading tasks,\n * and notifies every watcher when a file were downloaded. */\nexport class DownloadDriver implements BlobDriver {\n /** Represents a unique key to the path of a blob as a map. */\n private keyToDownload: Map<string, DownloadBlobTask> = new Map();\n\n /** Writes and removes files to a hard drive and holds a counter for every\n * file that should be kept. */\n private cache: FilesCache<DownloadBlobTask>;\n private rangesCache: SparseCache;\n\n /** Downloads files and writes them to the local dir. */\n private downloadQueue: TaskProcessor;\n\n private keyToOnDemand: Map<string, OnDemandBlobHolder> = new Map();\n\n private idToLastLines: Map<string, LastLinesGetter> = new Map();\n private idToProgressLog: Map<string, LastLinesGetter> = new Map();\n\n private readonly saveDir: string;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly clientDownload: ClientDownload,\n private readonly clientLogs: ClientLogs,\n saveDir: string,\n private readonly rangesCacheDir: string,\n private readonly signer: Signer,\n private readonly ops: DownloadDriverOps,\n ) {\n this.cache = new FilesCache(this.ops.cacheSoftSizeBytes);\n\n const fsRanges = new SparseCacheFsRanges(this.logger, this.rangesCacheDir);\n const fsStorage = new SparseCacheFsFile(this.logger, this.rangesCacheDir);\n this.rangesCache = new SparseCache(this.logger, this.ops.rangesCacheMaxSizeBytes, fsRanges, fsStorage);\n\n this.downloadQueue = new TaskProcessor(this.logger, ops.nConcurrentDownloads);\n\n this.saveDir = path.resolve(saveDir);\n }\n\n static async init(\n logger: MiLogger,\n clientDownload: ClientDownload,\n clientLogs: ClientLogs,\n saveDir: string,\n rangesCacheDir: string,\n signer: Signer,\n ops: DownloadDriverOps,\n ): Promise<DownloadDriver> {\n const driver = new DownloadDriver(logger, clientDownload, clientLogs, saveDir, rangesCacheDir, signer, ops);\n await driver.rangesCache.reset();\n\n return driver;\n }\n\n /** Gets a blob or part of the blob by its resource id or downloads a blob and sets it in a cache. */\n public getDownloadedBlob(\n res: ResourceInfo | PlTreeEntry,\n ctx: ComputableCtx,\n ): LocalBlobHandleAndSize | undefined;\n public getDownloadedBlob(\n res: ResourceInfo | PlTreeEntry,\n ): ComputableStableDefined<LocalBlobHandleAndSize>;\n public getDownloadedBlob(\n res: ResourceInfo | PlTreeEntry,\n ctx?: ComputableCtx,\n ): Computable<LocalBlobHandleAndSize | undefined> | LocalBlobHandleAndSize | undefined {\n if (ctx === undefined) {\n return Computable.make((ctx) => this.getDownloadedBlob(res, ctx));\n }\n\n const rInfo = treeEntryToResourceInfo(res, ctx);\n\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseBlob(rInfo, callerId));\n\n const result = this.getDownloadedBlobNoCtx(ctx.watcher, rInfo as ResourceSnapshot, callerId);\n if (result == undefined) {\n ctx.markUnstable('download blob is still undefined');\n }\n\n return result;\n }\n\n private getDownloadedBlobNoCtx(\n w: Watcher,\n rInfo: ResourceSnapshot,\n callerId: string,\n ): LocalBlobHandleAndSize | undefined {\n validateDownloadableResourceType('getDownloadedBlob', rInfo.type);\n\n // We don't need to request files with wider limits,\n // PFrame's engine does it disk-optimally by itself.\n\n const task = this.getOrSetNewTask(rInfo, callerId);\n task.attach(w, callerId);\n\n const result = task.getBlob();\n if (!result.done) {\n return undefined;\n }\n if (result.result.ok) {\n return result.result.value;\n }\n throw result.result.error;\n }\n\n private getOrSetNewTask(\n rInfo: ResourceSnapshot,\n callerId: string,\n ): DownloadBlobTask {\n const key = blobKey(rInfo.id);\n\n const inMemoryTask = this.keyToDownload.get(key);\n if (inMemoryTask) {\n return inMemoryTask;\n }\n\n // schedule the blob downloading, then it'll be added to the cache.\n const fPath = path.resolve(this.saveDir, key);\n\n const newTask = new DownloadBlobTask(\n this.logger,\n this.clientDownload,\n rInfo,\n newLocalHandle(fPath, this.signer),\n fPath,\n );\n this.keyToDownload.set(key, newTask);\n\n this.downloadQueue.push({\n fn: () => this.downloadBlob(newTask, callerId),\n recoverableErrorPredicate: (e) => !nonRecoverableError(e),\n });\n\n return newTask;\n }\n\n private async downloadBlob(task: DownloadBlobTask, callerId: string) {\n await task.download();\n const blob = task.getBlob();\n if (blob.done && blob.result.ok) {\n this.cache.addCache(task, callerId);\n }\n }\n\n /** Gets on demand blob. */\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ): Computable<RemoteBlobHandleAndSize>;\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ctx?: undefined,\n fromBytes?: number,\n toBytes?: number,\n ): Computable<RemoteBlobHandleAndSize>;\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ctx: ComputableCtx,\n fromBytes?: number,\n toBytes?: number,\n ): RemoteBlobHandleAndSize;\n public getOnDemandBlob(\n res: OnDemandBlobResourceSnapshot | PlTreeEntry,\n ctx?: ComputableCtx,\n ): ComputableStableDefined<RemoteBlobHandleAndSize> | RemoteBlobHandleAndSize | undefined {\n if (ctx === undefined) return Computable.make((ctx) => this.getOnDemandBlob(res, ctx));\n\n const rInfo: OnDemandBlobResourceSnapshot = isPlTreeEntry(res)\n ? makeResourceSnapshot(res, OnDemandBlobResourceSnapshot, ctx)\n : res;\n\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseOnDemandBlob(rInfo.id, callerId));\n\n // note that the watcher is not needed,\n // the handler never changes.\n const result = this.getOnDemandBlobNoCtx(rInfo, callerId);\n\n return result;\n }\n\n private getOnDemandBlobNoCtx(\n info: OnDemandBlobResourceSnapshot,\n callerId: string,\n ): RemoteBlobHandleAndSize {\n validateDownloadableResourceType('getOnDemandBlob', info.type);\n\n let blob = this.keyToOnDemand.get(blobKey(info.id));\n\n if (blob === undefined) {\n blob = new OnDemandBlobHolder(getSize(info), newRemoteHandle(info, this.signer));\n this.keyToOnDemand.set(blobKey(info.id), blob);\n }\n\n blob.attach(callerId);\n\n return blob.getHandle();\n }\n\n /** Gets a path from a handle. */\n public getLocalPath(handle: LocalBlobHandle): string {\n const { path } = parseLocalHandle(handle, this.signer);\n return path;\n }\n\n /** Gets a content of a blob by a handle. */\n public async getContent(handle: LocalBlobHandle | RemoteBlobHandle): Promise<Uint8Array>;\n public async getContent(\n handle: LocalBlobHandle | RemoteBlobHandle,\n options?: GetContentOptions,\n ): Promise<Uint8Array>;\n /** @deprecated Use {@link getContent} with {@link GetContentOptions} instead */\n public async getContent(\n handle: LocalBlobHandle | RemoteBlobHandle,\n range?: RangeBytes,\n ): Promise<Uint8Array>;\n public async getContent(\n handle: LocalBlobHandle | RemoteBlobHandle,\n optionsOrRange?: GetContentOptions | RangeBytes,\n ): Promise<Uint8Array> {\n let options: GetContentOptions = {};\n if (typeof optionsOrRange === 'object' && optionsOrRange !== null) {\n if ('range' in optionsOrRange) {\n options = optionsOrRange;\n } else {\n const range = optionsOrRange as RangeBytes;\n validateRangeBytes(range, `getContent`);\n options = { range };\n }\n }\n\n return await this.withContent(handle, {\n ...options,\n handler: async (content) => {\n const chunks: Uint8Array[] = [];\n for await (const chunk of content) {\n options.signal?.throwIfAborted();\n chunks.push(chunk);\n }\n return Buffer.concat(chunks);\n }\n });\n }\n\n /** Gets a content stream of a blob by a handle and calls handler with it. */\n public async withContent<T>(\n handle: LocalBlobHandle | RemoteBlobHandle,\n options: GetContentOptions & {\n handler: ContentHandler<T>;\n },\n ): Promise<T> {\n const { range, signal, handler } = options;\n\n if (isLocalBlobHandle(handle)) {\n return await withFileContent({ path: this.getLocalPath(handle), range, signal, handler });\n }\n\n if (isRemoteBlobHandle(handle)) {\n const result = parseRemoteHandle(handle, this.signer);\n\n const key = blobKey(result.info.id);\n const filePath = await this.rangesCache.get(key, range ?? { from: 0, to: result.size });\n signal?.throwIfAborted();\n\n if (filePath) return await withFileContent({ path: filePath, range, signal, handler });\n\n return await this.clientDownload.withBlobContent(\n result.info,\n { signal },\n options,\n async (content, size) => {\n const [handlerStream, cacheStream] = content.tee();\n \n const handlerPromise = handler(handlerStream, size);\n const _cachePromise = buffer(cacheStream)\n .then((data) => this.rangesCache.set(key, range ?? { from: 0, to: result.size }, data));\n\n return await handlerPromise;\n }\n );\n }\n\n throw new Error('Malformed remote handle');\n }\n\n /**\n * Creates computable that will return blob content once it is downloaded.\n * Uses downloaded blob handle under the hood, so stores corresponding blob in file system.\n */\n public getComputableContent(\n res: ResourceInfo | PlTreeEntry,\n range?: RangeBytes,\n ): ComputableStableDefined<Uint8Array> {\n if (range) {\n validateRangeBytes(range, `getComputableContent`);\n }\n\n return Computable.make((ctx) =>\n this.getDownloadedBlob(res, ctx), {\n postprocessValue: (v) => v ? this.getContent(v.handle, { range }) : undefined\n }\n ).withStableType()\n }\n\n /** Returns all logs and schedules a job that reads remain logs.\n * Notifies when a new portion of the log appeared. */\n public getLastLogs(\n res: ResourceInfo | PlTreeEntry,\n lines: number\n ): Computable<string | undefined>;\n public getLastLogs(\n res: ResourceInfo | PlTreeEntry,\n lines: number,\n ctx: ComputableCtx\n ): Computable<string | undefined>;\n public getLastLogs(\n res: ResourceInfo | PlTreeEntry,\n lines: number,\n ctx?: ComputableCtx,\n ): Computable<string | undefined> | string | undefined {\n if (ctx == undefined) return Computable.make((ctx) => this.getLastLogs(res, lines, ctx));\n\n const r = treeEntryToResourceInfo(res, ctx);\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseBlob(r, callerId));\n\n const result = this.getLastLogsNoCtx(ctx.watcher, r as ResourceSnapshot, lines, callerId);\n if (result == undefined)\n ctx.markUnstable('either a file was not downloaded or logs was not read');\n\n return result;\n }\n\n private getLastLogsNoCtx(\n w: Watcher,\n rInfo: ResourceSnapshot,\n lines: number,\n callerId: string,\n ): string | undefined {\n validateDownloadableResourceType('getLastLogs', rInfo.type);\n const blob = this.getDownloadedBlobNoCtx(w, rInfo, callerId);\n if (blob == undefined) return undefined;\n\n const { path } = parseLocalHandle(blob.handle, this.signer);\n\n let logGetter = this.idToLastLines.get(blobKey(rInfo.id));\n\n if (logGetter == undefined) {\n const newLogGetter = new LastLinesGetter(path, lines);\n this.idToLastLines.set(blobKey(rInfo.id), newLogGetter);\n logGetter = newLogGetter;\n }\n\n const result = logGetter.getOrSchedule(w);\n if (result.error) throw result.error;\n\n return result.log;\n }\n\n /** Returns a last line that has patternToSearch.\n * Notifies when a new line appeared or EOF reached. */\n public getProgressLog(\n res: ResourceInfo | PlTreeEntry,\n patternToSearch: string\n ): Computable<string | undefined>;\n public getProgressLog(\n res: ResourceInfo | PlTreeEntry,\n patternToSearch: string,\n ctx: ComputableCtx\n ): string | undefined;\n public getProgressLog(\n res: ResourceInfo | PlTreeEntry,\n patternToSearch: string,\n ctx?: ComputableCtx,\n ): Computable<string | undefined> | string | undefined {\n if (ctx == undefined)\n return Computable.make((ctx) => this.getProgressLog(res, patternToSearch, ctx));\n\n const r = treeEntryToResourceInfo(res, ctx);\n const callerId = randomUUID();\n ctx.addOnDestroy(() => this.releaseBlob(r, callerId));\n\n const result = this.getProgressLogNoCtx(\n ctx.watcher,\n r as ResourceSnapshot,\n patternToSearch,\n callerId,\n );\n if (result === undefined)\n ctx.markUnstable('either a file was not downloaded or a progress log was not read');\n\n return result;\n }\n\n private getProgressLogNoCtx(\n w: Watcher,\n rInfo: ResourceSnapshot,\n patternToSearch: string,\n callerId: string,\n ): string | undefined {\n validateDownloadableResourceType('getProgressLog', rInfo.type);\n\n const blob = this.getDownloadedBlobNoCtx(w, rInfo, callerId);\n if (blob == undefined) return undefined;\n const { path } = parseLocalHandle(blob.handle, this.signer);\n\n let logGetter = this.idToProgressLog.get(blobKey(rInfo.id));\n\n if (logGetter == undefined) {\n const newLogGetter = new LastLinesGetter(path, 1, patternToSearch);\n this.idToProgressLog.set(blobKey(rInfo.id), newLogGetter);\n\n logGetter = newLogGetter;\n }\n\n const result = logGetter.getOrSchedule(w);\n if (result.error) throw result.error;\n\n return result.log;\n }\n\n /** Returns an Id of a smart object, that can read logs directly from\n * the platform. */\n public getLogHandle(res: ResourceInfo | PlTreeEntry): Computable<AnyLogHandle>;\n public getLogHandle(res: ResourceInfo | PlTreeEntry, ctx: ComputableCtx): AnyLogHandle;\n public getLogHandle(\n res: ResourceInfo | PlTreeEntry,\n ctx?: ComputableCtx,\n ): Computable<AnyLogHandle> | AnyLogHandle {\n if (ctx == undefined) return Computable.make((ctx) => this.getLogHandle(res, ctx));\n\n const r = treeEntryToResourceInfo(res, ctx);\n\n return this.getLogHandleNoCtx(r as ResourceSnapshot);\n }\n\n private getLogHandleNoCtx(rInfo: ResourceSnapshot): AnyLogHandle {\n validateDownloadableResourceType('getLogHandle', rInfo.type);\n return newLogHandle(false, rInfo);\n }\n\n public async lastLines(\n handle: ReadyLogHandle,\n lineCount: number,\n offsetBytes?: number, // if 0n, then start from the end.\n searchStr?: string,\n ): Promise<StreamingApiResponse> {\n const resp = await this.clientLogs.lastLines(\n getResourceInfoFromLogHandle(handle),\n lineCount,\n BigInt(offsetBytes ?? 0),\n searchStr,\n );\n\n return {\n live: false,\n shouldUpdateHandle: false,\n data: resp.data,\n size: Number(resp.size),\n newOffset: Number(resp.newOffset),\n };\n }\n\n public async readText(\n handle: ReadyLogHandle,\n lineCount: number,\n offsetBytes?: number,\n searchStr?: string,\n ): Promise<StreamingApiResponse> {\n const resp = await this.clientLogs.readText(\n getResourceInfoFromLogHandle(handle),\n lineCount,\n BigInt(offsetBytes ?? 0),\n searchStr,\n );\n\n return {\n live: false,\n shouldUpdateHandle: false,\n data: resp.data,\n size: Number(resp.size),\n newOffset: Number(resp.newOffset),\n };\n }\n\n private async releaseBlob(rInfo: ResourceInfo, callerId: string) {\n const task = this.keyToDownload.get(blobKey(rInfo.id));\n if (task == undefined) {\n return;\n }\n\n if (this.cache.existsFile(blobKey(rInfo.id))) {\n const toDelete = this.cache.removeFile(blobKey(rInfo.id), callerId);\n\n await Promise.all(\n toDelete.map(async (cachedFile) => {\n await fsp.rm(cachedFile.path);\n\n this.cache.removeCache(cachedFile);\n\n this.removeTask(\n mapGet(this.keyToDownload, pathToKey(cachedFile.path)),\n `the task ${stringifyWithResourceId(cachedFile)} was removed`\n + `from cache along with ${stringifyWithResourceId(toDelete.map((d) => d.path))}`,\n );\n }),\n );\n } else {\n // The task is still in a downloading queue.\n const deleted = task.counter.dec(callerId);\n if (deleted) {\n this.removeTask(\n task,\n `the task ${stringifyWithResourceId(task.info())} was removed from cache`,\n );\n }\n }\n }\n\n private removeTask(task: DownloadBlobTask, reason: string) {\n task.abort(reason);\n task.change.markChanged(`download task for ${task.path} removed: ${reason}`);\n this.keyToDownload.delete(pathToKey(task.path));\n this.idToLastLines.delete(blobKey(task.rInfo.id));\n this.idToProgressLog.delete(blobKey(task.rInfo.id));\n }\n\n private async releaseOnDemandBlob(blobId: ResourceId, callerId: string) {\n const deleted = this.keyToOnDemand.get(blobKey(blobId))?.release(callerId) ?? false;\n if (deleted) this.keyToOnDemand.delete(blobKey(blobId));\n }\n\n /** Removes all files from a hard drive. */\n async releaseAll() {\n this.downloadQueue.stop();\n\n this.keyToDownload.forEach((task, key) => {\n this.keyToDownload.delete(key);\n task.change.markChanged(`task ${resourceIdToString(task.rInfo.id)} released`);\n });\n }\n}\n\n/** Keeps a counter to the on demand handle. */\nclass OnDemandBlobHolder {\n private readonly counter = new CallersCounter();\n\n constructor(\n private readonly size: number,\n private readonly handle: RemoteBlobHandle,\n ) {}\n\n public getHandle(): RemoteBlobHandleAndSize {\n return { handle: this.handle, size: this.size };\n }\n\n public attach(callerId: string) {\n this.counter.inc(callerId);\n }\n\n public release(callerId: string): boolean {\n return this.counter.dec(callerId);\n }\n}\n\nclass LastLinesGetter {\n private updater: Updater;\n private log: string | undefined;\n private readonly change: ChangeSource = new ChangeSource();\n private error: any | undefined = undefined;\n\n constructor(\n private readonly path: string,\n private readonly lines: number,\n private readonly patternToSearch?: string,\n ) {\n this.updater = new Updater(async () => this.update());\n }\n\n getOrSchedule(w: Watcher): {\n log: string | undefined;\n error?: any | undefined;\n } {\n this.change.attachWatcher(w);\n\n this.updater.schedule();\n\n return {\n log: this.log,\n error: this.error,\n };\n }\n\n async update(): Promise<void> {\n try {\n const newLogs = await getLastLines(this.path, this.lines, this.patternToSearch);\n\n if (this.log != newLogs) this.change.markChanged(`logs for ${this.path} updated`);\n this.log = newLogs;\n } catch (e: any) {\n if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {\n // No resource\n this.log = '';\n this.error = e;\n this.change.markChanged(`log update for ${this.path} failed, resource not found`);\n return;\n }\n\n throw e;\n }\n }\n}\n\n/** Gets last lines from a file by reading the file from the top and keeping\n * last N lines in a window queue. */\nasync function getLastLines(fPath: string, nLines: number, patternToSearch?: string): Promise<string> {\n let inStream: fs.ReadStream | undefined;\n let rl: readline.Interface | undefined;\n\n try {\n inStream = fs.createReadStream(fPath);\n rl = readline.createInterface({ input: inStream, crlfDelay: Infinity });\n\n const lines = new Denque();\n\n for await (const line of rl) {\n if (patternToSearch != undefined && !line.includes(patternToSearch)) continue;\n\n lines.push(line);\n if (lines.length > nLines) {\n lines.shift();\n }\n }\n\n // last EOL is for keeping backward compat with platforma implementation.\n return lines.toArray().join(os.EOL) + os.EOL;\n } finally {\n // Cleanup resources in finally block to ensure they're always cleaned up\n try {\n if (rl) {\n rl.close();\n }\n } catch (cleanupError) {\n console.error('Error closing readline interface:', cleanupError);\n }\n\n try {\n if (inStream && !inStream.destroyed) {\n inStream.destroy();\n }\n } catch (cleanupError) {\n console.error('Error destroying read stream:', cleanupError);\n }\n }\n}\n\nfunction validateDownloadableResourceType(methodName: string, rType: ResourceType) {\n if (!rType.name.startsWith('Blob/')) {\n let message = `${methodName}: wrong resource type: ${rType.name}, expected: a resource of type that starts with 'Blob/'.`;\n if (rType.name == 'Blob')\n message += ` If it's called from workflow, should a file be exported with 'file.exportFile' function?`;\n\n throw new WrongResourceTypeError(message);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AI,IAAI,MAAM;YACtB,OAAO,IAAI,2FAA2F;AAExG,QAAA,MAAM,IAAI,sBAAsB,CAAC,OAAO,CAAC;IAC3C;AACF;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"ranges.d.ts","sourceRoot":"","sources":["../../../../src/drivers/download_blob/sparse_cache/ranges.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAwB,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAG5E;;+CAE+C;AAC/C,QAAA,MAAM,MAAM;;cAoBwC,EAAE,SAAS;YAExD,EAAG,SAAS;;;;;;;;;;;;;;;;;;EApBjB,CAAC;AAEH,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,CAAC;AAE5C,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAuBpF;AAED,gEAAgE;AAChE,wBAAsB,eAAe,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAInF;AAED,2CAA2C;AAC3C,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,QASxC;AASD,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,UAEnC;AAED,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAQ5E;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU;;;;;EAKpD"}
1
+ {"version":3,"file":"ranges.d.ts","sourceRoot":"","sources":["../../../../src/drivers/download_blob/sparse_cache/ranges.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAwB,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAG5E;;+CAE+C;AAC/C,QAAA,MAAM,MAAM;;cAoB8B,EAAG,SAAS;YAC/B,EACjB,SAAI;;;;;;;;;;;;;;;;;;EApBR,CAAC;AAEH,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,CAAC;AAE5C,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAuBpF;AAED,gEAAgE;AAChE,wBAAsB,eAAe,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAInF;AAED,2CAA2C;AAC3C,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,QASxC;AASD,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,UAEnC;AAED,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAQ5E;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU;;;;;EAKpD"}
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.cjs","sources":["../../../src/drivers/helpers/helpers.ts"],"sourcesContent":["import {\n BasicResourceData,\n getField,\n isNullResourceId,\n PlClient,\n ResourceId,\n valErr,\n} from '@milaboratories/pl-client';\n\n/** Throws when a driver gets a resource with a wrong resource type. */\nexport class WrongResourceTypeError extends Error {\n name = 'WrongResourceTypeError';\n}\n\n/** Updater incorporates a pattern when someone wants to run a callback\n * that updates something only when it's not already running. */\nexport class Updater {\n private updating: Promise<void> | undefined;\n\n constructor(private readonly onUpdate: () => Promise<void>) {}\n\n schedule() {\n if (this.updating == undefined) {\n this.updating = (async () => {\n try {\n await this.onUpdate();\n } catch (e) {\n console.log(`error while updating in Updater: ${e}`);\n } finally {\n this.updating = undefined;\n }\n })();\n }\n }\n}\n"],"names":[],"mappings":";;AASA;AACM,MAAO,sBAAuB,SAAQ,KAAK,CAAA;IAC/C,IAAI,GAAG,wBAAwB;AAChC;AAED;AACgE;MACnD,OAAO,CAAA;AAGW,IAAA,QAAA;AAFrB,IAAA,QAAQ;AAEhB,IAAA,WAAA,CAA6B,QAA6B,EAAA;QAA7B,IAAA,CAAA,QAAQ,GAAR,QAAQ;IAAwB;IAE7D,QAAQ,GAAA;AACN,QAAA,IAAI,IAAI,CAAC,QAAQ,IAAI,SAAS,EAAE;AAC9B,YAAA,IAAI,CAAC,QAAQ,GAAG,CAAC,YAAW;AAC1B,gBAAA,IAAI;AACF,oBAAA,MAAM,IAAI,CAAC,QAAQ,EAAE;gBACvB;gBAAE,OAAO,CAAC,EAAE;AACV,oBAAA,OAAO,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAA,CAAE,CAAC;gBACtD;wBAAU;AACR,oBAAA,IAAI,CAAC,QAAQ,GAAG,SAAS;gBAC3B;YACF,CAAC,GAAG;QACN;IACF;AACD;;;;;"}
1
+ {"version":3,"file":"helpers.cjs","sources":["../../../src/drivers/helpers/helpers.ts"],"sourcesContent":["/** Throws when a driver gets a resource with a wrong resource type. */\nexport class WrongResourceTypeError extends Error {\n name = 'WrongResourceTypeError';\n}\n\n/** Updater incorporates a pattern when someone wants to run a callback\n * that updates something only when it's not already running. */\nexport class Updater {\n private updating: Promise<void> | undefined;\n\n constructor(private readonly onUpdate: () => Promise<void>) {}\n\n schedule() {\n if (this.updating == undefined) {\n this.updating = (async () => {\n try {\n await this.onUpdate();\n } catch (e) {\n console.log(`error while updating in Updater: ${e}`);\n } finally {\n this.updating = undefined;\n }\n })();\n }\n }\n}\n"],"names":[],"mappings":";;AAAA;AACM,MAAO,sBAAuB,SAAQ,KAAK,CAAA;IAC/C,IAAI,GAAG,wBAAwB;AAChC;AAED;AACgE;MACnD,OAAO,CAAA;AAGW,IAAA,QAAA;AAFrB,IAAA,QAAQ;AAEhB,IAAA,WAAA,CAA6B,QAA6B,EAAA;QAA7B,IAAA,CAAA,QAAQ,GAAR,QAAQ;IAAwB;IAE7D,QAAQ,GAAA;AACN,QAAA,IAAI,IAAI,CAAC,QAAQ,IAAI,SAAS,EAAE;AAC9B,YAAA,IAAI,CAAC,QAAQ,GAAG,CAAC,YAAW;AAC1B,gBAAA,IAAI;AACF,oBAAA,MAAM,IAAI,CAAC,QAAQ,EAAE;gBACvB;gBAAE,OAAO,CAAC,EAAE;AACV,oBAAA,OAAO,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAA,CAAE,CAAC;gBACtD;wBAAU;AACR,oBAAA,IAAI,CAAC,QAAQ,GAAG,SAAS;gBAC3B;YACF,CAAC,GAAG;QACN;IACF;AACD;;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/drivers/helpers/helpers.ts"],"names":[],"mappings":"AASA,uEAAuE;AACvE,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C,IAAI,SAA4B;CACjC;AAED;gEACgE;AAChE,qBAAa,OAAO;IAGN,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAFrC,OAAO,CAAC,QAAQ,CAA4B;gBAEf,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC;IAE1D,QAAQ;CAaT"}
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/drivers/helpers/helpers.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C,IAAI,SAA4B;CACjC;AAED;gEACgE;AAChE,qBAAa,OAAO;IAGN,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAFrC,OAAO,CAAC,QAAQ,CAA4B;gBAEf,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC;IAE1D,QAAQ;CAaT"}
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sources":["../../../src/drivers/helpers/helpers.ts"],"sourcesContent":["import {\n BasicResourceData,\n getField,\n isNullResourceId,\n PlClient,\n ResourceId,\n valErr,\n} from '@milaboratories/pl-client';\n\n/** Throws when a driver gets a resource with a wrong resource type. */\nexport class WrongResourceTypeError extends Error {\n name = 'WrongResourceTypeError';\n}\n\n/** Updater incorporates a pattern when someone wants to run a callback\n * that updates something only when it's not already running. */\nexport class Updater {\n private updating: Promise<void> | undefined;\n\n constructor(private readonly onUpdate: () => Promise<void>) {}\n\n schedule() {\n if (this.updating == undefined) {\n this.updating = (async () => {\n try {\n await this.onUpdate();\n } catch (e) {\n console.log(`error while updating in Updater: ${e}`);\n } finally {\n this.updating = undefined;\n }\n })();\n }\n }\n}\n"],"names":[],"mappings":"AASA;AACM,MAAO,sBAAuB,SAAQ,KAAK,CAAA;IAC/C,IAAI,GAAG,wBAAwB;AAChC;AAED;AACgE;MACnD,OAAO,CAAA;AAGW,IAAA,QAAA;AAFrB,IAAA,QAAQ;AAEhB,IAAA,WAAA,CAA6B,QAA6B,EAAA;QAA7B,IAAA,CAAA,QAAQ,GAAR,QAAQ;IAAwB;IAE7D,QAAQ,GAAA;AACN,QAAA,IAAI,IAAI,CAAC,QAAQ,IAAI,SAAS,EAAE;AAC9B,YAAA,IAAI,CAAC,QAAQ,GAAG,CAAC,YAAW;AAC1B,gBAAA,IAAI;AACF,oBAAA,MAAM,IAAI,CAAC,QAAQ,EAAE;gBACvB;gBAAE,OAAO,CAAC,EAAE;AACV,oBAAA,OAAO,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAA,CAAE,CAAC;gBACtD;wBAAU;AACR,oBAAA,IAAI,CAAC,QAAQ,GAAG,SAAS;gBAC3B;YACF,CAAC,GAAG;QACN;IACF;AACD;;;;"}
1
+ {"version":3,"file":"helpers.js","sources":["../../../src/drivers/helpers/helpers.ts"],"sourcesContent":["/** Throws when a driver gets a resource with a wrong resource type. */\nexport class WrongResourceTypeError extends Error {\n name = 'WrongResourceTypeError';\n}\n\n/** Updater incorporates a pattern when someone wants to run a callback\n * that updates something only when it's not already running. */\nexport class Updater {\n private updating: Promise<void> | undefined;\n\n constructor(private readonly onUpdate: () => Promise<void>) {}\n\n schedule() {\n if (this.updating == undefined) {\n this.updating = (async () => {\n try {\n await this.onUpdate();\n } catch (e) {\n console.log(`error while updating in Updater: ${e}`);\n } finally {\n this.updating = undefined;\n }\n })();\n }\n }\n}\n"],"names":[],"mappings":"AAAA;AACM,MAAO,sBAAuB,SAAQ,KAAK,CAAA;IAC/C,IAAI,GAAG,wBAAwB;AAChC;AAED;AACgE;MACnD,OAAO,CAAA;AAGW,IAAA,QAAA;AAFrB,IAAA,QAAQ;AAEhB,IAAA,WAAA,CAA6B,QAA6B,EAAA;QAA7B,IAAA,CAAA,QAAQ,GAAR,QAAQ;IAAwB;IAE7D,QAAQ,GAAA;AACN,QAAA,IAAI,IAAI,CAAC,QAAQ,IAAI,SAAS,EAAE;AAC9B,YAAA,IAAI,CAAC,QAAQ,GAAG,CAAC,YAAW;AAC1B,gBAAA,IAAI;AACF,oBAAA,MAAM,IAAI,CAAC,QAAQ,EAAE;gBACvB;gBAAE,OAAO,CAAC,EAAE;AACV,oBAAA,OAAO,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAA,CAAE,CAAC;gBACtD;wBAAU;AACR,oBAAA,IAAI,CAAC,QAAQ,GAAG,SAAS;gBAC3B;YACF,CAAC,GAAG;QACN;IACF;AACD;;;;"}
@@ -2,7 +2,8 @@
2
2
 
3
3
  var tsHelpers = require('@milaboratories/ts-helpers');
4
4
  var fs = require('node:fs');
5
- var consumers = require('node:stream/consumers');
5
+ var fsp = require('node:fs/promises');
6
+ var node_stream = require('node:stream');
6
7
 
7
8
  function _interopNamespaceDefault(e) {
8
9
  var n = Object.create(null);
@@ -22,6 +23,7 @@ function _interopNamespaceDefault(e) {
22
23
  }
23
24
 
24
25
  var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
26
+ var fsp__namespace = /*#__PURE__*/_interopNamespaceDefault(fsp);
25
27
 
26
28
  // Global concurrency limiter for file reads - limit to 32 parallel reads
27
29
  const fileReadLimiter = new tsHelpers.ConcurrencyLimitingExecutor(32);
@@ -29,20 +31,26 @@ const fileReadLimiter = new tsHelpers.ConcurrencyLimitingExecutor(32);
29
31
  * Reads file content with concurrency limiting and proper error handling.
30
32
  * Ensures file descriptors are properly cleaned up even in error cases.
31
33
  */
32
- async function readFileContent(path, range) {
34
+ async function withFileContent({ path, range, signal, handler, }) {
33
35
  return await fileReadLimiter.run(async () => {
34
- const ops = {};
35
- if (range) {
36
- ops.start = range.from;
37
- ops.end = range.to - 1;
38
- }
36
+ const readOps = {
37
+ start: range?.from,
38
+ end: range?.to !== undefined ? range.to - 1 : undefined,
39
+ signal: signal,
40
+ };
39
41
  let stream;
42
+ let handlerSuccess = false;
40
43
  try {
41
- stream = fs__namespace.createReadStream(path, ops);
42
- return await consumers.buffer(stream);
44
+ const stat = await fsp__namespace.stat(path);
45
+ stream = fs__namespace.createReadStream(path, readOps);
46
+ const webStream = node_stream.Readable.toWeb(stream);
47
+ const result = await handler(webStream, stat.size);
48
+ handlerSuccess = true;
49
+ return result;
43
50
  }
44
51
  catch (error) {
45
- if (stream && !stream.destroyed) {
52
+ // Cleanup on error (including handler errors)
53
+ if (!handlerSuccess && stream && !stream.destroyed) {
46
54
  stream.destroy();
47
55
  }
48
56
  throw error;
@@ -50,5 +58,5 @@ async function readFileContent(path, range) {
50
58
  });
51
59
  }
52
60
 
53
- exports.readFileContent = readFileContent;
61
+ exports.withFileContent = withFileContent;
54
62
  //# sourceMappingURL=read_file.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"read_file.cjs","sources":["../../../src/drivers/helpers/read_file.ts"],"sourcesContent":["import { ConcurrencyLimitingExecutor } from '@milaboratories/ts-helpers';\nimport type { RangeBytes } from '@milaboratories/pl-model-common';\nimport * as fs from 'node:fs';\nimport { buffer } from 'node:stream/consumers';\n\n// Global concurrency limiter for file reads - limit to 32 parallel reads\nconst fileReadLimiter = new ConcurrencyLimitingExecutor(32);\n\n/**\n * Reads file content with concurrency limiting and proper error handling.\n * Ensures file descriptors are properly cleaned up even in error cases.\n */\nexport async function readFileContent(path: string, range?: RangeBytes): Promise<Uint8Array> {\n return await fileReadLimiter.run(async () => {\n const ops: { start?: number; end?: number } = {};\n if (range) {\n ops.start = range.from;\n ops.end = range.to - 1;\n }\n\n let stream: fs.ReadStream | undefined;\n try {\n stream = fs.createReadStream(path, ops);\n return await buffer(stream);\n } catch (error) {\n if (stream && !stream.destroyed) {\n stream.destroy();\n }\n throw error;\n }\n });\n}\n"],"names":["ConcurrencyLimitingExecutor","fs","buffer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAKA;AACA,MAAM,eAAe,GAAG,IAAIA,qCAA2B,CAAC,EAAE,CAAC;AAE3D;;;AAGG;AACI,eAAe,eAAe,CAAC,IAAY,EAAE,KAAkB,EAAA;AACpE,IAAA,OAAO,MAAM,eAAe,CAAC,GAAG,CAAC,YAAW;QAC1C,MAAM,GAAG,GAAqC,EAAE;QAChD,IAAI,KAAK,EAAE;AACT,YAAA,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI;YACtB,GAAG,CAAC,GAAG,GAAG,KAAK,CAAC,EAAE,GAAG,CAAC;QACxB;AAEA,QAAA,IAAI,MAAiC;AACrC,QAAA,IAAI;YACF,MAAM,GAAGC,aAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,GAAG,CAAC;AACvC,YAAA,OAAO,MAAMC,gBAAM,CAAC,MAAM,CAAC;QAC7B;QAAE,OAAO,KAAK,EAAE;AACd,YAAA,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;gBAC/B,MAAM,CAAC,OAAO,EAAE;YAClB;AACA,YAAA,MAAM,KAAK;QACb;AACF,IAAA,CAAC,CAAC;AACJ;;;;"}
1
+ {"version":3,"file":"read_file.cjs","sources":["../../../src/drivers/helpers/read_file.ts"],"sourcesContent":["import { ConcurrencyLimitingExecutor } from '@milaboratories/ts-helpers';\nimport type { RangeBytes } from '@milaboratories/pl-model-common';\nimport * as fs from 'node:fs';\nimport * as fsp from 'node:fs/promises';\nimport { Readable } from 'node:stream';\n\n// Global concurrency limiter for file reads - limit to 32 parallel reads\nconst fileReadLimiter = new ConcurrencyLimitingExecutor(32);\n\n/**\n * Reads file content with concurrency limiting and proper error handling.\n * Ensures file descriptors are properly cleaned up even in error cases.\n */\nexport async function withFileContent<T>({\n path,\n range,\n signal,\n handler,\n}: {\n path: string;\n range?: RangeBytes;\n signal?: AbortSignal;\n handler: (content: ReadableStream, size: number) => Promise<T>;\n}): Promise<T> {\n return await fileReadLimiter.run(async () => {\n const readOps = {\n start: range?.from,\n end: range?.to !== undefined ? range.to - 1 : undefined,\n signal: signal,\n };\n let stream: fs.ReadStream | undefined;\n let handlerSuccess = false;\n\n try {\n const stat = await fsp.stat(path);\n stream = fs.createReadStream(path, readOps);\n const webStream = Readable.toWeb(stream);\n\n const result = await handler(webStream, stat.size);\n handlerSuccess = true;\n return result;\n } catch (error) {\n // Cleanup on error (including handler errors)\n if (!handlerSuccess && stream && !stream.destroyed) {\n stream.destroy();\n }\n throw error;\n }\n });\n}\n"],"names":["ConcurrencyLimitingExecutor","fsp","fs","Readable"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA;AACA,MAAM,eAAe,GAAG,IAAIA,qCAA2B,CAAC,EAAE,CAAC;AAE3D;;;AAGG;AACI,eAAe,eAAe,CAAI,EACvC,IAAI,EACJ,KAAK,EACL,MAAM,EACN,OAAO,GAMR,EAAA;AACC,IAAA,OAAO,MAAM,eAAe,CAAC,GAAG,CAAC,YAAW;AAC1C,QAAA,MAAM,OAAO,GAAG;YACd,KAAK,EAAE,KAAK,EAAE,IAAI;AAClB,YAAA,GAAG,EAAE,KAAK,EAAE,EAAE,KAAK,SAAS,GAAG,KAAK,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS;AACvD,YAAA,MAAM,EAAE,MAAM;SACf;AACD,QAAA,IAAI,MAAiC;QACrC,IAAI,cAAc,GAAG,KAAK;AAE1B,QAAA,IAAI;YACF,MAAM,IAAI,GAAG,MAAMC,cAAG,CAAC,IAAI,CAAC,IAAI,CAAC;YACjC,MAAM,GAAGC,aAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC;YAC3C,MAAM,SAAS,GAAGC,oBAAQ,CAAC,KAAK,CAAC,MAAM,CAAC;YAExC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC;YAClD,cAAc,GAAG,IAAI;AACrB,YAAA,OAAO,MAAM;QACf;QAAE,OAAO,KAAK,EAAE;;YAEd,IAAI,CAAC,cAAc,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;gBAClD,MAAM,CAAC,OAAO,EAAE;YAClB;AACA,YAAA,MAAM,KAAK;QACb;AACF,IAAA,CAAC,CAAC;AACJ;;;;"}
@@ -3,5 +3,10 @@ import type { RangeBytes } from '@milaboratories/pl-model-common';
3
3
  * Reads file content with concurrency limiting and proper error handling.
4
4
  * Ensures file descriptors are properly cleaned up even in error cases.
5
5
  */
6
- export declare function readFileContent(path: string, range?: RangeBytes): Promise<Uint8Array>;
6
+ export declare function withFileContent<T>({ path, range, signal, handler, }: {
7
+ path: string;
8
+ range?: RangeBytes;
9
+ signal?: AbortSignal;
10
+ handler: (content: ReadableStream, size: number) => Promise<T>;
11
+ }): Promise<T>;
7
12
  //# sourceMappingURL=read_file.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"read_file.d.ts","sourceRoot":"","sources":["../../../src/drivers/helpers/read_file.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAOlE;;;GAGG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAmB3F"}
1
+ {"version":3,"file":"read_file.d.ts","sourceRoot":"","sources":["../../../src/drivers/helpers/read_file.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAQlE;;;GAGG;AACH,wBAAsB,eAAe,CAAC,CAAC,EAAE,EACvC,IAAI,EACJ,KAAK,EACL,MAAM,EACN,OAAO,GACR,EAAE;IACD,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,OAAO,EAAE,CAAC,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;CAChE,GAAG,OAAO,CAAC,CAAC,CAAC,CA0Bb"}
@@ -1,6 +1,7 @@
1
1
  import { ConcurrencyLimitingExecutor } from '@milaboratories/ts-helpers';
2
2
  import * as fs from 'node:fs';
3
- import { buffer } from 'node:stream/consumers';
3
+ import * as fsp from 'node:fs/promises';
4
+ import { Readable } from 'node:stream';
4
5
 
5
6
  // Global concurrency limiter for file reads - limit to 32 parallel reads
6
7
  const fileReadLimiter = new ConcurrencyLimitingExecutor(32);
@@ -8,20 +9,26 @@ const fileReadLimiter = new ConcurrencyLimitingExecutor(32);
8
9
  * Reads file content with concurrency limiting and proper error handling.
9
10
  * Ensures file descriptors are properly cleaned up even in error cases.
10
11
  */
11
- async function readFileContent(path, range) {
12
+ async function withFileContent({ path, range, signal, handler, }) {
12
13
  return await fileReadLimiter.run(async () => {
13
- const ops = {};
14
- if (range) {
15
- ops.start = range.from;
16
- ops.end = range.to - 1;
17
- }
14
+ const readOps = {
15
+ start: range?.from,
16
+ end: range?.to !== undefined ? range.to - 1 : undefined,
17
+ signal: signal,
18
+ };
18
19
  let stream;
20
+ let handlerSuccess = false;
19
21
  try {
20
- stream = fs.createReadStream(path, ops);
21
- return await buffer(stream);
22
+ const stat = await fsp.stat(path);
23
+ stream = fs.createReadStream(path, readOps);
24
+ const webStream = Readable.toWeb(stream);
25
+ const result = await handler(webStream, stat.size);
26
+ handlerSuccess = true;
27
+ return result;
22
28
  }
23
29
  catch (error) {
24
- if (stream && !stream.destroyed) {
30
+ // Cleanup on error (including handler errors)
31
+ if (!handlerSuccess && stream && !stream.destroyed) {
25
32
  stream.destroy();
26
33
  }
27
34
  throw error;
@@ -29,5 +36,5 @@ async function readFileContent(path, range) {
29
36
  });
30
37
  }
31
38
 
32
- export { readFileContent };
39
+ export { withFileContent };
33
40
  //# sourceMappingURL=read_file.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"read_file.js","sources":["../../../src/drivers/helpers/read_file.ts"],"sourcesContent":["import { ConcurrencyLimitingExecutor } from '@milaboratories/ts-helpers';\nimport type { RangeBytes } from '@milaboratories/pl-model-common';\nimport * as fs from 'node:fs';\nimport { buffer } from 'node:stream/consumers';\n\n// Global concurrency limiter for file reads - limit to 32 parallel reads\nconst fileReadLimiter = new ConcurrencyLimitingExecutor(32);\n\n/**\n * Reads file content with concurrency limiting and proper error handling.\n * Ensures file descriptors are properly cleaned up even in error cases.\n */\nexport async function readFileContent(path: string, range?: RangeBytes): Promise<Uint8Array> {\n return await fileReadLimiter.run(async () => {\n const ops: { start?: number; end?: number } = {};\n if (range) {\n ops.start = range.from;\n ops.end = range.to - 1;\n }\n\n let stream: fs.ReadStream | undefined;\n try {\n stream = fs.createReadStream(path, ops);\n return await buffer(stream);\n } catch (error) {\n if (stream && !stream.destroyed) {\n stream.destroy();\n }\n throw error;\n }\n });\n}\n"],"names":[],"mappings":";;;;AAKA;AACA,MAAM,eAAe,GAAG,IAAI,2BAA2B,CAAC,EAAE,CAAC;AAE3D;;;AAGG;AACI,eAAe,eAAe,CAAC,IAAY,EAAE,KAAkB,EAAA;AACpE,IAAA,OAAO,MAAM,eAAe,CAAC,GAAG,CAAC,YAAW;QAC1C,MAAM,GAAG,GAAqC,EAAE;QAChD,IAAI,KAAK,EAAE;AACT,YAAA,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI;YACtB,GAAG,CAAC,GAAG,GAAG,KAAK,CAAC,EAAE,GAAG,CAAC;QACxB;AAEA,QAAA,IAAI,MAAiC;AACrC,QAAA,IAAI;YACF,MAAM,GAAG,EAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,GAAG,CAAC;AACvC,YAAA,OAAO,MAAM,MAAM,CAAC,MAAM,CAAC;QAC7B;QAAE,OAAO,KAAK,EAAE;AACd,YAAA,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;gBAC/B,MAAM,CAAC,OAAO,EAAE;YAClB;AACA,YAAA,MAAM,KAAK;QACb;AACF,IAAA,CAAC,CAAC;AACJ;;;;"}
1
+ {"version":3,"file":"read_file.js","sources":["../../../src/drivers/helpers/read_file.ts"],"sourcesContent":["import { ConcurrencyLimitingExecutor } from '@milaboratories/ts-helpers';\nimport type { RangeBytes } from '@milaboratories/pl-model-common';\nimport * as fs from 'node:fs';\nimport * as fsp from 'node:fs/promises';\nimport { Readable } from 'node:stream';\n\n// Global concurrency limiter for file reads - limit to 32 parallel reads\nconst fileReadLimiter = new ConcurrencyLimitingExecutor(32);\n\n/**\n * Reads file content with concurrency limiting and proper error handling.\n * Ensures file descriptors are properly cleaned up even in error cases.\n */\nexport async function withFileContent<T>({\n path,\n range,\n signal,\n handler,\n}: {\n path: string;\n range?: RangeBytes;\n signal?: AbortSignal;\n handler: (content: ReadableStream, size: number) => Promise<T>;\n}): Promise<T> {\n return await fileReadLimiter.run(async () => {\n const readOps = {\n start: range?.from,\n end: range?.to !== undefined ? range.to - 1 : undefined,\n signal: signal,\n };\n let stream: fs.ReadStream | undefined;\n let handlerSuccess = false;\n\n try {\n const stat = await fsp.stat(path);\n stream = fs.createReadStream(path, readOps);\n const webStream = Readable.toWeb(stream);\n\n const result = await handler(webStream, stat.size);\n handlerSuccess = true;\n return result;\n } catch (error) {\n // Cleanup on error (including handler errors)\n if (!handlerSuccess && stream && !stream.destroyed) {\n stream.destroy();\n }\n throw error;\n }\n });\n}\n"],"names":[],"mappings":";;;;;AAMA;AACA,MAAM,eAAe,GAAG,IAAI,2BAA2B,CAAC,EAAE,CAAC;AAE3D;;;AAGG;AACI,eAAe,eAAe,CAAI,EACvC,IAAI,EACJ,KAAK,EACL,MAAM,EACN,OAAO,GAMR,EAAA;AACC,IAAA,OAAO,MAAM,eAAe,CAAC,GAAG,CAAC,YAAW;AAC1C,QAAA,MAAM,OAAO,GAAG;YACd,KAAK,EAAE,KAAK,EAAE,IAAI;AAClB,YAAA,GAAG,EAAE,KAAK,EAAE,EAAE,KAAK,SAAS,GAAG,KAAK,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS;AACvD,YAAA,MAAM,EAAE,MAAM;SACf;AACD,QAAA,IAAI,MAAiC;QACrC,IAAI,cAAc,GAAG,KAAK;AAE1B,QAAA,IAAI;YACF,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;YACjC,MAAM,GAAG,EAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC;YAC3C,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC;YAExC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC;YAClD,cAAc,GAAG,IAAI;AACrB,YAAA,OAAO,MAAM;QACf;QAAE,OAAO,KAAK,EAAE;;YAEd,IAAI,CAAC,cAAc,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;gBAClD,MAAM,CAAC,OAAO,EAAE;YAClB;AACA,YAAA,MAAM,KAAK;QACb;AACF,IAAA,CAAC,CAAC;AACJ;;;;"}
@@ -24,12 +24,15 @@ class RemoteFileDownloader {
24
24
  headers,
25
25
  signal: ops.signal,
26
26
  });
27
+ ops.signal?.throwIfAborted();
27
28
  const webBody = node_stream.Readable.toWeb(body);
28
29
  let handlerSuccess = false;
29
30
  try {
30
31
  await checkStatusCodeOk(statusCode, webBody, url);
32
+ ops.signal?.throwIfAborted();
31
33
  const size = Number(responseHeaders['content-length']);
32
34
  const result = await handler(webBody, size);
35
+ ops.signal?.throwIfAborted();
33
36
  handlerSuccess = true;
34
37
  return result;
35
38
  }
@@ -1 +1 @@
1
- {"version":3,"file":"download.cjs","sources":["../../src/helpers/download.ts"],"sourcesContent":["// @TODO Gleb Zakharov\n/* eslint-disable n/no-unsupported-features/node-builtins */\nimport type { Dispatcher } from 'undici';\nimport { request } from 'undici';\nimport { Readable } from 'node:stream';\nimport type { ReadableStream } from 'node:stream/web';\nimport { text } from 'node:stream/consumers';\nimport type { RangeBytes } from '@milaboratories/pl-model-common';\n\nexport interface DownloadOps {\n signal?: AbortSignal;\n range?: RangeBytes;\n}\n\nexport type ContentHandler<T> = (content: ReadableStream, size: number) => Promise<T>;\n\n/** Throws when a status code of the downloading URL was in range [400, 500). */\nexport class NetworkError400 extends Error {\n name = 'NetworkError400';\n}\n\nexport class RemoteFileDownloader {\n constructor(public readonly httpClient: Dispatcher) {}\n\n async withContent<T>(\n url: string,\n reqHeaders: Record<string, string>,\n ops: DownloadOps,\n handler: ContentHandler<T>,\n ): Promise<T> {\n const headers = { ...reqHeaders };\n\n // Add range header if specified\n if (ops.range) {\n headers['Range'] = `bytes=${ops.range.from}-${ops.range.to - 1}`;\n }\n\n const { statusCode, body, headers: responseHeaders } = await request(url, {\n dispatcher: this.httpClient,\n headers,\n signal: ops.signal,\n });\n\n const webBody = Readable.toWeb(body);\n let handlerSuccess = false;\n\n try {\n await checkStatusCodeOk(statusCode, webBody, url);\n const size = Number(responseHeaders['content-length']);\n const result = await handler(webBody, size);\n handlerSuccess = true;\n return result;\n } catch (error) {\n // Cleanup on error (including handler errors)\n if (!handlerSuccess && !webBody.locked) {\n try {\n await webBody.cancel();\n } catch {\n // Ignore cleanup errors\n }\n }\n throw error;\n }\n }\n}\n\nasync function checkStatusCodeOk(statusCode: number, webBody: ReadableStream, url: string) {\n if (statusCode != 200 && statusCode != 206 /* partial content from range request */) {\n const beginning = (await text(webBody)).substring(0, 1000);\n\n if (400 <= statusCode && statusCode < 500) {\n throw new NetworkError400(\n `Http error: statusCode: ${statusCode} `\n + `url: ${url.toString()}, beginning of body: ${beginning}`);\n }\n\n throw new Error(`Http error: statusCode: ${statusCode} url: ${url.toString()}`);\n }\n}\n"],"names":["request","Readable","text"],"mappings":";;;;;;AAgBA;AACM,MAAO,eAAgB,SAAQ,KAAK,CAAA;IACxC,IAAI,GAAG,iBAAiB;AACzB;MAEY,oBAAoB,CAAA;AACH,IAAA,UAAA;AAA5B,IAAA,WAAA,CAA4B,UAAsB,EAAA;QAAtB,IAAA,CAAA,UAAU,GAAV,UAAU;IAAe;IAErD,MAAM,WAAW,CACf,GAAW,EACX,UAAkC,EAClC,GAAgB,EAChB,OAA0B,EAAA;AAE1B,QAAA,MAAM,OAAO,GAAG,EAAE,GAAG,UAAU,EAAE;;AAGjC,QAAA,IAAI,GAAG,CAAC,KAAK,EAAE;AACb,YAAA,OAAO,CAAC,OAAO,CAAC,GAAG,CAAA,MAAA,EAAS,GAAG,CAAC,KAAK,CAAC,IAAI,CAAA,CAAA,EAAI,GAAG,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,EAAE;QAClE;AAEA,QAAA,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,MAAMA,cAAO,CAAC,GAAG,EAAE;YACxE,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,OAAO;YACP,MAAM,EAAE,GAAG,CAAC,M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.cjs","sources":["../../src/helpers/download.ts"],"sourcesContent":["// @TODO Gleb Zakharov\n/* eslint-disable n/no-unsupported-features/node-builtins */\nimport type { Dispatcher } from 'undici';\nimport { request } from 'undici';\nimport { Readable } from 'node:stream';\nimport type { ReadableStream } from 'node:stream/web';\nimport { text } from 'node:stream/consumers';\nimport type { GetContentOptions } from '@milaboratories/pl-model-common';\n\nexport type ContentHandler<T> = (content: ReadableStream, size: number) => Promise<T>;\n\n/** Throws when a status code of the downloading URL was in range [400, 500). */\nexport class NetworkError400 extends Error {\n name = 'NetworkError400';\n}\n\nexport class RemoteFileDownloader {\n constructor(public readonly httpClient: Dispatcher) {}\n\n async withContent<T>(\n url: string,\n reqHeaders: Record<string, string>,\n ops: GetContentOptions,\n handler: ContentHandler<T>,\n ): Promise<T> {\n const headers = { ...reqHeaders };\n\n // Add range header if specified\n if (ops.range) {\n headers['Range'] = `bytes=${ops.range.from}-${ops.range.to - 1}`;\n }\n\n const { statusCode, body, headers: responseHeaders } = await request(url, {\n dispatcher: this.httpClient,\n headers,\n signal: ops.signal,\n });\n ops.signal?.throwIfAborted();\n\n const webBody = Readable.toWeb(body);\n let handlerSuccess = false;\n\n try {\n await checkStatusCodeOk(statusCode, webBody, url);\n ops.signal?.throwIfAborted();\n\n const size = Number(responseHeaders['content-length']);\n const result = await handler(webBody, size);\n ops.signal?.throwIfAborted();\n\n handlerSuccess = true;\n return result;\n } catch (error) {\n // Cleanup on error (including handler errors)\n if (!handlerSuccess && !webBody.locked) {\n try {\n await webBody.cancel();\n } catch {\n // Ignore cleanup errors\n }\n }\n throw error;\n }\n }\n}\n\nasync function checkStatusCodeOk(statusCode: number, webBody: ReadableStream, url: string) {\n if (statusCode != 200 && statusCode != 206 /* partial content from range request */) {\n const beginning = (await text(webBody)).substring(0, 1000);\n\n if (400 <= statusCode && statusCode < 500) {\n throw new NetworkError400(\n `Http error: statusCode: ${statusCode} `\n + `url: ${url.toString()}, beginning of body: ${beginning}`);\n }\n\n throw new Error(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YAAA,GAAG,CAAC,MAAM,EAAE,cAAc,EAAE;YAE5B,cAAc,GAAG,IAAI;AACrB,YAAA,OAAO,MAAM;QACf;QAAE,OAAO,KAAK,EAAE;;YAEd,IAAI,CAAC,cAAc,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;AACtC,gBAAA,IAAI;AACF,oBAAA,MAAM,OAAO,CAAC,MAAM,EAAE;gBACxB;AAAE,gBAAA,MAAM;;gBAER;YACF;AACA,YAAA,MAAM,KAAK;QACb;IACF;AACD;AAED,eAAe,iBAAiB,CAAC,UAAkB,EAAE,OAAuB,EAAE,GAAW,EAAA;IACvF,IAAI,UAAU,IAAI,GAAG,IAAI,UAAU,IAAI,GAAG,2CAA2C;AACnF,QAAA,MAAM,SAAS,GAAG,CAAC,MAAMC,cAAI,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC;QAE1D,IAAI,GAAG,IAAI,UAAU,IAAI,UAAU,GAAG,GAAG,EAAE;AACzC,YAAA,MAAM,IAAI,eAAe,CACvB,CAAA,wBAAA,EAA2B,UAAU,CAAA,CAAA;kBACnC,CAAA,KAAA,EAAQ,GAAG,CAAC,QAAQ,EAAE,wBAAwB,SAAS,CAAA,CAAE,CAAC;QAChE;AAEA,QAAA,MAAM,IAAI,KAAK,CAAC,CAAA,wBAAA,EAA2B,UAAU,CAAA,MAAA,EAAS,GAAG,CAAC,QAAQ,EAAE,CAAA,CAAE,CAAC;IACjF;AACF;;;;;"}
@@ -1,10 +1,6 @@
1
1
  import type { Dispatcher } from 'undici';
2
2
  import type { ReadableStream } from 'node:stream/web';
3
- import type { RangeBytes } from '@milaboratories/pl-model-common';
4
- export interface DownloadOps {
5
- signal?: AbortSignal;
6
- range?: RangeBytes;
7
- }
3
+ import type { GetContentOptions } from '@milaboratories/pl-model-common';
8
4
  export type ContentHandler<T> = (content: ReadableStream, size: number) => Promise<T>;
9
5
  /** Throws when a status code of the downloading URL was in range [400, 500). */
10
6
  export declare class NetworkError400 extends Error {
@@ -13,6 +9,6 @@ export declare class NetworkError400 extends Error {
13
9
  export declare class RemoteFileDownloader {
14
10
  readonly httpClient: Dispatcher;
15
11
  constructor(httpClient: Dispatcher);
16
- withContent<T>(url: string, reqHeaders: Record<string, string>, ops: DownloadOps, handler: ContentHandler<T>): Promise<T>;
12
+ withContent<T>(url: string, reqHeaders: Record<string, string>, ops: GetContentOptions, handler: ContentHandler<T>): Promise<T>;
17
13
  }
18
14
  //# sourceMappingURL=download.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"download.d.ts","sourceRoot":"","sources":["../../src/helpers/download.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAGzC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,KAAK,CAAC,EAAE,UAAU,CAAC;CACpB;AAED,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;AAEtF,gFAAgF;AAChF,qBAAa,eAAgB,SAAQ,KAAK;IACxC,IAAI,SAAqB;CAC1B;AAED,qBAAa,oBAAoB;aACH,UAAU,EAAE,UAAU;gBAAtB,UAAU,EAAE,UAAU;IAE5C,WAAW,CAAC,CAAC,EACjB,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAClC,GAAG,EAAE,WAAW,EAChB,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,GACzB,OAAO,CAAC,CAAC,CAAC;CAmCd"}
1
+ {"version":3,"file":"download.d.ts","sourceRoot":"","sources":["../../src/helpers/download.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAGzC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAEzE,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;AAEtF,gFAAgF;AAChF,qBAAa,eAAgB,SAAQ,KAAK;IACxC,IAAI,SAAqB;CAC1B;AAED,qBAAa,oBAAoB;aACH,UAAU,EAAE,UAAU;gBAAtB,UAAU,EAAE,UAAU;IAE5C,WAAW,CAAC,CAAC,EACjB,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAClC,GAAG,EAAE,iBAAiB,EACtB,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,GACzB,OAAO,CAAC,CAAC,CAAC;CAwCd"}
@@ -22,12 +22,15 @@ class RemoteFileDownloader {
22
22
  headers,
23
23
  signal: ops.signal,
24
24
  });
25
+ ops.signal?.throwIfAborted();
25
26
  const webBody = Readable.toWeb(body);
26
27
  let handlerSuccess = false;
27
28
  try {
28
29
  await checkStatusCodeOk(statusCode, webBody, url);
30
+ ops.signal?.throwIfAborted();
29
31
  const size = Number(responseHeaders['content-length']);
30
32
  const result = await handler(webBody, size);
33
+ ops.signal?.throwIfAborted();
31
34
  handlerSuccess = true;
32
35
  return result;
33
36
  }
@@ -1 +1 @@
1
- {"version":3,"file":"download.js","sources":["../../src/helpers/download.ts"],"sourcesContent":["// @TODO Gleb Zakharov\n/* eslint-disable n/no-unsupported-features/node-builtins */\nimport type { Dispatcher } from 'undici';\nimport { request } from 'undici';\nimport { Readable } from 'node:stream';\nimport type { ReadableStream } from 'node:stream/web';\nimport { text } from 'node:stream/consumers';\nimport type { RangeBytes } from '@milaboratories/pl-model-common';\n\nexport interface DownloadOps {\n signal?: AbortSignal;\n range?: RangeBytes;\n}\n\nexport type ContentHandler<T> = (content: ReadableStream, size: number) => Promise<T>;\n\n/** Throws when a status code of the downloading URL was in range [400, 500). */\nexport class NetworkError400 extends Error {\n name = 'NetworkError400';\n}\n\nexport class RemoteFileDownloader {\n constructor(public readonly httpClient: Dispatcher) {}\n\n async withContent<T>(\n url: string,\n reqHeaders: Record<string, string>,\n ops: DownloadOps,\n handler: ContentHandler<T>,\n ): Promise<T> {\n const headers = { ...reqHeaders };\n\n // Add range header if specified\n if (ops.range) {\n headers['Range'] = `bytes=${ops.range.from}-${ops.range.to - 1}`;\n }\n\n const { statusCode, body, headers: responseHeaders } = await request(url, {\n dispatcher: this.httpClient,\n headers,\n signal: ops.signal,\n });\n\n const webBody = Readable.toWeb(body);\n let handlerSuccess = false;\n\n try {\n await checkStatusCodeOk(statusCode, webBody, url);\n const size = Number(responseHeaders['content-length']);\n const result = await handler(webBody, size);\n handlerSuccess = true;\n return result;\n } catch (error) {\n // Cleanup on error (including handler errors)\n if (!handlerSuccess && !webBody.locked) {\n try {\n await webBody.cancel();\n } catch {\n // Ignore cleanup errors\n }\n }\n throw error;\n }\n }\n}\n\nasync function checkStatusCodeOk(statusCode: number, webBody: ReadableStream, url: string) {\n if (statusCode != 200 && statusCode != 206 /* partial content from range request */) {\n const beginning = (await text(webBody)).substring(0, 1000);\n\n if (400 <= statusCode && statusCode < 500) {\n throw new NetworkError400(\n `Http error: statusCode: ${statusCode} `\n + `url: ${url.toString()}, beginning of body: ${beginning}`);\n }\n\n throw new Error(`Http error: statusCode: ${statusCode} url: ${url.toString()}`);\n }\n}\n"],"names":[],"mappings":";;;;AAgBA;AACM,MAAO,eAAgB,SAAQ,KAAK,CAAA;IACxC,IAAI,GAAG,iBAAiB;AACzB;MAEY,oBAAoB,CAAA;AACH,IAAA,UAAA;AAA5B,IAAA,WAAA,CAA4B,UAAsB,EAAA;QAAtB,IAAA,CAAA,UAAU,G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,GAAG,IAAI,UAAU,IAAI,GAAG,2CAA2C;AACnF,QAAA,MAAM,SAAS,GAAG,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC;QAE1D,IAAI,GAAG,IAAI,UAAU,IAAI,UAAU,GAAG,GAAG,EAAE;AACzC,YAAA,MAAM,IAAI,eAAe,CACvB,CAAA,wBAAA,EAA2B,UAAU,CAAA,CAAA;kBACnC,CAAA,KAAA,EAAQ,GAAG,CAAC,QAAQ,EAAE,wBAAwB,SAAS,CAAA,CAAE,CAAC;QAChE;AAEA,QAAA,MAAM,IAAI,KAAK,CAAC,CAAA,wBAAA,EAA2B,UAAU,CAAA,MAAA,EAAS,GAAG,CAAC,QAAQ,EAAE,CAAA,CAAE,CAAC;IACjF;AACF;;;;"}
1
+ {"version":3,"file":"download.js","sources":["../../src/helpers/download.ts"],"sourcesContent":["// @TODO Gleb Zakharov\n/* eslint-disable n/no-unsupported-features/node-builtins */\nimport type { Dispatcher } from 'undici';\nimport { request } from 'undici';\nimport { Readable } from 'node:stream';\nimport type { ReadableStream } from 'node:stream/web';\nimport { text } from 'node:stream/consumers';\nimport type { GetContentOptions } from '@milaboratories/pl-model-common';\n\nexport type ContentHandler<T> = (content: ReadableStream, size: number) => Promise<T>;\n\n/** Throws when a status code of the downloading URL was in range [400, 500). */\nexport class NetworkError400 extends Error {\n name = 'NetworkError400';\n}\n\nexport class RemoteFileDownloader {\n constructor(public readonly httpClient: Dispatcher) {}\n\n async withContent<T>(\n url: string,\n reqHeaders: Record<string, string>,\n ops: GetContentOptions,\n handler: ContentHandler<T>,\n ): Promise<T> {\n const headers = { ...reqHeaders };\n\n // Add range header if specified\n if (ops.range) {\n headers['Range'] = `bytes=${ops.range.from}-${ops.range.to - 1}`;\n }\n\n const { statusCode, body, headers: responseHeaders } = await request(url, {\n dispatcher: this.httpClient,\n headers,\n signal: ops.signal,\n });\n ops.signal?.throwIfAborted();\n\n const webBody = Readable.toWeb(body);\n let handlerSuccess = false;\n\n try {\n await checkStatusCodeOk(statusCode, webBody, url);\n ops.signal?.throwIfAborted();\n\n const size = Number(responseHeaders['content-length']);\n const result = await handler(webBody, size);\n ops.signal?.throwIfAborted();\n\n handlerSuccess = true;\n return result;\n } catch (error) {\n // Cleanup on error (including handler errors)\n if (!handlerSuccess && !webBody.locked) {\n try {\n await webBody.cancel();\n } catch {\n // Ignore cleanup errors\n }\n }\n throw error;\n }\n }\n}\n\nasync function checkStatusCodeOk(statusCode: number, webBody: ReadableStream, url: string) {\n if (statusCode != 200 && statusCode != 206 /* partial content from range request */) {\n const beginning = (await text(webBody)).substring(0, 1000);\n\n if (400 <= statusCode && statusCode < 500) {\n throw new NetworkError400(\n `Http error: statusCode: ${statusCode} `\n + `url: ${url.toString()}, beginning of body: ${beginning}`);\n }\n\n throw new Error(`Http error: statusCode: ${statusCode} url: ${url.toString()}`);\n }\n}\n"],"names":[],"mappings":";;;;AAWA;AACM,MAAO,eAAgB,SAAQ,KAAK,CAAA;IACxC,IAAI,GAAG,iBAAiB;AACzB;MAEY,oBAAoB,CAAA;AACH,IAAA,UAAA;AAA5B,IAAA,WAAA,CAA4B,UAAsB,EAAA;QAAtB,IAAA,CAAA,UAAU,GAAV,UAAU;IAAe;IAErD,MAAM,WAAW,CACf,GAAW,EACX,UAAkC,EAClC,GAAsB,EACtB,OAA0B,EAAA;AAE1B,QAAA,MAAM,OAAO,GAAG,EAAE,GAAG,UAAU,EAAE;;AAGjC,QAAA,IAAI,GAAG,CAAC,KAAK,EAAE;AACb,YAAA,OAAO,CAAC,OAAO,CAAC,GAAG,CAAA,MAAA,EAAS,GAAG,CAAC,KAAK,CAAC,IAAI,CAAA,CAAA,EAAI,GAAG,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,EAAE;QAClE;AAEA,QAAA,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,MAAM,OAAO,CAAC,GAAG,EAA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GAAG,CAAC,QAAQ,EAAE,wBAAwB,SAAS,CAAA,CAAE,CAAC;QAChE;AAEA,QAAA,MAAM,IAAI,KAAK,CAAC,CAAA,wBAAA,EAA2B,UAAU,CAAA,MAAA,EAAS,GAAG,CAAC,QAAQ,EAAE,CAAA,CAAE,CAAC;IACjF;AACF;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/pl-drivers",
3
- "version": "1.10.9",
3
+ "version": "1.10.11",
4
4
  "engines": {
5
5
  "node": ">=20"
6
6
  },
@@ -31,12 +31,12 @@
31
31
  "undici": "~7.13.0",
32
32
  "upath": "^2.0.1",
33
33
  "zod": "~3.23.8",
34
- "@milaboratories/ts-helpers": "1.4.6",
35
- "@milaboratories/pl-client": "2.11.12",
36
34
  "@milaboratories/helpers": "1.7.0",
37
- "@milaboratories/pl-tree": "1.7.9",
38
- "@milaboratories/computable": "2.6.7",
39
- "@milaboratories/pl-model-common": "1.19.15"
35
+ "@milaboratories/ts-helpers": "1.4.7",
36
+ "@milaboratories/pl-client": "2.11.13",
37
+ "@milaboratories/computable": "2.6.8",
38
+ "@milaboratories/pl-tree": "1.7.10",
39
+ "@milaboratories/pl-model-common": "1.19.17"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/decompress": "^4.2.7",
@@ -47,9 +47,9 @@
47
47
  "typescript": "~5.6.3",
48
48
  "vitest": "^2.1.9",
49
49
  "@milaboratories/eslint-config": "1.0.4",
50
- "@milaboratories/ts-configs": "1.0.6",
51
50
  "@milaboratories/build-configs": "1.0.8",
52
- "@milaboratories/ts-builder": "1.0.5"
51
+ "@milaboratories/ts-builder": "1.0.5",
52
+ "@milaboratories/ts-configs": "1.0.6"
53
53
  },
54
54
  "scripts": {
55
55
  "type-check": "ts-builder types --target node",
@@ -11,11 +11,11 @@ import * as path from 'node:path';
11
11
  import { Readable } from 'node:stream';
12
12
  import type { Dispatcher } from 'undici';
13
13
  import type { LocalStorageProjection } from '../drivers/types';
14
- import type { DownloadOps, ContentHandler } from '../helpers/download';
15
- import { RemoteFileDownloader } from '../helpers/download';
14
+ import { type ContentHandler, RemoteFileDownloader } from '../helpers/download';
16
15
  import { validateAbsolute } from '../helpers/validate';
17
16
  import type { DownloadAPI_GetDownloadURL_Response } from '../proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol';
18
17
  import { DownloadClient } from '../proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client';
18
+ import { type GetContentOptions } from '@milaboratories/pl-model-common';
19
19
 
20
20
  /** Gets URLs for downloading from pl-core, parses them and reads or downloads
21
21
  * files locally and from the web. */
@@ -52,7 +52,7 @@ export class ClientDownload {
52
52
  async withBlobContent<T>(
53
53
  info: ResourceInfo,
54
54
  options: RpcOptions | undefined,
55
- ops: DownloadOps,
55
+ ops: GetContentOptions,
56
56
  handler: ContentHandler<T>,
57
57
  ): Promise<T> {
58
58
  const { downloadUrl, headers } = await this.grpcGetDownloadUrl(info, options, ops.signal);
@@ -67,7 +67,7 @@ export class ClientDownload {
67
67
 
68
68
  async withLocalFileContent<T>(
69
69
  url: string,
70
- ops: DownloadOps,
70
+ ops: GetContentOptions,
71
71
  handler: ContentHandler<T>,
72
72
  ): Promise<T> {
73
73
  const { storageId, relativePath } = parseLocalUrl(url);
@@ -77,6 +77,7 @@ export class ClientDownload {
77
77
  const readOps = {
78
78
  start: ops.range?.from,
79
79
  end: ops.range?.to !== undefined ? ops.range.to - 1 : undefined,
80
+ signal: ops.signal,
80
81
  };
81
82
  let stream: fs.ReadStream | undefined;
82
83
  let handlerSuccess = false;