forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: Add support for fetch-style ESM hooks
- Loading branch information
Showing
5 changed files
with
218 additions
and
1 deletion.
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
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,50 @@ | ||
/** | ||
* @param {string} urlString | ||
* @param {string} fileExtension | ||
*/ | ||
function isFileExtensionURL(urlString, fileExtension) { | ||
const url = new URL(urlString); | ||
return url.protocol === 'file:' && url.pathname.endsWith(fileExtension); | ||
} | ||
|
||
/** | ||
* @param {Response} res | ||
* @param {string} mimeType | ||
* @param {string} fileExtension | ||
*/ | ||
function isType(res, mimeType, fileExtension) { | ||
const contentType = (res.headers.get('content-type') || '').toLocaleLowerCase( | ||
'en' | ||
); | ||
if (contentType === mimeType) { | ||
return true; | ||
} | ||
return !contentType && isFileExtensionURL(res.url, fileExtension); | ||
} | ||
|
||
function compile(source) { | ||
return `\ | ||
const data = ${JSON.stringify(source)}; | ||
console.log(import.meta.url, data); | ||
export default data; | ||
`; | ||
} | ||
|
||
function isCustomScript(res) { | ||
return isType(res, 'application/vnd.customscript', '.custom'); | ||
} | ||
|
||
self.addEventListener('fetch', event => { | ||
event.respondWith( | ||
fetch(event.request).then(async res => { | ||
if (res.status !== 200 || !isCustomScript(res)) { | ||
return res; | ||
} | ||
const source = await res.text(); | ||
const body = compile(source); | ||
const headers = new Headers(res.headers); | ||
headers.set('content-type', 'text/javascript'); | ||
return new Response(body, { headers }); | ||
}) | ||
); | ||
}); |
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 @@ | ||
Custom file format |