Skip to content

Commit 02698cf

Browse files
committed
add transpiled files
1 parent e1f2a93 commit 02698cf

File tree

16 files changed

+531
-2
lines changed

16 files changed

+531
-2
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
lib

lib/array.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default class InterceptedArray<T> extends Array {
2+
private callback;
3+
constructor(callback: (object: object, method: string, ...args: any) => void);
4+
copyWithin(target: number, start: number, end?: number): this;
5+
fill(value: T, start?: number, end?: number): this;
6+
pop(): T | undefined;
7+
push(...items: T[]): number;
8+
reverse(): T[];
9+
shift(): T | undefined;
10+
sort(compareFn?: (a: T, b: T) => number): this;
11+
splice(start: number, deleteCount?: number): T[];
12+
unshift(...items: T[]): number;
13+
}

lib/array.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export default class InterceptedArray extends Array {
2+
callback;
3+
// intercepts only methods that change the object
4+
constructor(callback) {
5+
super();
6+
this.callback = callback;
7+
}
8+
copyWithin(target, start, end) {
9+
this.callback(this, 'copyWithin', target, start, end);
10+
return super.copyWithin(target, start, end);
11+
}
12+
fill(value, start, end) {
13+
this.callback(this, 'fill', value, start, end);
14+
return super.fill(value, start, end);
15+
}
16+
pop() {
17+
this.callback(this, 'pop');
18+
return super.pop();
19+
}
20+
push(...items) {
21+
this.callback(this, 'push', ...items);
22+
return super.push(...items);
23+
}
24+
reverse() {
25+
this.callback(this, 'reverse');
26+
return super.reverse();
27+
}
28+
shift() {
29+
this.callback(this, 'shift');
30+
return super.shift();
31+
}
32+
sort(compareFn) {
33+
this.callback(this, 'sort', compareFn);
34+
return super.sort(compareFn);
35+
}
36+
splice(start, deleteCount) {
37+
this.callback(this, 'splice', start, deleteCount);
38+
return super.splice(start, deleteCount);
39+
}
40+
unshift(...items) {
41+
this.callback(this, 'unshift', ...items);
42+
return super.unshift(...items);
43+
}
44+
}

lib/init-child.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import IPOS from './main.js';
2+
export default function initChild(this: IPOS): Promise<unknown>;

lib/init-child.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function initChild() {
2+
let resolve;
3+
const promise = new Promise(res => resolve = res);
4+
this.messaging?.listenForType('sync', message => {
5+
if (!message.fields)
6+
return;
7+
Object.entries(message.fields)
8+
.map(([key, value]) => {
9+
this.createStealthy(key, value);
10+
});
11+
this.messaging?.send('sync_ok');
12+
resolve();
13+
});
14+
// register with parent process
15+
this.messaging?.send('register');
16+
if (this.messaging)
17+
this.mountListeners(this.messaging);
18+
return promise;
19+
}

lib/intercept.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function intercept(value: object, key: string, interceptCallback: (key: string, method: string, ...args: any) => void): object;

lib/intercept.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default function intercept(value, key, interceptCallback) {
2+
const arrayMutatingMethods = ['copyWithin', 'fill', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'];
3+
const objectMutatingMethods = [];
4+
const mapMutatingMethods = ['clear', 'delete', 'set'];
5+
const setMutatingMethods = ['add', 'clear', 'delete'];
6+
const functionMutatingMethods = [];
7+
const mutatingMethods = new Map();
8+
mutatingMethods.set(Array, arrayMutatingMethods);
9+
mutatingMethods.set({}.constructor, objectMutatingMethods);
10+
mutatingMethods.set(Map, mapMutatingMethods);
11+
mutatingMethods.set(Set, setMutatingMethods);
12+
mutatingMethods.set(Function, functionMutatingMethods);
13+
if (!mutatingMethods.has(value.constructor))
14+
return value;
15+
return new Proxy(value, {
16+
get(target, name) {
17+
if (Reflect.has(target, name) && mutatingMethods.get(value.constructor).includes(name)) {
18+
const method = Reflect.get(target, name);
19+
return (...args) => {
20+
interceptCallback(key, name, ...args);
21+
method.call(value, ...args);
22+
};
23+
}
24+
else {
25+
return Reflect.get(target, name);
26+
}
27+
},
28+
set(target, name, value) {
29+
// @ts-ignore
30+
const result = (target[name] = value);
31+
interceptCallback(key, '$$iposDefine', name, value);
32+
return !!result;
33+
},
34+
});
35+
}

lib/intercept/array.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default class InterceptedArray<T> extends Array {
2+
private readonly callback;
3+
constructor(callback: (object: object, method: string, ...args: any) => void);
4+
static new<T>(arrayLike: ArrayLike<T>, callback: (object: object, method: string, ...args: any) => void): InterceptedArray<T>;
5+
copyWithin(target: number, start: number, end?: number): this;
6+
fill(value: T, start?: number, end?: number): this;
7+
pop(): T | undefined;
8+
push(...items: T[]): number;
9+
reverse(): T[];
10+
shift(): T | undefined;
11+
sort(compareFn?: (a: T, b: T) => number): this;
12+
splice(start: number, deleteCount?: number): T[];
13+
unshift(...items: T[]): number;
14+
}

lib/intercept/array.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export default class InterceptedArray extends Array {
2+
callback;
3+
// intercepts only methods that change the object
4+
constructor(callback) {
5+
super();
6+
this.callback = callback;
7+
}
8+
static new(arrayLike, callback) {
9+
const interceptedArray = new InterceptedArray(callback);
10+
for (let i = 0; i < arrayLike.length; i++) {
11+
interceptedArray[i] = arrayLike[i];
12+
}
13+
return interceptedArray;
14+
}
15+
copyWithin(target, start, end) {
16+
this.callback(this, 'copyWithin', target, start, end);
17+
return super.copyWithin(target, start, end);
18+
}
19+
fill(value, start, end) {
20+
this.callback(this, 'fill', value, start, end);
21+
return super.fill(value, start, end);
22+
}
23+
pop() {
24+
this.callback(this, 'pop');
25+
return super.pop();
26+
}
27+
push(...items) {
28+
this.callback(this, 'push', ...items);
29+
return super.push(...items);
30+
}
31+
reverse() {
32+
this.callback(this, 'reverse');
33+
return super.reverse();
34+
}
35+
shift() {
36+
this.callback(this, 'shift');
37+
return super.shift();
38+
}
39+
sort(compareFn) {
40+
this.callback(this, 'sort', compareFn);
41+
return super.sort(compareFn);
42+
}
43+
splice(start, deleteCount) {
44+
this.callback(this, 'splice', start, deleteCount);
45+
return super.splice(start, deleteCount);
46+
}
47+
unshift(...items) {
48+
this.callback(this, 'unshift', ...items);
49+
return super.unshift(...items);
50+
}
51+
}

lib/main.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference types="node" />
2+
import { ChildProcess } from 'child_process';
3+
import IPOSMessaging, { iposMessagingMessage, iposMessagingType } from './messaging.js';
4+
export default class IPOS {
5+
private readonly fields;
6+
private readonly fieldsRaw;
7+
private fieldsReverseMap;
8+
private processMessagingMap;
9+
private readonly proxy;
10+
protected messaging?: IPOSMessaging;
11+
static new(): IPOS | Promise<IPOS>;
12+
constructor();
13+
/****************** MESSAGING *******************/
14+
protected mountListeners(messaging: IPOSMessaging): void;
15+
protected sendToAll(type: iposMessagingType, data?: {}): void;
16+
/********************* GET **********************/
17+
get(key: string): any;
18+
private getRaw;
19+
/******************** CREATE ********************/
20+
create(key: string, value: any): void;
21+
protected createStealthy(key: string, value: object): void;
22+
protected performSet(message: iposMessagingMessage): void;
23+
/******************** UPDATE ********************/
24+
protected performUpdate(message: iposMessagingMessage): void;
25+
private sendMethodCall;
26+
/******************** DELETE ********************/
27+
delete(key: string): boolean;
28+
deleteStealthy(key: string): boolean;
29+
performDelete(message: iposMessagingMessage): boolean | undefined;
30+
/******************* PROCESS ********************/
31+
addProcess(process: ChildProcess): Promise<void>;
32+
private syncProcess;
33+
}

0 commit comments

Comments
 (0)