fast-json-stringify 5.4.1 → 5.5.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/README.md CHANGED
@@ -650,12 +650,16 @@ AJV Serialize large array x 58.76 ops/sec ±4.59% (60 runs sampled)
650
650
  Treat the schema definition as application code, it
651
651
  is not safe to use user-provided schemas.
652
652
 
653
- In order to achieve lowest cost/highest performance redaction `fast-json-stringify`
653
+ To achieve low cost and high performance redaction `fast-json-stringify`
654
654
  creates and compiles a function (using the `Function` constructor) on initialization.
655
655
  While the `schema` is currently validated for any developer errors,
656
656
  there is no guarantee that supplying user-generated schema could not
657
657
  expose your application to remote attacks.
658
658
 
659
+ Users are responsible for sending trusted data. `fast-json-stringify` guarantees that you will get
660
+ a valid output only if your input matches the schema or can be coerced to the schema. If your input
661
+ doesn't match the schema, you will get undefined behavior.
662
+
659
663
  <a name="debug"></a>
660
664
  ### Debug Mode
661
665
 
package/index.js CHANGED
@@ -888,6 +888,8 @@ function buildValue (location, input) {
888
888
  }
889
889
 
890
890
  module.exports = build
891
+ module.exports.default = build
892
+ module.exports.build = build
891
893
 
892
894
  module.exports.validLargeArrayMechanisms = validLargeArrayMechanisms
893
895
 
package/package.json CHANGED
@@ -1,13 +1,21 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "5.4.1",
3
+ "version": "5.5.0",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
- "types": "index.d.ts",
7
- "precommit": [
8
- "lint",
9
- "test"
10
- ],
6
+ "types": "types/index.d.ts",
7
+ "scripts": {
8
+ "bench": "node ./benchmark/bench.js",
9
+ "bench:cmp": "node ./benchmark/bench-cmp-branch.js",
10
+ "bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci",
11
+ "benchmark": "node ./benchmark/bench-cmp-lib.js",
12
+ "lint": "standard",
13
+ "lint:fix": "standard --fix",
14
+ "test:typescript": "tsd",
15
+ "test:unit": "tap -J test/*.test.js test/**/*.test.js",
16
+ "test": "npm run test:unit && npm run test:typescript"
17
+ },
18
+ "precommit": ["lint", "test"],
11
19
  "repository": {
12
20
  "type": "git",
13
21
  "url": "git+https://github.com/fastify/fast-json-stringify.git"
@@ -38,7 +46,6 @@
38
46
  "standard": "^17.0.0",
39
47
  "tap": "^16.0.1",
40
48
  "tsd": "^0.24.1",
41
- "typescript": "^4.0.2",
42
49
  "webpack": "^5.40.0"
43
50
  },
44
51
  "dependencies": {
@@ -54,19 +61,5 @@
54
61
  "schema-validator.js"
55
62
  ]
56
63
  },
57
- "runkitExampleFilename": "./examples/example.js",
58
- "tsd": {
59
- "directory": "test/types"
60
- },
61
- "scripts": {
62
- "bench": "node ./benchmark/bench.js",
63
- "bench:cmp": "node ./benchmark/bench-cmp-branch.js",
64
- "bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci",
65
- "benchmark": "node ./benchmark/bench-cmp-lib.js",
66
- "lint": "standard",
67
- "lint:fix": "standard --fix",
68
- "test:typescript": "tsc --project ./test/types/tsconfig.json && tsd",
69
- "test:unit": "tap -J test/*.test.js test/**/*.test.js",
70
- "test": "npm run test:unit && npm run test:typescript"
71
- }
72
- }
64
+ "runkitExampleFilename": "./examples/example.js"
65
+ }
@@ -1,5 +1,7 @@
1
1
  import Ajv, { Options as AjvOptions } from "ajv"
2
2
 
3
+ type Build = typeof build
4
+
3
5
  declare namespace build {
4
6
  interface BaseSchema {
5
7
  /**
@@ -168,6 +170,12 @@ declare namespace build {
168
170
  */
169
171
  mode?: 'debug' | 'standalone'
170
172
  }
173
+
174
+ export const validLargeArrayMechanisms: string[]
175
+ export function restore (value: <TDoc extends object = object>(doc: TDoc) => string): ReturnType<Build>
176
+
177
+ export const build: Build
178
+ export { build as default }
171
179
  }
172
180
 
173
181
  interface DebugOption extends build.Options {
@@ -1,5 +1,6 @@
1
1
  import Ajv from 'ajv'
2
- import build, { Schema } from '../..'
2
+ import build, { restore, Schema, validLargeArrayMechanisms } from '..'
3
+ import { expectError, expectType } from 'tsd'
3
4
 
4
5
  // Number schemas
5
6
  const schema1: Schema = {
@@ -150,4 +151,69 @@ str = build(schema1, { debugMode: true }).code
150
151
  ajv = build(schema1, { debugMode: true }).ajv
151
152
  str = build(schema1, { mode: 'debug' }).code
152
153
  ajv = build(schema1, { mode: 'debug' }).ajv
153
- str = build(schema1, { mode: 'standalone' })
154
+ str = build(schema1, { mode: 'standalone' })
155
+
156
+ const debugCompiled = build({
157
+ title: 'default string',
158
+ type: 'object',
159
+ properties: {
160
+ firstName: {
161
+ type: 'string'
162
+ }
163
+ }
164
+ }, { mode: 'debug' })
165
+ expectType<ReturnType<typeof build>>(build.restore(debugCompiled))
166
+ expectType<ReturnType<typeof build>>(restore(debugCompiled))
167
+
168
+ expectType<string[]>(build.validLargeArrayMechanisms)
169
+ expectType<string[]>(validLargeArrayMechanisms)
170
+
171
+ /**
172
+ * Schema inference
173
+ */
174
+
175
+ // With inference
176
+ interface InferenceSchema {
177
+ id: string;
178
+ a?: number;
179
+ }
180
+
181
+ const stringify3 = build({
182
+ type: "object",
183
+ properties: { a: { type: "string" } },
184
+ });
185
+ stringify3<InferenceSchema>({ id: "123" });
186
+ stringify3<InferenceSchema>({ a: 123, id: "123" });
187
+ expectError(stringify3<InferenceSchema>({ anotherOne: "bar" }));
188
+ expectError(stringify3<Schema>({ a: "bar" }));
189
+
190
+ // Without inference
191
+ const stringify4 = build({
192
+ type: "object",
193
+ properties: { a: { type: "string" } },
194
+ });
195
+ stringify4({ id: "123" });
196
+ stringify4({ a: 123, id: "123" });
197
+ stringify4({ anotherOne: "bar" });
198
+ stringify4({ a: "bar" });
199
+
200
+ // Without inference - string type
201
+ const stringify5 = build({
202
+ type: "string",
203
+ });
204
+ stringify5("foo");
205
+ expectError(stringify5({ id: "123" }));
206
+
207
+ // Without inference - null type
208
+ const stringify6 = build({
209
+ type: "null",
210
+ });
211
+ stringify6(null);
212
+ expectError(stringify6("a string"));
213
+
214
+ // Without inference - boolean type
215
+ const stringify7 = build({
216
+ type: "boolean",
217
+ });
218
+ stringify7(true);
219
+ expectError(stringify7("a string"));
@@ -1,48 +0,0 @@
1
- import { expectError } from "tsd";
2
- import build from "../..";
3
-
4
- // With inference
5
- interface Schema {
6
- id: string;
7
- a?: number;
8
- }
9
-
10
- const stringify3 = build({
11
- type: "object",
12
- properties: { a: { type: "string" } },
13
- });
14
- stringify3<Schema>({ id: "123" });
15
- stringify3<Schema>({ a: 123, id: "123" });
16
- expectError(stringify3<Schema>({ anotherOne: "bar" }));
17
- expectError(stringify3<Schema>({ a: "bar" }));
18
-
19
- // Without inference
20
- const stringify4 = build({
21
- type: "object",
22
- properties: { a: { type: "string" } },
23
- });
24
- stringify4({ id: "123" });
25
- stringify4({ a: 123, id: "123" });
26
- stringify4({ anotherOne: "bar" });
27
- stringify4({ a: "bar" });
28
-
29
- // Without inference - string type
30
- const stringify5 = build({
31
- type: "string",
32
- });
33
- stringify5("foo");
34
- expectError(stringify5({ id: "123" }));
35
-
36
- // Without inference - null type
37
- const stringify6 = build({
38
- type: "null",
39
- });
40
- stringify6(null);
41
- expectError(stringify6("a string"));
42
-
43
- // Without inference - boolean type
44
- const stringify7 = build({
45
- type: "boolean",
46
- });
47
- stringify7(true);
48
- expectError(stringify7("a string"));
@@ -1,13 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "esModuleInterop": true,
4
- "noEmit": true,
5
- "rootDir": "../..",
6
- "strict": true,
7
- "target": "ES2015",
8
- "moduleResolution": "node"
9
- },
10
- "include": [
11
- "./test.ts"
12
- ]
13
- }