fix
This commit is contained in:
parent
8dca0641f5
commit
cc44d46da7
|
@ -2,13 +2,13 @@
|
|||
/// <reference path="../types/jsonldDocumentLoader.d.ts" />
|
||||
import getNodeDocumentLoader from 'jsonld/lib/documentLoaders/node';
|
||||
import * as jsonld from 'jsonld';
|
||||
import * as AP from '@activity-kit/types';
|
||||
import { cast } from '@activity-kit/type-utilities';
|
||||
import { RemoteDocument } from 'jsonld/jsonld-spec';
|
||||
import {
|
||||
ACTIVITYSTREAMS_CONTEXT,
|
||||
CONTEXT_DEFINITIONS,
|
||||
DEFAULT_ACTOR_CONTEXT,
|
||||
SCHEMA_ORG_CONTEXT,
|
||||
W3ID_SECURITY_CONTEXT,
|
||||
} from './globals';
|
||||
|
||||
/**
|
||||
|
@ -23,16 +23,32 @@ import {
|
|||
export async function compactJsonObject(
|
||||
object: Record<string, unknown>,
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
const document = { ...object };
|
||||
let document = { ...object };
|
||||
|
||||
if (!('@context' in document)) {
|
||||
document['@context'] = DEFAULT_ACTOR_CONTEXT;
|
||||
if ('@context' in document) {
|
||||
const [expanded] = await jsonld.expand(document, {
|
||||
documentLoader: customLoader,
|
||||
});
|
||||
|
||||
document = expanded;
|
||||
} else {
|
||||
document['@context'] = {
|
||||
'@vocab': ACTIVITYSTREAMS_CONTEXT,
|
||||
sec: W3ID_SECURITY_CONTEXT,
|
||||
schema: SCHEMA_ORG_CONTEXT,
|
||||
id: '@id',
|
||||
type: '@type',
|
||||
};
|
||||
}
|
||||
|
||||
const result = await jsonld.compact(
|
||||
document,
|
||||
{
|
||||
'@context': ACTIVITYSTREAMS_CONTEXT,
|
||||
'@vocab': ACTIVITYSTREAMS_CONTEXT,
|
||||
sec: W3ID_SECURITY_CONTEXT,
|
||||
schema: SCHEMA_ORG_CONTEXT,
|
||||
id: '@id',
|
||||
type: '@type',
|
||||
},
|
||||
{
|
||||
documentLoader: customLoader,
|
||||
|
|
|
@ -56,7 +56,7 @@ export const CONTEXT_KEY = '@context';
|
|||
/**
|
||||
* The JSON-LD context for ActivityPub.
|
||||
*/
|
||||
export const ACTIVITYSTREAMS_CONTEXT = 'https://www.w3.org/ns/activitystreams';
|
||||
export const ACTIVITYSTREAMS_CONTEXT = 'https://www.w3.org/ns/activitystreams#';
|
||||
|
||||
/**
|
||||
* The JSON-LD context for the W3ID security vocabulary.
|
||||
|
@ -343,7 +343,7 @@ export const CONTEXT_DEFINITIONS: Record<string, jsonld.ContextDefinition> = {
|
|||
[ACTIVITYSTREAMS_CONTEXT]: {
|
||||
'@vocab': ACTIVITYSTREAMS_CONTEXT,
|
||||
xsd: 'http://www.w3.org/2001/XMLSchema#',
|
||||
as: `${ACTIVITYSTREAMS_CONTEXT}#`,
|
||||
as: ACTIVITYSTREAMS_CONTEXT,
|
||||
ldp: LDP_CONTEXT,
|
||||
vcard: 'http://www.w3.org/2006/vcard/ns#',
|
||||
id: '@id',
|
||||
|
|
|
@ -1,21 +1,63 @@
|
|||
import 'jasmine';
|
||||
|
||||
import { compactJsonObject } from '../src/compactJsonObject';
|
||||
import {
|
||||
ACTIVITYSTREAMS_CONTEXT,
|
||||
CONTEXT_KEY,
|
||||
SCHEMA_ORG_CONTEXT,
|
||||
W3ID_SECURITY_CONTEXT,
|
||||
} from '../src/globals';
|
||||
|
||||
describe('compactJsonObject', () => {
|
||||
it('should maintain JSON object', async () => {
|
||||
it('should maintain JSON-LD object with known contexts', async () => {
|
||||
const compacted = await compactJsonObject({
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
id: 'https://example.com',
|
||||
[CONTEXT_KEY]: ACTIVITYSTREAMS_CONTEXT,
|
||||
id: 'https://example.com/',
|
||||
type: 'Person',
|
||||
name: 'Example',
|
||||
});
|
||||
|
||||
expect(compacted).toEqual({
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'@context': {
|
||||
'@vocab': ACTIVITYSTREAMS_CONTEXT,
|
||||
sec: W3ID_SECURITY_CONTEXT,
|
||||
schema: SCHEMA_ORG_CONTEXT,
|
||||
id: '@id',
|
||||
type: '@type',
|
||||
},
|
||||
id: 'https://example.com/',
|
||||
type: 'Person',
|
||||
name: 'Example',
|
||||
});
|
||||
});
|
||||
|
||||
it('should maintain JSON-LD object with unknown contexts', async () => {
|
||||
const compacted = await compactJsonObject({
|
||||
'@context': {
|
||||
'@vocab': ACTIVITYSTREAMS_CONTEXT,
|
||||
changeset: 'http://purl.org/vocab/changeset/schema#',
|
||||
id: '@id',
|
||||
type: '@type',
|
||||
},
|
||||
id: 'https://example.com',
|
||||
type: 'Person',
|
||||
name: 'Example',
|
||||
'changeset:createdDate': '2021-01-01T00:00:00Z',
|
||||
});
|
||||
|
||||
expect(compacted).toEqual({
|
||||
'@context': {
|
||||
'@vocab': ACTIVITYSTREAMS_CONTEXT,
|
||||
sec: W3ID_SECURITY_CONTEXT,
|
||||
schema: SCHEMA_ORG_CONTEXT,
|
||||
id: '@id',
|
||||
type: '@type',
|
||||
},
|
||||
id: 'https://example.com',
|
||||
type: 'Person',
|
||||
name: 'Example',
|
||||
'http://purl.org/vocab/changeset/schema#createdDate':
|
||||
'2021-01-01T00:00:00Z',
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -31,7 +73,13 @@ describe('compactJsonObject', () => {
|
|||
});
|
||||
|
||||
expect(compacted).toEqual({
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'@context': {
|
||||
'@vocab': ACTIVITYSTREAMS_CONTEXT,
|
||||
sec: W3ID_SECURITY_CONTEXT,
|
||||
schema: SCHEMA_ORG_CONTEXT,
|
||||
id: '@id',
|
||||
type: '@type',
|
||||
},
|
||||
id: 'https://example.com',
|
||||
type: 'Person',
|
||||
name: 'Example',
|
||||
|
|
Loading…
Reference in New Issue