Skip to content

Commit 24f9e9c

Browse files
committed
Strip external schemas from standalone compiled code
1 parent b75db8e commit 24f9e9c

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

lib/standalone.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ function buildStandaloneCode (contextFunc, context, serializer, validator) {
88
} else {
99
ajvDependencyCode += 'const validator = null\n'
1010
}
11+
// Don't need to keep external schemas once compiled
12+
const { schema, ...options } = serializer.getState()
1113

1214
return `
1315
'use strict'
1416
const { dependencies } = require('fast-json-stringify/lib/standalone')
1517
1618
const { Serializer, Validator } = dependencies
1719
18-
const serializerState = ${JSON.stringify(serializer.getState())}
20+
const serializerState = ${JSON.stringify(options)}
1921
const serializer = Serializer.restoreFromState(serializerState)
2022
2123
${ajvDependencyCode}

test/standalone-mode.test.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ test('activate standalone mode', async (t) => {
2626
t.type(code, 'string')
2727
t.equal(code.indexOf('ajv'), -1)
2828

29-
const destionation = path.resolve(tmpDir, 'standalone.js')
29+
const destination = path.resolve(tmpDir, 'standalone.js')
3030

3131
t.teardown(async () => {
32-
await fs.promises.rm(destionation, { force: true })
32+
await fs.promises.rm(destination, { force: true })
3333
})
3434

35-
await fs.promises.writeFile(destionation, code)
36-
const standalone = require(destionation)
35+
await fs.promises.writeFile(destination, code)
36+
const standalone = require(destination)
3737
t.same(standalone({ firstName: 'Foo', surname: 'bar' }), JSON.stringify({ firstName: 'Foo' }), 'surname evicted')
3838
})
3939

@@ -89,14 +89,14 @@ test('test ajv schema', async (t) => {
8989
t.type(code, 'string')
9090
t.equal(code.indexOf('ajv') > 0, true)
9191

92-
const destionation = path.resolve(tmpDir, 'standalone2.js')
92+
const destination = path.resolve(tmpDir, 'standalone2.js')
9393

9494
t.teardown(async () => {
95-
await fs.promises.rm(destionation, { force: true })
95+
await fs.promises.rm(destination, { force: true })
9696
})
9797

98-
await fs.promises.writeFile(destionation, code)
99-
const standalone = require(destionation)
98+
await fs.promises.writeFile(destination, code)
99+
const standalone = require(destination)
100100
t.same(standalone({
101101
kind: 'foobar',
102102
foo: 'FOO',
@@ -119,3 +119,38 @@ test('test ajv schema', async (t) => {
119119
}]
120120
}))
121121
})
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+
})

0 commit comments

Comments
 (0)