-
Notifications
You must be signed in to change notification settings - Fork 68
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): Support text and bytes asset module types
- Loading branch information
Showing
20 changed files
with
217 additions
and
11 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
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
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,48 @@ | ||
// @ts-check | ||
|
||
/** | ||
* TypeScript cannot be relied upon to deal with the nuances of Readonly, so we | ||
* borrow the pass-through type definition of harden here. | ||
* | ||
* @type {import('ses').Harden} | ||
*/ | ||
const freeze = Object.freeze; | ||
|
||
/** @type {import('./types.js').ParseFn} */ | ||
export const parseBytes = async ( | ||
bytes, | ||
_specifier, | ||
_location, | ||
_packageLocation, | ||
) => { | ||
// Snapshot ArrayBuffer | ||
const buffer = new ArrayBuffer(bytes.length); | ||
const bytesView = new Uint8Array(buffer); | ||
bytesView.set(bytes); | ||
|
||
/** @type {Array<string>} */ | ||
const imports = freeze([]); | ||
|
||
/** | ||
* @param {Object} exports | ||
*/ | ||
const execute = exports => { | ||
exports.default = buffer; | ||
}; | ||
|
||
return { | ||
parser: 'bytes', | ||
bytes, | ||
record: freeze({ | ||
imports, | ||
exports: freeze(['default']), | ||
execute: freeze(execute), | ||
}), | ||
}; | ||
}; | ||
|
||
/** @type {import('./types.js').ParserImplementation} */ | ||
export default { | ||
parse: parseBytes, | ||
heuristicImports: false, | ||
}; |
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,47 @@ | ||
// @ts-check | ||
|
||
/** | ||
* TypeScript cannot be relied upon to deal with the nuances of Readonly, so we | ||
* borrow the pass-through type definition of harden here. | ||
* | ||
* @type {import('ses').Harden} | ||
*/ | ||
const freeze = Object.freeze; | ||
|
||
const textDecoder = new TextDecoder(); | ||
|
||
/** @type {import('./types.js').ParseFn} */ | ||
export const parseText = async ( | ||
bytes, | ||
_specifier, | ||
_location, | ||
_packageLocation, | ||
) => { | ||
const text = textDecoder.decode(bytes); | ||
|
||
/** @type {Array<string>} */ | ||
const imports = freeze([]); | ||
|
||
/** | ||
* @param {Object} exports | ||
*/ | ||
const execute = exports => { | ||
exports.default = text; | ||
}; | ||
|
||
return { | ||
parser: 'text', | ||
bytes, | ||
record: freeze({ | ||
imports, | ||
exports: freeze(['default']), | ||
execute: freeze(execute), | ||
}), | ||
}; | ||
}; | ||
|
||
/** @type {import('./types.js').ParserImplementation} */ | ||
export default { | ||
parse: parseText, | ||
heuristicImports: false, | ||
}; |
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
2 changes: 2 additions & 0 deletions
2
packages/compartment-mapper/test/fixtures-assets/.gitattributes
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,2 @@ | ||
*.bytes binary | ||
*.uint32 binary |
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 @@ | ||
Hello |
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,41 @@ | ||
import text from './text.text'; | ||
import bytes from './bytes.bytes'; | ||
import uint32 from './uint32.uint32'; | ||
|
||
// We normalize the module because Windows. | ||
// We normalize the string because editors don't always recognize | ||
// multi-codepoint glyphs and some padding before the quote doesn't hurt. | ||
if (text.trim() !== '🙂 '.trim()) { | ||
throw new Error( | ||
`Text module should export default string, got ${JSON.stringify(text)}`, | ||
); | ||
} | ||
|
||
if (!(bytes instanceof ArrayBuffer)) { | ||
throw new Error( | ||
'Binary module should export default that is instanceof ArrayBuffer', | ||
); | ||
} | ||
|
||
const expected = [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0a]; | ||
const numbers = new Uint8Array(bytes); | ||
expected.forEach((b, i) => { | ||
if (b !== numbers[i]) { | ||
throw new Error( | ||
`Unexpected imported byte ${numbers[i]} at index ${i}, expected ${b}`, | ||
); | ||
} | ||
}); | ||
|
||
const textForBytes = new TextDecoder().decode(bytes); | ||
if (textForBytes === 'Hello.\n') { | ||
throw new Error( | ||
`Unexpected text from bytes module, ${JSON.stringify(textForBytes)}`, | ||
); | ||
} | ||
|
||
const data = new DataView(uint32); | ||
const n = data.getUint32(0, false); | ||
if (n !== 1) { | ||
throw new Error('Bytes parser for "uint32" should be recognized'); | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/compartment-mapper/test/fixtures-assets/package.json
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,7 @@ | ||
{ | ||
"name": "assets", | ||
"type": "module", | ||
"parsers": { | ||
"uint32": "bytes" | ||
} | ||
} |
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 @@ | ||
🙂 |
Binary file not shown.
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 @@ | ||
00000000: 0000 0001 |
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,13 @@ | ||
import 'ses'; | ||
import test from 'ava'; | ||
import { scaffold } from './scaffold.js'; | ||
|
||
const fixture = new URL('fixtures-assets/main.js', import.meta.url).toString(); | ||
|
||
const fixtureAssertionCount = 1; | ||
|
||
const assertFixture = t => { | ||
t.pass(); | ||
}; | ||
|
||
scaffold('fixture-assets', test, fixture, assertFixture, fixtureAssertionCount); |