Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cod-dicomweb-server",
"title": "COD Dicomweb server",
"version": "1.3.13",
"version": "1.3.14",
"private": false,
"description": "A wadors server proxy that get data from a Cloud Optimized Dicom format.",
"main": "dist/umd/main.js",
Expand Down
26 changes: 13 additions & 13 deletions src/classes/CodDicomWebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ class CodDicomWebServer {
private options: CodDicomWebServerOptions = {
maxCacheSize: 4 * 1024 * 1024 * 1024, // 4GB
domain: constants.url.DOMAIN,
enableLocalCache: false
enableOPFSCache: false
};
private fileManager;
private metadataManager;
private seriesUidFileUrls: Record<string, Set<{ type: Enums.URLType; url: string }>> = {};

constructor(args: { maxCacheSize?: number; domain?: string; disableWorker?: boolean; enableLocalCache?: boolean } = {}) {
const { maxCacheSize, domain, disableWorker, enableLocalCache } = args;
constructor(args: { maxCacheSize?: number; domain?: string; disableWorker?: boolean; enableOPFSCache?: boolean } = {}) {
const { maxCacheSize, domain, disableWorker, enableOPFSCache } = args;

this.options.maxCacheSize = maxCacheSize || this.options.maxCacheSize;
this.options.domain = domain || this.options.domain;
this.options.enableLocalCache = !!enableLocalCache;
this.options.enableOPFSCache = !!enableOPFSCache;
const fileStreamingScriptName = constants.dataRetrieval.FILE_STREAMING_WORKER_NAME;
const filePartialScriptName = constants.dataRetrieval.FILE_PARTIAL_WORKER_NAME;
this.fileManager = new FileManager();
Expand Down Expand Up @@ -213,7 +213,7 @@ class CodDicomWebServer {
});
}

const directoryHandle = this.options.enableLocalCache && (await getDirectoryHandle());
const directoryHandle = this.options.enableOPFSCache && (await getDirectoryHandle());
const dataRetrievalManager = getDataRetrievalManager();
const { FILE_STREAMING_WORKER_NAME, FILE_PARTIAL_WORKER_NAME } = constants.dataRetrieval;
let tarPromise: Promise<void>;
Expand All @@ -232,7 +232,7 @@ class CodDicomWebServer {
const { url, position, fileArraybuffer } = evt.data;

if (url === fileUrl && fileArraybuffer) {
if (this.options.enableLocalCache) {
if (this.options.enableOPFSCache) {
this.files[fileUrl] = fileArraybuffer;
} else {
this.fileManager.set(url, { data: fileArraybuffer, position });
Expand Down Expand Up @@ -272,7 +272,7 @@ class CodDicomWebServer {
const { url, fileArraybuffer, offsets } = evt.data;

if (url === bytesRemovedUrl && offsets.startByte === startByte && offsets.endByte === endByte) {
if (this.options.enableLocalCache) {
if (this.options.enableOPFSCache) {
this.files[fileUrl] = fileArraybuffer;
} else {
this.fileManager.set(fileUrl, { data: fileArraybuffer, position: fileArraybuffer.length });
Expand Down Expand Up @@ -323,7 +323,7 @@ class CodDicomWebServer {

if (isAppending) {
if (chunk) {
if (this.options.enableLocalCache) {
if (this.options.enableOPFSCache) {
this.files[url].set(chunk, position - chunk.length);
} else {
this.fileManager.append(url, chunk, position);
Expand All @@ -342,7 +342,7 @@ class CodDicomWebServer {

if (!requestResolved && url === fileUrl && position > offsets.endByte) {
try {
const file = this.options.enableLocalCache
const file = this.options.enableOPFSCache
? this.files[url].slice(offsets.startByte, offsets.endByte)
: this.fileManager.get(url, offsets);

Expand All @@ -357,10 +357,10 @@ class CodDicomWebServer {

const completeRequest = (url: string) => {
requestResolved = true;
this.filePromises[url].requestCount--;
dataRetrievalManager.removeEventListener(FILE_STREAMING_WORKER_NAME, 'message', handleChunkAppend);

this.filePromises[url]?.requestCount && this.filePromises[url].requestCount--;

if (fileFetchingCompleted && this.filePromises[url] && !this.filePromises[url]?.requestCount) {
dataRetrievalManager.removeEventListener(FILE_STREAMING_WORKER_NAME, 'message', handleChunkAppend);
delete this.filePromises[url];
delete this.files[url];
}
Expand All @@ -377,7 +377,7 @@ class CodDicomWebServer {
if (!requestResolved) {
if (this.fileManager.getPosition(fileUrl) || this.files[fileUrl]) {
let file: Uint8Array;
if (this.options.enableLocalCache) {
if (this.options.enableOPFSCache) {
file =
isBytesOptimized || !offsets
? this.files[fileUrl]
Expand Down
2 changes: 1 addition & 1 deletion src/dataRetrieval/scripts/filePartial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const filePartial = {

if (directoryHandle) {
const file = (await readFile(directoryHandle, storageName, { offsets, isJson: false })) as ArrayBuffer;
if (file) {
if (file?.byteLength) {
const fileBuffer = new Uint8Array(file);
callBack({ url, fileArraybuffer: fileBuffer, offsets });
return fileBuffer;
Expand Down
2 changes: 1 addition & 1 deletion src/dataRetrieval/scripts/fileStreaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const fileStreaming = {
const fileName = createStreamingFileName(url);
if (directoryHandle) {
const file = (await readFile(directoryHandle, fileName, { isJson: false })) as ArrayBuffer;
if (file) {
if (file?.byteLength) {
const totalLength = file.byteLength;
const fileBuffer = new Uint8Array(file);
callBack({ url, position: totalLength, fileArraybuffer: fileBuffer, totalLength });
Expand Down
6 changes: 3 additions & 3 deletions src/tests/classes/CodDicomWebServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ describe('CodDicomWebServer', () => {
describe('getOptions', () => {
it('should return the default options if not set', () => {
const options = server.getOptions();
expect(options).toEqual({ maxCacheSize: 4 * 1024 * 1024 * 1024, domain: url.DOMAIN, enableLocalCache: false });
expect(options).toEqual({ maxCacheSize: 4 * 1024 * 1024 * 1024, domain: url.DOMAIN, enableOPFSCache: false });
});
});

describe('setOptions', () => {
it('should set new options', () => {
const newOptions = { maxCacheSize: 2000 };
server.setOptions(newOptions);
expect(server.getOptions()).toEqual({ domain: url.DOMAIN, enableLocalCache: false, ...newOptions });
expect(server.getOptions()).toEqual({ domain: url.DOMAIN, enableOPFSCache: false, ...newOptions });
});

it('should not set new options if the value is undefined', () => {
Expand All @@ -93,7 +93,7 @@ describe('CodDicomWebServer', () => {
expect(server.getOptions()).toEqual({
domain: url.DOMAIN,
maxCacheSize: newOptions.maxCacheSize,
enableLocalCache: false
enableOPFSCache: false
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/types/codDicomWebServerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type CodDicomWebServerOptions = {
[key: string]: number | string | boolean;
maxCacheSize: number;
domain: string;
enableLocalCache: boolean;
enableOPFSCache: boolean;
};

export type { CodDicomWebServerOptions };