@cobol-ts/stream-scope 0.0.0-stage → 0.0.40
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/README.md +12 -3
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/src/named.input.d.ts +16 -0
- package/dist/src/named.input.js +68 -0
- package/dist/src/named.input.js.map +1 -0
- package/dist/src/named.input.spec.d.ts +1 -0
- package/dist/src/named.input.spec.js +50 -0
- package/dist/src/named.input.spec.js.map +1 -0
- package/dist/src/relative.path.d.ts +1 -0
- package/dist/src/relative.path.js +16 -0
- package/dist/src/relative.path.js.map +1 -0
- package/dist/src/relative.path.spec.d.ts +1 -0
- package/dist/src/relative.path.spec.js +12 -0
- package/dist/src/relative.path.spec.js.map +1 -0
- package/dist/src/resources.d.ts +8 -0
- package/dist/src/resources.js +104 -0
- package/dist/src/resources.js.map +1 -0
- package/dist/src/resources.spec.d.ts +1 -0
- package/dist/src/resources.spec.js +99 -0
- package/dist/src/resources.spec.js.map +1 -0
- package/index.ts +3 -0
- package/package.json +58 -3
package/README.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @cobol-ts/stream-scope
|
|
2
|
+
|
|
3
|
+
Small, Node-specific resource scopes, independent of ZIP, cryptography and the repository.
|
|
4
|
+
|
|
5
|
+
- `withResource(open, close, use)` awaits acquisition, work and cleanup. When both work and cleanup fail, the error retains both exceptions.
|
|
6
|
+
- `withReadable` and `withWritable` share the same resource scope. Both wait for actual stream closure, including errors reported during destruction. Supply ordinary Node streams with `emitClose: true`; another completion contract needs its own `withResource` scope.
|
|
7
|
+
- `NamedInput.withStream(use)` opens a fresh stream lazily. Success requires consumption to EOF. `namedInput` and `namedFileInput` implement this contract.
|
|
8
|
+
- `NamedInputs.withFiles(use)` scopes the inventory, including any resource that owns its entries (such as an open ZIP). `namedInputs` wraps an inventory that has no separate resource.
|
|
9
|
+
- `observedInput` uses an ordinary Node pipeline to observe the bytes in the actual owned source. On failure the pipeline is stopped with the original error and awaited before the scope exits.
|
|
10
|
+
- `safeRelativePath` rejects unsafe or non-canonical object and archive member paths without silently rewriting them.
|
|
11
|
+
|
|
12
|
+
The contract is start, complete or fail, await cleanup, return. There is no public cancellation or pause/resume API. Do not let a live stream, lazy resource-owning iterator or unfinished work escape a callback. A scope owns only resources it acquires; a caller owns any borrowed resource. The tests delay closure using controlled promises, not sleeps or retries.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./src/resources"), exports);
|
|
18
|
+
__exportStar(require("./src/named.input"), exports);
|
|
19
|
+
__exportStar(require("./src/relative.path"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAA+B;AAC/B,oDAAiC;AACjC,sDAAmC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Readable } from 'node:stream';
|
|
2
|
+
export interface NamedInput {
|
|
3
|
+
readonly path: string;
|
|
4
|
+
withStream<T>(use: (stream: Readable) => Promise<T>): Promise<T>;
|
|
5
|
+
}
|
|
6
|
+
/** The file inventory itself can own resources, e.g. a ZIP archive. */
|
|
7
|
+
export interface NamedInputs {
|
|
8
|
+
withFiles<T>(use: (files: AsyncIterable<NamedInput>) => Promise<T>): Promise<T>;
|
|
9
|
+
}
|
|
10
|
+
/** An inventory of ordinary lazy files has no resource of its own. */
|
|
11
|
+
export declare function namedInputs(files: AsyncIterable<NamedInput>): NamedInputs;
|
|
12
|
+
export declare function namedInput(path: string, open: () => Readable | PromiseLike<Readable>): NamedInput;
|
|
13
|
+
export declare function namedFileInput(path: string, filename: string): NamedInput;
|
|
14
|
+
/** Workbench preview: scope the real input as well as the observation stream.
|
|
15
|
+
* The one input pipeline must finish before the callback scope can finish. */
|
|
16
|
+
export declare function observedInput(path: string, open: () => Readable | PromiseLike<Readable>, observe: (chunk: Buffer) => void): NamedInput;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.namedInputs = namedInputs;
|
|
4
|
+
exports.namedInput = namedInput;
|
|
5
|
+
exports.namedFileInput = namedFileInput;
|
|
6
|
+
exports.observedInput = observedInput;
|
|
7
|
+
const node_fs_1 = require("node:fs");
|
|
8
|
+
const node_stream_1 = require("node:stream");
|
|
9
|
+
const promises_1 = require("node:stream/promises");
|
|
10
|
+
const resources_1 = require("./resources");
|
|
11
|
+
/** An inventory of ordinary lazy files has no resource of its own. */
|
|
12
|
+
function namedInputs(files) {
|
|
13
|
+
return { withFiles: use => use(files) };
|
|
14
|
+
}
|
|
15
|
+
function namedInput(path, open) {
|
|
16
|
+
return { path, withStream: use => (0, resources_1.withReadable)(open, async (stream) => {
|
|
17
|
+
const result = await use(stream);
|
|
18
|
+
if (!stream.readableEnded)
|
|
19
|
+
throw new Error(`Input was not consumed completely: ${path}`);
|
|
20
|
+
return result;
|
|
21
|
+
}) };
|
|
22
|
+
}
|
|
23
|
+
function namedFileInput(path, filename) {
|
|
24
|
+
return namedInput(path, () => (0, node_fs_1.createReadStream)(filename));
|
|
25
|
+
}
|
|
26
|
+
/** Workbench preview: scope the real input as well as the observation stream.
|
|
27
|
+
* The one input pipeline must finish before the callback scope can finish. */
|
|
28
|
+
function observedInput(path, open, observe) {
|
|
29
|
+
return {
|
|
30
|
+
path, withStream: use => (0, resources_1.withReadable)(open, source => (0, resources_1.withReadable)(() => new node_stream_1.Transform({
|
|
31
|
+
transform(chunk, _encoding, callback) {
|
|
32
|
+
try {
|
|
33
|
+
observe(chunk);
|
|
34
|
+
callback(null, chunk);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
callback(error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}), async (observed) => {
|
|
41
|
+
const done = (0, promises_1.pipeline)(source, observed);
|
|
42
|
+
// The consumer can fail before it reads the first byte.
|
|
43
|
+
void done.catch(() => { });
|
|
44
|
+
try {
|
|
45
|
+
const result = await use(observed);
|
|
46
|
+
if (!observed.readableEnded)
|
|
47
|
+
throw new Error(`Input was not consumed completely: ${path}`);
|
|
48
|
+
await done;
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
const failure = error instanceof Error ? error : new Error(String(error), { cause: error });
|
|
53
|
+
// Forward the real failure through the pipeline, not a synthetic premature close.
|
|
54
|
+
source.destroy(failure);
|
|
55
|
+
observed.destroy(failure);
|
|
56
|
+
try {
|
|
57
|
+
await done;
|
|
58
|
+
}
|
|
59
|
+
catch (other) {
|
|
60
|
+
if (other !== failure)
|
|
61
|
+
throw (0, resources_1.combinedFailure)(error, [other], 'Observed input failed');
|
|
62
|
+
}
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}))
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=named.input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"named.input.js","sourceRoot":"","sources":["../../src/named.input.ts"],"names":[],"mappings":";;AAgBA,kCAEC;AAED,gCAMC;AAED,wCAEC;AAID,sCA6BC;AA/DD,qCAAwC;AACxC,6CAAoD;AACpD,mDAA6C;AAC7C,2CAAyD;AAYzD,sEAAsE;AACtE,SAAgB,WAAW,CAAC,KAAgC;IACxD,OAAO,EAAC,SAAS,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,EAAC,CAAA;AACzC,CAAC;AAED,SAAgB,UAAU,CAAC,IAAY,EAAE,IAA4C;IACjF,OAAO,EAAC,IAAI,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,CAAC,IAAA,wBAAY,EAAC,IAAI,EAAE,KAAK,EAAC,MAAM,EAAC,EAAE;YAC/D,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,CAAA;YAChC,IAAI,CAAC,MAAM,CAAC,aAAa;gBAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,IAAI,EAAE,CAAC,CAAA;YACxF,OAAO,MAAM,CAAA;QACjB,CAAC,CAAC,EAAC,CAAA;AACP,CAAC;AAED,SAAgB,cAAc,CAAC,IAAY,EAAE,QAAgB;IACzD,OAAO,UAAU,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,IAAA,0BAAgB,EAAC,QAAQ,CAAC,CAAC,CAAA;AAC7D,CAAC;AAED;8EAC8E;AAC9E,SAAgB,aAAa,CAAC,IAAY,EAAE,IAA4C,EAC1D,OAAgC;IAC1D,OAAO;QACH,IAAI,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,CAAC,IAAA,wBAAY,EAAC,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC,IAA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2BAAe,EAAC,KAAK,EAAE,CAAC,KAAK,CAAC,EAAE,uBAAuB,CAAC,CAAA;gBACzF,CAAC;gBACD,MAAM,KAAK,CAAA;YACf,CAAC;QACL,CAAC,CAAC,CAAC;KACN,CAAA;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_stream_1 = require("node:stream");
|
|
4
|
+
const named_input_1 = require("./named.input");
|
|
5
|
+
describe('observed named input', () => {
|
|
6
|
+
it('observes exactly the consumed bytes and closes the actual source', async () => {
|
|
7
|
+
const bytes = Buffer.from([1, 2, 255]);
|
|
8
|
+
const source = node_stream_1.Readable.from([bytes], { objectMode: false });
|
|
9
|
+
const seen = [];
|
|
10
|
+
const input = (0, named_input_1.observedInput)('data', () => source, chunk => seen.push(Buffer.from(chunk)));
|
|
11
|
+
const actual = await input.withStream(async (stream) => {
|
|
12
|
+
const chunks = [];
|
|
13
|
+
for await (const chunk of stream)
|
|
14
|
+
chunks.push(Buffer.from(chunk));
|
|
15
|
+
return Buffer.concat(chunks);
|
|
16
|
+
});
|
|
17
|
+
expect(actual).toEqual(bytes);
|
|
18
|
+
expect(Buffer.concat(seen)).toEqual(bytes);
|
|
19
|
+
expect(source.closed).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
it('cancels the actual source when its consumer rejects during a pending read', async () => {
|
|
22
|
+
let started;
|
|
23
|
+
const reading = new Promise(resolve => { started = resolve; });
|
|
24
|
+
const source = new node_stream_1.Readable({ read() { started(); } });
|
|
25
|
+
const failure = new Error('consumer failure');
|
|
26
|
+
await expect((0, named_input_1.observedInput)('data', () => source, () => { }).withStream(async () => {
|
|
27
|
+
await reading;
|
|
28
|
+
throw failure;
|
|
29
|
+
})).rejects.toThrow(failure);
|
|
30
|
+
expect(source.closed).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
it('propagates observation failures and closes the actual source', async () => {
|
|
33
|
+
const source = node_stream_1.Readable.from([Buffer.from('data')], { objectMode: false });
|
|
34
|
+
const failure = new Error('observation failure');
|
|
35
|
+
await expect((0, named_input_1.observedInput)('data', () => source, () => { throw failure; }).withStream(async (stream) => {
|
|
36
|
+
for await (const _chunk of stream) { /* drain */ }
|
|
37
|
+
})).rejects.toThrow(failure);
|
|
38
|
+
expect(source.closed).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
it('opens an input only inside its scope and rejects incomplete successful work', async () => {
|
|
42
|
+
const source = new node_stream_1.Readable({ read() { } });
|
|
43
|
+
const open = jest.fn(() => source);
|
|
44
|
+
const input = (0, named_input_1.namedInput)('data', open);
|
|
45
|
+
expect(open).not.toHaveBeenCalled();
|
|
46
|
+
await expect(input.withStream(async () => { })).rejects.toThrow('Input was not consumed completely: data');
|
|
47
|
+
expect(open).toHaveBeenCalledTimes(1);
|
|
48
|
+
expect(source.closed).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
//# sourceMappingURL=named.input.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"named.input.spec.js","sourceRoot":"","sources":["../../src/named.input.spec.ts"],"names":[],"mappings":";;AAAA,6CAAoC;AACpC,+CAAuD;AAEvD,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IAClC,EAAE,CAAC,kEAAkE,EAAE,KAAK,IAAI,EAAE;QAC9E,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAA;QACtC,MAAM,MAAM,GAAG,sBAAQ,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAC,UAAU,EAAE,KAAK,EAAC,CAAC,CAAA;QAC1D,MAAM,IAAI,GAAa,EAAE,CAAA;QACzB,MAAM,KAAK,GAAG,IAAA,2BAAa,EAAC,MAAM,EAAE,GAAG,EAAE,C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,CAAC,CAAA;IACtC,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAA;IACnC,MAAM,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,yCAAyC,CAAC,CAAA;IACzG,MAAM,CAAC,IAAI,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAA;IACrC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACpC,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function safeRelativePath(path: string): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.safeRelativePath = safeRelativePath;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
function safeRelativePath(path) {
|
|
6
|
+
if (!path || path.includes('\\') || path.includes('\0') || path.startsWith('/') || /^[a-zA-Z]:/.test(path))
|
|
7
|
+
throw new Error(`Unsafe relative path: ${path}`);
|
|
8
|
+
const segments = path.split('/');
|
|
9
|
+
if (segments.some(part => part === '..' || part === '.' || !part))
|
|
10
|
+
throw new Error(`Unsafe relative path: ${path}`);
|
|
11
|
+
const result = node_path_1.posix.normalize(path);
|
|
12
|
+
if (result !== path)
|
|
13
|
+
throw new Error(`Non-canonical relative path: ${path}`);
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=relative.path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relative.path.js","sourceRoot":"","sources":["../../src/relative.path.ts"],"names":[],"mappings":";;AAEA,4CASC;AAXD,yCAA+B;AAE/B,SAAgB,gBAAgB,CAAC,IAAY;IACzC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;QACtG,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,EAAE,CAAC,CAAA;IACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAChC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QAC7D,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,EAAE,CAAC,CAAA;IACpD,MAAM,MAAM,GAAG,iBAAK,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;IACpC,IAAI,MAAM,KAAK,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,IAAI,EAAE,CAAC,CAAA;IAC5E,OAAO,MAAM,CAAA;AACjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const relative_path_1 = require("./relative.path");
|
|
4
|
+
describe('safe relative paths', () => {
|
|
5
|
+
it('rejects unsafe object/ZIP member paths without normalising them', () => {
|
|
6
|
+
for (const path of ['/etc/secret', '../secret', 'a/../secret', 'a//b', 'a\\b', 'C:/secret']) {
|
|
7
|
+
expect(() => (0, relative_path_1.safeRelativePath)(path)).toThrow();
|
|
8
|
+
}
|
|
9
|
+
expect((0, relative_path_1.safeRelativePath)('nested/customer.csv')).toEqual('nested/customer.csv');
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=relative.path.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relative.path.spec.js","sourceRoot":"","sources":["../../src/relative.path.spec.ts"],"names":[],"mappings":";;AAAA,mDAAgD;AAEhD,QAAQ,CAAC,qBAAqB,EAAE,GAAG,EAAE;IACjC,EAAE,CAAC,iEAAiE,EAAE,GAAG,EAAE;QACvE,KAAK,MAAM,IAAI,IAAI,CAAC,aAAa,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC;YAC1F,MAAM,CAAC,GAAG,EAAE,CAAC,IAAA,gCAAgB,EAAC,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,CAAA;QAClD,CAAC;QACD,MAAM,CAAC,IAAA,gCAAgB,EAAC,qBAAqB,CAAC,CAAC,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAA;IAClF,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Readable, Writable } from 'node:stream';
|
|
2
|
+
/** Keep the original exception as the cause and avoid reporting it twice. */
|
|
3
|
+
export declare function includesFailure(error: unknown, candidate: unknown): boolean;
|
|
4
|
+
export declare function combinedFailure(primary: unknown, secondary: readonly unknown[], context: string): unknown;
|
|
5
|
+
/** Preserve the operation error even if releasing the resource also fails. */
|
|
6
|
+
export declare function withResource<R, T>(open: () => R | PromiseLike<R>, close: (resource: R) => void | Promise<void>, use: (resource: R) => Promise<T>): Promise<T>;
|
|
7
|
+
export declare function withReadable<R extends Readable, T>(open: () => R | PromiseLike<R>, use: (stream: R) => Promise<T>): Promise<T>;
|
|
8
|
+
export declare function withWritable<R extends Writable, T>(open: () => R | PromiseLike<R>, use: (stream: R) => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.includesFailure = includesFailure;
|
|
4
|
+
exports.combinedFailure = combinedFailure;
|
|
5
|
+
exports.withResource = withResource;
|
|
6
|
+
exports.withReadable = withReadable;
|
|
7
|
+
exports.withWritable = withWritable;
|
|
8
|
+
const node_stream_1 = require("node:stream");
|
|
9
|
+
/** Keep the original exception as the cause and avoid reporting it twice. */
|
|
10
|
+
function includesFailure(error, candidate) {
|
|
11
|
+
return error === candidate || (error instanceof AggregateError &&
|
|
12
|
+
error.errors.some(item => includesFailure(item, candidate)));
|
|
13
|
+
}
|
|
14
|
+
function combinedFailure(primary, secondary, context) {
|
|
15
|
+
const errors = [primary];
|
|
16
|
+
for (const error of secondary)
|
|
17
|
+
if (!errors.some(previous => includesFailure(previous, error)))
|
|
18
|
+
errors.push(error);
|
|
19
|
+
return errors.length === 1 ? primary : new AggregateError(errors, `${context}: ${errors.map(error => error instanceof Error ? error.message : String(error)).join('; ')}`, { cause: primary });
|
|
20
|
+
}
|
|
21
|
+
async function release(resource, close, failure) {
|
|
22
|
+
try {
|
|
23
|
+
await close(resource);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (failure)
|
|
27
|
+
throw combinedFailure(failure.error, [error], 'Resource operation and cleanup failed');
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Preserve the operation error even if releasing the resource also fails. */
|
|
32
|
+
async function withResource(open, close, use) {
|
|
33
|
+
const resource = await open();
|
|
34
|
+
let failure;
|
|
35
|
+
try {
|
|
36
|
+
return await use(resource);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
failure = { error };
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
await release(resource, close, failure);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Node sets `closed` just before emitting close. A pending close/error event
|
|
47
|
+
* must still be observed. Keep that Node-specific detail in this one place. */
|
|
48
|
+
function closeState(stream) {
|
|
49
|
+
const node = stream;
|
|
50
|
+
return node._readableState ?? node._writableState;
|
|
51
|
+
}
|
|
52
|
+
/** Own an ordinary Node stream until its close event. Attach observers before
|
|
53
|
+
* calling use (and synchronously for synchronous acquisition). The scope is
|
|
54
|
+
* for complete awaited work, not a facility for returning a live resource. */
|
|
55
|
+
async function withStream(open, use) {
|
|
56
|
+
return withResource(async () => {
|
|
57
|
+
const opened = open();
|
|
58
|
+
const stream = opened instanceof node_stream_1.Readable || opened instanceof node_stream_1.Writable ? opened : await opened;
|
|
59
|
+
const failures = stream.errored ? [stream.errored] : [];
|
|
60
|
+
const onError = (error) => { if (!failures.includes(error))
|
|
61
|
+
failures.push(error); };
|
|
62
|
+
stream.on('error', onError);
|
|
63
|
+
const state = closeState(stream);
|
|
64
|
+
if (state?.emitClose === false) {
|
|
65
|
+
stream.destroy();
|
|
66
|
+
throw new Error('Stream scope requires emitClose: true; use an explicit resource completion contract');
|
|
67
|
+
}
|
|
68
|
+
const closed = stream.closed && state?.closeEmitted !== false ? Promise.resolve() :
|
|
69
|
+
new Promise(resolve => stream.once('close', resolve));
|
|
70
|
+
return { stream, closed, onError, failures, reported: undefined };
|
|
71
|
+
}, async (resource) => {
|
|
72
|
+
const { stream, closed, onError, failures } = resource;
|
|
73
|
+
try {
|
|
74
|
+
stream.destroy();
|
|
75
|
+
await closed;
|
|
76
|
+
const additional = failures.filter(error => !resource.reported || !includesFailure(resource.reported.error, error));
|
|
77
|
+
if (additional.length)
|
|
78
|
+
throw combinedFailure(additional[0], additional.slice(1), 'Stream operation and cleanup failed');
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
stream.off('error', onError);
|
|
82
|
+
}
|
|
83
|
+
}, async (resource) => {
|
|
84
|
+
try {
|
|
85
|
+
if (resource.failures.length)
|
|
86
|
+
throw resource.failures[0];
|
|
87
|
+
const result = await use(resource.stream);
|
|
88
|
+
if (resource.failures.length)
|
|
89
|
+
throw resource.failures[0];
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
resource.reported = { error };
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function withReadable(open, use) {
|
|
99
|
+
return withStream(open, use);
|
|
100
|
+
}
|
|
101
|
+
function withWritable(open, use) {
|
|
102
|
+
return withStream(open, use);
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=resources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.js","sourceRoot":"","sources":["../../src/resources.ts"],"names":[],"mappings":";;AAGA,0CAGC;AAED,0CAOC;AAYD,oCAQC;AAkDD,oCAGC;AAED,oCAGC;AA7FD,6CAA8C;AAE9C,6EAA6E;AAC7E,SAAgB,eAAe,CAAC,KAAc,EAAE,SAAkB;IAC9D,OAAO,KAAK,KAAK,SAAS,IAAI,CAAC,KAAK,YAAY,cAAc;QAC1D,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,eAAe,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,CAAA;AACpE,CAAC;AAED,SAAgB,eAAe,CAAC,OAAgB,EAAE,SAA6B,EAAE,OAAe;IAC5F,MAAM,MAAM,GAAG,CAAC,OAAO,CAAC,CAAA;IACxB,KAAK,MAAM,KAAK,IAAI,SAAS;QACzB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;YAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACtF,OAAO,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,cAAc,CAAC,MAAM,EAC5D,GAAG,OAAO,KAAK,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CA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"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_stream_1 = require("node:stream");
|
|
4
|
+
const resources_1 = require("./resources");
|
|
5
|
+
describe('scoped resource ownership', () => {
|
|
6
|
+
it('awaits asynchronous close before returning the result', async () => {
|
|
7
|
+
let release;
|
|
8
|
+
const gate = new Promise(resolve => { release = resolve; });
|
|
9
|
+
let closing;
|
|
10
|
+
const started = new Promise(resolve => { closing = resolve; });
|
|
11
|
+
let complete = false;
|
|
12
|
+
const operation = (0, resources_1.withResource)(() => 42, async () => { closing(); await gate; }, async (value) => value)
|
|
13
|
+
.then(value => { complete = true; return value; });
|
|
14
|
+
await started;
|
|
15
|
+
expect(complete).toBe(false);
|
|
16
|
+
release();
|
|
17
|
+
expect(await operation).toBe(42);
|
|
18
|
+
});
|
|
19
|
+
it('preserves operation and cleanup failures', async () => {
|
|
20
|
+
const operation = new Error('operation');
|
|
21
|
+
const cleanup = new Error('cleanup');
|
|
22
|
+
await expect((0, resources_1.withResource)(() => 1, () => { throw cleanup; }, async () => { throw operation; }))
|
|
23
|
+
.rejects.toMatchObject({ errors: [operation, cleanup] });
|
|
24
|
+
});
|
|
25
|
+
it('awaits writable cleanup on both success and failure', async () => {
|
|
26
|
+
for (const fail of [false, true]) {
|
|
27
|
+
let release;
|
|
28
|
+
let closing;
|
|
29
|
+
const started = new Promise(resolve => { closing = resolve; });
|
|
30
|
+
const sink = new node_stream_1.Writable({ write(_chunk, _encoding, callback) { callback(); }, destroy(error, callback) {
|
|
31
|
+
release = () => callback(error);
|
|
32
|
+
closing();
|
|
33
|
+
} });
|
|
34
|
+
const failure = new Error('work failed');
|
|
35
|
+
let settled = false;
|
|
36
|
+
const operation = (0, resources_1.withWritable)(() => sink, async () => { if (fail)
|
|
37
|
+
throw failure; return 7; });
|
|
38
|
+
const observed = operation.then(value => { settled = true; return value; }, error => { settled = true; throw error; });
|
|
39
|
+
// Attach rejection observation before releasing the controlled cleanup.
|
|
40
|
+
void observed.catch(() => { });
|
|
41
|
+
await started;
|
|
42
|
+
expect(settled).toBe(false);
|
|
43
|
+
release();
|
|
44
|
+
if (fail)
|
|
45
|
+
await expect(observed).rejects.toThrow(failure);
|
|
46
|
+
else
|
|
47
|
+
expect(await observed).toBe(7);
|
|
48
|
+
expect(sink.closed).toBe(true);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
it('accepts an asynchronous stream provider which returns a thenable', async () => {
|
|
52
|
+
const input = node_stream_1.Readable.from([Buffer.from('data')]);
|
|
53
|
+
const thenable = { then: (resolve, reject) => Promise.resolve(input).then(resolve, reject) };
|
|
54
|
+
await (0, resources_1.withReadable)(() => thenable, async (stream) => { for await (const _ of stream) { /* drain */ } });
|
|
55
|
+
expect(input.closed).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
it('rejects unsupported close-event contracts instead of hanging', async () => {
|
|
58
|
+
const input = new node_stream_1.Readable({ emitClose: false, read() { } });
|
|
59
|
+
const use = jest.fn(async () => 7);
|
|
60
|
+
await expect((0, resources_1.withReadable)(() => input, use)).rejects.toThrow('requires emitClose: true');
|
|
61
|
+
expect(use).not.toHaveBeenCalled();
|
|
62
|
+
expect(input.destroyed).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
it('observes a pending close/error even if closed is already true', async () => {
|
|
65
|
+
const failure = new Error('pending stream error');
|
|
66
|
+
const input = new node_stream_1.Readable({ read() { } });
|
|
67
|
+
input.destroy(failure);
|
|
68
|
+
await expect((0, resources_1.withReadable)(() => input, async () => 7)).rejects.toThrow(failure);
|
|
69
|
+
});
|
|
70
|
+
it('awaits stream destruction even without consumption', async () => {
|
|
71
|
+
let release;
|
|
72
|
+
let closing;
|
|
73
|
+
const started = new Promise(resolve => { closing = resolve; });
|
|
74
|
+
const stream = new node_stream_1.Readable({ read() { }, destroy(error, callback) {
|
|
75
|
+
release = () => callback(error);
|
|
76
|
+
closing();
|
|
77
|
+
} });
|
|
78
|
+
let complete = false;
|
|
79
|
+
const result = (0, resources_1.withReadable)(() => stream, async () => 7).then(value => { complete = true; return value; });
|
|
80
|
+
await started;
|
|
81
|
+
expect(complete).toBe(false);
|
|
82
|
+
release();
|
|
83
|
+
expect(await result).toBe(7);
|
|
84
|
+
expect(stream.closed).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
it('closes a stream when the callback fails', async () => {
|
|
87
|
+
const stream = new node_stream_1.Readable({ read() { } });
|
|
88
|
+
await expect((0, resources_1.withReadable)(() => stream, async () => { throw new Error('failure'); })).rejects.toThrow('failure');
|
|
89
|
+
expect(stream.closed).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
it('preserves a stream cleanup error together with a callback error', async () => {
|
|
92
|
+
const operation = new Error('operation');
|
|
93
|
+
const cleanup = new Error('cleanup');
|
|
94
|
+
const stream = new node_stream_1.Readable({ read() { }, destroy(_error, callback) { callback(cleanup); } });
|
|
95
|
+
await expect((0, resources_1.withReadable)(() => stream, async () => { throw operation; }))
|
|
96
|
+
.rejects.toMatchObject({ errors: [operation, cleanup] });
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
//# sourceMappingURL=resources.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.spec.js","sourceRoot":"","sources":["../../src/resources.spec.ts"],"names":[],"mappings":";;AAAA,6CAA8C;AAC9C,2CAAoE;AAEpE,QAAQ,CAAC,2BAA2B,EAAE,GAAG,EAAE;IACvC,EAAE,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;QACnE,IAAI,OAAoB,CAAA;QACxB,MAAM,IAAI,GAAG,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE,GAAE,OAAO,GAAG,OAAO,CAAA,CAAA,CAAC,CAAC,CAAA;QAC9D,IAAI,OAAoB,CAAA;QACxB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE,GAAE,OAAO,GAAG,OAAO,CAAA,CAAA,CAAC,CAAC,CAAA;QACjE,IAAI,QAAQ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MAAM,SAAS,CAAA,CAAA,CAAC,CAAC,CAAC;aAClE,OAAO,CAAC,aAAa,CAAC,EAAC,MAAM,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,EAAC,CAAC,CAAA;IAC9D,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
|
package/index.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobol-ts/stream-scope",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Scoped Node stream ownership and named inputs",
|
|
4
|
+
"version": "0.0.40",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"types": "dist/index",
|
|
7
|
+
"browserslist": {
|
|
8
|
+
"development": [
|
|
9
|
+
"last 1 chrome version",
|
|
10
|
+
"last 1 firefox version",
|
|
11
|
+
"last 1 safari version"
|
|
12
|
+
],
|
|
13
|
+
"production": [
|
|
14
|
+
">0.2%",
|
|
15
|
+
"not dead",
|
|
16
|
+
"not op_mini all"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"typescript": "^5.2.2",
|
|
22
|
+
"@testing-library/jest-dom": "^6.1.4",
|
|
23
|
+
"@types/jest": "^29.0.0",
|
|
24
|
+
"@types/node": "^20.0.0",
|
|
25
|
+
"jest": "^29.0.0",
|
|
26
|
+
"ts-jest": "^29.0.0"
|
|
27
|
+
},
|
|
28
|
+
"eslintConfig": {
|
|
29
|
+
"extends": [
|
|
30
|
+
"react-app",
|
|
31
|
+
"react-app/jest"
|
|
32
|
+
],
|
|
33
|
+
"overrides": [
|
|
34
|
+
{
|
|
35
|
+
"files": [
|
|
36
|
+
"**/*.stories.*"
|
|
37
|
+
],
|
|
38
|
+
"rules": {
|
|
39
|
+
"import/no-anonymous-default-export": "off"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist/index.js",
|
|
46
|
+
"dist/index.d.ts",
|
|
47
|
+
"dist/src/",
|
|
48
|
+
"build/",
|
|
49
|
+
"dist/**/*"
|
|
50
|
+
],
|
|
51
|
+
"keywords": [
|
|
52
|
+
"indexing"
|
|
53
|
+
],
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"module": "./index.ts",
|
|
56
|
+
"repository": "git@github.com:intellimaintain/intellimaintain.git",
|
|
57
|
+
"scripts": {
|
|
58
|
+
"test": "jest --config jest.config.json --env node",
|
|
59
|
+
"test:watch": "npm run test -- --watch"
|
|
60
|
+
}
|
|
6
61
|
}
|