UNPKG

mp3tag

Version:

A library for reading/writing mp3 tag data

118 lines (117 loc) 3.89 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.encodeString = exports.decodeBuffer = exports.Encoding = void 0; /** Codepage conversion module. * decodes strings from buffers, and encodes strings into buffers. * * BOMs are not generated... */ const _ = __importStar(require("lodash")); var Encoding; (function (Encoding) { Encoding["ISO_8895_1"] = "ISO-8895-1"; Encoding["UTF_16LE"] = "UTF-16LE"; Encoding["UTF_16BE"] = "UTF-16BE"; Encoding["UTF_8"] = "UTF-8"; })(Encoding || (exports.Encoding = Encoding = {})); /** * @param buffer the buffer to decode * @param encoding the name of the buffer's encoding * * @return the decoded string */ function decodeBuffer(buffer, encoding) { const decoder = from[encoding]; if (!decoder) { throw new Error(`Unsupported encoding: '${encoding}'`); } return decoder(buffer); } exports.decodeBuffer = decodeBuffer; /** * @param string the string to encode * @param encoding the encoding to use * * @return {Buffer} the string encoded in the specified encoding */ function encodeString(string, encoding) { const encoder = to[encoding]; if (!encoder) { throw new Error(`Unsupported encoding: '${encoding}'`); } return encoder(string); } exports.encodeString = encodeString; const from = { /** * @param buffer the buffer to decode into a string * * @return the decoded string */ [Encoding.ISO_8895_1]: function (buffer) { let result = ""; _.each(buffer, function (byte) { result += String.fromCharCode(byte); }); return result; }, /** * @param buffer the buffer to decode into a string * * @return the decoded string */ [Encoding.UTF_16LE]: (buffer) => buffer.toString('utf16le'), /** * @param buffer the buffer to decode into a string * * @return the decoded string */ [Encoding.UTF_16BE]: function (buffer) { const bufferLE = Buffer.alloc(buffer.length); // Copy the passed buffer into bufferLE while swapping the bytes, making it a LE buffer for (let c = 0; c < buffer.length; ++c) { if (c % 2 == 0) { bufferLE[c + 1] = buffer[c]; } else { bufferLE[c - 1] = buffer[c]; } } // Now we can use our LE conversion return from[Encoding.UTF_16LE](bufferLE); }, /** * @param buffer the buffer to decode into a string * * @return the decoded string */ [Encoding.UTF_8]: (buffer) => buffer.toString('utf8') }; const to = { [Encoding.UTF_16LE]: (string) => Buffer.from(string, 'utf16le'), [Encoding.UTF_8]: (string) => Buffer.from(string, 'utf8'), [Encoding.ISO_8895_1]: (string) => Buffer.from(string, 'latin1') };