Skip to content

Commit

Permalink
fix: allow to configure XMLHttpRequest root
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Dec 28, 2023
1 parent 5f67bc2 commit 3207194
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
48 changes: 26 additions & 22 deletions .scripts/wpt-mock/XMLHttpRequest.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
const fs = require('node:fs');
const path = require('node:path');

// to be passed to wtp-runner step
// window.XMLHttpRequest = XMLHttpRequest;
class XMLHttpRequest {
constructor() {
this._pathname;
this.onload;
this.onerror;
this.response;
}
module.exports = function createXMLHttpRequest(basepath) {
console.log(basepath);
return class XMLHttpRequest {
constructor() {
this._pathname;
this.onload;
this.onerror;
this.response;
}

open(_protocol, url) {
this._pathname = url;
}
open(_protocol, url) {
this._pathname = url;
}

send() {
let buffer;
send() {
let buffer;

try {
buffer = fs.readFileSync(this._pathname).buffer;
} catch (err) {
this.onerror(err);
return;
}
try {
const pathname = path.join(basepath, this._pathname);
buffer = fs.readFileSync(pathname).buffer;
} catch (err) {
this.onerror(err);
return;
}

this.response = buffer;
this.onload();
this.response = buffer;
this.onload();
}
}
}

module.exports = XMLHttpRequest;
18 changes: 11 additions & 7 deletions .scripts/wpt-mock/wpt-buffer-loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const path = require('node:path');

const XMLHttpRequest = require('./XMLHttpRequest.js');
const createXMLHttpRequest = require('./XMLHttpRequest.js');
const { OfflineAudioContext } = require('../../index.cjs');

// create a XMLHttpRequest to be passed to the runner
// can be configured to handle the difference between process.cwd() and given path
// window.XMLHttpRequest = createXMLHttpRequest(rootURL (?))
const XMLHttpRequest = createXMLHttpRequest(path.join('examples', 'samples'));
// maybe should be passed to wtp-runner setup too
// window.alert = console.log.bind(console);
const alert = console.log.bind(console);
Expand Down Expand Up @@ -60,24 +64,24 @@ const offlineContext = new OfflineAudioContext({
sampleRate: 48000,
});

const okFile = [path.join('examples', 'samples', 'sample.wav')];
const err1File = [path.join('examples', 'samples', 'corrupt.wav')];
const err2File = [path.join('examples', 'samples', 'donotexists.wav')];
const okFiles = [path.join('sample.wav')];
const err1Files = [path.join('corrupt.wav')];
const err2Files = [path.join('donotexists.wav')];

{
// should work
const loader = new BufferLoader(offlineContext, okFile, audioBuffer => console.log(audioBuffer));
const loader = new BufferLoader(offlineContext, okFiles, audioBuffer => console.log(audioBuffer));
loader.load();
}

{
// should fail - decode error
const loader = new BufferLoader(offlineContext, err1File, audioBuffer => console.log(audioBuffer));
const loader = new BufferLoader(offlineContext, err1Files, audioBuffer => console.log(audioBuffer));
loader.load();
}

{
// should fail - file not found
const loader = new BufferLoader(offlineContext, err2File, audioBuffer => console.log(audioBuffer));
const loader = new BufferLoader(offlineContext, err2Files, audioBuffer => console.log(audioBuffer));
loader.load();
}

0 comments on commit 3207194

Please sign in to comment.