@algorithm.ts/base64 3.0.0-alpha.2 → 3.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@algorithm.ts/base64",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "A Base64 encoding implementation.",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -16,8 +16,11 @@
16
16
  "algorithm",
17
17
  "base64"
18
18
  ],
19
- "main": "lib/cjs/index.js",
20
- "module": "lib/esm/index.js",
19
+ "type": "module",
20
+ "exports": {
21
+ "import": "./lib/esm/index.mjs",
22
+ "require": "./lib/cjs/index.cjs"
23
+ },
21
24
  "types": "lib/types/index.d.ts",
22
25
  "source": "src/index.ts",
23
26
  "license": "MIT",
@@ -26,18 +29,21 @@
26
29
  },
27
30
  "files": [
28
31
  "lib/",
29
- "!lib/**/*.js.map",
30
- "!lib/**/*.d.ts.map",
32
+ "src/",
31
33
  "package.json",
32
34
  "CHANGELOG.md",
33
35
  "LICENSE",
34
36
  "README.md"
35
37
  ],
36
38
  "scripts": {
37
- "build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
38
- "prebuild": "rimraf lib/",
39
+ "build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
39
40
  "prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
40
41
  "test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
41
42
  },
42
- "gitHead": "3d718fba94605e2fd60a3056b502e2e1376ca0ed"
43
+ "devDependencies": {
44
+ "cross-env": "^7.0.3",
45
+ "rimraf": "^3.0.2",
46
+ "rollup": "^3.9.1"
47
+ },
48
+ "gitHead": "d50b4725bde1b43474f490a29672db5aa1482ae3"
43
49
  }
package/src/base64.ts ADDED
@@ -0,0 +1,125 @@
1
+ export interface IBase64Props {
2
+ /**
3
+ * @default 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
4
+ */
5
+ CODES?: string
6
+ /**
7
+ * @default '=''
8
+ */
9
+ CODE_PADDING?: string
10
+ }
11
+
12
+ const sanitizeRegex = /([*^$\\])/g
13
+
14
+ export class Base64 {
15
+ protected readonly CODE_LIST: string[]
16
+ protected readonly CODE_REFLECT: Record<string, number>
17
+ protected readonly CODE_PADDING: string
18
+ protected readonly VALIDATE_REGEX: RegExp
19
+
20
+ constructor(props: IBase64Props = {}) {
21
+ const CODES =
22
+ props.CODES?.length === 64
23
+ ? props.CODES
24
+ : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
25
+ const CODE_PADDING = props.CODE_PADDING?.length === 1 ? props.CODE_PADDING : '='
26
+
27
+ this.CODE_LIST = CODES.split('')
28
+ this.CODE_REFLECT = this.CODE_LIST.reduce((acc, v, idx) => ({ ...acc, [v]: idx }), {
29
+ [CODE_PADDING]: 0,
30
+ })
31
+ this.VALIDATE_REGEX = new RegExp(
32
+ `^[${CODES.replace(sanitizeRegex, '\\$1')}]+${CODE_PADDING.replace(
33
+ sanitizeRegex,
34
+ '\\$1',
35
+ )}{0,2}$`,
36
+ )
37
+ this.CODE_PADDING = CODE_PADDING
38
+ }
39
+
40
+ public encode(data: Uint8Array): string {
41
+ let result = ''
42
+ const _end: number = Math.floor(data.length / 3) * 3
43
+ for (let i = 0; i < _end; i += 3) {
44
+ result += this._encodeUnit((data[i] << 16) | (data[i + 1] << 8) | data[i + 2])
45
+ }
46
+
47
+ const { CODE_PADDING } = this
48
+ switch (data.length - _end) {
49
+ case 1: {
50
+ const tail: string = this._encodeUnit(data[_end] << 16)
51
+ result += tail.slice(0, 2) + CODE_PADDING + CODE_PADDING
52
+ break
53
+ }
54
+ case 2: {
55
+ const tail: string = this._encodeUnit((data[_end] << 16) | (data[_end + 1] << 8))
56
+ result += tail.slice(0, 3) + CODE_PADDING
57
+ }
58
+ }
59
+ return result
60
+ }
61
+
62
+ public decode(text: string): Uint8Array | never {
63
+ if (text.length <= 0 || !this.validate(text)) {
64
+ throw new TypeError('Invalid base64 string.')
65
+ }
66
+
67
+ let countOfPadding = 0
68
+ const { CODE_PADDING } = this
69
+ for (let i = text.length - 1; i >= 0 && text[i] === CODE_PADDING; --i) countOfPadding += 1
70
+ const _size: number = (text.length >> 2) * 3 - countOfPadding
71
+ const result: Uint8Array = new Uint8Array(_size)
72
+
73
+ const _end: number = ((text.length - countOfPadding) >> 2) << 2
74
+ let i = 0
75
+ let j = 0
76
+ for (; i < _end; i += 4, j += 3) {
77
+ const v: number = this._decodeUint(text, i)
78
+ result[j] = v >> 16
79
+ result[j + 1] = (v >> 8) & 0xff
80
+ result[j + 2] = v & 0xff
81
+ }
82
+
83
+ if (countOfPadding > 0) {
84
+ const v: number = this._decodeUint(text, i)
85
+ switch (countOfPadding) {
86
+ case 1: {
87
+ result[j] = v >> 16
88
+ result[j + 1] = (v >> 8) & 0xff
89
+ break
90
+ }
91
+ case 2: {
92
+ result[j] = v >> 16
93
+ break
94
+ }
95
+ }
96
+ }
97
+ return result
98
+ }
99
+
100
+ public validate(text: string): boolean {
101
+ // The length of a Base64 encoded string should be a multiple of 4.
102
+ if (text.length & 3) return false
103
+ return this.VALIDATE_REGEX.test(text)
104
+ }
105
+
106
+ protected _encodeUnit(v: number): string {
107
+ const { CODE_LIST } = this
108
+ return (
109
+ CODE_LIST[(v >> 18) & 0x3f] +
110
+ CODE_LIST[(v >> 12) & 0x3f] +
111
+ CODE_LIST[(v >> 6) & 0x3f] +
112
+ CODE_LIST[v & 0x3f]
113
+ )
114
+ }
115
+
116
+ protected _decodeUint(text: string, startIdx: number): number {
117
+ const { CODE_REFLECT } = this
118
+ const v: number =
119
+ (CODE_REFLECT[text.charAt(startIdx)] << 18) |
120
+ (CODE_REFLECT[text.charAt(startIdx | 1)] << 12) |
121
+ (CODE_REFLECT[text.charAt(startIdx | 2)] << 6) |
122
+ CODE_REFLECT[text.charAt(startIdx | 3)]
123
+ return v
124
+ }
125
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { Base64 } from './base64'
2
+
3
+ export * from './base64'
4
+
5
+ const base64 = new Base64()
6
+
7
+ export const encode = base64.encode.bind(base64)
8
+ export const decode = base64.decode.bind(base64)
9
+ export const validate = base64.validate.bind(base64)
10
+ export default { encode, decode, validate }