pico-engine 1.3.0 → 1.5.0
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/README.md +147 -0
- package/dist/RulesetRegistryLoaderFs.js +4 -7
- package/dist/auth/AuthService.js +537 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/cli.js +11 -0
- package/dist/index.js +83 -46
- package/dist/logging.js +36 -27
- package/dist/oauth/OAuthService.js +356 -0
- package/dist/oauth/acg.js +495 -0
- package/dist/oauth/channelEligibility.js +26 -0
- package/dist/oauth/errors.js +12 -0
- package/dist/oauth/index.js +21 -0
- package/dist/oauth/krlModule.js +27 -0
- package/dist/oauth/meshOAuth.js +43 -0
- package/dist/provisionRoot.js +80 -0
- package/dist/server.js +565 -17
- package/dist/utils/toFileUrl.js +1 -2
- package/krl/io.picolabs.did-o.krl +25 -5
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.pds.krl +344 -0
- package/krl/io.picolabs.pico-engine-ui.krl +43 -10
- package/krl/io.picolabs.wrangler.krl +21 -20
- package/package.json +29 -29
- package/public/pico-engine-ui.css +4 -4
- package/public/pico-engine-ui.js +50 -16
- package/public/pico-labs-logo.png +0 -0
- package/src/RulesetRegistryLoaderFs.ts +6 -10
- package/src/auth/AuthService.ts +755 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/cli.ts +11 -0
- package/src/index.ts +123 -56
- package/src/logging.ts +36 -47
- package/src/oauth/OAuthService.ts +474 -0
- package/src/oauth/acg.ts +666 -0
- package/src/oauth/channelEligibility.ts +26 -0
- package/src/oauth/errors.ts +10 -0
- package/src/oauth/index.ts +14 -0
- package/src/oauth/krlModule.ts +31 -0
- package/src/oauth/meshOAuth.ts +50 -0
- package/src/provisionRoot.ts +97 -0
- package/src/server.ts +647 -17
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export {
|
|
2
|
+
AuthService,
|
|
3
|
+
AuthError,
|
|
4
|
+
AuthServiceDeps,
|
|
5
|
+
StoredAccount,
|
|
6
|
+
CredentialInfo,
|
|
7
|
+
InviteInfo,
|
|
8
|
+
InvitePeek,
|
|
9
|
+
WhoAmI,
|
|
10
|
+
} from "./AuthService";
|
|
11
|
+
export {
|
|
12
|
+
WebAuthnAdapter,
|
|
13
|
+
WebAuthnCredentialKey,
|
|
14
|
+
VerifiedRegistration,
|
|
15
|
+
VerifiedAuthentication,
|
|
16
|
+
makeWebAuthnAdapter,
|
|
17
|
+
} from "./webauthn";
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generateAuthenticationOptions,
|
|
3
|
+
generateRegistrationOptions,
|
|
4
|
+
verifyAuthenticationResponse,
|
|
5
|
+
verifyRegistrationResponse,
|
|
6
|
+
} from "@simplewebauthn/server";
|
|
7
|
+
import type {
|
|
8
|
+
AuthenticationResponseJSON,
|
|
9
|
+
AuthenticatorTransportFuture,
|
|
10
|
+
PublicKeyCredentialCreationOptionsJSON,
|
|
11
|
+
PublicKeyCredentialRequestOptionsJSON,
|
|
12
|
+
RegistrationResponseJSON,
|
|
13
|
+
} from "@simplewebauthn/server";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A stored credential's crypto material, as understood by the WebAuthn library.
|
|
17
|
+
* `publicKey` is the raw COSE key bytes.
|
|
18
|
+
*/
|
|
19
|
+
export interface WebAuthnCredentialKey {
|
|
20
|
+
id: string;
|
|
21
|
+
publicKey: Uint8Array;
|
|
22
|
+
counter: number;
|
|
23
|
+
transports?: AuthenticatorTransportFuture[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface VerifiedRegistration {
|
|
27
|
+
verified: boolean;
|
|
28
|
+
credential?: WebAuthnCredentialKey;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface VerifiedAuthentication {
|
|
32
|
+
verified: boolean;
|
|
33
|
+
newCounter: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Thin, injectable wrapper around `@simplewebauthn/server`.
|
|
38
|
+
*
|
|
39
|
+
* The engine treats WebAuthn purely as a ceremony/verification library — the
|
|
40
|
+
* challenge/credential/session stores are engine primitives (see AuthService).
|
|
41
|
+
* This interface is also the seam that lets tests supply a deterministic
|
|
42
|
+
* authenticator without a real device.
|
|
43
|
+
*/
|
|
44
|
+
export interface ExcludeCredentialDescriptor {
|
|
45
|
+
id: string;
|
|
46
|
+
transports?: AuthenticatorTransportFuture[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface WebAuthnAdapter {
|
|
50
|
+
generateRegistrationOptions(req: {
|
|
51
|
+
rpID: string;
|
|
52
|
+
rpName: string;
|
|
53
|
+
userID: Uint8Array;
|
|
54
|
+
userName: string;
|
|
55
|
+
userDisplayName: string;
|
|
56
|
+
excludeCredentials?: ExcludeCredentialDescriptor[];
|
|
57
|
+
/** When true, relax resident-key requirements so another authenticator can enroll. */
|
|
58
|
+
additionalPasskey?: boolean;
|
|
59
|
+
}): Promise<PublicKeyCredentialCreationOptionsJSON>;
|
|
60
|
+
|
|
61
|
+
verifyRegistration(req: {
|
|
62
|
+
response: RegistrationResponseJSON;
|
|
63
|
+
expectedChallenge: string;
|
|
64
|
+
expectedOrigin: string;
|
|
65
|
+
expectedRPID: string;
|
|
66
|
+
}): Promise<VerifiedRegistration>;
|
|
67
|
+
|
|
68
|
+
generateAuthenticationOptions(req: {
|
|
69
|
+
rpID: string;
|
|
70
|
+
}): Promise<PublicKeyCredentialRequestOptionsJSON>;
|
|
71
|
+
|
|
72
|
+
verifyAuthentication(req: {
|
|
73
|
+
response: AuthenticationResponseJSON;
|
|
74
|
+
expectedChallenge: string;
|
|
75
|
+
expectedOrigin: string;
|
|
76
|
+
expectedRPID: string;
|
|
77
|
+
credential: WebAuthnCredentialKey;
|
|
78
|
+
}): Promise<VerifiedAuthentication>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function makeWebAuthnAdapter(): WebAuthnAdapter {
|
|
82
|
+
return {
|
|
83
|
+
async generateRegistrationOptions(req) {
|
|
84
|
+
const excludeCredentials = (req.excludeCredentials || []).map((cred) => {
|
|
85
|
+
const id = trimBase64URLPadding(cred.id);
|
|
86
|
+
const transports = (cred.transports || []).filter(Boolean);
|
|
87
|
+
return transports.length > 0 ? { id, transports } : { id };
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return generateRegistrationOptions({
|
|
91
|
+
rpName: req.rpName,
|
|
92
|
+
rpID: req.rpID,
|
|
93
|
+
userID: req.userID as any,
|
|
94
|
+
userName: req.userName,
|
|
95
|
+
userDisplayName: req.userDisplayName,
|
|
96
|
+
attestationType: "none",
|
|
97
|
+
excludeCredentials,
|
|
98
|
+
authenticatorSelection: req.additionalPasskey
|
|
99
|
+
? {
|
|
100
|
+
residentKey: "preferred",
|
|
101
|
+
userVerification: "preferred",
|
|
102
|
+
}
|
|
103
|
+
: {
|
|
104
|
+
residentKey: "required",
|
|
105
|
+
userVerification: "preferred",
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
async verifyRegistration(req) {
|
|
111
|
+
const verification = await verifyRegistrationResponse({
|
|
112
|
+
response: req.response,
|
|
113
|
+
expectedChallenge: req.expectedChallenge,
|
|
114
|
+
expectedOrigin: req.expectedOrigin,
|
|
115
|
+
expectedRPID: req.expectedRPID,
|
|
116
|
+
requireUserVerification: false,
|
|
117
|
+
});
|
|
118
|
+
if (!verification.verified || !verification.registrationInfo) {
|
|
119
|
+
return { verified: false };
|
|
120
|
+
}
|
|
121
|
+
const cred = verification.registrationInfo.credential;
|
|
122
|
+
return {
|
|
123
|
+
verified: true,
|
|
124
|
+
credential: {
|
|
125
|
+
id: cred.id,
|
|
126
|
+
publicKey: cred.publicKey,
|
|
127
|
+
counter: cred.counter,
|
|
128
|
+
transports: mergeTransports(cred.transports, req.response),
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
async generateAuthenticationOptions(req) {
|
|
134
|
+
return generateAuthenticationOptions({
|
|
135
|
+
rpID: req.rpID,
|
|
136
|
+
userVerification: "preferred",
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
async verifyAuthentication(req) {
|
|
141
|
+
const verification = await verifyAuthenticationResponse({
|
|
142
|
+
response: req.response,
|
|
143
|
+
expectedChallenge: req.expectedChallenge,
|
|
144
|
+
expectedOrigin: req.expectedOrigin,
|
|
145
|
+
expectedRPID: req.expectedRPID,
|
|
146
|
+
credential: {
|
|
147
|
+
id: req.credential.id,
|
|
148
|
+
publicKey: req.credential.publicKey as any,
|
|
149
|
+
counter: req.credential.counter,
|
|
150
|
+
transports: req.credential.transports,
|
|
151
|
+
},
|
|
152
|
+
requireUserVerification: false,
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
verified: verification.verified,
|
|
156
|
+
newCounter:
|
|
157
|
+
verification.authenticationInfo?.newCounter ?? req.credential.counter,
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function trimBase64URLPadding(id: string): string {
|
|
164
|
+
return id.replace(/=+$/g, "");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function mergeTransports(
|
|
168
|
+
fromAttestation: AuthenticatorTransportFuture[] | undefined,
|
|
169
|
+
response: RegistrationResponseJSON
|
|
170
|
+
): AuthenticatorTransportFuture[] {
|
|
171
|
+
const merged = [...(fromAttestation || [])].filter(Boolean);
|
|
172
|
+
if (merged.length > 0) {
|
|
173
|
+
return merged;
|
|
174
|
+
}
|
|
175
|
+
if (response.authenticatorAttachment === "platform") {
|
|
176
|
+
return ["internal"];
|
|
177
|
+
}
|
|
178
|
+
if (response.authenticatorAttachment === "cross-platform") {
|
|
179
|
+
return ["usb", "ble", "nfc"];
|
|
180
|
+
}
|
|
181
|
+
return [];
|
|
182
|
+
}
|
package/src/cli.ts
CHANGED
|
@@ -26,6 +26,10 @@ Environment Variables:
|
|
|
26
26
|
PORT - The port the http server should listen on. By default it's 3000
|
|
27
27
|
PICO_ENGINE_HOME - Where the database and other files should be stored. By default it's ~/.pico-engine/
|
|
28
28
|
PICO_ENGINE_BASE_URL - The public url prefix to reach this engine. By default it's http://localhost:$PORT
|
|
29
|
+
PICO_ENGINE_ALLOW_SELF_SIGNUP - Set to "true" or "1" to allow anyone to register a new account
|
|
30
|
+
after the engine is bootstrapped. Default is false (login only; bootstrap and invite still work).
|
|
31
|
+
PICO_ENGINE_ALLOW_LOCALHOST_C - Set to "0" to require passkey session on /c/* even from localhost.
|
|
32
|
+
Default (unset) allows localhost without session for in-engine ctx:event HTTP loops. See MEMORY.md.
|
|
29
33
|
`);
|
|
30
34
|
process.exit(0);
|
|
31
35
|
}
|
|
@@ -53,6 +57,13 @@ if (
|
|
|
53
57
|
env.base_url = process.env.PICO_ENGINE_BASE_URL;
|
|
54
58
|
}
|
|
55
59
|
|
|
60
|
+
const allowSelfSignupEnv = process.env.PICO_ENGINE_ALLOW_SELF_SIGNUP;
|
|
61
|
+
if (allowSelfSignupEnv === "true" || allowSelfSignupEnv === "1") {
|
|
62
|
+
env.allowSelfSignup = true;
|
|
63
|
+
} else if (allowSelfSignupEnv === "false" || allowSelfSignupEnv === "0") {
|
|
64
|
+
env.allowSelfSignup = false;
|
|
65
|
+
}
|
|
66
|
+
|
|
56
67
|
startEngine(env).catch((err) => {
|
|
57
68
|
console.error("Failed to start engine.");
|
|
58
69
|
console.error(err);
|
package/src/index.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { krl, KrlLogger, makeKrlLogger } from "krl-stdlib";
|
|
2
|
-
import
|
|
2
|
+
import { ClassicLevel } from "classic-level";
|
|
3
3
|
import * as _ from "lodash";
|
|
4
4
|
import * as makeDir from "make-dir";
|
|
5
5
|
import * as path from "path";
|
|
6
6
|
import { PicoEngineCore, RulesetRegistry } from "pico-engine-core";
|
|
7
|
-
import { PicoFramework } from "pico-framework";
|
|
7
|
+
import { PicoDbKey, PicoFramework } from "pico-framework";
|
|
8
|
+
import { AuthService, WebAuthnAdapter, makeWebAuthnAdapter } from "./auth";
|
|
9
|
+
import { OAuthService, initOAuthModule } from "./oauth";
|
|
8
10
|
import { getPicoLogs, makeRotatingFileLogWriter } from "./logging";
|
|
11
|
+
import { provisionRoot, uiECIForRoot } from "./provisionRoot";
|
|
9
12
|
import { RulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs";
|
|
10
13
|
import { server } from "./server";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import { reject } from "lodash";
|
|
14
|
+
const charwise = require("charwise");
|
|
15
|
+
const safeJsonCodec = require("level-json-coerce-null");
|
|
14
16
|
|
|
15
17
|
const homeDir = require("home-dir");
|
|
16
18
|
const version = require("../package.json").version;
|
|
@@ -53,6 +55,43 @@ export interface PicoEngineConfiguration {
|
|
|
53
55
|
useEventInputTime?: boolean;
|
|
54
56
|
|
|
55
57
|
log?: KrlLogger;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* WebAuthn relying-party id (a registrable domain). Defaults to the hostname
|
|
61
|
+
* of `base_url` (e.g. "localhost").
|
|
62
|
+
*/
|
|
63
|
+
rpID?: string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Human-friendly relying-party name shown by the OS passkey UI.
|
|
67
|
+
*/
|
|
68
|
+
rpName?: string;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Expected WebAuthn origin (scheme://host[:port]). Defaults to the origin of
|
|
72
|
+
* `base_url`.
|
|
73
|
+
*/
|
|
74
|
+
origin?: string;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* When true, anyone may register additional accounts/roots after bootstrap.
|
|
78
|
+
* When false (default), only bootstrap (zero accounts), legacy claim, or a valid
|
|
79
|
+
* invite may create a new account.
|
|
80
|
+
*/
|
|
81
|
+
allowSelfSignup?: boolean;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Override the WebAuthn ceremony/verification implementation. Intended for
|
|
85
|
+
* testing with a deterministic authenticator.
|
|
86
|
+
*/
|
|
87
|
+
webauthn?: WebAuthnAdapter;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* When true, boot auto-creates a single root (historical behavior). Defaults
|
|
91
|
+
* to false — fresh engines start with zero roots until passkey registration.
|
|
92
|
+
* Tests may set this to true.
|
|
93
|
+
*/
|
|
94
|
+
autoCreateRootPico?: boolean;
|
|
56
95
|
}
|
|
57
96
|
|
|
58
97
|
export interface PicoEngine {
|
|
@@ -63,8 +102,11 @@ export interface PicoEngine {
|
|
|
63
102
|
base_url: string;
|
|
64
103
|
|
|
65
104
|
pf: PicoFramework;
|
|
66
|
-
|
|
105
|
+
/** UI channel for the session root; null when no roots exist yet at boot. */
|
|
106
|
+
uiECI: string | null;
|
|
67
107
|
rsRegistry: RulesetRegistry;
|
|
108
|
+
auth: AuthService;
|
|
109
|
+
oauth: OAuthService;
|
|
68
110
|
}
|
|
69
111
|
|
|
70
112
|
export async function startEngine(
|
|
@@ -75,7 +117,16 @@ export async function startEngine(
|
|
|
75
117
|
let base_url = configuration.base_url;
|
|
76
118
|
|
|
77
119
|
if (typeof home !== "string") {
|
|
78
|
-
|
|
120
|
+
const envHome = process.env.PICO_ENGINE_HOME;
|
|
121
|
+
if (
|
|
122
|
+
typeof envHome === "string" &&
|
|
123
|
+
envHome.length > 0 &&
|
|
124
|
+
process.env.NODE_ENV === "test"
|
|
125
|
+
) {
|
|
126
|
+
home = envHome;
|
|
127
|
+
} else {
|
|
128
|
+
home = homeDir(".pico-engine") as string;
|
|
129
|
+
}
|
|
79
130
|
}
|
|
80
131
|
await makeDir(home);
|
|
81
132
|
|
|
@@ -85,11 +136,15 @@ export async function startEngine(
|
|
|
85
136
|
: makeKrlLogger(makeRotatingFileLogWriter(logFilePath));
|
|
86
137
|
|
|
87
138
|
const core = new PicoEngineCore({
|
|
88
|
-
|
|
139
|
+
db: new ClassicLevel<PicoDbKey, any>(path.resolve(home, "db"), {
|
|
140
|
+
keyEncoding: charwise,
|
|
141
|
+
valueEncoding: safeJsonCodec,
|
|
142
|
+
}),
|
|
89
143
|
rsRegLoader: RulesetRegistryLoaderFs(home),
|
|
90
144
|
log,
|
|
91
145
|
modules: configuration.modules,
|
|
92
146
|
useEventInputTime: configuration.useEventInputTime,
|
|
147
|
+
autoCreateRootPico: configuration.autoCreateRootPico ?? false,
|
|
93
148
|
getPicoLogs(picoId) {
|
|
94
149
|
return getPicoLogs(logFilePath, picoId);
|
|
95
150
|
},
|
|
@@ -98,51 +153,60 @@ export async function startEngine(
|
|
|
98
153
|
const rsRegistry = core.rsRegistry;
|
|
99
154
|
const pf = core.picoFramework;
|
|
100
155
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
),
|
|
109
|
-
toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.did-o.krl")),
|
|
110
|
-
];
|
|
111
|
-
for (const url of krl_urls) {
|
|
112
|
-
const { ruleset } = await rsRegistry.flush(url);
|
|
113
|
-
await pf.rootPico.install(ruleset, { url, config: {} });
|
|
156
|
+
// Fresh engines boot with zero roots. Adopt + (re)provision only when roots
|
|
157
|
+
// already exist (test harness with autoCreateRootPico, or a migrated DB).
|
|
158
|
+
let uiECI: string | null = null;
|
|
159
|
+
const existingRoots = pf.rootPicos();
|
|
160
|
+
if (existingRoots.length > 0) {
|
|
161
|
+
const provisioned = await provisionRoot(pf, core, { root: existingRoots[0] });
|
|
162
|
+
uiECI = provisioned.uiECI;
|
|
114
163
|
}
|
|
115
164
|
|
|
116
|
-
|
|
117
|
-
.
|
|
118
|
-
.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
165
|
+
const auth = new AuthService({
|
|
166
|
+
db: pf.db,
|
|
167
|
+
webauthn: configuration.webauthn || makeWebAuthnAdapter(),
|
|
168
|
+
getBaseUrl: () => core.base_url,
|
|
169
|
+
rpID: configuration.rpID,
|
|
170
|
+
rpName: configuration.rpName,
|
|
171
|
+
origin: configuration.origin,
|
|
172
|
+
allowSelfSignup: configuration.allowSelfSignup ?? false,
|
|
173
|
+
provisionRoot: async (opts) => {
|
|
174
|
+
const { root, uiECI } = await provisionRoot(pf, core, opts);
|
|
175
|
+
return { rootPicoId: root.id, uiECI };
|
|
176
|
+
},
|
|
177
|
+
getUiECI: (rootPicoId: string) => uiECIForRoot(pf, rootPicoId),
|
|
178
|
+
getPrimaryRootPicoId: () => {
|
|
179
|
+
try {
|
|
180
|
+
return pf.rootPico.id;
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
onAccountClaimed: async (rootPicoId, displayName) => {
|
|
186
|
+
const uiChannel = uiECIForRoot(pf, rootPicoId);
|
|
187
|
+
if (!uiChannel || !displayName.trim()) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
await pf.eventWait({
|
|
191
|
+
eci: uiChannel,
|
|
192
|
+
domain: "engine_ui",
|
|
193
|
+
name: "box",
|
|
194
|
+
data: { attrs: { name: displayName.trim() } },
|
|
195
|
+
time: 0,
|
|
196
|
+
});
|
|
197
|
+
},
|
|
138
198
|
});
|
|
139
199
|
|
|
140
|
-
const
|
|
200
|
+
const oauth = new OAuthService({
|
|
201
|
+
db: pf.db,
|
|
202
|
+
pf,
|
|
203
|
+
});
|
|
204
|
+
core.modules["oauth"] = initOAuthModule(oauth);
|
|
141
205
|
|
|
142
|
-
const app = server(core, uiECI);
|
|
206
|
+
const app = server(core, uiECI, auth, oauth);
|
|
143
207
|
|
|
144
208
|
if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
|
|
145
|
-
port = 3000;
|
|
209
|
+
port = process.env.NODE_ENV === "test" ? 0 : 3000;
|
|
146
210
|
}
|
|
147
211
|
await new Promise((resolve) => {
|
|
148
212
|
const listener = app.listen(port, () => {
|
|
@@ -163,16 +227,17 @@ export async function startEngine(
|
|
|
163
227
|
|
|
164
228
|
log.info(`Listening at ${base_url}`);
|
|
165
229
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
230
|
+
if (uiECI) {
|
|
231
|
+
pf.event({
|
|
232
|
+
eci: uiECI,
|
|
233
|
+
domain: "engine",
|
|
234
|
+
name: "started",
|
|
235
|
+
data: { attrs: {} },
|
|
236
|
+
time: 0, // TODO remove this typescript requirement
|
|
237
|
+
}).catch((error) => {
|
|
238
|
+
log.error("Error signaling engine:started event", { error });
|
|
239
|
+
});
|
|
240
|
+
}
|
|
176
241
|
|
|
177
242
|
return {
|
|
178
243
|
version,
|
|
@@ -184,5 +249,7 @@ export async function startEngine(
|
|
|
184
249
|
pf,
|
|
185
250
|
uiECI,
|
|
186
251
|
rsRegistry,
|
|
252
|
+
auth,
|
|
253
|
+
oauth,
|
|
187
254
|
};
|
|
188
255
|
}
|
package/src/logging.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
|
+
import * as readline from "readline";
|
|
2
3
|
import { krlLogLevelCodeToHuman, PicoLogEntry } from "krl-stdlib";
|
|
3
4
|
import * as path from "path";
|
|
4
|
-
const split = require("split");
|
|
5
|
-
const through2 = require("through2");
|
|
6
5
|
|
|
7
6
|
const rfs = require("rotating-file-stream");
|
|
8
7
|
|
|
@@ -21,14 +20,14 @@ function getRotatingFileStream(filePath: string): NodeJS.WritableStream {
|
|
|
21
20
|
|
|
22
21
|
size: "100M", // rotate every 10 MegaBytes written
|
|
23
22
|
maxFiles: 12,
|
|
24
|
-
}
|
|
23
|
+
},
|
|
25
24
|
) as NodeJS.WritableStream;
|
|
26
25
|
}
|
|
27
26
|
return logStreams[filePath];
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
export function makeRotatingFileLogWriter(
|
|
31
|
-
filePath: string
|
|
30
|
+
filePath: string,
|
|
32
31
|
): (line: string) => void {
|
|
33
32
|
const fileStream = getRotatingFileStream(filePath);
|
|
34
33
|
const isTest = process.env.NODE_ENV === "test";
|
|
@@ -45,50 +44,40 @@ export function makeRotatingFileLogWriter(
|
|
|
45
44
|
|
|
46
45
|
export async function getPicoLogs(
|
|
47
46
|
filePath: string,
|
|
48
|
-
picoId: string
|
|
47
|
+
picoId: string,
|
|
49
48
|
): Promise<PicoLogEntry[]> {
|
|
50
|
-
|
|
51
|
-
const output: PicoLogEntry[] = [];
|
|
52
|
-
fs.createReadStream(filePath)
|
|
53
|
-
.pipe(split())
|
|
54
|
-
.pipe(
|
|
55
|
-
through2((chunk: any, enc: any, next: any) => {
|
|
56
|
-
const line = chunk.toString();
|
|
57
|
-
if (line.indexOf(picoId) < 0) {
|
|
58
|
-
// not my pico
|
|
59
|
-
return next();
|
|
60
|
-
}
|
|
61
|
-
let entry;
|
|
62
|
-
try {
|
|
63
|
-
entry = JSON.parse(line);
|
|
64
|
-
} catch (err) {}
|
|
65
|
-
if (!entry || entry.picoId !== picoId) {
|
|
66
|
-
// not my pico
|
|
67
|
-
return next();
|
|
68
|
-
}
|
|
69
|
-
const time = new Date(entry.time);
|
|
70
|
-
if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
|
|
71
|
-
// too old
|
|
72
|
-
return next();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const out: PicoLogEntry = {
|
|
76
|
-
...entry,
|
|
77
|
-
level: krlLogLevelCodeToHuman[entry.level] || `${entry.level}`,
|
|
78
|
-
time: entry.time,
|
|
79
|
-
txnId: entry.txnId,
|
|
80
|
-
};
|
|
81
|
-
delete (out as any).picoId;
|
|
82
|
-
output.push(out);
|
|
49
|
+
const output: PicoLogEntry[] = [];
|
|
83
50
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
)
|
|
87
|
-
.on("finish", () => {
|
|
88
|
-
resolve(output);
|
|
89
|
-
})
|
|
90
|
-
.on("error", (err: any) => {
|
|
91
|
-
reject(err);
|
|
92
|
-
});
|
|
51
|
+
const rl = readline.createInterface({
|
|
52
|
+
input: fs.createReadStream(filePath),
|
|
93
53
|
});
|
|
54
|
+
|
|
55
|
+
for await (const line of rl) {
|
|
56
|
+
// Each line in the readline input will be successively available here as
|
|
57
|
+
// `line`.
|
|
58
|
+
if (line.indexOf(picoId) < 0) {
|
|
59
|
+
continue; // not my pico
|
|
60
|
+
}
|
|
61
|
+
let entry;
|
|
62
|
+
try {
|
|
63
|
+
entry = JSON.parse(line);
|
|
64
|
+
} catch (err) {}
|
|
65
|
+
if (!entry || entry.picoId !== picoId) {
|
|
66
|
+
continue; // not my pico
|
|
67
|
+
}
|
|
68
|
+
const time = new Date(entry.time);
|
|
69
|
+
if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
|
|
70
|
+
continue; // too old
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const out: PicoLogEntry = {
|
|
74
|
+
...entry,
|
|
75
|
+
level: krlLogLevelCodeToHuman[entry.level] || `${entry.level}`,
|
|
76
|
+
time: entry.time,
|
|
77
|
+
txnId: entry.txnId,
|
|
78
|
+
};
|
|
79
|
+
delete (out as any).picoId;
|
|
80
|
+
output.push(out);
|
|
81
|
+
}
|
|
82
|
+
return output;
|
|
94
83
|
}
|