Skip to content

Commit

Permalink
Drop legacy node.js support, remove polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeschastny committed Oct 10, 2023
1 parent 6d68c93 commit 5d637bd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 40 deletions.
14 changes: 4 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
"postpublish": "git push origin +HEAD:latest --follow-tags"
},
"engines": {
"node": ">= 0.10.0"
"node": ">=12"
},
"dependencies": {
"raw-body": "2",
"through": "~2.3",
"when": "3"
"raw-body": "2"
},
"devDependencies": {
"coffeescript": "^1.10.0",
Expand Down
28 changes: 10 additions & 18 deletions src/io.coffee
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
rawBody = require 'raw-body'
When = require 'when'

# It should work for any version of when.js
nodefn = try
require 'when/node'
catch
require 'when/node/function'
streamToBuffer = require 'raw-body'

{Buffer} = require 'buffer'
{Stream, PassThrough} = require 'stream'

streamToBuffer = nodefn.lift rawBody
PassThrough ||= require 'through'

bindCallback = (promise, next) ->
if typeof next is 'function'
nodefn.bindCallback promise, next
else
promise
promise.then (result) ->
process.nextTick next, null, result
, (err) ->
process.nextTick next, err
promise

module.exports =
_write: (source, outname) ->
Expand All @@ -39,13 +31,13 @@ module.exports =
source.pipe res.stdin
res
else
promise: When.reject new Error 'Mailformed source'
promise: Promise.reject new Error 'Mailformed source'

write: (outname, next) ->
promise = if outname
(@_write @source, outname).promise
else
When.reject new Error 'outname in not specified'
Promise.reject new Error 'outname in not specified'
bindCallback promise, next

toBuffer: (next) ->
Expand All @@ -59,9 +51,9 @@ module.exports =
res.promise
else
res.promise.then ->
When.reject new Error 'Failed to pipe stdout'
Promise.reject new Error 'Failed to pipe stdout'
promise.then ->
outstream.end()
.otherwise (err) ->
.catch (err) ->
outstream.emit 'error', err
outstream
11 changes: 8 additions & 3 deletions src/wrapper.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
When = require 'when'
{spawn} = require 'child_process'

{mixin} = require './utils'

defer = () ->
res = {}
res.promise = new Promise (resolve, reject) ->
res.resolve = resolve
res.reject = reject
res

module.exports = class Wrapper
mixin this, require './args'
Expand All @@ -20,7 +25,7 @@ module.exports = class Wrapper
return @

_spawn: (args, stdin = false, stdout = false) ->
{resolve, reject, promise} = When.defer()
{resolve, reject, promise} = defer()
stderr = ''
@_opts.stdio = [
if stdin then 'pipe' else 'ignore'
Expand All @@ -40,7 +45,7 @@ module.exports = class Wrapper
resolve()
proc.stderr.on 'data', onErrData = (data) ->
stderr += data
promise = promise.ensure ->
promise = promise.finally ->
# Cleanup
proc.removeListener 'error', onError
proc.removeListener 'close', onClose
Expand Down
5 changes: 1 addition & 4 deletions test/io.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
rawBody = require 'raw-body'
nodefn = require 'when/node'
streamToBuffer = require 'raw-body'
{ReadableStreamBuffer} = require 'stream-buffers'

{Buffer} = require 'buffer'
{Stream} = require 'stream'

streamToBuffer = nodefn.lift rawBody

run_mocked 'io', (Webp, {read, write}) ->

describe 'output', ->
Expand Down
2 changes: 1 addition & 1 deletion test/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ run_tests 'main', (Webp) ->
{stdout, stderr} = proc
stdout.listeners('data').should.be.empty
stderr.listeners('data').should.be.empty
.ensure ->
.finally ->
child_process.spawn = spawn

0 comments on commit 5d637bd

Please sign in to comment.