UNPKG

982 BJavaScriptView Raw
1/* global self */
2'use strict'
3
4const { Buffer } = require('buffer')
5// note: filePath needs to be relative to the module root
6module.exports = function loadFixtures (filePath, module) {
7 if (module) {
8 filePath = 'node_modules/' + module + '/' + filePath
9 }
10 return syncXhr(filePath)
11}
12
13// @dignifiedquire: I know this is considered bad practice (syncXhr), but it
14// makes testing life so much nicer!
15function syncXhr (filePath) {
16 const target = '/base/' + filePath
17
18 const request = new self.XMLHttpRequest()
19 request.open('GET', target, false)
20 request.overrideMimeType('text/plain; charset=x-user-defined')
21 request.send(null)
22
23 if (request.status === 200) {
24 const filestream = request.responseText
25 const res = new Uint8Array(filestream.length)
26
27 for (let i = 0; i < filestream.length; i++) {
28 res[i] = filestream.charCodeAt(i) & 0xff
29 }
30
31 return Buffer.from(res)
32 } else {
33 throw new Error(`Could not get the Fixture: ${filePath}`)
34 }
35}