@hackbuild/ai-views 0.1.13 → 0.1.14

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.
@@ -6016,18 +6016,19 @@ var HackbuildAiViews = (function(exports) {
6016
6016
  }
6017
6017
  };
6018
6018
  }));
6019
- // packages/weights/src/store.ts
6020
- function pickBackends(opts) {
6021
- const spec = opts.backends ?? "auto";
6022
- const onFile = (opts.protocol ?? globalThis.location?.protocol) === "file:";
6023
- const g = globalThis;
6024
- return (spec === "auto" ? [
6025
- !onFile && typeof g.navigator?.storage?.getDirectory === "function" ? "opfs" : null,
6026
- g.indexedDB ? "idb" : null,
6027
- !onFile && g.caches ? "cache" : null,
6028
- "memory"
6029
- ].filter(Boolean) : spec).map((n) => typeof n !== "string" ? n : n === "opfs" ? opfsBackend() : n === "idb" ? idbBackend() : n === "cache" ? cacheBackend() : memoryBackend());
6030
- }
6019
+ // packages/weights/src/types.ts
6020
+ var WeightError;
6021
+ var init_types$2 = __esmMin((() => {
6022
+ WeightError = class extends Error {
6023
+ code;
6024
+ constructor(code, message) {
6025
+ super(message);
6026
+ this.name = "WeightError";
6027
+ this.code = code;
6028
+ }
6029
+ };
6030
+ }));
6031
+ // packages/weights/src/download.ts
6031
6032
  function parseContentRange(h) {
6032
6033
  const m = h && /bytes\s+(\d+)-\d+\/(\d+|\*)/i.exec(h);
6033
6034
  if (!m) return null;
@@ -6058,6 +6059,175 @@ var HackbuildAiViews = (function(exports) {
6058
6059
  }
6059
6060
  return new Blob(parts);
6060
6061
  }
6062
+ async function download(d, ref, key, o) {
6063
+ const { backend: be, partBytes, fetch: doFetch, now, estimate } = d;
6064
+ let meta = await be.getMeta(key);
6065
+ if (meta?.complete) {
6066
+ const blob = await assemble(be, meta);
6067
+ if (blob && (!ref.sha256 || meta.sha256 === ref.sha256.toLowerCase())) return blob;
6068
+ await be.remove(key);
6069
+ meta = null;
6070
+ }
6071
+ const resume = o.resume !== false;
6072
+ if (meta && (!resume || meta.url !== ref.url)) {
6073
+ await be.remove(key);
6074
+ meta = null;
6075
+ }
6076
+ const hash = new Sha256();
6077
+ let offset = 0;
6078
+ if (meta && meta.parts.length) for (let n = 0; n < meta.parts.length; n++) {
6079
+ const p = await be.getPart(key, n);
6080
+ if (!p || p.size !== meta.parts[n]) {
6081
+ meta.parts = meta.parts.slice(0, n);
6082
+ break;
6083
+ }
6084
+ hash.update(new Uint8Array(await p.arrayBuffer()));
6085
+ offset += p.size;
6086
+ o.onProgress?.({
6087
+ key,
6088
+ loaded: offset,
6089
+ total: meta.total,
6090
+ fraction: meta.total ? offset / meta.total : null,
6091
+ resuming: true
6092
+ });
6093
+ }
6094
+ if (!meta) meta = {
6095
+ key,
6096
+ url: ref.url,
6097
+ licence: ref.licence,
6098
+ total: null,
6099
+ parts: [],
6100
+ complete: false,
6101
+ etag: null,
6102
+ lastModified: null,
6103
+ sha256: null,
6104
+ updated: now()
6105
+ };
6106
+ const headers = {};
6107
+ if (offset > 0) {
6108
+ headers["range"] = `bytes=${offset}-`;
6109
+ const validator = meta.etag ?? meta.lastModified;
6110
+ if (validator) headers["if-range"] = validator;
6111
+ }
6112
+ const init = { headers };
6113
+ if (o.signal) init.signal = o.signal;
6114
+ let res;
6115
+ try {
6116
+ res = await doFetch(ref.url, init);
6117
+ } catch (e) {
6118
+ if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted`);
6119
+ throw new WeightError("http", `fetch ${ref.url} failed: ${e instanceof Error ? e.message : String(e)}`);
6120
+ }
6121
+ if (offset > 0 && res.status === 416 && meta.total !== null && offset >= meta.total) {} else if (offset > 0 && res.status === 416 && meta.total === null) meta.total = offset;
6122
+ else if (offset > 0 && res.status === 206 && !sameFile(meta, res, !!ref.sha256)) {
6123
+ await res.body?.cancel().catch(() => void 0);
6124
+ await be.remove(key);
6125
+ return download(d, ref, key, {
6126
+ ...o,
6127
+ resume: false
6128
+ });
6129
+ } else if (offset > 0 && res.status === 206) {
6130
+ const cr = parseContentRange(res.headers.get("content-range"));
6131
+ if (!cr || cr.start !== offset) throw new WeightError("range", `server resumed ${key} at ${cr?.start ?? "unknown"}, expected ${offset}`);
6132
+ if (cr.total !== null) meta.total = cr.total;
6133
+ } else if (res.ok) {
6134
+ if (offset > 0) {
6135
+ await res.body?.cancel().catch(() => void 0);
6136
+ await be.remove(key);
6137
+ return download(d, ref, key, {
6138
+ ...o,
6139
+ resume: false
6140
+ });
6141
+ }
6142
+ const len = res.headers.get("content-length");
6143
+ meta.total = len !== null && /^\d+$/.test(len) && !res.headers.get("content-encoding") ? Number(len) : null;
6144
+ } else throw new WeightError("http", `http ${res.status} for ${ref.url}`);
6145
+ meta.etag = res.headers.get("etag") ?? meta.etag;
6146
+ meta.lastModified = res.headers.get("last-modified") ?? meta.lastModified;
6147
+ meta.updated = now();
6148
+ await be.putMeta(meta);
6149
+ if (meta.total !== null && offset === 0) {
6150
+ const est = await estimate();
6151
+ if (!est.fits(meta.total)) throw new WeightError("quota", `not enough storage for ${key}: needs ${meta.total} bytes plus a 20 percent margin, ${(est.quota ?? 0) - (est.used ?? 0)} free`);
6152
+ }
6153
+ let cur = [];
6154
+ let curBytes = 0;
6155
+ let loaded = offset;
6156
+ const flush = async () => {
6157
+ if (!curBytes) return;
6158
+ const n = meta.parts.length;
6159
+ await be.putPart(key, n, new Blob(cur));
6160
+ meta.parts.push(curBytes);
6161
+ meta.updated = now();
6162
+ await be.putMeta(meta);
6163
+ cur = [];
6164
+ curBytes = 0;
6165
+ };
6166
+ if (res.status !== 416 && res.body) {
6167
+ const reader = res.body.getReader();
6168
+ try {
6169
+ for (;;) {
6170
+ const { done, value } = await reader.read();
6171
+ if (done) break;
6172
+ if (o.signal?.aborted) throw new Error("aborted");
6173
+ hash.update(value);
6174
+ loaded += value.byteLength;
6175
+ let rest = value;
6176
+ while (rest.byteLength) {
6177
+ const take = Math.min(rest.byteLength, partBytes - curBytes);
6178
+ cur.push(rest.subarray(0, take));
6179
+ curBytes += take;
6180
+ rest = rest.subarray(take);
6181
+ if (curBytes >= partBytes) await flush();
6182
+ }
6183
+ o.onProgress?.({
6184
+ key,
6185
+ loaded,
6186
+ total: meta.total,
6187
+ fraction: meta.total ? Math.min(1, loaded / meta.total) : null,
6188
+ resuming: false
6189
+ });
6190
+ }
6191
+ } catch (e) {
6192
+ await flush().catch(() => void 0);
6193
+ if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted at ${loaded} bytes; parts kept for resume`);
6194
+ throw new WeightError("http", `download of ${key} broke at ${loaded} bytes, parts kept for resume: ${e instanceof Error ? e.message : String(e)}`);
6195
+ }
6196
+ }
6197
+ await flush();
6198
+ if (meta.total !== null && loaded !== meta.total) throw new WeightError("http", `download of ${key} ended at ${loaded} of ${meta.total} bytes; parts kept for resume`);
6199
+ const digest = hash.digestHex();
6200
+ if (ref.sha256 && digest !== ref.sha256.toLowerCase()) {
6201
+ await be.remove(key);
6202
+ throw new WeightError("sha256", `sha256 of ${key} is ${digest}, expected ${ref.sha256.toLowerCase()}; the parts were removed`);
6203
+ }
6204
+ meta.total = loaded;
6205
+ meta.sha256 = digest;
6206
+ meta.complete = true;
6207
+ meta.updated = now();
6208
+ await be.putMeta(meta);
6209
+ const blob = await assemble(be, meta);
6210
+ if (!blob) throw new WeightError("http", `parts of ${key} went missing after the download`);
6211
+ return blob;
6212
+ }
6213
+ var bareTag;
6214
+ var init_download = __esmMin((() => {
6215
+ init_sha256();
6216
+ init_types$2();
6217
+ bareTag = (t) => t ? t.replace(/^W\//, "").replace(/^"|"$/g, "") : null;
6218
+ }));
6219
+ // packages/weights/src/store.ts
6220
+ function pickBackends(opts) {
6221
+ const spec = opts.backends ?? "auto";
6222
+ const onFile = (opts.protocol ?? globalThis.location?.protocol) === "file:";
6223
+ const g = globalThis;
6224
+ return (spec === "auto" ? [
6225
+ !onFile && typeof g.navigator?.storage?.getDirectory === "function" ? "opfs" : null,
6226
+ g.indexedDB ? "idb" : null,
6227
+ !onFile && g.caches ? "cache" : null,
6228
+ "memory"
6229
+ ].filter(Boolean) : spec).map((n) => typeof n !== "string" ? n : n === "opfs" ? opfsBackend() : n === "idb" ? idbBackend() : n === "cache" ? cacheBackend() : memoryBackend());
6230
+ }
6061
6231
  /**
6062
6232
  * Open a store. The first backend whose meta read works is used; a backend
6063
6233
  * that throws on open (OPFS on file://, a blocked IndexedDB) is skipped.
@@ -6121,156 +6291,13 @@ var HackbuildAiViews = (function(exports) {
6121
6291
  fits: (bytes) => quota === null || quota - (used ?? 0) >= 1.2 * bytes
6122
6292
  };
6123
6293
  };
6124
- async function download(ref, key, o) {
6125
- let meta = await be.getMeta(key);
6126
- if (meta?.complete) {
6127
- const blob = await assemble(be, meta);
6128
- if (blob && (!ref.sha256 || meta.sha256 === ref.sha256.toLowerCase())) return blob;
6129
- await be.remove(key);
6130
- meta = null;
6131
- }
6132
- const resume = o.resume !== false;
6133
- if (meta && (!resume || meta.url !== ref.url)) {
6134
- await be.remove(key);
6135
- meta = null;
6136
- }
6137
- const hash = new Sha256();
6138
- let offset = 0;
6139
- if (meta && meta.parts.length) for (let n = 0; n < meta.parts.length; n++) {
6140
- const p = await be.getPart(key, n);
6141
- if (!p || p.size !== meta.parts[n]) {
6142
- meta.parts = meta.parts.slice(0, n);
6143
- break;
6144
- }
6145
- hash.update(new Uint8Array(await p.arrayBuffer()));
6146
- offset += p.size;
6147
- o.onProgress?.({
6148
- key,
6149
- loaded: offset,
6150
- total: meta.total,
6151
- fraction: meta.total ? offset / meta.total : null,
6152
- resuming: true
6153
- });
6154
- }
6155
- if (!meta) meta = {
6156
- key,
6157
- url: ref.url,
6158
- licence: ref.licence,
6159
- total: null,
6160
- parts: [],
6161
- complete: false,
6162
- etag: null,
6163
- lastModified: null,
6164
- sha256: null,
6165
- updated: now()
6166
- };
6167
- const headers = {};
6168
- if (offset > 0) {
6169
- headers["range"] = `bytes=${offset}-`;
6170
- const validator = meta.etag ?? meta.lastModified;
6171
- if (validator) headers["if-range"] = validator;
6172
- }
6173
- const init = { headers };
6174
- if (o.signal) init.signal = o.signal;
6175
- let res;
6176
- try {
6177
- res = await doFetch(ref.url, init);
6178
- } catch (e) {
6179
- if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted`);
6180
- throw new WeightError("http", `fetch ${ref.url} failed: ${e instanceof Error ? e.message : String(e)}`);
6181
- }
6182
- if (offset > 0 && res.status === 416 && meta.total !== null && offset >= meta.total) {} else if (offset > 0 && res.status === 416 && meta.total === null) meta.total = offset;
6183
- else if (offset > 0 && res.status === 206 && !sameFile(meta, res, !!ref.sha256)) {
6184
- await res.body?.cancel().catch(() => void 0);
6185
- await be.remove(key);
6186
- return download(ref, key, {
6187
- ...o,
6188
- resume: false
6189
- });
6190
- } else if (offset > 0 && res.status === 206) {
6191
- const cr = parseContentRange(res.headers.get("content-range"));
6192
- if (!cr || cr.start !== offset) throw new WeightError("range", `server resumed ${key} at ${cr?.start ?? "unknown"}, expected ${offset}`);
6193
- if (cr.total !== null) meta.total = cr.total;
6194
- } else if (res.ok) {
6195
- if (offset > 0) {
6196
- await res.body?.cancel().catch(() => void 0);
6197
- await be.remove(key);
6198
- return download(ref, key, {
6199
- ...o,
6200
- resume: false
6201
- });
6202
- }
6203
- const len = res.headers.get("content-length");
6204
- meta.total = len !== null && /^\d+$/.test(len) && !res.headers.get("content-encoding") ? Number(len) : null;
6205
- } else throw new WeightError("http", `http ${res.status} for ${ref.url}`);
6206
- meta.etag = res.headers.get("etag") ?? meta.etag;
6207
- meta.lastModified = res.headers.get("last-modified") ?? meta.lastModified;
6208
- meta.updated = now();
6209
- await be.putMeta(meta);
6210
- if (meta.total !== null && offset === 0) {
6211
- const est = await estimate();
6212
- if (!est.fits(meta.total)) throw new WeightError("quota", `not enough storage for ${key}: needs ${meta.total} bytes plus a 20 percent margin, ${(est.quota ?? 0) - (est.used ?? 0)} free`);
6213
- }
6214
- let cur = [];
6215
- let curBytes = 0;
6216
- let loaded = offset;
6217
- const flush = async () => {
6218
- if (!curBytes) return;
6219
- const n = meta.parts.length;
6220
- await be.putPart(key, n, new Blob(cur));
6221
- meta.parts.push(curBytes);
6222
- meta.updated = now();
6223
- await be.putMeta(meta);
6224
- cur = [];
6225
- curBytes = 0;
6226
- };
6227
- if (res.status !== 416 && res.body) {
6228
- const reader = res.body.getReader();
6229
- try {
6230
- for (;;) {
6231
- const { done, value } = await reader.read();
6232
- if (done) break;
6233
- if (o.signal?.aborted) throw new Error("aborted");
6234
- hash.update(value);
6235
- loaded += value.byteLength;
6236
- let rest = value;
6237
- while (rest.byteLength) {
6238
- const take = Math.min(rest.byteLength, partBytes - curBytes);
6239
- cur.push(rest.subarray(0, take));
6240
- curBytes += take;
6241
- rest = rest.subarray(take);
6242
- if (curBytes >= partBytes) await flush();
6243
- }
6244
- o.onProgress?.({
6245
- key,
6246
- loaded,
6247
- total: meta.total,
6248
- fraction: meta.total ? Math.min(1, loaded / meta.total) : null,
6249
- resuming: false
6250
- });
6251
- }
6252
- } catch (e) {
6253
- await flush().catch(() => void 0);
6254
- if (o.signal?.aborted) throw new WeightError("abort", `download of ${key} aborted at ${loaded} bytes; parts kept for resume`);
6255
- throw new WeightError("http", `download of ${key} broke at ${loaded} bytes, parts kept for resume: ${e instanceof Error ? e.message : String(e)}`);
6256
- }
6257
- }
6258
- await flush();
6259
- if (meta.total !== null && loaded !== meta.total) throw new WeightError("http", `download of ${key} ended at ${loaded} of ${meta.total} bytes; parts kept for resume`);
6260
- const digest = hash.digestHex();
6261
- if (ref.sha256 && digest !== ref.sha256.toLowerCase()) {
6262
- await be.remove(key);
6263
- throw new WeightError("sha256", `sha256 of ${key} is ${digest}, expected ${ref.sha256.toLowerCase()}; the parts were removed`);
6264
- }
6265
- meta.total = loaded;
6266
- meta.sha256 = digest;
6267
- meta.complete = true;
6268
- meta.updated = now();
6269
- await be.putMeta(meta);
6270
- const blob = await assemble(be, meta);
6271
- if (!blob) throw new WeightError("http", `parts of ${key} went missing after the download`);
6272
- return blob;
6273
- }
6294
+ const downloader = {
6295
+ backend: be,
6296
+ partBytes,
6297
+ fetch: doFetch,
6298
+ now,
6299
+ estimate
6300
+ };
6274
6301
  const store = {
6275
6302
  backend: be,
6276
6303
  get(ref, o = {}) {
@@ -6307,7 +6334,7 @@ var HackbuildAiViews = (function(exports) {
6307
6334
  progress,
6308
6335
  row
6309
6336
  };
6310
- flight.p = before.then(() => locked(key, ac.signal, () => download(ref, key, dl))).finally(() => {
6337
+ flight.p = before.then(() => locked(key, ac.signal, () => download(downloader, ref, key, dl))).finally(() => {
6311
6338
  if (inflight.get(key) === flight) inflight.delete(key);
6312
6339
  tell();
6313
6340
  });
@@ -6454,19 +6481,12 @@ var HackbuildAiViews = (function(exports) {
6454
6481
  seen.add(model);
6455
6482
  return true;
6456
6483
  }
6457
- var WeightError, bareTag, HF_REPO, SEEN;
6484
+ var HF_REPO, SEEN;
6458
6485
  var init_store = __esmMin((() => {
6459
6486
  init_backends();
6460
6487
  init_sha256();
6461
- WeightError = class extends Error {
6462
- code;
6463
- constructor(code, message) {
6464
- super(message);
6465
- this.name = "WeightError";
6466
- this.code = code;
6467
- }
6468
- };
6469
- bareTag = (t) => t ? t.replace(/^W\//, "").replace(/^"|"$/g, "") : null;
6488
+ init_download();
6489
+ init_types$2();
6470
6490
  HF_REPO = /^https:\/\/huggingface\.co\/([^/]+\/[^/]+)\/resolve\//;
6471
6491
  SEEN = Symbol.for("hackbuild.licence.seen");
6472
6492
  }));