Skip to content

Commit

Permalink
Merge branch 'main' of github.com:digidem/mapeo-schema into feat/addD…
Browse files Browse the repository at this point in the history
…eletedToCommon
  • Loading branch information
Tomás Ciccola committed Sep 25, 2023
2 parents 81fe06f + 73c6d15 commit e67f5c9
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 20 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mapeo/schema",
"version": "3.0.0-next.8",
"version": "3.0.0-next.9",
"description": "JSON schema and flow types for Mapeo",
"main": "dist/index.js",
"type": "module",
Expand Down
6 changes: 4 additions & 2 deletions proto/common/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ message Common_1 {
// 32-byte random generated number
optional bytes docId = 1 [(required) = true];
message Link {
bytes coreKey = 1;
bytes coreDiscoveryKey = 1;
int32 index = 2;
}
repeated Link links = 2;
google.protobuf.Timestamp createdAt = 3 [(required) = true];
google.protobuf.Timestamp updatedAt = 4 [(required) = true];
optional bool deleted = 5;
// 32-byte hash of the discovery key of a core
bytes createdBy = 5 [(required) = true];
optional bool deleted = 6;
}
/* ignored fields and differences from common.json jsonSchema
* id is a byte buffer here and a string in jsonSchema
Expand Down
1 change: 1 addition & 0 deletions proto/observation/v5.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ message Observation_5 {
bytes driveId = 1;
string name = 2;
AttachmentType type = 3;
bytes hash = 4;
}
repeated Attachment attachments = 8;

Expand Down
9 changes: 7 additions & 2 deletions schema/common/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "string"
},
"versionId": {
"description": "core id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
},
"schemaName": {
Expand All @@ -22,6 +22,10 @@
"type": "string",
"format": "date-time"
},
"createdBy": {
"description": "discovery id (hex-encoded 32-byte buffer) of the hypercore where the first version of this document is written",
"type": "string"
},
"updatedAt": {
"description": "RFC3339-formatted datetime of when this version of the element was created",
"type": "string",
Expand All @@ -47,6 +51,7 @@
"schemaName",
"updatedAt",
"links",
"versionId"
"versionId",
"createdBy"
]
}
6 changes: 5 additions & 1 deletion schema/observation/v5.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@
"UNRECOGNIZED": "future attachment type"
},
"enum": ["photo", "video", "audio", "UNRECOGNIZED"]
},
"hash": {
"type": "string",
"description": "SHA256 hash of the attachment"
}
},
"required": ["driveId", "name", "type"]
"required": ["driveId", "name", "type", "hash"]
}
},
"tags": {
Expand Down
2 changes: 1 addition & 1 deletion src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
convertRole,
convertDeviceInfo,
convertCoreOwnership,
} from './lib/encode-converstions.js'
} from './lib/encode-conversions.js'
import { CoreOwnership } from './index.js'

/**
Expand Down
5 changes: 3 additions & 2 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export const convertObservation: ConvertFunction<'observation'> = (
...jsonSchemaCommon,
...rest,
refs: message.refs?.map(({ id }) => ({ id: id.toString('hex') })),
attachments: message.attachments?.map(({ driveId, name, type }) => {
return { driveId: driveId.toString('hex'), name, type }
attachments: message.attachments?.map(({ driveId, name, type, hash }) => {
return { driveId: driveId.toString('hex'), name, type, hash: hash.toString('hex') }
}),
tags: convertTags(message.tags),
metadata: message.metadata || {},
Expand Down Expand Up @@ -250,5 +250,6 @@ function convertCommon(
links: common.links.map((link) => getVersionId(link)),
createdAt: common.createdAt,
updatedAt: common.updatedAt,
createdBy: common.createdBy.toString('hex'),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const convertObservation: ConvertFunction<'observation'> = (
driveId: Buffer.from(attachment.driveId, 'hex'),
name: attachment.name,
type: attachment.type,
hash: Buffer.from(attachment.hash, 'hex'),
}
})

Expand Down Expand Up @@ -138,6 +139,7 @@ function convertCommon(
docId: Buffer.from(common.docId, 'hex'),
createdAt: common.createdAt,
updatedAt: common.updatedAt,
createdBy: Buffer.from(common.createdBy, 'hex'),
links: common.links.map((link) => parseVersionId(link)),
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function capitalize<T extends string>(str: T): Capitalize<T> {
}

export type VersionIdObject = {
coreKey: Buffer
coreDiscoveryKey: Buffer
index: number
}

Expand All @@ -27,8 +27,8 @@ export type VersionIdObject = {
* @param versionIdObject
* @returns versionId string
*/
export function getVersionId({ coreKey, index }: VersionIdObject) {
return coreKey.toString('hex') + '/' + index
export function getVersionId({ coreDiscoveryKey, index }: VersionIdObject) {
return coreDiscoveryKey.toString('hex') + '/' + index
}

/**
Expand All @@ -41,9 +41,9 @@ export function getVersionId({ coreKey, index }: VersionIdObject) {
export function parseVersionId(versionId: string): VersionIdObject {
const items = versionId.split('/')
if (!items[0] || !items[1]) throw new Error('Invalid versionId')
const coreKey = Buffer.from(items[0], 'hex')
if (coreKey.length !== 32) throw new Error('Invalid versionId')
const coreDiscoveryKey = Buffer.from(items[0], 'hex')
if (coreDiscoveryKey.length !== 32) throw new Error('Invalid versionId')
const index = Number.parseInt(items[1])
if (isNaN(index)) throw new Error('Invalid versionId')
return { coreKey, index }
return { coreDiscoveryKey, index }
}
6 changes: 5 additions & 1 deletion test/fixtures/cached.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ export const cachedValues = {
projectId: randomBytes(32).toString('hex'),
authorId: randomBytes(32).toString('hex'),
coreId: randomBytes(32).toString('hex'),
createdBy: randomBytes(32).toString('hex'),
createdAt: date,
updatedAt: date,
attachments: { driveId: randomBytes(32).toString('hex') },
attachments: {
driveId: randomBytes(32).toString('hex'),
hash: randomBytes(32).toString('hex'),
},
metadata: {
position: {
timestamp: date,
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/good-docs-completed.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const goodDocsCompleted = [
schemaName: 'observation',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
lat: 24.0424,
lon: 21.0214,
Expand All @@ -25,6 +26,7 @@ export const goodDocsCompleted = [
name: 'myFile',
type: 'photo',
driveId: cachedValues.attachments.driveId,
hash: cachedValues.attachments.hash,
},
],
tags: {
Expand Down Expand Up @@ -74,6 +76,7 @@ export const goodDocsCompleted = [
schemaName: 'projectSettings',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
defaultPresets: {
point: cachedValues.defaultPresets.point,
Expand All @@ -93,6 +96,7 @@ export const goodDocsCompleted = [
schemaName: 'field',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
tagKey: 'otherTagKey',
type: 'number',
Expand All @@ -116,6 +120,7 @@ export const goodDocsCompleted = [
schemaName: 'preset',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
name: 'myPreset',
geometry: ['point', 'vertex', 'line'],
Expand All @@ -142,6 +147,7 @@ export const goodDocsCompleted = [
schemaName: 'role',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
roleId: '6fd029a78243',
fromIndex: 5,
Expand All @@ -155,6 +161,7 @@ export const goodDocsCompleted = [
schemaName: 'deviceInfo',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
name: 'my device name',
},
Expand All @@ -167,6 +174,7 @@ export const goodDocsCompleted = [
schemaName: 'coreOwnership',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
authCoreId: Buffer.from('authCoreId').toString('hex'),
configCoreId: Buffer.from('configCoreId').toString('hex'),
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/good-docs-minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const goodDocsMinimal = [
schemaName: 'observation',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
refs: [],
attachments: [],
Expand All @@ -33,6 +34,7 @@ export const goodDocsMinimal = [
schemaName: 'projectSettings',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
},
expected: {},
Expand All @@ -44,6 +46,7 @@ export const goodDocsMinimal = [
schemaName: 'field',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
tagKey: 'myTagKey',
label: 'my label',
Expand All @@ -58,6 +61,7 @@ export const goodDocsMinimal = [
schemaName: 'preset',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
name: 'myPreset',
geometry: ['point'],
Expand All @@ -76,6 +80,7 @@ export const goodDocsMinimal = [
schemaName: 'role',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
roleId: '3b0104e370f9',
fromIndex: 5,
Expand All @@ -89,6 +94,7 @@ export const goodDocsMinimal = [
schemaName: 'deviceInfo',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
name: 'my device name',
},
Expand All @@ -101,6 +107,7 @@ export const goodDocsMinimal = [
schemaName: 'coreOwnership',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
authCoreId: Buffer.from('authCoreId').toString('hex'),
configCoreId: Buffer.from('configCoreId').toString('hex'),
Expand Down
Loading

0 comments on commit e67f5c9

Please sign in to comment.