Skip to content

Commit

Permalink
fixed adding bundle metadata on stage when no main in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapaezbas committed Nov 22, 2024
1 parent 3a28dbc commit a1fe2c1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"brittle": "^3.5.2",
"graceful-goodbye": "^1.3.0",
"hyperschema": "^1.0.3",
"random-access-memory": "^6.2.1",
"standard": "^17.0.0"
},
"repository": {
Expand Down
9 changes: 8 additions & 1 deletion subsystems/sidecar/ops/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ module.exports = class Stage extends Opstream {
const opts = { ignore, dryRun, batch: true }
const builtins = isTerminal ? sidecar.gunk.bareBuiltins : sidecar.gunk.builtins
const linker = new ScriptLinker(src, { builtins })
const entrypoints = [...(state.manifest.main ? [state.main] : []), ...(state.manifest.pear?.stage?.entrypoints || [])].map((entry) => unixPathResolve('/', entry))

const mainExists = !!(await src.entry(unixPathResolve('/', state.main)))
const entrypoints = [
...(mainExists ? [state.main] : []),
...(state.manifest.pear?.stage?.entrypoints || [])
]
.map(entrypoint => unixPathResolve('/', entrypoint))
.filter(entrypoint => entrypoint !== null)

for (const entrypoint of entrypoints) {
const entry = await src.entry(entrypoint)
Expand Down
56 changes: 56 additions & 0 deletions test/07-warmup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ const test = require('brittle')
const path = require('bare-path')
const Helper = require('./helper')

const Hyperswarm = require('hyperswarm')
const Corestore = require('corestore')
const RAM = require('random-access-memory')
const Hyperdrive = require('hyperdrive')

const warmup = path.join(Helper.localDir, 'test', 'fixtures', 'warmup')
const desktop = path.join(Helper.localDir, 'test', 'fixtures', 'desktop-warmup')
const prefetch = path.join(Helper.localDir, 'test', 'fixtures', 'warmup-with-prefetch')
const appWithoutMain = path.join(Helper.localDir, 'test', 'fixtures', 'app-without-main')

test('stage warmup with entrypoints', async function ({ ok, is, plan, comment, teardown, timeout }) {
timeout(180000)
Expand Down Expand Up @@ -81,3 +87,53 @@ test('stage warmup with prefetch', async function ({ ok, is, plan, comment, tear
ok(warming.total > 0, 'Warmup total is correct')
is(warming.success, true, 'Warmup completed')
})

test('staged bundle contains entries metadata', async function ({ ok, is, plan, comment, teardown, timeout }) {
plan(2)

const dir = appWithoutMain

const helper = new Helper()
teardown(() => helper.close(), { order: Infinity })
await helper.ready()

const id = Math.floor(Math.random() * 10000)

comment('staging')
const staging = helper.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true })
teardown(() => Helper.teardownStream(staging))

const staged = await Helper.pick(staging, [{ tag: 'warming' }, { tag: 'final' }])
await staged.final

comment('seeding')
const seeding = helper.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] })
teardown(() => Helper.teardownStream(seeding))
const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }])
const key = await until.key
await until.announced

const swarm = new Hyperswarm({ bootstrap: Pear.config.dht.bootstrap })
const store = new Corestore(RAM)
await store.ready()
const drive = new Hyperdrive(store, key)
await drive.ready()

teardown(() => swarm.destroy())

swarm.on('connection', (conn) => {
drive.corestore.replicate(conn)
})

swarm.join(drive.discoveryKey)

await new Promise((resolve) => setTimeout(resolve, 500))

comment('bundle entries should contain metadata')
for await (const file of drive.list()) {
if (file.key === '/app.js' || file.key === '/dep.js') {
const entry = await drive.entry(file.key)
ok(entry.value.metadata)
}
}
})
1 change: 1 addition & 0 deletions test/fixtures/app-without-main/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import dep from './dep.js'
1 change: 1 addition & 0 deletions test/fixtures/app-without-main/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('-')
8 changes: 8 additions & 0 deletions test/fixtures/app-without-main/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<script type='module' src='./app.js'></script>
</head>
<body>
</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/app-without-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "app-without-main",
"pear": {
"name": "app-without-main",
"type": "desktop"
}
}

0 comments on commit a1fe2c1

Please sign in to comment.