From 6cda17965e02bee5db8314156db9b81d85ef71b7 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 17 Apr 2023 06:23:09 -0700 Subject: [PATCH 1/3] Align run identifier validation with Docker naming rules --- index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index aae4639..fddccf8 100644 --- a/index.js +++ b/index.js @@ -80,10 +80,18 @@ class Replicate { * @returns {Promise} - Resolves with the output of running the model */ async run(identifier, options) { - const pattern = - /^(?[a-zA-Z0-9-_]+?)\/(?[a-zA-Z0-9-_]+?):(?[0-9a-fA-F]+)$/; - const match = identifier.match(pattern); + // Define a pattern for owner and model names that allows + // letters, digits, and certain special characters. + // Example: "user123", "abc__123", "user.name" + const namePattern = /[a-zA-Z0-9]+(?:(?:[._]|__|[-]*)[a-zA-Z0-9]+)*/; + + // Define a pattern for "owner/name:version" format with named capturing groups. + // Example: "user123/repo_a:1a2b3c" + const pattern = new RegExp( + `^(?${namePattern.source})/(?${namePattern.source}):(?[0-9a-fA-F]+)$` + ); + const match = identifier.match(pattern); if (!match || !match.groups) { throw new Error( 'Invalid version. It must be in the format "owner/name:version"' From 7716cc7a40d91f4a38bce408f938d7a8106e66e2 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 17 Apr 2023 06:23:39 -0700 Subject: [PATCH 2/3] Add test for calling run with invalid identifiers --- index.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.test.ts b/index.test.ts index 89bcc8d..aeb936d 100644 --- a/index.test.ts +++ b/index.test.ts @@ -391,6 +391,20 @@ describe('Replicate client', () => { ); expect(output).toBe('foobar'); }); + + test('Throws an error for invalid identifiers', async () => { + const options = { input: { text: 'Hello, world!' } } + + await expect(client.run('owner/model:invalid', options)).rejects.toThrow(); + + // @ts-expect-error + await expect(client.run('owner:abc123', options)).rejects.toThrow(); + + await expect(client.run('/model:abc123', options)).rejects.toThrow(); + + // @ts-expect-error + await expect(client.run(':abc123', options)).rejects.toThrow(); + }); }); // Continue with tests for other methods From 93a5be56214d1e7def9e5ad89a42c45fb94405aa Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 17 Apr 2023 06:27:23 -0700 Subject: [PATCH 3/3] Add test for running with identifier containing hyphen and full stop --- index.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/index.test.ts b/index.test.ts index aeb936d..2b239a9 100644 --- a/index.test.ts +++ b/index.test.ts @@ -392,6 +392,23 @@ describe('Replicate client', () => { expect(output).toBe('foobar'); }); + test('Does not throw an error for identifier containing hyphen and full stop', async () => { + nock(BASE_URL) + .post('/predictions') + .reply(200, { + id: 'ufawqhfynnddngldkgtslldrkq', + status: 'processing', + }) + .get('/predictions/ufawqhfynnddngldkgtslldrkq') + .reply(200, { + id: 'ufawqhfynnddngldkgtslldrkq', + status: 'succeeded', + output: 'foobar', + }); + + await expect(client.run('a/b-1.0:abc123', { input: { text: 'Hello, world!' } })).resolves.not.toThrow(); + }); + test('Throws an error for invalid identifiers', async () => { const options = { input: { text: 'Hello, world!' } }