-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Accept Uint8Array where previously only Buffer worked
- Loading branch information
Showing
7 changed files
with
72 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import rolandHexParser from './peg/roland'; | ||
import assert from 'assert'; | ||
import isBufferEquivalent from './utils/isBufferEquivalent'; | ||
import asBuffer from './utils/asBuffer'; | ||
|
||
/** | ||
* @param {Buffer} buf A buffer containing MIDI messages. | ||
* @param {Buffer|Uint8Array} buf A buffer containing MIDI messages. | ||
* @returns {Array<MIDIMessage>} - Contains SysEx messages parsed from the buffer. | ||
*/ | ||
export default function parse (buf) { | ||
assert(Buffer.isBuffer(buf), 'Argument should be a Buffer'); | ||
return rolandHexParser.parse(buf.toString('hex')); | ||
assert(isBufferEquivalent(buf), 'buf must be a Buffer or Uint8Array'); | ||
return rolandHexParser.parse(asBuffer(buf).toString('hex')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import SafeBuffer from 'safe-buffer'; | ||
|
||
/** | ||
* @private | ||
* Ensure that {@link buf} is a @{link Buffer} | ||
* @param {Buffer|TypedArray|ArrayBuffer} buf A buffer or typed array. | ||
* @returns {Buffer} | ||
*/ | ||
export default function asBuffer (buf) { | ||
if (Buffer.isBuffer(buf)) return buf; | ||
if (buf.buffer instanceof ArrayBuffer) { | ||
return SafeBuffer.Buffer.from(buf.buffer); | ||
} | ||
return SafeBuffer.Buffer.from(buf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @private | ||
* Check that {@link buf} is a {@link Buffer} equivalent (e.g. a typed array) | ||
* @param buf | ||
* @returns {boolean} | ||
*/ | ||
export default function isBufferEquivalent (buf) { | ||
return Buffer.isBuffer(buf) || (buf.buffer instanceof ArrayBuffer) || (buf instanceof ArrayBuffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters