@editframe/api 0.12.0-beta.10 → 0.12.0-beta.12

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.
@@ -56,7 +56,7 @@ const createUnprocessedFile = async (client, payload) => {
56
56
  const uploadUnprocessedFile = async (client, uploadDetails, path) => {
57
57
  const { createReadStream } = await import("node:fs");
58
58
  const readStream = createReadStream(path);
59
- const { createReadableStreamFromReadable } = await import("../../../cli/src/utils/createReadableStreamFromReadable.js");
59
+ const { createReadableStreamFromReadable } = await import("../utils/createReadableStreamFromReadable.js");
60
60
  return uploadUnprocessedReadableStream(
61
61
  client,
62
62
  uploadDetails,
@@ -0,0 +1,4 @@
1
+ import { Readable } from 'node:stream';
2
+ export declare const createReadableStreamFromReadable: (source: Readable & {
3
+ readableHighWaterMark?: number;
4
+ }) => ReadableStream<Uint8Array>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@editframe/api",
3
- "version": "0.12.0-beta.10",
3
+ "version": "0.12.0-beta.12",
4
4
  "description": "API functions for EditFrame",
5
5
  "exports": {
6
6
  ".": {
@@ -29,7 +29,7 @@
29
29
  "vite-tsconfig-paths": "^4.3.2"
30
30
  },
31
31
  "dependencies": {
32
- "@editframe/assets": "0.12.0-beta.10",
32
+ "@editframe/assets": "0.12.0-beta.12",
33
33
  "debug": "^4.3.5",
34
34
  "jsonwebtoken": "^9.0.2",
35
35
  "node-fetch": "^3.3.2",
@@ -104,7 +104,7 @@ export const uploadUnprocessedFile = async (
104
104
  const readStream = createReadStream(path);
105
105
 
106
106
  const { createReadableStreamFromReadable } = await import(
107
- "packages/cli/src/utils/createReadableStreamFromReadable.ts"
107
+ "../utils/createReadableStreamFromReadable.ts"
108
108
  );
109
109
 
110
110
  return uploadUnprocessedReadableStream(
@@ -0,0 +1,117 @@
1
+ import { type Readable, Stream } from "node:stream";
2
+
3
+ export const createReadableStreamFromReadable = (
4
+ source: Readable & { readableHighWaterMark?: number },
5
+ ) => {
6
+ const pump = new StreamPump(source);
7
+ const stream = new ReadableStream(pump, pump);
8
+ return stream;
9
+ };
10
+
11
+ class StreamPump {
12
+ public highWaterMark: number;
13
+ public accumalatedSize: number;
14
+ private stream: Stream & {
15
+ readableHighWaterMark?: number;
16
+ readable?: boolean;
17
+ resume?: () => void;
18
+ pause?: () => void;
19
+ destroy?: (error?: Error) => void;
20
+ };
21
+ private controller?: ReadableStreamController<Uint8Array>;
22
+
23
+ constructor(
24
+ stream: Stream & {
25
+ readableHighWaterMark?: number;
26
+ readable?: boolean;
27
+ resume?: () => void;
28
+ pause?: () => void;
29
+ destroy?: (error?: Error) => void;
30
+ },
31
+ ) {
32
+ this.highWaterMark =
33
+ stream.readableHighWaterMark ||
34
+ new Stream.Readable().readableHighWaterMark;
35
+ this.accumalatedSize = 0;
36
+ this.stream = stream;
37
+ this.enqueue = this.enqueue.bind(this);
38
+ this.error = this.error.bind(this);
39
+ this.close = this.close.bind(this);
40
+ }
41
+
42
+ size(chunk: Uint8Array) {
43
+ return chunk?.byteLength || 0;
44
+ }
45
+
46
+ start(controller: ReadableStreamController<Uint8Array>) {
47
+ this.controller = controller;
48
+ this.stream.on("data", this.enqueue);
49
+ this.stream.once("error", this.error);
50
+ this.stream.once("end", this.close);
51
+ this.stream.once("close", this.close);
52
+ }
53
+
54
+ pull() {
55
+ this.resume();
56
+ }
57
+
58
+ cancel(reason?: Error) {
59
+ if (this.stream.destroy) {
60
+ this.stream.destroy(reason);
61
+ }
62
+
63
+ this.stream.off("data", this.enqueue);
64
+ this.stream.off("error", this.error);
65
+ this.stream.off("end", this.close);
66
+ this.stream.off("close", this.close);
67
+ }
68
+
69
+ enqueue(chunk: Uint8Array | string) {
70
+ if (this.controller) {
71
+ try {
72
+ const bytes = chunk instanceof Uint8Array ? chunk : Buffer.from(chunk);
73
+
74
+ const available = (this.controller.desiredSize || 0) - bytes.byteLength;
75
+ this.controller.enqueue(bytes);
76
+ if (available <= 0) {
77
+ this.pause();
78
+ }
79
+ } catch (error: any) {
80
+ this.controller.error(
81
+ new Error(
82
+ "Could not create Buffer, chunk must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object",
83
+ ),
84
+ );
85
+ this.cancel();
86
+ }
87
+ }
88
+ }
89
+
90
+ pause() {
91
+ if (this.stream.pause) {
92
+ this.stream.pause();
93
+ }
94
+ }
95
+
96
+ resume() {
97
+ if (this.stream.readable && this.stream.resume) {
98
+ this.stream.resume();
99
+ }
100
+ }
101
+
102
+ close() {
103
+ if (this.controller) {
104
+ this.controller.close();
105
+ // biome-ignore lint/performance/noDelete: infrequent use
106
+ delete this.controller;
107
+ }
108
+ }
109
+
110
+ error(error: Error) {
111
+ if (this.controller) {
112
+ this.controller.error(error);
113
+ // biome-ignore lint/performance/noDelete: infrequent use
114
+ delete this.controller;
115
+ }
116
+ }
117
+ }
File without changes
File without changes