encode-decode-codec 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +69 -0
- package/dist/constants.cjs +1 -0
- package/dist/constants.js +1 -0
- package/dist/core/codec.cjs +27 -0
- package/dist/core/codec.js +25 -0
- package/dist/core/options.cjs +26 -0
- package/dist/core/options.js +24 -0
- package/dist/index.cjs +11 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +12 -0
- package/dist/types.js +10 -0
- package/dist/utils/base64.cjs +9 -0
- package/dist/utils/base64.js +7 -0
- package/dist/utils/shuffle.cjs +47 -0
- package/dist/utils/shuffle.js +45 -0
- package/dist/validation/assert.cjs +13 -0
- package/dist/validation/assert.js +11 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# encode-decode-codec
|
|
2
|
+
|
|
3
|
+
Encode/decode helper package powered by [`position-unit-codec`](https://www.npmjs.com/package/position-unit-codec), with additional deterministic shuffling and optional Base64 wrapping.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install encode-decode-codec
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage (ESM)
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { encode, decode } from "encode-decode-codec";
|
|
15
|
+
|
|
16
|
+
const options = {
|
|
17
|
+
shift: 4,
|
|
18
|
+
unit: "_u_",
|
|
19
|
+
interval: 3,
|
|
20
|
+
shuffleSeed: 99,
|
|
21
|
+
useBase64: true
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const encoded = encode("hello world", options);
|
|
25
|
+
const decoded = decode(encoded, options);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage (CommonJS)
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
const { encode, decode } = require("encode-decode-codec");
|
|
32
|
+
|
|
33
|
+
const options = { shift: 4, unit: "_u_", interval: 3, shuffleSeed: 99 };
|
|
34
|
+
const encoded = encode("hello world", options);
|
|
35
|
+
const decoded = decode(encoded, options);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API
|
|
39
|
+
|
|
40
|
+
- `encode(text, options?) => string`
|
|
41
|
+
- `decode(encodedText, options?) => string`
|
|
42
|
+
|
|
43
|
+
Options:
|
|
44
|
+
|
|
45
|
+
- `shift` number (forwarded to `position-unit-codec`)
|
|
46
|
+
- `unit` string (forwarded to `position-unit-codec`)
|
|
47
|
+
- `interval` number > 0 (forwarded to `position-unit-codec`)
|
|
48
|
+
- `shuffleSeed` integer (default: `1337`) used for deterministic shuffle/unshuffle
|
|
49
|
+
- `useBase64` boolean (default: `true`) wraps shuffled output with Base64
|
|
50
|
+
|
|
51
|
+
Use the same options in `decode` that you used in `encode`.
|
|
52
|
+
|
|
53
|
+
## Publish
|
|
54
|
+
|
|
55
|
+
1. Update `name`, `repository.url`, and license details in `package.json`.
|
|
56
|
+
2. Install dependencies:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
3. Login and publish:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
npm login
|
|
66
|
+
npm publish --access public
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports.DEFAULT_SHUFFLE_SEED = 1337;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_SHUFFLE_SEED = 1337;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const { encode: baseEncode, decode: baseDecode } = require("position-unit-codec");
|
|
2
|
+
const { toBase64, fromBase64 } = require("../utils/base64.cjs");
|
|
3
|
+
const { shuffleText, unshuffleText } = require("../utils/shuffle.cjs");
|
|
4
|
+
const { assertString } = require("../validation/assert.cjs");
|
|
5
|
+
const { normalizeOptions } = require("./options.cjs");
|
|
6
|
+
|
|
7
|
+
function encodeWithLayers(text, options = {}) {
|
|
8
|
+
assertString(text, "text");
|
|
9
|
+
|
|
10
|
+
const normalized = normalizeOptions(options);
|
|
11
|
+
const positionEncoded = baseEncode(text, normalized.codecOptions);
|
|
12
|
+
const shuffled = shuffleText(positionEncoded, normalized.shuffleSeed);
|
|
13
|
+
|
|
14
|
+
return normalized.useBase64 ? toBase64(shuffled) : shuffled;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function decodeWithLayers(encodedText, options = {}) {
|
|
18
|
+
assertString(encodedText, "encodedText");
|
|
19
|
+
|
|
20
|
+
const normalized = normalizeOptions(options);
|
|
21
|
+
const unwrapped = normalized.useBase64 ? fromBase64(encodedText) : encodedText;
|
|
22
|
+
const unshuffled = unshuffleText(unwrapped, normalized.shuffleSeed);
|
|
23
|
+
|
|
24
|
+
return baseDecode(unshuffled, normalized.codecOptions);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = { encodeWithLayers, decodeWithLayers };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { encode as baseEncode, decode as baseDecode } from "position-unit-codec";
|
|
2
|
+
import { toBase64, fromBase64 } from "../utils/base64.js";
|
|
3
|
+
import { shuffleText, unshuffleText } from "../utils/shuffle.js";
|
|
4
|
+
import { assertString } from "../validation/assert.js";
|
|
5
|
+
import { normalizeOptions } from "./options.js";
|
|
6
|
+
|
|
7
|
+
export function encodeWithLayers(text, options = {}) {
|
|
8
|
+
assertString(text, "text");
|
|
9
|
+
|
|
10
|
+
const normalized = normalizeOptions(options);
|
|
11
|
+
const positionEncoded = baseEncode(text, normalized.codecOptions);
|
|
12
|
+
const shuffled = shuffleText(positionEncoded, normalized.shuffleSeed);
|
|
13
|
+
|
|
14
|
+
return normalized.useBase64 ? toBase64(shuffled) : shuffled;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function decodeWithLayers(encodedText, options = {}) {
|
|
18
|
+
assertString(encodedText, "encodedText");
|
|
19
|
+
|
|
20
|
+
const normalized = normalizeOptions(options);
|
|
21
|
+
const unwrapped = normalized.useBase64 ? fromBase64(encodedText) : encodedText;
|
|
22
|
+
const unshuffled = unshuffleText(unwrapped, normalized.shuffleSeed);
|
|
23
|
+
|
|
24
|
+
return baseDecode(unshuffled, normalized.codecOptions);
|
|
25
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { DEFAULT_SHUFFLE_SEED } = require("../constants.cjs");
|
|
2
|
+
const { assertOptionsObject } = require("../validation/assert.cjs");
|
|
3
|
+
|
|
4
|
+
function normalizeOptions(options = {}) {
|
|
5
|
+
assertOptionsObject(options);
|
|
6
|
+
|
|
7
|
+
if (options.shuffleSeed !== undefined && !Number.isInteger(options.shuffleSeed)) {
|
|
8
|
+
throw new TypeError("options.shuffleSeed must be an integer.");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (options.useBase64 !== undefined && typeof options.useBase64 !== "boolean") {
|
|
12
|
+
throw new TypeError("options.useBase64 must be a boolean.");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
codecOptions: {
|
|
17
|
+
shift: options.shift,
|
|
18
|
+
unit: options.unit,
|
|
19
|
+
interval: options.interval
|
|
20
|
+
},
|
|
21
|
+
shuffleSeed: Number.isInteger(options.shuffleSeed) ? options.shuffleSeed : DEFAULT_SHUFFLE_SEED,
|
|
22
|
+
useBase64: options.useBase64 !== false
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = { normalizeOptions };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DEFAULT_SHUFFLE_SEED } from "../constants.js";
|
|
2
|
+
import { assertOptionsObject } from "../validation/assert.js";
|
|
3
|
+
|
|
4
|
+
export function normalizeOptions(options = {}) {
|
|
5
|
+
assertOptionsObject(options);
|
|
6
|
+
|
|
7
|
+
if (options.shuffleSeed !== undefined && !Number.isInteger(options.shuffleSeed)) {
|
|
8
|
+
throw new TypeError("options.shuffleSeed must be an integer.");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (options.useBase64 !== undefined && typeof options.useBase64 !== "boolean") {
|
|
12
|
+
throw new TypeError("options.useBase64 must be a boolean.");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
codecOptions: {
|
|
17
|
+
shift: options.shift,
|
|
18
|
+
unit: options.unit,
|
|
19
|
+
interval: options.interval
|
|
20
|
+
},
|
|
21
|
+
shuffleSeed: Number.isInteger(options.shuffleSeed) ? options.shuffleSeed : DEFAULT_SHUFFLE_SEED,
|
|
22
|
+
useBase64: options.useBase64 !== false
|
|
23
|
+
};
|
|
24
|
+
}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const { encodeWithLayers, decodeWithLayers } = require("./core/codec.cjs");
|
|
2
|
+
|
|
3
|
+
function encode(text, options = {}) {
|
|
4
|
+
return encodeWithLayers(text, options);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function decode(encodedText, options = {}) {
|
|
8
|
+
return decodeWithLayers(encodedText, options);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = { encode, decode, default: { encode, decode } };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type CodecOptions = {
|
|
2
|
+
shift?: number;
|
|
3
|
+
unit?: string;
|
|
4
|
+
interval?: number;
|
|
5
|
+
shuffleSeed?: number;
|
|
6
|
+
useBase64?: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export declare function encode(text: string, options?: CodecOptions): string;
|
|
10
|
+
export declare function decode(encodedText: string, options?: CodecOptions): string;
|
|
11
|
+
|
|
12
|
+
declare const _default: {
|
|
13
|
+
encode: typeof encode;
|
|
14
|
+
decode: typeof decode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "./types.js";
|
|
2
|
+
import { encodeWithLayers, decodeWithLayers } from "./core/codec.js";
|
|
3
|
+
|
|
4
|
+
export function encode(text, options = {}) {
|
|
5
|
+
return encodeWithLayers(text, options);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function decode(encodedText, options = {}) {
|
|
9
|
+
return decodeWithLayers(encodedText, options);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default { encode, decode };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
function createSeededRng(seed) {
|
|
2
|
+
let state = Math.imul(seed ^ 0x9e3779b9, 1597334677) >>> 0;
|
|
3
|
+
return function next() {
|
|
4
|
+
state ^= state << 13;
|
|
5
|
+
state ^= state >>> 17;
|
|
6
|
+
state ^= state << 5;
|
|
7
|
+
return (state >>> 0) / 4294967296;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function buildPermutation(length, seed) {
|
|
12
|
+
const rng = createSeededRng(seed >>> 0);
|
|
13
|
+
const indexes = Array.from({ length }, (_, i) => i);
|
|
14
|
+
|
|
15
|
+
for (let i = indexes.length - 1; i > 0; i -= 1) {
|
|
16
|
+
const j = Math.floor(rng() * (i + 1));
|
|
17
|
+
const tmp = indexes[i];
|
|
18
|
+
indexes[i] = indexes[j];
|
|
19
|
+
indexes[j] = tmp;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return indexes;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function shuffleText(input, seed) {
|
|
26
|
+
if (input.length <= 1) return input;
|
|
27
|
+
|
|
28
|
+
const chars = Array.from(input);
|
|
29
|
+
const permutation = buildPermutation(chars.length, seed);
|
|
30
|
+
return permutation.map((idx) => chars[idx]).join("");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function unshuffleText(input, seed) {
|
|
34
|
+
if (input.length <= 1) return input;
|
|
35
|
+
|
|
36
|
+
const chars = Array.from(input);
|
|
37
|
+
const permutation = buildPermutation(chars.length, seed);
|
|
38
|
+
const restored = new Array(chars.length);
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < permutation.length; i += 1) {
|
|
41
|
+
restored[permutation[i]] = chars[i];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return restored.join("");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = { shuffleText, unshuffleText };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
function createSeededRng(seed) {
|
|
2
|
+
let state = Math.imul(seed ^ 0x9e3779b9, 1597334677) >>> 0;
|
|
3
|
+
return function next() {
|
|
4
|
+
state ^= state << 13;
|
|
5
|
+
state ^= state >>> 17;
|
|
6
|
+
state ^= state << 5;
|
|
7
|
+
return (state >>> 0) / 4294967296;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function buildPermutation(length, seed) {
|
|
12
|
+
const rng = createSeededRng(seed >>> 0);
|
|
13
|
+
const indexes = Array.from({ length }, (_, i) => i);
|
|
14
|
+
|
|
15
|
+
for (let i = indexes.length - 1; i > 0; i -= 1) {
|
|
16
|
+
const j = Math.floor(rng() * (i + 1));
|
|
17
|
+
const tmp = indexes[i];
|
|
18
|
+
indexes[i] = indexes[j];
|
|
19
|
+
indexes[j] = tmp;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return indexes;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function shuffleText(input, seed) {
|
|
26
|
+
if (input.length <= 1) return input;
|
|
27
|
+
|
|
28
|
+
const chars = Array.from(input);
|
|
29
|
+
const permutation = buildPermutation(chars.length, seed);
|
|
30
|
+
return permutation.map((idx) => chars[idx]).join("");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function unshuffleText(input, seed) {
|
|
34
|
+
if (input.length <= 1) return input;
|
|
35
|
+
|
|
36
|
+
const chars = Array.from(input);
|
|
37
|
+
const permutation = buildPermutation(chars.length, seed);
|
|
38
|
+
const restored = new Array(chars.length);
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < permutation.length; i += 1) {
|
|
41
|
+
restored[permutation[i]] = chars[i];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return restored.join("");
|
|
45
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function assertString(value, name) {
|
|
2
|
+
if (typeof value !== "string") {
|
|
3
|
+
throw new TypeError(`${name} must be a string.`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function assertOptionsObject(value) {
|
|
8
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
9
|
+
throw new TypeError("options must be an object.");
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = { assertString, assertOptionsObject };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function assertString(value, name) {
|
|
2
|
+
if (typeof value !== "string") {
|
|
3
|
+
throw new TypeError(`${name} must be a string.`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function assertOptionsObject(value) {
|
|
8
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
9
|
+
throw new TypeError("options must be an object.");
|
|
10
|
+
}
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "encode-decode-codec",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Encode/decode helper package powered by position-unit-codec.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "node scripts/build.mjs",
|
|
23
|
+
"test": "node test/run-all.mjs",
|
|
24
|
+
"prepublishOnly": "npm run build && npm test"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"encode",
|
|
28
|
+
"decode",
|
|
29
|
+
"codec",
|
|
30
|
+
"position-unit-codec"
|
|
31
|
+
],
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/yourname/encode-decode-codec.git"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"position-unit-codec": "^1.0.0"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|