Skip to content

Commit

Permalink
Upgrade to latest std lib. Save found 'outOfProcess' access record fi…
Browse files Browse the repository at this point in the history
…eld.
  • Loading branch information
johnspurlock committed Aug 11, 2021
1 parent c0a5617 commit 0d67a75
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serve, ServerRequest } from 'https://deno.land/std@0.103.0/http/server.ts';
import { readAll } from 'https://deno.land/std@0.103.0/io/util.ts';
import { resolve } from 'https://deno.land/std@0.103.0/path/mod.ts';
import { serve, ServerRequest } from 'https://deno.land/std@0.104.0/http/server.ts';
import { readAll } from 'https://deno.land/std@0.104.0/io/util.ts';
import { resolve } from 'https://deno.land/std@0.104.0/path/mod.ts';
import { Database } from './database.ts';
import { computeBundleIconHref, computeBundleIconMarkup, handleBundleIconRequest } from './icons.ts';
import { importReportFile } from './importer.ts';
Expand Down
10 changes: 5 additions & 5 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DB } from 'https://deno.land/x/[email protected]/mod.ts';
import { AccessRecord, checkAccessRecord, checkDomainRecord, DomainRecord, isAccessRecordBeta1, isDomainRecordBeta1 } from './model.ts';

const VERSION = 2;
const VERSION = 3;

export class Database {
private readonly _db: DB;
Expand All @@ -21,6 +21,7 @@ export class Database {
timestamp text not null,
version integer null,
category text null,
outOfProcess text null,
primary key (filename, line)) without rowid`);

this._db.query(`create table if not exists domain${VERSION}(
Expand Down Expand Up @@ -66,13 +67,12 @@ export class Database {
this._db.query(`delete from access${VERSION} where filename = ?`, [ filename ]);
}

insertAccess(filename: string, line: number, record: AccessRecord, category: string | undefined) {
insertAccess(filename: string, line: number, record: AccessRecord, category: string | undefined, outOfProcess: string | undefined) {
const timestamp = checkAccessRecord(record);
const version = isAccessRecordBeta1(record) ? record.version : undefined;
const stream = isAccessRecordBeta1(record) ? record.stream : undefined;

this._db.query(`insert into access${VERSION}(filename, line, stream, accessorIdentifier, accessorIdentifierType, tccService, identifier, kind, timestamp, version, category) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[filename, line, stream, record.accessor.identifier, record.accessor.identifierType, record.tccService, record.identifier, record.kind, timestamp, version, category]);
this._db.query(`insert into access${VERSION}(filename, line, stream, accessorIdentifier, accessorIdentifierType, tccService, identifier, kind, timestamp, version, category, outOfProcess) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[filename, line, stream, record.accessor.identifier, record.accessor.identifierType, record.tccService, record.identifier, record.kind, timestamp, version, category, outOfProcess]);
if (this._db.changes !== 1) throw new Error(`Failed to insert access record`);
}

Expand Down
5 changes: 3 additions & 2 deletions src/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export function importReportFile(text: string, filename: string, db: Database) {
if (!isAccessRecordBeta3(parsed)) throw new Error(`Expected beta 3 access record`);
const timestamp = convertZonedTimestampToUtc(parsed.timeStamp);
const record = { ...parsed, timestamp };
db.insertAccess(filename, nextLine++, record, record.category);
const outOfProcess = record.outOfProcess !== undefined ? record.outOfProcess.toString() : undefined;
db.insertAccess(filename, nextLine++, record, record.category, outOfProcess);
} else if (obj.type === 'networkActivity') {
const parsed = parseDomainRecordBeta3(obj, line);
const timeStamp = convertZonedTimestampToUtc(parsed.timeStamp);
Expand All @@ -67,7 +68,7 @@ export function importReportFile(text: string, filename: string, db: Database) {
const timestamp = convertZonedTimestampToUtc(parsed.timestamp);
const record = { ...parsed, timestamp };
const category = isAccessRecordBeta2(parsed) ? parsed.category : undefined;
db.insertAccess(filename, nextLine++, record, category);
db.insertAccess(filename, nextLine++, record, category, undefined);
} else if (recordType === 'networkActivity') {
const parsed = parseDomainRecordBeta2(obj, line);
const timeStamp = convertZonedTimestampToUtc(parsed.timeStamp);
Expand Down
3 changes: 2 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface AccessRecordBeta3 extends AccessRecordCommon {
readonly timeStamp: string; // e.g. 2021-07-07T05:52:14.106-05:00
readonly type: string; // e.g. access
readonly category: string; // e.g. photos, contacts
readonly outOfProcess?: boolean;
}

// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -64,7 +65,7 @@ export function isAccessRecordBeta3(obj: any): obj is AccessRecordBeta3 {
&& typeof obj.type === 'string'
&& typeof obj.category === 'string'
&& typeof obj.identifier === 'string'
&& Object.keys(obj).every(v => ['accessor', 'timeStamp', 'kind', 'type', 'category', 'identifier'].includes(v))
&& Object.keys(obj).every(v => ['accessor', 'timeStamp', 'kind', 'type', 'category', 'identifier', 'outOfProcess'].includes(v))
}

export function checkAccessRecord(record: AccessRecord): string /* timestamp */ {
Expand Down

0 comments on commit 0d67a75

Please sign in to comment.