Skip to content

Commit ed37fec

Browse files
committed
Remove redundant code and update tests and snapshots
1 parent 0adf1fd commit ed37fec

4 files changed

Lines changed: 13 additions & 143 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ The following changes have been implemented but not released yet:
240240

241241
### Bugfixes
242242

243-
- `getWellKnownSolid` fetches well known solid from the server's root by default.
243+
- `getWellKnownSolid` fetches well-known solid from the server's root by default.
244244

245245
## [1.22.0] - 2022-04-26
246246

src/resource/__snapshots__/solidDataset.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ exports[`getWellKnownSolid returns the contents of .well-known/solid for the giv
6161
{
6262
"graphs": {
6363
"default": {
64-
"_:n3-3": {
64+
"_:n3-2": {
6565
"predicates": {
6666
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type": {
6767
"namedNodes": [
@@ -99,7 +99,7 @@ exports[`getWellKnownSolid returns the contents of .well-known/solid for the giv
9999
},
100100
},
101101
"type": "Subject",
102-
"url": "_:n3-3",
102+
"url": "_:n3-2",
103103
},
104104
},
105105
},

src/resource/solidDataset.test.ts

Lines changed: 5 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,8 +3360,8 @@ describe("getWellKnownSolid", () => {
33603360
const mockESS = () =>
33613361
jest.spyOn(globalThis, "fetch").mockImplementation(essDiscoveryDoc);
33623362

3363-
it("fetches root well known solid by default", async () => {
3364-
// Fetches root well known
3363+
it("fetches root well-known solid by default", async () => {
3364+
// Fetches root well-known
33653365
mockESS();
33663366

33673367
await getWellKnownSolid(resourceUrl);
@@ -3373,109 +3373,17 @@ describe("getWellKnownSolid", () => {
33733373
);
33743374
});
33753375

3376-
it("uses the given fetcher for root well known solid if provided", async () => {
3377-
const mockFetch = jest.fn<typeof fetch>(essDiscoveryDoc);
3378-
3379-
await getWellKnownSolid(resourceUrl, { fetch: mockFetch });
3380-
3381-
expect(mockFetch).toHaveBeenCalledTimes(0);
3382-
// Unauthenticated fetch is still used to get the .well-known
3383-
expect(fetch).toHaveBeenCalledTimes(1);
3384-
expect(fetch).toHaveBeenCalledWith(
3385-
serverUrl.concat(wellKnownSolid),
3386-
expect.anything(),
3387-
);
3388-
});
3389-
3390-
it("appends a / to the Pod root if missing before appending .well-known/solid", async () => {
3391-
const spyFetch = jest.spyOn(globalThis, "fetch");
3392-
3393-
// Root cannot be fetched
3394-
spyFetch.mockResolvedValueOnce(new Response(undefined, { status: 404 }));
3395-
// Resource advertises Pod root
3396-
spyFetch.mockResolvedValueOnce(
3397-
mockResponse(
3398-
undefined,
3399-
{
3400-
headers: {
3401-
"Content-Type": "text/turtle",
3402-
link: `</username>; rel="http://www.w3.org/ns/pim/space#storage"`,
3403-
},
3404-
},
3405-
resourceUrl,
3406-
),
3407-
);
3408-
// Fetches Pod root well known
3409-
spyFetch.mockResolvedValueOnce(
3410-
new Response(
3411-
`{
3412-
"@context":"https://pod.inrupt.com/solid/v1",
3413-
"consent":"https://consent.pod.inrupt.com",
3414-
"notificationGateway":"https://notification.pod.inrupt.com",
3415-
"powerSwitch":"https://pod.inrupt.com/powerswitch/username",
3416-
"storage":"https://pod.inrupt.com/username/"
3417-
}`,
3418-
{
3419-
headers: {
3420-
"Content-Type": "application/ld+json",
3421-
},
3422-
},
3423-
),
3424-
);
3425-
3426-
await getWellKnownSolid(resourceUrl);
3427-
3428-
expect(spyFetch.mock.calls).toHaveLength(3);
3429-
3430-
// Tries the root well known solid first is used to determine well known Solid
3431-
expect(spyFetch.mock.calls[0][0]).toBe(serverUrl.concat(wellKnownSolid));
3432-
// Checks the resource's location header otherwise
3433-
expect(spyFetch.mock.calls[1][0]).toBe(resourceUrl);
3434-
// The advertised podIdentifier (as storage) is used to determine well known Solid
3435-
expect(spyFetch.mock.calls[2][0]).toBe(
3436-
serverUrl.concat("username/", wellKnownSolid),
3437-
);
3438-
});
3439-
3440-
it("Throws an error if the resource metadata can't be fetched", async () => {
3441-
const spyFetch = jest.spyOn(globalThis, "fetch");
3442-
3443-
// Can't fetch root well known
3444-
spyFetch.mockResolvedValueOnce(new Response(undefined, { status: 404 }));
3445-
// Resource advertises Pod root
3446-
spyFetch.mockResolvedValueOnce(
3447-
mockResponse(
3448-
undefined,
3449-
{
3450-
headers: {
3451-
"Content-Type": "text/turtle",
3452-
link: `</username>; rel="http://www.w3.org/ns/pim/space#storage"`,
3453-
},
3454-
},
3455-
resourceUrl,
3456-
),
3457-
);
3458-
// Can't fetch pod root well known solid
3459-
spyFetch.mockResolvedValueOnce(new Response(undefined, { status: 404 }));
3460-
3461-
await expect(getWellKnownSolid(resourceUrl)).rejects.toThrow();
3462-
3463-
expect(spyFetch.mock.calls).toHaveLength(3);
3464-
});
3465-
3466-
it("Throws an error if the pod root cannot be determined", async () => {
3376+
it("Throws an error if the well-known solid resource cannot be found", async () => {
34673377
const spyFetch = jest.spyOn(globalThis, "fetch");
34683378

3469-
// Can't fetch root well known
3379+
// Can't fetch root well-known
34703380
spyFetch.mockResolvedValueOnce(new Response(undefined, { status: 404 }));
3471-
// Resource does not advertise pod root
3472-
spyFetch.mockResolvedValueOnce(new Response(undefined));
34733381

34743382
await expect(getWellKnownSolid(resourceUrl)).rejects.toThrow(
34753383
"Could not determine storage root or well-known solid resource.",
34763384
);
34773385

3478-
expect(spyFetch.mock.calls).toHaveLength(2);
3386+
expect(spyFetch.mock.calls).toHaveLength(1);
34793387
});
34803388

34813389
it("returns the contents of .well-known/solid for the given resource", async () => {

src/resource/solidDataset.ts

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import type { Quad, NamedNode, Quad_Object, DatasetCore } from "@rdfjs/types";
2323
import { Store as N3Store } from "n3";
2424
import { DataFactory, toRdfJsQuads } from "../rdfjs.internal";
25-
import { ldp, pim } from "../constants";
26-
import { getJsonLdParser } from "../formats/jsonLd";
25+
import { ldp } from "../constants";
2726
import { triplesToTurtle, getTurtleParser } from "../formats/turtle";
2827
import { isLocalNode, isNamedNode, resolveIriForLocalNode } from "../datatypes";
2928
import type {
@@ -42,12 +41,10 @@ import { hasResourceInfo, hasChangelog } from "../interfaces";
4241
import { internal_toIriString, normalizeUrl } from "../interfaces.internal";
4342
import {
4443
getSourceUrl,
45-
getResourceInfo,
4644
isContainer,
4745
FetchError,
4846
responseToResourceInfo,
4947
getContentType,
50-
getLinkedResourceUrlAll,
5148
} from "./resource";
5249
import {
5350
internal_isUnsuccessfulResponse,
@@ -1117,14 +1114,10 @@ function resolveLocalIrisInThing(
11171114
* please see the [ESS
11181115
* Documentation](https://docs.inrupt.com/ess/latest/services/discovery-endpoint/#well-known-solid).
11191116
*
1120-
* **Note:** The data contained in this dataset has changed between ESS 1.1 and
1121-
* ESS 2.0, as such you will need to check for multiple predicates to support
1122-
* both versions.
1123-
*
11241117
* ```typescript
11251118
* const wellKnown = await getWellKnownSolid(resource);
11261119
*
1127-
* // The wellKnown dataset uses a blank node for the subject all of it’s predicates,
1120+
* // The wellKnown dataset uses a blank node for the subject all of its predicates,
11281121
* // such that we need to call getThingAll with acceptBlankNodes set to true to
11291122
* // retrieve back predicates contained within the dataset
11301123
* const wellKnownSubjects = getThingAll(wellKnown, {
@@ -1141,17 +1134,13 @@ function resolveLocalIrisInThing(
11411134
*
11421135
*
11431136
* @param url URL of a Resource.
1144-
* @param options Optional parameter `options.fetch`: An alternative `fetch`
1145-
* function to make the HTTP request, compatible with the browser-native [fetch
1146-
* API](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/fetch#parameters).
11471137
* @returns Promise resolving to a [[SolidDataset]] containing the data at
11481138
* '.well-known/solid' for the given Resource, or rejecting if fetching it
11491139
* failed.
11501140
* @since 1.12.0
11511141
*/
11521142
export async function getWellKnownSolid(
11531143
url: UrlString | Url,
1154-
options?: Partial<{ fetch?: typeof fetch } & ParseOptions>,
11551144
): Promise<SolidDataset & WithServerResourceInfo> {
11561145
const urlString = internal_toIriString(url);
11571146

@@ -1164,35 +1153,8 @@ export async function getWellKnownSolid(
11641153

11651154
return await getSolidDataset(wellKnownSolidUrl);
11661155
} catch {
1167-
// In case of error, do nothing and try to discover the .well-known
1168-
// at the pod's root.
1169-
}
1170-
1171-
// 1.1s implementation:
1172-
const resourceMetadata = await getResourceInfo(urlString, {
1173-
fetch: options?.fetch,
1174-
// Discovering the .well-known/solid document is useful even for resources
1175-
// we don't have access to.
1176-
ignoreAuthenticationErrors: true,
1177-
});
1178-
const linkedResources = getLinkedResourceUrlAll(resourceMetadata);
1179-
const rootResources = linkedResources[pim.storage];
1180-
const rootResource = rootResources?.length === 1 ? rootResources[0] : null;
1181-
// If pod root (storage) was advertised, retrieve well known solid from pod's root
1182-
if (rootResource !== null) {
1183-
const wellKnownSolidUrl = new URL(
1184-
".well-known/solid",
1185-
rootResource.endsWith("/") ? rootResource : `${rootResource}/`,
1186-
).href;
1187-
return getSolidDataset(wellKnownSolidUrl, {
1188-
...options,
1189-
parsers: {
1190-
"application/ld+json": getJsonLdParser(),
1191-
},
1192-
});
1156+
throw new Error(
1157+
"Could not determine storage root or well-known solid resource.",
1158+
);
11931159
}
1194-
1195-
throw new Error(
1196-
"Could not determine storage root or well-known solid resource.",
1197-
);
11981160
}

0 commit comments

Comments
 (0)