forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: move esm loader hooks to worker thread
- Loading branch information
1 parent
d12d5ef
commit c4f2cf9
Showing
21 changed files
with
574 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* eslint-disable */ | ||
let nextId = 1; | ||
function getNewId() { | ||
const id = nextId; | ||
nextId++; | ||
return id; | ||
} | ||
|
||
class LoaderWorker { | ||
resolveImportURL() { | ||
throw new TypeError('Not implemented'); | ||
} | ||
|
||
getFormat() { | ||
throw new TypeError('Not implemented'); | ||
} | ||
|
||
getSource() { | ||
throw new TypeError('Not implemented'); | ||
} | ||
|
||
transformSource() { | ||
throw new TypeError('Not implemented'); | ||
} | ||
|
||
addBelowPort() { | ||
throw new TypeError('Not implemented'); | ||
} | ||
} | ||
|
||
class RemoteLoaderWorker { | ||
constructor(port) { | ||
this._port = port; | ||
this._pending = new Map(); | ||
|
||
this._port.on('message', this._handleMessage.bind(this)); | ||
this._port.unref(); | ||
} | ||
|
||
_handleMessage({ id, error, result }) { | ||
if (!this._pending.has(id)) return; | ||
const { resolve, reject } = this._pending.get(id); | ||
this._pending.delete(id); | ||
this._port.unref(); | ||
|
||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
} | ||
|
||
_send(method, params, transferList) { | ||
const id = getNewId(); | ||
const message = { id, method, params }; | ||
return new Promise((resolve, reject) => { | ||
this._pending.set(id, { resolve, reject }); | ||
this._port.postMessage(message, transferList); | ||
this._port.ref(); | ||
}); | ||
} | ||
} | ||
|
||
for (const method of Object.getOwnPropertyNames(LoaderWorker.prototype)) { | ||
Object.defineProperty(RemoteLoaderWorker.prototype, method, { | ||
configurable: true, | ||
enumerable: false, | ||
value(data, transferList) { | ||
return this._send(method, data, transferList); | ||
}, | ||
}); | ||
} | ||
|
||
function connectIncoming(port, instance) { | ||
port.on('message', async ({ id, method, params }) => { | ||
if (!id) return; | ||
|
||
let result = null; | ||
let error = null; | ||
try { | ||
if (typeof instance[method] !== 'function') { | ||
throw new TypeError(`No such RPC method: ${method}`); | ||
} | ||
result = await instance[method](params); | ||
} catch (e) { | ||
error = e; | ||
} | ||
port.postMessage({ id, error, result }); | ||
}); | ||
|
||
return instance; | ||
} | ||
|
||
module.exports = { | ||
connectIncoming, | ||
getNewId, | ||
LoaderWorker, | ||
RemoteLoaderWorker, | ||
}; |
Oops, something went wrong.