From ca2570bb83d43219e84d5d2bcd3be5c8589eb0c6 Mon Sep 17 00:00:00 2001 From: Radu Dan Date: Thu, 18 Jul 2024 18:31:16 +0300 Subject: [PATCH] Node 18 compat aliases and imports --- cf-emu/lib/multipart.js | 5 +++-- cf-emu/lib/multipart.test.js | 7 +------ cf-emu/runtime.js | 3 +++ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cf-emu/lib/multipart.js b/cf-emu/lib/multipart.js index 29975c2..4c729ff 100644 --- a/cf-emu/lib/multipart.js +++ b/cf-emu/lib/multipart.js @@ -1,6 +1,7 @@ let {buffer} = require('./util') let busboy = require('busboy') +let File = global.File || require('node:buffer').File // parses a multpart body that contains the worker and bindings; input should @@ -61,7 +62,7 @@ exports.parse = async function parse(form, out_bindings) { throw new Error(`invalid request: part '${body_part}'` + ` referenced by ${debug} not present`) value = form.get(binding.part) - if(value instanceof Blob) + if(typeof value.text == 'function') value = await value.text() } } else @@ -69,7 +70,7 @@ exports.parse = async function parse(form, out_bindings) { out_bindings[binding.name] = value } let body = form.get(body_part) - if(body instanceof Blob) + if(typeof body.text == 'function') body = await body.text() return body } diff --git a/cf-emu/lib/multipart.test.js b/cf-emu/lib/multipart.test.js index 9592c5d..1511476 100644 --- a/cf-emu/lib/multipart.test.js +++ b/cf-emu/lib/multipart.test.js @@ -2,6 +2,7 @@ let {parse, piccolo} = require('./multipart') let {stream} = require('./util') let {assert} = require('chai') +let File = global.File || require('node:buffer').File describe('multipart', () => { @@ -18,13 +19,7 @@ describe('multipart', () => { }) it('implements the File interface', () => { - assert.instanceOf(file, File) assert.equal(file.lastModified, 1234) - if(MAJOR_NODE_VERSION < 20) { - // removed from standard - assert.instanceOf(file.lastModifiedDate, Date) - assert.equal(file.lastModifiedDate.getTime(), 1234) - } assert.equal(file.name, 'foo') }) diff --git a/cf-emu/runtime.js b/cf-emu/runtime.js index 37cc309..74584ac 100755 --- a/cf-emu/runtime.js +++ b/cf-emu/runtime.js @@ -13,6 +13,9 @@ exports.self = exports // https://developers.cloudflare.com/workers/reference/apis/web-crypto/ 'crypto', ].forEach(key => exports[key] = global[key]) +if(typeof exports.crypto === 'undefined') { + exports.crypto = require('node:crypto') +} // https://developers.cloudflare.com/workers/reference/apis/fetch-event/