This repository was archived by the owner on Dec 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstructured-clone-test.js
More file actions
217 lines (194 loc) · 6.21 KB
/
structured-clone-test.js
File metadata and controls
217 lines (194 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict'
var isDataCloneError = require('../util/is-data-clone-error')
var ta = require('./util/create-typed-array')
// level-js supports all types of the structured clone algorithm
// except for null and undefined (unless nested in another type).
var types = [
{ type: 'boolean', value: true },
{ type: 'number', value: -20 },
{
type: 'NaN',
value: NaN,
test: function (value) {
// Replacement for Number.isNaN (for IE <= 11)
return typeof value === 'number' && isNaN(value)
}
},
{ type: '+Infinity', value: Infinity },
{ type: '-Infinity', value: -Infinity },
{ type: 'string', value: 'test' },
{ type: 'Boolean object', value: new Boolean(false) }, // eslint-disable-line
{ type: 'String object', value: new String('test') }, // eslint-disable-line
{ type: 'Date', ctor: true, value: new Date() },
{ type: 'RegExp', ctor: true, value: /r/g },
{ type: 'Array', ctor: true, value: [0, null, undefined] },
{ type: 'Object', ctor: true, value: { a: null, b: [undefined] } },
{
type: 'Object',
name: 'Object (null prototype)',
ctor: true,
createValue: function () {
return Object.create(null)
}
},
{ type: 'ArrayBuffer', ctor: true, allowFailure: true, value: ta(Buffer).buffer },
{ type: 'Int8Array', ctor: true, allowFailure: true, createValue: ta },
// Don't allow failure as this is the primary type for binary (Buffer) data
{ type: 'Uint8Array', ctor: true, createValue: ta },
{ type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createValue: ta },
{ type: 'Int16Array', ctor: true, allowFailure: true, createValue: ta },
{ type: 'Uint16Array', ctor: true, allowFailure: true, createValue: ta },
{ type: 'Int32Array', ctor: true, allowFailure: true, createValue: ta },
{ type: 'Uint32Array', ctor: true, allowFailure: true, createValue: ta },
{ type: 'Float32Array', ctor: true, allowFailure: true, createValue: ta },
{ type: 'Float64Array', ctor: true, allowFailure: true, createValue: ta },
{
type: 'Map',
ctor: true,
allowFailure: true,
createValue: function (Constructor) {
// Replacement for Map constructor arguments (for IE 11)
var value = new Constructor()
value.set('test', 123)
return value
},
test: function (value) {
return value.get('test') === 123
}
},
{
type: 'Set',
ctor: true,
allowFailure: true,
createValue: function (Constructor) {
// Replacement for Set constructor arguments (for IE 11)
var value = new Constructor()
value.add(123)
return value
},
test: function (value) {
return value.has(123)
}
},
{
type: 'Blob',
ctor: true,
allowFailure: true,
createValue: function (Constructor) {
return new Constructor(['test'])
},
test: function (value) {
// TODO. This test would be asynchronous.
return true
}
},
{
type: 'File',
ctor: true,
allowFailure: true,
createValue: function (Constructor) {
return new Constructor(['test'], 'filename')
},
test: function (value) {
// TODO. This test would be asynchronous.
return true
}
},
{
type: 'FileList',
ctor: true,
allowFailure: true,
createValue: function () {
var input = global.document.createElement('input')
input.type = 'file'
return input.files
}
},
{
type: 'ImageData',
ctor: true,
allowFailure: true,
createValue: function (Constructor) {
return new Constructor(1, 1)
},
test: function (value) {
return value.data.length === 4
}
}
]
// Types that are not supported by the structured clone algorithm
var illegalTypes = [
{ name: 'Error', value: new Error() },
{ name: 'Function', value: function () {} },
{ name: 'DOMNode', value: global.document }
]
module.exports = function (leveljs, test, testCommon) {
var db
test('setUp', testCommon.setUp)
test('open', function (t) {
db = leveljs(testCommon.location())
db.open(t.end.bind(t))
})
types.forEach(function (item) {
var testName = item.name || item.type
test('structured clone: ' + testName, function (t) {
var ctor = item.ctor ? global[item.type] : null
var skip = item.allowFailure ? 'pass' : 'fail'
var input = item.value
if (item.ctor && !ctor) {
t[skip]('constructor is undefined in this environment')
return t.end()
}
if (item.createValue) {
try {
input = item.createValue(ctor)
} catch (err) {
t[skip]('constructor is not spec-compliant in this environment')
return t.end()
}
}
db.put(testName, input, function (err) {
if (err && isDataCloneError(err)) {
t[skip]('serializing is not supported by the structured clone algorithm of this environment')
return t.end()
}
t.notOk(err, 'no put error')
db.get(testName, { asBuffer: false }, function (err, value) {
t.notOk(err, 'no get error')
if (ctor) {
var expected = '[object ' + item.type + ']'
var actual = Object.prototype.toString.call(value)
if (actual === expected) {
t.is(actual, expected, 'prototype')
t.ok(value instanceof ctor, 'instanceof')
} else {
t[skip]('deserializing is not supported by the structured clone algorithm of this environment')
return t.end()
}
}
if (item.test) {
t.ok(item.test(value), 'correct value')
} else {
t.same(value, input, 'correct value')
}
t.end()
})
})
})
})
illegalTypes.forEach(function (item) {
test('structured clone (illegal type): ' + item.name, function (t) {
t.ok(item.value != null, 'got a value to test')
db.put(item.name, item.value, function (err) {
t.ok(err, 'got an error')
t.ok(isDataCloneError(err), 'is DataCloneError')
db.get(item.name, { asBuffer: false }, function (err, value) {
t.ok(/notfound/i.test(err), 'nothing was stored')
t.end()
})
})
})
})
test('close', function (t) { db.close(t.end.bind(t)) })
test('teardown', testCommon.tearDown)
}