master
yutent 2024-05-30 12:01:34 +08:00
parent 093392be0d
commit 11f33335bc
6 changed files with 143 additions and 2 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
(The MIT License)
Copyright (C) 2014-2017 by Vitaly Puzrin and Andrei Tuputcyn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

6
README.md Normal file
View File

@ -0,0 +1,6 @@
## Buffer
浏览器版的Buffer对象模拟, 提供近似Node.js的BUffer对象的API
[![@bytedo/buffer](https://img.shields.io/npm/v/@bytedo/buffer.svg)](https://www.npmjs.com/package/@bytedo/buffer)

16
build.js Normal file
View File

@ -0,0 +1,16 @@
/**
* {build}
* @author yutent<yutent.io@gmail.com>
* @date 2021/08/09 11:59:41
*/
import Es from 'esbuild'
Es.build({
entryPoints: ['src/index.js'],
outdir: 'dist',
target: 'es2017',
format: 'esm',
bundle: true,
minify: true
})

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "@bytedo/buffer",
"version": "1.0.0",
"type": "module",
"main": "dist/index.js",
"files": [
"dist/*"
],
"description": "Buffer浏览器版本,提供常用的API封装",
"keywords": [
"Buffer",
"base64"
],
"author": "Yutent <yutent.io@gmail.com>",
"scripts": {
"start": "node ./build.js"
},
"repository": {
"type": "git",
"url": "https://git.wkit.fun/bytedo/buffer"
},
"license": "MIT"
}

View File

@ -45,3 +45,9 @@ export function hex2u8(str) {
export function sum(list) {
return list.reduce((total, it) => total + it.length, 0)
}
export function compare(buf1, buf2) {
let s1 = buf1 + ''
let s2 = buf2 + ''
return s1.localeCompare(s2)
}

View File

@ -4,7 +4,15 @@
* @date 2024/05/29 12:29:17
*/
import { encode, decode, getType, to16, hex2u8, sum } from './helper.js'
import {
encode,
decode,
getType,
to16,
hex2u8,
sum,
compare
} from './helper.js'
import { base64encode, base64decode, str2bin, bin2str } from './base64.js'
/**
@ -28,7 +36,7 @@ function any2u8(data = '', encoding = 'utf8') {
}
return new Uint8Array(data)
} else {
switch (encoding) {
switch (encoding.toLowerCase()) {
case 'base64':
data = base64decode(data)
break
@ -53,6 +61,19 @@ export default class Buffer extends Uint8Array {
return 'Buffer'
}
/**
* 返回指定内容的字节数
*/
static byteLength(data, encoding = 'utf8') {
let u8 = any2u8(data, encoding)
return u8.length
}
/**
* 从给定的内容中创建Buffer对象
* @param data <any>
* @return <Buffer>
*/
static from(data, offsetOrEncoding, end) {
let buf = data
let start = 0
@ -71,6 +92,12 @@ export default class Buffer extends Uint8Array {
return new Buffer(buf.slice(start, end))
}
/**
* 从给定的内容中创建Buffer对象
* @param len <Int> Buffer长度
* @param str <String> 创建Buffer时, 填充的内容
* @return <Buffer>
*/
static alloc(len, str = '', encoding) {
let buf = new Buffer(len)
if (str) {
@ -79,6 +106,9 @@ export default class Buffer extends Uint8Array {
return buf
}
/**
* 合并多个Buffer对象
*/
static concat(list, encoding) {
let len = sum(list)
let buf = Buffer.alloc(len)
@ -92,10 +122,21 @@ export default class Buffer extends Uint8Array {
return buf
}
/**
* 判断是否为Buffer对象
*/
static isBuffer(target) {
return getType(target) === 'Buffer'
}
/**
* 比较2个对象
*/
static compare = compare
/**
* 替换指定字节内容, fill, copy, write, 都会用到这个方法
*/
#replace(target, start, end) {
for (let i = 0; i < end - start; i++) {
this[start + i] = target[i]
@ -195,4 +236,32 @@ export default class Buffer extends Uint8Array {
}
return written
}
/**
* 与目标对象比较
*/
compare(buf) {
return compare(this, buf)
}
/**
* 判断2个Buffer对象是否相等
*/
equals(buf) {
return this.toString() === buf.toString()
}
/**
* 判断是否包含指定内容
* 原生的includes只支持bytes, 这里拓展支持完整的类型
*/
includes(data, offset = 0, encoding = 'utf8') {
if (typeof data === 'number') {
return super.includes(data, offset)
}
let u8 = any2u8(data, encoding)
let str = decode(u8)
return this.toString().indexOf(str, offset) > -1
}
}