@noeldemartin/solid-utils 0.6.0 → 0.6.1-next.0d15181b9561088547ef2d0f22a04aef565e06bb

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/helpers/io.ts CHANGED
@@ -123,15 +123,25 @@ function preprocessSubjects(json: JsonLD): void {
123
123
  }
124
124
 
125
125
  json['@id'] = ANONYMOUS_PREFIX + json['@id'];
126
+
127
+ for (const [field, value] of Object.entries(json)) {
128
+ if (typeof value !== 'object' || value === null || !('@id' in value)) {
129
+ continue;
130
+ }
131
+
132
+ preprocessSubjects(json[field] as JsonLD);
133
+ }
126
134
  }
127
135
 
128
136
  function postprocessSubjects(quads: Quad[]): void {
129
137
  for (const quad of quads) {
130
- if (!quad.subject.value.startsWith(ANONYMOUS_PREFIX)) {
131
- continue;
138
+ if (quad.subject.value.startsWith(ANONYMOUS_PREFIX)) {
139
+ quad.subject.value = quad.subject.value.slice(ANONYMOUS_PREFIX_LENGTH);
132
140
  }
133
141
 
134
- quad.subject.value = quad.subject.value.slice(ANONYMOUS_PREFIX_LENGTH);
142
+ if (quad.object.value.startsWith(ANONYMOUS_PREFIX)) {
143
+ quad.object.value = quad.object.value.slice(ANONYMOUS_PREFIX_LENGTH);
144
+ }
135
145
  }
136
146
  }
137
147
 
@@ -202,6 +212,12 @@ export async function jsonldToQuads(json: JsonLD, baseIRI?: string): Promise<Qua
202
212
  return quads;
203
213
  }
204
214
 
215
+ export async function normalizeJsonLD(json: JsonLD, baseIRI?: string): Promise<JsonLD> {
216
+ const quads = await jsonldToQuads(structuredClone(json), baseIRI);
217
+
218
+ return quadsToJsonLD(quads);
219
+ }
220
+
205
221
  export function normalizeSparql(sparql: string): string {
206
222
  const quads = sparqlToQuadsSync(sparql);
207
223
 
@@ -10,11 +10,13 @@ describe('WAC helpers', () => {
10
10
 
11
11
  it('resolves relative ACL urls', async () => {
12
12
  // Arrange
13
- const server = new FakeServer();
14
13
  const documentUrl = 'https://example.com/alice/movies/my-favorite-movie';
15
14
 
16
- server.respondOnce(documentUrl, FakeResponse.success(undefined, { Link: '<my-favorite-movie.acl>;rel="acl"' }));
17
- server.respondOnce(
15
+ FakeServer.respondOnce(
16
+ documentUrl,
17
+ FakeResponse.success(undefined, { Link: '<my-favorite-movie.acl>;rel="acl"' }),
18
+ );
19
+ FakeServer.respondOnce(
18
20
  `${documentUrl}.acl`,
19
21
  FakeResponse.success(`
20
22
  @prefix acl: <http://www.w3.org/ns/auth/acl#>.
@@ -29,25 +31,27 @@ describe('WAC helpers', () => {
29
31
  );
30
32
 
31
33
  // Act
32
- const { url, effectiveUrl, document } = await fetchSolidDocumentACL(documentUrl, server.fetch);
34
+ const { url, effectiveUrl, document } = await fetchSolidDocumentACL(documentUrl, FakeServer.fetch);
33
35
 
34
36
  // Assert
35
37
  expect(url).toEqual(`${documentUrl}.acl`);
36
38
  expect(effectiveUrl).toEqual(url);
37
39
  expect(document.contains(`${documentUrl}.acl#owner`, 'rdf:type', 'acl:Authorization')).toBe(true);
38
40
 
39
- expect(server.getRequests()).toHaveLength(2);
40
- expect(server.getRequest(documentUrl)?.method).toEqual('HEAD');
41
- expect(server.getRequest(url)).not.toBeNull();
41
+ expect(FakeServer.getRequests()).toHaveLength(2);
42
+ expect(FakeServer.getRequest(documentUrl)?.method).toEqual('HEAD');
43
+ expect(FakeServer.getRequest(url)).not.toBeNull();
42
44
  });
43
45
 
44
46
  it('fails with ACP resources', async () => {
45
47
  // Arrange
46
- const server = new FakeServer();
47
48
  const documentUrl = 'https://example.com/alice/movies/my-favorite-movie';
48
49
 
49
- server.respondOnce(documentUrl, FakeResponse.success(undefined, { Link: '<my-favorite-movie.acl>;rel="acl"' }));
50
- server.respondOnce(
50
+ FakeServer.respondOnce(
51
+ documentUrl,
52
+ FakeResponse.success(undefined, { Link: '<my-favorite-movie.acl>;rel="acl"' }),
53
+ );
54
+ FakeServer.respondOnce(
51
55
  `${documentUrl}.acl`,
52
56
  FakeResponse.success(undefined, {
53
57
  Link: '<http://www.w3.org/ns/solid/acp#AccessControlResource>; rel="type"',
@@ -55,7 +59,7 @@ describe('WAC helpers', () => {
55
59
  );
56
60
 
57
61
  // Act
58
- const promisedDocument = fetchSolidDocumentACL(documentUrl, server.fetch);
62
+ const promisedDocument = fetchSolidDocumentACL(documentUrl, FakeServer.fetch);
59
63
 
60
64
  // Assert
61
65
  await expect(promisedDocument).rejects.toBeInstanceOf(UnsupportedAuthorizationProtocolError);
@@ -0,0 +1,4 @@
1
+ import { FakeServer } from '@noeldemartin/testing';
2
+ import { beforeEach } from 'vitest';
3
+
4
+ beforeEach(() => FakeServer.reset());