forked from google/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.ts
130 lines (127 loc) · 4.18 KB
/
backend.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @license
* Copyright 2019 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { decodeBlosc } from "#src/async_computation/decode_blosc_request.js";
import { decodeZstd } from "#src/async_computation/decode_zstd_request.js";
import { requestAsyncComputation } from "#src/async_computation/request.js";
import { WithParameters } from "#src/chunk_manager/backend.js";
import { WithSharedCredentialsProviderCounterpart } from "#src/credentials_provider/shared_counterpart.js";
import {
VolumeChunkEncoding,
VolumeChunkSourceParameters,
} from "#src/datasource/n5/base.js";
import { decodeRawChunk } from "#src/sliceview/backend_chunk_decoders/raw.js";
import type { VolumeChunk } from "#src/sliceview/volume/backend.js";
import { VolumeChunkSource } from "#src/sliceview/volume/backend.js";
import type { CancellationToken } from "#src/util/cancellation.js";
import { Endianness } from "#src/util/endian.js";
import { decodeGzip } from "#src/util/gzip.js";
import {
isNotFoundError,
responseArrayBuffer,
} from "#src/util/http_request.js";
import type { SpecialProtocolCredentials } from "#src/util/special_protocol_request.js";
import { cancellableFetchSpecialOk } from "#src/util/special_protocol_request.js";
import { registerSharedObject } from "#src/worker_rpc.js";
async function decodeChunk(
chunk: VolumeChunk,
cancellationToken: CancellationToken,
response: ArrayBuffer,
encoding: VolumeChunkEncoding,
) {
const dv = new DataView(response);
const mode = dv.getUint16(0, /*littleEndian=*/ false);
if (mode !== 0) {
throw new Error(`Unsupported mode: ${mode}.`);
}
const numDimensions = dv.getUint16(2, /*littleEndian=*/ false);
if (numDimensions !== chunk.source!.spec.rank) {
throw new Error("Number of dimensions must be 3.");
}
let offset = 4;
const shape = new Uint32Array(numDimensions);
for (let i = 0; i < numDimensions; ++i) {
shape[i] = dv.getUint32(offset, /*littleEndian=*/ false);
offset += 4;
}
chunk.chunkDataSize = shape;
let buffer = new Uint8Array(response, offset);
switch (encoding) {
case VolumeChunkEncoding.ZLIB:
buffer = new Uint8Array(await decodeGzip(buffer, "deflate"));
break;
case VolumeChunkEncoding.GZIP:
buffer = new Uint8Array(await decodeGzip(buffer, "gzip"));
break;
case VolumeChunkEncoding.BLOSC:
buffer = await requestAsyncComputation(
decodeBlosc,
cancellationToken,
[buffer.buffer],
buffer,
);
break;
case VolumeChunkEncoding.ZSTD:
buffer = await requestAsyncComputation(
decodeZstd,
cancellationToken,
[buffer.buffer],
buffer,
);
break;
}
await decodeRawChunk(
chunk,
cancellationToken,
buffer.buffer,
Endianness.BIG,
buffer.byteOffset,
buffer.byteLength,
);
}
@registerSharedObject()
export class PrecomputedVolumeChunkSource extends WithParameters(
WithSharedCredentialsProviderCounterpart<SpecialProtocolCredentials>()(
VolumeChunkSource,
),
VolumeChunkSourceParameters,
) {
async download(chunk: VolumeChunk, cancellationToken: CancellationToken) {
const { parameters } = this;
const { chunkGridPosition } = chunk;
let url = parameters.url;
const rank = this.spec.rank;
for (let i = 0; i < rank; ++i) {
url += `/${chunkGridPosition[i]}`;
}
try {
const response = await cancellableFetchSpecialOk(
this.credentialsProvider,
url,
{},
responseArrayBuffer,
cancellationToken,
);
await decodeChunk(
chunk,
cancellationToken,
response,
parameters.encoding,
);
} catch (e) {
if (!isNotFoundError(e)) throw e;
}
}
}