Skip to content

Commit 736bf22

Browse files
committed
add createLocalPod.js
1 parent 68a8cb8 commit 736bf22

File tree

7 files changed

+186
-4
lines changed

7 files changed

+186
-4
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
33
-- **a nodejs client for Solid** --
44

5-
This library uses [solid-auth-fetcher](https://github.com/solid/solid-auth-fetcher) to provide session management and fetching for https: URLs and uses [solid-rest](https://github.com/solid/solid-rest) to provide solid-like fetches on file: and other non-http URLs. It supports login and authenticated fetches to NSS servers. Support for login to other servers will be added in the future.
5+
This library provides access to [Solid](https://solidproject.org/) from the command line and from local scripts and apps which either run in the console or in a local browser based context like electron. It can be used to move resources back and forth between a local commputer and one or more pods, carry out remote operations on pods, and can also treat your local file system as a serverless pod.
6+
7+
Solid-Node-Client uses [solid-auth-fetcher](https://github.com/solid/solid-auth-fetcher) to provide session management and fetching for https: URLs and uses [solid-rest](https://github.com/solid/solid-rest) to provide solid-like fetches on file: and other non-http URLs. It supports login and authenticated fetches to NSS servers. Support for login to other servers will be added in the future.
8+
9+
Solid-node-client can be used stand-alone, or can be used with [rdflib](https://github.com/linkeddata/rdflib.js), [solid-file-client](https://github.com/jeff-zucker/solid-file-client/), and most other Solid apps. See [creating a serverless pod](#pod) for additional details.
610

711
As of version 1.2.0, solid node client now supports multiple logins from the same script. See [below](#multiple) for details
812

@@ -130,6 +134,16 @@ The solid-auth-fetcher session and authFetcher objects are available for each cl
130134
let authFetcher = client.authFetcher ...
131135
```
132136
137+
## <a name="pod">Creating a serverless pod</a>
138+
139+
Solid Node Client can access the local file system using file:// URLs without using a server. In most respects, it will treat the file system as a pod. To get the full benefit of this, it's best to create some local files such as a profile, a preferences file, and type indexes. You can create them manually or copy them from a remote pod, but the easiest thing to do is use the built-in createLocalPod method.
140+
```javascript
141+
import {SolidNodeClient} from 'solid-node-client';
142+
const client = new SolidNodeClient();
143+
client.createLocalPod('file:///home/jeff/myPod/');
144+
```
145+
The code above will create a profile, preferences and other key pod resources in the named folder. Your profile will be located at '/home/jeff/myPod/profile/card' and your preferences file will be located at '/home/jeff/myPod/settings/prefs.ttl'. You can now use a file:// URL to refer to your local webId: <file:///home/jeff/myPod/profile/card#me>. As with a server-based pod, this webId is the starting point for any app that wants to follow its nose through your local file system.
146+
133147
## Acknowledgements
134148
135149
All of the session management is from Jackson Creed's solid-auth-fetcher. The login is from Michiel de Jong's solid-crud-tests.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solid-node-client",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "a client for nodejs access to Solid",
55
"main": "./src/index.js",
66
"scripts": {

src/createLocalPod.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/createLocalPod.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
export const profile_content = `@prefix : <#>.
3+
@prefix jef: </>.
4+
@prefix ldp: <http://www.w3.org/ns/ldp#>.
5+
@prefix inbox: <../inbox/>.
6+
@prefix pro: <./>.
7+
@prefix ter: <http://www.w3.org/ns/solid/terms#>.
8+
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
9+
@prefix pim: <http://www.w3.org/ns/pim/space#>.
10+
@prefix n2: <http://>.
11+
@prefix n3: <https://>.
12+
@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
13+
@prefix sch: <http://schema.org/>.
14+
@prefix n1: <http://www.w3.org/ns/auth/acl#>.
15+
16+
pro:card a foaf:PersonalProfileDocument; foaf:maker :me; foaf:primaryTopic :me.
17+
18+
:me
19+
a sch:Person, foaf:Person;
20+
vcard:fn "Local Solid User";
21+
vcard:role "software developer";
22+
n1:trustedApp
23+
[
24+
n1:mode n1:Append, n1:Control, n1:Read, n1:Write;
25+
n1:origin <http://example.org>
26+
];
27+
ldp:inbox inbox:;
28+
pim:preferencesFile <../settings/prefs.ttl>;
29+
pim:storage jef:;
30+
ter:account jef:;
31+
ter:privateTypeIndex <../settings/privateTypeIndex.ttl>;
32+
ter:publicTypeIndex <../settings/publicTypeIndex.ttl>;
33+
foaf:name "Local Solid User".
34+
`;
35+
36+
export const prefs_content = `@prefix : <#>.
37+
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
38+
@prefix sp: <http://www.w3.org/ns/pim/space#>.
39+
@prefix dct: <http://purl.org/dc/terms/>.
40+
@prefix c: <../profile/card#>.
41+
@prefix mee: <http://www.w3.org/ns/pim/meeting#>.
42+
43+
<> a sp:ConfigurationFile; dct:title "Preferences file".
44+
45+
c:me
46+
a solid:Developer, solid:PowerUser;
47+
solid:privateTypeIndex <privateTypeIndex.ttl>;
48+
solid:publicTypeIndex <publicTypeIndex.ttl>.
49+
`;
50+
51+
export const public_content = `@prefix : <#>.
52+
@prefix solid: <https://www.w3.org/ns/solid/terms#>.
53+
@prefix terms: <http://purl.org/dc/terms/>.
54+
@prefix ter: <http://www.w3.org/ns/solid/terms#>.
55+
@prefix bookm: <http://www.w3.org/2002/01/bookmark#>.
56+
57+
<> a solid:ListedDocument, solid:TypeIndex; terms:references :Bookmark.
58+
59+
:Bookmark
60+
a ter:TypeRegistration;
61+
ter:forClass bookm:Bookmark;
62+
ter:instance <../bookmarks.ttl>.
63+
`;
64+
export const private_content = `
65+
@prefix solid: <https://www.w3.org/ns/solid/terms#>.
66+
<>
67+
a solid:TypeIndex ;
68+
a solid:UnlistedDocument.
69+
`;
70+
71+
/* ENDS */
72+

src/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4141
exports.__esModule = true;
4242
exports.SolidNodeClient = void 0;
4343
var solid_auth_fetcher_1 = require("solid-auth-fetcher");
44+
var createLocalPod_js_1 = require("./createLocalPod.js");
4445
var node_fetch_1 = __importDefault(require("node-fetch"));
4546
var solid_rest_1 = __importDefault(require("solid-rest"));
4647
var SolidNodeClient = /** @class */ (function () {
@@ -143,6 +144,55 @@ var SolidNodeClient = /** @class */ (function () {
143144
});
144145
});
145146
};
147+
SolidNodeClient.prototype.createLocalPod = function (base) {
148+
return __awaiter(this, void 0, void 0, function () {
149+
return __generator(this, function (_a) {
150+
switch (_a.label) {
151+
case 0:
152+
base = base.replace(/\/$/, '');
153+
return [4 /*yield*/, this.makeResource(base, "/profile/card", createLocalPod_js_1.profile_content)];
154+
case 1:
155+
_a.sent();
156+
return [4 /*yield*/, this.makeResource(base, "/settings/prefs.ttl", createLocalPod_js_1.prefs_content)];
157+
case 2:
158+
_a.sent();
159+
return [4 /*yield*/, this.makeResource(base, "/settings/privateTypeIndex.ttl", createLocalPod_js_1.private_content)];
160+
case 3:
161+
_a.sent();
162+
return [4 /*yield*/, this.makeResource(base, "/settings/publicTypeIndex.ttl", createLocalPod_js_1.public_content)];
163+
case 4:
164+
_a.sent();
165+
return [4 /*yield*/, this.makeResource(base, "/public/.meta", "")];
166+
case 5:
167+
_a.sent();
168+
return [4 /*yield*/, this.makeResource(base, "/inbox/.meta", "")];
169+
case 6:
170+
_a.sent();
171+
return [2 /*return*/];
172+
}
173+
});
174+
});
175+
};
176+
SolidNodeClient.prototype.makeResource = function (base, path, content) {
177+
return __awaiter(this, void 0, void 0, function () {
178+
var url;
179+
return __generator(this, function (_a) {
180+
switch (_a.label) {
181+
case 0:
182+
url = base + path;
183+
console.log("Creating " + url);
184+
return [4 /*yield*/, this.fetch(url, {
185+
method: "PUT",
186+
body: content,
187+
headers: { "content-type": "text/turtle" }
188+
})];
189+
case 1:
190+
_a.sent();
191+
return [2 /*return*/];
192+
}
193+
});
194+
});
195+
};
146196
return SolidNodeClient;
147197
}());
148198
exports.SolidNodeClient = SolidNodeClient;

src/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { getSession, getNodeSolidServerCookie, getAuthFetcher }
22
from "solid-auth-fetcher";
3+
import {profile_content,prefs_content,private_content,public_content}
4+
from './createLocalPod.js';
35
import fetch from "node-fetch";
46
import SolidRest from "solid-rest";
57

@@ -58,10 +60,29 @@ export class SolidNodeClient {
5860
}
5961
this.session = new NodeNoAuthSession({rest:this.rest});
6062
this.authFetcher = null;
61-
}
62-
async currentSession(){
63+
}
64+
async currentSession(){
6365
return ( this.session.loggedIn ) ? this.session : null ;
6466
}
67+
async createLocalPod( base:string ){
68+
base = base.replace(/\/$/,'');
69+
await this.makeResource( base,"/profile/card", profile_content );
70+
await this.makeResource( base,"/settings/prefs.ttl", prefs_content );
71+
await this.makeResource(base,"/settings/privateTypeIndex.ttl",private_content );
72+
await this.makeResource( base,"/settings/publicTypeIndex.ttl", public_content );
73+
await this.makeResource( base,"/public/.meta", "" );
74+
await this.makeResource( base,"/inbox/.meta", "" );
75+
}
76+
async makeResource( base:string, path:string, content:string ){
77+
let url = base + path
78+
console.log ( "Creating " + url )
79+
await this.fetch( url, {
80+
method:"PUT",
81+
body:content,
82+
headers:{"content-type":"text/turtle"}
83+
})
84+
}
85+
6586
}
6687

6788
/** UNAUTHENTICATED SESSION

tests/createLocalPod.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {SolidNodeClient} from '../';
2+
const client = new SolidNodeClient();
3+
// import * as readline from 'readline-sync';
4+
5+
async function main(){
6+
/*
7+
console.log('\nThis will create a local file-system-based pod.')
8+
console.log(
9+
'Enter a full path to the place you want your pod (e.g. /home/jeff/myPod/)'
10+
);
11+
let basePath = await readline.question(' > ').replace(/\/$/,'');
12+
*/
13+
let basePath = "/home/jeff/myPod/";
14+
client.createLocalPod(basePath);
15+
}
16+
main()
17+

0 commit comments

Comments
 (0)