@graphql-tools/url-loader 7.13.2-alpha-ec151071.0 → 7.13.2-alpha-4e77fdb4.0
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/cjs/event-stream/handleAsyncIterable.js +4 -10
- package/cjs/event-stream/handleEventStreamResponse.js +8 -2
- package/cjs/event-stream/handleReadableStream.js +43 -23
- package/esm/event-stream/handleAsyncIterable.js +3 -9
- package/esm/event-stream/handleEventStreamResponse.js +6 -1
- package/esm/event-stream/handleReadableStream.js +43 -23
- package/package.json +2 -2
- package/typings/event-stream/handleEventStreamResponse.d.ts +2 -1
- package/typings/event-stream/handleReadableStream.d.ts +2 -1
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable no-labels */
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.handleAsyncIterable = void 0;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
const textDecoder = new TextDecoder();
|
|
11
|
-
decodeUint8Array = uint8Array => textDecoder.decode(uint8Array, { stream: true });
|
|
12
|
-
}
|
|
4
|
+
/* eslint-disable no-labels */
|
|
5
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
6
|
+
const textDecoder = new fetch_1.TextDecoder('handleAsyncIterable');
|
|
13
7
|
async function* handleAsyncIterable(asyncIterable) {
|
|
14
8
|
outer: for await (const chunk of asyncIterable) {
|
|
15
|
-
const chunkStr = typeof chunk === 'string' ? chunk :
|
|
9
|
+
const chunkStr = typeof chunk === 'string' ? chunk : textDecoder.decode(chunk, { stream: true });
|
|
16
10
|
for (const part of chunkStr.split('\n\n')) {
|
|
17
11
|
if (part) {
|
|
18
12
|
const eventStr = part.split('event: ')[1];
|
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleEventStreamResponse = void 0;
|
|
3
|
+
exports.handleEventStreamResponse = exports.isReadableStream = void 0;
|
|
4
4
|
const utils_1 = require("@graphql-tools/utils");
|
|
5
5
|
const handleAsyncIterable_js_1 = require("./handleAsyncIterable.js");
|
|
6
6
|
const handleReadableStream_js_1 = require("./handleReadableStream.js");
|
|
7
|
+
function isReadableStream(value) {
|
|
8
|
+
return value && typeof value.getReader === 'function';
|
|
9
|
+
}
|
|
10
|
+
exports.isReadableStream = isReadableStream;
|
|
7
11
|
async function handleEventStreamResponse(response) {
|
|
8
12
|
// node-fetch returns body as a promise so we need to resolve it
|
|
9
13
|
const body = response.body;
|
|
10
14
|
if (body) {
|
|
15
|
+
if (isReadableStream(body)) {
|
|
16
|
+
return (0, handleReadableStream_js_1.handleReadableStream)(body);
|
|
17
|
+
}
|
|
11
18
|
if ((0, utils_1.isAsyncIterable)(body)) {
|
|
12
19
|
return (0, handleAsyncIterable_js_1.handleAsyncIterable)(body);
|
|
13
20
|
}
|
|
14
|
-
return (0, handleReadableStream_js_1.handleReadableStream)(body);
|
|
15
21
|
}
|
|
16
22
|
throw new Error('Response body is expected to be a readable stream but got; ' + (0, utils_1.inspect)(body));
|
|
17
23
|
}
|
|
@@ -1,31 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable no-labels */
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.handleReadableStream = void 0;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
break outer;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
6
|
+
const textDecoder = new fetch_1.TextDecoder('handleReadableStream');
|
|
7
|
+
function handleReadableStream(readableStream) {
|
|
8
|
+
return (0, utils_1.observableToAsyncIterable)({
|
|
9
|
+
subscribe: observer => {
|
|
10
|
+
const reader = readableStream.getReader();
|
|
11
|
+
let completed = false;
|
|
12
|
+
function pump() {
|
|
13
|
+
return reader.read().then(({ done, value }) => {
|
|
14
|
+
if (completed) {
|
|
15
|
+
return;
|
|
18
16
|
}
|
|
19
|
-
if (
|
|
20
|
-
const
|
|
21
|
-
|
|
17
|
+
if (value) {
|
|
18
|
+
const chunk = typeof value === 'string' ? value : textDecoder.decode(value, { stream: true });
|
|
19
|
+
for (const part of chunk.split('\n\n')) {
|
|
20
|
+
if (part) {
|
|
21
|
+
const eventStr = part.split('event: ')[1];
|
|
22
|
+
const dataStr = part.split('data: ')[1];
|
|
23
|
+
if (eventStr === 'complete') {
|
|
24
|
+
observer.complete();
|
|
25
|
+
}
|
|
26
|
+
if (dataStr) {
|
|
27
|
+
const data = JSON.parse(dataStr);
|
|
28
|
+
observer.next(data.payload || data);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
22
32
|
}
|
|
23
|
-
|
|
33
|
+
if (done) {
|
|
34
|
+
observer.complete();
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
pump();
|
|
38
|
+
}
|
|
39
|
+
});
|
|
24
40
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
41
|
+
pump();
|
|
42
|
+
return {
|
|
43
|
+
unsubscribe: () => {
|
|
44
|
+
reader.cancel();
|
|
45
|
+
completed = true;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
});
|
|
30
50
|
}
|
|
31
51
|
exports.handleReadableStream = handleReadableStream;
|
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
/* eslint-disable no-labels */
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
decodeUint8Array = uint8Array => globalThis.Buffer.from(uint8Array).toString('utf-8');
|
|
5
|
-
}
|
|
6
|
-
else {
|
|
7
|
-
const textDecoder = new TextDecoder();
|
|
8
|
-
decodeUint8Array = uint8Array => textDecoder.decode(uint8Array, { stream: true });
|
|
9
|
-
}
|
|
2
|
+
import { TextDecoder } from '@whatwg-node/fetch';
|
|
3
|
+
const textDecoder = new TextDecoder('handleAsyncIterable');
|
|
10
4
|
export async function* handleAsyncIterable(asyncIterable) {
|
|
11
5
|
outer: for await (const chunk of asyncIterable) {
|
|
12
|
-
const chunkStr = typeof chunk === 'string' ? chunk :
|
|
6
|
+
const chunkStr = typeof chunk === 'string' ? chunk : textDecoder.decode(chunk, { stream: true });
|
|
13
7
|
for (const part of chunkStr.split('\n\n')) {
|
|
14
8
|
if (part) {
|
|
15
9
|
const eventStr = part.split('event: ')[1];
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { inspect, isAsyncIterable } from '@graphql-tools/utils';
|
|
2
2
|
import { handleAsyncIterable } from './handleAsyncIterable.js';
|
|
3
3
|
import { handleReadableStream } from './handleReadableStream.js';
|
|
4
|
+
export function isReadableStream(value) {
|
|
5
|
+
return value && typeof value.getReader === 'function';
|
|
6
|
+
}
|
|
4
7
|
export async function handleEventStreamResponse(response) {
|
|
5
8
|
// node-fetch returns body as a promise so we need to resolve it
|
|
6
9
|
const body = response.body;
|
|
7
10
|
if (body) {
|
|
11
|
+
if (isReadableStream(body)) {
|
|
12
|
+
return handleReadableStream(body);
|
|
13
|
+
}
|
|
8
14
|
if (isAsyncIterable(body)) {
|
|
9
15
|
return handleAsyncIterable(body);
|
|
10
16
|
}
|
|
11
|
-
return handleReadableStream(body);
|
|
12
17
|
}
|
|
13
18
|
throw new Error('Response body is expected to be a readable stream but got; ' + inspect(body));
|
|
14
19
|
}
|
|
@@ -1,27 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (eventStr === 'complete') {
|
|
14
|
-
break outer;
|
|
1
|
+
import { observableToAsyncIterable } from '@graphql-tools/utils';
|
|
2
|
+
import { TextDecoder } from '@whatwg-node/fetch';
|
|
3
|
+
const textDecoder = new TextDecoder('handleReadableStream');
|
|
4
|
+
export function handleReadableStream(readableStream) {
|
|
5
|
+
return observableToAsyncIterable({
|
|
6
|
+
subscribe: observer => {
|
|
7
|
+
const reader = readableStream.getReader();
|
|
8
|
+
let completed = false;
|
|
9
|
+
function pump() {
|
|
10
|
+
return reader.read().then(({ done, value }) => {
|
|
11
|
+
if (completed) {
|
|
12
|
+
return;
|
|
15
13
|
}
|
|
16
|
-
if (
|
|
17
|
-
const
|
|
18
|
-
|
|
14
|
+
if (value) {
|
|
15
|
+
const chunk = typeof value === 'string' ? value : textDecoder.decode(value, { stream: true });
|
|
16
|
+
for (const part of chunk.split('\n\n')) {
|
|
17
|
+
if (part) {
|
|
18
|
+
const eventStr = part.split('event: ')[1];
|
|
19
|
+
const dataStr = part.split('data: ')[1];
|
|
20
|
+
if (eventStr === 'complete') {
|
|
21
|
+
observer.complete();
|
|
22
|
+
}
|
|
23
|
+
if (dataStr) {
|
|
24
|
+
const data = JSON.parse(dataStr);
|
|
25
|
+
observer.next(data.payload || data);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
19
29
|
}
|
|
20
|
-
|
|
30
|
+
if (done) {
|
|
31
|
+
observer.complete();
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
pump();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
21
37
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
38
|
+
pump();
|
|
39
|
+
return {
|
|
40
|
+
unsubscribe: () => {
|
|
41
|
+
reader.cancel();
|
|
42
|
+
completed = true;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
});
|
|
27
47
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-tools/url-loader",
|
|
3
|
-
"version": "7.13.2-alpha-
|
|
3
|
+
"version": "7.13.2-alpha-4e77fdb4.0",
|
|
4
4
|
"description": "A set of utils for faster development of GraphQL tools",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"@graphql-tools/wrap": "8.5.1",
|
|
13
13
|
"@n1ru4l/graphql-live-query": "^0.9.0",
|
|
14
14
|
"@types/ws": "^8.0.0",
|
|
15
|
-
"@whatwg-node/fetch": "^0.2.
|
|
15
|
+
"@whatwg-node/fetch": "^0.2.4",
|
|
16
16
|
"dset": "^3.1.2",
|
|
17
17
|
"extract-files": "^11.0.0",
|
|
18
18
|
"graphql-ws": "^5.4.1",
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { ExecutionResult } from 'graphql';
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function isReadableStream(value: any): value is ReadableStream;
|
|
3
|
+
export declare function handleEventStreamResponse(response: Response): Promise<AsyncIterable<ExecutionResult>>;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { ExecutionResult } from 'graphql';
|
|
2
|
+
export declare function handleReadableStream(readableStream: ReadableStream<Uint8Array>): AsyncIterableIterator<ExecutionResult<import("graphql/jsutils/ObjMap").ObjMap<unknown>, import("graphql/jsutils/ObjMap").ObjMap<unknown>>>;
|