@travetto/runtime 5.0.0-rc.11 → 5.0.0-rc.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/runtime",
3
- "version": "5.0.0-rc.11",
3
+ "version": "5.0.0-rc.13",
4
4
  "description": "Runtime for travetto applications.",
5
5
  "keywords": [
6
6
  "console-manager",
package/src/binary.ts CHANGED
@@ -9,6 +9,9 @@ import { text as toText, arrayBuffer as toBuffer } from 'node:stream/consumers';
9
9
 
10
10
  import { BinaryInput, BlobMeta } from './types';
11
11
  import { AppError } from './error';
12
+ import { Util } from './util';
13
+
14
+ const BlobMetaⲐ = Symbol.for('@travetto/runtime:blob-meta');
12
15
 
13
16
  /**
14
17
  * Common functions for dealing with binary data/streams
@@ -62,7 +65,9 @@ export class BinaryUtil {
62
65
  /**
63
66
  * Make a blob, and assign metadata
64
67
  */
65
- static readableBlob(input: () => (Readable | Promise<Readable>), metadata: BlobMeta): Blob {
68
+ static readableBlob(input: () => (Readable | Promise<Readable>), metadata: Omit<BlobMeta, 'filename'> & { filename: string }): File;
69
+ static readableBlob(input: () => (Readable | Promise<Readable>), metadata?: BlobMeta): Blob;
70
+ static readableBlob(input: () => (Readable | Promise<Readable>), metadata: BlobMeta = {}): Blob | File {
66
71
  const go = (): Readable => {
67
72
  const stream = new PassThrough();
68
73
  Promise.resolve(input()).then(v => v.pipe(stream), (err) => stream.destroy(err));
@@ -74,18 +79,22 @@ export class BinaryUtil {
74
79
  new File([], path.basename(metadata.filename), { type: metadata.contentType }) :
75
80
  new Blob([], { type: metadata.contentType });
76
81
 
77
- Object.defineProperties(out, {
82
+ return Object.defineProperties(out, {
78
83
  size: { value: size },
79
84
  stream: { value: () => ReadableStream.from(go()) },
80
85
  arrayBuffer: { value: () => toBuffer(go()) },
81
86
  text: { value: () => toText(go()) },
82
87
  bytes: { value: () => toBuffer(go()).then(v => new Uint8Array(v)) },
88
+ [BlobMetaⲐ]: { value: metadata }
83
89
  });
90
+ }
84
91
 
85
- // @ts-expect-error
86
- out.meta = metadata;
87
-
88
- return out;
92
+ /**
93
+ * Get blob metadata
94
+ */
95
+ static getBlobMeta(blob: Blob): BlobMeta | undefined {
96
+ const withMeta: Blob & { [BlobMetaⲐ]?: BlobMeta } = blob;
97
+ return withMeta[BlobMetaⲐ];
89
98
  }
90
99
 
91
100
  /**
@@ -105,4 +114,19 @@ export class BinaryUtil {
105
114
  },
106
115
  });
107
116
  }
117
+
118
+ /**
119
+ * Get a hashed location/path for a blob
120
+ */
121
+ static hashedBlobLocation(meta: BlobMeta): string {
122
+ const hash = meta.hash ?? Util.uuid();
123
+
124
+ let parts = hash.match(/(.{1,4})/g)!.slice();
125
+ if (parts.length > 4) {
126
+ parts = [...parts.slice(0, 4), parts.slice(4).join('')];
127
+ }
128
+
129
+ const ext = path.extname(meta.filename ?? '') || '.bin';
130
+ return `${parts.join('/')}${ext}`;
131
+ }
108
132
  }
package/src/global.d.ts CHANGED
@@ -1,20 +1,12 @@
1
- import { BlobMeta } from './types';
1
+ import './types';
2
2
 
3
- // https://github.com/microsoft/TypeScript/issues/59012
4
3
  declare const write: unique symbol;
5
4
  declare global {
5
+ // https://github.com/microsoft/TypeScript/issues/59012
6
6
  interface WritableStreamDefaultWriter<W = any> {
7
7
  [write]?: (a: W) => void;
8
8
  }
9
9
  interface Function {
10
10
  Ⲑid: string;
11
11
  }
12
-
13
- interface Blob {
14
- readonly meta?: Readonly<BlobMeta>;
15
- }
16
-
17
- interface File {
18
- readonly meta?: Readonly<BlobMeta>;
19
- }
20
12
  }