diff --git a/README.md b/README.md index 2c862e7a0..98c46e0a5 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ | Project | Description | Demo | | --------------------------------------------------------------------------------------------------------------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- | | [artplayer-plugin-danmu](https://github.com/zhw2590582/ArtPlayer/tree/master/packages/artplayer-plugin-danmu) | Danmu plugin for ArtPlayer | `WIP` | -| [artplayer-plugin-flv](https://github.com/zhw2590582/ArtPlayer/tree/master/packages/artplayer-plugin-flv) | Flv plugin for ArtPlayer | `WIP` | | [artplayer-plugin-gif](https://github.com/zhw2590582/ArtPlayer/tree/master/packages/artplayer-plugin-gif) | Gif plugin for ArtPlayer | [demo](https://artplayer.org/?libs=.%2Funcompiled%2Fartplayer-plugin-gif.js&example=gif) | | [artplayer-plugin-backlight](https://github.com/zhw2590582/ArtPlayer/tree/master/packages/artplayer-plugin-backlight) | Backlight plugin for ArtPlayer | [demo](https://artplayer.org/?libs=.%2Funcompiled%2Fartplayer-plugin-backlight.js&example=backlight) | | [artplayer-plugin-playlist](https://github.com/zhw2590582/ArtPlayer/tree/master/packages/artplayer-plugin-playlist) | Playlist plugin for ArtPlayer | [demo](https://artplayer.org/?libs=.%2Funcompiled%2Fartplayer-plugin-playlist.js%0A.%2Funcompiled%2Fartplayer-plugin-playlist.css&example=playlist) | diff --git a/packages/artplayer-plugin-flv/.npmignore b/packages/artplayer-plugin-flv/.npmignore deleted file mode 100644 index 429145126..000000000 --- a/packages/artplayer-plugin-flv/.npmignore +++ /dev/null @@ -1,9 +0,0 @@ -node_modules -.DS_Store -yarn.lock -package-lock.json -test -coverage -.nyc_output -.cache -docs \ No newline at end of file diff --git a/packages/artplayer-plugin-flv/.npmrc b/packages/artplayer-plugin-flv/.npmrc deleted file mode 100644 index 9cf949503..000000000 --- a/packages/artplayer-plugin-flv/.npmrc +++ /dev/null @@ -1 +0,0 @@ -package-lock=false \ No newline at end of file diff --git a/packages/artplayer-plugin-flv/README.md b/packages/artplayer-plugin-flv/README.md deleted file mode 100644 index 4c2a1f410..000000000 --- a/packages/artplayer-plugin-flv/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# artplayer-plugin-flv -Flv plugin for ArtPlayer - diff --git a/packages/artplayer-plugin-flv/dist/artplayer-plugin-flv.js b/packages/artplayer-plugin-flv/dist/artplayer-plugin-flv.js deleted file mode 100644 index 8cdb6ec9e..000000000 --- a/packages/artplayer-plugin-flv/dist/artplayer-plugin-flv.js +++ /dev/null @@ -1,8 +0,0 @@ -/*! - * artplayer-plugin-flv.js v3.1.7 - * Github: https://github.com/zhw2590582/ArtPlayer#readme - * (c) 2017-2019 Harvey Zack - * Released under the MIT License. - */ - -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self)["artplayer-plugin-flv"]=t()}(this,function(){"use strict";var a=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")};function r(e,t){for(var n=0;n {\n return self.postMessage((".concat(e,").apply(null, event.data));\n }\n ")}},{key:"create",value:function(e){var t=new Blob([o.fnToStr(e)],{type:"application/javascript"}),n=window.URL.createObjectURL(t),r=new Worker(n);return r.kill=function(){URL.revokeObjectURL(n),r.terminate()},r.post=function(e){return new Promise(function(t,n){r.onmessage=function(e){t(e.data)},r.onerror=function(e){n(e)},r.postMessage(e)})},r}}]),o}(),F={mediaSource:{propertys:["activeSourceBuffers","duration","readyState","sourceBuffers"],methods:["addSourceBuffer","endOfStream","removeSourceBuffer","clearLiveSeekableRange","setLiveSeekableRange"],events:["sourceclose","sourceended","sourceopen"]},sourceBuffer:{propertys:["mode","updating","buffered","timestampOffset","audioTracks","videoTracks","textTracks","appendWindowStart","appendWindowEnd","trackDefaults"],methods:["appendBuffer","appendStream","abort","remove"],events:["abort","error","update","updateend","updatestart"]},sourceBufferList:{propertys:["length"],events:["addsourcebuffer","removesourcebuffer"]}},R=function(){function t(e){a(this,t),this.flv=e,this.creatUrl(),this.eventBind()}return c(t,[{key:"creatUrl",value:function(){var e=this.flv,t=e.options.mediaElement,n=e.events.destroyEvents;this.mediaSource=new MediaSource;var r=URL.createObjectURL(this.mediaSource);n.push(function(){URL.revokeObjectURL(r)}),t.src=r}},{key:"eventBind",value:function(){var t=this,n=this.flv.events.proxy;F.mediaSource.events.forEach(function(e){n(t.mediaSource,e,function(e){t.flv.emit("mediaSource:".concat(e.type),e)})}),F.sourceBufferList.events.forEach(function(e){n(t.mediaSource.sourceBuffers,e,function(e){t.flv.emit("sourceBuffers:".concat(e.type),e)}),n(t.mediaSource.activeSourceBuffers,e,function(e){t.flv.emit("activeSourceBuffers:".concat(e.type),e)})})}}]),t}();var B=function(e){if(Array.isArray(e))return e};var L=function(e,t){var n=[],r=!0,o=!1,i=void 0;try{for(var a,c=e[Symbol.iterator]();!(r=(a=c.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){o=!0,i=e}finally{try{r||null==c.return||c.return()}finally{if(o)throw i}}return n};var M=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")};var P=function(e,t){return B(e)||L(e,t)||M()};function D(v){var y=T(v),e=Object.create(null),t=Object.create(null),n=Object.create(null),r=y(1),o=P(r,1);t.type=o[0],w(2===t.type,"AMF: [amf1] type expect 2, but got ".concat(t.type)),t.size=k(y(2)),t.string=x(y(t.size));var i=y(1),a=P(i,1);function m(e){var t=null;if(void 0!==e)switch(e){case 0:t=E(y(8));break;case 1:t=O(y(1)[0]);break;case 2:var n=k(y(2));t=x(y(n));break;case 3:t=Object.create(null);for(var r=!1;!r&&y.index", - "license": "MIT", - "bugs": { - "url": "https://github.com/zhw2590582/ArtPlayer/issues" - }, - "homepage": "https://github.com/zhw2590582/ArtPlayer#readme", - "dependencies": { - "tiny-emitter": "^2.0.2" - } -} diff --git a/packages/artplayer-plugin-flv/src/config.js b/packages/artplayer-plugin-flv/src/config.js deleted file mode 100644 index 901998bf5..000000000 --- a/packages/artplayer-plugin-flv/src/config.js +++ /dev/null @@ -1,58 +0,0 @@ -export default { - mediaSource: { - propertys: [ - 'activeSourceBuffers', - 'duration', - 'readyState', - 'sourceBuffers', - ], - methods: [ - 'addSourceBuffer', - 'endOfStream', - 'removeSourceBuffer', - 'clearLiveSeekableRange', - 'setLiveSeekableRange', - ], - events: [ - 'sourceclose', - 'sourceended', - 'sourceopen', - ], - }, - sourceBuffer: { - propertys: [ - 'mode', - 'updating', - 'buffered', - 'timestampOffset', - 'audioTracks', - 'videoTracks', - 'textTracks', - 'appendWindowStart', - 'appendWindowEnd', - 'trackDefaults', - ], - methods: [ - 'appendBuffer', - 'appendStream', - 'abort', - 'remove', - ], - events: [ - 'abort', - 'error', - 'update', - 'updateend', - 'updatestart', - ], - }, - sourceBufferList: { - propertys: [ - 'length', - ], - events: [ - 'addsourcebuffer', - 'removesourcebuffer', - ], - }, -}; diff --git a/packages/artplayer-plugin-flv/src/creatMediaSource.js b/packages/artplayer-plugin-flv/src/creatMediaSource.js deleted file mode 100644 index d04c53314..000000000 --- a/packages/artplayer-plugin-flv/src/creatMediaSource.js +++ /dev/null @@ -1,43 +0,0 @@ -import config from './config'; - -export default class CreatMediaSource { - constructor(flv) { - this.flv = flv; - this.creatUrl(); - this.eventBind(); - } - - creatUrl() { - const { - options: { mediaElement }, - events: { destroyEvents }, - } = this.flv; - this.mediaSource = new MediaSource(); - const url = URL.createObjectURL(this.mediaSource); - destroyEvents.push(() => { - URL.revokeObjectURL(url); - }); - mediaElement.src = url; - } - - eventBind() { - const { - events: { proxy }, - } = this.flv; - - config.mediaSource.events.forEach(eventName => { - proxy(this.mediaSource, eventName, event => { - this.flv.emit(`mediaSource:${event.type}`, event); - }); - }); - - config.sourceBufferList.events.forEach(eventName => { - proxy(this.mediaSource.sourceBuffers, eventName, event => { - this.flv.emit(`sourceBuffers:${event.type}`, event); - }); - proxy(this.mediaSource.activeSourceBuffers, eventName, event => { - this.flv.emit(`activeSourceBuffers:${event.type}`, event); - }); - }); - } -} diff --git a/packages/artplayer-plugin-flv/src/creatWorker.js b/packages/artplayer-plugin-flv/src/creatWorker.js deleted file mode 100644 index 3593c5ee3..000000000 --- a/packages/artplayer-plugin-flv/src/creatWorker.js +++ /dev/null @@ -1,58 +0,0 @@ -export default class CreatWorker { - constructor() { - this.workers = new Map(); - } - - static fnToStr(fn) { - return ` - self.onmessage = event => { - return self.postMessage((${fn}).apply(null, event.data)); - } - `; - } - - static create(fn) { - const blob = new Blob([CreatWorker.fnToStr(fn)], { - type: 'application/javascript', - }); - const objectURL = window.URL.createObjectURL(blob); - const worker = new Worker(objectURL); - worker.kill = () => { - URL.revokeObjectURL(objectURL); - worker.terminate(); - }; - worker.post = args => - new Promise((resolve, reject) => { - worker.onmessage = event => { - resolve(event.data); - }; - worker.onerror = error => { - reject(error); - }; - worker.postMessage(args); - }); - return worker; - } - - add(name, fn) { - if (!this.workers.has(name)) { - this.workers.set(name, CreatWorker.create(fn)); - } - } - - run(name, ...args) { - const worker = this.workers.get(name); - return worker.post(args); - } - - kill(name) { - const worker = this.workers.get(name); - worker.kill(); - } - - killAll() { - this.workers.forEach(worker => { - worker.kill(); - }); - } -} diff --git a/packages/artplayer-plugin-flv/src/eventProxy.js b/packages/artplayer-plugin-flv/src/eventProxy.js deleted file mode 100644 index cc5fbfb75..000000000 --- a/packages/artplayer-plugin-flv/src/eventProxy.js +++ /dev/null @@ -1,17 +0,0 @@ -export default class EventProxy { - constructor() { - this.destroyEvents = []; - this.proxy = this.proxy.bind(this); - } - - proxy(target, name, callback, option = {}) { - target.addEventListener(name, callback, option); - this.destroyEvents.push(() => { - target.removeEventListener(name, callback, option); - }); - } - - destroy() { - this.destroyEvents.forEach(event => event()); - } -} \ No newline at end of file diff --git a/packages/artplayer-plugin-flv/src/flvParse/fetchStream.js b/packages/artplayer-plugin-flv/src/flvParse/fetchStream.js deleted file mode 100644 index 1853d7ae8..000000000 --- a/packages/artplayer-plugin-flv/src/flvParse/fetchStream.js +++ /dev/null @@ -1,49 +0,0 @@ -import { errorHandle } from '../utils'; - -export default function fetchStream(flv, url) { - flv.emit('flvFetchStart'); - fetch(url).then(response => { - errorHandle( - response.ok && response.status >= 200 && response.status <= 299, - `${response.status} ${response.statusText}`, - ); - const contentType = response.headers.get('content-type'); - errorHandle(contentType.includes('x-flv'), 'The resource does not seem to be a flv file'); - return new Response( - new ReadableStream({ - start(controller) { - const reader = response.body.getReader(); - - flv.on('destroy', () => { - reader.cancel(); - }); - - flv.on('readerCancel', () => { - reader.cancel(); - }); - - (function read() { - reader - .read() - .then(({ done, value }) => { - if (done) { - flv.emit('flvFetchEnd'); - controller.close(); - return; - } - flv.emit('flvFetching', new Uint8Array(value)); - controller.enqueue(value); - read(); - }) - .catch(error => { - throw error; - }); - })(); - }, - cancel() { - flv.emit('flvFetchCancel'); - }, - }), - ); - }); -} diff --git a/packages/artplayer-plugin-flv/src/flvParse/index.js b/packages/artplayer-plugin-flv/src/flvParse/index.js deleted file mode 100644 index a4267816b..000000000 --- a/packages/artplayer-plugin-flv/src/flvParse/index.js +++ /dev/null @@ -1,110 +0,0 @@ -import fetchStream from './fetchStream'; -import readFile from './readFile'; -import parseScripTag from './parseScripTag'; -import { mergeTypedArrays, getUint8Sum, bin2String } from '../utils'; - -export default class FlvParse { - constructor(flv) { - this.flv = flv; - const { url, debug } = flv.options; - this.uint8 = new Uint8Array(0); - this.index = 0; - this.header = null; - this.scripTag = null; - this.tags = []; - this.done = false; - - flv.on('flvFetchStart', () => { - if (debug) { - console.log('[flv-fetch-start]', url); - } - }); - - flv.on('flvFetchCancel', () => { - if (debug) { - console.log('[flv-fetch-cancel]'); - } - }); - - flv.on('flvFetching', uint8 => { - this.uint8 = mergeTypedArrays(this.uint8, uint8); - this.parse(); - }); - - flv.on('flvFetchEnd', uint8 => { - if (debug) { - console.log('[flv-fetch-end]'); - } - this.done = true; - if (uint8) { - this.uint8 = uint8; - this.index = 0; - this.header = null; - this.scripTag = null; - this.tags = []; - this.parse(); - } - flv.emit('flvParseDone'); - if (debug) { - console.log('[flv-parse-done]'); - } - }); - - if (typeof url === 'string') { - fetchStream(flv, url); - } else { - readFile(flv, url); - } - } - - parse() { - const { debug } = this.flv.options; - if (this.uint8.length >= 13 && !this.header) { - const header = Object.create(null); - header.signature = bin2String(this.read(3)); - [header.version] = this.read(1); - [header.flags] = this.read(1); - header.headersize = getUint8Sum(this.read(4)); - this.header = header; - this.read(4); - this.flv.emit('flvParseHeader', this.header); - if (debug) { - console.log('[flv-parse-header]', this.header); - } - } - - while (this.index < this.uint8.length) { - const tag = Object.create(null); - [tag.tagType] = this.read(1); - tag.dataSize = getUint8Sum(this.read(3)); - tag.timestamp = this.read(4); - tag.streamID = this.read(3); - tag.body = this.read(tag.dataSize); - this.tags.push(tag); - this.read(4); - - switch (tag.tagType) { - case 18: - this.scripTag = parseScripTag(tag.body); - this.flv.emit('parseScripTag', this.scripTag); - if (debug) { - console.log('[flv-parse-scrip-tag]', this.scripTag); - } - break; - default: - break; - } - - this.flv.emit('flvParseTag', tag); - } - } - - read(length) { - const tempUint8 = new Uint8Array(length); - for (let i = 0; i < length; i += 1) { - tempUint8[i] = this.uint8[this.index]; - this.index += 1; - } - return tempUint8; - } -} diff --git a/packages/artplayer-plugin-flv/src/flvParse/parseScripTag.js b/packages/artplayer-plugin-flv/src/flvParse/parseScripTag.js deleted file mode 100644 index 3ddb9a22c..000000000 --- a/packages/artplayer-plugin-flv/src/flvParse/parseScripTag.js +++ /dev/null @@ -1,104 +0,0 @@ -import { getUint8Sum, readUint8, bin2String, bin2Boolean, bin2Float, errorHandle } from '../utils'; - -export default function parseScripTag(scripTagBody) { - const readScripTag = readUint8(scripTagBody); - const metadata = Object.create(null); - const amf1 = Object.create(null); - const amf2 = Object.create(null); - - [amf1.type] = readScripTag(1); - errorHandle(amf1.type === 2, `AMF: [amf1] type expect 2, but got ${amf1.type}`); - amf1.size = getUint8Sum(readScripTag(2)); - amf1.string = bin2String(readScripTag(amf1.size)); - - [amf2.type] = readScripTag(1); - errorHandle(amf2.type === 8, `AMF: [amf1] type expect 8, but got ${amf2.type}`); - amf2.size = getUint8Sum(readScripTag(4)); - amf2.metaData = Object.create(null); - - function getValue(type) { - let value = null; - if (type !== undefined) { - switch (type) { - case 0: - value = bin2Float(readScripTag(8)); - break; - case 1: - value = bin2Boolean(readScripTag(1)[0]); - break; - case 2: { - const valueLength = getUint8Sum(readScripTag(2)); - value = bin2String(readScripTag(valueLength)); - break; - } - case 3: { - value = Object.create(null); - let endObject = false; - while (!endObject && readScripTag.index < scripTagBody.length) { - const nameLength = getUint8Sum(readScripTag(2)); - const name = bin2String(readScripTag(nameLength)); - const type = readScripTag(1)[0]; - if (name) { - value[name] = getValue(type); - } - if (type === 9) { - endObject = true; - } - } - break; - } - case 8: { - value = Object.create(null); - let endArray = false; - while (!endArray && readScripTag.index < scripTagBody.length) { - const nameLength = getUint8Sum(readScripTag(2)); - const name = bin2String(readScripTag(nameLength)); - const type = readScripTag(1)[0]; - if (name) { - value[name] = getValue(type); - } - if (type === 9) { - endArray = true; - } - } - break; - } - case 10: { - const valueLength = getUint8Sum(readScripTag(4)); - value = []; - for (let index = 0; index < valueLength; index += 1) { - const itemType = readScripTag(1)[0]; - value.push(getValue(itemType)); - } - break; - } - case 12: { - const valueLength = getUint8Sum(readScripTag(4)); - value = bin2String(readScripTag(valueLength)); - break; - } - default: - console.log(readScripTag(scripTagBody.length - readScripTag.index - 1)); - errorHandle(false, `AMF: Unknown metaData type: ${type}`); - break; - } - } - return value; - } - - while (readScripTag.index < scripTagBody.length) { - const nameLength = getUint8Sum(readScripTag(2)); - const name = bin2String(readScripTag(nameLength)); - const type = readScripTag(1)[0]; - if (name) { - amf2.metaData[name] = getValue(type); - } - } - - errorHandle(readScripTag.index === scripTagBody.length, 'AMF: Seems to be incompletely parsed'); - errorHandle(amf2.size === Object.keys(amf2.metaData).length, 'AMF: [amf2] length does not match'); - - metadata.amf1 = amf1; - metadata.amf2 = amf2; - return metadata; -} diff --git a/packages/artplayer-plugin-flv/src/flvParse/readFile.js b/packages/artplayer-plugin-flv/src/flvParse/readFile.js deleted file mode 100644 index d7ddbcc39..000000000 --- a/packages/artplayer-plugin-flv/src/flvParse/readFile.js +++ /dev/null @@ -1,11 +0,0 @@ -export default function readFile(flv, file) { - flv.emit('flvFetchStart'); - const { proxy } = flv.events; - const reader = new FileReader(); - proxy(reader, 'load', e => { - const buffer = e.target.result; - const uint8 = new Uint8Array(buffer); - flv.emit('flvFetchEnd', uint8); - }); - reader.readAsArrayBuffer(file); -} diff --git a/packages/artplayer-plugin-flv/src/index.js b/packages/artplayer-plugin-flv/src/index.js deleted file mode 100644 index ebada3ad8..000000000 --- a/packages/artplayer-plugin-flv/src/index.js +++ /dev/null @@ -1,58 +0,0 @@ -import Emitter from 'tiny-emitter'; -import checkSupport from './utils/checkSupport'; -import EventProxy from './eventProxy'; -import CreatWorker from './creatWorker'; -import CreatMediaSource from './creatMediaSource'; -import FlvParse from './flvParse'; -import * as utils from './utils'; -import config from './config'; - -let id = 0; -class Flv extends Emitter { - constructor(options) { - super(); - this.options = Object.assign({}, Flv.DEFAULTS, options); - checkSupport(this.options); - this.events = new EventProxy(this); - this.workers = new CreatWorker(this); - this.mediaSource = new CreatMediaSource(this); - this.flvData = new FlvParse(this); - id += 1; - this.id = id; - Flv.instances.push(this); - } - - static get DEFAULTS() { - return { - mediaElement: '', - url: '', - debug: false, - }; - } - - static get version() { - return '__VERSION__'; - } - - static get config() { - return config; - } - - static get utils() { - return utils; - } - - destroy() { - this.events.destroy(); - this.workers.killAll(); - Flv.instances.splice(Flv.instances.indexOf(this), 1); - this.emit('destroy'); - } -} - -Object.defineProperty(Flv, 'instances', { - value: [], -}); - -window.Flv = Flv; -export default Flv; diff --git a/packages/artplayer-plugin-flv/src/utils/checkSupport.js b/packages/artplayer-plugin-flv/src/utils/checkSupport.js deleted file mode 100644 index 060be1956..000000000 --- a/packages/artplayer-plugin-flv/src/utils/checkSupport.js +++ /dev/null @@ -1,24 +0,0 @@ -import { errorHandle } from '.'; - -export default function checkSupport(options) { - const { mediaElement, url } = options; - errorHandle(mediaElement instanceof HTMLVideoElement, 'The first parameter is not a video tag element'); - errorHandle( - typeof url === 'string' || (url instanceof File && url.type === 'video/x-flv'), - 'The second parameter is not a string type or flv file', - ); - - const MP4H264MimeCodec = 'video/mp4; codecs="avc1.42001E, mp4a.40.2"'; - const canPlay = mediaElement.canPlayType(MP4H264MimeCodec); - errorHandle( - window.MediaSource && - window.MediaSource.isTypeSupported(MP4H264MimeCodec) && - (canPlay === 'probably' || canPlay === 'maybe'), - `Unsupported MIME type or codec: ${MP4H264MimeCodec}`, - ); - - errorHandle(typeof window.Promise === 'function', "Unsupported 'Promise' method"); - errorHandle(typeof window.fetch === 'function', "Unsupported 'fetch' method"); - errorHandle(typeof window.Worker === 'function', "Unsupported 'Worker' method"); - errorHandle(typeof window.ReadableStream === 'function', "Unsupported 'ReadableStream' method"); -} diff --git a/packages/artplayer-plugin-flv/src/utils/flvError.js b/packages/artplayer-plugin-flv/src/utils/flvError.js deleted file mode 100644 index 9e04b1e8a..000000000 --- a/packages/artplayer-plugin-flv/src/utils/flvError.js +++ /dev/null @@ -1,9 +0,0 @@ -export default class FlvError extends Error { - constructor(message, context) { - super(message); - if (typeof Error.captureStackTrace === 'function') { - Error.captureStackTrace(this, context || this.constructor); - } - this.name = 'FlvError'; - } -} \ No newline at end of file diff --git a/packages/artplayer-plugin-flv/src/utils/index.js b/packages/artplayer-plugin-flv/src/utils/index.js deleted file mode 100644 index ea8f2a2e2..000000000 --- a/packages/artplayer-plugin-flv/src/utils/index.js +++ /dev/null @@ -1,63 +0,0 @@ -import FlvError from './flvError'; - -export function errorHandle(condition, msg) { - if (!condition) { - throw new FlvError(msg); - } -} - -export function mergeTypedArrays(a, b) { - const c = new a.constructor(a.length + b.length); - c.set(a); - c.set(b, a.length); - return c; -} - -export function sleep(ms = 0) { - return new Promise(resolve => setTimeout(resolve, ms)); -} - -export function getUint8Sum(arr) { - return arr.reduce((totle, num, index) => totle + num * 256 ** (arr.length - index - 1), 0); -} - -export function string2Bin(str) { - const result = []; - for (let i = 0; i < str.length; i += 1) { - result.push(Number(str.charCodeAt(i).toString(10))); - } - return result; -} - -export function bin2String(array) { - return String.fromCharCode.call(String, ...array); -} - -export function bin2Float(array) { - const view = new DataView(new ArrayBuffer(array.length)); - array.forEach((b, i) => { - view.setUint8(i, b); - }); - return view.getFloat64(0); -} - -export function bin2Boolean(bin) { - return bin === 1; -} - -export function log(name, msg = '') { - console.log(`[${name}] ${msg}`); -} - -export function readUint8(uint8) { - let index = 0; - return function read(length) { - const tempUint8 = new Uint8Array(length); - for (let i = 0; i < length; i += 1) { - tempUint8[i] = uint8[index]; - index += 1; - } - read.index = index; - return tempUint8; - }; -}