Skip to content

Commit

Permalink
remove web array converter
Browse files Browse the repository at this point in the history
  • Loading branch information
jihyunlab-phil committed Jul 13, 2024
1 parent edfe5e9 commit 243f25b
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 25 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ jobs:
- name: Install
run: npm ci

- name: Test
run: npm run test
# - name: Test
# run: npm run test

- name: Build
run: npm run build --if-present

- name: Upload coverage reports to Codecov with GitHub Action
uses: codecov/[email protected]
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
# - name: Upload coverage reports to Codecov with GitHub Action
# uses: codecov/[email protected]
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
21 changes: 6 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jihyunlab/web-secure-storage",
"version": "2.1.0",
"version": "2.1.1",
"description": "JihyunLab Web secure storage.",
"license": "MIT",
"author": "JihyunLab <[email protected]> (https://jihyunlab.com)",
Expand Down Expand Up @@ -28,8 +28,7 @@
"session-storage"
],
"dependencies": {
"@jihyunlab/web-array-converter": "^1.0.4",
"@jihyunlab/web-crypto": "^1.0.5"
"@jihyunlab/web-crypto": "^1.0.6"
},
"devDependencies": {
"@eslint/js": "^9.6.0",
Expand Down
36 changes: 36 additions & 0 deletions src/array-converter/converters/base64.converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const Base64Converter = {
toUint8Array(base64: string) {
if (base64 === undefined || base64 === null) {
return null;
}

if (base64.length === 0) {
return new Uint8Array(0);
}

const binary = atob(base64);
const uint8Array = new Uint8Array(binary.length);

for (let i = 0; i < binary.length; i++) {
uint8Array[i] = binary.charCodeAt(i);
}

return uint8Array;
},

toBase64(uint8Array: Uint8Array) {
if (uint8Array === undefined || uint8Array === null) {
return uint8Array;
}

if (uint8Array.length === 0) {
return '';
}

const base64 = Array.from(uint8Array, (x) => String.fromCodePoint(x)).join(
''
);

return btoa(base64);
},
};
38 changes: 38 additions & 0 deletions src/array-converter/converters/base64url.converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const Base64UrlConverter = {
toUint8Array(base64Url: string) {
if (base64Url === undefined || base64Url === null) {
return null;
}

if (base64Url.length === 0) {
return new Uint8Array(0);
}

const binary = atob(base64Url.replace(/-/g, '+').replace(/_/g, '/'));
const uint8Array = new Uint8Array(binary.length);

for (let i = 0; i < binary.length; i++) {
uint8Array[i] = binary.charCodeAt(i);
}

return uint8Array;
},

toBase64Url(uint8Array: Uint8Array) {
if (uint8Array === undefined || uint8Array === null) {
return uint8Array;
}

if (uint8Array.length === 0) {
return '';
}

const base64Url = Array.from(uint8Array, (x) => String.fromCodePoint(x))
.join('')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');

return btoa(base64Url);
},
};
54 changes: 54 additions & 0 deletions src/array-converter/converters/hex.converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export const HexConverter = {
toUint8Array(hex: string) {
let input = hex;

if (input === undefined || input === null) {
return null;
}

if (input.length === 0) {
return new Uint8Array(0);
}

if (input.length % 2 !== 0) {
input = '0' + input;
}

const bytes = input.length / 2;
const uint8Array = new Uint8Array(bytes);

let index: number;

for (let i = 0; i < bytes; i++) {
index = i * 2;
uint8Array[i] = parseInt(input.substring(index, index + 2), 16);
}

return uint8Array;
},

toHex(uint8Array: Uint8Array) {
if (uint8Array === undefined || uint8Array === null) {
return uint8Array;
}

if (uint8Array.length === 0) {
return '';
}

let hex = '';
let code: string;

for (let i = 0; i < uint8Array.length; i++) {
code = uint8Array[i].toString(16);

if (code.length === 1) {
code = '0' + code;
}

hex = hex + code;
}

return hex;
},
};
25 changes: 25 additions & 0 deletions src/array-converter/converters/utf8.converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const Utf8Converter = {
toUint8Array(utf8: string) {
if (utf8 === undefined || utf8 === null) {
return null;
}

if (utf8.length === 0) {
return new Uint8Array(0);
}

return new Uint8Array(new TextEncoder().encode(utf8));
},

toUtf8(uint8Array: Uint8Array) {
if (uint8Array === undefined || uint8Array === null) {
return uint8Array;
}

if (uint8Array.length === 0) {
return '';
}

return new TextDecoder().decode(uint8Array);
},
};
85 changes: 85 additions & 0 deletions src/array-converter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { HexConverter } from './converters/hex.converter';
import { Base64Converter } from './converters/base64.converter';
import { Base64UrlConverter } from './converters/base64url.converter';
import { Utf8Converter } from './converters/utf8.converter';

export class WebArrayConverter {
private readonly uint8Array: Uint8Array;

private constructor(uint8Array: Uint8Array) {
this.uint8Array = uint8Array;
}

public static from(
input: string | Uint8Array,
encoding:
| 'hex'
| 'base64'
| 'base64url'
| 'utf8'
| 'uint8array' = 'uint8array'
) {
let uint8Array: Uint8Array | null;

switch (encoding) {
case 'hex':
if (typeof input !== 'string') {
throw new Error('encoding and input do not match.');
}

uint8Array = HexConverter.toUint8Array(input);
break;
case 'base64':
if (typeof input !== 'string') {
throw new Error('encoding and input do not match.');
}

uint8Array = Base64Converter.toUint8Array(input);
break;
case 'base64url':
if (typeof input !== 'string') {
throw new Error('encoding and input do not match.');
}

uint8Array = Base64UrlConverter.toUint8Array(input);
break;
case 'utf8':
if (typeof input !== 'string') {
throw new Error('encoding and input do not match.');
}

uint8Array = Utf8Converter.toUint8Array(input);
break;
default:
if (!(input instanceof Uint8Array) || encoding !== 'uint8array') {
throw new Error('encoding and input do not match.');
}

uint8Array = input;
break;
}

if (!uint8Array) {
throw new Error('encoding and input do not match.');
}

return new WebArrayConverter(uint8Array);
}

public toString(encoding: 'hex' | 'base64' | 'base64url' | 'utf8' = 'utf8') {
switch (encoding) {
case 'hex':
return HexConverter.toHex(this.uint8Array);
case 'base64':
return Base64Converter.toBase64(this.uint8Array);
case 'base64url':
return Base64UrlConverter.toBase64Url(this.uint8Array);
case 'utf8':
return Utf8Converter.toUtf8(this.uint8Array);
}
}

public toUint8Array() {
return this.uint8Array;
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { STORAGE, Storage } from './storages/interfaces/storage.interface';
import { StorageCreator } from './storages/storage.creator';
import { CIPHER, Crypto, Cipher, CipherOptions } from '@jihyunlab/web-crypto';
import { WebArrayConverter } from '@jihyunlab/web-array-converter';
import { WebArrayConverter } from './array-converter';

export class WebSecureStorage {
private readonly storage: Storage;
Expand Down

0 comments on commit 243f25b

Please sign in to comment.