Skip to content

Commit

Permalink
add createLocalPod.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-zucker committed Nov 19, 2020
1 parent 68a8cb8 commit 736bf22
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
-- **a nodejs client for Solid** --

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.
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.

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.

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.

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

Expand Down Expand Up @@ -130,6 +134,16 @@ The solid-auth-fetcher session and authFetcher objects are available for each cl
let authFetcher = client.authFetcher ...
```
## <a name="pod">Creating a serverless pod</a>
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.
```javascript
import {SolidNodeClient} from 'solid-node-client';
const client = new SolidNodeClient();
client.createLocalPod('file:///home/jeff/myPod/');
```
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.
## Acknowledgements
All of the session management is from Jackson Creed's solid-auth-fetcher. The login is from Michiel de Jong's solid-crud-tests.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solid-node-client",
"version": "1.2.2",
"version": "1.2.3",
"description": "a client for nodejs access to Solid",
"main": "./src/index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/createLocalPod.js

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

72 changes: 72 additions & 0 deletions src/createLocalPod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

export const profile_content = `@prefix : <#>.
@prefix jef: </>.
@prefix ldp: <http://www.w3.org/ns/ldp#>.
@prefix inbox: <../inbox/>.
@prefix pro: <./>.
@prefix ter: <http://www.w3.org/ns/solid/terms#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix pim: <http://www.w3.org/ns/pim/space#>.
@prefix n2: <http://>.
@prefix n3: <https://>.
@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
@prefix sch: <http://schema.org/>.
@prefix n1: <http://www.w3.org/ns/auth/acl#>.
pro:card a foaf:PersonalProfileDocument; foaf:maker :me; foaf:primaryTopic :me.
:me
a sch:Person, foaf:Person;
vcard:fn "Local Solid User";
vcard:role "software developer";
n1:trustedApp
[
n1:mode n1:Append, n1:Control, n1:Read, n1:Write;
n1:origin <http://example.org>
];
ldp:inbox inbox:;
pim:preferencesFile <../settings/prefs.ttl>;
pim:storage jef:;
ter:account jef:;
ter:privateTypeIndex <../settings/privateTypeIndex.ttl>;
ter:publicTypeIndex <../settings/publicTypeIndex.ttl>;
foaf:name "Local Solid User".
`;

export const prefs_content = `@prefix : <#>.
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix sp: <http://www.w3.org/ns/pim/space#>.
@prefix dct: <http://purl.org/dc/terms/>.
@prefix c: <../profile/card#>.
@prefix mee: <http://www.w3.org/ns/pim/meeting#>.
<> a sp:ConfigurationFile; dct:title "Preferences file".
c:me
a solid:Developer, solid:PowerUser;
solid:privateTypeIndex <privateTypeIndex.ttl>;
solid:publicTypeIndex <publicTypeIndex.ttl>.
`;

export const public_content = `@prefix : <#>.
@prefix solid: <https://www.w3.org/ns/solid/terms#>.
@prefix terms: <http://purl.org/dc/terms/>.
@prefix ter: <http://www.w3.org/ns/solid/terms#>.
@prefix bookm: <http://www.w3.org/2002/01/bookmark#>.
<> a solid:ListedDocument, solid:TypeIndex; terms:references :Bookmark.
:Bookmark
a ter:TypeRegistration;
ter:forClass bookm:Bookmark;
ter:instance <../bookmarks.ttl>.
`;
export const private_content = `
@prefix solid: <https://www.w3.org/ns/solid/terms#>.
<>
a solid:TypeIndex ;
a solid:UnlistedDocument.
`;

/* ENDS */

50 changes: 50 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
exports.__esModule = true;
exports.SolidNodeClient = void 0;
var solid_auth_fetcher_1 = require("solid-auth-fetcher");
var createLocalPod_js_1 = require("./createLocalPod.js");
var node_fetch_1 = __importDefault(require("node-fetch"));
var solid_rest_1 = __importDefault(require("solid-rest"));
var SolidNodeClient = /** @class */ (function () {
Expand Down Expand Up @@ -143,6 +144,55 @@ var SolidNodeClient = /** @class */ (function () {
});
});
};
SolidNodeClient.prototype.createLocalPod = function (base) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
base = base.replace(/\/$/, '');
return [4 /*yield*/, this.makeResource(base, "/profile/card", createLocalPod_js_1.profile_content)];
case 1:
_a.sent();
return [4 /*yield*/, this.makeResource(base, "/settings/prefs.ttl", createLocalPod_js_1.prefs_content)];
case 2:
_a.sent();
return [4 /*yield*/, this.makeResource(base, "/settings/privateTypeIndex.ttl", createLocalPod_js_1.private_content)];
case 3:
_a.sent();
return [4 /*yield*/, this.makeResource(base, "/settings/publicTypeIndex.ttl", createLocalPod_js_1.public_content)];
case 4:
_a.sent();
return [4 /*yield*/, this.makeResource(base, "/public/.meta", "")];
case 5:
_a.sent();
return [4 /*yield*/, this.makeResource(base, "/inbox/.meta", "")];
case 6:
_a.sent();
return [2 /*return*/];
}
});
});
};
SolidNodeClient.prototype.makeResource = function (base, path, content) {
return __awaiter(this, void 0, void 0, function () {
var url;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = base + path;
console.log("Creating " + url);
return [4 /*yield*/, this.fetch(url, {
method: "PUT",
body: content,
headers: { "content-type": "text/turtle" }
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
return SolidNodeClient;
}());
exports.SolidNodeClient = SolidNodeClient;
Expand Down
25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getSession, getNodeSolidServerCookie, getAuthFetcher }
from "solid-auth-fetcher";
import {profile_content,prefs_content,private_content,public_content}
from './createLocalPod.js';
import fetch from "node-fetch";
import SolidRest from "solid-rest";

Expand Down Expand Up @@ -58,10 +60,29 @@ export class SolidNodeClient {
}
this.session = new NodeNoAuthSession({rest:this.rest});
this.authFetcher = null;
}
async currentSession(){
}
async currentSession(){
return ( this.session.loggedIn ) ? this.session : null ;
}
async createLocalPod( base:string ){
base = base.replace(/\/$/,'');
await this.makeResource( base,"/profile/card", profile_content );
await this.makeResource( base,"/settings/prefs.ttl", prefs_content );
await this.makeResource(base,"/settings/privateTypeIndex.ttl",private_content );
await this.makeResource( base,"/settings/publicTypeIndex.ttl", public_content );
await this.makeResource( base,"/public/.meta", "" );
await this.makeResource( base,"/inbox/.meta", "" );
}
async makeResource( base:string, path:string, content:string ){
let url = base + path
console.log ( "Creating " + url )
await this.fetch( url, {
method:"PUT",
body:content,
headers:{"content-type":"text/turtle"}
})
}

}

/** UNAUTHENTICATED SESSION
Expand Down
17 changes: 17 additions & 0 deletions tests/createLocalPod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {SolidNodeClient} from '../';
const client = new SolidNodeClient();
// import * as readline from 'readline-sync';

async function main(){
/*
console.log('\nThis will create a local file-system-based pod.')
console.log(
'Enter a full path to the place you want your pod (e.g. /home/jeff/myPod/)'
);
let basePath = await readline.question(' > ').replace(/\/$/,'');
*/
let basePath = "/home/jeff/myPod/";
client.createLocalPod(basePath);
}
main()

0 comments on commit 736bf22

Please sign in to comment.