Skip to content

Commit

Permalink
fix(client): detach not returning elements
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Jul 27, 2023
1 parent df40699 commit 34a4340
Show file tree
Hide file tree
Showing 12 changed files with 987 additions and 1,078 deletions.
3 changes: 2 additions & 1 deletion client/lib/CoreTab.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import IDetachedResource from '@ulixee/hero-interfaces/IDetachedResource';
import ISessionMeta from '@ulixee/hero-interfaces/ISessionMeta';
import { IJsPath } from '@ulixee/js-path';
import IWaitForResourceOptions from '@ulixee/hero-interfaces/IWaitForResourceOptions';
Expand Down Expand Up @@ -253,7 +254,7 @@ export default class CoreTab implements IJsPathEventTarget {
await this.commandQueue.run('Tab.configure', options);
}

public async detachResource(name: string, resourceId: number): Promise<void> {
public async detachResource(name: string, resourceId: number): Promise<IDetachedResource> {
return await this.commandQueue.run('Tab.detachResource', name, resourceId, Date.now());
}

Expand Down
22 changes: 16 additions & 6 deletions client/lib/Hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import IResourceFilterProperties from '@ulixee/hero-interfaces/IResourceFilterPr
import { CanceledPromiseError } from '@ulixee/commons/interfaces/IPendingWaitEvent';
import ShutdownHandler from '@ulixee/commons/lib/ShutdownHandler';
import { EventEmitter } from 'events';
import DetachedElement from './DetachedElement';
import DetachedResource from './DetachedResource';
import WebsocketResource from './WebsocketResource';
import IWaitForResourceFilter from '../interfaces/IWaitForResourceFilter';
import Resource from './Resource';
Expand Down Expand Up @@ -278,16 +280,24 @@ export default class Hero extends AwaitedEventTarget<IHeroEvents> {
}
}


public async detach(
elementOrResource: Resource | WebsocketResource,
): Promise<DetachedResource>;
public async detach(
elementOrResource: Resource | WebsocketResource,
): Promise<DetachedElement>;
public async detach(
elementOrResource: Resource | WebsocketResource | IDomExtensionClass,
): Promise<void> {
): Promise<DetachedResource | DetachedElement> {
if (elementOrResource instanceof Resource || elementOrResource instanceof WebsocketResource) {
await elementOrResource.$detach();
} else if (isDomExtensionClass(elementOrResource)) {
await elementOrResource.$detach();
} else {
throw new Error('The first argument must be an Element or Resource');
return await elementOrResource.$detach();
}
if (isDomExtensionClass(elementOrResource)) {
return await elementOrResource.$detach();
}

throw new Error('The first argument must be an Element or Resource');
}

public close(): Promise<void> {
Expand Down
8 changes: 6 additions & 2 deletions client/lib/Resource.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import IDetachedResource from '@ulixee/hero-interfaces/IDetachedResource';
import IResourceType from '@ulixee/unblocked-specification/agent/net/IResourceType';
import IResourceMeta from '@ulixee/unblocked-specification/agent/net/IResourceMeta';
import Timer from '@ulixee/commons/lib/Timer';
import IWaitForResourceOptions from '@ulixee/hero-interfaces/IWaitForResourceOptions';
import TimeoutError from '@ulixee/commons/interfaces/TimeoutError';
import IResourceFilterProperties from '@ulixee/hero-interfaces/IResourceFilterProperties';
import { resourceLimits } from 'worker_threads';
import CoreTab from './CoreTab';
import DetachedResource from './DetachedResource';
import ResourceRequest, { createResourceRequest } from './ResourceRequest';
import ResourceResponse, { createResourceResponse } from './ResourceResponse';
import { createWebsocketResource } from './WebsocketResource';
Expand Down Expand Up @@ -58,8 +61,9 @@ export default class Resource {
return this.text.then(JSON.parse);
}

public $detach(): Promise<void> {
return this.#coreTabPromise.then(x => x.detachResource(undefined, this.#resourceMeta.id));
public async $detach(): Promise<DetachedResource> {
const resource = await this.#coreTabPromise.then(x => x.detachResource(undefined, this.#resourceMeta.id));
return new DetachedResource(resource);
}

public async $addToDetachedResources(name: string): Promise<void> {
Expand Down
6 changes: 4 additions & 2 deletions client/lib/WebsocketResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import IResourceMeta from '@ulixee/unblocked-specification/agent/net/IResourceMe
import IResourceType from '@ulixee/unblocked-specification/agent/net/IResourceType';
import IWebsocketMessage from '@ulixee/hero-interfaces/IWebsocketMessage';
import CoreTab from './CoreTab';
import DetachedResource from './DetachedResource';
import ResourceRequest, { createResourceRequest } from './ResourceRequest';
import ResourceResponse, { createResourceResponse } from './ResourceResponse';
import AwaitedEventTarget from './AwaitedEventTarget';
Expand Down Expand Up @@ -73,8 +74,9 @@ export default class WebsocketResource extends AwaitedEventTarget<IEventType> {
throw new Error(subscribeErrorMessage);
}

public $detach(): Promise<void> {
return this.#coreTabPromise.then(x => x.detachResource(undefined, this.#resourceMeta.id));
public async $detach(): Promise<DetachedResource> {
const resource = await this.#coreTabPromise.then(x => x.detachResource(undefined, this.#resourceMeta.id));
return new DetachedResource(resource);
}

public async $addToDetachedResources(name: string): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion core/lib/Tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export default class Tab
}
}

public async detachResource(name: string, resourceId: number, timestamp: number): Promise<void> {
public async detachResource(name: string, resourceId: number, timestamp: number): Promise<IDetachedResource> {
const resource = this.session.resources.get(resourceId);
if (!resource) throw new Error('Unknown resource collected');
this.session.db.detachedResources.insert(
Expand Down Expand Up @@ -363,6 +363,7 @@ export default class Tab
detachedResource.websocketMessages = this.session.websocketMessages.getMessages(resourceId);
}
this.session.emit('collected-asset', { type: 'resource', asset: detachedResource });
return detachedResource;
}

public async getResourceProperty(
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.4",
"@ulixee/chrome-app": "1.0.2",
"@ulixee/chrome-app": "1.0.3",
"@ulixee/hero": "2.0.0-alpha.23",
"@ulixee/hero-testing": "2.0.0-alpha.23",
"vue": "^2.6.12"
Expand Down
2 changes: 1 addition & 1 deletion docs/plugins/core-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Alter the object's values per Http2 Session.
### beforeHttpRequest<em>(request: IHttpResourceLoadDetails)</em>
A callback is provided for each HTTP request where you are given the opportunity to re-order, re-case, and add or remove headers so that they resemble real browser requests. Headless Chrome is known to provide headers is different order on occasion from headed. See [https://github.com/ulixee/double-agent](https://github.com/ulixee/double-agent) for details.
A callback is provided for each HTTP request where you are given the opportunity to re-order, re-case, and add or remove headers so that they resemble real browser requests. Headless Chrome is known to provide headers in different order on occasion from headed. See [https://github.com/ulixee/double-agent](https://github.com/ulixee/double-agent) for details.
#### **Returns** `Promise` | `void`
Expand Down
9 changes: 8 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"version": "2.0.0-alpha.1",
"private": true,
"dependencies": {
"yargs-parser": "^20.2.9"
"yargs-parser": "^20.2.9",
"@ulixee/hero": "2.0.0-alpha.23",
"@ulixee/hero-core": "2.0.0-alpha.23",
"@ulixee/hero-plugin-utils": "2.0.0-alpha.23",
"@ulixee/net": "2.0.0-alpha.23",
"@ulixee/commons": "2.0.0-alpha.23",
"@ulixee/hero-interfaces": "2.0.0-alpha.23",
"@ulixee/execute-js-plugin": "2.0.0-alpha.23"
}
}
2 changes: 1 addition & 1 deletion interfaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"@ulixee/commons": "2.0.0-alpha.23",
"@ulixee/js-path": "2.0.0-alpha.23",
"@ulixee/unblocked-specification": "2.0.0-alpha.23",
"devtools-protocol": "^0.0.981744"
"devtools-protocol": "^0.0.1137505"
}
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"@commitlint/config-conventional": "^17.6.3",
"@types/jest": "^29.5.1",
"@types/node": "^16.18.31",
"@ulixee/repo-tools": "^1.0.26",
"@ulixee/awaited-dom": "1.4.2",
"@ulixee/repo-tools": "^1.0.26",
"cross-env": "^7.0.3",
"decamelize": "^4.0.0",
"husky": "^8.0.3",
Expand Down Expand Up @@ -85,7 +85,6 @@
]
},
"resolutions": {
"tough-cookie": "^4.0.0",
"**/ua-parser-js": "0.7.28"
"tough-cookie": "^4.0.0"
}
}
1 change: 0 additions & 1 deletion plugin-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@ulixee/hero-plugin-utils",
"version": "2.0.0-alpha.23",
"description": "Base classes to build hero plugins",
"scripts": {},
"dependencies": {
"@ulixee/commons": "2.0.0-alpha.23",
"@ulixee/hero-interfaces": "2.0.0-alpha.23",
Expand Down
Loading

0 comments on commit 34a4340

Please sign in to comment.