music-metadata-browser
Advanced tools
Comparing version 0.9.0 to 0.9.1
/// <reference types="node" /> | ||
/** | ||
* A mock readable-stream, using string to read from | ||
* Converts a Web-API stream into Node stream.Readable class | ||
* Node stream readable: https://nodejs.org/api/stream.html#stream_readable_streams | ||
* Web API readable-stream: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamhttps://nodejs.org/api/stream.html#stream_readable_streams | ||
*/ | ||
@@ -12,14 +14,24 @@ import { Readable } from 'stream'; | ||
bytesRead: number; | ||
released: boolean; | ||
/** | ||
* Browser stream | ||
* Default web API stream reader | ||
* https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader | ||
*/ | ||
private reader; | ||
private pendingRead; | ||
/** | ||
* | ||
* @param stream ReadableStream: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream | ||
*/ | ||
constructor(stream: ReadableStream); | ||
_read(): void; | ||
_destroy(error: Error | null, callback: (error: Error | null) => void): void; | ||
/** | ||
* Wait for read to complete and release ReadableStreamReader | ||
* https://nodejs.org/api/stream.html#stream_readable_read_size_1 | ||
*/ | ||
release(): Promise<void>; | ||
_read(): Promise<void>; | ||
waitForReadToComplete(): Promise<void>; | ||
/** | ||
* Close wrapper | ||
*/ | ||
close(): Promise<void>; | ||
private syncAndRelease; | ||
} |
@@ -14,3 +14,5 @@ "use strict"; | ||
/** | ||
* A mock readable-stream, using string to read from | ||
* Converts a Web-API stream into Node stream.Readable class | ||
* Node stream readable: https://nodejs.org/api/stream.html#stream_readable_streams | ||
* Web API readable-stream: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamhttps://nodejs.org/api/stream.html#stream_readable_streams | ||
*/ | ||
@@ -23,41 +25,65 @@ const stream_1 = require("stream"); | ||
class Browser2NodeStream extends stream_1.Readable { | ||
/** | ||
* | ||
* @param stream ReadableStream: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream | ||
*/ | ||
constructor(stream) { | ||
super(); | ||
this.bytesRead = 0; | ||
this.released = false; | ||
this.reader = stream.getReader(); | ||
} | ||
/** | ||
* https://nodejs.org/api/stream.html#stream_readable_read_size_1 | ||
*/ | ||
_read() { | ||
this.pendingRead = this.reader.read(); | ||
this.pendingRead.then(res => { | ||
delete this.pendingRead; | ||
if (res.done) { | ||
this.push(null); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Should start pushing data into the queue | ||
// Read data from the underlying Web-API-readable-stream | ||
if (this.released) { | ||
return; | ||
} | ||
else { | ||
this.bytesRead += res.value.length; | ||
this.push(res.value); | ||
this.pendingRead = this.reader.read(); | ||
try { | ||
const data = yield this.pendingRead; | ||
if (data.done || this.released) { | ||
this.push(null); | ||
} | ||
else { | ||
this.bytesRead += data.value.length; | ||
this.push(data.value); | ||
} | ||
} | ||
finally { | ||
delete this.pendingRead; | ||
} | ||
}); | ||
} | ||
_destroy(error, callback) { | ||
this.release().then(() => { | ||
debug(`release browser stream.`); | ||
callback(null); | ||
}).catch(err => { | ||
callback(err); | ||
waitForReadToComplete() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.pendingRead) { | ||
yield this.pendingRead; | ||
} | ||
}); | ||
} | ||
/** | ||
* Wait for read to complete and release ReadableStreamReader | ||
* Close wrapper | ||
*/ | ||
release() { | ||
close() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
debug(`Release stream...`); | ||
if (this.pendingRead) { | ||
yield this.pendingRead; | ||
this.reader.releaseLock(); | ||
} | ||
debug(`close()`); | ||
yield this.syncAndRelease(); | ||
}); | ||
} | ||
syncAndRelease() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
debug(`syncAndRelease()`); | ||
this.released = true; | ||
yield this.waitForReadToComplete(); | ||
// Trick to release the reader after the read-promise resolved AND returned | ||
yield new Promise(resolve => setTimeout(resolve, 1)); | ||
yield this.reader.releaseLock(); | ||
}); | ||
} | ||
} | ||
exports.Browser2NodeStream = Browser2NodeStream; |
@@ -15,3 +15,3 @@ "use strict"; | ||
const Browser2NodeStream_1 = require("./fetch/Browser2NodeStream"); | ||
const debug = initDebug('music-metadata-browser'); | ||
const debug = initDebug('music-metadata-browser:main'); | ||
/** | ||
@@ -37,3 +37,3 @@ * Parse audio Stream | ||
debug(`Completed parsing from stream bytesRead=${ns.bytesRead} / fileSize=${options && options.fileSize ? options.fileSize : '?'}`); | ||
yield ns.release(); | ||
yield ns.close(); | ||
return res; | ||
@@ -81,4 +81,5 @@ }); | ||
const res = yield this.parseReadableStream(response.body, contentType, options); | ||
if (!response.body.locked) | ||
yield response.body.cancel(); | ||
debug('Closing HTTP-readable-stream...'); | ||
yield response.body.cancel(); | ||
debug('HTTP-readable-stream closed.'); | ||
return res; | ||
@@ -85,0 +86,0 @@ } |
{ | ||
"name": "music-metadata-browser", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Browserifed version of music-metadata", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
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
217798
471