Skip to content
Open
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
21 changes: 21 additions & 0 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class MapeoManager extends TypedEmitter {
#keyManager
#projectSettingsIndexWriter
#db
#sqlite
// Maps project public id -> project instance
/** @type {Map<string, MapeoProject>} */
#activeProjects
Expand Down Expand Up @@ -182,6 +183,8 @@ export class MapeoManager extends TypedEmitter {
},
})

this.#sqlite = sqlite

this.#localPeers = new LocalPeers({ logger })
this.#localPeers.on('peers', (peers) => {
this.emit('local-peers', omitPeerProtomux(peers))
Expand Down Expand Up @@ -734,6 +737,11 @@ export class MapeoManager extends TypedEmitter {
throw e
}

// Make sure to clean up when closed
project.once('close', () => {
this.#activeProjects.delete(projectPublicId)
})

try {
const deviceInfo = this.getDeviceInfo()
if (hasSavedDeviceInfo(deviceInfo)) {
Expand Down Expand Up @@ -1057,6 +1065,19 @@ export class MapeoManager extends TypedEmitter {
await pTimeout(this.#fastify.ready(), { milliseconds: 1000 })
return (await this.#getMediaBaseUrl('maps')) + '/style.json'
}

/**
* Cleans up open resorces and closes open projects
* @returns {Promise<void>}
*/
async close() {
// This added for workers PR
// await this.#projectSettingsIndexWriter.close()
await Promise.all(
[...this.#activeProjects.values()].map((project) => project.close())
)
this.#sqlite.close()
}
}

// We use the `protomux` property of connected peers internally, but we don't
Expand Down
63 changes: 13 additions & 50 deletions test-e2e/device-info.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { randomBytes } from 'crypto'
import { KeyManager } from '@mapeo/crypto'
import RAM from 'random-access-memory'
import Fastify from 'fastify'
import { pEvent } from 'p-event'
import { connectPeers, createManagers, waitForPeers } from './utils.js'

import { MapeoManager } from '../src/mapeo-manager.js'

const projectMigrationsFolder = new URL('../drizzle/project', import.meta.url)
.pathname
const clientMigrationsFolder = new URL('../drizzle/client', import.meta.url)
.pathname

test('write and read deviceInfo', async () => {
const fastify = Fastify()
const rootKey = KeyManager.generateRootKey()
const manager = new MapeoManager({
rootKey,
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify,
})

import {
connectPeers,
createManagers,
createManager,
waitForPeers,
} from './utils.js'

test('write and read deviceInfo', async (t) => {
const manager = createManager('test', t)

await manager.setDeviceInfo({ name: 'my device', deviceType: 'tablet' })
assert.deepEqual(manager.getDeviceInfo(), {
Expand Down Expand Up @@ -55,15 +42,7 @@ test('write and read deviceInfo', async () => {

test('device info written to projects', async (t) => {
await t.test('when creating project', async () => {
const fastify = Fastify()
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify,
})
const manager = createManager('test', t)

await manager.setDeviceInfo({ name: 'mapeo', deviceType: 'tablet' })

Expand All @@ -78,15 +57,7 @@ test('device info written to projects', async (t) => {
})

await t.test('when adding project', async () => {
const fastify = Fastify()
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify,
})
const manager = createManager('test', t)

await manager.setDeviceInfo({ name: 'mapeo', deviceType: 'tablet' })

Expand All @@ -108,15 +79,7 @@ test('device info written to projects', async (t) => {
})

await t.test('after updating global device info', async () => {
const fastify = Fastify()
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify,
})
const manager = createManager('test', t)

await manager.setDeviceInfo({ name: 'before', deviceType: 'tablet' })

Expand Down
75 changes: 11 additions & 64 deletions test-e2e/manager-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,14 @@ import assert from 'node:assert/strict'
import { randomBytes, createHash } from 'crypto'
import { KeyManager } from '@mapeo/crypto'
import RAM from 'random-access-memory'
import { MapeoManager } from '../src/mapeo-manager.js'
import { getExpectedConfig, createManager } from './utils.js'
import { MapeoProject } from '../src/mapeo-project.js'
import Fastify from 'fastify'
import { getExpectedConfig } from './utils.js'
import { defaultConfigPath } from '../test/helpers/default-config.js'
import { kDataTypes } from '../src/mapeo-project.js'
import { hashObject } from '../src/utils.js'

const projectMigrationsFolder = new URL('../drizzle/project', import.meta.url)
.pathname
const clientMigrationsFolder = new URL('../drizzle/client', import.meta.url)
.pathname

test('Managing created projects', async (t) => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify: Fastify(),
})
const manager = createManager('test', t)

const project1Id = await manager.createProject()
const project2Id = await manager.createProject({ name: 'project 2' })
Expand Down Expand Up @@ -181,13 +167,7 @@ test('Managing created projects', async (t) => {
})

test('Consistent loading of config', async (t) => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify: Fastify(),
const manager = createManager('test', t, {
defaultConfigPath,
})

Expand Down Expand Up @@ -297,14 +277,7 @@ test('Consistent loading of config', async (t) => {
})

test('Managing added projects', async (t) => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify: Fastify(),
})
const manager = createManager('test', t)

const project1Id = await manager.addProject(
{
Expand Down Expand Up @@ -382,15 +355,8 @@ test('Managing added projects', async (t) => {
)
})

test('Managing both created and added projects', async () => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify: Fastify(),
})
test('Managing both created and added projects', async (t) => {
const manager = createManager('test', t)

const createdProjectId = await manager.createProject({
name: 'created project',
Expand Down Expand Up @@ -426,16 +392,8 @@ test('Managing both created and added projects', async () => {
assert(addedProject)
})

test('Manager cannot add project that already exists', async () => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify: Fastify(),
})

test('Manager cannot add project that already exists', async (t) => {
const manager = createManager('test', t)
const existingProjectId = await manager.createProject()

const existingProjectsCountBefore = (await manager.listProjects()).length
Expand All @@ -455,15 +413,11 @@ test('Manager cannot add project that already exists', async () => {
assert.equal(existingProjectsCountBefore, existingProjectsCountAfter)
})

test('Consistent storage folders', async () => {
test('Consistent storage folders', async (t) => {
/** @type {string[]} */
const storageNames = []
const manager = new MapeoManager({
const manager = createManager('test', t, {
rootKey: randomBytesSeed('root_key').subarray(0, 16),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
fastify: Fastify(),
coreStorage: (name) => {
storageNames.push(name)
return new RAM()
Expand Down Expand Up @@ -492,14 +446,7 @@ test('Consistent storage folders', async () => {
})

test('Reusing port after start/stop of discovery', async (t) => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
fastify: Fastify(),
})
const manager = createManager('test', t)

t.after(() => manager.stopLocalPeerDiscoveryServer())

Expand Down
Loading
Loading