utilium 1.2.9 → 1.2.10
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.
- package/dist/cache.d.ts +5 -9
- package/dist/cache.js +36 -12
- package/dist/requests.js +1 -1
- package/package.json +1 -1
- package/src/cache.ts +41 -10
- package/src/requests.ts +1 -1
package/dist/cache.d.ts
CHANGED
@@ -37,20 +37,16 @@ export interface Region {
|
|
37
37
|
export declare class Resource<ID> {
|
38
38
|
/** The resource ID */
|
39
39
|
readonly id: ID;
|
40
|
-
|
41
|
-
size: number;
|
40
|
+
protected _size: number;
|
42
41
|
protected readonly options: Options;
|
43
42
|
/** Regions used to reduce unneeded allocations. Think of sparse arrays. */
|
44
43
|
readonly regions: Region[];
|
44
|
+
/** The full size of the resource */
|
45
|
+
get size(): number;
|
46
|
+
set size(value: number);
|
45
47
|
constructor(
|
46
48
|
/** The resource ID */
|
47
|
-
id: ID,
|
48
|
-
/** The full size of the resource */
|
49
|
-
size: number, options: Options, resources?: Map<ID, Resource<ID> | undefined>);
|
50
|
-
/**
|
51
|
-
* Ensure the full size of the resource is *at least* `newSize`
|
52
|
-
*/
|
53
|
-
grow(newSize: number): void;
|
49
|
+
id: ID, _size: number, options: Options, resources?: Map<ID, Resource<ID> | undefined>);
|
54
50
|
/** Combines adjacent regions and combines adjacent ranges within a region */
|
55
51
|
collect(): void;
|
56
52
|
/** Takes an initial range and finds the sub-ranges that are not in the cache */
|
package/dist/cache.js
CHANGED
@@ -6,29 +6,51 @@ import { extendBuffer } from './buffer.js';
|
|
6
6
|
*/
|
7
7
|
export class Resource {
|
8
8
|
id;
|
9
|
-
|
9
|
+
_size;
|
10
10
|
options;
|
11
11
|
/** Regions used to reduce unneeded allocations. Think of sparse arrays. */
|
12
12
|
regions = [];
|
13
|
+
/** The full size of the resource */
|
14
|
+
get size() {
|
15
|
+
return this._size;
|
16
|
+
}
|
17
|
+
set size(value) {
|
18
|
+
if (value >= this._size) {
|
19
|
+
this._size = value;
|
20
|
+
return;
|
21
|
+
}
|
22
|
+
this._size = value;
|
23
|
+
for (let i = this.regions.length - 1; i >= 0; i--) {
|
24
|
+
const region = this.regions[i];
|
25
|
+
if (region.offset >= value) {
|
26
|
+
this.regions.splice(i, 1);
|
27
|
+
continue;
|
28
|
+
}
|
29
|
+
const maxLength = value - region.offset;
|
30
|
+
if (region.data.byteLength > maxLength) {
|
31
|
+
region.data = region.data.subarray(0, maxLength);
|
32
|
+
}
|
33
|
+
region.ranges = region.ranges
|
34
|
+
.filter(range => range.start < value)
|
35
|
+
.map(range => {
|
36
|
+
if (range.end > value) {
|
37
|
+
return { start: range.start, end: value };
|
38
|
+
}
|
39
|
+
return range;
|
40
|
+
});
|
41
|
+
}
|
42
|
+
}
|
13
43
|
constructor(
|
14
44
|
/** The resource ID */
|
15
|
-
id,
|
16
|
-
/** The full size of the resource */
|
17
|
-
size, options, resources) {
|
45
|
+
id, _size, options, resources) {
|
18
46
|
this.id = id;
|
19
|
-
this.
|
47
|
+
this._size = _size;
|
20
48
|
this.options = options;
|
21
49
|
options.sparse ??= true;
|
22
50
|
if (!options.sparse)
|
23
|
-
this.regions.push({ offset: 0, data: new Uint8Array(
|
51
|
+
this.regions.push({ offset: 0, data: new Uint8Array(_size), ranges: [] });
|
24
52
|
resources?.set(id, this);
|
25
53
|
}
|
26
|
-
/**
|
27
|
-
* Ensure the full size of the resource is *at least* `newSize`
|
28
|
-
*/
|
29
|
-
grow(newSize) {
|
30
|
-
this.size = Math.max(this.size, newSize);
|
31
|
-
}
|
32
54
|
/** Combines adjacent regions and combines adjacent ranges within a region */
|
33
55
|
collect() {
|
34
56
|
if (!this.options.sparse)
|
@@ -143,6 +165,7 @@ export class Resource {
|
|
143
165
|
region.data.set(data, offset);
|
144
166
|
region.ranges.push({ start: offset, end });
|
145
167
|
region.ranges.sort((a, b) => a.start - b.start);
|
168
|
+
this.collect();
|
146
169
|
return this;
|
147
170
|
}
|
148
171
|
// Find the correct index to insert the new region
|
@@ -155,6 +178,7 @@ export class Resource {
|
|
155
178
|
else {
|
156
179
|
this.regions.splice(insertIndex, 0, newRegion); // Insert before the first region with a greater offset
|
157
180
|
}
|
181
|
+
this.collect();
|
158
182
|
return this;
|
159
183
|
}
|
160
184
|
}
|
package/dist/requests.js
CHANGED
@@ -116,7 +116,7 @@ export async function set(url, data, options, init = {}) {
|
|
116
116
|
const { offset = 0 } = options;
|
117
117
|
if (!options.cacheOnly)
|
118
118
|
await _fetch(new Request(url, init), { method: 'POST' }, true);
|
119
|
-
resource.add(data, offset)
|
119
|
+
resource.add(data, offset);
|
120
120
|
}
|
121
121
|
/**
|
122
122
|
* Make a DELETE request to remove the resource from the server and clear it from the cache.
|
package/package.json
CHANGED
package/src/cache.ts
CHANGED
@@ -45,27 +45,56 @@ export class Resource<ID> {
|
|
45
45
|
/** Regions used to reduce unneeded allocations. Think of sparse arrays. */
|
46
46
|
public readonly regions: Region[] = [];
|
47
47
|
|
48
|
+
/** The full size of the resource */
|
49
|
+
public get size() {
|
50
|
+
return this._size;
|
51
|
+
}
|
52
|
+
|
53
|
+
public set size(value: number) {
|
54
|
+
if (value >= this._size) {
|
55
|
+
this._size = value;
|
56
|
+
return;
|
57
|
+
}
|
58
|
+
|
59
|
+
this._size = value;
|
60
|
+
|
61
|
+
for (let i = this.regions.length - 1; i >= 0; i--) {
|
62
|
+
const region = this.regions[i];
|
63
|
+
|
64
|
+
if (region.offset >= value) {
|
65
|
+
this.regions.splice(i, 1);
|
66
|
+
continue;
|
67
|
+
}
|
68
|
+
|
69
|
+
const maxLength = value - region.offset;
|
70
|
+
if (region.data.byteLength > maxLength) {
|
71
|
+
region.data = region.data.subarray(0, maxLength);
|
72
|
+
}
|
73
|
+
|
74
|
+
region.ranges = region.ranges
|
75
|
+
.filter(range => range.start < value)
|
76
|
+
.map(range => {
|
77
|
+
if (range.end > value) {
|
78
|
+
return { start: range.start, end: value };
|
79
|
+
}
|
80
|
+
return range;
|
81
|
+
});
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
48
85
|
public constructor(
|
49
86
|
/** The resource ID */
|
50
87
|
public readonly id: ID,
|
51
|
-
|
52
|
-
public size: number,
|
88
|
+
protected _size: number,
|
53
89
|
protected readonly options: Options,
|
54
90
|
resources?: Map<ID, Resource<ID> | undefined>
|
55
91
|
) {
|
56
92
|
options.sparse ??= true;
|
57
|
-
if (!options.sparse) this.regions.push({ offset: 0, data: new Uint8Array(
|
93
|
+
if (!options.sparse) this.regions.push({ offset: 0, data: new Uint8Array(_size), ranges: [] });
|
58
94
|
|
59
95
|
resources?.set(id, this);
|
60
96
|
}
|
61
97
|
|
62
|
-
/**
|
63
|
-
* Ensure the full size of the resource is *at least* `newSize`
|
64
|
-
*/
|
65
|
-
public grow(newSize: number) {
|
66
|
-
this.size = Math.max(this.size, newSize);
|
67
|
-
}
|
68
|
-
|
69
98
|
/** Combines adjacent regions and combines adjacent ranges within a region */
|
70
99
|
public collect(): void {
|
71
100
|
if (!this.options.sparse) return;
|
@@ -191,6 +220,7 @@ export class Resource<ID> {
|
|
191
220
|
region.ranges.push({ start: offset, end });
|
192
221
|
region.ranges.sort((a, b) => a.start - b.start);
|
193
222
|
|
223
|
+
this.collect();
|
194
224
|
return this;
|
195
225
|
}
|
196
226
|
|
@@ -205,6 +235,7 @@ export class Resource<ID> {
|
|
205
235
|
this.regions.splice(insertIndex, 0, newRegion); // Insert before the first region with a greater offset
|
206
236
|
}
|
207
237
|
|
238
|
+
this.collect();
|
208
239
|
return this;
|
209
240
|
}
|
210
241
|
}
|
package/src/requests.ts
CHANGED
@@ -201,7 +201,7 @@ export async function set(url: string, data: Uint8Array, options: SetOptions, in
|
|
201
201
|
|
202
202
|
if (!options.cacheOnly) await _fetch(new Request(url, init), { method: 'POST' }, true);
|
203
203
|
|
204
|
-
resource.add(data, offset)
|
204
|
+
resource.add(data, offset);
|
205
205
|
}
|
206
206
|
|
207
207
|
/**
|