vk-io icon indicating copy to clipboard operation
vk-io copied to clipboard

Совместимость с Cloudflare Workers

Open AlexXanderGrib opened this issue 1 year ago • 3 comments

Зачем?

  • Ботов в воркерах делать дёшево и эффективно (нет проблемы cold-start'а как в Lambda или Yandex Functions)

Что сделать?

  • Мигрировать с node-fetch на любую кросс-платформенную альтернативу. Другие зависимости не вызывают проблем при сборке. Код vk-io тоже не вызывает ошибок, даже с учётом импортов из ноды.
Ошибки сборки

✘ [ERROR] Could not resolve "node:fs"

node_modules/.store/fetch-blob-npm-3.2.0-28e01becfc/node_modules/fetch-blob/from.js:1:59:
  1 │ import { statSync, createReadStream, promises as fs } from 'node:fs'
    ╵                                                            ~~~~~~~~~

You can mark the path "node:fs" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:path"

node_modules/.store/fetch-blob-npm-3.2.0-28e01becfc/node_modules/fetch-blob/from.js:2:25:
  2 │ import { basename } from 'node:path'
    ╵                          ~~~~~~~~~~~

You can mark the path "node:path" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:stream"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/body.js:8:34:
  8 │ import Stream, {PassThrough} from 'node:stream';
    ╵                                   ~~~~~~~~~~~~~

You can mark the path "node:stream" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:util"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/body.js:9:42:
  9 │ import {types, deprecate, promisify} from 'node:util';
    ╵                                           ~~~~~~~~~~~

You can mark the path "node:util" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:buffer"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/body.js:10:21:
  10 │ import {Buffer} from 'node:buffer';
     ╵                      ~~~~~~~~~~~~~

You can mark the path "node:buffer" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:util"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/headers.js:7:20:
  7 │ import {types} from 'node:util';
    ╵                     ~~~~~~~~~~~

You can mark the path "node:util" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:http"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/headers.js:8:17:
  8 │ import http from 'node:http';
    ╵                  ~~~~~~~~~~~

You can mark the path "node:http" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:http"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/index.js:9:17:
  9 │ import http from 'node:http';
    ╵                  ~~~~~~~~~~~

You can mark the path "node:http" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:https"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/index.js:10:18:
  10 │ import https from 'node:https';
     ╵                   ~~~~~~~~~~~~

You can mark the path "node:https" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:zlib"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/index.js:11:17:
  11 │ import zlib from 'node:zlib';
     ╵                  ~~~~~~~~~~~

You can mark the path "node:zlib" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:stream"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/index.js:12:52:
  12 │ import Stream, {PassThrough, pipeline as pump} from 'node:stream';
     ╵                                                     ~~~~~~~~~~~~~

You can mark the path "node:stream" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:buffer"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/index.js:13:21:
  13 │ import {Buffer} from 'node:buffer';
     ╵                      ~~~~~~~~~~~~~

You can mark the path "node:buffer" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:url"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/request.js:9:34:
  9 │ import {format as formatUrl} from 'node:url';
    ╵                                   ~~~~~~~~~~

You can mark the path "node:url" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:util"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/request.js:10:24:
  10 │ import {deprecate} from 'node:util';
     ╵                         ~~~~~~~~~~~

You can mark the path "node:util" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Could not resolve "node:net"

node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/utils/referrer.js:1:19:
  1 │ import {isIP} from 'node:net';
    ╵                    ~~~~~~~~~~

You can mark the path "node:net" as external to exclude it from the bundle, which will remove this error.

✘ [ERROR] Build failed with 15 errors:

node_modules/.store/fetch-blob-npm-3.2.0-28e01becfc/node_modules/fetch-blob/from.js:1:59: ERROR: Could not resolve "node:fs" node_modules/.store/fetch-blob-npm-3.2.0-28e01becfc/node_modules/fetch-blob/from.js:2:25: ERROR: Could not resolve "node:path" node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/body.js:8:34: ERROR: Could not resolve "node:stream" node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/body.js:9:42: ERROR: Could not resolve "node:util" node_modules/.store/node-fetch-npm-3.3.1-576511fc5a/node_modules/node-fetch/src/body.js:10:21: ERROR: Could not resolve "node:buffer" ...

AlexXanderGrib avatar Apr 07 '23 09:04 AlexXanderGrib

В мажорной версии есть планы отказаться от node-fetch в сторону нативного fetch. Однако built-in зависимости импортов node.js никуда не денется.

negezor avatar Apr 07 '23 09:04 negezor

Как я понял, флаг node_compat = true позвляет использовать модуль vk-io без изменений. Все ошибки, как вы могли видеть, исходят из модуля node-fetch

Идеальным вариантом было бы выделение всего API, Updates, Uploads, а также транспортов Webhook и Polling в отдельные модули (импорты), например:

  • vk-io/api
  • vk-io/transport-webhook

Это позволит не импортировать апи ноды в API и Updates

AlexXanderGrib avatar Apr 07 '23 10:04 AlexXanderGrib

Это потребует много работы, так как они напрямую завязаны на этом Node API. Так как зависимость будет уже непосредственно в под-пакетах. За примером далеко ходить не нужно, inspectable требует node:util, а это используется во всех контекстах и прикреплениях.

negezor avatar Apr 07 '23 11:04 negezor