@keyv/serialize 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 ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2021 Luke Childs
4
+ Copyright (c) 2021-2022 Jared Wray
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @keyv/serialize [<img width="100" align="right" src="https://jaredwray.com/images/keyv-symbol.svg" alt="keyv">](https://github.com/jaredwra/keyv)
2
+
3
+ > Serialization functionality for [Keyv](https://github.com/jaredwray/keyv)
4
+
5
+
6
+ [![build](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml)
7
+ [![codecov](https://codecov.io/gh/jaredwray/keyv/branch/main/graph/badge.svg?token=bRzR3RyOXZ)](https://codecov.io/gh/jaredwray/keyv)
8
+ [![GitHub license](https://img.shields.io/github/license/jaredwray/keyv)](https://github.com/jaredwray/keyv/blob/main/LICENSE)
9
+ [![npm](https://img.shields.io/npm/dm/@keyv/serialize)](https://npmjs.com/package/@keyv/serialize)
10
+
11
+ This is the serialization functionality for [Keyv](https://github.com/jaredwray/keyv/tree/main/packages/keyv). It is used to serialize and deserialize data for storage and retrieval. You can also create your own [custom serialization functions](https://github.com/jaredwray/keyv/tree/main/packages/keyv#custom-serializers).
12
+
13
+ ## License
14
+
15
+ [MIT © Jared Wray](LISCENCE)
@@ -0,0 +1,2 @@
1
+ export declare const defaultSerialize: (data: any) => string;
2
+ export declare const defaultDeserialize: <Value>(data: any) => Value;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultDeserialize = exports.defaultSerialize = void 0;
4
+ const buffer_1 = require("buffer");
5
+ // Improved version of the deprecated `json-buffer` (https://github.com/dominictarr/json-buffer) package.
6
+ // These default functionalities can be improved separately from the dependant packages.
7
+ const defaultSerialize = (data) => {
8
+ if (data === undefined || data === null) {
9
+ return 'null';
10
+ }
11
+ if (typeof data === 'string') {
12
+ return JSON.stringify(data.startsWith(':') ? ':' + data : data);
13
+ }
14
+ if (buffer_1.Buffer.isBuffer(data)) {
15
+ return JSON.stringify(':base64:' + data.toString('base64'));
16
+ }
17
+ if (data?.toJSON) {
18
+ data = data.toJSON();
19
+ }
20
+ if (typeof data === 'object') {
21
+ let s = '';
22
+ const array = Array.isArray(data);
23
+ s = array ? '[' : '{';
24
+ let first = true;
25
+ // eslint-disable-next-line guard-for-in
26
+ for (const k in data) {
27
+ const ignore = typeof data[k] === 'function' || (!array && data[k] === undefined);
28
+ // eslint-disable-next-line no-prototype-builtins
29
+ if (!data.hasOwnProperty(k) || ignore) {
30
+ continue;
31
+ }
32
+ if (!first) {
33
+ s += ',';
34
+ }
35
+ first = false;
36
+ if (array) {
37
+ s += (0, exports.defaultSerialize)(data[k]);
38
+ }
39
+ else if (data[k] !== undefined) {
40
+ s += (0, exports.defaultSerialize)(k) + ':' + (0, exports.defaultSerialize)(data[k]);
41
+ }
42
+ }
43
+ s += array ? ']' : '}';
44
+ return s;
45
+ }
46
+ return JSON.stringify(data);
47
+ };
48
+ exports.defaultSerialize = defaultSerialize;
49
+ const defaultDeserialize = (data) => JSON.parse(data, (_, value) => {
50
+ if (typeof value === 'string') {
51
+ if (value.startsWith(':base64:')) {
52
+ return buffer_1.Buffer.from(value.slice(8), 'base64');
53
+ }
54
+ return value.startsWith(':') ? value.slice(1) : value;
55
+ }
56
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
57
+ return value;
58
+ });
59
+ exports.defaultDeserialize = defaultDeserialize;
60
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAA8B;AAE9B,yGAAyG;AACzG,wFAAwF;AACjF,MAAM,gBAAgB,GAAG,CAAC,IAAS,EAAU,EAAE;IACrD,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QACzC,OAAO,MAAM,CAAC;IACf,CAAC;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACjE,CAAC;IAED,IAAI,eAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC7D,CAAC;IAED,IAAI,IAAI,EAAE,MAAM,EAAE,CAAC;QAClB,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,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QAAA,gBAAgB,oBAgD3B;AAEK,MAAM,kBAAkB,GAAG,CAAQ,IAAS,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAyB,EAAE,CAC7F,CAAC,EAAE,KAAK,EAAE,EAAE;IACZ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC/B,IAAI,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAClC,OAAO,eAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACvD,CAAC;IAED,+DAA+D;IAC/D,OAAO,KAAK,CAAC;AACd,CAAC,CAAU,CAAC;AAZC,QAAA,kBAAkB,sBAYnB"}
@@ -0,0 +1,2 @@
1
+ export declare const defaultSerialize: (data: any) => string;
2
+ export declare const defaultDeserialize: <Value>(data: any) => Value;
@@ -0,0 +1,55 @@
1
+ import { Buffer } from 'buffer';
2
+ // Improved version of the deprecated `json-buffer` (https://github.com/dominictarr/json-buffer) package.
3
+ // These default functionalities can be improved separately from the dependant packages.
4
+ export const defaultSerialize = (data) => {
5
+ if (data === undefined || data === null) {
6
+ return 'null';
7
+ }
8
+ if (typeof data === 'string') {
9
+ return JSON.stringify(data.startsWith(':') ? ':' + data : data);
10
+ }
11
+ if (Buffer.isBuffer(data)) {
12
+ return JSON.stringify(':base64:' + data.toString('base64'));
13
+ }
14
+ if (data?.toJSON) {
15
+ data = data.toJSON();
16
+ }
17
+ if (typeof data === 'object') {
18
+ let s = '';
19
+ const array = Array.isArray(data);
20
+ s = array ? '[' : '{';
21
+ let first = true;
22
+ // eslint-disable-next-line guard-for-in
23
+ for (const k in data) {
24
+ const ignore = typeof data[k] === 'function' || (!array && data[k] === undefined);
25
+ // eslint-disable-next-line no-prototype-builtins
26
+ if (!data.hasOwnProperty(k) || ignore) {
27
+ continue;
28
+ }
29
+ if (!first) {
30
+ s += ',';
31
+ }
32
+ first = false;
33
+ if (array) {
34
+ s += defaultSerialize(data[k]);
35
+ }
36
+ else if (data[k] !== undefined) {
37
+ s += defaultSerialize(k) + ':' + defaultSerialize(data[k]);
38
+ }
39
+ }
40
+ s += array ? ']' : '}';
41
+ return s;
42
+ }
43
+ return JSON.stringify(data);
44
+ };
45
+ export const defaultDeserialize = (data) => JSON.parse(data, (_, value) => {
46
+ if (typeof value === 'string') {
47
+ if (value.startsWith(':base64:')) {
48
+ return Buffer.from(value.slice(8), 'base64');
49
+ }
50
+ return value.startsWith(':') ? value.slice(1) : value;
51
+ }
52
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
53
+ return value;
54
+ });
55
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAE9B,yGAAyG;AACzG,wFAAwF;AACxF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,IAAS,EAAU,EAAE;IACrD,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QACzC,OAAO,MAAM,CAAC;IACf,CAAC;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACjE,CAAC;IAED,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,QA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AAC,CAAC;IACV,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAQ,IAAS,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAyB,EAAE,CAC7F,CAAC,EAAE,KAAK,EAAE,EAAE;IACZ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC/B,IAAI,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAClC,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACvD,CAAC;IAED,+DAA+D;IAC/D,OAAO,KAAK,CAAC;AACd,CAAC,CAAU,CAAC"}
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@keyv/serialize",
3
+ "version": "1.0.0",
4
+ "description": "Serialization for keyv",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "require": "./dist/cjs/index.js",
11
+ "import": "./dist/esm/index.js"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "build": "rm -rf dist && tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
16
+ "prepare": "yarn build",
17
+ "test": "xo --fix && vitest run --coverage",
18
+ "test:ci": "xo && vitest --run --sequence.setupFiles=list",
19
+ "clean": "rm -rf node_modules && rm -rf ./coverage"
20
+ },
21
+ "xo": {
22
+ "rules": {
23
+ "unicorn/prefer-module": "off",
24
+ "unicorn/prefer-event-target": "off",
25
+ "unicorn/no-array-reduce": "off",
26
+ "n/prefer-global/process": "off",
27
+ "node/prefer-global/process": "off",
28
+ "unicorn/prefer-object-from-entries": "off",
29
+ "unicorn/prefer-node-protocol": "off",
30
+ "@typescript-eslint/consistent-type-imports": "off",
31
+ "@typescript-eslint/consistent-type-definitions": "off",
32
+ "@typescript-eslint/no-empty-function": "off",
33
+ "import/extensions": "off",
34
+ "no-promise-executor-return": "off",
35
+ "import/no-extraneous-dependencies": "off",
36
+ "@typescript-eslint/no-unsafe-call": "off",
37
+ "@typescript-eslint/no-unsafe-assignment": "off"
38
+
39
+ }
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/jaredwray/keyv.git"
44
+ },
45
+ "keywords": [
46
+ "keyv",
47
+ "serialize",
48
+ "key",
49
+ "value",
50
+ "store"
51
+ ],
52
+ "author": "Jared Wray <me@jaredwray.com> (https://jaredwray.com)",
53
+ "license": "MIT",
54
+ "bugs": {
55
+ "url": "https://github.com/jaredwray/keyv/issues"
56
+ },
57
+ "homepage": "https://github.com/jaredwray/keyv",
58
+ "dependencies": {
59
+ "buffer": "^6.0.3"
60
+ },
61
+ "devDependencies": {
62
+ "@keyv/test-suite": "*",
63
+ "keyv": "^5.0.0",
64
+ "ts-node": "^10.9.2",
65
+ "tsd": "^0.31.1",
66
+ "typescript": "^5.5.4",
67
+ "xo": "^0.59.3"
68
+ },
69
+ "tsd": {
70
+ "directory": "test"
71
+ },
72
+ "files": [
73
+ "dist",
74
+ "LICENSE"
75
+ ]
76
+ }