utilium 2.3.3 → 2.3.4

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.
@@ -70,6 +70,8 @@ interface SetOptions extends Options {
70
70
  offset?: number;
71
71
  /** If a cache for the resource doesn't exist, this will be used as the full size */
72
72
  size?: number;
73
+ /** The method to use for the request */
74
+ method?: 'POST' | 'PUT';
73
75
  }
74
76
  /**
75
77
  * Make a POST request to set (or create) data on the server and update the cache.
package/dist/requests.js CHANGED
@@ -116,9 +116,26 @@ export async function set(url, data, options, init = {}) {
116
116
  new cache.Resource(url, options.size ?? data.byteLength, options, resourcesCache);
117
117
  }
118
118
  const resource = resourcesCache.get(url);
119
- const { offset = 0 } = options;
120
- if (!options.cacheOnly)
121
- await _fetch(new Request(url, init), { method: 'POST' }, true);
119
+ const { offset = 0, method = 'POST' } = options;
120
+ // Skip the server request if we are only updating the cache
121
+ if (!options.cacheOnly) {
122
+ const headers = new Headers(init.headers || {});
123
+ if (!headers.get('Content-Type')) {
124
+ headers.set('Content-Type', 'application/octet-stream');
125
+ }
126
+ if (!headers.get('Content-Range') && (offset !== 0 || data.byteLength !== resource.size)) {
127
+ const start = offset;
128
+ const end = offset + data.byteLength - 1;
129
+ const total = Math.max(resource.size, end + 1);
130
+ headers.set('Content-Range', `bytes ${start}-${end}/${total}`);
131
+ }
132
+ await _fetch(new Request(url, {
133
+ ...init,
134
+ method,
135
+ headers,
136
+ body: data,
137
+ }), {}, true);
138
+ }
122
139
  resource.add(data, offset);
123
140
  }
124
141
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "2.3.3",
3
+ "version": "2.3.4",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",