fast-json-stringify 5.15.1 → 5.16.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/lib/standalone.js +7 -4
- package/package.json +1 -1
- package/test/standalone-mode.test.js +103 -8
package/lib/standalone.js
CHANGED
|
@@ -3,19 +3,22 @@
|
|
|
3
3
|
function buildStandaloneCode (contextFunc, context, serializer, validator) {
|
|
4
4
|
let ajvDependencyCode = ''
|
|
5
5
|
if (context.validatorSchemasIds.size > 0) {
|
|
6
|
+
ajvDependencyCode += 'const Validator = require(\'fast-json-stringify/lib/validator\')\n'
|
|
6
7
|
ajvDependencyCode += `const validatorState = ${JSON.stringify(validator.getState())}\n`
|
|
7
8
|
ajvDependencyCode += 'const validator = Validator.restoreFromState(validatorState)\n'
|
|
8
9
|
} else {
|
|
9
10
|
ajvDependencyCode += 'const validator = null\n'
|
|
10
11
|
}
|
|
11
12
|
|
|
13
|
+
// Don't need to keep external schemas once compiled
|
|
14
|
+
// validatorState will hold external schemas if it needs them
|
|
15
|
+
const { schema, ...serializerState } = serializer.getState()
|
|
16
|
+
|
|
12
17
|
return `
|
|
13
18
|
'use strict'
|
|
14
|
-
const { dependencies } = require('fast-json-stringify/lib/standalone')
|
|
15
|
-
|
|
16
|
-
const { Serializer, Validator } = dependencies
|
|
17
19
|
|
|
18
|
-
const
|
|
20
|
+
const Serializer = require('fast-json-stringify/lib/serializer')
|
|
21
|
+
const serializerState = ${JSON.stringify(serializerState)}
|
|
19
22
|
const serializer = Serializer.restoreFromState(serializerState)
|
|
20
23
|
|
|
21
24
|
${ajvDependencyCode}
|
package/package.json
CHANGED
|
@@ -26,14 +26,14 @@ test('activate standalone mode', async (t) => {
|
|
|
26
26
|
t.type(code, 'string')
|
|
27
27
|
t.equal(code.indexOf('ajv'), -1)
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const destination = path.resolve(tmpDir, 'standalone.js')
|
|
30
30
|
|
|
31
31
|
t.teardown(async () => {
|
|
32
|
-
await fs.promises.rm(
|
|
32
|
+
await fs.promises.rm(destination, { force: true })
|
|
33
33
|
})
|
|
34
34
|
|
|
35
|
-
await fs.promises.writeFile(
|
|
36
|
-
const standalone = require(
|
|
35
|
+
await fs.promises.writeFile(destination, code)
|
|
36
|
+
const standalone = require(destination)
|
|
37
37
|
t.same(standalone({ firstName: 'Foo', surname: 'bar' }), JSON.stringify({ firstName: 'Foo' }), 'surname evicted')
|
|
38
38
|
})
|
|
39
39
|
|
|
@@ -89,14 +89,14 @@ test('test ajv schema', async (t) => {
|
|
|
89
89
|
t.type(code, 'string')
|
|
90
90
|
t.equal(code.indexOf('ajv') > 0, true)
|
|
91
91
|
|
|
92
|
-
const
|
|
92
|
+
const destination = path.resolve(tmpDir, 'standalone2.js')
|
|
93
93
|
|
|
94
94
|
t.teardown(async () => {
|
|
95
|
-
await fs.promises.rm(
|
|
95
|
+
await fs.promises.rm(destination, { force: true })
|
|
96
96
|
})
|
|
97
97
|
|
|
98
|
-
await fs.promises.writeFile(
|
|
99
|
-
const standalone = require(
|
|
98
|
+
await fs.promises.writeFile(destination, code)
|
|
99
|
+
const standalone = require(destination)
|
|
100
100
|
t.same(standalone({
|
|
101
101
|
kind: 'foobar',
|
|
102
102
|
foo: 'FOO',
|
|
@@ -119,3 +119,98 @@ test('test ajv schema', async (t) => {
|
|
|
119
119
|
}]
|
|
120
120
|
}))
|
|
121
121
|
})
|
|
122
|
+
|
|
123
|
+
test('no need to keep external schemas once compiled', async (t) => {
|
|
124
|
+
t.plan(1)
|
|
125
|
+
const externalSchema = {
|
|
126
|
+
first: {
|
|
127
|
+
definitions: {
|
|
128
|
+
id1: {
|
|
129
|
+
type: 'object',
|
|
130
|
+
properties: {
|
|
131
|
+
id1: {
|
|
132
|
+
type: 'integer'
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const code = fjs({
|
|
140
|
+
$ref: 'first#/definitions/id1'
|
|
141
|
+
}, {
|
|
142
|
+
mode: 'standalone',
|
|
143
|
+
schema: externalSchema
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
const destination = path.resolve(tmpDir, 'standalone3.js')
|
|
147
|
+
|
|
148
|
+
t.teardown(async () => {
|
|
149
|
+
await fs.promises.rm(destination, { force: true })
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
await fs.promises.writeFile(destination, code)
|
|
153
|
+
const standalone = require(destination)
|
|
154
|
+
|
|
155
|
+
t.same(standalone({ id1: 5 }), JSON.stringify({ id1: 5 }), 'serialization works with external schemas')
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('no need to keep external schemas once compiled - with oneOf validator', async (t) => {
|
|
159
|
+
t.plan(2)
|
|
160
|
+
|
|
161
|
+
const externalSchema = {
|
|
162
|
+
ext: {
|
|
163
|
+
definitions: {
|
|
164
|
+
oBaz: {
|
|
165
|
+
type: 'object',
|
|
166
|
+
properties: {
|
|
167
|
+
baz: { type: 'number' }
|
|
168
|
+
},
|
|
169
|
+
required: ['baz']
|
|
170
|
+
},
|
|
171
|
+
oBar: {
|
|
172
|
+
type: 'object',
|
|
173
|
+
properties: {
|
|
174
|
+
bar: { type: 'string' }
|
|
175
|
+
},
|
|
176
|
+
required: ['bar']
|
|
177
|
+
},
|
|
178
|
+
other: {
|
|
179
|
+
type: 'string',
|
|
180
|
+
const: 'other'
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const schema = {
|
|
187
|
+
title: 'object with oneOf property value containing refs to external schema',
|
|
188
|
+
type: 'object',
|
|
189
|
+
properties: {
|
|
190
|
+
oneOfSchema: {
|
|
191
|
+
oneOf: [
|
|
192
|
+
{ $ref: 'ext#/definitions/oBaz' },
|
|
193
|
+
{ $ref: 'ext#/definitions/oBar' }
|
|
194
|
+
]
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
required: ['oneOfSchema']
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const code = fjs(schema, {
|
|
201
|
+
mode: 'standalone',
|
|
202
|
+
schema: externalSchema
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
const destination = path.resolve(tmpDir, 'standalone-oneOf-ref.js')
|
|
206
|
+
|
|
207
|
+
t.teardown(async () => {
|
|
208
|
+
await fs.promises.rm(destination, { force: true })
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
await fs.promises.writeFile(destination, code)
|
|
212
|
+
const stringify = require(destination)
|
|
213
|
+
|
|
214
|
+
t.equal(stringify({ oneOfSchema: { baz: 5 } }), '{"oneOfSchema":{"baz":5}}')
|
|
215
|
+
t.equal(stringify({ oneOfSchema: { bar: 'foo' } }), '{"oneOfSchema":{"bar":"foo"}}')
|
|
216
|
+
})
|