@vercel/stega 0.0.2 → 0.0.4

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.
@@ -1,25 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.vercelStegaDecode = exports.VERCEL_STEGA_REGEX = void 0;
3
+ exports.vercelStegaDecodeAll = exports.vercelStegaDecode = exports.VERCEL_STEGA_REGEX = void 0;
4
4
  const map_1 = require("./map");
5
5
  const REVERSE_HEX_DIGIT_MAP = Object.fromEntries(Object.entries(map_1.HEX_DIGIT_MAP).map((x) => x.reverse()));
6
6
  const HEX_CHARS = `${Object.values(map_1.HEX_DIGIT_MAP)
7
7
  .map((x) => `\\u{${x.toString(16)}}`)
8
8
  .join('')}`;
9
- exports.VERCEL_STEGA_REGEX = new RegExp(`(?:[${HEX_CHARS}]{2})+`, 'u');
9
+ exports.VERCEL_STEGA_REGEX = new RegExp(`(?:[${HEX_CHARS}]{2})+`, 'gu');
10
10
  /**
11
- * Decodes a hidden string back into its original value
12
- * @param encoded - The encoded (hidden) string
11
+ * Decodes the first hidden string that's found in the source string back into its original value
12
+ * @param source - The source string with encoded data
13
13
  * @returns The decoded JSON value
14
14
  */
15
- function vercelStegaDecode(encoded) {
16
- const match = encoded.match(exports.VERCEL_STEGA_REGEX);
17
- if (!match)
15
+ function vercelStegaDecode(source) {
16
+ const matches = source.match(exports.VERCEL_STEGA_REGEX);
17
+ if (!matches)
18
18
  return;
19
- return decode(match[0]);
19
+ return decode(matches[0], true)[0];
20
20
  }
21
21
  exports.vercelStegaDecode = vercelStegaDecode;
22
- function decode(encodedString) {
22
+ /**
23
+ * Decodes every hidden string that's found in the source string back into their original values
24
+ * @param source - The source string with encoded data
25
+ * @returns The decoded JSON values
26
+ */
27
+ function vercelStegaDecodeAll(source) {
28
+ const matches = source.match(exports.VERCEL_STEGA_REGEX);
29
+ if (!matches)
30
+ return;
31
+ return matches.map((match) => decode(match)).flat();
32
+ }
33
+ exports.vercelStegaDecodeAll = vercelStegaDecodeAll;
34
+ function decode(encodedString, firstOnly = false) {
35
+ var _a;
23
36
  const encoded = Array.from(encodedString);
24
37
  if (encoded.length % 2) {
25
38
  throw new Error(`Invalid encoded text length. Expected length to be even, received: ${encoded.length}`);
@@ -29,5 +42,24 @@ function decode(encodedString) {
29
42
  const hex = `${REVERSE_HEX_DIGIT_MAP[encoded[i * 2].codePointAt(0)]}${REVERSE_HEX_DIGIT_MAP[encoded[i * 2 + 1].codePointAt(0)]}`;
30
43
  chars.unshift(String.fromCharCode(parseInt(hex, 16)));
31
44
  }
32
- return JSON.parse(chars.join(''));
45
+ const results = [];
46
+ const queue = [chars.join('')];
47
+ let breakLimit = 10;
48
+ while (queue.length) {
49
+ const string = queue.shift();
50
+ try {
51
+ results.push(JSON.parse(string));
52
+ if (firstOnly)
53
+ return results;
54
+ }
55
+ catch (error) {
56
+ if (!breakLimit--)
57
+ throw error;
58
+ const position = +((_a = error.message.match(/\sposition\s(\d+)$/)) === null || _a === void 0 ? void 0 : _a[1]);
59
+ if (!position)
60
+ throw error;
61
+ queue.unshift(string.substring(0, position), string.substring(position));
62
+ }
63
+ }
64
+ return results;
33
65
  }
package/dist/cjs/index.js CHANGED
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./encode"), exports);
18
18
  __exportStar(require("./decode"), exports);
19
+ __exportStar(require("./util"), exports);
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vercelStegaSplit = void 0;
4
+ const decode_1 = require("./decode");
5
+ /**
6
+ * Splits out encoded data from a string, if any is found
7
+ * @param original - The original string
8
+ * @returns The cleaned string and encoded data, separately
9
+ */
10
+ function vercelStegaSplit(original) {
11
+ var _a;
12
+ return {
13
+ cleaned: original.replace(decode_1.VERCEL_STEGA_REGEX, ''),
14
+ encoded: ((_a = original.match(decode_1.VERCEL_STEGA_REGEX)) === null || _a === void 0 ? void 0 : _a[0]) || '',
15
+ };
16
+ }
17
+ exports.vercelStegaSplit = vercelStegaSplit;
package/dist/decode.d.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  export declare const VERCEL_STEGA_REGEX: RegExp;
2
2
  /**
3
- * Decodes a hidden string back into its original value
4
- * @param encoded - The encoded (hidden) string
3
+ * Decodes the first hidden string that's found in the source string back into its original value
4
+ * @param source - The source string with encoded data
5
5
  * @returns The decoded JSON value
6
6
  */
7
- export declare function vercelStegaDecode<T>(encoded: string): T | undefined;
7
+ export declare function vercelStegaDecode<T>(source: string): T | undefined;
8
+ /**
9
+ * Decodes every hidden string that's found in the source string back into their original values
10
+ * @param source - The source string with encoded data
11
+ * @returns The decoded JSON values
12
+ */
13
+ export declare function vercelStegaDecodeAll<T>(source: string): T[];
@@ -3,19 +3,31 @@ const REVERSE_HEX_DIGIT_MAP = Object.fromEntries(Object.entries(HEX_DIGIT_MAP).m
3
3
  const HEX_CHARS = `${Object.values(HEX_DIGIT_MAP)
4
4
  .map((x) => `\\u{${x.toString(16)}}`)
5
5
  .join('')}`;
6
- export const VERCEL_STEGA_REGEX = new RegExp(`(?:[${HEX_CHARS}]{2})+`, 'u');
6
+ export const VERCEL_STEGA_REGEX = new RegExp(`(?:[${HEX_CHARS}]{2})+`, 'gu');
7
7
  /**
8
- * Decodes a hidden string back into its original value
9
- * @param encoded - The encoded (hidden) string
8
+ * Decodes the first hidden string that's found in the source string back into its original value
9
+ * @param source - The source string with encoded data
10
10
  * @returns The decoded JSON value
11
11
  */
12
- export function vercelStegaDecode(encoded) {
13
- const match = encoded.match(VERCEL_STEGA_REGEX);
14
- if (!match)
12
+ export function vercelStegaDecode(source) {
13
+ const matches = source.match(VERCEL_STEGA_REGEX);
14
+ if (!matches)
15
15
  return;
16
- return decode(match[0]);
16
+ return decode(matches[0], true)[0];
17
17
  }
18
- function decode(encodedString) {
18
+ /**
19
+ * Decodes every hidden string that's found in the source string back into their original values
20
+ * @param source - The source string with encoded data
21
+ * @returns The decoded JSON values
22
+ */
23
+ export function vercelStegaDecodeAll(source) {
24
+ const matches = source.match(VERCEL_STEGA_REGEX);
25
+ if (!matches)
26
+ return;
27
+ return matches.map((match) => decode(match)).flat();
28
+ }
29
+ function decode(encodedString, firstOnly = false) {
30
+ var _a;
19
31
  const encoded = Array.from(encodedString);
20
32
  if (encoded.length % 2) {
21
33
  throw new Error(`Invalid encoded text length. Expected length to be even, received: ${encoded.length}`);
@@ -25,5 +37,24 @@ function decode(encodedString) {
25
37
  const hex = `${REVERSE_HEX_DIGIT_MAP[encoded[i * 2].codePointAt(0)]}${REVERSE_HEX_DIGIT_MAP[encoded[i * 2 + 1].codePointAt(0)]}`;
26
38
  chars.unshift(String.fromCharCode(parseInt(hex, 16)));
27
39
  }
28
- return JSON.parse(chars.join(''));
40
+ const results = [];
41
+ const queue = [chars.join('')];
42
+ let breakLimit = 10;
43
+ while (queue.length) {
44
+ const string = queue.shift();
45
+ try {
46
+ results.push(JSON.parse(string));
47
+ if (firstOnly)
48
+ return results;
49
+ }
50
+ catch (error) {
51
+ if (!breakLimit--)
52
+ throw error;
53
+ const position = +((_a = error.message.match(/\sposition\s(\d+)$/)) === null || _a === void 0 ? void 0 : _a[1]);
54
+ if (!position)
55
+ throw error;
56
+ queue.unshift(string.substring(0, position), string.substring(position));
57
+ }
58
+ }
59
+ return results;
29
60
  }
package/dist/esm/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './encode';
2
2
  export * from './decode';
3
+ export * from './util';
@@ -0,0 +1,13 @@
1
+ import { VERCEL_STEGA_REGEX } from './decode';
2
+ /**
3
+ * Splits out encoded data from a string, if any is found
4
+ * @param original - The original string
5
+ * @returns The cleaned string and encoded data, separately
6
+ */
7
+ export function vercelStegaSplit(original) {
8
+ var _a;
9
+ return {
10
+ cleaned: original.replace(VERCEL_STEGA_REGEX, ''),
11
+ encoded: ((_a = original.match(VERCEL_STEGA_REGEX)) === null || _a === void 0 ? void 0 : _a[0]) || '',
12
+ };
13
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './encode';
2
2
  export * from './decode';
3
+ export * from './util';
package/dist/util.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Separated clean string and encoded string
3
+ */
4
+ interface SplitResult {
5
+ /** The original string with encoded substring removed */
6
+ cleaned: string;
7
+ /** The encoded substring from the original string */
8
+ encoded: string;
9
+ }
10
+ /**
11
+ * Splits out encoded data from a string, if any is found
12
+ * @param original - The original string
13
+ * @returns The cleaned string and encoded data, separately
14
+ */
15
+ export declare function vercelStegaSplit(original: string): SplitResult;
16
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/stega",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Utilities for steganography",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -10,18 +10,22 @@
10
10
  "dist"
11
11
  ],
12
12
  "license": "MPL-2.0",
13
- "scripts": {
14
- "dev": "npm run build",
15
- "build": "npm run build:types && npm run build:esm && npm run build:cjs",
16
- "build:esm": "tsc --module es2020 --outDir dist/esm",
17
- "build:cjs": "tsc --module commonjs --outDir dist/cjs",
18
- "build:types": "tsc --declaration --emitDeclarationOnly",
19
- "test": "jest"
20
- },
21
13
  "devDependencies": {
22
14
  "@jest/globals": "^28.1.3",
23
15
  "jest": "^28.1.3",
24
16
  "ts-jest": "^28.0.3",
25
17
  "typescript": "^4.6.3"
18
+ },
19
+ "access": "public",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "scripts": {
24
+ "dev": "pnpm build",
25
+ "build": "pnpm build:types && pnpm build:esm && pnpm build:cjs",
26
+ "build:esm": "tsc --module es2020 --outDir dist/esm",
27
+ "build:cjs": "tsc --module commonjs --outDir dist/cjs",
28
+ "build:types": "tsc --declaration --emitDeclarationOnly",
29
+ "test": "jest"
26
30
  }
27
- }
31
+ }