8.6.2 • Published 2 days ago

@mapeo/core v8.6.2

Weekly downloads
67
License
MIT
Repository
github
Last release
2 days ago

@mapeo/core

Build
Status

Offline p2p mapping library.

npm install @mapeo/core

API

var Mapeo = require('@mapeo/core')

mapeo = new Mapeo(osm, media, opts)

Valid opts options include:

  • opts.deviceType (string): one of {'desktop', 'mobile'}. This affects how sync works. Mobile peers will not receive full-resolution media from other devices, but share them to desktop devices.
  • opts.internetDiscovery (boolean): set to true if you want to discover peers on the internet. Otherwise only local network peers will be sought after.

Observations API

A observation is a point on the map with particular metadata.

See the spec..

Validated fields:

  • type: (required) must be the string literal "observation"
  • attachments: (optional) array of attachments, each with an id.
  • lat: (required) latitude
  • lon: (required) longitude

mapeo.observationCreate(obs, cb)

Create an observation.

mapeo.observationGet(id, cb)

Get the observation with the given id.

mapeo.observationUpdate(newObservation, cb)

Update the observation. It requires valid version and id fields that reference the observation you are updating.

mapeo.observationDelete(id, cb)

Delete the observation with the given id. Also deletes attached media.

mapeo.observationStream(opts)

Returns a stream of observations. By default only the most recent forks are returned (e.g. it will will return only one version of each observation id), sorted by timestamp. Options for filtering the data can be passed according to the levelup createReadStream API.

Valid opts include:

  • opts.forks - Defaults to false. If true then if multiple heads/forks exist for an observation ID (e.g. if two users have edited the same observation and then synced) then all forks are returned. By default, when false, only the most recent fork is returned.

mapeo.observationList(opts, cb)

Returns an array of observations to the given callback. By default only the most recent forks are returned (e.g. it will will return only one version of each observation id), sorted by timestamp. Any errors are propagated as the first argument to the callback. Same options accepted as mapeo.observationStream.

mapeo.exportData(filename, opts, cb)

Exports data from the osm database to the given filename.

Valid opts include:

  • opts.format: "geojson" or a "shapefile". Shapefiles export into zip format.
  • opts.presets: an object that represents the contents of a presets.json file

mapeo.observationConvert(obs, cb)

Convert an observation to an OSM type node.

mapeo.getDeviceId(cb)

Retrieves the current device's unique ID (string).

Sync API

Mapeo core provides some key functionality for syncing between two devices over WiFi and filesystem (i.e., over USB sneakernets).

var sync = mapeo.sync

sync.listen(function () {

})

sync.setName(name)

Set the name of this peer / device, which will appear to others when they call sync.peers().

sync.listen(cb)

Broadcast and listen on the local network for peers. cb is called once the service is up and broadcasting.

sync.join(projectKey)

Join the swarm and begin making introductory connections with other peers.

Optionally accepts a projectKey which must be a 32-byte buffer or a string hex encoding of a 32-byte buffer. This will swarm only with peers that have also passed in the same projectKey.

An invalid projectKey will throw an error.

sync.leave(projectKey)

Leave the swarm and no longer be discoverable by other peers. Any currently open connections are kept until the swarm is destroyed (using close or destroy).

Optionally accepts a projectKey which must be a 32-byte buffer or a string hex encoding of a 32-byte buffer, to leave the same swarm you joined.

An invalid projectKey will throw an error.

sync.close(cb)

Unannounces the sync service & cleans up the underlying UDP socket. cb is called once this is complete.

sync.peers()

Fetch a list of the current sync peers that have been found thus far.

A peer can have the following properties:

  • name: a human-readable identifier for the peer (e.g., hostname)
  • connection: The open connection to this peer. Can be closed manually to stop any data transfer.
  • host: the ip
  • port: the port
  • type: 'wifi' or 'file'
  • deviceType: either mobile or desktop, if type == 'wifi'

sync.on('peer', peer)

Emitted when a new wifi peer connection is discovered.

var ev = sync.replicate(target, opts)

peer is an object with properties host, port, and name or an object with the filename property, for local file replication. Calls replicateFromFile or replicateNetwork below.

filename is a string specifying where the find the file to sync with. If it doesn't exist, it will be created, and use the format specified in the constructor's opts.writeFormat.

Both replicate and replicateNetwork return an EventEmitter ev. It can emit

  • "error" (err): gives an Error object signalling an error in syncing.
  • "progress" (progress): gives information about how many objects have been synced and how many to be synced in total, e.g. { db: { sofar: 5, total: 10 }, media: { sofar: 18, total: 100 } }
  • "end": successful completion of OSM and media sync.

Valid opts include:

  • opts.projectKey (string): a unique identifier that prohibits sync with a syncfile that declares a different project ID. If either side doesn't have a project ID set, sync will be permitted.

var ev = sync.replicateNetwork(peer)

peer should be an already-discoverable peer object, emitted from the peer event or returned on the peers() method.

If you want to replicate with a peer that is not discovered yet, but you have the host and port, we haven't made this easy at the moment. The code is written internally but not exposed via a public API. PRs welcome.

Importer API (expertimental)

The Importer allows you to import data to the osm database from other formats.

mapeo.importer

This object reports on import progress with the progress, complete, and error events.

For example,

var mapeo = new Mapeo(osm, media)
mapeo.importer.on('progress', console.log)
mapeo.importer.on('error', console.error)
mapeo.importer.on('complete', () => { process.exit() })
mapeo.importFeatureCollection(myGeoJsonFile)

mapeo.importer.importFeatureCollection(geojson, [cb])

Import data from a geojson string. This is simply a wrapper around osm-p2p-geojson.

mapeo.importer.importFromFile(filename, [cb])

Import data from a .geojson file or a .shp file.

Security Notes

"Can a passive listener learn about Mapeo project data?"

By default, peer discovery and sync happens over the local network only, and not the internet. If a malicious computer on the network is listening to packets, they will be able to learn of a project's "discovery key": a blake2b hash of the project's "project key". The discovery key is a mechanism to help peers in the same project find each other (on the local network or the internet), but on its own is insufficient to decrypt communications between peers. The project key must be known by a peer in order to decrypt any data exchanged. So, passive listeners will be able to ascertain a unique identifier for a project and learn which IPs are interested in it, but not any of the data exchanged by those peers.

Community

Connect with the Mapeo community for support & to contribute!

License

MIT

9.0.0-alpha.8

2 days ago

9.0.0-alpha.7

15 days ago

9.0.0-alpha.6

20 days ago

9.0.0-alpha.5

2 months ago

9.0.0-alpha.4

5 months ago

9.0.0-alpha.3

5 months ago

9.0.0-alpha.2

5 months ago

9.0.0-alpha.1

7 months ago

9.0.0-alpha.0

9 months ago

8.6.2

2 years ago

8.6.1

3 years ago

8.6.0

3 years ago

8.5.0

3 years ago

8.4.0

3 years ago

8.3.2

4 years ago

8.3.1

4 years ago

8.3.0

4 years ago

8.2.5

4 years ago

8.2.4

4 years ago

8.2.3

4 years ago

8.2.2

4 years ago

8.2.1

4 years ago

8.2.0

4 years ago

8.1.3

4 years ago

8.1.2

4 years ago

9.0.0

4 years ago

8.1.1

4 years ago

8.1.0

4 years ago

8.0.4

4 years ago

8.0.3

5 years ago

8.0.2

5 years ago

8.0.1

5 years ago

8.0.0

5 years ago

7.1.0

5 years ago

7.0.3

5 years ago

7.0.2

5 years ago

7.0.0

5 years ago

6.2.2

5 years ago

6.2.1

5 years ago

6.2.0

5 years ago

6.1.1

5 years ago

6.1.0

5 years ago

6.0.2

5 years ago

6.0.1

5 years ago

6.0.0

5 years ago

5.0.0

5 years ago

4.3.0

5 years ago

4.2.2

5 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago