Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compartment-mapper): Add Node.js power adapter
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { makeNodeReadPowers, makeNodeWritePowers } from './src/node-powers.js'; |
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,49 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @param {typeof import('fs')} fs | ||
* @returns {ReadPowers} | ||
*/ | ||
export const makeNodeReadPowers = fs => { | ||
/** | ||
* @param {string} location | ||
*/ | ||
const read = async location => | ||
fs.promises.readFile(new URL(location).pathname); | ||
|
||
/** | ||
* @param {string} location | ||
*/ | ||
const canonical = async location => { | ||
try { | ||
if (location.endsWith('/')) { | ||
const realPath = await fs.promises.realpath( | ||
new URL(location).pathname.replace(/\/$/, ''), | ||
); | ||
return new URL(`${realPath}/`, location).toString(); | ||
} else { | ||
const realPath = await fs.promises.realpath(new URL(location).pathname); | ||
return new URL(realPath, location).toString(); | ||
} | ||
} catch { | ||
return location; | ||
} | ||
}; | ||
|
||
return { read, canonical }; | ||
}; | ||
|
||
/** | ||
* @param {typeof import('fs')} fs | ||
* @returns {WritePowers} | ||
*/ | ||
export const makeNodeWritePowers = fs => { | ||
/** | ||
* @param {string} location | ||
* @param {Uint8Array} data | ||
*/ | ||
const write = async (location, data) => | ||
fs.promises.writeFile(new URL(location).pathname, data); | ||
|
||
return { write }; | ||
}; |
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