Skip to content

Commit 5976675

Browse files
committed
test(browser): polyfill for ie11
1 parent cfd6005 commit 5976675

7 files changed

Lines changed: 86 additions & 8 deletions

File tree

.config/karma.base.conf.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ module.exports = {
77
],
88

99
files: [
10+
// { pattern: '.config/node_modules/fast-text-encoding/text.min.js', watched: false },
11+
{ pattern: '.config/node_modules/text-encoding/lib/encoding-indexes.js', watched: false },
12+
{ pattern: '.config/node_modules/text-encoding/lib/encoding.js', watched: false },
13+
{ pattern: '.config/node_modules/es6-shim/es6-shim.min.js', watched: false },
14+
// { pattern: '.config/node_modules/es7-shim/dist/es7-shim.min.js', watched: false },
1015
'src/**/*.ts',
1116
'test_browser/**/*.ts',
1217
],
@@ -41,7 +46,7 @@ module.exports = {
4146
moduleResolution: 'node',
4247
noUnusedLocals: false,
4348
strict: true,
44-
target: 'es6',
49+
target: 'es5',
4550
},
4651
include: [
4752
'src/**/*.ts',

.config/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"sauce": "karma start karma.sauce.conf.js"
1212
},
1313
"devDependencies": {
14-
"es7-shim": "^6.0.0",
14+
"text-encoding": "git+https://github.com/inexorabletash/text-encoding.git",
15+
"es6-shim": "^0.35.5",
1516
"karma": "^4.1.0",
1617
"karma-chrome-launcher": "^2.2.0",
1718
"karma-detect-browsers": "^2.2.5",

test_browser/10_index.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
input4,
1515
input44,
1616
input8,
17+
isEdge,
18+
isIE,
1719
} from './config'
1820

1921

@@ -77,7 +79,9 @@ describe(filename, () => {
7779
describe('Should b64fromBuffer() works', () => {
7880
it('with valid input', () => {
7981
input8.forEach(row => {
80-
const u8arr = Uint8Array.from(row[0])
82+
const u8arr = isIE || isEdge
83+
? new Uint8Array(row[0])
84+
: Uint8Array.from(row[0])
8185
const actual = b64fromBuffer(u8arr)
8286
const expected = row[1]
8387
assert(actual === expected, `Ensure that ${u8arr} serialise to ${expected}`)

test_browser/20_big.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import * as assert from 'power-assert'
44

5-
import { b64decode, b64encode, b64byteLength } from '../src'
5+
import { b64byteLength, b64decode, b64encode } from '../src'
66
import { fromUint8Array } from '../src/lib/from_buffer'
77
import { toUint8Array } from '../src/lib/to_buffer'
88

9-
import { input1, input2 } from './config'
9+
import { input1, input2, isEdge, isIE } from './config'
1010
import { equal } from './helper'
1111

1212

@@ -15,7 +15,8 @@ const filename = '20_big.test.ts'
1515
describe(filename, () => {
1616
describe('Should fromUint8Array() works with big input', () => {
1717
it('test1', () => {
18-
const big = new Uint8Array(128 * 1024 * 1024)
18+
const count = isIE || isEdge ? 64 * 1024 * 1024 : 128 * 1024 * 1024
19+
const big = new Uint8Array(count)
1920

2021
for (let i = 0, length = big.length; i < length; ++i) {
2122
big[i] = i % 256
@@ -27,7 +28,8 @@ describe(filename, () => {
2728
})
2829

2930
it('test2', () => {
30-
const str = input1.concat(input2).join('').repeat(300_000)
31+
const count = isIE || isEdge ? 100_000 : 300_000
32+
const str = input1.concat(input2).join('').repeat(count)
3133
const len = (str.length / 1024 / 1024).toFixed(2)
3234
const ret1 = b64encode(str)
3335
const ret2 = b64decode(ret1)

test_browser/20_from_buffer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as assert from 'power-assert'
44

55
import { fromUint8Array } from '../src/lib/from_buffer'
66

7-
import { input8 } from './config'
7+
import { input8, isEdge, isIE } from './config'
88

99

1010
const filename = '20_from_buffer.test.ts'

test_browser/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import {
22
baseChars,
33
} from '../src/lib/config'
44

5+
import { _dummy } from './patch'
6+
7+
8+
// @ts-ignore
9+
export const isIE = /* @cc_on!@*/false || !! document.documentMode
10+
// @ts-ignore
11+
export const isEdge = ! isIE && !! window.StyleMedia
512

613
export const input1: Array<string | number | bigint> = [
714
baseChars,
@@ -92,3 +99,4 @@ export const input8: Input5Item[] = [
9299
[ [0, -73, 23], 'ALcX'],
93100
[ [0xe4, 0xb8, 0xad, 0xe6, 0x96, 0x87], '5Lit5paH'],
94101
]
102+

test_browser/patch.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// @ts-nocheck
2+
3+
// @ts-ignore
4+
if (!Int8Array.__proto__.from) {
5+
// tslint:disable-next-line: only-arrow-functions
6+
(function() {
7+
// @ts-ignore
8+
Int8Array.__proto__.from = function(obj: any, func: any, thisObj: any) {
9+
10+
// @ts-ignore
11+
const typedArrayClass = Int8Array.__proto__
12+
if (typeof this !== 'function') {
13+
throw new TypeError('# is not a constructor')
14+
}
15+
if (this.__proto__ !== typedArrayClass) {
16+
throw new TypeError('this is not a typed array.')
17+
}
18+
19+
// tslint:disable-next-line: only-arrow-functions
20+
func = func || function(elem: any) {
21+
return elem
22+
}
23+
24+
if (typeof func !== 'function') {
25+
throw new TypeError('specified argument is not a function')
26+
}
27+
28+
obj = Object(obj)
29+
if (!obj.length) {
30+
return new this(0)
31+
}
32+
let copy_data = []
33+
// tslint:disable-next-line: prefer-for-of
34+
for (let i = 0; i < obj.length; i++) {
35+
copy_data.push(obj[i])
36+
}
37+
38+
copy_data = copy_data.map(func, thisObj)
39+
40+
const typed_array = new this(copy_data.length)
41+
for (let i = 0; i < typed_array.length; i++) {
42+
typed_array[i] = copy_data[i]
43+
}
44+
return typed_array
45+
}
46+
})()
47+
}
48+
49+
// @ts-ignore
50+
// if (!Uint8Array.prototype.slice) {
51+
// Object.defineProperty(Uint8Array.prototype, 'slice', {
52+
// value(begin: number, end: number) {
53+
// return new Uint8Array(Array.prototype.slice.call(this, begin, end))
54+
// },
55+
// })
56+
// }
57+
58+
export const _dummy = 1

0 commit comments

Comments
 (0)