From 3207194b5cf4f390c8fc700c7ef72b30eb2e3092 Mon Sep 17 00:00:00 2001 From: b-ma Date: Thu, 28 Dec 2023 19:29:16 +0100 Subject: [PATCH] fix: allow to configure XMLHttpRequest root --- .scripts/wpt-mock/XMLHttpRequest.js | 48 ++++++++++++++------------ .scripts/wpt-mock/wpt-buffer-loader.js | 18 ++++++---- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/.scripts/wpt-mock/XMLHttpRequest.js b/.scripts/wpt-mock/XMLHttpRequest.js index d18773b1..4ac9fb21 100644 --- a/.scripts/wpt-mock/XMLHttpRequest.js +++ b/.scripts/wpt-mock/XMLHttpRequest.js @@ -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; diff --git a/.scripts/wpt-mock/wpt-buffer-loader.js b/.scripts/wpt-mock/wpt-buffer-loader.js index f21adbd6..bc916bb7 100644 --- a/.scripts/wpt-mock/wpt-buffer-loader.js +++ b/.scripts/wpt-mock/wpt-buffer-loader.js @@ -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); @@ -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(); }