Skip to content

Commit

Permalink
Node 18 compat aliases and imports
Browse files Browse the repository at this point in the history
  • Loading branch information
za-creature committed Jul 18, 2024
1 parent 145a0e8 commit ca2570b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cf-emu/lib/multipart.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -61,15 +62,15 @@ 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
throw new Error(`unknown binding type: ${binding.type}`)
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
}
Expand Down
7 changes: 1 addition & 6 deletions cf-emu/lib/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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')
})

Expand Down
3 changes: 3 additions & 0 deletions cf-emu/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down

0 comments on commit ca2570b

Please sign in to comment.