@tstdl/base 0.92.87 → 0.92.88

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.
@@ -57,20 +57,17 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
57
57
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
58
58
  });
59
59
  import '../polyfills.js';
60
- import { stat, unlink, writeFile } from 'node:fs/promises';
61
- import { tmpdir } from 'node:os';
62
- import { join } from 'node:path';
63
60
  import { Storage } from '@google-cloud/storage';
64
61
  import { FileState, GoogleAIFileManager } from '@google/generative-ai/server';
65
62
  import { AsyncEnumerable } from '../enumerable/async-enumerable.js';
66
63
  import { DetailsError } from '../errors/details.error.js';
64
+ import { TemporaryFile } from '../file/temporary-file.js';
67
65
  import { Singleton } from '../injector/decorators.js';
68
66
  import { inject, injectArgument } from '../injector/inject.js';
69
67
  import { Logger } from '../logger/logger.js';
70
68
  import { createArray } from '../utils/array/array.js';
71
69
  import { formatBytes } from '../utils/format.js';
72
70
  import { timeout } from '../utils/timing.js';
73
- import { tryIgnoreAsync } from '../utils/try-ignore.js';
74
71
  import { assertDefinedPass, isBlob, isDefined, isUndefined } from '../utils/type-guards.js';
75
72
  import { millisecondsPerSecond } from '../utils/units.js';
76
73
  let AiFileService = class AiFileService {
@@ -118,15 +115,13 @@ let AiFileService = class AiFileService {
118
115
  async uploadFile(fileInput, id) {
119
116
  const env_1 = { stack: [], error: void 0, hasError: false };
120
117
  try {
121
- const path = isBlob(fileInput) ? join(tmpdir(), crypto.randomUUID()) : fileInput.path;
122
- const mimeType = isBlob(fileInput) ? fileInput.type : fileInput.mimeType;
123
- const stack = __addDisposableResource(env_1, new AsyncDisposableStack(), true);
124
- if (isBlob(fileInput)) {
125
- this.#logger.verbose(`Preparing file "${id}"...`);
126
- stack.defer(async () => tryIgnoreAsync(async () => unlink(path)));
127
- await writeFile(path, fileInput.stream());
128
- }
129
- const fileSize = isBlob(fileInput) ? fileInput.size : (await stat(path)).size;
118
+ const inputIsBlob = isBlob(fileInput);
119
+ const tmpFile = __addDisposableResource(env_1, inputIsBlob
120
+ ? await TemporaryFile.from(fileInput.stream())
121
+ : undefined, true);
122
+ const path = inputIsBlob ? tmpFile.path : fileInput.path;
123
+ const mimeType = inputIsBlob ? fileInput.type : fileInput.mimeType;
124
+ const fileSize = inputIsBlob ? fileInput.size : await tmpFile.size();
130
125
  this.#logger.verbose(`Uploading file "${id}" (${formatBytes(fileSize)})...`);
131
126
  if (isDefined(this.#storage)) {
132
127
  const bucket = await this.getBucket();
@@ -1,2 +1,3 @@
1
1
  export * from './module.js';
2
+ export * as schemas from './schemas.js';
2
3
  export * from './services/index.js';
@@ -1,2 +1,3 @@
1
1
  export * from './module.js';
2
+ export * as schemas from './schemas.js';
2
3
  export * from './services/index.js';
@@ -13,5 +13,6 @@ export declare class TemporaryFile implements AsyncDisposable {
13
13
  readStream(): ReadableStream<Uint8Array>;
14
14
  write(content: string | Uint8Array | ReadableStream<Uint8Array>): Promise<void>;
15
15
  delete(): Promise<void>;
16
+ size(): Promise<number>;
16
17
  [Symbol.asyncDispose](): Promise<void>;
17
18
  }
@@ -1,5 +1,5 @@
1
1
  import { createReadStream } from 'node:fs';
2
- import { readFile, unlink, writeFile } from 'node:fs/promises';
2
+ import { readFile, stat, unlink, writeFile } from 'node:fs/promises';
3
3
  import { tmpdir } from 'node:os';
4
4
  import { Readable } from 'node:stream';
5
5
  export class TemporaryFile {
@@ -40,6 +40,10 @@ export class TemporaryFile {
40
40
  async delete() {
41
41
  await unlink(this.#path);
42
42
  }
43
+ async size() {
44
+ const result = await stat(this.#path);
45
+ return result.size;
46
+ }
43
47
  async [Symbol.asyncDispose]() {
44
48
  try {
45
49
  await this.delete();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.92.87",
3
+ "version": "0.92.88",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"