rac-delta 1.0.14 → 1.0.16

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.
@@ -1 +1 @@
1
- {"version":3,"file":"memory-reconstruction-service.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/services/memory-reconstruction-service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAyB,MAAM,QAAQ,CAAC;AAIzD,OAAO,EACL,WAAW,EAEX,aAAa,EACb,qBAAqB,EACrB,qBAAqB,EACtB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,SAAS,EAAE,SAAS,EAAS,MAAM,mBAAmB,CAAC;AAEhE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,qBAAa,2BAA4B,YAAW,qBAAqB;IAC3D,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,aAAa;IAE5C,cAAc,CAClB,IAAI,EAAE,SAAS,EACf,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,WAAW,EACxB,OAAO,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GACvC,OAAO,CAAC,IAAI,CAAC;IAmFV,eAAe,CACnB,KAAK,EAAE,SAAS,EAChB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,WAAW,EACxB,OAAO,GAAE,qBAIR,GACA,OAAO,CAAC,IAAI,CAAC;IAgDV,mBAAmB,CACvB,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,WAAW,EACxB,cAAc,GAAE,MAAU,GACzB,OAAO,CAAC,QAAQ,CAAC;YAmCN,UAAU;IASxB,oEAAoE;YACtD,kBAAkB;IAiChC,oFAAoF;YACtE,iBAAiB;IAmF/B;;;OAGG;YACW,qBAAqB;YA2BrB,YAAY;YAcZ,aAAa;YAuDZ,gBAAgB;CAmChC"}
1
+ {"version":3,"file":"memory-reconstruction-service.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/services/memory-reconstruction-service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAyB,MAAM,QAAQ,CAAC;AAIzD,OAAO,EACL,WAAW,EAEX,aAAa,EACb,qBAAqB,EACrB,qBAAqB,EACtB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,SAAS,EAAE,SAAS,EAAS,MAAM,mBAAmB,CAAC;AAEhE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,qBAAa,2BAA4B,YAAW,qBAAqB;IAC3D,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,aAAa;IAE5C,cAAc,CAClB,IAAI,EAAE,SAAS,EACf,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,WAAW,EACxB,OAAO,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GACvC,OAAO,CAAC,IAAI,CAAC;IAmFV,eAAe,CACnB,KAAK,EAAE,SAAS,EAChB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,WAAW,EACxB,OAAO,GAAE,qBAIR,GACA,OAAO,CAAC,IAAI,CAAC;IAgDV,mBAAmB,CACvB,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,WAAW,EACxB,cAAc,SAAI,GACjB,OAAO,CAAC,QAAQ,CAAC;YA6CN,UAAU;IASxB,oEAAoE;YACtD,kBAAkB;IAiChC,oFAAoF;YACtE,iBAAiB;IAmF/B;;;OAGG;YACW,qBAAqB;YA2BrB,YAAY;YAcZ,aAAa;YAyCZ,gBAAgB;CAmChC"}
@@ -112,31 +112,44 @@ class MemoryReconstructionService {
112
112
  }
113
113
  // This will reconstruct to stream, not to disk
114
114
  async reconstructToStream(entry, chunkSource, maxConcurrency = 5) {
115
- const output = new stream_1.PassThrough({ highWaterMark: 1024 * 1024 });
116
115
  const chunks = entry.chunks ?? [];
117
- // Starts async reconstruction without blocking stream return
118
- const processChunks = async () => {
116
+ const pass = new stream_1.PassThrough({ highWaterMark: 2 * 1024 * 1024 });
117
+ const iterator = this.fetchChunksSmart(chunks, chunkSource, true);
118
+ (async () => {
119
+ const active = new Set();
120
+ const pipeChunk = async (data) => {
121
+ if (Buffer.isBuffer(data)) {
122
+ if (!pass.write(data)) {
123
+ await new Promise((resolve) => pass.once('drain', resolve));
124
+ }
125
+ }
126
+ else {
127
+ await new Promise((resolve, reject) => {
128
+ const onError = (err) => reject(err);
129
+ data.once('error', onError);
130
+ pass.once('error', onError);
131
+ data.once('end', resolve);
132
+ data.pipe(pass, { end: false });
133
+ });
134
+ }
135
+ };
119
136
  try {
120
- const active = new Set();
121
- for await (const { data } of this.fetchChunksSmart(chunks, chunkSource)) {
122
- const task = (async () => {
123
- await this.writeToStream(data, output);
124
- })();
125
- task.finally(() => active.delete(task));
137
+ for await (const { data } of iterator) {
138
+ const task = pipeChunk(data);
126
139
  active.add(task);
140
+ task.finally(() => active.delete(task));
127
141
  if (active.size >= maxConcurrency) {
128
142
  await Promise.race(active);
129
143
  }
130
144
  }
131
145
  await Promise.all(active);
132
- output.end();
146
+ pass.end();
133
147
  }
134
148
  catch (err) {
135
- output.destroy(err instanceof Error ? err : new Error(String(err)));
149
+ pass.destroy(err instanceof Error ? err : new Error(String(err)));
136
150
  }
137
- };
138
- void processChunks();
139
- return output;
151
+ })();
152
+ return pass;
140
153
  }
141
154
  async fileExists(path) {
142
155
  try {
@@ -272,41 +285,28 @@ class MemoryReconstructionService {
272
285
  return;
273
286
  }
274
287
  if (Buffer.isBuffer(data)) {
275
- const canContinue = stream.write(data);
276
- if (!canContinue) {
288
+ if (!stream.write(data)) {
277
289
  await new Promise((resolve) => stream.once('drain', resolve));
278
290
  }
279
291
  onFinish?.(data.length);
280
292
  return;
281
293
  }
282
294
  let totalBytes = 0;
283
- await new Promise((resolve, reject) => {
284
- const onError = (err) => {
285
- cleanup();
286
- reject(err instanceof Error ? err : new Error(String(err)));
287
- };
288
- const onEnd = () => {
289
- cleanup();
290
- onFinish?.(totalBytes);
291
- resolve();
292
- };
293
- const cleanup = () => {
294
- data.off('error', onError);
295
- data.off('end', onEnd);
296
- stream.off('error', onError);
297
- };
298
- data.on('error', onError);
299
- stream.on('error', onError);
300
- data.on('end', onEnd);
301
- data.on('data', (chunk) => {
295
+ await (0, promises_2.pipeline)(data, new stream_1.Writable({
296
+ write(chunk, _encoding, callBack) {
302
297
  totalBytes += chunk.length;
303
- const canContinue = stream.write(chunk);
304
- if (!canContinue) {
305
- data.pause();
306
- stream.once('drain', () => data.resume());
298
+ if (!stream.write(chunk)) {
299
+ stream.once('drain', callBack);
307
300
  }
308
- });
309
- });
301
+ else {
302
+ callBack();
303
+ }
304
+ },
305
+ final(callBack) {
306
+ callBack();
307
+ },
308
+ }));
309
+ onFinish?.(totalBytes);
310
310
  }
311
311
  async *fetchChunksSmart(chunks, chunkSource, preserveOrder = true) {
312
312
  const hashes = chunks.map((c) => c.hash);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rac-delta",
3
3
  "description": "Storage agnostic delta patching implementation of rac-delta protocol for NodeJs. With streaming support and file reconstruction.",
4
- "version": "1.0.14",
4
+ "version": "1.0.16",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "repository": {