@vercel/stega 0.0.4 → 0.0.5

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/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # @vercel/stega
2
+
3
+ A simple [steganography](https://en.wikipedia.org/wiki/Steganography) library for adding hidden JSON data to strings.
4
+
5
+ ## Usage
6
+
7
+ This package exports a few methods for encoding and decoding data. All of these methods have TypeScript type definitions and JSDoc comments explaining their parameters and return values.
8
+
9
+ ### `vercelStegaCombine(string, json, skip = 'auto')`
10
+
11
+ This method combines a `string` with some JSON data and returns the result. The `json` can be any value that can be JSON stringified.
12
+
13
+ When the `skip` property is `true`, the original string will be returned without combining the `json`. It supports `boolean` values and `'auto'`. The default is `'auto'`, which will only skip encoding when the `string` is an ISO date string or a URL.
14
+
15
+ ```js
16
+ vercelStegaCombine('Hello world', { foo: 'bar' });
17
+ // -> 'Hello world' (the JSON data is hidden in the new string)
18
+ ```
19
+
20
+ ### `vercelStegaEncode(json)`
21
+
22
+ This method encodes JSON data as a hidden string and returns the result. The `json` can be any value that can be JSON stringified.
23
+
24
+ ```js
25
+ vercelStegaEncode({ foo: 'bar' });
26
+ // -> '' (the JSON data is hidden)
27
+ ```
28
+
29
+ ### `vercelStegaSplit(string)`
30
+
31
+ This method splits out the original string (cleaned) and the encoded portion of the string.
32
+
33
+ ```js
34
+ // In 'Hello world' (the extra data is hidden)
35
+ vercelStegaSplit('Hello world');
36
+ /*
37
+ * -> {
38
+ * cleaned: 'Hello world', // This doesn't contains the encoded data
39
+ * encoded: '', // This is not an empty string, it contains the encoded data
40
+ * }
41
+ */
42
+ ```
43
+
44
+ ### `vercelStegaDecode(string)`
45
+
46
+ This method attempts to extract encoded JSON data from a `string`.
47
+
48
+ ```js
49
+ // In 'Hello world' (the extra data is hidden)
50
+ vercelStegaDecode('Hello world');
51
+ // -> { foo: 'bar' }
52
+ ```
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.vercelStegaEncode = void 0;
3
+ exports.vercelStegaCombine = exports.vercelStegaEncode = void 0;
4
4
  const map_1 = require("./map");
5
5
  /**
6
6
  * Encodes JSON as a hidden string
@@ -22,3 +22,29 @@ function vercelStegaEncode(json) {
22
22
  .join('');
23
23
  }
24
24
  exports.vercelStegaEncode = vercelStegaEncode;
25
+ function isDate(string) {
26
+ return Boolean(Date.parse(string));
27
+ }
28
+ function isUrl(string) {
29
+ try {
30
+ new URL(string);
31
+ }
32
+ catch {
33
+ return false;
34
+ }
35
+ return true;
36
+ }
37
+ /**
38
+ * Adds an encoded JSON object to a string as hidden characters
39
+ * @param string - The string the JSON will be added to
40
+ * @param json - The JSON to add to the string
41
+ * @param skip - Whether to skip encoding (default: "auto")
42
+ */
43
+ function vercelStegaCombine(string, json, skip = 'auto') {
44
+ if (skip === true)
45
+ return string;
46
+ if (skip === 'auto' && (isDate(string) || isUrl(string)))
47
+ return string;
48
+ return `${string}${vercelStegaEncode(json)}`;
49
+ }
50
+ exports.vercelStegaCombine = vercelStegaCombine;
package/dist/encode.d.ts CHANGED
@@ -4,3 +4,12 @@
4
4
  * @returns The hidden string
5
5
  */
6
6
  export declare function vercelStegaEncode<T>(json: T): string;
7
+ type SkipValue = 'auto' | boolean;
8
+ /**
9
+ * Adds an encoded JSON object to a string as hidden characters
10
+ * @param string - The string the JSON will be added to
11
+ * @param json - The JSON to add to the string
12
+ * @param skip - Whether to skip encoding (default: "auto")
13
+ */
14
+ export declare function vercelStegaCombine<T>(string: string, json: T, skip?: SkipValue): string;
15
+ export {};
@@ -18,3 +18,28 @@ export function vercelStegaEncode(json) {
18
18
  })
19
19
  .join('');
20
20
  }
21
+ function isDate(string) {
22
+ return Boolean(Date.parse(string));
23
+ }
24
+ function isUrl(string) {
25
+ try {
26
+ new URL(string);
27
+ }
28
+ catch {
29
+ return false;
30
+ }
31
+ return true;
32
+ }
33
+ /**
34
+ * Adds an encoded JSON object to a string as hidden characters
35
+ * @param string - The string the JSON will be added to
36
+ * @param json - The JSON to add to the string
37
+ * @param skip - Whether to skip encoding (default: "auto")
38
+ */
39
+ export function vercelStegaCombine(string, json, skip = 'auto') {
40
+ if (skip === true)
41
+ return string;
42
+ if (skip === 'auto' && (isDate(string) || isUrl(string)))
43
+ return string;
44
+ return `${string}${vercelStegaEncode(json)}`;
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/stega",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Utilities for steganography",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",