react-streaming
Advanced tools
Comparing version 0.3.33-commit-914f21d to 0.3.33-commit-a6da6e2
@@ -55,3 +55,3 @@ "use strict"; | ||
element = react_1.default.createElement(useSuspenseData_1.SuspenseData, null, element); | ||
let injectToStream = (chunk) => { | ||
let injectToStream = async (chunk) => { | ||
buffer.push(chunk); | ||
@@ -58,0 +58,0 @@ }; |
export { createBuffer }; | ||
export type { InjectToStream }; | ||
export type { StreamOperations }; | ||
export type { Chunk }; | ||
declare type InjectToStreamOptions = { | ||
flush?: boolean; | ||
}; | ||
declare type Chunk = string; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => void; | ||
declare type Chunk = string | Promise<string>; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => Promise<void>; | ||
declare type StreamOperations = { | ||
@@ -17,6 +18,6 @@ operations: null | { | ||
injectToStream: InjectToStream; | ||
onReactWriteBefore: (chunk: unknown) => void; | ||
onReactWriteAfter: () => void; | ||
onBeforeEnd: () => void; | ||
onReactWriteBefore: (chunk: unknown) => Promise<void>; | ||
onReactWriteAfter: () => Promise<void>; | ||
onBeforeEnd: () => Promise<void>; | ||
hasStreamEnded: () => boolean; | ||
}; |
@@ -15,3 +15,3 @@ "use strict"; | ||
return { injectToStream, onReactWriteBefore, onReactWriteAfter, onBeforeEnd, hasStreamEnded }; | ||
function injectToStream(chunk, options) { | ||
async function injectToStream(chunk, options) { | ||
if (debug.isEnabled) { | ||
@@ -24,5 +24,5 @@ debug('injectToStream()', getChunkAsString(chunk)); | ||
buffer.push({ chunk, flush: options === null || options === void 0 ? void 0 : options.flush }); | ||
flushBuffer(); | ||
await flushBuffer(); | ||
} | ||
function flushBuffer() { | ||
async function flushBuffer() { | ||
if (!writePermission) { | ||
@@ -39,10 +39,11 @@ return; | ||
let flushStream = false; | ||
buffer.forEach((bufferEntry) => { | ||
for (let { chunk, flush } of buffer) { | ||
(0, utils_1.assert)(streamOperations.operations); | ||
const { writeChunk } = streamOperations.operations; | ||
writeChunk(bufferEntry.chunk); | ||
if (bufferEntry.flush) { | ||
if ((0, utils_1.isPromise)(chunk)) | ||
chunk = await chunk; | ||
writeChunk(chunk); | ||
if (flush) | ||
flushStream = true; | ||
} | ||
}); | ||
} | ||
buffer.length = 0; | ||
@@ -55,9 +56,9 @@ (0, utils_1.assert)(streamOperations.operations); | ||
} | ||
function onReactWriteAfter() { | ||
async function onReactWriteAfter() { | ||
const writeWasBlocked = !writePermission; | ||
writePermission = true; | ||
if (writeWasBlocked) | ||
flushBuffer(); | ||
await flushBuffer(); | ||
} | ||
function onReactWriteBefore(chunk) { | ||
async function onReactWriteBefore(chunk) { | ||
state === 'UNSTARTED' && debug('>>> START'); | ||
@@ -68,7 +69,7 @@ if (debug.isEnabled) { | ||
state = 'STREAMING'; | ||
flushBuffer(); | ||
await flushBuffer(); | ||
} | ||
function onBeforeEnd() { | ||
async function onBeforeEnd() { | ||
writePermission = true; // in case React didn't write anything | ||
flushBuffer(); | ||
await flushBuffer(); | ||
(0, utils_1.assert)(buffer.length === 0); | ||
@@ -75,0 +76,0 @@ state = 'ENDED'; |
@@ -23,5 +23,5 @@ "use strict"; | ||
const writableForReact = new stream_1.Writable({ | ||
write(chunk, encoding, callback) { | ||
async write(chunk, encoding, callback) { | ||
debug('write'); | ||
onReactWriteBefore(chunk); | ||
await onReactWriteBefore(chunk); | ||
if (!writableFromUser.destroyed) { | ||
@@ -36,6 +36,6 @@ writableFromUser.write(chunk, encoding, callback); | ||
}, | ||
final(callback) { | ||
async final(callback) { | ||
debug('final'); | ||
stopTimeout === null || stopTimeout === void 0 ? void 0 : stopTimeout(); | ||
onBeforeEnd(); | ||
await onBeforeEnd(); | ||
writableFromUser.end(); | ||
@@ -42,0 +42,0 @@ onEnded(); |
@@ -46,5 +46,5 @@ "use strict"; | ||
} | ||
onReactWriteBefore(value); | ||
await onReactWriteBefore(value); | ||
streamOperations.operations.writeChunk(value); | ||
onReactWriteAfter(); | ||
await onReactWriteAfter(); | ||
} | ||
@@ -55,4 +55,4 @@ stopTimeout === null || stopTimeout === void 0 ? void 0 : stopTimeout(); | ||
// We should probably remove this workaround once we have a proper solution. | ||
setTimeout(() => { | ||
onBeforeEnd(); | ||
setTimeout(async () => { | ||
await onBeforeEnd(); | ||
controllerOfUserStream.close(); | ||
@@ -59,0 +59,0 @@ onEnded(); |
@@ -30,2 +30,3 @@ "use strict"; | ||
var _a; | ||
return true; | ||
let DEBUG; | ||
@@ -66,10 +67,10 @@ // - `process` can be undefined in edge workers | ||
const PADDING = ' '; | ||
const WIDTH = process.stdout.columns; | ||
const terminalWidth = getTerminalWidth(); | ||
const lines = []; | ||
str.split('\n').forEach((line) => { | ||
if (!WIDTH) { | ||
if (!terminalWidth) { | ||
lines.push(line); | ||
} | ||
else { | ||
chunk(line, WIDTH - PADDING.length).forEach((chunk) => { | ||
chunk(line, terminalWidth - PADDING.length).forEach((chunk) => { | ||
lines.push(chunk); | ||
@@ -101,1 +102,5 @@ }); | ||
} | ||
function getTerminalWidth() { | ||
// https://stackoverflow.com/questions/30335637/get-width-of-terminal-in-node-js/30335724#30335724 | ||
return ((typeof process !== 'undefined' && typeof process.stdout !== 'undefined' && process.stdout.columns) || undefined); | ||
} |
@@ -5,3 +5,3 @@ "use strict"; | ||
const getGlobalObject_1 = require("./getGlobalObject"); | ||
const PROJECT_VERSION = '0.3.33-commit-914f21d'; | ||
const PROJECT_VERSION = '0.3.33-commit-a6da6e2'; | ||
const projectInfo = { | ||
@@ -8,0 +8,0 @@ projectName: 'react-streaming', |
@@ -29,3 +29,3 @@ export { renderToStream }; | ||
element = React.createElement(SuspenseData, null, element); | ||
let injectToStream = (chunk) => { | ||
let injectToStream = async (chunk) => { | ||
buffer.push(chunk); | ||
@@ -32,0 +32,0 @@ }; |
export { createBuffer }; | ||
export type { InjectToStream }; | ||
export type { StreamOperations }; | ||
export type { Chunk }; | ||
declare type InjectToStreamOptions = { | ||
flush?: boolean; | ||
}; | ||
declare type Chunk = string; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => void; | ||
declare type Chunk = string | Promise<string>; | ||
declare type InjectToStream = (chunk: Chunk, options?: InjectToStreamOptions) => Promise<void>; | ||
declare type StreamOperations = { | ||
@@ -17,6 +18,6 @@ operations: null | { | ||
injectToStream: InjectToStream; | ||
onReactWriteBefore: (chunk: unknown) => void; | ||
onReactWriteAfter: () => void; | ||
onBeforeEnd: () => void; | ||
onReactWriteBefore: (chunk: unknown) => Promise<void>; | ||
onReactWriteAfter: () => Promise<void>; | ||
onBeforeEnd: () => Promise<void>; | ||
hasStreamEnded: () => boolean; | ||
}; |
export { createBuffer }; | ||
import { assert, assertUsage, createDebugger } from '../utils'; | ||
import { assert, assertUsage, createDebugger, isPromise } from '../utils'; | ||
const debug = createDebugger('react-streaming:buffer'); | ||
@@ -13,3 +13,3 @@ function createBuffer(streamOperations) { | ||
return { injectToStream, onReactWriteBefore, onReactWriteAfter, onBeforeEnd, hasStreamEnded }; | ||
function injectToStream(chunk, options) { | ||
async function injectToStream(chunk, options) { | ||
if (debug.isEnabled) { | ||
@@ -22,5 +22,5 @@ debug('injectToStream()', getChunkAsString(chunk)); | ||
buffer.push({ chunk, flush: options === null || options === void 0 ? void 0 : options.flush }); | ||
flushBuffer(); | ||
await flushBuffer(); | ||
} | ||
function flushBuffer() { | ||
async function flushBuffer() { | ||
if (!writePermission) { | ||
@@ -37,10 +37,11 @@ return; | ||
let flushStream = false; | ||
buffer.forEach((bufferEntry) => { | ||
for (let { chunk, flush } of buffer) { | ||
assert(streamOperations.operations); | ||
const { writeChunk } = streamOperations.operations; | ||
writeChunk(bufferEntry.chunk); | ||
if (bufferEntry.flush) { | ||
if (isPromise(chunk)) | ||
chunk = await chunk; | ||
writeChunk(chunk); | ||
if (flush) | ||
flushStream = true; | ||
} | ||
}); | ||
} | ||
buffer.length = 0; | ||
@@ -53,9 +54,9 @@ assert(streamOperations.operations); | ||
} | ||
function onReactWriteAfter() { | ||
async function onReactWriteAfter() { | ||
const writeWasBlocked = !writePermission; | ||
writePermission = true; | ||
if (writeWasBlocked) | ||
flushBuffer(); | ||
await flushBuffer(); | ||
} | ||
function onReactWriteBefore(chunk) { | ||
async function onReactWriteBefore(chunk) { | ||
state === 'UNSTARTED' && debug('>>> START'); | ||
@@ -66,7 +67,7 @@ if (debug.isEnabled) { | ||
state = 'STREAMING'; | ||
flushBuffer(); | ||
await flushBuffer(); | ||
} | ||
function onBeforeEnd() { | ||
async function onBeforeEnd() { | ||
writePermission = true; // in case React didn't write anything | ||
flushBuffer(); | ||
await flushBuffer(); | ||
assert(buffer.length === 0); | ||
@@ -73,0 +74,0 @@ state = 'ENDED'; |
@@ -21,5 +21,5 @@ export { createPipeWrapper }; | ||
const writableForReact = new Writable({ | ||
write(chunk, encoding, callback) { | ||
async write(chunk, encoding, callback) { | ||
debug('write'); | ||
onReactWriteBefore(chunk); | ||
await onReactWriteBefore(chunk); | ||
if (!writableFromUser.destroyed) { | ||
@@ -34,6 +34,6 @@ writableFromUser.write(chunk, encoding, callback); | ||
}, | ||
final(callback) { | ||
async final(callback) { | ||
debug('final'); | ||
stopTimeout === null || stopTimeout === void 0 ? void 0 : stopTimeout(); | ||
onBeforeEnd(); | ||
await onBeforeEnd(); | ||
writableFromUser.end(); | ||
@@ -40,0 +40,0 @@ onEnded(); |
@@ -44,5 +44,5 @@ export { createReadableWrapper }; | ||
} | ||
onReactWriteBefore(value); | ||
await onReactWriteBefore(value); | ||
streamOperations.operations.writeChunk(value); | ||
onReactWriteAfter(); | ||
await onReactWriteAfter(); | ||
} | ||
@@ -53,4 +53,4 @@ stopTimeout === null || stopTimeout === void 0 ? void 0 : stopTimeout(); | ||
// We should probably remove this workaround once we have a proper solution. | ||
setTimeout(() => { | ||
onBeforeEnd(); | ||
setTimeout(async () => { | ||
await onBeforeEnd(); | ||
controllerOfUserStream.close(); | ||
@@ -57,0 +57,0 @@ onEnded(); |
@@ -28,2 +28,3 @@ export { createDebugger }; | ||
var _a; | ||
return true; | ||
let DEBUG; | ||
@@ -63,10 +64,10 @@ // - `process` can be undefined in edge workers | ||
const PADDING = ' '; | ||
const WIDTH = process.stdout.columns; | ||
const terminalWidth = getTerminalWidth(); | ||
const lines = []; | ||
str.split('\n').forEach((line) => { | ||
if (!WIDTH) { | ||
if (!terminalWidth) { | ||
lines.push(line); | ||
} | ||
else { | ||
chunk(line, WIDTH - PADDING.length).forEach((chunk) => { | ||
chunk(line, terminalWidth - PADDING.length).forEach((chunk) => { | ||
lines.push(chunk); | ||
@@ -98,1 +99,5 @@ }); | ||
} | ||
function getTerminalWidth() { | ||
// https://stackoverflow.com/questions/30335637/get-width-of-terminal-in-node-js/30335724#30335724 | ||
return ((typeof process !== 'undefined' && typeof process.stdout !== 'undefined' && process.stdout.columns) || undefined); | ||
} |
export { projectInfo }; | ||
import { getGlobalObject } from './getGlobalObject'; | ||
const PROJECT_VERSION = '0.3.33-commit-914f21d'; | ||
const PROJECT_VERSION = '0.3.33-commit-a6da6e2'; | ||
const projectInfo = { | ||
@@ -5,0 +5,0 @@ projectName: 'react-streaming', |
{ | ||
"name": "react-streaming", | ||
"description": "React 18 Streaming. Full-fledged & Easy.", | ||
"version": "0.3.33-commit-914f21d", | ||
"version": "0.3.33-commit-a6da6e2", | ||
"peerDependencies": { | ||
@@ -6,0 +6,0 @@ "react": ">=18", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
146019
3083