Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support binary and text modules #13

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,14 @@ Options include:
{
isImport = false,
referrer = null,
type,
extensions,
protocol,
imports,
resolutions,
builtins,
conditions
conditions,
attributes
}
```

Expand Down
81 changes: 66 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const Module = module.exports = exports = class Module {
throw errors.MODULE_NOT_FOUND(msg)
}

const url = this.resolve(href, referrer._url, { isImport: true, referrer })
const url = this.resolve(href, referrer._url, { isImport: true, referrer, attributes })

const module = this.load(url, { isImport: true, isDynamicImport, referrer, attributes })

Expand Down Expand Up @@ -392,10 +392,10 @@ const Module = module.exports = exports = class Module {
module._builtins = builtins
module._conditions = conditions

let extension = extensionForType(type) || path.extname(url.pathname)
let extension = canonicalExtensionForType(type) || path.extname(url.pathname)

if (extension in self._extensions === false) {
if (defaultType) extension = extensionForType(defaultType) || '.js'
if (defaultType) extension = canonicalExtensionForType(defaultType) || '.js'
else extension = '.js'
}

Expand All @@ -422,6 +422,9 @@ const Module = module.exports = exports = class Module {
isImport = false,

referrer = null,
attributes,
type = typeForAttributes(attributes),
extensions = extensionsForType(type),
protocol = referrer ? referrer._protocol : self._protocol,
imports = referrer ? referrer._imports : null,
resolutions = referrer ? referrer._resolutions : null,
Expand All @@ -439,18 +442,11 @@ const Module = module.exports = exports = class Module {
conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
imports,
resolutions,
extensions,
builtins: builtins ? Object.keys(builtins) : [],
engines: {
...Bare.versions
},
extensions: [
'.js',
'.cjs',
'.mjs',
'.json',
'.bare',
'.node'
]
}
}, readPackage)) {
switch (resolution.protocol) {
case 'builtin:': return resolution
Expand Down Expand Up @@ -517,7 +513,28 @@ const Module = module.exports = exports = class Module {
}
}

function extensionForType (type) {
function extensionsForType (type) {
switch (type) {
case constants.types.SCRIPT:
return ['.js', '.cjs']
case constants.types.MODULE:
return ['.js', '.mjs']
case constants.types.JSON:
return ['.json']
case constants.types.BUNDLE:
return ['.json']
case constants.types.ADDON:
return ['.bare', '.node']
case constants.types.BINARY:
return ['.bin']
case constants.types.TEXT:
return ['.txt']
default:
return ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']
}
}

function canonicalExtensionForType (type) {
switch (type) {
case constants.types.SCRIPT:
return '.cjs'
Expand All @@ -529,6 +546,10 @@ function extensionForType (type) {
return '.bundle'
case constants.types.ADDON:
return '.bare'
case constants.types.BINARY:
return '.bin'
case constants.types.TEXT:
return '.txt'
default:
return null
}
Expand All @@ -548,6 +569,10 @@ function typeForAttributes (attributes) {
return constants.types.BUNDLE
case 'addon':
return constants.types.ADDON
case 'binary':
return constants.types.BINARY
case 'text':
return constants.types.TEXT
default:
return 0
}
Expand Down Expand Up @@ -611,9 +636,11 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
return require

function require (specifier, opts = {}) {
const resolved = self.resolve(specifier, referrer._url, { referrer })
const attributes = opts && opts.with

const resolved = self.resolve(specifier, referrer._url, { referrer, attributes })

const module = self.load(resolved, { referrer, attributes: opts.with })
const module = self.load(resolved, { referrer, attributes })

return module._exports
}
Expand Down Expand Up @@ -778,6 +805,30 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
}
}

Module._extensions['.bin'] = function (module, source, referrer) {
const protocol = module._protocol

module._type = constants.types.BINARY

if (source === null) source = protocol.read(module._url)

if (typeof source === 'string') source = Buffer.from(source)

module._exports = source
}

Module._extensions['.txt'] = function (module, source, referrer) {
const protocol = module._protocol

module._type = constants.types.TEXT

if (source === null) source = protocol.read(module._url)

if (typeof source !== 'string') source = Buffer.coerce(source).toString()

module._exports = source
}

Module._protocol = new Protocol({
postresolve (url) {
switch (url.protocol) {
Expand Down
4 changes: 3 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {
MODULE: 2,
JSON: 3,
BUNDLE: 4,
ADDON: 5
ADDON: 5,
BINARY: 6,
TEXT: 7
}
}
96 changes: 96 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,102 @@ test('load .bundle with asset import, resolutions map pointing outside .bundle',
t.is(Module.load(new URL(root + '/app.bundle'), bundle.toBuffer(), { protocol }).exports, isWindows ? 'c:\\bar.txt' : '/bar.txt')
})

test('load .js with .bin require', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists (url) {
return url.href === root + '/foo.bin'
},

read (url) {
if (url.href === root + '/index.js') {
return 'module.exports = require(\'./foo.bin\')'
}

if (url.href === root + '/foo.bin') {
return Buffer.from('hello world')
}

t.fail()
}
})

t.alike(Module.load(new URL(root + '/index.js'), { protocol }).exports, Buffer.from('hello world'))
})

test('load .js with .txt require', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists (url) {
return url.href === root + '/foo.txt'
},

read (url) {
if (url.href === root + '/index.js') {
return 'module.exports = require(\'./foo.txt\')'
}

if (url.href === root + '/foo.txt') {
return 'hello world'
}

t.fail()
}
})

t.is(Module.load(new URL(root + '/index.js'), { protocol }).exports, 'hello world')
})

test('load .js with .bin require, asserted type', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists (url) {
return url.href === root + '/asset'
},

read (url) {
if (url.href === root + '/index.js') {
return 'module.exports = require(\'./asset\', { with: { type: \'binary\' } })'
}

if (url.href === root + '/asset') {
return Buffer.from('hello world')
}

t.fail()
}
})

t.alike(Module.load(new URL(root + '/index.js'), { protocol }).exports, Buffer.from('hello world'))
})

test('load .js with .txt require, asserted type', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists (url) {
return url.href === root + '/asset'
},

read (url) {
if (url.href === root + '/index.js') {
return 'module.exports = require(\'./asset\', { with: { type: \'text\' } })'
}

if (url.href === root + '/asset') {
return 'hello world'
}

t.fail()
}
})

t.is(Module.load(new URL(root + '/index.js'), { protocol }).exports, 'hello world')
})

function onteardown () {
// TODO Provide a public API for clearing the cache.
Module._cache = Object.create(null)
Expand Down