mirror of
https://github.com/darksaid98/mediaplex.git
synced 2026-12-01 22:16:25 +00:00
0.0.0
This commit is contained in:
1 parent
12c4b0893d
commit
64224cb2ae
6 files changed
+95
-99
No files matched your search
Vendored
+1
-1
@@ -1,4 +1,4 @@
|
||||
import type { ProbeResult } from './lib/binding'
|
||||
import type { ProbeResult } from './js-binding'
|
||||
import type { Readable } from 'stream'
|
||||
|
||||
export type StreamProbeResult = {
|
||||
|
||||
@@ -1,4 +1,95 @@
|
||||
const { CodecType, probe, probeStream } = require('./lib/index.js');
|
||||
const binding = require('./js-binding');
|
||||
const { Readable } = require('stream');
|
||||
|
||||
/**
|
||||
* @typedef StreamProbeResult
|
||||
* @property {Readable} stream The source stream
|
||||
* @property {binding.ProbeResult} result The probe result
|
||||
*/
|
||||
|
||||
/**
|
||||
* Attempt to probe a Readable stream
|
||||
* @param {Readable} stream The readable stream to probe
|
||||
* @param {number} [probeSize] The number of bytes to read
|
||||
* @returns {Promise<StreamProbeResult>}
|
||||
*/
|
||||
async function probeStream(
|
||||
stream,
|
||||
probeSize = 1024
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// preconditions
|
||||
if (stream.readableObjectMode) {
|
||||
reject(new Error('Cannot probe a readable stream in object mode'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream.readableEnded) {
|
||||
reject(new Error('Cannot probe a stream that has ended'));
|
||||
return;
|
||||
}
|
||||
|
||||
let readBuffer = Buffer.alloc(0);
|
||||
|
||||
/**
|
||||
* @type {binding.ProbeResult}
|
||||
*/
|
||||
let resolved = null;
|
||||
|
||||
const finish = (data) => {
|
||||
stream.off('data', onData);
|
||||
stream.off('close', onClose);
|
||||
stream.off('end', onClose);
|
||||
stream.pause();
|
||||
|
||||
resolved = data;
|
||||
|
||||
if (stream.readableEnded) {
|
||||
resolve({
|
||||
stream: Readable.from(readBuffer),
|
||||
result: data,
|
||||
});
|
||||
} else {
|
||||
if (readBuffer.length > 0) {
|
||||
stream.push(readBuffer);
|
||||
}
|
||||
|
||||
resolve({
|
||||
stream,
|
||||
result: data,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const onClose = () => {
|
||||
if (!resolved) {
|
||||
finish(null);
|
||||
}
|
||||
};
|
||||
|
||||
const onData = (buffer) => {
|
||||
readBuffer = Buffer.concat([readBuffer, buffer]);
|
||||
|
||||
try {
|
||||
const result = binding.probe(readBuffer);
|
||||
return finish(result);
|
||||
} catch { }
|
||||
|
||||
if (readBuffer.length >= probeSize) {
|
||||
stream.off('data', onData);
|
||||
stream.pause();
|
||||
process.nextTick(onClose);
|
||||
}
|
||||
};
|
||||
|
||||
stream.once('error', reject);
|
||||
stream.on('data', onData);
|
||||
stream.once('close', onClose);
|
||||
stream.once('end', onClose);
|
||||
});
|
||||
}
|
||||
|
||||
const { CodecType, probe } = binding;
|
||||
|
||||
module.exports = {
|
||||
CodecType,
|
||||
|
||||
File renamed without changes.
File renamed without changes.
@@ -1,95 +0,0 @@
|
||||
const binding = require('./binding');
|
||||
const { Readable } = require('stream');
|
||||
|
||||
/**
|
||||
* @typedef StreamProbeResult
|
||||
* @property {Readable} stream The source stream
|
||||
* @property {binding.ProbeResult} result The probe result
|
||||
*/
|
||||
|
||||
/**
|
||||
* Attempt to probe a Readable stream
|
||||
* @param {Readable} stream The readable stream to probe
|
||||
* @param {number} [probeSize] The number of bytes to read
|
||||
* @returns {Promise<StreamProbeResult>}
|
||||
*/
|
||||
async function probeStream(
|
||||
stream,
|
||||
probeSize = 1024
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// preconditions
|
||||
if (stream.readableObjectMode) {
|
||||
reject(new Error('Cannot probe a readable stream in object mode'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream.readableEnded) {
|
||||
reject(new Error('Cannot probe a stream that has ended'));
|
||||
return;
|
||||
}
|
||||
|
||||
let readBuffer = Buffer.alloc(0);
|
||||
|
||||
/**
|
||||
* @type {binding.ProbeResult}
|
||||
*/
|
||||
let resolved = null;
|
||||
|
||||
const finish = (data) => {
|
||||
stream.off('data', onData);
|
||||
stream.off('close', onClose);
|
||||
stream.off('end', onClose);
|
||||
stream.pause();
|
||||
|
||||
resolved = data;
|
||||
|
||||
if (stream.readableEnded) {
|
||||
resolve({
|
||||
stream: Readable.from(readBuffer),
|
||||
result: data,
|
||||
});
|
||||
} else {
|
||||
if (readBuffer.length > 0) {
|
||||
stream.push(readBuffer);
|
||||
}
|
||||
|
||||
resolve({
|
||||
stream,
|
||||
result: data,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const onClose = () => {
|
||||
if (!resolved) {
|
||||
finish(null);
|
||||
}
|
||||
};
|
||||
|
||||
const onData = (buffer) => {
|
||||
readBuffer = Buffer.concat([readBuffer, buffer]);
|
||||
|
||||
try {
|
||||
const result = binding.probe(readBuffer);
|
||||
return finish(result);
|
||||
} catch {}
|
||||
|
||||
if (readBuffer.length >= probeSize) {
|
||||
stream.off('data', onData);
|
||||
stream.pause();
|
||||
process.nextTick(onClose);
|
||||
}
|
||||
};
|
||||
|
||||
stream.once('error', reject);
|
||||
stream.on('data', onData);
|
||||
stream.once('close', onClose);
|
||||
stream.once('end', onClose);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
...binding,
|
||||
probeStream,
|
||||
};
|
||||
+2
-2
@@ -34,8 +34,8 @@
|
||||
},
|
||||
"scripts": {
|
||||
"artifacts": "napi artifacts",
|
||||
"build": "napi build --platform --release --dts binding.d.ts --js binding.js ./lib",
|
||||
"build:debug": "napi build --platform --dts binding.d.ts --js binding.js ./lib",
|
||||
"build": "napi build --platform --release --dts js-binding.d.ts --js js-binding.js ./",
|
||||
"build:debug": "napi build --platform --dts js-binding.d.ts --js js-binding.js ./",
|
||||
"prepublishOnly": "napi prepublish -t npm",
|
||||
"test": "ava",
|
||||
"universal": "napi universal",
|
||||
|
||||
Reference in new issue
Block a user