Skip to content

Commit 49a7b22

Browse files
committed
Updates
1 parent 3c8e872 commit 49a7b22

6 files changed

Lines changed: 252 additions & 64 deletions

File tree

bin/format.js

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { fileprefix, semver } from "../lib/meta.bin.js";
33
import { SemVer, gt } from "semver";
44
import { concat } from 'uint8arrays/concat';
55
import crc32 from 'crc-32';
6-
import { convertBase } from "../lib/third-party/convertBase.js";
7-
import { stringChunks, binToDec, decToBin } from "../lib/utils.js";
6+
import { binToDec, decToBin } from "../lib/utils.js";
7+
import { B64toUI8A, UI8AtoB64 } from "../lib/uint8.js";
88

99
function int8(n, le = true) {
1010
const buffer = new ArrayBuffer(1);
@@ -20,42 +20,6 @@ function version() {
2020
return concat([major, minor, patch]);
2121
}
2222

23-
function B64toUI8A(B64) {
24-
const convert = [0, 3, 2, 1];
25-
const add = convert[B64.length % 4];
26-
B64 = '0'.repeat(add) + B64;
27-
28-
const bin6 = [];
29-
for (let i = 0; i < B64.length; i++) {
30-
const bin = convertBase(B64[i], 64, 2);
31-
if (bin != null) bin6.push(bin.padStart(6, '0'));
32-
}
33-
34-
const bin8 = stringChunks(bin6.join(''), 8);
35-
const int8 = [add];
36-
for (let i = 0; i < bin8.length; i++) {
37-
int8.push(parseInt(bin8[i], 2));
38-
}
39-
40-
return new Uint8Array(int8);
41-
}
42-
43-
function UI8AtoB64(UI8A) {
44-
const remove = UI8A[0];
45-
const bin8 = [];
46-
for (let i = 1; i < UI8A.length; i++) {
47-
bin8.push(UI8A[i].toString(2).padStart(8, '0'));
48-
}
49-
50-
const bin6 = stringChunks(bin8.join(''), 6);
51-
const B64 = [];
52-
for (let i = 0; i < bin6.length; i++) {
53-
B64.push(convertBase(bin6[i], 2, 64));
54-
}
55-
56-
return B64.join('').slice(remove);
57-
}
58-
5923
function int32(n, le = true) {
6024
const buffer = new ArrayBuffer(4);
6125
const view = new DataView(buffer);

lib/third-party/convertBase.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const B64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
2+
export const URL = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
23
/**
34
* This is a modified https://stackoverflow.com/a/55011290 code. https://stackoverflow.com/a/55011290 -
45
* by Slavik Meltser (https://stackoverflow.com/users/1291121/slavik-meltser) -

lib/uint8.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { convertBase } from "./third-party/convertBase.js";
2+
import { stringChunks } from "./utils.js";
3+
4+
export function B64toUI8A(B64) {
5+
const convert = [0, 3, 2, 1];
6+
const add = convert[B64.length % 4];
7+
B64 = '0'.repeat(add) + B64;
8+
9+
const bin6 = [];
10+
for (let i = 0; i < B64.length; i++) {
11+
const bin = convertBase(B64[i], 64, 2);
12+
if (bin != null) bin6.push(bin.padStart(6, '0'));
13+
}
14+
15+
const bin8 = stringChunks(bin6.join(''), 8);
16+
const int8 = [add];
17+
for (let i = 0; i < bin8.length; i++) {
18+
int8.push(parseInt(bin8[i], 2));
19+
}
20+
21+
return new Uint8Array(int8);
22+
}
23+
24+
export function UI8AtoB64(UI8A) {
25+
const remove = UI8A[0];
26+
const bin8 = [];
27+
for (let i = 1; i < UI8A.length; i++) {
28+
bin8.push(UI8A[i].toString(2).padStart(8, '0'));
29+
}
30+
31+
const bin6 = stringChunks(bin8.join(''), 6);
32+
const B64 = [];
33+
for (let i = 0; i < bin6.length; i++) {
34+
B64.push(convertBase(bin6[i], 2, 64));
35+
}
36+
37+
return B64.join('').slice(remove);
38+
}

src/core.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { freqMap, freqMapSplitters } from './modes/freqMap.js';
1616
import { segments, splitGraphemes } from './modes/segmentation.js';
1717
import { _JSSC } from './encodings.js';
1818
import { compressSequences, decompressSequences } from './sequences.js';
19-
import { convertBase } from '../lib/third-party/convertBase.js';
19+
import { convertBase, URL as B64URL } from '../lib/third-party/convertBase.js';
2020
import { compressB64, decompressB64 } from './modes/base64.js';
2121
import { encode, decode } from '@strc/utf16-to-any-base';
2222
import utf8 from "utf8"; const { eUTF8 } = (()=>{
@@ -30,6 +30,7 @@ import lz from 'lz-string'; const { cLZ, dLZ } = (()=>{
3030
import { runInWorkers, canUseWorkers, workerURL, workerMin } from './useWorker.js';
3131
import { validateCache, setCache } from './cache.js';
3232
import { compress as cAXOR, decompress as dAXOR } from './modes/axor.js';
33+
import { B64toUI8A, UI8AtoB64 } from '../lib/uint8.js';
3334

3435
function cryptCharCode(
3536
code, get = false,
@@ -855,19 +856,33 @@ export async function decompress(str, stringify = false) {
855856
}
856857
}
857858

858-
export async function compressToBase64(...input) {
859-
const compressed = await compress(...input);
860-
861-
if (compressed instanceof JSSC) throw new Error(prefix+'Invalid options input.');
859+
function noDebugMode(result) {
860+
if (result instanceof JSSC) throw new Error(prefix+'Invalid options input.');
861+
return result;
862+
}
862863

864+
export async function compressToBase64(...input) {
865+
const compressed = noDebugMode(await compress(...input));
863866
return B64Padding(encode(compressed));
864867
}
865868
export async function decompressFromBase64(base64, ...params) {
866-
const decompressed = await decompress(decode(base64.replace(/=+$/, '')), ...params);
869+
return noDebugMode(await decompress(decode(base64.replace(/=+$/, '')), ...params));
870+
}
867871

868-
if (decompressed instanceof JSSC) throw new Error(prefix+'Invalid options input.');
872+
export async function compressToBase64URL(...input) {
873+
const compressed = noDebugMode(await compress(...input));
874+
return encode(compressed, 64, B64URL);
875+
}
876+
export async function decompressFromBase64URL(base64url, ...params) {
877+
return noDebugMode(await decompress(decode(base64url, 64, B64URL), ...params));
878+
}
869879

870-
return decompressed;
880+
export async function compressToUint8Array(...input) {
881+
const compressed = await compressToBase64(...input);
882+
return B64toUI8A(compressed.replace(/=+$/, ''));
883+
}
884+
export async function decompressFromUint8Array(uint8array, ...params) {
885+
return await decompressFromBase64(UI8AtoB64(uint8array), ...params);
871886
}
872887

873888
export async function compressLarge(input, ...params) {
@@ -883,12 +898,17 @@ export async function compressLarge(input, ...params) {
883898
return result.join('');
884899
}
885900
export async function compressLargeToBase64(...input) {
886-
const compressed = await compress(...input);
887-
888-
if (compressed instanceof JSSC) throw new Error(prefix+'Invalid options input.');
889-
901+
const compressed = noDebugMode(await compress(...input));
890902
return B64Padding(encode(compressed));
891903
}
904+
export async function compressLargeToBase64URL(...input) {
905+
const compressed = noDebugMode(await compress(...input));
906+
return encode(compressed, 64, B64URL);
907+
}
908+
export async function compressLargeToUint8Array(...input) {
909+
const compressed = await compressLargeToBase64(...input);
910+
return B64toUI8A(compressed.replace(/=+$/, ''));
911+
}
892912

893913
async function validate(compressed, originalInput) {
894914
const cached = validateCache.get(compressed);

0 commit comments

Comments
 (0)