first-chunk-stream
Advanced tools
Comparing version 4.0.0 to 5.0.0
@@ -0,24 +1,35 @@ | ||
import {Buffer} from 'node:buffer'; | ||
import { | ||
Duplex as DuplexStream, | ||
DuplexOptions as DuplexStreamOption | ||
} from 'stream'; | ||
DuplexOptions as DuplexStreamOption, | ||
} from 'node:stream'; | ||
declare const stop: unique symbol; | ||
export interface Options extends Readonly<DuplexStreamOption> { | ||
/** | ||
The number of bytes to buffer. | ||
*/ | ||
readonly chunkSize: number; | ||
} | ||
declare namespace FirstChunkStream { | ||
interface Options extends Readonly<DuplexStreamOption> { | ||
/** | ||
How many bytes you want to buffer. | ||
*/ | ||
readonly chunkSize: number; | ||
} | ||
export type StopSymbol = typeof FirstChunkStream.stop; | ||
type StopSymbol = typeof stop; | ||
export type BufferLike = string | Buffer | Uint8Array; | ||
type BufferLike = string | Buffer | Uint8Array; | ||
export type TransformFunction = (chunk: Buffer, encoding: string) => Promise<StopSymbol | BufferLike | {buffer: BufferLike; encoding?: string}>; | ||
type TransformFunction = (chunk: Buffer, encoding: string) => Promise<StopSymbol | BufferLike | {buffer: BufferLike, encoding?: string}>; | ||
} | ||
export default class FirstChunkStream extends DuplexStream { | ||
/** | ||
Symbol used to end the stream early. | ||
declare class FirstChunkStream extends DuplexStream { | ||
@example | ||
``` | ||
import FirstChunkStream from 'first-chunk-stream'; | ||
new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => { | ||
return FirstChunkStream.stop; | ||
}); | ||
``` | ||
*/ | ||
static readonly stop: unique symbol; | ||
/** | ||
@@ -34,5 +45,5 @@ Buffer and transform the `n` first bytes of a stream. | ||
``` | ||
import * as fs from 'fs'; | ||
import getStream = require('get-stream'); | ||
import FirstChunkStream = require('first-chunk-stream'); | ||
import fs from 'node:fs'; | ||
import getStream from 'get-stream'; | ||
import FirstChunkStream from 'first-chunk-stream'; | ||
@@ -45,32 +56,16 @@ // unicorn.txt => unicorn rainbow | ||
(async () => { | ||
const data = await getStream(stream); | ||
const data = await getStream(stream); | ||
if (data.length < 7) { | ||
throw new Error('Couldn\'t get the minimum required first chunk length'); | ||
} | ||
if (data.length < 7) { | ||
throw new Error('Couldn\'t get the minimum required first chunk length'); | ||
} | ||
console.log(data); | ||
//=> 'UNICORN rainbow' | ||
})(); | ||
console.log(data); | ||
//=> 'UNICORN rainbow' | ||
``` | ||
*/ | ||
constructor( | ||
options: FirstChunkStream.Options, | ||
transform: FirstChunkStream.TransformFunction | ||
options: Options, | ||
transform: TransformFunction | ||
); | ||
/** | ||
Symbol used to end the stream early. | ||
@example | ||
``` | ||
new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => { | ||
return FirstChunkStream.stop; | ||
}); | ||
``` | ||
*/ | ||
static readonly stop: FirstChunkStream.StopSymbol; | ||
} | ||
export = FirstChunkStream; |
20
index.js
@@ -1,12 +0,12 @@ | ||
'use strict'; | ||
const {Duplex: DuplexStream} = require('stream'); | ||
import {Buffer} from 'node:buffer'; | ||
import {Duplex as DuplexStream} from 'node:stream'; | ||
const stop = Symbol('FirstChunkStream.stop'); | ||
class FirstChunkStream extends DuplexStream { | ||
export default class FirstChunkStream extends DuplexStream { | ||
constructor(options, callback) { | ||
const state = { | ||
sent: false, | ||
isSent: false, | ||
chunks: [], | ||
size: 0 | ||
size: 0, | ||
}; | ||
@@ -36,3 +36,3 @@ | ||
const processCallback = (buffer, encoding, done) => { | ||
state.sent = true; | ||
state.isSent = true; | ||
@@ -64,3 +64,3 @@ (async () => { | ||
state.encoding = encoding; | ||
if (state.sent) { | ||
if (state.isSent) { | ||
state.manager.programPush(chunk, state.encoding, done); | ||
@@ -88,3 +88,3 @@ } else if (chunk.length < options.chunkSize - state.size) { | ||
this.on('finish', () => { | ||
if (!state.sent) { | ||
if (!state.isSent) { | ||
return processCallback(Buffer.concat(state.chunks, state.size), state.encoding, () => { | ||
@@ -130,3 +130,3 @@ state.manager.programPush(null, state.encoding); | ||
} | ||
} | ||
}, | ||
}; | ||
@@ -147,3 +147,1 @@ | ||
FirstChunkStream.stop = stop; | ||
module.exports = FirstChunkStream; |
{ | ||
"name": "first-chunk-stream", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Buffer and transform the n first bytes of a stream", | ||
"license": "MIT", | ||
"repository": "sindresorhus/first-chunk-stream", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -30,3 +33,2 @@ "scripts": { | ||
"size", | ||
"min", | ||
"minimum", | ||
@@ -36,9 +38,9 @@ "bytes" | ||
"devDependencies": { | ||
"@types/node": "^12.0.8", | ||
"ava": "^2.1.0", | ||
"nyc": "^14.0.0", | ||
"streamtest": "^1.2.1", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
"@types/node": "^16.6.1", | ||
"ava": "^3.15.0", | ||
"nyc": "^15.1.0", | ||
"streamtest": "^2.0.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,6 +0,5 @@ | ||
# first-chunk-stream [![Build Status](https://travis-ci.org/sindresorhus/first-chunk-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/first-chunk-stream) | ||
# first-chunk-stream | ||
> Buffer and transform the n first bytes of a stream | ||
## Install | ||
@@ -12,9 +11,8 @@ | ||
## Usage | ||
```js | ||
const fs = require('fs'); | ||
const getStream = require('get-stream'); | ||
const FirstChunkStream = require('first-chunk-stream'); | ||
import fs from 'node:fs'; | ||
import getStream from 'get-stream'; | ||
import FirstChunkStream from 'first-chunk-stream'; | ||
@@ -27,15 +25,12 @@ // unicorn.txt => unicorn rainbow | ||
(async () => { | ||
const data = await getStream(stream); | ||
const data = await getStream(stream); | ||
if (data.length < 7) { | ||
throw new Error('Couldn\'t get the minimum required first chunk length'); | ||
} | ||
if (data.length < 7) { | ||
throw new Error('Couldn\'t get the minimum required first chunk length'); | ||
} | ||
console.log(data); | ||
//=> 'UNICORN rainbow' | ||
})(); | ||
console.log(data); | ||
//=> 'UNICORN rainbow' | ||
``` | ||
## API | ||
@@ -60,2 +55,4 @@ | ||
```js | ||
import FirstChunkStream from 'first-chunk-stream'; | ||
new FirstChunkStream({chunkSize: 7}, async (chunk, encoding) => { | ||
@@ -95,2 +92,2 @@ return chunk.toString(encoding).toUpperCase(); // Send string to stream | ||
How many bytes you want to buffer. | ||
The number of bytes to buffer. |
Sorry, the diff of this file is not supported yet
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
Yes
10429
174
90