@crawlee/playwright 4.0.0-rc.0 → 4.0.0-rc.1

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.
@@ -67,11 +67,10 @@ const stateCodec = z.codec(persistedState, predictorState, {
67
67
  */
68
68
  export class RenderingTypePredictor {
69
69
  #detectionRatio;
70
- // kept as TS-private: tests reach for it at runtime
71
- state;
70
+ #state;
72
71
  constructor({ detectionRatio, persistenceOptions }) {
73
72
  this.#detectionRatio = detectionRatio;
74
- this.state = new RecoverableState({
73
+ this.#state = new RecoverableState({
75
74
  defaultState: () => stateCodec.decode({}),
76
75
  // The codec validates in the decode direction, so it is a Standard Schema as-is; encoding needs a call.
77
76
  deserialize: stateCodec,
@@ -85,32 +84,41 @@ export class RenderingTypePredictor {
85
84
  * Initialize the predictor by restoring persisted state.
86
85
  */
87
86
  async initialize() {
88
- await this.state.initialize();
87
+ await this.#state.initialize();
88
+ }
89
+ /**
90
+ * Stop persisting the model, writing it out one last time. `initialize()` reopens the persistence window.
91
+ */
92
+ async teardown() {
93
+ await this.#state.teardown();
94
+ }
95
+ async [Symbol.asyncDispose]() {
96
+ await this.teardown();
89
97
  }
90
98
  /**
91
99
  * Predict the rendering type for a given URL and request label.
92
100
  */
93
101
  predict({ url, loadedUrl, label }) {
94
- const { logreg } = this.state.currentValue;
102
+ const { logreg } = this.#state.currentValue;
95
103
  if (logreg.classifiers.length === 0) {
96
104
  return { renderingType: 'clientOnly', detectionProbabilityRecommendation: 1 };
97
105
  }
98
106
  const predictionUrl = new URL(loadedUrl ?? url);
99
- const urlFeature = new Matrix([this.calculateFeatureVector(urlComponents(predictionUrl), label)]);
107
+ const urlFeature = new Matrix([this.#calculateFeatureVector(urlComponents(predictionUrl), label)]);
100
108
  const [prediction] = logreg.predict(urlFeature);
101
109
  const scores = [logreg.classifiers[0].testScores(urlFeature), logreg.classifiers[1].testScores(urlFeature)];
102
110
  return {
103
111
  renderingType: prediction === 1 ? 'static' : 'clientOnly',
104
112
  detectionProbabilityRecommendation: Math.abs(scores[0] - scores[1]) < 0.1
105
113
  ? 1
106
- : this.#detectionRatio * Math.max(1, 5 - this.resultCount(label)),
114
+ : this.#detectionRatio * Math.max(1, 5 - this.#resultCount(label)),
107
115
  };
108
116
  }
109
117
  /**
110
118
  * Store the rendering type for a given URL and request label. This updates the underlying prediction model, which may be costly.
111
119
  */
112
120
  storeResult(requests, renderingType) {
113
- const state = this.state.currentValue;
121
+ const state = this.#state.currentValue;
114
122
  for (const { url, loadedUrl, label } of Array.isArray(requests) ? requests : [requests]) {
115
123
  const resultUrl = new URL(loadedUrl ?? url);
116
124
  if (!state.detectionResults.has(renderingType)) {
@@ -121,33 +129,33 @@ export class RenderingTypePredictor {
121
129
  }
122
130
  state.detectionResults.get(renderingType).get(label).push(urlComponents(resultUrl));
123
131
  }
124
- this.retrain();
132
+ this.#retrain();
125
133
  }
126
- resultCount(label) {
127
- return Array.from(this.state.currentValue.detectionResults.values())
134
+ #resultCount(label) {
135
+ return Array.from(this.#state.currentValue.detectionResults.values())
128
136
  .map((results) => results.get(label)?.length ?? 0)
129
137
  .reduce((acc, value) => acc + value, 0);
130
138
  }
131
- calculateFeatureVector(url, label) {
139
+ #calculateFeatureVector(url, label) {
132
140
  return [
133
- mean((this.state.currentValue.detectionResults.get('static')?.get(label) ?? []).map((otherUrl) => calculateUrlSimilarity(url, otherUrl) ?? 0)) ?? 0,
134
- mean((this.state.currentValue.detectionResults.get('clientOnly')?.get(label) ?? []).map((otherUrl) => calculateUrlSimilarity(url, otherUrl) ?? 0)) ?? 0,
141
+ mean((this.#state.currentValue.detectionResults.get('static')?.get(label) ?? []).map((otherUrl) => calculateUrlSimilarity(url, otherUrl) ?? 0)) ?? 0,
142
+ mean((this.#state.currentValue.detectionResults.get('clientOnly')?.get(label) ?? []).map((otherUrl) => calculateUrlSimilarity(url, otherUrl) ?? 0)) ?? 0,
135
143
  ];
136
144
  }
137
- retrain() {
145
+ #retrain() {
138
146
  const X = [
139
147
  [0, 1],
140
148
  [1, 0],
141
149
  ];
142
150
  const Y = [0, 1];
143
- for (const [renderingType, urlsByLabel] of this.state.currentValue.detectionResults.entries()) {
151
+ for (const [renderingType, urlsByLabel] of this.#state.currentValue.detectionResults.entries()) {
144
152
  for (const [label, urls] of urlsByLabel) {
145
153
  for (const url of urls) {
146
- X.push(this.calculateFeatureVector(url, label));
154
+ X.push(this.#calculateFeatureVector(url, label));
147
155
  Y.push(renderingType === 'static' ? 1 : 0);
148
156
  }
149
157
  }
150
158
  }
151
- this.state.currentValue.logreg.train(new Matrix(X), Matrix.columnVector(Y));
159
+ this.#state.currentValue.logreg.train(new Matrix(X), Matrix.columnVector(Y));
152
160
  }
153
161
  }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@crawlee/playwright",
3
- "version": "4.0.0-rc.0",
3
+ "version": "4.0.0-rc.1",
4
4
  "description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
5
5
  "engines": {
6
- "node": ">=22.0.0"
6
+ "node": ">=22.13.0"
7
7
  },
8
8
  "type": "module",
9
9
  "exports": {
@@ -47,32 +47,27 @@
47
47
  "access": "public"
48
48
  },
49
49
  "dependencies": {
50
- "@apify/datastructures": "^2.0.3",
51
- "@apify/timeout": "^0.4.4",
52
- "@crawlee/basic": "4.0.0-rc.0",
53
- "@crawlee/browser": "4.0.0-rc.0",
54
- "@crawlee/browser-pool": "4.0.0-rc.0",
55
- "@crawlee/cheerio": "4.0.0-rc.0",
56
- "@crawlee/core": "4.0.0-rc.0",
57
- "@crawlee/types": "4.0.0-rc.0",
58
- "@crawlee/utils": "4.0.0-rc.0",
50
+ "@apify/datastructures": "^3.0.1",
51
+ "@apify/timeout": "^1.0.1",
52
+ "@crawlee/basic": "4.0.0-rc.1",
53
+ "@crawlee/browser": "4.0.0-rc.1",
54
+ "@crawlee/browser-pool": "4.0.0-rc.1",
55
+ "@crawlee/cheerio": "4.0.0-rc.1",
56
+ "@crawlee/core": "4.0.0-rc.1",
57
+ "@crawlee/types": "4.0.0-rc.1",
58
+ "@crawlee/utils": "4.0.0-rc.1",
59
59
  "cheerio": "^1.0.0",
60
60
  "jquery": "^3.7.1",
61
61
  "ml-logistic-regression": "^2.0.0",
62
62
  "ml-matrix": "^6.12.1",
63
63
  "string-comparison": "^1.3.0",
64
64
  "tslib": "^2.8.1",
65
- "type-fest": "^4.0.0",
66
- "zod": "^4.4.3"
65
+ "zod": "^4.5.4"
67
66
  },
68
67
  "peerDependencies": {
69
- "idcac-playwright": "^0.2.0",
70
68
  "playwright": "*"
71
69
  },
72
70
  "peerDependenciesMeta": {
73
- "idcac-playwright": {
74
- "optional": true
75
- },
76
71
  "playwright": {
77
72
  "optional": true
78
73
  }
@@ -84,5 +79,5 @@
84
79
  }
85
80
  }
86
81
  },
87
- "gitHead": "79ab33dacdacb83e0197e6516d145f3aceef80c7"
82
+ "gitHead": "f354ca5e943bed1a5c657a1fce1974c053f9fff0"
88
83
  }