segmind 0.5.0 → 0.5.1
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/index.cjs +29 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +29 -16
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -512,22 +512,22 @@ function mimeFromName(name) {
|
|
|
512
512
|
const extension = name.includes(".") ? name.split(".").pop().toLowerCase() : "";
|
|
513
513
|
return MIME_BY_EXTENSION[extension];
|
|
514
514
|
}
|
|
515
|
-
async function
|
|
516
|
-
let
|
|
515
|
+
async function openFileInput(input, options) {
|
|
516
|
+
let body;
|
|
517
517
|
let name = options.filename;
|
|
518
518
|
let mimeType = options.mimeType;
|
|
519
519
|
if (typeof input === "string") {
|
|
520
520
|
if (detectRuntime() !== "node") {
|
|
521
521
|
throw new SegmindError(`${options.caller}: file paths work in Node only; pass a Blob or bytes`);
|
|
522
522
|
}
|
|
523
|
-
const {
|
|
523
|
+
const { openAsBlob } = await import(
|
|
524
524
|
/* webpackIgnore: true */
|
|
525
525
|
/* turbopackIgnore: true */
|
|
526
526
|
/* @vite-ignore */
|
|
527
|
-
"node:fs
|
|
527
|
+
"node:fs"
|
|
528
528
|
);
|
|
529
529
|
try {
|
|
530
|
-
|
|
530
|
+
body = await openAsBlob(input);
|
|
531
531
|
} catch (err) {
|
|
532
532
|
throw new SegmindError(`${options.caller}: cannot read ${JSON.stringify(input)}: ${err.message}`, {
|
|
533
533
|
cause: err
|
|
@@ -535,13 +535,13 @@ async function readFileInput(input, options) {
|
|
|
535
535
|
}
|
|
536
536
|
name ??= input.split(/[\\/]/).pop();
|
|
537
537
|
} else if (typeof Blob !== "undefined" && input instanceof Blob) {
|
|
538
|
-
|
|
538
|
+
body = input;
|
|
539
539
|
name ??= input.name || void 0;
|
|
540
540
|
mimeType ??= input.type || void 0;
|
|
541
541
|
} else {
|
|
542
|
-
|
|
542
|
+
body = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
543
543
|
}
|
|
544
|
-
mimeType ??= name && mimeFromName(name) || sniff(
|
|
544
|
+
mimeType ??= name && mimeFromName(name) || sniff(await head(body));
|
|
545
545
|
if (!name || !name.includes(".")) {
|
|
546
546
|
const extension = mimeType ? EXTENSION_BY_MIME[mimeType] : void 0;
|
|
547
547
|
if (!extension) {
|
|
@@ -549,7 +549,17 @@ async function readFileInput(input, options) {
|
|
|
549
549
|
}
|
|
550
550
|
name = `${name || "upload"}.${extension}`;
|
|
551
551
|
}
|
|
552
|
-
|
|
552
|
+
const size = body instanceof Uint8Array ? body.byteLength : body.size;
|
|
553
|
+
return { body, size, filename: name, mimeType };
|
|
554
|
+
}
|
|
555
|
+
async function readFileInput(input, options) {
|
|
556
|
+
const { body, filename, mimeType } = await openFileInput(input, options);
|
|
557
|
+
const bytes = body instanceof Uint8Array ? body : new Uint8Array(await body.arrayBuffer());
|
|
558
|
+
return { bytes, filename, mimeType };
|
|
559
|
+
}
|
|
560
|
+
async function head(body) {
|
|
561
|
+
if (body instanceof Uint8Array) return body;
|
|
562
|
+
return new Uint8Array(await body.slice(0, 16).arrayBuffer());
|
|
553
563
|
}
|
|
554
564
|
function sniff(bytes) {
|
|
555
565
|
const at = (offset, signature) => signature.every((b, i) => bytes[offset + i] === b);
|
|
@@ -579,16 +589,16 @@ var Files = class {
|
|
|
579
589
|
return this.uploadOne(input, options);
|
|
580
590
|
}
|
|
581
591
|
async uploadOne(input, options) {
|
|
582
|
-
const file = await
|
|
592
|
+
const file = await openFileInput(input, { ...options, caller: "files.upload" });
|
|
583
593
|
const url = options.persist ? await this.persist(file, options.signal) : await this.stage(file, options.signal);
|
|
584
|
-
return { url, filename: file.filename, size: file.
|
|
594
|
+
return { url, filename: file.filename, size: file.size, persistent: options.persist === true };
|
|
585
595
|
}
|
|
586
596
|
/** A temporary model input on the inference API's input bucket. */
|
|
587
597
|
async stage(file, signal) {
|
|
588
598
|
const ticket = await this.client.http.json({
|
|
589
599
|
method: "POST",
|
|
590
600
|
url: `${this.client.spotURL}/inference-request/input-presign`,
|
|
591
|
-
body: { filename: file.filename, size: file.
|
|
601
|
+
body: { filename: file.filename, size: file.size },
|
|
592
602
|
signal,
|
|
593
603
|
retry: true
|
|
594
604
|
});
|
|
@@ -611,7 +621,7 @@ var Files = class {
|
|
|
611
621
|
const ticket = await http.json({
|
|
612
622
|
method: "POST",
|
|
613
623
|
url: `${assetsURL}/upload-asset/presign`,
|
|
614
|
-
body: { filename: file.filename, contentType, size: file.
|
|
624
|
+
body: { filename: file.filename, contentType, size: file.size },
|
|
615
625
|
signal,
|
|
616
626
|
retry: true
|
|
617
627
|
});
|
|
@@ -630,9 +640,12 @@ var Files = class {
|
|
|
630
640
|
});
|
|
631
641
|
return confirmed.asset?.url ?? ticket.publicUrl;
|
|
632
642
|
}
|
|
633
|
-
/**
|
|
643
|
+
/**
|
|
644
|
+
* A PUT to the same key is idempotent, so it retries; a `Blob` body can be
|
|
645
|
+
* sent again, unlike a stream. `bare` keeps the API key off the storage host.
|
|
646
|
+
*/
|
|
634
647
|
async put(url, file, headers, signal) {
|
|
635
|
-
await this.client.http.request({ method: "PUT", url, rawBody: file.
|
|
648
|
+
await this.client.http.request({ method: "PUT", url, rawBody: file.body, headers, bare: true, signal, retry: true });
|
|
636
649
|
}
|
|
637
650
|
};
|
|
638
651
|
|
|
@@ -877,7 +890,7 @@ function failureDetail(body) {
|
|
|
877
890
|
}
|
|
878
891
|
|
|
879
892
|
// src/version.ts
|
|
880
|
-
var VERSION = "0.5.
|
|
893
|
+
var VERSION = "0.5.1";
|
|
881
894
|
|
|
882
895
|
// src/webhooks.ts
|
|
883
896
|
var Webhooks = class {
|