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