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

Add bundle.id #4

Merged
merged 1 commit into from
Oct 7, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The header length is an integer literal denoting the total length of the header.
```js
{
"version": 0,
"id": null | "<string>",
"main": null | "<url>",
"imports": {
"<from>": "<to>"
Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const Bundle = module.exports = exports = class Bundle {
} = opts

this._File = File
this._id = null
this._main = null
this._imports = {}
this._resolutions = {}
Expand All @@ -64,6 +65,18 @@ const Bundle = module.exports = exports = class Bundle {
return Bundle.version
}

get id () {
return this._id
}

set id (value) {
if (typeof value !== 'string' && value !== null) {
throw new TypeError(`ID must be a string or null. Received type ${typeof value} (${value})`)
}

this._id = value
}

get main () {
return this._main
}
Expand Down Expand Up @@ -196,6 +209,7 @@ const Bundle = module.exports = exports = class Bundle {

const header = {
version: this.version,
id: this.id,
main: this.main,
imports: this.imports,
resolutions: this.resolutions,
Expand Down Expand Up @@ -241,6 +255,7 @@ const Bundle = module.exports = exports = class Bundle {
return {
__proto__: { constructor: Bundle },

id: this.id,
main: this.main,
imports: this.imports,
resolutions: this.resolutions,
Expand Down Expand Up @@ -300,6 +315,7 @@ function fromBuffer (buffer) {
// Go through the public API setters to ensure that the header fields are
// validated.

if (header.id) bundle.id = header.id
if (header.main) bundle.main = header.main
if (header.imports) bundle.imports = header.imports
if (header.resolutions) bundle.resolutions = header.resolutions
Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ const Bundle = require('.')
test('basic', (t) => {
const bundle = new Bundle()

bundle.id = 'my-bundle'

bundle
.write('/foo.js', 'foo', { main: true })
.write('/bar.js', 'bar', { alias: 'bar' })
.write('/baz', 'baz', { executable: true })

t.is(bundle.id, 'my-bundle')
t.is(bundle.version, 0)
t.is(bundle.main, '/foo.js')
t.is(bundle.mode('/baz'), 0o755)
Expand Down