@tstdl/base 0.90.65 → 0.90.67

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/api/types.d.ts CHANGED
@@ -44,7 +44,7 @@ export type ApiEndpointDefinition = {
44
44
  /** Root resource path. Overwrites default from {@link ApiDefinition}. */
45
45
  rootResource?: string | null;
46
46
  /**
47
- * sub resource in api
47
+ * Sub resource in api
48
48
  *
49
49
  * results in
50
50
  * ```ts
@@ -89,7 +89,7 @@ export type ApiEndpoint<T extends ApiDefinition, K extends ApiEndpointKeys<T>> =
89
89
  export type ApiEndpointParametersSchema<T extends ApiDefinition, K extends ApiEndpointKeys<T>> = NonUndefinable<ApiEndpoint<T, K>['parameters']>;
90
90
  export type ApiEndpointBodySchema<T extends ApiDefinition, K extends ApiEndpointKeys<T>> = NonUndefinable<ApiEndpoint<T, K>['body']>;
91
91
  export type ApiEndpointResultSchema<T extends ApiDefinition, K extends ApiEndpointKeys<T>> = NonUndefinable<ApiEndpoint<T, K>['result']>;
92
- export type ApiBinaryType = typeof Uint8Array | typeof Blob | typeof ReadableStream;
92
+ export type ApiBinaryType = typeof Uint8Array | typeof Blob | typeof ReadableStream<any>;
93
93
  export type ApiInputType<T extends SchemaTestable> = T extends ApiBinaryType ? (Uint8Array | Blob | ReadableStream<Uint8Array>) : T extends typeof ServerSentEvents ? ServerSentEventsSource : T extends SchemaTestable ? SchemaOutput<T> : never;
94
94
  export type ApiOutputType<T extends SchemaTestable> = T extends typeof ReadableStream ? ReadableStream<Uint8Array> : T extends SchemaTestable ? SchemaOutput<T> : never;
95
95
  export type ApiParameters<T extends ApiDefinition, K extends ApiEndpointKeys<T>> = ApiInputType<ApiEndpointParametersSchema<T, K>>;
@@ -0,0 +1 @@
1
+ export * from './mime-type.js';
package/file/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './mime-type.js';
@@ -0,0 +1,2 @@
1
+ export declare function getMimeType(file: Uint8Array | ReadableStream<Uint8Array>): Promise<string>;
2
+ export declare function getMimeTypeExtension(file: Uint8Array | ReadableStream<Uint8Array>): Promise<string | null>;
@@ -0,0 +1,36 @@
1
+ import { dynamicImport } from '../import.js';
2
+ import { decodeTextStream } from '../utils/encoding.js';
3
+ import { readTextStream } from '../utils/stream/stream-reader.js';
4
+ import { isUint8Array } from '../utils/type-guards.js';
5
+ export async function getMimeType(file) {
6
+ return spawnFileCommand(['--brief', '--mime-type', '-'], file);
7
+ }
8
+ export async function getMimeTypeExtension(file) {
9
+ const extensions = await spawnFileCommand(['--brief', '--extension', '-'], file);
10
+ const indexOfSlash = extensions.indexOf('/');
11
+ const extension = (indexOfSlash > 0) ? extensions.slice(0, indexOfSlash) : extensions;
12
+ if (extension == '???') {
13
+ return null;
14
+ }
15
+ return extension;
16
+ }
17
+ async function spawnFileCommand(args, file) {
18
+ const { spawn } = await dynamicImport('node:child_process');
19
+ const { Readable, Writable } = await dynamicImport('node:stream');
20
+ const process = spawn('file', args, { stdio: 'pipe' });
21
+ const stdin = Writable.toWeb(process.stdin);
22
+ const stdout = Readable.toWeb(process.stdout).pipeThrough(decodeTextStream());
23
+ if (isUint8Array(file)) {
24
+ const writer = stdin.getWriter();
25
+ try {
26
+ await writer.write(file);
27
+ await writer.close();
28
+ }
29
+ catch { /* File command closes stream as soon as it has the required data */ }
30
+ }
31
+ else {
32
+ await file.pipeTo(stdin);
33
+ }
34
+ const output = await readTextStream(stdout);
35
+ return output.trim();
36
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.90.65",
3
+ "version": "0.90.67",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -43,6 +43,7 @@
43
43
  "./distributed-loop": "./distributed-loop/index.js",
44
44
  "./enumerable": "./enumerable/index.js",
45
45
  "./errors": "./errors/index.js",
46
+ "./file": "./file/index.js",
46
47
  "./function": "./function/index.js",
47
48
  "./http": "./http/index.js",
48
49
  "./http/node": "./http/server/node/index.js",
@@ -1,4 +1,4 @@
1
- import type { Observable } from 'rxjs';
1
+ import { type Observable } from 'rxjs';
2
2
  export declare enum ServerSentEventsState {
3
3
  Connecting = 0,
4
4
  Open = 1,
@@ -1,5 +1,5 @@
1
1
  import { NotSupportedError } from '../errors/not-supported.error.js';
2
- import { distinctUntilChanged, filter, fromEvent, map, merge, ReplaySubject, share, shareReplay, startWith, takeUntil } from 'rxjs';
2
+ import { ReplaySubject, distinctUntilChanged, filter, fromEvent, map, merge, share, shareReplay, startWith, takeUntil } from 'rxjs';
3
3
  export var ServerSentEventsState;
4
4
  (function (ServerSentEventsState) {
5
5
  ServerSentEventsState[ServerSentEventsState["Connecting"] = 0] = "Connecting";
@@ -7,3 +7,4 @@ export type ReadBinaryStreamOptions = {
7
7
  onLengthSubceed?: 'error' | 'close' | 'leave-open';
8
8
  };
9
9
  export declare function readBinaryStream(iterableOrStream: AnyIterable<ArrayBufferView> | ReadableStream<ArrayBufferView>, { length, onLengthExceed, onLengthSubceed }?: ReadBinaryStreamOptions): Promise<Uint8Array>;
10
+ export declare function readTextStream(iterableOrStream: AnyIterable<string> | ReadableStream<string>): Promise<string>;
@@ -3,7 +3,7 @@ import { MaxBytesExceededError } from '../../errors/max-bytes-exceeded.error.js'
3
3
  import { NotSupportedError } from '../../errors/not-supported.error.js';
4
4
  import { concatArrayBufferViews } from '../binary.js';
5
5
  import { isDefined, isReadableStream } from '../type-guards.js';
6
- import { getReadableStreamFromIterable } from './readable-stream-adapter.js';
6
+ import { getReadableStreamFromIterable, getReadableStreamIterable } from './readable-stream-adapter.js';
7
7
  import { toBytesStream } from './to-bytes-stream.js';
8
8
  // eslint-disable-next-line max-statements
9
9
  export async function readBinaryStream(iterableOrStream, { length, onLengthExceed = 'error', onLengthSubceed = 'error' } = {}) {
@@ -76,3 +76,11 @@ export async function readBinaryStream(iterableOrStream, { length, onLengthExcee
76
76
  }
77
77
  return concatArrayBufferViews(views, Uint8Array, totalLength);
78
78
  }
79
+ export async function readTextStream(iterableOrStream) {
80
+ const iterable = isReadableStream(iterableOrStream) ? getReadableStreamIterable(iterableOrStream) : iterableOrStream;
81
+ let text = '';
82
+ for await (const chunk of iterable) {
83
+ text += chunk;
84
+ }
85
+ return text;
86
+ }