swipl-devel icon indicating copy to clipboard operation
swipl-devel copied to clipboard

[Feature Request]: Publish swipl-web as an npm module

Open jeswr opened this issue 2 years ago • 5 comments

Thanks again on the work to make swipl-web. I would be very grateful if this could be packaged up and released on npm so that we can version bump downstream when updates are made; rather than having to rebuild and publish swipl-web.js every time updates are made in this repo.

If you have time; it would be amazing if you could publish a type declaration file as well to make it easier to use downstream in TypeScript.

cc @rla as you appear to have already done this for the nodejs package.

jeswr avatar Aug 22 '22 05:08 jeswr

In addition the following utilities would be of great benefit in our use case. (Note the following code is untested - as I have converted this from a custom stream representation we use. I'll update this once I've had a chance to test the code properly. Also note that the SwiplOptions and SwiplModule are a very incomplete start on the requested type declarations).


import { Readable } from 'readable-stream'

export interface SwiplOptions {
  print?: (str: string) => void;
  printErr?: (str: string) => void;
}

export interface SwiplModule {
  FS: {
    writeFile(fileName: string, file: string): void;
  }
  prolog: {
    call_string(str: string): void;
  }
}

export async function createSWIPLModule(options: SwiplOptions = {}): Promise<SwiplModule> {
  const Module = {
    noInitialRun: true,
    arguments: [],
    locateFile: (file: string) => `./${file}`,
    print: (str: string) => {},
    printErr: (line: any) => {},
    ...options,
  } as any;
  await SWIPL(Module);
  return Module
}

export async function runCommandsOnFiles(options: SwiplOptions = {}, commands: string[], files?: Record<string, string>) {
  const Module = await createSWIPLModule(options);

  // Load the files
  if (files) {
    for (const key in files) {
      if (typeof key === 'string')
        Module.FS.writeFile(key, files[key]);
    }
  }

  for (const command of commands)
    Module.prolog.call_string(command);
}

export class SWIPLReadable extends Readable {
  constructor(commands: string[], files?: Record<string, string>) {
    super();
    runCommandsOnFiles({ print: str => this.push(str) }, commands, files);
  }

  read(): string {
    return super.read();
  }
}

jeswr avatar Aug 22 '22 06:08 jeswr

I would be very grateful if this could be packaged up and released on npm so that we can version bump downstream when updates are made

That is surely part of the plan. As yet, things are evolving a bit too fast and are too unstable. If you want to help improve on that (in whatever way), please contact me.

JanWielemaker avatar Aug 22 '22 07:08 JanWielemaker

Unfortunately I have very little spare time at the moment - but more than happy to help port the typings and quickstarts we are starting to put together downstream as things start to get more stable and closer to a first release here :)

jeswr avatar Aug 25 '22 00:08 jeswr

@jeswr I have uploaded the package to NPM: https://swi-prolog.discourse.group/t/wiki-discussion-swi-prolog-in-the-browser-using-wasm/5651/103?u=rla

No Typescript yet tho but at least it should take away pains of building the binaries.

rla avatar Aug 29 '22 14:08 rla

Amazing - thanks for the quick turnaround @rla . I'll have a go at using this tomorrow!

jeswr avatar Aug 29 '22 14:08 jeswr